@omacode/micromanager 0.0.0-stage → 1.0.0
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/LICENSE +201 -0
- package/README.md +30 -2
- package/THIRD_PARTY.md +100 -0
- package/decider/.python-version +1 -0
- package/decider/README.md +35 -0
- package/decider/decider/__init__.py +1 -0
- package/decider/decider/server.py +327 -0
- package/decider/pyproject.toml +34 -0
- package/decider/uv.lock +1255 -0
- package/dist/agenda.js +51 -0
- package/dist/agents.js +58 -0
- package/dist/artifact/handoff.js +281 -0
- package/dist/ask.js +200 -0
- package/dist/backends.js +219 -0
- package/dist/backlog.js +414 -0
- package/dist/bench.js +427 -0
- package/dist/board.js +1 -0
- package/dist/brief.js +202 -0
- package/dist/browser.js +679 -0
- package/dist/budget.js +61 -0
- package/dist/catalog.js +187 -0
- package/dist/checkchange.js +145 -0
- package/dist/checkpoint.js +94 -0
- package/dist/cli.js +764 -0
- package/dist/comfy-gate.js +258 -0
- package/dist/compact.js +142 -0
- package/dist/config.js +344 -0
- package/dist/context/capture.js +1 -0
- package/dist/context/digest.js +1 -0
- package/dist/context/embed.js +1 -0
- package/dist/context/index.js +2 -0
- package/dist/context/ledger.js +1 -0
- package/dist/context/manager.js +1 -0
- package/dist/context/memory.js +1 -0
- package/dist/context/policy.js +1 -0
- package/dist/context/prices.js +1 -0
- package/dist/context/recall.js +1 -0
- package/dist/context/rules.js +1 -0
- package/dist/context/search.js +1 -0
- package/dist/context/status.js +1 -0
- package/dist/context/store.js +1 -0
- package/dist/context/summary.js +1 -0
- package/dist/context/tokens.js +1 -0
- package/dist/context/tools.js +1 -0
- package/dist/continuation.js +184 -0
- package/dist/contract.js +294 -0
- package/dist/contractround.js +178 -0
- package/dist/control.js +1 -0
- package/dist/creative-tools.js +237 -0
- package/dist/dataset.js +175 -0
- package/dist/decide.js +676 -0
- package/dist/decider.js +171 -0
- package/dist/design-shell.js +448 -0
- package/dist/design.js +1410 -0
- package/dist/desktop.js +44 -0
- package/dist/egress.js +156 -0
- package/dist/eval.js +60 -0
- package/dist/evolver.js +302 -0
- package/dist/features.js +193 -0
- package/dist/followups.js +32 -0
- package/dist/forms.js +195 -0
- package/dist/gate.js +108 -0
- package/dist/gatebox.js +46 -0
- package/dist/grants.js +189 -0
- package/dist/graph.js +65 -0
- package/dist/guard/index.js +1 -0
- package/dist/guard/wire.js +1 -0
- package/dist/helpers.js +85 -0
- package/dist/hooks.js +158 -0
- package/dist/image-models.js +117 -0
- package/dist/integration.js +3 -0
- package/dist/journal.js +1 -0
- package/dist/look.js +363 -0
- package/dist/memory.js +1 -0
- package/dist/mode.js +97 -0
- package/dist/native.js +3348 -0
- package/dist/nogit.js +259 -0
- package/dist/outcomes.js +148 -0
- package/dist/permission.js +35 -0
- package/dist/pick.js +89 -0
- package/dist/pipelines.js +262 -0
- package/dist/plan.js +641 -0
- package/dist/plandoc.js +211 -0
- package/dist/plugin.js +4736 -0
- package/dist/policy.js +58 -0
- package/dist/pools.js +1 -0
- package/dist/preview.js +274 -0
- package/dist/profile.js +165 -0
- package/dist/progress.js +1 -0
- package/dist/promote.js +313 -0
- package/dist/redact.js +1 -0
- package/dist/registry.js +179 -0
- package/dist/riskscore.js +126 -0
- package/dist/roles.js +1 -0
- package/dist/routing.js +1 -0
- package/dist/runner.js +1892 -0
- package/dist/sandbox.js +3 -0
- package/dist/sandboxgate.js +90 -0
- package/dist/scheduler.js +170 -0
- package/dist/services-main.js +131 -0
- package/dist/services.js +265 -0
- package/dist/session.js +241 -0
- package/dist/setup.js +423 -0
- package/dist/shell/decide.js +1 -0
- package/dist/shell/index.js +2 -0
- package/dist/shell/sandbox.js +1 -0
- package/dist/shell/wire.js +1 -0
- package/dist/skills.js +103 -0
- package/dist/state.js +1 -0
- package/dist/store.js +1 -0
- package/dist/switcher.js +1 -0
- package/dist/synth.js +418 -0
- package/dist/tools.js +477 -0
- package/dist/trials.js +223 -0
- package/dist/usage.js +1 -0
- package/dist/wait.js +167 -0
- package/dist/watchdog.js +476 -0
- package/dist/web-guidance.js +43 -0
- package/dist/which.js +20 -0
- package/dist/worktree/git.js +545 -0
- package/dist/writeguard.js +311 -0
- package/evolver/mm_evolve/__init__.py +1 -0
- package/evolver/mm_evolve/entry.py +3 -0
- package/evolver/mm_evolve/evaluator.py +119 -0
- package/evolver/mm_evolve/llm.py +76 -0
- package/evolver/mm_evolve/run.py +89 -0
- package/evolver/mm_evolve/spec.py +62 -0
- package/evolver/pyproject.toml +21 -0
- package/evolver/uv.lock +772 -0
- package/package.json +74 -4
- package/skills/THIRD_PARTY.md +58 -0
- package/skills/code-review/SKILL.md +46 -0
- package/skills/deck/SKILL.md +98 -0
- package/skills/deliberate/SKILL.md +39 -0
- package/skills/design/SKILL.md +150 -0
- package/skills/diagnosing-bugs/SKILL.md +75 -0
- package/skills/image/SKILL.md +66 -0
- package/skills/orchestrator/SKILL.md +107 -0
- package/skills/receiving-code-review/SKILL.md +36 -0
- package/skills/research/SKILL.md +18 -0
- package/skills/tdd/SKILL.md +56 -0
- package/skills/tdd/mocking.md +59 -0
- package/skills/tdd/tests.md +77 -0
- package/skills/verification-before-completion/SKILL.md +50 -0
- package/skills/video/SKILL.md +97 -0
- package/skills/web/SKILL.md +60 -0
package/dist/look.js
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
// The `look` tool of the visual workers: a page, a drawing or an image of their directory as a picture, rendered by the
|
|
2
|
+
// plugin on the host.
|
|
3
|
+
//
|
|
4
|
+
// A browser cannot run in the worker sandbox (found 2026-10-27): Chromium's own sandbox needs user namespaces, which
|
|
5
|
+
// bubblewrap refuses inside, and its process singleton needs socket(AF_UNIX), which sandbox-runtime's seccomp filter
|
|
6
|
+
// blocks for every worker (unblocking it would open D-Bus, Docker and agent sockets to all of them). So the plugin
|
|
7
|
+
// renders: headless Chromium with its own sandbox on, a throwaway profile, the worker's directory served from
|
|
8
|
+
// 127.0.0.1, and a resolver that knows no host but that one and the hosts the job may reach. Full-page shots go through
|
|
9
|
+
// the DevTools protocol over --remote-debugging-pipe (spike, 2026-10-27: 0.3 s for a 1000 x 2219 page).
|
|
10
|
+
import { spawn } from "node:child_process";
|
|
11
|
+
import { closeSync, constants, existsSync, fstatSync, mkdtempSync, openSync, readFileSync, realpathSync, rmSync, statSync } from "node:fs";
|
|
12
|
+
import { createServer } from "node:http";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
|
|
15
|
+
import { inStateDir } from "@omacode/core/paths";
|
|
16
|
+
const IMAGES = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".avif": "image/avif" };
|
|
17
|
+
const PAGES = new Set([".html", ".htm", ".svg"]);
|
|
18
|
+
const MIME = {
|
|
19
|
+
...IMAGES, ".html": "text/html; charset=utf-8", ".htm": "text/html; charset=utf-8", ".svg": "image/svg+xml", ".css": "text/css",
|
|
20
|
+
".js": "text/javascript", ".mjs": "text/javascript", ".json": "application/json", ".woff": "font/woff", ".woff2": "font/woff2",
|
|
21
|
+
".ttf": "font/ttf", ".otf": "font/otf", ".ico": "image/x-icon", ".avif": "image/avif", ".mp4": "video/mp4", ".webm": "video/webm", ".txt": "text/plain",
|
|
22
|
+
};
|
|
23
|
+
// The tallest full-page shot: a vision model scales it down anyway, and an endless page should not make a huge file.
|
|
24
|
+
export const MAX_HEIGHT = 8000;
|
|
25
|
+
export const MAX_IMAGE_BYTES = 12 * 1024 * 1024;
|
|
26
|
+
// The browser to render with lives in @omacode/core, where the site adapter (B4) needs the same answer.
|
|
27
|
+
import { findChromium } from "@omacode/core/chromium";
|
|
28
|
+
export { findChromium };
|
|
29
|
+
// Whether a host matches what the job may reach: "api.example.com" or "*.example.com".
|
|
30
|
+
export function hostAllowed(host, allowed) {
|
|
31
|
+
const split = (value) => {
|
|
32
|
+
const m = /^(.*):(\d+)$/.exec(value.toLowerCase());
|
|
33
|
+
return m ? [m[1], m[2]] : [value.toLowerCase(), undefined];
|
|
34
|
+
};
|
|
35
|
+
const [h, port] = split(host);
|
|
36
|
+
return allowed.some((a) => {
|
|
37
|
+
const [p, wanted] = split(a);
|
|
38
|
+
if (wanted !== undefined && wanted !== port)
|
|
39
|
+
return false;
|
|
40
|
+
return p.startsWith("*.") ? h.endsWith(p.slice(1)) && h !== p.slice(2) : h === p;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// Chromium's resolver: nothing resolves but the plugin's own server and the allowed hosts.
|
|
44
|
+
export function resolverRules(allowed) {
|
|
45
|
+
return ["MAP * ~NOTFOUND", "EXCLUDE 127.0.0.1", ...allowed.map((a) => `EXCLUDE ${a.replace(/:\d+$/, "")}`)].join(", ");
|
|
46
|
+
}
|
|
47
|
+
// A real path stays inside its real root. Lexical checks alone are not containment: a worker can create a symlink.
|
|
48
|
+
function within(root, file) {
|
|
49
|
+
return file === root || file.startsWith(root + sep);
|
|
50
|
+
}
|
|
51
|
+
// URL path segments, so a file called `a#b.html` or `a?b.html` is a file and not a fragment or query.
|
|
52
|
+
function urlPath(parts) {
|
|
53
|
+
return "/" + parts.map((part) => encodeURIComponent(part)).join("/");
|
|
54
|
+
}
|
|
55
|
+
// What a look asks for, checked: a path inside the worker's directory, or a URL on a host the job may reach.
|
|
56
|
+
export function lookTarget(input, dir, allowed) {
|
|
57
|
+
const path = input.path?.trim();
|
|
58
|
+
const url = input.url?.trim();
|
|
59
|
+
if (!path && !url)
|
|
60
|
+
return { kind: "error", text: "Name a path in your directory (a page, an svg, an image, or a directory with an index.html) or a url." };
|
|
61
|
+
if (url) {
|
|
62
|
+
let u;
|
|
63
|
+
try {
|
|
64
|
+
u = new URL(url);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return { kind: "error", text: `${url} is no URL.` };
|
|
68
|
+
}
|
|
69
|
+
if (!/^https?:$/.test(u.protocol))
|
|
70
|
+
return { kind: "error", text: "Only http and https URLs; a file of yours goes in path." };
|
|
71
|
+
if (!hostAllowed(u.host, allowed))
|
|
72
|
+
return { kind: "error", text: `${u.host} is not a host this job may reach. Build the page and look at it by its path, or ask for the host with request_network.` };
|
|
73
|
+
return { kind: "url", url: u.toString() };
|
|
74
|
+
}
|
|
75
|
+
const root = resolve(dir);
|
|
76
|
+
const full = resolve(root, path);
|
|
77
|
+
if (!within(root, full))
|
|
78
|
+
return { kind: "error", text: `${path} is outside your directory.` };
|
|
79
|
+
if (!existsSync(full))
|
|
80
|
+
return { kind: "error", text: `${path} does not exist. Build the page first (e.g. npm run build) and look at its output.` };
|
|
81
|
+
let realRoot;
|
|
82
|
+
let realFull;
|
|
83
|
+
try {
|
|
84
|
+
realRoot = realpathSync(root);
|
|
85
|
+
realFull = realpathSync(full);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return { kind: "error", text: `${path} cannot be resolved.` };
|
|
89
|
+
}
|
|
90
|
+
if (!within(realRoot, realFull))
|
|
91
|
+
return { kind: "error", text: `${path} resolves outside your directory.` };
|
|
92
|
+
const rel = relative(realRoot, realFull).split(sep).join("/");
|
|
93
|
+
if (forbidden(rel))
|
|
94
|
+
return { kind: "error", text: `${path} is not shown.` };
|
|
95
|
+
if (statSync(realFull).isDirectory()) {
|
|
96
|
+
const index = join(realFull, "index.html");
|
|
97
|
+
if (!existsSync(index))
|
|
98
|
+
return { kind: "error", text: `${path} has no index.html; name the page.` };
|
|
99
|
+
try {
|
|
100
|
+
if (!within(realRoot, realpathSync(index)))
|
|
101
|
+
return { kind: "error", text: `${path}/index.html resolves outside your directory.` };
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return { kind: "error", text: `${path}/index.html cannot be resolved.` };
|
|
105
|
+
}
|
|
106
|
+
// The directory is the document root, so a built deck's /assets/... paths resolve there.
|
|
107
|
+
return { kind: "page", root: realFull, path: "/", label: rel ? `${rel}/` : "" };
|
|
108
|
+
}
|
|
109
|
+
const ext = extname(realFull).toLowerCase();
|
|
110
|
+
if (IMAGES[ext])
|
|
111
|
+
return { kind: "image", file: realFull, mime: IMAGES[ext] };
|
|
112
|
+
if (PAGES.has(ext)) {
|
|
113
|
+
// A page is served with its own directory as root; /assets/... then means the production output's assets.
|
|
114
|
+
return { kind: "page", root: dirname(realFull), path: urlPath([basename(realFull)]), label: rel };
|
|
115
|
+
}
|
|
116
|
+
return { kind: "error", text: `${path} is no page or image (html, svg, png, jpg, gif, webp).` };
|
|
117
|
+
}
|
|
118
|
+
// Files the static server never hands out: environment files, git's own and the plugin's.
|
|
119
|
+
function forbidden(rel) {
|
|
120
|
+
return inStateDir(rel) || rel.split("/").some((part) => /^\.env(\..*)?$/.test(part) && !/^\.env\.example$/.test(part) || part === ".git");
|
|
121
|
+
}
|
|
122
|
+
// The worker's directory over http on 127.0.0.1, read-only, so absolute asset paths (/assets/x.css) resolve.
|
|
123
|
+
// fallback: a path without a file extension and no file of its own is the single-page app's index.html, so a worker can
|
|
124
|
+
// drive a route (/dashboard) that only the client router knows.
|
|
125
|
+
export async function serveDir(dir, options = {}) {
|
|
126
|
+
const root = resolve(dir);
|
|
127
|
+
const realRoot = realpathSync(root);
|
|
128
|
+
const server = createServer((req, res) => {
|
|
129
|
+
let path;
|
|
130
|
+
try {
|
|
131
|
+
path = decodeURIComponent(new URL(req.url ?? "/", "http://x").pathname);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
res.writeHead(400).end();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const full = resolve(root, `.${path}`);
|
|
138
|
+
if (!within(root, full) || forbidden(relative(root, full).split(sep).join("/"))) {
|
|
139
|
+
res.writeHead(403).end();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
// Resolve first, then read the resolved path: a symlink must not escape the served directory after the check.
|
|
144
|
+
let real = realpathSync(full);
|
|
145
|
+
if (!within(realRoot, real)) {
|
|
146
|
+
res.writeHead(403).end();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (statSync(real).isDirectory())
|
|
150
|
+
real = realpathSync(join(real, "index.html"));
|
|
151
|
+
if (!within(realRoot, real) || forbidden(relative(realRoot, real).split(sep).join("/"))) {
|
|
152
|
+
res.writeHead(403).end();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const body = readFileSync(real);
|
|
156
|
+
res.writeHead(200, { "content-type": MIME[extname(real).toLowerCase()] ?? "application/octet-stream", "cache-control": "no-store" });
|
|
157
|
+
res.end(body);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
if (options.fallback && !extname(path)) {
|
|
161
|
+
try {
|
|
162
|
+
const index = realpathSync(join(realRoot, "index.html"));
|
|
163
|
+
if (within(realRoot, index) && !forbidden(relative(realRoot, index).split(sep).join("/"))) {
|
|
164
|
+
res.writeHead(200, { "content-type": MIME[".html"], "cache-control": "no-store" });
|
|
165
|
+
res.end(readFileSync(index));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
// no index either, then
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
res.writeHead(404).end();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
await new Promise((ok, fail) => {
|
|
177
|
+
server.once("error", fail);
|
|
178
|
+
server.listen(0, "127.0.0.1", () => ok());
|
|
179
|
+
});
|
|
180
|
+
const port = server.address().port;
|
|
181
|
+
return { origin: `http://127.0.0.1:${port}`, close: () => new Promise((ok) => server.close(() => ok())) };
|
|
182
|
+
}
|
|
183
|
+
// One screenshot of url, PNG bytes. The browser is started for it and gone afterwards, its profile with it.
|
|
184
|
+
export async function screenshot(chromium, url, shot, allowed) {
|
|
185
|
+
const profile = mkdtempSync(join(tmpdir(), "mm-look-"));
|
|
186
|
+
const browser = spawn(chromium, [
|
|
187
|
+
"--headless=new", "--remote-debugging-pipe", `--user-data-dir=${profile}`, "--no-first-run", "--no-default-browser-check",
|
|
188
|
+
"--disable-extensions", "--disable-sync", "--disable-background-networking", "--hide-scrollbars", "--mute-audio",
|
|
189
|
+
`--host-resolver-rules=${resolverRules(allowed)}`, "about:blank",
|
|
190
|
+
], { stdio: ["ignore", "ignore", "pipe", "pipe", "pipe"] });
|
|
191
|
+
let stderr = "";
|
|
192
|
+
browser.stderr.on("data", (d) => (stderr = (stderr + d.toString()).slice(-2000)));
|
|
193
|
+
const toBrowser = browser.stdio[3];
|
|
194
|
+
const fromBrowser = browser.stdio[4];
|
|
195
|
+
let id = 0;
|
|
196
|
+
let buf = "";
|
|
197
|
+
const localOrigin = new URL(url).origin;
|
|
198
|
+
let protocolFailed = () => undefined;
|
|
199
|
+
const protocol = new Promise((_, fail) => (protocolFailed = fail));
|
|
200
|
+
const pending = new Map();
|
|
201
|
+
const events = [];
|
|
202
|
+
const waiters = [];
|
|
203
|
+
fromBrowser.on("data", (d) => {
|
|
204
|
+
buf += d.toString();
|
|
205
|
+
let i;
|
|
206
|
+
while ((i = buf.indexOf("\0")) >= 0) {
|
|
207
|
+
const raw = buf.slice(0, i);
|
|
208
|
+
buf = buf.slice(i + 1);
|
|
209
|
+
let m;
|
|
210
|
+
try {
|
|
211
|
+
m = JSON.parse(raw);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
protocolFailed(new Error("the browser sent malformed protocol data"));
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (m.id !== undefined && pending.has(m.id)) {
|
|
218
|
+
pending.get(m.id)(m);
|
|
219
|
+
pending.delete(m.id);
|
|
220
|
+
}
|
|
221
|
+
else if (m.method) {
|
|
222
|
+
if (m.method === "Fetch.requestPaused" && m.sessionId) {
|
|
223
|
+
// Resolver rules are not a boundary for subresources, redirects or literal loopback addresses. Check every
|
|
224
|
+
// request here and let only the served page and the job's granted hosts through.
|
|
225
|
+
const requestId = String(m.params?.requestId ?? "");
|
|
226
|
+
const raw = String(m.params?.request?.url ?? "");
|
|
227
|
+
let allowedRequest = false;
|
|
228
|
+
try {
|
|
229
|
+
const u = new URL(raw);
|
|
230
|
+
allowedRequest = ["about:", "data:", "blob:"].includes(u.protocol) || u.origin === localOrigin || hostAllowed(u.host, allowed);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
allowedRequest = false;
|
|
234
|
+
}
|
|
235
|
+
const call = allowedRequest ? send("Fetch.continueRequest", { requestId }, m.sessionId) : send("Fetch.failRequest", { requestId, errorReason: "BlockedByClient" }, m.sessionId);
|
|
236
|
+
void call.catch(() => undefined);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const w = waiters.findIndex((x) => x.method === m.method && x.sessionId === m.sessionId);
|
|
240
|
+
if (w >= 0)
|
|
241
|
+
waiters.splice(w, 1)[0].ok();
|
|
242
|
+
else
|
|
243
|
+
events.push({ method: m.method, sessionId: m.sessionId });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
const send = (method, params = {}, sessionId) => new Promise((ok, fail) => {
|
|
248
|
+
const n = ++id;
|
|
249
|
+
pending.set(n, (m) => (m.error ? fail(new Error(`${method}: ${m.error.message}`)) : ok(m.result ?? {})));
|
|
250
|
+
toBrowser.write(`${JSON.stringify({ id: n, method, params, ...(sessionId ? { sessionId } : {}) })}\0`);
|
|
251
|
+
});
|
|
252
|
+
const event = (method, sessionId) => new Promise((ok) => {
|
|
253
|
+
const i = events.findIndex((e) => e.method === method && e.sessionId === sessionId);
|
|
254
|
+
if (i >= 0) {
|
|
255
|
+
events.splice(i, 1);
|
|
256
|
+
ok();
|
|
257
|
+
}
|
|
258
|
+
else
|
|
259
|
+
waiters.push({ method, sessionId, ok });
|
|
260
|
+
});
|
|
261
|
+
const broken = new Promise((_, fail) => browser.once("error", (err) => fail(new Error(`the browser could not start: ${err.message}`))));
|
|
262
|
+
browser.on("error", () => undefined); // handled by the race; a later error must not become an uncaught event
|
|
263
|
+
const exited = new Promise((_, fail) => browser.once("exit", (code) => fail(new Error(`the browser ended (${code}): ${stderr.trim().split("\n").pop() ?? ""}`))));
|
|
264
|
+
const timeout = new Promise((_, fail) => setTimeout(() => fail(new Error(`no picture within ${(shot.timeoutMs ?? 30_000) / 1000} s`)), shot.timeoutMs ?? 30_000).unref());
|
|
265
|
+
const work = (async () => {
|
|
266
|
+
const { targetId } = (await send("Target.createTarget", { url: "about:blank" }));
|
|
267
|
+
const { sessionId } = (await send("Target.attachToTarget", { targetId, flatten: true }));
|
|
268
|
+
await send("Page.enable", {}, sessionId);
|
|
269
|
+
await send("Fetch.enable", { patterns: [{ urlPattern: "*", requestStage: "Request" }] }, sessionId);
|
|
270
|
+
const height = shot.height ?? 800;
|
|
271
|
+
await send("Emulation.setDeviceMetricsOverride", { width: shot.width, height, deviceScaleFactor: 1, mobile: false }, sessionId);
|
|
272
|
+
const loaded = event("Page.loadEventFired", sessionId);
|
|
273
|
+
const nav = (await send("Page.navigate", { url }, sessionId));
|
|
274
|
+
if (nav.errorText)
|
|
275
|
+
throw new Error(`${url} did not load: ${nav.errorText}`);
|
|
276
|
+
await loaded;
|
|
277
|
+
// Web fonts first, then an explicit settle for late layout and animations. Both are bounded by the global timeout.
|
|
278
|
+
await send("Runtime.evaluate", {
|
|
279
|
+
expression: "document.fonts && document.fonts.ready ? Promise.race([document.fonts.ready.then(() => true), new Promise((r) => setTimeout(() => r(false), 3000))]) : true",
|
|
280
|
+
awaitPromise: true,
|
|
281
|
+
returnByValue: true,
|
|
282
|
+
}, sessionId);
|
|
283
|
+
await new Promise((r) => setTimeout(r, clamp(shot.settleMs ?? 300, 0, 10_000)));
|
|
284
|
+
let clipHeight = height;
|
|
285
|
+
if (shot.fullPage) {
|
|
286
|
+
const m = (await send("Page.getLayoutMetrics", {}, sessionId));
|
|
287
|
+
clipHeight = Math.min(MAX_HEIGHT, Math.max(height, Math.ceil(m.cssContentSize?.height ?? height)));
|
|
288
|
+
}
|
|
289
|
+
const r = (await send("Page.captureScreenshot", { format: "png", captureBeyondViewport: shot.fullPage, clip: { x: 0, y: 0, width: shot.width, height: clipHeight, scale: 1 } }, sessionId));
|
|
290
|
+
return Buffer.from(r.data, "base64");
|
|
291
|
+
})();
|
|
292
|
+
try {
|
|
293
|
+
return await Promise.race([work, broken, protocol, exited, timeout]);
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
broken.catch(() => undefined);
|
|
297
|
+
exited.catch(() => undefined);
|
|
298
|
+
protocol.catch(() => undefined);
|
|
299
|
+
browser.kill("SIGKILL");
|
|
300
|
+
// Wait briefly for the process to go, so the next capture does not inherit a live orphan.
|
|
301
|
+
if (browser.exitCode === null)
|
|
302
|
+
await new Promise((ok) => {
|
|
303
|
+
const timer = setTimeout(ok, 1000);
|
|
304
|
+
browser.once("close", () => {
|
|
305
|
+
clearTimeout(timer);
|
|
306
|
+
ok();
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
rmSync(profile, { recursive: true, force: true });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// A visual writer owes a picture, not just a successful shell command.
|
|
313
|
+
export function missingVisualEvidence(role, looked) {
|
|
314
|
+
return role === "visual" && !looked;
|
|
315
|
+
}
|
|
316
|
+
// One look: the checks, then the picture. dir: the worker's directory; allowed: the hosts its job may reach.
|
|
317
|
+
// chromium: the browser to use, null for none.
|
|
318
|
+
export async function look(input, dir, allowed, chromium = findChromium() ?? null) {
|
|
319
|
+
const target = lookTarget(input, dir, allowed);
|
|
320
|
+
if (target.kind === "error")
|
|
321
|
+
return { text: target.text };
|
|
322
|
+
if (target.kind === "image") {
|
|
323
|
+
let fd;
|
|
324
|
+
try {
|
|
325
|
+
fd = openSync(target.file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
326
|
+
const size = fstatSync(fd).size;
|
|
327
|
+
if (size > MAX_IMAGE_BYTES)
|
|
328
|
+
return { text: `${relative(dir, target.file)} is ${size} bytes; a look is limited to ${MAX_IMAGE_BYTES} bytes.` };
|
|
329
|
+
const data = readFileSync(fd);
|
|
330
|
+
return { text: `${relative(dir, target.file)} (${data.length} bytes)`, image: { data, mime: target.mime } };
|
|
331
|
+
}
|
|
332
|
+
catch (err) {
|
|
333
|
+
return { text: `${relative(dir, target.file)} could not be read safely: ${String(err)}` };
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
if (fd !== undefined)
|
|
337
|
+
closeSync(fd);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (!chromium)
|
|
341
|
+
return { text: "No browser is installed where the plugin runs (chromium, google-chrome or Playwright's chromium), so pages cannot be shown. Images still can." };
|
|
342
|
+
const width = clamp(Number(input.width) || 1280, 320, 2560);
|
|
343
|
+
const height = input.height ? clamp(Number(input.height) || 800, 240, 4000) : 800;
|
|
344
|
+
const fullPage = !/^(false|no|0)$/i.test(input.full?.trim() ?? "");
|
|
345
|
+
const settleMs = input.settle === undefined || input.settle === "" ? 300 : clamp(Number(input.settle) || 0, 0, 10_000);
|
|
346
|
+
if (target.kind === "url") {
|
|
347
|
+
const data = await screenshot(chromium, target.url, { width, height, fullPage, settleMs }, allowed);
|
|
348
|
+
if (data.length > MAX_IMAGE_BYTES)
|
|
349
|
+
return { text: `${target.url} rendered to ${data.length} bytes; a look is limited to ${MAX_IMAGE_BYTES}. Use a smaller width or full: no.` };
|
|
350
|
+
return { text: `${target.url} at ${width} px${fullPage ? ", the whole page" : ""}`, image: { data, mime: "image/png" } };
|
|
351
|
+
}
|
|
352
|
+
const server = await serveDir(target.root);
|
|
353
|
+
try {
|
|
354
|
+
const data = await screenshot(chromium, `${server.origin}${target.path}`, { width, height, fullPage, settleMs }, allowed);
|
|
355
|
+
if (data.length > MAX_IMAGE_BYTES)
|
|
356
|
+
return { text: `${target.label || "index.html"} rendered to ${data.length} bytes; a look is limited to ${MAX_IMAGE_BYTES}. Use a smaller width or full: no.` };
|
|
357
|
+
return { text: `${target.label || "index.html"} at ${width} px${fullPage ? ", the whole page" : ""}; only your directory and the hosts this job may reach load`, image: { data, mime: "image/png" } };
|
|
358
|
+
}
|
|
359
|
+
finally {
|
|
360
|
+
await server.close();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
const clamp = (n, lo, hi) => Math.min(hi, Math.max(lo, Math.round(n)));
|
package/dist/memory.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/context/project-memory";
|
package/dist/mode.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// The micromanager's mode: one agent, two modes. It used to be a second primary agent, "Micromanager (plan)", which
|
|
2
|
+
// made the state a third thing the user had to pick and remember. The state belongs to the session instead.
|
|
3
|
+
//
|
|
4
|
+
// Plan is the default, so a new session plans and never starts work on its own. Leaving plan mode takes the user's own
|
|
5
|
+
// yes: `askToLeave` records that the question was put, `noteUserSpoke` records that they answered, and only a turn that
|
|
6
|
+
// came after the question unlocks the mode. The agent cannot unlock itself, which is what makes the mode real.
|
|
7
|
+
import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { dirname } from "node:path";
|
|
9
|
+
import { modeIntentPath, modesPath } from "@omacode/core/paths";
|
|
10
|
+
export const DEFAULT_MODE = "plan";
|
|
11
|
+
// The mode per session, the sessions the user was asked about, and when the user last spoke in each.
|
|
12
|
+
const modes = new Map();
|
|
13
|
+
const asked = new Map();
|
|
14
|
+
const spoke = new Map();
|
|
15
|
+
const CAP = 500;
|
|
16
|
+
function file() {
|
|
17
|
+
return modesPath();
|
|
18
|
+
}
|
|
19
|
+
export function loadModes() {
|
|
20
|
+
try {
|
|
21
|
+
const raw = JSON.parse(readFileSync(file(), "utf8"));
|
|
22
|
+
for (const [sessionID, mode] of Object.entries(raw))
|
|
23
|
+
if (mode === "plan" || mode === "build")
|
|
24
|
+
modes.set(sessionID, mode);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// No file yet, or one we cannot read: every session starts from the default, which is the safe direction.
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function save() {
|
|
31
|
+
try {
|
|
32
|
+
mkdirSync(dirname(file()), { recursive: true });
|
|
33
|
+
const entries = [...modes].slice(-CAP);
|
|
34
|
+
writeFileSync(file(), JSON.stringify(Object.fromEntries(entries)), { mode: 0o600 });
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// The mode still holds in memory; a restart falls back to the default rather than to a wrong mode.
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function modeOf(sessionID) {
|
|
41
|
+
if (!sessionID)
|
|
42
|
+
return DEFAULT_MODE;
|
|
43
|
+
return modes.get(sessionID) ?? DEFAULT_MODE;
|
|
44
|
+
}
|
|
45
|
+
export function setMode(sessionID, mode) {
|
|
46
|
+
modes.delete(sessionID);
|
|
47
|
+
modes.set(sessionID, mode); // re-insert, so the cap drops the least recently used
|
|
48
|
+
save();
|
|
49
|
+
}
|
|
50
|
+
// The user's own turn: the only thing that can unlock leaving plan mode. A counter, not a clock: two events in the same
|
|
51
|
+
// millisecond must still order (the question, then the answer).
|
|
52
|
+
let tick = 0;
|
|
53
|
+
export function noteUserSpoke(sessionID) {
|
|
54
|
+
if (sessionID)
|
|
55
|
+
spoke.set(sessionID, ++tick);
|
|
56
|
+
}
|
|
57
|
+
export function askToLeave(sessionID) {
|
|
58
|
+
asked.set(sessionID, ++tick);
|
|
59
|
+
}
|
|
60
|
+
export function canLeave(sessionID) {
|
|
61
|
+
const when = asked.get(sessionID);
|
|
62
|
+
if (when === undefined)
|
|
63
|
+
return false;
|
|
64
|
+
return (spoke.get(sessionID) ?? 0) > when;
|
|
65
|
+
}
|
|
66
|
+
export function clearAsk(sessionID) {
|
|
67
|
+
asked.delete(sessionID);
|
|
68
|
+
}
|
|
69
|
+
// The TUI's switch: a user action, so the server applies it as their own word instead of asking them to repeat it in
|
|
70
|
+
// prose. The file is a one-shot: the next turn (or tool call) that sees it applies it and deletes it.
|
|
71
|
+
export function requestMode(sessionID, mode) {
|
|
72
|
+
const path = modeIntentPath(sessionID);
|
|
73
|
+
try {
|
|
74
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
75
|
+
writeFileSync(path, `${JSON.stringify({ mode, at: Date.now() })}\n`, { mode: 0o600 });
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// The panel shows the mode it read; the click simply has no effect if the file cannot be written.
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export function takeModeIntent(sessionID) {
|
|
82
|
+
const path = modeIntentPath(sessionID);
|
|
83
|
+
try {
|
|
84
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
85
|
+
unlinkSync(path);
|
|
86
|
+
return parsed.mode === "plan" || parsed.mode === "build" ? parsed.mode : undefined;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Tests set the same session up repeatedly.
|
|
93
|
+
export function forgetModes() {
|
|
94
|
+
modes.clear();
|
|
95
|
+
asked.clear();
|
|
96
|
+
spoke.clear();
|
|
97
|
+
}
|