Skip to content

Commit

Permalink
refactor(vercel/rsc)!: align RSC adapter function names with useExter…
Browse files Browse the repository at this point in the history
…nalStoreRuntime (#542)
  • Loading branch information
Yonom authored Jul 20, 2024
1 parent d6aeb23 commit 7e9e774
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-feet-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react-ai-sdk": patch
---

refactor: align RSC adapter function names with useExternalStore
4 changes: 2 additions & 2 deletions apps/docs/content/docs/docs/runtimes/vercel-ai-sdk/rsc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function MyRuntimeProvider({
const { continueConversation } = useActions();
const [messages, setMessages] = useUIState<typeof AI>();

const append = async (m: AppendMessage) => {
const onNew = async (m: AppendMessage) => {
if (m.content[0]?.type !== "text")
throw new Error("Only text messages are supported");

Expand All @@ -159,7 +159,7 @@ export function MyRuntimeProvider({
setMessages((currentConversation) => [...currentConversation, message]);
};

const runtime = useVercelRSCRuntime({ messages, append });
const runtime = useVercelRSCRuntime({ messages, onNew });

return (
<AssistantRuntimeProvider runtime={runtime}>
Expand Down
10 changes: 5 additions & 5 deletions apps/docs/content/docs/reference/integrations/vercel-ai-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import { useVercelRSCRuntime } from "@assistant-ui/react-ai-sdk";
const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
const [messages, setMessages] = useUIState<typeof AI>();

const append = async (m: AppendMessage) => {
const onNew = async (m: AppendMessage) => {
if (m.content[0]?.type !== "text")
throw new Error("Only text messages are supported");

Expand All @@ -91,7 +91,7 @@ const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
setMessages((currentConversation) => [...currentConversation, message]);
};

const runtime = useVercelRSCRuntime({ messages, append });
const runtime = useVercelRSCRuntime({ messages, onNew });

return (
<AssistantRuntimeProvider runtime={runtime}>
Expand All @@ -117,17 +117,17 @@ const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
description: "The messages in the thread.",
},
{
name: "append",
name: "onNew",
type: "(message: AppendMessage) => Promise<void>",
description: "A function to append a message to the thread.",
},
{
name: "edit",
name: "onEdit",
type: "(message: AppendMessage) => Promise<void>",
description: "A function to edit a message.",
},
{
name: "reload",
name: "onReload",
type: "(parentId: string | null) => Promise<void>",
description: "A function to reload a message.",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function MyRuntimeProvider({
const { continueConversation } = useActions();
const [messages, setMessages] = useUIState<typeof AI>();

const append = async (m: AppendMessage) => {
const onNew = async (m: AppendMessage) => {
if (m.content[0]?.type !== "text")
throw new Error("Only text messages are supported");

Expand All @@ -33,7 +33,7 @@ export function MyRuntimeProvider({
setMessages((currentConversation) => [...currentConversation, message]);
};

const runtime = useVercelRSCRuntime({ messages, append });
const runtime = useVercelRSCRuntime({ messages, onNew });

return (
<AssistantRuntimeProvider runtime={runtime}>
Expand Down
4 changes: 3 additions & 1 deletion examples/search-agent-for-e-commerce/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
}
],
"paths": {
"@/*": ["./src/*"]
"@/*": ["./src/*"],
"@assistant-ui/*": ["../../packages/*/src"],
"@assistant-ui/react/*": ["../../packages/react/src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
Expand Down
4 changes: 2 additions & 2 deletions examples/with-vercel-ai-rsc/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
const { continueConversation } = useActions();
const [messages, setMessages] = useUIState<typeof AI>();

const append = async (m: AppendMessage) => {
const onNew = async (m: AppendMessage) => {
if (m.content[0]?.type !== "text")
throw new Error("Only text messages are supported");

Expand All @@ -40,7 +40,7 @@ const MyRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
setMessages((currentConversation) => [...currentConversation, message]);
};

const runtime = useVercelRSCRuntime({ messages, append });
const runtime = useVercelRSCRuntime({ messages, onNew });

return (
<AssistantRuntimeProvider runtime={runtime}>
Expand Down
17 changes: 15 additions & 2 deletions packages/react-ai-sdk/src/rsc/VercelRSCAdapter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@ type RSCMessageConverter<T> = {

type VercelRSCAdapterBase<T> = {
messages: T[];
append: (message: AppendMessage) => Promise<void>;
onNew?: (message: AppendMessage) => Promise<void>;
onEdit?: ((message: AppendMessage) => Promise<void>) | undefined;
onReload?: ((parentId: string | null) => Promise<void>) | undefined;
convertMessage?: ((message: T) => VercelRSCMessage) | undefined;

/**
* @deprecated Use `onNew` instead. This will be removed in 0.6.0.
*/
append?: (message: AppendMessage) => Promise<void>;
/**
* @deprecated Use `onEdit` instead. This will be removed in 0.6.0.
*/
edit?: ((message: AppendMessage) => Promise<void>) | undefined;
/**
* @deprecated Use `onReload` instead. This will be removed in 0.6.0.
*/
reload?: ((parentId: string | null) => Promise<void>) | undefined;
convertMessage?: ((message: T) => VercelRSCMessage) | undefined;
};

export type VercelRSCAdapter<T = VercelRSCMessage> = VercelRSCAdapterBase<T> &
Expand Down
9 changes: 6 additions & 3 deletions packages/react-ai-sdk/src/rsc/useVercelRSCRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ const vercelToThreadMessage = <T,>(
export const useVercelRSCRuntime = <T extends WeakKey>(
adapter: VercelRSCAdapter<T>,
) => {
const onNew = adapter.onNew ?? adapter.append;
if (!onNew)
throw new Error("You must pass a onNew function to useVercelRSCRuntime");
const eAdapter: ExternalStoreAdapter<any> = {
messages: adapter.messages,
onNew: adapter.append,
onEdit: adapter.edit,
onReload: adapter.reload,
onNew,
onEdit: adapter.onEdit ?? adapter.edit,
onReload: adapter.onReload ?? adapter.reload,
convertMessage: (m: T) =>
vercelToThreadMessage(
adapter.convertMessage ?? ((m) => m as VercelRSCMessage),
Expand Down

0 comments on commit 7e9e774

Please sign in to comment.