Skip to content

feat(go-sdk): deduplicate tool_calling, add ToolCallResult wrapper, PromptConfig, system prompt support, and tool name sanitization#483

Open
Luffy2208 wants to merge 3 commits intoAgent-Field:mainfrom
Luffy2208:feat/go-tool-calling-refactor
Open

feat(go-sdk): deduplicate tool_calling, add ToolCallResult wrapper, PromptConfig, system prompt support, and tool name sanitization#483
Luffy2208 wants to merge 3 commits intoAgent-Field:mainfrom
Luffy2208:feat/go-tool-calling-refactor

Conversation

@Luffy2208
Copy link
Copy Markdown
Contributor

Summary

Refactors the Go SDK tool-calling loop to improve code quality, remove duplication, and align behavior with Python and TypeScript SDKs. This PR introduces structured abstractions, configurable prompts, system prompt support, and tool name sanitization while preserving backward compatibility.


Key Changes

1. Deduplicated Capability Conversion

  • Removed duplicated logic for ReasonerCapability and SkillCapability
  • Introduced shared helpers:
    • capabilityToTool(...)
    • normalizeToolParameters(...)
  • Updated CapabilityToToolDefinition to delegate to helper functions

2. Added ToolCallResult Wrapper (API Parity)

  • Introduced:
    type ToolCallResult struct {
        Response *Response
        Trace    *ToolCallTrace
    }
  • Added .Text() helper for convenience
  • Introduced ExecuteToolCallLoopResult(...)
  • Preserved backward compatibility via existing ExecuteToolCallLoop(...)

3. Introduced PromptConfig (Removed Hardcoded Strings)

  • Added:
    type PromptConfig struct {
        ToolCallLimitReached string
        ToolErrorFormatter   func(...)
        ToolResultFormatter  func(...)
    }
  • Implemented:
    • DefaultPromptConfig()
    • resolvePromptConfig(...)
  • Replaced all hardcoded tool-related messages with configurable formatters

4. Added System Prompt Support

  • Extended ToolCallConfig:
    SystemPrompt string
  • Injected using:
    WithSystem(config.SystemPrompt)

5. Added Tool Name Sanitization

  • Implemented:
    • sanitizeToolName(...)
    • unsanitizeToolName(...)
  • Applied across:
    • Tool definitions
    • Tool execution
    • Trace recording

6. Refactored Tool-Calling Loop

  • Extracted request builder:
    • buildToolCallRequest(...)
  • Introduced:
    • loopMessages
    • effectiveOpts
  • Removed duplicated request-building logic
  • Improved error handling flow

7. Added encodeToolContent Helper

  • Centralized encoding logic for:
    • Tool errors
    • Tool results
    • Limit messages
  • Handles string/JSON formatting safely

8. Improved Default Configuration

  • Updated DefaultToolCallConfig() to initialize PromptConfig

9. Updated agent.go for Compatibility

  • Added transformation layer:
    • Converts :-based tool names to . format before execution

10. Expanded Test Coverage

Added tests for:

  • Tool name sanitization (round-trip + capability conversion)
  • PromptConfig behavior (limit messages, error formatting, result formatting)
  • System prompt injection
  • ToolCallResult wrapper
  • Default config initialization

Testing

  • ./scripts/test-all.sh
  • Ran go test ./... successfully
  • Ran go vet ./... successfully

Checklist

  • I updated documentation where applicable.
  • I added or updated tests (or none were needed).
  • I updated CHANGELOG.md (or this change does not warrant a changelog entry).

Screenshots (if UI-related)

Not applicable


Related Issues

Fixes #234

@Luffy2208 Luffy2208 requested review from a team and AbirAbbas as code owners April 19, 2026 10:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 19, 2026

Performance

SDK Memory Δ Latency Δ Tests Status
Go 210 B -25% 0.57 µs -43%

✓ No regressions detected

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 19, 2026

📊 Coverage gate

Thresholds from .coverage-gate.toml: per-surface ≥ 86%, aggregate ≥ 88%, max per-surface regression ≤ 1.0 pp, max aggregate regression ≤ 0.50 pp.

Surface Current Baseline Δ
control-plane 87.20% 87.30% ↓ -0.10 pp 🟡
sdk-go 90.70% 90.70% → +0.00 pp 🟢
sdk-python 93.63% 93.63% ↑ +0.00 pp 🟢
sdk-typescript 92.63% 92.56% ↑ +0.07 pp 🟢
web-ui 90.02% 90.01% ↑ +0.01 pp 🟢
aggregate 88.98% 89.01% ↓ -0.03 pp 🟡

✅ Gate passed

No surface regressed past the allowed threshold and the aggregate stayed above the floor.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 19, 2026

📐 Patch coverage gate

Threshold: 80% on lines this PR touches vs origin/main (from .coverage-gate.toml:thresholds.min_patch).

Surface Touched lines Patch coverage Status
control-plane 0 ➖ no changes
sdk-go 140 84.00%
sdk-python 0 ➖ no changes
sdk-typescript 0 ➖ no changes
web-ui 0 ➖ no changes

✅ Patch gate passed

Every surface whose lines were touched by this PR has patch coverage at or above the threshold.

@Luffy2208
Copy link
Copy Markdown
Contributor Author

Working on test coverage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Go SDK] Code quality: deduplicate tool_calling.go, add ToolCallResponse wrapper, add PromptConfig parity

1 participant