1ad81e3b69
Signed-off-by: Adrian Cole <adrian@tetrate.io>
467 lines
17 KiB
Rust
467 lines
17 KiB
Rust
use anyhow::Result;
|
|
use async_trait::async_trait;
|
|
use rmcp::model::Role;
|
|
use serde_json::{json, Value};
|
|
use std::ffi::OsString;
|
|
use std::path::PathBuf;
|
|
use std::process::Stdio;
|
|
use tokio::io::{AsyncBufReadExt, BufReader};
|
|
use tokio::process::Command;
|
|
|
|
use super::base::{ConfigKey, Provider, ProviderDef, ProviderMetadata, ProviderUsage, Usage};
|
|
use super::errors::ProviderError;
|
|
use super::utils::{filter_extensions_from_system_prompt, RequestLog};
|
|
use crate::config::base::ClaudeCodeCommand;
|
|
use crate::config::search_path::SearchPaths;
|
|
use crate::config::{Config, GooseMode};
|
|
use crate::conversation::message::{Message, MessageContent};
|
|
use crate::model::ModelConfig;
|
|
use crate::subprocess::configure_command_no_window;
|
|
use futures::future::BoxFuture;
|
|
use rmcp::model::Tool;
|
|
|
|
const CLAUDE_CODE_PROVIDER_NAME: &str = "claude-code";
|
|
pub const CLAUDE_CODE_DEFAULT_MODEL: &str = "claude-sonnet-4-20250514";
|
|
pub const CLAUDE_CODE_KNOWN_MODELS: &[&str] = &["sonnet", "opus"];
|
|
pub const CLAUDE_CODE_DOC_URL: &str = "https://code.claude.com/docs/en/setup";
|
|
|
|
#[derive(Debug, serde::Serialize)]
|
|
pub struct ClaudeCodeProvider {
|
|
command: PathBuf,
|
|
model: ModelConfig,
|
|
#[serde(skip)]
|
|
name: String,
|
|
}
|
|
|
|
impl ClaudeCodeProvider {
|
|
pub async fn from_env(model: ModelConfig) -> Result<Self> {
|
|
let config = crate::config::Config::global();
|
|
let command: OsString = config.get_claude_code_command().unwrap_or_default().into();
|
|
let resolved_command = SearchPaths::builder().with_npm().resolve(command)?;
|
|
|
|
Ok(Self {
|
|
command: resolved_command,
|
|
model,
|
|
name: CLAUDE_CODE_PROVIDER_NAME.to_string(),
|
|
})
|
|
}
|
|
|
|
/// Convert goose messages to the format expected by claude CLI
|
|
fn messages_to_claude_format(&self, _system: &str, messages: &[Message]) -> Result<Value> {
|
|
let mut claude_messages = Vec::new();
|
|
|
|
for message in messages.iter().filter(|m| m.is_agent_visible()) {
|
|
let role = match message.role {
|
|
Role::User => "user",
|
|
Role::Assistant => "assistant",
|
|
};
|
|
|
|
let mut content_parts = Vec::new();
|
|
for content in &message.content {
|
|
match content {
|
|
MessageContent::Text(text_content) => {
|
|
content_parts.push(json!({
|
|
"type": "text",
|
|
"text": text_content.text
|
|
}));
|
|
}
|
|
MessageContent::ToolRequest(tool_request) => {
|
|
if let Ok(tool_call) = &tool_request.tool_call {
|
|
content_parts.push(json!({
|
|
"type": "tool_use",
|
|
"id": tool_request.id,
|
|
"name": tool_call.name,
|
|
"input": tool_call.arguments
|
|
}));
|
|
}
|
|
}
|
|
MessageContent::ToolResponse(tool_response) => {
|
|
if let Ok(result) = &tool_response.tool_result {
|
|
// Convert tool result contents to text
|
|
let content_text = result
|
|
.content
|
|
.iter()
|
|
.filter_map(|content| match &content.raw {
|
|
rmcp::model::RawContent::Text(text_content) => {
|
|
Some(text_content.text.as_str())
|
|
}
|
|
_ => None,
|
|
})
|
|
.collect::<Vec<&str>>()
|
|
.join("\n");
|
|
|
|
content_parts.push(json!({
|
|
"type": "tool_result",
|
|
"tool_use_id": tool_response.id,
|
|
"content": content_text
|
|
}));
|
|
}
|
|
}
|
|
_ => {
|
|
// Skip other content types for now
|
|
}
|
|
}
|
|
}
|
|
|
|
claude_messages.push(json!({
|
|
"role": role,
|
|
"content": content_parts
|
|
}));
|
|
}
|
|
|
|
Ok(json!(claude_messages))
|
|
}
|
|
|
|
/// Parse the JSON response from claude CLI
|
|
fn apply_permission_flags(cmd: &mut Command) -> Result<(), ProviderError> {
|
|
let config = Config::global();
|
|
let goose_mode = config.get_goose_mode().unwrap_or(GooseMode::Auto);
|
|
|
|
match goose_mode {
|
|
GooseMode::Auto => {
|
|
cmd.arg("--dangerously-skip-permissions");
|
|
}
|
|
GooseMode::SmartApprove => {
|
|
cmd.arg("--permission-mode").arg("acceptEdits");
|
|
}
|
|
GooseMode::Approve => {
|
|
return Err(ProviderError::RequestFailed(
|
|
"\n\n\n### NOTE\n\n\n \
|
|
Claude Code CLI provider does not support Approve mode.\n \
|
|
Please use Auto (which will run anything it needs to) or \
|
|
SmartApprove (most things will run or Chat Mode)\n\n\n"
|
|
.to_string(),
|
|
));
|
|
}
|
|
GooseMode::Chat => {
|
|
// Chat mode doesn't need permission flags
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn parse_claude_response(
|
|
&self,
|
|
json_lines: &[String],
|
|
) -> Result<(Message, Usage), ProviderError> {
|
|
let mut all_text_content = Vec::new();
|
|
let mut usage = Usage::default();
|
|
|
|
// Join all lines and parse as a single JSON array
|
|
let full_response = json_lines.join("");
|
|
let json_array: Vec<Value> = serde_json::from_str(&full_response).map_err(|e| {
|
|
ProviderError::RequestFailed(format!("Failed to parse JSON response: {}", e))
|
|
})?;
|
|
|
|
for parsed in json_array {
|
|
if let Some(msg_type) = parsed.get("type").and_then(|t| t.as_str()) {
|
|
match msg_type {
|
|
"assistant" => {
|
|
if let Some(message) = parsed.get("message") {
|
|
// Extract text content from this assistant message
|
|
if let Some(content) = message.get("content").and_then(|c| c.as_array())
|
|
{
|
|
for item in content {
|
|
if let Some(content_type) =
|
|
item.get("type").and_then(|t| t.as_str())
|
|
{
|
|
if content_type == "text" {
|
|
if let Some(text) =
|
|
item.get("text").and_then(|t| t.as_str())
|
|
{
|
|
all_text_content.push(text.to_string());
|
|
}
|
|
}
|
|
// Skip tool_use - those are claude CLI's internal tools
|
|
}
|
|
}
|
|
}
|
|
|
|
// Extract usage information
|
|
if let Some(usage_info) = message.get("usage") {
|
|
usage.input_tokens = usage_info
|
|
.get("input_tokens")
|
|
.and_then(|v| v.as_i64())
|
|
.map(|v| v as i32);
|
|
usage.output_tokens = usage_info
|
|
.get("output_tokens")
|
|
.and_then(|v| v.as_i64())
|
|
.map(|v| v as i32);
|
|
|
|
// Calculate total if not provided
|
|
if usage.total_tokens.is_none() {
|
|
if let (Some(input), Some(output)) =
|
|
(usage.input_tokens, usage.output_tokens)
|
|
{
|
|
usage.total_tokens = Some(input + output);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"result" => {
|
|
// Extract additional usage info from result if available
|
|
if let Some(result_usage) = parsed.get("usage") {
|
|
if usage.input_tokens.is_none() {
|
|
usage.input_tokens = result_usage
|
|
.get("input_tokens")
|
|
.and_then(|v| v.as_i64())
|
|
.map(|v| v as i32);
|
|
}
|
|
if usage.output_tokens.is_none() {
|
|
usage.output_tokens = result_usage
|
|
.get("output_tokens")
|
|
.and_then(|v| v.as_i64())
|
|
.map(|v| v as i32);
|
|
}
|
|
}
|
|
}
|
|
_ => {} // Ignore other message types
|
|
}
|
|
}
|
|
}
|
|
|
|
// Combine all text content into a single message
|
|
let combined_text = all_text_content.join("\n\n");
|
|
if combined_text.is_empty() {
|
|
return Err(ProviderError::RequestFailed(
|
|
"No text content found in response".to_string(),
|
|
));
|
|
}
|
|
|
|
let message_content = vec![MessageContent::text(combined_text)];
|
|
|
|
let response_message = Message::new(
|
|
Role::Assistant,
|
|
chrono::Utc::now().timestamp(),
|
|
message_content,
|
|
);
|
|
|
|
Ok((response_message, usage))
|
|
}
|
|
|
|
async fn execute_command(
|
|
&self,
|
|
system: &str,
|
|
messages: &[Message],
|
|
_tools: &[Tool],
|
|
) -> Result<Vec<String>, ProviderError> {
|
|
let messages_json = self
|
|
.messages_to_claude_format(system, messages)
|
|
.map_err(|e| {
|
|
ProviderError::RequestFailed(format!("Failed to format messages: {}", e))
|
|
})?;
|
|
|
|
let filtered_system = filter_extensions_from_system_prompt(system);
|
|
|
|
if std::env::var("GOOSE_CLAUDE_CODE_DEBUG").is_ok() {
|
|
println!("=== CLAUDE CODE PROVIDER DEBUG ===");
|
|
println!("Command: {:?}", self.command);
|
|
println!("Original system prompt length: {} chars", system.len());
|
|
println!(
|
|
"Filtered system prompt length: {} chars",
|
|
filtered_system.len()
|
|
);
|
|
println!("Filtered system prompt: {}", filtered_system);
|
|
println!(
|
|
"Messages JSON: {}",
|
|
serde_json::to_string_pretty(&messages_json)
|
|
.unwrap_or_else(|_| "Failed to serialize".to_string())
|
|
);
|
|
println!("================================");
|
|
}
|
|
|
|
let mut cmd = Command::new(&self.command);
|
|
configure_command_no_window(&mut cmd);
|
|
cmd.arg("-p")
|
|
.arg(messages_json.to_string())
|
|
.arg("--system-prompt")
|
|
.arg(&filtered_system);
|
|
|
|
// Only pass model parameter if it's in the known models list
|
|
if CLAUDE_CODE_KNOWN_MODELS.contains(&self.model.model_name.as_str()) {
|
|
cmd.arg("--model").arg(&self.model.model_name);
|
|
}
|
|
|
|
cmd.arg("--verbose").arg("--output-format").arg("json");
|
|
|
|
// Add permission mode based on GOOSE_MODE setting
|
|
Self::apply_permission_flags(&mut cmd)?;
|
|
|
|
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
|
|
|
|
let mut child = cmd.spawn().map_err(|e| {
|
|
ProviderError::RequestFailed(format!(
|
|
"Failed to spawn Claude CLI command '{:?}': {}.",
|
|
self.command, e
|
|
))
|
|
})?;
|
|
|
|
let stdout = child
|
|
.stdout
|
|
.take()
|
|
.ok_or_else(|| ProviderError::RequestFailed("Failed to capture stdout".to_string()))?;
|
|
|
|
let mut reader = BufReader::new(stdout);
|
|
let mut lines = Vec::new();
|
|
let mut line = String::new();
|
|
|
|
loop {
|
|
line.clear();
|
|
match reader.read_line(&mut line).await {
|
|
Ok(0) => break, // EOF
|
|
Ok(_) => {
|
|
let trimmed = line.trim();
|
|
if !trimmed.is_empty() {
|
|
lines.push(trimmed.to_string());
|
|
}
|
|
}
|
|
Err(e) => {
|
|
return Err(ProviderError::RequestFailed(format!(
|
|
"Failed to read output: {}",
|
|
e
|
|
)));
|
|
}
|
|
}
|
|
}
|
|
|
|
let exit_status = child.wait().await.map_err(|e| {
|
|
ProviderError::RequestFailed(format!("Failed to wait for command: {}", e))
|
|
})?;
|
|
|
|
if !exit_status.success() {
|
|
return Err(ProviderError::RequestFailed(format!(
|
|
"Command failed with exit code: {:?}",
|
|
exit_status.code()
|
|
)));
|
|
}
|
|
|
|
tracing::debug!("Command executed successfully, got {} lines", lines.len());
|
|
for (i, line) in lines.iter().enumerate() {
|
|
tracing::debug!("Line {}: {}", i, line);
|
|
}
|
|
|
|
Ok(lines)
|
|
}
|
|
|
|
/// Generate a simple session description without calling subprocess
|
|
fn generate_simple_session_description(
|
|
&self,
|
|
messages: &[Message],
|
|
) -> Result<(Message, ProviderUsage), ProviderError> {
|
|
// Extract the first user message text
|
|
let description = messages
|
|
.iter()
|
|
.find(|m| m.role == Role::User)
|
|
.and_then(|m| {
|
|
m.content.iter().find_map(|c| match c {
|
|
MessageContent::Text(text_content) => Some(&text_content.text),
|
|
_ => None,
|
|
})
|
|
})
|
|
.map(|text| {
|
|
// Take first few words, limit to 4 words
|
|
text.split_whitespace()
|
|
.take(4)
|
|
.collect::<Vec<_>>()
|
|
.join(" ")
|
|
})
|
|
.unwrap_or_else(|| "Simple task".to_string());
|
|
|
|
if std::env::var("GOOSE_CLAUDE_CODE_DEBUG").is_ok() {
|
|
println!("=== CLAUDE CODE PROVIDER DEBUG ===");
|
|
println!("Generated simple session description: {}", description);
|
|
println!("Skipped subprocess call for session description");
|
|
println!("================================");
|
|
}
|
|
|
|
let message = Message::new(
|
|
Role::Assistant,
|
|
chrono::Utc::now().timestamp(),
|
|
vec![MessageContent::text(description.clone())],
|
|
);
|
|
|
|
let usage = Usage::default();
|
|
|
|
Ok((
|
|
message,
|
|
ProviderUsage::new(self.model.model_name.clone(), usage),
|
|
))
|
|
}
|
|
}
|
|
|
|
impl ProviderDef for ClaudeCodeProvider {
|
|
type Provider = Self;
|
|
|
|
fn metadata() -> ProviderMetadata {
|
|
ProviderMetadata::new(
|
|
CLAUDE_CODE_PROVIDER_NAME,
|
|
"Claude Code CLI",
|
|
"Requires claude CLI installed, no MCPs. Use Anthropic provider for full features.",
|
|
CLAUDE_CODE_DEFAULT_MODEL,
|
|
CLAUDE_CODE_KNOWN_MODELS.to_vec(),
|
|
CLAUDE_CODE_DOC_URL,
|
|
vec![ConfigKey::from_value_type::<ClaudeCodeCommand>(true, false)],
|
|
)
|
|
}
|
|
|
|
fn from_env(model: ModelConfig) -> BoxFuture<'static, Result<Self::Provider>> {
|
|
Box::pin(Self::from_env(model))
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Provider for ClaudeCodeProvider {
|
|
fn get_name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
fn get_model_config(&self) -> ModelConfig {
|
|
// Return the model config with appropriate context limit for Claude models
|
|
self.model.clone()
|
|
}
|
|
|
|
#[tracing::instrument(
|
|
skip(self, model_config, system, messages, tools),
|
|
fields(model_config, input, output, input_tokens, output_tokens, total_tokens)
|
|
)]
|
|
async fn complete_with_model(
|
|
&self,
|
|
_session_id: Option<&str>, // create_session == YYYYMMDD_N, but --session-id requires a UUID
|
|
model_config: &ModelConfig,
|
|
system: &str,
|
|
messages: &[Message],
|
|
tools: &[Tool],
|
|
) -> Result<(Message, ProviderUsage), ProviderError> {
|
|
// Check if this is a session description request (short system prompt asking for 4 words or less)
|
|
if system.contains("four words or less") || system.contains("4 words or less") {
|
|
return self.generate_simple_session_description(messages);
|
|
}
|
|
|
|
let json_lines = self.execute_command(system, messages, tools).await?;
|
|
|
|
let (message, usage) = self.parse_claude_response(&json_lines)?;
|
|
|
|
// Create a dummy payload for debug tracing
|
|
let payload = json!({
|
|
"command": self.command,
|
|
"model": model_config.model_name,
|
|
"system": system,
|
|
"messages": messages.len()
|
|
});
|
|
let mut log = RequestLog::start(model_config, &payload)?;
|
|
|
|
let response = json!({
|
|
"lines": json_lines.len(),
|
|
"usage": usage
|
|
});
|
|
|
|
log.write(&response, Some(&usage))?;
|
|
|
|
Ok((
|
|
message,
|
|
ProviderUsage::new(model_config.model_name.clone(), usage),
|
|
))
|
|
}
|
|
}
|