refactor: Enhance AgentQARequest structure with additional fields and improve session handling methods for clarity and functionality
This commit is contained in:
+31
-8
@@ -12,9 +12,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AgentQARequest agent Q&A request
|
// AgentQARequest agent Q&A request payload.
|
||||||
type AgentQARequest struct {
|
type AgentQARequest struct {
|
||||||
Query string `json:"query"`
|
Query string `json:"query"` // Required query text
|
||||||
|
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"` // Optional KBs for this query
|
||||||
|
AgentEnabled bool `json:"agent_enabled"` // Whether to run in agent mode
|
||||||
|
WebSearchEnabled bool `json:"web_search_enabled"` // Whether to enable web search
|
||||||
|
SummaryModelID string `json:"summary_model_id,omitempty"` // Optional summary model override
|
||||||
|
MCPServiceIDs []string `json:"mcp_service_ids,omitempty"` // Optional MCP service allow list
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentResponseType defines the type of agent response
|
// AgentResponseType defines the type of agent response
|
||||||
@@ -44,15 +49,28 @@ type AgentStreamResponse struct {
|
|||||||
// Return error to stop processing the stream
|
// Return error to stop processing the stream
|
||||||
type AgentEventCallback func(*AgentStreamResponse) error
|
type AgentEventCallback func(*AgentStreamResponse) error
|
||||||
|
|
||||||
// AgentQAStream performs agent-based Q&A with SSE streaming
|
// AgentQAStream performs agent-based Q&A with SSE streaming using default agent settings.
|
||||||
// The callback will be invoked for each event received from the server
|
// Deprecated: prefer AgentQAStreamWithRequest to customize agent behavior.
|
||||||
func (c *Client) AgentQAStream(ctx context.Context, sessionID string, query string, callback AgentEventCallback) error {
|
func (c *Client) AgentQAStream(ctx context.Context, sessionID string, query string, callback AgentEventCallback) error {
|
||||||
path := fmt.Sprintf("/api/v1/agent-chat/%s", sessionID)
|
req := &AgentQARequest{
|
||||||
|
Query: query,
|
||||||
|
AgentEnabled: true,
|
||||||
|
}
|
||||||
|
return c.AgentQAStreamWithRequest(ctx, sessionID, req, callback)
|
||||||
|
}
|
||||||
|
|
||||||
request := &AgentQARequest{
|
// AgentQAStreamWithRequest performs agent-based Q&A with SSE streaming using the full request payload.
|
||||||
Query: query,
|
func (c *Client) AgentQAStreamWithRequest(ctx context.Context,
|
||||||
|
sessionID string, request *AgentQARequest, callback AgentEventCallback,
|
||||||
|
) error {
|
||||||
|
if request == nil {
|
||||||
|
return fmt.Errorf("agent QA request cannot be nil")
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(request.Query) == "" {
|
||||||
|
return fmt.Errorf("agent QA query cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path := fmt.Sprintf("/api/v1/agent-chat/%s", sessionID)
|
||||||
resp, err := c.doRequest(ctx, http.MethodPost, path, request, nil)
|
resp, err := c.doRequest(ctx, http.MethodPost, path, request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("request failed: %w", err)
|
return fmt.Errorf("request failed: %w", err)
|
||||||
@@ -126,11 +144,16 @@ func (c *Client) NewAgentSession(sessionID string) *AgentSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ask sends a query to the agent and processes the streaming response
|
// Ask sends a query to the agent with default agent-enabled behavior.
|
||||||
func (as *AgentSession) Ask(ctx context.Context, query string, callback AgentEventCallback) error {
|
func (as *AgentSession) Ask(ctx context.Context, query string, callback AgentEventCallback) error {
|
||||||
return as.client.AgentQAStream(ctx, as.sessionID, query, callback)
|
return as.client.AgentQAStream(ctx, as.sessionID, query, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AskWithRequest sends a customized agent request for this session.
|
||||||
|
func (as *AgentSession) AskWithRequest(ctx context.Context, request *AgentQARequest, callback AgentEventCallback) error {
|
||||||
|
return as.client.AgentQAStreamWithRequest(ctx, as.sessionID, request, callback)
|
||||||
|
}
|
||||||
|
|
||||||
// GetSessionID returns the session ID
|
// GetSessionID returns the session ID
|
||||||
func (as *AgentSession) GetSessionID() string {
|
func (as *AgentSession) GetSessionID() string {
|
||||||
return as.sessionID
|
return as.sessionID
|
||||||
|
|||||||
+237
@@ -0,0 +1,237 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FAQEntry represents a FAQ item stored under a knowledge base.
|
||||||
|
type FAQEntry struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
ChunkID string `json:"chunk_id"`
|
||||||
|
KnowledgeID string `json:"knowledge_id"`
|
||||||
|
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||||
|
TagID string `json:"tag_id"`
|
||||||
|
IsEnabled bool `json:"is_enabled"`
|
||||||
|
StandardQuestion string `json:"standard_question"`
|
||||||
|
SimilarQuestions []string `json:"similar_questions"`
|
||||||
|
NegativeQuestions []string `json:"negative_questions"`
|
||||||
|
Answers []string `json:"answers"`
|
||||||
|
IndexMode string `json:"index_mode"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
Score float64 `json:"score,omitempty"`
|
||||||
|
MatchType string `json:"match_type,omitempty"`
|
||||||
|
ChunkType string `json:"chunk_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQEntryPayload is used to create or update a FAQ entry.
|
||||||
|
type FAQEntryPayload struct {
|
||||||
|
StandardQuestion string `json:"standard_question"`
|
||||||
|
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
||||||
|
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
||||||
|
Answers []string `json:"answers"`
|
||||||
|
TagID string `json:"tag_id,omitempty"`
|
||||||
|
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQBatchUpsertPayload represents the request body for batch import (append/replace).
|
||||||
|
type FAQBatchUpsertPayload struct {
|
||||||
|
Entries []FAQEntryPayload `json:"entries"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
KnowledgeID string `json:"knowledge_id,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQEntryStatusBatchRequest toggles enable states in bulk.
|
||||||
|
type FAQEntryStatusBatchRequest struct {
|
||||||
|
Updates map[string]bool `json:"updates"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQEntryTagBatchRequest updates tags in bulk.
|
||||||
|
type FAQEntryTagBatchRequest struct {
|
||||||
|
Updates map[string]*string `json:"updates"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQDeleteRequest deletes entries in bulk.
|
||||||
|
type FAQDeleteRequest struct {
|
||||||
|
IDs []string `json:"ids"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQSearchRequest represents the hybrid FAQ search request.
|
||||||
|
type FAQSearchRequest struct {
|
||||||
|
QueryText string `json:"query_text"`
|
||||||
|
VectorThreshold float64 `json:"vector_threshold"`
|
||||||
|
MatchCount int `json:"match_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQEntriesPage contains paginated FAQ results.
|
||||||
|
type FAQEntriesPage struct {
|
||||||
|
Total int64 `json:"total"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
PageSize int `json:"page_size"`
|
||||||
|
Entries []FAQEntry `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQEntriesResponse wraps the paginated FAQ response.
|
||||||
|
type FAQEntriesResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Data *FAQEntriesPage `json:"data"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Code string `json:"code,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQUpsertResponse wraps the asynchronous import response.
|
||||||
|
type FAQUpsertResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Data *FAQTaskPayload `json:"data"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Code string `json:"code,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQTaskPayload carries the task identifier for async imports.
|
||||||
|
type FAQTaskPayload struct {
|
||||||
|
TaskID string `json:"task_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FAQSearchResponse wraps the hybrid FAQ search results.
|
||||||
|
type FAQSearchResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Data []FAQEntry `json:"data"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Code string `json:"code,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type faqSimpleResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Code string `json:"code,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListFAQEntries returns paginated FAQ entries under a knowledge base.
|
||||||
|
func (c *Client) ListFAQEntries(ctx context.Context,
|
||||||
|
knowledgeBaseID string, page, pageSize int, tagID string,
|
||||||
|
) (*FAQEntriesPage, error) {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
|
||||||
|
query := url.Values{}
|
||||||
|
if page > 0 {
|
||||||
|
query.Add("page", strconv.Itoa(page))
|
||||||
|
}
|
||||||
|
if pageSize > 0 {
|
||||||
|
query.Add("page_size", strconv.Itoa(pageSize))
|
||||||
|
}
|
||||||
|
if tagID != "" {
|
||||||
|
query.Add("tag_id", tagID)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response FAQEntriesResponse
|
||||||
|
if err := parseResponse(resp, &response); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if response.Data == nil {
|
||||||
|
return &FAQEntriesPage{}, nil
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpsertFAQEntries imports or appends FAQ entries asynchronously and returns the task ID.
|
||||||
|
func (c *Client) UpsertFAQEntries(ctx context.Context,
|
||||||
|
knowledgeBaseID string, payload *FAQBatchUpsertPayload,
|
||||||
|
) (string, error) {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodPost, path, payload, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response FAQUpsertResponse
|
||||||
|
if err := parseResponse(resp, &response); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if response.Data == nil {
|
||||||
|
return "", fmt.Errorf("missing task information in response")
|
||||||
|
}
|
||||||
|
return response.Data.TaskID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateFAQEntry updates a single FAQ entry.
|
||||||
|
func (c *Client) UpdateFAQEntry(ctx context.Context,
|
||||||
|
knowledgeBaseID, entryID string, payload *FAQEntryPayload,
|
||||||
|
) error {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodPut, path, payload, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response faqSimpleResponse
|
||||||
|
return parseResponse(resp, &response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateFAQEntryStatusBatch enables/disables FAQ entries in bulk.
|
||||||
|
func (c *Client) UpdateFAQEntryStatusBatch(ctx context.Context,
|
||||||
|
knowledgeBaseID string, updates map[string]bool,
|
||||||
|
) error {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/status", knowledgeBaseID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodPut, path, &FAQEntryStatusBatchRequest{Updates: updates}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response faqSimpleResponse
|
||||||
|
return parseResponse(resp, &response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateFAQEntryTagBatch updates FAQ entry tags in bulk.
|
||||||
|
func (c *Client) UpdateFAQEntryTagBatch(ctx context.Context,
|
||||||
|
knowledgeBaseID string, updates map[string]*string,
|
||||||
|
) error {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/tags", knowledgeBaseID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodPut, path, &FAQEntryTagBatchRequest{Updates: updates}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response faqSimpleResponse
|
||||||
|
return parseResponse(resp, &response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteFAQEntries deletes FAQ entries in bulk.
|
||||||
|
func (c *Client) DeleteFAQEntries(ctx context.Context,
|
||||||
|
knowledgeBaseID string, ids []string,
|
||||||
|
) error {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodDelete, path, &FAQDeleteRequest{IDs: ids}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response faqSimpleResponse
|
||||||
|
return parseResponse(resp, &response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchFAQEntries performs hybrid FAQ search inside a knowledge base.
|
||||||
|
func (c *Client) SearchFAQEntries(ctx context.Context,
|
||||||
|
knowledgeBaseID string, payload *FAQSearchRequest,
|
||||||
|
) ([]FAQEntry, error) {
|
||||||
|
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/search", knowledgeBaseID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodPost, path, payload, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response FAQSearchResponse
|
||||||
|
if err := parseResponse(resp, &response); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
+34
-6
@@ -49,13 +49,11 @@ type SummaryConfig struct {
|
|||||||
MaxCompletionTokens int `json:"max_completion_tokens"`
|
MaxCompletionTokens int `json:"max_completion_tokens"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentConfig defines agent configuration for agent mode
|
// AgentConfig defines session-level agent configuration (matches server struct).
|
||||||
type AgentConfig struct {
|
type AgentConfig struct {
|
||||||
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
|
AgentModeEnabled bool `json:"agent_mode_enabled"` // Whether agent mode is enabled for this session
|
||||||
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
|
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search is enabled for this session
|
||||||
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
|
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
|
||||||
Temperature float64 `json:"temperature"` // LLM temperature for agent
|
|
||||||
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateSessionRequest session creation request
|
// CreateSessionRequest session creation request
|
||||||
@@ -195,6 +193,11 @@ type GenerateTitleResponse struct {
|
|||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StopSessionRequest stop generation payload.
|
||||||
|
type StopSessionRequest struct {
|
||||||
|
MessageID string `json:"message_id"`
|
||||||
|
}
|
||||||
|
|
||||||
// GenerateTitle generates a session title
|
// GenerateTitle generates a session title
|
||||||
func (c *Client) GenerateTitle(ctx context.Context, sessionID string, request *GenerateTitleRequest) (string, error) {
|
func (c *Client) GenerateTitle(ctx context.Context, sessionID string, request *GenerateTitleRequest) (string, error) {
|
||||||
path := fmt.Sprintf("/api/v1/sessions/%s/generate_title", sessionID)
|
path := fmt.Sprintf("/api/v1/sessions/%s/generate_title", sessionID)
|
||||||
@@ -372,6 +375,31 @@ func (c *Client) ContinueStream(ctx context.Context, sessionID string, messageID
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StopSession stops the generation for a specific assistant message under a session.
|
||||||
|
func (c *Client) StopSession(ctx context.Context, sessionID string, messageID string) error {
|
||||||
|
if strings.TrimSpace(sessionID) == "" {
|
||||||
|
return fmt.Errorf("sessionID cannot be empty")
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(messageID) == "" {
|
||||||
|
return fmt.Errorf("messageID cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
path := fmt.Sprintf("/api/v1/sessions/%s/stop", sessionID)
|
||||||
|
resp, err := c.doRequest(ctx, http.MethodPost, path, &StopSessionRequest{
|
||||||
|
MessageID: messageID,
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var response struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseResponse(resp, &response)
|
||||||
|
}
|
||||||
|
|
||||||
// SearchKnowledgeRequest knowledge search request
|
// SearchKnowledgeRequest knowledge search request
|
||||||
type SearchKnowledgeRequest struct {
|
type SearchKnowledgeRequest struct {
|
||||||
Query string `json:"query"` // Query content
|
Query string `json:"query"` // Query content
|
||||||
|
|||||||
@@ -193,29 +193,26 @@ export interface WebFetchResultsData {
|
|||||||
count?: number;
|
count?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grep chunk result item
|
// Grep knowledge aggregation item
|
||||||
export interface GrepChunkItem {
|
export interface GrepKnowledgeResult {
|
||||||
id: string;
|
|
||||||
content: string;
|
|
||||||
chunk_index: number;
|
|
||||||
knowledge_id: string;
|
knowledge_id: string;
|
||||||
knowledge_base_id: string;
|
knowledge_base_id: string;
|
||||||
chunk_type?: string;
|
knowledge_title: string;
|
||||||
knowledge_title?: string;
|
chunk_hit_count: number;
|
||||||
|
pattern_counts: Record<string, number>;
|
||||||
|
total_pattern_hits: number;
|
||||||
|
distinct_patterns: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grep results data
|
// Grep results data
|
||||||
export interface GrepResultsData {
|
export interface GrepResultsData {
|
||||||
display_type: 'grep_results';
|
display_type: 'grep_results';
|
||||||
patterns: string[];
|
patterns: string[];
|
||||||
case_sensitive: boolean;
|
knowledge_results: GrepKnowledgeResult[];
|
||||||
use_regex: boolean;
|
|
||||||
results: GrepChunkItem[];
|
|
||||||
result_count: number;
|
result_count: number;
|
||||||
total_matches: number;
|
total_matches: number;
|
||||||
knowledge_base_ids?: string[];
|
knowledge_base_ids?: string[];
|
||||||
knowledge_ids?: string[];
|
max_results: number;
|
||||||
limit: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Union type for all tool result data
|
// Union type for all tool result data
|
||||||
|
|||||||
@@ -1,38 +1,16 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="grep-results">
|
<div class="grep-results">
|
||||||
<!-- Results List -->
|
<div v-if="results.length" class="results-list">
|
||||||
<div v-if="results && results.length > 0" class="results-list">
|
|
||||||
<div
|
<div
|
||||||
v-for="(result, index) in results"
|
v-for="(result, index) in results"
|
||||||
:key="result.id"
|
:key="result.knowledge_id"
|
||||||
class="result-item"
|
class="result-row"
|
||||||
>
|
>
|
||||||
<t-popup
|
<div class="result-row__index">#{{ index + 1 }}</div>
|
||||||
:overlayClassName="`grep-popup-${result.id}`"
|
<div class="result-row__title">{{ result.knowledge_title || 'Untitled' }}</div>
|
||||||
placement="bottom-left"
|
|
||||||
width="400"
|
|
||||||
:showArrow="false"
|
|
||||||
trigger="click"
|
|
||||||
destroy-on-close
|
|
||||||
>
|
|
||||||
<template #content>
|
|
||||||
<ContentPopup
|
|
||||||
:content="result.content"
|
|
||||||
:chunk-id="result.id"
|
|
||||||
:knowledge-id="result.knowledge_id"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
<div class="result-header">
|
|
||||||
<div class="result-title">
|
|
||||||
<span class="result-index">#{{ index + 1 }}</span>
|
|
||||||
<span class="knowledge-title">{{ result.knowledge_title || 'Untitled' }}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</t-popup>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Empty State -->
|
|
||||||
<div v-else class="empty-state">
|
<div v-else class="empty-state">
|
||||||
未找到匹配的内容
|
未找到匹配的内容
|
||||||
</div>
|
</div>
|
||||||
@@ -42,22 +20,20 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed } from 'vue';
|
import { computed } from 'vue';
|
||||||
import type { GrepResultsData } from '@/types/tool-results';
|
import type { GrepResultsData } from '@/types/tool-results';
|
||||||
import ContentPopup from './ContentPopup.vue';
|
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
data: GrepResultsData;
|
data: GrepResultsData;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
const maxDisplayPatterns = 2;
|
const patterns = computed(() => props.data.patterns ?? []);
|
||||||
|
const results = computed(() => props.data.knowledge_results ?? []);
|
||||||
|
|
||||||
const displayPatterns = computed(() => {
|
const resultCount = computed(() => props.data.result_count ?? results.value.length);
|
||||||
if (!props.data.patterns || props.data.patterns.length === 0) {
|
const totalMatches = computed(() => props.data.total_matches ?? results.value.length);
|
||||||
return [];
|
const maxResults = computed(() => props.data.max_results ?? results.value.length);
|
||||||
}
|
const hasMoreResults = computed(() => totalMatches.value > resultCount.value);
|
||||||
return props.data.patterns.slice(0, maxDisplayPatterns);
|
|
||||||
});
|
|
||||||
|
|
||||||
const results = computed(() => props.data.results || []);
|
// Compact view, no per-pattern stats
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="less" scoped>
|
<style lang="less" scoped>
|
||||||
@@ -66,98 +42,72 @@ const results = computed(() => props.data.results || []);
|
|||||||
.grep-results {
|
.grep-results {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 3px;
|
gap: 8px;
|
||||||
padding: 0 0 0 12px;
|
}
|
||||||
|
|
||||||
|
.summary-inline {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #6b7280;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
|
||||||
|
&__divider {
|
||||||
|
color: #d1d5db;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__truncated {
|
||||||
|
color: #f59e0b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.results-list {
|
.results-list {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 3px;
|
gap: 4px;
|
||||||
|
padding: 10px 12px 10px 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.result-item {
|
.result-row {
|
||||||
background: transparent;
|
display: grid;
|
||||||
border: none;
|
grid-template-columns: 40px minmax(120px, 1fr) auto;
|
||||||
border-radius: 0;
|
|
||||||
overflow: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-header {
|
|
||||||
padding: 2px 0;
|
|
||||||
cursor: pointer;
|
|
||||||
user-select: none;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 6px;
|
gap: 8px;
|
||||||
transition: color 0.15s ease;
|
padding: 4px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
&:hover {
|
background: #f9fafb;
|
||||||
color: #07c05f;
|
border: 1px solid #edf2f7;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.result-title {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 6px;
|
|
||||||
flex: 1;
|
|
||||||
min-width: 0;
|
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
line-height: 1.4;
|
line-height: 1.4;
|
||||||
}
|
}
|
||||||
|
|
||||||
.result-index {
|
.result-row__index {
|
||||||
font-size: 11px;
|
|
||||||
color: #9ca3af;
|
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
flex-shrink: 0;
|
color: #9ca3af;
|
||||||
}
|
}
|
||||||
|
|
||||||
.pattern-badge {
|
.result-row__title {
|
||||||
display: inline-flex;
|
color: #111827;
|
||||||
align-items: center;
|
|
||||||
gap: 3px;
|
|
||||||
flex-shrink: 0;
|
|
||||||
|
|
||||||
.pattern-text {
|
|
||||||
font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
|
|
||||||
font-size: 10px;
|
|
||||||
background: #f3f4f6;
|
|
||||||
color: #6b7280;
|
|
||||||
padding: 2px 5px;
|
|
||||||
border-radius: 3px;
|
|
||||||
white-space: nowrap;
|
|
||||||
font-weight: 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
.more-patterns {
|
|
||||||
font-size: 10px;
|
|
||||||
color: #9ca3af;
|
|
||||||
padding: 2px 4px;
|
|
||||||
background: #f3f4f6;
|
|
||||||
border-radius: 3px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.knowledge-title {
|
|
||||||
font-size: 12px;
|
|
||||||
color: #374151;
|
|
||||||
flex: 1;
|
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
min-width: 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.chunk-info {
|
.result-row__meta {
|
||||||
font-size: 10px;
|
display: flex;
|
||||||
color: #9ca3af;
|
gap: 6px;
|
||||||
background: #f3f4f6;
|
flex-wrap: wrap;
|
||||||
padding: 2px 5px;
|
justify-content: flex-end;
|
||||||
border-radius: 3px;
|
}
|
||||||
flex-shrink: 0;
|
|
||||||
|
.meta-pill {
|
||||||
|
font-size: 11px;
|
||||||
|
color: #6b7280;
|
||||||
|
background: #fff;
|
||||||
|
border: 1px solid #e5e7eb;
|
||||||
|
border-radius: 999px;
|
||||||
|
padding: 2px 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.empty-state {
|
.empty-state {
|
||||||
@@ -166,20 +116,8 @@ const results = computed(() => props.data.results || []);
|
|||||||
color: #9ca3af;
|
color: #9ca3af;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
background: #f9fafb;
|
||||||
|
border-radius: 6px;
|
||||||
// Popup overlay styles
|
border: 1px dashed #e5e7eb;
|
||||||
:deep([class*="grep-popup-"]) {
|
|
||||||
.t-popup__content {
|
|
||||||
max-height: 400px;
|
|
||||||
max-width: 500px;
|
|
||||||
overflow-y: auto;
|
|
||||||
overflow-x: hidden;
|
|
||||||
padding: 0;
|
|
||||||
border-radius: 6px;
|
|
||||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
|
|
||||||
word-wrap: break-word;
|
|
||||||
word-break: break-word;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
+94
-91
@@ -240,70 +240,67 @@ func BuildProgressiveRAGSystemPrompt(knowledgeBases []*KnowledgeBaseInfo, webSea
|
|||||||
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
|
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
|
||||||
// This version emphasizes hybrid retrieval strategy: KB-first with web supplementation
|
// This version emphasizes hybrid retrieval strategy: KB-first with web supplementation
|
||||||
var ProgressiveRAGSystemPromptWithWeb = `### Role
|
var ProgressiveRAGSystemPromptWithWeb = `### Role
|
||||||
You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. You operate in a multi-tenant environment with strictly isolated knowledge bases (Document-based & FAQ-based). Your core philosophy is "Evidence-First": you never rely on internal parametric knowledge but construct answers solely from retrieved verified data.
|
You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. You operate in a multi-tenant environment with strictly isolated knowledge bases. Your core philosophy is "Evidence-First": you never rely on internal parametric knowledge but construct answers solely from verified data retrieved from the Knowledge Base (KB) or Web.
|
||||||
|
|
||||||
### Mission
|
### Mission
|
||||||
To deliver accurate, traceable, and verifiable answers by orchestrating a dynamic retrieval process. You must first gauge the information landscape through preliminary retrieval, then rigorously execute and reflect upon specific research tasks until the user's intent is fully satisfied.
|
To deliver accurate, traceable, and verifiable answers by orchestrating a dynamic retrieval process. You must first gauge the information landscape through preliminary retrieval, then rigorously execute and reflect upon specific research tasks. **You prioritize "Deep Reading" over superficial scanning.**
|
||||||
|
|
||||||
### Critical Constraints (ABSOLUTE RULES)
|
### Critical Constraints (ABSOLUTE RULES)
|
||||||
1. **NO Internal Knowledge:** You must behave as if your training data does not exist regarding facts. If it's not in the retrieved content (KB or Web), it does not exist.
|
1. **NO Internal Knowledge:** You must behave as if your training data does not exist regarding facts.
|
||||||
2. **KB First, Web Second:** Always exhaust Knowledge Base retrieval strategies before attempting Web Search. Web Search is a fallback for missing or outdated data, not the default.
|
2. **Mandatory Deep Read:** Whenever grep_chunks or knowledge_search returns matched knowledge_ids or chunk_ids, you **MUST** immediately call list_knowledge_chunks to read the full content of those specific chunks. Do not rely on search snippets alone.
|
||||||
3. **Strict Plan Adherence:** If a todo_write plan exists, you must follow it sequentially. You cannot skip steps or summarize until all tasks are explicitly marked "completed".
|
3. **KB First, Web Second:** Always exhaust KB strategies (including the Deep Read) before attempting Web Search.
|
||||||
4. **Tool Privacy:** Never expose tool names or internal mechanics to the user.
|
4. **Strict Plan Adherence:** If a todo_write plan exists, execute it sequentially. No skipping.
|
||||||
|
5. **Tool Privacy:** Never expose tool names to the user.
|
||||||
|
|
||||||
### Workflow: The "Reconnaissance-Plan-Execute" Cycle
|
### Workflow: The "Reconnaissance-Plan-Execute" Cycle
|
||||||
|
|
||||||
You must follow this **Specific Operational Sequence** for every user query:
|
|
||||||
|
|
||||||
#### Phase 1: Preliminary Reconnaissance (Mandatory Initial Step)
|
#### Phase 1: Preliminary Reconnaissance (Mandatory Initial Step)
|
||||||
Before answering or creating a complex plan, you MUST perform an initial "test" of the knowledge base to gain preliminary cognition.
|
Before answering or creating a plan, you MUST perform a "Deep Read" test of the KB to gain preliminary cognition.
|
||||||
* **Action:** Immediately execute grep_chunks (keyword match) and knowledge_search (semantic match) based on the core entities of the user's query.
|
1. **Search:** Execute grep_chunks (keyword) and knowledge_search (semantic) based on core entities.
|
||||||
* **Purpose:** To determine: Does the KB contain direct answers? Is the data fragmented? Is the query complex enough to require a multi-step plan?
|
2. **DEEP READ (Crucial):** If the search returns IDs, you **MUST** call list_knowledge_chunks on the top relevant IDs to fetch their actual text.
|
||||||
* **Output:** Analyze these initial results in your think block.
|
3. **Analyze:** In your think block, evaluate the *full text* you just retrieved.
|
||||||
|
* *Does this text fully answer the user?*
|
||||||
|
* *Is the information complete or partial?*
|
||||||
|
|
||||||
#### Phase 2: Strategic Decision & Planning
|
#### Phase 2: Strategic Decision & Planning
|
||||||
Based on Phase 1 results, decide your path in the think block:
|
Based on the **Deep Read** results from Phase 1:
|
||||||
* **Path A (Direct Answer):** If the initial retrieval contains sufficient, unambiguous, and complete evidence to answer the user fully → Proceed directly to **Answer Generation**.
|
* **Path A (Direct Answer):** If the full text provides sufficient, unambiguous evidence → Proceed to **Answer Generation**.
|
||||||
* **Path B (Complex Research):** If the query involves comparison, multi-hop reasoning, missing data, or the initial results are fragmented → You MUST use todo_write to formulate a Work Plan.
|
* **Path B (Complex Research):** If the query involves comparison, missing data, or the content requires synthesis → Use todo_write to formulate a Work Plan.
|
||||||
* **Plan Structure:** Break the problem into distinct, independent retrieval tasks (e.g., "Retrieve specs for Product A", "Retrieve specs for Product B", "Search for regulatory changes").
|
* *Structure:* Break the problem into distinct retrieval tasks (e.g., "Deep read specs for Product A", "Deep read safety protocols").
|
||||||
|
|
||||||
#### Phase 3: Disciplined Execution & Deep Reflection (The Loop)
|
#### Phase 3: Disciplined Execution & Deep Reflection (The Loop)
|
||||||
If you are in **Path B**, execute the tasks in todo_write one by one. For **EACH** task:
|
If in **Path B**, execute tasks in todo_write sequentially. For **EACH** task:
|
||||||
1. **Execute Retrieval:** Perform grep_chunks → knowledge_search (→ web_search if KB fails) specific to *that sub-task*.
|
1. **Search:** Perform grep_chunks / knowledge_search for the sub-task.
|
||||||
2. **MANDATORY Deep Reflection (in think):** After receiving results for a task, you MUST pause and reflect:
|
2. **DEEP READ (Mandatory):** Call list_knowledge_chunks for any relevant IDs found. **Never skip this step.**
|
||||||
* *Validity Check:* "Does this content actually answer the specific sub-task?"
|
3. **MANDATORY Deep Reflection (in think):** Pause and evaluate the full text:
|
||||||
* *Gap Analysis:* "Is the information outdated? Is it missing key details?"
|
* *Validity:* "Does this full text specifically address the sub-task?"
|
||||||
* *Correction:* If the content is insufficient, you CANNOT mark the task as done. You must immediately formulate a *remedial action* (e.g., "Try different keywords", "Use knowledge graph", or "Escalate to Web Search") and execute it.
|
* *Gap Analysis:* "Is anything missing?"
|
||||||
* *Completion:* Only when the sub-task is truly satisfied by evidence can you update todo_write to "completed".
|
* *Correction:* If insufficient, formulate a remedial action (e.g., "Search for synonym X", "Web Search") immediately.
|
||||||
|
* *Completion:* Mark task as "completed" ONLY when evidence is secured.
|
||||||
|
|
||||||
#### Phase 4: Final Synthesis
|
#### Phase 4: Final Synthesis
|
||||||
Only when ALL todo_write tasks are "completed":
|
Only when ALL tasks are "completed":
|
||||||
* Synthesize the findings from all tasks.
|
* Synthesize findings from the full text of all retrieved chunks.
|
||||||
* Check for consistency across different retrieved chunks.
|
* Check for consistency.
|
||||||
* Generate the final response.
|
* Generate the final response.
|
||||||
|
|
||||||
### Core Retrieval Strategy (For Phase 1 & Phase 3)
|
### Core Retrieval Strategy (Strict Sequence)
|
||||||
For every retrieval attempt, strictly follow this hierarchy:
|
For every retrieval attempt (Phase 1 or Phase 3), follow this exact chain:
|
||||||
1. **Entity Anchoring (grep_chunks):** precise keyword matching to find specific documents.
|
1. **Entity Anchoring (grep_chunks):** Use short keywords (1-3 words) to find candidate documents.
|
||||||
* *Rule:* Use short, specific tokens (1-3 words).
|
2. **Semantic Expansion (knowledge_search):** Use vector search for context (filter by IDs from step 1 if applicable).
|
||||||
2. **Semantic Expansion (knowledge_search):** vector-based search to find context.
|
3. **Content Verification (list_knowledge_chunks):** **MANDATORY.** You cannot process the results of Step 1 or 2 without this. You must fetch the content of the top candidates to verify details.
|
||||||
* *Rule:* If grep_chunks returned IDs, filter this search by those knowledge_ids. Use 2-3 query variations.
|
4. **Fallback (web_search):** Use ONLY if the Deep Read in Step 3 confirms the data is missing or irrelevant.
|
||||||
3. **Graph Exploration (query_knowledge_graph):** *Optional*. Use only if the task involves relationships (e.g., "manager of", "dependency of").
|
|
||||||
4. **Fallback (web_search):** Use ONLY if specific data is missing from KB after steps 1-3.
|
|
||||||
* *Rule:* If web content is truncated, you MUST use web_fetch to get the full text.
|
|
||||||
|
|
||||||
### Tool Selection Guidelines
|
### Tool Selection Guidelines
|
||||||
* **grep_chunks** Your "eyes". Use first to locate entities.
|
* **grep_chunks / knowledge_search:** Your "Index". Use these to find *where* the information might be.
|
||||||
* **knowledge_search** Your "brain". Use to understand concepts within the documents found by grep.
|
* **list_knowledge_chunks:** Your "Eyes". **Required** after every search to actually *read* the information.
|
||||||
* **todo_write** Your "project manager". Use for tracking multi-step research. **CRITICAL:** Do not summarize if this tool reports "pending" tasks.
|
* **todo_write:** Your "Manager". Tracks multi-step research.
|
||||||
* **think** Your "conscience". Use BEFORE every tool call to plan, and AFTER every tool output to reflect/verify.
|
* **think:** Your "Brain". Use to analyze the content returned by list_knowledge_chunks.
|
||||||
|
|
||||||
### Final Output Standards
|
### Final Output Standards
|
||||||
Your final answer must be:
|
* **Definitive:** Based strictly on the "Deep Read" content.
|
||||||
1. **Definitive:** Avoid vague phrases like "based on general understanding".
|
* **Sourced:** Cite sources immediately after claims: <kb doc="..." chunk_id="..." />.
|
||||||
2. **Sourced:** Every key claim must be immediately followed by a citation: <kb doc="..." chunk_id="..." /> or <web url="..." title="..." />.
|
* **Structured:** Clear hierarchy and logic.
|
||||||
3. **Structured:** Use clear headings, bullet points, and tables (if comparing data).
|
|
||||||
4. **Verified:** If conflict exists between KB and Web, prioritize the most recent source but explicitly note the conflict.
|
|
||||||
|
|
||||||
### System Status
|
### System Status
|
||||||
Current Time: {{current_time}}
|
Current Time: {{current_time}}
|
||||||
@@ -313,71 +310,77 @@ Knowledge Bases: {{knowledge_bases}}
|
|||||||
// ProgressiveRAGSystemPromptWithoutWeb is the progressive RAG system prompt template without web search
|
// ProgressiveRAGSystemPromptWithoutWeb is the progressive RAG system prompt template without web search
|
||||||
// This version emphasizes deep KB-only retrieval with advanced techniques
|
// This version emphasizes deep KB-only retrieval with advanced techniques
|
||||||
var ProgressiveRAGSystemPromptWithoutWeb = `### Role
|
var ProgressiveRAGSystemPromptWithoutWeb = `### Role
|
||||||
You are WeKnora, an intelligent retrieval assistant powered by Progressive Agentic RAG. You operate in a strictly isolated, **Closed-Loop Knowledge Environment**. You do NOT have access to the internet. Your sole source of truth is the provided internal Knowledge Bases.
|
You are WeKnora, a meticulous retrieval assistant powered by Progressive Agentic RAG. You operate in a strictly isolated, **Closed-Loop Knowledge Environment** (No Internet). You are defined by your "Deep Reading" philosophy: you never trust a snippet alone; you always verify the full context.
|
||||||
|
|
||||||
### Mission
|
### Mission
|
||||||
To deliver accurate, traceable answers by exhaustively retrieving and synthesizing information **exclusively** from internal Knowledge Bases. You must first gauge the information landscape through preliminary retrieval, then rigorously execute specific research tasks.
|
To provide answers that are not only accurate but contextually complete. You achieve this by following a strict **"Locate-then-Read"** protocol: finding documents via search, then reading their full content before synthesizing an answer.
|
||||||
|
|
||||||
### Critical Constraints (ABSOLUTE RULES)
|
### Critical Constraints (ABSOLUTE RULES)
|
||||||
1. **Strict Closed Loop:** You are FORBIDDEN from accessing the internet. Do not attempt to use web tools.
|
1. **No Snippet-Only Answers:** You are FORBIDDEN from answering based solely on the short text snippets returned by grep_chunks or knowledge_search.
|
||||||
2. **NO Internal Knowledge:** You must behave as if your pre-training data does not exist regarding facts. If the answer is not in the retrieved KB documents, it does not exist.
|
2. **Mandatory Deep Reading:** Whenever a search tool returns relevant knowledge_ids, you MUST use list_knowledge_chunks to read the actual content of those chunks/documents before using them as evidence.
|
||||||
3. **Honest Gap Reporting:** If, after exhaustive search, the information is not found in the KB, you must explicitly state: "Information not found in internal documents." NEVER fabricate, infer, or hallucinate an answer.
|
3. **No Internet:** You are strictly confined to internal Knowledge Bases.
|
||||||
4. **Strict Plan Adherence:** If a todo_write plan exists, you must follow it sequentially. You cannot summarize until all tasks are explicitly marked "completed".
|
4. **Evidence Verification:** If the full text read via list_knowledge_chunks contradicts the search snippet or shows the info is irrelevant, you must discard it and search again.
|
||||||
|
|
||||||
### Workflow: The "Reconnaissance-Plan-Execute" Cycle
|
### Workflow: The "Locate-Read-Plan-Execute" Cycle
|
||||||
|
|
||||||
You must follow this **Specific Operational Sequence** for every user query:
|
You must follow this **Specific Operational Sequence** for every user query:
|
||||||
|
|
||||||
#### Phase 1: Preliminary Reconnaissance (Mandatory Initial Step)
|
#### Phase 1: Preliminary Reconnaissance & Context Verification
|
||||||
Before answering or creating a complex plan, you MUST perform an initial "test" of the knowledge base to gain preliminary cognition.
|
Before answering or creating a plan, you MUST perform an initial "Test & Read" loop.
|
||||||
* **Action:** Immediately execute grep_chunks (keyword match) and knowledge_search (semantic match) based on the core entities of the user's query.
|
1. **Locate:** Execute grep_chunks (keyword) and knowledge_search (semantic) to find potential documents.
|
||||||
* **Purpose:** To determine: Is the data present? Is it fragmented? Do I need a complex plan?
|
2. **READ (Mandatory):** Identify the most relevant knowledge_ids from step 1 (you can select multiple, e.g., top 3-5). **IMMEDIATELY call list_knowledge_chunks** on these IDs to retrieve their full content.
|
||||||
* **Output:** Analyze these initial results in your think block.
|
3. **Analyze:** In your think block, evaluate the *full text* you just read. Does it cover the user's intent?
|
||||||
|
* *Decision:* If this full text is sufficient → Go to **Answer Generation**.
|
||||||
|
* *Decision:* If complex/incomplete → Go to **Phase 2**.
|
||||||
|
|
||||||
#### Phase 2: Strategic Decision & Planning
|
#### Phase 2: Strategic Decision & Planning
|
||||||
Based on Phase 1 results, decide your path in the think block:
|
If Phase 1 is insufficient, create a todo_write Work Plan.
|
||||||
* **Path A (Direct Answer):** If the initial retrieval contains sufficient, unambiguous evidence → Proceed directly to **Answer Generation**.
|
* **Plan Structure:** Break the problem into distinct retrieval tasks.
|
||||||
* **Path B (Complex Research):** If the query involves comparison, multi-hop reasoning, or the initial results are fragmented → You MUST use todo_write to formulate a Work Plan.
|
* **Context Awareness:** Use the full text read in Phase 1 to inform your plan (e.g., "Doc A mentions Protocol X, I need to create a task to specifically search for Protocol X details").
|
||||||
* **Plan Structure:** Break the problem into distinct, independent retrieval tasks (e.g., "Retrieve specs for Product A", "Analyze warranty policy").
|
|
||||||
|
|
||||||
#### Phase 3: Disciplined Execution & Deep Reflection (The Loop)
|
#### Phase 3: Disciplined Execution with Deep Reading
|
||||||
If you are in **Path B**, execute the tasks in todo_write one by one. For **EACH** task:
|
Execute tasks in todo_write sequentially. For **EACH** task:
|
||||||
1. **Execute Retrieval:** Perform grep_chunks → knowledge_search specific to *that sub-task*.
|
1. **Search:** Perform grep_chunks or knowledge_search specific to the sub-task.
|
||||||
2. **MANDATORY Deep Reflection (in think):** After receiving results for a task, you MUST pause and reflect:
|
2. **READ (Mandatory):**
|
||||||
* *Validity Check:* "Does this content actually answer the specific sub-task?"
|
* Extract the knowledge_ids of the most promising results.
|
||||||
* *Gap Analysis:* "Is the information missing?"
|
* **Call list_knowledge_chunks** to fetch the content for these IDs. **Do not skip this step.**
|
||||||
* *Correction (Iterative Search):* If results are poor, do NOT give up immediately. Formulate a *remedial action* (e.g., "Try synonym keywords", "Search for broader concept", "Check Knowledge Graph").
|
* *Note:* You are encouraged to check multiple files if the answer might be spread across them.
|
||||||
* *Dead End Handling:* If, after remedial attempts, the KB still yields nothing, you must mark the task as "completed" but record the finding as **"Data Unavailable in KB"**.
|
3. **Reflect (Deep Reflection):**
|
||||||
* *Completion:* Update todo_write to "completed" only when you have exhausted all search avenues for that task.
|
* "Based on the *full text* I just read, is this sub-task resolved?"
|
||||||
|
* If no, formulate a remedial search action immediately.
|
||||||
|
* Only mark as "completed" when the full text evidence is secured.
|
||||||
|
|
||||||
#### Phase 4: Final Synthesis
|
#### Phase 4: Final Synthesis
|
||||||
Only when ALL todo_write tasks are "completed":
|
* Synthesize findings based **only** on the content read via list_knowledge_chunks.
|
||||||
* Synthesize the findings from all tasks.
|
* Generate the final response with citations.
|
||||||
* Check for consistency across retrieved chunks.
|
|
||||||
* If some tasks resulted in "Data Unavailable", explicitly mention this limitation in the final answer.
|
|
||||||
* Generate the final response.
|
|
||||||
|
|
||||||
### Core Retrieval Strategy (Strict Hierarchy)
|
### Core Retrieval Strategy (The "Locate-then-Read" Pattern)
|
||||||
For every retrieval attempt, strictly follow this hierarchy to maximize Recall within the KB:
|
For every information seeking step, strictly follow this 3-step atomic unit:
|
||||||
1. **Entity Anchoring (grep_chunks):** Precise keyword matching.
|
|
||||||
* *Rule:* Use short, specific tokens (1-3 words). Try multiple aliases (e.g., "HR", "Human Resources").
|
1. **Step A: Locate (Search)**
|
||||||
2. **Semantic Expansion (knowledge_search):** Vector-based search.
|
* Use grep_chunks for specific entities (Error codes, product names).
|
||||||
* *Rule:* Use 2-4 query variations to capture different phrasings. Filter by knowledge_ids from step 1 if applicable.
|
* Use knowledge_search for concepts.
|
||||||
3. **Graph Exploration (query_knowledge_graph):** *Optional*. Use only if the task involves relationships or if standard search fails to connect concepts.
|
* *Goal:* Get a list of candidate knowledge_ids.
|
||||||
4. **Deep Reading (list_knowledge_chunks):** Use if you need the full context of a specific document found in previous steps.
|
|
||||||
|
2. **Step B: Read (Fetch Context)**
|
||||||
|
* **Action:** Call list_knowledge_chunks(knowledge_ids=[id1, id2, ...]).
|
||||||
|
* *Rule:* Always fetch the content. Snippets are often truncated or lack necessary context (like prerequisites or exceptions).
|
||||||
|
* *Scope:* It is acceptable and encouraged to fetch 3-5 distinct documents to ensure comprehensive coverage.
|
||||||
|
|
||||||
|
3. **Step C: Evaluate (Filter)**
|
||||||
|
* Read the output of list_knowledge_chunks.
|
||||||
|
* Discard irrelevant documents.
|
||||||
|
* Extract facts from valid documents to build your answer.
|
||||||
|
|
||||||
### Tool Selection Guidelines
|
### Tool Selection Guidelines
|
||||||
* **grep_chunks:** Your "eyes". Use first to locate entities.
|
* **grep_chunks / knowledge_search:** Used ONLY as "Pointers" or "Index Lookups". They tell you *where* to look, not *what* the answer is.
|
||||||
* **knowledge_search:** Your "brain". Use to understand concepts.
|
* **list_knowledge_chunks:** Your "Eyes". **This is your primary reading tool.** You must use it after every search to verify data.
|
||||||
* **todo_write:** Your "project manager". Use for tracking multi-step research.
|
* **todo_write:** Use for managing multi-step research.
|
||||||
* **think:** Your "conscience". Use to plan before tools and reflect after tools.
|
|
||||||
|
|
||||||
### Final Output Standards
|
### Final Output Standards
|
||||||
Your final answer must be:
|
1. **Context-Backed:** Your answer must reflect the nuance found in the full text (e.g., conditions, warnings, detailed steps) which might be missing from search snippets.
|
||||||
1. **Definitive & Honest:** If data is found, answer confidently. If data is missing, admit it clearly.
|
2. **Sourced:** Cite the specific documents you read.
|
||||||
2. **Sourced:** Every key claim must be immediately followed by a citation: <kb doc="..." chunk_id="..." />.
|
3. **Honest:** If the full text reveals the search hit was a false positive, admit it and search again.
|
||||||
3. **Structured:** Use clear headings, bullet points, and tables.
|
|
||||||
4. **No External References:** Do not mention "Google", "Web", or "Internet".
|
|
||||||
|
|
||||||
### System Status
|
### System Status
|
||||||
Current Time: {{current_time}}
|
Current Time: {{current_time}}
|
||||||
|
|||||||
+170
-183
@@ -45,10 +45,12 @@ Provide only the **minimal core entities** extracted from user query, such as:
|
|||||||
- Focus exclusively on **core entities**, not descriptions
|
- Focus exclusively on **core entities**, not descriptions
|
||||||
- Break complex input into individual, essential keywords
|
- Break complex input into individual, essential keywords
|
||||||
- Avoid phrases, explanations, or anything that reduces match probability
|
- Avoid phrases, explanations, or anything that reduces match probability
|
||||||
|
- Preserve precision details embedded in the query (e.g., version numbers, build IDs) when they materially define the entity being matched.
|
||||||
|
|
||||||
Long phrases dramatically reduce recall because chunks rarely contain identical wording.
|
Long phrases dramatically reduce recall because chunks rarely contain identical wording.
|
||||||
Only short, atomic keywords ensure accurate matching and avoid unrelated retrieval.
|
Only short, atomic keywords ensure accurate matching and avoid unrelated retrieval.
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
grep_chunks scans enabled chunks across the specified knowledge bases and returns those containing any provided keyword. Matching is case-insensitive, with chunk indices and local context included.
|
grep_chunks scans enabled chunks across the specified knowledge bases and returns those containing any provided keyword. Matching is case-insensitive, with chunk indices and local context included.
|
||||||
|
|
||||||
@@ -88,13 +90,13 @@ func (t *GrepChunksTool) Parameters() map[string]interface{} {
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"knowledge_ids": map[string]interface{}{
|
// "knowledge_ids": map[string]interface{}{
|
||||||
"type": "array",
|
// "type": "array",
|
||||||
"description": "Filter by document/knowledge IDs. If empty, searches all documents.",
|
// "description": "Filter by document/knowledge IDs. If empty, searches all documents.",
|
||||||
"items": map[string]interface{}{
|
// "items": map[string]interface{}{
|
||||||
"type": "string",
|
// "type": "string",
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
"max_results": map[string]interface{}{
|
"max_results": map[string]interface{}{
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"description": "Maximum number of matching chunks to return (default: 50, max: 200)",
|
"description": "Maximum number of matching chunks to return (default: 50, max: 200)",
|
||||||
@@ -135,9 +137,7 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use default values for all options
|
// Use default values for all options
|
||||||
contextLines := 50 // default: 50 context characters
|
countOnly := false // default: show results
|
||||||
countOnly := false // default: show results
|
|
||||||
showLineNumbers := true // default: show chunk indices
|
|
||||||
|
|
||||||
maxResults := 50
|
maxResults := 50
|
||||||
if mr, ok := args["max_results"].(float64); ok {
|
if mr, ok := args["max_results"].(float64); ok {
|
||||||
@@ -162,21 +162,21 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
|
|||||||
kbIDs = t.knowledgeBaseIDs
|
kbIDs = t.knowledgeBaseIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse knowledge_ids filter
|
// // Parse knowledge_ids filter
|
||||||
var knowledgeIDs []string
|
// var knowledgeIDs []string
|
||||||
if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
|
// if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
|
||||||
for _, id := range knowledgeIDsRaw {
|
// for _, id := range knowledgeIDsRaw {
|
||||||
if idStr, ok := id.(string); ok && idStr != "" {
|
// if idStr, ok := id.(string); ok && idStr != "" {
|
||||||
knowledgeIDs = append(knowledgeIDs, idStr)
|
// knowledgeIDs = append(knowledgeIDs, idStr)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
logger.Infof(ctx, "[Tool][GrepChunks] Patterns: %v, MaxResults: %d",
|
logger.Infof(ctx, "[Tool][GrepChunks] Patterns: %v, MaxResults: %d",
|
||||||
patterns, maxResults)
|
patterns, maxResults)
|
||||||
|
|
||||||
// Build and execute query
|
// Build and execute query
|
||||||
results, totalCount, err := t.searchChunks(ctx, patterns, kbIDs, knowledgeIDs, maxResults)
|
results, totalCount, err := t.searchChunks(ctx, patterns, kbIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
|
logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
|
||||||
return &types.ToolResult{
|
return &types.ToolResult{
|
||||||
@@ -213,29 +213,37 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
|
|||||||
|
|
||||||
// Sort by match score (descending), then by chunk index
|
// Sort by match score (descending), then by chunk index
|
||||||
sort.Slice(finalResults, func(i, j int) bool {
|
sort.Slice(finalResults, func(i, j int) bool {
|
||||||
|
if finalResults[i].MatchedPatterns != finalResults[j].MatchedPatterns {
|
||||||
|
return finalResults[i].MatchedPatterns > finalResults[j].MatchedPatterns
|
||||||
|
}
|
||||||
if finalResults[i].MatchScore != finalResults[j].MatchScore {
|
if finalResults[i].MatchScore != finalResults[j].MatchScore {
|
||||||
return finalResults[i].MatchScore > finalResults[j].MatchScore
|
return finalResults[i].MatchScore > finalResults[j].MatchScore
|
||||||
}
|
}
|
||||||
return finalResults[i].ChunkIndex < finalResults[j].ChunkIndex
|
return finalResults[i].ChunkIndex < finalResults[j].ChunkIndex
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(finalResults) > 20 {
|
aggregatedResults := t.aggregateByKnowledge(finalResults, patterns)
|
||||||
finalResults = finalResults[:20]
|
|
||||||
|
totalKnowledge := len(aggregatedResults)
|
||||||
|
|
||||||
|
if len(aggregatedResults) > 20 {
|
||||||
|
aggregatedResults = aggregatedResults[:20]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "[Tool][GrepChunks] Aggregated results: %d", len(aggregatedResults))
|
||||||
|
|
||||||
// Format output
|
// Format output
|
||||||
output := t.formatOutput(ctx, finalResults, totalCount, patterns, contextLines, countOnly, showLineNumbers, kbIDs, knowledgeIDs)
|
output := t.formatOutput(ctx, aggregatedResults, totalCount, patterns, countOnly)
|
||||||
|
|
||||||
return &types.ToolResult{
|
return &types.ToolResult{
|
||||||
Success: true,
|
Success: true,
|
||||||
Output: output,
|
Output: output,
|
||||||
Data: map[string]interface{}{
|
Data: map[string]interface{}{
|
||||||
"patterns": patterns,
|
"patterns": patterns,
|
||||||
"results": finalResults,
|
"knowledge_results": aggregatedResults,
|
||||||
"result_count": len(finalResults),
|
"result_count": len(aggregatedResults),
|
||||||
"total_matches": totalCount,
|
"total_matches": totalKnowledge,
|
||||||
"knowledge_base_ids": kbIDs,
|
"knowledge_base_ids": kbIDs,
|
||||||
"knowledge_ids": knowledgeIDs,
|
|
||||||
"max_results": maxResults,
|
"max_results": maxResults,
|
||||||
"display_type": "grep_results",
|
"display_type": "grep_results",
|
||||||
},
|
},
|
||||||
@@ -244,8 +252,9 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
|
|||||||
|
|
||||||
type chunkWithTitle struct {
|
type chunkWithTitle struct {
|
||||||
types.Chunk
|
types.Chunk
|
||||||
KnowledgeTitle string `json:"knowledge_title" gorm:"column:knowledge_title"`
|
KnowledgeTitle string `json:"knowledge_title" gorm:"column:knowledge_title"`
|
||||||
MatchScore float64 `json:"match_score" gorm:"column:match_score"` // Score based on match count and position
|
MatchScore float64 `json:"match_score" gorm:"column:match_score"` // Score based on match count and position
|
||||||
|
MatchedPatterns int `json:"matched_patterns"` // Number of unique patterns matched
|
||||||
}
|
}
|
||||||
|
|
||||||
// searchChunks performs the database search with pattern matching
|
// searchChunks performs the database search with pattern matching
|
||||||
@@ -253,11 +262,9 @@ func (t *GrepChunksTool) searchChunks(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
patterns []string,
|
patterns []string,
|
||||||
kbIDs []string,
|
kbIDs []string,
|
||||||
knowledgeIDs []string,
|
|
||||||
maxResults int,
|
|
||||||
) ([]chunkWithTitle, int64, error) {
|
) ([]chunkWithTitle, int64, error) {
|
||||||
// Build base query
|
// Build base query
|
||||||
query := t.db.WithContext(ctx).Table("chunks").
|
query := t.db.Debug().WithContext(ctx).Table("chunks").
|
||||||
Select("chunks.id, chunks.content, chunks.chunk_index, chunks.knowledge_id, chunks.knowledge_base_id, chunks.chunk_type, chunks.created_at, knowledges.title as knowledge_title").
|
Select("chunks.id, chunks.content, chunks.chunk_index, chunks.knowledge_id, chunks.knowledge_base_id, chunks.chunk_type, chunks.created_at, knowledges.title as knowledge_title").
|
||||||
Joins("LEFT JOIN knowledges ON chunks.knowledge_id = knowledges.id").
|
Joins("LEFT JOIN knowledges ON chunks.knowledge_id = knowledges.id").
|
||||||
Where("chunks.tenant_id = ?", t.tenantID).
|
Where("chunks.tenant_id = ?", t.tenantID).
|
||||||
@@ -270,11 +277,6 @@ func (t *GrepChunksTool) searchChunks(
|
|||||||
query = query.Where("chunks.knowledge_base_id IN ?", kbIDs)
|
query = query.Where("chunks.knowledge_base_id IN ?", kbIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply knowledge filter
|
|
||||||
if len(knowledgeIDs) > 0 {
|
|
||||||
query = query.Where("chunks.knowledge_id IN ?", knowledgeIDs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply pattern matching (case-insensitive fixed string matching, OR logic for multiple patterns)
|
// Apply pattern matching (case-insensitive fixed string matching, OR logic for multiple patterns)
|
||||||
if len(patterns) == 1 {
|
if len(patterns) == 1 {
|
||||||
query = query.Where("chunks.content ILIKE ?", "%"+patterns[0]+"%")
|
query = query.Where("chunks.content ILIKE ?", "%"+patterns[0]+"%")
|
||||||
@@ -297,7 +299,7 @@ func (t *GrepChunksTool) searchChunks(
|
|||||||
|
|
||||||
// Fetch results
|
// Fetch results
|
||||||
var results []chunkWithTitle
|
var results []chunkWithTitle
|
||||||
if err := query.Limit(maxResults).Order("chunks.created_at DESC").Find(&results).Error; err != nil {
|
if err := query.Order("chunks.created_at DESC").Find(&results).Error; err != nil {
|
||||||
logger.Errorf(ctx, "[Tool][GrepChunks] Failed to fetch results: %v", err)
|
logger.Errorf(ctx, "[Tool][GrepChunks] Failed to fetch results: %v", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
@@ -308,14 +310,10 @@ func (t *GrepChunksTool) searchChunks(
|
|||||||
// formatOutput formats the search results for display (grep-style output)
|
// formatOutput formats the search results for display (grep-style output)
|
||||||
func (t *GrepChunksTool) formatOutput(
|
func (t *GrepChunksTool) formatOutput(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
results []chunkWithTitle,
|
results []knowledgeAggregation,
|
||||||
totalCount int64,
|
totalCount int64,
|
||||||
patterns []string,
|
patterns []string,
|
||||||
contextLines int,
|
|
||||||
countOnly bool,
|
countOnly bool,
|
||||||
showLineNumbers bool,
|
|
||||||
kbIDs []string,
|
|
||||||
knowledgeIDs []string,
|
|
||||||
) string {
|
) string {
|
||||||
var output strings.Builder
|
var output strings.Builder
|
||||||
|
|
||||||
@@ -331,7 +329,7 @@ func (t *GrepChunksTool) formatOutput(
|
|||||||
} else {
|
} else {
|
||||||
output.WriteString(fmt.Sprintf("Patterns (%d): %v (case-insensitive, OR logic)\n", len(patterns), patterns))
|
output.WriteString(fmt.Sprintf("Patterns (%d): %v (case-insensitive, OR logic)\n", len(patterns), patterns))
|
||||||
}
|
}
|
||||||
output.WriteString(fmt.Sprintf("Matches: %d chunk(s)\n\n", len(results)))
|
output.WriteString(fmt.Sprintf("Matches: %d knowledge item(s)\n\n", len(results)))
|
||||||
|
|
||||||
if len(results) == 0 {
|
if len(results) == 0 {
|
||||||
output.WriteString("No matches found.\n")
|
output.WriteString("No matches found.\n")
|
||||||
@@ -343,156 +341,143 @@ func (t *GrepChunksTool) formatOutput(
|
|||||||
return output.String()
|
return output.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group by document (like grep showing filename)
|
for idx, result := range results {
|
||||||
docGroups := make(map[string]struct {
|
var patternSummaries []string
|
||||||
title string
|
for _, pattern := range patterns {
|
||||||
knowledgeBaseID string
|
count := result.PatternCounts[pattern]
|
||||||
chunks []chunkWithTitle
|
patternSummaries = append(patternSummaries, fmt.Sprintf("%s=%d", pattern, count))
|
||||||
})
|
|
||||||
|
|
||||||
for _, result := range results {
|
|
||||||
knowledgeID := fmt.Sprintf("%v", result.KnowledgeID)
|
|
||||||
title := "Untitled"
|
|
||||||
if t := result.KnowledgeTitle; t != "" {
|
|
||||||
title = fmt.Sprintf("%v", t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group := docGroups[knowledgeID]
|
output.WriteString(fmt.Sprintf("%d) knowledge_id=%s | title=%s | chunk_hits=%d | pattern_hits=[%s]\n",
|
||||||
group.title = title
|
idx+1,
|
||||||
group.knowledgeBaseID = result.KnowledgeBaseID
|
result.KnowledgeID,
|
||||||
group.chunks = append(group.chunks, result)
|
result.KnowledgeTitle,
|
||||||
docGroups[knowledgeID] = group
|
result.ChunkHitCount,
|
||||||
}
|
strings.Join(patternSummaries, ", "),
|
||||||
|
))
|
||||||
// Display results in grep-style format
|
|
||||||
for docID, group := range docGroups {
|
|
||||||
// Show document header (like grep showing filename)
|
|
||||||
output.WriteString(fmt.Sprintf("\n--- knowledge_base_id: %s, knowledge_id: %s, title: %s ---\n",
|
|
||||||
group.knowledgeBaseID, docID, group.title))
|
|
||||||
|
|
||||||
// Show each matching chunk
|
|
||||||
for _, chunk := range group.chunks {
|
|
||||||
chunkIndex := chunk.ChunkIndex
|
|
||||||
content := chunk.Content
|
|
||||||
|
|
||||||
// Extract preview with context around match (case-insensitive)
|
|
||||||
// Try each pattern and use the first match
|
|
||||||
preview := extractPreviewMultiPattern(content, patterns, false, contextLines)
|
|
||||||
|
|
||||||
// Format like grep: [filename:]line:content
|
|
||||||
if showLineNumbers {
|
|
||||||
if len(docGroups) > 1 {
|
|
||||||
output.WriteString(fmt.Sprintf("%s:chunk[%d]:%s\n", group.title, chunkIndex, preview))
|
|
||||||
} else {
|
|
||||||
output.WriteString(fmt.Sprintf("chunk[%d]:%s\n", chunkIndex, preview))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if len(docGroups) > 1 {
|
|
||||||
output.WriteString(fmt.Sprintf("%s:%s\n", group.title, preview))
|
|
||||||
} else {
|
|
||||||
output.WriteString(fmt.Sprintf("%s\n", preview))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return output.String()
|
return output.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractPreview extracts a preview of content around the matched pattern
|
type knowledgeAggregation struct {
|
||||||
func extractPreview(content, pattern string, caseSensitive bool, contextLen int) string {
|
KnowledgeID string `json:"knowledge_id"`
|
||||||
if content == "" {
|
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||||
return ""
|
KnowledgeTitle string `json:"knowledge_title"`
|
||||||
}
|
ChunkHitCount int `json:"chunk_hit_count"`
|
||||||
|
PatternCounts map[string]int `json:"pattern_counts"`
|
||||||
// Find the pattern in content
|
TotalPatternHits int `json:"total_pattern_hits"`
|
||||||
searchContent := content
|
DistinctPatterns int `json:"distinct_patterns"`
|
||||||
searchPattern := pattern
|
|
||||||
if !caseSensitive {
|
|
||||||
searchContent = strings.ToLower(content)
|
|
||||||
searchPattern = strings.ToLower(pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
pos := strings.Index(searchContent, searchPattern)
|
|
||||||
if pos == -1 {
|
|
||||||
// If pattern not found (might be regex), just return first 60 chars
|
|
||||||
runes := []rune(content)
|
|
||||||
if len(runes) <= contextLen*2 {
|
|
||||||
return string(runes)
|
|
||||||
}
|
|
||||||
return string(runes[:contextLen*2])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract context around the match
|
|
||||||
runes := []rune(content)
|
|
||||||
start := pos
|
|
||||||
|
|
||||||
// Convert to rune positions for proper unicode handling
|
|
||||||
runePos := 0
|
|
||||||
for i, r := range runes {
|
|
||||||
if runePos >= start {
|
|
||||||
start = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
runePos += len(string(r))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate preview bounds
|
|
||||||
previewStart := start - contextLen
|
|
||||||
if previewStart < 0 {
|
|
||||||
previewStart = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
previewEnd := start + len([]rune(pattern)) + contextLen
|
|
||||||
if previewEnd > len(runes) {
|
|
||||||
previewEnd = len(runes)
|
|
||||||
}
|
|
||||||
|
|
||||||
preview := string(runes[previewStart:previewEnd])
|
|
||||||
|
|
||||||
// Clean up whitespace
|
|
||||||
preview = strings.ReplaceAll(preview, "\n", " ")
|
|
||||||
preview = strings.ReplaceAll(preview, "\t", " ")
|
|
||||||
|
|
||||||
// Collapse multiple spaces
|
|
||||||
for strings.Contains(preview, " ") {
|
|
||||||
preview = strings.ReplaceAll(preview, " ", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.TrimSpace(preview)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractPreviewMultiPattern extracts a preview around the first matched pattern
|
func (t *GrepChunksTool) aggregateByKnowledge(results []chunkWithTitle, patterns []string) []knowledgeAggregation {
|
||||||
func extractPreviewMultiPattern(content string, patterns []string, caseSensitive bool, contextLen int) string {
|
if len(results) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
patternKeys := make([]string, 0, len(patterns))
|
||||||
|
for _, p := range patterns {
|
||||||
|
if strings.TrimSpace(p) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
patternKeys = append(patternKeys, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
aggregated := make(map[string]*knowledgeAggregation)
|
||||||
|
for _, chunk := range results {
|
||||||
|
knowledgeID := chunk.KnowledgeID
|
||||||
|
if knowledgeID == "" {
|
||||||
|
knowledgeID = fmt.Sprintf("chunk-%s", chunk.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := aggregated[knowledgeID]; !ok {
|
||||||
|
title := chunk.KnowledgeTitle
|
||||||
|
if strings.TrimSpace(title) == "" {
|
||||||
|
title = "Untitled"
|
||||||
|
}
|
||||||
|
aggregated[knowledgeID] = &knowledgeAggregation{
|
||||||
|
KnowledgeID: knowledgeID,
|
||||||
|
KnowledgeBaseID: chunk.KnowledgeBaseID,
|
||||||
|
KnowledgeTitle: title,
|
||||||
|
PatternCounts: make(map[string]int, len(patternKeys)),
|
||||||
|
}
|
||||||
|
for _, pKey := range patternKeys {
|
||||||
|
aggregated[knowledgeID].PatternCounts[pKey] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := aggregated[knowledgeID]
|
||||||
|
entry.ChunkHitCount++
|
||||||
|
|
||||||
|
patternOccurrences := t.countPatternOccurrences(chunk.Content, patternKeys)
|
||||||
|
for _, p := range patternKeys {
|
||||||
|
count := patternOccurrences[p]
|
||||||
|
if count == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry.PatternCounts[p] += count
|
||||||
|
entry.TotalPatternHits += count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resultSlice := make([]knowledgeAggregation, 0, len(aggregated))
|
||||||
|
for _, entry := range aggregated {
|
||||||
|
distinct := 0
|
||||||
|
for _, count := range entry.PatternCounts {
|
||||||
|
if count > 0 {
|
||||||
|
distinct++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entry.DistinctPatterns = distinct
|
||||||
|
resultSlice = append(resultSlice, *entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(resultSlice, func(i, j int) bool {
|
||||||
|
if resultSlice[i].DistinctPatterns != resultSlice[j].DistinctPatterns {
|
||||||
|
return resultSlice[i].DistinctPatterns > resultSlice[j].DistinctPatterns
|
||||||
|
}
|
||||||
|
if resultSlice[i].TotalPatternHits != resultSlice[j].TotalPatternHits {
|
||||||
|
return resultSlice[i].TotalPatternHits > resultSlice[j].TotalPatternHits
|
||||||
|
}
|
||||||
|
if resultSlice[i].ChunkHitCount != resultSlice[j].ChunkHitCount {
|
||||||
|
return resultSlice[i].ChunkHitCount > resultSlice[j].ChunkHitCount
|
||||||
|
}
|
||||||
|
return resultSlice[i].KnowledgeTitle < resultSlice[j].KnowledgeTitle
|
||||||
|
})
|
||||||
|
return resultSlice
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GrepChunksTool) countPatternOccurrences(content string, patterns []string) map[string]int {
|
||||||
|
counts := make(map[string]int, len(patterns))
|
||||||
if content == "" || len(patterns) == 0 {
|
if content == "" || len(patterns) == 0 {
|
||||||
return ""
|
return counts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try each pattern and find the first match
|
contentLower := strings.ToLower(content)
|
||||||
for _, pattern := range patterns {
|
for _, pattern := range patterns {
|
||||||
preview := extractPreview(content, pattern, caseSensitive, contextLen)
|
p := strings.ToLower(pattern)
|
||||||
// Check if this pattern found a match (not just truncated content)
|
if strings.TrimSpace(p) == "" {
|
||||||
searchContent := content
|
continue
|
||||||
searchPattern := pattern
|
|
||||||
if !caseSensitive {
|
|
||||||
searchContent = strings.ToLower(content)
|
|
||||||
searchPattern = strings.ToLower(pattern)
|
|
||||||
}
|
}
|
||||||
if strings.Contains(searchContent, searchPattern) {
|
counts[pattern] = countOccurrences(contentLower, p)
|
||||||
return preview
|
}
|
||||||
|
return counts
|
||||||
|
}
|
||||||
|
|
||||||
|
func countOccurrences(text string, pattern string) int {
|
||||||
|
if pattern == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
count := 0
|
||||||
|
index := 0
|
||||||
|
for index < len(text) {
|
||||||
|
pos := strings.Index(text[index:], pattern)
|
||||||
|
if pos == -1 {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
count++
|
||||||
|
index += pos + len(pattern)
|
||||||
}
|
}
|
||||||
|
return count
|
||||||
// If no pattern matched, return preview from first pattern
|
|
||||||
if len(patterns) > 0 {
|
|
||||||
return extractPreview(content, patterns[0], caseSensitive, contextLen)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: return first N chars
|
|
||||||
runes := []rune(content)
|
|
||||||
if len(runes) <= contextLen*2 {
|
|
||||||
return string(runes)
|
|
||||||
}
|
|
||||||
return string(runes[:contextLen*2])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// deduplicateChunks removes duplicate or near-duplicate chunks using content signature
|
// deduplicateChunks removes duplicate or near-duplicate chunks using content signature
|
||||||
@@ -573,15 +558,17 @@ func (t *GrepChunksTool) scoreChunks(ctx context.Context, results []chunkWithTit
|
|||||||
scored := make([]chunkWithTitle, len(results))
|
scored := make([]chunkWithTitle, len(results))
|
||||||
for i := range results {
|
for i := range results {
|
||||||
scored[i] = results[i]
|
scored[i] = results[i]
|
||||||
scored[i].MatchScore = t.calculateMatchScore(results[i].Content, patterns)
|
score, patternCount := t.calculateMatchScore(results[i].Content, patterns)
|
||||||
|
scored[i].MatchScore = score
|
||||||
|
scored[i].MatchedPatterns = patternCount
|
||||||
}
|
}
|
||||||
return scored
|
return scored
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculateMatchScore calculates a score based on how many patterns match and their positions
|
// calculateMatchScore calculates a score based on how many patterns match and their positions
|
||||||
func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string) float64 {
|
func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string) (float64, int) {
|
||||||
if content == "" || len(patterns) == 0 {
|
if content == "" || len(patterns) == 0 {
|
||||||
return 0.0
|
return 0.0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
contentLower := strings.ToLower(content)
|
contentLower := strings.ToLower(content)
|
||||||
@@ -613,7 +600,7 @@ func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string)
|
|||||||
positionBonus = positionRatio * 0.1
|
positionBonus = positionRatio * 0.1
|
||||||
}
|
}
|
||||||
|
|
||||||
return math.Min(baseScore+positionBonus, 1.0)
|
return math.Min(baseScore+positionBonus, 1.0), matchCount
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyMMR applies Maximal Marginal Relevance algorithm to reduce redundancy
|
// applyMMR applies Maximal Marginal Relevance algorithm to reduce redundancy
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ type CreateKnowledgeQARequest struct {
|
|||||||
AgentEnabled bool `json:"agent_enabled"` // Whether agent mode is enabled for this request
|
AgentEnabled bool `json:"agent_enabled"` // Whether agent mode is enabled for this request
|
||||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search is enabled for this request
|
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search is enabled for this request
|
||||||
SummaryModelID string `json:"summary_model_id"` // Optional summary model ID for this request (overrides session default)
|
SummaryModelID string `json:"summary_model_id"` // Optional summary model ID for this request (overrides session default)
|
||||||
MCPServiceIDs []string `json:"mcp_service_ids"` // Optional MCP service IDs to use in agent mode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchKnowledgeRequest defines the request structure for searching knowledge without LLM summarization
|
// SearchKnowledgeRequest defines the request structure for searching knowledge without LLM summarization
|
||||||
|
|||||||
Reference in New Issue
Block a user