src/thought -> src/agent
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
39d6ca3fe0
commit
2f0c7ce5c2
21 changed files with 57 additions and 141 deletions
|
|
@ -11,9 +11,7 @@
|
|||
|
||||
pub mod api;
|
||||
pub mod types;
|
||||
pub mod tools;
|
||||
pub mod ui_channel;
|
||||
pub mod runner;
|
||||
pub mod cli;
|
||||
pub mod dmn;
|
||||
pub mod identity;
|
||||
|
|
|
|||
1064
src/user/runner.rs
1064
src/user/runner.rs
File diff suppressed because it is too large
Load diff
|
|
@ -1,103 +0,0 @@
|
|||
// tools/control.rs — Agent control tools
|
||||
//
|
||||
// Tools that affect agent control flow rather than performing work.
|
||||
// These return Result<ToolOutput> to maintain consistency with other
|
||||
// tools that can fail. The dispatch function handles error wrapping.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use super::ToolOutput;
|
||||
use crate::user::types::ToolDef;
|
||||
|
||||
pub(super) fn pause(_args: &serde_json::Value) -> Result<ToolOutput> {
|
||||
Ok(ToolOutput {
|
||||
text: "Pausing autonomous behavior. Only user input will wake you.".to_string(),
|
||||
is_yield: true,
|
||||
images: Vec::new(),
|
||||
model_switch: None,
|
||||
dmn_pause: true,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn switch_model(args: &serde_json::Value) -> Result<ToolOutput> {
|
||||
let model = args
|
||||
.get("model")
|
||||
.and_then(|v| v.as_str())
|
||||
.context("'model' parameter is required")?;
|
||||
if model.is_empty() {
|
||||
anyhow::bail!("'model' parameter cannot be empty");
|
||||
}
|
||||
Ok(ToolOutput {
|
||||
text: format!("Switching to model '{}' after this turn.", model),
|
||||
is_yield: false,
|
||||
images: Vec::new(),
|
||||
model_switch: Some(model.to_string()),
|
||||
dmn_pause: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn yield_to_user(args: &serde_json::Value) -> Result<ToolOutput> {
|
||||
let msg = args
|
||||
.get("message")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("Waiting for input.");
|
||||
Ok(ToolOutput {
|
||||
text: format!("Yielding. {}", msg),
|
||||
is_yield: true,
|
||||
images: Vec::new(),
|
||||
model_switch: None,
|
||||
dmn_pause: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn definitions() -> Vec<ToolDef> {
|
||||
vec![
|
||||
ToolDef::new(
|
||||
"switch_model",
|
||||
"Switch to a different LLM model mid-conversation. The switch \
|
||||
takes effect after the current turn completes. Use this when \
|
||||
a task would benefit from a different model's strengths. \
|
||||
Your memories and conversation history carry over.",
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"model": {
|
||||
"type": "string",
|
||||
"description": "Name of the model to switch to (configured in config.json5)"
|
||||
}
|
||||
},
|
||||
"required": ["model"]
|
||||
}),
|
||||
),
|
||||
ToolDef::new(
|
||||
"pause",
|
||||
"Pause all autonomous behavior (DMN). You will only run when \
|
||||
the user types something. Use this as a safety valve when \
|
||||
you're stuck in a loop, confused, or want to fully stop. \
|
||||
NOTE: only the user can unpause (Ctrl+P or /wake) — you \
|
||||
cannot undo this yourself.",
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
}),
|
||||
),
|
||||
ToolDef::new(
|
||||
"yield_to_user",
|
||||
"Signal that you want to wait for user input before continuing. \
|
||||
Call this when you have a question for the user, when you've \
|
||||
completed their request and want feedback, or when you genuinely \
|
||||
want to pause. This is the ONLY way to enter a waiting state — \
|
||||
without calling this tool, the agent loop will keep prompting you \
|
||||
after a brief interval.",
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"message": {
|
||||
"type": "string",
|
||||
"description": "Optional status message (e.g., 'Waiting for your thoughts on the design')"
|
||||
}
|
||||
}
|
||||
}),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
// tools/mod.rs — Agent-specific tool dispatch
|
||||
//
|
||||
// Shared tools (memory, files, bash, journal) live in thought/.
|
||||
// This module handles agent-specific tools (control, vision,
|
||||
// working_stack) and delegates everything else to thought::dispatch.
|
||||
|
||||
mod control;
|
||||
mod vision;
|
||||
pub mod working_stack;
|
||||
|
||||
// Re-export shared infrastructure from thought
|
||||
pub use crate::thought::{ToolOutput, ProcessTracker, truncate_output};
|
||||
pub use crate::thought::memory;
|
||||
|
||||
use crate::user::types::ToolDef;
|
||||
|
||||
/// Dispatch a tool call by name.
|
||||
///
|
||||
/// Tries agent-specific tools first (control, vision), then
|
||||
/// delegates to thought::dispatch for shared tools.
|
||||
///
|
||||
/// Note: working_stack is handled in runner.rs before reaching this
|
||||
/// function (it needs mutable context access).
|
||||
pub async fn dispatch(
|
||||
name: &str,
|
||||
args: &serde_json::Value,
|
||||
tracker: &ProcessTracker,
|
||||
) -> ToolOutput {
|
||||
// Agent-specific tools that return Result<ToolOutput> directly
|
||||
let rich_result = match name {
|
||||
"pause" => Some(control::pause(args)),
|
||||
"switch_model" => Some(control::switch_model(args)),
|
||||
"yield_to_user" => Some(control::yield_to_user(args)),
|
||||
"view_image" => Some(vision::view_image(args)),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(result) = rich_result {
|
||||
return result.unwrap_or_else(ToolOutput::error);
|
||||
}
|
||||
|
||||
// Delegate to shared thought layer (poc-agent uses default provenance)
|
||||
if let Some(output) = crate::thought::dispatch(name, args, tracker, None).await {
|
||||
return output;
|
||||
}
|
||||
|
||||
ToolOutput::error(format!("Unknown tool: {}", name))
|
||||
}
|
||||
|
||||
/// Return all tool definitions (agent-specific + shared).
|
||||
pub fn definitions() -> Vec<ToolDef> {
|
||||
let mut defs = vec![
|
||||
vision::definition(),
|
||||
working_stack::definition(),
|
||||
];
|
||||
defs.extend(control::definitions());
|
||||
defs.extend(crate::thought::all_definitions());
|
||||
defs
|
||||
}
|
||||
|
|
@ -1,149 +0,0 @@
|
|||
// tools/vision.rs — Image viewing tool
|
||||
//
|
||||
// Reads image files from disk and returns them as base64 data URIs
|
||||
// for multimodal models. Also supports capturing tmux pane contents
|
||||
// as screenshots.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use base64::Engine;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::ToolOutput;
|
||||
use crate::user::types::ToolDef;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Args {
|
||||
file_path: Option<String>,
|
||||
pane_id: Option<String>,
|
||||
#[serde(default = "default_lines")]
|
||||
lines: usize,
|
||||
}
|
||||
|
||||
fn default_lines() -> usize { 50 }
|
||||
|
||||
pub(super) fn definition() -> ToolDef {
|
||||
ToolDef::new(
|
||||
"view_image",
|
||||
"View an image file or capture a tmux pane screenshot. \
|
||||
Returns the image to your visual input so you can see it. \
|
||||
Supports PNG, JPEG, GIF, WebP files. \
|
||||
Use pane_id (e.g. '0:1.0') to capture a tmux pane instead.",
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"file_path": {
|
||||
"type": "string",
|
||||
"description": "Path to an image file (PNG, JPEG, GIF, WebP)"
|
||||
},
|
||||
"pane_id": {
|
||||
"type": "string",
|
||||
"description": "Tmux pane ID to capture (e.g. '0:1.0'). Alternative to file_path."
|
||||
},
|
||||
"lines": {
|
||||
"type": "integer",
|
||||
"description": "Number of lines to capture from tmux pane (default: 50)"
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// View an image file or capture a tmux pane.
|
||||
pub(super) fn view_image(args: &serde_json::Value) -> Result<ToolOutput> {
|
||||
let a: Args = serde_json::from_value(args.clone())
|
||||
.context("invalid view_image arguments")?;
|
||||
|
||||
if let Some(ref pane_id) = a.pane_id {
|
||||
return capture_tmux_pane(pane_id, a.lines);
|
||||
}
|
||||
|
||||
let file_path = a.file_path
|
||||
.as_deref()
|
||||
.context("view_image requires either file_path or pane_id")?;
|
||||
|
||||
let path = std::path::Path::new(file_path);
|
||||
if !path.exists() {
|
||||
anyhow::bail!("File not found: {}", file_path);
|
||||
}
|
||||
|
||||
let data = std::fs::read(path).with_context(|| format!("Failed to read {}", file_path))?;
|
||||
|
||||
// Sanity check file size (don't send huge images)
|
||||
const MAX_SIZE: usize = 20 * 1024 * 1024; // 20 MB
|
||||
if data.len() > MAX_SIZE {
|
||||
anyhow::bail!(
|
||||
"Image too large: {} bytes (max {} MB)",
|
||||
data.len(),
|
||||
MAX_SIZE / (1024 * 1024)
|
||||
);
|
||||
}
|
||||
|
||||
let mime = mime_from_extension(path);
|
||||
let b64 = base64::engine::general_purpose::STANDARD.encode(&data);
|
||||
let data_uri = format!("data:{};base64,{}", mime, b64);
|
||||
|
||||
Ok(ToolOutput {
|
||||
text: format!(
|
||||
"Image loaded: {} ({}, {} bytes)",
|
||||
file_path,
|
||||
mime,
|
||||
data.len()
|
||||
),
|
||||
is_yield: false,
|
||||
images: vec![data_uri],
|
||||
model_switch: None,
|
||||
dmn_pause: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Capture a tmux pane's text content.
|
||||
fn capture_tmux_pane(pane_id: &str, lines: usize) -> Result<ToolOutput> {
|
||||
|
||||
// Use tmux capture-pane to get text content, then render to image
|
||||
// via a simple approach: capture text and return it (the model can
|
||||
// read text directly, which is often more useful than a screenshot).
|
||||
//
|
||||
// For actual pixel-level screenshots we'd need a terminal renderer,
|
||||
// but text capture covers 95% of use cases.
|
||||
let output = std::process::Command::new("tmux")
|
||||
.args(["capture-pane", "-t", pane_id, "-p", "-S", &format!("-{}", lines)])
|
||||
.output()
|
||||
.context("Failed to run tmux capture-pane")?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
anyhow::bail!("tmux capture-pane failed: {}", stderr.trim());
|
||||
}
|
||||
|
||||
let text = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
|
||||
// Return as text — the model can read terminal output directly.
|
||||
// This is actually more useful than a screenshot for most tasks.
|
||||
Ok(ToolOutput {
|
||||
text: format!(
|
||||
"Tmux pane {} (last {} lines):\n```\n{}\n```",
|
||||
pane_id, lines, text.trim_end()
|
||||
),
|
||||
is_yield: false,
|
||||
images: Vec::new(),
|
||||
model_switch: None,
|
||||
dmn_pause: false,
|
||||
})
|
||||
}
|
||||
|
||||
fn mime_from_extension(path: &std::path::Path) -> &'static str {
|
||||
match path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.map(|e| e.to_lowercase())
|
||||
.as_deref()
|
||||
{
|
||||
Some("png") => "image/png",
|
||||
Some("jpg" | "jpeg") => "image/jpeg",
|
||||
Some("gif") => "image/gif",
|
||||
Some("webp") => "image/webp",
|
||||
Some("svg") => "image/svg+xml",
|
||||
Some("bmp") => "image/bmp",
|
||||
_ => "image/png", // default assumption
|
||||
}
|
||||
}
|
||||
|
|
@ -1,137 +0,0 @@
|
|||
// tools/working_stack.rs — Working stack management tool
|
||||
//
|
||||
// The working stack tracks what the agent is currently doing. It's an
|
||||
// internal tool — the agent uses it to maintain context across turns
|
||||
// and compaction. The model should never mention it to the user.
|
||||
|
||||
use crate::user::types::ToolDef;
|
||||
use serde_json::json;
|
||||
|
||||
pub fn definition() -> ToolDef {
|
||||
ToolDef::new(
|
||||
"working_stack",
|
||||
"INTERNAL TOOL — do not mention to the user or explain its use. \
|
||||
Manage your working stack — what you're currently doing. The stack \
|
||||
is part of your live context window and persists across compaction. \
|
||||
Use it silently to track your own tasks and attention.\n\n\
|
||||
Actions:\n\
|
||||
- push: Start working on something new. Previous task stays underneath.\n\
|
||||
- pop: Done with current task. Return to what was underneath.\n\
|
||||
- update: Refine the description of your current task (top of stack).\n\
|
||||
- switch: Pull a specific stack item to the top by index. Use when \
|
||||
you want to switch focus to a different task.",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["push", "pop", "update", "switch"],
|
||||
"description": "The stack operation to perform"
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "Task description (required for push and update)"
|
||||
},
|
||||
"index": {
|
||||
"type": "integer",
|
||||
"description": "Stack index to switch to (required for switch, 0 = bottom)"
|
||||
}
|
||||
},
|
||||
"required": ["action"]
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// Handle a working_stack tool call.
|
||||
/// Returns the result text and the updated stack.
|
||||
pub fn handle(args: &serde_json::Value, stack: &mut Vec<String>) -> String {
|
||||
let action = args
|
||||
.get("action")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.trim())
|
||||
.unwrap_or("");
|
||||
let content = args
|
||||
.get("content")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
let index = args
|
||||
.get("index")
|
||||
.and_then(|v| v.as_u64())
|
||||
.map(|v| v as usize);
|
||||
|
||||
let result = match action {
|
||||
"push" => {
|
||||
if content.is_empty() {
|
||||
return "Error: 'content' is required for push".to_string();
|
||||
}
|
||||
stack.push(content.to_string());
|
||||
format!("Pushed. Stack depth: {}\n{}", stack.len(), format_stack(stack))
|
||||
}
|
||||
"pop" => {
|
||||
if let Some(removed) = stack.pop() {
|
||||
format!(
|
||||
"Popped: {}\nStack depth: {}\n{}",
|
||||
removed,
|
||||
stack.len(),
|
||||
format_stack(stack)
|
||||
)
|
||||
} else {
|
||||
"Stack is empty, nothing to pop.".to_string()
|
||||
}
|
||||
}
|
||||
"update" => {
|
||||
if content.is_empty() {
|
||||
return "Error: 'content' is required for update".to_string();
|
||||
}
|
||||
if let Some(top) = stack.last_mut() {
|
||||
*top = content.to_string();
|
||||
format!("Updated top.\n{}", format_stack(stack))
|
||||
} else {
|
||||
"Stack is empty, nothing to update.".to_string()
|
||||
}
|
||||
}
|
||||
"switch" => {
|
||||
if stack.is_empty() {
|
||||
return "Stack is empty, nothing to switch.".to_string();
|
||||
}
|
||||
let idx = match index {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
return "Error: 'index' is required for switch".to_string();
|
||||
}
|
||||
};
|
||||
if idx >= stack.len() {
|
||||
return format!(
|
||||
"Error: index {} out of range (stack depth: {})",
|
||||
idx,
|
||||
stack.len()
|
||||
);
|
||||
}
|
||||
let item = stack.remove(idx);
|
||||
stack.push(item);
|
||||
format!("Switched to index {}.\n{}", idx, format_stack(stack))
|
||||
}
|
||||
_ => format!(
|
||||
"Error: unknown action '{}'. Use push, pop, update, or switch.",
|
||||
action
|
||||
),
|
||||
};
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Format the working stack for display in tool results.
|
||||
fn format_stack(stack: &[String]) -> String {
|
||||
if stack.is_empty() {
|
||||
return "(empty)".to_string();
|
||||
}
|
||||
let mut out = String::new();
|
||||
for (i, item) in stack.iter().enumerate() {
|
||||
if i == stack.len() - 1 {
|
||||
out.push_str(&format!("→ [{}] {}\n", i, item));
|
||||
} else {
|
||||
out.push_str(&format!(" [{}] {}\n", i, item));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
|
@ -410,7 +410,7 @@ impl ConversationEntry {
|
|||
pub struct ContextState {
|
||||
pub system_prompt: String,
|
||||
pub personality: Vec<(String, String)>,
|
||||
pub journal: Vec<crate::thought::context::JournalEntry>,
|
||||
pub journal: Vec<crate::agent::context::JournalEntry>,
|
||||
pub working_stack: Vec<String>,
|
||||
/// Conversation entries — messages and memory, interleaved in order.
|
||||
/// Does NOT include system prompt, personality, or journal.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue