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"
|
||||
)
|
||||
|
||||
// AgentQARequest agent Q&A request
|
||||
// AgentQARequest agent Q&A request payload.
|
||||
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
|
||||
@@ -44,15 +49,28 @@ type AgentStreamResponse struct {
|
||||
// Return error to stop processing the stream
|
||||
type AgentEventCallback func(*AgentStreamResponse) error
|
||||
|
||||
// AgentQAStream performs agent-based Q&A with SSE streaming
|
||||
// The callback will be invoked for each event received from the server
|
||||
// AgentQAStream performs agent-based Q&A with SSE streaming using default agent settings.
|
||||
// Deprecated: prefer AgentQAStreamWithRequest to customize agent behavior.
|
||||
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{
|
||||
Query: query,
|
||||
// AgentQAStreamWithRequest performs agent-based Q&A with SSE streaming using the full request payload.
|
||||
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)
|
||||
if err != nil {
|
||||
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 {
|
||||
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
|
||||
func (as *AgentSession) GetSessionID() string {
|
||||
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"`
|
||||
}
|
||||
|
||||
// AgentConfig defines agent configuration for agent mode
|
||||
// AgentConfig defines session-level agent configuration (matches server struct).
|
||||
type AgentConfig struct {
|
||||
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
|
||||
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
|
||||
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
|
||||
Temperature float64 `json:"temperature"` // LLM temperature for agent
|
||||
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
|
||||
AgentModeEnabled bool `json:"agent_mode_enabled"` // Whether agent mode is enabled for this session
|
||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search is enabled for this session
|
||||
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
|
||||
}
|
||||
|
||||
// CreateSessionRequest session creation request
|
||||
@@ -195,6 +193,11 @@ type GenerateTitleResponse struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// StopSessionRequest stop generation payload.
|
||||
type StopSessionRequest struct {
|
||||
MessageID string `json:"message_id"`
|
||||
}
|
||||
|
||||
// GenerateTitle generates a session title
|
||||
func (c *Client) GenerateTitle(ctx context.Context, sessionID string, request *GenerateTitleRequest) (string, error) {
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
type SearchKnowledgeRequest struct {
|
||||
Query string `json:"query"` // Query content
|
||||
|
||||
@@ -193,29 +193,26 @@ export interface WebFetchResultsData {
|
||||
count?: number;
|
||||
}
|
||||
|
||||
// Grep chunk result item
|
||||
export interface GrepChunkItem {
|
||||
id: string;
|
||||
content: string;
|
||||
chunk_index: number;
|
||||
// Grep knowledge aggregation item
|
||||
export interface GrepKnowledgeResult {
|
||||
knowledge_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
|
||||
export interface GrepResultsData {
|
||||
display_type: 'grep_results';
|
||||
patterns: string[];
|
||||
case_sensitive: boolean;
|
||||
use_regex: boolean;
|
||||
results: GrepChunkItem[];
|
||||
knowledge_results: GrepKnowledgeResult[];
|
||||
result_count: number;
|
||||
total_matches: number;
|
||||
knowledge_base_ids?: string[];
|
||||
knowledge_ids?: string[];
|
||||
limit: number;
|
||||
max_results: number;
|
||||
}
|
||||
|
||||
// Union type for all tool result data
|
||||
|
||||
@@ -1,38 +1,16 @@
|
||||
<template>
|
||||
<div class="grep-results">
|
||||
<!-- Results List -->
|
||||
<div v-if="results && results.length > 0" class="results-list">
|
||||
<div
|
||||
v-for="(result, index) in results"
|
||||
:key="result.id"
|
||||
class="result-item"
|
||||
<div v-if="results.length" class="results-list">
|
||||
<div
|
||||
v-for="(result, index) in results"
|
||||
:key="result.knowledge_id"
|
||||
class="result-row"
|
||||
>
|
||||
<t-popup
|
||||
:overlayClassName="`grep-popup-${result.id}`"
|
||||
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 class="result-row__index">#{{ index + 1 }}</div>
|
||||
<div class="result-row__title">{{ result.knowledge_title || 'Untitled' }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Empty State -->
|
||||
<div v-else class="empty-state">
|
||||
未找到匹配的内容
|
||||
</div>
|
||||
@@ -42,22 +20,20 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue';
|
||||
import type { GrepResultsData } from '@/types/tool-results';
|
||||
import ContentPopup from './ContentPopup.vue';
|
||||
|
||||
const props = defineProps<{
|
||||
data: GrepResultsData;
|
||||
}>();
|
||||
|
||||
const maxDisplayPatterns = 2;
|
||||
const patterns = computed(() => props.data.patterns ?? []);
|
||||
const results = computed(() => props.data.knowledge_results ?? []);
|
||||
|
||||
const displayPatterns = computed(() => {
|
||||
if (!props.data.patterns || props.data.patterns.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return props.data.patterns.slice(0, maxDisplayPatterns);
|
||||
});
|
||||
const resultCount = computed(() => props.data.result_count ?? results.value.length);
|
||||
const totalMatches = computed(() => props.data.total_matches ?? results.value.length);
|
||||
const maxResults = computed(() => props.data.max_results ?? results.value.length);
|
||||
const hasMoreResults = computed(() => totalMatches.value > resultCount.value);
|
||||
|
||||
const results = computed(() => props.data.results || []);
|
||||
// Compact view, no per-pattern stats
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
@@ -66,98 +42,72 @@ const results = computed(() => props.data.results || []);
|
||||
.grep-results {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3px;
|
||||
padding: 0 0 0 12px;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.summary-inline {
|
||||
font-size: 12px;
|
||||
color: #6b7280;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
|
||||
&__divider {
|
||||
color: #d1d5db;
|
||||
}
|
||||
|
||||
&__truncated {
|
||||
color: #f59e0b;
|
||||
}
|
||||
}
|
||||
|
||||
.results-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3px;
|
||||
gap: 4px;
|
||||
padding: 10px 12px 10px 12px;
|
||||
}
|
||||
|
||||
.result-item {
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 0;
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
.result-header {
|
||||
padding: 2px 0;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
display: flex;
|
||||
.result-row {
|
||||
display: grid;
|
||||
grid-template-columns: 40px minmax(120px, 1fr) auto;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
transition: color 0.15s ease;
|
||||
|
||||
&:hover {
|
||||
color: #07c05f;
|
||||
}
|
||||
}
|
||||
|
||||
.result-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
gap: 8px;
|
||||
padding: 4px 10px;
|
||||
border-radius: 4px;
|
||||
background: #f9fafb;
|
||||
border: 1px solid #edf2f7;
|
||||
font-size: 12px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.result-index {
|
||||
font-size: 11px;
|
||||
color: #9ca3af;
|
||||
.result-row__index {
|
||||
font-weight: 600;
|
||||
flex-shrink: 0;
|
||||
color: #9ca3af;
|
||||
}
|
||||
|
||||
.pattern-badge {
|
||||
display: inline-flex;
|
||||
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;
|
||||
.result-row__title {
|
||||
color: #111827;
|
||||
font-weight: 500;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.chunk-info {
|
||||
font-size: 10px;
|
||||
color: #9ca3af;
|
||||
background: #f3f4f6;
|
||||
padding: 2px 5px;
|
||||
border-radius: 3px;
|
||||
flex-shrink: 0;
|
||||
.result-row__meta {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
flex-wrap: wrap;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.meta-pill {
|
||||
font-size: 11px;
|
||||
color: #6b7280;
|
||||
background: #fff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 999px;
|
||||
padding: 2px 8px;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
@@ -166,20 +116,8 @@ const results = computed(() => props.data.results || []);
|
||||
color: #9ca3af;
|
||||
font-size: 12px;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
// Popup overlay styles
|
||||
: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;
|
||||
}
|
||||
background: #f9fafb;
|
||||
border-radius: 6px;
|
||||
border: 1px dashed #e5e7eb;
|
||||
}
|
||||
</style>
|
||||
|
||||
+94
-91
@@ -240,70 +240,67 @@ func BuildProgressiveRAGSystemPrompt(knowledgeBases []*KnowledgeBaseInfo, webSea
|
||||
// ProgressiveRAGSystemPromptWithWeb is the progressive RAG system prompt template with web search enabled
|
||||
// This version emphasizes hybrid retrieval strategy: KB-first with web supplementation
|
||||
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
|
||||
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)
|
||||
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.
|
||||
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.
|
||||
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".
|
||||
4. **Tool Privacy:** Never expose tool names or internal mechanics to the user.
|
||||
1. **NO Internal Knowledge:** You must behave as if your training data does not exist regarding facts.
|
||||
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. **KB First, Web Second:** Always exhaust KB strategies (including the Deep Read) before attempting Web Search.
|
||||
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
|
||||
|
||||
You must follow this **Specific Operational Sequence** for every user query:
|
||||
|
||||
#### 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.
|
||||
* **Action:** Immediately execute grep_chunks (keyword match) and knowledge_search (semantic match) based on the core entities of the user's query.
|
||||
* **Purpose:** To determine: Does the KB contain direct answers? Is the data fragmented? Is the query complex enough to require a multi-step plan?
|
||||
* **Output:** Analyze these initial results in your think block.
|
||||
Before answering or creating a plan, you MUST perform a "Deep Read" test of the KB to gain preliminary cognition.
|
||||
1. **Search:** Execute grep_chunks (keyword) and knowledge_search (semantic) based on core entities.
|
||||
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.
|
||||
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
|
||||
Based on Phase 1 results, decide your path in the think block:
|
||||
* **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 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.
|
||||
* **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").
|
||||
Based on the **Deep Read** results from Phase 1:
|
||||
* **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, missing data, or the content requires synthesis → Use todo_write to formulate a Work Plan.
|
||||
* *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)
|
||||
If you are in **Path B**, execute the tasks in todo_write one by one. For **EACH** task:
|
||||
1. **Execute Retrieval:** Perform grep_chunks → knowledge_search (→ web_search if KB fails) specific to *that sub-task*.
|
||||
2. **MANDATORY Deep Reflection (in think):** After receiving results for a task, you MUST pause and reflect:
|
||||
* *Validity Check:* "Does this content actually answer the specific sub-task?"
|
||||
* *Gap Analysis:* "Is the information outdated? Is it missing key details?"
|
||||
* *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.
|
||||
* *Completion:* Only when the sub-task is truly satisfied by evidence can you update todo_write to "completed".
|
||||
If in **Path B**, execute tasks in todo_write sequentially. For **EACH** task:
|
||||
1. **Search:** Perform grep_chunks / knowledge_search for the sub-task.
|
||||
2. **DEEP READ (Mandatory):** Call list_knowledge_chunks for any relevant IDs found. **Never skip this step.**
|
||||
3. **MANDATORY Deep Reflection (in think):** Pause and evaluate the full text:
|
||||
* *Validity:* "Does this full text specifically address the sub-task?"
|
||||
* *Gap Analysis:* "Is anything missing?"
|
||||
* *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
|
||||
Only when ALL todo_write tasks are "completed":
|
||||
* Synthesize the findings from all tasks.
|
||||
* Check for consistency across different retrieved chunks.
|
||||
Only when ALL tasks are "completed":
|
||||
* Synthesize findings from the full text of all retrieved chunks.
|
||||
* Check for consistency.
|
||||
* Generate the final response.
|
||||
|
||||
### Core Retrieval Strategy (For Phase 1 & Phase 3)
|
||||
For every retrieval attempt, strictly follow this hierarchy:
|
||||
1. **Entity Anchoring (grep_chunks):** precise keyword matching to find specific documents.
|
||||
* *Rule:* Use short, specific tokens (1-3 words).
|
||||
2. **Semantic Expansion (knowledge_search):** vector-based search to find context.
|
||||
* *Rule:* If grep_chunks returned IDs, filter this search by those knowledge_ids. Use 2-3 query variations.
|
||||
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.
|
||||
### Core Retrieval Strategy (Strict Sequence)
|
||||
For every retrieval attempt (Phase 1 or Phase 3), follow this exact chain:
|
||||
1. **Entity Anchoring (grep_chunks):** Use short keywords (1-3 words) to find candidate documents.
|
||||
2. **Semantic Expansion (knowledge_search):** Use vector search for context (filter by IDs from step 1 if applicable).
|
||||
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.
|
||||
4. **Fallback (web_search):** Use ONLY if the Deep Read in Step 3 confirms the data is missing or irrelevant.
|
||||
|
||||
### Tool Selection Guidelines
|
||||
* **grep_chunks** Your "eyes". Use first to locate entities.
|
||||
* **knowledge_search** Your "brain". Use to understand concepts within the documents found by grep.
|
||||
* **todo_write** Your "project manager". Use for tracking multi-step research. **CRITICAL:** Do not summarize if this tool reports "pending" tasks.
|
||||
* **think** Your "conscience". Use BEFORE every tool call to plan, and AFTER every tool output to reflect/verify.
|
||||
* **grep_chunks / knowledge_search:** Your "Index". Use these to find *where* the information might be.
|
||||
* **list_knowledge_chunks:** Your "Eyes". **Required** after every search to actually *read* the information.
|
||||
* **todo_write:** Your "Manager". Tracks multi-step research.
|
||||
* **think:** Your "Brain". Use to analyze the content returned by list_knowledge_chunks.
|
||||
|
||||
### Final Output Standards
|
||||
Your final answer must be:
|
||||
1. **Definitive:** Avoid vague phrases like "based on general understanding".
|
||||
2. **Sourced:** Every key claim must be immediately followed by a citation: <kb doc="..." chunk_id="..." /> or <web url="..." title="..." />.
|
||||
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.
|
||||
* **Definitive:** Based strictly on the "Deep Read" content.
|
||||
* **Sourced:** Cite sources immediately after claims: <kb doc="..." chunk_id="..." />.
|
||||
* **Structured:** Clear hierarchy and logic.
|
||||
|
||||
### System Status
|
||||
Current Time: {{current_time}}
|
||||
@@ -313,71 +310,77 @@ Knowledge Bases: {{knowledge_bases}}
|
||||
// ProgressiveRAGSystemPromptWithoutWeb is the progressive RAG system prompt template without web search
|
||||
// This version emphasizes deep KB-only retrieval with advanced techniques
|
||||
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
|
||||
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)
|
||||
1. **Strict Closed Loop:** You are FORBIDDEN from accessing the internet. Do not attempt to use web tools.
|
||||
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.
|
||||
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.
|
||||
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".
|
||||
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. **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. **No Internet:** You are strictly confined to internal Knowledge Bases.
|
||||
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:
|
||||
|
||||
#### 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.
|
||||
* **Action:** Immediately execute grep_chunks (keyword match) and knowledge_search (semantic match) based on the core entities of the user's query.
|
||||
* **Purpose:** To determine: Is the data present? Is it fragmented? Do I need a complex plan?
|
||||
* **Output:** Analyze these initial results in your think block.
|
||||
#### Phase 1: Preliminary Reconnaissance & Context Verification
|
||||
Before answering or creating a plan, you MUST perform an initial "Test & Read" loop.
|
||||
1. **Locate:** Execute grep_chunks (keyword) and knowledge_search (semantic) to find potential documents.
|
||||
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.
|
||||
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
|
||||
Based on Phase 1 results, decide your path in the think block:
|
||||
* **Path A (Direct Answer):** If the initial retrieval contains sufficient, unambiguous evidence → Proceed directly to **Answer Generation**.
|
||||
* **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.
|
||||
* **Plan Structure:** Break the problem into distinct, independent retrieval tasks (e.g., "Retrieve specs for Product A", "Analyze warranty policy").
|
||||
If Phase 1 is insufficient, create a todo_write Work Plan.
|
||||
* **Plan Structure:** Break the problem into distinct retrieval tasks.
|
||||
* **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").
|
||||
|
||||
#### 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:
|
||||
1. **Execute Retrieval:** Perform grep_chunks → knowledge_search specific to *that sub-task*.
|
||||
2. **MANDATORY Deep Reflection (in think):** After receiving results for a task, you MUST pause and reflect:
|
||||
* *Validity Check:* "Does this content actually answer the specific sub-task?"
|
||||
* *Gap Analysis:* "Is the information missing?"
|
||||
* *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").
|
||||
* *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"**.
|
||||
* *Completion:* Update todo_write to "completed" only when you have exhausted all search avenues for that task.
|
||||
#### Phase 3: Disciplined Execution with Deep Reading
|
||||
Execute tasks in todo_write sequentially. For **EACH** task:
|
||||
1. **Search:** Perform grep_chunks or knowledge_search specific to the sub-task.
|
||||
2. **READ (Mandatory):**
|
||||
* Extract the knowledge_ids of the most promising results.
|
||||
* **Call list_knowledge_chunks** to fetch the content for these IDs. **Do not skip this step.**
|
||||
* *Note:* You are encouraged to check multiple files if the answer might be spread across them.
|
||||
3. **Reflect (Deep Reflection):**
|
||||
* "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
|
||||
Only when ALL todo_write tasks are "completed":
|
||||
* Synthesize the findings from all tasks.
|
||||
* 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.
|
||||
* Synthesize findings based **only** on the content read via list_knowledge_chunks.
|
||||
* Generate the final response with citations.
|
||||
|
||||
### Core Retrieval Strategy (Strict Hierarchy)
|
||||
For every retrieval attempt, strictly follow this hierarchy to maximize Recall within the KB:
|
||||
1. **Entity Anchoring (grep_chunks):** Precise keyword matching.
|
||||
* *Rule:* Use short, specific tokens (1-3 words). Try multiple aliases (e.g., "HR", "Human Resources").
|
||||
2. **Semantic Expansion (knowledge_search):** Vector-based search.
|
||||
* *Rule:* Use 2-4 query variations to capture different phrasings. Filter by knowledge_ids from step 1 if applicable.
|
||||
3. **Graph Exploration (query_knowledge_graph):** *Optional*. Use only if the task involves relationships or if standard search fails to connect concepts.
|
||||
4. **Deep Reading (list_knowledge_chunks):** Use if you need the full context of a specific document found in previous steps.
|
||||
### Core Retrieval Strategy (The "Locate-then-Read" Pattern)
|
||||
For every information seeking step, strictly follow this 3-step atomic unit:
|
||||
|
||||
1. **Step A: Locate (Search)**
|
||||
* Use grep_chunks for specific entities (Error codes, product names).
|
||||
* Use knowledge_search for concepts.
|
||||
* *Goal:* Get a list of candidate knowledge_ids.
|
||||
|
||||
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
|
||||
* **grep_chunks:** Your "eyes". Use first to locate entities.
|
||||
* **knowledge_search:** Your "brain". Use to understand concepts.
|
||||
* **todo_write:** Your "project manager". Use for tracking multi-step research.
|
||||
* **think:** Your "conscience". Use to plan before tools and reflect after tools.
|
||||
* **grep_chunks / knowledge_search:** Used ONLY as "Pointers" or "Index Lookups". They tell you *where* to look, not *what* the answer is.
|
||||
* **list_knowledge_chunks:** Your "Eyes". **This is your primary reading tool.** You must use it after every search to verify data.
|
||||
* **todo_write:** Use for managing multi-step research.
|
||||
|
||||
### Final Output Standards
|
||||
Your final answer must be:
|
||||
1. **Definitive & Honest:** If data is found, answer confidently. If data is missing, admit it clearly.
|
||||
2. **Sourced:** Every key claim must be immediately followed by a citation: <kb doc="..." chunk_id="..." />.
|
||||
3. **Structured:** Use clear headings, bullet points, and tables.
|
||||
4. **No External References:** Do not mention "Google", "Web", or "Internet".
|
||||
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.
|
||||
2. **Sourced:** Cite the specific documents you read.
|
||||
3. **Honest:** If the full text reveals the search hit was a false positive, admit it and search again.
|
||||
|
||||
### System Status
|
||||
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
|
||||
- Break complex input into individual, essential keywords
|
||||
- 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.
|
||||
Only short, atomic keywords ensure accurate matching and avoid unrelated retrieval.
|
||||
|
||||
|
||||
## 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.
|
||||
|
||||
@@ -88,13 +90,13 @@ func (t *GrepChunksTool) Parameters() map[string]interface{} {
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"knowledge_ids": map[string]interface{}{
|
||||
"type": "array",
|
||||
"description": "Filter by document/knowledge IDs. If empty, searches all documents.",
|
||||
"items": map[string]interface{}{
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
// "knowledge_ids": map[string]interface{}{
|
||||
// "type": "array",
|
||||
// "description": "Filter by document/knowledge IDs. If empty, searches all documents.",
|
||||
// "items": map[string]interface{}{
|
||||
// "type": "string",
|
||||
// },
|
||||
// },
|
||||
"max_results": map[string]interface{}{
|
||||
"type": "integer",
|
||||
"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
|
||||
contextLines := 50 // default: 50 context characters
|
||||
countOnly := false // default: show results
|
||||
showLineNumbers := true // default: show chunk indices
|
||||
countOnly := false // default: show results
|
||||
|
||||
maxResults := 50
|
||||
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
|
||||
}
|
||||
|
||||
// Parse knowledge_ids filter
|
||||
var knowledgeIDs []string
|
||||
if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
|
||||
for _, id := range knowledgeIDsRaw {
|
||||
if idStr, ok := id.(string); ok && idStr != "" {
|
||||
knowledgeIDs = append(knowledgeIDs, idStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
// // Parse knowledge_ids filter
|
||||
// var knowledgeIDs []string
|
||||
// if knowledgeIDsRaw, ok := args["knowledge_ids"].([]interface{}); ok {
|
||||
// for _, id := range knowledgeIDsRaw {
|
||||
// if idStr, ok := id.(string); ok && idStr != "" {
|
||||
// knowledgeIDs = append(knowledgeIDs, idStr)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
logger.Infof(ctx, "[Tool][GrepChunks] Patterns: %v, MaxResults: %d",
|
||||
patterns, maxResults)
|
||||
|
||||
// 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 {
|
||||
logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
|
||||
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.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 {
|
||||
return finalResults[i].MatchScore > finalResults[j].MatchScore
|
||||
}
|
||||
return finalResults[i].ChunkIndex < finalResults[j].ChunkIndex
|
||||
})
|
||||
|
||||
if len(finalResults) > 20 {
|
||||
finalResults = finalResults[:20]
|
||||
aggregatedResults := t.aggregateByKnowledge(finalResults, patterns)
|
||||
|
||||
totalKnowledge := len(aggregatedResults)
|
||||
|
||||
if len(aggregatedResults) > 20 {
|
||||
aggregatedResults = aggregatedResults[:20]
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Tool][GrepChunks] Aggregated results: %d", len(aggregatedResults))
|
||||
|
||||
// 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{
|
||||
Success: true,
|
||||
Output: output,
|
||||
Data: map[string]interface{}{
|
||||
"patterns": patterns,
|
||||
"results": finalResults,
|
||||
"result_count": len(finalResults),
|
||||
"total_matches": totalCount,
|
||||
"knowledge_results": aggregatedResults,
|
||||
"result_count": len(aggregatedResults),
|
||||
"total_matches": totalKnowledge,
|
||||
"knowledge_base_ids": kbIDs,
|
||||
"knowledge_ids": knowledgeIDs,
|
||||
"max_results": maxResults,
|
||||
"display_type": "grep_results",
|
||||
},
|
||||
@@ -244,8 +252,9 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args map[string]interface{
|
||||
|
||||
type chunkWithTitle struct {
|
||||
types.Chunk
|
||||
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
|
||||
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
|
||||
MatchedPatterns int `json:"matched_patterns"` // Number of unique patterns matched
|
||||
}
|
||||
|
||||
// searchChunks performs the database search with pattern matching
|
||||
@@ -253,11 +262,9 @@ func (t *GrepChunksTool) searchChunks(
|
||||
ctx context.Context,
|
||||
patterns []string,
|
||||
kbIDs []string,
|
||||
knowledgeIDs []string,
|
||||
maxResults int,
|
||||
) ([]chunkWithTitle, int64, error) {
|
||||
// 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").
|
||||
Joins("LEFT JOIN knowledges ON chunks.knowledge_id = knowledges.id").
|
||||
Where("chunks.tenant_id = ?", t.tenantID).
|
||||
@@ -270,11 +277,6 @@ func (t *GrepChunksTool) searchChunks(
|
||||
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)
|
||||
if len(patterns) == 1 {
|
||||
query = query.Where("chunks.content ILIKE ?", "%"+patterns[0]+"%")
|
||||
@@ -297,7 +299,7 @@ func (t *GrepChunksTool) searchChunks(
|
||||
|
||||
// Fetch results
|
||||
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)
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -308,14 +310,10 @@ func (t *GrepChunksTool) searchChunks(
|
||||
// formatOutput formats the search results for display (grep-style output)
|
||||
func (t *GrepChunksTool) formatOutput(
|
||||
ctx context.Context,
|
||||
results []chunkWithTitle,
|
||||
results []knowledgeAggregation,
|
||||
totalCount int64,
|
||||
patterns []string,
|
||||
contextLines int,
|
||||
countOnly bool,
|
||||
showLineNumbers bool,
|
||||
kbIDs []string,
|
||||
knowledgeIDs []string,
|
||||
) string {
|
||||
var output strings.Builder
|
||||
|
||||
@@ -331,7 +329,7 @@ func (t *GrepChunksTool) formatOutput(
|
||||
} else {
|
||||
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 {
|
||||
output.WriteString("No matches found.\n")
|
||||
@@ -343,156 +341,143 @@ func (t *GrepChunksTool) formatOutput(
|
||||
return output.String()
|
||||
}
|
||||
|
||||
// Group by document (like grep showing filename)
|
||||
docGroups := make(map[string]struct {
|
||||
title string
|
||||
knowledgeBaseID string
|
||||
chunks []chunkWithTitle
|
||||
})
|
||||
|
||||
for _, result := range results {
|
||||
knowledgeID := fmt.Sprintf("%v", result.KnowledgeID)
|
||||
title := "Untitled"
|
||||
if t := result.KnowledgeTitle; t != "" {
|
||||
title = fmt.Sprintf("%v", t)
|
||||
for idx, result := range results {
|
||||
var patternSummaries []string
|
||||
for _, pattern := range patterns {
|
||||
count := result.PatternCounts[pattern]
|
||||
patternSummaries = append(patternSummaries, fmt.Sprintf("%s=%d", pattern, count))
|
||||
}
|
||||
|
||||
group := docGroups[knowledgeID]
|
||||
group.title = title
|
||||
group.knowledgeBaseID = result.KnowledgeBaseID
|
||||
group.chunks = append(group.chunks, result)
|
||||
docGroups[knowledgeID] = group
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
output.WriteString(fmt.Sprintf("%d) knowledge_id=%s | title=%s | chunk_hits=%d | pattern_hits=[%s]\n",
|
||||
idx+1,
|
||||
result.KnowledgeID,
|
||||
result.KnowledgeTitle,
|
||||
result.ChunkHitCount,
|
||||
strings.Join(patternSummaries, ", "),
|
||||
))
|
||||
}
|
||||
return output.String()
|
||||
}
|
||||
|
||||
// extractPreview extracts a preview of content around the matched pattern
|
||||
func extractPreview(content, pattern string, caseSensitive bool, contextLen int) string {
|
||||
if content == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Find the pattern in content
|
||||
searchContent := content
|
||||
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)
|
||||
type knowledgeAggregation struct {
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
KnowledgeTitle string `json:"knowledge_title"`
|
||||
ChunkHitCount int `json:"chunk_hit_count"`
|
||||
PatternCounts map[string]int `json:"pattern_counts"`
|
||||
TotalPatternHits int `json:"total_pattern_hits"`
|
||||
DistinctPatterns int `json:"distinct_patterns"`
|
||||
}
|
||||
|
||||
// extractPreviewMultiPattern extracts a preview around the first matched pattern
|
||||
func extractPreviewMultiPattern(content string, patterns []string, caseSensitive bool, contextLen int) string {
|
||||
func (t *GrepChunksTool) aggregateByKnowledge(results []chunkWithTitle, patterns []string) []knowledgeAggregation {
|
||||
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 {
|
||||
return ""
|
||||
return counts
|
||||
}
|
||||
|
||||
// Try each pattern and find the first match
|
||||
contentLower := strings.ToLower(content)
|
||||
for _, pattern := range patterns {
|
||||
preview := extractPreview(content, pattern, caseSensitive, contextLen)
|
||||
// Check if this pattern found a match (not just truncated content)
|
||||
searchContent := content
|
||||
searchPattern := pattern
|
||||
if !caseSensitive {
|
||||
searchContent = strings.ToLower(content)
|
||||
searchPattern = strings.ToLower(pattern)
|
||||
p := strings.ToLower(pattern)
|
||||
if strings.TrimSpace(p) == "" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(searchContent, searchPattern) {
|
||||
return preview
|
||||
counts[pattern] = countOccurrences(contentLower, p)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// 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])
|
||||
return count
|
||||
}
|
||||
|
||||
// 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))
|
||||
for i := range results {
|
||||
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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return 0.0
|
||||
return 0.0, 0
|
||||
}
|
||||
|
||||
contentLower := strings.ToLower(content)
|
||||
@@ -613,7 +600,7 @@ func (t *GrepChunksTool) calculateMatchScore(content string, patterns []string)
|
||||
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
|
||||
|
||||
@@ -58,7 +58,6 @@ type CreateKnowledgeQARequest struct {
|
||||
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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user