Add OpenAI Responses API (/v1/responses) endpoint
Translate Responses API requests (input→messages, instructions→system,
max_output_tokens→max_tokens, text.format→response_format, flat tools→nested
{function:{…}}) to upstream chat/completions, then convert responses back to
Responses format (streaming SSE event lifecycle + non-streaming JSON).
Verified against OpenAI migration guide and Python SDK Response model:
- Echo back required fields parallel_tool_calls/tool_choice/tools
- Include content:[] in reasoning items, logprobs:[] in output_text parts
- Support function_call/function_call_output multi-turn input items
- Map usage fields prompt_tokens→input_tokens, completion_tokens→output_tokens
This commit is contained in:
@@ -0,0 +1,591 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.misaka.ren/M1saka/zhanlu_proxy/internal/openai"
|
||||
)
|
||||
|
||||
// responses handles POST /v1/responses — the OpenAI Responses API. It
|
||||
// translates the request to a chat-completions request for the upstream
|
||||
// gateway, then translates the response back into the Responses format.
|
||||
func (s *Server) responses(w http.ResponseWriter, r *http.Request) {
|
||||
creds, err := s.currentCredentials()
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusUnauthorized, "zhanlu credentials are not configured; open /login first", "auth_error", "missing_credentials")
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusBadRequest, err.Error(), "invalid_request_error", "bad_body")
|
||||
return
|
||||
}
|
||||
|
||||
chatReq, err := openai.ParseResponsesRequest(body)
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusBadRequest, err.Error(), "invalid_request_error", "bad_json")
|
||||
return
|
||||
}
|
||||
meta := openai.ParseResponsesMeta(body)
|
||||
if chatReq.Model == "" {
|
||||
chatReq.Model = "zhanlu/auto"
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
clientWantsStream := chatReq.Stream
|
||||
chatReq.Stream = true // upstream always streams
|
||||
upstreamBody, err := chatReq.MarshalForUpstream()
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusBadRequest, err.Error(), "invalid_request_error", "bad_body")
|
||||
return
|
||||
}
|
||||
|
||||
if !creds.HasAPIKey() {
|
||||
creds, err = s.provisionCredentials(r.Context(), creds)
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusBadGateway, "zhanlu api key provisioning failed: "+err.Error(), "auth_error", "zhanlu_provision_failed")
|
||||
return
|
||||
}
|
||||
s.cfg.Credentials = creds
|
||||
_ = s.st.SaveCredentials(creds)
|
||||
}
|
||||
modelBaseURL := firstNonEmpty(creds.ModelBaseURL, s.cfg.MobileModelBaseURL)
|
||||
client, err := s.zhanluClientWithBase(modelBaseURL)
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusInternalServerError, err.Error(), "sign_error", "signer_init_failed")
|
||||
return
|
||||
}
|
||||
resp, err := client.ChatCompletions(r.Context(), creds.APIKey, upstreamBody)
|
||||
if err != nil {
|
||||
msg := "zhanlu upstream request failed"
|
||||
if s.cfg.Debug {
|
||||
msg = redactSensitive(err.Error())
|
||||
}
|
||||
writeOpenAIError(w, http.StatusBadGateway, msg, "upstream_error", "zhanlu_request_failed")
|
||||
s.record(chatReq.Model, clientWantsStream, nil, "upstream_error", start)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
msg := fmt.Sprintf("zhanlu upstream returned %d", resp.StatusCode)
|
||||
if s.cfg.Debug && len(b) > 0 {
|
||||
msg += ": " + string(b)
|
||||
}
|
||||
writeOpenAIError(w, http.StatusBadGateway, msg, "upstream_error", "zhanlu_bad_status")
|
||||
s.record(chatReq.Model, clientWantsStream, nil, "upstream_error", start)
|
||||
return
|
||||
}
|
||||
|
||||
if clientWantsStream {
|
||||
usage, status := s.proxyResponsesStream(w, resp, chatReq.Model, meta)
|
||||
s.record(chatReq.Model, true, usage, status, start)
|
||||
return
|
||||
}
|
||||
usage, status := s.aggregateResponsesStream(w, resp, chatReq.Model, meta)
|
||||
s.record(chatReq.Model, false, usage, status, start)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- non-streaming
|
||||
|
||||
// aggregateResponsesStream reads the upstream SSE, aggregates it (same logic
|
||||
// as aggregateStream), then emits a Responses API JSON object.
|
||||
func (s *Server) aggregateResponsesStream(w http.ResponseWriter, resp *http.Response, model string, meta openai.ResponsesMeta) (any, string) {
|
||||
var content, reasoning, id string
|
||||
var usage any
|
||||
finishReason := "stop"
|
||||
type toolCall struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Function struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"`
|
||||
} `json:"function"`
|
||||
}
|
||||
toolCalls := map[int]*toolCall{}
|
||||
err := forEachSSEChunk(resp.Body, func(chunk []byte) error {
|
||||
var event struct {
|
||||
ID string `json:"id"`
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
Content string `json:"content"`
|
||||
ReasoningContent string `json:"reasoning_content"`
|
||||
Reasoning string `json:"reasoning"`
|
||||
ToolCalls []struct {
|
||||
Index int `json:"index"`
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Function struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"`
|
||||
} `json:"function"`
|
||||
} `json:"tool_calls"`
|
||||
} `json:"delta"`
|
||||
FinishReason *string `json:"finish_reason"`
|
||||
} `json:"choices"`
|
||||
Usage any `json:"usage"`
|
||||
}
|
||||
if err := json.Unmarshal(chunk, &event); err != nil {
|
||||
return err
|
||||
}
|
||||
if event.ID != "" {
|
||||
id = event.ID
|
||||
}
|
||||
if event.Usage != nil {
|
||||
usage = event.Usage
|
||||
}
|
||||
if len(event.Choices) > 0 {
|
||||
content += event.Choices[0].Delta.Content
|
||||
reasoning += event.Choices[0].Delta.ReasoningContent + event.Choices[0].Delta.Reasoning
|
||||
for _, part := range event.Choices[0].Delta.ToolCalls {
|
||||
call := toolCalls[part.Index]
|
||||
if call == nil {
|
||||
call = &toolCall{Type: "function"}
|
||||
toolCalls[part.Index] = call
|
||||
}
|
||||
if part.ID != "" {
|
||||
call.ID = part.ID
|
||||
}
|
||||
if part.Type != "" {
|
||||
call.Type = part.Type
|
||||
}
|
||||
call.Function.Name += part.Function.Name
|
||||
call.Function.Arguments += part.Function.Arguments
|
||||
}
|
||||
if event.Choices[0].FinishReason != nil {
|
||||
finishReason = *event.Choices[0].FinishReason
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
writeOpenAIError(w, http.StatusBadGateway, err.Error(), "upstream_error", "zhanlu_stream_error")
|
||||
return nil, "upstream_error"
|
||||
}
|
||||
|
||||
_ = id
|
||||
_ = finishReason
|
||||
respID := "resp_" + randomRequestID()
|
||||
output := []any{}
|
||||
|
||||
if reasoning != "" {
|
||||
output = append(output, map[string]any{
|
||||
"type": "reasoning", "id": "rs_" + randomRequestID(), "status": "completed",
|
||||
"content": []any{},
|
||||
"summary": []map[string]any{{"type": "summary_text", "text": reasoning}},
|
||||
})
|
||||
}
|
||||
|
||||
// Message output item — included when there is text content.
|
||||
if content != "" {
|
||||
output = append(output, map[string]any{
|
||||
"type": "message", "id": "msg_" + randomRequestID(), "status": "completed",
|
||||
"role": "assistant",
|
||||
"content": []map[string]any{{
|
||||
"type": "output_text", "text": content,
|
||||
"annotations": []any{}, "logprobs": []any{},
|
||||
}},
|
||||
})
|
||||
}
|
||||
|
||||
if len(toolCalls) > 0 {
|
||||
ordered := make([]*toolCall, 0, len(toolCalls))
|
||||
for i := 0; i < len(toolCalls); i++ {
|
||||
if call := toolCalls[i]; call != nil {
|
||||
ordered = append(ordered, call)
|
||||
}
|
||||
}
|
||||
for _, tc := range ordered {
|
||||
output = append(output, map[string]any{
|
||||
"type": "function_call", "id": "fc_" + randomRequestID(),
|
||||
"call_id": tc.ID, "name": tc.Function.Name,
|
||||
"arguments": tc.Function.Arguments, "status": "completed",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// If there was no content and no tool calls, still emit an empty message
|
||||
// so the response always has at least one output item.
|
||||
if len(output) == 0 {
|
||||
output = append(output, map[string]any{
|
||||
"type": "message", "id": "msg_" + randomRequestID(), "status": "completed",
|
||||
"role": "assistant", "content": []map[string]any{},
|
||||
})
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"id": respID, "object": "response", "created_at": time.Now().Unix(),
|
||||
"model": model, "status": "completed", "output": output,
|
||||
"parallel_tool_calls": meta.ParallelToolCalls,
|
||||
"tool_choice": meta.ToolChoice,
|
||||
"tools": meta.Tools,
|
||||
}
|
||||
if usage != nil {
|
||||
result["usage"] = openai.UsageToResponses(usage)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, result)
|
||||
return usage, "success"
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- streaming
|
||||
|
||||
// responsesStreamState tracks the lifecycle of output items while converting
|
||||
// Chat Completions SSE into Responses API SSE events.
|
||||
type responsesStreamState struct {
|
||||
w http.ResponseWriter
|
||||
flusher http.Flusher
|
||||
respID string
|
||||
msgID string
|
||||
rsID string
|
||||
meta openai.ResponsesMeta
|
||||
|
||||
outputIdx int
|
||||
reasoningOn bool
|
||||
reasonPartOn bool
|
||||
messageOn bool
|
||||
partOn bool
|
||||
fullContent string
|
||||
fullReasoning string
|
||||
usage any
|
||||
status string
|
||||
|
||||
// tool call tracking
|
||||
tools map[int]*streamToolCall
|
||||
toolOrder []int
|
||||
}
|
||||
|
||||
type streamToolCall struct {
|
||||
itemID string
|
||||
callID string
|
||||
name string
|
||||
args string
|
||||
started bool
|
||||
doneIdx int // assigned output index when started
|
||||
}
|
||||
|
||||
func newResponsesStreamState(w http.ResponseWriter, model string, meta openai.ResponsesMeta) *responsesStreamState {
|
||||
flusher, _ := w.(http.Flusher)
|
||||
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("X-Accel-Buffering", "no")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
st := &responsesStreamState{
|
||||
w: w,
|
||||
flusher: flusher,
|
||||
respID: "resp_" + randomRequestID(),
|
||||
msgID: "msg_" + randomRequestID(),
|
||||
rsID: "rs_" + randomRequestID(),
|
||||
meta: meta,
|
||||
tools: map[int]*streamToolCall{},
|
||||
status: "success",
|
||||
}
|
||||
baseResp := func(st2 string) map[string]any {
|
||||
return map[string]any{
|
||||
"id": st.respID, "object": "response", "created_at": time.Now().Unix(),
|
||||
"model": model, "status": st2, "output": []any{},
|
||||
"parallel_tool_calls": meta.ParallelToolCalls,
|
||||
"tool_choice": meta.ToolChoice,
|
||||
"tools": meta.Tools,
|
||||
}
|
||||
}
|
||||
st.emit("response.created", map[string]any{"type": "response.created", "response": baseResp("in_progress")})
|
||||
st.emit("response.in_progress", map[string]any{"type": "response.in_progress", "response": baseResp("in_progress")})
|
||||
return st
|
||||
}
|
||||
|
||||
func (st *responsesStreamState) emit(event string, data any) {
|
||||
b, _ := json.Marshal(data)
|
||||
fmt.Fprintf(st.w, "event: %s\ndata: %s\n\n", event, b)
|
||||
if st.flusher != nil {
|
||||
st.flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// closeReasoning closes the reasoning output item if it is open.
|
||||
func (st *responsesStreamState) closeReasoning() {
|
||||
if !st.reasoningOn {
|
||||
return
|
||||
}
|
||||
if st.reasonPartOn {
|
||||
st.emit("response.reasoning_summary_text.done", map[string]any{
|
||||
"type": "response.reasoning_summary_text.done", "item_id": st.rsID,
|
||||
"output_index": st.outputIdx, "summary_index": 0, "text": st.fullReasoning,
|
||||
})
|
||||
st.emit("response.reasoning_summary_part.done", map[string]any{
|
||||
"type": "response.reasoning_summary_part.done", "item_id": st.rsID,
|
||||
"output_index": st.outputIdx, "summary_index": 0,
|
||||
"part": map[string]any{"type": "summary_text", "text": st.fullReasoning},
|
||||
})
|
||||
st.reasonPartOn = false
|
||||
}
|
||||
st.emit("response.output_item.done", map[string]any{
|
||||
"type": "response.output_item.done", "output_index": st.outputIdx,
|
||||
"item": map[string]any{
|
||||
"type": "reasoning", "id": st.rsID, "status": "completed",
|
||||
"content": []any{},
|
||||
"summary": []map[string]any{{"type": "summary_text", "text": st.fullReasoning}},
|
||||
},
|
||||
})
|
||||
st.reasoningOn = false
|
||||
st.outputIdx++
|
||||
}
|
||||
|
||||
// closeMessage closes the message output item if it is open.
|
||||
func (st *responsesStreamState) closeMessage() {
|
||||
if !st.messageOn {
|
||||
return
|
||||
}
|
||||
if st.partOn {
|
||||
st.emit("response.output_text.done", map[string]any{
|
||||
"type": "response.output_text.done", "item_id": st.msgID,
|
||||
"output_index": st.outputIdx, "content_index": 0, "text": st.fullContent,
|
||||
})
|
||||
st.emit("response.content_part.done", map[string]any{
|
||||
"type": "response.content_part.done", "item_id": st.msgID,
|
||||
"output_index": st.outputIdx, "content_index": 0,
|
||||
"part": map[string]any{"type": "output_text", "text": st.fullContent, "annotations": []any{}, "logprobs": []any{}},
|
||||
})
|
||||
st.partOn = false
|
||||
}
|
||||
st.emit("response.output_item.done", map[string]any{
|
||||
"type": "response.output_item.done", "output_index": st.outputIdx,
|
||||
"item": map[string]any{
|
||||
"type": "message", "id": st.msgID, "status": "completed", "role": "assistant",
|
||||
"content": []map[string]any{{"type": "output_text", "text": st.fullContent, "annotations": []any{}, "logprobs": []any{}}},
|
||||
},
|
||||
})
|
||||
st.messageOn = false
|
||||
st.outputIdx++
|
||||
}
|
||||
|
||||
// handleReasoning processes a reasoning content delta.
|
||||
func (st *responsesStreamState) handleReasoning(delta string) {
|
||||
if !st.reasoningOn {
|
||||
st.reasoningOn = true
|
||||
st.emit("response.output_item.added", map[string]any{
|
||||
"type": "response.output_item.added", "output_index": st.outputIdx,
|
||||
"item": map[string]any{
|
||||
"type": "reasoning", "id": st.rsID, "status": "in_progress",
|
||||
"content": []any{}, "summary": []any{},
|
||||
},
|
||||
})
|
||||
st.emit("response.reasoning_summary_part.added", map[string]any{
|
||||
"type": "response.reasoning_summary_part.added", "item_id": st.rsID,
|
||||
"output_index": st.outputIdx, "summary_index": 0,
|
||||
"part": map[string]any{"type": "summary_text", "text": ""},
|
||||
})
|
||||
st.reasonPartOn = true
|
||||
}
|
||||
st.fullReasoning += delta
|
||||
st.emit("response.reasoning_summary_text.delta", map[string]any{
|
||||
"type": "response.reasoning_summary_text.delta", "item_id": st.rsID,
|
||||
"output_index": st.outputIdx, "summary_index": 0, "delta": delta,
|
||||
})
|
||||
}
|
||||
|
||||
// handleContent processes a text content delta.
|
||||
func (st *responsesStreamState) handleContent(delta string) {
|
||||
// Close reasoning if open — text content comes after reasoning.
|
||||
st.closeReasoning()
|
||||
if !st.messageOn {
|
||||
st.messageOn = true
|
||||
st.emit("response.output_item.added", map[string]any{
|
||||
"type": "response.output_item.added", "output_index": st.outputIdx,
|
||||
"item": map[string]any{
|
||||
"type": "message", "id": st.msgID, "status": "in_progress", "role": "assistant", "content": []any{},
|
||||
},
|
||||
})
|
||||
st.emit("response.content_part.added", map[string]any{
|
||||
"type": "response.content_part.added", "item_id": st.msgID,
|
||||
"output_index": st.outputIdx, "content_index": 0,
|
||||
"part": map[string]any{"type": "output_text", "text": "", "annotations": []any{}, "logprobs": []any{}},
|
||||
})
|
||||
st.partOn = true
|
||||
}
|
||||
st.fullContent += delta
|
||||
st.emit("response.output_text.delta", map[string]any{
|
||||
"type": "response.output_text.delta", "item_id": st.msgID,
|
||||
"output_index": st.outputIdx, "content_index": 0, "delta": delta,
|
||||
})
|
||||
}
|
||||
|
||||
// handleToolCall processes a tool call delta from the chat completion stream.
|
||||
func (st *responsesStreamState) handleToolCall(index int, id, name, args string) {
|
||||
// Close reasoning/message if open — tool calls are separate output items.
|
||||
st.closeReasoning()
|
||||
st.closeMessage()
|
||||
|
||||
call := st.tools[index]
|
||||
if call == nil {
|
||||
call = &streamToolCall{itemID: "fc_" + randomRequestID()}
|
||||
st.tools[index] = call
|
||||
st.toolOrder = append(st.toolOrder, index)
|
||||
}
|
||||
if id != "" {
|
||||
call.callID = id
|
||||
}
|
||||
if name != "" {
|
||||
call.name += name
|
||||
}
|
||||
if !call.started {
|
||||
call.started = true
|
||||
call.doneIdx = st.outputIdx
|
||||
st.emit("response.output_item.added", map[string]any{
|
||||
"type": "response.output_item.added", "output_index": st.outputIdx,
|
||||
"item": map[string]any{
|
||||
"type": "function_call", "id": call.itemID, "call_id": call.callID,
|
||||
"name": call.name, "arguments": "", "status": "in_progress",
|
||||
},
|
||||
})
|
||||
st.outputIdx++
|
||||
}
|
||||
if args != "" {
|
||||
call.args += args
|
||||
st.emit("response.function_call_arguments.delta", map[string]any{
|
||||
"type": "response.function_call_arguments.delta", "item_id": call.itemID,
|
||||
"output_index": call.doneIdx, "delta": args,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// finish closes all open items and sends the response.completed event.
|
||||
func (st *responsesStreamState) finish(model string) {
|
||||
st.closeReasoning()
|
||||
st.closeMessage()
|
||||
for _, idx := range st.toolOrder {
|
||||
call := st.tools[idx]
|
||||
st.emit("response.function_call_arguments.done", map[string]any{
|
||||
"type": "response.function_call_arguments.done", "item_id": call.itemID,
|
||||
"output_index": call.doneIdx, "arguments": call.args,
|
||||
})
|
||||
st.emit("response.output_item.done", map[string]any{
|
||||
"type": "response.output_item.done", "output_index": call.doneIdx,
|
||||
"item": map[string]any{
|
||||
"type": "function_call", "id": call.itemID, "call_id": call.callID,
|
||||
"name": call.name, "arguments": call.args, "status": "completed",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Build the final output array for the completed event.
|
||||
finalOutput := []any{}
|
||||
if st.fullReasoning != "" {
|
||||
finalOutput = append(finalOutput, map[string]any{
|
||||
"type": "reasoning", "id": st.rsID, "status": "completed",
|
||||
"content": []any{},
|
||||
"summary": []map[string]any{{"type": "summary_text", "text": st.fullReasoning}},
|
||||
})
|
||||
}
|
||||
if st.fullContent != "" {
|
||||
finalOutput = append(finalOutput, map[string]any{
|
||||
"type": "message", "id": st.msgID, "status": "completed", "role": "assistant",
|
||||
"content": []map[string]any{{"type": "output_text", "text": st.fullContent, "annotations": []any{}, "logprobs": []any{}}},
|
||||
})
|
||||
}
|
||||
for _, idx := range st.toolOrder {
|
||||
call := st.tools[idx]
|
||||
finalOutput = append(finalOutput, map[string]any{
|
||||
"type": "function_call", "id": call.itemID, "call_id": call.callID,
|
||||
"name": call.name, "arguments": call.args, "status": "completed",
|
||||
})
|
||||
}
|
||||
// Ensure at least one output item exists.
|
||||
if len(finalOutput) == 0 {
|
||||
finalOutput = append(finalOutput, map[string]any{
|
||||
"type": "message", "id": st.msgID, "status": "completed", "role": "assistant",
|
||||
"content": []map[string]any{},
|
||||
})
|
||||
}
|
||||
|
||||
completedResp := map[string]any{
|
||||
"id": st.respID, "object": "response", "created_at": time.Now().Unix(),
|
||||
"model": model, "status": "completed", "output": finalOutput,
|
||||
"parallel_tool_calls": st.meta.ParallelToolCalls,
|
||||
"tool_choice": st.meta.ToolChoice,
|
||||
"tools": st.meta.Tools,
|
||||
}
|
||||
if st.usage != nil {
|
||||
completedResp["usage"] = openai.UsageToResponses(st.usage)
|
||||
}
|
||||
st.emit("response.completed", map[string]any{"type": "response.completed", "response": completedResp})
|
||||
}
|
||||
|
||||
// proxyResponsesStream reads the upstream Chat Completions SSE and emits
|
||||
// Responses API SSE events via a responsesStreamState state machine.
|
||||
func (s *Server) proxyResponsesStream(w http.ResponseWriter, resp *http.Response, model string, meta openai.ResponsesMeta) (any, string) {
|
||||
st := newResponsesStreamState(w, model, meta)
|
||||
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
if line != "" {
|
||||
payload := sseDataPayload(line)
|
||||
if payload != "" && payload != "[DONE]" {
|
||||
var evt struct {
|
||||
State string `json:"state"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
Usage any `json:"usage"`
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
Content string `json:"content"`
|
||||
ReasoningContent string `json:"reasoning_content"`
|
||||
Reasoning string `json:"reasoning"`
|
||||
ToolCalls []struct {
|
||||
Index int `json:"index"`
|
||||
ID string `json:"id"`
|
||||
Function struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"`
|
||||
} `json:"function"`
|
||||
} `json:"tool_calls"`
|
||||
} `json:"delta"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if jErr := json.Unmarshal([]byte(payload), &evt); jErr == nil {
|
||||
if evt.State == "ERROR" {
|
||||
st.status = "upstream_error"
|
||||
}
|
||||
if evt.Usage != nil {
|
||||
st.usage = evt.Usage
|
||||
}
|
||||
if len(evt.Choices) > 0 {
|
||||
delta := evt.Choices[0].Delta
|
||||
reasoningDelta := delta.ReasoningContent + delta.Reasoning
|
||||
if reasoningDelta != "" {
|
||||
st.handleReasoning(reasoningDelta)
|
||||
}
|
||||
if delta.Content != "" {
|
||||
st.handleContent(delta.Content)
|
||||
}
|
||||
for _, tc := range delta.ToolCalls {
|
||||
st.handleToolCall(tc.Index, tc.ID, tc.Function.Name, tc.Function.Arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
b, _ := json.Marshal(map[string]any{
|
||||
"message": err.Error(), "type": "upstream_error", "code": "zhanlu_stream_error",
|
||||
})
|
||||
fmt.Fprintf(w, "data: %s\n\n", b)
|
||||
if st.flusher != nil {
|
||||
st.flusher.Flush()
|
||||
}
|
||||
st.status = "upstream_error"
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
st.finish(model)
|
||||
return st.usage, st.status
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.misaka.ren/M1saka/zhanlu_proxy/internal/auth"
|
||||
"git.misaka.ren/M1saka/zhanlu_proxy/internal/config"
|
||||
"git.misaka.ren/M1saka/zhanlu_proxy/internal/store"
|
||||
)
|
||||
|
||||
// TestResponsesNonStreaming verifies POST /v1/responses with stream:false
|
||||
// returns a properly formatted Responses API JSON object.
|
||||
func TestResponsesNonStreaming(t *testing.T) {
|
||||
upstream, proxy, st := setupTestServer(t)
|
||||
defer upstream.Close()
|
||||
defer proxy.Close()
|
||||
|
||||
creds := auth.Credentials{
|
||||
AccessKey: "AK", SecretKey: "SK", Token: "TOKEN",
|
||||
APIKey: "sk-test-456", ModelBaseURL: upstream.URL, Email: "[email protected]",
|
||||
}
|
||||
if err := st.SaveCredentials(creds); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body := `{"model":"GLM-4.7","input":"hi","stream":false}`
|
||||
resp, err := http.Post(proxy.URL+"/v1/responses", "application/json", strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
t.Fatalf("status %d: %s", resp.StatusCode, string(b))
|
||||
}
|
||||
var result map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["object"] != "response" {
|
||||
t.Fatalf("object = %v", result["object"])
|
||||
}
|
||||
if result["status"] != "completed" {
|
||||
t.Fatalf("status = %v", result["status"])
|
||||
}
|
||||
output, ok := result["output"].([]any)
|
||||
if !ok || len(output) == 0 {
|
||||
t.Fatalf("output = %v", result["output"])
|
||||
}
|
||||
msg, ok := output[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("output[0] type = %T", output[0])
|
||||
}
|
||||
if msg["type"] != "message" {
|
||||
t.Fatalf("output[0] type = %v", msg["type"])
|
||||
}
|
||||
if msg["role"] != "assistant" {
|
||||
t.Fatalf("output[0] role = %v", msg["role"])
|
||||
}
|
||||
content, ok := msg["content"].([]any)
|
||||
if !ok || len(content) == 0 {
|
||||
t.Fatalf("content = %v", msg["content"])
|
||||
}
|
||||
part, ok := content[0].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("content[0] type = %T", content[0])
|
||||
}
|
||||
if part["type"] != "output_text" {
|
||||
t.Fatalf("content[0] type = %v", part["type"])
|
||||
}
|
||||
if part["text"] != "hello" {
|
||||
t.Fatalf("content[0] text = %v", part["text"])
|
||||
}
|
||||
// Verify required SDK fields are present.
|
||||
if _, ok := result["parallel_tool_calls"]; !ok {
|
||||
t.Fatal("missing parallel_tool_calls in response")
|
||||
}
|
||||
if _, ok := result["tool_choice"]; !ok {
|
||||
t.Fatal("missing tool_choice in response")
|
||||
}
|
||||
if _, ok := result["tools"]; !ok {
|
||||
t.Fatal("missing tools in response")
|
||||
}
|
||||
// Verify output_text has logprobs field.
|
||||
if _, ok := part["logprobs"]; !ok {
|
||||
t.Fatal("missing logprobs in output_text content part")
|
||||
}
|
||||
usage, ok := result["usage"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("usage = %v", result["usage"])
|
||||
}
|
||||
if totNum(usage["input_tokens"]) != 10 {
|
||||
t.Fatalf("input_tokens = %v", usage["input_tokens"])
|
||||
}
|
||||
if totNum(usage["output_tokens"]) != 20 {
|
||||
t.Fatalf("output_tokens = %v", usage["output_tokens"])
|
||||
}
|
||||
if totNum(usage["total_tokens"]) != 30 {
|
||||
t.Fatalf("total_tokens = %v", usage["total_tokens"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestResponsesStreaming verifies POST /v1/responses with stream:true
|
||||
// emits proper Responses API SSE events.
|
||||
func TestResponsesStreaming(t *testing.T) {
|
||||
upstream, proxy, st := setupTestServer(t)
|
||||
defer upstream.Close()
|
||||
defer proxy.Close()
|
||||
|
||||
creds := auth.Credentials{
|
||||
AccessKey: "AK", SecretKey: "SK", Token: "TOKEN",
|
||||
APIKey: "sk-test-456", ModelBaseURL: upstream.URL, Email: "[email protected]",
|
||||
}
|
||||
if err := st.SaveCredentials(creds); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body := `{"model":"GLM-4.7","input":"hi","stream":true}`
|
||||
resp, err := http.Post(proxy.URL+"/v1/responses", "application/json", strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
t.Fatalf("status %d: %s", resp.StatusCode, string(b))
|
||||
}
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
s := string(raw)
|
||||
|
||||
// Check key lifecycle events are present.
|
||||
checks := []string{
|
||||
"event: response.created",
|
||||
"event: response.in_progress",
|
||||
"event: response.output_item.added",
|
||||
"event: response.content_part.added",
|
||||
"event: response.output_text.delta",
|
||||
"event: response.output_text.done",
|
||||
"event: response.content_part.done",
|
||||
"event: response.output_item.done",
|
||||
"event: response.completed",
|
||||
}
|
||||
for _, c := range checks {
|
||||
if !strings.Contains(s, c) {
|
||||
t.Fatalf("missing %q in SSE body:\n%s", c, s)
|
||||
}
|
||||
}
|
||||
|
||||
// Verify the text delta contains "hello".
|
||||
if !strings.Contains(s, `"delta":"hello"`) {
|
||||
t.Fatalf("text delta missing 'hello' in SSE body:\n%s", s)
|
||||
}
|
||||
|
||||
// Verify required SDK fields are present in completed event.
|
||||
if !strings.Contains(s, `"parallel_tool_calls"`) {
|
||||
t.Fatalf("missing parallel_tool_calls in SSE body:\n%s", s)
|
||||
}
|
||||
if !strings.Contains(s, `"tool_choice"`) {
|
||||
t.Fatalf("missing tool_choice in SSE body:\n%s", s)
|
||||
}
|
||||
// Verify logprobs field in output_text.
|
||||
if !strings.Contains(s, `"logprobs"`) {
|
||||
t.Fatalf("missing logprobs in SSE body:\n%s", s)
|
||||
}
|
||||
|
||||
// Verify usage in the completed event has input_tokens.
|
||||
if !strings.Contains(s, `"input_tokens":`) {
|
||||
t.Fatalf("usage missing input_tokens in SSE body:\n%s", s)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResponsesStringInput verifies the proxy correctly translates a string
|
||||
// "input" to a chat-completions "messages" array before forwarding upstream.
|
||||
func TestResponsesStringInput(t *testing.T) {
|
||||
var capturedBody []byte
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/chat/completions" {
|
||||
capturedBody, _ = io.ReadAll(r.Body)
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write([]byte("data: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n"))
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
st, err := store.Open(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
cfg := config.Config{
|
||||
MobileLoginBaseURL: upstream.URL,
|
||||
MobileModelBaseURL: upstream.URL,
|
||||
UpstreamPath: "/chat/completions",
|
||||
DBPath: filepath.Join(t.TempDir(), "zhanlu.db"),
|
||||
PublicKeyPEM: defaultTestPublicKey,
|
||||
PhonePublicKeyPEM: defaultTestPublicKey,
|
||||
SM2PrivateKey: testSM2Key,
|
||||
PluginVersion: "1.4.2",
|
||||
}
|
||||
h := New(cfg, st)
|
||||
proxy := httptest.NewServer(h)
|
||||
defer proxy.Close()
|
||||
|
||||
creds := auth.Credentials{
|
||||
APIKey: "sk-test-456", ModelBaseURL: upstream.URL, Email: "[email protected]",
|
||||
}
|
||||
_ = st.SaveCredentials(creds)
|
||||
|
||||
body := `{"model":"GLM-4.7","input":"hello world","stream":false}`
|
||||
resp, err := http.Post(proxy.URL+"/v1/responses", "application/json", strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
|
||||
if len(capturedBody) == 0 {
|
||||
t.Fatal("no upstream request body captured")
|
||||
}
|
||||
var upstreamReq map[string]any
|
||||
if err := json.Unmarshal(capturedBody, &upstreamReq); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
messages, ok := upstreamReq["messages"].([]any)
|
||||
if !ok || len(messages) != 1 {
|
||||
t.Fatalf("messages = %v", upstreamReq["messages"])
|
||||
}
|
||||
msg, _ := messages[0].(map[string]any)
|
||||
if msg["role"] != "user" || msg["content"] != "hello world" {
|
||||
t.Fatalf("upstream message = %v", msg)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResponsesInstructions verifies instructions are prepended as a system
|
||||
// message in the upstream request.
|
||||
func TestResponsesInstructions(t *testing.T) {
|
||||
var capturedBody []byte
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/chat/completions" {
|
||||
capturedBody, _ = io.ReadAll(r.Body)
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write([]byte("data: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n"))
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
st, err := store.Open(filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
cfg := config.Config{
|
||||
MobileLoginBaseURL: upstream.URL,
|
||||
MobileModelBaseURL: upstream.URL,
|
||||
UpstreamPath: "/chat/completions",
|
||||
DBPath: filepath.Join(t.TempDir(), "zhanlu.db"),
|
||||
PublicKeyPEM: defaultTestPublicKey,
|
||||
PhonePublicKeyPEM: defaultTestPublicKey,
|
||||
SM2PrivateKey: testSM2Key,
|
||||
PluginVersion: "1.4.2",
|
||||
}
|
||||
h := New(cfg, st)
|
||||
proxy := httptest.NewServer(h)
|
||||
defer proxy.Close()
|
||||
|
||||
creds := auth.Credentials{
|
||||
APIKey: "sk-test-456", ModelBaseURL: upstream.URL, Email: "[email protected]",
|
||||
}
|
||||
_ = st.SaveCredentials(creds)
|
||||
|
||||
body := `{"model":"GLM-4.7","instructions":"be concise","input":"hello","stream":false}`
|
||||
resp, err := http.Post(proxy.URL+"/v1/responses", "application/json", strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
|
||||
if len(capturedBody) == 0 {
|
||||
t.Fatal("no upstream request body captured")
|
||||
}
|
||||
var upstreamReq map[string]any
|
||||
_ = json.Unmarshal(capturedBody, &upstreamReq)
|
||||
messages, _ := upstreamReq["messages"].([]any)
|
||||
if len(messages) != 2 {
|
||||
t.Fatalf("messages = %d items", len(messages))
|
||||
}
|
||||
sys, _ := messages[0].(map[string]any)
|
||||
if sys["role"] != "system" || sys["content"] != "be concise" {
|
||||
t.Fatalf("system message = %v", sys)
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,7 @@ func (s *Server) routes() {
|
||||
s.mux.HandleFunc("POST /api/models/test", s.withLoginSession(s.testModel))
|
||||
s.mux.HandleFunc("GET /v1/models", s.withAPIKey(s.models))
|
||||
s.mux.HandleFunc("POST /v1/chat/completions", s.withAPIKey(s.chatCompletions))
|
||||
s.mux.HandleFunc("POST /v1/responses", s.withAPIKey(s.responses))
|
||||
}
|
||||
|
||||
func (s *Server) healthz(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -704,9 +705,9 @@ func (s *Server) testModel(w http.ResponseWriter, r *http.Request) {
|
||||
payload := sseDataPayload(line)
|
||||
if payload != "" && payload != "[DONE]" {
|
||||
var evt struct {
|
||||
State string `json:"state"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
Choices []struct {
|
||||
State string `json:"state"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"delta"`
|
||||
|
||||
Reference in New Issue
Block a user