Skip to content

Commit e9bc5d6

Browse files
committed
readd assistant ui demo
1 parent 305e0ab commit e9bc5d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5265
-113
lines changed

agent-demo/.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts

agent-demo/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
This is the [assistant-ui](https://github.com/Yonom/assistant-ui) starter project.
2+
3+
## Getting Started
4+
5+
First, add your OpenAI API key to `.env.local` file:
6+
7+
```
8+
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9+
```
10+
11+
Then, run the development server:
12+
13+
```bash
14+
npm run dev
15+
# or
16+
yarn dev
17+
# or
18+
pnpm dev
19+
# or
20+
bun dev
21+
```
22+
23+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
24+
25+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

agent-demo/app/api/chat/route.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { openai } from "@ai-sdk/openai";
2+
import { frontendTools } from "@assistant-ui/react-ai-sdk";
3+
import {
4+
aiDocumentFormats,
5+
injectDocumentStateMessages,
6+
} from "@blocknote/xl-ai/server";
7+
import { convertToModelMessages, streamText, UIMessage } from "ai";
8+
import { JSONSchema7 } from "json-schema";
9+
10+
export async function POST(req: Request) {
11+
const {
12+
messages,
13+
tools,
14+
}: {
15+
messages: UIMessage[];
16+
tools: Record<string, { description?: string; parameters: JSONSchema7 }>;
17+
} = await req.json();
18+
const result = streamText({
19+
system: aiDocumentFormats.html.systemPrompt,
20+
model: openai("gpt-4o-2024-08-06"), // openai("gpt-5-nano"),
21+
messages: convertToModelMessages(injectDocumentStateMessages(messages)),
22+
tools: {
23+
...(frontendTools(tools) as any), // TODO: tools vs toolDefinitions
24+
web_search: openai.tools.webSearch({}),
25+
},
26+
// providerOptions: {
27+
// openai: {
28+
// reasoningEffort: "low",
29+
// },
30+
// },
31+
// toolChoice: "required", // TODO: make configurable from client and make toolbar "required"
32+
});
33+
34+
return result.toUIMessageStreamResponse();
35+
}
36+
37+
// - the "getDocument" tool shows the document as an array of html blocks (the cursor is BETWEEN two blocks as indicated by cursor: true).
38+
// - the "getDocumentSelection" tool shows the current user selection, if any.
39+
// - when the document is empty, prefer updating the empty block before adding new blocks. Otherwise, prefer updating existing blocks over removing and adding (but this also depends on the user's question).
40+
// Don't call "getDocument" or "getDocumentSelection" tools directly, the information is already available.
41+
// When there is a selection, issue operations against the result of "getDocumentSelection". You can still use the result of "getDocument" (which includes the selection as well) to understand the context.

agent-demo/app/assistant.tsx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"use client";
2+
3+
import { AssistantModal } from "@/components/assistant-ui/assistant-modal";
4+
import { ThreadListSidebar } from "@/components/assistant-ui/threadlist-sidebar";
5+
import {
6+
Breadcrumb,
7+
BreadcrumbItem,
8+
BreadcrumbList,
9+
} from "@/components/ui/breadcrumb";
10+
import { Separator } from "@/components/ui/separator";
11+
import {
12+
SidebarInset,
13+
SidebarProvider,
14+
SidebarTrigger,
15+
} from "@/components/ui/sidebar";
16+
import { AssistantRuntimeProvider } from "@assistant-ui/react";
17+
import { useAISDKRuntime } from "@assistant-ui/react-ai-sdk";
18+
19+
import { useChatContext } from "@/components/ChatContext";
20+
import { useChat } from "@ai-sdk/react";
21+
import dynamic from "next/dynamic";
22+
import { useEffect } from "react";
23+
24+
const Document = dynamic(() => import("./document"), {
25+
ssr: false,
26+
});
27+
28+
console.log("Assistant", Document);
29+
export const Assistant = () => {
30+
const ctx = useChatContext();
31+
const chat = useChat({
32+
chat: ctx.chat,
33+
});
34+
35+
const runtime = useAISDKRuntime(chat);
36+
37+
useEffect(() => {
38+
// not documented!
39+
(ctx.transport as any).setRuntime(runtime);
40+
}, [runtime, ctx.transport]);
41+
42+
return (
43+
<AssistantRuntimeProvider runtime={runtime}>
44+
<SidebarProvider>
45+
<div className="flex h-dvh w-full pr-0.5">
46+
<ThreadListSidebar />
47+
<SidebarInset>
48+
<header className="flex h-16 shrink-0 items-center gap-2 border-b px-4">
49+
<SidebarTrigger />
50+
<Separator orientation="vertical" className="mr-2 h-4" />
51+
<Breadcrumb>
52+
<BreadcrumbList>
53+
<BreadcrumbItem className="hidden md:block">
54+
{/* <BreadcrumbLink
55+
href="https://www.assistant-ui.com/docs/getting-started"
56+
target="_blank"
57+
rel="noopener noreferrer"
58+
> */}
59+
Agent demo
60+
{/* </BreadcrumbLink> */}
61+
</BreadcrumbItem>
62+
{/* <BreadcrumbSeparator className="hidden md:block" />
63+
<BreadcrumbItem>
64+
<BreadcrumbPage>Starter Template</BreadcrumbPage>
65+
</BreadcrumbItem> */}
66+
</BreadcrumbList>
67+
</Breadcrumb>
68+
</header>
69+
<div className="flex-1 overflow-scroll">
70+
{/* <Thread /> */}
71+
<div className="mx-auto max-w-2xl">
72+
<Document />
73+
</div>
74+
</div>
75+
</SidebarInset>
76+
</div>
77+
</SidebarProvider>
78+
<AssistantModal />
79+
</AssistantRuntimeProvider>
80+
);
81+
};

0 commit comments

Comments
 (0)