move working_stack code to correct file
This commit is contained in:
parent
e9d803c4ea
commit
375a8d9738
5 changed files with 24 additions and 30 deletions
|
|
@ -10,6 +10,7 @@ use crate::agent::api::types::*;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tiktoken_rs::CoreBPE;
|
use tiktoken_rs::CoreBPE;
|
||||||
|
use crate::agent::tools::working_stack;
|
||||||
|
|
||||||
/// A section of the context window, possibly with children.
|
/// A section of the context window, possibly with children.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -231,15 +232,6 @@ pub struct ContextState {
|
||||||
pub entries: Vec<ConversationEntry>,
|
pub entries: Vec<ConversationEntry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: these should not be hardcoded absolute paths
|
|
||||||
pub fn working_stack_instructions_path() -> std::path::PathBuf {
|
|
||||||
dirs::home_dir().unwrap_or_default().join(".consciousness/config/working-stack.md")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn working_stack_file_path() -> std::path::PathBuf {
|
|
||||||
dirs::home_dir().unwrap_or_default().join(".consciousness/working-stack.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ContextState {
|
impl ContextState {
|
||||||
/// Compute the context budget from typed sources.
|
/// Compute the context budget from typed sources.
|
||||||
pub fn budget(&self, count_str: &dyn Fn(&str) -> usize,
|
pub fn budget(&self, count_str: &dyn Fn(&str) -> usize,
|
||||||
|
|
@ -267,7 +259,7 @@ impl ContextState {
|
||||||
let mut parts: Vec<String> = self.personality.iter()
|
let mut parts: Vec<String> = self.personality.iter()
|
||||||
.map(|(name, content)| format!("## {}\n\n{}", name, content))
|
.map(|(name, content)| format!("## {}\n\n{}", name, content))
|
||||||
.collect();
|
.collect();
|
||||||
let instructions = std::fs::read_to_string(working_stack_instructions_path()).unwrap_or_default();
|
let instructions = std::fs::read_to_string(working_stack::instructions_path()).unwrap_or_default();
|
||||||
let mut stack_section = instructions;
|
let mut stack_section = instructions;
|
||||||
if self.working_stack.is_empty() {
|
if self.working_stack.is_empty() {
|
||||||
stack_section.push_str("\n## Current stack\n\n(empty)\n");
|
stack_section.push_str("\n## Current stack\n\n(empty)\n");
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,8 @@ use tools::{ToolCall, FunctionCall, summarize_args};
|
||||||
|
|
||||||
use crate::user::log::ConversationLog;
|
use crate::user::log::ConversationLog;
|
||||||
use crate::agent::api::types::*;
|
use crate::agent::api::types::*;
|
||||||
use crate::agent::context::{
|
use crate::agent::context::{ConversationEntry, ContextState, ContextBudget};
|
||||||
ConversationEntry, ContextState, ContextBudget,
|
use crate::agent::tools::working_stack;
|
||||||
working_stack_instructions_path, working_stack_file_path,
|
|
||||||
};
|
|
||||||
use crate::user::ui_channel::{ContextSection, SharedContextState, StreamTarget, StatusInfo, UiMessage, UiSender};
|
use crate::user::ui_channel::{ContextSection, SharedContextState, StreamTarget, StatusInfo, UiMessage, UiSender};
|
||||||
|
|
||||||
/// Result of a single agent turn.
|
/// Result of a single agent turn.
|
||||||
|
|
@ -720,7 +718,7 @@ impl Agent {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Working stack — instructions + items as children
|
// Working stack — instructions + items as children
|
||||||
let instructions = std::fs::read_to_string(working_stack_instructions_path())
|
let instructions = std::fs::read_to_string(working_stack::instructions_path())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let mut stack_children = vec![ContextSection {
|
let mut stack_children = vec![ContextSection {
|
||||||
name: "Instructions".into(),
|
name: "Instructions".into(),
|
||||||
|
|
@ -941,21 +939,12 @@ impl Agent {
|
||||||
|
|
||||||
/// Called after any change to context state (working stack, etc).
|
/// Called after any change to context state (working stack, etc).
|
||||||
fn refresh_context_state(&mut self) {
|
fn refresh_context_state(&mut self) {
|
||||||
|
|
||||||
self.publish_context_state();
|
self.publish_context_state();
|
||||||
self.save_working_stack();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Persist working stack to disk.
|
|
||||||
fn save_working_stack(&self) {
|
|
||||||
if let Ok(json) = serde_json::to_string(&self.context.working_stack) {
|
|
||||||
let _ = std::fs::write(working_stack_file_path(), json);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load working stack from disk.
|
/// Load working stack from disk.
|
||||||
fn load_working_stack(&mut self) {
|
fn load_working_stack(&mut self) {
|
||||||
if let Ok(data) = std::fs::read_to_string(working_stack_file_path()) {
|
if let Ok(data) = std::fs::read_to_string(working_stack::file_path()) {
|
||||||
if let Ok(stack) = serde_json::from_str::<Vec<String>>(&data) {
|
if let Ok(stack) = serde_json::from_str::<Vec<String>>(&data) {
|
||||||
self.context.working_stack = stack;
|
self.context.working_stack = stack;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@
|
||||||
// These set agent state directly via the Arc<Mutex<Agent>> handle,
|
// These set agent state directly via the Arc<Mutex<Agent>> handle,
|
||||||
// then return a text confirmation.
|
// then return a text confirmation.
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
|
||||||
|
|
||||||
pub(super) fn tools() -> [super::Tool; 3] {
|
pub(super) fn tools() -> [super::Tool; 3] {
|
||||||
use super::Tool;
|
use super::Tool;
|
||||||
[
|
[
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ mod write;
|
||||||
// Agent-specific tools
|
// Agent-specific tools
|
||||||
mod control;
|
mod control;
|
||||||
mod vision;
|
mod vision;
|
||||||
mod working_stack;
|
pub mod working_stack;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,15 @@
|
||||||
// internal tool — the agent uses it to maintain context across turns
|
// internal tool — the agent uses it to maintain context across turns
|
||||||
// and compaction. The model should never mention it to the user.
|
// and compaction. The model should never mention it to the user.
|
||||||
|
|
||||||
|
// TODO: these should not be hardcoded absolute paths
|
||||||
|
pub fn instructions_path() -> std::path::PathBuf {
|
||||||
|
dirs::home_dir().unwrap_or_default().join(".consciousness/config/working-stack.md")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file_path() -> std::path::PathBuf {
|
||||||
|
dirs::home_dir().unwrap_or_default().join(".consciousness/working-stack.json")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn tool() -> super::Tool {
|
pub fn tool() -> super::Tool {
|
||||||
super::Tool {
|
super::Tool {
|
||||||
name: "working_stack",
|
name: "working_stack",
|
||||||
|
|
@ -25,7 +34,7 @@ fn handle(args: &serde_json::Value, stack: &mut Vec<String>) -> String {
|
||||||
let content = args.get("content").and_then(|v| v.as_str()).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 index = args.get("index").and_then(|v| v.as_u64()).map(|v| v as usize);
|
||||||
|
|
||||||
match action {
|
let out = match action {
|
||||||
"push" => {
|
"push" => {
|
||||||
if content.is_empty() { return "Error: 'content' is required for push".into(); }
|
if content.is_empty() { return "Error: 'content' is required for push".into(); }
|
||||||
stack.push(content.to_string());
|
stack.push(content.to_string());
|
||||||
|
|
@ -56,7 +65,13 @@ fn handle(args: &serde_json::Value, stack: &mut Vec<String>) -> String {
|
||||||
format!("Switched to index {}.\n{}", idx, format_stack(stack))
|
format!("Switched to index {}.\n{}", idx, format_stack(stack))
|
||||||
}
|
}
|
||||||
_ => format!("Error: unknown action '{}'. Use push, pop, update, or switch.", action),
|
_ => format!("Error: unknown action '{}'. Use push, pop, update, or switch.", action),
|
||||||
}
|
};
|
||||||
|
|
||||||
|
if let Ok(json) = serde_json::to_string(stack) {
|
||||||
|
let _ = std::fs::write(file_path(), json);
|
||||||
|
};
|
||||||
|
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_stack(stack: &[String]) -> String {
|
fn format_stack(stack: &[String]) -> String {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue