@@ -48,6 +48,15 @@ type todoHandler struct {
4848 todos * concurrent.Map [string , Todo ]
4949}
5050
51+ func (h * todoHandler ) allTodos () []Todo {
52+ var todos []Todo
53+ h .todos .Range (func (_ string , todo Todo ) bool {
54+ todos = append (todos , todo )
55+ return true
56+ })
57+ return todos
58+ }
59+
5160var NewSharedTodoTool = sync .OnceValue (NewTodoTool )
5261
5362func NewTodoTool () * TodoTool {
@@ -81,17 +90,20 @@ This toolset is REQUIRED for maintaining task state and ensuring all steps are c
8190
8291func (h * todoHandler ) createTodo (_ context.Context , params CreateTodoArgs ) (* tools.ToolCallResult , error ) {
8392 id := fmt .Sprintf ("todo_%d" , h .todos .Length ()+ 1 )
84- h . todos . Store ( id , Todo {
93+ todo := Todo {
8594 ID : id ,
8695 Description : params .Description ,
8796 Status : "pending" ,
88- })
97+ }
98+ h .todos .Store (id , todo )
8999
90- return tools .ResultSuccess (fmt .Sprintf ("Created todo [%s]: %s" , id , params .Description )), nil
100+ return & tools.ToolCallResult {
101+ Output : fmt .Sprintf ("Created todo [%s]: %s" , id , params .Description ),
102+ Meta : h .allTodos (),
103+ }, nil
91104}
92105
93106func (h * todoHandler ) createTodos (_ context.Context , params CreateTodosArgs ) (* tools.ToolCallResult , error ) {
94- ids := make ([]string , len (params .Descriptions ))
95107 start := h .todos .Length ()
96108 for i , desc := range params .Descriptions {
97109 id := fmt .Sprintf ("todo_%d" , start + i + 1 )
@@ -100,43 +112,51 @@ func (h *todoHandler) createTodos(_ context.Context, params CreateTodosArgs) (*t
100112 Description : desc ,
101113 Status : "pending" ,
102114 })
103- ids [i ] = id
104115 }
105116
106- output := fmt .Sprintf ("Created %d todos: " , len (params .Descriptions ))
107- for i , id := range ids {
117+ var output strings.Builder
118+ fmt .Fprintf (& output , "Created %d todos: " , len (params .Descriptions ))
119+ for i := range params .Descriptions {
108120 if i > 0 {
109- output += ", "
121+ output . WriteString ( ", " )
110122 }
111- output += fmt .Sprintf ( "[%s ]" , id )
123+ fmt .Fprintf ( & output , "[todo_%d ]" , start + i + 1 )
112124 }
113125
114- return tools .ResultSuccess (output ), nil
126+ return & tools.ToolCallResult {
127+ Output : output .String (),
128+ Meta : h .allTodos (),
129+ }, nil
115130}
116131
117132func (h * todoHandler ) updateTodo (_ context.Context , params UpdateTodoArgs ) (* tools.ToolCallResult , error ) {
118133 todo , exists := h .todos .Load (params .ID )
119134 if ! exists {
120- return nil , fmt .Errorf ("todo [%s] not found" , params .ID )
135+ return tools . ResultError ( fmt .Sprintf ("todo [%s] not found" , params .ID )), nil
121136 }
122137
123138 todo .Status = params .Status
124139 h .todos .Store (params .ID , todo )
125140
126- return tools .ResultSuccess (fmt .Sprintf ("Updated todo [%s] to status: [%s]" , params .ID , params .Status )), nil
141+ return & tools.ToolCallResult {
142+ Output : fmt .Sprintf ("Updated todo [%s] to status: [%s]" , params .ID , params .Status ),
143+ Meta : h .allTodos (),
144+ }, nil
127145}
128146
129- func (h * todoHandler ) listTodos (_ context.Context , _ map [ string ] any ) (* tools.ToolCallResult , error ) {
147+ func (h * todoHandler ) listTodos (_ context.Context , _ tools. ToolCall ) (* tools.ToolCallResult , error ) {
130148 var output strings.Builder
131149 output .WriteString ("Current todos:\n " )
132150
133- h .todos .Range (func (_ string , todo Todo ) bool {
134- output .WriteString (fmt .Sprintf ("- [%s] %s (Status: %s)\n " ,
135- todo .ID , todo .Description , todo .Status ))
136- return true
137- })
151+ todos := h .allTodos ()
152+ for _ , todo := range todos {
153+ fmt .Fprintf (& output , "- [%s] %s (Status: %s)\n " , todo .ID , todo .Description , todo .Status )
154+ }
138155
139- return tools .ResultSuccess (output .String ()), nil
156+ return & tools.ToolCallResult {
157+ Output : output .String (),
158+ Meta : todos ,
159+ }, nil
140160}
141161
142162func (t * TodoTool ) Tools (context.Context ) ([]tools.Tool , error ) {
@@ -182,7 +202,7 @@ func (t *TodoTool) Tools(context.Context) ([]tools.Tool, error) {
182202 Category : "todo" ,
183203 Description : "List all current todos with their status" ,
184204 OutputSchema : tools .MustSchemaFor [string ](),
185- Handler : NewHandler ( t .handler .listTodos ) ,
205+ Handler : t .handler .listTodos ,
186206 Annotations : tools.ToolAnnotations {
187207 Title : "List TODOs" ,
188208 ReadOnlyHint : true ,
0 commit comments