mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
## Why Cursor session messages can place `<cursor_commands>` and `<timestamp>` context before the actual `<user_query>`. Treating both external session formats with one parser left that context in the imported message and generated title. ## What changed - Route detected sessions through format-specific Claude and Cursor parsers. - Strip recognized Cursor context wrappers when extracting a trailing `<user_query>`, while preserving messages with unknown leading context. - Keep shared content-block, tool-call, and timestamp parsing in a common module. ## Testing Add parser coverage for wrapped Cursor queries, unknown context, working-directory precedence, Claude metadata, content hashing, and shared tool annotations. GitOrigin-RevId: b49328d46ab4937e3c599588a3d3081e8ab798cd
155 lines
4.7 KiB
Rust
155 lines
4.7 KiB
Rust
use super::truncate;
|
|
use serde_json::Value as JsonValue;
|
|
|
|
const NOTE_MAX_LEN: usize = 2_000;
|
|
const TOOL_RESULT_MAX_LEN: usize = 4_000;
|
|
const EXTERNAL_AGENT_TOOL_CALL_TAG: &str = "external_agent_tool_call";
|
|
const EXTERNAL_AGENT_TOOL_RESULT_TAG: &str = "external_agent_tool_result";
|
|
|
|
pub(super) struct ExtractedMessage {
|
|
pub text: String,
|
|
pub only_tool_result: bool,
|
|
}
|
|
|
|
pub(super) fn extract_message_text(content: &JsonValue) -> Option<ExtractedMessage> {
|
|
let blocks = content_blocks(content);
|
|
let mut parts = Vec::new();
|
|
let mut only_tool_result = !blocks.is_empty();
|
|
|
|
for block in &blocks {
|
|
let block_type = block.get("type").and_then(JsonValue::as_str);
|
|
match block_type {
|
|
Some("text") => {
|
|
if let Some(text) = block.get("text").and_then(JsonValue::as_str)
|
|
&& !text.is_empty()
|
|
{
|
|
parts.push(text.to_string());
|
|
only_tool_result = false;
|
|
}
|
|
}
|
|
Some("tool_use") => {
|
|
parts.push(tool_call_note(block));
|
|
only_tool_result = false;
|
|
}
|
|
Some("tool_result") => {
|
|
parts.push(tool_result_note(block));
|
|
}
|
|
Some("thinking") => {}
|
|
Some(other) => {
|
|
parts.push(format!("[external unsupported block: {other}]"));
|
|
only_tool_result = false;
|
|
}
|
|
None => {}
|
|
}
|
|
}
|
|
|
|
let text = parts
|
|
.into_iter()
|
|
.filter(|part| !part.trim().is_empty())
|
|
.collect::<Vec<_>>()
|
|
.join("\n\n");
|
|
if text.is_empty() {
|
|
None
|
|
} else {
|
|
Some(ExtractedMessage {
|
|
text,
|
|
only_tool_result,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub(super) fn parse_timestamp(timestamp: &str) -> Option<i64> {
|
|
chrono::DateTime::parse_from_rfc3339(timestamp)
|
|
.ok()
|
|
.map(|value| value.timestamp())
|
|
}
|
|
|
|
fn content_blocks(content: &JsonValue) -> Vec<JsonValue> {
|
|
if let Some(text) = content.as_str() {
|
|
return vec![serde_json::json!({
|
|
"type": "text",
|
|
"text": text,
|
|
})];
|
|
}
|
|
content
|
|
.as_array()
|
|
.map(|items| {
|
|
items
|
|
.iter()
|
|
.filter(|item| item.is_object())
|
|
.cloned()
|
|
.collect()
|
|
})
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
fn tool_call_note(block: &JsonValue) -> String {
|
|
let name = block
|
|
.get("name")
|
|
.and_then(JsonValue::as_str)
|
|
.unwrap_or("unknown");
|
|
let mut lines = vec![format!("[{EXTERNAL_AGENT_TOOL_CALL_TAG}: {name}]")];
|
|
if let Some(input) = block.get("input").and_then(JsonValue::as_object) {
|
|
if let Some(description) = input.get("description").and_then(JsonValue::as_str) {
|
|
lines.push(format!("description: {description}"));
|
|
}
|
|
if let Some(command) = input.get("command").and_then(JsonValue::as_str) {
|
|
lines.push(format!("command: {command}"));
|
|
}
|
|
if let Some(file) = input
|
|
.get("file_path")
|
|
.or_else(|| input.get("file"))
|
|
.and_then(JsonValue::as_str)
|
|
{
|
|
lines.push(format!("file: {file}"));
|
|
}
|
|
if lines.len() == 1 {
|
|
lines.push(format!(
|
|
"input: {}",
|
|
truncate(&JsonValue::Object(input.clone()).to_string(), NOTE_MAX_LEN)
|
|
));
|
|
}
|
|
} else if let Some(input) = block.get("input") {
|
|
lines.push(format!(
|
|
"input: {}",
|
|
truncate(&input.to_string(), NOTE_MAX_LEN)
|
|
));
|
|
}
|
|
lines.push(format!("[/{EXTERNAL_AGENT_TOOL_CALL_TAG}]"));
|
|
lines.join("\n")
|
|
}
|
|
|
|
fn tool_result_note(block: &JsonValue) -> String {
|
|
let label = if block.get("is_error").and_then(JsonValue::as_bool) == Some(true) {
|
|
format!("[{EXTERNAL_AGENT_TOOL_RESULT_TAG}: error]")
|
|
} else {
|
|
format!("[{EXTERNAL_AGENT_TOOL_RESULT_TAG}]")
|
|
};
|
|
let text = tool_result_text(block.get("content"));
|
|
if text.is_empty() {
|
|
format!("{label}\n[/{EXTERNAL_AGENT_TOOL_RESULT_TAG}]")
|
|
} else {
|
|
format!(
|
|
"{label}\n{}\n[/{EXTERNAL_AGENT_TOOL_RESULT_TAG}]",
|
|
truncate(&text, TOOL_RESULT_MAX_LEN)
|
|
)
|
|
}
|
|
}
|
|
|
|
fn tool_result_text(content: Option<&JsonValue>) -> String {
|
|
match content {
|
|
Some(JsonValue::String(text)) => text.clone(),
|
|
Some(JsonValue::Array(items)) => items
|
|
.iter()
|
|
.filter_map(|item| item.get("text").and_then(JsonValue::as_str))
|
|
.filter(|text| !text.is_empty())
|
|
.collect::<Vec<_>>()
|
|
.join("\n"),
|
|
_ => String::new(),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "records_common_tests.rs"]
|
|
mod tests;
|