Skip to content

Commit 72a3cbb

Browse files
committed
feat: 增加流式响应类型和工具调用支持
新增 LLMToolCall 和 FunctionCall 结构体支持工具调用,扩展 ResponseType 枚举添加多种响应类型,优化 StreamResponse 结构体增加会话和工具调用字段,并改进引用事件处理逻辑。
1 parent c5228ac commit 72a3cbb

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

client/session.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,45 @@ type KnowledgeQARequest struct {
191191
DisableTitle bool `json:"disable_title"` // Whether to disable auto title generation
192192
}
193193

194+
// LLMToolCall represents a function/tool call from the LLM
195+
type LLMToolCall struct {
196+
ID string `json:"id"`
197+
Type string `json:"type"` // "function"
198+
Function FunctionCall `json:"function"`
199+
}
200+
201+
// FunctionCall represents the function details
202+
type FunctionCall struct {
203+
Name string `json:"name"`
204+
Arguments string `json:"arguments"` // JSON string
205+
}
206+
194207
type ResponseType string
195208

196209
const (
197-
ResponseTypeAnswer ResponseType = "answer"
198-
ResponseTypeReferences ResponseType = "references"
210+
ResponseTypeAnswer ResponseType = "answer"
211+
ResponseTypeReferences ResponseType = "references"
212+
ResponseTypeThinking ResponseType = "thinking"
213+
ResponseTypeToolCall ResponseType = "tool_call"
214+
ResponseTypeToolResult ResponseType = "tool_result"
215+
ResponseTypeError ResponseType = "error"
216+
ResponseTypeReflection ResponseType = "reflection"
217+
ResponseTypeSessionTitle ResponseType = "session_title"
218+
ResponseTypeAgentQuery ResponseType = "agent_query"
219+
ResponseTypeComplete ResponseType = "complete"
199220
)
200221

201222
// StreamResponse streaming response
202223
type StreamResponse struct {
203-
ID string `json:"id"` // Unique identifier
204-
ResponseType ResponseType `json:"response_type"` // Response type
205-
Content string `json:"content"` // Current content fragment
206-
Done bool `json:"done"` // Whether completed
207-
KnowledgeReferences []*SearchResult `json:"knowledge_references"` // Knowledge references
224+
ID string `json:"id"` // Unique identifier
225+
ResponseType ResponseType `json:"response_type"` // Response type
226+
Content string `json:"content"` // Current content fragment
227+
Done bool `json:"done"` // Whether completed
228+
KnowledgeReferences []*SearchResult `json:"knowledge_references,omitempty"` // Knowledge references
229+
SessionID string `json:"session_id,omitempty"` // Session ID (for agent_query event)
230+
AssistantMessageID string `json:"assistant_message_id,omitempty"` // Assistant Message ID (for agent_query event)
231+
ToolCalls []LLMToolCall `json:"tool_calls,omitempty"` // Tool calls for streaming (partial)
232+
Data map[string]interface{} `json:"data,omitempty"` // Additional metadata for enhanced display
208233
}
209234

210235
// KnowledgeQAStream knowledge Q&A streaming API

internal/handler/session/helpers.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,19 @@ func buildStreamResponse(evt interfaces.StreamEvent, requestID string) *types.St
5959

6060
// Special handling for references event
6161
if evt.Type == types.ResponseTypeReferences {
62-
if refs, ok := evt.Data["references"].(types.References); ok {
62+
refsData := evt.Data["references"]
63+
logger.GetLogger(context.Background()).Info("buildStreamResponse references event",
64+
"refsData_type", fmt.Sprintf("%T", refsData),
65+
"refsData", refsData)
66+
if refs, ok := refsData.(types.References); ok {
67+
logger.GetLogger(context.Background()).Info("buildStreamResponse: matched types.References")
6368
response.KnowledgeReferences = refs
69+
} else if refs, ok := refsData.([]*types.SearchResult); ok {
70+
logger.GetLogger(context.Background()).Info("buildStreamResponse: matched []*types.SearchResult")
71+
response.KnowledgeReferences = types.References(refs)
72+
} else {
73+
logger.GetLogger(context.Background()).Warn("buildStreamResponse: references type assertion failed",
74+
"actual_type", fmt.Sprintf("%T", refsData))
6475
}
6576
}
6677

0 commit comments

Comments
 (0)