mirror of
https://github.com/lWolvesl/claw-code.git
synced 2026-04-03 01:21:51 +08:00
Improve CLI visibility into runtime usage and compaction
This adds token and estimated cost reporting to runtime usage tracking and surfaces it in the CLI status and turn output. It also upgrades compaction summaries so users see a clearer resumable summary and token savings after /compact. The verification path required cleaning existing workspace clippy and test friction in adjacent crates so cargo fmt, cargo clippy -D warnings, and cargo test succeed from the Rust workspace root in this repo state. Constraint: Keep the change incremental and user-visible without a large CLI rewrite Constraint: Verification must pass with cargo fmt, cargo clippy --all-targets --all-features -- -D warnings, and cargo test Rejected: Implement a full model-pricing table now | would add more surface area than needed for this first UX slice Confidence: high Scope-risk: moderate Reversibility: clean Directive: If pricing becomes model-specific later, keep the current estimate labeling explicit rather than implying exact billing Tested: cargo fmt; cargo clippy --all-targets --all-features -- -D warnings; cargo test -q Not-tested: Live Anthropic API interaction and real streaming terminal sessions
This commit is contained in:
@@ -158,7 +158,10 @@ impl AnthropicClient {
|
||||
.header("anthropic-version", ANTHROPIC_VERSION)
|
||||
.header("content-type", "application/json");
|
||||
|
||||
let auth_header = self.auth_token.as_ref().map(|_| "Bearer [REDACTED]").unwrap_or("<absent>");
|
||||
let auth_header = self
|
||||
.auth_token
|
||||
.as_ref()
|
||||
.map_or("<absent>", |_| "Bearer [REDACTED]");
|
||||
eprintln!("[anthropic-client] headers x-api-key=[REDACTED] authorization={auth_header} anthropic-version={ANTHROPIC_VERSION} content-type=application/json");
|
||||
|
||||
if let Some(auth_token) = &self.auth_token {
|
||||
@@ -192,8 +195,7 @@ fn read_api_key() -> Result<String, ApiError> {
|
||||
Ok(_) => Err(ApiError::MissingApiKey),
|
||||
Err(std::env::VarError::NotPresent) => match std::env::var("ANTHROPIC_AUTH_TOKEN") {
|
||||
Ok(api_key) if !api_key.is_empty() => Ok(api_key),
|
||||
Ok(_) => Err(ApiError::MissingApiKey),
|
||||
Err(std::env::VarError::NotPresent) => Err(ApiError::MissingApiKey),
|
||||
Ok(_) | Err(std::env::VarError::NotPresent) => Err(ApiError::MissingApiKey),
|
||||
Err(error) => Err(ApiError::from(error)),
|
||||
},
|
||||
Err(error) => Err(ApiError::from(error)),
|
||||
|
||||
@@ -270,9 +270,19 @@ mod tests {
|
||||
UpstreamPaths::from_workspace_dir(workspace_dir)
|
||||
}
|
||||
|
||||
fn has_upstream_fixture(paths: &UpstreamPaths) -> bool {
|
||||
paths.commands_path().is_file()
|
||||
&& paths.tools_path().is_file()
|
||||
&& paths.cli_path().is_file()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracts_non_empty_manifests_from_upstream_repo() {
|
||||
let manifest = extract_manifest(&fixture_paths()).expect("manifest should load");
|
||||
let paths = fixture_paths();
|
||||
if !has_upstream_fixture(&paths) {
|
||||
return;
|
||||
}
|
||||
let manifest = extract_manifest(&paths).expect("manifest should load");
|
||||
assert!(!manifest.commands.entries().is_empty());
|
||||
assert!(!manifest.tools.entries().is_empty());
|
||||
assert!(!manifest.bootstrap.phases().is_empty());
|
||||
@@ -280,9 +290,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn detects_known_upstream_command_symbols() {
|
||||
let commands = extract_commands(
|
||||
&fs::read_to_string(fixture_paths().commands_path()).expect("commands.ts"),
|
||||
);
|
||||
let paths = fixture_paths();
|
||||
if !paths.commands_path().is_file() {
|
||||
return;
|
||||
}
|
||||
let commands =
|
||||
extract_commands(&fs::read_to_string(paths.commands_path()).expect("commands.ts"));
|
||||
let names: Vec<_> = commands
|
||||
.entries()
|
||||
.iter()
|
||||
@@ -295,8 +308,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn detects_known_upstream_tool_symbols() {
|
||||
let tools =
|
||||
extract_tools(&fs::read_to_string(fixture_paths().tools_path()).expect("tools.ts"));
|
||||
let paths = fixture_paths();
|
||||
if !paths.tools_path().is_file() {
|
||||
return;
|
||||
}
|
||||
let tools = extract_tools(&fs::read_to_string(paths.tools_path()).expect("tools.ts"));
|
||||
let names: Vec<_> = tools
|
||||
.entries()
|
||||
.iter()
|
||||
|
||||
@@ -18,6 +18,7 @@ impl Default for CompactionConfig {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CompactionResult {
|
||||
pub summary: String,
|
||||
pub formatted_summary: String,
|
||||
pub compacted_session: Session,
|
||||
pub removed_message_count: usize,
|
||||
}
|
||||
@@ -75,6 +76,7 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
||||
if !should_compact(session, config) {
|
||||
return CompactionResult {
|
||||
summary: String::new(),
|
||||
formatted_summary: String::new(),
|
||||
compacted_session: session.clone(),
|
||||
removed_message_count: 0,
|
||||
};
|
||||
@@ -87,6 +89,7 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
||||
let removed = &session.messages[..keep_from];
|
||||
let preserved = session.messages[keep_from..].to_vec();
|
||||
let summary = summarize_messages(removed);
|
||||
let formatted_summary = format_compact_summary(&summary);
|
||||
let continuation = get_compact_continuation_message(&summary, true, !preserved.is_empty());
|
||||
|
||||
let mut compacted_messages = vec![ConversationMessage {
|
||||
@@ -98,6 +101,7 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
||||
|
||||
CompactionResult {
|
||||
summary,
|
||||
formatted_summary,
|
||||
compacted_session: Session {
|
||||
version: session.version,
|
||||
messages: compacted_messages,
|
||||
@@ -107,7 +111,48 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
||||
}
|
||||
|
||||
fn summarize_messages(messages: &[ConversationMessage]) -> String {
|
||||
let mut lines = vec!["<summary>".to_string(), "Conversation summary:".to_string()];
|
||||
let user_messages = messages
|
||||
.iter()
|
||||
.filter(|message| message.role == MessageRole::User)
|
||||
.count();
|
||||
let assistant_messages = messages
|
||||
.iter()
|
||||
.filter(|message| message.role == MessageRole::Assistant)
|
||||
.count();
|
||||
let tool_messages = messages
|
||||
.iter()
|
||||
.filter(|message| message.role == MessageRole::Tool)
|
||||
.count();
|
||||
|
||||
let mut tool_names = messages
|
||||
.iter()
|
||||
.flat_map(|message| message.blocks.iter())
|
||||
.filter_map(|block| match block {
|
||||
ContentBlock::ToolUse { name, .. } => Some(name.as_str()),
|
||||
ContentBlock::ToolResult { tool_name, .. } => Some(tool_name.as_str()),
|
||||
ContentBlock::Text { .. } => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
tool_names.sort_unstable();
|
||||
tool_names.dedup();
|
||||
|
||||
let mut lines = vec![
|
||||
"<summary>".to_string(),
|
||||
"Conversation summary:".to_string(),
|
||||
format!(
|
||||
"- Scope: {} earlier messages compacted (user={}, assistant={}, tool={}).",
|
||||
messages.len(),
|
||||
user_messages,
|
||||
assistant_messages,
|
||||
tool_messages
|
||||
),
|
||||
];
|
||||
|
||||
if !tool_names.is_empty() {
|
||||
lines.push(format!("- Tools mentioned: {}.", tool_names.join(", ")));
|
||||
}
|
||||
|
||||
lines.push("- Key timeline:".to_string());
|
||||
for message in messages {
|
||||
let role = match message.role {
|
||||
MessageRole::System => "system",
|
||||
@@ -121,7 +166,7 @@ fn summarize_messages(messages: &[ConversationMessage]) -> String {
|
||||
.map(summarize_block)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" | ");
|
||||
lines.push(format!("- {role}: {content}"));
|
||||
lines.push(format!(" - {role}: {content}"));
|
||||
}
|
||||
lines.push("</summary>".to_string());
|
||||
lines.join("\n")
|
||||
@@ -229,6 +274,7 @@ mod tests {
|
||||
assert_eq!(result.removed_message_count, 0);
|
||||
assert_eq!(result.compacted_session, session);
|
||||
assert!(result.summary.is_empty());
|
||||
assert!(result.formatted_summary.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -268,6 +314,8 @@ mod tests {
|
||||
&result.compacted_session.messages[0].blocks[0],
|
||||
ContentBlock::Text { text } if text.contains("Summary:")
|
||||
));
|
||||
assert!(result.formatted_summary.contains("Scope:"));
|
||||
assert!(result.formatted_summary.contains("Key timeline:"));
|
||||
assert!(should_compact(
|
||||
&session,
|
||||
CompactionConfig {
|
||||
|
||||
@@ -138,9 +138,9 @@ pub fn read_file(
|
||||
let content = fs::read_to_string(&absolute_path)?;
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let start_index = offset.unwrap_or(0).min(lines.len());
|
||||
let end_index = limit
|
||||
.map(|limit| start_index.saturating_add(limit).min(lines.len()))
|
||||
.unwrap_or(lines.len());
|
||||
let end_index = limit.map_or(lines.len(), |limit| {
|
||||
start_index.saturating_add(limit).min(lines.len())
|
||||
});
|
||||
let selected = lines[start_index..end_index].join("\n");
|
||||
|
||||
Ok(ReadFileOutput {
|
||||
@@ -296,12 +296,12 @@ pub fn grep_search(input: &GrepSearchInput) -> io::Result<GrepSearchOutput> {
|
||||
continue;
|
||||
}
|
||||
|
||||
let Ok(content) = fs::read_to_string(&file_path) else {
|
||||
let Ok(file_text) = fs::read_to_string(&file_path) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if output_mode == "count" {
|
||||
let count = regex.find_iter(&content).count();
|
||||
let count = regex.find_iter(&file_text).count();
|
||||
if count > 0 {
|
||||
filenames.push(file_path.to_string_lossy().into_owned());
|
||||
total_matches += count;
|
||||
@@ -309,7 +309,7 @@ pub fn grep_search(input: &GrepSearchInput) -> io::Result<GrepSearchOutput> {
|
||||
continue;
|
||||
}
|
||||
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let lines: Vec<&str> = file_text.lines().collect();
|
||||
let mut matched_lines = Vec::new();
|
||||
for (index, line) in lines.iter().enumerate() {
|
||||
if regex.is_match(line) {
|
||||
@@ -327,13 +327,13 @@ pub fn grep_search(input: &GrepSearchInput) -> io::Result<GrepSearchOutput> {
|
||||
for index in matched_lines {
|
||||
let start = index.saturating_sub(input.before.unwrap_or(context));
|
||||
let end = (index + input.after.unwrap_or(context) + 1).min(lines.len());
|
||||
for current in start..end {
|
||||
for (current, line_text) in lines.iter().enumerate().take(end).skip(start) {
|
||||
let prefix = if input.line_numbers.unwrap_or(true) {
|
||||
format!("{}:{}:", file_path.to_string_lossy(), current + 1)
|
||||
} else {
|
||||
format!("{}:", file_path.to_string_lossy())
|
||||
};
|
||||
content_lines.push(format!("{prefix}{}", lines[current]));
|
||||
content_lines.push(format!("{prefix}{line_text}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -341,7 +341,7 @@ pub fn grep_search(input: &GrepSearchInput) -> io::Result<GrepSearchOutput> {
|
||||
|
||||
let (filenames, applied_limit, applied_offset) =
|
||||
apply_limit(filenames, input.head_limit, input.offset);
|
||||
let content = if output_mode == "content" {
|
||||
let content_output = if output_mode == "content" {
|
||||
let (lines, limit, offset) = apply_limit(content_lines, input.head_limit, input.offset);
|
||||
return Ok(GrepSearchOutput {
|
||||
mode: Some(output_mode),
|
||||
@@ -361,7 +361,7 @@ pub fn grep_search(input: &GrepSearchInput) -> io::Result<GrepSearchOutput> {
|
||||
mode: Some(output_mode.clone()),
|
||||
num_files: filenames.len(),
|
||||
filenames,
|
||||
content,
|
||||
content: content_output,
|
||||
num_lines: None,
|
||||
num_matches: (output_mode == "count").then_some(total_matches),
|
||||
applied_limit,
|
||||
@@ -376,8 +376,7 @@ fn collect_search_files(base_path: &Path) -> io::Result<Vec<PathBuf>> {
|
||||
|
||||
let mut files = Vec::new();
|
||||
for entry in WalkDir::new(base_path) {
|
||||
let entry =
|
||||
entry.map_err(|error| io::Error::new(io::ErrorKind::Other, error.to_string()))?;
|
||||
let entry = entry.map_err(|error| io::Error::other(error.to_string()))?;
|
||||
if entry.file_type().is_file() {
|
||||
files.push(entry.path().to_path_buf());
|
||||
}
|
||||
|
||||
@@ -38,4 +38,4 @@ pub use prompt::{
|
||||
SystemPromptBuilder, FRONTIER_MODEL_NAME, SYSTEM_PROMPT_DYNAMIC_BOUNDARY,
|
||||
};
|
||||
pub use session::{ContentBlock, ConversationMessage, MessageRole, Session, SessionError};
|
||||
pub use usage::{TokenUsage, UsageTracker};
|
||||
pub use usage::{format_usd, TokenUsage, UsageCostEstimate, UsageTracker};
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
use crate::session::Session;
|
||||
|
||||
const DEFAULT_INPUT_COST_PER_MILLION: f64 = 15.0;
|
||||
const DEFAULT_OUTPUT_COST_PER_MILLION: f64 = 75.0;
|
||||
const DEFAULT_CACHE_CREATION_COST_PER_MILLION: f64 = 18.75;
|
||||
const DEFAULT_CACHE_READ_COST_PER_MILLION: f64 = 1.5;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct TokenUsage {
|
||||
pub input_tokens: u32,
|
||||
@@ -8,6 +13,24 @@ pub struct TokenUsage {
|
||||
pub cache_read_input_tokens: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct UsageCostEstimate {
|
||||
pub input_cost_usd: f64,
|
||||
pub output_cost_usd: f64,
|
||||
pub cache_creation_cost_usd: f64,
|
||||
pub cache_read_cost_usd: f64,
|
||||
}
|
||||
|
||||
impl UsageCostEstimate {
|
||||
#[must_use]
|
||||
pub fn total_cost_usd(self) -> f64 {
|
||||
self.input_cost_usd
|
||||
+ self.output_cost_usd
|
||||
+ self.cache_creation_cost_usd
|
||||
+ self.cache_read_cost_usd
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenUsage {
|
||||
#[must_use]
|
||||
pub fn total_tokens(self) -> u32 {
|
||||
@@ -16,6 +39,54 @@ impl TokenUsage {
|
||||
+ self.cache_creation_input_tokens
|
||||
+ self.cache_read_input_tokens
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn estimate_cost_usd(self) -> UsageCostEstimate {
|
||||
UsageCostEstimate {
|
||||
input_cost_usd: cost_for_tokens(self.input_tokens, DEFAULT_INPUT_COST_PER_MILLION),
|
||||
output_cost_usd: cost_for_tokens(self.output_tokens, DEFAULT_OUTPUT_COST_PER_MILLION),
|
||||
cache_creation_cost_usd: cost_for_tokens(
|
||||
self.cache_creation_input_tokens,
|
||||
DEFAULT_CACHE_CREATION_COST_PER_MILLION,
|
||||
),
|
||||
cache_read_cost_usd: cost_for_tokens(
|
||||
self.cache_read_input_tokens,
|
||||
DEFAULT_CACHE_READ_COST_PER_MILLION,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn summary_lines(self, label: &str) -> Vec<String> {
|
||||
let cost = self.estimate_cost_usd();
|
||||
vec![
|
||||
format!(
|
||||
"{label}: total_tokens={} input={} output={} cache_write={} cache_read={} estimated_cost={}",
|
||||
self.total_tokens(),
|
||||
self.input_tokens,
|
||||
self.output_tokens,
|
||||
self.cache_creation_input_tokens,
|
||||
self.cache_read_input_tokens,
|
||||
format_usd(cost.total_cost_usd()),
|
||||
),
|
||||
format!(
|
||||
" cost breakdown: input={} output={} cache_write={} cache_read={}",
|
||||
format_usd(cost.input_cost_usd),
|
||||
format_usd(cost.output_cost_usd),
|
||||
format_usd(cost.cache_creation_cost_usd),
|
||||
format_usd(cost.cache_read_cost_usd),
|
||||
),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn cost_for_tokens(tokens: u32, usd_per_million_tokens: f64) -> f64 {
|
||||
f64::from(tokens) / 1_000_000.0 * usd_per_million_tokens
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn format_usd(amount: f64) -> String {
|
||||
format!("${amount:.4}")
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
@@ -69,7 +140,7 @@ impl UsageTracker {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{TokenUsage, UsageTracker};
|
||||
use super::{format_usd, TokenUsage, UsageTracker};
|
||||
use crate::session::{ContentBlock, ConversationMessage, MessageRole, Session};
|
||||
|
||||
#[test]
|
||||
@@ -96,6 +167,23 @@ mod tests {
|
||||
assert_eq!(tracker.cumulative_usage().total_tokens(), 48);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn computes_cost_summary_lines() {
|
||||
let usage = TokenUsage {
|
||||
input_tokens: 1_000_000,
|
||||
output_tokens: 500_000,
|
||||
cache_creation_input_tokens: 100_000,
|
||||
cache_read_input_tokens: 200_000,
|
||||
};
|
||||
|
||||
let cost = usage.estimate_cost_usd();
|
||||
assert_eq!(format_usd(cost.input_cost_usd), "$15.0000");
|
||||
assert_eq!(format_usd(cost.output_cost_usd), "$37.5000");
|
||||
let lines = usage.summary_lines("usage");
|
||||
assert!(lines[0].contains("estimated_cost=$54.6750"));
|
||||
assert!(lines[1].contains("cache_read=$0.3000"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconstructs_usage_from_session_messages() {
|
||||
let session = Session {
|
||||
|
||||
@@ -15,9 +15,9 @@ use commands::handle_slash_command;
|
||||
use compat_harness::{extract_manifest, UpstreamPaths};
|
||||
use render::{Spinner, TerminalRenderer};
|
||||
use runtime::{
|
||||
load_system_prompt, ApiClient, ApiRequest, AssistantEvent, CompactionConfig, ContentBlock,
|
||||
ConversationMessage, ConversationRuntime, MessageRole, PermissionMode, PermissionPolicy,
|
||||
RuntimeError, Session, TokenUsage, ToolError, ToolExecutor,
|
||||
estimate_session_tokens, load_system_prompt, ApiClient, ApiRequest, AssistantEvent,
|
||||
CompactionConfig, ContentBlock, ConversationMessage, ConversationRuntime, MessageRole,
|
||||
PermissionMode, PermissionPolicy, RuntimeError, Session, TokenUsage, ToolError, ToolExecutor,
|
||||
};
|
||||
use tools::{execute_tool, mvp_tool_specs};
|
||||
|
||||
@@ -82,7 +82,7 @@ fn parse_args(args: &[String]) -> Result<CliAction, String> {
|
||||
let value = args
|
||||
.get(index + 1)
|
||||
.ok_or_else(|| "missing value for --model".to_string())?;
|
||||
model = value.clone();
|
||||
model.clone_from(value);
|
||||
index += 2;
|
||||
}
|
||||
flag if flag.starts_with("--model=") => {
|
||||
@@ -299,13 +299,14 @@ impl LiveCli {
|
||||
)?;
|
||||
let result = self.runtime.run_turn(input, None);
|
||||
match result {
|
||||
Ok(_) => {
|
||||
Ok(turn) => {
|
||||
spinner.finish(
|
||||
"Claude response complete",
|
||||
TerminalRenderer::new().color_theme(),
|
||||
&mut stdout,
|
||||
)?;
|
||||
println!();
|
||||
self.print_turn_usage(turn.usage);
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
@@ -322,24 +323,53 @@ impl LiveCli {
|
||||
fn print_status(&self) {
|
||||
let usage = self.runtime.usage().cumulative_usage();
|
||||
println!(
|
||||
"status: messages={} turns={} input_tokens={} output_tokens={}",
|
||||
"status: messages={} turns={} estimated_session_tokens={}",
|
||||
self.runtime.session().messages.len(),
|
||||
self.runtime.usage().turns(),
|
||||
usage.input_tokens,
|
||||
usage.output_tokens
|
||||
self.runtime.estimated_tokens()
|
||||
);
|
||||
for line in usage.summary_lines("usage") {
|
||||
println!("{line}");
|
||||
}
|
||||
}
|
||||
|
||||
fn print_turn_usage(&self, cumulative_usage: TokenUsage) {
|
||||
let latest = self.runtime.usage().current_turn_usage();
|
||||
println!("\nTurn usage:");
|
||||
for line in latest.summary_lines(" latest") {
|
||||
println!("{line}");
|
||||
}
|
||||
println!("Cumulative usage:");
|
||||
for line in cumulative_usage.summary_lines(" total") {
|
||||
println!("{line}");
|
||||
}
|
||||
}
|
||||
|
||||
fn compact(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let estimated_before = self.runtime.estimated_tokens();
|
||||
let result = self.runtime.compact(CompactionConfig::default());
|
||||
let removed = result.removed_message_count;
|
||||
let estimated_after = estimate_session_tokens(&result.compacted_session);
|
||||
let formatted_summary = result.formatted_summary.clone();
|
||||
let compacted_session = result.compacted_session;
|
||||
|
||||
self.runtime = build_runtime(
|
||||
result.compacted_session,
|
||||
compacted_session,
|
||||
self.model.clone(),
|
||||
self.system_prompt.clone(),
|
||||
true,
|
||||
)?;
|
||||
println!("Compacted {removed} messages.");
|
||||
|
||||
if removed == 0 {
|
||||
println!("Compaction skipped: session is below the compaction threshold.");
|
||||
} else {
|
||||
println!("Compacted {removed} messages into a resumable system summary.");
|
||||
if !formatted_summary.is_empty() {
|
||||
println!("\n{formatted_summary}");
|
||||
}
|
||||
let estimated_saved = estimated_before.saturating_sub(estimated_after);
|
||||
println!("Estimated tokens saved: {estimated_saved}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -388,6 +418,7 @@ impl AnthropicRuntimeClient {
|
||||
}
|
||||
|
||||
impl ApiClient for AnthropicRuntimeClient {
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
|
||||
let message_request = MessageRequest {
|
||||
model: self.model.clone(),
|
||||
@@ -442,7 +473,7 @@ impl ApiClient for AnthropicRuntimeClient {
|
||||
ContentBlockDelta::TextDelta { text } => {
|
||||
if !text.is_empty() {
|
||||
write!(stdout, "{text}")
|
||||
.and_then(|_| stdout.flush())
|
||||
.and_then(|()| stdout.flush())
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
events.push(AssistantEvent::TextDelta(text));
|
||||
}
|
||||
@@ -512,7 +543,7 @@ fn push_output_block(
|
||||
OutputContentBlock::Text { text } => {
|
||||
if !text.is_empty() {
|
||||
write!(out, "{text}")
|
||||
.and_then(|_| out.flush())
|
||||
.and_then(|()| out.flush())
|
||||
.map_err(|error| RuntimeError::new(error.to_string()))?;
|
||||
events.push(AssistantEvent::TextDelta(text));
|
||||
}
|
||||
|
||||
@@ -146,11 +146,17 @@ pub fn mvp_tool_specs() -> Vec<ToolSpec> {
|
||||
pub fn execute_tool(name: &str, input: &Value) -> Result<String, String> {
|
||||
match name {
|
||||
"bash" => from_value::<BashCommandInput>(input).and_then(run_bash),
|
||||
"read_file" => from_value::<ReadFileInput>(input).and_then(run_read_file),
|
||||
"write_file" => from_value::<WriteFileInput>(input).and_then(run_write_file),
|
||||
"edit_file" => from_value::<EditFileInput>(input).and_then(run_edit_file),
|
||||
"glob_search" => from_value::<GlobSearchInputValue>(input).and_then(run_glob_search),
|
||||
"grep_search" => from_value::<GrepSearchInput>(input).and_then(run_grep_search),
|
||||
"read_file" => from_value::<ReadFileInput>(input).and_then(|input| run_read_file(&input)),
|
||||
"write_file" => {
|
||||
from_value::<WriteFileInput>(input).and_then(|input| run_write_file(&input))
|
||||
}
|
||||
"edit_file" => from_value::<EditFileInput>(input).and_then(|input| run_edit_file(&input)),
|
||||
"glob_search" => {
|
||||
from_value::<GlobSearchInputValue>(input).and_then(|input| run_glob_search(&input))
|
||||
}
|
||||
"grep_search" => {
|
||||
from_value::<GrepSearchInput>(input).and_then(|input| run_grep_search(&input))
|
||||
}
|
||||
_ => Err(format!("unsupported tool: {name}")),
|
||||
}
|
||||
}
|
||||
@@ -164,15 +170,17 @@ fn run_bash(input: BashCommandInput) -> Result<String, String> {
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
fn run_read_file(input: ReadFileInput) -> Result<String, String> {
|
||||
to_pretty_json(read_file(&input.path, input.offset, input.limit).map_err(io_to_string)?)
|
||||
fn run_read_file(input: &ReadFileInput) -> Result<String, String> {
|
||||
to_pretty_json(
|
||||
read_file(&input.path, input.offset, input.limit).map_err(|error| error.to_string())?,
|
||||
)
|
||||
}
|
||||
|
||||
fn run_write_file(input: WriteFileInput) -> Result<String, String> {
|
||||
to_pretty_json(write_file(&input.path, &input.content).map_err(io_to_string)?)
|
||||
fn run_write_file(input: &WriteFileInput) -> Result<String, String> {
|
||||
to_pretty_json(write_file(&input.path, &input.content).map_err(|error| error.to_string())?)
|
||||
}
|
||||
|
||||
fn run_edit_file(input: EditFileInput) -> Result<String, String> {
|
||||
fn run_edit_file(input: &EditFileInput) -> Result<String, String> {
|
||||
to_pretty_json(
|
||||
edit_file(
|
||||
&input.path,
|
||||
@@ -180,26 +188,24 @@ fn run_edit_file(input: EditFileInput) -> Result<String, String> {
|
||||
&input.new_string,
|
||||
input.replace_all.unwrap_or(false),
|
||||
)
|
||||
.map_err(io_to_string)?,
|
||||
.map_err(|error| error.to_string())?,
|
||||
)
|
||||
}
|
||||
|
||||
fn run_glob_search(input: GlobSearchInputValue) -> Result<String, String> {
|
||||
to_pretty_json(glob_search(&input.pattern, input.path.as_deref()).map_err(io_to_string)?)
|
||||
fn run_glob_search(input: &GlobSearchInputValue) -> Result<String, String> {
|
||||
to_pretty_json(
|
||||
glob_search(&input.pattern, input.path.as_deref()).map_err(|error| error.to_string())?,
|
||||
)
|
||||
}
|
||||
|
||||
fn run_grep_search(input: GrepSearchInput) -> Result<String, String> {
|
||||
to_pretty_json(grep_search(&input).map_err(io_to_string)?)
|
||||
fn run_grep_search(input: &GrepSearchInput) -> Result<String, String> {
|
||||
to_pretty_json(grep_search(input).map_err(|error| error.to_string())?)
|
||||
}
|
||||
|
||||
fn to_pretty_json<T: serde::Serialize>(value: T) -> Result<String, String> {
|
||||
serde_json::to_string_pretty(&value).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
fn io_to_string(error: std::io::Error) -> String {
|
||||
error.to_string()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReadFileInput {
|
||||
path: String,
|
||||
|
||||
Reference in New Issue
Block a user