mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
analytics: add responses api call schema
This commit is contained in:
@@ -9,6 +9,8 @@ use crate::facts::TurnStatus;
|
|||||||
use crate::facts::TurnSteerRejectionReason;
|
use crate::facts::TurnSteerRejectionReason;
|
||||||
use crate::facts::TurnSteerResult;
|
use crate::facts::TurnSteerResult;
|
||||||
use crate::facts::TurnSubmissionType;
|
use crate::facts::TurnSubmissionType;
|
||||||
|
use crate::responses_api::CodexResponsesApiCallStatus;
|
||||||
|
use crate::responses_api::CodexResponsesApiItemMetadata;
|
||||||
use codex_app_server_protocol::CodexErrorInfo;
|
use codex_app_server_protocol::CodexErrorInfo;
|
||||||
use codex_login::default_client::originator;
|
use codex_login::default_client::originator;
|
||||||
use codex_plugin::PluginTelemetryMetadata;
|
use codex_plugin::PluginTelemetryMetadata;
|
||||||
@@ -40,6 +42,7 @@ pub(crate) enum TrackEventRequest {
|
|||||||
AppMentioned(CodexAppMentionedEventRequest),
|
AppMentioned(CodexAppMentionedEventRequest),
|
||||||
AppUsed(CodexAppUsedEventRequest),
|
AppUsed(CodexAppUsedEventRequest),
|
||||||
Compaction(Box<CodexCompactionEventRequest>),
|
Compaction(Box<CodexCompactionEventRequest>),
|
||||||
|
ResponsesApiCall(Box<CodexResponsesApiCallEventRequest>),
|
||||||
TurnEvent(Box<CodexTurnEventRequest>),
|
TurnEvent(Box<CodexTurnEventRequest>),
|
||||||
TurnSteer(CodexTurnSteerEventRequest),
|
TurnSteer(CodexTurnSteerEventRequest),
|
||||||
PluginUsed(CodexPluginUsedEventRequest),
|
PluginUsed(CodexPluginUsedEventRequest),
|
||||||
@@ -329,6 +332,43 @@ pub(crate) struct CodexCompactionEventRequest {
|
|||||||
pub(crate) event_params: CodexCompactionEventParams,
|
pub(crate) event_params: CodexCompactionEventParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(crate) struct CodexResponsesApiCallEventParams {
|
||||||
|
pub(crate) thread_id: String,
|
||||||
|
pub(crate) turn_id: String,
|
||||||
|
pub(crate) ephemeral: bool,
|
||||||
|
pub(crate) thread_source: Option<String>,
|
||||||
|
pub(crate) initialization_mode: ThreadInitializationMode,
|
||||||
|
pub(crate) subagent_source: Option<String>,
|
||||||
|
pub(crate) parent_thread_id: Option<String>,
|
||||||
|
pub(crate) app_server_client: CodexAppServerClientMetadata,
|
||||||
|
pub(crate) runtime: CodexRuntimeMetadata,
|
||||||
|
pub(crate) responses_id: Option<String>,
|
||||||
|
pub(crate) turn_responses_call_index: u64,
|
||||||
|
pub(crate) model: Option<String>,
|
||||||
|
pub(crate) model_provider: Option<String>,
|
||||||
|
pub(crate) reasoning_effort: Option<String>,
|
||||||
|
pub(crate) status: CodexResponsesApiCallStatus,
|
||||||
|
pub(crate) error: Option<String>,
|
||||||
|
pub(crate) started_at: u64,
|
||||||
|
pub(crate) completed_at: Option<u64>,
|
||||||
|
pub(crate) duration_ms: Option<u64>,
|
||||||
|
pub(crate) input_item_count: usize,
|
||||||
|
pub(crate) output_item_count: usize,
|
||||||
|
pub(crate) input_tokens: Option<i64>,
|
||||||
|
pub(crate) cached_input_tokens: Option<i64>,
|
||||||
|
pub(crate) output_tokens: Option<i64>,
|
||||||
|
pub(crate) reasoning_output_tokens: Option<i64>,
|
||||||
|
pub(crate) total_tokens: Option<i64>,
|
||||||
|
pub(crate) items: Vec<CodexResponsesApiItemMetadata>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(crate) struct CodexResponsesApiCallEventRequest {
|
||||||
|
pub(crate) event_type: &'static str,
|
||||||
|
pub(crate) event_params: CodexResponsesApiCallEventParams,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub(crate) struct CodexTurnEventParams {
|
pub(crate) struct CodexTurnEventParams {
|
||||||
pub(crate) thread_id: String,
|
pub(crate) thread_id: String,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ mod client;
|
|||||||
mod events;
|
mod events;
|
||||||
mod facts;
|
mod facts;
|
||||||
mod reducer;
|
mod reducer;
|
||||||
|
mod responses_api;
|
||||||
|
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use std::time::UNIX_EPOCH;
|
use std::time::UNIX_EPOCH;
|
||||||
@@ -42,6 +43,14 @@ pub use facts::TurnSteerRequestError;
|
|||||||
pub use facts::TurnSteerResult;
|
pub use facts::TurnSteerResult;
|
||||||
pub use facts::TurnTokenUsageFact;
|
pub use facts::TurnTokenUsageFact;
|
||||||
pub use facts::build_track_events_context;
|
pub use facts::build_track_events_context;
|
||||||
|
pub use responses_api::CodexResponseItemRole;
|
||||||
|
pub use responses_api::CodexResponseItemType;
|
||||||
|
pub use responses_api::CodexResponseMessagePhase;
|
||||||
|
pub use responses_api::CodexResponsesApiCallFact;
|
||||||
|
pub use responses_api::CodexResponsesApiCallStatus;
|
||||||
|
pub use responses_api::CodexResponsesApiItemMetadata;
|
||||||
|
pub use responses_api::CodexResponsesApiItemPhase;
|
||||||
|
pub use responses_api::response_items_metadata;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod analytics_client_tests;
|
mod analytics_client_tests;
|
||||||
|
|||||||
@@ -0,0 +1,486 @@
|
|||||||
|
use codex_protocol::models::ContentItem;
|
||||||
|
use codex_protocol::models::FunctionCallOutputBody;
|
||||||
|
use codex_protocol::models::FunctionCallOutputContentItem;
|
||||||
|
use codex_protocol::models::FunctionCallOutputPayload;
|
||||||
|
use codex_protocol::models::MessagePhase;
|
||||||
|
use codex_protocol::models::ReasoningItemContent;
|
||||||
|
use codex_protocol::models::ReasoningItemReasoningSummary;
|
||||||
|
use codex_protocol::models::ResponseItem;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum CodexResponsesApiCallStatus {
|
||||||
|
Completed,
|
||||||
|
Failed,
|
||||||
|
Interrupted,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum CodexResponsesApiItemPhase {
|
||||||
|
Input,
|
||||||
|
Output,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum CodexResponseItemType {
|
||||||
|
Message,
|
||||||
|
Reasoning,
|
||||||
|
LocalShellCall,
|
||||||
|
FunctionCall,
|
||||||
|
FunctionCallOutput,
|
||||||
|
CustomToolCall,
|
||||||
|
CustomToolCallOutput,
|
||||||
|
ToolSearchCall,
|
||||||
|
ToolSearchOutput,
|
||||||
|
WebSearchCall,
|
||||||
|
ImageGenerationCall,
|
||||||
|
GhostSnapshot,
|
||||||
|
Compaction,
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum CodexResponseItemRole {
|
||||||
|
User,
|
||||||
|
Assistant,
|
||||||
|
System,
|
||||||
|
Developer,
|
||||||
|
Tool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Eq)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum CodexResponseMessagePhase {
|
||||||
|
Commentary,
|
||||||
|
FinalAnswer,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
|
||||||
|
pub struct CodexResponsesApiItemMetadata {
|
||||||
|
pub item_phase: CodexResponsesApiItemPhase,
|
||||||
|
pub item_index: usize,
|
||||||
|
pub response_item_type: CodexResponseItemType,
|
||||||
|
pub role: Option<CodexResponseItemRole>,
|
||||||
|
pub status: Option<String>,
|
||||||
|
pub message_phase: Option<CodexResponseMessagePhase>,
|
||||||
|
pub call_id: Option<String>,
|
||||||
|
pub tool_name: Option<String>,
|
||||||
|
pub content_bytes: Option<i64>,
|
||||||
|
pub arguments_bytes: Option<i64>,
|
||||||
|
pub output_bytes: Option<i64>,
|
||||||
|
pub encrypted_content_bytes: Option<i64>,
|
||||||
|
pub image_generation_revised_prompt_bytes: Option<i64>,
|
||||||
|
pub image_generation_result_bytes: Option<i64>,
|
||||||
|
pub text_part_count: Option<usize>,
|
||||||
|
pub image_part_count: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct CodexResponsesApiCallFact {
|
||||||
|
pub thread_id: String,
|
||||||
|
pub turn_id: String,
|
||||||
|
pub responses_id: Option<String>,
|
||||||
|
pub turn_responses_call_index: u64,
|
||||||
|
pub status: CodexResponsesApiCallStatus,
|
||||||
|
pub error: Option<String>,
|
||||||
|
pub started_at: u64,
|
||||||
|
pub completed_at: Option<u64>,
|
||||||
|
pub duration_ms: Option<u64>,
|
||||||
|
pub input_item_count: usize,
|
||||||
|
pub output_item_count: usize,
|
||||||
|
pub input_tokens: Option<i64>,
|
||||||
|
pub cached_input_tokens: Option<i64>,
|
||||||
|
pub output_tokens: Option<i64>,
|
||||||
|
pub reasoning_output_tokens: Option<i64>,
|
||||||
|
pub total_tokens: Option<i64>,
|
||||||
|
pub items: Vec<CodexResponsesApiItemMetadata>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn response_items_metadata(
|
||||||
|
phase: CodexResponsesApiItemPhase,
|
||||||
|
items: &[ResponseItem],
|
||||||
|
) -> Vec<CodexResponsesApiItemMetadata> {
|
||||||
|
items
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(item_index, item)| response_item_metadata(phase, item_index, item))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn response_item_metadata(
|
||||||
|
item_phase: CodexResponsesApiItemPhase,
|
||||||
|
item_index: usize,
|
||||||
|
item: &ResponseItem,
|
||||||
|
) -> CodexResponsesApiItemMetadata {
|
||||||
|
let mut metadata =
|
||||||
|
CodexResponsesApiItemMetadata::new(item_phase, item_index, response_item_type(item));
|
||||||
|
|
||||||
|
match item {
|
||||||
|
ResponseItem::Message {
|
||||||
|
role,
|
||||||
|
content,
|
||||||
|
phase,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.role = response_item_role(role);
|
||||||
|
metadata.message_phase = phase.as_ref().and_then(response_message_phase);
|
||||||
|
metadata.content_bytes = nonzero_i64(message_content_text_bytes(content));
|
||||||
|
let (text_part_count, image_part_count) = message_content_part_counts(content);
|
||||||
|
metadata.text_part_count = Some(text_part_count);
|
||||||
|
metadata.image_part_count = Some(image_part_count);
|
||||||
|
}
|
||||||
|
ResponseItem::Reasoning {
|
||||||
|
summary,
|
||||||
|
content,
|
||||||
|
encrypted_content,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.content_bytes = nonzero_i64(reasoning_content_bytes(summary, content));
|
||||||
|
metadata.encrypted_content_bytes =
|
||||||
|
encrypted_content.as_ref().map(|value| byte_len(value));
|
||||||
|
metadata.text_part_count = Some(
|
||||||
|
summary.len()
|
||||||
|
+ content
|
||||||
|
.as_ref()
|
||||||
|
.map(|content| content.len())
|
||||||
|
.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
metadata.image_part_count = Some(0);
|
||||||
|
}
|
||||||
|
ResponseItem::LocalShellCall {
|
||||||
|
call_id,
|
||||||
|
status,
|
||||||
|
action,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.call_id = call_id.clone();
|
||||||
|
metadata.tool_name = Some("local_shell".to_string());
|
||||||
|
metadata.status = serialized_string(status);
|
||||||
|
metadata.arguments_bytes = serialized_bytes(action);
|
||||||
|
}
|
||||||
|
ResponseItem::FunctionCall {
|
||||||
|
name,
|
||||||
|
arguments,
|
||||||
|
call_id,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.call_id = Some(call_id.clone());
|
||||||
|
metadata.tool_name = Some(name.clone());
|
||||||
|
metadata.arguments_bytes = Some(byte_len(arguments));
|
||||||
|
}
|
||||||
|
ResponseItem::ToolSearchCall {
|
||||||
|
call_id,
|
||||||
|
status,
|
||||||
|
arguments,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.call_id = call_id.clone();
|
||||||
|
metadata.tool_name = Some("tool_search".to_string());
|
||||||
|
metadata.status = status.clone();
|
||||||
|
metadata.arguments_bytes = serialized_bytes(arguments);
|
||||||
|
}
|
||||||
|
ResponseItem::FunctionCallOutput { call_id, output } => {
|
||||||
|
metadata.call_id = Some(call_id.clone());
|
||||||
|
metadata.output_bytes = function_call_output_bytes(output);
|
||||||
|
let (text_part_count, image_part_count) = function_call_output_part_counts(output);
|
||||||
|
metadata.text_part_count = text_part_count;
|
||||||
|
metadata.image_part_count = image_part_count;
|
||||||
|
}
|
||||||
|
ResponseItem::CustomToolCall {
|
||||||
|
status,
|
||||||
|
call_id,
|
||||||
|
name,
|
||||||
|
input,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.call_id = Some(call_id.clone());
|
||||||
|
metadata.tool_name = Some(name.clone());
|
||||||
|
metadata.status = status.clone();
|
||||||
|
metadata.arguments_bytes = Some(byte_len(input));
|
||||||
|
}
|
||||||
|
ResponseItem::CustomToolCallOutput {
|
||||||
|
call_id,
|
||||||
|
name,
|
||||||
|
output,
|
||||||
|
} => {
|
||||||
|
metadata.call_id = Some(call_id.clone());
|
||||||
|
metadata.tool_name = name.clone();
|
||||||
|
metadata.output_bytes = function_call_output_bytes(output);
|
||||||
|
let (text_part_count, image_part_count) = function_call_output_part_counts(output);
|
||||||
|
metadata.text_part_count = text_part_count;
|
||||||
|
metadata.image_part_count = image_part_count;
|
||||||
|
}
|
||||||
|
ResponseItem::ToolSearchOutput {
|
||||||
|
call_id,
|
||||||
|
status,
|
||||||
|
tools,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
metadata.call_id = call_id.clone();
|
||||||
|
metadata.tool_name = Some("tool_search".to_string());
|
||||||
|
metadata.status = Some(status.clone());
|
||||||
|
metadata.output_bytes = serialized_bytes(tools);
|
||||||
|
}
|
||||||
|
ResponseItem::WebSearchCall { status, action, .. } => {
|
||||||
|
metadata.tool_name = Some("web_search".to_string());
|
||||||
|
metadata.status = status.clone();
|
||||||
|
metadata.arguments_bytes = action.as_ref().and_then(serialized_bytes);
|
||||||
|
}
|
||||||
|
ResponseItem::ImageGenerationCall {
|
||||||
|
id,
|
||||||
|
status,
|
||||||
|
revised_prompt,
|
||||||
|
result,
|
||||||
|
} => {
|
||||||
|
metadata.call_id = Some(id.clone());
|
||||||
|
metadata.tool_name = Some("image_generation".to_string());
|
||||||
|
metadata.status = Some(status.clone());
|
||||||
|
metadata.image_generation_revised_prompt_bytes =
|
||||||
|
revised_prompt.as_ref().map(|value| byte_len(value));
|
||||||
|
metadata.image_generation_result_bytes = Some(byte_len(result));
|
||||||
|
}
|
||||||
|
ResponseItem::Compaction { encrypted_content } => {
|
||||||
|
metadata.encrypted_content_bytes = Some(byte_len(encrypted_content));
|
||||||
|
}
|
||||||
|
ResponseItem::GhostSnapshot { .. } | ResponseItem::Other => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodexResponsesApiItemMetadata {
|
||||||
|
fn new(
|
||||||
|
item_phase: CodexResponsesApiItemPhase,
|
||||||
|
item_index: usize,
|
||||||
|
response_item_type: CodexResponseItemType,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
item_phase,
|
||||||
|
item_index,
|
||||||
|
response_item_type,
|
||||||
|
role: None,
|
||||||
|
status: None,
|
||||||
|
message_phase: None,
|
||||||
|
call_id: None,
|
||||||
|
tool_name: None,
|
||||||
|
content_bytes: None,
|
||||||
|
arguments_bytes: None,
|
||||||
|
output_bytes: None,
|
||||||
|
encrypted_content_bytes: None,
|
||||||
|
image_generation_revised_prompt_bytes: None,
|
||||||
|
image_generation_result_bytes: None,
|
||||||
|
text_part_count: None,
|
||||||
|
image_part_count: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn response_item_type(item: &ResponseItem) -> CodexResponseItemType {
|
||||||
|
match item {
|
||||||
|
ResponseItem::Message { .. } => CodexResponseItemType::Message,
|
||||||
|
ResponseItem::Reasoning { .. } => CodexResponseItemType::Reasoning,
|
||||||
|
ResponseItem::LocalShellCall { .. } => CodexResponseItemType::LocalShellCall,
|
||||||
|
ResponseItem::FunctionCall { .. } => CodexResponseItemType::FunctionCall,
|
||||||
|
ResponseItem::ToolSearchCall { .. } => CodexResponseItemType::ToolSearchCall,
|
||||||
|
ResponseItem::FunctionCallOutput { .. } => CodexResponseItemType::FunctionCallOutput,
|
||||||
|
ResponseItem::CustomToolCall { .. } => CodexResponseItemType::CustomToolCall,
|
||||||
|
ResponseItem::CustomToolCallOutput { .. } => CodexResponseItemType::CustomToolCallOutput,
|
||||||
|
ResponseItem::ToolSearchOutput { .. } => CodexResponseItemType::ToolSearchOutput,
|
||||||
|
ResponseItem::WebSearchCall { .. } => CodexResponseItemType::WebSearchCall,
|
||||||
|
ResponseItem::ImageGenerationCall { .. } => CodexResponseItemType::ImageGenerationCall,
|
||||||
|
ResponseItem::GhostSnapshot { .. } => CodexResponseItemType::GhostSnapshot,
|
||||||
|
ResponseItem::Compaction { .. } => CodexResponseItemType::Compaction,
|
||||||
|
ResponseItem::Other => CodexResponseItemType::Other,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn response_item_role(role: &str) -> Option<CodexResponseItemRole> {
|
||||||
|
match role {
|
||||||
|
"user" => Some(CodexResponseItemRole::User),
|
||||||
|
"assistant" => Some(CodexResponseItemRole::Assistant),
|
||||||
|
"system" => Some(CodexResponseItemRole::System),
|
||||||
|
"developer" => Some(CodexResponseItemRole::Developer),
|
||||||
|
"tool" => Some(CodexResponseItemRole::Tool),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn response_message_phase(phase: &MessagePhase) -> Option<CodexResponseMessagePhase> {
|
||||||
|
match phase {
|
||||||
|
MessagePhase::Commentary => Some(CodexResponseMessagePhase::Commentary),
|
||||||
|
MessagePhase::FinalAnswer => Some(CodexResponseMessagePhase::FinalAnswer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn message_content_text_bytes(content: &[ContentItem]) -> i64 {
|
||||||
|
content
|
||||||
|
.iter()
|
||||||
|
.map(|item| match item {
|
||||||
|
ContentItem::InputText { text } | ContentItem::OutputText { text } => byte_len(text),
|
||||||
|
ContentItem::InputImage { .. } => 0,
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn message_content_part_counts(content: &[ContentItem]) -> (usize, usize) {
|
||||||
|
let mut text_part_count = 0;
|
||||||
|
let mut image_part_count = 0;
|
||||||
|
for item in content {
|
||||||
|
match item {
|
||||||
|
ContentItem::InputText { .. } | ContentItem::OutputText { .. } => {
|
||||||
|
text_part_count += 1;
|
||||||
|
}
|
||||||
|
ContentItem::InputImage { .. } => {
|
||||||
|
image_part_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(text_part_count, image_part_count)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reasoning_content_bytes(
|
||||||
|
summary: &[ReasoningItemReasoningSummary],
|
||||||
|
content: &Option<Vec<ReasoningItemContent>>,
|
||||||
|
) -> i64 {
|
||||||
|
let summary_bytes = summary
|
||||||
|
.iter()
|
||||||
|
.map(|summary| match summary {
|
||||||
|
ReasoningItemReasoningSummary::SummaryText { text } => byte_len(text),
|
||||||
|
})
|
||||||
|
.sum::<i64>();
|
||||||
|
let content_bytes = content
|
||||||
|
.as_ref()
|
||||||
|
.map(|content| {
|
||||||
|
content
|
||||||
|
.iter()
|
||||||
|
.map(|content| match content {
|
||||||
|
ReasoningItemContent::ReasoningText { text }
|
||||||
|
| ReasoningItemContent::Text { text } => byte_len(text),
|
||||||
|
})
|
||||||
|
.sum::<i64>()
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
summary_bytes + content_bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
fn function_call_output_bytes(output: &FunctionCallOutputPayload) -> Option<i64> {
|
||||||
|
match &output.body {
|
||||||
|
FunctionCallOutputBody::Text(text) => Some(byte_len(text)),
|
||||||
|
FunctionCallOutputBody::ContentItems(items) => serialized_bytes(items),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn function_call_output_part_counts(
|
||||||
|
output: &FunctionCallOutputPayload,
|
||||||
|
) -> (Option<usize>, Option<usize>) {
|
||||||
|
let Some(content_items) = output.content_items() else {
|
||||||
|
return (None, None);
|
||||||
|
};
|
||||||
|
let mut text_part_count = 0;
|
||||||
|
let mut image_part_count = 0;
|
||||||
|
for item in content_items {
|
||||||
|
match item {
|
||||||
|
FunctionCallOutputContentItem::InputText { .. } => {
|
||||||
|
text_part_count += 1;
|
||||||
|
}
|
||||||
|
FunctionCallOutputContentItem::InputImage { .. } => {
|
||||||
|
image_part_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Some(text_part_count), Some(image_part_count))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialized_string<T: Serialize>(value: &T) -> Option<String> {
|
||||||
|
match serde_json::to_value(value).ok()? {
|
||||||
|
serde_json::Value::String(value) => Some(value),
|
||||||
|
value => Some(value.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialized_bytes<T: Serialize>(value: &T) -> Option<i64> {
|
||||||
|
serde_json::to_string(value)
|
||||||
|
.ok()
|
||||||
|
.map(|value| byte_len(&value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn nonzero_i64(value: i64) -> Option<i64> {
|
||||||
|
(value > 0).then_some(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn byte_len(value: &str) -> i64 {
|
||||||
|
i64::try_from(value.len()).unwrap_or(i64::MAX)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use codex_protocol::models::ImageDetail;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn maps_message_metadata() {
|
||||||
|
let items = vec![ResponseItem::Message {
|
||||||
|
id: None,
|
||||||
|
role: "assistant".to_string(),
|
||||||
|
content: vec![
|
||||||
|
ContentItem::OutputText {
|
||||||
|
text: "hello".to_string(),
|
||||||
|
},
|
||||||
|
ContentItem::InputImage {
|
||||||
|
image_url: "data:image/png;base64,abc".to_string(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
end_turn: None,
|
||||||
|
phase: Some(MessagePhase::FinalAnswer),
|
||||||
|
}];
|
||||||
|
|
||||||
|
let metadata = response_items_metadata(CodexResponsesApiItemPhase::Output, &items);
|
||||||
|
|
||||||
|
assert_eq!(metadata[0].item_phase, CodexResponsesApiItemPhase::Output);
|
||||||
|
assert_eq!(
|
||||||
|
metadata[0].response_item_type,
|
||||||
|
CodexResponseItemType::Message
|
||||||
|
);
|
||||||
|
assert_eq!(metadata[0].role, Some(CodexResponseItemRole::Assistant));
|
||||||
|
assert_eq!(
|
||||||
|
metadata[0].message_phase,
|
||||||
|
Some(CodexResponseMessagePhase::FinalAnswer)
|
||||||
|
);
|
||||||
|
assert_eq!(metadata[0].content_bytes, Some(5));
|
||||||
|
assert_eq!(metadata[0].text_part_count, Some(1));
|
||||||
|
assert_eq!(metadata[0].image_part_count, Some(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn maps_tool_call_output_metadata() {
|
||||||
|
let items = vec![ResponseItem::CustomToolCallOutput {
|
||||||
|
call_id: "call_1".to_string(),
|
||||||
|
name: Some("custom_tool".to_string()),
|
||||||
|
output: FunctionCallOutputPayload::from_content_items(vec![
|
||||||
|
FunctionCallOutputContentItem::InputText {
|
||||||
|
text: "result".to_string(),
|
||||||
|
},
|
||||||
|
FunctionCallOutputContentItem::InputImage {
|
||||||
|
image_url: "https://example.test/image.png".to_string(),
|
||||||
|
detail: Some(ImageDetail::High),
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
}];
|
||||||
|
|
||||||
|
let metadata = response_items_metadata(CodexResponsesApiItemPhase::Output, &items);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
metadata[0].response_item_type,
|
||||||
|
CodexResponseItemType::CustomToolCallOutput
|
||||||
|
);
|
||||||
|
assert_eq!(metadata[0].call_id.as_deref(), Some("call_1"));
|
||||||
|
assert_eq!(metadata[0].tool_name.as_deref(), Some("custom_tool"));
|
||||||
|
assert!(metadata[0].output_bytes.unwrap_or_default() > 0);
|
||||||
|
assert_eq!(metadata[0].text_part_count, Some(1));
|
||||||
|
assert_eq!(metadata[0].image_part_count, Some(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user