From 44a578e824a715251b9c4261e2335f6ac42ecf90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E6=98=8E?= <83812544+Ed1s0nZ@users.noreply.github.com> Date: Sat, 15 Aug 2026 01:37:28 +0800 Subject: [PATCH] Add files via upload --- .../database/tool_execution_args_lookup.go | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 internal/database/tool_execution_args_lookup.go diff --git a/internal/database/tool_execution_args_lookup.go b/internal/database/tool_execution_args_lookup.go new file mode 100644 index 00000000..f2583359 --- /dev/null +++ b/internal/database/tool_execution_args_lookup.go @@ -0,0 +1,53 @@ +package database + +import ( + "database/sql" + "encoding/json" + "fmt" + "strings" + "time" +) + +// FindNearestToolExecutionArguments returns the arguments for the execution record +// closest to a persisted tool_call detail. Eino can persist a tool_call with empty +// model arguments while the monitor execution row still has the real command/URL. +func (db *DB) FindNearestToolExecutionArguments(conversationID, toolName string, at time.Time, window time.Duration) (string, map[string]interface{}, error) { + conversationID = strings.TrimSpace(conversationID) + toolName = strings.TrimSpace(toolName) + if db == nil || conversationID == "" || toolName == "" || at.IsZero() { + return "", nil, sql.ErrNoRows + } + if window <= 0 { + window = 5 * time.Second + } + start := at.Add(-window) + end := at.Add(window) + rows, err := db.Query(` +SELECT id, arguments +FROM tool_executions +WHERE conversation_id = ? + AND tool_name = ? + AND julianday(start_time) BETWEEN julianday(?) AND julianday(?) +ORDER BY ABS(julianday(start_time) - julianday(?)) ASC, start_time ASC +LIMIT 1`, conversationID, toolName, start, end, at) + if err != nil { + return "", nil, err + } + defer rows.Close() + if !rows.Next() { + if err := rows.Err(); err != nil { + return "", nil, err + } + return "", nil, sql.ErrNoRows + } + var id string + var raw string + if err := rows.Scan(&id, &raw); err != nil { + return "", nil, err + } + var args map[string]interface{} + if err := json.Unmarshal([]byte(raw), &args); err != nil { + return "", nil, fmt.Errorf("parse tool execution arguments: %w", err) + } + return strings.TrimSpace(id), args, nil +}