mirror of
https://github.com/open-webui/open-webui
synced 2025-03-04 03:18:03 +00:00
refac: autocompletion
This commit is contained in:
parent
9e436fe6b0
commit
c192475528
@ -112,39 +112,45 @@ export const AIAutocompletion = Extension.create({
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up debounce for AI generation
|
|
||||||
if (this.options.debounceTime !== null) {
|
|
||||||
clearTimeout(debounceTimer)
|
|
||||||
|
|
||||||
// Capture current position
|
|
||||||
const currentPos = $head.before()
|
|
||||||
|
|
||||||
debounceTimer = setTimeout(() => {
|
// Start debounce logic for AI generation only if the cursor is at the end of the paragraph
|
||||||
const newState = view.state
|
if (selection.empty && $head.pos === $head.end()) {
|
||||||
const newNode = newState.doc.nodeAt(currentPos)
|
|
||||||
|
// Set up debounce for AI generation
|
||||||
|
if (this.options.debounceTime !== null) {
|
||||||
|
clearTimeout(debounceTimer)
|
||||||
|
|
||||||
// Check if the node still exists and is still a paragraph
|
// Capture current position
|
||||||
if (newNode && newNode.type.name === 'paragraph') {
|
const currentPos = $head.before()
|
||||||
const prompt = newNode.textContent
|
|
||||||
|
|
||||||
if (prompt.trim() !== ''){
|
debounceTimer = setTimeout(() => {
|
||||||
if (loading) return true
|
const newState = view.state
|
||||||
loading = true
|
const newNode = newState.doc.nodeAt(currentPos)
|
||||||
this.options.generateCompletion(prompt).then(suggestion => {
|
|
||||||
if (suggestion && suggestion.trim() !== '') {
|
const currentIsAtEnd = newState.selection.$head.pos === newState.selection.$head.end()
|
||||||
view.dispatch(newState.tr.setNodeMarkup(currentPos, null, {
|
// Check if the node still exists and is still a paragraph
|
||||||
...newNode.attrs,
|
if (newNode && newNode.type.name === 'paragraph' && currentIsAtEnd) {
|
||||||
class: 'ai-autocompletion',
|
const prompt = newNode.textContent
|
||||||
'data-prompt': prompt,
|
|
||||||
'data-suggestion': suggestion,
|
if (prompt.trim() !== ''){
|
||||||
}))
|
if (loading) return true
|
||||||
}
|
loading = true
|
||||||
}).finally(() => {
|
this.options.generateCompletion(prompt).then(suggestion => {
|
||||||
loading = false
|
if (suggestion && suggestion.trim() !== '') {
|
||||||
})
|
view.dispatch(newState.tr.setNodeMarkup(currentPos, null, {
|
||||||
|
...newNode.attrs,
|
||||||
|
class: 'ai-autocompletion',
|
||||||
|
'data-prompt': prompt,
|
||||||
|
'data-suggestion': suggestion,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}).finally(() => {
|
||||||
|
loading = false
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}, this.options.debounceTime)
|
||||||
}, this.options.debounceTime)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -185,6 +191,12 @@ export const AIAutocompletion = Extension.create({
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
mousedown: () => {
|
||||||
|
// Reset debounce timer on mouse click
|
||||||
|
clearTimeout(debounceTimer)
|
||||||
|
return false
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user