File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 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" ;
33import { components } from "../_generated/api" ;
44import { defaultConfig } from "./config" ;
5+ import { z } from "zod/v3" ;
56
67// Define an agent similarly to the AI SDK
78export 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} ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import {
1010import { useEffect , useRef , useState } from "react" ;
1111import { cn } from "@/lib/utils" ;
1212import { useDemoThread } from "@/hooks/use-demo-thread" ;
13+ import { ToolUIPart } from "ai" ;
1314
1415export 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 >
You can’t perform that action at this time.
0 commit comments