| 1 |
|
- |
//! Open Responses streaming client for coder-lite.
|
|
1
|
+ |
//! coder-lite's turn: its own voice over the capable runtime next door.
|
|
2
|
+ |
//!
|
|
3
|
+ |
//! The session underneath is [`openagents_cli::runtime::CoderRuntimeSession`]
|
|
4
|
+ |
//! — threads, grants, the inference proxy, the live model catalog, lanes,
|
|
5
|
+ |
//! metering, and revocation — and it is used rather than copied, so there is
|
|
6
|
+ |
//! one implementation of each of those and this crate cannot drift from it.
|
|
7
|
+ |
//!
|
|
8
|
+ |
//! What this file owns is the part that is coder-lite's:
|
|
9
|
+ |
//!
|
|
10
|
+ |
//! - [`SYSTEM_INSTRUCTIONS`], carried verbatim. It is the reason the session
|
|
11
|
+ |
//! answers as a terminal rather than as an assistant, and a merge that
|
|
12
|
+ |
//! reworded it would have changed the product.
|
|
13
|
+ |
//! - [`Control`], the one-way channel the TUI loop reads. Text, tool calls,
|
|
14
|
+ |
//! tool results, the model that answered, what the turn spent, and the
|
|
15
|
+ |
//! failures — each as its own message, so the frame shows what happened and
|
|
16
|
+ |
//! not a summary written afterwards.
|
|
17
|
+ |
//!
|
|
18
|
+ |
//! ## Nothing here invents an answer
|
|
19
|
+ |
//!
|
|
20
|
+ |
//! Every path is the model's own words or a [`Control::Failed`] naming what
|
|
21
|
+ |
//! refused. There is no fallback model, no synthesized reply, and no invented
|
|
22
|
+ |
//! grant: the session below refuses out loud and this file carries the refusal
|
|
23
|
+ |
//! to the screen instead of painting over it.
|
| 2 |
24
|
|
|
| 3 |
|
- |
use futures::StreamExt;
|
| 4 |
|
- |
use openresponses_rust::{
|
| 5 |
|
- |
CreateResponseBody, FunctionOutput, Input, Item, StreamingClient, StreamingEvent, Tool,
|
| 6 |
|
- |
ToolChoice, ToolChoiceParam,
|
| 7 |
|
- |
};
|
| 8 |
25
|
|
use std::env;
|
| 9 |
|
- |
use std::path::PathBuf;
|
|
26
|
+ |
use std::sync::atomic::{AtomicBool, Ordering};
|
| 10 |
27
|
|
use std::sync::mpsc::Sender;
|
|
28
|
+ |
use std::sync::{Arc, Mutex};
|
| 11 |
29
|
|
|
| 12 |
|
- |
use crate::acp::Agent;
|
| 13 |
|
- |
use crate::acp_harness::{AcpEvent, AcpHarness};
|
|
30
|
+ |
use openagents_cli::runtime::{
|
|
31
|
+ |
ChatMessage, CoderRuntimeSession, Lane, ToolEvent, TurnUsage,
|
|
32
|
+ |
};
|
|
33
|
+ |
use openagents_cli::tools::{DelegationGate, HarnessToolRegistry, ToolDefinition};
|
| 14 |
34
|
|
|
|
35
|
+ |
/// coder-lite's voice. Carried verbatim from the first version of this file;
|
|
36
|
+ |
/// see the module header for why it does not move.
|
| 15 |
37
|
|
const SYSTEM_INSTRUCTIONS: &str = "You are OpenAgents Coder. Do not say you are from Google, Anthropic, OpenAI, or any other company. Do not mention your model, training, or architecture. Respond as a neutral, terse terminal: no greetings, no \"As an AI\", no explanations of your role, and no unnecessary padding. Use short sentences and dense, factual output. Answer questions directly. Output only code and minimal context when asked for code.";
|
| 16 |
38
|
|
|
|
39
|
+ |
type Failure = Box<dyn std::error::Error + Send + Sync>;
|
|
40
|
+ |
|
|
41
|
+ |
/// What the runtime tells the frame, in the order it happened.
|
|
42
|
+ |
#[derive(Debug, Clone)]
|
| 17 |
43
|
|
pub enum Control {
|
|
44
|
+ |
/// A piece of the reply, as the model wrote it.
|
| 18 |
45
|
|
Chunk(String),
|
| 19 |
|
- |
Done,
|
|
46
|
+ |
/// A tool call started. The header goes up now; the box fills in later.
|
| 20 |
47
|
|
Tool {
|
| 21 |
|
- |
function_name: String,
|
|
48
|
+ |
call_id: String,
|
|
49
|
+ |
name: String,
|
|
50
|
+ |
/// The raw JSON string from the wire.
|
| 22 |
51
|
|
arguments: String,
|
| 23 |
|
- |
title: String,
|
| 24 |
52
|
|
},
|
| 25 |
|
- |
ToolTitle(String),
|
| 26 |
|
- |
ToolText(String),
|
| 27 |
|
- |
ToolDone,
|
|
53
|
+ |
/// More of what that call has printed, appended to its box.
|
|
54
|
+ |
ToolOutput { call_id: String, chunk: String },
|
|
55
|
+ |
/// That call finished, and whether it worked.
|
|
56
|
+ |
ToolDone { call_id: String, is_error: bool },
|
|
57
|
+ |
/// The model that actually answered, as its grant pins it.
|
|
58
|
+ |
Model(String),
|
|
59
|
+ |
/// What the turn spent, as the server reported it.
|
|
60
|
+ |
Usage(TurnUsage),
|
|
61
|
+ |
/// Something worth saying that is not the model talking.
|
|
62
|
+ |
Notice(String),
|
|
63
|
+ |
/// What one of the session's own commands printed. Markdown, rendered the
|
|
64
|
+ |
/// way an answer is, and exported as a notice rather than a model step.
|
|
65
|
+ |
Output(String),
|
|
66
|
+ |
/// The turn did not answer, and this is why. Never an answer.
|
|
67
|
+ |
Failed(String),
|
|
68
|
+ |
/// The turn is over, one way or the other.
|
|
69
|
+ |
Done,
|
| 28 |
70
|
|
}
|
| 29 |
71
|
|
|
| 30 |
|
- |
pub struct CoderRuntimeSession {
|
| 31 |
|
- |
pub api_key: String,
|
| 32 |
|
- |
pub base_url: String,
|
| 33 |
|
- |
pub history: Vec<Item>,
|
| 34 |
|
- |
pub agents: Vec<Agent>,
|
|
72
|
+ |
/// A `Sender` an observer can hold: `Fn` observers are shared, and the frame
|
|
73
|
+ |
/// loop's receiver is on the other end of exactly one channel.
|
|
74
|
+ |
pub type Sink = Arc<Mutex<Sender<Control>>>;
|
|
75
|
+ |
|
|
76
|
+ |
/// Put a message on the frame's channel, or drop it if the frame is gone.
|
|
77
|
+ |
pub fn send(sink: &Sink, message: Control) {
|
|
78
|
+ |
if let Ok(tx) = sink.lock() {
|
|
79
|
+ |
let _ = tx.send(message);
|
|
80
|
+ |
}
|
| 35 |
81
|
|
}
|
| 36 |
82
|
|
|
| 37 |
|
- |
impl CoderRuntimeSession {
|
| 38 |
|
- |
pub fn new() -> Self {
|
| 39 |
|
- |
Self {
|
| 40 |
|
- |
api_key: env::var("OPENAGENTS_API_KEY").unwrap_or_default(),
|
| 41 |
|
- |
base_url: env::var("OPENAGENTS_BASE_URL")
|
| 42 |
|
- |
.unwrap_or_else(|_| "https://openagents.com/api/v1".to_string()),
|
| 43 |
|
- |
history: vec![Item::system_message(SYSTEM_INSTRUCTIONS)],
|
| 44 |
|
- |
agents: Vec::new(),
|
|
83
|
+ |
/// The system message this session opens with.
|
|
84
|
+ |
///
|
|
85
|
+ |
/// [`SYSTEM_INSTRUCTIONS`] first and unchanged, then the tools — because a
|
|
86
|
+ |
/// model that is told it has no tools when it has five will not use them, and
|
|
87
|
+ |
/// one told it has tools it does not have will claim to have run them. The
|
|
88
|
+ |
/// list is generated from what was actually declared, so the two cannot
|
|
89
|
+ |
/// disagree.
|
|
90
|
+ |
pub fn system_prompt(tools: &[ToolDefinition]) -> String {
|
|
91
|
+ |
let mut lines = vec![SYSTEM_INSTRUCTIONS.to_string(), String::new()];
|
|
92
|
+ |
if tools.is_empty() {
|
|
93
|
+ |
lines.push(
|
|
94
|
+ |
"You have no tools in this session: you cannot read or write files, run commands, or \
|
|
95
|
+ |
reach anything outside this conversation. Say plainly when something would need a \
|
|
96
|
+ |
tool you do not have."
|
|
97
|
+ |
.to_string(),
|
|
98
|
+ |
);
|
|
99
|
+ |
} else {
|
|
100
|
+ |
lines.push(format!("You have {} tools, and no others:", tools.len()));
|
|
101
|
+ |
for tool in tools {
|
|
102
|
+ |
lines.push(format!("- `{}`", tool.name));
|
|
103
|
+ |
}
|
|
104
|
+ |
lines.push(String::new());
|
|
105
|
+ |
lines.push(
|
|
106
|
+ |
"That list is complete: a capability not on it is one you do not have. Read a tool's \
|
|
107
|
+ |
description before assuming what it covers. Never say you ran something you did not \
|
|
108
|
+ |
run."
|
|
109
|
+ |
.to_string(),
|
|
110
|
+ |
);
|
|
111
|
+ |
}
|
|
112
|
+ |
lines.join("\n")
|
|
113
|
+ |
}
|
|
114
|
+ |
|
|
115
|
+ |
/// The `/api/v1` base this session talks to.
|
|
116
|
+ |
///
|
|
117
|
+ |
/// `OPENAGENTS_BASE_URL` first, because that is what `--dev` sets and a reader
|
|
118
|
+ |
/// who pointed the session at a server on this machine meant it. Then the
|
|
119
|
+ |
/// endpoint `oa auth` selected, so coder-lite and `oa` agree about where they
|
|
120
|
+ |
/// are without a second configuration file.
|
|
121
|
+ |
pub fn api_base() -> String {
|
|
122
|
+ |
for name in ["OPENAGENTS_BASE_URL", "OPENAGENTS_API_BASE"] {
|
|
123
|
+ |
if let Ok(value) = env::var(name) {
|
|
124
|
+ |
let value = value.trim().to_string();
|
|
125
|
+ |
if !value.is_empty() {
|
|
126
|
+ |
return value;
|
|
127
|
+ |
}
|
|
128
|
+ |
}
|
|
129
|
+ |
}
|
|
130
|
+ |
match openagents_cli::auth::resolve_endpoint(None, None) {
|
|
131
|
+ |
Ok(endpoint) => format!("{}/api/v1", endpoint.origin),
|
|
132
|
+ |
Err(_) => "https://openagents.com/api/v1".to_string(),
|
|
133
|
+ |
}
|
|
134
|
+ |
}
|
|
135
|
+ |
|
|
136
|
+ |
/// The credential this session spends, or `None`.
|
|
137
|
+ |
///
|
|
138
|
+ |
/// `None` is carried rather than papered over: a thread request without one is
|
|
139
|
+ |
/// refused by the server, and that refusal is what the reader should see.
|
|
140
|
+ |
pub fn user_token() -> Option<String> {
|
|
141
|
+ |
if let Ok(value) = env::var("OPENAGENTS_API_KEY") {
|
|
142
|
+ |
let value = value.trim().to_string();
|
|
143
|
+ |
if !value.is_empty() {
|
|
144
|
+ |
return Some(value);
|
| 45 |
145
|
|
}
|
| 46 |
146
|
|
}
|
|
147
|
+ |
let endpoint = openagents_cli::auth::resolve_endpoint(None, None).ok()?;
|
|
148
|
+ |
openagents_cli::auth::CredentialStore::for_origin(&endpoint.origin).get_token()
|
|
149
|
+ |
}
|
|
150
|
+ |
|
|
151
|
+ |
/// The session a coder-lite frame drives.
|
|
152
|
+ |
pub struct Session {
|
|
153
|
+ |
inner: CoderRuntimeSession,
|
|
154
|
+ |
lane: Lane,
|
|
155
|
+ |
/// Whether this user turn has already handed work to an ACP agent.
|
|
156
|
+ |
///
|
|
157
|
+ |
/// Cleared at the top of every turn and set by the `acp` tool itself; see
|
|
158
|
+ |
/// [`crate::acp_tool`] for why one is the limit.
|
|
159
|
+ |
acp_spent: Arc<AtomicBool>,
|
|
160
|
+ |
}
|
|
161
|
+ |
|
|
162
|
+ |
impl Session {
|
|
163
|
+ |
/// Open a session on `lane`, reporting everything it does to `tx`.
|
|
164
|
+ |
///
|
|
165
|
+ |
/// The tools are the full set — `shell`, `skill`, `openagents`,
|
|
166
|
+ |
/// `capability`, and `delegate` — on the same terms `oa coder` gets them:
|
|
167
|
+ |
/// children run on this lane, on this credential, and cannot delegate
|
|
168
|
+ |
/// again.
|
|
169
|
+ |
pub fn open(
|
|
170
|
+ |
lane: Lane,
|
|
171
|
+ |
lane_name: &str,
|
|
172
|
+ |
reasoning: Option<String>,
|
|
173
|
+ |
agents: Vec<crate::acp::Agent>,
|
|
174
|
+ |
tx: Sender<Control>,
|
|
175
|
+ |
) -> Self {
|
|
176
|
+ |
Self::open_at(
|
|
177
|
+ |
lane,
|
|
178
|
+ |
lane_name,
|
|
179
|
+ |
reasoning,
|
|
180
|
+ |
agents,
|
|
181
|
+ |
api_base(),
|
|
182
|
+ |
user_token(),
|
|
183
|
+ |
tx,
|
|
184
|
+ |
)
|
|
185
|
+ |
}
|
| 47 |
186
|
|
|
| 48 |
|
- |
pub async fn execute_turn(
|
| 49 |
|
- |
&mut self,
|
| 50 |
|
- |
prompt: &str,
|
|
187
|
+ |
/// [`Self::open`] against a named server with a named credential.
|
|
188
|
+ |
///
|
|
189
|
+ |
/// The environment is process-global and a test that set it would race
|
|
190
|
+ |
/// every other test in the same binary, so the two values the environment
|
|
191
|
+ |
/// supplies are parameters here and read from the environment exactly once,
|
|
192
|
+ |
/// in `open`.
|
|
193
|
+ |
#[allow(clippy::too_many_arguments)]
|
|
194
|
+ |
pub fn open_at(
|
|
195
|
+ |
lane: Lane,
|
|
196
|
+ |
lane_name: &str,
|
|
197
|
+ |
reasoning: Option<String>,
|
|
198
|
+ |
agents: Vec<crate::acp::Agent>,
|
|
199
|
+ |
api_base: String,
|
|
200
|
+ |
token: Option<String>,
|
| 51 |
201
|
|
tx: Sender<Control>,
|
| 52 |
|
- |
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
| 53 |
|
- |
if self.api_key.is_empty() {
|
| 54 |
|
- |
let _ = tx.send(Control::Chunk(
|
| 55 |
|
- |
"[error: OPENAGENTS_API_KEY is not set]".to_string(),
|
| 56 |
|
- |
));
|
| 57 |
|
- |
let _ = tx.send(Control::Done);
|
| 58 |
|
- |
return Err("OPENAGENTS_API_KEY is not set".into());
|
|
202
|
+ |
) -> Self {
|
|
203
|
+ |
let mut tools = HarnessToolRegistry::with_delegation(
|
|
204
|
+ |
None,
|
|
205
|
+ |
DelegationGate {
|
|
206
|
+ |
lane: lane_name.to_string(),
|
|
207
|
+ |
user_token: token.clone(),
|
|
208
|
+ |
max_count: openagents_cli::delegate::MAX_DELEGATE_COUNT,
|
|
209
|
+ |
// coder-lite takes no `--child-*` flags yet, so children start
|
|
210
|
+ |
// on the defaults. Set rather than defaulted at the struct so
|
|
211
|
+ |
// adding those flags here is a visible edit, not a silent
|
|
212
|
+ |
// inheritance of whatever the field grows into.
|
|
213
|
+ |
child: openagents_cli::delegate::ChildOptions::default(),
|
|
214
|
+ |
},
|
|
215
|
+ |
);
|
|
216
|
+ |
|
|
217
|
+ |
let sink: Sink = Arc::new(Mutex::new(tx));
|
|
218
|
+ |
let observed = Arc::clone(&sink);
|
|
219
|
+ |
|
|
220
|
+ |
// coder-lite's own capability, declared only where there is one to
|
|
221
|
+ |
// declare: `find_agents` reports installed agents, so a machine with
|
|
222
|
+ |
// none does not see the tool.
|
|
223
|
+ |
let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
|
|
224
|
+ |
let found = !agents.is_empty();
|
|
225
|
+ |
let acp_spent = Arc::new(AtomicBool::new(false));
|
|
226
|
+ |
if let Some(tool) = crate::acp_tool::acp_host_tool(
|
|
227
|
+ |
agents,
|
|
228
|
+ |
cwd,
|
|
229
|
+ |
Arc::clone(&sink),
|
|
230
|
+ |
Arc::clone(&acp_spent),
|
|
231
|
+ |
) {
|
|
232
|
+ |
if let Err(refusal) = tools.add_host_tool(tool) {
|
|
233
|
+ |
send(&sink, Control::Notice(refusal));
|
|
234
|
+ |
}
|
|
235
|
+ |
} else if found {
|
|
236
|
+ |
// Unreachable while `acp_host_tool` refuses only an empty list,
|
|
237
|
+ |
// and here so that stops being silently true if it stops being.
|
|
238
|
+ |
send(
|
|
239
|
+ |
&sink,
|
|
240
|
+ |
Control::Notice("ACP agents were found but no `acp` tool was declared".to_string()),
|
|
241
|
+ |
);
|
| 59 |
242
|
|
}
|
| 60 |
243
|
|
|
| 61 |
|
- |
self.history.push(Item::user_message(prompt));
|
| 62 |
|
- |
|
| 63 |
|
- |
let client = StreamingClient::with_base_url(&self.api_key, &self.base_url);
|
| 64 |
|
- |
let tools = self.delegate_tool();
|
| 65 |
|
- |
let mut post_tool = false;
|
| 66 |
|
- |
|
| 67 |
|
- |
loop {
|
| 68 |
|
- |
let tool_choice = if post_tool {
|
| 69 |
|
- |
Some(ToolChoiceParam::Simple(ToolChoice::None))
|
| 70 |
|
- |
} else {
|
| 71 |
|
- |
Some(ToolChoiceParam::Simple(ToolChoice::Auto))
|
| 72 |
|
- |
};
|
| 73 |
|
- |
let request = CreateResponseBody {
|
| 74 |
|
- |
model: env::var("OPENAGENTS_MODEL").ok(),
|
| 75 |
|
- |
input: Some(Input::Items(self.history.clone())),
|
| 76 |
|
- |
tools: tools.clone(),
|
| 77 |
|
- |
tool_choice,
|
| 78 |
|
- |
stream: Some(true),
|
| 79 |
|
- |
..Default::default()
|
| 80 |
|
- |
};
|
| 81 |
|
- |
|
| 82 |
|
- |
let mut stream = match client.stream_response(request).await {
|
| 83 |
|
- |
Ok(s) => s,
|
| 84 |
|
- |
Err(e) => {
|
| 85 |
|
- |
let _ = tx.send(Control::Chunk(format!("[error: {}]", e)));
|
| 86 |
|
- |
let _ = tx.send(Control::Done);
|
| 87 |
|
- |
return Err(e.into());
|
| 88 |
|
- |
}
|
| 89 |
|
- |
};
|
| 90 |
|
- |
|
| 91 |
|
- |
let mut collected = String::new();
|
| 92 |
|
- |
let mut pending_tool: Option<(String, String, String, String)> = None;
|
| 93 |
|
- |
|
| 94 |
|
- |
while let Some(event) = stream.next().await {
|
| 95 |
|
- |
match event {
|
| 96 |
|
- |
Ok(StreamingEvent::OutputTextDelta { delta, .. }) => {
|
| 97 |
|
- |
collected.push_str(&delta);
|
| 98 |
|
- |
let _ = tx.send(Control::Chunk(delta));
|
| 99 |
|
- |
}
|
| 100 |
|
- |
Ok(StreamingEvent::ReasoningDelta { delta, .. }) => {
|
| 101 |
|
- |
let _ = tx.send(Control::Chunk(delta));
|
| 102 |
|
- |
}
|
| 103 |
|
- |
Ok(StreamingEvent::OutputItemDone {
|
| 104 |
|
- |
item: Some(Item::FunctionCall {
|
| 105 |
|
- |
call_id,
|
| 106 |
|
- |
name,
|
| 107 |
|
- |
arguments,
|
| 108 |
|
- |
..
|
| 109 |
|
- |
}),
|
| 110 |
|
- |
..
|
| 111 |
|
- |
}) if name == "delegate" => {
|
| 112 |
|
- |
let args = serde_json::from_str::<serde_json::Value>(&arguments)
|
| 113 |
|
- |
.unwrap_or(serde_json::json!({}));
|
| 114 |
|
- |
let agent = args
|
| 115 |
|
- |
.get("agent")
|
| 116 |
|
- |
.and_then(|v| v.as_str())
|
| 117 |
|
- |
.unwrap_or("")
|
| 118 |
|
- |
.to_string();
|
| 119 |
|
- |
let task = args
|
| 120 |
|
- |
.get("prompt")
|
| 121 |
|
- |
.and_then(|v| v.as_str())
|
| 122 |
|
- |
.unwrap_or("")
|
| 123 |
|
- |
.to_string();
|
| 124 |
|
- |
pending_tool = Some((call_id, agent, task, arguments));
|
| 125 |
|
- |
}
|
| 126 |
|
- |
Ok(StreamingEvent::Error { error, .. }) => {
|
| 127 |
|
- |
let msg = format!("[error: {:?}]", error);
|
| 128 |
|
- |
let _ = tx.send(Control::Chunk(msg));
|
| 129 |
|
- |
}
|
| 130 |
|
- |
Ok(_) => {}
|
| 131 |
|
- |
Err(e) => {
|
| 132 |
|
- |
let _ = tx.send(Control::Chunk(format!("[error: {}]", e)));
|
| 133 |
|
- |
let _ = tx.send(Control::Done);
|
| 134 |
|
- |
return Err(e.into());
|
| 135 |
|
- |
}
|
| 136 |
|
- |
}
|
|
244
|
+ |
let mut inner = CoderRuntimeSession::new(lane.clone(), Some(api_base), token, tools)
|
|
245
|
+ |
.observing_tools(Arc::new(move |event: ToolEvent| match event {
|
|
246
|
+ |
ToolEvent::Started {
|
|
247
|
+ |
call_id,
|
|
248
|
+ |
name,
|
|
249
|
+ |
arguments,
|
|
250
|
+ |
} => send(
|
|
251
|
+ |
&observed,
|
|
252
|
+ |
Control::Tool {
|
|
253
|
+ |
call_id,
|
|
254
|
+ |
name,
|
|
255
|
+ |
arguments,
|
|
256
|
+ |
},
|
|
257
|
+ |
),
|
|
258
|
+ |
ToolEvent::Finished {
|
|
259
|
+ |
call_id,
|
|
260
|
+ |
output,
|
|
261
|
+ |
is_error,
|
|
262
|
+ |
..
|
|
263
|
+ |
} => {
|
|
264
|
+ |
send(
|
|
265
|
+ |
&observed,
|
|
266
|
+ |
Control::ToolOutput {
|
|
267
|
+ |
call_id: call_id.clone(),
|
|
268
|
+ |
chunk: output,
|
|
269
|
+ |
},
|
|
270
|
+ |
);
|
|
271
|
+ |
send(&observed, Control::ToolDone { call_id, is_error });
|
| 137 |
272
|
|
}
|
|
273
|
+ |
}));
|
|
274
|
+ |
inner.reasoning = reasoning;
|
|
275
|
+ |
inner.repository = repository();
|
|
276
|
+ |
|
|
277
|
+ |
// Seeded here so the session below leaves it alone: `execute_turn`
|
|
278
|
+ |
// writes its own system prompt only into an empty message list, and
|
|
279
|
+ |
// this one is coder-lite's.
|
|
280
|
+ |
let prompt = system_prompt(&inner.tools.list_tools());
|
|
281
|
+ |
inner.messages.push(ChatMessage {
|
|
282
|
+ |
role: "system".to_string(),
|
|
283
|
+ |
content: Some(prompt),
|
|
284
|
+ |
tool_calls: None,
|
|
285
|
+ |
tool_call_id: None,
|
|
286
|
+ |
});
|
| 138 |
287
|
|
|
| 139 |
|
- |
let mut turn_failed = false;
|
| 140 |
|
- |
if let Some((call_id, agent_id, task, raw_args)) = pending_tool.take() {
|
| 141 |
|
- |
if let Some(agent) = self.agents.iter().find(|a| a.id == agent_id).cloned() {
|
| 142 |
|
- |
let _ = tx.send(Control::Tool {
|
| 143 |
|
- |
function_name: "delegate".to_string(),
|
| 144 |
|
- |
arguments: raw_args,
|
| 145 |
|
- |
title: task.clone(),
|
| 146 |
|
- |
});
|
| 147 |
|
- |
|
| 148 |
|
- |
let cwd = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
|
| 149 |
|
- |
let mut header_sent = false;
|
| 150 |
|
- |
let result = {
|
| 151 |
|
- |
let tx = tx.clone();
|
| 152 |
|
- |
AcpHarness {
|
| 153 |
|
- |
command: agent.command,
|
| 154 |
|
- |
args: agent.args,
|
| 155 |
|
- |
}
|
| 156 |
|
- |
.run(&task, &cwd, |event| {
|
| 157 |
|
- |
match event {
|
| 158 |
|
- |
AcpEvent::Tool { title, .. } => {
|
| 159 |
|
- |
header_sent = true;
|
| 160 |
|
- |
let _ = tx.send(Control::ToolTitle(title));
|
| 161 |
|
- |
}
|
| 162 |
|
- |
AcpEvent::Text { chunk } => {
|
| 163 |
|
- |
let _ = tx.send(Control::ToolText(chunk));
|
| 164 |
|
- |
}
|
| 165 |
|
- |
_ => {}
|
| 166 |
|
- |
}
|
| 167 |
|
- |
})
|
| 168 |
|
- |
.await
|
| 169 |
|
- |
};
|
| 170 |
|
- |
|
| 171 |
|
- |
match &result {
|
| 172 |
|
- |
Ok(answer)
|
| 173 |
|
- |
if answer
|
| 174 |
|
- |
.to_lowercase()
|
| 175 |
|
- |
.contains("upgrade your plan to continue") =>
|
| 176 |
|
- |
{
|
| 177 |
|
- |
let _ = tx.send(Control::ToolTitle("refused".to_string()));
|
| 178 |
|
- |
let _ = tx.send(Control::ToolText(answer.clone()));
|
| 179 |
|
- |
turn_failed = true;
|
| 180 |
|
- |
}
|
| 181 |
|
- |
Ok(_) if !header_sent => {
|
| 182 |
|
- |
let _ = tx.send(Control::ToolTitle("completed".to_string()));
|
| 183 |
|
- |
}
|
| 184 |
|
- |
Err(_) => {
|
| 185 |
|
- |
let _ = tx.send(Control::ToolTitle("error".to_string()));
|
| 186 |
|
- |
let _ = tx.send(Control::ToolText(
|
| 187 |
|
- |
result.as_ref().err().unwrap().to_string(),
|
| 188 |
|
- |
));
|
| 189 |
|
- |
turn_failed = true;
|
| 190 |
|
- |
}
|
| 191 |
|
- |
_ => {}
|
| 192 |
|
- |
}
|
| 193 |
|
- |
|
| 194 |
|
- |
let _ = tx.send(Control::ToolDone);
|
| 195 |
|
- |
let output = result.unwrap_or_else(|e| e.to_string());
|
| 196 |
|
- |
self.history.push(Item::FunctionCallOutput {
|
| 197 |
|
- |
id: None,
|
| 198 |
|
- |
call_id,
|
| 199 |
|
- |
output: FunctionOutput::Text(output),
|
| 200 |
|
- |
status: None,
|
| 201 |
|
- |
});
|
| 202 |
|
- |
if turn_failed {
|
| 203 |
|
- |
break;
|
| 204 |
|
- |
}
|
| 205 |
|
- |
post_tool = true;
|
| 206 |
|
- |
continue;
|
| 207 |
|
- |
} else {
|
| 208 |
|
- |
let msg = format!("unknown ACP agent: {}", agent_id);
|
| 209 |
|
- |
let _ = tx.send(Control::Chunk(msg.clone()));
|
| 210 |
|
- |
self.history.push(Item::FunctionCallOutput {
|
| 211 |
|
- |
id: None,
|
| 212 |
|
- |
call_id,
|
| 213 |
|
- |
output: FunctionOutput::Text(msg),
|
| 214 |
|
- |
status: None,
|
| 215 |
|
- |
});
|
| 216 |
|
- |
post_tool = true;
|
| 217 |
|
- |
continue;
|
|
288
|
+ |
Self {
|
|
289
|
+ |
inner,
|
|
290
|
+ |
lane,
|
|
291
|
+ |
acp_spent,
|
|
292
|
+ |
}
|
|
293
|
+ |
}
|
|
294
|
+ |
|
|
295
|
+ |
/// The lane this session was opened on. What was asked for, not what
|
|
296
|
+ |
/// answered — [`Control::Model`] carries that.
|
|
297
|
+ |
pub fn lane(&self) -> &Lane {
|
|
298
|
+ |
&self.lane
|
|
299
|
+ |
}
|
|
300
|
+ |
|
|
301
|
+ |
/// Run one turn, streaming everything it does down `tx`.
|
|
302
|
+ |
///
|
|
303
|
+ |
/// Always ends with exactly one [`Control::Done`], so a frame cannot be
|
|
304
|
+ |
/// left spinning over a turn that has finished.
|
|
305
|
+ |
pub async fn execute_turn(&mut self, prompt: &str, tx: Sender<Control>) {
|
|
306
|
+ |
// A fresh turn may hand work to an agent again. The limit is per user
|
|
307
|
+ |
// turn, not per session.
|
|
308
|
+ |
self.acp_spent.store(false, Ordering::SeqCst);
|
|
309
|
+ |
let sink: Sink = Arc::new(Mutex::new(tx));
|
|
310
|
+ |
let chunks = Arc::clone(&sink);
|
|
311
|
+ |
// Whether the reply reached the frame as it was written. The answer
|
|
312
|
+ |
// `execute_turn` returns has normally already streamed, so repeating
|
|
313
|
+ |
// it would print it twice; this is what tells the one case from the
|
|
314
|
+ |
// other.
|
|
315
|
+ |
let streamed = Arc::new(AtomicBool::new(false));
|
|
316
|
+ |
let saw = Arc::clone(&streamed);
|
|
317
|
+ |
let result = self
|
|
318
|
+ |
.inner
|
|
319
|
+ |
.execute_turn(prompt, move |chunk| {
|
|
320
|
+ |
if !chunk.is_empty() {
|
|
321
|
+ |
saw.store(true, Ordering::Relaxed);
|
|
322
|
+ |
send(&chunks, Control::Chunk(chunk.to_string()));
|
| 218 |
323
|
|
}
|
| 219 |
|
- |
}
|
|
324
|
+ |
})
|
|
325
|
+ |
.await;
|
| 220 |
326
|
|
|
| 221 |
|
- |
if !collected.is_empty() {
|
| 222 |
|
- |
self.history.push(Item::assistant_message(collected));
|
|
327
|
+ |
if let Some(model) = &self.inner.last_model {
|
|
328
|
+ |
send(&sink, Control::Model(model.clone()));
|
|
329
|
+ |
}
|
|
330
|
+ |
if self.inner.last_usage.reported() {
|
|
331
|
+ |
send(&sink, Control::Usage(self.inner.last_usage));
|
|
332
|
+ |
}
|
|
333
|
+ |
match result {
|
|
334
|
+ |
Ok(answer) => {
|
|
335
|
+ |
// The fallback for a path that answered without streaming, and
|
|
336
|
+ |
// nothing else: an empty answer stays empty rather than
|
|
337
|
+ |
// becoming a sentence somebody could read as a reply.
|
|
338
|
+ |
if !answer.is_empty() && !streamed.load(Ordering::Relaxed) {
|
|
339
|
+ |
send(&sink, Control::Chunk(answer));
|
|
340
|
+ |
}
|
| 223 |
341
|
|
}
|
| 224 |
|
- |
break;
|
|
342
|
+ |
Err(error) => send(&sink, Control::Failed(error.to_string())),
|
| 225 |
343
|
|
}
|
|
344
|
+ |
for failure in self.inner.record_failures.drain(..) {
|
|
345
|
+ |
send(&sink, Control::Notice(failure));
|
|
346
|
+ |
}
|
|
347
|
+ |
send(&sink, Control::Done);
|
|
348
|
+ |
}
|
| 226 |
349
|
|
|
| 227 |
|
- |
let _ = tx.send(Control::Done);
|
| 228 |
|
- |
Ok(())
|
|
350
|
+ |
/// Revoke this session's thread and say what the server billed.
|
|
351
|
+ |
///
|
|
352
|
+ |
/// Awaited by the caller rather than left to `Drop`: a thread left open
|
|
353
|
+ |
/// holds its grant's remaining budget, and the `Drop` backstop can only
|
|
354
|
+ |
/// spawn a `DELETE` this process may exit before polling.
|
|
355
|
+ |
pub async fn close(&mut self) -> Result<Option<String>, Failure> {
|
|
356
|
+ |
let spent = self.inner.close().await?;
|
|
357
|
+ |
Ok(self.inner.spend_line(spent))
|
| 229 |
358
|
|
}
|
|
359
|
+ |
}
|
|
360
|
+ |
|
|
361
|
+ |
/// The repository this session was opened in, as `owner/name`, when it is one.
|
|
362
|
+ |
///
|
|
363
|
+ |
/// Recorded on the thread so `oa coder --resume` has something to filter on. A
|
|
364
|
+ |
/// directory that is not an OpenAgents checkout has none, which is not an
|
|
365
|
+ |
/// error — the thread is simply not attributable to a repository.
|
|
366
|
+ |
fn repository() -> Option<String> {
|
|
367
|
+ |
let endpoint = openagents_cli::auth::resolve_endpoint(None, None).ok()?;
|
|
368
|
+ |
openagents_cli::repo::infer_repository(&endpoint.origin, None).ok()
|
|
369
|
+ |
}
|
|
370
|
+ |
|
|
371
|
+ |
/// The one-line header a tool call shows above its output box.
|
|
372
|
+ |
///
|
|
373
|
+ |
/// Built from the call's own arguments, so it says what was actually asked
|
|
374
|
+ |
/// for. A tool this does not know by name still gets a header rather than a
|
|
375
|
+ |
/// blank one, because a call with no header is a call the reader cannot see.
|
|
376
|
+ |
pub fn tool_title(name: &str, arguments: &str) -> String {
|
|
377
|
+ |
let parsed: serde_json::Value =
|
|
378
|
+ |
serde_json::from_str(arguments).unwrap_or(serde_json::Value::Null);
|
|
379
|
+ |
let string = |key: &str| {
|
|
380
|
+ |
parsed
|
|
381
|
+ |
.get(key)
|
|
382
|
+ |
.and_then(|v| v.as_str())
|
|
383
|
+ |
.map(str::trim)
|
|
384
|
+ |
.filter(|v| !v.is_empty())
|
|
385
|
+ |
.map(str::to_string)
|
|
386
|
+ |
};
|
| 230 |
387
|
|
|
| 231 |
|
- |
fn delegate_tool(&self) -> Option<Vec<Tool>> {
|
| 232 |
|
- |
if self.agents.is_empty() {
|
| 233 |
|
- |
return None;
|
|
388
|
+ |
let detail = match name {
|
|
389
|
+ |
"shell" => string("command"),
|
|
390
|
+ |
"skill" => string("name"),
|
|
391
|
+ |
"openagents" => parsed.get("args").and_then(|v| v.as_array()).map(|args| {
|
|
392
|
+ |
args.iter()
|
|
393
|
+ |
.filter_map(|v| v.as_str())
|
|
394
|
+ |
.collect::<Vec<_>>()
|
|
395
|
+ |
.join(" ")
|
|
396
|
+ |
}),
|
|
397
|
+ |
"capability" => string("name").or_else(|| string("query")),
|
|
398
|
+ |
"delegate" => {
|
|
399
|
+ |
let count = parsed
|
|
400
|
+ |
.get("count")
|
|
401
|
+ |
.and_then(|v| v.as_u64())
|
|
402
|
+ |
.filter(|n| *n > 1)
|
|
403
|
+ |
.map(|n| format!("×{n} "))
|
|
404
|
+ |
.unwrap_or_default();
|
|
405
|
+ |
string("prompt").map(|prompt| format!("{count}{prompt}"))
|
| 234 |
406
|
|
}
|
| 235 |
|
- |
let ids: Vec<String> = self.agents.iter().map(|a| a.id.clone()).collect();
|
| 236 |
|
- |
let tool = Tool::function("delegate")
|
| 237 |
|
- |
.with_description("Delegate a coding task to an ACP agent on this machine.")
|
| 238 |
|
- |
.with_parameters(serde_json::json!({
|
| 239 |
|
- |
"type": "object",
|
| 240 |
|
- |
"properties": {
|
| 241 |
|
- |
"agent": {
|
| 242 |
|
- |
"type": "string",
|
| 243 |
|
- |
"enum": ids,
|
| 244 |
|
- |
"description": "the ACP agent to delegate to"
|
| 245 |
|
- |
},
|
| 246 |
|
- |
"prompt": {
|
| 247 |
|
- |
"type": "string",
|
| 248 |
|
- |
"description": "the task for the child agent"
|
| 249 |
|
- |
}
|
| 250 |
|
- |
},
|
| 251 |
|
- |
"required": ["agent", "prompt"]
|
| 252 |
|
- |
}));
|
| 253 |
|
- |
Some(vec![tool])
|
|
407
|
+ |
// A plugin loaded through `capability` declares a tool under its own
|
|
408
|
+ |
// name and over its own schema, so there is nothing general to read
|
|
409
|
+ |
// out of it but the arguments themselves.
|
|
410
|
+ |
_ => (parsed != serde_json::Value::Null).then(|| parsed.to_string()),
|
|
411
|
+ |
};
|
|
412
|
+ |
|
|
413
|
+ |
match detail {
|
|
414
|
+ |
Some(detail) => format!("{name} {}", one_line(&detail)),
|
|
415
|
+ |
None => name.to_string(),
|
|
416
|
+ |
}
|
|
417
|
+ |
}
|
|
418
|
+ |
|
|
419
|
+ |
/// The first line of `text`, marked when there was more.
|
|
420
|
+ |
fn one_line(text: &str) -> String {
|
|
421
|
+ |
let first = text.lines().next().unwrap_or_default().trim();
|
|
422
|
+ |
if text.lines().nth(1).is_some() {
|
|
423
|
+ |
format!("{first} …")
|
|
424
|
+ |
} else {
|
|
425
|
+ |
first.to_string()
|
|
426
|
+ |
}
|
|
427
|
+ |
}
|
|
428
|
+ |
|
|
429
|
+ |
#[cfg(test)]
|
|
430
|
+ |
mod tests {
|
|
431
|
+ |
use super::*;
|
|
432
|
+ |
|
|
433
|
+ |
/// The prompt is the product. A merge that reworded it would pass every
|
|
434
|
+ |
/// other test in this crate.
|
|
435
|
+ |
#[test]
|
|
436
|
+ |
fn the_system_prompt_opens_with_the_terse_instructions_unchanged() {
|
|
437
|
+ |
let prompt = system_prompt(&[]);
|
|
438
|
+ |
assert!(
|
|
439
|
+ |
prompt.starts_with(SYSTEM_INSTRUCTIONS),
|
|
440
|
+ |
"the instructions were not carried verbatim: {prompt}"
|
|
441
|
+ |
);
|
|
442
|
+ |
assert!(SYSTEM_INSTRUCTIONS.contains("no greetings"));
|
|
443
|
+ |
assert!(SYSTEM_INSTRUCTIONS.contains("no unnecessary padding"));
|
|
444
|
+ |
}
|
|
445
|
+ |
|
|
446
|
+ |
/// A model told it has tools it does not have will claim to have run them.
|
|
447
|
+ |
#[test]
|
|
448
|
+ |
fn the_prompt_names_every_declared_tool_and_claims_no_others() {
|
|
449
|
+ |
let tools = vec![
|
|
450
|
+ |
ToolDefinition {
|
|
451
|
+ |
name: "shell".to_string(),
|
|
452
|
+ |
description: String::new(),
|
|
453
|
+ |
parameters: serde_json::json!({}),
|
|
454
|
+ |
},
|
|
455
|
+ |
ToolDefinition {
|
|
456
|
+ |
name: "skill".to_string(),
|
|
457
|
+ |
description: String::new(),
|
|
458
|
+ |
parameters: serde_json::json!({}),
|
|
459
|
+ |
},
|
|
460
|
+ |
];
|
|
461
|
+ |
let prompt = system_prompt(&tools);
|
|
462
|
+ |
assert!(prompt.contains("You have 2 tools, and no others:"), "{prompt}");
|
|
463
|
+ |
assert!(prompt.contains("- `shell`"), "{prompt}");
|
|
464
|
+ |
assert!(prompt.contains("- `skill`"), "{prompt}");
|
|
465
|
+ |
|
|
466
|
+ |
let none = system_prompt(&[]);
|
|
467
|
+ |
assert!(none.contains("You have no tools in this session"), "{none}");
|
|
468
|
+ |
}
|
|
469
|
+ |
|
|
470
|
+ |
#[test]
|
|
471
|
+ |
fn a_tool_header_says_what_the_call_asked_for() {
|
|
472
|
+ |
assert_eq!(
|
|
473
|
+ |
tool_title("shell", r#"{"command":"cargo test -p coder-lite"}"#),
|
|
474
|
+ |
"shell cargo test -p coder-lite"
|
|
475
|
+ |
);
|
|
476
|
+ |
assert_eq!(tool_title("skill", r#"{"name":"effect"}"#), "skill effect");
|
|
477
|
+ |
assert_eq!(
|
|
478
|
+ |
tool_title("openagents", r#"{"args":["issue","list"]}"#),
|
|
479
|
+ |
"openagents issue list"
|
|
480
|
+ |
);
|
|
481
|
+ |
assert_eq!(
|
|
482
|
+ |
tool_title("delegate", r#"{"prompt":"read it","count":3}"#),
|
|
483
|
+ |
"delegate ×3 read it"
|
|
484
|
+ |
);
|
|
485
|
+ |
// A multi-line command is one line on the header, and says so.
|
|
486
|
+ |
assert_eq!(
|
|
487
|
+ |
tool_title("shell", "{\"command\":\"one\\ntwo\"}"),
|
|
488
|
+ |
"shell one …"
|
|
489
|
+ |
);
|
|
490
|
+ |
// Arguments that will not parse are not a reason to draw no header.
|
|
491
|
+ |
assert_eq!(tool_title("shell", "not json"), "shell");
|
| 254 |
492
|
|
}
|
| 255 |
493
|
|
}
|