Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

delete dead TS-port remnants (#3605)

Sebastian Müller791ce3d8f93a
diff --git a/Cargo.lock b/Cargo.lock
index 6413b5d5b4..2d73147bc7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1438,16 +1438,10 @@ name = "pa-agent"
version = "0.10.0"
dependencies = [
"anyhow",
- "futures",
- "rcgen",
- "reqwest",
- "rustls",
"serde",
"serde_json",
- "tempfile",
"thiserror",
"tokio",
- "tokio-rustls",
]
[[package]]
diff --git a/crates/pa-agent/Cargo.toml b/crates/pa-agent/Cargo.toml
index f9b5fc1a8f..d472ecd6fd 100644
--- a/crates/pa-agent/Cargo.toml
+++ b/crates/pa-agent/Cargo.toml
@@ -7,18 +7,10 @@ description = "Agent loop for Prime Agent."
[dependencies]
anyhow.workspace = true
-futures.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tokio.workspace = true
-reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls-native-roots", "rustls-tls-webpki-roots"] }
-rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
-
-[dev-dependencies]
-tempfile = "3"
-rcgen = { version = "0.14", default-features = false, features = ["pem", "ring"] }
-tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }
[lints]
workspace = true
diff --git a/crates/pa-agent/README.md b/crates/pa-agent/README.md
index ed27ecb1c3..2fb46ef26b 100644
--- a/crates/pa-agent/README.md
+++ b/crates/pa-agent/README.md
@@ -12,9 +12,4 @@ No tool implementations (pa-core), no providers (pa-ai), no session persistence,
`run_loop` (session inputs -> stream of loop events), `ToolDispatcher` trait. Loop internals `pub(crate)`.
## Depends on
-No workspace dependencies. The proxy transport uses the already-resolved
-`rustls` PEM iterator and DER validation to retain the valid prefix of
-`NODE_EXTRA_CA_CERTS` when a later certificate is malformed, matching Node.
-reqwest's whole-bundle parser discards that prefix on error; directly using
-the existing transport dependency avoids a peer dependency on pa-ai and adds
-no package version or public API.
+No workspace dependencies.
diff --git a/crates/pa-agent/src/lib.rs b/crates/pa-agent/src/lib.rs
index 4426068f5b..8e641e2400 100644
--- a/crates/pa-agent/src/lib.rs
+++ b/crates/pa-agent/src/lib.rs
@@ -5,7 +5,6 @@
pub mod abort;
pub mod agent;
pub mod agent_loop;
-pub mod proxy;
pub mod scripted;
pub mod stream;
pub mod types;
diff --git a/crates/pa-agent/src/proxy.rs b/crates/pa-agent/src/proxy.rs
deleted file mode 100644
index b9e76789df..0000000000
--- a/crates/pa-agent/src/proxy.rs
+++ /dev/null
@@ -1,1264 +0,0 @@
-//! Streams through a proxy server (`POST {proxyUrl}/api/stream`) instead of
-//! calling LLM providers directly. The server strips the `partial` field to
-//! reduce bandwidth; the partial message is reconstructed client-side. Wire
-//! events are SSE `data: ` lines; failures are a terminal `error` event.
-
-use std::collections::HashMap;
-use std::fmt::Write;
-use std::sync::LazyLock;
-
-use futures::StreamExt;
-use rustls::pki_types::pem::PemObject;
-use rustls::pki_types::CertificateDer;
-use serde::Deserialize;
-
-use crate::abort::AbortSignal;
-use crate::stream::{
- event_stream, AssistantMessageEvent, AssistantMessageEventStreamHandle, LlmContext,
- StreamRequestOptions,
-};
-use crate::types::{
- AssistantContent, AssistantMessage, Model, StopReason, TextContent, ThinkingContent, ToolCall,
- Usage,
-};
-
-// pa-agent is a leaf crate: use the already-resolved rustls PEM/DER parser
-// instead of a peer provider dependency. Cache once, retaining Node's valid
-// prefix if a later certificate is malformed.
-static PROXY_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
- let mut certs = Vec::new();
- if let Some(path) = std::env::var_os("NODE_EXTRA_CA_CERTS") {
- let loaded = (|| -> anyhow::Result<()> {
- let mut roots = rustls::RootCertStore::empty();
- for cert in CertificateDer::pem_file_iter(&path)? {
- let cert = cert?;
- roots.add(cert.clone())?;
- certs.push(reqwest::Certificate::from_der(&cert)?);
- }
- anyhow::ensure!(!certs.is_empty(), "empty CA bundle");
- Ok(())
- })();
- if let Err(error) = loaded {
- eprintln!(
- "NODE_EXTRA_CA_CERTS could not be fully loaded for the proxy; keeping certificates loaded before the error: {error}"
- );
- }
- }
- let mut builder = reqwest::Client::builder();
- for cert in certs {
- builder = builder.add_root_certificate(cert);
- }
- builder.build().expect("proxy HTTP client")
-});
-
-#[derive(Debug, Clone, Deserialize)]
-#[serde(tag = "type", rename_all = "snake_case")]
-pub enum ProxyAssistantMessageEvent {
- Start,
- TextStart {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- },
- TextDelta {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- delta: String,
- },
- TextEnd {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- content_signature: Option<String>,
- },
- ThinkingStart {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- },
- ThinkingDelta {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- delta: String,
- },
- ThinkingEnd {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- content_signature: Option<String>,
- },
- ToolcallStart {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- id: String,
- #[serde(rename = "toolName")]
- tool_name: String,
- },
- ToolcallDelta {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- delta: String,
- },
- ToolcallEnd {
- #[serde(rename = "contentIndex")]
- content_index: usize,
- },
- Done {
- reason: ProxyDoneReason,
- usage: Usage,
- },
- Error {
- reason: ProxyErrorReason,
- #[serde(rename = "errorMessage")]
- error_message: Option<String>,
- usage: Usage,
- },
-}
-
-#[derive(Debug, Clone, Copy, Deserialize, PartialEq)]
-pub enum ProxyDoneReason {
- #[serde(rename = "stop")]
- Stop,
- #[serde(rename = "length")]
- Length,
- #[serde(rename = "toolUse")]
- ToolUse,
-}
-
-#[derive(Debug, Clone, Copy, Deserialize, PartialEq)]
-pub enum ProxyErrorReason {
- #[serde(rename = "aborted")]
- Aborted,
- #[serde(rename = "error")]
- Error,
-}
-
-pub struct ProxyStreamOptions {
- pub auth_token: String,
- pub proxy_url: String,
- pub signal: AbortSignal,
-}
-
-/// State for reconstructing the partial message (TS keeps `partialJson` on the
-/// tool-call content; here it lives in a side map because the domain type has
-/// no partial-JSON field).
-struct ProxyReconstruction {
- partial: AssistantMessage,
- partial_json: HashMap<usize, StreamingJsonAccumulator>,
-}
-
-fn empty_usage() -> Usage {
- Usage::zero()
-}
-
-/// The consumer side implements [`crate::stream::ModelStream`]; use it as the
-/// `streamFn` for an agent that goes through the proxy.
-#[must_use]
-pub fn stream_proxy(
- model: Model,
- context: LlmContext,
- options: StreamRequestOptions,
- proxy_options: ProxyStreamOptions,
-) -> (
- AssistantMessageEventStreamHandle,
- crate::stream::AssistantMessageEventStream,
-) {
- let (handle, stream) = event_stream();
- let task_handle = handle.clone();
- let signal = proxy_options.signal.clone();
-
- tokio::spawn(async move {
- let mut reconstruction = ProxyReconstruction {
- partial: AssistantMessage {
- content: Vec::new(),
- api: model.api.clone(),
- provider: model.provider.clone(),
- model: model.id.clone(),
- response_model: None,
- response_id: None,
- diagnostics: None,
- usage: empty_usage(),
- stop_reason: StopReason::Stop,
- stop_reason_raw: None,
- error_message: None,
- timestamp: crate::now_ms(),
- },
- partial_json: HashMap::new(),
- };
-
- let result = proxy_run(
- &proxy_options,
- &options,
- model,
- context,
- &mut reconstruction,
- &task_handle,
- &signal,
- )
- .await;
-
- // Encode the failure as a terminal error event; the catch settles
- // partial tool calls first.
- match result {
- Ok(()) => {
- if signal.is_aborted() {
- settle_partial_tool_calls(&mut reconstruction);
- reconstruction.partial.stop_reason = StopReason::Aborted;
- reconstruction.partial.error_message =
- Some("Request aborted by user".to_string());
- task_handle.push(AssistantMessageEvent::Error {
- reason: StopReason::Aborted,
- error: reconstruction.partial.clone(),
- });
- }
- task_handle.end(None);
- }
- Err(error_message) => {
- let reason = if signal.is_aborted() {
- StopReason::Aborted
- } else {
- StopReason::Error
- };
- settle_partial_tool_calls(&mut reconstruction);
- reconstruction.partial.stop_reason = reason;
- reconstruction.partial.error_message = Some(error_message);
- task_handle.push(AssistantMessageEvent::Error {
- reason,
- error: reconstruction.partial.clone(),
- });
- task_handle.end(None);
- }
- }
- });
-
- (handle, stream)
-}
-
-/// Request body options subset: every field of
-/// [`crate::stream::StreamRequestOptions`] is either serialized here or
-/// explicitly client-local. A new stream option must be classified on both
-/// sides or the proxy transport silently drops it (serviceTier itself was the
-/// drop).
-#[derive(serde::Serialize)]
-#[serde(rename_all = "camelCase")]
-struct ProxyRequestOptions {
- #[serde(skip_serializing_if = "Option::is_none")]
- temperature: Option<f64>,
- #[serde(skip_serializing_if = "Option::is_none")]
- max_tokens: Option<u64>,
- reasoning: ThinkingLevelWire,
- #[serde(skip_serializing_if = "Option::is_none")]
- session_id: Option<String>,
- #[serde(skip_serializing_if = "Option::is_none")]
- service_tier: Option<crate::types::ServiceTier>,
- #[serde(skip_serializing_if = "Option::is_none")]
- headers: Option<std::collections::BTreeMap<String, String>>,
-}
-
-#[derive(serde::Serialize)]
-#[serde(rename_all = "lowercase")]
-enum ThinkingLevelWire {
- Off,
- Minimal,
- Low,
- Medium,
- High,
- Xhigh,
- Max,
-}
-
-async fn proxy_run(
- proxy_options: &ProxyStreamOptions,
- options: &StreamRequestOptions,
- model: Model,
- context: LlmContext,
- reconstruction: &mut ProxyReconstruction,
- handle: &AssistantMessageEventStreamHandle,
- signal: &AbortSignal,
-) -> Result<(), String> {
- let request_options = ProxyRequestOptions {
- temperature: options.temperature,
- max_tokens: options.max_tokens,
- reasoning: match options.reasoning {
- crate::types::ThinkingLevel::Off => ThinkingLevelWire::Off,
- crate::types::ThinkingLevel::Minimal => ThinkingLevelWire::Minimal,
- crate::types::ThinkingLevel::Low => ThinkingLevelWire::Low,
- crate::types::ThinkingLevel::Medium => ThinkingLevelWire::Medium,
- crate::types::ThinkingLevel::High => ThinkingLevelWire::High,
- crate::types::ThinkingLevel::Xhigh => ThinkingLevelWire::Xhigh,
- crate::types::ThinkingLevel::Max => ThinkingLevelWire::Max,
- },
- session_id: options.session_id.clone(),
- service_tier: options.service_tier,
- headers: options.headers.clone(),
- };
- let body = serde_json::json!({
- "model": model,
- "context": context,
- "options": request_options,
- });
-
- let client = &*PROXY_CLIENT;
- let send = async {
- client
- .post(format!("{}/api/stream", proxy_options.proxy_url))
- .header(
- "Authorization",
- format!("Bearer {}", proxy_options.auth_token),
- )
- .header("Content-Type", "application/json")
- .json(&body)
- .send()
- .await
- };
-
- let response = match run_aborting(send, signal).await {
- Ok(Ok(response)) => response,
- Ok(Err(error)) => return Err(format!("{error}")),
- Err(()) => return Err("Request aborted by user".to_string()),
- };
-
- if !response.status().is_success() {
- let status = response.status();
- let mut error_message = format!(
- "Proxy error: {} {}",
- status.as_u16(),
- status.canonical_reason().unwrap_or("")
- );
- if let Ok(payload) = response.json::<serde_json::Value>().await {
- if let Some(error) = payload.get("error").and_then(|v| v.as_str()) {
- error_message = format!("Proxy error: {error}");
- }
- }
- return Err(error_message);
- }
-
- let mut byte_buffer: Vec<u8> = Vec::new();
- let mut stream = response.bytes_stream();
- loop {
- let chunk = async { stream.next().await };
- let chunk = match run_aborting(chunk, signal).await {
- Err(()) => return Err("Request aborted by user".to_string()),
- Ok(None) => break,
- Ok(Some(Err(error))) => return Err(format!("{error}")),
- Ok(Some(Ok(bytes))) => bytes,
- };
-
- byte_buffer.extend_from_slice(&chunk);
- // Split on newline; keep the trailing partial line in the buffer.
- while let Some(position) = byte_buffer.iter().position(|b| *b == b'\n') {
- let line: Vec<u8> = byte_buffer.drain(..=position).collect();
- let line = String::from_utf8_lossy(&line[..line.len() - 1]).to_string();
- if let Some(data) = line.strip_prefix("data: ") {
- let data = data.trim();
- if !data.is_empty() {
- let Ok(proxy_event) = serde_json::from_str::<ProxyAssistantMessageEvent>(data)
- else {
- return Err(format!("Invalid proxy event: {data}"));
- };
- match process_proxy_event(proxy_event, reconstruction) {
- Ok(Some(event)) => handle.push(event),
- Ok(None) => {}
- Err(message) => return Err(message),
- }
- }
- }
- }
- }
-
- if signal.is_aborted() {
- return Err("Request aborted by user".to_string());
- }
-
- // Clean EOF without a terminal event.
- Ok(())
-}
-
-async fn run_aborting<T>(
- future: impl std::future::Future<Output = T>,
- signal: &AbortSignal,
-) -> Result<T, ()> {
- tokio::select! {
- value = future => Ok(value),
- () = signal.aborted() => Err(()),
- }
-}
-
-/// Mutate the reconstructed partial message and return the full assistant
-/// event to push. `Err` mirrors the TS `throw`s for mismatched content types.
-// One arm per proxy event kind, mirroring the TS switch.
-#[allow(clippy::too_many_lines)]
-fn process_proxy_event(
- proxy_event: ProxyAssistantMessageEvent,
- state: &mut ProxyReconstruction,
-) -> Result<Option<AssistantMessageEvent>, String> {
- fn ensure_content(partial: &mut AssistantMessage, index: usize) -> usize {
- if partial.content.len() < index + 1 {
- partial.content.resize(
- index + 1,
- AssistantContent::Text(TextContent {
- text: String::new(),
- text_signature: None,
- }),
- );
- }
- index
- }
- let partial = &mut state.partial;
-
- match proxy_event {
- ProxyAssistantMessageEvent::Start => Ok(Some(AssistantMessageEvent::Start {
- partial: partial.clone(),
- })),
- ProxyAssistantMessageEvent::TextStart { content_index } => {
- ensure_content(partial, content_index);
- partial.content[content_index] = AssistantContent::Text(TextContent {
- text: String::new(),
- text_signature: None,
- });
- Ok(Some(AssistantMessageEvent::TextStart {
- content_index,
- partial: partial.clone(),
- }))
- }
- ProxyAssistantMessageEvent::TextDelta {
- content_index,
- delta,
- } => match partial.content.get_mut(content_index) {
- Some(AssistantContent::Text(text)) => {
- text.text.push_str(&delta);
- Ok(Some(AssistantMessageEvent::TextDelta {
- content_index,
- delta,
- partial: partial.clone(),
- }))
- }
- _ => Err("Received text_delta for non-text content".to_string()),
- },
- ProxyAssistantMessageEvent::TextEnd {
- content_index,
- content_signature,
- } => match partial.content.get_mut(content_index) {
- Some(AssistantContent::Text(text)) => {
- text.text_signature = content_signature;
- let content = text.text.clone();
- Ok(Some(AssistantMessageEvent::TextEnd {
- content_index,
- content,
- partial: partial.clone(),
- }))
- }
- _ => Err("Received text_end for non-text content".to_string()),
- },
- ProxyAssistantMessageEvent::ThinkingStart { content_index } => {
- ensure_content(partial, content_index);
- partial.content[content_index] = AssistantContent::Thinking(ThinkingContent {
- thinking: String::new(),
- thinking_signature: None,
- redacted: None,
- });
- Ok(Some(AssistantMessageEvent::ThinkingStart {
- content_index,
- partial: partial.clone(),
- }))
- }
- ProxyAssistantMessageEvent::ThinkingDelta {
- content_index,
- delta,
- } => match partial.content.get_mut(content_index) {
- Some(AssistantContent::Thinking(thinking)) => {
- thinking.thinking.push_str(&delta);
- Ok(Some(AssistantMessageEvent::ThinkingDelta {
- content_index,
- delta,
- partial: partial.clone(),
- }))
- }
- _ => Err("Received thinking_delta for non-thinking content".to_string()),
- },
- ProxyAssistantMessageEvent::ThinkingEnd {
- content_index,
- content_signature,
- } => match partial.content.get_mut(content_index) {
- Some(AssistantContent::Thinking(thinking)) => {
- thinking.thinking_signature = content_signature;
- Ok(Some(AssistantMessageEvent::ThinkingEnd {
- content_index,
- partial: partial.clone(),
- }))
- }
- _ => Err("Received thinking_end for non-thinking content".to_string()),
- },
- ProxyAssistantMessageEvent::ToolcallStart {
- content_index,
- id,
- tool_name,
- } => {
- ensure_content(partial, content_index);
- partial.content[content_index] = AssistantContent::ToolCall(ToolCall {
- id,
- name: tool_name,
- arguments: serde_json::Value::Object(serde_json::Map::new()),
- thought_signature: None,
- });
- state
- .partial_json
- .insert(content_index, StreamingJsonAccumulator::default());
- Ok(Some(AssistantMessageEvent::ToolCallStart {
- content_index,
- partial: partial.clone(),
- }))
- }
- ProxyAssistantMessageEvent::ToolcallDelta {
- content_index,
- delta,
- } => match partial.content.get_mut(content_index) {
- Some(AssistantContent::ToolCall(tool_call)) => {
- let parsed = state
- .partial_json
- .entry(content_index)
- .or_default()
- .append(&delta);
- if let Some(parsed) = parsed {
- tool_call.arguments = parsed;
- }
- Ok(Some(AssistantMessageEvent::ToolCallDelta {
- content_index,
- delta,
- partial: partial.clone(),
- }))
- }
- _ => Err("Received toolcall_delta for non-toolCall content".to_string()),
- },
- ProxyAssistantMessageEvent::ToolcallEnd { content_index } => {
- match partial.content.get_mut(content_index) {
- Some(AssistantContent::ToolCall(tool_call)) => {
- // The throttled preview can lag the final text;
- // `toolcall_end` finalizes the arguments.
- let flushed = state
- .partial_json
- .remove(&content_index)
- .and_then(|mut accumulator| accumulator.flush());
- if let Some(parsed) = flushed {
- tool_call.arguments = parsed;
- }
- Ok(Some(AssistantMessageEvent::ToolCallEnd {
- content_index,
- tool_call: tool_call.clone(),
- partial: partial.clone(),
- }))
- }
- // TS returns undefined for toolcall_end on non-toolCall content.
- _ => Ok(None),
- }
- }
- ProxyAssistantMessageEvent::Done { reason, usage } => {
- partial.stop_reason = match reason {
- ProxyDoneReason::Stop => StopReason::Stop,
- ProxyDoneReason::Length => StopReason::Length,
- ProxyDoneReason::ToolUse => StopReason::ToolUse,
- };
- partial.usage = usage;
- Ok(Some(AssistantMessageEvent::Done {
- reason: partial.stop_reason,
- message: partial.clone(),
- }))
- }
- ProxyAssistantMessageEvent::Error {
- reason,
- error_message,
- usage,
- } => {
- let stop_reason = match reason {
- ProxyErrorReason::Aborted => StopReason::Aborted,
- ProxyErrorReason::Error => StopReason::Error,
- };
- // The throttled preview can lag the accumulated text.
- settle_partial_tool_calls(state);
- state.partial.stop_reason = stop_reason;
- state.partial.error_message = error_message;
- state.partial.usage = usage;
- Ok(Some(AssistantMessageEvent::Error {
- reason: state.partial.stop_reason,
- error: state.partial.clone(),
- }))
- }
- }
-}
-
-/// Finalize tool-call blocks whose parsed preview may lag the accumulated
-/// text under the growth throttle. Error paths call this before the error
-/// event carries the message.
-fn settle_partial_tool_calls(state: &mut ProxyReconstruction) {
- let keys: Vec<usize> = state.partial_json.keys().copied().collect();
- for key in keys {
- let Some(parsed) = state
- .partial_json
- .get_mut(&key)
- .and_then(StreamingJsonAccumulator::flush)
- else {
- continue;
- };
- if let Some(AssistantContent::ToolCall(tool_call)) = state.partial.content.get_mut(key) {
- tool_call.arguments = parsed;
- }
- }
-}
-
-const VALID_JSON_ESCAPES: [char; 8] = ['"', '\\', '/', 'b', 'f', 'n', 'r', 't'];
-
-fn is_control_character(ch: char) -> bool {
- (ch as u32) <= 0x1f
-}
-
-fn escape_control_character(ch: char) -> String {
- match ch {
- '\u{08}' => "\\b".to_string(),
- '\u{0c}' => "\\f".to_string(),
- '\n' => "\\n".to_string(),
- '\r' => "\\r".to_string(),
- '\t' => "\\t".to_string(),
- _ => format!("\\u{:04x}", ch as u32),
- }
-}
-
-/// Escape raw control characters inside strings and double backslashes
-/// before invalid escape characters.
-#[must_use]
-pub fn repair_json(json: &str) -> String {
- let mut repaired = String::new();
- let mut in_string = false;
- let chars: Vec<char> = json.chars().collect();
- let mut index = 0;
- while index < chars.len() {
- let ch = chars[index];
- if !in_string {
- repaired.push(ch);
- if ch == '"' {
- in_string = true;
- }
- index += 1;
- continue;
- }
- if ch == '"' {
- repaired.push(ch);
- in_string = false;
- index += 1;
- continue;
- }
- if ch == '\\' {
- let next_char = chars.get(index + 1);
- match next_char {
- Some('u') => {
- let digits: String = chars[index + 2..(index + 6).min(chars.len())]
- .iter()
- .collect();
- if digits.len() == 4 && digits.chars().all(|c| c.is_ascii_hexdigit()) {
- let _ = write!(repaired, "\\u{digits}");
- index += 6;
- } else {
- repaired.push_str("\\\\");
- index += 1;
- }
- }
- Some(&next) if VALID_JSON_ESCAPES.contains(&next) => {
- repaired.push('\\');
- repaired.push(next);
- index += 2;
- }
- None | Some(_) => {
- repaired.push_str("\\\\");
- index += 1;
- }
- }
- continue;
- }
- if is_control_character(ch) {
- repaired.push_str(&escape_control_character(ch));
- } else {
- repaired.push(ch);
- }
- index += 1;
- }
- repaired
-}
-
-fn parse_json_with_repair(json: &str) -> Result<serde_json::Value, ()> {
- serde_json::from_str(json).map_err(|_| ()).or_else(|()| {
- let repaired = repair_json(json);
- if repaired == json {
- Err(())
- } else {
- serde_json::from_str(&repaired).map_err(|_| ())
- }
- })
-}
-
-#[derive(Clone, Copy, PartialEq)]
-enum ContainerKind {
- Object,
- Array,
-}
-
-#[derive(Clone, Copy, PartialEq)]
-enum Expect {
- KeyOrValue,
- Colon,
- Value,
- AfterValue,
-}
-
-/// Completes a truncated JSON document by closing open strings and containers,
-/// filling missing values with `null` and dropping dangling separators - the
-/// same results the TS `partial-json` library produces.
-// State machine over the input.
-#[allow(clippy::too_many_lines)]
-fn complete_partial_json(input: &str) -> String {
- let mut stack: Vec<ContainerKind> = Vec::new();
- let mut expects: Vec<Expect> = Vec::new();
- let mut in_string = false;
- let mut escaping = false;
- let mut literal_start: Option<usize> = None;
- let mut last_comma: Option<usize> = None;
-
- for (position, ch) in input.char_indices() {
- if in_string {
- if escaping {
- escaping = false;
- } else if ch == '\\' {
- escaping = true;
- } else if ch == '"' {
- in_string = false;
- if let Some(top) = stack.last() {
- let top_kind = *top;
- let top_expect = expects.last().copied();
- match (top_kind, top_expect) {
- (ContainerKind::Object, Some(Expect::KeyOrValue)) => {
- *expects.last_mut().unwrap() = Expect::Colon;
- }
- _ => {
- *expects.last_mut().unwrap() = Expect::AfterValue;
- }
- }
- }
- }
- continue;
- }
- match ch {
- '"' => {
- in_string = true;
- literal_start = None;
- }
- '{' => {
- stack.push(ContainerKind::Object);
- expects.push(Expect::KeyOrValue);
- literal_start = None;
- }
- '[' => {
- stack.push(ContainerKind::Array);
- expects.push(Expect::Value);
- literal_start = None;
- }
- '}' | ']' => {
- stack.pop();
- expects.pop();
- if let Some(expect) = expects.last_mut() {
- *expect = Expect::AfterValue;
- }
- literal_start = None;
- }
- ',' => {
- last_comma = Some(position);
- if let Some(expect) = expects.last_mut() {
- let kind = *stack.last().unwrap_or(&ContainerKind::Array);
- *expect = if kind == ContainerKind::Object {
- Expect::KeyOrValue
- } else {
- Expect::Value
- };
- }
- literal_start = None;
- }
- ':' => {
- if let Some(expect) = expects.last_mut() {
- if *expect == Expect::Colon {
- *expect = Expect::Value;
- }
- }
- literal_start = None;
- }
- c if c.is_ascii_digit()
- || c == '-'
- || literal_start.is_some()
- && (c == '.'
- || c == 'e'
- || c == 'E'
- || c == '+'
- || c.is_ascii_alphanumeric()) =>
- {
- if literal_start.is_none() {
- literal_start = Some(position);
- }
- }
- _ => {
- if !ch.is_whitespace() && literal_start.is_some() {
- // literal like true/false/null continues
- }
- }
- }
- }
-
- let mut completed = input.to_string();
-
- // Close an unterminated string.
- if in_string {
- if escaping {
- completed.pop(); // drop the dangling backslash
- }
- completed.push('"');
- // The closed string is either a key or a value; replicate the close
- // handling from the scan loop.
- if let Some(top) = stack.last() {
- let top_kind = *top;
- match (top_kind, expects.last().copied()) {
- (ContainerKind::Object, Some(Expect::KeyOrValue)) => {
- *expects.last_mut().unwrap() = Expect::Colon;
- }
- _ => {
- *expects.last_mut().unwrap() = Expect::AfterValue;
- }
- }
- }
- } else if let Some(literal_start) = literal_start {
- // Possibly truncated number; strip trailing invalid characters.
- let literal = &input[literal_start..];
- let trimmed = literal.trim_end_matches(['.', 'e', 'E', '+', '-']);
- completed = format!("{}{}", &input[..literal_start], trimmed);
- if let Some(expect) = expects.last_mut() {
- *expect = Expect::AfterValue;
- }
- }
-
- while let Some(kind) = stack.pop() {
- let expect = expects.pop().unwrap_or(Expect::AfterValue);
- match (kind, expect) {
- (_, Expect::Colon) => completed.push_str(": null"),
- (_, Expect::Value) => {
- // Array/object expecting a value: a dangling comma is invalid.
- if completed.trim_end().ends_with(',') {
- completed = completed.trim_end().trim_end_matches(',').to_string();
- } else if kind == ContainerKind::Object {
- completed.push_str("null");
- }
- }
- (_, Expect::KeyOrValue | Expect::AfterValue) => {}
- }
- completed.push(match kind {
- ContainerKind::Object => '}',
- ContainerKind::Array => ']',
- });
- let _ = last_comma.take();
- }
-
- completed
-}
-
-/// Parse potentially incomplete JSON, always returning a value (empty object
-/// on total failure).
-#[must_use]
-pub fn parse_streaming_json(partial_json: &str) -> serde_json::Value {
- if partial_json.trim().is_empty() {
- return serde_json::Value::Object(serde_json::Map::new());
- }
-
- if let Ok(value) = parse_json_with_repair(partial_json) {
- return value;
- }
- for candidate in [partial_json.to_string(), repair_json(partial_json)] {
- let completed = complete_partial_json(&candidate);
- if let Ok(value) = serde_json::from_str(&completed) {
- return value;
- }
- }
- serde_json::Value::Object(serde_json::Map::new())
-}
-
-const EAGER_PARSE_LENGTH: usize = 8 * 1024;
-
-/// Streamed tool-call argument JSON with a best-effort parsed preview
-/// (`pa-agent` keeps its own copy of the json-parse utilities because it
-/// does not depend on `pa-ai`). Re-parsing the whole buffer on every delta
-/// is quadratic, so past `EAGER_PARSE_LENGTH` the preview is refreshed only
-/// after the buffer grew by 1/16.
-struct StreamingJsonAccumulator {
- text: String,
- /// Buffer length in UTF-16 code units, the metric of the TS reference
- /// (`String::length`); maintained incrementally so `append` stays O(delta).
- len_utf16: usize,
- parsed_length: usize,
-}
-
-impl StreamingJsonAccumulator {
- fn new(text: impl Into<String>) -> Self {
- let text = text.into();
- let len_utf16 = text.chars().map(char::len_utf16).sum();
- Self {
- text,
- len_utf16,
- parsed_length: 0,
- }
- }
-
- /// Appends a delta and returns a fresh partial parse, or `None` while the
- /// refresh is throttled (the caller keeps the previous preview).
- fn append(&mut self, delta: &str) -> Option<serde_json::Value> {
- self.text.push_str(delta);
- self.len_utf16 += delta.chars().map(char::len_utf16).sum::<usize>();
- let length = self.len_utf16;
- // `length - parsed_length < parsed_length / 16` in exact integer form.
- if length > EAGER_PARSE_LENGTH && 16 * (length - self.parsed_length) < self.parsed_length {
- return None;
- }
- Some(self.parse())
- }
-
- /// Parses text not covered by the last returned parse; `None` when the
- /// preview is already current.
- fn flush(&mut self) -> Option<serde_json::Value> {
- (self.parsed_length != self.len_utf16).then(|| self.parse())
- }
-
- fn parse(&mut self) -> serde_json::Value {
- self.parsed_length = self.len_utf16;
- parse_streaming_json(&self.text)
- }
-}
-
-impl Default for StreamingJsonAccumulator {
- fn default() -> Self {
- Self::new(String::new())
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::abort::AbortSignal;
- use crate::stream::{LlmContext, ModelStream, StreamRequestOptions};
- use crate::types::ThinkingLevel;
- use std::time::Duration;
- use tokio::io::{AsyncReadExt, AsyncWriteExt};
- use tokio::net::TcpListener;
-
- /// Serve the SSE body for the proxy's POST. `truncate` claims a larger
- /// Content-Length than the bytes actually sent, so the client's body
- /// stream errors mid-response.
- async fn serve_proxy_sse(body: String, truncate: bool) -> String {
- let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
- let addr = listener.local_addr().unwrap();
- tokio::spawn(async move {
- let (mut socket, _) = listener.accept().await.unwrap();
- let mut request = Vec::new();
- let mut chunk = [0u8; 4096];
- let headers_end = loop {
- let read = socket.read(&mut chunk).await.unwrap();
- request.extend_from_slice(&chunk[..read]);
- if let Some(position) = request.windows(4).position(|w| w == b"\r\n\r\n") {
- break position + 4;
- }
- };
- let headers = String::from_utf8_lossy(&request[..headers_end]).to_string();
- let content_length = headers
- .lines()
- .find_map(|line| {
- let (name, value) = line.split_once(':')?;
- if !name.eq_ignore_ascii_case("content-length") {
- return None;
- }
- value.trim().parse::<usize>().ok()
- })
- .unwrap_or(0);
- while request.len() < headers_end + content_length {
- let read = socket.read(&mut chunk).await.unwrap();
- if read == 0 {
- break;
- }
- request.extend_from_slice(&chunk[..read]);
- }
- let claimed = if truncate {
- body.len() + 8192
- } else {
- body.len()
- };
- let head = format!(
- "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {claimed}\r\nConnection: close\r\n\r\n"
- );
- socket.write_all(head.as_bytes()).await.unwrap();
- socket.write_all(body.as_bytes()).await.unwrap();
- let _ = socket.shutdown().await;
- });
- format!("http://{addr}")
- }
-
- fn proxy_options(proxy_url: String) -> ProxyStreamOptions {
- ProxyStreamOptions {
- auth_token: "test-token".to_string(),
- proxy_url,
- signal: AbortSignal::never(),
- }
- }
-
- fn request_options() -> StreamRequestOptions {
- StreamRequestOptions {
- temperature: None,
- max_tokens: None,
- reasoning: ThinkingLevel::Off,
- session_id: None,
- service_tier: None,
- api_key: None,
- signal: AbortSignal::never(),
- on_payload: None,
- on_response: None,
- headers: None,
- }
- }
-
- /// 20 KiB arguments in 16-byte deltas stay past the eager-parse
- /// threshold, so the per-delta preview lags; only the `toolcall_end`
- /// flush (ends cleanly) or the error settle carries the full parse.
- async fn finalize_throttled_tool_call_arguments(ends_cleanly: bool) {
- let arguments = serde_json::json!({"content": "x".repeat(20 * 1024)}).to_string();
- let mut events = vec![
- r#"{"type":"start"}"#.to_string(),
- r#"{"type":"toolcall_start","contentIndex":0,"id":"call","toolName":"write"}"#
- .to_string(),
- ];
- for delta in arguments.as_bytes().chunks(16) {
- let delta = String::from_utf8_lossy(delta);
- events.push(
- serde_json::json!({"type": "toolcall_delta", "contentIndex": 0, "delta": delta})
- .to_string(),
- );
- }
- if ends_cleanly {
- events.push(r#"{"type":"toolcall_end","contentIndex":0}"#.to_string());
- events.push(r#"{"type":"done","reason":"toolUse","usage":{}}"#.to_string());
- }
- let mut body = String::new();
- for event in &events {
- use std::fmt::Write as _;
- let _ = write!(body, "data: {event}\n\n");
- }
-
- let base_url = serve_proxy_sse(body, !ends_cleanly).await;
- let (_handle, mut stream) = stream_proxy(
- test_model(base_url.clone()),
- LlmContext {
- system_prompt: None,
- messages: Vec::new(),
- tools: Vec::new(),
- },
- request_options(),
- proxy_options(base_url),
- );
-
- let final_message = tokio::time::timeout(Duration::from_secs(60), async {
- loop {
- let Some(event) = stream.next_event().await else {
- break None;
- };
- if let Some(message) = event.terminal_message() {
- break Some(message.clone());
- }
- }
- })
- .await
- .expect("terminal event within the timeout");
- let message = final_message.expect("terminal done/error event");
- let expected_stop_reason = if ends_cleanly {
- StopReason::ToolUse
- } else {
- StopReason::Error
- };
- assert_eq!(message.stop_reason, expected_stop_reason);
- let Some(AssistantContent::ToolCall(tool_call)) = message.content.first() else {
- panic!("expected a tool call");
- };
- assert_eq!(
- tool_call.arguments,
- serde_json::json!({"content": "x".repeat(20 * 1024)})
- );
- }
-
- fn test_model(base_url: String) -> Model {
- Model {
- id: "proxy-model".into(),
- name: "Proxy Model".into(),
- api: "proxy".into(),
- provider: "proxy".into(),
- base_url,
- reasoning: false,
- cost: crate::types::UsageCost::default(),
- context_window: 128_000,
- max_tokens: 8_192,
- max_tokens_explicit: false,
- }
- }
-
- #[tokio::test]
- async fn finalizes_throttled_large_tool_call_arguments_when_the_call_ends_cleanly() {
- finalize_throttled_tool_call_arguments(true).await;
- }
-
- #[tokio::test]
- async fn finalizes_throttled_large_tool_call_arguments_when_the_stream_fails_mid_call() {
- finalize_throttled_tool_call_arguments(false).await;
- }
-
- use crate::types::ServiceTier;
-
- /// Serve one `POST /api/stream` on a local listener, capture the
- /// request body, and answer with `sse`; returns the proxy URL base and
- /// the captured body.
- fn spawn_proxy_stub(
- sse: &'static str,
- ) -> (String, std::sync::mpsc::Receiver<serde_json::Value>) {
- let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind proxy stub");
- let addr = listener.local_addr().expect("proxy stub addr");
- let (tx, rx) = std::sync::mpsc::channel();
- std::thread::spawn(move || {
- let (mut stream, _) = listener.accept().expect("accept proxy request");
- // 64 KiB read buffer on a dedicated stub thread is fine for a test.
- #[allow(clippy::large_stack_arrays)]
- let mut buffer = [0u8; 64 * 1024];
- let mut request = String::new();
- loop {
- let n = std::io::Read::read(&mut stream, &mut buffer).expect("read request");
- if n == 0 {
- break;
- }
- request.push_str(&String::from_utf8_lossy(&buffer[..n]));
- // The terminator may not be in this read yet; only inspect
- // the content length once it is.
- if let Some(header_end) = request.find("\r\n\r\n") {
- if let Some(len) = content_length(&request[..header_end]) {
- if request.len() - header_end - 4 >= len {
- break;
- }
- }
- }
- }
- let header_end = request.find("\r\n\r\n").expect("headers terminator");
- let body: serde_json::Value =
- serde_json::from_str(&request[header_end + 4..]).expect("request body");
- tx.send(body).expect("send captured body");
- let reply = format!(
- "HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{sse}",
- sse.len()
- );
- std::io::Write::write_all(&mut stream, reply.as_bytes()).expect("write sse");
- });
- (format!("http://{addr}"), rx)
- }
-
- fn content_length(header_text: &str) -> Option<usize> {
- for line in header_text.lines() {
- if let Some((name, value)) = line.split_once(':') {
- if name.trim().eq_ignore_ascii_case("content-length") {
- return value.trim().parse().ok();
- }
- }
- }
- None
- }
-
- fn proxy_tier_model() -> Model {
- serde_json::from_value(serde_json::json!({
- "id": "mock-1", "name": "Mock 1", "api": "openai-completions",
- "provider": "prime-inference", "baseUrl": "http://127.0.0.1:9/v1",
- "reasoning": false, "input": [], "contextWindow": 1000, "maxTokens": 100,
- "cost": { "input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0 }
- }))
- .expect("test model")
- }
-
- fn context() -> LlmContext {
- LlmContext::default()
- }
-
- const DONE_SSE: &str = "data: {\"type\":\"start\"}\n\ndata: {\"type\":\"done\",\"reason\":\"stop\",\"usage\":{\"input\":1,\"output\":2,\"cacheRead\":0,\"cacheWrite\":0,\"totalTokens\":3,\"cost\":{\"input\":0,\"output\":0,\"cacheRead\":0,\"cacheWrite\":0,\"total\":0}}}\n\n";
-
- #[tokio::test]
- async fn serializes_service_tier_into_the_proxy_request() {
- let (proxy_url, rx) = spawn_proxy_stub(DONE_SSE);
- let (handle, mut stream) = stream_proxy(
- proxy_tier_model(),
- context(),
- StreamRequestOptions {
- service_tier: Some(ServiceTier::Priority),
- ..Default::default()
- },
- ProxyStreamOptions {
- auth_token: "token".to_string(),
- proxy_url,
- signal: AbortSignal::never(),
- },
- );
- let _ = stream.result().await;
- handle.end(None);
- let body = rx.recv().expect("stub captured request");
- assert_eq!(
- body["options"]["serviceTier"],
- serde_json::json!("priority"),
- "the requested tier must reach the proxy request"
- );
- }
-
- /// An unset tier stays off the wire.
- #[tokio::test]
- async fn without_a_tier_the_proxy_request_omits_it() {
- let (proxy_url, rx) = spawn_proxy_stub(DONE_SSE);
- let (handle, mut stream) = stream_proxy(
- proxy_tier_model(),
- context(),
- StreamRequestOptions::default(),
- ProxyStreamOptions {
- auth_token: "token".to_string(),
- proxy_url,
- signal: AbortSignal::never(),
- },
- );
- let _ = stream.result().await;
- handle.end(None);
- let body = rx.recv().expect("stub captured request");
- assert!(body["options"].get("serviceTier").is_none());
- }
-
- /// The serialized option set is total: adding a stream option without
- /// classifying it shows up here (TS's compile-time
- /// `PROXY_SERIALIZED_OPTIONS` guard, pinned as a test).
- #[test]
- fn proxy_request_options_stay_total() {
- let options = ProxyRequestOptions {
- temperature: Some(0.5),
- max_tokens: Some(128),
- reasoning: ThinkingLevelWire::High,
- session_id: Some("session".to_string()),
- service_tier: Some(ServiceTier::Flex),
- headers: Some([("X-ACP-Model-Request-ID".to_string(), "id".to_string())].into()),
- };
- let value = serde_json::to_value(&options).expect("serialize");
- let keys: std::collections::BTreeSet<String> =
- value.as_object().expect("object").keys().cloned().collect();
- assert_eq!(
- keys,
- [
- "headers",
- "maxTokens",
- "reasoning",
- "serviceTier",
- "sessionId",
- "temperature"
- ]
- .into_iter()
- .map(String::from)
- .collect::<std::collections::BTreeSet<_>>()
- );
- }
-}
diff --git a/crates/pa-agent/src/stream.rs b/crates/pa-agent/src/stream.rs
index c9de875996..b87c54b1c7 100644
--- a/crates/pa-agent/src/stream.rs
+++ b/crates/pa-agent/src/stream.rs
@@ -131,12 +131,7 @@ pub type OnPayloadHook =
pub type OnResponseHook = std::sync::Arc<dyn Fn(ProviderResponse, &Model) + Send + Sync>;
/// Stream request options (subset of the TS `SimpleStreamOptions` the loop
-/// uses, plus the request hooks the TS options carry). Every option is
-/// either serialized into the proxy request (`temperature`, `max_tokens`,
-/// `reasoning`, `session_id`, `service_tier`, `headers` — see
-/// [`crate::proxy`]) or client-local (`api_key`, `signal`); TS
-/// `PROXY_SERIALIZED_OPTIONS` marks the same classification so a new
-/// shared option cannot be silently dropped by the proxy transport.
+/// uses, plus the request hooks the TS options carry).
#[derive(Clone)]
pub struct StreamRequestOptions {
pub temperature: Option<f64>,
diff --git a/crates/pa-agent/src/types.rs b/crates/pa-agent/src/types.rs
index bd7d06f925..f8dada2b63 100644
--- a/crates/pa-agent/src/types.rs
+++ b/crates/pa-agent/src/types.rs
@@ -1,7 +1,7 @@
//! Domain types for the agent loop.
//!
//! Serde field names use camelCase so serialized messages match the TS wire
-//! format exactly (important for the proxy protocol and session JSONL parity).
+//! format exactly (important for session JSONL parity).
use serde::{Deserialize, Serialize};
use std::sync::Arc;
diff --git a/crates/pa-agent/tests/proxy_extra_ca.rs b/crates/pa-agent/tests/proxy_extra_ca.rs
deleted file mode 100644
index c94a55ea0a..0000000000
--- a/crates/pa-agent/tests/proxy_extra_ca.rs
+++ /dev/null
@@ -1,169 +0,0 @@
-//! A dedicated binary keeps the proxy's once-only CA cache isolated from other tests.
-
-use std::sync::Arc;
-use std::time::Duration;
-
-use pa_agent::abort::AbortSignal;
-use pa_agent::proxy::{stream_proxy, ProxyStreamOptions};
-use pa_agent::stream::{LlmContext, ModelStream, StreamRequestOptions};
-use pa_agent::types::{Model, StopReason};
-use rcgen::{BasicConstraints, CertificateParams, IsCa, Issuer, KeyPair};
-use tokio::io::{AsyncReadExt, AsyncWriteExt};
-use tokio::net::TcpListener;
-use tokio_rustls::rustls::pki_types::PrivateKeyDer;
-use tokio_rustls::rustls::ServerConfig;
-use tokio_rustls::TlsAcceptor;
-
-struct FixtureEnvironment(Vec<(&'static str, Option<std::ffi::OsString>)>);
-
-impl Drop for FixtureEnvironment {
- fn drop(&mut self) {
- for (name, previous) in self.0.drain(..) {
- match previous {
- Some(value) => std::env::set_var(name, value),
- None => std::env::remove_var(name),
- }
- }
- }
-}
-
-#[tokio::test]
-// Keep setup, the untrusted control, and the public-path assertion together so
-// the single test binary owns every socket and environment mutation it creates.
-#[allow(clippy::too_many_lines)]
-async fn the_default_proxy_stream_honors_node_extra_ca_certs() {
- let mut ca_params = CertificateParams::new(Vec::<String>::new()).expect("CA params");
- ca_params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
- let ca_key = KeyPair::generate().expect("CA key");
- let ca_cert = ca_params.self_signed(&ca_key).expect("CA cert");
- let leaf_params = CertificateParams::new(vec!["127.0.0.1".to_string()]).expect("leaf params");
- let leaf_key = KeyPair::generate().expect("leaf key");
- let leaf_cert = leaf_params
- .signed_by(&leaf_key, &Issuer::from_params(&ca_params, &ca_key))
- .expect("leaf cert");
- let config = ServerConfig::builder()
- .with_no_client_auth()
- .with_single_cert(
- vec![leaf_cert.der().clone()],
- PrivateKeyDer::Pkcs8(leaf_key.serialize_der().into()),
- )
- .expect("TLS config");
- let acceptor = TlsAcceptor::from(Arc::new(config));
- let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
- let proxy_url = format!("https://{}", listener.local_addr().expect("address"));
-
- let server = tokio::spawn(async move {
- // The plain-client control must reject this CA, which is absent from
- // both the OS store and the bundled public roots.
- let (socket, _) = listener.accept().await.expect("control connection");
- assert!(acceptor.accept(socket).await.is_err());
- let (socket, _) = listener.accept().await.expect("proxy connection");
- let mut stream = acceptor.accept(socket).await.expect("proxy TLS handshake");
- let mut request = Vec::new();
- let mut buffer = [0; 4096];
- loop {
- let count = stream.read(&mut buffer).await.expect("request read");
- assert!(count > 0, "proxy closed before sending its request");
- request.extend_from_slice(&buffer[..count]);
- if let Some(end) = request.windows(4).position(|bytes| bytes == b"\r\n\r\n") {
- let headers = String::from_utf8_lossy(&request[..end]).to_ascii_lowercase();
- let length: usize = headers
- .lines()
- .find_map(|line| line.strip_prefix("content-length: "))
- .expect("content length")
- .parse()
- .expect("numeric content length");
- if request.len() >= end + 4 + length {
- assert!(headers.starts_with("post /api/stream http/1.1\r\n"));
- assert!(headers
- .lines()
- .any(|line| line == "authorization: bearer fixture-token"));
- break;
- }
- }
- }
- let sse = "data: {\"type\":\"start\"}\n\ndata: {\"type\":\"done\",\"reason\":\"stop\",\"usage\":{}}\n\n";
- let response = format!(
- "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{sse}",
- sse.len()
- );
- stream
- .write_all(response.as_bytes())
- .await
- .expect("response");
- stream.flush().await.expect("flush");
- });
-
- let bundle = tempfile::NamedTempFile::new().expect("CA bundle");
- // Node retains the trusted prefix when a later PEM certificate has invalid DER.
- let pem = format!(
- "{}-----BEGIN CERTIFICATE-----\nAQID\n-----END CERTIFICATE-----\n",
- ca_cert.pem()
- );
- std::fs::write(bundle.path(), pem).expect("write CA");
- let _environment = FixtureEnvironment(
- [
- "NODE_EXTRA_CA_CERTS",
- "HTTP_PROXY",
- "http_proxy",
- "HTTPS_PROXY",
- "https_proxy",
- "ALL_PROXY",
- "all_proxy",
- "NO_PROXY",
- "no_proxy",
- ]
- .into_iter()
- .map(|name| (name, std::env::var_os(name)))
- .collect(),
- );
- std::env::set_var("NODE_EXTRA_CA_CERTS", bundle.path());
- // Exercise the real default client while keeping inherited corporate
- // proxies from sending this fixture's requests outside the loopback server.
- std::env::set_var("NO_PROXY", "127.0.0.1,localhost");
- std::env::set_var("no_proxy", "127.0.0.1,localhost");
- for name in [
- "HTTP_PROXY",
- "http_proxy",
- "HTTPS_PROXY",
- "https_proxy",
- "ALL_PROXY",
- "all_proxy",
- ] {
- std::env::remove_var(name);
- }
-
- tokio::time::timeout(Duration::from_secs(10), async {
- let error = reqwest::Client::builder()
- .no_proxy()
- .build()
- .expect("plain client")
- .get(&proxy_url)
- .send()
- .await
- .expect_err("plain reqwest must reject the fixture CA");
- assert!(error.is_connect(), "control must fail during TLS: {error}");
- let model: Model = serde_json::from_value(serde_json::json!({
- "id": "fixture", "name": "Fixture", "api": "proxy", "provider": "proxy",
- "baseUrl": proxy_url, "reasoning": false, "contextWindow": 1000, "maxTokens": 100,
- "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}
- }))
- .expect("model");
- let (_handle, mut events) = stream_proxy(
- model,
- LlmContext::default(),
- StreamRequestOptions::default(),
- ProxyStreamOptions {
- auth_token: "fixture-token".to_string(),
- proxy_url,
- signal: AbortSignal::never(),
- },
- );
- let result = events.result().await.expect("terminal event");
- assert_eq!(result.stop_reason, StopReason::Stop, "{result:?}");
- assert_eq!(result.error_message, None);
- server.await.expect("TLS fixture completed");
- })
- .await
- .expect("TLS proxy completes within the timeout");
-}
diff --git a/crates/pa-core/src/tools/code_preview.rs b/crates/pa-core/src/tools/code_preview.rs
deleted file mode 100644
index 5d26bfd1d3..0000000000
--- a/crates/pa-core/src/tools/code_preview.rs
+++ /dev/null
@@ -1,535 +0,0 @@
-//! Bash/Python command preview extraction for tool-call rendering: regexes keep
-//! JavaScript semantics (whitespace/word classes, UTF-16 string indexing).
-
-use crate::tools::code_preview_python::preview_python_code;
-#[cfg(test)]
-use crate::tools::code_preview_python::{preview_ipython_code, python_statement_lines};
-
-const DESCRIPTOR_MAX_WIDTH: usize = 64;
-
-/// JavaScript backslash-s character class.
-pub(crate) const S: &str = r"[\t\n\x0B\f\r \u{00A0}\u{1680}\u{2000}-\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{FEFF}]";
-/// JavaScript backslash-w character class.
-pub(crate) const W: &str = r"[A-Za-z0-9_]";
-
-pub(crate) struct Rx {
- inner: fancy_regex::Regex,
-}
-
-impl Rx {
- /// Compile a preview regex; errors abort (patterns are compile-time constants).
- pub(crate) fn new(pattern: &str) -> Self {
- Rx {
- inner: fancy_regex::Regex::new(pattern).expect("code-preview regex must compile"),
- }
- }
-
- pub(crate) fn is_match(&self, text: &str) -> bool {
- self.inner.is_match(text).unwrap_or(false)
- }
-
- pub(crate) fn captures<'t>(&self, text: &'t str) -> Option<fancy_regex::Captures<'t>> {
- self.inner.captures(text).ok().flatten()
- }
-
- pub(crate) fn captures_iter<'t>(
- &'t self,
- text: &'t str,
- ) -> impl Iterator<Item = fancy_regex::Captures<'t>> + 't {
- self.inner.captures_iter(text).flatten()
- }
-
- pub(crate) fn find<'t>(&self, text: &'t str) -> Option<fancy_regex::Match<'t>> {
- self.inner.find(text).ok().flatten()
- }
-
- pub(crate) fn replace(&self, text: &str, rep: &str) -> String {
- self.inner.replace(text, rep).into_owned()
- }
-
- pub(crate) fn replace_all(&self, text: &str, rep: &str) -> String {
- self.inner.replace_all(text, rep).into_owned()
- }
-
- pub(crate) fn split<'t>(&'t self, text: &'t str) -> Vec<&'t str> {
- self.inner.split(text).filter_map(Result::ok).collect()
- }
-}
-
-pub(crate) fn re(pattern: &str) -> Rx {
- Rx::new(pattern)
-}
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum CodePreviewLanguage {
- Bash,
- Python,
-}
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct CodePreview {
- pub language: CodePreviewLanguage,
- pub text: String,
-}
-
-impl CodePreview {
- fn bash(text: impl Into<String>) -> Self {
- CodePreview {
- language: CodePreviewLanguage::Bash,
- text: text.into(),
- }
- }
-
- pub(crate) fn python(text: impl Into<String>) -> Self {
- CodePreview {
- language: CodePreviewLanguage::Python,
- text: text.into(),
- }
- }
-}
-
-/// JS String.prototype.trim whitespace set.
-fn is_js_whitespace(ch: char) -> bool {
- matches!(
- ch,
- '\u{09}'
- ..='\u{0D}'
- | ' '
- | '\u{00A0}'
- | '\u{1680}'
- | '\u{2028}'
- | '\u{2029}'
- | '\u{202F}'
- | '\u{205F}'
- | '\u{3000}'
- | '\u{FEFF}'
- ) || ('\u{2000}'..='\u{200A}').contains(&ch)
-}
-
-pub(crate) fn js_trim(text: &str) -> &str {
- text.trim_matches(is_js_whitespace)
-}
-
-pub(crate) fn js_trim_end(s: &str) -> &str {
- s.trim_end_matches(is_js_whitespace)
-}
-
-/// Number of UTF-16 code units (JS String.length).
-fn utf16_len(s: &str) -> usize {
- s.chars().map(char::len_utf16).sum()
-}
-
-/// JS s.slice(0, n): first n UTF-16 code units.
-fn utf16_slice_prefix(s: &str, n: usize) -> String {
- let mut units = 0usize;
- for (idx, ch) in s.char_indices() {
- if units + ch.len_utf16() > n {
- return s[..idx].to_string();
- }
- units += ch.len_utf16();
- }
- s.to_string()
-}
-
-pub(crate) fn collapse_whitespace(text: &str) -> String {
- re(&format!(r"{S}+")).replace_all(text, " ")
-}
-
-fn truncate_descriptor(text: &str) -> String {
- if utf16_len(text) <= DESCRIPTOR_MAX_WIDTH {
- return text.to_string();
- }
- let cut = utf16_slice_prefix(text, DESCRIPTOR_MAX_WIDTH - 1);
- format!("{}\u{2026}", js_trim_end(&cut))
-}
-
-/// Mask secrets, blobs, and oversized strings before display.
-pub(crate) fn redact_noise(text: &str) -> String {
- let step1 = re(r"[A-Za-z0-9+/]{80,}={0,2}").replace_all(text, "<blob>");
- let step2 = re(&format!(
- r#"\b((?={W}*(?:token|key|secret|password))[A-Za-z_]{W}*){S}*={S}*(["'])[^"']*\2"#
- ))
- .replace_all(&step1, "$1=<redacted>");
- let step3 = re(&format!(
- r#"(?i)\b((?={W}*(?:token|key|secret|password))[A-Za-z_]{W}*){S}*={S}*(?!<redacted>)(?!["'])\S+"#
- ))
- .replace_all(&step2, "$1=<redacted>");
- let step4 = re(r#"(?i)\b(authorization:\s*(?:bearer\s+)?)[^\s"']+"#)
- .replace_all(&step3, "$1<redacted>");
- let step5 = re(r#"(["'])sk-[^"']+\1"#).replace_all(&step4, "$1<redacted>$1");
- re(r#"(["']).{160,}\1"#).replace_all(&step5, "$1\u{2026}$1")
-}
-
-pub(crate) fn descriptor(text: &str) -> String {
- truncate_descriptor(js_trim(&collapse_whitespace(&redact_noise(text))))
-}
-
-/// Strip a leading ! magic and a leading cd-prefix chain segment.
-pub(crate) fn strip_bash_prefix(line: &str) -> String {
- let no_magic = re(&format!(r"^{S}*!")).replace(line, "");
- let trimmed = js_trim(&no_magic);
- let no_cd = re(&format!(r"^{S}*cd{S}+([^&;|]+)(?:&&|;){S}*")).replace(trimmed, "");
- js_trim(&no_cd).to_string()
-}
-
-pub(crate) fn is_comment_line(line: &str) -> bool {
- re(&format!(r"^{S}*#")).is_match(line)
-}
-
-pub(crate) fn is_skippable_bash_line(line: &str) -> bool {
- let trimmed = js_trim(line);
- trimmed.is_empty()
- || is_comment_line(trimmed)
- || re(&format!(
- r"^{S}*set{S}+[-+][A-Za-z]*(?:{S}+[-+]?{W}+)*(?:{S}+pipefail)?{S}*$"
- ))
- .is_match(trimmed)
- || re(&format!(r"^(?:export{S}+{W}+=|source{S}+\S+|\.{S}+\S+)")).is_match(trimmed)
-}
-
-/// Split a command line into shell-quoted words.
-fn shell_words(line: &str) -> Vec<String> {
- let re_words = re(r#""([^"]*)"|'([^']*)'|(\S+)"#);
- re_words
- .captures_iter(line)
- .filter_map(|c| {
- c.get(1)
- .or_else(|| c.get(2))
- .or_else(|| c.get(3))
- .map(|g| g.as_str().to_string())
- })
- .collect()
-}
-
-/// Drop a leading ./ from a path.
-pub(crate) fn path_tail(path: &str) -> String {
- re(r"^\./").replace(path, "")
-}
-
-/// Shorten well-known runner invocations for display.
-fn simplify_runner_command(line: &str) -> Option<String> {
- let words = shell_words(line);
- let joined = words.join(" ");
- let vitest_index = words
- .iter()
- .position(|w| re(r"(?:^|/)vitest/dist/cli\.js$").is_match(w));
- if words.first().map(String::as_str) == Some("npx")
- && words.get(1).map(String::as_str) == Some("tsx")
- {
- if let Some(vi) = vitest_index.filter(|&i| i >= 2) {
- return Some(
- format!("vitest {}", words[vi + 1..].join(" "))
- .trim()
- .to_string(),
- );
- }
- }
- if words.first().map(String::as_str) == Some("npm") {
- let prefix_index = words.iter().position(|w| w == "--prefix");
- let cwd = prefix_index.and_then(|i| words.get(i + 1)).cloned();
- if let Some(ri) = words.iter().position(|w| w == "run") {
- if let Some(next) = words.get(ri + 1) {
- let command = format!("npm {} {}", next, words[ri + 2..].join(" "))
- .trim()
- .to_string();
- return cwd
- .map(|cwd| format!("{command} ({})", path_tail(&cwd)))
- .or(Some(command));
- }
- }
- }
- if words.first().map(String::as_str) == Some("pnpm") {
- let cwd_index = words.iter().position(|w| w == "-C" || w == "--dir");
- let cwd = cwd_index.and_then(|i| words.get(i + 1)).cloned();
- if let Some(ci) = cwd_index {
- let rest: Vec<String> = words
- .into_iter()
- .enumerate()
- .filter(|(i, _)| *i != ci && *i != ci + 1)
- .map(|(_, w)| w)
- .collect();
- return cwd.map(|cwd| format!("{} ({})", rest.join(" "), path_tail(&cwd)));
- }
- return None;
- }
- // TS findIndex: word === "pytest" (the -m clause is unreachable there).
- if words.first().map(String::as_str) == Some("uv")
- && words.get(1).map(String::as_str) == Some("run")
- {
- if let Some(pi) = words.iter().position(|w| w == "pytest") {
- return Some(
- format!("pytest {}", words[pi + 1..].join(" "))
- .trim()
- .to_string(),
- );
- }
- }
- if matches!(
- words.first().map(String::as_str),
- Some("python" | "python3")
- ) && words.get(1).map(String::as_str) == Some("-m")
- && words.get(2).map(String::as_str) == Some("pytest")
- {
- return Some(
- format!("pytest {}", words[3..].join(" "))
- .trim()
- .to_string(),
- );
- }
- if joined.contains("node_modules/.bin/") {
- return Some(re(r"\S*node_modules/\.bin/").replace_all(&joined, ""));
- }
- None
-}
-
-/// Shorten file-mutation commands (cat >, tee, `apply_patch`) for display.
-fn simplify_mutation_command(line: &str) -> Option<String> {
- let words = shell_words(line);
- if words.is_empty() {
- return None;
- }
- let first = words[0].as_str();
- if first == "cat" && words.get(1).map(String::as_str) == Some(">") && words.len() > 2 {
- return Some(format!("write {}", path_tail(&words[2])));
- }
- if first == "tee" {
- if let Some(last) = words.last() {
- let action = if words.iter().any(|w| w == "-a") {
- "append"
- } else {
- "write"
- };
- return Some(format!("{action} {}", path_tail(last)));
- }
- }
- if first == "apply_patch" {
- return Some("apply patch".to_string());
- }
- if matches!(first, "rm" | "mv" | "cp" | "git" | "npm") {
- return Some(line.to_string());
- }
- if (first == "sed" && words.iter().any(|w| w.starts_with("-i")))
- || (first == "perl" && words.iter().any(|w| w == "-pi"))
- {
- return Some(line.to_string());
- }
- None
-}
-
-pub(crate) fn simplify_bash_command_line(line: &str) -> String {
- simplify_runner_command(line)
- .or_else(|| simplify_mutation_command(line))
- .unwrap_or_else(|| line.to_string())
-}
-
-/// Split a && b; c into its command segments.
-pub(crate) fn split_command_chain(line: &str) -> Vec<String> {
- re(r"\s*(?:&&|;)\s*")
- .split(line)
- .into_iter()
- .map(|p| js_trim(p).to_string())
- .filter(|p| !p.is_empty())
- .collect()
-}
-
-fn heredoc_body(lines: &[String], start_index: usize, delimiter: &str) -> Option<String> {
- // While args stream, preview the partial heredoc body rather than the
- // low-signal heredoc opener.
- let mut body: Vec<&str> = Vec::new();
- for line in lines.iter().skip(start_index + 1) {
- if js_trim(line) == delimiter {
- return Some(body.join("\n"));
- }
- body.push(line);
- }
- if body.is_empty() {
- None
- } else {
- Some(body.join("\n"))
- }
-}
-
-pub(crate) fn preview_heredoc(lines: &[String]) -> Option<CodePreview> {
- // A generic heredoc body is low-signal; keep it as a fallback and prefer a
- // later, more specific heredoc (python/bash/node/write) if one follows.
- let mut fallback: Option<CodePreview> = None;
- for (i, raw) in lines.iter().enumerate() {
- let line = strip_bash_prefix(raw);
- if is_skippable_bash_line(&line) {
- continue;
- }
- let captures = re(r#"<<-?\s*['"]?([A-Za-z_][A-Za-z0-9_]*)['"]?"#).captures(&line);
- let delimiter = captures
- .as_ref()
- .and_then(|c| c.get(1))
- .map(|m| m.as_str().to_string());
- let Some(delimiter) = delimiter else { continue };
- let Some(body) = heredoc_body(lines, i, &delimiter) else {
- continue;
- };
- if re(&format!(r"\b(?:uv{S}+run{S}+)?python3?\b")).is_match(&line) {
- let preview = preview_python_code(&body);
- if !preview.text.is_empty() {
- return Some(preview);
- }
- continue;
- }
- // Match bash/sh as an interpreter word (incl. /bin/sh), not a path suffix like script.sh.
- if re(r"(?<![\w.])(?:bash|sh)\b").is_match(&line) {
- let preview = preview_bash_command(&body);
- if !preview.text.is_empty() {
- return Some(preview);
- }
- return Some(CodePreview::bash(descriptor(&body)));
- }
- if re(r"\bnode\b").is_match(&line) {
- return Some(CodePreview::bash(format!("node: {}", descriptor(&body))));
- }
- let cat_write = re(r"\b(?:cat|tee)\b.*(?:>|\s)(\S+)\s*<<-?")
- .captures(&line)
- .and_then(|c| c.get(1))
- .map(|m| m.as_str().to_string());
- if let Some(target) = cat_write {
- let action = if line.contains("tee -a") {
- "append"
- } else {
- "write"
- };
- return Some(CodePreview::bash(format!(
- "{action} {}",
- path_tail(&target)
- )));
- }
- if re(r"\bapply_patch\b").is_match(&line) {
- return Some(CodePreview::bash("apply patch".to_string()));
- }
- fallback = Some(CodePreview::bash(descriptor(&body)));
- }
- fallback
-}
-
-pub(crate) fn bash_line_score(line: &str, index: usize) -> usize {
- let simplified = simplify_bash_command_line(line);
- let words = shell_words(line);
- let mut score = 30usize;
- if simplified != line {
- score += 40;
- }
- if words.first().is_some_and(|w| {
- matches!(
- w.as_str(),
- "rm" | "mv" | "cp" | "git" | "npm" | "pnpm" | "pytest" | "vitest"
- )
- }) {
- score += 20;
- }
- if re(&format!(r"\b(?:rm|mv|cp|git{S}+(?:add|commit)|npm{S}+install|sed{S}+-i|perl{S}+-pi|tee|cat{S}*>|apply_patch)\b"))
- .is_match(line)
- {
- score += 40;
- }
- score + index
-}
-
-/// Pick the highest-signal line of a bash command as its preview.
-pub fn preview_bash_command(command: &str) -> CodePreview {
- let lines: Vec<String> = command.split('\n').map(String::from).collect();
- let heredoc = preview_heredoc(&lines);
- if let Some(h) = heredoc.filter(|h| !h.text.is_empty()) {
- return CodePreview {
- language: h.language,
- text: descriptor(&h.text),
- };
- }
-
- let mut best: Option<(String, usize)> = None;
- let mut index = 0usize;
- for raw_line in &lines {
- for raw_part in split_command_chain(raw_line) {
- let command_line = strip_bash_prefix(js_trim(&raw_part));
- if command_line.is_empty() || is_skippable_bash_line(&command_line) {
- continue;
- }
- let text = simplify_bash_command_line(&command_line);
- let score = bash_line_score(&command_line, index);
- if best.as_ref().is_none_or(|(_, s)| score > *s) {
- best = Some((text, score));
- }
- index += 1;
- }
- }
- CodePreview::bash(best.map_or(String::new(), |(text, _)| descriptor(&text)))
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn descriptor_truncates_at_64_units() {
- let long = "hello world ".repeat(12);
- let d = descriptor(&long);
- assert_eq!(utf16_len(&d), 64);
- assert!(d.ends_with('\u{2026}'));
- }
-
- #[test]
- fn redact_hides_tokens_and_blobs() {
- let text = "api_token = \"abc123\" and 01234567890123456789012345678901234567890123456789012345678901234567890123456789";
- let red = redact_noise(text);
- assert!(red.contains("api_token=<redacted>"));
- assert!(red.contains("<blob>"));
- }
-
- #[test]
- fn bash_preview_prefers_mutation_line() {
- let p = preview_bash_command("echo start && git commit -m x && ls");
- assert_eq!(p.text, "git commit -m x");
- assert_eq!(p.language, CodePreviewLanguage::Bash);
- }
-
- #[test]
- fn bash_preview_simplifies_npm_run() {
- let p = preview_bash_command("npm run test -- --watch");
- assert_eq!(p.text, "npm test -- --watch");
- }
-
- #[test]
- fn bash_preview_heredoc_python() {
- let cmd = "python3 <<'EOF'\nprint('hi')\nEOF";
- let p = preview_bash_command(cmd);
- assert_eq!(p.text, "print('hi')");
- }
-
- #[test]
- fn python_preview_file_operation() {
- let code = "p = Path('src/a.ts')\nprint('x')\np.write_text('data')";
- let p = preview_python_code(code);
- assert_eq!(p.text, "write src/a.ts");
- assert_eq!(p.language, CodePreviewLanguage::Python);
- }
-
- #[test]
- fn python_preview_bash_skill_call() {
- let code = "print('x')\nawait bash(\"git status --porcelain\")";
- let p = preview_python_code(code);
- assert_eq!(p.language, CodePreviewLanguage::Bash);
- assert_eq!(p.text, "git status --porcelain");
- }
-
- #[test]
- fn ipython_cell_magic_preview() {
- let p = preview_ipython_code("%%bash\necho hi");
- assert_eq!(p.language, CodePreviewLanguage::Bash);
- assert_eq!(p.text, "echo hi");
- }
-
- #[test]
- fn statement_lines_mask_multiline_strings() {
- let code = "x = \"\"\"\na\nb\n\"\"\"\nrun(1)";
- let lines = python_statement_lines(code);
- assert_eq!(lines[0], "x = \"\"\"");
- assert_eq!(lines[3].trim(), "");
- assert_eq!(lines[4], "run(1)");
- }
-}
diff --git a/crates/pa-core/src/tools/code_preview_python.rs b/crates/pa-core/src/tools/code_preview_python.rs
deleted file mode 100644
index 49f30bacdd..0000000000
--- a/crates/pa-core/src/tools/code_preview_python.rs
+++ /dev/null
@@ -1,469 +0,0 @@
-//! Python-side preview analysis: `previewPythonCode`/`previewIpythonCode`
-//! and their statement scanner.
-
-use crate::tools::code_preview::{
- descriptor, is_comment_line, js_trim, js_trim_end, path_tail, preview_bash_command, re,
- simplify_bash_command_line, CodePreview, S,
-};
-use crate::tools::ipython_cell_code::parse_ipython_bash_cell;
-
-/// Skip blank, comment, and import lines when scoring python previews.
-fn is_skippable_python_line(line: &str) -> bool {
- let trimmed = js_trim(line);
- trimmed.is_empty()
- || is_comment_line(trimmed)
- || re(&format!(r"^{S}*(?:import{S}+\S|from{S}+\S+{S}+import{S}+)")).is_match(trimmed)
-}
-
-fn python_indent(line: &str) -> usize {
- re(&format!(r"^{S}*"))
- .find(line)
- .map_or(0, |m| m.as_str().chars().count())
-}
-
-fn python_call_pattern(inner: &str) -> bool {
- re(&format!(
- r"^{S}*(?:await{S}+)?[A-Za-z_][A-Za-z0-9_.]*{S}*\("
- ))
- .is_match(inner)
-}
-
-fn python_low_signal_call_pattern(inner: &str) -> bool {
- re(&format!(
- r"^{S}*(?:await{S}+)?(?:print|len|str|repr|int|float|list|dict|set|tuple){S}*\("
- ))
- .is_match(inner)
-}
-
-fn python_print_inner_call(line: &str) -> Option<String> {
- let trimmed = js_trim(line);
- let inner = re(r"^print\((.*)\)$")
- .captures(trimmed)
- .and_then(|c| c.get(1))
- .map(|m| js_trim(m.as_str()).to_string());
- inner.filter(|inner| python_call_pattern(inner))
-}
-
-fn python_path_vars(lines: &[String]) -> std::collections::HashMap<String, String> {
- let mut vars = std::collections::HashMap::new();
- let path_assign = re(&format!(
- r#"^{S}*([A-Za-z_][A-Za-z0-9_]*){S}*={S}*(?:Path|pathlib\.Path)\((["'])([^"']+)\2\)"#
- ));
- let string_assign = re(&format!(
- r#"^{S}*([A-Za-z_][A-Za-z0-9_]*){S}*={S}*(["'])([^"']+)\2"#
- ));
- for line in lines {
- let m = path_assign
- .captures(line)
- .or_else(|| string_assign.captures(line));
- if let Some(c) = m {
- let name = c.get(1).map(|g| g.as_str().to_string());
- let value = c.get(3).map(|g| g.as_str().to_string());
- if let (Some(name), Some(value)) = (name, value) {
- if value.contains('/') {
- vars.insert(name, value);
- }
- }
- }
- }
- vars
-}
-
-fn python_file_operation(
- line: &str,
- paths: &std::collections::HashMap<String, String>,
-) -> Option<String> {
- let m = re(
- r"^(?:await\s+)?([A-Za-z_][A-Za-z0-9_]*)\.(write_text|write_bytes|read_text|read_bytes|mkdir|unlink|rename|replace|touch)\s*\(",
- )
- .captures(js_trim(line))?;
- let name = m.get(1)?.as_str();
- let method = m.get(2)?.as_str();
- let path = paths.get(name)?;
- let action = match method {
- "write_text" | "write_bytes" => "write",
- "read_text" | "read_bytes" => "read",
- "mkdir" => "mkdir",
- "unlink" => "delete",
- "rename" => "rename",
- "replace" => "replace",
- "touch" => "touch",
- other => other,
- };
- Some(format!("{action} {}", path_tail(path)))
-}
-
-fn python_subprocess_command(line: &str) -> Option<String> {
- let trimmed = js_trim(line);
- if let Some(c) = re(r#"subprocess\.(?:run|check_call|check_output|Popen)\(\s*(["`])([^"`]+)\1"#)
- .captures(trimmed)
- {
- return c.get(2).map(|g| simplify_bash_command_line(g.as_str()));
- }
- if let Some(c) =
- re(r"subprocess\.(?:run|check_call|check_output|Popen)\(\s*\[([^\]]+)\]").captures(trimmed)
- {
- let inner = c.get(1)?.as_str();
- let words_re = re(r#"["']([^"']+)["']"#);
- let words: Vec<&str> = words_re
- .captures_iter(inner)
- .filter_map(|c| c.get(1).map(|g| g.as_str()))
- .collect();
- return Some(simplify_bash_command_line(&words.join(" ")));
- }
- None
-}
-
-fn simplify_python_preview_line(
- line: &str,
- paths: &std::collections::HashMap<String, String>,
-) -> String {
- python_file_operation(line, paths)
- .or_else(|| python_subprocess_command(line))
- .or_else(|| python_print_inner_call(line))
- .unwrap_or_else(|| js_trim(line).to_string())
-}
-
-fn python_preview_line(
- lines: &[String],
- index: usize,
- paths: &std::collections::HashMap<String, String>,
-) -> String {
- let line = lines.get(index).map_or("", String::as_str);
- if index > 0 && re(&format!(r"^{S}*(?:async{S}+def|def|class){S}+")).is_match(line) {
- let previous = lines.get(index - 1).map_or("", String::as_str);
- if re(&format!(r"^{S}*@")).is_match(js_trim(previous)) {
- return format!("{} {}", js_trim(previous), js_trim(line));
- }
- }
- if re(&format!(
- r"^{S}*(?:if|elif|else|for|while|with|try|except|finally)\b.*:\s*$"
- ))
- .is_match(line)
- {
- if let Some(child_index) = first_python_child_line(lines, index) {
- let head = re(r":\s*$").replace(js_trim(line), ":");
- let child = lines.get(child_index).cloned().unwrap_or_default();
- return format!("{head} {}", simplify_python_preview_line(&child, paths));
- }
- }
- simplify_python_preview_line(line, paths)
-}
-
-fn first_python_child_line(lines: &[String], parent_index: usize) -> Option<usize> {
- let parent_line = lines.get(parent_index).cloned().unwrap_or_default();
- let parent_indent = python_indent(&parent_line);
- for (i, line) in lines.iter().enumerate().skip(parent_index + 1) {
- if is_skippable_python_line(line) || re(&format!(r"^{S}*@")).is_match(js_trim(line)) {
- continue;
- }
- if python_indent(line) <= parent_indent {
- return None;
- }
- return Some(i);
- }
- None
-}
-
-fn python_line_score(
- lines: &[String],
- index: usize,
- paths: &std::collections::HashMap<String, String>,
-) -> i64 {
- let line = lines.get(index).cloned().unwrap_or_default();
- let trimmed = js_trim(&line).to_string();
- if is_skippable_python_line(&line)
- || re(&format!(r"^{S}*@")).is_match(&trimmed)
- || re(r"^[)\]},;\s]+(?:#.*)?$").is_match(&trimmed)
- {
- return -1;
- }
- if python_file_operation(&line, paths).is_some() {
- return 95;
- }
- if python_subprocess_command(&line).is_some() {
- return 90;
- }
- if re(&format!(
- r#"^{S}*if{S}+__name__{S}*=={S}*['"]__main__['"]{S}*:"#
- ))
- .is_match(&line)
- {
- return 70;
- }
- if re(&format!(
- r"^{S}*(?:await{S}+)?[A-Za-z_][A-Za-z0-9_.]*\.(?:write_text|write_bytes|mkdir|unlink|rename|replace|touch|append|extend|update|add|remove|discard|close|commit|execute|run){S}*\("
- ))
- .is_match(&line)
- {
- return 80;
- }
- if re(&format!(
- r"^{S}*(?:if|elif|else|for|while|with|try|except|finally)\b.*:\s*$"
- ))
- .is_match(&line)
- {
- return match first_python_child_line(lines, index) {
- None => 20,
- Some(child_index) => (python_line_score(lines, child_index, paths) - 5).max(20),
- };
- }
- if re(&format!(r"^{S}*(?:async{S}+def|def|class){S}+")).is_match(&line) {
- return 50;
- }
- if re(&format!(
- r"^{S}*[A-Za-z_][A-Za-z0-9_]*(?:{S}*:\s*[^=]+)?{S}*={S}*(?:await{S}+)?(?:Path|pathlib\.Path|json\.loads|json\.dumps|str|int|float|list|dict|set|tuple){S}*\("
- ))
- .is_match(&line)
- {
- return 25;
- }
- let print_inner_call = python_print_inner_call(&line);
- let is_low_signal_call = print_inner_call
- .as_deref()
- .is_some_and(python_low_signal_call_pattern);
- if print_inner_call.is_some() && !is_low_signal_call {
- return 55;
- }
- if re(&format!(
- r"^{S}*[A-Za-z_][A-Za-z0-9_]*(?:{S}*:\s*[^=]+)?{S}*={S}*(?:await{S}+)?[A-Za-z_][A-Za-z0-9_.]*{S}*\("
- ))
- .is_match(&line)
- {
- return 60;
- }
- let matches_call = python_call_pattern(&line);
- let matches_low_signal = python_low_signal_call_pattern(&line);
- if matches_call && !matches_low_signal {
- return 65;
- }
- if matches_call {
- return 15;
- }
- 30
-}
-
-fn python_preview_index(lines: &[String], index: usize) -> usize {
- let line = lines.get(index).cloned().unwrap_or_default();
- if !re(&format!(
- r"^{S}*(?:if|elif|else|for|while|with|try|except|finally)\b.*:\s*$"
- ))
- .is_match(&line)
- {
- return index;
- }
- match first_python_child_line(lines, index) {
- None => index,
- Some(child_index) => python_preview_index(lines, child_index),
- }
-}
-
-struct PythonStringScan {
- value: String,
- end: usize,
- closed: bool,
- /// Saw a cooked escape whose value is not computed here.
- unsupported_escape: bool,
-}
-
-fn is_unsupported_escape_char(ch: char) -> bool {
- matches!(
- ch,
- 'x' | 'u' | 'U' | 'N' | 'a' | 'b' | 'f' | 'v' | '0'..='7'
- )
-}
-
-/// Walk a python string-literal body from just after the opening delimiter, following python's
-/// escape rules (in raw strings backslash-quote never closes). Offsets index bytes into `code`.
-fn scan_python_string_literal(
- code: &str,
- start: usize,
- quote: &str,
- raw: bool,
-) -> PythonStringScan {
- let mut value = String::new();
- let mut i = start;
- let mut unsupported_escape = false;
- let quote_bytes = quote.as_bytes();
- while i < code.len() {
- let ch = code[i..].chars().next().expect("char boundary");
- if ch == '\\' && i + 1 < code.len() {
- let next = code[i + 1..].chars().next().expect("char boundary");
- if raw {
- value.push('\\');
- value.push(next);
- } else {
- if is_unsupported_escape_char(next) {
- unsupported_escape = true;
- }
- match next {
- '\n' => {} // backslash-newline is a line continuation
- '"' => value.push('"'),
- '\'' => value.push('\''),
- '\\' => value.push('\\'),
- 'n' => value.push('\n'),
- 'r' => value.push('\r'),
- 't' => value.push('\t'),
- other => {
- value.push('\\');
- value.push(other);
- }
- }
- }
- i += 1 + next.len_utf8();
- continue;
- }
- if code.as_bytes()[i..].starts_with(quote_bytes) {
- return PythonStringScan {
- value,
- end: i + quote.len(),
- closed: true,
- unsupported_escape,
- };
- }
- if quote.len() == 1 && ch == '\n' {
- break; // single-quoted literals cannot span lines
- }
- value.push(ch);
- i += ch.len_utf8();
- }
- PythonStringScan {
- value,
- end: i,
- closed: false,
- unsupported_escape,
- }
-}
-
-/// Keep source-line positions while masking multiline-string continuations.
-pub fn python_statement_lines(code: &str) -> Vec<String> {
- let mut lines: Vec<String> = code.split('\n').map(String::from).collect();
- let mut line = 0usize;
- let mut i = 0usize;
- while i < code.len() {
- let ch = code[i..].chars().next().expect("char boundary");
- if ch == '#' {
- let Some(nl) = code[i..].find('\n') else {
- break;
- };
- i += nl;
- continue;
- }
- if ch == '"' || ch == '\'' {
- let quote = if code[i..].starts_with(&ch.to_string().repeat(3)) {
- ch.to_string().repeat(3)
- } else {
- ch.to_string()
- };
- let scan = scan_python_string_literal(code, i + quote.len(), &quote, true);
- let start_line = line;
- for end in i..scan.end {
- if code.as_bytes().get(end) == Some(&b'\n') {
- line += 1;
- while lines.len() <= line {
- lines.push(String::new());
- }
- lines[line] = String::new();
- }
- }
- if scan.closed && line > start_line {
- let column = scan.end - (code[..scan.end].rfind('\n').map_or(0, |p| p + 1));
- let rest_start = scan.end;
- let rest_end = code[rest_start..]
- .find('\n')
- .map_or(code.len(), |p| rest_start + p);
- while lines.len() <= line {
- lines.push(String::new());
- }
- lines[line] = format!("{}{}", " ".repeat(column), &code[rest_start..rest_end]);
- }
- i = scan.end;
- continue;
- }
- if ch == '\n' {
- line += 1;
- }
- i += ch.len_utf8();
- }
- lines
-}
-
-fn extract_bash_skill_command(code: &str) -> Option<String> {
- let triple_double = "\"\"\"";
- let triple_single = concat!("''", "'");
- let m = re(&format!(
- r#"{S}*(?:[A-Za-z_][A-Za-z0-9_]*{S}*={S}*)?(?:await{S}+)?bash{S}*\({S}*[rR]?({triple_double}|{triple_single}|"|')"#
- ))
- .captures(code)?;
- let quote = m.get(1)?.as_str();
- let start = m.get(0)?.end();
- // The character before the quote (r/R) marks a raw literal.
- let head: Vec<char> = code[..start].chars().collect();
- let quote_chars = quote.chars().count();
- let prefix_char = if head.len() > quote_chars {
- head[head.len() - quote_chars - 1]
- } else {
- ' '
- };
- let raw = matches!(prefix_char, 'r' | 'R');
- let scan = scan_python_string_literal(code, start, quote, raw);
- if !scan.closed || scan.unsupported_escape {
- return None;
- }
- let rest = code[scan.end..].trim_start();
- // Require a plain literal first argument; concatenation or other expressions fall back.
- if !rest.starts_with(',') && !rest.starts_with(')') {
- return None;
- }
- Some(scan.value)
-}
-
-/// Pick the highest-signal line of a python cell as its preview.
-pub fn preview_python_code(code: &str) -> CodePreview {
- let raw_lines: Vec<String> = code.split('\n').map(String::from).collect();
- let lines: Vec<String> = python_statement_lines(code)
- .into_iter()
- .map(|line| re(&format!(r"^({S}*);{S}*")).replace(&line, "$1"))
- .collect();
- let paths = python_path_vars(&lines);
- let mut best_index: Option<usize> = None;
- let mut best_score: i64 = -1;
-
- for (i, _) in lines.iter().enumerate() {
- let score = python_line_score(&lines, i, &paths);
- if score > best_score {
- best_index = Some(i);
- best_score = score;
- }
- }
-
- if let Some(best_index) = best_index.filter(|_| best_score >= 0) {
- let preview_index = python_preview_index(&lines, best_index);
- // Keep the original tail for multiline commands, excluding any
- // preceding string continuation.
- let mut joined = String::new();
- if let Some(first) = lines.get(preview_index) {
- joined.push_str(first);
- }
- for raw in raw_lines.iter().skip(preview_index + 1) {
- joined.push('\n');
- joined.push_str(raw);
- }
- if let Some(bash_command) = extract_bash_skill_command(&joined) {
- return preview_bash_command(&bash_command);
- }
- let text = python_preview_line(&lines, preview_index, &paths);
- return CodePreview::python(descriptor(&text));
- }
- CodePreview::python(String::new())
-}
-
-/// Preview an ipython cell: %%bash cells preview as bash, the rest as python.
-#[cfg_attr(not(test), allow(dead_code))]
-pub(crate) fn preview_ipython_code(code: &str) -> CodePreview {
- let trimmed = js_trim_end(code);
- if let Some(cell) = parse_ipython_bash_cell(trimmed) {
- return preview_bash_command(&cell.body);
- }
- preview_python_code(trimmed)
-}
diff --git a/crates/pa-core/src/tools/golden_replay.rs b/crates/pa-core/src/tools/golden_replay.rs
index 5e8f4fa38a..94c021eff3 100644
--- a/crates/pa-core/src/tools/golden_replay.rs
+++ b/crates/pa-core/src/tools/golden_replay.rs
@@ -12,8 +12,6 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use crate::tools::bash::{self, BashToolOptions};
-use crate::tools::code_preview::preview_bash_command;
-use crate::tools::code_preview_python::preview_ipython_code;
use crate::tools::edit::{create_edit_tool_definition, execute_edit, LocalEditOperations};
use crate::tools::ipython::{
self, ExecuteResult, ExecuteStatus, IpythonKernelProvisioner, IpythonToolOptions,
@@ -21,7 +19,7 @@ use crate::tools::ipython::{
KernelExecError, KernelExecuteOptions, KernelExecutor,
};
use crate::tools::tool_definition::ToolDefinition;
-use crate::tools::truncate::{truncate_head, truncate_tail, TruncationOptions};
+use crate::tools::truncate::{truncate_tail, TruncationOptions};
const CORPUS_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/golden/corpus");
@@ -317,13 +315,7 @@ fn golden_truncate_group_matches_ts() {
case["maxLines"].as_u64().expect("maxLines") as usize,
case["maxBytes"].as_u64().expect("maxBytes") as usize,
);
- let head = truncate_head(content, options);
let tail = truncate_tail(content, options);
- assert_json_eq(
- &truncation_json(&head),
- &case["head"].clone(),
- &format!("truncate/{name} head"),
- );
assert_json_eq(
&truncation_json(&tail),
&case["tail"].clone(),
@@ -334,37 +326,6 @@ fn golden_truncate_group_matches_ts() {
assert_eq!(checked, corpus["caseCount"].as_u64().expect("caseCount"));
}
-#[test]
-fn golden_preview_group_matches_ts() {
- let corpus = corpus("preview");
- let mut checked = 0;
- for case in corpus["cases"].as_array().expect("cases array") {
- let input = case["input"].as_str().expect("input");
- let language = |l: crate::tools::code_preview::CodePreviewLanguage| {
- serde_json::json!({
- "language": match l {
- crate::tools::code_preview::CodePreviewLanguage::Bash => "bash",
- crate::tools::code_preview::CodePreviewLanguage::Python => "python",
- },
- })
- };
- let preview = preview_bash_command(input);
- let actual = serde_json::json!({
- "language": language(preview.language)["language"],
- "text": preview.text,
- });
- assert_json_eq(&actual, &case["bash"].clone(), "preview bash");
- let preview = preview_ipython_code(input);
- let actual = serde_json::json!({
- "language": language(preview.language)["language"],
- "text": preview.text,
- });
- assert_json_eq(&actual, &case["ipython"].clone(), "preview ipython");
- checked += 1;
- }
- assert_eq!(checked, corpus["caseCount"].as_u64().expect("caseCount"));
-}
-
struct MockKernel {
executions: Mutex<Vec<MockOutcome>>,
exec_calls: AtomicUsize,
diff --git a/crates/pa-core/src/tools/ipython_cell_code.rs b/crates/pa-core/src/tools/ipython_cell_code.rs
deleted file mode 100644
index 7661213954..0000000000
--- a/crates/pa-core/src/tools/ipython_cell_code.rs
+++ /dev/null
@@ -1,82 +0,0 @@
-/// Matches leading blank lines and a `%%bash` cell-magic line.
-#[cfg_attr(not(test), allow(dead_code))]
-fn bash_cell_magic_match(code: &str) -> Option<usize> {
- let bytes = code.as_bytes();
- let mut i = 0;
- // (?:[ \t]*\r?\n)*
- loop {
- let start = i;
- while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
- i += 1;
- }
- if i < bytes.len() && bytes[i] == b'\r' && i + 1 < bytes.len() && bytes[i + 1] == b'\n' {
- i += 2;
- } else if i < bytes.len() && bytes[i] == b'\n' {
- i += 1;
- } else {
- i = start;
- break;
- }
- }
- // [ \t]*%%bash
- while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
- i += 1;
- }
- if !code[i..].starts_with("%%bash") {
- return None;
- }
- i += "%%bash".len();
- // \b: the char after "%%bash" must not be a word char.
- if let Some(next) = code[i..].chars().next() {
- if next.is_alphanumeric() || next == '_' {
- return None;
- }
- }
- // [^\r\n]* then (\r?\n or end of string)
- while i < bytes.len() && bytes[i] != b'\r' && bytes[i] != b'\n' {
- i += 1;
- }
- if i < bytes.len() && bytes[i] == b'\r' && i + 1 < bytes.len() && bytes[i + 1] == b'\n' {
- i += 2;
- } else if i < bytes.len() && bytes[i] == b'\n' {
- i += 1;
- }
- Some(i)
-}
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct ParsedIpythonBashCell {
- pub body: String,
-}
-
-/// Detect an `%%bash` cell magic and return the cell body that follows it.
-pub fn parse_ipython_bash_cell(code: &str) -> Option<ParsedIpythonBashCell> {
- let matched = bash_cell_magic_match(code)?;
- Some(ParsedIpythonBashCell {
- body: code[matched..].to_string(),
- })
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn parses_bash_cell() {
- let p = parse_ipython_bash_cell("%%bash\necho hi").unwrap();
- assert_eq!(p.body, "echo hi");
- }
-
- #[test]
- fn ignores_leading_blank_lines() {
- let p = parse_ipython_bash_cell("\n\t\n %%bash -l\nls").unwrap();
- assert_eq!(p.body, "ls");
- }
-
- #[test]
- fn no_magic() {
- assert!(parse_ipython_bash_cell("print(1)").is_none());
- assert!(parse_ipython_bash_cell("x = '%%bash'").is_none());
- assert!(parse_ipython_bash_cell(" %%bashish\n").is_none());
- }
-}
diff --git a/crates/pa-core/src/tools/mod.rs b/crates/pa-core/src/tools/mod.rs
index 1a1101cc82..7bda0cb5e2 100644
--- a/crates/pa-core/src/tools/mod.rs
+++ b/crates/pa-core/src/tools/mod.rs
@@ -4,25 +4,17 @@
pub(crate) mod bash;
pub(crate) mod bash_guard;
pub(crate) mod bash_local;
-// Rendering preview helpers: unused by the engine's headless core
-// (pa-tui owns terminal rendering); lives here for parity tests.
-#[allow(dead_code)]
-pub(crate) mod code_preview;
-pub(crate) mod code_preview_python;
pub(crate) mod edit;
pub(crate) mod edit_diff;
pub(crate) mod file_mutation_queue;
pub(crate) mod golden_replay;
pub(crate) mod ipython;
-pub(crate) mod ipython_cell_code;
pub(crate) mod jsdiff;
pub(crate) mod output_accumulator;
#[allow(dead_code)] // util module; every fn exercised by unit + golden tests
pub(crate) mod path_utils;
-#[allow(dead_code)] // render helpers; consumed by the TUI-side renderer (parity-ported here)
pub(crate) mod render_utils;
#[allow(dead_code)] // util module; fully exercised by unit + golden tests
pub(crate) mod shell_utils;
pub(crate) mod tool_definition;
-#[allow(dead_code)] // util module; fully exercised by unit + golden tests
pub(crate) mod truncate;
diff --git a/crates/pa-core/src/tools/render_utils.rs b/crates/pa-core/src/tools/render_utils.rs
index f5751fd01d..f29742759e 100644
--- a/crates/pa-core/src/tools/render_utils.rs
+++ b/crates/pa-core/src/tools/render_utils.rs
@@ -1,50 +1,13 @@
-//! Result-rendering helpers shared by tool renderers.
+//! Image dimension parsing from base64 headers.
use base64::Engine;
-use pa_types::ai::{ImageContent, TextContent, UserContentBlock};
-
-use crate::tools::shell_utils::sanitize_binary_output;
-
-/// `[Image: ...]` fallback text for an image that cannot be displayed.
-pub fn image_fallback(
- mime_type: &str,
- dimensions: Option<ImageDimensions>,
- filename: Option<&str>,
-) -> String {
- let mut parts: Vec<String> = Vec::new();
- if let Some(filename) = filename {
- parts.push(filename.to_string());
- }
- parts.push(format!("[{mime_type}]"));
- if let Some(dims) = dimensions {
- parts.push(format!("{}x{}", dims.width_px, dims.height_px));
- }
- format!("[Image: {}]", parts.join(" "))
-}
-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageDimensions {
pub width_px: u32,
pub height_px: u32,
}
-/// Decode base64 image bytes and read their pixel dimensions.
-///
-/// Returns `None` for unknown mime types or undecodable payloads.
-pub fn get_image_dimensions(data: &str, mime_type: &str) -> Option<ImageDimensions> {
- let bytes = base64::engine::general_purpose::STANDARD
- .decode(data.trim())
- .ok()?;
- match mime_type {
- "image/png" => png_dimensions(&bytes),
- "image/gif" => gif_dimensions(&bytes),
- "image/jpeg" => jpeg_dimensions(&bytes),
- "image/webp" => webp_dimensions(&bytes),
- _ => None,
- }
-}
-
/// The bounded-prefix decode budget for [`get_image_dimensions_prefix`]:
/// every supported format's dimension header lives in the first bytes.
pub const IMAGE_DIMENSIONS_PREFIX_BYTES: usize = 1024;
@@ -182,164 +145,10 @@ fn webp_dimensions(b: &[u8]) -> Option<ImageDimensions> {
}
}
-/// Shorten an absolute path under the user's home directory to `~/...`.
-pub fn shorten_path(path: &str) -> String {
- let home = pa_types::platform::home_dir()
- .map(|home| home.to_string_lossy().into_owned())
- .unwrap_or_default();
- if !home.is_empty() && path.starts_with(&home) {
- return format!("~{}", &path[home.len()..]);
- }
- path.to_string()
-}
-
-/// Replace tab characters with three spaces (TUI tool-call rendering).
-pub fn replace_tabs(text: &str) -> String {
- text.replace('\t', " ")
-}
-
-/// A tool-result content block (text or image).
-pub type ContentBlock = UserContentBlock;
-
-/// Build a text block.
-pub fn text_block(text: impl Into<String>) -> ContentBlock {
- ContentBlock::Text(TextContent {
- text: text.into(),
- text_signature: None,
- rest: serde_json::Map::default(),
- })
-}
-
-/// Build an image block.
-pub fn image_block(data: impl Into<String>, mime_type: impl Into<String>) -> ContentBlock {
- ContentBlock::Image(ImageContent {
- data: data.into(),
- mime_type: mime_type.into(),
- rest: serde_json::Map::default(),
- })
-}
-
-#[derive(Debug, Clone, Copy, Default)]
-pub struct TextOutputOptions {
- pub include_image_dimensions: bool,
-}
-
-/// Join a tool result's content into the plain-text output shown for it:
-/// text blocks are ANSI-stripped, sanitized, and joined with newlines;
-/// non-inline images become `[Image: ...]` fallback lines.
-pub fn get_text_output(
- content: &[ContentBlock],
- show_images: bool,
- options: TextOutputOptions,
-) -> String {
- let text_blocks: Vec<&str> = content
- .iter()
- .filter_map(|b| match b {
- ContentBlock::Text(t) => Some(t.text.as_str()),
- _ => None,
- })
- .collect();
- let image_blocks: Vec<(&str, &str)> = content
- .iter()
- .filter_map(|b| match b {
- ContentBlock::Image(img) => Some((img.data.as_str(), img.mime_type.as_str())),
- _ => None,
- })
- .collect();
-
- let mut output = text_blocks
- .iter()
- .map(|text| {
- let stripped = strip_ansi(text);
- let sanitized = sanitize_binary_output(&stripped);
- sanitized.replace('\r', "")
- })
- .collect::<Vec<_>>()
- .join("\n");
-
- if !image_blocks.is_empty() && !show_images {
- let indicators = image_blocks
- .iter()
- .map(|(data, mime_type)| {
- let dims = if options.include_image_dimensions {
- get_image_dimensions(data, mime_type)
- } else {
- None
- };
- image_fallback(mime_type, dims, None)
- })
- .collect::<Vec<_>>()
- .join("\n");
- output = if output.is_empty() {
- indicators
- } else {
- format!("{output}\n{indicators}")
- };
- }
-
- output
-}
-
-/// Remove ANSI escape sequences, using the exact pattern of the `strip-ansi` npm package.
-pub fn strip_ansi(text: &str) -> String {
- let re = fancy_regex::Regex::new(
- r"[\x{001B}\x{009B}][\[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d/#&.:=?%@~_]*)*)?\x{0007})|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~]))",
- )
- .expect("valid strip-ansi pattern");
- re.replace_all(text, "").into_owned()
-}
-
#[cfg(test)]
mod tests {
use super::*;
- #[test]
- fn strip_ansi_removes_csi_sequences() {
- assert_eq!(strip_ansi("\x1b[31mred\x1b[0m"), "red");
- assert_eq!(strip_ansi("\x1b[1;32mbold green\x1b[39;49m"), "bold green");
- assert_eq!(strip_ansi("plain"), "plain");
- }
-
- // The home read goes through the platform wall exactly like the product's
- // shortening (USERPROFILE on win32), so the test runs everywhere the home resolves.
- #[test]
- fn shorten_path_replaces_home() {
- let Some(home) = pa_types::platform::home_dir() else {
- return;
- };
- let home = home.to_string_lossy();
- assert_eq!(shorten_path(&format!("{home}/a/b")), "~/a/b");
- assert_eq!(shorten_path("/etc/passwd"), "/etc/passwd");
- }
-
- #[test]
- fn text_output_joins_and_strips() {
- let blocks = vec![text_block("a\r\n"), text_block("\x1b[2mb\x1b[0m")];
- assert_eq!(
- get_text_output(&blocks, true, TextOutputOptions::default()),
- "a\n\nb"
- );
- }
-
- #[test]
- fn image_fallback_text() {
- assert_eq!(
- image_fallback("image/png", None, None),
- "[Image: [image/png]]"
- );
- assert_eq!(
- image_fallback(
- "image/png",
- Some(ImageDimensions {
- width_px: 4,
- height_px: 2
- }),
- None
- ),
- "[Image: [image/png] 4x2]"
- );
- }
-
#[test]
fn png_dimensions_parsed() {
// Minimal PNG header with IHDR 2x3.
@@ -350,7 +159,8 @@ mod tests {
b.extend_from_slice(&3u32.to_be_bytes());
b.extend_from_slice(&[8, 6, 0, 0, 0]);
let data = base64::engine::general_purpose::STANDARD.encode(&b);
- let dims = get_image_dimensions(&data, "image/png").unwrap();
+ let dims = get_image_dimensions_prefix(&data, "image/png", IMAGE_DIMENSIONS_PREFIX_BYTES)
+ .expect("minimal png parses");
assert_eq!((dims.width_px, dims.height_px), (2, 3));
}
}
diff --git a/crates/pa-core/src/tools/truncate.rs b/crates/pa-core/src/tools/truncate.rs
index 9cde490e94..42dedf5580 100644
--- a/crates/pa-core/src/tools/truncate.rs
+++ b/crates/pa-core/src/tools/truncate.rs
@@ -2,24 +2,12 @@
//! line limit (default 2000) and byte limit (default 50KB). Never returns partial lines (except
//! the bash tail truncation edge case).
-/// Max chars per grep match line.
-pub const GREP_MAX_LINE_LENGTH: usize = 500;
-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TruncationOptions {
pub max_lines: usize,
pub max_bytes: usize,
}
-impl Default for TruncationOptions {
- fn default() -> Self {
- Self {
- max_lines: DEFAULT_MAX_LINES,
- max_bytes: DEFAULT_MAX_BYTES,
- }
- }
-}
-
impl TruncationOptions {
pub fn with_limits(max_lines: usize, max_bytes: usize) -> Self {
Self {
@@ -66,89 +54,6 @@ pub fn format_size(bytes: usize) -> String {
}
}
-/// Truncate content from the head (keep first N lines/bytes); suitable for file
-/// reads. Never returns partial lines; a first line over the byte limit returns
-/// empty content with `first_line_exceeds_limit = true`.
-pub fn truncate_head(content: &str, options: TruncationOptions) -> TruncationResult {
- let TruncationOptions {
- max_lines,
- max_bytes,
- } = options;
-
- let total_bytes = content.len();
- let lines: Vec<&str> = content.split('\n').collect();
- let total_lines = lines.len();
-
- if total_lines <= max_lines && total_bytes <= max_bytes {
- return TruncationResult {
- content: content.to_string(),
- truncated: false,
- truncated_by: None,
- total_lines,
- total_bytes,
- output_lines: total_lines,
- output_bytes: total_bytes,
- last_line_partial: false,
- first_line_exceeds_limit: false,
- max_lines,
- max_bytes,
- };
- }
-
- let first_line_bytes = lines[0].len();
- if first_line_bytes > max_bytes {
- return TruncationResult {
- content: String::new(),
- truncated: true,
- truncated_by: Some(TruncatedBy::Bytes),
- total_lines,
- total_bytes,
- output_lines: 0,
- output_bytes: 0,
- last_line_partial: false,
- first_line_exceeds_limit: true,
- max_lines,
- max_bytes,
- };
- }
-
- let mut output_lines: Vec<&str> = Vec::new();
- let mut output_bytes_count = 0usize;
- let mut truncated_by = TruncatedBy::Lines;
-
- for (i, line) in lines.iter().enumerate().take(max_lines) {
- // +1 for the newline before every line after the first.
- let line_bytes = line.len() + usize::from(i > 0);
- if output_bytes_count + line_bytes > max_bytes {
- truncated_by = TruncatedBy::Bytes;
- break;
- }
- output_lines.push(line);
- output_bytes_count += line_bytes;
- }
-
- if output_lines.len() >= max_lines && output_bytes_count <= max_bytes {
- truncated_by = TruncatedBy::Lines;
- }
-
- let output_content = output_lines.join("\n");
- let final_output_bytes = output_content.len();
-
- TruncationResult {
- content: output_content,
- truncated: true,
- truncated_by: Some(truncated_by),
- total_lines,
- total_bytes,
- output_lines: output_lines.len(),
- output_bytes: final_output_bytes,
- last_line_partial: false,
- first_line_exceeds_limit: false,
- max_lines,
- max_bytes,
- }
-}
-
/// Truncate content from the tail (keep last N lines/bytes); suitable for bash output where you
/// want to see the end. May return a partial first line when the last line exceeds the byte limit.
pub fn truncate_tail(content: &str, options: TruncationOptions) -> TruncationResult {
@@ -246,43 +151,6 @@ fn truncate_string_to_bytes_from_end(s: &str, max_bytes: usize) -> String {
String::from_utf8_lossy(&bytes[start..]).into_owned()
}
-/// Truncate a single line to max UTF-16 code units, adding a `[truncated]` suffix.
-/// Used for grep match lines.
-pub fn truncate_line(line: &str, max_chars: usize) -> TruncatedLine {
- if line.chars().map(char::len_utf16).sum::<usize>() <= max_chars {
- return TruncatedLine {
- text: line.to_string(),
- was_truncated: false,
- };
- }
- let cut = utf16_truncate_end(line, max_chars);
- TruncatedLine {
- text: format!("{cut}... [truncated]"),
- was_truncated: true,
- }
-}
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct TruncatedLine {
- pub text: String,
- pub was_truncated: bool,
-}
-
-/// Slice `s` to its first `max_units` UTF-16 code units.
-fn utf16_truncate_end(s: &str, max_units: usize) -> String {
- let mut units = 0usize;
- let mut end = 0usize;
- for ch in s.chars() {
- let w = ch.len_utf16();
- if units + w > max_units {
- break;
- }
- units += w;
- end += ch.len_utf8();
- }
- s[..end].to_string()
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -297,34 +165,6 @@ mod tests {
assert_eq!(format_size(2 * 1024 * 1024 + 512 * 1024), "2.5MB");
}
- #[test]
- fn head_within_limits_is_passthrough() {
- let r = truncate_head("a\nb\nc", TruncationOptions::default());
- assert!(!r.truncated);
- assert_eq!(r.content, "a\nb\nc");
- assert_eq!(r.total_lines, 3);
- assert_eq!(r.output_bytes, 5);
- }
-
- #[test]
- fn head_line_limit() {
- let r = truncate_head("a\nb\nc\nd", TruncationOptions::with_limits(2, 100));
- assert!(r.truncated);
- assert_eq!(r.truncated_by, Some(TruncatedBy::Lines));
- assert_eq!(r.content, "a\nb");
- assert_eq!(r.output_lines, 2);
- }
-
- #[test]
- fn head_first_line_exceeds_byte_limit() {
- let r = truncate_head("xxxxxxxxxx\ny", TruncationOptions::with_limits(10, 4));
- assert!(r.truncated);
- assert!(r.first_line_exceeds_limit);
- assert_eq!(r.content, "");
- assert_eq!(r.output_bytes, 0);
- assert_eq!(r.truncated_by, Some(TruncatedBy::Bytes));
- }
-
#[test]
fn tail_keeps_last_lines() {
let r = truncate_tail("1\n2\n3\n4\n5", TruncationOptions::with_limits(2, 100));
@@ -364,13 +204,4 @@ mod tests {
let cut = truncate_string_to_bytes_from_end(s, 4);
assert_eq!(cut, "llo");
}
-
- #[test]
- fn truncate_line_utf16() {
- let r = truncate_line("short", 10);
- assert!(!r.was_truncated);
- let r = truncate_line("a".repeat(600).as_str(), 500);
- assert!(r.was_truncated);
- assert_eq!(r.text.len(), 500 + "... [truncated]".len());
- }
}
diff --git a/crates/pa-tui/src/code_preview/golden_tests.rs b/crates/pa-tui/src/code_preview/golden_tests.rs
new file mode 100644
index 0000000000..b7e4cf2a10
--- /dev/null
+++ b/crates/pa-tui/src/code_preview/golden_tests.rs
@@ -0,0 +1,34 @@
+//! Golden preview cases recorded from the TypeScript tools; the shipped
+//! previews must match them.
+
+use super::{preview_bash_command, preview_ipython_code, CodePreviewLanguage};
+
+#[test]
+fn golden_previews_match_ts() {
+ let corpus_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/golden/preview.json");
+ let corpus: serde_json::Value =
+ serde_json::from_str(&std::fs::read_to_string(corpus_path).expect("corpus file"))
+ .expect("corpus JSON");
+ let language = |preview: CodePreviewLanguage| match preview {
+ CodePreviewLanguage::Bash => "bash",
+ CodePreviewLanguage::Python => "python",
+ };
+ let mut checked = 0;
+ for case in corpus["cases"].as_array().expect("cases array") {
+ let input = case["input"].as_str().expect("input");
+ let preview = preview_bash_command(input);
+ let actual = serde_json::json!({
+ "language": language(preview.language),
+ "text": preview.text,
+ });
+ assert_eq!(actual, case["bash"], "bash preview for {input:?}");
+ let preview = preview_ipython_code(input);
+ let actual = serde_json::json!({
+ "language": language(preview.language),
+ "text": preview.text,
+ });
+ assert_eq!(actual, case["ipython"], "ipython preview for {input:?}");
+ checked += 1;
+ }
+ assert_eq!(checked, corpus["caseCount"].as_u64().expect("caseCount"));
+}
diff --git a/crates/pa-tui/src/code_preview/mod.rs b/crates/pa-tui/src/code_preview/mod.rs
index 45ab32dbca..17791deb14 100644
--- a/crates/pa-tui/src/code_preview/mod.rs
+++ b/crates/pa-tui/src/code_preview/mod.rs
@@ -1,10 +1,12 @@
//! Code-preview extraction for tool-call cards (`ipython` collapsed line).
//! The pa-tui dependency boundary (pa-types only) keeps the session engine
-//! out of the UI, so the display-side preview algorithm lives here; pa-core
-//! holds the same algorithm for its golden-replay corpus.
+//! out of the UI, so the display-side preview algorithm lives here; the
+//! TypeScript golden corpus test lives here too.
pub(crate) mod bash;
mod cell;
+#[cfg(test)]
+mod golden_tests;
pub use cell::parse_ipython_bash_cell;
mod python;
diff --git a/crates/pa-core/tests/golden/corpus/preview.json b/crates/pa-tui/tests/golden/preview.json
similarity index 100%
rename from crates/pa-core/tests/golden/corpus/preview.json
rename to crates/pa-tui/tests/golden/preview.json