2026-05-10 00:02:23 +08:00
|
|
|
package tooling
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ToolResult struct {
|
|
|
|
|
Handled bool `json:"handled"`
|
|
|
|
|
Terminal bool `json:"terminal"`
|
|
|
|
|
Action string `json:"action"`
|
2026-08-28 22:23:13 +08:00
|
|
|
ReplyText string `json:"reply_text,omitempty"`
|
|
|
|
|
ReplySent bool `json:"reply_sent,omitempty"`
|
|
|
|
|
ShouldRetry bool `json:"should_retry"`
|
2026-05-10 00:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MarshalToolResult(result ToolResult) string {
|
|
|
|
|
result.Action = strings.TrimSpace(result.Action)
|
|
|
|
|
result.ReplyText = strings.TrimSpace(result.ReplyText)
|
|
|
|
|
buf, err := json.Marshal(result)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return string(buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseToolResult(raw string) (ToolResult, bool) {
|
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
|
if raw == "" {
|
|
|
|
|
return ToolResult{}, false
|
|
|
|
|
}
|
|
|
|
|
var result ToolResult
|
|
|
|
|
if err := json.Unmarshal([]byte(raw), &result); err != nil {
|
|
|
|
|
return ToolResult{}, false
|
|
|
|
|
}
|
|
|
|
|
result.Action = strings.TrimSpace(result.Action)
|
|
|
|
|
result.ReplyText = strings.TrimSpace(result.ReplyText)
|
|
|
|
|
if result.Action == "" && result.ReplyText == "" && !result.Handled && !result.Terminal {
|
|
|
|
|
return ToolResult{}, false
|
|
|
|
|
}
|
|
|
|
|
return result, true
|
|
|
|
|
}
|