返回 DeepSeek-Reasonix
responses.go
根目录 / internal / provider / responses / responses.go
1 // Package responses implements the OpenAI Responses API wire protocol.
2 // DeepSeek uses it statelessly and requires the complete input history on every
3 // request; compatible stateful endpoints may opt into previous_response_id.
4 package responses
5
6 import (
7 "bufio"
8 "bytes"
9 "context"
10 "crypto/sha256"
11 "encoding/hex"
12 "encoding/json"
13 "fmt"
14 "io"
15 "maps"
16 "net/http"
17 "strings"
18 "sync"
19 "sync/atomic"
20 "time"
21
22 "reasonix/internal/netclient"
23 "reasonix/internal/provider"
24 "reasonix/internal/provider/openai"
25 )
26
27 const (
28 defaultStreamIdleTimeout = 300 * time.Second
29 maxReplayableSearchItemBytes = 512 * 1024
30 )
31
32 func init() {
33 provider.RegisterReasoning("responses", ReasoningForConfig)
34 provider.RegisterReasoning("dashscope-responses", ReasoningForConfig)
35 provider.Register("responses", newFromConfig)
36 provider.Register("dashscope-responses", newFromConfig)
37 }
38
39 // Config holds Responses API provider settings.
40 type Config struct {
41 HTTPClient *http.Client
42 Name string
43 DisplayName string
44 Protocol string
45 APIKey string
46 BaseURL string
47 Model string
48 ModelInfo *provider.ModelInfo
49 Effort string
50 Mode string // stateful | stateless; empty uses vendor detection.
51 Stateful *bool // legacy form of Mode; nil preserves vendor detection.
52 WebSearch bool // expose the provider-executed web_search tool.
53 Proxy netclient.ProxySpec
54 KeyEnv string
55 KeySource string
56 RequestURL string // optional exact Responses request URL; empty derives from BaseURL
57 // MaxOutputTokens is the total provider output budget. Zero omits the field
58 // on official DeepSeek (server 384K ceiling) and unknown endpoints; MiMo
59 // still applies its 16K/32K ladder. Negative values omit it.
60 MaxOutputTokens int
61 // SessionCache controls DashScope's opt-in header. The header is never sent
62 // to non-DashScope endpoints even when this value is true.
63 SessionCache *bool
64 // Extra carries kind-specific options; "vision" (bool) enables embedding
65 // attached Images as input_image parts on user turns.
66 Extra map[string]any
67 }
68
69 func (c Config) mode() string {
70 mode := strings.ToLower(strings.TrimSpace(c.Mode))
71 if mode == "stateful" || mode == "stateless" {
72 return mode
73 }
74 if c.Stateful != nil {
75 if *c.Stateful {
76 return "stateful"
77 }
78 return "stateless"
79 }
80 if capabilitiesFor(DetectVendor(c.BaseURL)).stateless {
81 return "stateless"
82 }
83 return "stateful"
84 }
85
86 // DetectVendor lives in vendor.go (capabilities table): it covers dashscope/
87 // deepseek (incl. eu.deepseek.com) / mimo via exact-host matching.
88
89 type client struct {
90 identityHeaders http.Header
91 reasoning provider.ReasoningCapability
92 name string
93 identity provider.RequestIdentity
94 apiKey, keyEnv, keySource string
95 baseURL, requestURL, model, effort string
96 vendor, mode string
97 caps vendorCapabilities
98 sessionCache bool
99 search provider.SearchPolicy
100 maxOutputTokens int
101 vision bool // model accepts image input; embed Images as input_image parts
102 modelInfo provider.ModelInfo
103 http *http.Client
104 idleTimeout time.Duration
105 authed atomic.Bool
106
107 mu sync.Mutex
108 lastResponseID string
109 expectedPrefixDigest string
110 }
111
112 // New creates a Responses API provider.
113 func New(cfg Config) provider.Provider {
114 cfg.Extra = maps.Clone(cfg.Extra)
115 if cfg.Extra == nil {
116 cfg.Extra = map[string]any{}
117 }
118 if cfg.RequestURL != "" {
119 cfg.Extra["request_url"] = cfg.RequestURL
120 }
121 resolved := provider.ApplyOpenCodeGoContract("responses", provider.Config{BaseURL: cfg.BaseURL, Model: cfg.Model, Extra: cfg.Extra})
122 cfg.Extra = resolved.Extra
123 vendor := DetectVendor(cfg.BaseURL)
124 cap := capabilitiesFor(vendor)
125 // Explicit replay contracts apply to compatible gateways as well as exact
126 // vendor hosts. Do not inherit endpoint defaults, headers, or output limits.
127 if protocol, _ := cfg.Extra["reasoning_protocol"].(string); strings.EqualFold(strings.TrimSpace(protocol), "deepseek") || strings.EqualFold(strings.TrimSpace(protocol), "mimo") {
128 cap.toolCallReasoning = true
129 }
130 maxOutputTokens := cfg.MaxOutputTokens
131 // Official DeepSeek omits max_output_tokens (server 384K). MiMo still uses
132 // the 16K/32K effort ladder. Compact_ratio is independent.
133 if maxOutputTokens == 0 && vendor == "mimo" {
134 maxOutputTokens = responsesAutoOutputBudget(vendor, cfg.Effort)
135 } else if maxOutputTokens == 0 && vendor != "deepseek" && cap.defaultMaxOutputTokens > 0 {
136 maxOutputTokens = cap.defaultMaxOutputTokens
137 }
138 sessionCache := cap.sessionCacheHeader
139 if cfg.SessionCache != nil {
140 sessionCache = *cfg.SessionCache
141 }
142 vision, _ := cfg.Extra["vision"].(bool)
143 if cfg.ModelInfo != nil {
144 vision = cfg.ModelInfo.SupportsInput(provider.ModalityImage)
145 }
146 // Official DeepSeek image input is pinned to one SKU. Ignore metadata or
147 // Extra["vision"] for Flash/Pro.
148 vision = openai.DeepSeekImageInputAllowed(vendor == "deepseek", cfg.RequestURL, cfg.Model, cfg.ModelInfo != nil, vision)
149 httpClient := &http.Client{}
150 if built, err := netclient.NewHTTPClient(cfg.Proxy, netclient.TransportOptions{
151 DialTimeout: 30 * time.Second, KeepAlive: 30 * time.Second,
152 TLSHandshakeTimeout: 15 * time.Second, ResponseHeaderTimeout: 300 * time.Second,
153 }); err == nil {
154 httpClient = provider.WithProtocolCompatibility(built, provider.RequestIdentity{Provider: cfg.Name, Protocol: "responses"})
155 }
156 if cfg.HTTPClient != nil {
157 httpClient = cfg.HTTPClient
158 }
159 baseURL, requestURL := resolveEndpoints(cfg.BaseURL, cfg.RequestURL)
160 modelInfo := provider.ModelInfo{ID: cfg.Model, InputModalities: []provider.ModelModality{provider.ModalityText}}
161 if cfg.ModelInfo != nil {
162 modelInfo = *cfg.ModelInfo
163 modelInfo.ID = cfg.Model
164 }
165 clientWebSearch, _ := cfg.Extra["client_web_search"].(bool)
166 if reject, _ := cfg.Extra["reject_redirects"].(bool); reject {
167 httpClient.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
168 }
169 if vision {
170 modelInfo.InputModalities = []provider.ModelModality{provider.ModalityText, provider.ModalityImage}
171 } else if modelInfo.SupportsInput(provider.ModalityImage) {
172 modelInfo.InputModalities = []provider.ModelModality{provider.ModalityText}
173 }
174 return &client{
175 identityHeaders: provider.NewClientIdentityHeaders(),
176 name: cfg.Name,
177 identity: provider.RequestIdentity{Provider: cfg.Name, DisplayName: cfg.DisplayName, Protocol: cfg.Protocol},
178 apiKey: cfg.APIKey, keyEnv: cfg.KeyEnv, keySource: cfg.KeySource,
179 reasoning: ReasoningForConfig(provider.Config{BaseURL: cfg.BaseURL, Model: cfg.Model, Extra: cfg.Extra}),
180 baseURL: baseURL, requestURL: requestURL, model: cfg.Model, effort: cfg.Effort,
181 vendor: vendor, caps: cap, mode: cfg.mode(), sessionCache: sessionCache, search: provider.SearchPolicy{NativeEnabled: cfg.WebSearch, ClientEnabled: clientWebSearch}, maxOutputTokens: maxOutputTokens,
182 vision: vision,
183 modelInfo: modelInfo,
184 http: httpClient, idleTimeout: defaultStreamIdleTimeout,
185 }
186 }
187
188 func (c *client) ModelInfo() provider.ModelInfo {
189 if c == nil {
190 return provider.ModelInfo{}
191 }
192 info := c.modelInfo
193 info.InputModalities = append([]provider.ModelModality(nil), info.InputModalities...)
194 return info
195 }
196
197 func responsesReasoningDisabled(effort string) bool {
198 switch strings.ToLower(strings.TrimSpace(effort)) {
199 case "none", "disabled", "off":
200 return true
201 default:
202 return false
203 }
204 }
205
206 // responsesAutoOutputBudget is the MiMo (and similar) 16K/32K ladder.
207 // Official DeepSeek must not call this; it omits max_output_tokens instead.
208 func responsesAutoOutputBudget(vendor, effort string) int {
209 if responsesReasoningDisabled(effort) {
210 return provider.AutoOutputBudget(false, effort)
211 }
212 e := strings.ToLower(strings.TrimSpace(effort))
213 if vendor == "deepseek" && (e == "" || e == "auto") {
214 e = "high"
215 }
216 return provider.AutoOutputBudget(true, e)
217 }
218
219 func (c *client) Name() string { return c.name }
220
221 func (c *client) NativeToolSearchAvailable() bool {
222 return c != nil && provider.IsFirstPartyOpenAI(c.baseURL) && nativeToolSearchModel(c.model)
223 }
224
225 func nativeToolSearchModel(model string) bool {
226 model = strings.ToLower(strings.TrimSpace(model))
227 return strings.HasPrefix(model, "gpt-5.4") || strings.HasPrefix(model, "gpt-5.5") || strings.HasPrefix(model, "gpt-5.6")
228 }
229
230 func (c *client) sendOpts() provider.SendOptions {
231 return provider.SendOptions{Provider: c.name, ProviderDisplayName: c.identity.DisplayName, Protocol: c.identity.Protocol, KeyEnv: c.keyEnv, KeySource: c.keySource, KeyPresent: c.apiKey != "", RetryAuth: c.authed.Load()}
232 }
233
234 // ResetContext drops stateful continuation metadata. Full-input stateless mode
235 // is unaffected.
236 func (c *client) ResetContext() {
237 c.mu.Lock()
238 c.lastResponseID = ""
239 c.expectedPrefixDigest = ""
240 c.mu.Unlock()
241 }
242
243 func (c *client) Stream(ctx context.Context, req provider.Request) (<-chan provider.Chunk, error) {
244 if c.effort != "auto" && c.effort != "off" {
245 if err := c.reasoning.Validate(c.model, c.effort); err != nil {
246 return nil, err
247 }
248 }
249 if err := c.reasoning.Validate(c.model, req.EffortOverride); err != nil {
250 return nil, err
251 }
252 requestCtx := provider.WithRequestAttemptCounter(ctx)
253 body, usedPrevious, wireMessages := c.buildRequestBody(req)
254 resp, err := c.send(requestCtx, body)
255 if err != nil && usedPrevious && isStalePreviousResponseError(err) {
256 // A stateful response ID may expire server-side. Retrying once with full
257 // history is safe because no response body has started streaming.
258 c.ResetContext()
259 body, _, wireMessages = c.buildRequestBody(req)
260 resp, err = c.send(requestCtx, body)
261 }
262 if err != nil && isCommandCodeTransientResponsesError(c.requestURL, err) {
263 // Retry once only for Command Code's narrow anonymous-400 signature; the
264 // byte-identical body is safe to resend and named schema failures remain
265 // terminal.
266 resp, err = c.send(requestCtx, body)
267 }
268 if err != nil {
269 return nil, err
270 }
271 c.authed.Store(true)
272 out := make(chan provider.Chunk, 64)
273 go c.readStream(requestCtx, resp, out, wireMessages)
274 return out, nil
275 }
276
277 func (c *client) send(ctx context.Context, body map[string]any) (*http.Response, error) {
278 payload, err := json.Marshal(body)
279 if err != nil {
280 return nil, fmt.Errorf("responses: marshal request: %w", err)
281 }
282 newRequest := func(ctx context.Context) (*http.Request, error) {
283 req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.requestURL, bytes.NewReader(payload))
284 if err != nil {
285 return nil, err
286 }
287 req.Header.Set("Content-Type", "application/json")
288 req.Header.Set("Authorization", "Bearer "+c.apiKey)
289 provider.ApplyOpenCodeGoHeaders(req, c.baseURL, c.identityHeaders)
290 if c.caps.sessionCacheHeader && c.sessionCache {
291 req.Header.Set("x-dashscope-session-cache", "enable")
292 }
293 return req, nil
294 }
295 return provider.SendWithRetry(ctx, c.http, c.sendOpts(), newRequest)
296 }
297
298 func (c *client) buildRequestBody(req provider.Request) (map[string]any, bool, []provider.Message) {
299 messages := provider.SanitizeToolPairing(provider.ModelMessages(req.Messages))
300 body := map[string]any{"model": c.model, "stream": true}
301
302 effort := c.effort
303 if req.EffortOverride != "" {
304 effort = req.EffortOverride
305 }
306
307 switch effort {
308 case "auto":
309 effort = ""
310 case "disabled", "off":
311 effort = "none"
312 }
313 if effort != "" {
314 body["reasoning"] = map[string]any{"effort": effort}
315 }
316 maxOutputTokens := req.MaxTokens
317 if maxOutputTokens == 0 {
318 maxOutputTokens = c.maxOutputTokens
319 }
320 if maxOutputTokens == 0 && c.vendor == "mimo" {
321 maxOutputTokens = responsesAutoOutputBudget(c.vendor, c.effort)
322 } else if maxOutputTokens == 0 && c.vendor != "deepseek" && c.caps.defaultMaxOutputTokens > 0 {
323 maxOutputTokens = c.caps.defaultMaxOutputTokens
324 }
325 if maxOutputTokens > 0 {
326 body["max_output_tokens"] = maxOutputTokens
327 }
328 if req.ResponseFormat != nil && req.ResponseFormat.Type != "" {
329 // Structured output: Responses text.format. MiMo/DashScope/OpenAI
330 // all accept {"text":{"format":{"type":"json_object"}}}. The model
331 // only emits JSON when the instructions also demand it.
332 body["text"] = map[string]any{
333 "format": map[string]any{"type": req.ResponseFormat.Type},
334 }
335 }
336 if req.Temperature != nil && !c.caps.ignoresTemperature {
337 body["temperature"] = *req.Temperature
338 }
339 if c.search.NativeEnabled || len(req.Tools) > 0 {
340 body["tools"] = encodeResponsesTools(c, req)
341 }
342 instructions, rest := splitInstructions(messages)
343 if instructions != "" {
344 body["instructions"] = instructions
345 }
346
347 c.mu.Lock()
348 previousID, expectedDigest := c.lastResponseID, c.expectedPrefixDigest
349 c.mu.Unlock()
350 if c.canUseStatefulContinuation(messages, previousID, expectedDigest) {
351 body["input"] = messages[len(messages)-1].Content
352 body["previous_response_id"] = previousID
353 return body, true, messages
354 }
355
356 body["input"] = messagesToInput(rest, c.vision, c.search.NativeEnabled, c.caps.summaryRequired)
357 return body, false, messages
358 }
359
360 func inputImagePart(ref string) map[string]string {
361 switch provider.ClassifyImage(ref) {
362 case provider.ImageFileID:
363 return map[string]string{"type": "input_image", "file_id": ref}
364 case provider.ImageDataURL, provider.ImageHTTPURL:
365 return map[string]string{"type": "input_image", "image_url": ref}
366 default:
367 return nil
368 }
369 }
370
371 func splitInstructions(messages []provider.Message) (string, []provider.Message) {
372 if len(messages) == 0 || messages[0].Role != provider.RoleSystem {
373 return "", messages
374 }
375 return messages[0].Content, messages[1:]
376 }
377
378 func decodeReplayableWebSearchItem(raw json.RawMessage) (map[string]any, bool) {
379 if len(raw) == 0 || len(raw) > maxReplayableSearchItemBytes || !json.Valid(raw) {
380 return nil, false
381 }
382 var item map[string]any
383 if err := json.Unmarshal(raw, &item); err != nil || item["type"] != "web_search_call" {
384 return nil, false
385 }
386 id, _ := item["id"].(string)
387 status, _ := item["status"].(string)
388 if strings.TrimSpace(id) == "" || status != "completed" {
389 return nil, false
390 }
391 return item, true
392 }
393
394 func (c *client) conversationDigest(messages []provider.Message) string {
395 instructions, rest := splitInstructions(messages)
396 // Digest must mirror buildRequestBody's wire shape and vision/summary knobs;
397 // a mismatch skips previous_response_id and forces a full replay (cache-hit loss).
398 payload, _ := json.Marshal(struct {
399 Instructions string `json:"instructions,omitempty"`
400 Input []map[string]any `json:"input"`
401 }{Instructions: instructions, Input: messagesToInput(rest, c.vision, c.search.NativeEnabled, c.caps.summaryRequired)})
402 sum := sha256.Sum256(payload)
403 return hex.EncodeToString(sum[:])
404 }
405
406 type streamedCall struct {
407 id, name, arguments string
408 argChars int
409 completed bool
410 announced bool
411 }
412
413 func (c *client) readStream(ctx context.Context, resp *http.Response, out chan<- provider.Chunk, requestMessages []provider.Message) {
414 defer resp.Body.Close()
415 defer close(out)
416
417 scanner := bufio.NewScanner(resp.Body)
418 scanner.Buffer(make([]byte, 64*1024), 4*1024*1024)
419 idle := c.idleTimeout
420 if idle <= 0 {
421 idle = defaultStreamIdleTimeout
422 }
423 watchDone := make(chan struct{})
424 activity := make(chan struct{}, 1)
425 var reasoningSnapshots responseReasoningSnapshots
426 var stalled atomic.Bool
427 go func() {
428 timer := time.NewTimer(idle)
429 defer timer.Stop()
430 for {
431 select {
432 case <-ctx.Done():
433 _ = resp.Body.Close()
434 return
435 case <-watchDone:
436 return
437 case <-activity:
438 if !timer.Stop() {
439 select {
440 case <-timer.C:
441 default:
442 }
443 }
444 timer.Reset(idle)
445 case <-timer.C:
446 stalled.Store(true)
447 _ = resp.Body.Close()
448 return
449 }
450 }
451 }()
452 defer close(watchDone)
453
454 calls := make(map[string]*streamedCall)
455 callOrder := make([]string, 0)
456 callForItem := func(itemID string) *streamedCall {
457 if call := calls[itemID]; call != nil {
458 return call
459 }
460 call := &streamedCall{id: itemID}
461 calls[itemID] = call
462 callOrder = append(callOrder, itemID)
463 return call
464 }
465 textDeltas := make(map[string]bool)
466 reasoningDeltas := make(map[string]bool)
467 seenSearchItems := make(map[string]struct{})
468 var responsesItems []json.RawMessage
469 var text, reasoning strings.Builder
470 reasoningID := ""
471 reasoningStatus := ""
472 terminal := false
473 failed := false
474 completedResponseID := ""
475
476 for scanner.Scan() {
477 select {
478 case activity <- struct{}{}:
479 default:
480 }
481 line := scanner.Text()
482 if !strings.HasPrefix(line, "data:") {
483 continue
484 }
485 data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
486 if data == "[DONE]" {
487 if !checkTerminalCalls(ctx, out, nil, calls) {
488 return
489 }
490 terminal = true
491 break
492 }
493 var event sseEvent
494 if json.Unmarshal([]byte(data), &event) != nil {
495 continue
496 }
497 key := fmt.Sprintf("%s:%d", event.ItemID, event.ContentIndex)
498 reasoningSnapshots.capture(event)
499 switch event.Type {
500 case "response.output_text.delta":
501 textDeltas[key] = true
502 text.WriteString(event.Delta)
503 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkText, Text: event.Delta}) {
504 return
505 }
506 case "response.output_text.done":
507 if event.Text != "" && !textDeltas[key] {
508 text.WriteString(event.Text)
509 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkText, Text: event.Text}) {
510 return
511 }
512 }
513 case "response.reasoning_text.delta", "response.reasoning_summary_text.delta":
514 reasoningDeltas[key] = true
515 reasoning.WriteString(event.Delta)
516 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkReasoning, Text: event.Delta}) {
517 return
518 }
519 case "response.reasoning_text.done", "response.reasoning_summary_text.done":
520 if event.Text != "" && !reasoningDeltas[key] {
521 reasoning.WriteString(event.Text)
522 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkReasoning, Text: event.Text}) {
523 return
524 }
525 }
526 case "response.output_item.added":
527 if event.Item != nil {
528 switch event.Item.Type {
529 case "function_call":
530 call := callForItem(event.Item.ID)
531 call.announced = true
532 call.id = event.Item.CallID
533 call.name = event.Item.Name
534 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkToolCallStart, ToolCall: &provider.ToolCall{ID: call.id, Name: call.name}}) {
535 return
536 }
537 case "reasoning":
538 // Capture the provider-issued reasoning item id so the
539 // next turn's input reasoning item can carry it (the
540 // OpenAI Responses schema marks Reasoning.id required).
541 if event.Item.ID != "" {
542 // 多段推理(DeepSeek 长思考分多段)时末段 id 覆盖:round-trip
543 // 合并为一个 reasoning item 只带末段 id(服务端接受)。
544 reasoningID = event.Item.ID
545 }
546 }
547 }
548 case "response.function_call_arguments.delta":
549 call := callForItem(event.ItemID)
550 call.arguments += event.Delta
551 call.argChars += len(event.Delta)
552 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkToolCallArgsDelta, ToolCall: &provider.ToolCall{ID: call.id, Name: call.name}, ArgChars: call.argChars}) {
553 return
554 }
555 case "response.function_call_arguments.done":
556 call := callForItem(event.ItemID)
557 if event.Arguments != "" {
558 call.arguments = event.Arguments
559 }
560 if !completeFunctionCall(ctx, out, call) {
561 return
562 }
563 case "response.output_item.done":
564 if event.Item != nil && event.Item.Type == "web_search_call" && c.search.NativeEnabled {
565 if _, ok := decodeReplayableWebSearchItem(event.Item.Raw); ok {
566 key := event.Item.ID
567 if key == "" {
568 key = string(event.Item.Raw)
569 }
570 if _, seen := seenSearchItems[key]; !seen {
571 seenSearchItems[key] = struct{}{}
572 raw := append(json.RawMessage(nil), event.Item.Raw...)
573 responsesItems = append(responsesItems, raw)
574 if !emitSearchReplay(ctx, out, raw) {
575 return
576 }
577 }
578 }
579 }
580 if event.Item != nil {
581 switch event.Item.Type {
582 case "function_call":
583 if !finishFunctionCall(ctx, out, callForItem(event.Item.ID), event.Item) {
584 return
585 }
586 case "reasoning":
587 // Preserve the done event's final status when round-tripping
588 // the reasoning item so the input matches the wire schema.
589 if event.Item.Status != "" {
590 reasoningStatus = event.Item.Status
591 }
592 }
593 }
594 case "response.completed", "response.incomplete", "response.failed":
595 terminal = true
596 completedResponseID = terminalResponseID(event)
597 var ok bool
598 failed, ok = c.finishResponse(ctx, out, event, calls, callForItem)
599 if !ok {
600 return
601 }
602 }
603 if terminal {
604 break
605 }
606 }
607
608 if ctx.Err() != nil {
609 return
610 }
611 if err := scanner.Err(); err != nil {
612 var reason string
613 if stalled.Load() {
614 err = fmt.Errorf("responses: stream idle timeout after %s", idle)
615 reason = provider.StreamInterruptIdleTimeout
616 } else {
617 reason = provider.ClassifyStreamInterrupt(err)
618 }
619 _ = sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkError, Err: provider.StreamInterrupt(err, reason)})
620 return
621 }
622 // Protocol-defined terminal response events are required. Connection close
623 // before a terminal event leaves the attempt uncommitted — including any
624 // complete tool calls already forwarded as speculative output.
625 if !terminal {
626 _ = sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkError, Err: provider.StreamInterrupt(io.ErrUnexpectedEOF, provider.StreamInterruptPrematureEOF)})
627 return
628 }
629 if !reasoningSnapshots.emit(ctx, out) {
630 return
631 }
632 responsesItems = append(responsesItems, reasoningSnapshots.items...)
633 if len(reasoningSnapshots.items) > 0 {
634 reasoningID, reasoningStatus = reasoningSnapshots.metadata()
635 }
636 if completedResponseID != "" {
637 assistant := provider.Message{Role: provider.RoleAssistant, Content: text.String(), ReasoningContent: reasoning.String(), ReasoningID: reasoningID, ReasoningStatus: reasoningStatus, ResponsesItems: responsesItems}
638 for _, itemID := range callOrder {
639 call := calls[itemID]
640 if call.completed {
641 assistant.ToolCalls = append(assistant.ToolCalls, provider.ToolCall{ID: call.id, Name: call.name, Arguments: call.arguments})
642 }
643 }
644 expected := append(append([]provider.Message(nil), requestMessages...), assistant)
645 c.mu.Lock()
646 c.lastResponseID = completedResponseID
647 c.expectedPrefixDigest = c.conversationDigest(expected)
648 c.mu.Unlock()
649 } else {
650 c.ResetContext()
651 }
652 if !failed {
653 // 把 reasoning item 的 id/status 作为元数据 chunk 流给 Agent
654 // (空 Text,随 ChunkReasoning 语义)——Agent 持久化进 session,
655 // 下一轮 input reasoning item 回传 id/status(评审 #7234 第 1 点)。
656 if reasoningID != "" || reasoningStatus != "" {
657 if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkReasoning, ReasoningID: reasoningID, ReasoningStatus: reasoningStatus}) {
658 return
659 }
660 }
661 _ = sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkDone})
662 }
663 }
664
665 func sendChunk(ctx context.Context, out chan<- provider.Chunk, chunk provider.Chunk) bool {
666 select {
667 case out <- chunk:
668 return true
669 default:
670 }
671 notifySendChunkEnterBlocking()
672 select {
673 case out <- chunk:
674 return true
675 case <-ctx.Done():
676 return false
677 }
678 }
679
680 func usageFromResponse(response *sseResponse) *provider.Usage {
681 usage := &provider.Usage{}
682 if response == nil || response.Usage == nil {
683 return usage
684 }
685 u := response.Usage
686 cached, reasoning := 0, 0
687 if u.InputTokensDetails != nil {
688 cached = u.InputTokensDetails.CachedTokens
689 }
690 if u.OutputTokensDetails != nil {
691 reasoning = u.OutputTokensDetails.ReasoningTokens
692 }
693 miss := max(u.InputTokens-cached, 0)
694 total := u.TotalTokens
695 if total == 0 {
696 total = u.InputTokens + u.OutputTokens
697 }
698 return &provider.Usage{PromptTokens: u.InputTokens, CompletionTokens: u.OutputTokens, TotalTokens: total, CacheHitTokens: cached, CacheMissTokens: miss, ReasoningTokens: reasoning}
699 }
700
701 func authErrorFromResponse(c *client, responseError *sseError) error {
702 if responseError == nil {
703 return nil
704 }
705 value := strings.ToLower(responseError.Code + " " + responseError.Message)
706 if !strings.Contains(value, "auth") && !strings.Contains(value, "api key") && !strings.Contains(value, "unauthorized") && !strings.Contains(value, "forbidden") && !strings.Contains(value, "permission") {
707 return nil
708 }
709 status := http.StatusUnauthorized
710 if strings.Contains(value, "forbidden") || strings.Contains(value, "permission") {
711 status = http.StatusForbidden
712 }
713 return &provider.AuthError{Provider: c.name, ProviderDisplayName: c.identity.DisplayName, Protocol: c.identity.Protocol, KeyEnv: c.keyEnv, KeySource: c.keySource, Status: status, HasKey: c.apiKey != "", Body: responseError.Message}
714 }
715
716 type sseEvent struct {
717 Type string `json:"type"`
718 Delta string `json:"delta"`
719 Text string `json:"text"`
720 Arguments string `json:"arguments"`
721 ItemID string `json:"item_id"`
722 ContentIndex int `json:"content_index"`
723 Item *sseItem `json:"item"`
724 Response *sseResponse `json:"response"`
725 }
726
727 type sseItem struct {
728 ID, Type, CallID, Name, Arguments, Status string
729 Raw json.RawMessage
730 }
731
732 func (i *sseItem) UnmarshalJSON(data []byte) error {
733 var wire struct {
734 ID string `json:"id"`
735 Type string `json:"type"`
736 CallID string `json:"call_id"`
737 Name string `json:"name"`
738 Arguments string `json:"arguments"`
739 Status string `json:"status"`
740 }
741 if err := json.Unmarshal(data, &wire); err != nil {
742 return err
743 }
744 *i = sseItem{ID: wire.ID, Type: wire.Type, CallID: wire.CallID, Name: wire.Name, Arguments: wire.Arguments, Status: wire.Status, Raw: append(json.RawMessage(nil), data...)}
745 return nil
746 }
747
748 type sseResponse struct {
749 Output []json.RawMessage `json:"output"`
750 ID string `json:"id"`
751 Usage *sseUsage `json:"usage"`
752 Error *sseError `json:"error"`
753 IncompleteDetails incompleteDetails `json:"incomplete_details"`
754 }
755
756 type incompleteDetails struct {
757 Reason string `json:"reason"`
758 }
759 type sseError struct {
760 Message string `json:"message"`
761 Code string `json:"code"`
762 }
763 type sseUsage struct {
764 InputTokens int `json:"input_tokens"`
765 OutputTokens int `json:"output_tokens"`
766 TotalTokens int `json:"total_tokens"`
767 InputTokensDetails *struct {
768 CachedTokens int `json:"cached_tokens"`
769 } `json:"input_tokens_details"`
770 OutputTokensDetails *struct {
771 ReasoningTokens int `json:"reasoning_tokens"`
772 } `json:"output_tokens_details"`
773 }
774
774 lines GO