Skip to content

Commit 48f26c1

Browse files
ryoppippiigorovh
andcommitted
perf(opencode): read Claude Agent links only for claude-agent rows
Every v2 row deserialized its whole content array through serde_json::Value, because the lenient helpers build a Value per field, so message text was materialized just to find content[].native.sessionID on the few rows that carry one. The link is now read in a second, typed pass that only claude-agent rows take, and derived structs skip every other field without building it. A malformed part leaves the row unlinked instead of dropping it. Refs #2004 Co-authored-by: igorovh <[email protected]>
1 parent b109121 commit 48f26c1

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

‎rust/adapters/opencode/src/parser.rs‎

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,42 @@ struct OpenCodeV2Message {
113113
time: Option<OpenCodeTime>,
114114
#[serde(default, deserialize_with = "jsonl::lenient_f64")]
115115
cost: Option<f64>,
116-
#[serde(default, deserialize_with = "jsonl::lenient_vec")]
116+
}
117+
118+
/// Reads only `content[].native.sessionID`. Derived structs skip every other
119+
/// field, such as message text, without building it, and a malformed part
120+
/// leaves the row unlinked rather than dropping it.
121+
#[derive(Deserialize)]
122+
struct OpenCodeNativeLinks {
123+
#[serde(default)]
117124
content: Vec<OpenCodeContentPart>,
118125
}
119126

120-
#[derive(Debug, Deserialize)]
127+
#[derive(Deserialize)]
121128
struct OpenCodeContentPart {
122-
#[serde(default, deserialize_with = "jsonl::lenient_object")]
129+
#[serde(default)]
123130
native: Option<OpenCodeNativeReference>,
124131
}
125132

126-
#[derive(Debug, Deserialize)]
133+
#[derive(Deserialize)]
127134
struct OpenCodeNativeReference {
128-
#[serde(
129-
rename = "sessionID",
130-
default,
131-
deserialize_with = "jsonl::non_empty_string"
132-
)]
135+
#[serde(rename = "sessionID", default)]
133136
session_id: Option<String>,
134137
}
135138

139+
/// The Claude Code session a `claude-agent` row ran in, which names the
140+
/// Claude transcript holding the same turns.
141+
fn native_session_id(data: &str) -> Option<Arc<str>> {
142+
serde_json::from_str::<OpenCodeNativeLinks>(data)
143+
.ok()?
144+
.content
145+
.into_iter()
146+
.filter_map(|part| part.native?.session_id)
147+
.map(|session_id| session_id.trim().to_owned())
148+
.find(|session_id| !session_id.is_empty())
149+
.map(Arc::from)
150+
}
151+
136152
impl OpenCodeV2Message {
137153
fn provider(&self) -> Option<&str> {
138154
self.model
@@ -141,19 +157,6 @@ impl OpenCodeV2Message {
141157
.or(self.provider_id.as_deref())
142158
}
143159

144-
/// The Claude Code session a `claude-agent` row ran in, which names the
145-
/// Claude transcript holding the same turns.
146-
fn native_session_id(&self) -> Option<Arc<str>> {
147-
(self.provider() == Some("claude-agent"))
148-
.then(|| {
149-
self.content
150-
.iter()
151-
.find_map(|part| part.native.as_ref()?.session_id.as_deref())
152-
})
153-
.flatten()
154-
.map(Arc::from)
155-
}
156-
157160
fn into_legacy_message(self, id: String, session_id: String, created: i64) -> OpenCodeMessage {
158161
let model_id = self
159162
.model
@@ -313,7 +316,9 @@ pub(crate) fn session_message_value_to_entry(
313316
pricing: Option<&PricingMap>,
314317
) -> Option<(LoadedEntry, Option<Arc<str>>)> {
315318
let value = serde_json::from_str::<OpenCodeV2Message>(data).ok()?;
316-
let native_session = value.native_session_id();
319+
let native_session = (value.provider() == Some("claude-agent"))
320+
.then(|| native_session_id(data))
321+
.flatten();
317322
let value = value.into_legacy_message(id, session_id, created);
318323
message_value_to_entry(&value, None, None, tz, mode, pricing)
319324
.map(|entry| (entry, native_session))
@@ -541,6 +546,17 @@ mod tests {
541546
);
542547
}
543548

549+
#[test]
550+
fn skips_blank_native_session_ids() {
551+
assert_eq!(
552+
v2_native_session(
553+
r#"{"model":{"id":"opus","providerID":"claude-agent"},"tokens":{"input":100},"content":[{"type":"text","native":{"sessionID":" "}},{"type":"tool","native":{"sessionID":"claude-session-2","resultState":{"x":[1]}}}]}"#
554+
)
555+
.as_deref(),
556+
Some("claude-session-2")
557+
);
558+
}
559+
544560
fn message(value: serde_json::Value) -> OpenCodeMessage {
545561
serde_json::from_value(value).unwrap()
546562
}

0 commit comments

Comments
 (0)