fix(skill): 解析 stringified JSON 数组形式的 args

部分模型供应商把 execute_skill_script 的 args(schema 为 []string)
emit 成 JSON 字符串,内容本身是个 JSON 数组("[\"--project-name\",\"X\"]")。
原 UnmarshalJSON 兜底用 strings.Fields 拆这种字符串,把方括号/引号拆成
垃圾 token,脚本 argv 全是乱码,argparse 永远报参数缺失,模型无论如何
调整格式都失败。改为先把字符串内容当 JSON 数组解析,成功即用;否则再
退回 strings.Fields。补 UnmarshalJSON 各形态单测。

Co-Authored-By: Claude <noreply@anthropic.com>
main
养乐多 2026-08-28 09:58:48 +08:00 committed by lyingbug
parent a2328f0470
commit 13abc41b0d
2 changed files with 65 additions and 3 deletions

View File

@ -108,9 +108,18 @@ func (i *ExecuteSkillScriptInput) UnmarshalJSON(data []byte) error {
return fmt.Errorf("args must be a string or an array of strings: %w", err)
}
// A string is interpreted as a conventional space-separated command line.
// The tool schema continues to advertise []string, so well-formed calls are
// unaffected; this is only a compatibility fallback for model output.
// Some providers emit the array as a stringified JSON payload
// (e.g. "[\"--project-name\",\"X\"]"). Treat that as an array first so the
// model's intent is preserved; strings.Fields would otherwise split the
// brackets/quotes into garbage tokens and the script would see nonsense argv.
if err := json.Unmarshal([]byte(argsString), &i.Args); err == nil {
return nil
}
// A plain string is interpreted as a conventional space-separated command
// line. The tool schema continues to advertise []string, so well-formed
// calls are unaffected; this is only a compatibility fallback for model
// output.
i.Args = strings.Fields(argsString)
return nil
}

View File

@ -1,6 +1,7 @@
package tools
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
@ -33,3 +34,55 @@ func TestExecuteSkillScriptDescriptionAcceptsWorkspacePaths(t *testing.T) {
assert.Contains(t, executeSkillScriptTool.Description(), "install_deps.py")
assert.Contains(t, executeSkillScriptTool.Description(), ".skill-packages")
}
// TestExecuteSkillScriptInputUnmarshalJSON covers the model-emitted shapes the
// UnmarshalJSON fallback must tolerate. The provider does not always honor the
// []string schema: it sometimes emits a stringified JSON array or a single
// command-line string. Each must round-trip to the intended argv.
func TestExecuteSkillScriptInputUnmarshalJSON(t *testing.T) {
t.Run("real array", func(t *testing.T) {
var in ExecuteSkillScriptInput
require.NoError(t, json.Unmarshal([]byte(`{
"skill_name": "s", "script_path": "p",
"args": ["--project-name", "X", "--creator", "admin"]
}`), &in))
require.Equal(t, []string{"--project-name", "X", "--creator", "admin"}, in.Args)
})
t.Run("stringified json array", func(t *testing.T) {
// Some providers emit args as a JSON string whose content is itself a
// JSON array. strings.Fields would mangle the brackets/quotes into
// one garbage token; the array must be recovered instead.
var in ExecuteSkillScriptInput
require.NoError(t, json.Unmarshal([]byte(`{
"skill_name": "s", "script_path": "p",
"args": "[\"--project-name\", \"X\", \"--creator\", \"admin\"]"
}`), &in))
require.Equal(t, []string{"--project-name", "X", "--creator", "admin"}, in.Args)
})
t.Run("single command line string", func(t *testing.T) {
var in ExecuteSkillScriptInput
require.NoError(t, json.Unmarshal([]byte(`{
"skill_name": "s", "script_path": "p",
"args": "ls --workspace /workspace/output"
}`), &in))
require.Equal(t, []string{"ls", "--workspace", "/workspace/output"}, in.Args)
})
t.Run("absent args", func(t *testing.T) {
var in ExecuteSkillScriptInput
require.NoError(t, json.Unmarshal([]byte(`{
"skill_name": "s", "script_path": "p"
}`), &in))
require.Empty(t, in.Args)
})
t.Run("null args", func(t *testing.T) {
var in ExecuteSkillScriptInput
require.NoError(t, json.Unmarshal([]byte(`{
"skill_name": "s", "script_path": "p", "args": null
}`), &in))
require.Empty(t, in.Args)
})
}