Refactor tool output format

This commit is contained in:
dwrz
2026-07-08 11:52:45 +00:00
parent 6592bae15d
commit 6c4b74fd6d
3 changed files with 285 additions and 33 deletions

View File

@@ -2,11 +2,10 @@ package llm
import (
"context"
"fmt"
"log/slog"
"runtime"
"strings"
"sync"
"log/slog"
)
// callTools executes each tool call in the message concurrently and returns
@@ -41,26 +40,31 @@ func (c *Client) callTools(ctx context.Context, msg Message) []Message {
slog.Any("error", err),
slog.String("name", tc.Function.Name),
)
result = fmt.Sprintf(
`{"ok": false, "error": %q}`, err,
)
} else {
c.log.InfoContext(
ctx,
"called tool",
slog.String("name", tc.Function.Name),
)
results[idx] = Message{
Role: RoleTool,
ContentParts: []ContentPart{{
Type: ContentTypeText,
Text: err.Error(),
}},
Name: tc.Function.Name,
ToolCallID: tc.ID,
}
return
}
// Content cannot be empty.
if strings.TrimSpace(result) == "" {
result = `{"ok": true, "result": null}`
}
c.log.InfoContext(
ctx,
"called tool",
slog.String("name", tc.Function.Name),
)
formatted := result.FormatResult()
results[idx] = Message{
Role: RoleTool,
ContentParts: []ContentPart{{
Type: ContentTypeText, Text: result,
Type: ContentTypeText,
Text: formatted,
}},
Name: tc.Function.Name,
ToolCallID: tc.ID,