1use anyhow::{Context as _, Result, anyhow};
2use futures::{AsyncBufReadExt, AsyncReadExt, StreamExt, io::BufReader, stream::BoxStream};
3use http_client::{
4 AsyncBody, HttpClient, Method, Request as HttpRequest, StatusCode,
5 http::{HeaderMap, HeaderValue},
6};
7use serde::{Deserialize, Serialize};
8use serde_json::Value;
9pub use settings::OpenAiReasoningEffort as ReasoningEffort;
10use std::{convert::TryFrom, future::Future};
11use strum::EnumIter;
12use thiserror::Error;
13
14pub const OPEN_AI_API_URL: &str = "https://api.openai.com/v1";
15
16fn is_none_or_empty<T: AsRef<[U]>, U>(opt: &Option<T>) -> bool {
17 opt.as_ref().is_none_or(|v| v.as_ref().is_empty())
18}
19
20#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
21#[serde(rename_all = "lowercase")]
22pub enum Role {
23 User,
24 Assistant,
25 System,
26 Tool,
27}
28
29impl TryFrom<String> for Role {
30 type Error = anyhow::Error;
31
32 fn try_from(value: String) -> Result<Self> {
33 match value.as_str() {
34 "user" => Ok(Self::User),
35 "assistant" => Ok(Self::Assistant),
36 "system" => Ok(Self::System),
37 "tool" => Ok(Self::Tool),
38 _ => anyhow::bail!("invalid role '{value}'"),
39 }
40 }
41}
42
43impl From<Role> for String {
44 fn from(val: Role) -> Self {
45 match val {
46 Role::User => "user".to_owned(),
47 Role::Assistant => "assistant".to_owned(),
48 Role::System => "system".to_owned(),
49 Role::Tool => "tool".to_owned(),
50 }
51 }
52}
53
54#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
55#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
56pub enum Model {
57 #[serde(rename = "gpt-3.5-turbo")]
58 ThreePointFiveTurbo,
59 #[serde(rename = "gpt-4")]
60 Four,
61 #[serde(rename = "gpt-4-turbo")]
62 FourTurbo,
63 #[serde(rename = "gpt-4o")]
64 #[default]
65 FourOmni,
66 #[serde(rename = "gpt-4o-mini")]
67 FourOmniMini,
68 #[serde(rename = "gpt-4.1")]
69 FourPointOne,
70 #[serde(rename = "gpt-4.1-mini")]
71 FourPointOneMini,
72 #[serde(rename = "gpt-4.1-nano")]
73 FourPointOneNano,
74 #[serde(rename = "o1")]
75 O1,
76 #[serde(rename = "o3-mini")]
77 O3Mini,
78 #[serde(rename = "o3")]
79 O3,
80 #[serde(rename = "o4-mini")]
81 O4Mini,
82 #[serde(rename = "gpt-5")]
83 Five,
84 #[serde(rename = "gpt-5-mini")]
85 FiveMini,
86 #[serde(rename = "gpt-5-nano")]
87 FiveNano,
88
89 #[serde(rename = "custom")]
90 Custom {
91 name: String,
92 /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
93 display_name: Option<String>,
94 max_tokens: u64,
95 max_output_tokens: Option<u64>,
96 max_completion_tokens: Option<u64>,
97 reasoning_effort: Option<ReasoningEffort>,
98 },
99}
100
101impl Model {
102 pub fn default_fast() -> Self {
103 // TODO: Replace with FiveMini since all other models are deprecated
104 Self::FourPointOneMini
105 }
106
107 pub fn from_id(id: &str) -> Result<Self> {
108 match id {
109 "gpt-3.5-turbo" => Ok(Self::ThreePointFiveTurbo),
110 "gpt-4" => Ok(Self::Four),
111 "gpt-4-turbo-preview" => Ok(Self::FourTurbo),
112 "gpt-4o" => Ok(Self::FourOmni),
113 "gpt-4o-mini" => Ok(Self::FourOmniMini),
114 "gpt-4.1" => Ok(Self::FourPointOne),
115 "gpt-4.1-mini" => Ok(Self::FourPointOneMini),
116 "gpt-4.1-nano" => Ok(Self::FourPointOneNano),
117 "o1" => Ok(Self::O1),
118 "o3-mini" => Ok(Self::O3Mini),
119 "o3" => Ok(Self::O3),
120 "o4-mini" => Ok(Self::O4Mini),
121 "gpt-5" => Ok(Self::Five),
122 "gpt-5-mini" => Ok(Self::FiveMini),
123 "gpt-5-nano" => Ok(Self::FiveNano),
124 invalid_id => anyhow::bail!("invalid model id '{invalid_id}'"),
125 }
126 }
127
128 pub fn id(&self) -> &str {
129 match self {
130 Self::ThreePointFiveTurbo => "gpt-3.5-turbo",
131 Self::Four => "gpt-4",
132 Self::FourTurbo => "gpt-4-turbo",
133 Self::FourOmni => "gpt-4o",
134 Self::FourOmniMini => "gpt-4o-mini",
135 Self::FourPointOne => "gpt-4.1",
136 Self::FourPointOneMini => "gpt-4.1-mini",
137 Self::FourPointOneNano => "gpt-4.1-nano",
138 Self::O1 => "o1",
139 Self::O3Mini => "o3-mini",
140 Self::O3 => "o3",
141 Self::O4Mini => "o4-mini",
142 Self::Five => "gpt-5",
143 Self::FiveMini => "gpt-5-mini",
144 Self::FiveNano => "gpt-5-nano",
145 Self::Custom { name, .. } => name,
146 }
147 }
148
149 pub fn display_name(&self) -> &str {
150 match self {
151 Self::ThreePointFiveTurbo => "gpt-3.5-turbo",
152 Self::Four => "gpt-4",
153 Self::FourTurbo => "gpt-4-turbo",
154 Self::FourOmni => "gpt-4o",
155 Self::FourOmniMini => "gpt-4o-mini",
156 Self::FourPointOne => "gpt-4.1",
157 Self::FourPointOneMini => "gpt-4.1-mini",
158 Self::FourPointOneNano => "gpt-4.1-nano",
159 Self::O1 => "o1",
160 Self::O3Mini => "o3-mini",
161 Self::O3 => "o3",
162 Self::O4Mini => "o4-mini",
163 Self::Five => "gpt-5",
164 Self::FiveMini => "gpt-5-mini",
165 Self::FiveNano => "gpt-5-nano",
166 Self::Custom {
167 name, display_name, ..
168 } => display_name.as_ref().unwrap_or(name),
169 }
170 }
171
172 pub fn max_token_count(&self) -> u64 {
173 match self {
174 Self::ThreePointFiveTurbo => 16_385,
175 Self::Four => 8_192,
176 Self::FourTurbo => 128_000,
177 Self::FourOmni => 128_000,
178 Self::FourOmniMini => 128_000,
179 Self::FourPointOne => 1_047_576,
180 Self::FourPointOneMini => 1_047_576,
181 Self::FourPointOneNano => 1_047_576,
182 Self::O1 => 200_000,
183 Self::O3Mini => 200_000,
184 Self::O3 => 200_000,
185 Self::O4Mini => 200_000,
186 Self::Five => 272_000,
187 Self::FiveMini => 272_000,
188 Self::FiveNano => 272_000,
189 Self::Custom { max_tokens, .. } => *max_tokens,
190 }
191 }
192
193 pub fn max_output_tokens(&self) -> Option<u64> {
194 match self {
195 Self::Custom {
196 max_output_tokens, ..
197 } => *max_output_tokens,
198 Self::ThreePointFiveTurbo => Some(4_096),
199 Self::Four => Some(8_192),
200 Self::FourTurbo => Some(4_096),
201 Self::FourOmni => Some(16_384),
202 Self::FourOmniMini => Some(16_384),
203 Self::FourPointOne => Some(32_768),
204 Self::FourPointOneMini => Some(32_768),
205 Self::FourPointOneNano => Some(32_768),
206 Self::O1 => Some(100_000),
207 Self::O3Mini => Some(100_000),
208 Self::O3 => Some(100_000),
209 Self::O4Mini => Some(100_000),
210 Self::Five => Some(128_000),
211 Self::FiveMini => Some(128_000),
212 Self::FiveNano => Some(128_000),
213 }
214 }
215
216 pub fn reasoning_effort(&self) -> Option<ReasoningEffort> {
217 match self {
218 Self::Custom {
219 reasoning_effort, ..
220 } => reasoning_effort.to_owned(),
221 _ => None,
222 }
223 }
224
225 /// Returns whether the given model supports the `parallel_tool_calls` parameter.
226 ///
227 /// If the model does not support the parameter, do not pass it up, or the API will return an error.
228 pub fn supports_parallel_tool_calls(&self) -> bool {
229 match self {
230 Self::ThreePointFiveTurbo
231 | Self::Four
232 | Self::FourTurbo
233 | Self::FourOmni
234 | Self::FourOmniMini
235 | Self::FourPointOne
236 | Self::FourPointOneMini
237 | Self::FourPointOneNano
238 | Self::Five
239 | Self::FiveMini
240 | Self::FiveNano => true,
241 Self::O1 | Self::O3 | Self::O3Mini | Self::O4Mini | Model::Custom { .. } => false,
242 }
243 }
244
245 /// Returns whether the given model supports the `prompt_cache_key` parameter.
246 ///
247 /// If the model does not support the parameter, do not pass it up.
248 pub fn supports_prompt_cache_key(&self) -> bool {
249 true
250 }
251}
252
253#[derive(Debug, Serialize, Deserialize)]
254pub struct Request {
255 pub model: String,
256 pub messages: Vec<RequestMessage>,
257 pub stream: bool,
258 #[serde(default, skip_serializing_if = "Option::is_none")]
259 pub max_completion_tokens: Option<u64>,
260 #[serde(default, skip_serializing_if = "Vec::is_empty")]
261 pub stop: Vec<String>,
262 pub temperature: f32,
263 #[serde(default, skip_serializing_if = "Option::is_none")]
264 pub tool_choice: Option<ToolChoice>,
265 /// Whether to enable parallel function calling during tool use.
266 #[serde(default, skip_serializing_if = "Option::is_none")]
267 pub parallel_tool_calls: Option<bool>,
268 #[serde(default, skip_serializing_if = "Vec::is_empty")]
269 pub tools: Vec<ToolDefinition>,
270 #[serde(default, skip_serializing_if = "Option::is_none")]
271 pub prompt_cache_key: Option<String>,
272 #[serde(default, skip_serializing_if = "Option::is_none")]
273 pub reasoning_effort: Option<ReasoningEffort>,
274}
275
276#[derive(Debug, Serialize, Deserialize)]
277#[serde(rename_all = "lowercase")]
278pub enum ToolChoice {
279 Auto,
280 Required,
281 None,
282 #[serde(untagged)]
283 Other(ToolDefinition),
284}
285
286#[derive(Clone, Deserialize, Serialize, Debug)]
287#[serde(tag = "type", rename_all = "snake_case")]
288pub enum ToolDefinition {
289 #[allow(dead_code)]
290 Function { function: FunctionDefinition },
291}
292
293#[derive(Clone, Debug, Serialize, Deserialize)]
294pub struct FunctionDefinition {
295 pub name: String,
296 pub description: Option<String>,
297 pub parameters: Option<Value>,
298}
299
300#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
301#[serde(tag = "role", rename_all = "lowercase")]
302pub enum RequestMessage {
303 Assistant {
304 content: Option<MessageContent>,
305 #[serde(default, skip_serializing_if = "Vec::is_empty")]
306 tool_calls: Vec<ToolCall>,
307 },
308 User {
309 content: MessageContent,
310 },
311 System {
312 content: MessageContent,
313 },
314 Tool {
315 content: MessageContent,
316 tool_call_id: String,
317 },
318}
319
320#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
321#[serde(untagged)]
322pub enum MessageContent {
323 Plain(String),
324 Multipart(Vec<MessagePart>),
325}
326
327impl MessageContent {
328 pub fn empty() -> Self {
329 MessageContent::Multipart(vec![])
330 }
331
332 pub fn push_part(&mut self, part: MessagePart) {
333 match self {
334 MessageContent::Plain(text) => {
335 *self =
336 MessageContent::Multipart(vec![MessagePart::Text { text: text.clone() }, part]);
337 }
338 MessageContent::Multipart(parts) if parts.is_empty() => match part {
339 MessagePart::Text { text } => *self = MessageContent::Plain(text),
340 MessagePart::Image { .. } => *self = MessageContent::Multipart(vec![part]),
341 },
342 MessageContent::Multipart(parts) => parts.push(part),
343 }
344 }
345}
346
347impl From<Vec<MessagePart>> for MessageContent {
348 fn from(mut parts: Vec<MessagePart>) -> Self {
349 if let [MessagePart::Text { text }] = parts.as_mut_slice() {
350 MessageContent::Plain(std::mem::take(text))
351 } else {
352 MessageContent::Multipart(parts)
353 }
354 }
355}
356
357#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
358#[serde(tag = "type")]
359pub enum MessagePart {
360 #[serde(rename = "text")]
361 Text { text: String },
362 #[serde(rename = "image_url")]
363 Image { image_url: ImageUrl },
364}
365
366#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
367pub struct ImageUrl {
368 pub url: String,
369 #[serde(skip_serializing_if = "Option::is_none")]
370 pub detail: Option<String>,
371}
372
373#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
374pub struct ToolCall {
375 pub id: String,
376 #[serde(flatten)]
377 pub content: ToolCallContent,
378}
379
380#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
381#[serde(tag = "type", rename_all = "lowercase")]
382pub enum ToolCallContent {
383 Function { function: FunctionContent },
384}
385
386#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)]
387pub struct FunctionContent {
388 pub name: String,
389 pub arguments: String,
390}
391
392#[derive(Clone, Serialize, Deserialize, Debug)]
393pub struct Response {
394 pub id: String,
395 pub object: String,
396 pub created: u64,
397 pub model: String,
398 pub choices: Vec<Choice>,
399 pub usage: Usage,
400}
401
402#[derive(Clone, Serialize, Deserialize, Debug)]
403pub struct Choice {
404 pub index: u32,
405 pub message: RequestMessage,
406 pub finish_reason: Option<String>,
407}
408
409#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
410pub struct ResponseMessageDelta {
411 pub role: Option<Role>,
412 pub content: Option<String>,
413 #[serde(default, skip_serializing_if = "is_none_or_empty")]
414 pub tool_calls: Option<Vec<ToolCallChunk>>,
415}
416
417#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
418pub struct ToolCallChunk {
419 pub index: usize,
420 pub id: Option<String>,
421
422 // There is also an optional `type` field that would determine if a
423 // function is there. Sometimes this streams in with the `function` before
424 // it streams in the `type`
425 pub function: Option<FunctionChunk>,
426}
427
428#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
429pub struct FunctionChunk {
430 pub name: Option<String>,
431 pub arguments: Option<String>,
432}
433
434#[derive(Clone, Serialize, Deserialize, Debug)]
435pub struct Usage {
436 pub prompt_tokens: u64,
437 pub completion_tokens: u64,
438 pub total_tokens: u64,
439}
440
441#[derive(Serialize, Deserialize, Debug)]
442pub struct ChoiceDelta {
443 pub index: u32,
444 pub delta: Option<ResponseMessageDelta>,
445 pub finish_reason: Option<String>,
446}
447
448#[derive(Error, Debug)]
449pub enum RequestError {
450 #[error("HTTP response error from {provider}'s API: status {status_code} - {body:?}")]
451 HttpResponseError {
452 provider: String,
453 status_code: StatusCode,
454 body: String,
455 headers: HeaderMap<HeaderValue>,
456 },
457 #[error(transparent)]
458 Other(#[from] anyhow::Error),
459}
460
461#[derive(Serialize, Deserialize, Debug)]
462pub struct ResponseStreamError {
463 message: String,
464}
465
466#[derive(Serialize, Deserialize, Debug)]
467#[serde(untagged)]
468pub enum ResponseStreamResult {
469 Ok(ResponseStreamEvent),
470 Err { error: ResponseStreamError },
471}
472
473#[derive(Serialize, Deserialize, Debug)]
474pub struct ResponseStreamEvent {
475 pub choices: Vec<ChoiceDelta>,
476 pub usage: Option<Usage>,
477}
478
479pub async fn stream_completion(
480 client: &dyn HttpClient,
481 provider_name: &str,
482 api_url: &str,
483 api_key: &str,
484 request: Request,
485) -> Result<BoxStream<'static, Result<ResponseStreamEvent>>, RequestError> {
486 let uri = format!("{api_url}/chat/completions");
487 let request_builder = HttpRequest::builder()
488 .method(Method::POST)
489 .uri(uri)
490 .header("Content-Type", "application/json")
491 .header("Authorization", format!("Bearer {}", api_key.trim()));
492
493 let request = request_builder
494 .body(AsyncBody::from(
495 serde_json::to_string(&request).map_err(|e| RequestError::Other(e.into()))?,
496 ))
497 .map_err(|e| RequestError::Other(e.into()))?;
498
499 let mut response = client.send(request).await?;
500 if response.status().is_success() {
501 let reader = BufReader::new(response.into_body());
502 Ok(reader
503 .lines()
504 .filter_map(|line| async move {
505 match line {
506 Ok(line) => {
507 let line = line.strip_prefix("data: ").or_else(|| line.strip_prefix("data:"))?;
508 if line == "[DONE]" {
509 None
510 } else {
511 match serde_json::from_str(line) {
512 Ok(ResponseStreamResult::Ok(response)) => Some(Ok(response)),
513 Ok(ResponseStreamResult::Err { error }) => {
514 Some(Err(anyhow!(error.message)))
515 }
516 Err(error) => {
517 log::error!(
518 "Failed to parse OpenAI response into ResponseStreamResult: `{}`\n\
519 Response: `{}`",
520 error,
521 line,
522 );
523 Some(Err(anyhow!(error)))
524 }
525 }
526 }
527 }
528 Err(error) => Some(Err(anyhow!(error))),
529 }
530 })
531 .boxed())
532 } else {
533 let mut body = String::new();
534 response
535 .body_mut()
536 .read_to_string(&mut body)
537 .await
538 .map_err(|e| RequestError::Other(e.into()))?;
539
540 Err(RequestError::HttpResponseError {
541 provider: provider_name.to_owned(),
542 status_code: response.status(),
543 body,
544 headers: response.headers().clone(),
545 })
546 }
547}
548
549#[derive(Copy, Clone, Serialize, Deserialize)]
550pub enum OpenAiEmbeddingModel {
551 #[serde(rename = "text-embedding-3-small")]
552 TextEmbedding3Small,
553 #[serde(rename = "text-embedding-3-large")]
554 TextEmbedding3Large,
555}
556
557#[derive(Serialize)]
558struct OpenAiEmbeddingRequest<'a> {
559 model: OpenAiEmbeddingModel,
560 input: Vec<&'a str>,
561}
562
563#[derive(Deserialize)]
564pub struct OpenAiEmbeddingResponse {
565 pub data: Vec<OpenAiEmbedding>,
566}
567
568#[derive(Deserialize)]
569pub struct OpenAiEmbedding {
570 pub embedding: Vec<f32>,
571}
572
573pub fn embed<'a>(
574 client: &dyn HttpClient,
575 api_url: &str,
576 api_key: &str,
577 model: OpenAiEmbeddingModel,
578 texts: impl IntoIterator<Item = &'a str>,
579) -> impl 'static + Future<Output = Result<OpenAiEmbeddingResponse>> {
580 let uri = format!("{api_url}/embeddings");
581
582 let request = OpenAiEmbeddingRequest {
583 model,
584 input: texts.into_iter().collect(),
585 };
586 let body = AsyncBody::from(serde_json::to_string(&request).unwrap());
587 let request = HttpRequest::builder()
588 .method(Method::POST)
589 .uri(uri)
590 .header("Content-Type", "application/json")
591 .header("Authorization", format!("Bearer {}", api_key.trim()))
592 .body(body)
593 .map(|request| client.send(request));
594
595 async move {
596 let mut response = request?.await?;
597 let mut body = String::new();
598 response.body_mut().read_to_string(&mut body).await?;
599
600 anyhow::ensure!(
601 response.status().is_success(),
602 "error during embedding, status: {:?}, body: {:?}",
603 response.status(),
604 body
605 );
606 let response: OpenAiEmbeddingResponse =
607 serde_json::from_str(&body).context("failed to parse OpenAI embedding response")?;
608 Ok(response)
609 }
610}