From 686c57c8301c62c433c97ca268acec0236dc4b35 Mon Sep 17 00:00:00 2001 From: xgopilot Date: Wed, 6 May 2026 04:51:53 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E5=86=97?= =?UTF-8?q?=E4=BD=99=E7=9A=84=E9=98=B2=E5=BE=A1=E6=80=A7=E7=BC=96=E7=A8=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - write_tool.go: 移除 map 访问前不必要的 nil 检查(Go 中访问 nil map 是安全的) - interceptors.go: 同上,移除 stringArg 中的 nil 检查 - registry.go: 移除 json.Unmarshal 前多余的长度检查(error 已被忽略) - agent.go: 将倒置的错误检查改为标准 Go 错误处理惯用法 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: minorcell <0+minorcell@users.noreply.github.com> --- internal/core/agent.go | 6 +++--- internal/tools/interceptors.go | 3 --- internal/tools/registry.go | 4 +--- internal/tools/write_tool.go | 6 ++---- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/internal/core/agent.go b/internal/core/agent.go index b9081b9..e08110f 100644 --- a/internal/core/agent.go +++ b/internal/core/agent.go @@ -235,10 +235,10 @@ func stringifyArguments(arguments json.RawMessage) string { } var out bytes.Buffer - if err := json.Indent(&out, arguments, "", " "); err == nil { - return out.String() + if err := json.Indent(&out, arguments, "", " "); err != nil { + return string(arguments) } - return string(arguments) + return out.String() } func emitProgress(observer TurnObserver, event ProgressEvent) { diff --git a/internal/tools/interceptors.go b/internal/tools/interceptors.go index c68afef..8338efd 100644 --- a/internal/tools/interceptors.go +++ b/internal/tools/interceptors.go @@ -91,9 +91,6 @@ func SafeJoin(root string, candidate string) (string, error) { // stringArg 从已解析参数中安全读取字符串值。 func stringArg(arguments map[string]any, key string) (string, bool) { - if arguments == nil { - return "", false - } value, ok := arguments[key] if !ok { return "", false diff --git a/internal/tools/registry.go b/internal/tools/registry.go index e20685c..9f637be 100644 --- a/internal/tools/registry.go +++ b/internal/tools/registry.go @@ -77,9 +77,7 @@ func (r *Registry) Execute(ctx context.Context, call provider.ToolCall, state St SessionID: state.SessionID, Arguments: call.Arguments, } - if len(call.Arguments) > 0 { - _ = json.Unmarshal(call.Arguments, &invocation.ParsedArgs) - } + _ = json.Unmarshal(call.Arguments, &invocation.ParsedArgs) for _, interceptor := range r.interceptors { if err := interceptor.Before(ctx, &invocation); err != nil { diff --git a/internal/tools/write_tool.go b/internal/tools/write_tool.go index 234e2b8..57bf4f1 100644 --- a/internal/tools/write_tool.go +++ b/internal/tools/write_tool.go @@ -77,10 +77,8 @@ func (t *WriteTool) Execute(_ context.Context, invocation Invocation) (Result, e } create := true - if invocation.ParsedArgs != nil { - if v, ok := invocation.ParsedArgs["create"].(bool); ok { - create = v - } + if v, ok := invocation.ParsedArgs["create"].(bool); ok { + create = v } return t.write(target, args.Content, args.Append, create)