1110 lines
38 KiB
Rust
1110 lines
38 KiB
Rust
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use codex_client::HttpTransport;
|
|
use codex_client::Request;
|
|
use codex_client::ReqwestTransport;
|
|
use codex_client::TransportError;
|
|
use codex_client::sse_stream;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiAdapterStatus;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiAuthMode;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiInputItem;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiModelReceipt;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiModelRequest;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiStreamEvent;
|
|
use epiphany_openai_adapter::EpiphanyOpenAiStreamPayload;
|
|
use epiphany_openai_adapter::OPENAI_ADAPTER_STATUS_SCHEMA_ID;
|
|
use epiphany_openai_auth_spine::AuthCredentialsStoreMode;
|
|
use epiphany_openai_auth_spine::AuthManager;
|
|
use epiphany_openai_auth_spine::AuthMode;
|
|
use epiphany_openai_auth_spine::CodexAuth;
|
|
use epiphany_openai_auth_spine::build_reqwest_client;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use serde_json::Value;
|
|
|
|
const CHATGPT_CODEX_BASE_URL: &str = "https://chatgpt.com/backend-api/codex";
|
|
const OPENAI_API_BASE_URL: &str = "https://api.openai.com/v1";
|
|
// Keep provider silence shorter than Epiphany worker watchdogs so the runtime can
|
|
// write a typed stream failure instead of being killed with no model evidence.
|
|
const RESPONSES_STREAM_IDLE_TIMEOUT: Duration = Duration::from_secs(45);
|
|
|
|
pub const CODEX_SPINE_ADAPTER_ID: &str = "codex-openai-subscription-spine";
|
|
|
|
pub fn default_codex_home() -> Result<std::path::PathBuf> {
|
|
if let Ok(path) = std::env::var("CODEX_HOME") {
|
|
return Ok(std::path::PathBuf::from(path));
|
|
}
|
|
let home = std::env::var("USERPROFILE")
|
|
.or_else(|_| std::env::var("HOME"))
|
|
.context("CODEX_HOME is unset and no home directory environment variable exists")?;
|
|
Ok(std::path::PathBuf::from(home).join(".codex"))
|
|
}
|
|
|
|
pub fn auth_manager(codex_home: std::path::PathBuf) -> Arc<AuthManager> {
|
|
AuthManager::shared(
|
|
codex_home,
|
|
/*enable_codex_api_key_env*/ true,
|
|
AuthCredentialsStoreMode::Auto,
|
|
/*chatgpt_base_url*/ None,
|
|
)
|
|
}
|
|
|
|
pub async fn status_from_auth_manager(
|
|
auth_manager: &AuthManager,
|
|
default_model: Option<String>,
|
|
supports_websockets: bool,
|
|
) -> EpiphanyOpenAiAdapterStatus {
|
|
let auth = auth_manager.auth().await;
|
|
let auth_mode = auth_mode_from_manager(auth_manager, auth.as_ref());
|
|
let account_id = auth.as_ref().and_then(CodexAuth::get_account_id);
|
|
let plan_type = auth
|
|
.as_ref()
|
|
.and_then(CodexAuth::account_plan_type)
|
|
.map(|plan| format!("{plan:?}"));
|
|
|
|
EpiphanyOpenAiAdapterStatus {
|
|
schema_id: OPENAI_ADAPTER_STATUS_SCHEMA_ID.to_string(),
|
|
adapter_id: CODEX_SPINE_ADAPTER_ID.to_string(),
|
|
auth_mode,
|
|
account_id,
|
|
plan_type,
|
|
default_model,
|
|
supports_websockets,
|
|
codex_transport_attached: true,
|
|
}
|
|
}
|
|
|
|
pub fn status_from_codex_auth(
|
|
auth: Option<&CodexAuth>,
|
|
default_model: Option<String>,
|
|
supports_websockets: bool,
|
|
) -> EpiphanyOpenAiAdapterStatus {
|
|
EpiphanyOpenAiAdapterStatus {
|
|
schema_id: OPENAI_ADAPTER_STATUS_SCHEMA_ID.to_string(),
|
|
adapter_id: CODEX_SPINE_ADAPTER_ID.to_string(),
|
|
auth_mode: auth_mode_from_codex_auth(auth),
|
|
account_id: auth.and_then(CodexAuth::get_account_id),
|
|
plan_type: auth
|
|
.and_then(CodexAuth::account_plan_type)
|
|
.map(|plan| format!("{plan:?}")),
|
|
default_model,
|
|
supports_websockets,
|
|
codex_transport_attached: true,
|
|
}
|
|
}
|
|
|
|
pub struct EpiphanyCodexOpenAiTransport {
|
|
auth_manager: Arc<AuthManager>,
|
|
base_url: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct EpiphanyResponsesFrameObservation {
|
|
pub frame_sequence: u64,
|
|
pub kind: String,
|
|
pub recognized: bool,
|
|
pub delta_preview: Option<String>,
|
|
}
|
|
|
|
impl EpiphanyCodexOpenAiTransport {
|
|
pub fn new(auth_manager: Arc<AuthManager>, base_url: Option<String>) -> Self {
|
|
Self {
|
|
auth_manager,
|
|
base_url,
|
|
}
|
|
}
|
|
|
|
pub fn openai(auth_manager: Arc<AuthManager>) -> Self {
|
|
Self::new(auth_manager, None)
|
|
}
|
|
|
|
pub async fn collect_model_events(
|
|
&self,
|
|
request: EpiphanyOpenAiModelRequest,
|
|
) -> Result<Vec<EpiphanyOpenAiStreamEvent>> {
|
|
self.collect_model_events_with_frame_observer(request, |_| {})
|
|
.await
|
|
}
|
|
|
|
pub async fn collect_model_events_with_frame_observer(
|
|
&self,
|
|
request: EpiphanyOpenAiModelRequest,
|
|
mut observe_frame: impl FnMut(EpiphanyResponsesFrameObservation),
|
|
) -> Result<Vec<EpiphanyOpenAiStreamEvent>> {
|
|
let request_id = request.request_id.clone();
|
|
let model = request.model.clone();
|
|
let auth = self
|
|
.auth_manager
|
|
.auth()
|
|
.await
|
|
.ok_or_else(|| anyhow::anyhow!("Codex auth is unavailable"))?;
|
|
let stream_response = self.open_responses_stream(&auth, request).await?;
|
|
let (tx, mut rx) = tokio::sync::mpsc::channel(1600);
|
|
sse_stream(stream_response.bytes, RESPONSES_STREAM_IDLE_TIMEOUT, tx);
|
|
|
|
let mut stream_state = EpiphanyResponsesStreamState::new(&request_id, &model);
|
|
while let Some(frame) = rx.recv().await {
|
|
match frame {
|
|
Ok(frame) => {
|
|
let observation = stream_state.push_sse_frame(&frame);
|
|
observe_frame(observation);
|
|
}
|
|
Err(err) => stream_state.push_failed(err.to_string()),
|
|
}
|
|
if stream_state.completed {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if stream_state.events.is_empty() {
|
|
stream_state.push_failed("Responses stream closed without typed events".to_string());
|
|
}
|
|
Ok(stream_state.events)
|
|
}
|
|
|
|
async fn open_responses_stream(
|
|
&self,
|
|
auth: &CodexAuth,
|
|
request: EpiphanyOpenAiModelRequest,
|
|
) -> Result<codex_client::StreamResponse> {
|
|
let base_url = self
|
|
.base_url
|
|
.clone()
|
|
.unwrap_or_else(|| default_base_url_for_auth(auth).to_string());
|
|
let url = format!("{}/responses", base_url.trim_end_matches('/'));
|
|
let conversation_id = request.conversation_id.clone();
|
|
let mut outbound = Request::new(http::Method::POST, url)
|
|
.with_json(&responses_body_from_epiphany(request)?);
|
|
attach_codex_auth_headers(auth, &mut outbound.headers)
|
|
.context("failed to attach Codex auth headers")?;
|
|
outbound
|
|
.headers
|
|
.insert(http::header::ACCEPT, "text/event-stream".parse()?);
|
|
outbound
|
|
.headers
|
|
.insert("session_id", conversation_id.parse()?);
|
|
outbound
|
|
.headers
|
|
.insert("x-client-request-id", conversation_id.parse()?);
|
|
attach_optional_env_header(
|
|
&mut outbound.headers,
|
|
"OpenAI-Organization",
|
|
"OPENAI_ORGANIZATION",
|
|
);
|
|
attach_optional_env_header(&mut outbound.headers, "OpenAI-Project", "OPENAI_PROJECT");
|
|
|
|
let transport = ReqwestTransport::new(build_reqwest_client());
|
|
transport
|
|
.stream(outbound)
|
|
.await
|
|
.map_err(transport_error_to_anyhow)
|
|
}
|
|
}
|
|
|
|
pub fn responses_body_from_epiphany(
|
|
request: EpiphanyOpenAiModelRequest,
|
|
) -> Result<serde_json::Value> {
|
|
let requires_initial_tool = !request.tools.is_empty()
|
|
&& !request
|
|
.input
|
|
.iter()
|
|
.any(|item| matches!(item, EpiphanyOpenAiInputItem::ToolResult { .. }));
|
|
let text = responses_text_format(
|
|
request.output_contract_id.as_deref(),
|
|
request.output_schema_json.as_deref(),
|
|
&request.request_id,
|
|
)?;
|
|
let body = EpiphanyResponsesBody {
|
|
model: request.model,
|
|
instructions: request.instructions,
|
|
input: request
|
|
.input
|
|
.into_iter()
|
|
.map(openai_input_item_from_epiphany_input)
|
|
.collect(),
|
|
tools: request
|
|
.tools
|
|
.into_iter()
|
|
.map(openai_tool_from_epiphany_tool)
|
|
.collect::<Result<Vec<_>>>()?,
|
|
tool_choice: if requires_initial_tool {
|
|
"required"
|
|
} else {
|
|
"auto"
|
|
}
|
|
.to_string(),
|
|
parallel_tool_calls: false,
|
|
reasoning: Some(EpiphanyResponsesReasoning {
|
|
effort: parse_reasoning_effort(request.reasoning_effort.as_deref())?,
|
|
summary: parse_reasoning_summary(request.reasoning_summary.as_deref())?,
|
|
}),
|
|
store: false,
|
|
stream: true,
|
|
include: Vec::new(),
|
|
service_tier: parse_service_tier(request.service_tier.as_deref())?,
|
|
previous_response_id: request.previous_response_id,
|
|
prompt_cache_key: None,
|
|
text,
|
|
client_metadata: None,
|
|
};
|
|
serde_json::to_value(body).context("failed to encode typed Epiphany Responses body")
|
|
}
|
|
|
|
fn responses_text_format(
|
|
output_contract_id: Option<&str>,
|
|
output_schema_json: Option<&str>,
|
|
request_id: &str,
|
|
) -> Result<Option<serde_json::Value>> {
|
|
let Some(schema_json) = output_schema_json else {
|
|
return Ok(None);
|
|
};
|
|
let mut schema: serde_json::Value =
|
|
serde_json::from_str(schema_json).context("output_schema_json is not valid JSON Schema")?;
|
|
lower_schema_for_responses_format(&mut schema);
|
|
let strict = responses_schema_is_strict(&schema);
|
|
let raw_name = output_contract_id.unwrap_or(request_id);
|
|
let mut name = raw_name
|
|
.chars()
|
|
.map(|ch| {
|
|
if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' {
|
|
ch
|
|
} else {
|
|
'_'
|
|
}
|
|
})
|
|
.take(64)
|
|
.collect::<String>();
|
|
if name.is_empty() {
|
|
name = "epiphany_worker_result".to_string();
|
|
}
|
|
Ok(Some(serde_json::json!({
|
|
"format": {
|
|
"type": "json_schema",
|
|
"name": name,
|
|
"strict": strict,
|
|
"schema": schema
|
|
}
|
|
})))
|
|
}
|
|
|
|
fn responses_schema_is_strict(schema: &serde_json::Value) -> bool {
|
|
match schema {
|
|
serde_json::Value::Object(map) => {
|
|
if map.get("type").and_then(serde_json::Value::as_str) == Some("object") {
|
|
if map.get("additionalProperties") != Some(&serde_json::Value::Bool(false)) {
|
|
return false;
|
|
}
|
|
let Some(properties) = map
|
|
.get("properties")
|
|
.and_then(serde_json::Value::as_object)
|
|
else {
|
|
return false;
|
|
};
|
|
let Some(required) = map
|
|
.get("required")
|
|
.and_then(serde_json::Value::as_array)
|
|
else {
|
|
return false;
|
|
};
|
|
if properties
|
|
.keys()
|
|
.any(|key| !required.iter().any(|item| item.as_str() == Some(key)))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
map.values().all(responses_schema_is_strict)
|
|
}
|
|
serde_json::Value::Array(values) => values.iter().all(responses_schema_is_strict),
|
|
_ => true,
|
|
}
|
|
}
|
|
|
|
fn lower_schema_for_responses_format(schema: &mut serde_json::Value) {
|
|
match schema {
|
|
serde_json::Value::Object(map) => {
|
|
// Responses structured output accepts only a JSON-Schema subset.
|
|
// Canonical conditional authority remains enforced by Epiphany ingress
|
|
// and Mind admission; this projection owns formatting only.
|
|
for unsupported in ["allOf", "if", "then", "else"] {
|
|
map.remove(unsupported);
|
|
}
|
|
if let Some(one_of) = map.remove("oneOf") {
|
|
map.insert("anyOf".to_string(), one_of);
|
|
}
|
|
for value in map.values_mut() {
|
|
lower_schema_for_responses_format(value);
|
|
}
|
|
}
|
|
serde_json::Value::Array(values) => {
|
|
for value in values {
|
|
lower_schema_for_responses_format(value);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn openai_tool_from_epiphany_tool(
|
|
tool: epiphany_openai_adapter::EpiphanyOpenAiToolDefinition,
|
|
) -> Result<serde_json::Value> {
|
|
let parameters: serde_json::Value = serde_json::from_str(&tool.parameters_json)
|
|
.with_context(|| format!("tool {} parameters_json is not valid JSON", tool.name))?;
|
|
Ok(serde_json::json!({
|
|
"type": "function",
|
|
"name": tool.name,
|
|
"description": tool.description,
|
|
"parameters": parameters,
|
|
"strict": false
|
|
}))
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
struct EpiphanyResponsesBody {
|
|
model: String,
|
|
#[serde(skip_serializing_if = "String::is_empty")]
|
|
instructions: String,
|
|
input: Vec<EpiphanyResponsesInputItem>,
|
|
tools: Vec<serde_json::Value>,
|
|
tool_choice: String,
|
|
parallel_tool_calls: bool,
|
|
reasoning: Option<EpiphanyResponsesReasoning>,
|
|
store: bool,
|
|
stream: bool,
|
|
include: Vec<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
service_tier: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
previous_response_id: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
prompt_cache_key: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
text: Option<serde_json::Value>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
client_metadata: Option<std::collections::HashMap<String, String>>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
struct EpiphanyResponsesReasoning {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
effort: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
summary: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
enum EpiphanyResponsesInputItem {
|
|
Message {
|
|
role: String,
|
|
content: Vec<EpiphanyResponsesContentItem>,
|
|
},
|
|
FunctionCall {
|
|
call_id: String,
|
|
name: String,
|
|
arguments: String,
|
|
},
|
|
FunctionCallOutput {
|
|
call_id: String,
|
|
output: String,
|
|
},
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
enum EpiphanyResponsesContentItem {
|
|
InputText { text: String },
|
|
OutputText { text: String },
|
|
}
|
|
|
|
fn openai_input_item_from_epiphany_input(
|
|
input: EpiphanyOpenAiInputItem,
|
|
) -> EpiphanyResponsesInputItem {
|
|
match input {
|
|
EpiphanyOpenAiInputItem::UserText { text } => EpiphanyResponsesInputItem::Message {
|
|
role: "user".to_string(),
|
|
content: vec![EpiphanyResponsesContentItem::InputText { text }],
|
|
},
|
|
EpiphanyOpenAiInputItem::AssistantText { text } => EpiphanyResponsesInputItem::Message {
|
|
role: "assistant".to_string(),
|
|
content: vec![EpiphanyResponsesContentItem::OutputText { text }],
|
|
},
|
|
EpiphanyOpenAiInputItem::ToolCall {
|
|
call_id,
|
|
name,
|
|
arguments,
|
|
} => EpiphanyResponsesInputItem::FunctionCall {
|
|
call_id,
|
|
name,
|
|
arguments,
|
|
},
|
|
EpiphanyOpenAiInputItem::ToolResult { call_id, output } => {
|
|
EpiphanyResponsesInputItem::FunctionCallOutput { call_id, output }
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct EpiphanyResponsesStreamEvent {
|
|
#[serde(rename = "type")]
|
|
kind: String,
|
|
response: Option<Value>,
|
|
item: Option<Value>,
|
|
item_id: Option<String>,
|
|
call_id: Option<String>,
|
|
delta: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct EpiphanyResponseCompleted {
|
|
id: String,
|
|
#[serde(default)]
|
|
usage: Option<EpiphanyResponseCompletedUsage>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct EpiphanyResponseCompletedUsage {
|
|
input_tokens: i64,
|
|
output_tokens: i64,
|
|
output_tokens_details: Option<EpiphanyResponseCompletedOutputTokensDetails>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct EpiphanyResponseCompletedOutputTokensDetails {
|
|
reasoning_tokens: i64,
|
|
}
|
|
|
|
struct EpiphanyResponsesStreamState {
|
|
request_id: String,
|
|
requested_model: String,
|
|
sequence: u64,
|
|
frame_sequence: u64,
|
|
completed: bool,
|
|
pending_tool_calls: HashMap<String, PendingToolCall>,
|
|
events: Vec<EpiphanyOpenAiStreamEvent>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
struct PendingToolCall {
|
|
call_id: Option<String>,
|
|
name: Option<String>,
|
|
arguments: String,
|
|
}
|
|
|
|
impl EpiphanyResponsesStreamState {
|
|
fn new(request_id: &str, requested_model: &str) -> Self {
|
|
Self {
|
|
request_id: request_id.to_string(),
|
|
requested_model: requested_model.to_string(),
|
|
sequence: 0,
|
|
frame_sequence: 0,
|
|
completed: false,
|
|
pending_tool_calls: HashMap::new(),
|
|
events: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn push_sse_frame(&mut self, frame: &str) -> EpiphanyResponsesFrameObservation {
|
|
let frame_sequence = self.frame_sequence;
|
|
self.frame_sequence += 1;
|
|
let Ok(event) = serde_json::from_str::<EpiphanyResponsesStreamEvent>(frame) else {
|
|
return EpiphanyResponsesFrameObservation {
|
|
frame_sequence,
|
|
kind: "unparseable".to_string(),
|
|
recognized: false,
|
|
delta_preview: None,
|
|
};
|
|
};
|
|
let kind = event.kind.clone();
|
|
let delta_preview = event.delta.as_deref().map(delta_preview);
|
|
let recognized = match event.kind.as_str() {
|
|
"response.output_text.delta" => {
|
|
if let Some(text) = event.delta {
|
|
self.push_payload(EpiphanyOpenAiStreamPayload::TextDelta { text });
|
|
}
|
|
true
|
|
}
|
|
"response.reasoning_summary_text.delta" | "response.reasoning_text.delta" => {
|
|
if let Some(text) = event.delta {
|
|
self.push_payload(EpiphanyOpenAiStreamPayload::ReasoningDelta { text });
|
|
}
|
|
true
|
|
}
|
|
"response.output_item.added" => {
|
|
if let Some(item) = event.item {
|
|
self.seed_tool_call_from_item(&item);
|
|
}
|
|
true
|
|
}
|
|
"response.function_call_arguments.delta" => {
|
|
if let (Some(arguments), Some(item_id)) =
|
|
(event.delta, event.item_id.clone().or(event.call_id.clone()))
|
|
{
|
|
self.pending_tool_calls
|
|
.entry(item_id.clone())
|
|
.or_insert_with(|| PendingToolCall {
|
|
call_id: event.call_id.clone().or(Some(item_id)),
|
|
name: None,
|
|
arguments: String::new(),
|
|
})
|
|
.arguments
|
|
.push_str(&arguments);
|
|
}
|
|
true
|
|
}
|
|
"response.custom_tool_call_input.delta" => {
|
|
if let (Some(arguments), Some(item_id)) =
|
|
(event.delta, event.item_id.clone().or(event.call_id.clone()))
|
|
{
|
|
self.pending_tool_calls
|
|
.entry(item_id.clone())
|
|
.or_insert_with(|| PendingToolCall {
|
|
call_id: event.call_id.clone().or(Some(item_id)),
|
|
name: None,
|
|
arguments: String::new(),
|
|
})
|
|
.arguments
|
|
.push_str(&arguments);
|
|
}
|
|
true
|
|
}
|
|
"response.output_item.done" => {
|
|
if let Some(item) = event.item {
|
|
self.push_tool_call_from_done_item(&item);
|
|
}
|
|
true
|
|
}
|
|
"response.completed" => {
|
|
if let Some(response) = event.response {
|
|
match serde_json::from_value::<EpiphanyResponseCompleted>(response) {
|
|
Ok(completed) => self.push_completed(completed),
|
|
Err(err) => self.push_failed(format!(
|
|
"failed to parse response.completed event: {err}"
|
|
)),
|
|
}
|
|
}
|
|
self.completed = true;
|
|
true
|
|
}
|
|
"response.failed" | "response.incomplete" => {
|
|
self.push_failed(response_error_message(event.response.as_ref()));
|
|
true
|
|
}
|
|
_ => false,
|
|
};
|
|
EpiphanyResponsesFrameObservation {
|
|
frame_sequence,
|
|
kind,
|
|
recognized,
|
|
delta_preview,
|
|
}
|
|
}
|
|
|
|
fn push_completed(&mut self, completed: EpiphanyResponseCompleted) {
|
|
let mut receipt = EpiphanyOpenAiModelReceipt::new(&self.request_id, &self.requested_model);
|
|
receipt.response_id = Some(completed.id);
|
|
receipt.transport = Some("epiphany_direct_responses_http".to_string());
|
|
if let Some(usage) = completed.usage {
|
|
receipt.input_tokens = nonnegative_i64_to_u64(usage.input_tokens);
|
|
receipt.output_tokens = nonnegative_i64_to_u64(usage.output_tokens);
|
|
receipt.reasoning_output_tokens = usage
|
|
.output_tokens_details
|
|
.and_then(|details| nonnegative_i64_to_u64(details.reasoning_tokens));
|
|
}
|
|
self.push_payload(EpiphanyOpenAiStreamPayload::Completed { receipt });
|
|
}
|
|
|
|
fn seed_tool_call_from_item(&mut self, item: &Value) {
|
|
let Some(kind) = item_type(item) else {
|
|
return;
|
|
};
|
|
if kind != "function_call" && kind != "custom_tool_call" {
|
|
return;
|
|
}
|
|
let Some(item_id) = item_identity(item) else {
|
|
return;
|
|
};
|
|
let pending = self.pending_tool_calls.entry(item_id.clone()).or_default();
|
|
if pending.call_id.is_none() {
|
|
pending.call_id = item_call_id(item).or(Some(item_id));
|
|
}
|
|
if pending.name.is_none() {
|
|
pending.name = item_name(item);
|
|
}
|
|
if pending.arguments.is_empty()
|
|
&& let Some(arguments) = item_arguments(item)
|
|
{
|
|
pending.arguments = arguments;
|
|
}
|
|
}
|
|
|
|
fn push_tool_call_from_done_item(&mut self, item: &Value) {
|
|
let Some(kind) = item_type(item) else {
|
|
return;
|
|
};
|
|
if kind != "function_call" && kind != "custom_tool_call" {
|
|
return;
|
|
}
|
|
let item_id = item_identity(item);
|
|
let pending = item_id
|
|
.as_ref()
|
|
.and_then(|id| self.pending_tool_calls.remove(id))
|
|
.unwrap_or_default();
|
|
let Some(name) = item_name(item).or(pending.name) else {
|
|
return;
|
|
};
|
|
let Some(call_id) = item_call_id(item).or(pending.call_id).or(item_id) else {
|
|
return;
|
|
};
|
|
let arguments = item_arguments(item).unwrap_or(pending.arguments);
|
|
self.push_payload(EpiphanyOpenAiStreamPayload::ToolCall {
|
|
call_id,
|
|
name,
|
|
arguments,
|
|
});
|
|
}
|
|
|
|
fn push_failed(&mut self, message: String) {
|
|
self.push_payload(EpiphanyOpenAiStreamPayload::Failed { message });
|
|
self.completed = true;
|
|
}
|
|
|
|
fn push_payload(&mut self, payload: EpiphanyOpenAiStreamPayload) {
|
|
self.events.push(EpiphanyOpenAiStreamEvent {
|
|
schema_id: epiphany_openai_adapter::OPENAI_ADAPTER_EVENT_SCHEMA_ID.to_string(),
|
|
request_id: self.request_id.clone(),
|
|
sequence: self.sequence,
|
|
payload,
|
|
});
|
|
self.sequence += 1;
|
|
}
|
|
}
|
|
|
|
fn delta_preview(delta: &str) -> String {
|
|
const MAX_PREVIEW_CHARS: usize = 120;
|
|
let mut preview = delta.chars().take(MAX_PREVIEW_CHARS).collect::<String>();
|
|
if delta.chars().count() > MAX_PREVIEW_CHARS {
|
|
preview.push_str("...");
|
|
}
|
|
preview.replace(['\r', '\n', '\t'], " ")
|
|
}
|
|
|
|
fn item_type(item: &Value) -> Option<&str> {
|
|
item.get("type").and_then(Value::as_str)
|
|
}
|
|
|
|
fn item_identity(item: &Value) -> Option<String> {
|
|
item.get("id")
|
|
.and_then(Value::as_str)
|
|
.or_else(|| item.get("item_id").and_then(Value::as_str))
|
|
.or_else(|| item.get("call_id").and_then(Value::as_str))
|
|
.map(ToString::to_string)
|
|
}
|
|
|
|
fn item_call_id(item: &Value) -> Option<String> {
|
|
item.get("call_id")
|
|
.and_then(Value::as_str)
|
|
.map(ToString::to_string)
|
|
}
|
|
|
|
fn item_name(item: &Value) -> Option<String> {
|
|
item.get("name")
|
|
.and_then(Value::as_str)
|
|
.map(ToString::to_string)
|
|
}
|
|
|
|
fn item_arguments(item: &Value) -> Option<String> {
|
|
item.get("arguments")
|
|
.or_else(|| item.get("input"))
|
|
.and_then(Value::as_str)
|
|
.map(ToString::to_string)
|
|
}
|
|
|
|
fn parse_reasoning_effort(value: Option<&str>) -> Result<Option<String>> {
|
|
match value {
|
|
None => Ok(None),
|
|
Some("none" | "minimal" | "low" | "medium" | "high" | "xhigh") => {
|
|
Ok(value.map(ToString::to_string))
|
|
}
|
|
Some(other) => anyhow::bail!("invalid reasoning_effort: {other}"),
|
|
}
|
|
}
|
|
|
|
fn parse_reasoning_summary(value: Option<&str>) -> Result<Option<String>> {
|
|
match value {
|
|
None => Ok(None),
|
|
Some("auto" | "concise" | "detailed" | "none") => Ok(value.map(ToString::to_string)),
|
|
Some(other) => anyhow::bail!("invalid reasoning_summary: {other}"),
|
|
}
|
|
}
|
|
|
|
fn parse_service_tier(value: Option<&str>) -> Result<Option<String>> {
|
|
match value {
|
|
None => Ok(None),
|
|
Some("fast" | "flex") => Ok(value.map(ToString::to_string)),
|
|
Some(other) => anyhow::bail!("invalid service_tier: {other}"),
|
|
}
|
|
}
|
|
|
|
fn nonnegative_i64_to_u64(value: i64) -> Option<u64> {
|
|
u64::try_from(value).ok()
|
|
}
|
|
|
|
fn default_base_url_for_auth(auth: &CodexAuth) -> &'static str {
|
|
if auth.uses_codex_backend() {
|
|
CHATGPT_CODEX_BASE_URL
|
|
} else {
|
|
OPENAI_API_BASE_URL
|
|
}
|
|
}
|
|
|
|
fn attach_codex_auth_headers(
|
|
auth: &CodexAuth,
|
|
headers: &mut http::HeaderMap,
|
|
) -> std::io::Result<()> {
|
|
let token = auth.get_token()?;
|
|
let value = format!("Bearer {token}");
|
|
if let Ok(value) = value.parse() {
|
|
headers.insert(http::header::AUTHORIZATION, value);
|
|
}
|
|
if let Some(account_id) = auth.get_account_id()
|
|
&& let Ok(value) = account_id.parse()
|
|
{
|
|
headers.insert("ChatGPT-Account-ID", value);
|
|
}
|
|
if auth.is_fedramp_account() {
|
|
headers.insert("X-OpenAI-Fedramp", "true".parse().expect("valid header"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn attach_optional_env_header(headers: &mut http::HeaderMap, name: &'static str, env_key: &str) {
|
|
let Ok(value) = std::env::var(env_key) else {
|
|
return;
|
|
};
|
|
let value = value.trim();
|
|
if value.is_empty() {
|
|
return;
|
|
}
|
|
if let Ok(value) = value.parse() {
|
|
headers.insert(name, value);
|
|
}
|
|
}
|
|
|
|
fn response_error_message(response: Option<&Value>) -> String {
|
|
response
|
|
.and_then(|response| response.get("error"))
|
|
.and_then(|error| error.get("message"))
|
|
.and_then(Value::as_str)
|
|
.or_else(|| {
|
|
response
|
|
.and_then(|response| response.get("incomplete_details"))
|
|
.and_then(|details| details.get("reason"))
|
|
.and_then(Value::as_str)
|
|
})
|
|
.unwrap_or("Responses stream failed")
|
|
.to_string()
|
|
}
|
|
|
|
fn transport_error_to_anyhow(err: TransportError) -> anyhow::Error {
|
|
anyhow::anyhow!("failed to open direct Responses stream: {err}")
|
|
}
|
|
|
|
fn auth_mode_from_manager(
|
|
auth_manager: &AuthManager,
|
|
auth: Option<&CodexAuth>,
|
|
) -> EpiphanyOpenAiAuthMode {
|
|
match auth_manager.auth_mode() {
|
|
Some(AuthMode::ApiKey) => EpiphanyOpenAiAuthMode::ApiKey,
|
|
Some(AuthMode::Chatgpt) | Some(AuthMode::ChatgptAuthTokens) => {
|
|
EpiphanyOpenAiAuthMode::ChatGptSubscription
|
|
}
|
|
Some(AuthMode::AgentIdentity) => EpiphanyOpenAiAuthMode::ExternalBearer,
|
|
None => auth_mode_from_codex_auth(auth),
|
|
}
|
|
}
|
|
|
|
fn auth_mode_from_codex_auth(auth: Option<&CodexAuth>) -> EpiphanyOpenAiAuthMode {
|
|
let Some(auth) = auth else {
|
|
return EpiphanyOpenAiAuthMode::Unknown;
|
|
};
|
|
if auth.is_api_key_auth() {
|
|
EpiphanyOpenAiAuthMode::ApiKey
|
|
} else if auth.is_chatgpt_auth() {
|
|
EpiphanyOpenAiAuthMode::ChatGptSubscription
|
|
} else {
|
|
EpiphanyOpenAiAuthMode::ExternalBearer
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn api_key_auth_maps_to_typed_adapter_status() {
|
|
let auth = CodexAuth::from_api_key("test-key");
|
|
let status = status_from_codex_auth(Some(&auth), Some("gpt-5.4".to_string()), true);
|
|
|
|
assert_eq!(status.adapter_id, CODEX_SPINE_ADAPTER_ID);
|
|
assert_eq!(status.auth_mode, EpiphanyOpenAiAuthMode::ApiKey);
|
|
assert_eq!(status.default_model.as_deref(), Some("gpt-5.4"));
|
|
assert!(status.supports_websockets);
|
|
assert!(status.codex_transport_attached);
|
|
}
|
|
|
|
#[test]
|
|
fn missing_auth_maps_to_unknown_status() {
|
|
let status = status_from_codex_auth(None, None, false);
|
|
|
|
assert_eq!(status.auth_mode, EpiphanyOpenAiAuthMode::Unknown);
|
|
assert_eq!(status.account_id, None);
|
|
assert!(!status.supports_websockets);
|
|
}
|
|
|
|
#[test]
|
|
fn maps_typed_request_to_responses_body_without_codex_protocol_payload() {
|
|
let mut request = EpiphanyOpenAiModelRequest::new(
|
|
"req-1",
|
|
"conversation-1",
|
|
"gpt-5.4",
|
|
"Answer plainly.",
|
|
);
|
|
request.input.push(EpiphanyOpenAiInputItem::UserText {
|
|
text: "hello".to_string(),
|
|
});
|
|
request.input.push(EpiphanyOpenAiInputItem::ToolCall {
|
|
call_id: "call-1".to_string(),
|
|
name: "mcp__epiphany_source__read_file".to_string(),
|
|
arguments: "{\"path\":\"README.md\"}".to_string(),
|
|
});
|
|
request.input.push(EpiphanyOpenAiInputItem::ToolResult {
|
|
call_id: "call-1".to_string(),
|
|
output: "{\"ok\":true}".to_string(),
|
|
});
|
|
request.reasoning_effort = Some("low".to_string());
|
|
request.reasoning_summary = Some("concise".to_string());
|
|
request.service_tier = Some("flex".to_string());
|
|
request.previous_response_id = Some("resp-1".to_string());
|
|
request
|
|
.tools
|
|
.push(epiphany_openai_adapter::EpiphanyOpenAiToolDefinition {
|
|
name: "mcp__epiphany_source__read_file".to_string(),
|
|
description: "Read a bounded file slice.".to_string(),
|
|
parameters_json: serde_json::json!({
|
|
"type": "object",
|
|
"properties": {"path": {"type": "string"}},
|
|
"required": ["path"]
|
|
})
|
|
.to_string(),
|
|
});
|
|
|
|
let responses = responses_body_from_epiphany(request).expect("request should map");
|
|
|
|
assert_eq!(responses["model"], "gpt-5.4");
|
|
assert_eq!(responses["instructions"], "Answer plainly.");
|
|
assert_eq!(responses["previous_response_id"], "resp-1");
|
|
assert_eq!(responses["input"][0]["content"][0]["type"], "input_text");
|
|
assert_eq!(responses["input"][1]["type"], "function_call");
|
|
assert_eq!(
|
|
responses["input"][1]["name"],
|
|
"mcp__epiphany_source__read_file"
|
|
);
|
|
assert_eq!(responses["input"][2]["type"], "function_call_output");
|
|
assert_eq!(responses["stream"], true);
|
|
assert_eq!(responses["store"], false);
|
|
assert_eq!(responses["service_tier"], "flex");
|
|
assert_eq!(responses["tools"].as_array().expect("tools").len(), 1);
|
|
assert_eq!(
|
|
responses["tools"][0]["name"],
|
|
"mcp__epiphany_source__read_file"
|
|
);
|
|
assert_eq!(responses["tool_choice"], "auto");
|
|
}
|
|
|
|
#[test]
|
|
fn tool_bearing_initial_request_requires_one_tool_call() {
|
|
let mut request = EpiphanyOpenAiModelRequest::new(
|
|
"req-1",
|
|
"conversation-1",
|
|
"gpt-5.4",
|
|
"Inspect before answering.",
|
|
);
|
|
request.input.push(EpiphanyOpenAiInputItem::UserText {
|
|
text: "verify".to_string(),
|
|
});
|
|
request
|
|
.tools
|
|
.push(epiphany_openai_adapter::EpiphanyOpenAiToolDefinition {
|
|
name: "mcp__epiphany_source__read_file".to_string(),
|
|
description: "Read a bounded file slice.".to_string(),
|
|
parameters_json: serde_json::json!({
|
|
"type": "object",
|
|
"properties": {"path": {"type": "string"}},
|
|
"required": ["path"]
|
|
})
|
|
.to_string(),
|
|
});
|
|
|
|
let responses = responses_body_from_epiphany(request).expect("request should map");
|
|
|
|
assert_eq!(responses["tool_choice"], "required");
|
|
}
|
|
|
|
#[test]
|
|
fn declared_output_schema_reaches_responses_text_format() {
|
|
let mut request = EpiphanyOpenAiModelRequest::new(
|
|
"req-schema",
|
|
"conversation-schema",
|
|
"gpt-5.4",
|
|
"Return the typed result.",
|
|
);
|
|
request.output_contract_id = Some("epiphany.role_worker_output.v3".to_string());
|
|
request.output_schema_json = Some(
|
|
serde_json::json!({
|
|
"type": "object",
|
|
"additionalProperties": false,
|
|
"properties": {"summary": {"type": "string"}},
|
|
"required": ["summary"]
|
|
})
|
|
.to_string(),
|
|
);
|
|
|
|
let responses = responses_body_from_epiphany(request).expect("request should map");
|
|
|
|
assert_eq!(responses["text"]["format"]["type"], "json_schema");
|
|
assert_eq!(
|
|
responses["text"]["format"]["name"],
|
|
"epiphany_role_worker_output_v3"
|
|
);
|
|
assert_eq!(responses["text"]["format"]["strict"], true);
|
|
assert_eq!(
|
|
responses["text"]["format"]["schema"]["required"][0],
|
|
"summary"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn provider_schema_projection_drops_conditional_authority_only() {
|
|
let mut request = EpiphanyOpenAiModelRequest::new(
|
|
"req-conditional",
|
|
"conversation-conditional",
|
|
"gpt-5.4",
|
|
"Return the typed result.",
|
|
);
|
|
request.output_contract_id = Some("epiphany.worker".to_string());
|
|
request.output_schema_json = Some(
|
|
serde_json::json!({
|
|
"type": "object",
|
|
"properties": {
|
|
"purpose": {
|
|
"oneOf": [
|
|
{"type": "string", "const": "evolution"},
|
|
{"type": "string", "const": "repair"}
|
|
]
|
|
}
|
|
},
|
|
"allOf": [{"if": {"properties": {"purpose": {"const": "repair"}}}, "then": {"required": ["receipt"]}}]
|
|
})
|
|
.to_string(),
|
|
);
|
|
|
|
let responses = responses_body_from_epiphany(request).expect("request should map");
|
|
let schema = &responses["text"]["format"]["schema"];
|
|
assert_eq!(responses["text"]["format"]["strict"], false);
|
|
assert!(schema.get("allOf").is_none());
|
|
assert!(schema["properties"]["purpose"].get("oneOf").is_none());
|
|
assert!(schema["properties"]["purpose"]["anyOf"].is_array());
|
|
}
|
|
|
|
#[test]
|
|
fn parses_function_call_output_item_done_as_complete_tool_call() {
|
|
let mut state = EpiphanyResponsesStreamState::new("req-tools", "gpt-5.4");
|
|
let observation = state.push_sse_frame(
|
|
&serde_json::json!({
|
|
"type": "response.output_item.done",
|
|
"item": {
|
|
"id": "fc_1",
|
|
"type": "function_call",
|
|
"name": "mcp__epiphany_source__read_file",
|
|
"arguments": "{\"path\":\"README.md\"}",
|
|
"call_id": "call_1"
|
|
}
|
|
})
|
|
.to_string(),
|
|
);
|
|
|
|
assert!(observation.recognized);
|
|
assert_eq!(state.events.len(), 1);
|
|
assert_eq!(
|
|
state.events[0].payload,
|
|
EpiphanyOpenAiStreamPayload::ToolCall {
|
|
call_id: "call_1".to_string(),
|
|
name: "mcp__epiphany_source__read_file".to_string(),
|
|
arguments: "{\"path\":\"README.md\"}".to_string(),
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn assembles_function_call_argument_deltas_until_output_item_done() {
|
|
let mut state = EpiphanyResponsesStreamState::new("req-tools", "gpt-5.4");
|
|
state.push_sse_frame(
|
|
&serde_json::json!({
|
|
"type": "response.output_item.added",
|
|
"item": {
|
|
"id": "fc_1",
|
|
"type": "function_call",
|
|
"name": "mcp__epiphany_source__git_show",
|
|
"call_id": "call_1",
|
|
"arguments": ""
|
|
}
|
|
})
|
|
.to_string(),
|
|
);
|
|
state.push_sse_frame(
|
|
&serde_json::json!({
|
|
"type": "response.function_call_arguments.delta",
|
|
"item_id": "fc_1",
|
|
"call_id": "call_1",
|
|
"delta": "{\"revision\":"
|
|
})
|
|
.to_string(),
|
|
);
|
|
state.push_sse_frame(
|
|
&serde_json::json!({
|
|
"type": "response.function_call_arguments.delta",
|
|
"item_id": "fc_1",
|
|
"call_id": "call_1",
|
|
"delta": "\"HEAD\"}"
|
|
})
|
|
.to_string(),
|
|
);
|
|
state.push_sse_frame(
|
|
&serde_json::json!({
|
|
"type": "response.output_item.done",
|
|
"item": {
|
|
"id": "fc_1",
|
|
"type": "function_call",
|
|
"name": "mcp__epiphany_source__git_show",
|
|
"call_id": "call_1"
|
|
}
|
|
})
|
|
.to_string(),
|
|
);
|
|
|
|
assert_eq!(state.events.len(), 1);
|
|
assert_eq!(
|
|
state.events[0].payload,
|
|
EpiphanyOpenAiStreamPayload::ToolCall {
|
|
call_id: "call_1".to_string(),
|
|
name: "mcp__epiphany_source__git_show".to_string(),
|
|
arguments: "{\"revision\":\"HEAD\"}".to_string(),
|
|
}
|
|
);
|
|
}
|
|
}
|