fix: update grep_chunks tool to support multiple patterns

- Renamed the input parameter from "pattern" to "patterns" to reflect that multiple text patterns can be provided.
- Updated the schema definition and input structure accordingly to ensure consistency in handling multiple patterns.
- Enhanced error logging to specify "patterns" when no valid input is provided, improving clarity for debugging.

These changes improve the functionality of the grep_chunks tool by allowing users to search for multiple patterns simultaneously.
This commit is contained in:
wizardchen
2026-03-13 11:57:40 +08:00
parent 890dfb4bb3
commit f41b0e77fd
2 changed files with 23 additions and 13 deletions
+5 -5
View File
@@ -56,7 +56,7 @@ grep_chunks scans enabled chunks across the specified knowledge bases and return
schema: json.RawMessage(`{ schema: json.RawMessage(`{
"type": "object", "type": "object",
"properties": { "properties": {
"pattern": { "patterns": {
"type": "array", "type": "array",
"description": "REQUIRED: Text patterns to search for. Can be a single pattern or multiple patterns. Treated as literal text (fixed string matching). Results match any of the patterns (OR logic).", "description": "REQUIRED: Text patterns to search for. Can be a single pattern or multiple patterns. Treated as literal text (fixed string matching). Results match any of the patterns (OR logic).",
"items": { "items": {
@@ -79,13 +79,13 @@ grep_chunks scans enabled chunks across the specified knowledge bases and return
"maximum": 200 "maximum": 200
} }
}, },
"required": ["pattern"] "required": ["patterns"]
}`), }`),
} }
// GrepChunksInput defines the input parameters for grep chunks tool // GrepChunksInput defines the input parameters for grep chunks tool
type GrepChunksInput struct { type GrepChunksInput struct {
Pattern []string `json:"pattern" ` Patterns []string `json:"patterns" `
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"` KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
MaxResults int `json:"max_results,omitempty"` MaxResults int `json:"max_results,omitempty"`
} }
@@ -122,11 +122,11 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args json.RawMessage) (*ty
} }
// Parse pattern parameter (required) - support multiple patterns // Parse pattern parameter (required) - support multiple patterns
patterns := input.Pattern patterns := input.Patterns
// Validate patterns // Validate patterns
if len(patterns) == 0 { if len(patterns) == 0 {
logger.Errorf(ctx, "[Tool][GrepChunks] Missing or invalid pattern parameter") logger.Errorf(ctx, "[Tool][GrepChunks] Missing or invalid patterns parameter")
return &types.ToolResult{ return &types.ToolResult{
Success: false, Success: false,
Error: "pattern parameter is required and must contain at least one non-empty pattern", Error: "pattern parameter is required and must contain at least one non-empty pattern",
+18 -8
View File
@@ -3,6 +3,7 @@ package middleware
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"slices" "slices"
@@ -190,16 +191,25 @@ func Auth(
types.TenantInfoContextKey, t, types.TenantInfoContextKey, t,
) )
// 通过 TenantID 关联查询用户 // 通过 TenantID 关联查询用户;找不到时构造系统虚拟用户,
// 确保所有依赖 UserContextKey 的下游 handler 正常工作。
user, err := userService.GetUserByTenantID(c.Request.Context(), tenantID) user, err := userService.GetUserByTenantID(c.Request.Context(), tenantID)
if err == nil && user != nil { if err != nil || user == nil {
c.Set(types.UserContextKey.String(), user) user = &types.User{
c.Set(types.UserIDContextKey.String(), user.ID) ID: fmt.Sprintf("system-%d", tenantID),
ctx = context.WithValue( Username: fmt.Sprintf("system-%d", tenantID),
context.WithValue(ctx, types.UserContextKey, user), Email: fmt.Sprintf("system-%d@api-key.local", tenantID),
types.UserIDContextKey, user.ID, TenantID: tenantID,
) IsActive: true,
}
log.Printf("No user found for tenant %d via API key, using synthetic system user %s", tenantID, user.ID)
} }
c.Set(types.UserContextKey.String(), user)
c.Set(types.UserIDContextKey.String(), user.ID)
ctx = context.WithValue(
context.WithValue(ctx, types.UserContextKey, user),
types.UserIDContextKey, user.ID,
)
c.Request = c.Request.WithContext(ctx) c.Request = c.Request.WithContext(ctx)
c.Next() c.Next()