refactor(agent): remove query display from DatabaseQuery component and update tool result structure
- Removed the query display section from the DatabaseQuery component to streamline the UI. - Updated the DatabaseQueryData interface to eliminate the query field, reflecting changes in tool result handling. - Enhanced the DatabaseQuery tool execution to focus on returning structured results without exposing raw SQL queries. - Added a new utility to strip <think> blocks from LLM outputs, improving content clarity and user experience.
This commit is contained in:
@@ -236,7 +236,7 @@ func (t *DatabaseQueryTool) Execute(ctx context.Context, args json.RawMessage) (
|
||||
|
||||
// Format output
|
||||
logger.Debugf(ctx, "[Tool][DatabaseQuery] Formatting query results...")
|
||||
output := t.formatQueryResults(columns, results, securedSQL)
|
||||
output := t.formatQueryResults(columns, results)
|
||||
|
||||
logger.Infof(ctx, "[Tool][DatabaseQuery] Execute completed successfully: %d rows returned", len(results))
|
||||
return &types.ToolResult{
|
||||
@@ -246,8 +246,6 @@ func (t *DatabaseQueryTool) Execute(ctx context.Context, args json.RawMessage) (
|
||||
"columns": columns,
|
||||
"rows": results,
|
||||
"row_count": len(results),
|
||||
"query": securedSQL,
|
||||
"tenant_id": tenantID,
|
||||
"display_type": "database_query",
|
||||
},
|
||||
}, nil
|
||||
@@ -255,11 +253,11 @@ func (t *DatabaseQueryTool) Execute(ctx context.Context, args json.RawMessage) (
|
||||
|
||||
// validateAndSecureSQL validates the SQL query and injects tenant_id conditions
|
||||
func (t *DatabaseQueryTool) validateAndSecureSQL(sqlQuery string, tenantID uint64) (string, error) {
|
||||
// Use the new ValidateAndSecureSQL with comprehensive security options
|
||||
securedSQL, validationResult, err := utils.ValidateAndSecureSQL(
|
||||
sqlQuery,
|
||||
utils.WithSecurityDefaults(tenantID),
|
||||
utils.WithSoftDeleteFilter("knowledge_bases", "knowledges", "chunks"),
|
||||
utils.WithHiddenKBFilter(),
|
||||
utils.WithInjectionRiskCheck(),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -267,7 +265,6 @@ func (t *DatabaseQueryTool) validateAndSecureSQL(sqlQuery string, tenantID uint6
|
||||
}
|
||||
|
||||
if !validationResult.Valid {
|
||||
// Build error message from validation errors
|
||||
var errMsgs []string
|
||||
for _, valErr := range validationResult.Errors {
|
||||
errMsgs = append(errMsgs, fmt.Sprintf("%s: %s", valErr.Type, valErr.Message))
|
||||
@@ -282,10 +279,8 @@ func (t *DatabaseQueryTool) validateAndSecureSQL(sqlQuery string, tenantID uint6
|
||||
func (t *DatabaseQueryTool) formatQueryResults(
|
||||
columns []string,
|
||||
results []map[string]interface{},
|
||||
query string,
|
||||
) string {
|
||||
output := "=== Query Results ===\n\n"
|
||||
output += fmt.Sprintf("Executed SQL: %s\n\n", query)
|
||||
output += fmt.Sprintf("Returned %d rows\n\n", len(results))
|
||||
|
||||
if len(results) == 0 {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package tools
|
||||
|
||||
import "regexp"
|
||||
|
||||
// thinkBlockRe matches <think>…</think> blocks that some models embed in content.
|
||||
// Uses (?s) flag so . matches newlines.
|
||||
var thinkBlockRe = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
||||
|
||||
// StripThinkBlocks removes <think>…</think> blocks from LLM output content.
|
||||
// Some models (DeepSeek, Qwen, etc.) embed chain-of-thought reasoning inside
|
||||
// <think> tags in the content field. These should be stripped before:
|
||||
// - Displaying content to users
|
||||
// - Storing content in agent state / context manager
|
||||
// - Emitting content via EventBus
|
||||
//
|
||||
// Returns the cleaned string, or empty string if input is empty or becomes empty.
|
||||
func StripThinkBlocks(content string) string {
|
||||
if content == "" {
|
||||
return ""
|
||||
}
|
||||
cleaned := thinkBlockRe.ReplaceAllString(content, "")
|
||||
// Trim leading/trailing whitespace that may remain after removal
|
||||
result := trimWhitespace(cleaned)
|
||||
return result
|
||||
}
|
||||
|
||||
// trimWhitespace trims leading and trailing whitespace without importing strings.
|
||||
func trimWhitespace(s string) string {
|
||||
start, end := 0, len(s)
|
||||
for start < end && (s[start] == ' ' || s[start] == '\n' || s[start] == '\r' || s[start] == '\t') {
|
||||
start++
|
||||
}
|
||||
for end > start && (s[end-1] == ' ' || s[end-1] == '\n' || s[end-1] == '\r' || s[end-1] == '\t') {
|
||||
end--
|
||||
}
|
||||
return s[start:end]
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package tools
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestStripThinkBlocks(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "no think blocks",
|
||||
input: "Hello, world!",
|
||||
expected: "Hello, world!",
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
input: "",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "single think block",
|
||||
input: "<think>Let me reason about this</think>The answer is 42.",
|
||||
expected: "The answer is 42.",
|
||||
},
|
||||
{
|
||||
name: "multiline think block",
|
||||
input: "<think>\nStep 1: analyze\nStep 2: solve\n</think>\nHere is my answer.",
|
||||
expected: "Here is my answer.",
|
||||
},
|
||||
{
|
||||
name: "multiple think blocks",
|
||||
input: "<think>first</think>Answer part 1. <think>second</think>Answer part 2.",
|
||||
expected: "Answer part 1. Answer part 2.",
|
||||
},
|
||||
{
|
||||
name: "only think block",
|
||||
input: "<think>just thinking</think>",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "think block with surrounding whitespace",
|
||||
input: " <think>reasoning</think> Answer ",
|
||||
expected: "Answer",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := StripThinkBlocks(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("StripThinkBlocks(%q) = %q, want %q", tt.input, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user