Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/dim-agent-aimux/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ impl ModelDriver for AimuxDriver {
result,
tool_names,
protocol,
preset == Some(OPENAI_CODEX_PRESET),
cancel,
abort,
self.generation.clone(),
Expand Down
202 changes: 188 additions & 14 deletions crates/dim-agent-aimux/src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod content;
mod tags;

use std::{
collections::{HashMap, HashSet},
Expand All @@ -19,8 +20,8 @@ use dim_agent_capabilities::host::{
ModelStream,
};
use dim_agent_types::{
FAILURE_CAUSE_CHARS, FAILURE_CAUSE_FRAMES, FAILURE_REASON_CHARS, MessagePart, ModelFailureKind,
ToolCall, Usage, one_line,
ContentMetadata, FAILURE_CAUSE_CHARS, FAILURE_CAUSE_FRAMES, FAILURE_REASON_CHARS, MessagePart,
ModelFailureKind, ToolCall, Usage, one_line,
};
use futures_util::{StreamExt, future::poll_immediate};
use serde_json::Value;
Expand All @@ -47,10 +48,18 @@ use crate::request::ToolNames;
/// idle bound would let a trickle of bytes extend it.
pub(crate) const STREAM_IDLE_TIMEOUT: Duration = Duration::from_secs(300);

/// `scan_thinking_tags` converts the `<thinking>` sections the managed
/// openai-codex provider writes into its text stream; every other provider
/// streams text verbatim.
#[expect(
clippy::too_many_arguments,
reason = "one parameter per stream or cancellation handle the conversion is assembled from"
)]
pub(crate) fn convert(
result: StreamResult,
tool_names: ToolNames,
protocol: ModelProtocol,
scan_thinking_tags: bool,
cancel: CancellationToken,
abort: AbortSignal,
generation: CancellationToken,
Expand All @@ -72,6 +81,10 @@ pub(crate) fn convert(
let mut ended_inputs = HashSet::new();
let mut completed_calls = HashSet::new();
let mut thinking = HashMap::<String, String>::new();
// Per text block, the `<thinking>` scan and the metadata held back
// from a converted part; both stay empty without the scan.
let mut tag_scans = HashMap::<String, tags::TagScan>::new();
let mut scan_metadata = HashMap::<String, Option<ContentMetadata>>::new();
let mut answered = false;
let mut unfinished_call = None;
let terminal = loop {
Expand Down Expand Up @@ -171,6 +184,9 @@ pub(crate) fn convert(
}
StreamPart::TextStart { id, provider_metadata } => {
block_active.insert((false, id.clone()));
if scan_thinking_tags {
tag_scans.insert(id.clone(), tags::TagScan::new());
}
if preserve {
let metadata =
match content::metadata(protocol, provider_metadata.as_ref()) {
Expand Down Expand Up @@ -205,28 +221,80 @@ pub(crate) fn convert(
}
}
StreamPart::TextDelta { id, delta, provider_metadata } => {
if preserve {
let metadata =
if scan_thinking_tags && tag_scans.contains_key(&id) {
let metadata = if preserve {
match content::metadata(protocol, provider_metadata.as_ref()) {
Ok(metadata) => metadata,
Err(error) => break Some(Err(error)),
};
let Some(part) = blocks
.get(&(false, id))
.and_then(|index| parts.get_mut(*index))
else {
}
} else {
None
};
let Some(scan) = tag_scans.get_mut(&id) else {
break Some(Err(error(
AiMuxError::invalid_response_data("Missing text block"),
&cancel,
)));
};
if let MessagePart::Text { text, .. } = part {
text.push_str(&delta);
let outputs = scan.feed(&delta);
let chunks = match apply_scan_outputs(
outputs,
&id,
preserve,
&mut parts,
&mut blocks,
&mut scan_metadata,
&mut answered,
) {
Ok(chunks) => chunks,
Err(error) => break Some(Err(error)),
};
for chunk in chunks {
yield Ok(chunk);
}
content::update(part, metadata);
// While the scan holds the block inside the tag, the
// metadata folds into the trailing text part instead
// of the converted thinking part.
if preserve && let Some(incoming) = metadata {
if let Some(held) = scan_metadata.get_mut(&id) {
content::merge(held, incoming);
} else {
let Some(part) = blocks
.get(&(false, id.clone()))
.and_then(|index| parts.get_mut(*index))
else {
break Some(Err(error(
AiMuxError::invalid_response_data("Missing text block"),
&cancel,
)));
};
content::update(part, Some(incoming));
}
}
} else {
if preserve {
let metadata =
match content::metadata(protocol, provider_metadata.as_ref()) {
Ok(metadata) => metadata,
Err(error) => break Some(Err(error)),
};
let Some(part) = blocks
.get(&(false, id))
.and_then(|index| parts.get_mut(*index))
else {
break Some(Err(error(
AiMuxError::invalid_response_data("Missing text block"),
&cancel,
)));
};
if let MessagePart::Text { text, .. } = part {
text.push_str(&delta);
}
content::update(part, metadata);
}
if !delta.trim().is_empty() { answered = true; }
if !delta.is_empty() { yield Ok(ModelChunk::Text(delta)); }
}
if !delta.trim().is_empty() { answered = true; }
if !delta.is_empty() { yield Ok(ModelChunk::Text(delta)); }
}
StreamPart::ReasoningDelta { id, delta, provider_metadata } => {
let Some(thought) = thinking.get_mut(&id) else {
Expand Down Expand Up @@ -266,6 +334,27 @@ pub(crate) fn convert(
_ => unreachable!(),
};
block_active.remove(&(reasoning, id.clone()));
if !reasoning
&& scan_thinking_tags
&& let Some(mut scan) = tag_scans.remove(&id)
{
let outputs = scan.end();
let chunks = match apply_scan_outputs(
outputs,
&id,
preserve,
&mut parts,
&mut blocks,
&mut scan_metadata,
&mut answered,
) {
Ok(chunks) => chunks,
Err(error) => break Some(Err(error)),
};
for chunk in chunks {
yield Ok(chunk);
}
}
if preserve {
let metadata =
match content::metadata(protocol, provider_metadata.as_ref()) {
Expand Down Expand Up @@ -486,6 +575,91 @@ pub(crate) fn convert(
})
}

/// Applies one batch of scan decisions of the text block `id` to the reply's
/// parts and returns the chunks they stream, in order. `Opened` converts the
/// block's part in place: the scan never decides before the open tag
/// completed, so the part is still empty. `ThinkingEnd` ends that part and
/// starts the block's trailing text part, which receives what follows the
/// close tag and carries the continuation metadata the thinking part must
/// not keep.
fn apply_scan_outputs(
outputs: Vec<tags::ScanOutput>,
id: &str,
preserve: bool,
parts: &mut Vec<MessagePart>,
blocks: &mut HashMap<(bool, String), usize>,
scan_metadata: &mut HashMap<String, Option<ContentMetadata>>,
answered: &mut bool,
) -> Result<Vec<ModelChunk>, HostError> {
let mut chunks = Vec::new();
for output in outputs {
match output {
tags::ScanOutput::Opened => {
scan_metadata.entry(id.to_owned()).or_insert(None);
if preserve {
let Some(part) = blocks
.get(&(false, id.to_owned()))
.and_then(|index| parts.get_mut(*index))
else {
return Err(HostError::failed("Missing scanned text block"));
};
if let MessagePart::Text {
provider_metadata, ..
} = part
&& let Some(held) = scan_metadata.get_mut(id)
{
*held = provider_metadata.take();
}
*part = MessagePart::Thinking {
thinking: String::new(),
start_time: None,
end_time: None,
provider_metadata: None,
};
}
}
tags::ScanOutput::Thinking(delta) => {
if preserve
&& let Some(index) = blocks.get(&(false, id.to_owned()))
&& let Some(MessagePart::Thinking { thinking, .. }) = parts.get_mut(*index)
{
thinking.push_str(&delta);
}
if !delta.trim().is_empty() {
*answered = true;
}
chunks.push(ModelChunk::Thinking(delta));
}
tags::ScanOutput::ThinkingEnd(total) => {
if preserve {
let metadata = scan_metadata.remove(id).unwrap_or(None);
blocks.insert((false, id.to_owned()), parts.len());
parts.push(MessagePart::Text {
text: String::new(),
start_time: None,
end_time: None,
provider_metadata: metadata,
});
}
chunks.push(ModelChunk::ThinkingEnd { thinking: total });
}
tags::ScanOutput::Text(text) => {
if preserve
&& let Some(index) = blocks.get(&(false, id.to_owned()))
&& let Some(MessagePart::Text { text: part, .. }) = parts.get_mut(*index)
{
part.push_str(&text);
}
if !text.trim().is_empty() {
*answered = true;
}
chunks.push(ModelChunk::Text(text));
}
}
}
Ok(chunks)
}

/// The metadata of a reasoning block. A Bedrock reasoning block without a
/// signature or redacted payload is still a Bedrock block, so it is stored as
/// an unsigned one; the converter leaves such a block out of a replayed
Expand Down
51 changes: 29 additions & 22 deletions crates/dim-agent-aimux/src/stream/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,34 @@ pub(super) fn update(part: &mut MessagePart, incoming: Option<ContentMetadata>)
} => provider_metadata,
_ => unreachable!(),
};
if let Some(mut incoming) = incoming {
if let (
Some(ContentMetadata::OpenaiResponses {
item_id: old_id,
encrypted_content: old_encrypted,
summary_index: old_index,
phase: old_phase,
}),
ContentMetadata::OpenaiResponses {
item_id,
encrypted_content,
summary_index,
phase,
},
) = (target.as_ref(), &mut incoming)
&& old_id == item_id
{
*encrypted_content = encrypted_content.take().or_else(|| old_encrypted.clone());
*summary_index = summary_index.or(*old_index);
*phase = phase.take().or_else(|| old_phase.clone());
}
*target = Some(incoming);
if let Some(incoming) = incoming {
merge(target, incoming);
}
}

/// Folds one metadata event into the accumulated continuation fields. Later
/// events replace the value, except within one OpenAI Responses item, where a
/// field the event leaves out keeps what the item already carried.
pub(super) fn merge(target: &mut Option<ContentMetadata>, mut incoming: ContentMetadata) {
if let (
Some(ContentMetadata::OpenaiResponses {
item_id: old_id,
encrypted_content: old_encrypted,
summary_index: old_index,
phase: old_phase,
}),
ContentMetadata::OpenaiResponses {
item_id,
encrypted_content,
summary_index,
phase,
},
) = (target.as_ref(), &mut incoming)
&& old_id == item_id
{
*encrypted_content = encrypted_content.take().or_else(|| old_encrypted.clone());
*summary_index = summary_index.or(*old_index);
*phase = phase.take().or_else(|| old_phase.clone());
}
*target = Some(incoming);
}
Loading