77 "strings"
88 "sync"
99
10+ "github.com/docker/cagent/pkg/concurrent"
1011 "github.com/docker/cagent/pkg/tools"
1112)
1213
@@ -38,15 +39,15 @@ type UpdateTodoArgs struct {
3839}
3940
4041type todoHandler struct {
41- todos map [string ] Todo
42+ todos * concurrent. Map [string , Todo ]
4243}
4344
4445var NewSharedTodoTool = sync .OnceValue (NewTodoTool )
4546
4647func NewTodoTool () * TodoTool {
4748 return & TodoTool {
4849 handler : & todoHandler {
49- todos : make ( map [string ] Todo ),
50+ todos : concurrent . NewMap [string , Todo ]( ),
5051 },
5152 }
5253}
@@ -78,12 +79,12 @@ func (h *todoHandler) createTodo(_ context.Context, toolCall tools.ToolCall) (*t
7879 return nil , fmt .Errorf ("invalid arguments: %w" , err )
7980 }
8081
81- id := fmt .Sprintf ("todo_%d" , len ( h .todos )+ 1 )
82- h .todos [ id ] = Todo {
82+ id := fmt .Sprintf ("todo_%d" , h .todos . Length ( )+ 1 )
83+ h .todos . Store ( id , Todo {
8384 ID : id ,
8485 Description : params .Description ,
8586 Status : "pending" ,
86- }
87+ })
8788
8889 return & tools.ToolCallResult {
8990 Output : fmt .Sprintf ("Created todo [%s]: %s" , id , params .Description ),
@@ -97,14 +98,14 @@ func (h *todoHandler) createTodos(_ context.Context, toolCall tools.ToolCall) (*
9798 }
9899
99100 ids := make ([]string , len (params .Descriptions ))
100- start := len ( h .todos )
101+ start := h .todos . Length ( )
101102 for i , desc := range params .Descriptions {
102103 id := fmt .Sprintf ("todo_%d" , start + i + 1 )
103- h .todos [ id ] = Todo {
104+ h .todos . Store ( id , Todo {
104105 ID : id ,
105106 Description : desc ,
106107 Status : "pending" ,
107- }
108+ })
108109 ids [i ] = id
109110 }
110111
@@ -127,13 +128,13 @@ func (h *todoHandler) updateTodo(_ context.Context, toolCall tools.ToolCall) (*t
127128 return nil , fmt .Errorf ("invalid arguments: %w" , err )
128129 }
129130
130- todo , exists := h .todos [ params .ID ]
131+ todo , exists := h .todos . Load ( params .ID )
131132 if ! exists {
132133 return nil , fmt .Errorf ("todo [%s] not found" , params .ID )
133134 }
134135
135136 todo .Status = params .Status
136- h .todos [ params .ID ] = todo
137+ h .todos . Store ( params .ID , todo )
137138
138139 return & tools.ToolCallResult {
139140 Output : fmt .Sprintf ("Updated todo [%s] to status: [%s]" , params .ID , params .Status ),
@@ -144,10 +145,11 @@ func (h *todoHandler) listTodos(context.Context, tools.ToolCall) (*tools.ToolCal
144145 var output strings.Builder
145146 output .WriteString ("Current todos:\n " )
146147
147- for _ , todo := range h . todos {
148+ h . todos . Range ( func ( _ string , todo Todo ) bool {
148149 output .WriteString (fmt .Sprintf ("- [%s] %s (Status: %s)\n " ,
149150 todo .ID , todo .Description , todo .Status ))
150- }
151+ return true
152+ })
151153
152154 return & tools.ToolCallResult {
153155 Output : output .String (),
0 commit comments