1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use super::*;
use crate::io::{AgentIO, NullIO};
use crate::llm::retry::{retry_with_backoff, RetryConfig, RetryableError};
use anyhow::{bail, Result};
use async_trait::async_trait;
use futures_util::StreamExt;
use reqwest::Client;
use reqwest_eventsource::{Event, RequestBuilderExt};
use serde::Deserialize;
use serde_json::json;
use std::collections::HashMap;
use std::io::Write;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use thiserror::Error;
use tokio::sync::Mutex;
/// Sentinel value for `api_base` that signals GitHub Copilot mode.
pub const COPILOT_API_BASE: &str = "copilot";
const COPILOT_CHAT_URL: &str = "https://api.githubcopilot.com/chat/completions";
// ─── Provider ────────────────────────────────────────────────────────────────
/// Shared Copilot token state managed across async calls.
#[derive(Default)]
struct CopilotState {
/// The long-lived GitHub OAuth token (persisted to disk).
oauth_token: Option<String>,
/// Short-lived Copilot API token, refreshed automatically.
api_token: Option<crate::auth::CopilotApiToken>,
}
pub struct OpenAiProvider {
pub api_base: String,
pub api_key: String,
pub model: String,
client: Client,
/// Only populated when `api_base == COPILOT_API_BASE`.
copilot: Arc<Mutex<CopilotState>>,
/// Set to true when Copilot mode is activated (either at init or via set_copilot_oauth_token).
copilot_mode: AtomicBool,
/// When true (the default), stream tokens are printed to stdout in real time.
/// Set to false in Plan mode so xcodeai can post-process the reply before displaying.
pub stream_print: AtomicBool,
/// I/O handle for reporting retry status messages to the user.
/// Defaults to `NullIO` (silent) — call `with_io()` to inject a real terminal I/O.
io: Arc<dyn AgentIO>,
/// Retry / back-off configuration for this provider.
/// Defaults to `RetryConfig::default()` — call `with_retry()` to override.
retry: RetryConfig,
}
impl OpenAiProvider {
/// Create a standard (non-Copilot) provider.
///
/// The provider is created with `NullIO` (silent retries) and the default
/// `RetryConfig`. Use the builder methods `with_io()` and `with_retry()` to
/// configure them before use.
pub fn new(api_base: String, api_key: String, model: String) -> Self {
let client = Client::builder()
.timeout(Duration::from_secs(300))
.build()
.expect("Failed to create HTTP client");
let is_copilot = api_base == COPILOT_API_BASE;
Self {
api_base,
api_key,
model,
client,
copilot: Arc::new(Mutex::new(CopilotState::default())),
copilot_mode: AtomicBool::new(is_copilot),
stream_print: AtomicBool::new(true),
io: Arc::new(NullIO),
retry: RetryConfig::default(),
}
}
/// Create a Copilot-mode provider from a persisted OAuth token.
pub fn new_copilot(oauth_token: String, model: String) -> Self {
let client = Client::builder()
.timeout(Duration::from_secs(300))
.build()
.expect("Failed to create HTTP client");
let copilot = Arc::new(Mutex::new(CopilotState {
oauth_token: Some(oauth_token),
api_token: None,
}));
Self {
api_base: COPILOT_API_BASE.to_string(),
api_key: String::new(),
model,
client,
copilot,
copilot_mode: AtomicBool::new(true),
stream_print: AtomicBool::new(true),
io: Arc::new(NullIO),
retry: RetryConfig::default(),
}
}
/// Builder method: inject an I/O handle for retry status reporting.
///
/// # Example
/// ```rust,ignore
/// let provider = OpenAiProvider::new(base, key, model)
/// .with_io(io.clone())
/// .with_retry(config.agent.retry.clone());
/// ```
pub fn with_io(mut self, io: Arc<dyn AgentIO>) -> Self {
self.io = io;
self
}
/// Builder method: override the retry configuration.
pub fn with_retry(mut self, retry: RetryConfig) -> Self {
self.retry = retry;
self
}
/// Exchange the OAuth token for a short-lived Copilot API bearer token.
///
/// Checks the cached `CopilotState.api_token` first; if it is expired or
/// missing, calls `auth::refresh_copilot_token` to get a fresh one, caches
/// it, and returns the bearer string.
async fn copilot_bearer_token(&self) -> anyhow::Result<String> {
let mut state = self.copilot.lock().await;
// Refresh if we have no token or it's about to expire.
let needs_refresh = state
.api_token
.as_ref()
.map(|t| t.is_expired())
.unwrap_or(true);
if needs_refresh {
let oauth = state
.oauth_token
.as_deref()
.ok_or_else(|| anyhow::anyhow!("No Copilot OAuth token — run /login first"))?;
let fresh = crate::auth::refresh_copilot_token(&self.client, oauth).await?;
state.api_token = Some(fresh);
}
// Safe to unwrap: we just ensured api_token is Some above.
Ok(state.api_token.as_ref().unwrap().token.clone())
}
/// Execute a single SSE streaming request and return the assembled response.
///
/// This is the "one attempt" that `retry_with_backoff` will call repeatedly.
/// On transient HTTP errors (429, 500, 502, 503, 504), it returns a
/// `RetryableError` wrapped in `anyhow::Error` so the retry logic can
/// recognise it and schedule another attempt.
///
/// On permanent errors (400, 422, parse errors) it returns a plain error
/// that propagates immediately without retrying.
async fn try_once(
&self,
messages: &[Message],
tools: &[ToolDefinition],
) -> Result<LlmResponse> {
// ── Build the request body ────────────────────────────────────────────
let mut body = json!({
"model": self.model,
"messages": messages,
"stream": true,
// Ask the API to include token usage in the final SSE chunk.
// OpenAI sends a trailing chunk with `choices: []` and a `usage` field.
// Not all providers support this — we silently ignore missing usage.
"stream_options": { "include_usage": true },
});
if !tools.is_empty() {
body["tools"] = serde_json::to_value(tools)?;
body["tool_choice"] = json!("auto");
}
// ── Build request — Copilot needs different URL + auth headers ────────
let request = if self.is_copilot() {
let bearer = self.copilot_bearer_token().await?;
self.client
.post(COPILOT_CHAT_URL)
.header("Authorization", format!("Bearer {}", bearer))
.header("Copilot-Integration-Id", "vscode-chat")
.header("Content-Type", "application/json")
.header("User-Agent", "GithubCopilot/1.155.0")
.json(&body)
} else {
self.client
.post(format!("{}/chat/completions", self.api_base))
.header("Authorization", format!("Bearer {}", self.api_key))
.header("Content-Type", "application/json")
.json(&body)
};
// ── Open the SSE stream ───────────────────────────────────────────────
let mut es = match request.eventsource() {
Ok(es) => es,
Err(e) => bail!("Failed to start eventsource: {}", e),
};
let mut content = String::new();
let mut tool_calls: HashMap<usize, ToolCallBuilder> = HashMap::new();
// Accumulates usage data from the final SSE chunk (sent after [DONE] by OpenAI
// when stream_options.include_usage is true).
let mut usage_acc: Option<StreamChunkUsage> = None;
// ── Consume the SSE event stream ──────────────────────────────────────
while let Some(event) = es.next().await {
match event {
Ok(Event::Open) => {}
Ok(Event::Message(msg)) => {
if msg.data == "[DONE]" {
es.close();
break;
}
let chunk: StreamChunk = match serde_json::from_str(&msg.data) {
Ok(c) => c,
Err(e) => {
// Parse errors are permanent — no point retrying the same message.
return Err(LlmError::ParseError(format!("{}: {}", msg.data, e)).into());
}
};
for choice in chunk.choices {
let delta = choice.delta;
if let Some(text) = delta.content {
// Only stream to stdout if stream_print is enabled.
// Plan mode disables this so replies can be post-processed
// (e.g. parsed for CHOICES: blocks) before display.
if self.stream_print.load(Ordering::Relaxed) {
print!("{}", text);
std::io::stdout().flush().ok();
}
content.push_str(&text);
}
if let Some(partials) = delta.tool_calls {
for partial in partials {
let entry = tool_calls.entry(partial.index).or_default();
if let Some(id) = partial.id {
entry.id = Some(id);
}
if let Some(call_type) = partial.call_type {
entry.call_type = Some(call_type);
}
if let Some(function) = partial.function {
if let Some(name) = function.name {
entry.name.push_str(&name);
}
if let Some(args) = function.arguments {
entry.arguments.push_str(&args);
}
}
}
}
}
// Capture usage if the API sent it (final chunk from OpenAI).
// OpenAI sends a trailing chunk with `choices: []` and `usage: {...}`.
if let Some(u) = chunk.usage {
usage_acc = Some(u);
}
}
Err(e) => {
use reqwest_eventsource::Error as EsError;
match e {
// HTTP errors with a status code: translate to RetryableError
// so the retry wrapper can decide whether to retry.
EsError::InvalidStatusCode(status, ref resp) => {
let code = status.as_u16();
// Try to extract the Retry-After header value (in seconds).
// reqwest_eventsource gives us a `Response` reference here.
let retry_after_secs: Option<u64> = resp
.headers()
.get("retry-after")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.trim().parse::<u64>().ok());
// Return a RetryableError for transient codes.
// For other codes, return a plain LlmError — no retry.
return Err(match code {
429 | 500 | 502 | 503 | 504 => {
anyhow::Error::new(RetryableError::Http {
status: code,
retry_after: retry_after_secs,
})
}
_ => LlmError::HttpError {
status: code,
body: status.to_string(),
}
.into(),
});
}
// reqwest timeout error
EsError::Transport(ref transport_err) if transport_err.is_timeout() => {
return Err(anyhow::Error::new(RetryableError::Timeout));
}
// Other network/transport errors (connection refused, DNS, etc.)
EsError::Transport(transport_err) => {
return Err(anyhow::Error::new(RetryableError::Network(
transport_err.to_string(),
)));
}
// Everything else is a parse or stream error — permanent
other => {
return Err(LlmError::ParseError(other.to_string()).into());
}
}
}
}
}
// ── Assemble and return the final response ────────────────────────────
let tc = if tool_calls.is_empty() {
None
} else {
Some(tool_calls.into_values().map(|b| b.build()).collect())
};
Ok(LlmResponse {
content: if content.is_empty() {
None
} else {
Some(content)
},
tool_calls: tc,
// Convert the raw SSE usage into our public Usage type.
// If the provider didn't return usage, this is None — callers must handle that.
usage: usage_acc.map(|u| super::Usage {
prompt_tokens: u.prompt_tokens,
completion_tokens: u.completion_tokens,
total_tokens: u.total_tokens,
}),
})
}
}
// ─── Errors ──────────────────────────────────────────────────────────────────
#[derive(Error, Debug)]
pub enum LlmError {
/// Permanent HTTP error (e.g. 400 Bad Request, 401 Unauthorized).
/// Transient errors (429, 5xx) are handled by `RetryableError` in retry.rs.
#[error("HTTP error {status}: {body}")]
HttpError { status: u16, body: String },
#[error("Stream parse error: {0}")]
ParseError(String),
}
// ─── SSE deserialization ─────────────────────────────────────────────────────
#[derive(Deserialize)]
struct StreamChunk {
choices: Vec<ChunkChoice>,
/// Usage statistics. OpenAI sends this in a final chunk with `choices: []`
/// when `stream_options.include_usage` is `true`.
usage: Option<StreamChunkUsage>,
}
#[derive(Deserialize)]
struct ChunkChoice {
delta: Delta,
#[allow(dead_code)]
finish_reason: Option<String>,
}
#[derive(Deserialize, Default)]
struct Delta {
content: Option<String>,
tool_calls: Option<Vec<PartialToolCall>>,
}
#[derive(Deserialize)]
struct PartialToolCall {
index: usize,
id: Option<String>,
#[serde(rename = "type")]
call_type: Option<String>,
function: Option<PartialFunction>,
}
#[derive(Deserialize, Default)]
struct PartialFunction {
name: Option<String>,
arguments: Option<String>,
}
/// Usage data sent by OpenAI in the final SSE chunk when `stream_options.include_usage` is set.
#[derive(Deserialize, Default)]
struct StreamChunkUsage {
prompt_tokens: u32,
completion_tokens: u32,
total_tokens: u32,
}
// ─── LlmProvider impl ────────────────────────────────────────────────────────
#[async_trait]
impl LlmProvider for OpenAiProvider {
async fn chat_completion(
&self,
messages: &[Message],
tools: &[ToolDefinition],
) -> Result<LlmResponse> {
// Delegate to retry_with_backoff, which will call try_once() repeatedly
// on transient failures (429, 5xx, timeouts, network drops).
//
// The closure captures `messages` and `tools` by reference. Rust requires
// the closure to be `Fn` (not `FnOnce`) because it may be called multiple
// times, so we use shared references inside.
retry_with_backoff(&self.retry, self.io.as_ref(), || async {
self.try_once(messages, tools).await
})
.await
}
fn is_copilot(&self) -> bool {
self.copilot_mode.load(Ordering::Relaxed)
}
fn set_stream_print(&self, enabled: bool) {
self.stream_print.store(enabled, Ordering::Relaxed);
}
async fn set_copilot_oauth_token(&self, token: String) {
let mut state = self.copilot.lock().await;
state.oauth_token = Some(token);
state.api_token = None; // force refresh on next call
self.copilot_mode.store(true, Ordering::Relaxed);
}
}
// ─── Builder ─────────────────────────────────────────────────────────────────
#[derive(Default)]
struct ToolCallBuilder {
id: Option<String>,
call_type: Option<String>,
name: String,
arguments: String,
}
impl ToolCallBuilder {
fn build(self) -> ToolCall {
ToolCall {
id: self.id.unwrap_or_default(),
call_type: self.call_type.unwrap_or_else(|| "function".to_string()),
function: FunctionCall {
name: self.name,
arguments: self.arguments,
},
}
}
}
// ─── Tests ───────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{json, Value};
#[test]
fn test_message_serialization() {
let sys = Message::system("hello");
let user = Message::user("hi");
let tc = ToolCall {
id: "abc123".to_string(),
call_type: "function".to_string(),
function: FunctionCall {
name: "file_write".to_string(),
arguments: "{\"path\":\"foo.txt\"}".to_string(),
},
};
let assistant = Message::assistant(Some("ok".to_string()), Some(vec![tc.clone()]));
let tool = Message::tool("abc123", "done");
let msgs = vec![sys, user, assistant, tool];
for msg in msgs {
let json_str = serde_json::to_string(&msg).unwrap();
let back: Message = serde_json::from_str(&json_str).unwrap();
assert_eq!(msg, back);
let v: Value = serde_json::from_str(&json_str).unwrap();
assert!(matches!(
v["role"].as_str(),
Some("system") | Some("user") | Some("assistant") | Some("tool")
));
}
}
#[test]
fn test_tool_definition_format() {
let def = ToolDefinition {
def_type: "function".to_string(),
function: FunctionDefinition {
name: "file_write".to_string(),
description: "Write a file".to_string(),
parameters: json!({"type": "object", "properties": {"path": {"type": "string"}}}),
},
};
let v: Value = serde_json::to_value(&def).unwrap();
assert_eq!(v["type"], "function");
assert!(v["function"].is_object());
}
fn parse_sse_chunks(chunks: &[&str]) -> LlmResponse {
let mut content = String::new();
let mut tool_calls: HashMap<usize, ToolCallBuilder> = HashMap::new();
for chunk in chunks {
if *chunk == "[DONE]" {
break;
}
let chunk: StreamChunk = serde_json::from_str(chunk).unwrap();
for choice in chunk.choices {
let delta = choice.delta;
if let Some(text) = delta.content {
content.push_str(&text);
}
if let Some(partials) = delta.tool_calls {
for partial in partials {
let entry = tool_calls
.entry(partial.index)
.or_insert_with(ToolCallBuilder::default);
if let Some(id) = partial.id {
entry.id = Some(id);
}
if let Some(call_type) = partial.call_type {
entry.call_type = Some(call_type);
}
if let Some(function) = partial.function {
if let Some(name) = function.name {
entry.name.push_str(&name);
}
if let Some(args) = function.arguments {
entry.arguments.push_str(&args);
}
}
}
}
}
}
let tc = if tool_calls.is_empty() {
None
} else {
Some(tool_calls.into_iter().map(|(_, b)| b.build()).collect())
};
LlmResponse {
content: if content.is_empty() {
None
} else {
Some(content)
},
tool_calls: tc,
usage: None, // test helper — no real API call
}
}
#[test]
fn test_sse_parsing_text_only() {
let chunks = vec![
r#"{"choices":[{"delta":{"content":"Hello "}}]}"#,
r#"{"choices":[{"delta":{"content":"world!"}}]}"#,
"[DONE]",
];
let resp = parse_sse_chunks(&chunks);
assert_eq!(resp.content, Some("Hello world!".to_string()));
assert!(resp.tool_calls.is_none());
}
#[test]
fn test_sse_parsing_tool_call() {
let chunks = vec![
r#"{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"abc","type":"function","function":{"name":"file_","arguments":"{\"path\":"}}]}}]}"#,
r#"{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"write","arguments":"\"foo.txt\"}"}}]}}]}"#,
"[DONE]",
];
let resp = parse_sse_chunks(&chunks);
assert!(resp.content.is_none());
let tc = resp.tool_calls.unwrap();
assert_eq!(tc.len(), 1);
assert_eq!(tc[0].id, "abc");
assert_eq!(tc[0].function.name, "file_write");
assert_eq!(tc[0].function.arguments, "{\"path\":\"foo.txt\"}");
}
#[test]
fn test_partial_tool_call_assembly() {
let chunks = vec![
r#"{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"file_","arguments":"{"}}]}}]}"#,
r#"{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"write","arguments":"}"}}]}}]}"#,
"[DONE]",
];
let resp = parse_sse_chunks(&chunks);
let tc = resp.tool_calls.unwrap();
assert_eq!(tc[0].function.name, "file_write");
assert_eq!(tc[0].function.arguments, "{}".to_string());
}
#[test]
fn test_is_copilot() {
let p = OpenAiProvider::new(
COPILOT_API_BASE.to_string(),
String::new(),
"gpt-4o".to_string(),
);
assert!(p.is_copilot());
let p2 = OpenAiProvider::new(
"https://api.openai.com/v1".to_string(),
"key".to_string(),
"gpt-4o".to_string(),
);
assert!(!p2.is_copilot());
}
}