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
|
||||
|
||||
Reference in New Issue
Block a user