Repository navigation
Expand file tree
/
Copy pathagent_cli.rs
More file actions
532 lines (500 loc) · 19.3 KB
/
Copy pathagent_cli.rs
File metadata and controls
532 lines (500 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! `openhuman agent` — developer CLI for inspecting agent definitions and
//! the system prompts the context engine produces for them.
//!
//! This is intentionally scoped to *debugging*: no execution, no provider
//! calls, no server boot. Every subcommand boils down to reading config /
//! agent definitions / tool registry and printing something.
//!
//! Usage:
//! openhuman agent dump-prompt --agent <id> [--toolkit <slug>] [--workspace <path>] [--json] [--with-tools] [-v]
//! (--toolkit is REQUIRED when --agent is `integrations_agent`.)
//! openhuman agent dump-all --out <dir> [--workspace <path>] [--model <name>] [-v]
//! openhuman agent list [--json] [-v]
//!
//! `dump-prompt` is the main tool: it renders the exact system prompt the
//! context engine would hand to the LLM when that agent is spawned. The
//! dump routes through [`Agent::from_config_for_agent`] and calls
//! [`Agent::build_system_prompt`] on the live session, so the output is
//! byte-identical to what the LLM sees on turn 1. Pass
//! `--agent orchestrator` for the orchestrator prompt; otherwise pass
//! any built-in or workspace-custom agent id (e.g. `integrations_agent`,
//! `welcome`, `code_executor`).
use anyhow::{anyhow, Result};
use std::path::PathBuf;
use crate::openhuman::agent::debug::{
dump_agent_prompt, dump_all_agent_prompts, write_prompt_dumps, DumpPromptOptions, DumpedPrompt,
};
use crate::openhuman::agent::harness::definition::AgentDefinitionRegistry;
/// Entry point for `openhuman agent <subcommand>`.
pub fn run_agent_command(args: &[String]) -> Result<()> {
if args.is_empty() || is_help(&args[0]) {
print_agent_help();
return Ok(());
}
match args[0].as_str() {
"dump-prompt" => run_dump_prompt(&args[1..]),
"dump-all" => run_dump_all(&args[1..]),
"list" => run_list(&args[1..]),
other => Err(anyhow!(
"unknown agent subcommand '{other}'. Run `openhuman agent --help`."
)),
}
}
// ---------------------------------------------------------------------------
// dump-all
// ---------------------------------------------------------------------------
struct DumpAllFlags {
out: PathBuf,
workspace: Option<PathBuf>,
model: Option<String>,
verbose: bool,
}
fn parse_dump_all_flags(args: &[String]) -> Result<DumpAllFlags> {
let mut out: Option<PathBuf> = None;
let mut workspace: Option<PathBuf> = None;
let mut model: Option<String> = None;
let mut verbose = false;
let mut i = 0usize;
while i < args.len() {
match args[i].as_str() {
"--out" | "-o" => {
out = Some(PathBuf::from(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --out"))?,
));
i += 2;
}
"--workspace" | "-w" => {
workspace = Some(PathBuf::from(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --workspace"))?,
));
i += 2;
}
"--model" | "-m" => {
model = Some(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --model"))?
.clone(),
);
i += 2;
}
"-v" | "--verbose" => {
verbose = true;
i += 1;
}
"-h" | "--help" => {
println!("Usage: openhuman agent dump-all --out <dir> [--workspace <path>] [--model <name>] [-v]");
println!();
println!("Render every registered agent's turn-1 system prompt into <dir>.");
println!("`integrations_agent` is expanded into one file per currently-connected");
println!("Composio toolkit; if no toolkit is connected, it is skipped.");
std::process::exit(0);
}
other => return Err(anyhow!("unknown dump-all arg: {other}")),
}
}
Ok(DumpAllFlags {
out: out.ok_or_else(|| anyhow!("--out <dir> is required"))?,
workspace,
model,
verbose,
})
}
fn run_dump_all(args: &[String]) -> Result<()> {
let flags = parse_dump_all_flags(args)?;
init_quiet_logging(flags.verbose);
log::debug!(
"[agent-cli] run_dump_all entry: out={} workspace={:?} model={:?}",
flags.out.display(),
flags.workspace,
flags.model
);
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_stack_size(crate::core::runtime::AGENT_WORKER_STACK_BYTES)
.max_blocking_threads(crate::core::runtime::MAX_BLOCKING_THREADS)
.build()?;
log::debug!("[agent-cli] run_dump_all: calling dump_all_agent_prompts");
let dumps = rt.block_on(async {
dump_all_agent_prompts(flags.workspace.clone(), flags.model.clone()).await
})?;
log::debug!(
"[agent-cli] run_dump_all: dump_all_agent_prompts returned {} prompt(s)",
dumps.len()
);
write_prompt_dumps(&flags.out, &dumps)?;
log::debug!(
"[agent-cli] run_dump_all exit: wrote {} prompt(s) + SUMMARY.txt",
dumps.len()
);
Ok(())
}
// ---------------------------------------------------------------------------
// dump-prompt
// ---------------------------------------------------------------------------
struct DumpFlags {
agent: Option<String>,
toolkit: Option<String>,
workspace: Option<PathBuf>,
model: Option<String>,
json: bool,
with_tools: bool,
verbose: bool,
}
fn parse_dump_flags(args: &[String]) -> Result<DumpFlags> {
let mut out = DumpFlags {
agent: None,
toolkit: None,
workspace: None,
model: None,
json: false,
with_tools: false,
verbose: false,
};
let mut i = 0usize;
while i < args.len() {
match args[i].as_str() {
"--agent" | "-a" => {
out.agent = Some(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --agent"))?
.clone(),
);
i += 2;
}
"--toolkit" | "-t" => {
out.toolkit = Some(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --toolkit"))?
.clone(),
);
i += 2;
}
"--workspace" | "-w" => {
out.workspace = Some(PathBuf::from(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --workspace"))?,
));
i += 2;
}
"--model" | "-m" => {
out.model = Some(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --model"))?
.clone(),
);
i += 2;
}
"--json" => {
out.json = true;
i += 1;
}
"--with-tools" => {
out.with_tools = true;
i += 1;
8000
}
"-v" | "--verbose" => {
out.verbose = true;
i += 1;
}
"-h" | "--help" => {
print_dump_prompt_help();
std::process::exit(0);
}
other => return Err(anyhow!("unknown dump-prompt arg: {other}")),
}
let _ = i; // silence unused-warning in the `help` branch
}
Ok(out)
}
fn run_dump_prompt(args: &[String]) -> Result<()> {
let flags = parse_dump_flags(args)?;
let agent = flags.agent.clone().ok_or_else(|| {
anyhow!("--agent <id> is required (e.g. `orchestrator`, `integrations_agent`, `welcome`)")
})?;
if agent == "integrations_agent" && flags.toolkit.is_none() {
return Err(anyhow!(
"--toolkit <slug> is required when --agent is `integrations_agent` \
(e.g. `--toolkit gmail`). Run `composio list_connection` to see active slugs."
));
}
init_quiet_logging(flags.verbose);
log::debug!(
"[agent-cli] run_dump_prompt entry: agent={} toolkit={:?} workspace={:?} model={:?}",
agent,
flags.toolkit,
flags.workspace,
flags.model
);
let options = DumpPromptOptions {
agent_id: agent,
toolkit: flags.toolkit.clone(),
workspace_dir_override: flags.workspace.clone(),
model_override: flags.model.clone(),
};
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_stack_size(crate::core::runtime::AGENT_WORKER_STACK_BYTES)
.max_blocking_threads(crate::core::runtime::MAX_BLOCKING_THREADS)
.build()?;
log::debug!("[agent-cli] run_dump_prompt: calling dump_agent_prompt");
let dumped = rt.block_on(async { dump_agent_prompt(options).await })?;
log::debug!(
"[agent-cli] run_dump_prompt: dump returned (tools={}, skill_tools={}, prompt_bytes={})",
dumped.tool_names.len(),
dumped.skill_tool_count,
dumped.text.len()
);
if flags.json {
print_json(&dumped, flags.with_tools)?;
} else {
print_human(&dumped, flags.with_tools);
}
Ok(())
}
fn print_human(dumped: &DumpedPrompt, with_tools: bool) {
// Banner on stderr so `openhuman agent dump-prompt ... > file.md` stays
// clean — stdout is the prompt, stderr is the metadata. This matches
// the pattern already used by `run_call_command` / `run_server_command`
// in `core/cli.rs` (banner to stderr, JSON result to stdout).
eprintln!("# Agent prompt dump");
eprintln!("agent: {}", dumped.agent_id);
if let Some(tk) = &dumped.toolkit {
eprintln!("toolkit: {tk}");
}
eprintln!("mode: {}", dumped.mode);
eprintln!("model: {}", dumped.model);
eprintln!("workspace: {}", dumped.workspace_dir.display());
eprintln!("tool_count: {}", dumped.tool_names.len());
eprintln!("skill_tools: {}", dumped.skill_tool_count);
if with_tools {
eprintln!("tools:");
for name in &dumped.tool_names {
eprintln!(" - {name}");
}
}
eprintln!();
eprintln!("─── BEGIN SYSTEM PROMPT ───");
println!("{}", dumped.text);
eprintln!("─── END SYSTEM PROMPT ───");
}
fn print_json(dumped: &DumpedPrompt, with_tools: bool) -> Result<()> {
// Use a plain serde_json::Value so we don't need to add Serialize to
// DumpedPrompt (which would pull the agent harness types into our
// serde surface). This output is stable and scriptable from bash.
let mut obj = serde_json::Map::new();
obj.insert(
"agent_id".into(),
serde_json::Value::String(dumped.agent_id.clone()),
);
obj.insert(
"toolkit".into(),
match &dumped.toolkit {
Some(tk) => serde_json::Value::String(tk.clone()),
None => serde_json::Value::Null,
},
);
obj.insert(
"mode".into(),
serde_json::Value::String(dumped.mode.to_string()),
);
obj.insert(
"model".into(),
serde_json::Value::String(dumped.model.clone()),
);
obj.insert(
"workspace_dir".into(),
serde_json::Value::String(dumped.workspace_dir.display().to_string()),
);
obj.insert(
"tool_count".into(),
serde_json::Value::Number(dumped.tool_names.len().into()),
);
obj.insert(
"skill_tool_count".into(),
serde_json::Value::Number(dumped.skill_tool_count.into()),
);
obj.insert(
"system_prompt".into(),
serde_json::Value::String(dumped.text.clone()),
);
if with_tools {
obj.insert(
"tools".into(),
serde_json::Value::Array(
dumped
.tool_names
.iter()
.cloned()
.map(serde_json::Value::String)
.collect(),
),
);
// The schemas, not just the names: they ride alongside the prompt on
// every request and for a few-hundred-tool agent they are the larger
// half of the fixed cost. Emitting only `tools` measures the smaller.
obj.insert(
"tool_specs".into(),
serde_json::Value::Array(dumped.tool_specs.clone()),
);
}
println!(
"{}",
serde_json::to_string_pretty(&serde_json::Value::Object(obj))?
);
Ok(())
}
// ---------------------------------------------------------------------------
// list
// ---------------------------------------------------------------------------
fn run_list(args: &[String]) -> Result<()> {
let mut as_json = false;
let mut workspace: Option<PathBuf> = None;
let mut verbose = false;
let mut i = 0usize;
while i < args.len() {
match args[i].as_str() {
"--json" => {
as_json = true;
i += 1;
}
"--workspace" | "-w" => {
workspace = Some(PathBuf::from(
args.get(i + 1)
.ok_or_else(|| anyhow!("missing value for --workspace"))?,
));
i += 2;
}
"-v" | "--verbose" => {
verbose = true;
i += 1;
}
"-h" | "--help" => {
println!("Usage: openhuman agent list [--workspace <path>] [--json] [-v]");
println!();
println!(" List every built-in agent plus any custom `<workspace>/agents/*.toml` overrides.");
return Ok(());
}
other => return Err(anyhow!("unknown list arg: {other}")),
}
}
// Silence the logger so Config::load_or_init and AgentDefinitionRegistry::load
// don't write warnings/info to stdout, which would corrupt --json output.
// (The project's CLI logger writes to stdout, not stderr.)
init_quiet_logging(verbose);
// Resolve workspace-custom overrides the same way the runtime does
// at spawn time. When --workspace is explicit we load against it
// directly; otherwise the registry helper does the Config dance.
let registry = if let Some(ws) = workspace {
AgentDefinitionRegistry::load(&ws)?
} else {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
rt.block_on(AgentDefinitionRegistry::load_for_default_workspace())?
};
if as_json {
let mut arr = Vec::new();
for def in registry.list() {
let mut obj = serde_json::Map::new();
obj.insert("id".into(), serde_json::Value::String(def.id.clone()));
obj.insert(
"display_name".into(),
serde_json::Value::String(def.display_name().to_string()),
);
obj.insert(
"when_to_use".into(),
serde_json::Value::String(def.when_to_use.clone()),
);
obj.insert(
"omit_safety_preamble".into(),
serde_json::Value::Bool(def.omit_safety_preamble),
);
obj.insert(
"omit_identity".into(),
serde_json::Value::Bool(def.omit_identity),
);
obj.insert(
"omit_skills_catalog".into(),
serde_json::Value::Bool(def.omit_skills_catalog),
);
arr.push(serde_json::Value::Object(obj));
}
println!(
"{}",
serde_json::to_string_pretty(&serde_json::Value::Array(arr))?
);
} else {
println!("{:<20} WHEN TO USE", "ID");
println!("{}", "-".repeat(90));
for def in registry.list() {
let when = def.when_to_use.chars().take(68).collect::<String>();
println!("{:<20} {}", def.id, when);
}
println!();
println!("{} agent(s) registered.", registry.len());
}
Ok(())
}
// ---------------------------------------------------------------------------
// Help
// ---------------------------------------------------------------------------
fn print_agent_help() {
println!("openhuman agent — inspect agents and the prompts they receive");
println!();
println!("Usage:");
println!(" openhuman agent list [--workspace <path>] [--json]");
println!(" openhuman agent dump-prompt --agent <id> [--workspace <path>] [--model <name>] [--with-tools] [--json] [-v]");
println!(" openhuman agent dump-all --out <dir> [--workspace <path>] [--model <name>] [-v]");
println!();
println!("Run `openhuman agent <subcommand> --help` for details.");
}
fn print_dump_prompt_help() {
println!("openhuman agent dump-prompt — render the exact system prompt an agent receives");
println!();
println!("Usage:");
println!(" openhuman agent dump-prompt --agent <id> [options]");
println!();
println!("Required:");
println!(" --agent, -a <id> Target agent id — any built-in or workspace-custom id");
println!(" (e.g. `orchestrator`, `integrations_agent`, `welcome`).");
println!();
println!("Options:");
println!(" --toolkit, -t <slug> REQUIRED when `--agent integrations_agent`. Names the");
println!(" Composio toolkit to bind this dump to (e.g. `gmail`,");
println!(" `notion`). Must match a currently-connected integration —");
println!(" run `composio list_connection` to see the active slugs.");
println!(" --workspace, -w <p> Override the workspace directory (defaults to");
println!(" Config::workspace_dir / ~/.openhuman/workspace).");
println!(" --model, -m <name> Override the resolved model name (affects only the");
println!(" `## Runtime` section).");
println!(" --with-tools Also print the full list of tool names the agent sees.");
println!(" --json Emit a machine-readable JSON object on stdout.");
println!(" -v, --verbose Enable debug logging on stderr.");
println!();
println!("Examples:");
println!(" # Orchestrator prompt, JSON for scripting.");
println!(" openhuman agent dump-prompt --agent orchestrator --json");
println!();
println!(" # integrations_agent bound to the user's gmail connection.");
println!(
" openhuman agent dump-prompt --agent integrations_agent --toolkit gmail --with-tools"
);
}
fn is_help(value: &str) -> bool {
matches!(value, "-h" | "--help" | "help")
}
/// Quiet logging: only `error` unless verbose. We pin this lower than
/// `warn` (the default in `skills_cli::init_quiet_logging`) because
/// `agent dump-prompt` is designed to be redirected into a file, and
/// expected warnings like `[integrations] no auth token available …`
/// would otherwise interleave with the rendered prompt body on stdout
/// (the project's CLI logger writes to stdout, not stderr). Verbose
/// users can opt back in with `-v` or `RUST_LOG=…`.
fn init_quiet_logging(verbose: bool) {
if !verbose && std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "error");
}
crate::core::logging::init_for_cli_run(verbose, crate::core::logging::CliLogDefault::Global);
}