Skip to content

Commit 97aa6ec

Browse files
committed
fix(native): remove pyo3-serde; add internal serde_json->PyObject conversion; unblock maturin build
1 parent a20a4d2 commit 97aa6ec

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

crates/codex_native/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ name = "codex_native"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
pyo3 = { version = "0.21", features = ["extension-module", "serde"] }
12-
pyo3-serde = "0.5"
11+
pyo3 = { version = "0.21", features = ["extension-module"] }
1312
anyhow = "1"
1413
serde = { version = "1", features = ["derive"] }
1514
serde_json = "1"
@@ -19,4 +18,3 @@ tracing = "0.1"
1918
# Path deps into the upstream workspace
2019
codex-core = { path = "../../codex-proj/codex-rs/core" }
2120
codex-protocol = { path = "../../codex-proj/codex-rs/protocol" }
22-

crates/codex_native/src/lib.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use codex_core::protocol::{AskForApproval, Event, EventMsg, InputItem};
44
use codex_core::{AuthManager, ConversationManager};
55
use codex_protocol::config_types::SandboxMode;
66
use pyo3::prelude::*;
7-
use pyo3_serde::Serializer;
7+
use pyo3::types::{PyDict, PyList, PyString};
88
use serde_json::Value as JsonValue;
99
use std::path::PathBuf;
1010

@@ -23,7 +23,7 @@ fn run_exec_collect(
2323
// Convert serde_json::Value -> PyObject
2424
let mut out = Vec::with_capacity(events.len());
2525
for v in events {
26-
let obj = pyo3_serde::to_py(py, &v).map_err(to_py)?;
26+
let obj = json_to_py(py, &v)?;
2727
out.push(obj);
2828
}
2929
Ok(out)
@@ -105,3 +105,35 @@ fn codex_native(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
105105
Ok(())
106106
}
107107

108+
fn json_to_py(py: Python<'_>, v: &JsonValue) -> PyResult<PyObject> {
109+
Ok(match v {
110+
JsonValue::Null => py.None().into_py(py),
111+
JsonValue::Bool(b) => b.into_py(py),
112+
JsonValue::Number(n) => {
113+
if let Some(i) = n.as_i64() {
114+
i.into_py(py)
115+
} else if let Some(u) = n.as_u64() {
116+
u.into_py(py)
117+
} else if let Some(f) = n.as_f64() {
118+
f.into_py(py)
119+
} else {
120+
py.None().into_py(py)
121+
}
122+
}
123+
JsonValue::String(s) => PyString::new(py, s).into_py(py),
124+
JsonValue::Array(arr) => {
125+
let list = PyList::empty(py);
126+
for item in arr {
127+
list.append(json_to_py(py, item)?)?;
128+
}
129+
list.into_py(py)
130+
}
131+
JsonValue::Object(map) => {
132+
let dict = PyDict::new(py);
133+
for (k, val) in map.iter() {
134+
dict.set_item(k, json_to_py(py, val)?)?;
135+
}
136+
dict.into_py(py)
137+
}
138+
})
139+
}

0 commit comments

Comments
 (0)