@@ -13,52 +13,15 @@ import (
13
13
"github.com/coder/coder/v2/codersdk/agentsdk"
14
14
)
15
15
16
- // Toolbox provides access to tool dependencies.
17
- type Toolbox interface {
18
- CoderClient () * codersdk.Client
19
- AgentClient () (* agentsdk.Client , bool )
20
- AppStatusSlug () (string , bool )
21
-
22
- WithAgentClient (* agentsdk.Client ) Toolbox
23
- WithAppStatusSlug (string ) Toolbox
24
- }
25
-
26
- // toolbox is the concrete implementation of Toolbox.
27
- type toolbox struct {
28
- coderClient * codersdk.Client
29
- agentClient * agentsdk.Client
30
- appStatusSlug string
31
- }
32
-
33
- // NewToolbox constructs a Toolbox with a required CoderClient.
34
- func NewToolbox (coder * codersdk.Client ) Toolbox {
35
- return & toolbox {coderClient : coder }
36
- }
37
-
38
- func (tb * toolbox ) CoderClient () * codersdk.Client {
39
- return tb .coderClient
40
- }
41
-
42
- func (tb * toolbox ) AgentClient () (* agentsdk.Client , bool ) {
43
- return tb .agentClient , tb .agentClient != nil
44
- }
45
-
46
- func (tb * toolbox ) AppStatusSlug () (string , bool ) {
47
- return tb .appStatusSlug , tb .appStatusSlug != ""
48
- }
49
-
50
- func (tb * toolbox ) WithAgentClient (agent * agentsdk.Client ) Toolbox {
51
- tb .agentClient = agent
52
- return tb
53
- }
54
-
55
- func (tb * toolbox ) WithAppStatusSlug (slug string ) Toolbox {
56
- tb .appStatusSlug = slug
57
- return tb
16
+ // Deps provides access to tool dependencies.
17
+ type Deps struct {
18
+ CoderClient * codersdk.Client
19
+ AgentClient * agentsdk.Client
20
+ AppStatusSlug string
58
21
}
59
22
60
23
// HandlerFunc is a function that handles a tool call.
61
- type HandlerFunc [Arg , Ret any ] func (tb Toolbox , args Arg ) (Ret , error )
24
+ type HandlerFunc [Arg , Ret any ] func (tb Deps , args Arg ) (Ret , error )
62
25
63
26
type Tool [Arg , Ret any ] struct {
64
27
aisdk.Tool
@@ -69,7 +32,7 @@ type Tool[Arg, Ret any] struct {
69
32
func (t Tool [Arg , Ret ]) Generic () Tool [any , any ] {
70
33
return Tool [any , any ]{
71
34
Tool : t .Tool ,
72
- Handler : func (tb Toolbox , args any ) (any , error ) {
35
+ Handler : func (tb Deps , args any ) (any , error ) {
73
36
typedArg , ok := args .(Arg )
74
37
if ! ok {
75
38
return nil , xerrors .Errorf ("developer error: invalid argument type for tool %s" , t .Tool .Name )
@@ -152,7 +115,7 @@ type UploadTarFileArgs struct {
152
115
153
116
// WithRecover wraps a HandlerFunc to recover from panics and return an error.
154
117
func WithRecover [Arg , Ret any ](h HandlerFunc [Arg , Ret ]) HandlerFunc [Arg , Ret ] {
155
- return func (tb Toolbox , args Arg ) (ret Ret , err error ) {
118
+ return func (tb Deps , args Arg ) (ret Ret , err error ) {
156
119
defer func () {
157
120
if r := recover (); r != nil {
158
121
err = xerrors .Errorf ("tool handler panic: %v" , r )
@@ -220,17 +183,15 @@ var (
220
183
Required : []string {"summary" , "link" , "state" },
221
184
},
222
185
},
223
- Handler : func (tb Toolbox , args ReportTaskArgs ) (string , error ) {
224
- agentClient , ok := tb .AgentClient ()
225
- if ! ok {
186
+ Handler : func (tb Deps , args ReportTaskArgs ) (string , error ) {
187
+ if tb .AgentClient == nil {
226
188
return "" , xerrors .New ("tool unavailable as CODER_AGENT_TOKEN or CODER_AGENT_TOKEN_FILE not set" )
227
189
}
228
- appStatusSlug , ok := tb .AppStatusSlug ()
229
- if ! ok {
190
+ if tb .AppStatusSlug == "" {
230
191
return "" , xerrors .New ("workspace app status slug not found in toolbox" )
231
192
}
232
- if err := agentClient .PatchAppStatus (context .TODO (), agentsdk.PatchAppStatus {
233
- AppSlug : appStatusSlug ,
193
+ if err := tb . AgentClient .PatchAppStatus (context .TODO (), agentsdk.PatchAppStatus {
194
+ AppSlug : tb . AppStatusSlug ,
234
195
Message : args .Summary ,
235
196
URI : args .Link ,
236
197
State : codersdk .WorkspaceAppStatusState (args .State ),
@@ -256,12 +217,12 @@ This returns more data than list_workspaces to reduce token usage.`,
256
217
Required : []string {"workspace_id" },
257
218
},
258
219
},
259
- Handler : func (tb Toolbox , args GetWorkspaceArgs ) (codersdk.Workspace , error ) {
220
+ Handler : func (tb Deps , args GetWorkspaceArgs ) (codersdk.Workspace , error ) {
260
221
wsID , err := uuid .Parse (args .WorkspaceID )
261
222
if err != nil {
262
223
return codersdk.Workspace {}, xerrors .New ("workspace_id must be a valid UUID" )
263
224
}
264
- return tb .CoderClient () .Workspace (context .TODO (), wsID )
225
+ return tb .CoderClient .Workspace (context .TODO (), wsID )
265
226
},
266
227
}
267
228
@@ -296,7 +257,7 @@ is provisioned correctly and the agent can connect to the control plane.
296
257
Required : []string {"user" , "template_version_id" , "name" , "rich_parameters" },
297
258
},
298
259
},
299
- Handler : func (tb Toolbox , args CreateWorkspaceArgs ) (codersdk.Workspace , error ) {
260
+ Handler : func (tb Deps , args CreateWorkspaceArgs ) (codersdk.Workspace , error ) {
300
261
tvID , err := uuid .Parse (args .TemplateVersionID )
301
262
if err != nil {
302
263
return codersdk.Workspace {}, xerrors .New ("template_version_id must be a valid UUID" )
@@ -311,7 +272,7 @@ is provisioned correctly and the agent can connect to the control plane.
311
272
Value : v ,
312
273
})
313
274
}
314
- workspace , err := tb .CoderClient () .CreateUserWorkspace (context .TODO (), args .User , codersdk.CreateWorkspaceRequest {
275
+ workspace , err := tb .CoderClient .CreateUserWorkspace (context .TODO (), args .User , codersdk.CreateWorkspaceRequest {
315
276
TemplateVersionID : tvID ,
316
277
Name : args .Name ,
317
278
RichParameterValues : buildParams ,
@@ -336,12 +297,12 @@ is provisioned correctly and the agent can connect to the control plane.
336
297
},
337
298
},
338
299
},
339
- Handler : func (tb Toolbox , args ListWorkspacesArgs ) ([]MinimalWorkspace , error ) {
300
+ Handler : func (tb Deps , args ListWorkspacesArgs ) ([]MinimalWorkspace , error ) {
340
301
owner := args .Owner
341
302
if owner == "" {
342
303
owner = codersdk .Me
343
304
}
344
- workspaces , err := tb .CoderClient () .Workspaces (context .TODO (), codersdk.WorkspaceFilter {
305
+ workspaces , err := tb .CoderClient .Workspaces (context .TODO (), codersdk.WorkspaceFilter {
345
306
Owner : owner ,
346
307
})
347
308
if err != nil {
@@ -373,8 +334,8 @@ is provisioned correctly and the agent can connect to the control plane.
373
334
Required : []string {},
374
335
},
375
336
},
376
- Handler : func (tb Toolbox , _ NoArgs ) ([]MinimalTemplate , error ) {
377
- templates , err := tb .CoderClient () .Templates (context .TODO (), codersdk.TemplateFilter {})
337
+ Handler : func (tb Deps , _ NoArgs ) ([]MinimalTemplate , error ) {
338
+ templates , err := tb .CoderClient .Templates (context .TODO (), codersdk.TemplateFilter {})
378
339
if err != nil {
379
340
return nil , err
380
341
}
@@ -406,12 +367,12 @@ is provisioned correctly and the agent can connect to the control plane.
406
367
Required : []string {"template_version_id" },
407
368
},
408
369
},
409
- Handler : func (tb Toolbox , args ListTemplateVersionParametersArgs ) ([]codersdk.TemplateVersionParameter , error ) {
370
+ Handler : func (tb Deps , args ListTemplateVersionParametersArgs ) ([]codersdk.TemplateVersionParameter , error ) {
410
371
templateVersionID , err := uuid .Parse (args .TemplateVersionID )
411
372
if err != nil {
412
373
return nil , xerrors .Errorf ("template_version_id must be a valid UUID: %w" , err )
413
374
}
414
- parameters , err := tb .CoderClient () .TemplateVersionRichParameters (context .TODO (), templateVersionID )
375
+ parameters , err := tb .CoderClient .TemplateVersionRichParameters (context .TODO (), templateVersionID )
415
376
if err != nil {
416
377
return nil , err
417
378
}
@@ -428,8 +389,8 @@ is provisioned correctly and the agent can connect to the control plane.
428
389
Required : []string {},
429
390
},
430
391
},
431
- Handler : func (tb Toolbox , _ NoArgs ) (codersdk.User , error ) {
432
- return tb .CoderClient () .User (context .TODO (), "me" )
392
+ Handler : func (tb Deps , _ NoArgs ) (codersdk.User , error ) {
393
+ return tb .CoderClient .User (context .TODO (), "me" )
433
394
},
434
395
}
435
396
@@ -455,7 +416,7 @@ is provisioned correctly and the agent can connect to the control plane.
455
416
Required : []string {"workspace_id" , "transition" },
456
417
},
457
418
},
458
- Handler : func (tb Toolbox , args CreateWorkspaceBuildArgs ) (codersdk.WorkspaceBuild , error ) {
419
+ Handler : func (tb Deps , args CreateWorkspaceBuildArgs ) (codersdk.WorkspaceBuild , error ) {
459
420
workspaceID , err := uuid .Parse (args .WorkspaceID )
460
421
if err != nil {
461
422
return codersdk.WorkspaceBuild {}, xerrors .Errorf ("workspace_id must be a valid UUID: %w" , err )
@@ -474,7 +435,7 @@ is provisioned correctly and the agent can connect to the control plane.
474
435
if templateVersionID != uuid .Nil {
475
436
cbr .TemplateVersionID = templateVersionID
476
437
}
477
- return tb .CoderClient () .CreateWorkspaceBuild (context .TODO (), workspaceID , cbr )
438
+ return tb .CoderClient .CreateWorkspaceBuild (context .TODO (), workspaceID , cbr )
478
439
},
479
440
}
480
441
@@ -936,8 +897,8 @@ The file_id provided is a reference to a tar file you have uploaded containing t
936
897
Required : []string {"file_id" },
937
898
},
938
899
},
939
- Handler : func (tb Toolbox , args CreateTemplateVersionArgs ) (codersdk.TemplateVersion , error ) {
940
- me , err := tb .CoderClient () .User (context .TODO (), "me" )
900
+ Handler : func (tb Deps , args CreateTemplateVersionArgs ) (codersdk.TemplateVersion , error ) {
901
+ me , err := tb .CoderClient .User (context .TODO (), "me" )
941
902
if err != nil {
942
903
return codersdk.TemplateVersion {}, err
943
904
}
@@ -949,7 +910,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
949
910
if err != nil {
950
911
return codersdk.TemplateVersion {}, xerrors .Errorf ("template_id must be a valid UUID: %w" , err )
951
912
}
952
- templateVersion , err := tb .CoderClient () .CreateTemplateVersion (context .TODO (), me .OrganizationIDs [0 ], codersdk.CreateTemplateVersionRequest {
913
+ templateVersion , err := tb .CoderClient .CreateTemplateVersion (context .TODO (), me .OrganizationIDs [0 ], codersdk.CreateTemplateVersionRequest {
953
914
Message : "Created by AI" ,
954
915
StorageMethod : codersdk .ProvisionerStorageMethodFile ,
955
916
FileID : fileID ,
@@ -978,12 +939,12 @@ The file_id provided is a reference to a tar file you have uploaded containing t
978
939
Required : []string {"workspace_agent_id" },
979
940
},
980
941
},
981
- Handler : func (tb Toolbox , args GetWorkspaceAgentLogsArgs ) ([]string , error ) {
942
+ Handler : func (tb Deps , args GetWorkspaceAgentLogsArgs ) ([]string , error ) {
982
943
workspaceAgentID , err := uuid .Parse (args .WorkspaceAgentID )
983
944
if err != nil {
984
945
return nil , xerrors .Errorf ("workspace_agent_id must be a valid UUID: %w" , err )
985
946
}
986
- logs , closer , err := tb .CoderClient () .WorkspaceAgentLogsAfter (context .TODO (), workspaceAgentID , 0 , false )
947
+ logs , closer , err := tb .CoderClient .WorkspaceAgentLogsAfter (context .TODO (), workspaceAgentID , 0 , false )
987
948
if err != nil {
988
949
return nil , err
989
950
}
@@ -1013,12 +974,12 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1013
974
Required : []string {"workspace_build_id" },
1014
975
},
1015
976
},
1016
- Handler : func (tb Toolbox , args GetWorkspaceBuildLogsArgs ) ([]string , error ) {
977
+ Handler : func (tb Deps , args GetWorkspaceBuildLogsArgs ) ([]string , error ) {
1017
978
workspaceBuildID , err := uuid .Parse (args .WorkspaceBuildID )
1018
979
if err != nil {
1019
980
return nil , xerrors .Errorf ("workspace_build_id must be a valid UUID: %w" , err )
1020
981
}
1021
- logs , closer , err := tb .CoderClient () .WorkspaceBuildLogsAfter (context .TODO (), workspaceBuildID , 0 )
982
+ logs , closer , err := tb .CoderClient .WorkspaceBuildLogsAfter (context .TODO (), workspaceBuildID , 0 )
1022
983
if err != nil {
1023
984
return nil , err
1024
985
}
@@ -1044,13 +1005,13 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1044
1005
Required : []string {"template_version_id" },
1045
1006
},
1046
1007
},
1047
- Handler : func (tb Toolbox , args GetTemplateVersionLogsArgs ) ([]string , error ) {
1008
+ Handler : func (tb Deps , args GetTemplateVersionLogsArgs ) ([]string , error ) {
1048
1009
templateVersionID , err := uuid .Parse (args .TemplateVersionID )
1049
1010
if err != nil {
1050
1011
return nil , xerrors .Errorf ("template_version_id must be a valid UUID: %w" , err )
1051
1012
}
1052
1013
1053
- logs , closer , err := tb .CoderClient () .TemplateVersionLogsAfter (context .TODO (), templateVersionID , 0 )
1014
+ logs , closer , err := tb .CoderClient .TemplateVersionLogsAfter (context .TODO (), templateVersionID , 0 )
1054
1015
if err != nil {
1055
1016
return nil , err
1056
1017
}
@@ -1079,7 +1040,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1079
1040
Required : []string {"template_id" , "template_version_id" },
1080
1041
},
1081
1042
},
1082
- Handler : func (tb Toolbox , args UpdateTemplateActiveVersionArgs ) (string , error ) {
1043
+ Handler : func (tb Deps , args UpdateTemplateActiveVersionArgs ) (string , error ) {
1083
1044
templateID , err := uuid .Parse (args .TemplateID )
1084
1045
if err != nil {
1085
1046
return "" , xerrors .Errorf ("template_id must be a valid UUID: %w" , err )
@@ -1088,7 +1049,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1088
1049
if err != nil {
1089
1050
return "" , xerrors .Errorf ("template_version_id must be a valid UUID: %w" , err )
1090
1051
}
1091
- err = tb .CoderClient () .UpdateActiveTemplateVersion (context .TODO (), templateID , codersdk.UpdateActiveTemplateVersion {
1052
+ err = tb .CoderClient .UpdateActiveTemplateVersion (context .TODO (), templateID , codersdk.UpdateActiveTemplateVersion {
1092
1053
ID : templateVersionID ,
1093
1054
})
1094
1055
if err != nil {
@@ -1112,7 +1073,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1112
1073
Required : []string {"mime_type" , "files" },
1113
1074
},
1114
1075
},
1115
- Handler : func (tb Toolbox , args UploadTarFileArgs ) (codersdk.UploadResponse , error ) {
1076
+ Handler : func (tb Deps , args UploadTarFileArgs ) (codersdk.UploadResponse , error ) {
1116
1077
pipeReader , pipeWriter := io .Pipe ()
1117
1078
go func () {
1118
1079
defer pipeWriter .Close ()
@@ -1137,7 +1098,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1137
1098
}
1138
1099
}()
1139
1100
1140
- resp , err := tb .CoderClient () .Upload (context .TODO (), codersdk .ContentTypeTar , pipeReader )
1101
+ resp , err := tb .CoderClient .Upload (context .TODO (), codersdk .ContentTypeTar , pipeReader )
1141
1102
if err != nil {
1142
1103
return codersdk.UploadResponse {}, err
1143
1104
}
@@ -1172,16 +1133,16 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1172
1133
Required : []string {"name" , "display_name" , "description" , "version_id" },
1173
1134
},
1174
1135
},
1175
- Handler : func (tb Toolbox , args CreateTemplateArgs ) (codersdk.Template , error ) {
1176
- me , err := tb .CoderClient () .User (context .TODO (), "me" )
1136
+ Handler : func (tb Deps , args CreateTemplateArgs ) (codersdk.Template , error ) {
1137
+ me , err := tb .CoderClient .User (context .TODO (), "me" )
1177
1138
if err != nil {
1178
1139
return codersdk.Template {}, err
1179
1140
}
1180
1141
versionID , err := uuid .Parse (args .VersionID )
1181
1142
if err != nil {
1182
1143
return codersdk.Template {}, xerrors .Errorf ("version_id must be a valid UUID: %w" , err )
1183
1144
}
1184
- template , err := tb .CoderClient () .CreateTemplate (context .TODO (), me .OrganizationIDs [0 ], codersdk.CreateTemplateRequest {
1145
+ template , err := tb .CoderClient .CreateTemplate (context .TODO (), me .OrganizationIDs [0 ], codersdk.CreateTemplateRequest {
1185
1146
Name : args .Name ,
1186
1147
DisplayName : args .DisplayName ,
1187
1148
Description : args .Description ,
@@ -1206,12 +1167,12 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1206
1167
},
1207
1168
},
1208
1169
},
1209
- Handler : func (tb Toolbox , args DeleteTemplateArgs ) (string , error ) {
1170
+ Handler : func (tb Deps , args DeleteTemplateArgs ) (string , error ) {
1210
1171
templateID , err := uuid .Parse (args .TemplateID )
1211
1172
if err != nil {
1212
1173
return "" , xerrors .Errorf ("template_id must be a valid UUID: %w" , err )
1213
1174
}
1214
- err = tb .CoderClient () .DeleteTemplate (context .TODO (), templateID )
1175
+ err = tb .CoderClient .DeleteTemplate (context .TODO (), templateID )
1215
1176
if err != nil {
1216
1177
return "" , err
1217
1178
}
0 commit comments