-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.ts
More file actions
448 lines (409 loc) · 17.6 KB
/
Copy pathprogram.ts
File metadata and controls
448 lines (409 loc) · 17.6 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
119
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
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { Command, Option } from "commander";
import { applyCommand } from "./commands/apply.ts";
import {
deploymentGetCommand,
deploymentListCommand,
deploymentPauseCommand,
deploymentRunCommand,
} from "./commands/deployment.ts";
import { destroyCommand } from "./commands/destroy.ts";
import { initCommand } from "./commands/init.ts";
import {
memoryBatchCreateCommand,
memoryCreateCommand,
memoryDeleteCommand,
memoryGetCommand,
memoryListCommand,
memoryStoreArchiveCommand,
memoryStoreCreateCommand,
memoryStoreDeleteCommand,
memoryStoreGetCommand,
memoryStoreListCommand,
memoryStoreUpdateCommand,
memoryUpdateCommand,
memoryVersionGetCommand,
memoryVersionListCommand,
memoryVersionRedactCommand,
} from "./commands/memory.ts";
import { migrateCommand } from "./commands/migrate.ts";
import { modelsListCommand } from "./commands/models.ts";
import { planCommand } from "./commands/plan.ts";
import { playgroundCommand } from "./commands/playground.ts";
import {
sessionCreateCommand,
sessionDeleteCommand,
sessionEventsCommand,
sessionGetCommand,
sessionListCommand,
sessionRunCommand,
sessionSendCommand,
} from "./commands/session.ts";
import { stateImportCommand, stateListCommand, stateRemoveCommand, stateShowCommand } from "./commands/state.ts";
import { syncCommand } from "./commands/sync.ts";
import { validateCommand } from "./commands/validate.ts";
import { configureLogger } from "./logger.ts";
import {
configFileOption,
DEFAULT_CONFIG_FILE,
parseBooleanOption,
parsePositiveInteger,
providerOption,
withResolvedConfigFile,
} from "./runtime.ts";
function formatCliError(message: string, args = process.argv.slice(2)): string {
const trimmed = message.trimEnd();
if (trimmed.startsWith("error: unknown option")) {
return `${trimmed}\n\nRun \`agents --help\` for available commands, or \`agents <command> --help\` for command options.\n`;
}
if (trimmed.includes("missing required argument")) {
const cmd = args.filter((a) => !a.startsWith("-")).join(" ");
const examples: Record<string, string> = {
"session run": 'agents session run "your prompt here" -f agents.yaml',
"session send": 'agents session send <session-id> "your message" -f agents.yaml',
"session get": "agents session get <session-id> -f agents.yaml",
"session delete": "agents session delete <session-id> -f agents.yaml",
};
const example = Object.entries(examples).find(([k]) => cmd.startsWith(k));
if (example) {
return `${trimmed}\n\nExample:\n ${example[1]}\n`;
}
return `${trimmed}\n\nRun \`agents ${cmd} --help\` for usage details.\n`;
}
return `${message}`;
}
function readCliVersion(): string {
const packageJsonPath = resolve(dirname(fileURLToPath(import.meta.url)), "../package.json");
const manifest = JSON.parse(readFileSync(packageJsonPath, "utf8")) as {
version?: string;
};
return manifest.version ?? "0.0.0-dev";
}
function countVerbose(_value: string, previous: number): number {
return previous + 1;
}
export const program = new Command()
.name("agents")
.version(readCliVersion())
.description("Open Agent Pack — Declaratively manage AI agent infrastructure")
.option("-v, --verbose", "Increase logging verbosity (repeat: -vv)", countVerbose, 0)
.option("-q, --quiet", "Suppress non-error output")
.option("--no-color", "Disable colored output")
.addOption(configFileOption().default(DEFAULT_CONFIG_FILE))
.configureOutput({
outputError: (message, write) => write(formatCliError(message)),
})
.hook("preAction", (cmd) => {
const opts = cmd.opts();
configureLogger({
verbose: typeof opts.verbose === "number" ? opts.verbose : opts.verbose ? 1 : 0,
quiet: !!opts.quiet,
color: opts.color !== false,
});
});
program.command("init").description("Create a new agents.yaml template").action(initCommand);
program
.command("playground")
.description("Launch the local web UI (fetches @openagentpack/playground on demand) and open it in a browser")
.option("--port <n>", "Port to serve on (default 4848)")
.addOption(providerOption("Provider the UI targets"))
.option("--no-open", "Do not open a browser automatically")
.action(playgroundCommand);
program
.command("validate")
.description("Validate the configuration file (offline)")
.addOption(configFileOption())
.action(withResolvedConfigFile(validateCommand));
program
.command("plan")
.description("Show what changes would be applied")
.addOption(configFileOption())
.addOption(providerOption("Target provider", { allowAll: true, defaultValue: "all" }))
.option("--refresh <value>", "Refresh state from remote before planning (true/false)", parseBooleanOption, true)
.option("--refresh-only", "Refresh state and show drift without planning remote mutations")
.option("--json", "Output as JSON")
.action(withResolvedConfigFile(planCommand));
program
.command("apply")
.description("Apply the planned changes to create/update/delete resources")
.addOption(configFileOption())
.option("-y, --yes", "Skip confirmation prompt")
.option("--refresh <value>", "Refresh state from remote before planning (true/false)", parseBooleanOption, true)
.option("--refresh-only", "Refresh state without mutating remote resources")
.option(
"--concurrency <n>",
"Max independent resources to apply in parallel (default 6, max 10)",
parsePositiveInteger,
)
.addOption(providerOption("Target provider", { allowAll: true, defaultValue: "all" }))
.action(withResolvedConfigFile(applyCommand));
program
.command("destroy")
.description("Destroy all managed resources")
.addOption(configFileOption())
.option("-y, --yes", "Skip confirmation prompt")
.option("--cascade", "Auto-delete dependent resources (e.g., sessions referencing an environment)")
.action(withResolvedConfigFile(destroyCommand));
program
.command("sync")
.description("Export a provider's remote configuration into a local agents.yaml")
.addOption(configFileOption())
.addOption(providerOption("Source provider to sync from (defaults from config when -f is set)"))
.option("-o, --out <path>", "Output file path", "agents.synced.yaml")
.option("--force", "Overwrite the output file if it already exists")
.option("--skip-missing-files", "Do not prompt for remote files that cannot be downloaded; omit them from output")
.action(withResolvedConfigFile(syncCommand));
program
.command("migrate")
.description("Merge synced resources into the project agents.yaml (incremental, skip existing)")
.option("--from <path>", "Source synced file", "agents.synced.yaml")
.option("--to <path>", "Target agents.yaml file", "agents.yaml")
.action(migrateCommand);
const stateCmd = program.command("state").description("Manage state file");
stateCmd
.command("list")
.description("List all resources in state")
.addOption(configFileOption())
.action(withResolvedConfigFile(stateListCommand));
stateCmd
.command("show <address>")
.description("Show details of a resource in state")
.addOption(configFileOption())
.action(withResolvedConfigFile(stateShowCommand));
stateCmd
.command("rm <address>")
.description("Remove a resource from state without destroying it remotely")
.addOption(configFileOption())
.action(withResolvedConfigFile(stateRemoveCommand));
stateCmd
.command("import <address> <remote-id>")
.description("Import an existing remote resource into state")
.addOption(configFileOption())
.addOption(
new Option("--resource-version <number>", "Resource version (for versioned resources like agents)").argParser(
parsePositiveInteger,
),
)
.action(withResolvedConfigFile(stateImportCommand));
const sessionCmd = program.command("session").description("Manage agent sessions (runtime)");
sessionCmd
.command("create [agent-name]")
.description("Create a new session for an agent")
.addOption(configFileOption())
.option("--agent <name>", "Agent name (auto-detected when only one agent is configured)")
.option("--identity-id <id>", "Override the configured Qoder Forward Identity")
.option("--environment <name>", "Override agent's declared environment")
.option("--environment-id <id>", "Use an explicit remote environment id instead of the configured one")
.option("--tunnel <name>", "Override agent's declared tunnel")
.option("--tunnel-id <id>", "Use an explicit remote tunnel id instead of the configured one")
.option("--vault <name>", "Override agent's declared vault")
.option("--memory-stores <names>", "Override agent's declared memory stores (comma-separated)")
.option("--title <title>", "Session title")
.addOption(providerOption("Target provider (required for multi-provider agents)"))
.action(withResolvedConfigFile(sessionCreateCommand));
sessionCmd
.command("list")
.description("List sessions from the provider")
.addOption(configFileOption())
.option("--agent <name>", "Filter by agent name")
.option("--all", "Fetch all pages by following the cursor")
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(sessionListCommand));
sessionCmd
.command("get <session-id>")
.description("Get details of a session")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(sessionGetCommand));
sessionCmd
.command("delete <session-id>")
.description("Delete a session")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(sessionDeleteCommand));
sessionCmd
.command("run <prompt-or-agent> [prompt]")
.description("Create a session, send a message, and wait for the response")
.addOption(configFileOption())
.option("--agent <name>", "Agent name (auto-detected when only one agent is configured)")
.option("--identity-id <id>", "Override the configured Qoder Forward Identity")
.option("--environment <name>", "Override agent's declared environment")
.option("--environment-id <id>", "Use an explicit remote environment id instead of the configured one")
.option("--tunnel <name>", "Override agent's declared tunnel")
.option("--tunnel-id <id>", "Use an explicit remote tunnel id instead of the configured one")
.option("--vault <name>", "Override agent's declared vault")
.option("--memory-stores <names>", "Override agent's declared memory stores (comma-separated)")
.option("--title <title>", "Session title")
.addOption(providerOption("Target provider"))
.option("--json", "Output events as JSONL")
.addOption(new Option("--stream", "Stream events over SSE instead of polling").conflicts("noStream"))
.addOption(new Option("--no-stream", "Use polling (deprecated; polling is now the default)").hideHelp())
.action(withResolvedConfigFile(sessionRunCommand));
sessionCmd
.command("send <session-id> <message>")
.description("Send a message to an existing session and wait for the response")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--json", "Output events as JSONL")
.addOption(new Option("--stream", "Stream events over SSE instead of polling").conflicts("noStream"))
.addOption(new Option("--no-stream", "Use polling (deprecated; polling is now the default)").hideHelp())
.action(withResolvedConfigFile(sessionSendCommand));
sessionCmd
.command("events <session-id>")
.description("List event history for a session")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.addOption(new Option("--limit <count>", "Maximum number of events to fetch").argParser(parsePositiveInteger))
.option("--all", "Fetch all pages by following the cursor")
.option("--json", "Output as JSON")
.action(withResolvedConfigFile(sessionEventsCommand));
const deploymentCmd = program
.command("deployment")
.description("Manage agent deployments (scheduled / triggered runs)");
deploymentCmd
.command("list")
.description("List deployments tracked in state")
.addOption(configFileOption())
.addOption(providerOption("Filter by provider"))
.option("--remote", "List deployments from the provider API")
.addOption(new Option("--status <status>", "Filter remote deployments by status").choices(["active", "paused"]))
.option("--include-archived", "Include archived remote deployments")
.option("--agent-id <id>", "Filter remote deployments by agent ID")
.option("--limit <count>", "Maximum remote deployments per page", parsePositiveInteger)
.option("--all", "Fetch all remote pages")
.action(withResolvedConfigFile(deploymentListCommand));
deploymentCmd
.command("get <name>")
.description("Show a deployment's status and resolved bindings")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(deploymentGetCommand));
deploymentCmd
.command("pause <name>")
.description("Pause a native deployment's scheduled runs")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile((name, options) => deploymentPauseCommand(name, options, true)));
deploymentCmd
.command("unpause <name>")
.description("Resume a paused native deployment")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile((name, options) => deploymentPauseCommand(name, options, false)));
deploymentCmd
.command("run <name>")
.description("Trigger a deployment run (native on Bailian/Qoder/Claude, emulated on Ark)")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(deploymentRunCommand));
const memoryStoreCmd = program.command("memory-store").description("Manage persistent memory stores");
memoryStoreCmd
.command("create <name>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--description <description>")
.action(withResolvedConfigFile(memoryStoreCreateCommand));
memoryStoreCmd
.command("list")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--limit <n>", "Page size", parsePositiveInteger)
.option("--cursor <cursor>")
.option("--include-archived")
.action(withResolvedConfigFile(memoryStoreListCommand));
memoryStoreCmd
.command("get <store-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(memoryStoreGetCommand));
memoryStoreCmd
.command("update <store-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--name <name>")
.option("--description <description>")
.action(withResolvedConfigFile(memoryStoreUpdateCommand));
memoryStoreCmd
.command("archive <store-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(memoryStoreArchiveCommand));
memoryStoreCmd
.command("delete <store-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(memoryStoreDeleteCommand));
const memoryCmd = program.command("memory").description("Manage memories inside a store");
memoryCmd
.command("create <store-id> <path>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--content <text>")
.option("--content-file <path>")
.action(withResolvedConfigFile(memoryCreateCommand));
memoryCmd
.command("batch-create <store-id> <json-file>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.addOption(new Option("--on-conflict <mode>", "Conflict handling (Ark)").choices(["overwrite", "fail"]))
.action(withResolvedConfigFile(memoryBatchCreateCommand));
memoryCmd
.command("list <store-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--limit <n>", "Page size", parsePositiveInteger)
.option("--cursor <cursor>")
.option("--prefix <path>")
.option("--depth <n>", "Hierarchy depth", parsePositiveInteger)
.option("--full", "Include content")
.action(withResolvedConfigFile(memoryListCommand));
memoryCmd
.command("get <store-id> <memory-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(memoryGetCommand));
memoryCmd
.command("update <store-id> <memory-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--path <path>")
.option("--content <text>")
.option("--content-file <path>")
.option("--expected-sha256 <sha256>", "Optimistic concurrency precondition")
.action(withResolvedConfigFile(memoryUpdateCommand));
memoryCmd
.command("delete <store-id> <memory-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--expected-sha256 <sha256>", "Optimistic concurrency precondition")
.action(withResolvedConfigFile(memoryDeleteCommand));
const memoryVersionCmd = memoryCmd.command("version").description("Inspect immutable memory history");
memoryVersionCmd
.command("list <store-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--limit <n>", "Page size", parsePositiveInteger)
.option("--cursor <cursor>")
.option("--memory-id <id>")
.option("--full", "Include version content")
.action(withResolvedConfigFile(memoryVersionListCommand));
memoryVersionCmd
.command("get <store-id> <version-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(memoryVersionGetCommand));
memoryVersionCmd
.command("redact <store-id> <version-id>")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.action(withResolvedConfigFile(memoryVersionRedactCommand));
const modelsCmd = program.command("models").description("Discover available models from providers");
modelsCmd
.command("list")
.description("List models available on the configured provider(s)")
.addOption(configFileOption())
.addOption(providerOption("Target provider"))
.option("--json", "Output as JSON")
.action(withResolvedConfigFile(modelsListCommand));