Changeset 0.22.0.1 (#94)

This commit is contained in:
2026-03-02 01:55:19 +00:00
parent 12e03cec8b
commit 06c4e2a5a1
33 changed files with 1929 additions and 418 deletions

View File

@@ -27,6 +27,20 @@ type streamResult struct {
CacheReadTokens int
}
// recordHealthFn records provider health from standalone streaming functions.
func recordHealthFn(hr HealthRecorder, configID string, start time.Time, err error) {
if hr == nil || configID == "" {
return
}
latencyMs := int(time.Since(start).Milliseconds())
if err != nil {
errMsg := err.Error()
hr.RecordError(configID, latencyMs, errMsg)
} else {
hr.RecordSuccess(configID, latencyMs)
}
}
// streamWithToolLoop is the single, canonical streaming implementation.
// It handles SSE setup, multi-round tool execution, reasoning_content
// forwarding, and client notifications. Returns the accumulated content
@@ -43,8 +57,9 @@ func streamWithToolLoop(
provider providers.Provider,
cfg providers.ProviderConfig,
req *providers.CompletionRequest,
model, userID, channelID, personaID, workspaceID string,
model, userID, channelID, personaID, workspaceID, configID string,
hub *events.Hub,
health HealthRecorder,
) streamResult {
// Set SSE headers
c.Header("Content-Type", "text/event-stream")
@@ -74,8 +89,10 @@ func streamWithToolLoop(
var result streamResult
for iteration := 0; iteration < maxToolIterations; iteration++ {
callStart := time.Now()
ch, err := provider.StreamCompletion(c.Request.Context(), cfg, *req)
if err != nil {
recordHealthFn(health, configID, callStart, err)
sendSSE(fmt.Sprintf(`{"error":"%s"}`, escapeJSON(err.Error())))
return result
}
@@ -86,6 +103,7 @@ func streamWithToolLoop(
for event := range ch {
if event.Error != nil {
recordHealthFn(health, configID, callStart, event.Error)
sendSSE(fmt.Sprintf(`{"error":"%s"}`, escapeJSON(event.Error.Error())))
return result
}
@@ -107,8 +125,9 @@ func streamWithToolLoop(
}
if event.Done {
recordHealthFn(health, configID, callStart, nil)
if event.FinishReason == "tool_calls" && len(event.ToolCalls) > 0 {
toolCalls = event.ToolCalls
toolCalls = event.ToolCalls
// Accumulate tokens from this tool-call iteration
result.InputTokens += event.InputTokens
result.OutputTokens += event.OutputTokens
@@ -268,8 +287,9 @@ func streamModelResponse(
provider providers.Provider,
cfg providers.ProviderConfig,
req *providers.CompletionRequest,
model, displayName, userID, channelID, personaID, workspaceID string,
model, displayName, userID, channelID, personaID, workspaceID, configID string,
hub *events.Hub,
health HealthRecorder,
) streamResult {
flusher, _ := c.Writer.(http.Flusher)
flush := func() {
@@ -289,8 +309,10 @@ func streamModelResponse(
var result streamResult
for iteration := 0; iteration < maxToolIterations; iteration++ {
callStart := time.Now()
ch, err := provider.StreamCompletion(c.Request.Context(), cfg, *req)
if err != nil {
recordHealthFn(health, configID, callStart, err)
sendSSE(fmt.Sprintf(`{"error":"%s"}`, escapeJSON(err.Error())))
return result
}
@@ -301,6 +323,7 @@ func streamModelResponse(
for event := range ch {
if event.Error != nil {
recordHealthFn(health, configID, callStart, event.Error)
sendSSE(fmt.Sprintf(`{"error":"%s"}`, escapeJSON(event.Error.Error())))
return result
}
@@ -320,6 +343,7 @@ func streamModelResponse(
}
if event.Done {
recordHealthFn(health, configID, callStart, nil)
if event.FinishReason == "tool_calls" && len(event.ToolCalls) > 0 {
toolCalls = event.ToolCalls
result.InputTokens += event.InputTokens