Skip to content

Commit 5ae1962

Browse files
committed
have streaming example use tool
1 parent 7badb52 commit 5ae1962

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

example/convex/agents/story.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
// See the docs at https://docs.convex.dev/agents/getting-started
2-
import { Agent } from "@convex-dev/agent";
2+
import { Agent, createTool, stepCountIs } from "@convex-dev/agent";
33
import { components } from "../_generated/api";
44
import { defaultConfig } from "./config";
5+
import { z } from "zod/v3";
56

67
// Define an agent similarly to the AI SDK
78
export const storyAgent = new Agent(components.agent, {
89
name: "Story Agent",
910
instructions: "You tell stories with twist endings. ~ 200 words.",
1011
...defaultConfig,
12+
stopWhen: stepCountIs(3),
13+
tools: {
14+
getCharacterNames: createTool({
15+
description:
16+
"Get the names of characters for the story. Only call this once.",
17+
args: z.object({
18+
count: z.number().describe("The number of character names to get"),
19+
}),
20+
handler: async (ctx, args) => {
21+
return [
22+
"Eleanor",
23+
"Henry",
24+
"Clara",
25+
"Samuel",
26+
"Margaret",
27+
"Jordan",
28+
"Maya",
29+
"Lucas",
30+
"Riley",
31+
"Aiden",
32+
"Elira",
33+
"Kaelen",
34+
"Seraphine",
35+
"Thorne",
36+
"Lyra",
37+
"Dorian",
38+
"Isolde",
39+
"Malachai",
40+
"Selene",
41+
"Victor",
42+
].slice(0, args.count);
43+
},
44+
}),
45+
},
1146
});

example/ui/chat/ChatStreaming.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { useEffect, useRef, useState } from "react";
1111
import { cn } from "@/lib/utils";
1212
import { useDemoThread } from "@/hooks/use-demo-thread";
13+
import { ToolUIPart } from "ai";
1314

1415
export default function ChatStreaming() {
1516
const { threadId, resetThread } = useDemoThread("Streaming Chat Example");
@@ -192,6 +193,9 @@ function Message({ message }: { message: UIMessage }) {
192193
startStreaming: message.status === "streaming",
193194
},
194195
);
196+
const nameToolCalls = message.parts.filter(
197+
(p): p is ToolUIPart => p.type === "tool-getCharacterNames",
198+
);
195199
return (
196200
<div className={cn("flex", isUser ? "justify-end" : "justify-start")}>
197201
<div
@@ -207,6 +211,13 @@ function Message({ message }: { message: UIMessage }) {
207211
{reasoningText && (
208212
<div className="text-xs text-gray-500">💭{reasoningText}</div>
209213
)}
214+
{nameToolCalls.map((p) => (
215+
<div key={p.toolCallId} className="text-xs text-gray-500">
216+
Names generated:{" "}
217+
{p.output ? (p.output as string[]).join(", ") : p.state}
218+
<br />
219+
</div>
220+
))}
210221
{visibleText || "..."}
211222
</div>
212223
</div>

0 commit comments

Comments
 (0)