[router] implement response api get input item function and refactor input/output store (#11924)
This commit is contained in:
@@ -94,6 +94,14 @@ pub enum ReasoningSummary {
|
||||
// Input/Output Items
|
||||
// ============================================================================
|
||||
|
||||
/// Content can be either a simple string or array of content parts (for SimpleInputMessage)
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum StringOrContentParts {
|
||||
String(String),
|
||||
Array(Vec<ResponseContentPart>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(tag = "type")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
@@ -125,6 +133,14 @@ pub enum ResponseInputOutputItem {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
status: Option<String>,
|
||||
},
|
||||
#[serde(untagged)]
|
||||
SimpleInputMessage {
|
||||
content: StringOrContentParts,
|
||||
role: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(rename = "type")]
|
||||
r#type: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
@@ -551,8 +567,8 @@ pub struct ResponsesRequest {
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum ResponseInput {
|
||||
Text(String),
|
||||
Items(Vec<ResponseInputOutputItem>),
|
||||
Text(String),
|
||||
}
|
||||
|
||||
impl Default for ResponsesRequest {
|
||||
@@ -622,6 +638,28 @@ impl GenerationRequest for ResponsesRequest {
|
||||
Some(texts.join(" "))
|
||||
}
|
||||
}
|
||||
ResponseInputOutputItem::SimpleInputMessage { content, .. } => {
|
||||
match content {
|
||||
StringOrContentParts::String(s) => Some(s.clone()),
|
||||
StringOrContentParts::Array(parts) => {
|
||||
// SimpleInputMessage only supports InputText
|
||||
let texts: Vec<String> = parts
|
||||
.iter()
|
||||
.filter_map(|part| match part {
|
||||
ResponseContentPart::InputText { text } => {
|
||||
Some(text.clone())
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
if texts.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(texts.join(" "))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ResponseInputOutputItem::Reasoning { content, .. } => {
|
||||
let texts: Vec<String> = content
|
||||
.iter()
|
||||
@@ -645,6 +683,50 @@ impl GenerationRequest for ResponsesRequest {
|
||||
}
|
||||
}
|
||||
|
||||
/// Normalize a SimpleInputMessage to a proper Message item
|
||||
///
|
||||
/// This helper converts SimpleInputMessage (which can have flexible content)
|
||||
/// into a fully-structured Message item with a generated ID, role, and content array.
|
||||
///
|
||||
/// SimpleInputMessage items are converted to Message items with IDs generated using
|
||||
/// the centralized ID generation pattern with "msg_" prefix for consistency.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `item` - The input item to normalize
|
||||
///
|
||||
/// # Returns
|
||||
/// A normalized ResponseInputOutputItem (either Message if converted, or original if not SimpleInputMessage)
|
||||
pub fn normalize_input_item(item: &ResponseInputOutputItem) -> ResponseInputOutputItem {
|
||||
match item {
|
||||
ResponseInputOutputItem::SimpleInputMessage { content, role, .. } => {
|
||||
let content_vec = match content {
|
||||
StringOrContentParts::String(s) => {
|
||||
vec![ResponseContentPart::InputText { text: s.clone() }]
|
||||
}
|
||||
StringOrContentParts::Array(parts) => parts.clone(),
|
||||
};
|
||||
|
||||
ResponseInputOutputItem::Message {
|
||||
id: generate_id("msg"),
|
||||
role: role.clone(),
|
||||
content: content_vec,
|
||||
status: Some("completed".to_string()),
|
||||
}
|
||||
}
|
||||
_ => item.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_id(prefix: &str) -> String {
|
||||
use rand::RngCore;
|
||||
let mut rng = rand::rng();
|
||||
// Generate exactly 50 hex characters (25 bytes) for the part after the underscore
|
||||
let mut bytes = [0u8; 25];
|
||||
rng.fill_bytes(&mut bytes);
|
||||
let hex_string: String = bytes.iter().map(|b| format!("{:02x}", b)).collect();
|
||||
format!("{}_{}", prefix, hex_string)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct ResponsesResponse {
|
||||
/// Response ID
|
||||
|
||||
Reference in New Issue
Block a user