Files
zeekayandHanzo Dev c264aa6086 refactor(go-openai): import the hanzoai/go-openai fork directly, drop the replace
ai used the fork only through 'replace github.com/sashabaranov/go-openai =>
github.com/hanzoai/go-openai', and Go replace directives do NOT propagate to
consumers — so every downstream repo (cloud, gateway, iam, …) had to copy the
same replace or fail to compile against fork-only fields like
model/local.go's ReasoningContent. The fork now declares its own module path
(github.com/hanzoai/go-openai v1.41.0), so ai requires it DIRECTLY and every
consumer inherits it transitively. One and only one way — no per-repo replace.

22 import sites renamed (package identifier 'openai' unchanged). Upstream
sashabaranov/go-openai remains ONLY as an indirect dep of the go-cosyvoice /
go-openai-realtime TTS chain (a separate upstream we don't bridge types with;
that chain is being force-owned next). model + tts + object build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-17 22:07:39 -07:00

201 lines
5.6 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2025 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hanzoai/ai/util"
"github.com/hanzoai/ai/web"
"github.com/hanzoai/go-openai"
)
// OpenAIWriter implements a writer that formats responses in OpenAI format
type OpenAIWriter struct {
web.Response
Cleaner Cleaner
Buffer []byte
MessageBuf []byte
RequestID string
Stream bool
StreamSent bool
Model string
// IncludeUsage mirrors the request's stream_options.include_usage. Per the
// OpenAI spec the trailing empty-choices usage chunk is emitted ONLY when the
// client asks for it; sending it unconditionally breaks clients that read
// choices[0] on every chunk.
IncludeUsage bool
}
// Write processes incoming data chunks and formats them for OpenAI compatibility
func (w *OpenAIWriter) Write(p []byte) (n int, err error) {
// Parse the incoming SSE message format
var content string
if bytes.HasPrefix(p, []byte("event: message\ndata: ")) {
prefix := []byte("event: message\ndata: ")
suffix := []byte("\n\n")
content = string(bytes.TrimSuffix(bytes.TrimPrefix(p, prefix), suffix))
// Add content to message buffer
w.MessageBuf = append(w.MessageBuf, []byte(content)...)
} else if bytes.HasPrefix(p, []byte("event: reason\ndata: ")) {
// We don't expose reason data in OpenAI format, but we'll store it
prefix := []byte("event: reason\ndata: ")
suffix := []byte("\n\n")
content = string(bytes.TrimSuffix(bytes.TrimPrefix(p, prefix), suffix))
} else {
// If we can't parse, just store the raw bytes and attempt to clean
content = w.Cleaner.CleanString(string(p))
if content != "" {
w.MessageBuf = append(w.MessageBuf, []byte(content)...)
}
}
// Always store the original bytes
w.Buffer = append(w.Buffer, p...)
// For non-streaming, just collect the data
if !w.Stream {
return len(p), nil
}
// Skip empty content
if content == "" {
return len(p), nil
}
// OpenAI streams the assistant role on the FIRST delta chunk; clients
// (LangChain/LibreChat agents) read it to classify the streamed message.
// Omitting it breaks their parser with "Cannot read properties of undefined
// (reading 'role')" and the reply never renders.
delta := openai.ChatCompletionStreamChoiceDelta{Content: content}
if !w.StreamSent {
delta.Role = "assistant"
}
// Create SSE chunk using go-openai library structure
chunk := openai.ChatCompletionStreamResponse{
ID: "chatcmpl-" + w.RequestID,
Object: "chat.completion.chunk",
Created: util.GetCurrentUnixTime(),
Model: w.Model,
Choices: []openai.ChatCompletionStreamChoice{
{
Index: 0,
Delta: delta,
FinishReason: openai.FinishReasonNull,
},
},
}
jsonData, err := json.Marshal(chunk)
if err != nil {
return 0, err
}
// Send as SSE data chunk - use ResponseWriter to avoid recursion
_, err = w.ResponseWriter.Write([]byte(fmt.Sprintf("data: %s\n\n", jsonData)))
if err != nil {
return 0, err
}
w.StreamSent = true
w.Flush()
return len(p), nil
}
// MessageString returns the complete buffered message
func (w *OpenAIWriter) MessageString() string {
return string(w.MessageBuf)
}
// Close finalizes the stream by sending completion message and DONE marker
func (w *OpenAIWriter) Close(promptTokens, completionTokens, totalTokens int) error {
if !w.Stream {
return nil
}
if w.StreamSent {
// Send final message with finish_reason
chunk := openai.ChatCompletionStreamResponse{
ID: "chatcmpl-" + w.RequestID,
Object: "chat.completion.chunk",
Created: util.GetCurrentUnixTime(),
Model: w.Model,
Choices: []openai.ChatCompletionStreamChoice{
{
Index: 0,
Delta: openai.ChatCompletionStreamChoiceDelta{}, // Empty delta
FinishReason: openai.FinishReasonStop,
},
},
}
jsonData, err := json.Marshal(chunk)
if err != nil {
return err
}
_, err = w.ResponseWriter.Write([]byte(fmt.Sprintf("data: %s\n\n", jsonData)))
if err != nil {
return err
}
// Send usage as a proper OpenAI SSE chunk so SDK clients (v6+) can parse
// it — but ONLY when the client opted in via stream_options.include_usage.
// This chunk's choices array is always empty; emitting it unconditionally
// crashes clients that read choices[0] on every chunk.
if w.IncludeUsage {
usageChunk := map[string]interface{}{
"id": "chatcmpl-" + w.RequestID,
"object": "chat.completion.chunk",
"created": util.GetCurrentUnixTime(),
"model": w.Model,
"choices": []interface{}{},
"usage": openai.Usage{
PromptTokens: promptTokens,
CompletionTokens: completionTokens,
TotalTokens: totalTokens,
},
}
usageData, err := json.Marshal(usageChunk)
if err != nil {
return err
}
_, err = w.ResponseWriter.Write([]byte(fmt.Sprintf("data: %s\n\n", usageData)))
if err != nil {
return err
}
}
// Final [DONE] marker for SSE
_, err = w.ResponseWriter.Write([]byte("data: [DONE]\n\n"))
if err != nil {
return err
}
w.Flush()
}
return nil
}