@b3os/cli 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -7
- package/assets/b3os-run.py +137 -0
- package/assets/skill/b3os/SKILL.md +111 -0
- package/dist/commands/_agents.d.ts +14 -0
- package/dist/commands/_agents.js +33 -0
- package/dist/commands/_agents.js.map +1 -0
- package/dist/commands/_common.js +6 -2
- package/dist/commands/_common.js.map +1 -1
- package/dist/commands/_context.d.ts +49 -0
- package/dist/commands/_context.js +260 -0
- package/dist/commands/_context.js.map +1 -0
- package/dist/commands/_next.d.ts +39 -0
- package/dist/commands/_next.js +77 -0
- package/dist/commands/_next.js.map +1 -0
- package/dist/commands/agent.d.ts +42 -0
- package/dist/commands/agent.js +218 -0
- package/dist/commands/agent.js.map +1 -0
- package/dist/commands/app.d.ts +2 -0
- package/dist/commands/app.js +559 -54
- package/dist/commands/app.js.map +1 -1
- package/dist/commands/auth.d.ts +88 -2
- package/dist/commands/auth.js +354 -44
- package/dist/commands/auth.js.map +1 -1
- package/dist/commands/docs.d.ts +54 -0
- package/dist/commands/docs.js +202 -0
- package/dist/commands/docs.js.map +1 -0
- package/dist/commands/doctor.d.ts +23 -0
- package/dist/commands/doctor.js +313 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/init.d.ts +38 -1
- package/dist/commands/init.js +259 -60
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/listen.d.ts +30 -0
- package/dist/commands/listen.js +194 -0
- package/dist/commands/listen.js.map +1 -0
- package/dist/commands/manifest.d.ts +13 -1
- package/dist/commands/manifest.js +81 -18
- package/dist/commands/manifest.js.map +1 -1
- package/dist/commands/mcp.d.ts +25 -2
- package/dist/commands/mcp.js +120 -11
- package/dist/commands/mcp.js.map +1 -1
- package/dist/commands/sandbox.d.ts +35 -6
- package/dist/commands/sandbox.js +208 -9
- package/dist/commands/sandbox.js.map +1 -1
- package/dist/commands/setup.d.ts +39 -0
- package/dist/commands/setup.js +203 -0
- package/dist/commands/setup.js.map +1 -0
- package/dist/commands/tool.d.ts +154 -0
- package/dist/commands/tool.js +785 -0
- package/dist/commands/tool.js.map +1 -0
- package/dist/config.d.ts +30 -6
- package/dist/config.js +39 -8
- package/dist/config.js.map +1 -1
- package/dist/ctx.d.ts +5 -45
- package/dist/ctx.js +25 -160
- package/dist/ctx.js.map +1 -1
- package/dist/docs/index.json +1 -0
- package/dist/docs/pages/actions/call.md +300 -0
- package/dist/docs/pages/actions/catalog/airtable.md +737 -0
- package/dist/docs/pages/actions/catalog/arxiv.md +166 -0
- package/dist/docs/pages/actions/catalog/chainalysis-sanctions.md +125 -0
- package/dist/docs/pages/actions/catalog/coingecko.md +1558 -0
- package/dist/docs/pages/actions/catalog/deps-dev.md +465 -0
- package/dist/docs/pages/actions/catalog/dexscreener.md +568 -0
- package/dist/docs/pages/actions/catalog/discord.md +784 -0
- package/dist/docs/pages/actions/catalog/finnhub.md +776 -0
- package/dist/docs/pages/actions/catalog/frankfurter.md +438 -0
- package/dist/docs/pages/actions/catalog/github.md +1853 -0
- package/dist/docs/pages/actions/catalog/gmail.md +575 -0
- package/dist/docs/pages/actions/catalog/google-calendar.md +654 -0
- package/dist/docs/pages/actions/catalog/google-drive.md +502 -0
- package/dist/docs/pages/actions/catalog/google-sheets.md +700 -0
- package/dist/docs/pages/actions/catalog/hacker-news.md +542 -0
- package/dist/docs/pages/actions/catalog/http.md +824 -0
- package/dist/docs/pages/actions/catalog/hyperliquid.md +942 -0
- package/dist/docs/pages/actions/catalog/jira.md +920 -0
- package/dist/docs/pages/actions/catalog/kalshi.md +917 -0
- package/dist/docs/pages/actions/catalog/linear.md +1030 -0
- package/dist/docs/pages/actions/catalog/markets.md +21 -0
- package/dist/docs/pages/actions/catalog/morpho.md +247 -0
- package/dist/docs/pages/actions/catalog/notion.md +1077 -0
- package/dist/docs/pages/actions/catalog/nws.md +485 -0
- package/dist/docs/pages/actions/catalog/open-meteo.md +559 -0
- package/dist/docs/pages/actions/catalog/polymarket.md +1523 -0
- package/dist/docs/pages/actions/catalog/pushbullet.md +220 -0
- package/dist/docs/pages/actions/catalog/resend.md +307 -0
- package/dist/docs/pages/actions/catalog/s3.md +513 -0
- package/dist/docs/pages/actions/catalog/sec-edgar.md +487 -0
- package/dist/docs/pages/actions/catalog/slack.md +2629 -0
- package/dist/docs/pages/actions/catalog/stripe.md +1405 -0
- package/dist/docs/pages/actions/catalog/wikipedia.md +379 -0
- package/dist/docs/pages/actions/catalog.md +46 -0
- package/dist/docs/pages/actions/limits.md +50 -0
- package/dist/docs/pages/actions/model-tools.md +225 -0
- package/dist/docs/pages/actions/packs.md +148 -0
- package/dist/docs/pages/actions/settings.md +69 -0
- package/dist/docs/pages/actions/tools.md +244 -0
- package/dist/docs/pages/actions/triggers.md +149 -0
- package/dist/docs/pages/actions.md +88 -0
- package/dist/docs/pages/compat/anthropic.md +168 -0
- package/dist/docs/pages/compat/e2b.md +194 -0
- package/dist/docs/pages/compat/firecrawl.md +138 -0
- package/dist/docs/pages/compat/openai.md +168 -0
- package/dist/docs/pages/concepts/auth.md +151 -0
- package/dist/docs/pages/concepts/beta.md +55 -0
- package/dist/docs/pages/concepts/glossary.md +71 -0
- package/dist/docs/pages/concepts/ids.md +98 -0
- package/dist/docs/pages/concepts/jobs.md +229 -0
- package/dist/docs/pages/concepts/latency.md +138 -0
- package/dist/docs/pages/concepts/limits.md +191 -0
- package/dist/docs/pages/concepts/stack.md +59 -0
- package/dist/docs/pages/concepts/timing.md +86 -0
- package/dist/docs/pages/concepts/usage.md +110 -0
- package/dist/docs/pages/design/b3os-yaml.md +370 -0
- package/dist/docs/pages/design/deploy-from-github.md +122 -0
- package/dist/docs/pages/design/observability.md +134 -0
- package/dist/docs/pages/errors.md +1824 -0
- package/dist/docs/pages/infer/chat.md +570 -0
- package/dist/docs/pages/infer/limits.md +55 -0
- package/dist/docs/pages/infer/messages.md +185 -0
- package/dist/docs/pages/infer/responses.md +173 -0
- package/dist/docs/pages/infer/settings.md +154 -0
- package/dist/docs/pages/infer/speech.md +323 -0
- package/dist/docs/pages/infer.md +154 -0
- package/dist/docs/pages/introduction.md +162 -0
- package/dist/docs/pages/platform/addresses.md +174 -0
- package/dist/docs/pages/platform/call-log.md +99 -0
- package/dist/docs/pages/platform/custom-domains.md +104 -0
- package/dist/docs/pages/platform/keys.md +79 -0
- package/dist/docs/pages/platform/limits.md +57 -0
- package/dist/docs/pages/platform/members.md +101 -0
- package/dist/docs/pages/platform/requests.md +116 -0
- package/dist/docs/pages/platform/settings.md +129 -0
- package/dist/docs/pages/platform/webhooks.md +68 -0
- package/dist/docs/pages/platform.md +67 -0
- package/dist/docs/pages/quickstart/mcp.md +94 -0
- package/dist/docs/pages/quickstart/sandbox-agent.md +205 -0
- package/dist/docs/pages/quickstart/scrape.md +134 -0
- package/dist/docs/pages/quickstart.md +45 -0
- package/dist/docs/pages/render/inspections.md +162 -0
- package/dist/docs/pages/render/limits.md +72 -0
- package/dist/docs/pages/render/screenshots.md +179 -0
- package/dist/docs/pages/render/settings.md +431 -0
- package/dist/docs/pages/render/styles.md +342 -0
- package/dist/docs/pages/render/videos.md +272 -0
- package/dist/docs/pages/render.md +118 -0
- package/dist/docs/pages/routines/callables.md +174 -0
- package/dist/docs/pages/routines/definitions.md +361 -0
- package/dist/docs/pages/routines/limits.md +117 -0
- package/dist/docs/pages/routines/runs.md +328 -0
- package/dist/docs/pages/routines/settings.md +104 -0
- package/dist/docs/pages/routines/triggers.md +339 -0
- package/dist/docs/pages/routines.md +216 -0
- package/dist/docs/pages/sandboxes/agents.md +217 -0
- package/dist/docs/pages/sandboxes/commands.md +272 -0
- package/dist/docs/pages/sandboxes/images.md +166 -0
- package/dist/docs/pages/sandboxes/limits.md +143 -0
- package/dist/docs/pages/sandboxes/ports.md +260 -0
- package/dist/docs/pages/sandboxes/settings.md +358 -0
- package/dist/docs/pages/sandboxes/snapshots.md +423 -0
- package/dist/docs/pages/sandboxes/swarms.md +146 -0
- package/dist/docs/pages/sandboxes/volumes.md +131 -0
- package/dist/docs/pages/sandboxes.md +231 -0
- package/dist/docs/pages/sdks/cli.md +121 -0
- package/dist/docs/pages/sdks/mcp.md +169 -0
- package/dist/docs/pages/sdks/python.md +146 -0
- package/dist/docs/pages/sdks/typescript.md +202 -0
- package/dist/docs/pages/sdks.md +282 -0
- package/dist/docs/pages/settings.md +33 -0
- package/dist/docs/pages/vault/limits.md +46 -0
- package/dist/docs/pages/vault.md +133 -0
- package/dist/docs/pages/vision/agent-browser.md +115 -0
- package/dist/docs/pages/vision/crawl.md +333 -0
- package/dist/docs/pages/vision/extract.md +152 -0
- package/dist/docs/pages/vision/fetcher.md +15 -0
- package/dist/docs/pages/vision/limits.md +79 -0
- package/dist/docs/pages/vision/profiles.md +111 -0
- package/dist/docs/pages/vision/proxy.md +131 -0
- package/dist/docs/pages/vision/scrape.md +356 -0
- package/dist/docs/pages/vision/sessions.md +311 -0
- package/dist/docs/pages/vision/settings.md +656 -0
- package/dist/docs/pages/vision.md +175 -0
- package/dist/errors.d.ts +129 -0
- package/dist/errors.js +326 -0
- package/dist/errors.js.map +1 -0
- package/dist/main.d.ts +1 -1
- package/dist/main.js +26 -13
- package/dist/main.js.map +1 -1
- package/dist/manifest/apply.d.ts +4 -0
- package/dist/manifest/apply.js +2 -0
- package/dist/manifest/apply.js.map +1 -1
- package/dist/manifest/domains.d.ts +50 -0
- package/dist/manifest/domains.js +159 -0
- package/dist/manifest/domains.js.map +1 -0
- package/dist/manifest/link.d.ts +20 -0
- package/dist/manifest/link.js +24 -0
- package/dist/manifest/link.js.map +1 -0
- package/dist/manifest/record.d.ts +1 -1
- package/dist/tool/archive.d.ts +33 -0
- package/dist/tool/archive.js +174 -0
- package/dist/tool/archive.js.map +1 -0
- package/dist/tool/calls.d.ts +46 -0
- package/dist/tool/calls.js +274 -0
- package/dist/tool/calls.js.map +1 -0
- package/dist/tool/checks.d.ts +21 -0
- package/dist/tool/checks.js +145 -0
- package/dist/tool/checks.js.map +1 -0
- package/dist/tool/elf.d.ts +19 -0
- package/dist/tool/elf.js +107 -0
- package/dist/tool/elf.js.map +1 -0
- package/dist/tool/predict.d.ts +8 -0
- package/dist/tool/predict.js +38 -0
- package/dist/tool/predict.js.map +1 -0
- package/dist/tool/ustar.d.ts +5 -0
- package/dist/tool/ustar.js +220 -0
- package/dist/tool/ustar.js.map +1 -0
- package/package.json +6 -5
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// What `b3os init` and `b3os setup agents` teach a coding agent (B3-7254, ROADMAP decision 4): the B3OS skill in each
|
|
2
|
+
// agent's skill folder, and a short managed block in the project's context file.
|
|
3
|
+
//
|
|
4
|
+
// Where each agent looks (read 2026-11-26):
|
|
5
|
+
// - Claude Code skills: ~/.claude/skills/<name>/SKILL.md (personal) and .claude/skills/<name>/SKILL.md (project),
|
|
6
|
+
// https://code.claude.com/docs/en/skills.md. CLAUDE_CONFIG_DIR moves ~/.claude ("all settings, session history,
|
|
7
|
+
// and plugins", https://code.claude.com/docs/en/env-vars.md), so the personal folder follows it.
|
|
8
|
+
// - Codex skills: $HOME/.agents/skills and .agents/skills from the working directory up to the repository root,
|
|
9
|
+
// https://learn.chatgpt.com/docs/build-skills ($CODEX_HOME/skills is still read but deprecated:
|
|
10
|
+
// openai/codex codex-rs/ext/skills/src/host_roots.rs). `name` at most 64 characters (codex-rs/skills/src/parser.rs).
|
|
11
|
+
// - Claude Code reads CLAUDE.md, .claude/CLAUDE.md and CLAUDE.local.md in the working directory and every one above
|
|
12
|
+
// it, and reads AGENTS.md "only when you have no CLAUDE.md in your working directory or above it"; a CLAUDE.md
|
|
13
|
+
// may import it with an `@AGENTS.md` line (https://code.claude.com/docs/en/memory.md). ~/.claude/CLAUDE.md does
|
|
14
|
+
// not count.
|
|
15
|
+
// - Codex reads AGENTS.md (AGENTS.override.md first) in each folder from the git root down to the working
|
|
16
|
+
// directory, up to 32 KiB in all, and never CLAUDE.md unless it is named in project_doc_fallback_filenames
|
|
17
|
+
// (https://learn.chatgpt.com/docs/agent-configuration/agents-md).
|
|
18
|
+
// So the block always goes in the project's AGENTS.md, and also in its CLAUDE.md when Claude Code would not read
|
|
19
|
+
// AGENTS.md there.
|
|
20
|
+
import { execFileSync } from "node:child_process";
|
|
21
|
+
import { createHash } from "node:crypto";
|
|
22
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
23
|
+
import { homedir } from "node:os";
|
|
24
|
+
import { dirname, join, parse as parsePath, resolve } from "node:path";
|
|
25
|
+
import { createInterface } from "node:readline/promises";
|
|
26
|
+
import { CLI_VERSION } from "../main.js";
|
|
27
|
+
const home = () => process.env.HOME || homedir();
|
|
28
|
+
const sha256 = (s) => createHash("sha256").update(s).digest("hex");
|
|
29
|
+
/** The B3OS skill as this package ships it (generated from docs-site by docs-site/scripts/agent-files.mjs). */
|
|
30
|
+
export function skillText() {
|
|
31
|
+
return readFileSync(new URL("../../assets/skill/b3os/SKILL.md", import.meta.url), "utf8");
|
|
32
|
+
}
|
|
33
|
+
/** The b3os skill folder of one agent, at user or project scope. */
|
|
34
|
+
export function skillDir(agent, scope, project) {
|
|
35
|
+
if (scope === "project") {
|
|
36
|
+
if (!project)
|
|
37
|
+
throw new Error("a project skill needs the project folder");
|
|
38
|
+
return join(project, agent === "claude" ? ".claude" : ".agents", "skills", "b3os");
|
|
39
|
+
}
|
|
40
|
+
return agent === "claude"
|
|
41
|
+
? join(process.env.CLAUDE_CONFIG_DIR || join(home(), ".claude"), "skills", "b3os")
|
|
42
|
+
: join(home(), ".agents", "skills", "b3os");
|
|
43
|
+
}
|
|
44
|
+
/** The file beside SKILL.md that says b3os wrote it, and what it wrote: a later run updates only its own copy. */
|
|
45
|
+
const STAMP = ".b3os-cli.json";
|
|
46
|
+
/**
|
|
47
|
+
* Writes the skill into `dir` unless it is there already. A SKILL.md that b3os did not write, or that was edited
|
|
48
|
+
* since, is left as it is. With `dryRun`, says what it would do and writes nothing.
|
|
49
|
+
*/
|
|
50
|
+
export function installSkill(dir, o = {}) {
|
|
51
|
+
const want = skillText();
|
|
52
|
+
const path = join(dir, "SKILL.md");
|
|
53
|
+
const stampPath = join(dir, STAMP);
|
|
54
|
+
const write = (detail) => {
|
|
55
|
+
if (!o.dryRun) {
|
|
56
|
+
mkdirSync(dir, { recursive: true });
|
|
57
|
+
writeFileSync(path, want);
|
|
58
|
+
writeFileSync(stampPath, `${JSON.stringify({ written_by: "b3os-cli", version: CLI_VERSION, sha256: sha256(want) }, null, 2)}\n`);
|
|
59
|
+
}
|
|
60
|
+
return { status: "done", path, detail };
|
|
61
|
+
};
|
|
62
|
+
if (!existsSync(path))
|
|
63
|
+
return write(o.dryRun ? `would write ${path}` : path);
|
|
64
|
+
const have = readFileSync(path, "utf8");
|
|
65
|
+
if (have === want)
|
|
66
|
+
return { status: "already set", path, detail: `already set (${path})` };
|
|
67
|
+
let stamp = {};
|
|
68
|
+
try {
|
|
69
|
+
stamp = JSON.parse(readFileSync(stampPath, "utf8"));
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// No stamp: not ours.
|
|
73
|
+
}
|
|
74
|
+
if (stamp.written_by === "b3os-cli" && stamp.sha256 === sha256(have))
|
|
75
|
+
return write(o.dryRun ? `would update ${path}` : `${path} (updated from ${stamp.version ?? "an older version"})`);
|
|
76
|
+
return {
|
|
77
|
+
status: "skipped",
|
|
78
|
+
path,
|
|
79
|
+
detail: `${path} is not the one b3os writes (yours, or edited since); left as it is`,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// ---------------------------------------------------------------- the context block
|
|
83
|
+
/** The block's format version: a later CLI replaces any version's block whole. */
|
|
84
|
+
export const BLOCK_VERSION = 1;
|
|
85
|
+
const BEGIN = "<!-- BEGIN b3os agent rules";
|
|
86
|
+
const END = "<!-- END b3os agent rules -->";
|
|
87
|
+
/** Codex keeps 32 KiB of AGENTS.md for the user's own text too: the block stays small. */
|
|
88
|
+
export const BLOCK_MAX_LINES = 60;
|
|
89
|
+
/** The managed block: what B3OS is, a command index, the output rules and how to check a result. */
|
|
90
|
+
export function contextBlock() {
|
|
91
|
+
return `${BEGIN} v${BLOCK_VERSION} (written by b3os init, which replaces what is between these lines) -->
|
|
92
|
+
## B3OS beta
|
|
93
|
+
|
|
94
|
+
B3OS beta is newer than your training data: use the commands here, \`b3os <command> --help\` and
|
|
95
|
+
\`b3os docs search WORDS\` (the docs, offline), and don't guess flags or hosts. It runs browsers, screenshots, sandboxes, routines, third-party actions and models on one node.
|
|
96
|
+
Run it as \`b3os\`, or \`npx -y @b3os/cli@0.2\` where \`b3os\` is not on PATH. The \`b3os\` MCP server has the same
|
|
97
|
+
calls as tools (\`vision_scrape\`, \`render_screenshot\`, \`sandbox_exec\`, ...) and runs outside your sandbox.
|
|
98
|
+
|
|
99
|
+
| To | Run |
|
|
100
|
+
|---|---|
|
|
101
|
+
| read a page as Markdown | \`b3os scrape URL > page.md\` |
|
|
102
|
+
| read many pages of a site | \`b3os crawl URL --limit 20 --out pages.jsonl\` |
|
|
103
|
+
| screenshot a page | \`b3os screenshot URL --out shot.png\` |
|
|
104
|
+
| run code in a microVM | \`ID=$(b3os sandbox create)\`, \`b3os sandbox exec $ID -- python3 -c 'print(1)'\`, \`b3os sandbox rm $ID --yes\` |
|
|
105
|
+
| deploy an app from b3os.yaml | \`b3os apply --dry-run\`, then \`b3os apply --yes\`; \`b3os app status --json\` |
|
|
106
|
+
| run a durable workflow | \`b3os routine publish flow.yaml\`, then \`b3os routine run NAME\` |
|
|
107
|
+
| call GitHub, Slack and others | \`b3os action search "create issue"\`, \`b3os action describe REF\`, \`b3os action call REF --input @in.json\` |
|
|
108
|
+
| call a model | \`b3os infer chat "PROMPT"\` |
|
|
109
|
+
| see the credential in use | \`b3os whoami --json\` |
|
|
110
|
+
|
|
111
|
+
Output:
|
|
112
|
+
- Add \`--json\` for machine-readable stdout. An error is one JSON object on stderr; exit 2 is usage, 3 auth,
|
|
113
|
+
4 not found, 6 rate limited, 8 timeout, 10 a check or run failed (\`b3os --help\` lists them all).
|
|
114
|
+
- Write large results to files (\`--out FILE\`, or \`> file\`) and read only the part you need.
|
|
115
|
+
- A command's own arguments go after \`--\` (\`b3os sandbox exec ID -- ls -la\`).
|
|
116
|
+
|
|
117
|
+
Sign-in:
|
|
118
|
+
- Exit 3, not signed in: run \`b3os login --no-wait --json\`, show the person its \`verification_url\`, and after
|
|
119
|
+
they approve it run \`b3os login --resume\`. The beta is invite-only: a new account needs an invite code.
|
|
120
|
+
- Never put a key on a command line or in a committed file. A key the person gives you goes in with
|
|
121
|
+
\`printf %s "$KEY" | b3os login --with-key\`.
|
|
122
|
+
|
|
123
|
+
Check a result before you report success:
|
|
124
|
+
- A screenshot: open the file and look at it. A scrape: read the Markdown. A sandbox command: its exit code.
|
|
125
|
+
- A deploy: \`b3os app status --json\` gives its address; fetch it and check the status code and the body.
|
|
126
|
+
- Remove what you made and no longer need: \`b3os sandbox rm ID --yes\`, \`b3os app down APP --yes\`.
|
|
127
|
+
|
|
128
|
+
Docs: https://docs.beta.b3os.org (each page also as \`.md\`) and https://beta.b3os.org/llms.txt.
|
|
129
|
+
${END}`;
|
|
130
|
+
}
|
|
131
|
+
/** `text` with the block in place of any earlier one, or appended after a blank line. */
|
|
132
|
+
export function upsertBlock(text, block) {
|
|
133
|
+
const start = text.indexOf(BEGIN);
|
|
134
|
+
const endAt = start < 0 ? -1 : text.indexOf(END, start);
|
|
135
|
+
if (start >= 0 && endAt >= 0)
|
|
136
|
+
return text.slice(0, start) + block + text.slice(endAt + END.length);
|
|
137
|
+
if (!text.trim())
|
|
138
|
+
return `${block}\n`;
|
|
139
|
+
return `${text.replace(/\n*$/, "")}\n\n${block}\n`;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The project folder for the block: the git repository's top folder, else the working directory. Null for the
|
|
143
|
+
* home folder or the root, where a context file would apply to every project.
|
|
144
|
+
*/
|
|
145
|
+
export function projectDir(cwd = process.cwd(), given) {
|
|
146
|
+
if (given !== undefined)
|
|
147
|
+
return isWholeMachine(resolve(given)) ? null : resolve(given);
|
|
148
|
+
let dir = resolve(cwd);
|
|
149
|
+
try {
|
|
150
|
+
dir = execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
151
|
+
cwd,
|
|
152
|
+
encoding: "utf8",
|
|
153
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
154
|
+
}).trim();
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Not a git repository (or no git): the working directory.
|
|
158
|
+
}
|
|
159
|
+
return isWholeMachine(dir) ? null : dir;
|
|
160
|
+
}
|
|
161
|
+
/** The home folder or the root: a context file there would apply to every project under it. */
|
|
162
|
+
function isWholeMachine(dir) {
|
|
163
|
+
return dir === resolve(home()) || dir === parsePath(dir).root;
|
|
164
|
+
}
|
|
165
|
+
/** True when `file` imports AGENTS.md with an `@AGENTS.md` line. */
|
|
166
|
+
function importsAgentsMd(file) {
|
|
167
|
+
try {
|
|
168
|
+
return /^@(\.\/)?AGENTS\.md\s*$/m.test(readFileSync(file, "utf8"));
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The context files the block goes into: AGENTS.md (Codex, and Claude Code when no CLAUDE.md is in the project or
|
|
176
|
+
* above it), plus the project's CLAUDE.md when one there or above stops Claude Code reading AGENTS.md and the
|
|
177
|
+
* project's own CLAUDE.md does not import it.
|
|
178
|
+
*/
|
|
179
|
+
export function contextFiles(project) {
|
|
180
|
+
const files = [join(project, "AGENTS.md")];
|
|
181
|
+
const own = [join(project, "CLAUDE.md"), join(project, ".claude", "CLAUDE.md")];
|
|
182
|
+
if (own.some(importsAgentsMd))
|
|
183
|
+
return files;
|
|
184
|
+
const userMemory = join(resolve(home()), ".claude", "CLAUDE.md");
|
|
185
|
+
let dir = project;
|
|
186
|
+
let blocked = false;
|
|
187
|
+
for (;;) {
|
|
188
|
+
for (const f of [join(dir, "CLAUDE.md"), join(dir, "CLAUDE.local.md"), join(dir, ".claude", "CLAUDE.md")])
|
|
189
|
+
if (f !== userMemory && existsSync(f))
|
|
190
|
+
blocked = true;
|
|
191
|
+
const up = dirname(dir);
|
|
192
|
+
if (up === dir || blocked)
|
|
193
|
+
break;
|
|
194
|
+
dir = up;
|
|
195
|
+
}
|
|
196
|
+
if (blocked)
|
|
197
|
+
files.push(own.find((f) => existsSync(f)) ?? join(project, "CLAUDE.md"));
|
|
198
|
+
return files;
|
|
199
|
+
}
|
|
200
|
+
/** Writes the block into each file (creating AGENTS.md or CLAUDE.md when missing). With `dryRun`, writes nothing. */
|
|
201
|
+
export function writeContext(files, o = {}) {
|
|
202
|
+
const block = contextBlock();
|
|
203
|
+
return files.map((path) => {
|
|
204
|
+
const text = existsSync(path) ? readFileSync(path, "utf8") : "";
|
|
205
|
+
const next = upsertBlock(text, block);
|
|
206
|
+
if (next === text)
|
|
207
|
+
return { status: "already set", path, detail: `already set (${path})` };
|
|
208
|
+
const verb = text.includes(BEGIN) ? "updated" : existsSync(path) ? "added to" : "created";
|
|
209
|
+
if (!o.dryRun) {
|
|
210
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
211
|
+
writeFileSync(path, next);
|
|
212
|
+
}
|
|
213
|
+
return { status: "done", path, detail: o.dryRun ? `would write the block (${path})` : `${verb} ${path}` };
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/** True when every file already holds this block. */
|
|
217
|
+
export function contextInPlace(files) {
|
|
218
|
+
const block = contextBlock();
|
|
219
|
+
return files.every((p) => existsSync(p) && readFileSync(p, "utf8").includes(block));
|
|
220
|
+
}
|
|
221
|
+
/** A yes/no question on the terminal (asked on stderr, so stdout stays data), yes by default. */
|
|
222
|
+
export async function askYes(ctx, question) {
|
|
223
|
+
ctx.io.stderr.write(`${question} [Y/n] `);
|
|
224
|
+
const rl = createInterface({ input: ctx.io.stdin, terminal: false });
|
|
225
|
+
try {
|
|
226
|
+
const a = (await rl.question("")).trim().toLowerCase();
|
|
227
|
+
return a === "" || a === "y" || a === "yes";
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
rl.close();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** The skill in `dir`: missing, this CLI's copy, an older copy b3os wrote, or someone else's (or edited). */
|
|
234
|
+
export function skillState(dir) {
|
|
235
|
+
const path = join(dir, "SKILL.md");
|
|
236
|
+
if (!existsSync(path))
|
|
237
|
+
return "missing";
|
|
238
|
+
const have = readFileSync(path, "utf8");
|
|
239
|
+
if (have === skillText())
|
|
240
|
+
return "current";
|
|
241
|
+
try {
|
|
242
|
+
const stamp = JSON.parse(readFileSync(join(dir, STAMP), "utf8"));
|
|
243
|
+
if (stamp.written_by === "b3os-cli" && stamp.sha256 === sha256(have))
|
|
244
|
+
return "older";
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
// No stamp: not ours.
|
|
248
|
+
}
|
|
249
|
+
return "other";
|
|
250
|
+
}
|
|
251
|
+
/** The block in one context file: missing, this CLI's block, or another version's. */
|
|
252
|
+
export function blockState(path) {
|
|
253
|
+
if (!existsSync(path))
|
|
254
|
+
return "missing";
|
|
255
|
+
const text = readFileSync(path, "utf8");
|
|
256
|
+
if (text.includes(contextBlock()))
|
|
257
|
+
return "current";
|
|
258
|
+
return text.includes(BEGIN) ? "older" : "missing";
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=_context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_context.js","sourceRoot":"","sources":["../../src/commands/_context.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH,iFAAiF;AACjF,EAAE;AACF,4CAA4C;AAC5C,kHAAkH;AAClH,kHAAkH;AAClH,mGAAmG;AACnG,gHAAgH;AAChH,kGAAkG;AAClG,uHAAuH;AACvH,oHAAoH;AACpH,iHAAiH;AACjH,kHAAkH;AAClH,eAAe;AACf,0GAA0G;AAC1G,6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C,CAAC;IACnC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IACxC,MAAM,IAAI,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACxC,IAAI,IAAI,KAAK,SAAS,EAAE;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,CAA6C,CAAC;QAC7G,IAAI,KAAK,CAAC,UAAU,KAAK,UAAU,IAAI,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC;YAAE,OAAO,OAAO,CAAC;IACvF,CAAC;IAAC,MAAM,CAAC;QACP,sBAAsB;IACxB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IACxC,MAAM,IAAI,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACxC,IAAI,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IACpD,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { CliError } from "../ctx.ts";
|
|
2
|
+
export interface RunStep {
|
|
3
|
+
run: string[];
|
|
4
|
+
why: string;
|
|
5
|
+
who?: "agent" | "human";
|
|
6
|
+
}
|
|
7
|
+
export interface OpenStep {
|
|
8
|
+
open: string;
|
|
9
|
+
why: string;
|
|
10
|
+
who?: "agent" | "human";
|
|
11
|
+
}
|
|
12
|
+
export type NextStep = RunStep | OpenStep;
|
|
13
|
+
/** A command to run next. */
|
|
14
|
+
export declare function runStep(argv: string[], why: string, who?: "agent" | "human"): RunStep;
|
|
15
|
+
/**
|
|
16
|
+
* How to run this CLI again: `b3os`, or npx with this exact @b3os/cli version when it runs from npx's cache, where no
|
|
17
|
+
* `b3os` is on PATH.
|
|
18
|
+
*/
|
|
19
|
+
export declare function b3osCommand(...args: string[]): string[];
|
|
20
|
+
/** True for https URLs on b3os.org or one of its subdomains. */
|
|
21
|
+
export declare function isB3osUrl(url: string): boolean;
|
|
22
|
+
/** A page to open next, or undefined when the URL is not on a b3os.org host (it then stays in the data only). */
|
|
23
|
+
export declare function openStep(url: string, why: string, who?: "agent" | "human"): OpenStep | undefined;
|
|
24
|
+
/** The steps in order, without the ones that were left out (an openStep that was refused). */
|
|
25
|
+
export declare function nextSteps(...steps: Array<NextStep | undefined | false>): NextStep[];
|
|
26
|
+
/** One step as a person reads it: the command line or the URL. */
|
|
27
|
+
export declare function stepText(s: NextStep): string;
|
|
28
|
+
/** `Next: b3os login --resume (after the login is approved)`, one line per step. */
|
|
29
|
+
export declare function nextLines(steps: readonly NextStep[]): string[];
|
|
30
|
+
/**
|
|
31
|
+
* An error with next steps. Until lane B's error module lands, the steps ride in the hint (one per line, as
|
|
32
|
+
* nextLines prints them), which --json errors already carry; switching to the shared module changes only this.
|
|
33
|
+
*/
|
|
34
|
+
export declare function stepError(message: string, o: {
|
|
35
|
+
code: string;
|
|
36
|
+
exit: number;
|
|
37
|
+
next?: readonly NextStep[];
|
|
38
|
+
retryable?: boolean;
|
|
39
|
+
}): CliError;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// What to do next, in one shape for success JSON and errors (B3-7253 ROADMAP decision 7; the orchestrator's ruling of
|
|
2
|
+
// 2026-11-26): a list of steps, each either a command to run (`run`, an argv) or a page a person opens (`open`).
|
|
3
|
+
// {"run": ["b3os", "login", "--resume"], "why": "after the login is approved"}
|
|
4
|
+
// {"open": "https://beta.b3os.org/app/device?...", "why": "a person approves the login here", "who": "human"}
|
|
5
|
+
// `why` is always the CLI's own text, never remote text. `open` is only a URL from a structured API field on a
|
|
6
|
+
// b3os.org host; any other URL stays in the command's data. `who` is "agent" unless only a person can do the step.
|
|
7
|
+
// Human output prints one `Next: ...` line per step.
|
|
8
|
+
//
|
|
9
|
+
// Lane B (B3-7255) is building the shared error module with the same `next`; until it merges, stepError() keeps the
|
|
10
|
+
// error side behind this one adapter: the steps go into the error's hint, so --json errors carry them as text.
|
|
11
|
+
import { sep } from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { CliError } from "../ctx.js";
|
|
14
|
+
import { CLI_VERSION } from "../main.js";
|
|
15
|
+
/** A command to run next. */
|
|
16
|
+
export function runStep(argv, why, who = "agent") {
|
|
17
|
+
return who === "agent" ? { run: argv, why } : { run: argv, why, who };
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* How to run this CLI again: `b3os`, or npx with this exact @b3os/cli version when it runs from npx's cache, where no
|
|
21
|
+
* `b3os` is on PATH.
|
|
22
|
+
*/
|
|
23
|
+
export function b3osCommand(...args) {
|
|
24
|
+
return [...selfArgv(), ...args];
|
|
25
|
+
}
|
|
26
|
+
function selfArgv() {
|
|
27
|
+
let file = "";
|
|
28
|
+
try {
|
|
29
|
+
file = fileURLToPath(import.meta.url);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Not a file URL: run as `b3os`.
|
|
33
|
+
}
|
|
34
|
+
return file.includes(`${sep}_npx${sep}`) ? ["npx", "-y", `@b3os/cli@${CLI_VERSION}`] : ["b3os"];
|
|
35
|
+
}
|
|
36
|
+
/** True for https URLs on b3os.org or one of its subdomains. */
|
|
37
|
+
export function isB3osUrl(url) {
|
|
38
|
+
try {
|
|
39
|
+
const u = new URL(url);
|
|
40
|
+
return u.protocol === "https:" && (u.hostname === "b3os.org" || u.hostname.endsWith(".b3os.org"));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** A page to open next, or undefined when the URL is not on a b3os.org host (it then stays in the data only). */
|
|
47
|
+
export function openStep(url, why, who = "human") {
|
|
48
|
+
if (!isB3osUrl(url))
|
|
49
|
+
return undefined;
|
|
50
|
+
return who === "agent" ? { open: url, why } : { open: url, why, who };
|
|
51
|
+
}
|
|
52
|
+
/** The steps in order, without the ones that were left out (an openStep that was refused). */
|
|
53
|
+
export function nextSteps(...steps) {
|
|
54
|
+
return steps.filter((s) => !!s);
|
|
55
|
+
}
|
|
56
|
+
/** A word in a shell line, quoted when it needs to be. */
|
|
57
|
+
function shellWord(w) {
|
|
58
|
+
return /^[A-Za-z0-9_@%+=:,./-]+$/.test(w) ? w : `'${w.replace(/'/g, `'\\''`)}'`;
|
|
59
|
+
}
|
|
60
|
+
/** One step as a person reads it: the command line or the URL. */
|
|
61
|
+
export function stepText(s) {
|
|
62
|
+
return "run" in s ? s.run.map(shellWord).join(" ") : `open ${s.open}`;
|
|
63
|
+
}
|
|
64
|
+
/** `Next: b3os login --resume (after the login is approved)`, one line per step. */
|
|
65
|
+
export function nextLines(steps) {
|
|
66
|
+
return steps.map((s) => `Next: ${stepText(s)} (${s.why})`);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* An error with next steps. Until lane B's error module lands, the steps ride in the hint (one per line, as
|
|
70
|
+
* nextLines prints them), which --json errors already carry; switching to the shared module changes only this.
|
|
71
|
+
*/
|
|
72
|
+
export function stepError(message, o) {
|
|
73
|
+
const steps = o.next ?? [];
|
|
74
|
+
const hint = steps.length ? nextLines(steps).join("\n") : undefined;
|
|
75
|
+
return new CliError(message, { code: o.code, exit: o.exit, hint });
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=_next.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_next.js","sourceRoot":"","sources":["../../src/commands/_next.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH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CAAC,KAA0B;IAClD,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;AAC9D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CACvB,OAAe,EACf,CAAkF;IAElF,MAAM,KAAK,GAAG,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC;IAC3B,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACpE,OAAO,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Ctx } from "../ctx.ts";
|
|
2
|
+
import type { CliCommand } from "../registry.ts";
|
|
3
|
+
/** `b3os agent`'s options. One named after a request field sets that field (ctx.ts CliOption). */
|
|
4
|
+
export declare const AGENT_OPTIONS: {
|
|
5
|
+
readonly name: {
|
|
6
|
+
readonly type: "string";
|
|
7
|
+
readonly fields: readonly ["agentsCreateAgent:name"];
|
|
8
|
+
};
|
|
9
|
+
readonly limit: {
|
|
10
|
+
readonly type: "string";
|
|
11
|
+
readonly fields: readonly ["agentsListAgents:limit", "agentsListInstances:limit", "agentsListSessions:limit"];
|
|
12
|
+
};
|
|
13
|
+
readonly message: {
|
|
14
|
+
readonly type: "string";
|
|
15
|
+
};
|
|
16
|
+
readonly conversation: {
|
|
17
|
+
readonly type: "string";
|
|
18
|
+
readonly fields: readonly ["agentsSendMessage:conversation"];
|
|
19
|
+
};
|
|
20
|
+
readonly "no-wait": {
|
|
21
|
+
readonly type: "boolean";
|
|
22
|
+
};
|
|
23
|
+
readonly follow: {
|
|
24
|
+
readonly type: "boolean";
|
|
25
|
+
readonly short: "f";
|
|
26
|
+
};
|
|
27
|
+
readonly tier: {
|
|
28
|
+
readonly type: "string";
|
|
29
|
+
readonly fields: readonly ["agentsSleepInstance:tier"];
|
|
30
|
+
};
|
|
31
|
+
readonly key: {
|
|
32
|
+
readonly type: "string";
|
|
33
|
+
readonly fields: readonly ["agentsListSessions:key"];
|
|
34
|
+
};
|
|
35
|
+
readonly status: {
|
|
36
|
+
readonly type: "string";
|
|
37
|
+
readonly fields: readonly ["agentsListSessions:status"];
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export declare function agent(ctx: Ctx, argv: string[]): Promise<number>;
|
|
41
|
+
/** This module's commands, found by main.ts (src/registry.ts). */
|
|
42
|
+
export declare const commands: CliCommand[];
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// b3os agent create | ls | get | rm | send | instances | instance | sleep | sessions | cancel (Actors, B3-7151:
|
|
2
|
+
// `/v1/agents`, research/actors-design.md). An agent is a definition; an instance is one microVM per key, made by its
|
|
3
|
+
// first message and kept until it is deleted; a session is one turn.
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { EXIT, intFlag, jsonFlag, parse, splitSub, UsageError } from "../ctx.js";
|
|
6
|
+
import { jobStatusFlag, removeEach, showUsage, unknownSub } from "./_common.js";
|
|
7
|
+
const AGENT_USAGE = `usage: b3os agent <command>
|
|
8
|
+
create <file.json> [--name NAME] create an agent from its definition (source, entry, sleep, turns...)
|
|
9
|
+
ls [--limit N] agents, with how many instances each has
|
|
10
|
+
get <agent> one agent's definition and revision
|
|
11
|
+
rm <agent>... [--yes] delete agents, with their instances and their disks
|
|
12
|
+
send <agent> <key> [--message JSON|@file] [--conversation ID] [--no-wait] [--follow]
|
|
13
|
+
one message to the instance for <key> (made and woken as needed);
|
|
14
|
+
waits for the turn's result, or --follow prints its events
|
|
15
|
+
instances <agent> [--limit N] its instances: key, status (running, paused, hibernated, dormant)
|
|
16
|
+
instance <agent> <key> one instance: its tier, sandbox, resident memory and checkpoint
|
|
17
|
+
sleep <agent> <key> [--tier ram|snapshot] put an instance to sleep now (after its current turn)
|
|
18
|
+
rm-instance <agent> <key> [--yes] delete one instance and its disk
|
|
19
|
+
sessions <agent> [--key KEY] [--status S] [--limit N]
|
|
20
|
+
its sessions (turns), newest first
|
|
21
|
+
cancel <session_id>`;
|
|
22
|
+
/** `b3os agent`'s options. One named after a request field sets that field (ctx.ts CliOption). */
|
|
23
|
+
export const AGENT_OPTIONS = {
|
|
24
|
+
name: { type: "string", fields: ["agentsCreateAgent:name"] },
|
|
25
|
+
limit: {
|
|
26
|
+
type: "string",
|
|
27
|
+
fields: ["agentsListAgents:limit", "agentsListInstances:limit", "agentsListSessions:limit"],
|
|
28
|
+
},
|
|
29
|
+
message: { type: "string" },
|
|
30
|
+
conversation: { type: "string", fields: ["agentsSendMessage:conversation"] },
|
|
31
|
+
"no-wait": { type: "boolean" },
|
|
32
|
+
follow: { type: "boolean", short: "f" },
|
|
33
|
+
tier: { type: "string", fields: ["agentsSleepInstance:tier"] },
|
|
34
|
+
key: { type: "string", fields: ["agentsListSessions:key"] },
|
|
35
|
+
status: { type: "string", fields: ["agentsListSessions:status"] },
|
|
36
|
+
};
|
|
37
|
+
/** `--message '{"text":"hi"}'` or `--message @msg.json` (a JSON object; `type` defaults to `message`). */
|
|
38
|
+
function messageFlag(v) {
|
|
39
|
+
if (v === undefined)
|
|
40
|
+
return {};
|
|
41
|
+
const s = String(v);
|
|
42
|
+
const parsed = jsonFlag(s.startsWith("@") ? readFileSync(s.slice(1), "utf8") : s, "--message");
|
|
43
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
44
|
+
throw new UsageError("--message must be a JSON object");
|
|
45
|
+
}
|
|
46
|
+
return parsed;
|
|
47
|
+
}
|
|
48
|
+
/** An instance as `agent instance` prints it. */
|
|
49
|
+
function printInstance(ctx, i) {
|
|
50
|
+
ctx.out(`${i.id} ${i.key} ${i.status}`);
|
|
51
|
+
const mib = i.resident_mib ? ` resident ${i.resident_mib} MiB` : "";
|
|
52
|
+
const cp = i.snapshot_at ? ` checkpoint ${i.storage_bytes} bytes at ${i.snapshot_at}` : "";
|
|
53
|
+
ctx.info(`sandbox ${i.sandbox_id ?? "none yet"}${mib}${cp}; ${i.queued} queued`);
|
|
54
|
+
}
|
|
55
|
+
function logSessionEvent(ctx, ev) {
|
|
56
|
+
if (ctx.json) {
|
|
57
|
+
ctx.printJsonLine(ev);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const e = ev;
|
|
61
|
+
if (e.type === "session.output")
|
|
62
|
+
ctx.out(typeof e.data === "string" ? e.data : JSON.stringify(e.data));
|
|
63
|
+
else if (e.type === "session.waking")
|
|
64
|
+
ctx.info(`waking (${e.woke_from ?? "?"})`);
|
|
65
|
+
else
|
|
66
|
+
ctx.info(e.type);
|
|
67
|
+
}
|
|
68
|
+
/** Wait for a sent message's session (or follow its events) and print its result; exit FAILED unless it succeeded. */
|
|
69
|
+
async function finishTurn(ctx, start, follow) {
|
|
70
|
+
let done;
|
|
71
|
+
if (follow) {
|
|
72
|
+
const stream = start().events();
|
|
73
|
+
for await (const ev of stream)
|
|
74
|
+
logSessionEvent(ctx, ev);
|
|
75
|
+
done = await stream.result;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
done = await start().wait({ throwOnFailure: false });
|
|
79
|
+
}
|
|
80
|
+
if (ctx.json && !follow)
|
|
81
|
+
ctx.printJson(done);
|
|
82
|
+
else if (!ctx.json && !follow)
|
|
83
|
+
ctx.out(JSON.stringify(done.result ?? null, null, 2));
|
|
84
|
+
const woke = done.woke_from ? `, woke from ${done.woke_from}${done.recovered ? " (recovered)" : ""}` : "";
|
|
85
|
+
if (done.status !== "succeeded") {
|
|
86
|
+
ctx.warn(`session ${done.id} ${done.status}${done.error ? `: ${done.error.code}: ${done.error.detail}` : ""}`);
|
|
87
|
+
return EXIT.FAILED;
|
|
88
|
+
}
|
|
89
|
+
ctx.info(`session ${done.id} succeeded${woke}`);
|
|
90
|
+
return EXIT.OK;
|
|
91
|
+
}
|
|
92
|
+
export async function agent(ctx, argv) {
|
|
93
|
+
const [sub, rest] = splitSub(argv);
|
|
94
|
+
const a = parse(rest, AGENT_OPTIONS);
|
|
95
|
+
ctx.applyGlobals(a.values);
|
|
96
|
+
if (!sub || a.values.help)
|
|
97
|
+
return showUsage(ctx, AGENT_USAGE, sub, a.values.help);
|
|
98
|
+
const b3 = ctx.client();
|
|
99
|
+
const [arg, key] = a.positionals;
|
|
100
|
+
const limit = () => intFlag(a.values.limit, "--limit");
|
|
101
|
+
switch (sub) {
|
|
102
|
+
case "create": {
|
|
103
|
+
if (!arg)
|
|
104
|
+
throw new UsageError("agent create needs the definition's JSON file");
|
|
105
|
+
const def = jsonFlag(readFileSync(arg, "utf8"), arg);
|
|
106
|
+
if (typeof def !== "object" || def === null || Array.isArray(def)) {
|
|
107
|
+
throw new UsageError(`${arg} is not a JSON object`);
|
|
108
|
+
}
|
|
109
|
+
const body = { ...def };
|
|
110
|
+
if (typeof a.values.name === "string")
|
|
111
|
+
body.name = a.values.name;
|
|
112
|
+
const r = await b3.agents.createAgent(body);
|
|
113
|
+
ctx.result(r, () => ctx.out(`${r.id} ${r.name} revision ${r.revision}`));
|
|
114
|
+
return EXIT.OK;
|
|
115
|
+
}
|
|
116
|
+
case "ls":
|
|
117
|
+
case "list": {
|
|
118
|
+
const page = await b3.agents.listAgents({ limit: limit() });
|
|
119
|
+
ctx.result(page, () => ctx.table(page.data, ["id", "name", "revision", "instances", "created_at"]));
|
|
120
|
+
return EXIT.OK;
|
|
121
|
+
}
|
|
122
|
+
case "get": {
|
|
123
|
+
if (!arg)
|
|
124
|
+
throw new UsageError("agent get needs an agent id or name");
|
|
125
|
+
const r = await b3.agents.getAgent(arg);
|
|
126
|
+
ctx.result(r, () => ctx.printJson(r));
|
|
127
|
+
return EXIT.OK;
|
|
128
|
+
}
|
|
129
|
+
case "rm":
|
|
130
|
+
case "delete":
|
|
131
|
+
return removeEach(ctx, a.positionals, {
|
|
132
|
+
need: "agent rm needs at least one agent id or name",
|
|
133
|
+
question: `Delete ${a.positionals.join(", ")}? Every instance of each is deleted too, with its disk.`,
|
|
134
|
+
remove: (r) => b3.agents.deleteAgent(r),
|
|
135
|
+
});
|
|
136
|
+
case "send": {
|
|
137
|
+
if (!arg || !key)
|
|
138
|
+
throw new UsageError("agent send needs <agent> <key>");
|
|
139
|
+
const body = messageFlag(a.values.message);
|
|
140
|
+
if (typeof a.values.conversation === "string")
|
|
141
|
+
body.conversation = a.values.conversation;
|
|
142
|
+
const msg = body;
|
|
143
|
+
if (a.values["no-wait"]) {
|
|
144
|
+
// Answered 202 once the message is on the node's disk: the session, queued.
|
|
145
|
+
const s = await b3.agents.sendMessage(arg, key, msg);
|
|
146
|
+
ctx.result(s, () => ctx.out(`${s.id} ${s.status}`));
|
|
147
|
+
return EXIT.OK;
|
|
148
|
+
}
|
|
149
|
+
return finishTurn(ctx, () => b3.agents.sendMessage(arg, key, msg), Boolean(a.values.follow));
|
|
150
|
+
}
|
|
151
|
+
case "instances": {
|
|
152
|
+
if (!arg)
|
|
153
|
+
throw new UsageError("agent instances needs an agent id or name");
|
|
154
|
+
const page = await b3.agents.listInstances(arg, { limit: limit() });
|
|
155
|
+
ctx.result(page, () => ctx.table(page.data, ["id", "key", "status", "resident_mib", "storage_bytes", "last_message_at"]));
|
|
156
|
+
return EXIT.OK;
|
|
157
|
+
}
|
|
158
|
+
case "instance": {
|
|
159
|
+
if (!arg || !key)
|
|
160
|
+
throw new UsageError("agent instance needs <agent> <key>");
|
|
161
|
+
const i = await b3.agents.getInstance(arg, key);
|
|
162
|
+
ctx.result(i, () => printInstance(ctx, i));
|
|
163
|
+
return EXIT.OK;
|
|
164
|
+
}
|
|
165
|
+
case "sleep": {
|
|
166
|
+
if (!arg || !key)
|
|
167
|
+
throw new UsageError("agent sleep needs <agent> <key>");
|
|
168
|
+
const tier = a.values.tier;
|
|
169
|
+
if (tier !== undefined && tier !== "ram" && tier !== "snapshot") {
|
|
170
|
+
throw new UsageError("--tier is ram or snapshot");
|
|
171
|
+
}
|
|
172
|
+
const i = await b3.agents.sleepInstance(arg, key, tier ? { tier } : undefined);
|
|
173
|
+
ctx.result(i, () => printInstance(ctx, i));
|
|
174
|
+
return EXIT.OK;
|
|
175
|
+
}
|
|
176
|
+
case "rm-instance": {
|
|
177
|
+
if (!arg || !key)
|
|
178
|
+
throw new UsageError("agent rm-instance needs <agent> <key>");
|
|
179
|
+
return removeEach(ctx, [key], {
|
|
180
|
+
need: "agent rm-instance needs <agent> <key>",
|
|
181
|
+
question: `Delete the instance ${key} of ${arg}? Its microVM and disk go; idleness never deletes one.`,
|
|
182
|
+
remove: (k) => b3.agents.deleteInstance(arg, k),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
case "sessions": {
|
|
186
|
+
if (!arg)
|
|
187
|
+
throw new UsageError("agent sessions needs an agent id or name");
|
|
188
|
+
const page = await b3.agents.listSessions(arg, {
|
|
189
|
+
limit: limit(),
|
|
190
|
+
...(typeof a.values.key === "string" ? { key: a.values.key } : {}),
|
|
191
|
+
...(a.values.status !== undefined ? { status: jobStatusFlag(a.values.status) } : {}),
|
|
192
|
+
});
|
|
193
|
+
ctx.result(page, () => ctx.table(page.data, ["id", "key", "status", "woke_from", "created_at"]));
|
|
194
|
+
return EXIT.OK;
|
|
195
|
+
}
|
|
196
|
+
case "cancel": {
|
|
197
|
+
if (!arg)
|
|
198
|
+
throw new UsageError("agent cancel needs a session id");
|
|
199
|
+
const s = await b3.agents.cancelSession(arg);
|
|
200
|
+
ctx.result(s, () => ctx.out(`${s.id} ${s.status}`));
|
|
201
|
+
return EXIT.OK;
|
|
202
|
+
}
|
|
203
|
+
default:
|
|
204
|
+
throw unknownSub("agent", sub, AGENT_USAGE);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** This module's commands, found by main.ts (src/registry.ts). */
|
|
208
|
+
export const commands = [
|
|
209
|
+
{
|
|
210
|
+
name: "agent",
|
|
211
|
+
summary: "Run agents as actors: one microVM per key that sleeps between messages",
|
|
212
|
+
order: 135,
|
|
213
|
+
run: agent,
|
|
214
|
+
options: AGENT_OPTIONS,
|
|
215
|
+
oldSubcommands: { list: "ls", delete: "rm" },
|
|
216
|
+
},
|
|
217
|
+
];
|
|
218
|
+
//# sourceMappingURL=agent.js.map
|