glue: typed NewTool[T] + TextResult/ErrorResult helpers (closes #88)#95
Conversation
Every Glue agent re-implemented the same per-tool boilerplate: a json.Unmarshal of call.Arguments into a private struct, plus local textResult / errorResult helpers. Promote the pattern into the framework so tool definitions drop ~15 lines each and gain consistent malformed- argument handling. - glue.TextResult(string) and glue.ErrorResult(error) replace the duplicated helpers (the agent had its own copies; future agents would too). - glue.NewTool[T any](spec, fn) decodes ToolCall.Arguments into T, treats empty arguments as the zero value, and returns an error ToolResult on JSON decode failure so the loop never crashes on a malformed call. Migrate agents/glue-review/tools.go as the first internal consumer: three tools converted, the local textResult/errorResult helpers removed. Schema generation from T is intentionally out of scope; callers continue to pass Parameters explicitly. Verification: go build ./... # ok go vet ./... # ok go test ./... # ok (all packages green) Closes #88 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Summary
This branch introduces typed tool helpers (glue.NewTool[T], glue.TextResult, glue.ErrorResult) to eliminate repetitive JSON unmarshalling boilerplate across Glue agents and migrates the glue-review agent as the first consumer.
Issues
No correctness problems found.
Suggestions
-
[minor] tool_helpers.go:24 —
TextResultandErrorResultare missing doc comments forToolResult,ContentPart, andContentTypeTexttypes in their public API. While the current comments are good, consider adding a package-level doc comment for the new file. Fix: Add a package-level comment totool_helpers.goexplaining the purpose of these helpers, e.g.// Package glue provides typed tool builders and result helpers for glue agents. -
[minor] tool_helpers_test.go:95 —
TestNewTool_MalformedArgsReturnsErrorResultonly tests that the error contains the tool name; it doesn't verify the full error prefixdecode arguments for tool. Fix: Intool_helpers_test.go, change the assertion on line 95 from!strings.Contains(res.Content[0].Text,tool "echo")to!strings.Contains(res.Content[0].Text,decode arguments for tool "echo")to more precisely match the expected error format.
Looks good
- The
NewTool[T]generic design cleanly separates the typed argument decoding from the tool specification, reducing per-tool boilerplate significantly. - Error handling for malformed arguments is consistent and prevents loop crashes — good defensive design.
- Test coverage includes the zero-value empty-arguments case, which is easy to miss but important.
- The migration of
agents/glue-review/tools.goremoves duplicated localtextResult/errorResulthelpers as intended.
Open questions
None.
🤖 Posted by glue-review.
| return ToolResult{ | ||
| Content: []ContentPart{{Type: ContentTypeText, Text: err.Error()}}, | ||
| IsError: true, | ||
| } |
There was a problem hiding this comment.
[minor] TextResult and ErrorResult are missing doc comments for ToolResult, ContentPart, and ContentTypeText types in their public API. While the current comments are good, consider adding a package-level doc comment for the new file
💡 AI prompt to fix
Add a package-level comment to `tool_helpers.go` explaining the purpose of these helpers, e.g. `// Package glue provides typed tool builders and result helpers for glue agents.`
|
|
||
| func intStr(n int) string { | ||
| if n == 0 { | ||
| return "0" |
There was a problem hiding this comment.
[minor] TestNewTool_MalformedArgsReturnsErrorResult only tests that the error contains the tool name; it doesn't verify the full error prefix decode arguments for tool
💡 AI prompt to fix
In `tool_helpers_test.go`, change the assertion on line 95 from `!strings.Contains(res.Content[0].Text, `tool "echo"`)` to `!strings.Contains(res.Content[0].Text, `decode arguments for tool "echo"`)` to more precisely match the expected error format.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Summary
glue.TextResult(string)andglue.ErrorResult(error)so agents stop redefining them locally.glue.NewTool[T any](spec, fn)that decodesToolCall.ArgumentsintoTbefore invoking the executor; malformed JSON surfaces to the model as an errorToolResultrather than crashing the loop.agents/glue-review/tools.goas the first internal consumer (three tools converted, local helpers deleted).Schema generation from
Tis intentionally out of scope; callers continue to passParametersexplicitly.Closes #88
Test plan
go build ./...go vet ./...go test ./...(all packages green, agent fixture replay still passes)tool_helpers_test.gocover typed decode, empty-args zero value, malformed-args → error ToolResult.🤖 Generated with Claude Code