Enhance stream data structures in Assistant for improved context and metadata handling

- Added Metadata field to StreamStartData and StreamEndData structs to facilitate passing additional context information.
- Updated sendAgentStreamStart and sendAgentStreamEnd methods to include new fields for better traceability and context management during streaming events.
- Enhanced the overall structure of stream data to support more comprehensive event tracking and context sharing.
This commit is contained in:
Max 2025-11-25 11:11:43 +08:00
parent bc3dd567b5
commit 130d765ec7
2 changed files with 20 additions and 12 deletions

View file

@ -710,6 +710,7 @@ func (ast *Assistant) sendAgentStreamStart(ctx *context.Context, handler context
RequestID: ctx.RequestID(),
Timestamp: startTime.UnixMilli(),
Assistant: ast.Info(ctx.Locale),
Metadata: ctx.Metadata,
}
if startJSON, err := jsoniter.Marshal(startData); err == nil {
@ -731,9 +732,12 @@ func (ast *Assistant) sendAgentStreamEnd(ctx *context.Context, handler context.S
endData := &context.StreamEndData{
RequestID: ctx.RequestID(),
ContextID: ctx.ID,
Timestamp: time.Now().UnixMilli(),
DurationMs: time.Since(startTime).Milliseconds(),
Status: status,
TraceID: ctx.TraceID(),
Metadata: ctx.Metadata,
}
if err != nil {

View file

@ -221,23 +221,27 @@ type JSONSchema struct {
// StreamStartData represents the data for stream_start event
// Sent when a streaming request begins
type StreamStartData struct {
ContextID string `json:"context_id"` // Context ID for the response
RequestID string `json:"request_id"` // Unique identifier for this request
Timestamp int64 `json:"timestamp"` // Unix timestamp when stream started
ChatID string `json:"chat_id"` // Chat ID being used (e.g., "chat-123")
TraceID string `json:"trace_id"` // Trace ID being used (e.g., "trace-123")
Assistant *AssistantInfo `json:"assistant,omitempty"` // Assistant information
ContextID string `json:"context_id"` // Context ID for the response
RequestID string `json:"request_id"` // Unique identifier for this request
Timestamp int64 `json:"timestamp"` // Unix timestamp when stream started
ChatID string `json:"chat_id"` // Chat ID being used (e.g., "chat-123")
TraceID string `json:"trace_id"` // Trace ID being used (e.g., "trace-123")
Assistant *AssistantInfo `json:"assistant,omitempty"` // Assistant information
Metadata map[string]interface{} `json:"metadata,omitempty"` // Metadata to pass to the page for CUI context
}
// StreamEndData represents the data for stream_end event
// Sent when a streaming request completes (successfully or with error)
type StreamEndData struct {
RequestID string `json:"request_id"` // Corresponding request ID
Timestamp int64 `json:"timestamp"` // Unix timestamp when stream ended
DurationMs int64 `json:"duration_ms"` // Total duration in milliseconds
Status string `json:"status"` // "completed" | "error" | "cancelled"
Error string `json:"error,omitempty"` // Error message if status is "error"
Usage *UsageInfo `json:"usage,omitempty"` // Token usage statistics
RequestID string `json:"request_id"` // Corresponding request ID
ContextID string `json:"context_id"` // Context ID for the response
TraceID string `json:"trace_id"` // Trace ID being used (e.g., "trace-123")
Timestamp int64 `json:"timestamp"` // Unix timestamp when stream ended
DurationMs int64 `json:"duration_ms"` // Total duration in milliseconds
Status string `json:"status"` // "completed" | "error" | "cancelled"
Error string `json:"error,omitempty"` // Error message if status is "error"
Usage *UsageInfo `json:"usage,omitempty"` // Token usage statistics
Metadata map[string]interface{} `json:"metadata,omitempty"` // Metadata to pass to the page for CUI context
}
// GroupStartData represents the data for group_start event