2026-03-22 02:09:40 -04:00
|
|
|
// LLM utilities: model invocation via direct API
|
2026-03-03 17:18:18 -05:00
|
|
|
|
|
|
|
|
use crate::store::Store;
|
|
|
|
|
use std::fs;
|
2026-03-05 15:30:57 -05:00
|
|
|
|
2026-03-22 02:08:32 -04:00
|
|
|
/// Simple LLM call for non-agent uses (audit, digest, compare).
|
|
|
|
|
/// Logs to llm-logs/{caller}/ file.
|
|
|
|
|
pub(crate) fn call_simple(caller: &str, prompt: &str) -> Result<String, String> {
|
2026-03-28 20:39:20 -04:00
|
|
|
let log_dir = dirs::home_dir().unwrap_or_default()
|
|
|
|
|
.join(".consciousness/logs/llm").join(caller);
|
2026-03-22 02:08:32 -04:00
|
|
|
fs::create_dir_all(&log_dir).ok();
|
|
|
|
|
let log_path = log_dir.join(format!("{}.txt", crate::store::compact_timestamp()));
|
|
|
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
let log = move |msg: &str| {
|
|
|
|
|
if let Ok(mut f) = fs::OpenOptions::new()
|
|
|
|
|
.create(true).append(true).open(&log_path)
|
|
|
|
|
{
|
|
|
|
|
let _ = writeln!(f, "{}", msg);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-26 14:21:43 -04:00
|
|
|
let prompts = vec![prompt.to_string()];
|
2026-03-27 15:44:39 -04:00
|
|
|
let phases = vec![];
|
2026-04-01 23:21:39 -04:00
|
|
|
super::api::call_api_with_tools_sync(caller, &prompts, &phases, None, 10, &[], None, &log)
|
2026-03-22 02:08:32 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:21:43 -04:00
|
|
|
/// Call a model using an agent definition's configuration (multi-step).
|
2026-03-26 14:48:42 -04:00
|
|
|
/// Optional bail_fn is called between steps — return Err to stop the pipeline.
|
2026-03-26 14:21:43 -04:00
|
|
|
pub(crate) fn call_for_def_multi(
|
2026-03-22 01:57:47 -04:00
|
|
|
def: &super::defs::AgentDef,
|
2026-03-26 14:21:43 -04:00
|
|
|
prompts: &[String],
|
2026-03-27 15:44:39 -04:00
|
|
|
phases: &[String],
|
2026-03-26 14:48:42 -04:00
|
|
|
bail_fn: Option<&(dyn Fn(usize) -> Result<(), String> + Sync)>,
|
2026-03-22 01:57:47 -04:00
|
|
|
log: &(dyn Fn(&str) + Sync),
|
|
|
|
|
) -> Result<String, String> {
|
2026-04-01 23:21:39 -04:00
|
|
|
super::api::call_api_with_tools_sync(&def.agent, prompts, phases, def.temperature, def.priority, &def.tools, bail_fn, log)
|
2026-03-13 18:50:06 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-03 17:18:18 -05:00
|
|
|
|
2026-03-08 20:07:07 -04:00
|
|
|
/// Get all keys for prompt context.
|
2026-03-03 17:18:18 -05:00
|
|
|
pub(crate) fn semantic_keys(store: &Store) -> Vec<String> {
|
|
|
|
|
let mut keys: Vec<String> = store.nodes.keys()
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
keys.sort();
|
|
|
|
|
keys.truncate(200);
|
|
|
|
|
keys
|
|
|
|
|
}
|