Skip to content

glue: typed NewTool[T] + TextResult/ErrorResult helpers (closes #88)#95

Merged
erain merged 2 commits into
mainfrom
issue/88-typed-tool-helpers
May 7, 2026
Merged

glue: typed NewTool[T] + TextResult/ErrorResult helpers (closes #88)#95
erain merged 2 commits into
mainfrom
issue/88-typed-tool-helpers

Conversation

@erain
Copy link
Copy Markdown
Owner

@erain erain commented May 7, 2026

Summary

  • Add glue.TextResult(string) and glue.ErrorResult(error) so agents stop redefining them locally.
  • Add glue.NewTool[T any](spec, fn) that decodes ToolCall.Arguments into T before invoking the executor; malformed JSON surfaces to the model as an error ToolResult rather than crashing the loop.
  • Migrate agents/glue-review/tools.go as the first internal consumer (three tools converted, local helpers deleted).
  • README gains a typed-tool example.

Schema generation from T is intentionally out of scope; callers continue to pass Parameters explicitly.

Closes #88

Test plan

  • go build ./...
  • go vet ./...
  • go test ./... (all packages green, agent fixture replay still passes)
  • Unit tests in tool_helpers_test.go cover typed decode, empty-args zero value, malformed-args → error ToolResult.

🤖 Generated with Claude Code

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>
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 — 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. 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.

  • [minor] tool_helpers_test.go:95 — TestNewTool_MalformedArgsReturnsErrorResult only tests that the error contains the tool name; it doesn't verify the full error prefix decode arguments for tool. 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.

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.go removes duplicated local textResult/errorResult helpers as intended.

Open questions

None.


🤖 Posted by glue-review.

Comment thread tool_helpers.go
return ToolResult{
Content: []ContentPart{{Type: ContentTypeText, Text: err.Error()}},
IsError: true,
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.`

Comment thread tool_helpers_test.go Outdated

func intStr(n int) string {
if n == 0 {
return "0"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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>
@erain erain merged commit 43c499d into main May 7, 2026
4 checks passed
@erain erain deleted the issue/88-typed-tool-helpers branch May 7, 2026 14:16
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.

Typed tool helper + exported result helpers (closes agent boilerplate)

1 participant