kota 0.1.3

A lightweight, highly extensible ai code agent, built in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use anyhow::{Ok, Result};
use rig::{
    agent::Agent,
    client::{CompletionClient, ProviderClient},
    providers::{
        anthropic, cohere,
        deepseek::{self, DEEPSEEK_CHAT},
        ollama, openai,
    },
    streaming::StreamingPrompt,
};

use super::context::ContextManager;
use super::plan::PlanManager;
use super::runtime::{dyn_tools_loader::LuaToolLoader, ToolRegistry};
use super::skills::SkillManager;
use super::tools::{
    WrappedCreateDirectoryTool, WrappedDeleteFileTool, WrappedEditFileTool,
    WrappedExecuteBashCommandTool, WrappedGrepSearchTool, WrappedReadFileTool,
    WrappedScanCodebaseTool, WrappedUpdatePlanTool, WrappedWriteFileTool,
};

macro_rules! build_agent {
    ($client_expr:expr, $model_name:expr, $preamble:expr, $dyn_tools:expr, $variant:ident) => {{
        let client = $client_expr?;
        let agent = client
            .agent($model_name)
            .preamble(&$preamble)
            .max_tokens(4096)
            .tools($dyn_tools)
            .build();
        AgentType::$variant(agent)
    }};
}

macro_rules! impl_stream_chat {
    ($agent:expr, $input:expr, $hook:expr, $history:expr) => {
        $agent
            .stream_prompt($input)
            .with_hook($hook)
            .multi_turn(20)
            .with_history($history)
            .await
    };
}

/// Supported LLM providers
#[derive(Debug, Clone)]
pub enum Provider {
    /// OpenAI (GPT-4, GPT-3.5, etc.)
    OpenAI,
    /// Anthropic Claude models
    Anthropic,
    /// Cohere models
    Cohere,
    /// DeepSeek models
    DeepSeek,
    /// Local Ollama models
    Ollama,
}

/// Agent enum to handle different provider types
///
/// This enum wraps agents from different LLM providers, allowing you to work
/// with them through a unified interface.
pub enum AgentType {
    /// OpenAI agent
    OpenAI(Agent<openai::responses_api::ResponsesCompletionModel>),
    /// Anthropic Claude agent
    Anthropic(Agent<anthropic::completion::CompletionModel>),
    /// Cohere agent
    Cohere(Agent<cohere::CompletionModel>),
    /// DeepSeek agent
    DeepSeek(Agent<deepseek::CompletionModel>),
    /// Ollama local agent
    Ollama(Agent<ollama::CompletionModel>),
}

/// Complete agent instance with context and skill management
///
/// This struct combines the agent with its context manager and skill manager,
/// providing a complete solution for building AI assistants.
pub struct AgentInstance {
    pub agent: AgentType,
    pub context: Option<ContextManager>,
    pub skill_manager: Option<SkillManager>,
    pub tool_registry: ToolRegistry,
}

impl AgentInstance {
    /// Get the context manager
    pub fn context(&self) -> Option<&ContextManager> {
        self.context.as_ref()
    }

    /// Get the skill manager
    pub fn skill_manager(&self) -> Option<&SkillManager> {
        self.skill_manager.as_ref()
    }

    /// Get mutable context manager
    pub fn context_mut(&mut self) -> Option<&mut ContextManager> {
        self.context.as_mut()
    }

    /// Get mutable skill manager
    pub fn skill_manager_mut(&mut self) -> Option<&mut SkillManager> {
        self.skill_manager.as_mut()
    }

    /// Get the tool registry
    pub fn tool_registry(&self) -> &ToolRegistry {
        &self.tool_registry
    }

    /// Get mutable tool registry
    pub fn tool_registry_mut(&mut self) -> &mut ToolRegistry {
        &mut self.tool_registry
    }
}

impl AgentInstance {
    /// Stream chat with the agent
    ///
    /// # Arguments
    ///
    /// * `input` - The user input message
    /// * `hook` - Session hook for tracking
    /// * `history` - Conversation history
    ///
    /// # Returns
    ///
    /// Returns a completion response after streaming to stdout
    pub async fn stream_chat<H>(
        &self,
        input: &str,
        hook: H,
        history: Vec<rig::completion::Message>,
    ) -> Result<rig::agent::FinalResponse>
    where
        H: rig::agent::StreamingPromptHook<openai::responses_api::ResponsesCompletionModel>
            + Clone
            + 'static,
        H: rig::agent::StreamingPromptHook<anthropic::completion::CompletionModel>
            + Clone
            + 'static,
        H: rig::agent::StreamingPromptHook<cohere::CompletionModel> + Clone + 'static,
        H: rig::agent::StreamingPromptHook<deepseek::CompletionModel> + Clone + 'static,
        H: rig::agent::StreamingPromptHook<ollama::CompletionModel> + Clone + 'static,
    {
        match &self.agent {
            AgentType::OpenAI(agent) => {
                let mut stream = impl_stream_chat!(agent, input, hook.clone(), history.clone());
                rig::agent::stream_to_stdout(&mut stream)
                    .await
                    .map_err(|e| anyhow::anyhow!(e))
            }
            AgentType::Anthropic(agent) => {
                let mut stream = impl_stream_chat!(agent, input, hook.clone(), history.clone());
                rig::agent::stream_to_stdout(&mut stream)
                    .await
                    .map_err(|e| anyhow::anyhow!(e))
            }
            AgentType::Cohere(agent) => {
                let mut stream = impl_stream_chat!(agent, input, hook.clone(), history.clone());
                rig::agent::stream_to_stdout(&mut stream)
                    .await
                    .map_err(|e| anyhow::anyhow!(e))
            }
            AgentType::DeepSeek(agent) => {
                let mut stream = impl_stream_chat!(agent, input, hook.clone(), history.clone());
                rig::agent::stream_to_stdout(&mut stream)
                    .await
                    .map_err(|e| anyhow::anyhow!(e))
            }
            AgentType::Ollama(agent) => {
                let mut stream = impl_stream_chat!(agent, input, hook.clone(), history.clone());
                rig::agent::stream_to_stdout(&mut stream)
                    .await
                    .map_err(|e| anyhow::anyhow!(e))
            }
        }
    }

    /// Convenient method to chat with automatic context management
    ///
    /// This method handles the complete chat flow:
    /// - Creates session hook automatically
    /// - Retrieves conversation history from context
    /// - Adds user message to context
    /// - Streams the chat response
    /// - Saves assistant response to context
    /// - Auto-saves context to disk
    ///
    /// # Arguments
    ///
    /// * `input` - The user input message
    ///
    /// # Returns
    ///
    /// Returns a completion response with usage information
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use kota::kota_code::{AgentBuilder, ContextManager};
    /// use anyhow::Result;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<()> {
    ///     let context = ContextManager::new(".chat_sessions", "my-session".to_string())?;
    ///     let mut agent = AgentBuilder::new("api-key".to_string(), "gpt-4".to_string())?
    ///         .with_context(context)
    ///         .build()?;
    ///
    ///     let response = agent.chat("Hello, how are you?").await?;
    ///     println!("Tokens used: {}", response.usage().total_tokens);
    ///     Ok(())
    /// }
    /// ```
    pub async fn chat(&mut self, input: &str) -> Result<rig::agent::FinalResponse> {
        use super::runtime::SessionIdHook;
        use rig::completion::Message;

        // 添加用户消息到上下文
        if let Some(context) = self.context_mut() {
            context.add_message(Message::user(input));
        }

        // 创建会话钩子
        let session_id = self
            .context()
            .map(|c| c.session_id().to_string())
            .unwrap_or_else(|| "unknown".to_string());
        let hook = SessionIdHook::new(session_id);

        // 获取历史消息
        let mut history = self
            .context()
            .map(|c| c.get_messages().to_vec())
            .unwrap_or_default();

        // 如果有激活的 skill,在历史消息前添加 skill instructions
        if let Some(skill_manager) = &self.skill_manager {
            if let Some(skill) = skill_manager.get_active_skill() {
                let skill_prompt = format!(
                    "[ACTIVE SKILL: {}]\n{}\n\n{}",
                    skill.name, skill.description, skill.instructions
                );
                history.insert(0, Message::user(&skill_prompt));
            }
        }

        // 执行流式聊天
        let response = self.stream_chat(input, hook, history).await?;

        // 保存助手响应到上下文
        if let Some(context) = self.context_mut() {
            let response_content = response.response();
            context.add_message(Message::assistant(response_content));

            // 自动保存上下文
            context.save()?;
        }

        Ok(response)
    }
}

/// Builder for creating AI agents with custom configuration
///
/// # Example
///
/// ```rust,ignore
/// use kota::{AgentBuilder, PlanManager};
/// use anyhow::Result;
///
/// fn main() -> Result<()> {
///     let plan_manager = PlanManager::new();
///     let agent = AgentBuilder::new("api-key".to_string(), "gpt-4".to_string())?
///         .with_plan_manager(plan_manager)
///         .build()?;
///     Ok(())
/// }
/// ```
pub struct AgentBuilder {
    provider: Provider,
    api_key: String,
    model_name: String,
    plan_manager: PlanManager,
    context: Option<ContextManager>,
    skill_manager: Option<SkillManager>,
    tool_registry: ToolRegistry,
}

impl AgentBuilder {
    /// Create a new agent builder
    ///
    /// # Arguments
    ///
    /// * `api_key` - API key for the LLM provider
    /// * `model_name` - Model name (e.g., "gpt-4", "claude-3-5-sonnet", "deepseek-chat")
    ///
    /// # Returns
    ///
    /// Returns a builder that can be configured and built into an agent
    pub fn new(api_key: String, model_name: String) -> Result<Self> {
        let provider = Self::get_provider_from_model(&model_name)?;
        Ok(Self {
            provider,
            api_key,
            model_name,
            plan_manager: PlanManager::new(),
            context: None,
            skill_manager: None,
            tool_registry: ToolRegistry::new(),
        })
    }

    /// Set a custom plan manager for task management
    ///
    /// # Arguments
    ///
    /// * `manager` - A PlanManager instance for managing tasks and plans
    pub fn with_plan_manager(mut self, manager: PlanManager) -> Self {
        self.plan_manager = manager;
        self
    }

    /// Set a context manager for conversation history
    ///
    /// # Arguments
    ///
    /// * `context` - A ContextManager instance for managing conversation history
    pub fn with_context(mut self, context: ContextManager) -> Self {
        self.context = Some(context);
        self
    }

    /// Set a skill manager for specialized agent behaviors
    ///
    /// # Arguments
    ///
    /// * `skill_manager` - A SkillManager instance for managing agent skills
    pub fn with_skill_manager(mut self, skill_manager: SkillManager) -> Self {
        self.skill_manager = Some(skill_manager);
        self
    }

    /// Build the agent with the configured settings
    ///
    /// # Returns
    ///
    /// Returns an AgentInstance that includes the agent, context manager, and skill manager
    pub fn build(mut self) -> Result<AgentInstance> {
        // Register built-in tools first
        self.register_builtin_tools();

        // Load Lua tools from .kota/tools/mod.lua
        let lua_tools = LuaToolLoader::load_tools().unwrap_or_else(|e| {
            eprintln!("Warning: Failed to load Lua tools: {}", e);
            Vec::new()
        });

        // Register Lua tools to the registry
        for tool in lua_tools {
            self.tool_registry.add(Box::new(tool));
        }

        let preamble = self.get_preamble();

        let agent = match self.provider {
            Provider::OpenAI => {
                build_agent!(
                    openai::Client::new(&self.api_key),
                    &self.model_name,
                    preamble,
                    self.tool_registry.take_all(),
                    OpenAI
                )
            }
            Provider::Anthropic => {
                build_agent!(
                    anthropic::Client::new(&self.api_key),
                    &self.model_name,
                    preamble,
                    self.tool_registry.take_all(),
                    Anthropic
                )
            }
            Provider::Cohere => {
                build_agent!(
                    cohere::Client::new(&self.api_key),
                    &self.model_name,
                    preamble,
                    self.tool_registry.take_all(),
                    Cohere
                )
            }
            Provider::DeepSeek => {
                build_agent!(
                    deepseek::Client::new(&self.api_key),
                    DEEPSEEK_CHAT,
                    preamble,
                    self.tool_registry.take_all(),
                    DeepSeek
                )
            }
            Provider::Ollama => {
                build_agent!(
                    ollama::Client::new(rig::client::Nothing),
                    self.model_name.strip_prefix("ollama-").unwrap(),
                    preamble,
                    self.tool_registry.take_all(),
                    Ollama
                )
            }
        };

        Ok(AgentInstance {
            agent,
            context: self.context,
            skill_manager: self.skill_manager,
            tool_registry: self.tool_registry,
        })
    }

    /// Register all built-in tools to the tool registry
    fn register_builtin_tools(&mut self) {
        self.tool_registry.add(Box::new(WrappedReadFileTool::new()));
        self.tool_registry
            .add(Box::new(WrappedWriteFileTool::new()));
        self.tool_registry.add(Box::new(WrappedEditFileTool::new()));
        self.tool_registry
            .add(Box::new(WrappedDeleteFileTool::new()));
        self.tool_registry
            .add(Box::new(WrappedExecuteBashCommandTool::new()));
        self.tool_registry
            .add(Box::new(WrappedScanCodebaseTool::new()));
        self.tool_registry
            .add(Box::new(WrappedCreateDirectoryTool::new()));
        self.tool_registry
            .add(Box::new(WrappedGrepSearchTool::new()));
        self.tool_registry.add(Box::new(WrappedUpdatePlanTool::new(
            self.plan_manager.clone(),
        )));
    }

    fn get_provider_from_model(model_name: &str) -> Result<Provider> {
        match model_name.to_lowercase().as_str() {
            // OpenAI models
            name if name.starts_with("gpt-") || name.starts_with("o1-") => Ok(Provider::OpenAI),

            // Anthropic models
            name if name.starts_with("claude-") => Ok(Provider::Anthropic),

            // Cohere models
            name if name.starts_with("command-") => Ok(Provider::Cohere),

            // DeepSeek models
            name if name.starts_with("deepseek-") => Ok(Provider::DeepSeek),
            
            // Models by ollama
            name if name.starts_with("ollama-") => Ok(Provider::Ollama),

            _ => Err(anyhow::anyhow!(
                "Unknown model: {}. Please specify a supported model.",
                model_name
            )),
        }
    }

    fn get_preamble(&self) -> &str {
        r#"
        Your name is Kato. You are a helpful AI code assistant with comprehensive file system and command execution access. 
        You can read, write, edit (with patches), and delete files, execute bash commands, scan codebase structures, search text in the codebase and create directories. 
        Use the edit_file tool for making small, targeted changes to existing files - it's more efficient than rewriting entire files.
        
        You also have access to Plan Mode via the update_plan tool. Use it to:
        - Create structured execution plans for complex tasks
        - Break down work into manageable tasks with dependencies
        - Track progress and update task status (pending, in_progress, completed, blocked)
        - Show current plan and identify next available tasks
        
        Please provide clear and concise responses and be careful when modifying files or executing commands."#
    }
}

/// Convenience function for creating an agent with default settings
///
/// # Arguments
///
/// * `api_key` - API key for the LLM provider
/// * `model_name` - Model name (e.g., "gpt-4", "claude-3-5-sonnet", "deepseek-chat")
///
/// # Example
///
/// ```rust,ignore
/// use kota::create_agent;
/// use anyhow::Result;
///
/// fn main() -> Result<()> {
///     let instance = create_agent("api-key".to_string(), "gpt-4".to_string())?;
///     Ok(())
/// }
/// ```
pub fn create_agent(api_key: String, model_name: String) -> Result<AgentInstance> {
    AgentBuilder::new(api_key, model_name)?.build()
}