llm: make Tool.Run return ToolOut
This is preliminary work towards
allowing tools to add additional information.
No functional changes (at least, that's the intent).
diff --git a/llm/conversation/convo.go b/llm/conversation/convo.go
index b6472e1..a52d494 100644
--- a/llm/conversation/convo.go
+++ b/llm/conversation/convo.go
@@ -484,8 +484,8 @@
defer cancel()
// TODO: move this into newToolUseContext?
toolUseCtx = context.WithValue(toolUseCtx, toolCallInfoKey, ToolCallInfo{ToolUseID: part.ID, Convo: c})
- toolResult, err := tool.Run(toolUseCtx, part.ToolInput)
- if errors.Is(err, ErrDoNotRespond) {
+ toolOut := tool.Run(toolUseCtx, part.ToolInput)
+ if errors.Is(toolOut.Error, ErrDoNotRespond) {
return
}
if toolUseCtx.Err() != nil {
@@ -493,11 +493,11 @@
return
}
- if err != nil {
- sendErr(err)
+ if toolOut.Error != nil {
+ sendErr(toolOut.Error)
return
}
- sendRes(toolResult)
+ sendRes(toolOut.LLMContent)
}()
}
wg.Wait()
diff --git a/llm/llm.go b/llm/llm.go
index 3192ba9..2c1011e 100644
--- a/llm/llm.go
+++ b/llm/llm.go
@@ -90,7 +90,18 @@
// The outputs from Run will be sent back to Claude.
// If you do not want to respond to the tool call request from Claude, return ErrDoNotRespond.
// ctx contains extra (rarely used) tool call information; retrieve it with ToolCallInfoFromContext.
- Run func(ctx context.Context, input json.RawMessage) ([]Content, error) `json:"-"`
+ Run func(ctx context.Context, input json.RawMessage) ToolOut `json:"-"`
+}
+
+// ToolOut represents the output of a tool run.
+type ToolOut struct {
+ // LLMContent is the output of the tool to be sent back to the LLM.
+ // May be nil on error.
+ LLMContent []Content
+ // Error is the error (if any) that occurred during the tool run.
+ // The text contents of the error will be sent back to the LLM.
+ // If non-nil, LLMContent will be ignored.
+ Error error
}
type Content struct {
@@ -270,3 +281,16 @@
Text: text,
}}
}
+
+func ErrorToolOut(err error) ToolOut {
+ if err == nil {
+ panic("ErrorToolOut called with nil error")
+ }
+ return ToolOut{
+ Error: err,
+ }
+}
+
+func ErrorfToolOut(format string, args ...any) ToolOut {
+ return ErrorToolOut(fmt.Errorf(format, args...))
+}