| 1 | package responses |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | // ErrInvalidFunctionCall identifies a malformed function call in completed output. |
| 13 | var ErrInvalidFunctionCall = errors.New("responses: completed response contains an invalid function call") |
| 14 | |
| 15 | // ErrUnfinishedFunctionCall identifies a stream that ended with a pending call. |
| 16 | var ErrUnfinishedFunctionCall = errors.New("responses: stream ended with an unfinished function call") |
| 17 | |
| 18 | // Terminal failures invalidate the whole speculative attempt. Never execute a |
| 19 | // readable neighbor while silently dropping a malformed function-call item. |
| 20 | func checkTerminalCalls(ctx context.Context, out chan<- provider.Chunk, response *sseResponse, calls map[string]*streamedCall) bool { |
| 21 | if err := terminalCallsError(response, calls); err != nil { |
| 22 | _ = sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkError, Err: err}) |
| 23 | return false |
| 24 | } |
| 25 | return true |
| 26 | } |
| 27 | |
| 28 | func terminalCallsError(response *sseResponse, calls map[string]*streamedCall) error { |
| 29 | if response != nil { |
| 30 | for _, raw := range response.Output { |
| 31 | var envelope struct { |
| 32 | Type string `json:"type"` |
| 33 | Arguments json.RawMessage `json:"arguments"` |
| 34 | } |
| 35 | if json.Unmarshal(raw, &envelope) != nil || envelope.Type != "function_call" { |
| 36 | continue |
| 37 | } |
| 38 | var item sseItem |
| 39 | if (len(envelope.Arguments) == 0 || envelope.Arguments[0] != '"') || json.Unmarshal(raw, &item) != nil || item.CallID == "" || item.Name == "" || (item.Status != "" && item.Status != "completed") { |
| 40 | return ErrInvalidFunctionCall |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | closed := make(map[string]bool, len(calls)) |
| 45 | for _, call := range calls { |
| 46 | if call.completed && call.id != "" { |
| 47 | closed[call.id] = true |
| 48 | } |
| 49 | } |
| 50 | for _, call := range calls { |
| 51 | // Unidentified deltas may be recovered by the terminal output under a new |
| 52 | // item ID; they do not establish an independent executable call obligation. |
| 53 | if !call.completed && !closed[call.id] && (call.announced || call.name != "" || ((call.argChars > 0 || call.arguments != "") && len(closed) == 0)) { |
| 54 | return ErrUnfinishedFunctionCall |
| 55 | } |
| 56 | } |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | func (c *client) finishResponse(ctx context.Context, out chan<- provider.Chunk, event sseEvent, calls map[string]*streamedCall, callForItem func(string) *streamedCall) (failed, ok bool) { |
| 61 | if event.Type != "response.failed" { |
| 62 | for _, item := range unclosedOutputCalls(event.Response, calls) { |
| 63 | if !finishFunctionCall(ctx, out, callForItem(item.ID), item) { |
| 64 | return false, false |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | if event.Type == "response.incomplete" { |
| 69 | if !sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkReasoning, ReasoningState: provider.ReasoningIncomplete}) { |
| 70 | return false, false |
| 71 | } |
| 72 | } |
| 73 | if !emitTerminalResponseUsage(ctx, out, event) { |
| 74 | return false, false |
| 75 | } |
| 76 | if event.Type == "response.completed" && !checkTerminalCalls(ctx, out, event.Response, calls) { |
| 77 | return false, false |
| 78 | } |
| 79 | if event.Type != "response.failed" { |
| 80 | return false, true |
| 81 | } |
| 82 | err := fmt.Errorf("responses: response failed") |
| 83 | if event.Response != nil && event.Response.Error != nil { |
| 84 | if authErr := authErrorFromResponse(c, event.Response.Error); authErr != nil { |
| 85 | err = authErr |
| 86 | } else { |
| 87 | err = fmt.Errorf("responses: %s", event.Response.Error.Message) |
| 88 | } |
| 89 | } |
| 90 | return true, sendChunk(ctx, out, provider.Chunk{Type: provider.ChunkError, Err: err}) |
| 91 | } |
| 92 |