use async_trait::async_trait;
use futures::StreamExt;
use serde::Serialize;
use crate::ai::message::{ContentBlock, Message, Role, StopReason, Usage};
use crate::ai::model::{Context, Model};
use crate::ai::provider::{
CompleteOptions, Completion, Error, EventStream, Provider, Reasoning, StreamEvent,
};
use crate::ai::sse;
pub struct OpenAi;
#[async_trait]
impl Provider for OpenAi {
async fn complete(
&self,
http: &reqwest::Client,
model: &Model,
ctx: &Context,
opts: &CompleteOptions,
) -> Result<Completion, Error> {
let body = build_request(model, ctx, opts, false);
let resp = request(http, model, opts, &body).send().await?;
let status = resp.status();
let text = resp.text().await?;
if !status.is_success() {
return Err(Error::Api {
status: status.as_u16(),
message: text,
});
}
let parsed: WireResponse = serde_json::from_str(&text).map_err(|_| {
let preview = text.chars().take(200).collect::<String>();
Error::Parse(format!(
"server response was not in the expected format; got: {preview}"
))
})?;
let mut completion = parsed.into_completion()?;
if let Some((name, _)) = constrained_schema(ctx, opts) {
completion.message.content =
synthesize_constrained_call(&completion.message.content, name);
}
Ok(completion)
}
async fn stream(
&self,
http: &reqwest::Client,
model: &Model,
ctx: &Context,
opts: &CompleteOptions,
) -> Result<EventStream, Error> {
let body = build_request(model, ctx, opts, true);
let resp = request(http, model, opts, &body).send().await?;
let status = resp.status();
if !status.is_success() {
let text = resp.text().await?;
return Err(Error::Api {
status: status.as_u16(),
message: text,
});
}
let stream = async_stream::try_stream! {
let mut lines = Box::pin(sse::data_lines(resp));
let mut acc = StreamAccumulator::default();
let mut finished = false;
while let Some(line) = lines.next().await {
let line = line?;
if line == "[DONE]" {
yield acc.finish();
finished = true;
break;
}
let value: serde_json::Value = serde_json::from_str(&line)
.map_err(|_| Error::Parse(format!("stream contained an unexpected event: {line}")))?;
for event in acc.handle(&value) {
yield event;
}
}
if !finished {
yield acc.finish();
}
};
Ok(Box::pin(stream))
}
}
#[derive(Serialize)]
struct WireRequest {
model: String,
messages: Vec<WireMessage>,
max_tokens: u32,
#[serde(skip_serializing_if = "Vec::is_empty")]
tools: Vec<WireTool>,
#[serde(skip_serializing_if = "Option::is_none")]
tool_choice: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
response_format: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
reasoning_effort: Option<&'static str>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
stream: bool,
}
#[derive(Serialize)]
struct WireMessage {
role: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
tool_calls: Option<Vec<WireToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
tool_call_id: Option<String>,
}
#[derive(Serialize)]
struct WireToolCall {
id: String,
#[serde(rename = "type")]
kind: &'static str,
function: WireFunction,
}
#[derive(Serialize)]
struct WireFunction {
name: String,
arguments: String,
}
#[derive(Serialize)]
struct WireTool {
#[serde(rename = "type")]
kind: &'static str,
function: WireToolFunction,
}
#[derive(Serialize)]
struct WireToolFunction {
name: String,
description: String,
parameters: serde_json::Value,
}
fn build_request(
model: &Model,
ctx: &Context,
opts: &CompleteOptions,
stream: bool,
) -> serde_json::Value {
let mut messages = Vec::new();
if let Some(system) = &ctx.system {
messages.push(WireMessage {
role: "system",
content: Some(system.clone()),
tool_calls: None,
tool_call_id: None,
});
}
for message in &ctx.messages {
append_message(&mut messages, message);
}
let all_tools: Vec<WireTool> = ctx
.tools
.iter()
.map(|t| WireTool {
kind: "function",
function: WireToolFunction {
name: t.name.clone(),
description: t.description.clone(),
parameters: t.parameters.clone(),
},
})
.collect();
let (tools, tool_choice, response_format) = match constrained_schema(ctx, opts) {
Some((name, schema)) => (Vec::new(), None, Some(json_schema_format(name, schema))),
None if opts.tool_choice.is_some() => (all_tools, Some("required"), None),
None => (all_tools, None, None),
};
let request = WireRequest {
model: model.id.clone(),
messages,
max_tokens: opts.max_tokens.unwrap_or(model.max_tokens),
tools,
tool_choice,
response_format,
reasoning_effort: match opts.reasoning {
Reasoning::Off => None,
Reasoning::Low => Some("low"),
Reasoning::Medium => Some("medium"),
Reasoning::High => Some("high"),
},
stream,
};
serde_json::to_value(request).expect("request serializes")
}
fn constrained_schema<'a>(
ctx: &'a Context,
opts: &'a CompleteOptions,
) -> Option<(&'a str, serde_json::Value)> {
if !opts.constrained_decoding {
return None;
}
let name = opts.tool_choice.as_deref()?;
let schema = ctx
.tools
.iter()
.find(|t| t.name == name)?
.parameters
.clone();
Some((name, schema))
}
fn json_schema_format(name: &str, mut schema: serde_json::Value) -> serde_json::Value {
if let Some(obj) = schema.as_object_mut() {
obj.insert(
"additionalProperties".into(),
serde_json::Value::Bool(false),
);
}
serde_json::json!({
"type": "json_schema",
"json_schema": { "name": name, "schema": schema, "strict": true },
})
}
fn synthesize_constrained_call(content: &[ContentBlock], tool: &str) -> Vec<ContentBlock> {
let args = content
.iter()
.find_map(|b| match b {
ContentBlock::Text { text } => Some(text.as_str()),
_ => None,
})
.unwrap_or("");
vec![ContentBlock::ToolCall {
id: format!("constrained_{tool}"),
name: tool.to_string(),
arguments: parse_args(args),
}]
}
fn append_message(messages: &mut Vec<WireMessage>, message: &Message) {
match message.role {
Role::User => {
let mut text = String::new();
for block in &message.content {
match block {
ContentBlock::Text { text: t } => {
if !text.is_empty() {
text.push('\n');
}
text.push_str(t);
}
ContentBlock::ToolResult {
tool_call_id,
content,
..
} => {
messages.push(WireMessage {
role: "tool",
content: Some(content.clone()),
tool_calls: None,
tool_call_id: Some(tool_call_id.clone()),
});
}
_ => {}
}
}
if !text.is_empty() {
messages.push(WireMessage {
role: "user",
content: Some(text),
tool_calls: None,
tool_call_id: None,
});
}
}
Role::Assistant => {
let mut text = String::new();
let mut tool_calls = Vec::new();
for block in &message.content {
match block {
ContentBlock::Text { text: t } => {
if !text.is_empty() {
text.push('\n');
}
text.push_str(t);
}
ContentBlock::ToolCall {
id,
name,
arguments,
} => {
tool_calls.push(WireToolCall {
id: id.clone(),
kind: "function",
function: WireFunction {
name: name.clone(),
arguments: arguments.to_string(),
},
});
}
_ => {}
}
}
messages.push(WireMessage {
role: "assistant",
content: (!text.is_empty()).then_some(text),
tool_calls: (!tool_calls.is_empty()).then_some(tool_calls),
tool_call_id: None,
});
}
}
}
fn request(
http: &reqwest::Client,
model: &Model,
opts: &CompleteOptions,
body: &serde_json::Value,
) -> reqwest::RequestBuilder {
let base = model.base_url.trim_end_matches("/v1");
let mut builder = http
.post(format!("{base}/v1/chat/completions"))
.header("content-type", "application/json")
.json(body);
if let Some(key) = opts
.api_key
.clone()
.or_else(|| std::env::var("LMSTUDIO_API_KEY").ok())
{
builder = builder.bearer_auth(key);
}
builder
}
pub(crate) async fn list_models(http: &reqwest::Client) -> Result<Vec<Model>, Error> {
let base_url = crate::ai::catalog::lmstudio_base_url();
let resp = http.get(format!("{base_url}/v1/models")).send().await?;
let status = resp.status();
let text = resp.text().await?;
if !status.is_success() {
return Err(Error::Api {
status: status.as_u16(),
message: text,
});
}
let parsed: WireModelList =
serde_json::from_str(&text).map_err(|e| Error::Parse(e.to_string()))?;
Ok(parsed
.data
.into_iter()
.map(|m| crate::ai::catalog::lmstudio_model(&m.id))
.collect())
}
#[derive(serde::Deserialize)]
struct WireModelList {
data: Vec<WireModelEntry>,
}
#[derive(serde::Deserialize)]
struct WireModelEntry {
id: String,
}
#[derive(serde::Deserialize)]
struct WireResponse {
choices: Vec<WireChoice>,
#[serde(default)]
usage: WireUsage,
}
#[derive(serde::Deserialize)]
struct WireChoice {
message: WireResponseMessage,
finish_reason: Option<String>,
}
#[derive(serde::Deserialize)]
struct WireResponseMessage {
content: Option<String>,
#[serde(default)]
tool_calls: Vec<WireResponseToolCall>,
}
#[derive(serde::Deserialize)]
struct WireResponseToolCall {
id: String,
function: WireResponseFunction,
}
#[derive(serde::Deserialize)]
struct WireResponseFunction {
name: String,
arguments: String,
}
#[derive(serde::Deserialize, Default)]
struct WireUsage {
#[serde(default)]
prompt_tokens: u32,
#[serde(default)]
completion_tokens: u32,
}
fn map_finish_reason(reason: Option<&str>) -> StopReason {
match reason {
Some("stop") => StopReason::Stop,
Some("length") => StopReason::Length,
Some("tool_calls") => StopReason::ToolUse,
Some("content_filter") => StopReason::Refusal,
_ => StopReason::Error,
}
}
impl WireResponse {
fn into_completion(self) -> Result<Completion, Error> {
let choice = self
.choices
.into_iter()
.next()
.ok_or_else(|| Error::Parse("response had no choices".into()))?;
let mut content = Vec::new();
if let Some(text) = choice.message.content {
if !text.is_empty() {
content.push(ContentBlock::Text { text });
}
}
for call in choice.message.tool_calls {
content.push(ContentBlock::ToolCall {
id: call.id,
name: call.function.name,
arguments: parse_args(&call.function.arguments),
});
}
Ok(Completion {
message: Message {
role: Role::Assistant,
content,
},
stop_reason: map_finish_reason(choice.finish_reason.as_deref()),
usage: Usage {
input: self.usage.prompt_tokens,
output: self.usage.completion_tokens,
},
})
}
}
#[derive(Default)]
struct ToolCallBuild {
id: String,
name: String,
args: String,
}
#[derive(Default)]
struct StreamAccumulator {
text: String,
tool_calls: Vec<ToolCallBuild>,
finish_reason: Option<String>,
prompt_tokens: u32,
completion_tokens: u32,
}
impl StreamAccumulator {
fn handle(&mut self, value: &serde_json::Value) -> Vec<StreamEvent> {
let mut events = Vec::new();
if let Some(usage) = value.get("usage").filter(|u| u.is_object()) {
self.prompt_tokens = usage["prompt_tokens"]
.as_u64()
.unwrap_or(self.prompt_tokens as u64) as u32;
self.completion_tokens = usage["completion_tokens"]
.as_u64()
.unwrap_or(self.completion_tokens as u64)
as u32;
}
let Some(choice) = value["choices"].get(0) else {
return events;
};
if let Some(reason) = choice["finish_reason"].as_str() {
self.finish_reason = Some(reason.to_string());
}
let delta = &choice["delta"];
if let Some(text) = delta["content"].as_str() {
if !text.is_empty() {
self.text.push_str(text);
events.push(StreamEvent::TextDelta {
content_index: 0,
delta: text.to_string(),
});
}
}
if let Some(calls) = delta["tool_calls"].as_array() {
for call in calls {
let index = call["index"].as_u64().unwrap_or(0) as usize;
if index >= self.tool_calls.len() {
self.tool_calls
.resize_with(index + 1, ToolCallBuild::default);
}
let build = &mut self.tool_calls[index];
let id = call["id"].as_str();
if let Some(id) = id {
build.id = id.to_string();
}
let name = call["function"]["name"].as_str();
if let Some(name) = name {
build.name = name.to_string();
}
let args = call["function"]["arguments"].as_str().unwrap_or("");
build.args.push_str(args);
events.push(StreamEvent::ToolCallDelta {
content_index: index + 1,
id: id.map(str::to_string),
name: name.map(str::to_string),
arguments_delta: args.to_string(),
});
}
}
events
}
fn finish(&mut self) -> StreamEvent {
let mut content = Vec::new();
if !self.text.is_empty() {
content.push(ContentBlock::Text {
text: std::mem::take(&mut self.text),
});
}
for call in self.tool_calls.drain(..) {
content.push(ContentBlock::ToolCall {
id: call.id,
name: call.name,
arguments: parse_args(&call.args),
});
}
StreamEvent::Done {
stop_reason: map_finish_reason(self.finish_reason.as_deref()),
usage: Usage {
input: self.prompt_tokens,
output: self.completion_tokens,
},
message: Message {
role: Role::Assistant,
content,
},
}
}
}
fn parse_args(raw: &str) -> serde_json::Value {
if raw.trim().is_empty() {
return serde_json::json!({});
}
serde_json::from_str(raw).unwrap_or_else(|_| serde_json::Value::String(raw.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ai::catalog::lmstudio_model;
fn model() -> Model {
lmstudio_model("local-model")
}
#[test]
fn system_is_folded_into_messages_and_tool_result_is_a_tool_message() {
let ctx = Context {
system: Some("be terse".into()),
messages: vec![Message {
role: Role::User,
content: vec![
ContentBlock::ToolResult {
tool_call_id: "call_1".into(),
content: "42".into(),
is_error: false,
},
ContentBlock::text("thanks"),
],
}],
tools: vec![],
};
let body = build_request(&model(), &ctx, &CompleteOptions::default(), false);
let messages = body["messages"].as_array().unwrap();
assert_eq!(messages[0]["role"], "system");
assert_eq!(messages[1]["role"], "tool");
assert_eq!(messages[1]["tool_call_id"], "call_1");
assert_eq!(messages[2]["role"], "user");
assert_eq!(messages[2]["content"], "thanks");
}
#[test]
fn assistant_tool_call_serializes_as_function_call() {
let ctx = Context {
system: None,
messages: vec![Message {
role: Role::Assistant,
content: vec![ContentBlock::ToolCall {
id: "call_1".into(),
name: "get_weather".into(),
arguments: serde_json::json!({ "city": "Paris" }),
}],
}],
tools: vec![],
};
let body = build_request(&model(), &ctx, &CompleteOptions::default(), false);
let call = &body["messages"][0]["tool_calls"][0];
assert_eq!(call["function"]["name"], "get_weather");
assert_eq!(call["function"]["arguments"], r#"{"city":"Paris"}"#);
}
#[test]
fn forced_tool_choice_uses_required_and_default_omits_it() {
let opts = CompleteOptions {
tool_choice: Some("get_weather".into()),
..Default::default()
};
let body = build_request(&model(), &Context::default(), &opts, false);
assert_eq!(body["tool_choice"], "required");
assert!(body.get("response_format").is_none());
let default = build_request(
&model(),
&Context::default(),
&CompleteOptions::default(),
false,
);
assert!(default.get("tool_choice").is_none());
}
#[test]
fn constrained_decoding_emits_response_format_and_drops_tools() {
let ctx = Context {
system: None,
messages: vec![],
tools: vec![crate::ai::model::Tool {
name: "classify".into(),
description: "Classify".into(),
parameters: serde_json::json!({
"type": "object",
"properties": { "label": { "type": "string" } },
"required": ["label"],
}),
}],
};
let opts = CompleteOptions {
tool_choice: Some("classify".into()),
constrained_decoding: true,
..Default::default()
};
let body = build_request(&model(), &ctx, &opts, false);
assert!(body.get("tools").is_none());
assert!(body.get("tool_choice").is_none());
let format = &body["response_format"];
assert_eq!(format["type"], "json_schema");
assert_eq!(format["json_schema"]["name"], "classify");
assert_eq!(format["json_schema"]["strict"], true);
assert_eq!(
format["json_schema"]["schema"]["additionalProperties"],
false
);
assert_eq!(
format["json_schema"]["schema"]["properties"]["label"]["type"],
"string"
);
}
#[test]
fn synthesize_constrained_call_wraps_json_text_as_a_tool_call() {
let content = vec![ContentBlock::Text {
text: r#"{"label":"spam"}"#.into(),
}];
let call = synthesize_constrained_call(&content, "classify");
assert_eq!(
call,
vec![ContentBlock::ToolCall {
id: "constrained_classify".into(),
name: "classify".into(),
arguments: serde_json::json!({ "label": "spam" }),
}]
);
}
#[test]
fn parses_response_with_tool_call() {
let raw = r#"{
"choices": [{
"message": {"content": null, "tool_calls": [
{"id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": "{\"city\":\"Paris\"}"}}
]},
"finish_reason": "tool_calls"
}],
"usage": {"prompt_tokens": 8, "completion_tokens": 4}
}"#;
let parsed: WireResponse = serde_json::from_str(raw).unwrap();
let completion = parsed.into_completion().unwrap();
assert_eq!(completion.stop_reason, StopReason::ToolUse);
assert_eq!(
completion.message.content[0],
ContentBlock::ToolCall {
id: "call_1".into(),
name: "get_weather".into(),
arguments: serde_json::json!({ "city": "Paris" }),
}
);
assert_eq!(completion.usage.input, 8);
assert_eq!(completion.usage.output, 4);
}
#[test]
fn reasoning_effort_maps_per_level_and_off_is_omitted() {
let off = build_request(
&model(),
&Context::default(),
&CompleteOptions::default(),
false,
);
assert!(off.get("reasoning_effort").is_none());
for (level, effort) in [
(Reasoning::Low, "low"),
(Reasoning::Medium, "medium"),
(Reasoning::High, "high"),
] {
let opts = CompleteOptions {
reasoning: level,
..Default::default()
};
let body = build_request(&model(), &Context::default(), &opts, false);
assert_eq!(body["reasoning_effort"], effort);
}
}
#[test]
fn parses_text_only_response() {
let raw = r#"{
"choices": [{"message": {"content": "hello"}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 2, "completion_tokens": 1}
}"#;
let parsed: WireResponse = serde_json::from_str(raw).unwrap();
let completion = parsed.into_completion().unwrap();
assert_eq!(
completion.message.content,
vec![ContentBlock::text("hello")]
);
assert_eq!(completion.stop_reason, StopReason::Stop);
}
#[test]
fn response_without_choices_is_a_parse_error() {
let raw = r#"{"choices": [], "usage": {"prompt_tokens": 0, "completion_tokens": 0}}"#;
let parsed: WireResponse = serde_json::from_str(raw).unwrap();
assert!(matches!(parsed.into_completion(), Err(Error::Parse(_))));
}
#[test]
fn finish_reasons_map_to_unified_variants() {
assert_eq!(map_finish_reason(Some("stop")), StopReason::Stop);
assert_eq!(map_finish_reason(Some("length")), StopReason::Length);
assert_eq!(map_finish_reason(Some("tool_calls")), StopReason::ToolUse);
assert_eq!(
map_finish_reason(Some("content_filter")),
StopReason::Refusal
);
assert_eq!(map_finish_reason(Some("something_new")), StopReason::Error);
assert_eq!(map_finish_reason(None), StopReason::Error);
}
#[test]
fn stream_accumulator_assembles_text_then_done() {
let mut acc = StreamAccumulator::default();
let mut out = Vec::new();
for chunk in [
serde_json::json!({"choices": [{"delta": {"content": "Hel"}}]}),
serde_json::json!({"choices": [{"delta": {"content": "lo"}}]}),
serde_json::json!({"choices": [{"delta": {}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 3, "completion_tokens": 2}}),
] {
out.extend(acc.handle(&chunk));
}
out.push(acc.finish());
assert_eq!(out.len(), 3);
match out.last().unwrap() {
StreamEvent::Done {
stop_reason,
usage,
message,
} => {
assert_eq!(*stop_reason, StopReason::Stop);
assert_eq!(usage.output, 2);
assert_eq!(message.content, vec![ContentBlock::text("Hello")]);
}
other => panic!("expected Done, got {other:?}"),
}
}
}