init
This commit is contained in:
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Rust
|
||||
/target/
|
||||
Cargo.lock
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Environment
|
||||
.env
|
||||
.env.local
|
||||
47
Cargo.toml
Normal file
47
Cargo.toml
Normal file
@@ -0,0 +1,47 @@
|
||||
[package]
|
||||
name = "aishell"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["syui"]
|
||||
description = "AI-powered shell automation tool - A generic alternative to Claude Code"
|
||||
|
||||
[lib]
|
||||
name = "aishell"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "aishell"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
# CLI and async (following aigpt pattern)
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
tokio = { version = "1.40", features = ["rt", "rt-multi-thread", "macros", "io-std", "process", "fs"] }
|
||||
async-trait = "0.1"
|
||||
|
||||
# HTTP client for LLM APIs
|
||||
reqwest = { version = "0.12", features = ["json", "stream"] }
|
||||
|
||||
# Serialization
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
# Error handling
|
||||
thiserror = "1.0"
|
||||
anyhow = "1.0"
|
||||
|
||||
# Utilities
|
||||
dirs = "5.0"
|
||||
|
||||
# Shell execution
|
||||
duct = "0.13"
|
||||
|
||||
# Configuration
|
||||
toml = "0.8"
|
||||
|
||||
# Logging
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
# Interactive REPL
|
||||
rustyline = "14.0"
|
||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# aishell
|
||||
|
||||
A single-stream shell where commands and AI coexist — type a command, it runs; type anything else, AI responds.
|
||||
168
claude.md
Normal file
168
claude.md
Normal file
@@ -0,0 +1,168 @@
|
||||
# aishell
|
||||
|
||||
A single-stream shell where commands and AI coexist.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌──────────────────────────────────────────────┐
|
||||
│ aishell │
|
||||
│ │
|
||||
│ ┌──────────┐ judge ┌───────────────┐ │
|
||||
│ │ rustyline │───────────→│ sh -c command │ │
|
||||
│ │ (input) │ is_command └───────────────┘ │
|
||||
│ └──────────┘ │
|
||||
│ │ !is_command │
|
||||
│ ▼ │
|
||||
│ ┌──────────┐ stdin(JSON) ┌──────────────┐ │
|
||||
│ │ send() │─────────────→│ claude │ │
|
||||
│ │ (async) │ │ (persistent) │ │
|
||||
│ └──────────┘ │ stream-json │ │
|
||||
│ └──────────────┘ │
|
||||
│ ┌──────────┐ stdout(JSON) │ │
|
||||
│ │ reader │←────────────────────┘ │
|
||||
│ │ thread │ mpsc channel │
|
||||
│ └──────────┘ │
|
||||
│ │ │
|
||||
│ ▼ │
|
||||
│ ┌──────────┐ │
|
||||
│ │ drain() │──→ println! (unified stream) │
|
||||
│ └──────────┘ │
|
||||
│ │
|
||||
│ ┌───────────────────────────────────────┐ │
|
||||
│ │ ● [1] responding... | [2] thinking... │ │
|
||||
│ └───────────────────────────────────────┘ │
|
||||
└──────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Claude Process (ai.rs)
|
||||
|
||||
### Startup
|
||||
|
||||
One persistent process for the entire session:
|
||||
|
||||
```
|
||||
claude --input-format stream-json --output-format stream-json \
|
||||
--verbose --dangerously-skip-permissions
|
||||
```
|
||||
|
||||
- `--input-format stream-json`: accepts JSON lines on stdin
|
||||
- `--output-format stream-json`: emits JSON events on stdout
|
||||
- Process stays alive until aishell exits. No restart per message.
|
||||
|
||||
### Input Format
|
||||
|
||||
```json
|
||||
{"type":"user","message":{"role":"user","content":"user input here"}}
|
||||
```
|
||||
|
||||
Written to claude's stdin via `Arc<Mutex<ChildStdin>>`.
|
||||
|
||||
### Output Events
|
||||
|
||||
```
|
||||
type: "system" → init event (tools list, MCP servers, model info)
|
||||
type: "assistant" → response content
|
||||
content[].type: "text" → AI text (accumulated)
|
||||
content[].type: "tool_use" → tool execution (name shown in status)
|
||||
type: "result" → turn complete (final text, cost, usage)
|
||||
```
|
||||
|
||||
### Thread Model
|
||||
|
||||
```
|
||||
Main Thread Background Thread
|
||||
│ │
|
||||
├─ readline() ├─ BufReader::lines() on claude stdout
|
||||
├─ judge::is_command() ├─ serde_json::from_str() each line
|
||||
├─ command → executor::execute() ├─ "assistant" → accumulate text, update status
|
||||
├─ AI → claude.send() (non-blocking) ├─ "tool_use" → update status
|
||||
├─ drain_responses() before prompt ├─ "result" → mpsc::send() completed text
|
||||
└─ status.set(status_line()) └─ remove session from status vec
|
||||
|
||||
Polling Thread (200ms interval)
|
||||
├─ try_recv() completed responses → print immediately
|
||||
└─ update status bar
|
||||
```
|
||||
|
||||
### Shared State
|
||||
|
||||
```rust
|
||||
stdin: Arc<Mutex<ChildStdin>> // main → claude stdin writes
|
||||
status: Arc<Mutex<Vec<SessionStatus>>> // both threads read/write status
|
||||
output_tx: mpsc::Sender<String> // background → main completed responses
|
||||
output_rx: mpsc::Receiver<String> // main drains with try_recv()
|
||||
id_tx: mpsc::Sender<usize> // main → background session ID assignment
|
||||
```
|
||||
|
||||
### Non-blocking Send
|
||||
|
||||
```rust
|
||||
pub fn send(&mut self, input: &str) -> usize {
|
||||
let id = self.next_id; // assign session ID
|
||||
self.next_id += 1;
|
||||
status.push(SessionStatus { id, state: "thinking..." });
|
||||
self.id_tx.send(id); // notify reader thread
|
||||
writeln!(stdin, "{}", json); // write JSON to claude stdin
|
||||
stdin.flush();
|
||||
// returns immediately — does NOT wait for response
|
||||
id
|
||||
}
|
||||
```
|
||||
|
||||
## Input Detection (judge.rs)
|
||||
|
||||
Priority order:
|
||||
|
||||
1. Shell operators (`|`, `>`, `<`, `;`, `&`) outside quotes → shell
|
||||
2. Variable assignment (`FOO=bar`) → shell
|
||||
3. Shell builtins (cd, echo, export, etc. — 50 builtins) → shell
|
||||
4. Absolute/relative path to existing file → shell
|
||||
5. Command found in PATH → shell
|
||||
6. **None of the above → AI**
|
||||
|
||||
Quote-aware: operators inside `'...'` or `"..."` are ignored.
|
||||
|
||||
## Command Execution (executor.rs)
|
||||
|
||||
- `cd` → `env::set_current_dir()` (changes process directory)
|
||||
- `cd -` → OLDPWD support, `~` → HOME expansion
|
||||
- Everything else → `sh -c "input"` (pipes, redirects, globs all work)
|
||||
|
||||
## Status Bar (status.rs)
|
||||
|
||||
Terminal last line reserved for Claude status:
|
||||
|
||||
```
|
||||
● idle ← waiting
|
||||
● [1] thinking... ← processing
|
||||
● [1] responding... ← generating text
|
||||
● [1] running: Bash... ← executing tool
|
||||
● [1] responding... | [2] thinking... ← multiple sessions
|
||||
```
|
||||
|
||||
Implementation:
|
||||
- ANSI escape `\x1b[1;{rows-1}r` sets scroll region excluding last line
|
||||
- `\x1b7` save cursor → draw status on last row → `\x1b8` restore cursor
|
||||
- Auto-cleanup on Drop (reset scroll region)
|
||||
|
||||
## Files
|
||||
|
||||
```
|
||||
src/
|
||||
├── main.rs Main loop: input → judge → execute/AI → drain responses
|
||||
├── lib.rs Module declarations
|
||||
├── ai.rs ClaudeManager: persistent process, async send/receive
|
||||
├── judge.rs is_command(): input classification (6 tests)
|
||||
├── executor.rs execute(): sh -c + cd handling
|
||||
└── status.rs StatusBar: ANSI last-line status display
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
```toml
|
||||
rustyline = "14.0" # line input + history
|
||||
serde = "1" # JSON serialization
|
||||
serde_json = "1" # stream-json protocol parsing
|
||||
terminal_size = "0.4" # terminal dimensions for status bar
|
||||
```
|
||||
3
src/cli/mod.rs
Normal file
3
src/cli/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod repl;
|
||||
|
||||
pub use repl::Repl;
|
||||
148
src/cli/repl.rs
Normal file
148
src/cli/repl.rs
Normal file
@@ -0,0 +1,148 @@
|
||||
use anyhow::{Context, Result};
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::DefaultEditor;
|
||||
|
||||
use crate::llm::{create_provider, LLMProvider, Message};
|
||||
use crate::shell::{execute_tool, get_tool_definitions, ShellExecutor};
|
||||
|
||||
pub struct Repl {
|
||||
llm: Box<dyn LLMProvider>,
|
||||
executor: ShellExecutor,
|
||||
messages: Vec<Message>,
|
||||
}
|
||||
|
||||
impl Repl {
|
||||
pub async fn new(provider: &str, model: Option<&str>) -> Result<Self> {
|
||||
let llm = create_provider(provider, model).await?;
|
||||
let executor = ShellExecutor::default();
|
||||
|
||||
let system_prompt = Message::system(
|
||||
"You are an AI assistant that helps users interact with their system through shell commands. \
|
||||
You have access to tools like bash, read, write, and list to help users accomplish their tasks. \
|
||||
When a user asks you to do something, use the appropriate tools to complete the task. \
|
||||
Always explain what you're doing and show the results to the user."
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
llm,
|
||||
executor,
|
||||
messages: vec![system_prompt],
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<()> {
|
||||
println!("aishell - AI-powered shell automation");
|
||||
println!("Type 'exit' or 'quit' to exit, 'clear' to clear history\n");
|
||||
|
||||
let mut rl = DefaultEditor::new()?;
|
||||
|
||||
loop {
|
||||
let readline = rl.readline("aishell> ");
|
||||
|
||||
match readline {
|
||||
Ok(line) => {
|
||||
let line = line.trim();
|
||||
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if line == "exit" || line == "quit" {
|
||||
println!("Goodbye!");
|
||||
break;
|
||||
}
|
||||
|
||||
if line == "clear" {
|
||||
self.messages.truncate(1); // Keep only system message
|
||||
println!("History cleared.");
|
||||
continue;
|
||||
}
|
||||
|
||||
rl.add_history_entry(line)?;
|
||||
|
||||
if let Err(e) = self.process_input(line).await {
|
||||
eprintln!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
Err(ReadlineError::Interrupted) => {
|
||||
println!("^C");
|
||||
continue;
|
||||
}
|
||||
Err(ReadlineError::Eof) => {
|
||||
println!("^D");
|
||||
break;
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {:?}", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn execute_once(&mut self, prompt: &str) -> Result<()> {
|
||||
self.process_input(prompt).await
|
||||
}
|
||||
|
||||
async fn process_input(&mut self, input: &str) -> Result<()> {
|
||||
// Add user message
|
||||
self.messages.push(Message::user(input));
|
||||
|
||||
let tools = get_tool_definitions();
|
||||
|
||||
// Agent loop: keep calling LLM until it's done (no more tool calls)
|
||||
let max_iterations = 10;
|
||||
for iteration in 0..max_iterations {
|
||||
tracing::debug!("Agent loop iteration {}", iteration + 1);
|
||||
|
||||
let response = self
|
||||
.llm
|
||||
.chat(self.messages.clone(), Some(tools.clone()))
|
||||
.await
|
||||
.context("Failed to get LLM response")?;
|
||||
|
||||
// If there are tool calls, execute them
|
||||
if let Some(tool_calls) = response.tool_calls {
|
||||
tracing::info!("LLM requested {} tool calls", tool_calls.len());
|
||||
|
||||
// Add assistant message with tool calls
|
||||
let mut assistant_msg = Message::assistant(response.content.clone());
|
||||
assistant_msg.tool_calls = Some(tool_calls.clone());
|
||||
self.messages.push(assistant_msg);
|
||||
|
||||
// Execute each tool call
|
||||
for tool_call in tool_calls {
|
||||
let tool_name = &tool_call.function.name;
|
||||
let tool_args = &tool_call.function.arguments;
|
||||
|
||||
println!("\n[Executing tool: {}]", tool_name);
|
||||
|
||||
let result = match execute_tool(tool_name, tool_args, &self.executor) {
|
||||
Ok(output) => output,
|
||||
Err(e) => format!("Error executing tool: {}", e),
|
||||
};
|
||||
|
||||
println!("{}", result);
|
||||
|
||||
// Add tool result message
|
||||
self.messages.push(Message::tool(result, tool_call.id.clone()));
|
||||
}
|
||||
|
||||
// Continue the loop to get the next response
|
||||
continue;
|
||||
}
|
||||
|
||||
// No tool calls, so the LLM is done
|
||||
if !response.content.is_empty() {
|
||||
println!("\n{}\n", response.content);
|
||||
self.messages.push(Message::assistant(response.content));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
53
src/config/mod.rs
Normal file
53
src/config/mod.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub llm: LLMConfig,
|
||||
pub shell: ShellConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LLMConfig {
|
||||
pub default_provider: String,
|
||||
pub openai: OpenAIConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OpenAIConfig {
|
||||
pub model: String,
|
||||
pub base_url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ShellConfig {
|
||||
pub max_execution_time: u64,
|
||||
pub workdir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
llm: LLMConfig {
|
||||
default_provider: "openai".to_string(),
|
||||
openai: OpenAIConfig {
|
||||
model: "gpt-4".to_string(),
|
||||
base_url: None,
|
||||
},
|
||||
},
|
||||
shell: ShellConfig {
|
||||
max_execution_time: 300,
|
||||
workdir: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self> {
|
||||
// For now, just return default config
|
||||
// TODO: Load from file in ~/.config/aishell/config.toml
|
||||
Ok(Self::default())
|
||||
}
|
||||
}
|
||||
7
src/lib.rs
Normal file
7
src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod cli;
|
||||
pub mod config;
|
||||
pub mod llm;
|
||||
pub mod mcp;
|
||||
pub mod shell;
|
||||
|
||||
pub use config::Config;
|
||||
18
src/llm/mod.rs
Normal file
18
src/llm/mod.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
pub mod provider;
|
||||
pub mod openai;
|
||||
|
||||
pub use provider::{LLMProvider, Message, Role, ToolCall, ToolDefinition, ChatResponse};
|
||||
pub use openai::OpenAIProvider;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
/// Create an LLM provider based on the provider name
|
||||
pub async fn create_provider(provider: &str, model: Option<&str>) -> Result<Box<dyn LLMProvider>> {
|
||||
match provider.to_lowercase().as_str() {
|
||||
"openai" => {
|
||||
let provider = OpenAIProvider::new(model)?;
|
||||
Ok(Box::new(provider))
|
||||
}
|
||||
_ => anyhow::bail!("Unsupported provider: {}", provider),
|
||||
}
|
||||
}
|
||||
126
src/llm/openai.rs
Normal file
126
src/llm/openai.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use anyhow::{Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
|
||||
use super::provider::{ChatResponse, LLMProvider, Message, ToolCall, ToolDefinition};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct ChatRequest {
|
||||
model: String,
|
||||
messages: Vec<Message>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
tools: Option<Vec<ToolDefinition>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
tool_choice: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ChatCompletionResponse {
|
||||
choices: Vec<Choice>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Choice {
|
||||
message: ResponseMessage,
|
||||
finish_reason: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ResponseMessage {
|
||||
#[serde(default)]
|
||||
content: Option<String>,
|
||||
#[serde(default)]
|
||||
tool_calls: Option<Vec<ToolCall>>,
|
||||
}
|
||||
|
||||
pub struct OpenAIProvider {
|
||||
client: Client,
|
||||
api_key: String,
|
||||
base_url: String,
|
||||
model: String,
|
||||
}
|
||||
|
||||
impl OpenAIProvider {
|
||||
pub fn new(model: Option<&str>) -> Result<Self> {
|
||||
let api_key = env::var("OPENAI_API_KEY")
|
||||
.context("OPENAI_API_KEY environment variable not set")?;
|
||||
|
||||
let base_url = env::var("OPENAI_BASE_URL")
|
||||
.unwrap_or_else(|_| "https://api.openai.com/v1".to_string());
|
||||
|
||||
let model = model
|
||||
.map(|s| s.to_string())
|
||||
.or_else(|| env::var("OPENAI_MODEL").ok())
|
||||
.unwrap_or_else(|| "gpt-4".to_string());
|
||||
|
||||
Ok(Self {
|
||||
client: Client::new(),
|
||||
api_key,
|
||||
base_url,
|
||||
model,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl LLMProvider for OpenAIProvider {
|
||||
async fn chat(
|
||||
&self,
|
||||
messages: Vec<Message>,
|
||||
tools: Option<Vec<ToolDefinition>>,
|
||||
) -> Result<ChatResponse> {
|
||||
let url = format!("{}/chat/completions", self.base_url);
|
||||
|
||||
let tool_choice = if tools.is_some() {
|
||||
Some("auto".to_string())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let request = ChatRequest {
|
||||
model: self.model.clone(),
|
||||
messages,
|
||||
tools,
|
||||
tool_choice,
|
||||
};
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.post(&url)
|
||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&request)
|
||||
.send()
|
||||
.await
|
||||
.context("Failed to send request to OpenAI API")?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let error_text = response.text().await.unwrap_or_default();
|
||||
anyhow::bail!("OpenAI API error ({}): {}", status, error_text);
|
||||
}
|
||||
|
||||
let completion: ChatCompletionResponse = response
|
||||
.json()
|
||||
.await
|
||||
.context("Failed to parse OpenAI API response")?;
|
||||
|
||||
let choice = completion
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.context("No choices in response")?;
|
||||
|
||||
Ok(ChatResponse {
|
||||
content: choice.message.content.unwrap_or_default(),
|
||||
tool_calls: choice.message.tool_calls,
|
||||
finish_reason: choice.finish_reason,
|
||||
})
|
||||
}
|
||||
|
||||
fn model_name(&self) -> &str {
|
||||
&self.model
|
||||
}
|
||||
}
|
||||
104
src/llm/provider.rs
Normal file
104
src/llm/provider.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Role {
|
||||
System,
|
||||
User,
|
||||
Assistant,
|
||||
Tool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Message {
|
||||
pub role: Role,
|
||||
pub content: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tool_calls: Option<Vec<ToolCall>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tool_call_id: Option<String>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn system(content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::System,
|
||||
content: content.into(),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn user(content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::User,
|
||||
content: content.into(),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assistant(content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
role: Role::Assistant,
|
||||
content: content.into(),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tool(content: impl Into<String>, tool_call_id: String) -> Self {
|
||||
Self {
|
||||
role: Role::Tool,
|
||||
content: content.into(),
|
||||
tool_calls: None,
|
||||
tool_call_id: Some(tool_call_id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolCall {
|
||||
pub id: String,
|
||||
#[serde(rename = "type")]
|
||||
pub call_type: String,
|
||||
pub function: FunctionCall,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FunctionCall {
|
||||
pub name: String,
|
||||
pub arguments: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolDefinition {
|
||||
#[serde(rename = "type")]
|
||||
pub tool_type: String,
|
||||
pub function: FunctionDefinition,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FunctionDefinition {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub parameters: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ChatResponse {
|
||||
pub content: String,
|
||||
pub tool_calls: Option<Vec<ToolCall>>,
|
||||
pub finish_reason: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait LLMProvider: Send + Sync {
|
||||
/// Send a chat completion request
|
||||
async fn chat(&self, messages: Vec<Message>, tools: Option<Vec<ToolDefinition>>) -> Result<ChatResponse>;
|
||||
|
||||
/// Get the model name
|
||||
fn model_name(&self) -> &str;
|
||||
}
|
||||
74
src/main.rs
Normal file
74
src/main.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use tracing_subscriber;
|
||||
|
||||
use aishell::cli::Repl;
|
||||
use aishell::mcp::MCPServer;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "aishell")]
|
||||
#[command(about = "AI-powered shell automation - A generic alternative to Claude Code")]
|
||||
#[command(version)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Start interactive AI shell
|
||||
Shell {
|
||||
/// LLM provider (openai, anthropic, ollama)
|
||||
#[arg(short, long, default_value = "openai")]
|
||||
provider: String,
|
||||
|
||||
/// Model name
|
||||
#[arg(short, long)]
|
||||
model: Option<String>,
|
||||
},
|
||||
|
||||
/// Execute a single command via AI
|
||||
Exec {
|
||||
/// Command prompt
|
||||
prompt: String,
|
||||
|
||||
/// LLM provider
|
||||
#[arg(short = 'p', long, default_value = "openai")]
|
||||
provider: String,
|
||||
},
|
||||
|
||||
/// Start MCP server (for Claude Desktop integration)
|
||||
Server,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize logging
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::from_default_env()
|
||||
.add_directive(tracing::Level::INFO.into()),
|
||||
)
|
||||
.init();
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Commands::Shell { provider, model } => {
|
||||
let mut repl = Repl::new(&provider, model.as_deref()).await?;
|
||||
repl.run().await?;
|
||||
}
|
||||
|
||||
Commands::Exec { prompt, provider } => {
|
||||
let mut repl = Repl::new(&provider, None).await?;
|
||||
repl.execute_once(&prompt).await?;
|
||||
}
|
||||
|
||||
Commands::Server => {
|
||||
let server = MCPServer::new()?;
|
||||
server.run().await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
121
src/mcp/mod.rs
Normal file
121
src/mcp/mod.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use anyhow::Result;
|
||||
use serde_json::json;
|
||||
use tokio::io::{self, AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
|
||||
use crate::shell::{execute_tool, get_tool_definitions, ShellExecutor};
|
||||
|
||||
pub struct MCPServer {
|
||||
executor: ShellExecutor,
|
||||
}
|
||||
|
||||
impl MCPServer {
|
||||
pub fn new() -> Result<Self> {
|
||||
Ok(Self {
|
||||
executor: ShellExecutor::default(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(&self) -> Result<()> {
|
||||
tracing::info!("Starting MCP server");
|
||||
|
||||
let stdin = io::stdin();
|
||||
let mut stdout = io::stdout();
|
||||
let mut reader = BufReader::new(stdin);
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
line.clear();
|
||||
let n = reader.read_line(&mut line).await?;
|
||||
|
||||
if n == 0 {
|
||||
break; // EOF
|
||||
}
|
||||
|
||||
let request: serde_json::Value = match serde_json::from_str(&line) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to parse request: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let response = self.handle_request(&request).await;
|
||||
let response_str = serde_json::to_string(&response)?;
|
||||
|
||||
stdout.write_all(response_str.as_bytes()).await?;
|
||||
stdout.write_all(b"\n").await?;
|
||||
stdout.flush().await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_request(&self, request: &serde_json::Value) -> serde_json::Value {
|
||||
let method = request["method"].as_str().unwrap_or("");
|
||||
|
||||
match method {
|
||||
"initialize" => {
|
||||
json!({
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {
|
||||
"tools": {}
|
||||
},
|
||||
"serverInfo": {
|
||||
"name": "aishell",
|
||||
"version": "0.1.0"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
"tools/list" => {
|
||||
let tools = get_tool_definitions();
|
||||
let tool_list: Vec<_> = tools
|
||||
.iter()
|
||||
.map(|t| {
|
||||
json!({
|
||||
"name": t.function.name,
|
||||
"description": t.function.description,
|
||||
"inputSchema": t.function.parameters
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
json!({
|
||||
"tools": tool_list
|
||||
})
|
||||
}
|
||||
|
||||
"tools/call" => {
|
||||
let tool_name = request["params"]["name"].as_str().unwrap_or("");
|
||||
let arguments = request["params"]["arguments"].to_string();
|
||||
|
||||
let result = match execute_tool(tool_name, &arguments, &self.executor) {
|
||||
Ok(output) => json!({
|
||||
"content": [{
|
||||
"type": "text",
|
||||
"text": output
|
||||
}]
|
||||
}),
|
||||
Err(e) => json!({
|
||||
"content": [{
|
||||
"type": "text",
|
||||
"text": format!("Error: {}", e)
|
||||
}],
|
||||
"isError": true
|
||||
}),
|
||||
};
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
_ => {
|
||||
json!({
|
||||
"error": {
|
||||
"code": -32601,
|
||||
"message": format!("Method not found: {}", method)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
112
src/shell/executor.rs
Normal file
112
src/shell/executor.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
use anyhow::{Context, Result};
|
||||
use duct::cmd;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ExecutionResult {
|
||||
pub stdout: String,
|
||||
pub stderr: String,
|
||||
pub exit_code: i32,
|
||||
pub success: bool,
|
||||
}
|
||||
|
||||
pub struct ShellExecutor {
|
||||
workdir: PathBuf,
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl ShellExecutor {
|
||||
pub fn new(workdir: Option<PathBuf>) -> Result<Self> {
|
||||
let workdir = workdir.unwrap_or_else(|| {
|
||||
std::env::current_dir().expect("Failed to get current directory")
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
workdir,
|
||||
timeout: Duration::from_secs(300), // 5 minutes default
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
||||
self.timeout = timeout;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn execute(&self, command: &str) -> Result<ExecutionResult> {
|
||||
tracing::info!("Executing command: {}", command);
|
||||
|
||||
let output = cmd!("sh", "-c", command)
|
||||
.dir(&self.workdir)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()
|
||||
.context("Failed to execute command")?;
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
||||
let exit_code = output.status.code().unwrap_or(-1);
|
||||
let success = output.status.success();
|
||||
|
||||
tracing::debug!(
|
||||
"Command result: exit_code={}, stdout_len={}, stderr_len={}",
|
||||
exit_code,
|
||||
stdout.len(),
|
||||
stderr.len()
|
||||
);
|
||||
|
||||
Ok(ExecutionResult {
|
||||
stdout,
|
||||
stderr,
|
||||
exit_code,
|
||||
success,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_file(&self, path: &str) -> Result<String> {
|
||||
let full_path = self.workdir.join(path);
|
||||
std::fs::read_to_string(&full_path)
|
||||
.with_context(|| format!("Failed to read file: {}", path))
|
||||
}
|
||||
|
||||
pub fn write_file(&self, path: &str, content: &str) -> Result<()> {
|
||||
let full_path = self.workdir.join(path);
|
||||
|
||||
// Create parent directories if needed
|
||||
if let Some(parent) = full_path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
std::fs::write(&full_path, content)
|
||||
.with_context(|| format!("Failed to write file: {}", path))
|
||||
}
|
||||
|
||||
pub fn list_files(&self, pattern: Option<&str>) -> Result<Vec<String>> {
|
||||
let pattern = pattern.unwrap_or("*");
|
||||
|
||||
let output = cmd!("sh", "-c", format!("ls -1 {}", pattern))
|
||||
.dir(&self.workdir)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let files = String::from_utf8_lossy(&output.stdout)
|
||||
.lines()
|
||||
.map(|s| s.to_string())
|
||||
.collect();
|
||||
|
||||
Ok(files)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ShellExecutor {
|
||||
fn default() -> Self {
|
||||
Self::new(None).expect("Failed to create default ShellExecutor")
|
||||
}
|
||||
}
|
||||
5
src/shell/mod.rs
Normal file
5
src/shell/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod executor;
|
||||
pub mod tools;
|
||||
|
||||
pub use executor::{ShellExecutor, ExecutionResult};
|
||||
pub use tools::{get_tool_definitions, execute_tool, ToolArguments};
|
||||
162
src/shell/tools.rs
Normal file
162
src/shell/tools.rs
Normal file
@@ -0,0 +1,162 @@
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::llm::ToolDefinition;
|
||||
use super::executor::ShellExecutor;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(tag = "tool", rename_all = "snake_case")]
|
||||
pub enum ToolArguments {
|
||||
Bash { command: String },
|
||||
Read { path: String },
|
||||
Write { path: String, content: String },
|
||||
List { pattern: Option<String> },
|
||||
}
|
||||
|
||||
/// Get all available tool definitions for the LLM
|
||||
pub fn get_tool_definitions() -> Vec<ToolDefinition> {
|
||||
vec![
|
||||
ToolDefinition {
|
||||
tool_type: "function".to_string(),
|
||||
function: crate::llm::provider::FunctionDefinition {
|
||||
name: "bash".to_string(),
|
||||
description: "Execute a bash command and return the output. Use this for running shell commands, git operations, package management, etc.".to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"command": {
|
||||
"type": "string",
|
||||
"description": "The bash command to execute"
|
||||
}
|
||||
},
|
||||
"required": ["command"]
|
||||
}),
|
||||
},
|
||||
},
|
||||
ToolDefinition {
|
||||
tool_type: "function".to_string(),
|
||||
function: crate::llm::provider::FunctionDefinition {
|
||||
name: "read".to_string(),
|
||||
description: "Read the contents of a file. Returns the file content as a string.".to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "The path to the file to read"
|
||||
}
|
||||
},
|
||||
"required": ["path"]
|
||||
}),
|
||||
},
|
||||
},
|
||||
ToolDefinition {
|
||||
tool_type: "function".to_string(),
|
||||
function: crate::llm::provider::FunctionDefinition {
|
||||
name: "write".to_string(),
|
||||
description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does.".to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "The path to the file to write"
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "The content to write to the file"
|
||||
}
|
||||
},
|
||||
"required": ["path", "content"]
|
||||
}),
|
||||
},
|
||||
},
|
||||
ToolDefinition {
|
||||
tool_type: "function".to_string(),
|
||||
function: crate::llm::provider::FunctionDefinition {
|
||||
name: "list".to_string(),
|
||||
description: "List files in the current directory. Optionally filter by pattern.".to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"pattern": {
|
||||
"type": "string",
|
||||
"description": "Optional glob pattern to filter files (e.g., '*.rs')"
|
||||
}
|
||||
},
|
||||
"required": []
|
||||
}),
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Execute a tool call
|
||||
pub fn execute_tool(
|
||||
tool_name: &str,
|
||||
arguments: &str,
|
||||
executor: &ShellExecutor,
|
||||
) -> Result<String> {
|
||||
tracing::info!("Executing tool: {} with args: {}", tool_name, arguments);
|
||||
|
||||
match tool_name {
|
||||
"bash" => {
|
||||
let args: serde_json::Value = serde_json::from_str(arguments)?;
|
||||
let command = args["command"]
|
||||
.as_str()
|
||||
.context("Missing 'command' argument")?;
|
||||
|
||||
let result = executor.execute(command)?;
|
||||
|
||||
let output = if result.success {
|
||||
format!("Exit code: {}\n\nStdout:\n{}\n\nStderr:\n{}",
|
||||
result.exit_code,
|
||||
result.stdout,
|
||||
result.stderr
|
||||
)
|
||||
} else {
|
||||
format!("Command failed with exit code: {}\n\nStdout:\n{}\n\nStderr:\n{}",
|
||||
result.exit_code,
|
||||
result.stdout,
|
||||
result.stderr
|
||||
)
|
||||
};
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
"read" => {
|
||||
let args: serde_json::Value = serde_json::from_str(arguments)?;
|
||||
let path = args["path"]
|
||||
.as_str()
|
||||
.context("Missing 'path' argument")?;
|
||||
|
||||
let content = executor.read_file(path)?;
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
"write" => {
|
||||
let args: serde_json::Value = serde_json::from_str(arguments)?;
|
||||
let path = args["path"]
|
||||
.as_str()
|
||||
.context("Missing 'path' argument")?;
|
||||
let content = args["content"]
|
||||
.as_str()
|
||||
.context("Missing 'content' argument")?;
|
||||
|
||||
executor.write_file(path, content)?;
|
||||
Ok(format!("Successfully wrote to file: {}", path))
|
||||
}
|
||||
|
||||
"list" => {
|
||||
let args: serde_json::Value = serde_json::from_str(arguments)?;
|
||||
let pattern = args["pattern"].as_str();
|
||||
|
||||
let files = executor.list_files(pattern)?;
|
||||
Ok(files.join("\n"))
|
||||
}
|
||||
|
||||
_ => anyhow::bail!("Unknown tool: {}", tool_name),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user