Skip to content

Commit

Permalink
docs: improve external store runtime documentation (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Aug 18, 2024
1 parent ce4ed84 commit 7d9c1fb
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions apps/docs/content/docs/runtimes/external-store.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ Use the `ExternalStoreRuntime` if you want to manage the message state yourself
This runtime requires a `ExternalStoreAdapter<TMessage>` handles communication between `assistant-ui`and your state.
Unless you are storing messages as `ThreadMessage`, you need to define a `convertMessage` function to convert your messages to `ThreadMessage`.

```tsx
import { useExternalStoreRuntime, ThreadMessageLike } from "@assistant-ui/react";
```tsx twoslash title="@/app/MyRuntimeProvider.tsx"
type MyMessage = {
role: "user" | "assistant";
content: string;
};
const backendApi = async (input: string): Promise<MyMessage> => {
return { role: "assistant", content: "Hello, world!" };
};

// ---cut---
import { useState, ReactNode } from "react";
import {
useExternalStoreRuntime,
ThreadMessageLike,
AppendMessage,
AssistantRuntimeProvider,
} from "@assistant-ui/react";

const convertMessage = (message: MyMessage): ThreadMessageLike => {
return {
Expand All @@ -22,24 +37,27 @@ const convertMessage = (message: MyMessage): ThreadMessageLike => {
export function MyRuntimeProvider({
children,
}: Readonly<{
children: React.ReactNode;
children: ReactNode;
}>) {
const [isRunning, setIsRunning] = useState(false);
const [messages, setMessages] = useState<MyMessage[]>([]);

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

const input = m.content[0].text;
const input = message.content[0].text;
setMessages((currentConversation) => [
...currentConversation,
{ id: nanoid(), role: "user", display: input },
{ role: "user", content: input },
]);

setIsRunning(true);
const message = await backendApi(input);
setMessages((currentConversation) => [...currentConversation, message]);
const assistantMessage = await backendApi(input);
setMessages((currentConversation) => [
...currentConversation,
assistantMessage,
]);
setIsRunning(false);
};

Expand Down

0 comments on commit 7d9c1fb

Please sign in to comment.