-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Unified Runtime & Context API (Part 1) (#873)
* feat: Unified Runtime & Context API * add export/import/reset functions * cleanup exports
- Loading branch information
Showing
72 changed files
with
1,271 additions
and
506 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@assistant-ui/react-playground": patch | ||
"@assistant-ui/react-hook-form": patch | ||
"@assistant-ui/react-langgraph": patch | ||
"@assistant-ui/react-ai-sdk": patch | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: New unified Runtime API (part 1/n) |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,33 +1,31 @@ | ||
import { useRef, useEffect } from "react"; | ||
import { | ||
ExternalStoreRuntime, | ||
subscribeToMainThread, | ||
} from "@assistant-ui/react"; | ||
import { useEffect } from "react"; | ||
import { useAssistant, useChat } from "ai/react"; | ||
import { AssistantRuntime } from "@assistant-ui/react"; | ||
import { useCallbackRef } from "@radix-ui/react-use-callback-ref"; | ||
|
||
type VercelHelpers = | ||
| ReturnType<typeof useChat> | ||
| ReturnType<typeof useAssistant>; | ||
|
||
export const useInputSync = ( | ||
helpers: VercelHelpers, | ||
runtime: ExternalStoreRuntime, | ||
runtime: AssistantRuntime, | ||
) => { | ||
// sync input from vercel to assistant-ui | ||
const helpersRef = useRef(helpers); | ||
useEffect(() => { | ||
helpersRef.current = helpers; | ||
if (runtime.thread.composer.text !== helpers.input) { | ||
if (runtime.thread.composer.getState().text !== helpers.input) { | ||
runtime.thread.composer.setText(helpers.input); | ||
} | ||
}, [helpers, runtime]); | ||
|
||
// sync input from assistant-ui to vercel | ||
const handleThreadUpdate = useCallbackRef(() => { | ||
if (runtime.thread.composer.getState().text !== helpers.input) { | ||
helpers.setInput(runtime.thread.composer.getState().text); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
return subscribeToMainThread(runtime, () => { | ||
if (runtime.thread.composer.text !== helpersRef.current.input) { | ||
helpersRef.current.setInput(runtime.thread.composer.text); | ||
} | ||
}); | ||
}, [runtime]); | ||
return runtime.thread.subscribe(handleThreadUpdate); | ||
}, [runtime, handleThreadUpdate]); | ||
}; |
This file contains 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 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 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 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.