feat(phases4,7,8): implement Agent/ReAct, Code Execution, and Prompt Server

Completes Phase 4 (Agentic Loop with ReAct), Phase 7 (Code Execution),
and Phase 8 (Prompt Server) as specified in the implementation plan.

**Phase 4: Agentic Loop with ReAct Pattern (agent.rs - 398 lines)**
- Complete AgentExecutor with reasoning loop
- LlmResponse enum: ToolCall, FinalAnswer, Reasoning
- ReAct parser supporting THOUGHT/ACTION/ACTION_INPUT/FINAL_ANSWER
- Tool discovery and execution integration
- AgentResult with iteration tracking and message history
- Integration with owlen-agent CLI binary and TUI

**Phase 7: Code Execution with Docker Sandboxing**

*Sandbox Module (sandbox.rs - 255 lines):*
- Docker-based execution using bollard
- Resource limits: 512MB memory, 50% CPU
- Network isolation (no network access)
- Timeout handling (30s default)
- Container auto-cleanup
- Support for Rust, Node.js, Python environments

*Tool Suite (tools.rs - 410 lines):*
- CompileProjectTool: Build projects with auto-detection
- RunTestsTool: Execute test suites with optional filters
- FormatCodeTool: Run formatters (rustfmt/prettier/black)
- LintCodeTool: Run linters (clippy/eslint/pylint)
- All tools support check-only and auto-fix modes

*MCP Server (lib.rs - 183 lines):*
- Full JSON-RPC protocol implementation
- Tool registry with dynamic dispatch
- Initialize/tools/list/tools/call support

**Phase 8: Prompt Server with YAML & Handlebars**

*Prompt Server (lib.rs - 405 lines):*
- YAML-based template storage in ~/.config/owlen/prompts/
- Handlebars 6.0 template engine integration
- PromptTemplate with metadata (name, version, mode, description)
- Four MCP tools:
  - get_prompt: Retrieve template by name
  - render_prompt: Render with Handlebars variables
  - list_prompts: List all available templates
  - reload_prompts: Hot-reload from disk

*Default Templates:*
- chat_mode_system.yaml: ReAct prompt for chat mode
- code_mode_system.yaml: ReAct prompt with code tools

**Configuration & Integration:**
- Added Agent module to owlen-core
- Updated owlen-agent binary to use new AgentExecutor API
- Updated TUI to integrate with agent result structure
- Added error handling for Agent variant

**Dependencies Added:**
- bollard 0.17 (Docker API)
- handlebars 6.0 (templating)
- serde_yaml 0.9 (YAML parsing)
- tempfile 3.0 (temporary directories)
- uuid 1.0 with v4 feature

**Tests:**
- mode_tool_filter.rs: Tool filtering by mode
- prompt_server.rs: Prompt management tests
- Sandbox tests (Docker-dependent, marked #[ignore])

All code compiles successfully and follows project conventions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-10 20:50:40 +02:00
parent cdf95002fc
commit e94df2c48a
17 changed files with 1885 additions and 388 deletions

View File

@@ -1,5 +1,5 @@
use super::{McpToolCall, McpToolDescriptor, McpToolResponse};
use crate::{Error, Result};
use crate::Result;
use async_trait::async_trait;
/// Trait for a client that can interact with an MCP server
@@ -12,40 +12,5 @@ pub trait McpClient: Send + Sync {
async fn call_tool(&self, call: McpToolCall) -> Result<McpToolResponse>;
}
/// Placeholder for a client that connects to a remote MCP server.
pub struct RemoteMcpClient;
impl RemoteMcpClient {
pub fn new() -> Result<Self> {
// Attempt to spawn the MCP server binary located at ./target/debug/owlen-mcp-server
// The server runs over STDIO and will be managed by the client instance.
// For now we just verify that the binary exists; the actual process handling
// is performed lazily in the async methods.
let path = "./target/debug/owlen-mcp-server";
if std::path::Path::new(path).exists() {
Ok(Self)
} else {
Err(Error::NotImplemented(format!(
"Remote MCP server binary not found at {}",
path
)))
}
}
}
#[async_trait]
impl McpClient for RemoteMcpClient {
async fn list_tools(&self) -> Result<Vec<McpToolDescriptor>> {
// TODO: Implement remote call
Err(Error::NotImplemented(
"Remote MCP client is not implemented".to_string(),
))
}
async fn call_tool(&self, _call: McpToolCall) -> Result<McpToolResponse> {
// TODO: Implement remote call
Err(Error::NotImplemented(
"Remote MCP client is not implemented".to_string(),
))
}
}
// Re-export the concrete implementation that supports stdio and HTTP transports.
pub use super::remote_client::RemoteMcpClient;

View File

@@ -41,16 +41,25 @@ impl McpClientFactory {
)))
}
McpMode::Enabled => {
// Attempt to use remote client, fall back to local if unavailable
match RemoteMcpClient::new() {
Ok(client) => Ok(Box::new(client)),
Err(e) => {
eprintln!("Warning: Failed to start remote MCP client: {}. Falling back to local mode.", e);
Ok(Box::new(LocalMcpClient::new(
self.registry.clone(),
self.validator.clone(),
)))
// Use the first configured MCP server, if any.
if let Some(server_cfg) = self.config.mcp_servers.first() {
match RemoteMcpClient::new_with_config(server_cfg) {
Ok(client) => Ok(Box::new(client)),
Err(e) => {
eprintln!("Warning: Failed to start remote MCP client '{}': {}. Falling back to local mode.", server_cfg.name, e);
Ok(Box::new(LocalMcpClient::new(
self.registry.clone(),
self.validator.clone(),
)))
}
}
} else {
// No servers configured fall back to local client.
eprintln!("Warning: No MCP servers defined in config. Using local client.");
Ok(Box::new(LocalMcpClient::new(
self.registry.clone(),
self.validator.clone(),
)))
}
}
}