feat: enhance tool result handling by adding reduction info parsing and updating trace item structure
This commit is contained in:
@@ -3,6 +3,8 @@ package adapter
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"cs-agent/internal/ai/mcps"
|
"cs-agent/internal/ai/mcps"
|
||||||
@@ -13,6 +15,14 @@ const (
|
|||||||
maxToolResultSegments = 12
|
maxToolResultSegments = 12
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var reductionInfoPattern = regexp.MustCompile(`\[tool result reduced: original_length=(\d+), kept_length=(\d+)\]`)
|
||||||
|
|
||||||
|
type ReductionInfo struct {
|
||||||
|
Reduced bool
|
||||||
|
OriginalChars int
|
||||||
|
KeptChars int
|
||||||
|
}
|
||||||
|
|
||||||
// BuildReducedToolResultSummary returns a bounded text summary for MCP tool results.
|
// BuildReducedToolResultSummary returns a bounded text summary for MCP tool results.
|
||||||
// It keeps the main payload visible to the model while preventing a single large tool
|
// It keeps the main payload visible to the model while preventing a single large tool
|
||||||
// response from exhausting too much context.
|
// response from exhausting too much context.
|
||||||
@@ -36,6 +46,23 @@ func BuildReducedToolResultSummary(result *mcps.ToolCallResult) string {
|
|||||||
return fmt.Sprintf("%s\n\n[tool result reduced: original_length=%d, kept_length=%d]", truncated, len(runes), maxToolResultSummaryChars)
|
return fmt.Sprintf("%s\n\n[tool result reduced: original_length=%d, kept_length=%d]", truncated, len(runes), maxToolResultSummaryChars)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseReductionInfo(summary string) ReductionInfo {
|
||||||
|
matches := reductionInfoPattern.FindStringSubmatch(strings.TrimSpace(summary))
|
||||||
|
if len(matches) != 3 {
|
||||||
|
return ReductionInfo{}
|
||||||
|
}
|
||||||
|
originalChars, err1 := strconv.Atoi(matches[1])
|
||||||
|
keptChars, err2 := strconv.Atoi(matches[2])
|
||||||
|
if err1 != nil || err2 != nil {
|
||||||
|
return ReductionInfo{}
|
||||||
|
}
|
||||||
|
return ReductionInfo{
|
||||||
|
Reduced: true,
|
||||||
|
OriginalChars: originalChars,
|
||||||
|
KeptChars: keptChars,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func collectToolResultSegments(result *mcps.ToolCallResult) []string {
|
func collectToolResultSegments(result *mcps.ToolCallResult) []string {
|
||||||
segments := make([]string, 0, len(result.Content)+2)
|
segments := make([]string, 0, len(result.Content)+2)
|
||||||
if result.IsError {
|
if result.IsError {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
impladapter "cs-agent/internal/ai/runtime/internal/impl/adapter"
|
||||||
"cs-agent/internal/pkg/toolx"
|
"cs-agent/internal/pkg/toolx"
|
||||||
|
|
||||||
einotool "github.com/cloudwego/eino/components/tool"
|
einotool "github.com/cloudwego/eino/components/tool"
|
||||||
@@ -43,6 +44,10 @@ func (h *RuntimeTraceHandler) WrapInvokableToolCall(_ context.Context, endpoint
|
|||||||
LatencyMs: time.Since(startedAt).Milliseconds(),
|
LatencyMs: time.Since(startedAt).Milliseconds(),
|
||||||
Status: "ok",
|
Status: "ok",
|
||||||
}
|
}
|
||||||
|
reductionInfo := impladapter.ParseReductionInfo(result)
|
||||||
|
item.ResultReduced = reductionInfo.Reduced
|
||||||
|
item.OriginalChars = reductionInfo.OriginalChars
|
||||||
|
item.KeptChars = reductionInfo.KeptChars
|
||||||
if tCtx != nil {
|
if tCtx != nil {
|
||||||
item.ToolName = strings.TrimSpace(tCtx.Name)
|
item.ToolName = strings.TrimSpace(tCtx.Name)
|
||||||
if metadata, ok := h.toolMetadataBy[item.ToolName]; ok {
|
if metadata, ok := h.toolMetadataBy[item.ToolName]; ok {
|
||||||
@@ -65,6 +70,9 @@ func (h *RuntimeTraceHandler) WrapInvokableToolCall(_ context.Context, endpoint
|
|||||||
ToolName: item.ToolName,
|
ToolName: item.ToolName,
|
||||||
Arguments: item.Arguments,
|
Arguments: item.Arguments,
|
||||||
ResultPreview: item.ResultPreview,
|
ResultPreview: item.ResultPreview,
|
||||||
|
ResultReduced: item.ResultReduced,
|
||||||
|
OriginalChars: item.OriginalChars,
|
||||||
|
KeptChars: item.KeptChars,
|
||||||
LatencyMs: item.LatencyMs,
|
LatencyMs: item.LatencyMs,
|
||||||
Status: item.Status,
|
Status: item.Status,
|
||||||
ErrorMessage: item.ErrorMessage,
|
ErrorMessage: item.ErrorMessage,
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ type ToolTraceItem struct {
|
|||||||
ToolName string `json:"toolName"`
|
ToolName string `json:"toolName"`
|
||||||
Arguments map[string]any `json:"arguments,omitempty"`
|
Arguments map[string]any `json:"arguments,omitempty"`
|
||||||
ResultPreview string `json:"resultPreview,omitempty"`
|
ResultPreview string `json:"resultPreview,omitempty"`
|
||||||
|
ResultReduced bool `json:"resultReduced,omitempty"`
|
||||||
|
OriginalChars int `json:"originalChars,omitempty"`
|
||||||
|
KeptChars int `json:"keptChars,omitempty"`
|
||||||
LatencyMs int64 `json:"latencyMs,omitempty"`
|
LatencyMs int64 `json:"latencyMs,omitempty"`
|
||||||
Status string `json:"status,omitempty"`
|
Status string `json:"status,omitempty"`
|
||||||
ErrorMessage string `json:"errorMessage,omitempty"`
|
ErrorMessage string `json:"errorMessage,omitempty"`
|
||||||
@@ -27,6 +30,9 @@ type GraphToolTraceItem struct {
|
|||||||
ToolName string `json:"toolName"`
|
ToolName string `json:"toolName"`
|
||||||
Arguments map[string]any `json:"arguments,omitempty"`
|
Arguments map[string]any `json:"arguments,omitempty"`
|
||||||
ResultPreview string `json:"resultPreview,omitempty"`
|
ResultPreview string `json:"resultPreview,omitempty"`
|
||||||
|
ResultReduced bool `json:"resultReduced,omitempty"`
|
||||||
|
OriginalChars int `json:"originalChars,omitempty"`
|
||||||
|
KeptChars int `json:"keptChars,omitempty"`
|
||||||
LatencyMs int64 `json:"latencyMs,omitempty"`
|
LatencyMs int64 `json:"latencyMs,omitempty"`
|
||||||
Status string `json:"status,omitempty"`
|
Status string `json:"status,omitempty"`
|
||||||
ErrorMessage string `json:"errorMessage,omitempty"`
|
ErrorMessage string `json:"errorMessage,omitempty"`
|
||||||
|
|||||||
Reference in New Issue
Block a user