Skip to content

Commit be6b376

Browse files
fix(sdk/native): honor model_provider + stream delta events
1 parent 4b6e551 commit be6b376

3 files changed

Lines changed: 116 additions & 9 deletions

File tree

‎codex-rs/exec/src/event_processor_with_jsonl_output.rs‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ pub struct EventProcessorWithJsonOutput {
5252
last_total_token_usage: Option<codex_core::protocol::TokenUsage>,
5353
running_mcp_tool_calls: HashMap<String, RunningMcpToolCall>,
5454
last_critical_error: Option<ThreadErrorEvent>,
55+
// Aggregated streaming text for delta events. We synthesize `item.updated` events
56+
// with the full accumulated text so JS clients can diff and print the delta.
57+
running_agent_message: Option<RunningTextItem>,
58+
running_reasoning_summary: Option<RunningTextItem>,
59+
running_reasoning_raw: Option<RunningTextItem>,
5560
}
5661

5762
#[derive(Debug, Clone)]
@@ -75,6 +80,12 @@ struct RunningMcpToolCall {
7580
arguments: JsonValue,
7681
}
7782

83+
#[derive(Debug, Clone)]
84+
struct RunningTextItem {
85+
item_id: String,
86+
text: String,
87+
}
88+
7889
impl EventProcessorWithJsonOutput {
7990
pub fn new(last_message_path: Option<PathBuf>) -> Self {
8091
Self {
@@ -86,6 +97,9 @@ impl EventProcessorWithJsonOutput {
8697
last_total_token_usage: None,
8798
running_mcp_tool_calls: HashMap::new(),
8899
last_critical_error: None,
100+
running_agent_message: None,
101+
running_reasoning_summary: None,
102+
running_reasoning_raw: None,
89103
}
90104
}
91105

@@ -94,6 +108,11 @@ impl EventProcessorWithJsonOutput {
94108
protocol::EventMsg::SessionConfigured(ev) => self.handle_session_configured(ev),
95109
protocol::EventMsg::AgentMessage(ev) => self.handle_agent_message(ev),
96110
protocol::EventMsg::AgentReasoning(ev) => self.handle_reasoning_event(ev),
111+
// Forward streaming deltas so JS consumers (Thread.runStreamed / CodexProvider)
112+
// can show incremental output as it is generated.
113+
protocol::EventMsg::AgentMessageContentDelta(ev) => self.handle_agent_message_delta(ev),
114+
protocol::EventMsg::ReasoningContentDelta(ev) => self.handle_reasoning_delta(ev),
115+
protocol::EventMsg::ReasoningRawContentDelta(ev) => self.handle_reasoning_raw_delta(ev),
97116
protocol::EventMsg::ExecCommandBegin(ev) => self.handle_exec_command_begin(ev),
98117
protocol::EventMsg::ExecCommandEnd(ev) => self.handle_exec_command_end(ev),
99118
protocol::EventMsg::TerminalInteraction(ev) => self.handle_terminal_interaction(ev),
@@ -208,6 +227,78 @@ impl EventProcessorWithJsonOutput {
208227

209228
vec![ThreadEvent::ItemCompleted(ItemCompletedEvent { item })]
210229
}
230+
231+
fn handle_agent_message_delta(
232+
&mut self,
233+
ev: &protocol::AgentMessageContentDeltaEvent,
234+
) -> Vec<ThreadEvent> {
235+
if self.running_agent_message.is_none() {
236+
self.running_agent_message = Some(RunningTextItem {
237+
item_id: self.get_next_item_id(),
238+
text: String::new(),
239+
});
240+
}
241+
let entry = self
242+
.running_agent_message
243+
.as_mut()
244+
.expect("running_agent_message initialized above");
245+
entry.text.push_str(&ev.delta);
246+
let item = ThreadItem {
247+
id: entry.item_id.clone(),
248+
details: ThreadItemDetails::AgentMessage(AgentMessageItem {
249+
text: entry.text.clone(),
250+
}),
251+
};
252+
vec![ThreadEvent::ItemUpdated(ItemUpdatedEvent { item })]
253+
}
254+
255+
fn handle_reasoning_delta(
256+
&mut self,
257+
ev: &protocol::ReasoningContentDeltaEvent,
258+
) -> Vec<ThreadEvent> {
259+
if self.running_reasoning_summary.is_none() {
260+
self.running_reasoning_summary = Some(RunningTextItem {
261+
item_id: self.get_next_item_id(),
262+
text: String::new(),
263+
});
264+
}
265+
let entry = self
266+
.running_reasoning_summary
267+
.as_mut()
268+
.expect("running_reasoning_summary initialized above");
269+
entry.text.push_str(&ev.delta);
270+
let item = ThreadItem {
271+
id: entry.item_id.clone(),
272+
details: ThreadItemDetails::Reasoning(ReasoningItem {
273+
text: entry.text.clone(),
274+
}),
275+
};
276+
vec![ThreadEvent::ItemUpdated(ItemUpdatedEvent { item })]
277+
}
278+
279+
fn handle_reasoning_raw_delta(
280+
&mut self,
281+
ev: &protocol::ReasoningRawContentDeltaEvent,
282+
) -> Vec<ThreadEvent> {
283+
if self.running_reasoning_raw.is_none() {
284+
self.running_reasoning_raw = Some(RunningTextItem {
285+
item_id: self.get_next_item_id(),
286+
text: String::new(),
287+
});
288+
}
289+
let entry = self
290+
.running_reasoning_raw
291+
.as_mut()
292+
.expect("running_reasoning_raw initialized above");
293+
entry.text.push_str(&ev.delta);
294+
let item = ThreadItem {
295+
id: entry.item_id.clone(),
296+
details: ThreadItemDetails::Reasoning(ReasoningItem {
297+
text: entry.text.clone(),
298+
}),
299+
};
300+
vec![ThreadEvent::ItemUpdated(ItemUpdatedEvent { item })]
301+
}
211302
fn handle_exec_command_begin(
212303
&mut self,
213304
ev: &protocol::ExecCommandBeginEvent,
@@ -461,6 +552,10 @@ impl EventProcessorWithJsonOutput {
461552

462553
fn handle_task_started(&mut self, _: &protocol::TurnStartedEvent) -> Vec<ThreadEvent> {
463554
self.last_critical_error = None;
555+
// New turn: reset streaming text accumulation.
556+
self.running_agent_message = None;
557+
self.running_reasoning_summary = None;
558+
self.running_reasoning_raw = None;
464559
vec![ThreadEvent::TurnStarted(TurnStartedEvent {})]
465560
}
466561

‎sdk/native/package.json‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codex-native/sdk",
3-
"version": "0.0.25",
3+
"version": "0.0.27",
44
"description": "Native NAPI-based Codex SDK - complete standalone implementation.",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",
@@ -47,14 +47,14 @@
4747
]
4848
},
4949
"optionalDependencies": {
50-
"@codex-native/sdk-darwin-arm64": "0.0.25",
51-
"@codex-native/sdk-darwin-x64": "0.0.25",
52-
"@codex-native/sdk-linux-arm64-gnu": "0.0.25",
53-
"@codex-native/sdk-linux-x64-gnu": "0.0.25",
54-
"@codex-native/sdk-linux-arm64-musl": "0.0.25",
55-
"@codex-native/sdk-linux-x64-musl": "0.0.25",
56-
"@codex-native/sdk-win32-arm64-msvc": "0.0.25",
57-
"@codex-native/sdk-win32-x64-msvc": "0.0.25"
50+
"@codex-native/sdk-darwin-arm64": "0.0.27",
51+
"@codex-native/sdk-darwin-x64": "0.0.27",
52+
"@codex-native/sdk-linux-arm64-gnu": "0.0.27",
53+
"@codex-native/sdk-linux-x64-gnu": "0.0.27",
54+
"@codex-native/sdk-linux-arm64-musl": "0.0.27",
55+
"@codex-native/sdk-linux-x64-musl": "0.0.27",
56+
"@codex-native/sdk-win32-arm64-msvc": "0.0.27",
57+
"@codex-native/sdk-win32-x64-msvc": "0.0.27"
5858
},
5959
"dependencies": {
6060
"@modelcontextprotocol/sdk": "^1.22.0",

‎sdk/native/rust-bindings/run/cli_builder.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ pub fn build_cli(
3939
raw_overrides.push(format!("approval_policy={approval_str}"));
4040
}
4141

42+
// Forward model provider selection for non-OSS runs via config overrides.
43+
//
44+
// `codex-rs/exec` currently only populates `ConfigOverrides.model_provider` in OSS mode.
45+
// For remote providers, the provider is resolved from the layered config (including CLI -c
46+
// overrides). Without this, `modelProvider` supplied by the JS SDK can be ignored and
47+
// codex falls back to ~/.codex/config.toml, which is surprising and can hit the wrong backend.
48+
if !options.oss {
49+
if let Some(provider) = options.model_provider.as_ref().map(|s| s.trim()).filter(|s| !s.is_empty()) {
50+
raw_overrides.push(format!("model_provider={provider}"));
51+
}
52+
}
53+
4254
if let Some(ws_opts) = &options.workspace_write_options {
4355
if let Some(network_access) = ws_opts.network_access {
4456
raw_overrides.push(format!(

0 commit comments

Comments
 (0)