1package provider
2
3import (
4 "bytes"
5 "context"
6 "encoding/base64"
7 "encoding/json"
8 "fmt"
9 "io"
10 "net/http"
11 "strings"
12 "time"
13)
14
15const openaiDefaultBaseURL = "https://api.openai.com"
16
17type openaiClient struct {
18 apiKey string
19 baseURL string
20 oauth bool // when true, apiKey actually holds an OAuth access token
21 http *http.Client
22}
23
24// NewOpenAI creates an OpenAI client using an API key. baseURL may be empty.
25func NewOpenAI(apiKey, baseURL string) Client {
26 if baseURL == "" {
27 baseURL = openaiDefaultBaseURL
28 }
29 return &openaiClient{
30 apiKey: apiKey,
31 baseURL: strings.TrimRight(baseURL, "/"),
32 http: &http.Client{Timeout: 0},
33 }
34}
35
36// NewOpenAIOAuth creates an OpenAI client using a subscription OAuth access token.
37// The token is sent as an HTTP Bearer credential on the standard chat/completions endpoint.
38func NewOpenAIOAuth(accessToken, baseURL string) Client {
39 if baseURL == "" {
40 baseURL = openaiDefaultBaseURL
41 }
42 return &openaiClient{
43 apiKey: accessToken,
44 baseURL: strings.TrimRight(baseURL, "/"),
45 oauth: true,
46 http: &http.Client{Timeout: 0},
47 }
48}
49
50func (c *openaiClient) Name() string { return "openai" }
51
52// ---- wire types ----
53
54type oaiContentText struct {
55 Type string `json:"type"` // "text"
56 Text string `json:"text"`
57}
58
59type oaiContentImage struct {
60 Type string `json:"type"` // "image_url"
61 ImageURL struct {
62 URL string `json:"url"`
63 } `json:"image_url"`
64}
65
66type oaiToolCallFn struct {
67 Name string `json:"name"`
68 Arguments string `json:"arguments"` // JSON string
69}
70
71type oaiToolCall struct {
72 ID string `json:"id"`
73 Type string `json:"type"` // "function"
74 Function oaiToolCallFn `json:"function"`
75}
76
77type oaiMessage struct {
78 Role string `json:"role"`
79 Content interface{} `json:"content,omitempty"` // string or []block
80 Name string `json:"name,omitempty"`
81 ToolCalls []oaiToolCall `json:"tool_calls,omitempty"`
82 ToolCallID string `json:"tool_call_id,omitempty"`
83}
84
85type oaiTool struct {
86 Type string `json:"type"` // "function"
87 Function struct {
88 Name string `json:"name"`
89 Description string `json:"description,omitempty"`
90 Parameters json.RawMessage `json:"parameters"`
91 } `json:"function"`
92}
93
94type oaiStreamOptions struct {
95 IncludeUsage bool `json:"include_usage"`
96}
97
98type oaiRequest struct {
99 Model string `json:"model"`
100 Messages []oaiMessage `json:"messages"`
101 Tools []oaiTool `json:"tools,omitempty"`
102 ToolChoice string `json:"tool_choice,omitempty"`
103 Stream bool `json:"stream"`
104 StreamOptions *oaiStreamOptions `json:"stream_options,omitempty"`
105 Temperature *float32 `json:"temperature,omitempty"`
106 MaxTokens *int `json:"max_tokens,omitempty"`
107 MaxCompletionTok *int `json:"max_completion_tokens,omitempty"`
108 ReasoningEffort string `json:"reasoning_effort,omitempty"`
109}
110
111// ---- request building ----
112
113func (c *openaiClient) buildRequest(req Request) (*oaiRequest, error) {
114 m, err := FindModel("openai", req.Model)
115 if err != nil {
116 return nil, err
117 }
118 out := &oaiRequest{
119 Model: req.Model,
120 Stream: true,
121 StreamOptions: &oaiStreamOptions{IncludeUsage: true},
122 Temperature: req.Temperature,
123 }
124
125 maxTok := req.MaxTokens
126 if maxTok <= 0 {
127 maxTok = m.MaxOutput
128 }
129 if m.Reasoning {
130 out.MaxCompletionTok = &maxTok
131 if req.Reasoning != "" {
132 out.ReasoningEffort = strings.ToLower(req.Reasoning)
133 }
134 } else {
135 out.MaxTokens = &maxTok
136 }
137
138 if req.System != "" {
139 out.Messages = append(out.Messages, oaiMessage{Role: "system", Content: req.System})
140 }
141
142 for _, msg := range req.Messages {
143 switch msg.Role {
144 case RoleUser:
145 content := buildOAIUserContent(msg.Content)
146 out.Messages = append(out.Messages, oaiMessage{Role: "user", Content: content})
147 case RoleAssistant:
148 am := oaiMessage{Role: "assistant"}
149 var text strings.Builder
150 for _, b := range msg.Content {
151 switch v := b.(type) {
152 case TextBlock:
153 if text.Len() > 0 {
154 text.WriteString("\n")
155 }
156 text.WriteString(v.Text)
157 case ToolCallBlock:
158 args := v.Arguments
159 if len(args) == 0 {
160 args = json.RawMessage("{}")
161 }
162 am.ToolCalls = append(am.ToolCalls, oaiToolCall{
163 ID: v.ID,
164 Type: "function",
165 Function: oaiToolCallFn{
166 Name: v.Name,
167 Arguments: string(args),
168 },
169 })
170 }
171 }
172 if text.Len() > 0 {
173 am.Content = text.String()
174 }
175 out.Messages = append(out.Messages, am)
176 case RoleTool:
177 // Each ToolResultBlock becomes its own tool message. Preserve
178 // image blocks for vision-capable OpenAI models instead of
179 // flattening the tool output to plain text.
180 for _, b := range msg.Content {
181 if tr, ok := b.(ToolResultBlock); ok {
182 content := buildOAIToolContent(tr.Content, tr.IsError)
183 out.Messages = append(out.Messages, oaiMessage{
184 Role: "tool",
185 ToolCallID: tr.CallID,
186 Content: content,
187 })
188 }
189 }
190 }
191 }
192
193 for _, t := range req.Tools {
194 var tool oaiTool
195 tool.Type = "function"
196 tool.Function.Name = t.Name
197 tool.Function.Description = t.Description
198 tool.Function.Parameters = t.Schema
199 out.Tools = append(out.Tools, tool)
200 }
201 if len(out.Tools) > 0 {
202 out.ToolChoice = "auto"
203 }
204
205 return out, nil
206}
207
208func buildOAIUserContent(blocks []Content) interface{} {
209 hasImage := false
210 for _, b := range blocks {
211 if _, ok := b.(ImageBlock); ok {
212 hasImage = true
213 break
214 }
215 }
216 if !hasImage {
217 var sb strings.Builder
218 for _, b := range blocks {
219 if tb, ok := b.(TextBlock); ok {
220 if sb.Len() > 0 {
221 sb.WriteString("\n")
222 }
223 sb.WriteString(tb.Text)
224 }
225 }
226 return sb.String()
227 }
228 return buildOAIContentBlocks(blocks, false)
229}
230
231func buildOAIToolContent(blocks []Content, isError bool) interface{} {
232 hasImage := false
233 for _, b := range blocks {
234 if _, ok := b.(ImageBlock); ok {
235 hasImage = true
236 break
237 }
238 }
239 if !hasImage {
240 var sb strings.Builder
241 for _, b := range blocks {
242 if tb, ok := b.(TextBlock); ok {
243 if sb.Len() > 0 {
244 sb.WriteString("\n")
245 }
246 sb.WriteString(tb.Text)
247 }
248 }
249 if isError && sb.Len() > 0 {
250 sb.WriteString(" [error]")
251 }
252 return sb.String()
253 }
254 return buildOAIContentBlocks(blocks, isError)
255}
256
257func buildOAIContentBlocks(blocks []Content, isError bool) []interface{} {
258 var arr []interface{}
259 for _, b := range blocks {
260 switch v := b.(type) {
261 case TextBlock:
262 arr = append(arr, oaiContentText{Type: "text", Text: v.Text})
263 case ImageBlock:
264 var img oaiContentImage
265 img.Type = "image_url"
266 img.ImageURL.URL = "data:" + v.MimeType + ";base64," + base64.StdEncoding.EncodeToString(v.Data)
267 arr = append(arr, img)
268 }
269 }
270 if isError {
271 arr = append(arr, oaiContentText{Type: "text", Text: "[error]"})
272 }
273 return arr
274}
275
276// ---- streaming ----
277
278func (c *openaiClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
279 wire, err := c.buildRequest(req)
280 if err != nil {
281 return nil, err
282 }
283 body, err := json.Marshal(wire)
284 if err != nil {
285 return nil, err
286 }
287 httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/v1/chat/completions", bytes.NewReader(body))
288 if err != nil {
289 return nil, err
290 }
291 httpReq.Header.Set("content-type", "application/json")
292 httpReq.Header.Set("accept", "text/event-stream")
293 httpReq.Header.Set("authorization", "Bearer "+c.apiKey)
294
295 resp, err := c.http.Do(httpReq)
296 if err != nil {
297 return nil, fmt.Errorf("openai: %w", err)
298 }
299 if resp.StatusCode != http.StatusOK {
300 b, _ := io.ReadAll(resp.Body)
301 resp.Body.Close()
302 return nil, fmt.Errorf("openai: http %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
303 }
304
305 out := make(chan Event, 16)
306 go c.runStream(ctx, resp, req, out)
307 return out, nil
308}
309
310func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
311 defer close(out)
312 defer resp.Body.Close()
313
314 model, _ := FindModel("openai", req.Model)
315 out <- EventStart{Model: req.Model, Provider: "openai"}
316
317 raw := make(chan sseEvent, 16)
318 go readSSE(resp.Body, raw)
319
320 // Interleaved block tracking: text and tool_calls preserve their
321 // emission order so the assistant message renders in the same order
322 // the model produced it. The builder fires one kind of block at a
323 // time — incoming text deltas after a tool_call split into a fresh
324 // text block; subsequent tool_calls each get their own slot.
325 type blockEntry struct {
326 kind string // "text" | "tool_use"
327 textBuf strings.Builder
328 toolID string
329 toolName string
330 toolArgs strings.Builder
331 announced bool
332 }
333 var (
334 blocks []*blockEntry
335 currentText *blockEntry // most-recent text block, nil if none
336 toolByIdx = map[int]*blockEntry{} // openai tool_call index -> block
337 usage Usage
338 stop StopReason = StopEnd
339 finalErr error
340 )
341
342 appendText := func(delta string) {
343 if currentText == nil {
344 currentText = &blockEntry{kind: "text"}
345 blocks = append(blocks, currentText)
346 }
347 currentText.textBuf.WriteString(delta)
348 }
349
350 getOrCreateTool := func(idx int) *blockEntry {
351 if t, ok := toolByIdx[idx]; ok {
352 return t
353 }
354 t := &blockEntry{kind: "tool_use"}
355 toolByIdx[idx] = t
356 blocks = append(blocks, t)
357 // A new tool block breaks the current text block. Subsequent text
358 // deltas will start a fresh text block after this tool.
359 currentText = nil
360 return t
361 }
362
363 assembleMsg := func() Message {
364 content := []Content{}
365 for _, b := range blocks {
366 switch b.kind {
367 case "text":
368 if b.textBuf.Len() > 0 {
369 content = append(content, TextBlock{Text: b.textBuf.String()})
370 }
371 case "tool_use":
372 args := b.toolArgs.String()
373 if args == "" {
374 args = "{}"
375 }
376 content = append(content, ToolCallBlock{
377 ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args),
378 })
379 }
380 }
381 return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
382 }
383
384 sendDone := func() {
385 usage.CostUSD = ComputeCost(model, usage)
386 out <- EventUsage{Usage: usage}
387 out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
388 }
389
390 for {
391 select {
392 case <-ctx.Done():
393 stop = StopAborted
394 finalErr = ctx.Err()
395 sendDone()
396 return
397 case ev, ok := <-raw:
398 if !ok {
399 sendDone()
400 return
401 }
402 if ev.Data == "[DONE]" {
403 sendDone()
404 return
405 }
406 var chunk struct {
407 Choices []struct {
408 Index int `json:"index"`
409 Delta struct {
410 Content string `json:"content"`
411 ToolCalls []struct {
412 Index int `json:"index"`
413 ID string `json:"id"`
414 Type string `json:"type"`
415 Function struct {
416 Name string `json:"name"`
417 Arguments string `json:"arguments"`
418 } `json:"function"`
419 } `json:"tool_calls"`
420 } `json:"delta"`
421 FinishReason string `json:"finish_reason"`
422 } `json:"choices"`
423 Usage *struct {
424 PromptTokens int `json:"prompt_tokens"`
425 CompletionTokens int `json:"completion_tokens"`
426 PromptTokensDetails struct {
427 CachedTokens int `json:"cached_tokens"`
428 } `json:"prompt_tokens_details"`
429 } `json:"usage"`
430 Error *struct {
431 Message string `json:"message"`
432 Type string `json:"type"`
433 } `json:"error"`
434 }
435 if err := json.Unmarshal([]byte(ev.Data), &chunk); err != nil {
436 continue
437 }
438 if chunk.Error != nil {
439 stop = StopError
440 finalErr = fmt.Errorf("openai: %s", chunk.Error.Message)
441 sendDone()
442 return
443 }
444 if chunk.Usage != nil {
445 usage.InputTokens = chunk.Usage.PromptTokens - chunk.Usage.PromptTokensDetails.CachedTokens
446 if usage.InputTokens < 0 {
447 usage.InputTokens = chunk.Usage.PromptTokens
448 }
449 usage.OutputTokens = chunk.Usage.CompletionTokens
450 usage.CacheReadTokens = chunk.Usage.PromptTokensDetails.CachedTokens
451 }
452 for _, ch := range chunk.Choices {
453 if ch.Delta.Content != "" {
454 appendText(ch.Delta.Content)
455 out <- EventTextDelta{Delta: ch.Delta.Content}
456 }
457 for _, tc := range ch.Delta.ToolCalls {
458 t := getOrCreateTool(tc.Index)
459 if tc.ID != "" {
460 t.toolID = tc.ID
461 }
462 if tc.Function.Name != "" {
463 t.toolName = tc.Function.Name
464 }
465 if !t.announced && t.toolID != "" && t.toolName != "" {
466 t.announced = true
467 out <- EventToolStart{ID: t.toolID, Name: t.toolName}
468 }
469 if tc.Function.Arguments != "" {
470 t.toolArgs.WriteString(tc.Function.Arguments)
471 if t.announced {
472 out <- EventToolArgs{ID: t.toolID, Delta: tc.Function.Arguments}
473 }
474 }
475 }
476 switch ch.FinishReason {
477 case "stop":
478 stop = StopEnd
479 case "length":
480 stop = StopLength
481 case "tool_calls", "function_call":
482 stop = StopToolUse
483 for _, b := range blocks {
484 if b.kind == "tool_use" && b.announced {
485 out <- EventToolEnd{ID: b.toolID}
486 }
487 }
488 }
489 }
490 }
491 }
492}