Compare commits
5 Commits
rcc/jsonl-
...
rcc/grok
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40008b6513 | ||
|
|
f477dde4a6 | ||
|
|
178934a9a0 | ||
|
|
2a0f4b677a | ||
|
|
cbc0a83059 |
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,10 @@ use std::time::Duration;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ApiError {
|
||||
MissingApiKey,
|
||||
MissingCredentials {
|
||||
provider: &'static str,
|
||||
env_vars: &'static [&'static str],
|
||||
},
|
||||
ExpiredOAuthToken,
|
||||
Auth(String),
|
||||
InvalidApiKeyEnv(VarError),
|
||||
@@ -30,13 +33,21 @@ pub enum ApiError {
|
||||
}
|
||||
|
||||
impl ApiError {
|
||||
#[must_use]
|
||||
pub const fn missing_credentials(
|
||||
provider: &'static str,
|
||||
env_vars: &'static [&'static str],
|
||||
) -> Self {
|
||||
Self::MissingCredentials { provider, env_vars }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_retryable(&self) -> bool {
|
||||
match self {
|
||||
Self::Http(error) => error.is_connect() || error.is_timeout() || error.is_request(),
|
||||
Self::Api { retryable, .. } => *retryable,
|
||||
Self::RetriesExhausted { last_error, .. } => last_error.is_retryable(),
|
||||
Self::MissingApiKey
|
||||
Self::MissingCredentials { .. }
|
||||
| Self::ExpiredOAuthToken
|
||||
| Self::Auth(_)
|
||||
| Self::InvalidApiKeyEnv(_)
|
||||
@@ -51,12 +62,11 @@ impl ApiError {
|
||||
impl Display for ApiError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::MissingApiKey => {
|
||||
write!(
|
||||
f,
|
||||
"ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY is not set; export one before calling the Anthropic API"
|
||||
)
|
||||
}
|
||||
Self::MissingCredentials { provider, env_vars } => write!(
|
||||
f,
|
||||
"missing {provider} credentials; export {} before calling the {provider} API",
|
||||
env_vars.join(" or ")
|
||||
),
|
||||
Self::ExpiredOAuthToken => {
|
||||
write!(
|
||||
f,
|
||||
@@ -65,10 +75,7 @@ impl Display for ApiError {
|
||||
}
|
||||
Self::Auth(message) => write!(f, "auth error: {message}"),
|
||||
Self::InvalidApiKeyEnv(error) => {
|
||||
write!(
|
||||
f,
|
||||
"failed to read ANTHROPIC_AUTH_TOKEN / ANTHROPIC_API_KEY: {error}"
|
||||
)
|
||||
write!(f, "failed to read credential environment variable: {error}")
|
||||
}
|
||||
Self::Http(error) => write!(f, "http error: {error}"),
|
||||
Self::Io(error) => write!(f, "io error: {error}"),
|
||||
@@ -81,20 +88,14 @@ impl Display for ApiError {
|
||||
..
|
||||
} => match (error_type, message) {
|
||||
(Some(error_type), Some(message)) => {
|
||||
write!(
|
||||
f,
|
||||
"anthropic api returned {status} ({error_type}): {message}"
|
||||
)
|
||||
write!(f, "api returned {status} ({error_type}): {message}")
|
||||
}
|
||||
_ => write!(f, "anthropic api returned {status}: {body}"),
|
||||
_ => write!(f, "api returned {status}: {body}"),
|
||||
},
|
||||
Self::RetriesExhausted {
|
||||
attempts,
|
||||
last_error,
|
||||
} => write!(
|
||||
f,
|
||||
"anthropic api failed after {attempts} attempts: {last_error}"
|
||||
),
|
||||
} => write!(f, "api failed after {attempts} attempts: {last_error}"),
|
||||
Self::InvalidSseFrame(message) => write!(f, "invalid sse frame: {message}"),
|
||||
Self::BackoffOverflow {
|
||||
attempt,
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
mod client;
|
||||
mod error;
|
||||
mod providers;
|
||||
mod sse;
|
||||
mod types;
|
||||
|
||||
pub use client::{
|
||||
oauth_token_is_expired, read_base_url, resolve_saved_oauth_token, resolve_startup_auth_source,
|
||||
AnthropicClient, AuthSource, MessageStream, OAuthTokenSet,
|
||||
oauth_token_is_expired, read_base_url, read_xai_base_url, resolve_saved_oauth_token,
|
||||
resolve_startup_auth_source, MessageStream, OAuthTokenSet, ProviderClient,
|
||||
};
|
||||
pub use error::ApiError;
|
||||
pub use providers::anthropic::{AnthropicClient, AuthSource};
|
||||
pub use providers::openai_compat::{OpenAiCompatClient, OpenAiCompatConfig};
|
||||
pub use providers::{
|
||||
detect_provider_kind, max_tokens_for_model, resolve_model_alias, ProviderKind,
|
||||
};
|
||||
pub use sse::{parse_frame, SseParser};
|
||||
pub use types::{
|
||||
ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent,
|
||||
|
||||
1038
rust/crates/api/src/providers/anthropic.rs
Normal file
1038
rust/crates/api/src/providers/anthropic.rs
Normal file
File diff suppressed because it is too large
Load Diff
216
rust/crates/api/src/providers/mod.rs
Normal file
216
rust/crates/api/src/providers/mod.rs
Normal file
@@ -0,0 +1,216 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use crate::error::ApiError;
|
||||
use crate::types::{MessageRequest, MessageResponse};
|
||||
|
||||
pub mod anthropic;
|
||||
pub mod openai_compat;
|
||||
|
||||
pub type ProviderFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, ApiError>> + Send + 'a>>;
|
||||
|
||||
pub trait Provider {
|
||||
type Stream;
|
||||
|
||||
fn send_message<'a>(
|
||||
&'a self,
|
||||
request: &'a MessageRequest,
|
||||
) -> ProviderFuture<'a, MessageResponse>;
|
||||
|
||||
fn stream_message<'a>(
|
||||
&'a self,
|
||||
request: &'a MessageRequest,
|
||||
) -> ProviderFuture<'a, Self::Stream>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ProviderKind {
|
||||
Anthropic,
|
||||
Xai,
|
||||
OpenAi,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ProviderMetadata {
|
||||
pub provider: ProviderKind,
|
||||
pub auth_env: &'static str,
|
||||
pub base_url_env: &'static str,
|
||||
pub default_base_url: &'static str,
|
||||
}
|
||||
|
||||
const MODEL_REGISTRY: &[(&str, ProviderMetadata)] = &[
|
||||
(
|
||||
"opus",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Anthropic,
|
||||
auth_env: "ANTHROPIC_API_KEY",
|
||||
base_url_env: "ANTHROPIC_BASE_URL",
|
||||
default_base_url: anthropic::DEFAULT_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"sonnet",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Anthropic,
|
||||
auth_env: "ANTHROPIC_API_KEY",
|
||||
base_url_env: "ANTHROPIC_BASE_URL",
|
||||
default_base_url: anthropic::DEFAULT_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"haiku",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Anthropic,
|
||||
auth_env: "ANTHROPIC_API_KEY",
|
||||
base_url_env: "ANTHROPIC_BASE_URL",
|
||||
default_base_url: anthropic::DEFAULT_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"grok",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Xai,
|
||||
auth_env: "XAI_API_KEY",
|
||||
base_url_env: "XAI_BASE_URL",
|
||||
default_base_url: openai_compat::DEFAULT_XAI_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"grok-3",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Xai,
|
||||
auth_env: "XAI_API_KEY",
|
||||
base_url_env: "XAI_BASE_URL",
|
||||
default_base_url: openai_compat::DEFAULT_XAI_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"grok-mini",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Xai,
|
||||
auth_env: "XAI_API_KEY",
|
||||
base_url_env: "XAI_BASE_URL",
|
||||
default_base_url: openai_compat::DEFAULT_XAI_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"grok-3-mini",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Xai,
|
||||
auth_env: "XAI_API_KEY",
|
||||
base_url_env: "XAI_BASE_URL",
|
||||
default_base_url: openai_compat::DEFAULT_XAI_BASE_URL,
|
||||
},
|
||||
),
|
||||
(
|
||||
"grok-2",
|
||||
ProviderMetadata {
|
||||
provider: ProviderKind::Xai,
|
||||
auth_env: "XAI_API_KEY",
|
||||
base_url_env: "XAI_BASE_URL",
|
||||
default_base_url: openai_compat::DEFAULT_XAI_BASE_URL,
|
||||
},
|
||||
),
|
||||
];
|
||||
|
||||
#[must_use]
|
||||
pub fn resolve_model_alias(model: &str) -> String {
|
||||
let trimmed = model.trim();
|
||||
let lower = trimmed.to_ascii_lowercase();
|
||||
MODEL_REGISTRY
|
||||
.iter()
|
||||
.find_map(|(alias, metadata)| {
|
||||
(*alias == lower).then_some(match metadata.provider {
|
||||
ProviderKind::Anthropic => match *alias {
|
||||
"opus" => "claude-opus-4-6",
|
||||
"sonnet" => "claude-sonnet-4-6",
|
||||
"haiku" => "claude-haiku-4-5-20251213",
|
||||
_ => trimmed,
|
||||
},
|
||||
ProviderKind::Xai => match *alias {
|
||||
"grok" | "grok-3" => "grok-3",
|
||||
"grok-mini" | "grok-3-mini" => "grok-3-mini",
|
||||
"grok-2" => "grok-2",
|
||||
_ => trimmed,
|
||||
},
|
||||
ProviderKind::OpenAi => trimmed,
|
||||
})
|
||||
})
|
||||
.map_or_else(|| trimmed.to_string(), ToOwned::to_owned)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn metadata_for_model(model: &str) -> Option<ProviderMetadata> {
|
||||
let canonical = resolve_model_alias(model);
|
||||
if canonical.starts_with("claude") {
|
||||
return Some(ProviderMetadata {
|
||||
provider: ProviderKind::Anthropic,
|
||||
auth_env: "ANTHROPIC_API_KEY",
|
||||
base_url_env: "ANTHROPIC_BASE_URL",
|
||||
default_base_url: anthropic::DEFAULT_BASE_URL,
|
||||
});
|
||||
}
|
||||
if canonical.starts_with("grok") {
|
||||
return Some(ProviderMetadata {
|
||||
provider: ProviderKind::Xai,
|
||||
auth_env: "XAI_API_KEY",
|
||||
base_url_env: "XAI_BASE_URL",
|
||||
default_base_url: openai_compat::DEFAULT_XAI_BASE_URL,
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn detect_provider_kind(model: &str) -> ProviderKind {
|
||||
if let Some(metadata) = metadata_for_model(model) {
|
||||
return metadata.provider;
|
||||
}
|
||||
if anthropic::has_auth_from_env_or_saved().unwrap_or(false) {
|
||||
return ProviderKind::Anthropic;
|
||||
}
|
||||
if openai_compat::has_api_key("OPENAI_API_KEY") {
|
||||
return ProviderKind::OpenAi;
|
||||
}
|
||||
if openai_compat::has_api_key("XAI_API_KEY") {
|
||||
return ProviderKind::Xai;
|
||||
}
|
||||
ProviderKind::Anthropic
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn max_tokens_for_model(model: &str) -> u32 {
|
||||
let canonical = resolve_model_alias(model);
|
||||
if canonical.contains("opus") {
|
||||
32_000
|
||||
} else {
|
||||
64_000
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{detect_provider_kind, max_tokens_for_model, resolve_model_alias, ProviderKind};
|
||||
|
||||
#[test]
|
||||
fn resolves_grok_aliases() {
|
||||
assert_eq!(resolve_model_alias("grok"), "grok-3");
|
||||
assert_eq!(resolve_model_alias("grok-mini"), "grok-3-mini");
|
||||
assert_eq!(resolve_model_alias("grok-2"), "grok-2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_provider_from_model_name_first() {
|
||||
assert_eq!(detect_provider_kind("grok"), ProviderKind::Xai);
|
||||
assert_eq!(
|
||||
detect_provider_kind("claude-sonnet-4-6"),
|
||||
ProviderKind::Anthropic
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_existing_max_token_heuristic() {
|
||||
assert_eq!(max_tokens_for_model("opus"), 32_000);
|
||||
assert_eq!(max_tokens_for_model("grok-3"), 64_000);
|
||||
}
|
||||
}
|
||||
1050
rust/crates/api/src/providers/openai_compat.rs
Normal file
1050
rust/crates/api/src/providers/openai_compat.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -3,9 +3,9 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use api::{
|
||||
AnthropicClient, ApiError, ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent,
|
||||
InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest, OutputContentBlock,
|
||||
StreamEvent, ToolChoice, ToolDefinition,
|
||||
AnthropicClient, ApiError, AuthSource, ContentBlockDelta, ContentBlockDeltaEvent,
|
||||
ContentBlockStartEvent, InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest,
|
||||
OutputContentBlock, ProviderClient, StreamEvent, ToolChoice, ToolDefinition,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
@@ -195,6 +195,47 @@ async fn retries_retryable_failures_before_succeeding() {
|
||||
assert_eq!(state.lock().await.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn provider_client_dispatches_anthropic_requests() {
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
let server = spawn_server(
|
||||
state.clone(),
|
||||
vec![http_response(
|
||||
"200 OK",
|
||||
"application/json",
|
||||
"{\"id\":\"msg_provider\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"Dispatched\"}],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":3,\"output_tokens\":2}}",
|
||||
)],
|
||||
)
|
||||
.await;
|
||||
|
||||
let client = ProviderClient::from_model_with_anthropic_auth(
|
||||
"claude-sonnet-4-6",
|
||||
Some(AuthSource::ApiKey("test-key".to_string())),
|
||||
)
|
||||
.expect("anthropic provider client should be constructed");
|
||||
let client = match client {
|
||||
ProviderClient::Anthropic(client) => {
|
||||
ProviderClient::Anthropic(client.with_base_url(server.base_url()))
|
||||
}
|
||||
other => panic!("expected anthropic provider, got {other:?}"),
|
||||
};
|
||||
|
||||
let response = client
|
||||
.send_message(&sample_request(false))
|
||||
.await
|
||||
.expect("provider-dispatched request should succeed");
|
||||
|
||||
assert_eq!(response.total_tokens(), 5);
|
||||
|
||||
let captured = state.lock().await;
|
||||
let request = captured.first().expect("server should capture request");
|
||||
assert_eq!(request.path, "/v1/messages");
|
||||
assert_eq!(
|
||||
request.headers.get("x-api-key").map(String::as_str),
|
||||
Some("test-key")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn surfaces_retry_exhaustion_for_persistent_retryable_errors() {
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
|
||||
415
rust/crates/api/tests/openai_compat_integration.rs
Normal file
415
rust/crates/api/tests/openai_compat_integration.rs
Normal file
@@ -0,0 +1,415 @@
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsString;
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Mutex as StdMutex, OnceLock};
|
||||
|
||||
use api::{
|
||||
ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent,
|
||||
InputContentBlock, InputMessage, MessageRequest, OpenAiCompatClient, OpenAiCompatConfig,
|
||||
OutputContentBlock, ProviderClient, StreamEvent, ToolChoice, ToolDefinition,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[tokio::test]
|
||||
async fn send_message_uses_openai_compatible_endpoint_and_auth() {
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
let body = concat!(
|
||||
"{",
|
||||
"\"id\":\"chatcmpl_test\",",
|
||||
"\"model\":\"grok-3\",",
|
||||
"\"choices\":[{",
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":\"Hello from Grok\",\"tool_calls\":[]},",
|
||||
"\"finish_reason\":\"stop\"",
|
||||
"}],",
|
||||
"\"usage\":{\"prompt_tokens\":11,\"completion_tokens\":5}",
|
||||
"}"
|
||||
);
|
||||
let server = spawn_server(
|
||||
state.clone(),
|
||||
vec![http_response("200 OK", "application/json", body)],
|
||||
)
|
||||
.await;
|
||||
|
||||
let client = OpenAiCompatClient::new("xai-test-key", OpenAiCompatConfig::xai())
|
||||
.with_base_url(server.base_url());
|
||||
let response = client
|
||||
.send_message(&sample_request(false))
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.model, "grok-3");
|
||||
assert_eq!(response.total_tokens(), 16);
|
||||
assert_eq!(
|
||||
response.content,
|
||||
vec![OutputContentBlock::Text {
|
||||
text: "Hello from Grok".to_string(),
|
||||
}]
|
||||
);
|
||||
|
||||
let captured = state.lock().await;
|
||||
let request = captured.first().expect("server should capture request");
|
||||
assert_eq!(request.path, "/chat/completions");
|
||||
assert_eq!(
|
||||
request.headers.get("authorization").map(String::as_str),
|
||||
Some("Bearer xai-test-key")
|
||||
);
|
||||
let body: serde_json::Value = serde_json::from_str(&request.body).expect("json body");
|
||||
assert_eq!(body["model"], json!("grok-3"));
|
||||
assert_eq!(body["messages"][0]["role"], json!("system"));
|
||||
assert_eq!(body["tools"][0]["type"], json!("function"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn send_message_accepts_full_chat_completions_endpoint_override() {
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
let body = concat!(
|
||||
"{",
|
||||
"\"id\":\"chatcmpl_full_endpoint\",",
|
||||
"\"model\":\"grok-3\",",
|
||||
"\"choices\":[{",
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":\"Endpoint override works\",\"tool_calls\":[]},",
|
||||
"\"finish_reason\":\"stop\"",
|
||||
"}],",
|
||||
"\"usage\":{\"prompt_tokens\":7,\"completion_tokens\":3}",
|
||||
"}"
|
||||
);
|
||||
let server = spawn_server(
|
||||
state.clone(),
|
||||
vec![http_response("200 OK", "application/json", body)],
|
||||
)
|
||||
.await;
|
||||
|
||||
let endpoint_url = format!("{}/chat/completions", server.base_url());
|
||||
let client = OpenAiCompatClient::new("xai-test-key", OpenAiCompatConfig::xai())
|
||||
.with_base_url(endpoint_url);
|
||||
let response = client
|
||||
.send_message(&sample_request(false))
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.total_tokens(), 10);
|
||||
|
||||
let captured = state.lock().await;
|
||||
let request = captured.first().expect("server should capture request");
|
||||
assert_eq!(request.path, "/chat/completions");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stream_message_normalizes_text_and_multiple_tool_calls() {
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
let sse = concat!(
|
||||
"data: {\"id\":\"chatcmpl_stream\",\"model\":\"grok-3\",\"choices\":[{\"delta\":{\"content\":\"Hello\"}}]}\n\n",
|
||||
"data: {\"id\":\"chatcmpl_stream\",\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"weather\",\"arguments\":\"{\\\"city\\\":\\\"Paris\\\"}\"}},{\"index\":1,\"id\":\"call_2\",\"function\":{\"name\":\"clock\",\"arguments\":\"{\\\"zone\\\":\\\"UTC\\\"}\"}}]}}]}\n\n",
|
||||
"data: {\"id\":\"chatcmpl_stream\",\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
|
||||
"data: [DONE]\n\n"
|
||||
);
|
||||
let server = spawn_server(
|
||||
state.clone(),
|
||||
vec![http_response_with_headers(
|
||||
"200 OK",
|
||||
"text/event-stream",
|
||||
sse,
|
||||
&[("x-request-id", "req_grok_stream")],
|
||||
)],
|
||||
)
|
||||
.await;
|
||||
|
||||
let client = OpenAiCompatClient::new("xai-test-key", OpenAiCompatConfig::xai())
|
||||
.with_base_url(server.base_url());
|
||||
let mut stream = client
|
||||
.stream_message(&sample_request(false))
|
||||
.await
|
||||
.expect("stream should start");
|
||||
|
||||
assert_eq!(stream.request_id(), Some("req_grok_stream"));
|
||||
|
||||
let mut events = Vec::new();
|
||||
while let Some(event) = stream.next_event().await.expect("event should parse") {
|
||||
events.push(event);
|
||||
}
|
||||
|
||||
assert!(matches!(events[0], StreamEvent::MessageStart(_)));
|
||||
assert!(matches!(
|
||||
events[1],
|
||||
StreamEvent::ContentBlockStart(ContentBlockStartEvent {
|
||||
content_block: OutputContentBlock::Text { .. },
|
||||
..
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
events[2],
|
||||
StreamEvent::ContentBlockDelta(ContentBlockDeltaEvent {
|
||||
delta: ContentBlockDelta::TextDelta { .. },
|
||||
..
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
events[3],
|
||||
StreamEvent::ContentBlockStart(ContentBlockStartEvent {
|
||||
index: 1,
|
||||
content_block: OutputContentBlock::ToolUse { .. },
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
events[4],
|
||||
StreamEvent::ContentBlockDelta(ContentBlockDeltaEvent {
|
||||
index: 1,
|
||||
delta: ContentBlockDelta::InputJsonDelta { .. },
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
events[5],
|
||||
StreamEvent::ContentBlockStart(ContentBlockStartEvent {
|
||||
index: 2,
|
||||
content_block: OutputContentBlock::ToolUse { .. },
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
events[6],
|
||||
StreamEvent::ContentBlockDelta(ContentBlockDeltaEvent {
|
||||
index: 2,
|
||||
delta: ContentBlockDelta::InputJsonDelta { .. },
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
events[7],
|
||||
StreamEvent::ContentBlockStop(ContentBlockStopEvent { index: 1 })
|
||||
));
|
||||
assert!(matches!(
|
||||
events[8],
|
||||
StreamEvent::ContentBlockStop(ContentBlockStopEvent { index: 2 })
|
||||
));
|
||||
assert!(matches!(
|
||||
events[9],
|
||||
StreamEvent::ContentBlockStop(ContentBlockStopEvent { index: 0 })
|
||||
));
|
||||
assert!(matches!(events[10], StreamEvent::MessageDelta(_)));
|
||||
assert!(matches!(events[11], StreamEvent::MessageStop(_)));
|
||||
|
||||
let captured = state.lock().await;
|
||||
let request = captured.first().expect("captured request");
|
||||
assert_eq!(request.path, "/chat/completions");
|
||||
assert!(request.body.contains("\"stream\":true"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn provider_client_dispatches_xai_requests_from_env() {
|
||||
let _lock = env_lock();
|
||||
let _api_key = ScopedEnvVar::set("XAI_API_KEY", "xai-test-key");
|
||||
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
let server = spawn_server(
|
||||
state.clone(),
|
||||
vec![http_response(
|
||||
"200 OK",
|
||||
"application/json",
|
||||
"{\"id\":\"chatcmpl_provider\",\"model\":\"grok-3\",\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"Through provider client\",\"tool_calls\":[]},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":9,\"completion_tokens\":4}}",
|
||||
)],
|
||||
)
|
||||
.await;
|
||||
let _base_url = ScopedEnvVar::set("XAI_BASE_URL", server.base_url());
|
||||
|
||||
let client =
|
||||
ProviderClient::from_model("grok").expect("xAI provider client should be constructed");
|
||||
assert!(matches!(client, ProviderClient::Xai(_)));
|
||||
|
||||
let response = client
|
||||
.send_message(&sample_request(false))
|
||||
.await
|
||||
.expect("provider-dispatched request should succeed");
|
||||
|
||||
assert_eq!(response.total_tokens(), 13);
|
||||
|
||||
let captured = state.lock().await;
|
||||
let request = captured.first().expect("captured request");
|
||||
assert_eq!(request.path, "/chat/completions");
|
||||
assert_eq!(
|
||||
request.headers.get("authorization").map(String::as_str),
|
||||
Some("Bearer xai-test-key")
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct CapturedRequest {
|
||||
path: String,
|
||||
headers: HashMap<String, String>,
|
||||
body: String,
|
||||
}
|
||||
|
||||
struct TestServer {
|
||||
base_url: String,
|
||||
join_handle: tokio::task::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl TestServer {
|
||||
fn base_url(&self) -> String {
|
||||
self.base_url.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TestServer {
|
||||
fn drop(&mut self) {
|
||||
self.join_handle.abort();
|
||||
}
|
||||
}
|
||||
|
||||
async fn spawn_server(
|
||||
state: Arc<Mutex<Vec<CapturedRequest>>>,
|
||||
responses: Vec<String>,
|
||||
) -> TestServer {
|
||||
let listener = TcpListener::bind("127.0.0.1:0")
|
||||
.await
|
||||
.expect("listener should bind");
|
||||
let address = listener.local_addr().expect("listener addr");
|
||||
let join_handle = tokio::spawn(async move {
|
||||
for response in responses {
|
||||
let (mut socket, _) = listener.accept().await.expect("accept");
|
||||
let mut buffer = Vec::new();
|
||||
let mut header_end = None;
|
||||
loop {
|
||||
let mut chunk = [0_u8; 1024];
|
||||
let read = socket.read(&mut chunk).await.expect("read request");
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
buffer.extend_from_slice(&chunk[..read]);
|
||||
if let Some(position) = find_header_end(&buffer) {
|
||||
header_end = Some(position);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let header_end = header_end.expect("headers should exist");
|
||||
let (header_bytes, remaining) = buffer.split_at(header_end);
|
||||
let header_text = String::from_utf8(header_bytes.to_vec()).expect("utf8 headers");
|
||||
let mut lines = header_text.split("\r\n");
|
||||
let request_line = lines.next().expect("request line");
|
||||
let path = request_line
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.expect("path")
|
||||
.to_string();
|
||||
let mut headers = HashMap::new();
|
||||
let mut content_length = 0_usize;
|
||||
for line in lines {
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let (name, value) = line.split_once(':').expect("header");
|
||||
let value = value.trim().to_string();
|
||||
if name.eq_ignore_ascii_case("content-length") {
|
||||
content_length = value.parse().expect("content length");
|
||||
}
|
||||
headers.insert(name.to_ascii_lowercase(), value);
|
||||
}
|
||||
|
||||
let mut body = remaining[4..].to_vec();
|
||||
while body.len() < content_length {
|
||||
let mut chunk = vec![0_u8; content_length - body.len()];
|
||||
let read = socket.read(&mut chunk).await.expect("read body");
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
body.extend_from_slice(&chunk[..read]);
|
||||
}
|
||||
|
||||
state.lock().await.push(CapturedRequest {
|
||||
path,
|
||||
headers,
|
||||
body: String::from_utf8(body).expect("utf8 body"),
|
||||
});
|
||||
|
||||
socket
|
||||
.write_all(response.as_bytes())
|
||||
.await
|
||||
.expect("write response");
|
||||
}
|
||||
});
|
||||
|
||||
TestServer {
|
||||
base_url: format!("http://{address}"),
|
||||
join_handle,
|
||||
}
|
||||
}
|
||||
|
||||
fn find_header_end(bytes: &[u8]) -> Option<usize> {
|
||||
bytes.windows(4).position(|window| window == b"\r\n\r\n")
|
||||
}
|
||||
|
||||
fn http_response(status: &str, content_type: &str, body: &str) -> String {
|
||||
http_response_with_headers(status, content_type, body, &[])
|
||||
}
|
||||
|
||||
fn http_response_with_headers(
|
||||
status: &str,
|
||||
content_type: &str,
|
||||
body: &str,
|
||||
headers: &[(&str, &str)],
|
||||
) -> String {
|
||||
let mut extra_headers = String::new();
|
||||
for (name, value) in headers {
|
||||
use std::fmt::Write as _;
|
||||
write!(&mut extra_headers, "{name}: {value}\r\n").expect("header write");
|
||||
}
|
||||
format!(
|
||||
"HTTP/1.1 {status}\r\ncontent-type: {content_type}\r\n{extra_headers}content-length: {}\r\nconnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
)
|
||||
}
|
||||
|
||||
fn sample_request(stream: bool) -> MessageRequest {
|
||||
MessageRequest {
|
||||
model: "grok-3".to_string(),
|
||||
max_tokens: 64,
|
||||
messages: vec![InputMessage {
|
||||
role: "user".to_string(),
|
||||
content: vec![InputContentBlock::Text {
|
||||
text: "Say hello".to_string(),
|
||||
}],
|
||||
}],
|
||||
system: Some("Use tools when needed".to_string()),
|
||||
tools: Some(vec![ToolDefinition {
|
||||
name: "weather".to_string(),
|
||||
description: Some("Fetches weather".to_string()),
|
||||
input_schema: json!({
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
"required": ["city"]
|
||||
}),
|
||||
}]),
|
||||
tool_choice: Some(ToolChoice::Auto),
|
||||
stream,
|
||||
}
|
||||
}
|
||||
|
||||
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
|
||||
static LOCK: OnceLock<StdMutex<()>> = OnceLock::new();
|
||||
LOCK.get_or_init(|| StdMutex::new(()))
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
}
|
||||
|
||||
struct ScopedEnvVar {
|
||||
key: &'static str,
|
||||
previous: Option<OsString>,
|
||||
}
|
||||
|
||||
impl ScopedEnvVar {
|
||||
fn set(key: &'static str, value: impl AsRef<std::ffi::OsStr>) -> Self {
|
||||
let previous = std::env::var_os(key);
|
||||
std::env::set_var(key, value);
|
||||
Self { key, previous }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ScopedEnvVar {
|
||||
fn drop(&mut self) {
|
||||
match &self.previous {
|
||||
Some(value) => std::env::set_var(self.key, value),
|
||||
None => std::env::remove_var(self.key),
|
||||
}
|
||||
}
|
||||
}
|
||||
86
rust/crates/api/tests/provider_client_integration.rs
Normal file
86
rust/crates/api/tests/provider_client_integration.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use std::ffi::OsString;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
use api::{read_xai_base_url, ApiError, AuthSource, ProviderClient, ProviderKind};
|
||||
|
||||
#[test]
|
||||
fn provider_client_routes_grok_aliases_through_xai() {
|
||||
let _lock = env_lock();
|
||||
let _xai_api_key = EnvVarGuard::set("XAI_API_KEY", Some("xai-test-key"));
|
||||
|
||||
let client = ProviderClient::from_model("grok-mini").expect("grok alias should resolve");
|
||||
|
||||
assert_eq!(client.provider_kind(), ProviderKind::Xai);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_client_reports_missing_xai_credentials_for_grok_models() {
|
||||
let _lock = env_lock();
|
||||
let _xai_api_key = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
|
||||
let error = ProviderClient::from_model("grok-3")
|
||||
.expect_err("grok requests without XAI_API_KEY should fail fast");
|
||||
|
||||
match error {
|
||||
ApiError::MissingCredentials { provider, env_vars } => {
|
||||
assert_eq!(provider, "xAI");
|
||||
assert_eq!(env_vars, &["XAI_API_KEY"]);
|
||||
}
|
||||
other => panic!("expected missing xAI credentials, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_client_uses_explicit_anthropic_auth_without_env_lookup() {
|
||||
let _lock = env_lock();
|
||||
let _anthropic_api_key = EnvVarGuard::set("ANTHROPIC_API_KEY", None);
|
||||
let _anthropic_auth_token = EnvVarGuard::set("ANTHROPIC_AUTH_TOKEN", None);
|
||||
|
||||
let client = ProviderClient::from_model_with_anthropic_auth(
|
||||
"claude-sonnet-4-6",
|
||||
Some(AuthSource::ApiKey("anthropic-test-key".to_string())),
|
||||
)
|
||||
.expect("explicit anthropic auth should avoid env lookup");
|
||||
|
||||
assert_eq!(client.provider_kind(), ProviderKind::Anthropic);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_xai_base_url_prefers_env_override() {
|
||||
let _lock = env_lock();
|
||||
let _xai_base_url = EnvVarGuard::set("XAI_BASE_URL", Some("https://example.xai.test/v1"));
|
||||
|
||||
assert_eq!(read_xai_base_url(), "https://example.xai.test/v1");
|
||||
}
|
||||
|
||||
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
|
||||
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
|
||||
LOCK.get_or_init(|| Mutex::new(()))
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
}
|
||||
|
||||
struct EnvVarGuard {
|
||||
key: &'static str,
|
||||
original: Option<OsString>,
|
||||
}
|
||||
|
||||
impl EnvVarGuard {
|
||||
fn set(key: &'static str, value: Option<&str>) -> Self {
|
||||
let original = std::env::var_os(key);
|
||||
match value {
|
||||
Some(value) => std::env::set_var(key, value),
|
||||
None => std::env::remove_var(key),
|
||||
}
|
||||
Self { key, original }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for EnvVarGuard {
|
||||
fn drop(&mut self) {
|
||||
match &self.original {
|
||||
Some(value) => std::env::set_var(self.key, value),
|
||||
None => std::env::remove_var(self.key),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,8 +125,8 @@ const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[
|
||||
},
|
||||
SlashCommandSpec {
|
||||
name: "session",
|
||||
summary: "List, switch, or fork managed local sessions",
|
||||
argument_hint: Some("[list|switch <session-id>|fork [branch-name]]"),
|
||||
summary: "List or switch managed local sessions",
|
||||
argument_hint: Some("[list|switch <session-id>]"),
|
||||
resume_supported: false,
|
||||
},
|
||||
];
|
||||
@@ -229,7 +229,7 @@ pub fn resume_supported_slash_commands() -> Vec<&'static SlashCommandSpec> {
|
||||
pub fn render_slash_command_help() -> String {
|
||||
let mut lines = vec![
|
||||
"Slash commands".to_string(),
|
||||
" [resume] means the command also works with --resume SESSION.jsonl".to_string(),
|
||||
" [resume] means the command also works with --resume SESSION.json".to_string(),
|
||||
];
|
||||
for spec in slash_command_specs() {
|
||||
let name = match spec.argument_hint {
|
||||
@@ -365,19 +365,12 @@ mod tests {
|
||||
target: Some("abc123".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
SlashCommand::parse("/session fork incident-review"),
|
||||
Some(SlashCommand::Session {
|
||||
action: Some("fork".to_string()),
|
||||
target: Some("incident-review".to_string())
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_help_from_shared_specs() {
|
||||
let help = render_slash_command_help();
|
||||
assert!(help.contains("works with --resume SESSION.jsonl"));
|
||||
assert!(help.contains("works with --resume SESSION.json"));
|
||||
assert!(help.contains("/help"));
|
||||
assert!(help.contains("/status"));
|
||||
assert!(help.contains("/compact"));
|
||||
@@ -392,24 +385,26 @@ mod tests {
|
||||
assert!(help.contains("/diff"));
|
||||
assert!(help.contains("/version"));
|
||||
assert!(help.contains("/export [file]"));
|
||||
assert!(help.contains("/session [list|switch <session-id>|fork [branch-name]]"));
|
||||
assert!(help.contains("/session [list|switch <session-id>]"));
|
||||
assert_eq!(slash_command_specs().len(), 15);
|
||||
assert_eq!(resume_supported_slash_commands().len(), 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compacts_sessions_via_slash_command() {
|
||||
let mut session = Session::new();
|
||||
session.messages = vec![
|
||||
ConversationMessage::user_text("a ".repeat(200)),
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "b ".repeat(200),
|
||||
}]),
|
||||
ConversationMessage::tool_result("1", "bash", "ok ".repeat(200), false),
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "recent".to_string(),
|
||||
}]),
|
||||
];
|
||||
let session = Session {
|
||||
version: 1,
|
||||
messages: vec![
|
||||
ConversationMessage::user_text("a ".repeat(200)),
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "b ".repeat(200),
|
||||
}]),
|
||||
ConversationMessage::tool_result("1", "bash", "ok ".repeat(200), false),
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "recent".to_string(),
|
||||
}]),
|
||||
],
|
||||
};
|
||||
|
||||
let result = handle_slash_command(
|
||||
"/compact",
|
||||
@@ -460,12 +455,6 @@ mod tests {
|
||||
CompactionConfig::default()
|
||||
)
|
||||
.is_none());
|
||||
assert!(handle_slash_command(
|
||||
"/resume session.jsonl",
|
||||
&session,
|
||||
CompactionConfig::default()
|
||||
)
|
||||
.is_none());
|
||||
assert!(handle_slash_command("/config", &session, CompactionConfig::default()).is_none());
|
||||
assert!(
|
||||
handle_slash_command("/config env", &session, CompactionConfig::default()).is_none()
|
||||
|
||||
@@ -99,14 +99,13 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
||||
}];
|
||||
compacted_messages.extend(preserved);
|
||||
|
||||
let mut compacted_session = session.clone();
|
||||
compacted_session.messages = compacted_messages;
|
||||
compacted_session.record_compaction(summary.clone(), removed.len());
|
||||
|
||||
CompactionResult {
|
||||
summary,
|
||||
formatted_summary,
|
||||
compacted_session,
|
||||
compacted_session: Session {
|
||||
version: session.version,
|
||||
messages: compacted_messages,
|
||||
},
|
||||
removed_message_count: removed.len(),
|
||||
}
|
||||
}
|
||||
@@ -391,8 +390,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn leaves_small_sessions_unchanged() {
|
||||
let mut session = Session::new();
|
||||
session.messages = vec![ConversationMessage::user_text("hello")];
|
||||
let session = Session {
|
||||
version: 1,
|
||||
messages: vec![ConversationMessage::user_text("hello")],
|
||||
};
|
||||
|
||||
let result = compact_session(&session, CompactionConfig::default());
|
||||
assert_eq!(result.removed_message_count, 0);
|
||||
@@ -403,21 +404,23 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn compacts_older_messages_into_a_system_summary() {
|
||||
let mut session = Session::new();
|
||||
session.messages = vec![
|
||||
ConversationMessage::user_text("one ".repeat(200)),
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "two ".repeat(200),
|
||||
}]),
|
||||
ConversationMessage::tool_result("1", "bash", "ok ".repeat(200), false),
|
||||
ConversationMessage {
|
||||
role: MessageRole::Assistant,
|
||||
blocks: vec![ContentBlock::Text {
|
||||
text: "recent".to_string(),
|
||||
}],
|
||||
usage: None,
|
||||
},
|
||||
];
|
||||
let session = Session {
|
||||
version: 1,
|
||||
messages: vec![
|
||||
ConversationMessage::user_text("one ".repeat(200)),
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "two ".repeat(200),
|
||||
}]),
|
||||
ConversationMessage::tool_result("1", "bash", "ok ".repeat(200), false),
|
||||
ConversationMessage {
|
||||
role: MessageRole::Assistant,
|
||||
blocks: vec![ContentBlock::Text {
|
||||
text: "recent".to_string(),
|
||||
}],
|
||||
usage: None,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
let result = compact_session(
|
||||
&session,
|
||||
|
||||
@@ -118,7 +118,7 @@ where
|
||||
tool_executor,
|
||||
permission_policy,
|
||||
system_prompt,
|
||||
RuntimeFeatureConfig::default(),
|
||||
&RuntimeFeatureConfig::default(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ where
|
||||
tool_executor: T,
|
||||
permission_policy: PermissionPolicy,
|
||||
system_prompt: Vec<String>,
|
||||
feature_config: RuntimeFeatureConfig,
|
||||
feature_config: &RuntimeFeatureConfig,
|
||||
) -> Self {
|
||||
let usage_tracker = UsageTracker::from_session(&session);
|
||||
Self {
|
||||
@@ -140,7 +140,7 @@ where
|
||||
system_prompt,
|
||||
max_iterations: usize::MAX,
|
||||
usage_tracker,
|
||||
hook_runner: HookRunner::from_feature_config(&feature_config),
|
||||
hook_runner: HookRunner::from_feature_config(feature_config),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,8 +156,8 @@ where
|
||||
mut prompter: Option<&mut dyn PermissionPrompter>,
|
||||
) -> Result<TurnSummary, RuntimeError> {
|
||||
self.session
|
||||
.push_user_text(user_input.into())
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
.messages
|
||||
.push(ConversationMessage::user_text(user_input.into()));
|
||||
|
||||
let mut assistant_messages = Vec::new();
|
||||
let mut tool_results = Vec::new();
|
||||
@@ -191,9 +191,7 @@ where
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
self.session
|
||||
.push_message(assistant_message.clone())
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
self.session.messages.push(assistant_message.clone());
|
||||
assistant_messages.push(assistant_message);
|
||||
|
||||
if pending_tool_uses.is_empty() {
|
||||
@@ -251,9 +249,7 @@ where
|
||||
ConversationMessage::tool_result(tool_use_id, tool_name, reason, true)
|
||||
}
|
||||
};
|
||||
self.session
|
||||
.push_message(result_message.clone())
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
self.session.messages.push(result_message.clone());
|
||||
tool_results.push(result_message);
|
||||
}
|
||||
}
|
||||
@@ -286,11 +282,6 @@ where
|
||||
&self.session
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn fork_session(&self, branch_name: Option<String>) -> Session {
|
||||
self.session.fork(branch_name)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn into_session(self) -> Session {
|
||||
self.session
|
||||
@@ -417,9 +408,7 @@ mod tests {
|
||||
use crate::prompt::{ProjectContext, SystemPromptBuilder};
|
||||
use crate::session::{ContentBlock, MessageRole, Session};
|
||||
use crate::usage::TokenUsage;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
struct ScriptedApiClient {
|
||||
call_count: usize,
|
||||
@@ -620,7 +609,7 @@ mod tests {
|
||||
}),
|
||||
PermissionPolicy::new(PermissionMode::DangerFullAccess),
|
||||
vec!["system".to_string()],
|
||||
RuntimeFeatureConfig::default().with_hooks(RuntimeHookConfig::new(
|
||||
&RuntimeFeatureConfig::default().with_hooks(RuntimeHookConfig::new(
|
||||
vec![shell_snippet("printf 'blocked by hook'; exit 2")],
|
||||
Vec::new(),
|
||||
)),
|
||||
@@ -686,7 +675,7 @@ mod tests {
|
||||
StaticToolExecutor::new().register("add", |_input| Ok("4".to_string())),
|
||||
PermissionPolicy::new(PermissionMode::DangerFullAccess),
|
||||
vec!["system".to_string()],
|
||||
RuntimeFeatureConfig::default().with_hooks(RuntimeHookConfig::new(
|
||||
&RuntimeFeatureConfig::default().with_hooks(RuntimeHookConfig::new(
|
||||
vec![shell_snippet("printf 'pre hook ran'")],
|
||||
vec![shell_snippet("printf 'post hook ran'")],
|
||||
)),
|
||||
@@ -708,7 +697,7 @@ mod tests {
|
||||
"post hook should preserve non-error result: {output:?}"
|
||||
);
|
||||
assert!(
|
||||
output.contains("4"),
|
||||
output.contains('4'),
|
||||
"tool output missing value: {output:?}"
|
||||
);
|
||||
assert!(
|
||||
@@ -798,86 +787,6 @@ mod tests {
|
||||
result.compacted_session.messages[0].role,
|
||||
MessageRole::System
|
||||
);
|
||||
assert_eq!(
|
||||
result.compacted_session.session_id,
|
||||
runtime.session().session_id
|
||||
);
|
||||
assert!(result.compacted_session.compaction.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persists_conversation_turn_messages_to_jsonl_session() {
|
||||
struct SimpleApi;
|
||||
impl ApiClient for SimpleApi {
|
||||
fn stream(
|
||||
&mut self,
|
||||
_request: ApiRequest,
|
||||
) -> Result<Vec<AssistantEvent>, RuntimeError> {
|
||||
Ok(vec![
|
||||
AssistantEvent::TextDelta("done".to_string()),
|
||||
AssistantEvent::MessageStop,
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
let path = temp_session_path("persisted-turn");
|
||||
let session = Session::new().with_persistence_path(path.clone());
|
||||
let mut runtime = ConversationRuntime::new(
|
||||
session,
|
||||
SimpleApi,
|
||||
StaticToolExecutor::new(),
|
||||
PermissionPolicy::new(PermissionMode::DangerFullAccess),
|
||||
vec!["system".to_string()],
|
||||
);
|
||||
|
||||
runtime
|
||||
.run_turn("persist this turn", None)
|
||||
.expect("turn should succeed");
|
||||
|
||||
let restored = Session::load_from_path(&path).expect("persisted session should reload");
|
||||
fs::remove_file(&path).expect("temp session file should be removable");
|
||||
|
||||
assert_eq!(restored.messages.len(), 2);
|
||||
assert_eq!(restored.messages[0].role, MessageRole::User);
|
||||
assert_eq!(restored.messages[1].role, MessageRole::Assistant);
|
||||
assert_eq!(restored.session_id, runtime.session().session_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forks_runtime_session_without_mutating_original() {
|
||||
let mut session = Session::new();
|
||||
session
|
||||
.push_user_text("branch me")
|
||||
.expect("message should append");
|
||||
|
||||
let runtime = ConversationRuntime::new(
|
||||
session.clone(),
|
||||
ScriptedApiClient { call_count: 0 },
|
||||
StaticToolExecutor::new(),
|
||||
PermissionPolicy::new(PermissionMode::DangerFullAccess),
|
||||
vec!["system".to_string()],
|
||||
);
|
||||
|
||||
let forked = runtime.fork_session(Some("alt-path".to_string()));
|
||||
|
||||
assert_eq!(forked.messages, session.messages);
|
||||
assert_ne!(forked.session_id, session.session_id);
|
||||
assert_eq!(
|
||||
forked
|
||||
.fork
|
||||
.as_ref()
|
||||
.map(|fork| (fork.parent_session_id.as_str(), fork.branch_name.as_deref())),
|
||||
Some((session.session_id.as_str(), Some("alt-path")))
|
||||
);
|
||||
assert!(runtime.session().fork.is_none());
|
||||
}
|
||||
|
||||
fn temp_session_path(label: &str) -> PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("system time should be after epoch")
|
||||
.as_nanos();
|
||||
std::env::temp_dir().join(format!("runtime-conversation-{label}-{nanos}.json"))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -51,6 +51,16 @@ pub struct HookRunner {
|
||||
config: RuntimeHookConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct HookCommandRequest<'a> {
|
||||
event: HookEvent,
|
||||
tool_name: &'a str,
|
||||
tool_input: &'a str,
|
||||
tool_output: Option<&'a str>,
|
||||
is_error: bool,
|
||||
payload: &'a str,
|
||||
}
|
||||
|
||||
impl HookRunner {
|
||||
#[must_use]
|
||||
pub fn new(config: RuntimeHookConfig) -> Self {
|
||||
@@ -118,14 +128,16 @@ impl HookRunner {
|
||||
let mut messages = Vec::new();
|
||||
|
||||
for command in commands {
|
||||
match self.run_command(
|
||||
match Self::run_command(
|
||||
command,
|
||||
event,
|
||||
tool_name,
|
||||
tool_input,
|
||||
tool_output,
|
||||
is_error,
|
||||
&payload,
|
||||
HookCommandRequest {
|
||||
event,
|
||||
tool_name,
|
||||
tool_input,
|
||||
tool_output,
|
||||
is_error,
|
||||
payload: &payload,
|
||||
},
|
||||
) {
|
||||
HookCommandOutcome::Allow { message } => {
|
||||
if let Some(message) = message {
|
||||
@@ -149,29 +161,23 @@ impl HookRunner {
|
||||
HookRunResult::allow(messages)
|
||||
}
|
||||
|
||||
fn run_command(
|
||||
&self,
|
||||
command: &str,
|
||||
event: HookEvent,
|
||||
tool_name: &str,
|
||||
tool_input: &str,
|
||||
tool_output: Option<&str>,
|
||||
is_error: bool,
|
||||
payload: &str,
|
||||
) -> HookCommandOutcome {
|
||||
fn run_command(command: &str, request: HookCommandRequest<'_>) -> HookCommandOutcome {
|
||||
let mut child = shell_command(command);
|
||||
child.stdin(std::process::Stdio::piped());
|
||||
child.stdout(std::process::Stdio::piped());
|
||||
child.stderr(std::process::Stdio::piped());
|
||||
child.env("HOOK_EVENT", event.as_str());
|
||||
child.env("HOOK_TOOL_NAME", tool_name);
|
||||
child.env("HOOK_TOOL_INPUT", tool_input);
|
||||
child.env("HOOK_TOOL_IS_ERROR", if is_error { "1" } else { "0" });
|
||||
if let Some(tool_output) = tool_output {
|
||||
child.env("HOOK_EVENT", request.event.as_str());
|
||||
child.env("HOOK_TOOL_NAME", request.tool_name);
|
||||
child.env("HOOK_TOOL_INPUT", request.tool_input);
|
||||
child.env(
|
||||
"HOOK_TOOL_IS_ERROR",
|
||||
if request.is_error { "1" } else { "0" },
|
||||
);
|
||||
if let Some(tool_output) = request.tool_output {
|
||||
child.env("HOOK_TOOL_OUTPUT", tool_output);
|
||||
}
|
||||
|
||||
match child.output_with_stdin(payload.as_bytes()) {
|
||||
match child.output_with_stdin(request.payload.as_bytes()) {
|
||||
Ok(output) => {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
|
||||
@@ -189,16 +195,18 @@ impl HookRunner {
|
||||
},
|
||||
None => HookCommandOutcome::Warn {
|
||||
message: format!(
|
||||
"{} hook `{command}` terminated by signal while handling `{tool_name}`",
|
||||
event.as_str()
|
||||
"{} hook `{command}` terminated by signal while handling `{}`",
|
||||
request.event.as_str(),
|
||||
request.tool_name
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
Err(error) => HookCommandOutcome::Warn {
|
||||
message: format!(
|
||||
"{} hook `{command}` failed to start for `{tool_name}`: {error}",
|
||||
event.as_str()
|
||||
"{} hook `{command}` failed to start for `{}`: {error}",
|
||||
request.event.as_str(),
|
||||
request.tool_name
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -76,10 +76,7 @@ pub use remote::{
|
||||
RemoteSessionContext, UpstreamProxyBootstrap, UpstreamProxyState, DEFAULT_REMOTE_BASE_URL,
|
||||
DEFAULT_SESSION_TOKEN_PATH, DEFAULT_SYSTEM_CA_BUNDLE, NO_PROXY_HOSTS, UPSTREAM_PROXY_ENV_KEYS,
|
||||
};
|
||||
pub use session::{
|
||||
ContentBlock, ConversationMessage, MessageRole, Session, SessionCompaction, SessionError,
|
||||
SessionFork,
|
||||
};
|
||||
pub use session::{ContentBlock, ConversationMessage, MessageRole, Session, SessionError};
|
||||
pub use usage::{
|
||||
format_usd, pricing_for_model, ModelPricing, TokenUsage, UsageCostEstimate, UsageTracker,
|
||||
};
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::json::{JsonError, JsonValue};
|
||||
use crate::usage::TokenUsage;
|
||||
|
||||
const SESSION_VERSION: u32 = 1;
|
||||
const ROTATE_AFTER_BYTES: u64 = 256 * 1024;
|
||||
const MAX_ROTATED_FILES: usize = 3;
|
||||
static SESSION_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MessageRole {
|
||||
System,
|
||||
@@ -48,49 +40,11 @@ pub struct ConversationMessage {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SessionCompaction {
|
||||
pub count: u32,
|
||||
pub removed_message_count: usize,
|
||||
pub summary: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SessionFork {
|
||||
pub parent_session_id: String,
|
||||
pub branch_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct SessionPersistence {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Session {
|
||||
pub version: u32,
|
||||
pub session_id: String,
|
||||
pub created_at_ms: u64,
|
||||
pub updated_at_ms: u64,
|
||||
pub messages: Vec<ConversationMessage>,
|
||||
pub compaction: Option<SessionCompaction>,
|
||||
pub fork: Option<SessionFork>,
|
||||
persistence: Option<SessionPersistence>,
|
||||
}
|
||||
|
||||
impl PartialEq for Session {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.version == other.version
|
||||
&& self.session_id == other.session_id
|
||||
&& self.created_at_ms == other.created_at_ms
|
||||
&& self.updated_at_ms == other.updated_at_ms
|
||||
&& self.messages == other.messages
|
||||
&& self.compaction == other.compaction
|
||||
&& self.fork == other.fork
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Session {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SessionError {
|
||||
Io(std::io::Error),
|
||||
@@ -125,84 +79,20 @@ impl From<JsonError> for SessionError {
|
||||
impl Session {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
let now = current_time_millis();
|
||||
Self {
|
||||
version: SESSION_VERSION,
|
||||
session_id: generate_session_id(),
|
||||
created_at_ms: now,
|
||||
updated_at_ms: now,
|
||||
version: 1,
|
||||
messages: Vec::new(),
|
||||
compaction: None,
|
||||
fork: None,
|
||||
persistence: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_persistence_path(mut self, path: impl Into<PathBuf>) -> Self {
|
||||
self.persistence = Some(SessionPersistence { path: path.into() });
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn persistence_path(&self) -> Option<&Path> {
|
||||
self.persistence.as_ref().map(|value| value.path.as_path())
|
||||
}
|
||||
|
||||
pub fn save_to_path(&self, path: impl AsRef<Path>) -> Result<(), SessionError> {
|
||||
let path = path.as_ref();
|
||||
rotate_session_file_if_needed(path)?;
|
||||
write_atomic(path, &self.render_jsonl_snapshot())?;
|
||||
cleanup_rotated_logs(path)?;
|
||||
fs::write(path, self.to_json().render())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, SessionError> {
|
||||
let path = path.as_ref();
|
||||
let contents = fs::read_to_string(path)?;
|
||||
let session = match JsonValue::parse(&contents) {
|
||||
Ok(value) => Self::from_json(&value)?,
|
||||
Err(_) => Self::from_jsonl(&contents)?,
|
||||
};
|
||||
Ok(session.with_persistence_path(path.to_path_buf()))
|
||||
}
|
||||
|
||||
pub fn push_message(&mut self, message: ConversationMessage) -> Result<(), SessionError> {
|
||||
self.touch();
|
||||
self.messages.push(message.clone());
|
||||
self.append_persisted_message(&message)
|
||||
}
|
||||
|
||||
pub fn push_user_text(&mut self, text: impl Into<String>) -> Result<(), SessionError> {
|
||||
self.push_message(ConversationMessage::user_text(text))
|
||||
}
|
||||
|
||||
pub fn record_compaction(&mut self, summary: impl Into<String>, removed_message_count: usize) {
|
||||
self.touch();
|
||||
let count = self.compaction.as_ref().map_or(1, |value| value.count + 1);
|
||||
self.compaction = Some(SessionCompaction {
|
||||
count,
|
||||
removed_message_count,
|
||||
summary: summary.into(),
|
||||
});
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn fork(&self, branch_name: Option<String>) -> Self {
|
||||
let now = current_time_millis();
|
||||
Self {
|
||||
version: self.version,
|
||||
session_id: generate_session_id(),
|
||||
created_at_ms: now,
|
||||
updated_at_ms: now,
|
||||
messages: self.messages.clone(),
|
||||
compaction: self.compaction.clone(),
|
||||
fork: Some(SessionFork {
|
||||
parent_session_id: self.session_id.clone(),
|
||||
branch_name: normalize_optional_string(branch_name),
|
||||
}),
|
||||
persistence: None,
|
||||
}
|
||||
Self::from_json(&JsonValue::parse(&contents)?)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
@@ -212,18 +102,6 @@ impl Session {
|
||||
"version".to_string(),
|
||||
JsonValue::Number(i64::from(self.version)),
|
||||
);
|
||||
object.insert(
|
||||
"session_id".to_string(),
|
||||
JsonValue::String(self.session_id.clone()),
|
||||
);
|
||||
object.insert(
|
||||
"created_at_ms".to_string(),
|
||||
JsonValue::Number(i64_from_u64(self.created_at_ms, "created_at_ms")),
|
||||
);
|
||||
object.insert(
|
||||
"updated_at_ms".to_string(),
|
||||
JsonValue::Number(i64_from_u64(self.updated_at_ms, "updated_at_ms")),
|
||||
);
|
||||
object.insert(
|
||||
"messages".to_string(),
|
||||
JsonValue::Array(
|
||||
@@ -233,12 +111,6 @@ impl Session {
|
||||
.collect(),
|
||||
),
|
||||
);
|
||||
if let Some(compaction) = &self.compaction {
|
||||
object.insert("compaction".to_string(), compaction.to_json());
|
||||
}
|
||||
if let Some(fork) = &self.fork {
|
||||
object.insert("fork".to_string(), fork.to_json());
|
||||
}
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
@@ -259,179 +131,7 @@ impl Session {
|
||||
.iter()
|
||||
.map(ConversationMessage::from_json)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let now = current_time_millis();
|
||||
let session_id = object
|
||||
.get("session_id")
|
||||
.and_then(JsonValue::as_str)
|
||||
.map(ToOwned::to_owned)
|
||||
.unwrap_or_else(generate_session_id);
|
||||
let created_at_ms = object
|
||||
.get("created_at_ms")
|
||||
.map(|value| required_u64_from_value(value, "created_at_ms"))
|
||||
.transpose()?
|
||||
.unwrap_or(now);
|
||||
let updated_at_ms = object
|
||||
.get("updated_at_ms")
|
||||
.map(|value| required_u64_from_value(value, "updated_at_ms"))
|
||||
.transpose()?
|
||||
.unwrap_or(created_at_ms);
|
||||
let compaction = object
|
||||
.get("compaction")
|
||||
.map(SessionCompaction::from_json)
|
||||
.transpose()?;
|
||||
let fork = object.get("fork").map(SessionFork::from_json).transpose()?;
|
||||
Ok(Self {
|
||||
version,
|
||||
session_id,
|
||||
created_at_ms,
|
||||
updated_at_ms,
|
||||
messages,
|
||||
compaction,
|
||||
fork,
|
||||
persistence: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn from_jsonl(contents: &str) -> Result<Self, SessionError> {
|
||||
let mut version = SESSION_VERSION;
|
||||
let mut session_id = None;
|
||||
let mut created_at_ms = None;
|
||||
let mut updated_at_ms = None;
|
||||
let mut messages = Vec::new();
|
||||
let mut compaction = None;
|
||||
let mut fork = None;
|
||||
|
||||
for (line_number, raw_line) in contents.lines().enumerate() {
|
||||
let line = raw_line.trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let value = JsonValue::parse(line).map_err(|error| {
|
||||
SessionError::Format(format!(
|
||||
"invalid JSONL record at line {}: {}",
|
||||
line_number + 1,
|
||||
error
|
||||
))
|
||||
})?;
|
||||
let object = value.as_object().ok_or_else(|| {
|
||||
SessionError::Format(format!(
|
||||
"JSONL record at line {} must be an object",
|
||||
line_number + 1
|
||||
))
|
||||
})?;
|
||||
match object
|
||||
.get("type")
|
||||
.and_then(JsonValue::as_str)
|
||||
.ok_or_else(|| {
|
||||
SessionError::Format(format!(
|
||||
"JSONL record at line {} missing type",
|
||||
line_number + 1
|
||||
))
|
||||
})? {
|
||||
"session_meta" => {
|
||||
version = required_u32(object, "version")?;
|
||||
session_id = Some(required_string(object, "session_id")?);
|
||||
created_at_ms = Some(required_u64(object, "created_at_ms")?);
|
||||
updated_at_ms = Some(required_u64(object, "updated_at_ms")?);
|
||||
fork = object.get("fork").map(SessionFork::from_json).transpose()?;
|
||||
}
|
||||
"message" => {
|
||||
let message_value = object.get("message").ok_or_else(|| {
|
||||
SessionError::Format(format!(
|
||||
"JSONL record at line {} missing message",
|
||||
line_number + 1
|
||||
))
|
||||
})?;
|
||||
messages.push(ConversationMessage::from_json(message_value)?);
|
||||
}
|
||||
"compaction" => {
|
||||
compaction = Some(SessionCompaction::from_json(&JsonValue::Object(
|
||||
object.clone(),
|
||||
))?);
|
||||
}
|
||||
other => {
|
||||
return Err(SessionError::Format(format!(
|
||||
"unsupported JSONL record type at line {}: {other}",
|
||||
line_number + 1
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let now = current_time_millis();
|
||||
Ok(Self {
|
||||
version,
|
||||
session_id: session_id.unwrap_or_else(generate_session_id),
|
||||
created_at_ms: created_at_ms.unwrap_or(now),
|
||||
updated_at_ms: updated_at_ms.unwrap_or(created_at_ms.unwrap_or(now)),
|
||||
messages,
|
||||
compaction,
|
||||
fork,
|
||||
persistence: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn render_jsonl_snapshot(&self) -> String {
|
||||
let mut lines = vec![self.meta_record().render()];
|
||||
if let Some(compaction) = &self.compaction {
|
||||
lines.push(compaction.to_jsonl_record().render());
|
||||
}
|
||||
lines.extend(
|
||||
self.messages
|
||||
.iter()
|
||||
.map(|message| message_record(message).render()),
|
||||
);
|
||||
let mut rendered = lines.join("\n");
|
||||
rendered.push('\n');
|
||||
rendered
|
||||
}
|
||||
|
||||
fn append_persisted_message(&self, message: &ConversationMessage) -> Result<(), SessionError> {
|
||||
let Some(path) = self.persistence_path() else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let needs_bootstrap = !path.exists() || fs::metadata(path)?.len() == 0;
|
||||
if needs_bootstrap {
|
||||
self.save_to_path(path)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut file = OpenOptions::new().append(true).open(path)?;
|
||||
writeln!(file, "{}", message_record(message).render())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn meta_record(&self) -> JsonValue {
|
||||
let mut object = BTreeMap::new();
|
||||
object.insert(
|
||||
"type".to_string(),
|
||||
JsonValue::String("session_meta".to_string()),
|
||||
);
|
||||
object.insert(
|
||||
"version".to_string(),
|
||||
JsonValue::Number(i64::from(self.version)),
|
||||
);
|
||||
object.insert(
|
||||
"session_id".to_string(),
|
||||
JsonValue::String(self.session_id.clone()),
|
||||
);
|
||||
object.insert(
|
||||
"created_at_ms".to_string(),
|
||||
JsonValue::Number(i64_from_u64(self.created_at_ms, "created_at_ms")),
|
||||
);
|
||||
object.insert(
|
||||
"updated_at_ms".to_string(),
|
||||
JsonValue::Number(i64_from_u64(self.updated_at_ms, "updated_at_ms")),
|
||||
);
|
||||
if let Some(fork) = &self.fork {
|
||||
object.insert("fork".to_string(), fork.to_json());
|
||||
}
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
fn touch(&mut self) {
|
||||
self.updated_at_ms = current_time_millis();
|
||||
Ok(Self { version, messages })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -624,92 +324,6 @@ impl ContentBlock {
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionCompaction {
|
||||
#[must_use]
|
||||
pub fn to_json(&self) -> JsonValue {
|
||||
let mut object = BTreeMap::new();
|
||||
object.insert(
|
||||
"count".to_string(),
|
||||
JsonValue::Number(i64::from(self.count)),
|
||||
);
|
||||
object.insert(
|
||||
"removed_message_count".to_string(),
|
||||
JsonValue::Number(i64_from_usize(
|
||||
self.removed_message_count,
|
||||
"removed_message_count",
|
||||
)),
|
||||
);
|
||||
object.insert(
|
||||
"summary".to_string(),
|
||||
JsonValue::String(self.summary.clone()),
|
||||
);
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn to_jsonl_record(&self) -> JsonValue {
|
||||
let mut object = self
|
||||
.to_json()
|
||||
.as_object()
|
||||
.cloned()
|
||||
.expect("compaction should render to object");
|
||||
object.insert(
|
||||
"type".to_string(),
|
||||
JsonValue::String("compaction".to_string()),
|
||||
);
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
fn from_json(value: &JsonValue) -> Result<Self, SessionError> {
|
||||
let object = value
|
||||
.as_object()
|
||||
.ok_or_else(|| SessionError::Format("compaction must be an object".to_string()))?;
|
||||
Ok(Self {
|
||||
count: required_u32(object, "count")?,
|
||||
removed_message_count: required_usize(object, "removed_message_count")?,
|
||||
summary: required_string(object, "summary")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionFork {
|
||||
#[must_use]
|
||||
pub fn to_json(&self) -> JsonValue {
|
||||
let mut object = BTreeMap::new();
|
||||
object.insert(
|
||||
"parent_session_id".to_string(),
|
||||
JsonValue::String(self.parent_session_id.clone()),
|
||||
);
|
||||
if let Some(branch_name) = &self.branch_name {
|
||||
object.insert(
|
||||
"branch_name".to_string(),
|
||||
JsonValue::String(branch_name.clone()),
|
||||
);
|
||||
}
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
fn from_json(value: &JsonValue) -> Result<Self, SessionError> {
|
||||
let object = value
|
||||
.as_object()
|
||||
.ok_or_else(|| SessionError::Format("fork metadata must be an object".to_string()))?;
|
||||
Ok(Self {
|
||||
parent_session_id: required_string(object, "parent_session_id")?,
|
||||
branch_name: object
|
||||
.get("branch_name")
|
||||
.and_then(JsonValue::as_str)
|
||||
.map(ToOwned::to_owned),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn message_record(message: &ConversationMessage) -> JsonValue {
|
||||
let mut object = BTreeMap::new();
|
||||
object.insert("type".to_string(), JsonValue::String("message".to_string()));
|
||||
object.insert("message".to_string(), message.to_json());
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
fn usage_to_json(usage: TokenUsage) -> JsonValue {
|
||||
let mut object = BTreeMap::new();
|
||||
object.insert(
|
||||
@@ -762,155 +376,22 @@ fn required_u32(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<u32,
|
||||
u32::try_from(value).map_err(|_| SessionError::Format(format!("{key} out of range")))
|
||||
}
|
||||
|
||||
fn required_u64(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<u64, SessionError> {
|
||||
let value = object
|
||||
.get(key)
|
||||
.ok_or_else(|| SessionError::Format(format!("missing {key}")))?;
|
||||
required_u64_from_value(value, key)
|
||||
}
|
||||
|
||||
fn required_u64_from_value(value: &JsonValue, key: &str) -> Result<u64, SessionError> {
|
||||
let value = value
|
||||
.as_i64()
|
||||
.ok_or_else(|| SessionError::Format(format!("missing {key}")))?;
|
||||
u64::try_from(value).map_err(|_| SessionError::Format(format!("{key} out of range")))
|
||||
}
|
||||
|
||||
fn required_usize(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<usize, SessionError> {
|
||||
let value = object
|
||||
.get(key)
|
||||
.and_then(JsonValue::as_i64)
|
||||
.ok_or_else(|| SessionError::Format(format!("missing {key}")))?;
|
||||
usize::try_from(value).map_err(|_| SessionError::Format(format!("{key} out of range")))
|
||||
}
|
||||
|
||||
fn i64_from_u64(value: u64, key: &str) -> i64 {
|
||||
i64::try_from(value).unwrap_or_else(|_| panic!("{key} out of range for JSON number"))
|
||||
}
|
||||
|
||||
fn i64_from_usize(value: usize, key: &str) -> i64 {
|
||||
i64::try_from(value).unwrap_or_else(|_| panic!("{key} out of range for JSON number"))
|
||||
}
|
||||
|
||||
fn normalize_optional_string(value: Option<String>) -> Option<String> {
|
||||
value.and_then(|value| {
|
||||
let trimmed = value.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed.to_string())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn current_time_millis() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis() as u64)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn generate_session_id() -> String {
|
||||
let millis = current_time_millis();
|
||||
let counter = SESSION_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
format!("session-{millis}-{counter}")
|
||||
}
|
||||
|
||||
fn write_atomic(path: &Path, contents: &str) -> Result<(), SessionError> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
let temp_path = temporary_path_for(path);
|
||||
fs::write(&temp_path, contents)?;
|
||||
fs::rename(temp_path, path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn temporary_path_for(path: &Path) -> PathBuf {
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or("session");
|
||||
path.with_file_name(format!(
|
||||
"{file_name}.tmp-{}-{}",
|
||||
current_time_millis(),
|
||||
SESSION_ID_COUNTER.fetch_add(1, Ordering::Relaxed)
|
||||
))
|
||||
}
|
||||
|
||||
fn rotate_session_file_if_needed(path: &Path) -> Result<(), SessionError> {
|
||||
let Ok(metadata) = fs::metadata(path) else {
|
||||
return Ok(());
|
||||
};
|
||||
if metadata.len() < ROTATE_AFTER_BYTES {
|
||||
return Ok(());
|
||||
}
|
||||
let rotated_path = rotated_log_path(path);
|
||||
fs::rename(path, rotated_path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rotated_log_path(path: &Path) -> PathBuf {
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or("session");
|
||||
path.with_file_name(format!("{stem}.rot-{}.jsonl", current_time_millis()))
|
||||
}
|
||||
|
||||
fn cleanup_rotated_logs(path: &Path) -> Result<(), SessionError> {
|
||||
let Some(parent) = path.parent() else {
|
||||
return Ok(());
|
||||
};
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or("session");
|
||||
let prefix = format!("{stem}.rot-");
|
||||
let mut rotated_paths = fs::read_dir(parent)?
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.path())
|
||||
.filter(|entry_path| {
|
||||
entry_path
|
||||
.file_name()
|
||||
.and_then(|value| value.to_str())
|
||||
.is_some_and(|name| name.starts_with(&prefix) && name.ends_with(".jsonl"))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
rotated_paths.sort_by_key(|entry_path| {
|
||||
fs::metadata(entry_path)
|
||||
.and_then(|metadata| metadata.modified())
|
||||
.unwrap_or(UNIX_EPOCH)
|
||||
});
|
||||
|
||||
let remove_count = rotated_paths.len().saturating_sub(MAX_ROTATED_FILES);
|
||||
for stale_path in rotated_paths.into_iter().take(remove_count) {
|
||||
fs::remove_file(stale_path)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
cleanup_rotated_logs, rotate_session_file_if_needed, ContentBlock, ConversationMessage,
|
||||
MessageRole, Session, SessionFork,
|
||||
};
|
||||
use crate::json::JsonValue;
|
||||
use super::{ContentBlock, ConversationMessage, MessageRole, Session};
|
||||
use crate::usage::TokenUsage;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[test]
|
||||
fn persists_and_restores_session_jsonl() {
|
||||
fn persists_and_restores_session_json() {
|
||||
let mut session = Session::new();
|
||||
session
|
||||
.push_user_text("hello")
|
||||
.expect("user message should append");
|
||||
.messages
|
||||
.push(ConversationMessage::user_text("hello"));
|
||||
session
|
||||
.push_message(ConversationMessage::assistant_with_usage(
|
||||
.messages
|
||||
.push(ConversationMessage::assistant_with_usage(
|
||||
vec![
|
||||
ContentBlock::Text {
|
||||
text: "thinking".to_string(),
|
||||
@@ -927,15 +408,16 @@ mod tests {
|
||||
cache_creation_input_tokens: 1,
|
||||
cache_read_input_tokens: 2,
|
||||
}),
|
||||
))
|
||||
.expect("assistant message should append");
|
||||
session
|
||||
.push_message(ConversationMessage::tool_result(
|
||||
"tool-1", "bash", "hi", false,
|
||||
))
|
||||
.expect("tool result should append");
|
||||
));
|
||||
session.messages.push(ConversationMessage::tool_result(
|
||||
"tool-1", "bash", "hi", false,
|
||||
));
|
||||
|
||||
let path = temp_session_path("jsonl");
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("system time should be after epoch")
|
||||
.as_nanos();
|
||||
let path = std::env::temp_dir().join(format!("runtime-session-{nanos}.json"));
|
||||
session.save_to_path(&path).expect("session should save");
|
||||
let restored = Session::load_from_path(&path).expect("session should load");
|
||||
fs::remove_file(&path).expect("temp file should be removable");
|
||||
@@ -946,157 +428,5 @@ mod tests {
|
||||
restored.messages[1].usage.expect("usage").total_tokens(),
|
||||
17
|
||||
);
|
||||
assert_eq!(restored.session_id, session.session_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loads_legacy_session_json_object() {
|
||||
let path = temp_session_path("legacy");
|
||||
let legacy = JsonValue::Object(
|
||||
[
|
||||
("version".to_string(), JsonValue::Number(1)),
|
||||
(
|
||||
"messages".to_string(),
|
||||
JsonValue::Array(vec![ConversationMessage::user_text("legacy").to_json()]),
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
);
|
||||
fs::write(&path, legacy.render()).expect("legacy file should write");
|
||||
|
||||
let restored = Session::load_from_path(&path).expect("legacy session should load");
|
||||
fs::remove_file(&path).expect("temp file should be removable");
|
||||
|
||||
assert_eq!(restored.messages.len(), 1);
|
||||
assert_eq!(
|
||||
restored.messages[0],
|
||||
ConversationMessage::user_text("legacy")
|
||||
);
|
||||
assert!(!restored.session_id.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn appends_messages_to_persisted_jsonl_session() {
|
||||
let path = temp_session_path("append");
|
||||
let mut session = Session::new().with_persistence_path(path.clone());
|
||||
session
|
||||
.save_to_path(&path)
|
||||
.expect("initial save should succeed");
|
||||
session
|
||||
.push_user_text("hi")
|
||||
.expect("user append should succeed");
|
||||
session
|
||||
.push_message(ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "hello".to_string(),
|
||||
}]))
|
||||
.expect("assistant append should succeed");
|
||||
|
||||
let restored = Session::load_from_path(&path).expect("session should replay from jsonl");
|
||||
fs::remove_file(&path).expect("temp file should be removable");
|
||||
|
||||
assert_eq!(restored.messages.len(), 2);
|
||||
assert_eq!(restored.messages[0], ConversationMessage::user_text("hi"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persists_compaction_metadata() {
|
||||
let path = temp_session_path("compaction");
|
||||
let mut session = Session::new();
|
||||
session
|
||||
.push_user_text("before")
|
||||
.expect("message should append");
|
||||
session.record_compaction("summarized earlier work", 4);
|
||||
session.save_to_path(&path).expect("session should save");
|
||||
|
||||
let restored = Session::load_from_path(&path).expect("session should load");
|
||||
fs::remove_file(&path).expect("temp file should be removable");
|
||||
|
||||
let compaction = restored.compaction.expect("compaction metadata");
|
||||
assert_eq!(compaction.count, 1);
|
||||
assert_eq!(compaction.removed_message_count, 4);
|
||||
assert!(compaction.summary.contains("summarized"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forks_sessions_with_branch_metadata_and_persists_it() {
|
||||
let path = temp_session_path("fork");
|
||||
let mut session = Session::new();
|
||||
session
|
||||
.push_user_text("before fork")
|
||||
.expect("message should append");
|
||||
|
||||
let forked = session
|
||||
.fork(Some("investigation".to_string()))
|
||||
.with_persistence_path(path.clone());
|
||||
forked
|
||||
.save_to_path(&path)
|
||||
.expect("forked session should save");
|
||||
|
||||
let restored = Session::load_from_path(&path).expect("forked session should load");
|
||||
fs::remove_file(&path).expect("temp file should be removable");
|
||||
|
||||
assert_ne!(restored.session_id, session.session_id);
|
||||
assert_eq!(
|
||||
restored.fork,
|
||||
Some(SessionFork {
|
||||
parent_session_id: session.session_id,
|
||||
branch_name: Some("investigation".to_string()),
|
||||
})
|
||||
);
|
||||
assert_eq!(restored.messages, forked.messages);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotates_and_cleans_up_large_session_logs() {
|
||||
let path = temp_session_path("rotation");
|
||||
fs::write(&path, "x".repeat((super::ROTATE_AFTER_BYTES + 10) as usize))
|
||||
.expect("oversized file should write");
|
||||
rotate_session_file_if_needed(&path).expect("rotation should succeed");
|
||||
assert!(
|
||||
!path.exists(),
|
||||
"original path should be rotated away before rewrite"
|
||||
);
|
||||
|
||||
for _ in 0..5 {
|
||||
let rotated = super::rotated_log_path(&path);
|
||||
fs::write(&rotated, "old").expect("rotated file should write");
|
||||
}
|
||||
cleanup_rotated_logs(&path).expect("cleanup should succeed");
|
||||
|
||||
let rotated_count = rotation_files(&path).len();
|
||||
assert!(rotated_count <= super::MAX_ROTATED_FILES);
|
||||
for rotated in rotation_files(&path) {
|
||||
fs::remove_file(rotated).expect("rotated file should be removable");
|
||||
}
|
||||
}
|
||||
|
||||
fn temp_session_path(label: &str) -> PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("system time should be after epoch")
|
||||
.as_nanos();
|
||||
std::env::temp_dir().join(format!("runtime-session-{label}-{nanos}.json"))
|
||||
}
|
||||
|
||||
fn rotation_files(path: &PathBuf) -> Vec<PathBuf> {
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.expect("temp path should have file stem")
|
||||
.to_string();
|
||||
fs::read_dir(path.parent().expect("temp path should have parent"))
|
||||
.expect("temp dir should read")
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.path())
|
||||
.filter(|entry_path| {
|
||||
entry_path
|
||||
.file_name()
|
||||
.and_then(|value| value.to_str())
|
||||
.is_some_and(|name| {
|
||||
name.starts_with(&format!("{stem}.rot-")) && name.ends_with(".jsonl")
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,19 +286,21 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn reconstructs_usage_from_session_messages() {
|
||||
let mut session = Session::new();
|
||||
session.messages = vec![ConversationMessage {
|
||||
role: MessageRole::Assistant,
|
||||
blocks: vec![ContentBlock::Text {
|
||||
text: "done".to_string(),
|
||||
let session = Session {
|
||||
version: 1,
|
||||
messages: vec![ConversationMessage {
|
||||
role: MessageRole::Assistant,
|
||||
blocks: vec![ContentBlock::Text {
|
||||
text: "done".to_string(),
|
||||
}],
|
||||
usage: Some(TokenUsage {
|
||||
input_tokens: 5,
|
||||
output_tokens: 2,
|
||||
cache_creation_input_tokens: 1,
|
||||
cache_read_input_tokens: 0,
|
||||
}),
|
||||
}],
|
||||
usage: Some(TokenUsage {
|
||||
input_tokens: 5,
|
||||
output_tokens: 2,
|
||||
cache_creation_input_tokens: 1,
|
||||
cache_read_input_tokens: 0,
|
||||
}),
|
||||
}];
|
||||
};
|
||||
|
||||
let tracker = UsageTracker::from_session(&session);
|
||||
assert_eq!(tracker.turns(), 1);
|
||||
|
||||
@@ -9,11 +9,12 @@ use std::io::{self, Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
use std::time::UNIX_EPOCH;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use api::{
|
||||
resolve_startup_auth_source, AnthropicClient, AuthSource, ContentBlockDelta, InputContentBlock,
|
||||
InputMessage, MessageRequest, MessageResponse, OutputContentBlock,
|
||||
detect_provider_kind, max_tokens_for_model, resolve_model_alias, resolve_startup_auth_source,
|
||||
AnthropicClient, AuthSource, ContentBlockDelta, InputContentBlock, InputMessage,
|
||||
MessageRequest, MessageResponse, OutputContentBlock, ProviderClient, ProviderKind,
|
||||
StreamEvent as ApiStreamEvent, ToolChoice, ToolDefinition, ToolResultContentBlock,
|
||||
};
|
||||
|
||||
@@ -35,20 +36,11 @@ use serde_json::json;
|
||||
use tools::{execute_tool, mvp_tool_specs, ToolSpec};
|
||||
|
||||
const DEFAULT_MODEL: &str = "claude-opus-4-6";
|
||||
fn max_tokens_for_model(model: &str) -> u32 {
|
||||
if model.contains("opus") {
|
||||
32_000
|
||||
} else {
|
||||
64_000
|
||||
}
|
||||
}
|
||||
const DEFAULT_DATE: &str = "2026-03-31";
|
||||
const DEFAULT_OAUTH_CALLBACK_PORT: u16 = 4545;
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
const BUILD_TARGET: Option<&str> = option_env!("TARGET");
|
||||
const GIT_SHA: Option<&str> = option_env!("GIT_SHA");
|
||||
const PRIMARY_SESSION_EXTENSION: &str = "jsonl";
|
||||
const LEGACY_SESSION_EXTENSION: &str = "json";
|
||||
|
||||
type AllowedToolSet = BTreeSet<String>;
|
||||
|
||||
@@ -290,15 +282,6 @@ fn parse_args(args: &[String]) -> Result<CliAction, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_model_alias(model: &str) -> &str {
|
||||
match model {
|
||||
"opus" => "claude-opus-4-6",
|
||||
"sonnet" => "claude-sonnet-4-6",
|
||||
"haiku" => "claude-haiku-4-5-20251213",
|
||||
_ => model,
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_allowed_tools(values: &[String]) -> Result<Option<AllowedToolSet>, String> {
|
||||
if values.is_empty() {
|
||||
return Ok(None);
|
||||
@@ -591,19 +574,7 @@ fn print_version() {
|
||||
}
|
||||
|
||||
fn resume_session(session_path: &Path, commands: &[String]) {
|
||||
let resolved_path = if session_path.exists() {
|
||||
session_path.to_path_buf()
|
||||
} else {
|
||||
match resolve_session_reference(&session_path.display().to_string()) {
|
||||
Ok(handle) => handle.path,
|
||||
Err(error) => {
|
||||
eprintln!("failed to restore session: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let session = match Session::load_from_path(&resolved_path) {
|
||||
let session = match Session::load_from_path(session_path) {
|
||||
Ok(session) => session,
|
||||
Err(error) => {
|
||||
eprintln!("failed to restore session: {error}");
|
||||
@@ -614,7 +585,7 @@ fn resume_session(session_path: &Path, commands: &[String]) {
|
||||
if commands.is_empty() {
|
||||
println!(
|
||||
"Restored session from {} ({} messages).",
|
||||
resolved_path.display(),
|
||||
session_path.display(),
|
||||
session.messages.len()
|
||||
);
|
||||
return;
|
||||
@@ -626,7 +597,7 @@ fn resume_session(session_path: &Path, commands: &[String]) {
|
||||
eprintln!("unsupported resumed command: {raw_command}");
|
||||
std::process::exit(2);
|
||||
};
|
||||
match run_resume_command(&resolved_path, &session, &command) {
|
||||
match run_resume_command(session_path, &session, &command) {
|
||||
Ok(ResumeCommandOutcome {
|
||||
session: next_session,
|
||||
message,
|
||||
@@ -987,8 +958,6 @@ struct ManagedSessionSummary {
|
||||
path: PathBuf,
|
||||
modified_epoch_secs: u64,
|
||||
message_count: usize,
|
||||
parent_session_id: Option<String>,
|
||||
branch_name: Option<String>,
|
||||
}
|
||||
|
||||
struct LiveCli {
|
||||
@@ -996,7 +965,7 @@ struct LiveCli {
|
||||
allowed_tools: Option<AllowedToolSet>,
|
||||
permission_mode: PermissionMode,
|
||||
system_prompt: Vec<String>,
|
||||
runtime: ConversationRuntime<AnthropicRuntimeClient, CliToolExecutor>,
|
||||
runtime: ConversationRuntime<ProviderRuntimeClient, CliToolExecutor>,
|
||||
session: SessionHandle,
|
||||
}
|
||||
|
||||
@@ -1008,10 +977,9 @@ impl LiveCli {
|
||||
permission_mode: PermissionMode,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let system_prompt = build_system_prompt()?;
|
||||
let session_state = Session::new();
|
||||
let session = create_managed_session_handle(&session_state.session_id)?;
|
||||
let session = create_managed_session_handle()?;
|
||||
let runtime = build_runtime(
|
||||
session_state.with_persistence_path(session.path.clone()),
|
||||
Session::new(),
|
||||
model.clone(),
|
||||
system_prompt.clone(),
|
||||
enable_tools,
|
||||
@@ -1314,10 +1282,9 @@ impl LiveCli {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let session_state = Session::new();
|
||||
self.session = create_managed_session_handle(&session_state.session_id)?;
|
||||
self.session = create_managed_session_handle()?;
|
||||
self.runtime = build_runtime(
|
||||
session_state.with_persistence_path(self.session.path.clone()),
|
||||
Session::new(),
|
||||
self.model.clone(),
|
||||
self.system_prompt.clone(),
|
||||
true,
|
||||
@@ -1351,7 +1318,6 @@ impl LiveCli {
|
||||
let handle = resolve_session_reference(&session_ref)?;
|
||||
let session = Session::load_from_path(&handle.path)?;
|
||||
let message_count = session.messages.len();
|
||||
let session_id = session.session_id.clone();
|
||||
self.runtime = build_runtime(
|
||||
session,
|
||||
self.model.clone(),
|
||||
@@ -1361,10 +1327,7 @@ impl LiveCli {
|
||||
self.allowed_tools.clone(),
|
||||
self.permission_mode,
|
||||
)?;
|
||||
self.session = SessionHandle {
|
||||
id: session_id,
|
||||
path: handle.path,
|
||||
};
|
||||
self.session = handle;
|
||||
println!(
|
||||
"{}",
|
||||
format_resume_report(
|
||||
@@ -1427,7 +1390,6 @@ impl LiveCli {
|
||||
let handle = resolve_session_reference(target)?;
|
||||
let session = Session::load_from_path(&handle.path)?;
|
||||
let message_count = session.messages.len();
|
||||
let session_id = session.session_id.clone();
|
||||
self.runtime = build_runtime(
|
||||
session,
|
||||
self.model.clone(),
|
||||
@@ -1437,10 +1399,7 @@ impl LiveCli {
|
||||
self.allowed_tools.clone(),
|
||||
self.permission_mode,
|
||||
)?;
|
||||
self.session = SessionHandle {
|
||||
id: session_id,
|
||||
path: handle.path,
|
||||
};
|
||||
self.session = handle;
|
||||
println!(
|
||||
"Session switched\n Active session {}\n File {}\n Messages {}",
|
||||
self.session.id,
|
||||
@@ -1449,41 +1408,8 @@ impl LiveCli {
|
||||
);
|
||||
Ok(true)
|
||||
}
|
||||
Some("fork") => {
|
||||
let forked = self.runtime.fork_session(target.map(ToOwned::to_owned));
|
||||
let parent_session_id = self.session.id.clone();
|
||||
let handle = create_managed_session_handle(&forked.session_id)?;
|
||||
let branch_name = forked
|
||||
.fork
|
||||
.as_ref()
|
||||
.and_then(|fork| fork.branch_name.clone());
|
||||
let forked = forked.with_persistence_path(handle.path.clone());
|
||||
let message_count = forked.messages.len();
|
||||
forked.save_to_path(&handle.path)?;
|
||||
self.runtime = build_runtime(
|
||||
forked,
|
||||
self.model.clone(),
|
||||
self.system_prompt.clone(),
|
||||
true,
|
||||
true,
|
||||
self.allowed_tools.clone(),
|
||||
self.permission_mode,
|
||||
)?;
|
||||
self.session = handle;
|
||||
println!(
|
||||
"Session forked\n Parent session {}\n Active session {}\n Branch {}\n File {}\n Messages {}",
|
||||
parent_session_id,
|
||||
self.session.id,
|
||||
branch_name.as_deref().unwrap_or("(unnamed)"),
|
||||
self.session.path.display(),
|
||||
message_count,
|
||||
);
|
||||
Ok(true)
|
||||
}
|
||||
Some(other) => {
|
||||
println!(
|
||||
"Unknown /session action '{other}'. Use /session list, /session switch <session-id>, or /session fork [branch-name]."
|
||||
);
|
||||
println!("Unknown /session action '{other}'. Use /session list or /session switch <session-id>.");
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
@@ -1516,61 +1442,44 @@ fn sessions_dir() -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn create_managed_session_handle(
|
||||
session_id: &str,
|
||||
) -> Result<SessionHandle, Box<dyn std::error::Error>> {
|
||||
let id = session_id.to_string();
|
||||
let path = sessions_dir()?.join(format!("{id}.{PRIMARY_SESSION_EXTENSION}"));
|
||||
fn create_managed_session_handle() -> Result<SessionHandle, Box<dyn std::error::Error>> {
|
||||
let id = generate_session_id();
|
||||
let path = sessions_dir()?.join(format!("{id}.json"));
|
||||
Ok(SessionHandle { id, path })
|
||||
}
|
||||
|
||||
fn generate_session_id() -> String {
|
||||
let millis = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis())
|
||||
.unwrap_or_default();
|
||||
format!("session-{millis}")
|
||||
}
|
||||
|
||||
fn resolve_session_reference(reference: &str) -> Result<SessionHandle, Box<dyn std::error::Error>> {
|
||||
let direct = PathBuf::from(reference);
|
||||
let looks_like_path = direct.extension().is_some() || direct.components().count() > 1;
|
||||
let path = if direct.exists() {
|
||||
direct
|
||||
} else if looks_like_path {
|
||||
return Err(format!("session not found: {reference}").into());
|
||||
} else {
|
||||
resolve_managed_session_path(reference)?
|
||||
sessions_dir()?.join(format!("{reference}.json"))
|
||||
};
|
||||
if !path.exists() {
|
||||
return Err(format!("session not found: {reference}").into());
|
||||
}
|
||||
let id = path
|
||||
.file_name()
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.and_then(|name| {
|
||||
name.strip_suffix(&format!(".{PRIMARY_SESSION_EXTENSION}"))
|
||||
.or_else(|| name.strip_suffix(&format!(".{LEGACY_SESSION_EXTENSION}")))
|
||||
})
|
||||
.unwrap_or(reference)
|
||||
.to_string();
|
||||
Ok(SessionHandle { id, path })
|
||||
}
|
||||
|
||||
fn resolve_managed_session_path(session_id: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||
let directory = sessions_dir()?;
|
||||
for extension in [PRIMARY_SESSION_EXTENSION, LEGACY_SESSION_EXTENSION] {
|
||||
let path = directory.join(format!("{session_id}.{extension}"));
|
||||
if path.exists() {
|
||||
return Ok(path);
|
||||
}
|
||||
}
|
||||
Err(format!("session not found: {session_id}").into())
|
||||
}
|
||||
|
||||
fn is_managed_session_file(path: &Path) -> bool {
|
||||
path.extension()
|
||||
.and_then(|ext| ext.to_str())
|
||||
.is_some_and(|extension| {
|
||||
extension == PRIMARY_SESSION_EXTENSION || extension == LEGACY_SESSION_EXTENSION
|
||||
})
|
||||
}
|
||||
|
||||
fn list_managed_sessions() -> Result<Vec<ManagedSessionSummary>, Box<dyn std::error::Error>> {
|
||||
let mut sessions = Vec::new();
|
||||
for entry in fs::read_dir(sessions_dir()?)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if !is_managed_session_file(&path) {
|
||||
if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
|
||||
continue;
|
||||
}
|
||||
let metadata = entry.metadata()?;
|
||||
@@ -1580,41 +1489,19 @@ fn list_managed_sessions() -> Result<Vec<ManagedSessionSummary>, Box<dyn std::er
|
||||
.and_then(|time| time.duration_since(UNIX_EPOCH).ok())
|
||||
.map(|duration| duration.as_secs())
|
||||
.unwrap_or_default();
|
||||
let (id, message_count, parent_session_id, branch_name) = Session::load_from_path(&path)
|
||||
.map(|session| {
|
||||
let parent_session_id = session
|
||||
.fork
|
||||
.as_ref()
|
||||
.map(|fork| fork.parent_session_id.clone());
|
||||
let branch_name = session
|
||||
.fork
|
||||
.as_ref()
|
||||
.and_then(|fork| fork.branch_name.clone());
|
||||
(
|
||||
session.session_id,
|
||||
session.messages.len(),
|
||||
parent_session_id,
|
||||
branch_name,
|
||||
)
|
||||
})
|
||||
.unwrap_or_else(|_| {
|
||||
(
|
||||
path.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or("unknown")
|
||||
.to_string(),
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
});
|
||||
let message_count = Session::load_from_path(&path)
|
||||
.map(|session| session.messages.len())
|
||||
.unwrap_or_default();
|
||||
let id = path
|
||||
.file_stem()
|
||||
.and_then(|value| value.to_str())
|
||||
.unwrap_or("unknown")
|
||||
.to_string();
|
||||
sessions.push(ManagedSessionSummary {
|
||||
id,
|
||||
path,
|
||||
modified_epoch_secs,
|
||||
message_count,
|
||||
parent_session_id,
|
||||
branch_name,
|
||||
});
|
||||
}
|
||||
sessions.sort_by(|left, right| right.modified_epoch_secs.cmp(&left.modified_epoch_secs));
|
||||
@@ -1637,23 +1524,11 @@ fn render_session_list(active_session_id: &str) -> Result<String, Box<dyn std::e
|
||||
} else {
|
||||
"○ saved"
|
||||
};
|
||||
let lineage = match (
|
||||
session.branch_name.as_deref(),
|
||||
session.parent_session_id.as_deref(),
|
||||
) {
|
||||
(Some(branch_name), Some(parent_session_id)) => {
|
||||
format!(" branch={branch_name} from={parent_session_id}")
|
||||
}
|
||||
(None, Some(parent_session_id)) => format!(" from={parent_session_id}"),
|
||||
(Some(branch_name), None) => format!(" branch={branch_name}"),
|
||||
(None, None) => String::new(),
|
||||
};
|
||||
lines.push(format!(
|
||||
" {id:<20} {marker:<10} msgs={msgs:<4} modified={modified}{lineage} path={path}",
|
||||
" {id:<20} {marker:<10} msgs={msgs:<4} modified={modified} path={path}",
|
||||
id = session.id,
|
||||
msgs = session.message_count,
|
||||
modified = session.modified_epoch_secs,
|
||||
lineage = lineage,
|
||||
path = session.path.display(),
|
||||
));
|
||||
}
|
||||
@@ -2030,15 +1905,16 @@ fn build_runtime(
|
||||
emit_output: bool,
|
||||
allowed_tools: Option<AllowedToolSet>,
|
||||
permission_mode: PermissionMode,
|
||||
) -> Result<ConversationRuntime<AnthropicRuntimeClient, CliToolExecutor>, Box<dyn std::error::Error>>
|
||||
) -> Result<ConversationRuntime<ProviderRuntimeClient, CliToolExecutor>, Box<dyn std::error::Error>>
|
||||
{
|
||||
let feature_config = build_runtime_feature_config()?;
|
||||
Ok(ConversationRuntime::new_with_features(
|
||||
session,
|
||||
AnthropicRuntimeClient::new(model, enable_tools, emit_output, allowed_tools.clone())?,
|
||||
ProviderRuntimeClient::new(model, enable_tools, emit_output, allowed_tools.clone())?,
|
||||
CliToolExecutor::new(allowed_tools, emit_output),
|
||||
permission_policy(permission_mode),
|
||||
system_prompt,
|
||||
build_runtime_feature_config()?,
|
||||
&feature_config,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -2088,26 +1964,33 @@ impl runtime::PermissionPrompter for CliPermissionPrompter {
|
||||
}
|
||||
}
|
||||
|
||||
struct AnthropicRuntimeClient {
|
||||
struct ProviderRuntimeClient {
|
||||
runtime: tokio::runtime::Runtime,
|
||||
client: AnthropicClient,
|
||||
client: ProviderClient,
|
||||
model: String,
|
||||
enable_tools: bool,
|
||||
emit_output: bool,
|
||||
allowed_tools: Option<AllowedToolSet>,
|
||||
}
|
||||
|
||||
impl AnthropicRuntimeClient {
|
||||
impl ProviderRuntimeClient {
|
||||
fn new(
|
||||
model: String,
|
||||
enable_tools: bool,
|
||||
emit_output: bool,
|
||||
allowed_tools: Option<AllowedToolSet>,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let model = resolve_model_alias(&model).to_string();
|
||||
let client = match detect_provider_kind(&model) {
|
||||
ProviderKind::Anthropic => ProviderClient::from_model_with_anthropic_auth(
|
||||
&model,
|
||||
Some(resolve_cli_auth_source()?),
|
||||
)?,
|
||||
ProviderKind::Xai | ProviderKind::OpenAi => ProviderClient::from_model(&model)?,
|
||||
};
|
||||
Ok(Self {
|
||||
runtime: tokio::runtime::Runtime::new()?,
|
||||
client: AnthropicClient::from_auth(resolve_cli_auth_source()?)
|
||||
.with_base_url(api::read_base_url()),
|
||||
client,
|
||||
model,
|
||||
enable_tools,
|
||||
emit_output,
|
||||
@@ -2126,7 +2009,7 @@ fn resolve_cli_auth_source() -> Result<AuthSource, Box<dyn std::error::Error>> {
|
||||
})?)
|
||||
}
|
||||
|
||||
impl ApiClient for AnthropicRuntimeClient {
|
||||
impl ApiClient for ProviderRuntimeClient {
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
|
||||
let message_request = MessageRequest {
|
||||
@@ -2164,7 +2047,7 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
let renderer = TerminalRenderer::new();
|
||||
let mut markdown_stream = MarkdownStreamState::default();
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tool: Option<(String, String, String)> = None;
|
||||
let mut pending_tools: BTreeMap<u32, (String, String, String)> = BTreeMap::new();
|
||||
let mut saw_stop = false;
|
||||
|
||||
while let Some(event) = stream
|
||||
@@ -2175,15 +2058,23 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
match event {
|
||||
ApiStreamEvent::MessageStart(start) => {
|
||||
for block in start.message.content {
|
||||
push_output_block(block, out, &mut events, &mut pending_tool, true)?;
|
||||
push_output_block(
|
||||
block,
|
||||
0,
|
||||
out,
|
||||
&mut events,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
ApiStreamEvent::ContentBlockStart(start) => {
|
||||
push_output_block(
|
||||
start.content_block,
|
||||
start.index,
|
||||
out,
|
||||
&mut events,
|
||||
&mut pending_tool,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
)?;
|
||||
}
|
||||
@@ -2199,18 +2090,18 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
}
|
||||
}
|
||||
ContentBlockDelta::InputJsonDelta { partial_json } => {
|
||||
if let Some((_, _, input)) = &mut pending_tool {
|
||||
if let Some((_, _, input)) = pending_tools.get_mut(&delta.index) {
|
||||
input.push_str(&partial_json);
|
||||
}
|
||||
}
|
||||
},
|
||||
ApiStreamEvent::ContentBlockStop(_) => {
|
||||
ApiStreamEvent::ContentBlockStop(stop) => {
|
||||
if let Some(rendered) = markdown_stream.flush(&renderer) {
|
||||
write!(out, "{rendered}")
|
||||
.and_then(|()| out.flush())
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
}
|
||||
if let Some((id, name, input)) = pending_tool.take() {
|
||||
if let Some((id, name, input)) = pending_tools.remove(&stop.index) {
|
||||
// Display tool call now that input is fully accumulated
|
||||
writeln!(out, "\n{}", format_tool_call_start(&name, &input))
|
||||
.and_then(|()| out.flush())
|
||||
@@ -2674,9 +2565,10 @@ fn truncate_for_summary(value: &str, limit: usize) -> String {
|
||||
|
||||
fn push_output_block(
|
||||
block: OutputContentBlock,
|
||||
block_index: u32,
|
||||
out: &mut (impl Write + ?Sized),
|
||||
events: &mut Vec<AssistantEvent>,
|
||||
pending_tool: &mut Option<(String, String, String)>,
|
||||
pending_tools: &mut BTreeMap<u32, (String, String, String)>,
|
||||
streaming_tool_input: bool,
|
||||
) -> Result<(), RuntimeError> {
|
||||
match block {
|
||||
@@ -2701,7 +2593,7 @@ fn push_output_block(
|
||||
} else {
|
||||
input.to_string()
|
||||
};
|
||||
*pending_tool = Some((id, name, initial_input));
|
||||
pending_tools.insert(block_index, (id, name, initial_input));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -2712,11 +2604,13 @@ fn response_to_events(
|
||||
out: &mut (impl Write + ?Sized),
|
||||
) -> Result<Vec<AssistantEvent>, RuntimeError> {
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tool = None;
|
||||
let mut pending_tools = BTreeMap::new();
|
||||
|
||||
for block in response.content {
|
||||
push_output_block(block, out, &mut events, &mut pending_tool, false)?;
|
||||
if let Some((id, name, input)) = pending_tool.take() {
|
||||
for (index, block) in response.content.into_iter().enumerate() {
|
||||
let index =
|
||||
u32::try_from(index).map_err(|_| RuntimeError::new("response block index overflow"))?;
|
||||
push_output_block(block, index, out, &mut events, &mut pending_tools, false)?;
|
||||
if let Some((id, name, input)) = pending_tools.remove(&index) {
|
||||
events.push(AssistantEvent::ToolUse { id, name, input });
|
||||
}
|
||||
}
|
||||
@@ -2857,7 +2751,7 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> {
|
||||
writeln!(out, " Shorthand non-interactive prompt mode")?;
|
||||
writeln!(
|
||||
out,
|
||||
" claw --resume SESSION.jsonl [/status] [/compact] [...]"
|
||||
" claw --resume SESSION.json [/status] [/compact] [...]"
|
||||
)?;
|
||||
writeln!(
|
||||
out,
|
||||
@@ -2917,7 +2811,7 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> {
|
||||
)?;
|
||||
writeln!(
|
||||
out,
|
||||
" claw --resume session.jsonl /status /diff /export notes.txt"
|
||||
" claw --resume session.json /status /diff /export notes.txt"
|
||||
)?;
|
||||
writeln!(out, " claw login")?;
|
||||
writeln!(out, " claw init")?;
|
||||
@@ -2931,23 +2825,19 @@ fn print_help() {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
create_managed_session_handle, filter_tool_specs, format_compact_report,
|
||||
format_cost_report, format_model_report, format_model_switch_report,
|
||||
format_permissions_report, format_permissions_switch_report, format_resume_report,
|
||||
format_status_report, format_tool_call_start, format_tool_result,
|
||||
filter_tool_specs, format_compact_report, format_cost_report, format_model_report,
|
||||
format_model_switch_report, format_permissions_report, format_permissions_switch_report,
|
||||
format_resume_report, format_status_report, format_tool_call_start, format_tool_result,
|
||||
normalize_permission_mode, parse_args, parse_git_status_metadata, print_help_to,
|
||||
push_output_block, render_config_report, render_memory_report, render_repl_help,
|
||||
resolve_model_alias, resolve_session_reference, response_to_events,
|
||||
resume_supported_slash_commands, status_context, CliAction, CliOutputFormat, SlashCommand,
|
||||
StatusUsage, DEFAULT_MODEL,
|
||||
resolve_model_alias, response_to_events, resume_supported_slash_commands, status_context,
|
||||
CliAction, CliOutputFormat, SlashCommand, StatusUsage, DEFAULT_MODEL,
|
||||
};
|
||||
use api::{MessageResponse, OutputContentBlock, Usage};
|
||||
use runtime::{
|
||||
AssistantEvent, ContentBlock, ConversationMessage, MessageRole, PermissionMode, Session,
|
||||
};
|
||||
use runtime::{AssistantEvent, ContentBlock, ConversationMessage, MessageRole, PermissionMode};
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
#[test]
|
||||
fn defaults_to_repl_when_no_args() {
|
||||
@@ -3026,6 +2916,9 @@ mod tests {
|
||||
assert_eq!(resolve_model_alias("opus"), "claude-opus-4-6");
|
||||
assert_eq!(resolve_model_alias("sonnet"), "claude-sonnet-4-6");
|
||||
assert_eq!(resolve_model_alias("haiku"), "claude-haiku-4-5-20251213");
|
||||
assert_eq!(resolve_model_alias("grok"), "grok-3");
|
||||
assert_eq!(resolve_model_alias("grok-mini"), "grok-3-mini");
|
||||
assert_eq!(resolve_model_alias("grok-2"), "grok-2");
|
||||
assert_eq!(resolve_model_alias("claude-opus"), "claude-opus");
|
||||
}
|
||||
|
||||
@@ -3121,13 +3014,13 @@ mod tests {
|
||||
fn parses_resume_flag_with_slash_command() {
|
||||
let args = vec![
|
||||
"--resume".to_string(),
|
||||
"session.jsonl".to_string(),
|
||||
"session.json".to_string(),
|
||||
"/compact".to_string(),
|
||||
];
|
||||
assert_eq!(
|
||||
parse_args(&args).expect("args should parse"),
|
||||
CliAction::ResumeSession {
|
||||
session_path: PathBuf::from("session.jsonl"),
|
||||
session_path: PathBuf::from("session.json"),
|
||||
commands: vec!["/compact".to_string()],
|
||||
}
|
||||
);
|
||||
@@ -3137,7 +3030,7 @@ mod tests {
|
||||
fn parses_resume_flag_with_multiple_slash_commands() {
|
||||
let args = vec![
|
||||
"--resume".to_string(),
|
||||
"session.jsonl".to_string(),
|
||||
"session.json".to_string(),
|
||||
"/status".to_string(),
|
||||
"/compact".to_string(),
|
||||
"/cost".to_string(),
|
||||
@@ -3145,7 +3038,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
parse_args(&args).expect("args should parse"),
|
||||
CliAction::ResumeSession {
|
||||
session_path: PathBuf::from("session.jsonl"),
|
||||
session_path: PathBuf::from("session.json"),
|
||||
commands: vec![
|
||||
"/status".to_string(),
|
||||
"/compact".to_string(),
|
||||
@@ -3173,7 +3066,7 @@ mod tests {
|
||||
fn shared_help_uses_resume_annotation_copy() {
|
||||
let help = commands::render_slash_command_help();
|
||||
assert!(help.contains("Slash commands"));
|
||||
assert!(help.contains("works with --resume SESSION.jsonl"));
|
||||
assert!(help.contains("works with --resume SESSION.json"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -3193,7 +3086,7 @@ mod tests {
|
||||
assert!(help.contains("/diff"));
|
||||
assert!(help.contains("/version"));
|
||||
assert!(help.contains("/export [file]"));
|
||||
assert!(help.contains("/session [list|switch <session-id>|fork [branch-name]]"));
|
||||
assert!(help.contains("/session [list|switch <session-id>]"));
|
||||
assert!(help.contains("/exit"));
|
||||
}
|
||||
|
||||
@@ -3214,9 +3107,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn resume_report_uses_sectioned_layout() {
|
||||
let report = format_resume_report("session.jsonl", 14, 6);
|
||||
let report = format_resume_report("session.json", 14, 6);
|
||||
assert!(report.contains("Session resumed"));
|
||||
assert!(report.contains("Session file session.jsonl"));
|
||||
assert!(report.contains("Session file session.json"));
|
||||
assert!(report.contains("Messages 14"));
|
||||
assert!(report.contains("Turns 6"));
|
||||
}
|
||||
@@ -3318,7 +3211,7 @@ mod tests {
|
||||
"workspace-write",
|
||||
&super::StatusContext {
|
||||
cwd: PathBuf::from("/tmp/project"),
|
||||
session_path: Some(PathBuf::from("session.jsonl")),
|
||||
session_path: Some(PathBuf::from("session.json")),
|
||||
loaded_config_files: 2,
|
||||
discovered_config_files: 3,
|
||||
memory_file_count: 4,
|
||||
@@ -3335,7 +3228,7 @@ mod tests {
|
||||
assert!(status.contains("Cwd /tmp/project"));
|
||||
assert!(status.contains("Project root /tmp"));
|
||||
assert!(status.contains("Git branch main"));
|
||||
assert!(status.contains("Session session.jsonl"));
|
||||
assert!(status.contains("Session session.json"));
|
||||
assert!(status.contains("Config files loaded 2/3"));
|
||||
assert!(status.contains("Memory files 4"));
|
||||
}
|
||||
@@ -3410,9 +3303,9 @@ mod tests {
|
||||
#[test]
|
||||
fn parses_resume_and_config_slash_commands() {
|
||||
assert_eq!(
|
||||
SlashCommand::parse("/resume saved-session.jsonl"),
|
||||
SlashCommand::parse("/resume saved-session.json"),
|
||||
Some(SlashCommand::Resume {
|
||||
session_path: Some("saved-session.jsonl".to_string())
|
||||
session_path: Some("saved-session.json".to_string())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -3431,65 +3324,6 @@ mod tests {
|
||||
);
|
||||
assert_eq!(SlashCommand::parse("/memory"), Some(SlashCommand::Memory));
|
||||
assert_eq!(SlashCommand::parse("/init"), Some(SlashCommand::Init));
|
||||
assert_eq!(
|
||||
SlashCommand::parse("/session fork incident-review"),
|
||||
Some(SlashCommand::Session {
|
||||
action: Some("fork".to_string()),
|
||||
target: Some("incident-review".to_string())
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_mentions_jsonl_resume_examples() {
|
||||
let mut help = Vec::new();
|
||||
print_help_to(&mut help).expect("help should render");
|
||||
let help = String::from_utf8(help).expect("help should be utf8");
|
||||
assert!(help.contains("claw --resume SESSION.jsonl"));
|
||||
assert!(help.contains("claw --resume session.jsonl /status /diff /export notes.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn managed_sessions_default_to_jsonl_and_resolve_legacy_json() {
|
||||
let _guard = cwd_lock().lock().expect("cwd lock");
|
||||
let workspace = temp_workspace("session-resolution");
|
||||
std::fs::create_dir_all(&workspace).expect("workspace should create");
|
||||
let previous = std::env::current_dir().expect("cwd");
|
||||
std::env::set_current_dir(&workspace).expect("switch cwd");
|
||||
|
||||
let handle = create_managed_session_handle("session-alpha").expect("jsonl handle");
|
||||
assert!(handle.path.ends_with("session-alpha.jsonl"));
|
||||
|
||||
let legacy_path = workspace.join(".claude/sessions/legacy.json");
|
||||
std::fs::create_dir_all(
|
||||
legacy_path
|
||||
.parent()
|
||||
.expect("legacy path should have parent directory"),
|
||||
)
|
||||
.expect("session dir should exist");
|
||||
Session::new()
|
||||
.with_persistence_path(legacy_path.clone())
|
||||
.save_to_path(&legacy_path)
|
||||
.expect("legacy session should save");
|
||||
|
||||
let resolved = resolve_session_reference("legacy").expect("legacy session should resolve");
|
||||
assert_eq!(resolved.path, legacy_path);
|
||||
|
||||
std::env::set_current_dir(previous).expect("restore cwd");
|
||||
std::fs::remove_dir_all(workspace).expect("workspace should clean up");
|
||||
}
|
||||
|
||||
fn cwd_lock() -> &'static Mutex<()> {
|
||||
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
|
||||
LOCK.get_or_init(|| Mutex::new(()))
|
||||
}
|
||||
|
||||
fn temp_workspace(label: &str) -> PathBuf {
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.expect("system time should be after epoch")
|
||||
.as_nanos();
|
||||
std::env::temp_dir().join(format!("claw-cli-{label}-{nanos}"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -3552,15 +3386,16 @@ mod tests {
|
||||
fn push_output_block_renders_markdown_text() {
|
||||
let mut out = Vec::new();
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tool = None;
|
||||
let mut pending_tools = BTreeMap::new();
|
||||
|
||||
push_output_block(
|
||||
OutputContentBlock::Text {
|
||||
text: "# Heading".to_string(),
|
||||
},
|
||||
0,
|
||||
&mut out,
|
||||
&mut events,
|
||||
&mut pending_tool,
|
||||
&mut pending_tools,
|
||||
false,
|
||||
)
|
||||
.expect("text block should render");
|
||||
@@ -3574,7 +3409,7 @@ mod tests {
|
||||
fn push_output_block_skips_empty_object_prefix_for_tool_streams() {
|
||||
let mut out = Vec::new();
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tool = None;
|
||||
let mut pending_tools = BTreeMap::new();
|
||||
|
||||
push_output_block(
|
||||
OutputContentBlock::ToolUse {
|
||||
@@ -3582,20 +3417,83 @@ mod tests {
|
||||
name: "read_file".to_string(),
|
||||
input: json!({}),
|
||||
},
|
||||
1,
|
||||
&mut out,
|
||||
&mut events,
|
||||
&mut pending_tool,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
)
|
||||
.expect("tool block should accumulate");
|
||||
|
||||
assert!(events.is_empty());
|
||||
assert_eq!(
|
||||
pending_tool,
|
||||
pending_tools.remove(&1),
|
||||
Some(("tool-1".to_string(), "read_file".to_string(), String::new(),))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_tools_preserve_multiple_streaming_tool_calls_by_index() {
|
||||
let mut out = Vec::new();
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tools = BTreeMap::new();
|
||||
|
||||
push_output_block(
|
||||
OutputContentBlock::ToolUse {
|
||||
id: "tool-1".to_string(),
|
||||
name: "read_file".to_string(),
|
||||
input: json!({}),
|
||||
},
|
||||
1,
|
||||
&mut out,
|
||||
&mut events,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
)
|
||||
.expect("first tool should accumulate");
|
||||
push_output_block(
|
||||
OutputContentBlock::ToolUse {
|
||||
id: "tool-2".to_string(),
|
||||
name: "grep_search".to_string(),
|
||||
input: json!({}),
|
||||
},
|
||||
2,
|
||||
&mut out,
|
||||
&mut events,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
)
|
||||
.expect("second tool should accumulate");
|
||||
|
||||
pending_tools
|
||||
.get_mut(&1)
|
||||
.expect("first tool pending")
|
||||
.2
|
||||
.push_str("{\"path\":\"src/main.rs\"}");
|
||||
pending_tools
|
||||
.get_mut(&2)
|
||||
.expect("second tool pending")
|
||||
.2
|
||||
.push_str("{\"pattern\":\"TODO\"}");
|
||||
|
||||
assert_eq!(
|
||||
pending_tools.remove(&1),
|
||||
Some((
|
||||
"tool-1".to_string(),
|
||||
"read_file".to_string(),
|
||||
"{\"path\":\"src/main.rs\"}".to_string(),
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
pending_tools.remove(&2),
|
||||
Some((
|
||||
"tool-2".to_string(),
|
||||
"grep_search".to_string(),
|
||||
"{\"pattern\":\"TODO\"}".to_string(),
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_to_events_preserves_empty_object_json_input_outside_streaming() {
|
||||
let mut out = Vec::new();
|
||||
|
||||
@@ -4,9 +4,9 @@ use std::process::Command;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use api::{
|
||||
read_base_url, AnthropicClient, ContentBlockDelta, InputContentBlock, InputMessage,
|
||||
MessageRequest, MessageResponse, OutputContentBlock, StreamEvent as ApiStreamEvent, ToolChoice,
|
||||
ToolDefinition, ToolResultContentBlock,
|
||||
max_tokens_for_model, resolve_model_alias, ContentBlockDelta, InputContentBlock, InputMessage,
|
||||
MessageRequest, MessageResponse, OutputContentBlock, ProviderClient,
|
||||
StreamEvent as ApiStreamEvent, ToolChoice, ToolDefinition, ToolResultContentBlock,
|
||||
};
|
||||
use reqwest::blocking::Client;
|
||||
use runtime::{
|
||||
@@ -1459,14 +1459,14 @@ fn run_agent_job(job: &AgentJob) -> Result<(), String> {
|
||||
|
||||
fn build_agent_runtime(
|
||||
job: &AgentJob,
|
||||
) -> Result<ConversationRuntime<AnthropicRuntimeClient, SubagentToolExecutor>, String> {
|
||||
) -> Result<ConversationRuntime<ProviderRuntimeClient, SubagentToolExecutor>, String> {
|
||||
let model = job
|
||||
.manifest
|
||||
.model
|
||||
.clone()
|
||||
.unwrap_or_else(|| DEFAULT_AGENT_MODEL.to_string());
|
||||
let allowed_tools = job.allowed_tools.clone();
|
||||
let api_client = AnthropicRuntimeClient::new(model, allowed_tools.clone())?;
|
||||
let api_client = ProviderRuntimeClient::new(model, allowed_tools.clone())?;
|
||||
let tool_executor = SubagentToolExecutor::new(allowed_tools);
|
||||
Ok(ConversationRuntime::new(
|
||||
Session::new(),
|
||||
@@ -1635,18 +1635,17 @@ fn format_agent_terminal_output(status: &str, result: Option<&str>, error: Optio
|
||||
sections.join("")
|
||||
}
|
||||
|
||||
struct AnthropicRuntimeClient {
|
||||
struct ProviderRuntimeClient {
|
||||
runtime: tokio::runtime::Runtime,
|
||||
client: AnthropicClient,
|
||||
client: ProviderClient,
|
||||
model: String,
|
||||
allowed_tools: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl AnthropicRuntimeClient {
|
||||
impl ProviderRuntimeClient {
|
||||
fn new(model: String, allowed_tools: BTreeSet<String>) -> Result<Self, String> {
|
||||
let client = AnthropicClient::from_env()
|
||||
.map_err(|error| error.to_string())?
|
||||
.with_base_url(read_base_url());
|
||||
let model = resolve_model_alias(&model).to_string();
|
||||
let client = ProviderClient::from_model(&model).map_err(|error| error.to_string())?;
|
||||
Ok(Self {
|
||||
runtime: tokio::runtime::Runtime::new().map_err(|error| error.to_string())?,
|
||||
client,
|
||||
@@ -1656,7 +1655,7 @@ impl AnthropicRuntimeClient {
|
||||
}
|
||||
}
|
||||
|
||||
impl ApiClient for AnthropicRuntimeClient {
|
||||
impl ApiClient for ProviderRuntimeClient {
|
||||
fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
|
||||
let tools = tool_specs_for_allowed_tools(Some(&self.allowed_tools))
|
||||
.into_iter()
|
||||
@@ -1668,7 +1667,7 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
.collect::<Vec<_>>();
|
||||
let message_request = MessageRequest {
|
||||
model: self.model.clone(),
|
||||
max_tokens: 32_000,
|
||||
max_tokens: max_tokens_for_model(&self.model),
|
||||
messages: convert_messages(&request.messages),
|
||||
system: (!request.system_prompt.is_empty()).then(|| request.system_prompt.join("\n\n")),
|
||||
tools: (!tools.is_empty()).then_some(tools),
|
||||
@@ -1683,7 +1682,7 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
.await
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tool: Option<(String, String, String)> = None;
|
||||
let mut pending_tools: BTreeMap<u32, (String, String, String)> = BTreeMap::new();
|
||||
let mut saw_stop = false;
|
||||
|
||||
while let Some(event) = stream
|
||||
@@ -1694,14 +1693,15 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
match event {
|
||||
ApiStreamEvent::MessageStart(start) => {
|
||||
for block in start.message.content {
|
||||
push_output_block(block, &mut events, &mut pending_tool, true);
|
||||
push_output_block(block, 0, &mut events, &mut pending_tools, true);
|
||||
}
|
||||
}
|
||||
ApiStreamEvent::ContentBlockStart(start) => {
|
||||
push_output_block(
|
||||
start.content_block,
|
||||
start.index,
|
||||
&mut events,
|
||||
&mut pending_tool,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
);
|
||||
}
|
||||
@@ -1712,13 +1712,13 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
}
|
||||
}
|
||||
ContentBlockDelta::InputJsonDelta { partial_json } => {
|
||||
if let Some((_, _, input)) = &mut pending_tool {
|
||||
if let Some((_, _, input)) = pending_tools.get_mut(&delta.index) {
|
||||
input.push_str(&partial_json);
|
||||
}
|
||||
}
|
||||
},
|
||||
ApiStreamEvent::ContentBlockStop(_) => {
|
||||
if let Some((id, name, input)) = pending_tool.take() {
|
||||
ApiStreamEvent::ContentBlockStop(stop) => {
|
||||
if let Some((id, name, input)) = pending_tools.remove(&stop.index) {
|
||||
events.push(AssistantEvent::ToolUse { id, name, input });
|
||||
}
|
||||
}
|
||||
@@ -1839,8 +1839,9 @@ fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
|
||||
|
||||
fn push_output_block(
|
||||
block: OutputContentBlock,
|
||||
block_index: u32,
|
||||
events: &mut Vec<AssistantEvent>,
|
||||
pending_tool: &mut Option<(String, String, String)>,
|
||||
pending_tools: &mut BTreeMap<u32, (String, String, String)>,
|
||||
streaming_tool_input: bool,
|
||||
) {
|
||||
match block {
|
||||
@@ -1858,18 +1859,19 @@ fn push_output_block(
|
||||
} else {
|
||||
input.to_string()
|
||||
};
|
||||
*pending_tool = Some((id, name, initial_input));
|
||||
pending_tools.insert(block_index, (id, name, initial_input));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn response_to_events(response: MessageResponse) -> Vec<AssistantEvent> {
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tool = None;
|
||||
let mut pending_tools = BTreeMap::new();
|
||||
|
||||
for block in response.content {
|
||||
push_output_block(block, &mut events, &mut pending_tool, false);
|
||||
if let Some((id, name, input)) = pending_tool.take() {
|
||||
for (index, block) in response.content.into_iter().enumerate() {
|
||||
let index = u32::try_from(index).expect("response block index overflow");
|
||||
push_output_block(block, index, &mut events, &mut pending_tools, false);
|
||||
if let Some((id, name, input)) = pending_tools.remove(&index) {
|
||||
events.push(AssistantEvent::ToolUse { id, name, input });
|
||||
}
|
||||
}
|
||||
@@ -2893,6 +2895,7 @@ fn parse_skill_description(contents: &str) -> Option<String> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::BTreeSet;
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
@@ -2905,8 +2908,9 @@ mod tests {
|
||||
use super::{
|
||||
agent_permission_policy, allowed_tools_for_subagent, execute_agent_with_spawn,
|
||||
execute_tool, final_assistant_text, mvp_tool_specs, persist_agent_terminal_state,
|
||||
AgentInput, AgentJob, SubagentToolExecutor,
|
||||
push_output_block, AgentInput, AgentJob, SubagentToolExecutor,
|
||||
};
|
||||
use api::OutputContentBlock;
|
||||
use runtime::{ApiRequest, AssistantEvent, ConversationRuntime, RuntimeError, Session};
|
||||
use serde_json::json;
|
||||
|
||||
@@ -3121,6 +3125,63 @@ mod tests {
|
||||
assert!(error.contains("relative URL without a base") || error.contains("empty host"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_tools_preserve_multiple_streaming_tool_calls_by_index() {
|
||||
let mut events = Vec::new();
|
||||
let mut pending_tools = BTreeMap::new();
|
||||
|
||||
push_output_block(
|
||||
OutputContentBlock::ToolUse {
|
||||
id: "tool-1".to_string(),
|
||||
name: "read_file".to_string(),
|
||||
input: json!({}),
|
||||
},
|
||||
1,
|
||||
&mut events,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
);
|
||||
push_output_block(
|
||||
OutputContentBlock::ToolUse {
|
||||
id: "tool-2".to_string(),
|
||||
name: "grep_search".to_string(),
|
||||
input: json!({}),
|
||||
},
|
||||
2,
|
||||
&mut events,
|
||||
&mut pending_tools,
|
||||
true,
|
||||
);
|
||||
|
||||
pending_tools
|
||||
.get_mut(&1)
|
||||
.expect("first tool pending")
|
||||
.2
|
||||
.push_str("{\"path\":\"src/main.rs\"}");
|
||||
pending_tools
|
||||
.get_mut(&2)
|
||||
.expect("second tool pending")
|
||||
.2
|
||||
.push_str("{\"pattern\":\"TODO\"}");
|
||||
|
||||
assert_eq!(
|
||||
pending_tools.remove(&1),
|
||||
Some((
|
||||
"tool-1".to_string(),
|
||||
"read_file".to_string(),
|
||||
"{\"path\":\"src/main.rs\"}".to_string(),
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
pending_tools.remove(&2),
|
||||
Some((
|
||||
"tool-2".to_string(),
|
||||
"grep_search".to_string(),
|
||||
"{\"pattern\":\"TODO\"}".to_string(),
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn todo_write_persists_and_returns_previous_state() {
|
||||
let _guard = env_lock()
|
||||
|
||||
Reference in New Issue
Block a user