@@ -532,24 +532,24 @@ export class YourAgent extends Agent {
532532 )
533533 ` ;
534534 }
535-
535+
536536 async createUser(id : string , name : string , email : string ) {
537537 // Insert a new user
538538 this .sql `
539539 INSERT INTO users (id, name, email, created_at)
540540 VALUES (${id }, ${name }, ${email }, ${Date .now ()})
541541 ` ;
542542 }
543-
543+
544544 async getUserById(id : string ): Promise <User | null > {
545545 // Query a user by ID
546546 const users = this .sql <User >`
547547 SELECT * FROM users WHERE id = ${id }
548548 ` ;
549-
549+
550550 return users .length ? users [0 ] : null ;
551551 }
552-
552+
553553 async searchUsers(term : string ): Promise <User []> {
554554 // Search users with a wildcard
555555 return this .sql <User >`
@@ -737,73 +737,6 @@ function useAgent<State = unknown>(
737737};
738738```
739739
740- <TypeScriptExample >
741-
742- ``` tsx
743- // Example of using useAgent in a React component
744- import { useAgent } from " agents/react" ;
745- import { useState } from " react" ;
746-
747- interface TaskState {
748- tasks: Array <{ id: string ; text: string ; completed: boolean }>;
749- filter: " all" | " active" | " completed" ;
750- }
751-
752- function TaskManager() {
753- const [newTask, setNewTask] = useState (" " );
754-
755- // Connect to a task-manager Agent instance
756- const agent = useAgent <TaskState >({
757- agent: " task-manager" ,
758- name: " user-123-tasks" ,
759- onStateUpdate : (state , source ) => {
760- console .log (` State updated from ${source } ` , state );
761- },
762- onOpen : () => console .log (" Connected to task manager agent" ),
763- onClose : () => console .log (" Disconnected from task manager agent" )
764- });
765-
766- function addTask(e : React .FormEvent ) {
767- e .preventDefault ();
768- if (! newTask .trim ()) return ;
769-
770- // Update the Agent's state
771- agent .setState ({
772- ... agent .state ,
773- tasks: [
774- ... (agent .state ?.tasks || []),
775- { id: Date .now ().toString (), text: newTask , completed: false }
776- ]
777- });
778-
779- setNewTask (" " );
780- }
781-
782- return (
783- <div >
784- <h1 >Task Manager</h1 >
785- <form onSubmit = { addTask } >
786- <input
787- value = { newTask }
788- onChange = { e => setNewTask (e .target .value )}
789- placeholder = " Add a new task"
790- />
791- <button type = " submit" >Add</button >
792- </form >
793- <ul >
794- { agent .state ?.tasks ?.map (task => (
795- <li key = { task .id } >{ task .text } </li >
796- ))}
797- </ul >
798- </div >
799- );
800- }
801-
802- export default TaskManager ;
803- ```
804-
805- </TypeScriptExample >
806-
807740### Chat Agent
808741
809742The Agents SDK exposes an ` AIChatAgent ` class that extends the ` Agent ` class and exposes an ` onChatMessage ` method that simplifies building interactive chat agents.
0 commit comments