-
-
Notifications
You must be signed in to change notification settings - Fork 575
feat: make it possible to abort AI requests #1806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YousefED
wants to merge
4
commits into
main
Choose a base branch
from
feature/ai-abort
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
suggestChanges, | ||
} from "@blocknote/prosemirror-suggest-changes"; | ||
import { APICallError, LanguageModel, RetryError } from "ai"; | ||
import { Fragment, Slice } from "prosemirror-model"; | ||
import { Plugin, PluginKey } from "prosemirror-state"; | ||
import { fixTablesKey } from "prosemirror-tables"; | ||
import { createStore, StoreApi } from "zustand/vanilla"; | ||
|
@@ -17,7 +18,6 @@ import { LLMResponse } from "./api/LLMResponse.js"; | |
import { PromptBuilder } from "./api/formats/PromptBuilder.js"; | ||
import { LLMFormat, llmFormats } from "./api/index.js"; | ||
import { createAgentCursorPlugin } from "./plugins/AgentCursorPlugin.js"; | ||
import { Fragment, Slice } from "prosemirror-model"; | ||
|
||
type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
|
||
|
@@ -87,6 +87,7 @@ const PLUGIN_KEY = new PluginKey(`blocknote-ai-plugin`); | |
|
||
export class AIExtension extends BlockNoteExtension { | ||
private previousRequestOptions: LLMRequestOptions | undefined; | ||
private currentAbortController?: AbortController; | ||
|
||
public static key(): string { | ||
return "ai"; | ||
|
@@ -297,6 +298,24 @@ export class AIExtension extends BlockNoteExtension { | |
} | ||
} | ||
|
||
/** | ||
* Abort the current LLM request. | ||
* | ||
* Only valid when executing an LLM request (status is "thinking" or "ai-writing") | ||
*/ | ||
public async abort() { | ||
const state = this.store.getState().aiMenuState; | ||
if (state === "closed") { | ||
throw new Error("abort() is only valid during LLM execution"); | ||
} | ||
if (state.status !== "thinking" && state.status !== "ai-writing") { | ||
throw new Error("abort() is only valid during LLM execution"); | ||
} | ||
|
||
// Abort the current request | ||
this.currentAbortController?.abort(); | ||
} | ||
|
||
/** | ||
* Update the status of a call to an LLM | ||
* | ||
|
@@ -349,15 +368,20 @@ export class AIExtension extends BlockNoteExtension { | |
* Execute a call to an LLM and apply the result to the editor | ||
*/ | ||
public async callLLM(opts: MakeOptional<LLMRequestOptions, "model">) { | ||
const startState = this.store.getState().aiMenuState; | ||
this.setAIResponseStatus("thinking"); | ||
this.editor.forkYDocPlugin?.fork(); | ||
|
||
// Create abort controller for this request | ||
this.currentAbortController = new AbortController(); | ||
|
||
let ret: LLMResponse | undefined; | ||
try { | ||
const requestOptions = { | ||
...this.options.getState(), | ||
...opts, | ||
previousResponse: this.store.getState().llmResponse, | ||
abortSignal: this.currentAbortController.signal, | ||
}; | ||
this.previousRequestOptions = requestOptions; | ||
|
||
|
@@ -383,18 +407,42 @@ export class AIExtension extends BlockNoteExtension { | |
llmResponse: ret, | ||
}); | ||
|
||
await ret.execute(); | ||
await ret.execute(this.currentAbortController?.signal); | ||
|
||
this.setAIResponseStatus("user-reviewing"); | ||
} catch (e) { | ||
// TODO in error state, should we discard the forked document? | ||
// Handle abort errors gracefully | ||
if (e instanceof Error && e.name === "AbortError") { | ||
// Request was aborted, don't set error status as abort() handles cleanup | ||
const state = this.store.getState().aiMenuState; | ||
if (state === "closed" || startState === "closed") { | ||
throw new Error( | ||
"Unexpected: AbortError occurred while the AI menu was closed", | ||
); | ||
} | ||
if (state.status === "ai-writing") { | ||
// we were already writing. Set to reviewing to show the user the partial result | ||
this.setAIResponseStatus("user-reviewing"); | ||
} else { | ||
// we were not writing yet. Set to the previous state | ||
if (startState.status === "error") { | ||
this.setAIResponseStatus({ status: startState.status, error: e }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to show the abort error again? |
||
} else { | ||
this.setAIResponseStatus(startState.status); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
this.setAIResponseStatus({ | ||
status: "error", | ||
error: e, | ||
}); | ||
// eslint-disable-next-line no-console | ||
console.warn("Error calling LLM", e); | ||
} finally { | ||
// Clean up abort controller | ||
this.currentAbortController = undefined; | ||
} | ||
return ret; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,9 +94,12 @@ export const AIMenu = (props: AIMenuProps) => { | |
const rightSection = useMemo(() => { | ||
if (aiResponseStatus === "thinking" || aiResponseStatus === "ai-writing") { | ||
return ( | ||
<Components.SuggestionMenu.Loader | ||
className={"bn-suggestion-menu-loader bn-combobox-right-section"} | ||
/> | ||
// TODO | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still todo? |
||
<div onClick={() => ai.abort()}> | ||
<Components.SuggestionMenu.Loader | ||
className={"bn-suggestion-menu-loader bn-combobox-right-section"} | ||
/> | ||
</div> | ||
); | ||
} else if (aiResponseStatus === "error") { | ||
return ( | ||
|
@@ -117,7 +120,7 @@ export const AIMenu = (props: AIMenuProps) => { | |
} | ||
|
||
return undefined; | ||
}, [Components, aiResponseStatus]); | ||
}, [Components, aiResponseStatus, ai]); | ||
|
||
return ( | ||
<PromptSuggestionMenu | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be an early return, user-facing functionality should not throw errors when it is trivial to know.
This would be similar to having to check if you can abort something before you abort it. It should just no-op