aboutsummaryrefslogtreecommitdiffstats
path: root/src/agent.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent.rs')
-rw-r--r--src/agent.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/agent.rs b/src/agent.rs
new file mode 100644
index 0000000..2d84566
--- /dev/null
+++ b/src/agent.rs
@@ -0,0 +1,67 @@
1use std::sync::atomic::AtomicBool;
2
3use serde_json::{json, Value};
4
5use crate::config::Config;
6use crate::provider::{self, Error, Outcome, Sink};
7
8/// Frozen for the life of the process. Anything session-varying added here
9/// invalidates the provider's prompt cache, so keep it boring.
10pub fn system_message() -> Value {
11 json!({
12 "role": "system",
13 "content": "You are agent686, a coding agent working in a Unix terminal. \
14 You edit files and run commands. Be terse and do not explain \
15 what you are about to do before doing it."
16 })
17}
18
19/// Appends the user message, runs one provider round trip, and appends the
20/// assistant message to the history whether or not it asked for tools.
21pub fn run_turn(
22 cfg: &Config,
23 history: &mut Vec<Value>,
24 prompt: &str,
25 cancel: &AtomicBool,
26 sink: &mut dyn Sink,
27) -> Result<Outcome, Error> {
28 history.push(json!({ "role": "user", "content": prompt }));
29
30 let outcome = provider::chat(cfg, history, cancel, sink)?;
31
32 let mut message = serde_json::Map::new();
33 message.insert("role".into(), json!("assistant"));
34 message.insert(
35 "content".into(),
36 if outcome.content.is_empty() {
37 Value::Null
38 } else {
39 json!(outcome.content)
40 },
41 );
42 if !outcome.tool_calls.is_empty() {
43 let calls: Vec<Value> = outcome
44 .tool_calls
45 .iter()
46 .map(|call| {
47 json!({
48 "id": call.id,
49 "type": "function",
50 "function": {
51 "name": call.name,
52 // Byte-for-byte what the model sent.
53 "arguments": call.arguments,
54 }
55 })
56 })
57 .collect();
58 message.insert("tool_calls".into(), Value::Array(calls));
59 }
60 history.push(Value::Object(message));
61
62 Ok(outcome)
63}
64
65pub fn new_history() -> Vec<Value> {
66 vec![system_message()]
67}