Skip to content

Commit 77e2521

Browse files
authored
feat: support workspace name in get workspace tool (coder#20474)
This lets the LLM skip the list workspace step in some cases. Closes coder/internal#1022
1 parent c627a68 commit 77e2521

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

codersdk/toolsdk/toolsdk.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,14 @@ type GetWorkspaceArgs struct {
317317
var GetWorkspace = Tool[GetWorkspaceArgs, codersdk.Workspace]{
318318
Tool: aisdk.Tool{
319319
Name: ToolNameGetWorkspace,
320-
Description: `Get a workspace by ID.
320+
Description: `Get a workspace by name or ID.
321321
322322
This returns more data than list_workspaces to reduce token usage.`,
323323
Schema: aisdk.Schema{
324324
Properties: map[string]any{
325325
"workspace_id": map[string]any{
326-
"type": "string",
326+
"type": "string",
327+
"description": workspaceDescription,
327328
},
328329
},
329330
Required: []string{"workspace_id"},
@@ -332,7 +333,7 @@ This returns more data than list_workspaces to reduce token usage.`,
332333
Handler: func(ctx context.Context, deps Deps, args GetWorkspaceArgs) (codersdk.Workspace, error) {
333334
wsID, err := uuid.Parse(args.WorkspaceID)
334335
if err != nil {
335-
return codersdk.Workspace{}, xerrors.New("workspace_id must be a valid UUID")
336+
return namedWorkspace(ctx, deps.coderClient, NormalizeWorkspaceInput(args.WorkspaceID))
336337
}
337338
return deps.coderClient.Workspace(ctx, wsID)
338339
},
@@ -1432,7 +1433,7 @@ var WorkspaceLS = Tool[WorkspaceLSArgs, WorkspaceLSResponse]{
14321433
Properties: map[string]any{
14331434
"workspace": map[string]any{
14341435
"type": "string",
1435-
"description": workspaceDescription,
1436+
"description": workspaceAgentDescription,
14361437
},
14371438
"path": map[string]any{
14381439
"type": "string",
@@ -1489,7 +1490,7 @@ var WorkspaceReadFile = Tool[WorkspaceReadFileArgs, WorkspaceReadFileResponse]{
14891490
Properties: map[string]any{
14901491
"workspace": map[string]any{
14911492
"type": "string",
1492-
"description": workspaceDescription,
1493+
"description": workspaceAgentDescription,
14931494
},
14941495
"path": map[string]any{
14951496
"type": "string",
@@ -1566,7 +1567,7 @@ content you are trying to write, then re-encode it properly.
15661567
Properties: map[string]any{
15671568
"workspace": map[string]any{
15681569
"type": "string",
1569-
"description": workspaceDescription,
1570+
"description": workspaceAgentDescription,
15701571
},
15711572
"path": map[string]any{
15721573
"type": "string",
@@ -1614,7 +1615,7 @@ var WorkspaceEditFile = Tool[WorkspaceEditFileArgs, codersdk.Response]{
16141615
Properties: map[string]any{
16151616
"workspace": map[string]any{
16161617
"type": "string",
1617-
"description": workspaceDescription,
1618+
"description": workspaceAgentDescription,
16181619
},
16191620
"path": map[string]any{
16201621
"type": "string",
@@ -1681,7 +1682,7 @@ var WorkspaceEditFiles = Tool[WorkspaceEditFilesArgs, codersdk.Response]{
16811682
Properties: map[string]any{
16821683
"workspace": map[string]any{
16831684
"type": "string",
1684-
"description": workspaceDescription,
1685+
"description": workspaceAgentDescription,
16851686
},
16861687
"files": map[string]any{
16871688
"type": "array",
@@ -1755,7 +1756,7 @@ var WorkspacePortForward = Tool[WorkspacePortForwardArgs, WorkspacePortForwardRe
17551756
Properties: map[string]any{
17561757
"workspace": map[string]any{
17571758
"type": "string",
1758-
"description": workspaceDescription,
1759+
"description": workspaceAgentDescription,
17591760
},
17601761
"port": map[string]any{
17611762
"type": "number",
@@ -1812,7 +1813,7 @@ var WorkspaceListApps = Tool[WorkspaceListAppsArgs, WorkspaceListAppsResponse]{
18121813
Properties: map[string]any{
18131814
"workspace": map[string]any{
18141815
"type": "string",
1815-
"description": workspaceDescription,
1816+
"description": workspaceAgentDescription,
18161817
},
18171818
},
18181819
Required: []string{"workspace"},
@@ -2199,7 +2200,9 @@ func newAgentConn(ctx context.Context, client *codersdk.Client, workspace string
21992200
return conn, nil
22002201
}
22012202

2202-
const workspaceDescription = "The workspace name in the format [owner/]workspace[.agent]. If an owner is not specified, the authenticated user is used."
2203+
const workspaceDescription = "The workspace ID or name in the format [owner/]workspace. If an owner is not specified, the authenticated user is used."
2204+
2205+
const workspaceAgentDescription = "The workspace name in the format [owner/]workspace[.agent]. If an owner is not specified, the authenticated user is used."
22032206

22042207
func taskIDDescription(action string) string {
22052208
return fmt.Sprintf("ID or workspace identifier in the format [owner/]workspace[.agent] for the task to %s. If an owner is not specified, the authenticated user is used.", action)

codersdk/toolsdk/toolsdk_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,32 @@ func TestTools(t *testing.T) {
126126
t.Run("GetWorkspace", func(t *testing.T) {
127127
tb, err := toolsdk.NewDeps(memberClient)
128128
require.NoError(t, err)
129-
result, err := testTool(t, toolsdk.GetWorkspace, tb, toolsdk.GetWorkspaceArgs{
130-
WorkspaceID: r.Workspace.ID.String(),
131-
})
132129

133-
require.NoError(t, err)
134-
require.Equal(t, r.Workspace.ID, result.ID, "expected the workspace ID to match")
130+
tests := []struct {
131+
name string
132+
workspace string
133+
}{
134+
{
135+
name: "ByID",
136+
workspace: r.Workspace.ID.String(),
137+
},
138+
{
139+
name: "ByName",
140+
workspace: r.Workspace.Name,
141+
},
142+
}
143+
144+
for _, tt := range tests {
145+
t.Run(tt.name, func(t *testing.T) {
146+
t.Parallel()
147+
148+
result, err := testTool(t, toolsdk.GetWorkspace, tb, toolsdk.GetWorkspaceArgs{
149+
WorkspaceID: tt.workspace,
150+
})
151+
require.NoError(t, err)
152+
require.Equal(t, r.Workspace.ID, result.ID, "expected the workspace ID to match")
153+
})
154+
}
135155
})
136156

137157
t.Run("ListTemplates", func(t *testing.T) {

0 commit comments

Comments
 (0)