Skip to content

Commit eec794b

Browse files
committed
Improve the schema
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 3b69537 commit eec794b

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

examples/script_shell.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ agents:
1515

1616
docker_images:
1717
cmd: "docker images $img"
18-
description: "List running Docker containers"
19-
required: []
18+
description: List running Docker containers
2019
args:
2120
img:
22-
description: "Docker image to list"
23-
type: "string"
21+
description: Docker image to list
22+
type: string
2423

2524
github_user_repos:
2625
cmd: "curl -s https://api.github.com/users/$username/repos | jq '.[].name'"
27-
description: "List GitHub repositories of the provided user"
26+
description: List GitHub repositories of the provided user
27+
required: ["username"]
2828
args:
2929
username:
30-
description: "GitHub username to get the repository list for"
31-
type: "string"
30+
description: GitHub username to get the repository list for
31+
type: string
3232

3333
models:
3434
openai:

pkg/tools/builtin/script_shell.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ func (t *ScriptShellTool) Tools(context.Context) ([]tools.Tool, error) {
7979
description = fmt.Sprintf("Execute shell command: %s", cfg.Cmd)
8080
}
8181

82-
inputSchema := map[string]any{
82+
inputSchema, err := tools.SchemaToMap(map[string]any{
8383
"type": "object",
8484
"properties": cfg.Args,
8585
"required": cfg.Required,
86+
})
87+
if err != nil {
88+
return nil, fmt.Errorf("invalid schema for tool %s: %w", toolName, err)
8689
}
8790

8891
toolsList = append(toolsList, tools.Tool{

pkg/tools/builtin/script_shell_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package builtin
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -32,11 +33,11 @@ func TestNewScriptShellTool_ToolNoArg(t *testing.T) {
3233
assert.Len(t, allTools, 1)
3334

3435
schema, err := json.Marshal(allTools[0].Parameters)
36+
fmt.Println(string(schema))
3537
require.NoError(t, err)
3638
assert.JSONEq(t, `{
37-
"properties": null,
38-
"required": null,
39-
"type": "object"
39+
"type": "object",
40+
"properties": {}
4041
}`, string(schema))
4142
}
4243

@@ -50,6 +51,7 @@ func TestNewScriptShellTool_Tool(t *testing.T) {
5051
"type": "string",
5152
},
5253
},
54+
Required: []string{"username"},
5355
},
5456
}
5557

@@ -62,13 +64,13 @@ func TestNewScriptShellTool_Tool(t *testing.T) {
6264
schema, err := json.Marshal(allTools[0].Parameters)
6365
require.NoError(t, err)
6466
assert.JSONEq(t, `{
67+
"type": "object",
6568
"properties": {
6669
"username": {
6770
"description": "GitHub username to get the repository list for",
6871
"type": "string"
6972
}
7073
},
71-
"required": null,
72-
"type": "object"
74+
"required": ["username"]
7375
}`, string(schema))
7476
}

pkg/tools/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func SchemaToMap(params any) (map[string]any, error) {
4343
if m["properties"] == nil {
4444
m["properties"] = map[string]any{}
4545
}
46+
if m["required"] == nil {
47+
delete(m, "required")
48+
}
4649

4750
return m, nil
4851
}

0 commit comments

Comments
 (0)