aKode 0.9.1: immagini degli strumenti in un messaggio utente, errore 500 chiamato per nome

- view_image con server llama.cpp/ik_llama.cpp: l'immagine dentro function_call_output
  faceva rispondere 500 ("Output of tool call should be 'Input text'") e chiudeva il
  turno con un errore (prove s2/s3 di Fourier). Con merge_system_messages l'output resta
  testo con un segnaposto e l'immagine segue in un messaggio utente (verificato sul
  server: 500 prima, risposta corretta dopo).
- Il messaggio per HTTP 500 non parla più di «high demand».

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-17 01:20:19 +01:00
parent a1a92535bb
commit 2d933d0926
6 changed files with 159 additions and 2 deletions

View File

@@ -4169,7 +4169,7 @@ async fn realtime_webrtc_start_surfaces_backend_error() -> Result<()> {
let error =
read_notification::<ThreadRealtimeErrorNotification>(&mut mcp, "thread/realtime/error")
.await?;
assert!(error.message.contains("currently experiencing high demand"));
assert!(error.message.contains("errore interno (HTTP 500)"));
realtime_server.shutdown().await;
Ok(())

View File

@@ -1168,6 +1168,8 @@ impl ModelClient {
let include = vec!["reasoning.encrypted_content".to_string()];
if self.state.provider.info().merge_system_messages() {
crate::merge_system_messages::merge_system_messages(&mut input);
// aKode: chat-template servers accept images only in user messages.
crate::tool_output_images::move_tool_output_images(&mut input);
}
let verbosity = if model_info.support_verbosity {
self.state.model_verbosity.or(model_info.default_verbosity)

View File

@@ -41,6 +41,7 @@ mod compact_retained_tool_outputs;
mod compact_token_budget;
pub mod compaction_metadata;
mod merge_system_messages;
mod tool_output_images;
mod tool_order;
mod tool_output_clearing;
mod thread_startup_metadata;

View File

@@ -0,0 +1,153 @@
//! aKode: moves images out of tool outputs for chat-template servers.
//!
//! llama.cpp / ik_llama.cpp accept images only in user messages: an image inside a
//! `function_call_output` (for example from `view_image`) makes `/v1/responses` fail
//! with HTTP 500 ("Output of tool call should be 'Input text'"), and the turn ends with
//! an error. With `merge_system_messages` enabled (the chat-template compatibility
//! switch, on by default) each tool output keeps its text plus a short placeholder and
//! its images follow it in a `user` message. The transformation only depends on the
//! history, so the prompt prefix stays the same between turns.
use akode_protocol::models::ContentItem;
use akode_protocol::models::FunctionCallOutputBody;
use akode_protocol::models::FunctionCallOutputContentItem;
use akode_protocol::models::FunctionCallOutputPayload;
use akode_protocol::models::ResponseItem;
/// Text left in the tool output in place of its images.
pub(crate) const IMAGE_MOVED_PLACEHOLDER: &str = "[image attached in the next message]";
/// Splits `output` into a text-only output and the images it contained.
fn split_images(output: &mut FunctionCallOutputPayload) -> Vec<ContentItem> {
let FunctionCallOutputBody::ContentItems(items) = &output.body else {
return Vec::new();
};
if !items
.iter()
.any(|item| matches!(item, FunctionCallOutputContentItem::InputImage { .. }))
{
return Vec::new();
}
let mut texts = Vec::new();
let mut images = Vec::new();
for item in items {
match item {
FunctionCallOutputContentItem::InputText { text } => texts.push(text.clone()),
FunctionCallOutputContentItem::InputImage { image, detail } => {
images.push(ContentItem::InputImage {
image: image.clone(),
detail: *detail,
});
}
#[allow(unreachable_patterns)]
_ => {}
}
}
texts.push(IMAGE_MOVED_PLACEHOLDER.to_string());
output.body = FunctionCallOutputBody::Text(texts.join("\n"));
images
}
fn image_message(images: Vec<ContentItem>) -> ResponseItem {
ResponseItem::Message {
encrypted_content: None,
status: None,
id: None,
role: "user".to_string(),
content: images,
phase: None,
internal_chat_message_metadata_passthrough: None,
}
}
/// Moves the images of every tool output into a `user` message right after it.
pub(crate) fn move_tool_output_images(input: &mut Vec<ResponseItem>) {
let mut index = 0;
while index < input.len() {
let images = match &mut input[index] {
ResponseItem::FunctionCallOutput { output, .. }
| ResponseItem::CustomToolCallOutput { output, .. } => split_images(output),
_ => Vec::new(),
};
if images.is_empty() {
index += 1;
continue;
}
input.insert(index + 1, image_message(images));
index += 2;
}
}
#[cfg(test)]
mod tests {
use super::*;
use akode_protocol::models::ImageReference;
use pretty_assertions::assert_eq;
fn output_item(body: FunctionCallOutputBody) -> ResponseItem {
ResponseItem::FunctionCallOutput {
id: None,
call_id: Some("c1".to_string()),
name: None,
namespace: None,
output: FunctionCallOutputPayload {
body,
success: Some(true),
},
internal_chat_message_metadata_passthrough: None,
}
}
fn image(url: &str) -> ImageReference {
ImageReference::Inline {
image_url: url.to_string(),
}
}
#[test]
fn moves_images_after_the_tool_output() {
let mut input = vec![
output_item(FunctionCallOutputBody::ContentItems(vec![
FunctionCallOutputContentItem::InputText {
text: "chart.png".to_string(),
},
FunctionCallOutputContentItem::InputImage {
image: image("data:image/png;base64,AAA"),
detail: None,
},
])),
output_item(FunctionCallOutputBody::Text("plain".to_string())),
];
move_tool_output_images(&mut input);
assert_eq!(input.len(), 3);
assert_eq!(
input[0],
output_item(FunctionCallOutputBody::Text(format!(
"chart.png\n{IMAGE_MOVED_PLACEHOLDER}"
)))
);
assert_eq!(
input[1],
image_message(vec![ContentItem::InputImage {
image: image("data:image/png;base64,AAA"),
detail: None,
}])
);
assert_eq!(
input[2],
output_item(FunctionCallOutputBody::Text("plain".to_string()))
);
}
#[test]
fn leaves_text_outputs_alone() {
let original = vec![output_item(FunctionCallOutputBody::ContentItems(vec![
FunctionCallOutputContentItem::InputText {
text: "a".to_string(),
},
]))];
let mut input = original.clone();
move_tool_output_images(&mut input);
assert_eq!(input, original);
}
}

View File

@@ -205,7 +205,7 @@ pub enum CodexErrorDetails {
QuotaExceeded,
#[error("Your model provider account does not include access to this model.")]
UsageNotIncluded,
#[error("We’re currently experiencing high demand, which may cause temporary errors.")]
#[error("Il server del modello ha risposto con un errore interno (HTTP 500).")]
InternalServerError,
/// Retry limit exceeded.
#[error("{0}")]