-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate AI SDK integration to external-store runtime (#684)
* refactor: migrate AI SDK integration to external-store runtime * addToolResult support * cleanup * useInsertionEffect -> useEffect * changeset * do not overwrite composer if it has content
- Loading branch information
Showing
22 changed files
with
401 additions
and
785 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,6 @@ | ||
--- | ||
"@assistant-ui/react-ai-sdk": minor | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
refactor: rewrite ai-sdk integration to use external runtime |
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,12 +1,9 @@ | ||
import type { ThreadMessage } from "@assistant-ui/react"; | ||
import { | ||
getExternalStoreMessage, | ||
type ThreadMessage, | ||
} from "@assistant-ui/react"; | ||
import type { Message } from "ai"; | ||
|
||
export const symbolInnerAIMessage = Symbol("innerVercelAIUIMessage"); | ||
|
||
export type VercelAIThreadMessage = ThreadMessage & { | ||
[symbolInnerAIMessage]?: Message[]; | ||
}; | ||
|
||
export const getVercelAIMessage = (message: ThreadMessage) => { | ||
return (message as VercelAIThreadMessage)[symbolInnerAIMessage]; | ||
return getExternalStoreMessage(message) as Message[]; | ||
}; |
54 changes: 0 additions & 54 deletions
54
packages/react-ai-sdk/src/ui/use-assistant/VercelUseAssistantRuntime.tsx
This file was deleted.
Oops, something went wrong.
135 changes: 0 additions & 135 deletions
135
packages/react-ai-sdk/src/ui/use-assistant/VercelUseAssistantThreadRuntime.tsx
This file was deleted.
Oops, something went wrong.
38 changes: 28 additions & 10 deletions
38
packages/react-ai-sdk/src/ui/use-assistant/useVercelUseAssistantRuntime.tsx
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,20 +1,38 @@ | ||
import type { useAssistant } from "ai/react"; | ||
import { useEffect, useInsertionEffect, useState } from "react"; | ||
import { VercelUseAssistantRuntime } from "./VercelUseAssistantRuntime"; | ||
import { useExternalStoreRuntime } from "@assistant-ui/react"; | ||
import { useCachedChunkedMessages } from "../utils/useCachedChunkedMessages"; | ||
import { convertMessage } from "../utils/convertMessage"; | ||
import { useInputSync } from "../utils/useInputSync"; | ||
|
||
export const useVercelUseAssistantRuntime = ( | ||
assistantHelpers: ReturnType<typeof useAssistant>, | ||
) => { | ||
const [runtime] = useState( | ||
() => new VercelUseAssistantRuntime(assistantHelpers), | ||
); | ||
const messages = useCachedChunkedMessages(assistantHelpers.messages); | ||
const runtime = useExternalStoreRuntime({ | ||
isRunning: assistantHelpers.status === "in_progress", | ||
messages, | ||
onCancel: async () => assistantHelpers.stop(), | ||
onNew: async (message) => { | ||
if (message.content.length !== 1 || message.content[0]?.type !== "text") | ||
throw new Error( | ||
"VercelUseAssistantRuntime only supports text content.", | ||
); | ||
|
||
useInsertionEffect(() => { | ||
runtime.vercel = assistantHelpers; | ||
}); | ||
useEffect(() => { | ||
runtime.onVercelUpdated(); | ||
await assistantHelpers.append({ | ||
role: message.role, | ||
content: message.content[0].text, | ||
}); | ||
}, | ||
onNewThread: () => { | ||
assistantHelpers.messages = []; | ||
assistantHelpers.input = ""; | ||
assistantHelpers.setMessages([]); | ||
assistantHelpers.setInput(""); | ||
}, | ||
convertMessage, | ||
}); | ||
|
||
useInputSync(assistantHelpers, runtime); | ||
|
||
return runtime; | ||
}; |
45 changes: 0 additions & 45 deletions
45
packages/react-ai-sdk/src/ui/use-chat/VercelUseChatRuntime.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.