@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/decider.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// The decision client: program state + typed questions in, typed answers with probabilities out. It was renamed
|
|
2
|
+
// before A2-T3 to say what it is, because it now serves the cloud service and the local decision servers
|
|
3
|
+
// alike. Wire format (the TypeSafe format, unchanged): POST https://api.typesafe.ai/v1/systemone {model, state, questions}
|
|
4
|
+
// -> {model, answers, usage} (the same shape the oc-voice project uses in decision/systemone.py).
|
|
5
|
+
//
|
|
6
|
+
// This class is URL-agnostic on purpose: the local GLiNER/Open-Jev servers speak the same wire format. The cloud
|
|
7
|
+
// client — the cloud URL and a key — is built in one place only, src/backends.ts cloudClient, and only when the
|
|
8
|
+
// decider's mode is "cloud" (D11/D19). In "local" mode no key is read and no cloud URL is touched.
|
|
9
|
+
//
|
|
10
|
+
// A destination that is not this machine passes the egress gate (src/egress.ts) inside ask(): the redaction, the
|
|
11
|
+
// preview and the user's confirmation happen here, in the one function that makes a request, so a caller with a raw
|
|
12
|
+
// client cannot send anything the gate did not see (D13, A2-T6). A loopback URL is a local server and sends as before.
|
|
13
|
+
import { execFileSync } from "node:child_process";
|
|
14
|
+
import { isStarting, retryAfterMs, startingWaitMs } from "@omacode/core/services";
|
|
15
|
+
import { egress, EgressRefused, hostOf, remote, summary } from "./egress.js";
|
|
16
|
+
import { mapStrings, scrub } from "./redact.js";
|
|
17
|
+
export class DeciderError extends Error {
|
|
18
|
+
}
|
|
19
|
+
// The cloud key: JEV_API_KEY, else the desktop keyring (secret-tool store --label="TypeSafe Jev API key" service
|
|
20
|
+
// typesafe key jev). Only src/backends.ts cloudKey calls this, and only in "cloud" mode: a local start reads neither.
|
|
21
|
+
export function cloudApiKey(env = process.env) {
|
|
22
|
+
if (env.JEV_API_KEY)
|
|
23
|
+
return env.JEV_API_KEY;
|
|
24
|
+
try {
|
|
25
|
+
const key = execFileSync("secret-tool", ["lookup", "service", "typesafe", "key", "jev"], {
|
|
26
|
+
encoding: "utf8",
|
|
27
|
+
timeout: 3000,
|
|
28
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
29
|
+
}).trim();
|
|
30
|
+
return key || undefined;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// Private data is swapped for the vault's placeholders before the state leaves the machine (src/redact.ts); with
|
|
37
|
+
// redaction off, keys and tokens are still masked. A payload for another machine goes through the egress gate instead
|
|
38
|
+
// (src/egress.ts), which redacts a little more and asks the user first.
|
|
39
|
+
export function redact(text) {
|
|
40
|
+
return scrub(text);
|
|
41
|
+
}
|
|
42
|
+
// The question texts carry the same private data the state does, so the wire body gets a redacted copy. The caller's
|
|
43
|
+
// questions stay as they are: the ask flow journals them, and the local backends read them too.
|
|
44
|
+
function wireQuestions(questions) {
|
|
45
|
+
const copy = JSON.parse(JSON.stringify(questions));
|
|
46
|
+
mapStrings(copy, redact);
|
|
47
|
+
return copy;
|
|
48
|
+
}
|
|
49
|
+
export class DecisionClient {
|
|
50
|
+
#key;
|
|
51
|
+
#url;
|
|
52
|
+
#model;
|
|
53
|
+
#timeoutMs;
|
|
54
|
+
#retries;
|
|
55
|
+
#startingWaitMs;
|
|
56
|
+
#maxStateChars;
|
|
57
|
+
#fetch;
|
|
58
|
+
#egress;
|
|
59
|
+
#remote;
|
|
60
|
+
constructor(key, options = {}) {
|
|
61
|
+
this.#key = key;
|
|
62
|
+
this.#url = options.url ?? "https://api.typesafe.ai/v1/systemone";
|
|
63
|
+
this.#model = options.model ?? "jev-latest";
|
|
64
|
+
this.#timeoutMs = options.timeoutMs ?? 15000;
|
|
65
|
+
this.#retries = options.retries ?? 1;
|
|
66
|
+
this.#startingWaitMs = options.startingWaitMs ?? startingWaitMs();
|
|
67
|
+
this.#maxStateChars = options.maxStateChars ?? 2000;
|
|
68
|
+
this.#fetch = options.fetch ?? fetch;
|
|
69
|
+
this.#egress = options.egress ?? {};
|
|
70
|
+
this.#remote = remote(this.#url);
|
|
71
|
+
}
|
|
72
|
+
async ask(state, questions, context) {
|
|
73
|
+
// The newest part of the state is at the end; a cut keeps it.
|
|
74
|
+
const cut = state.length > this.#maxStateChars ? state.slice(-this.#maxStateChars) : state;
|
|
75
|
+
let body;
|
|
76
|
+
let sent;
|
|
77
|
+
let egressSummary;
|
|
78
|
+
if (this.#remote) {
|
|
79
|
+
// Nothing goes out of this machine without passing the gate: it redacts, previews and takes the user's answer, or
|
|
80
|
+
// it refuses and this throws before a request exists.
|
|
81
|
+
const outcome = await egress({ text: cut, questions }, {
|
|
82
|
+
...this.#egress,
|
|
83
|
+
destination: hostOf(this.#url),
|
|
84
|
+
...(context?.node ? { node: context.node } : {}),
|
|
85
|
+
...(context?.job ? { job: context.job } : {}),
|
|
86
|
+
...(context?.task ? { task: context.task } : {}),
|
|
87
|
+
...(context?.session ? { session: context.session } : {}),
|
|
88
|
+
});
|
|
89
|
+
if (outcome.verdict !== "send")
|
|
90
|
+
throw new EgressRefused(outcome);
|
|
91
|
+
sent = outcome.text;
|
|
92
|
+
egressSummary = summary(outcome);
|
|
93
|
+
body = JSON.stringify({ model: this.#model, state: outcome.text, questions: outcome.questions });
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
body = JSON.stringify({ model: this.#model, state: redact(cut), questions: wireQuestions(questions) });
|
|
97
|
+
}
|
|
98
|
+
const started = Date.now();
|
|
99
|
+
for (let attempt = 0;; attempt++) {
|
|
100
|
+
let response;
|
|
101
|
+
try {
|
|
102
|
+
response = await this.#fetch(this.#url, {
|
|
103
|
+
method: "POST",
|
|
104
|
+
headers: { Authorization: `Bearer ${this.#key}`, "Content-Type": "application/json" },
|
|
105
|
+
body,
|
|
106
|
+
signal: AbortSignal.timeout(this.#timeoutMs),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
throw new DeciderError(`decision server unreachable: ${err instanceof Error ? err.name : String(err)}`);
|
|
111
|
+
}
|
|
112
|
+
// The container's supervisor is starting a decider it stopped after an idle time: wait for it rather than fall
|
|
113
|
+
// back, within the wait the environment allows (zero on a desktop, where nothing answers "starting").
|
|
114
|
+
if (isStarting(response) && Date.now() - started + retryAfterMs(response) <= this.#startingWaitMs) {
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, retryAfterMs(response)));
|
|
116
|
+
attempt--;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if ((response.status === 429 || response.status === 529) && attempt < this.#retries) {
|
|
120
|
+
await new Promise((resolve) => setTimeout(resolve, 200 * 2 ** attempt));
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (response.status === 401)
|
|
124
|
+
throw new DeciderError("decision server: API key rejected (401)");
|
|
125
|
+
if (response.status !== 200)
|
|
126
|
+
throw new DeciderError(`decision server: HTTP ${response.status}`);
|
|
127
|
+
const payload = (await response.json());
|
|
128
|
+
return {
|
|
129
|
+
answers: parseAnswers(questions, payload.answers ?? {}),
|
|
130
|
+
model: payload.model ?? this.#model,
|
|
131
|
+
latencyMs: Date.now() - started,
|
|
132
|
+
...(sent !== undefined ? { sent } : {}),
|
|
133
|
+
...(egressSummary ? { egress: egressSummary } : {}),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function num(value, what) {
|
|
139
|
+
if (typeof value !== "number" || Number.isNaN(value))
|
|
140
|
+
throw new DeciderError(`decision server: missing numeric ${what}`);
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
function probs(value) {
|
|
144
|
+
if (!value || typeof value !== "object")
|
|
145
|
+
return {};
|
|
146
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => typeof v === "number"));
|
|
147
|
+
}
|
|
148
|
+
export function parseAnswers(questions, raw) {
|
|
149
|
+
const answers = {};
|
|
150
|
+
for (const [id, question] of Object.entries(questions)) {
|
|
151
|
+
const data = raw[id];
|
|
152
|
+
if (!data)
|
|
153
|
+
continue;
|
|
154
|
+
if (question.type === "choice") {
|
|
155
|
+
const choice = data.choice;
|
|
156
|
+
if (typeof choice !== "string" || !(choice in question.criteria))
|
|
157
|
+
throw new DeciderError(`decision server: ${id}: choice not offered`);
|
|
158
|
+
answers[id] = { type: "choice", choice, confidence: num(data.confidence, `${id}.confidence`), probabilities: probs(data.probabilities) };
|
|
159
|
+
}
|
|
160
|
+
else if (question.type === "score") {
|
|
161
|
+
answers[id] = { type: "score", score: num(data.score, `${id}.score`), confidence: num(data.confidence, `${id}.confidence`), probabilities: probs(data.probabilities) };
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const noul = num(data.noul, `${id}.noul`);
|
|
165
|
+
if (noul < 0 || noul > 1)
|
|
166
|
+
throw new DeciderError(`decision server: ${id}: noul outside 0..1`);
|
|
167
|
+
answers[id] = { type: "noul", noul };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return answers;
|
|
171
|
+
}
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
// The shell page served by the control port (S4, extended in S7). The shell is the only page that talks to the /api/*
|
|
2
|
+
// surface; it carries the session token the server minted and hands it to every API request. Part iframes live on the
|
|
3
|
+
// content origin and never need the token — the overlay posts selections to the shell origin, and the shell relays them
|
|
4
|
+
// to /api/selection with the token it was handed.
|
|
5
|
+
//
|
|
6
|
+
// S7 turns the shell into the canvas: one cross-origin iframe per part, a selection panel, a prompt box scoped to the
|
|
7
|
+
// selection, and a status line from /api/state. No third-party assets, tiny vanilla CSS, and it survives a reload. The
|
|
8
|
+
// token is carried in a head script (not the HTML body) so a page capture does not leak it; the S4 contract — the shell
|
|
9
|
+
// has the token and reaches the API with it — is preserved. The shell also subscribes to /api/events itself (S6), so a
|
|
10
|
+
// part the agent committed is redrawn in place without a page reload.
|
|
11
|
+
function escapeAttr(value) {
|
|
12
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
13
|
+
}
|
|
14
|
+
function escapeText(value) {
|
|
15
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
16
|
+
}
|
|
17
|
+
// A JSON literal is safe inside a script tag only when `<` cannot close the tag.
|
|
18
|
+
function scriptJson(value) {
|
|
19
|
+
return JSON.stringify(value).replace(/</g, "\\u003c");
|
|
20
|
+
}
|
|
21
|
+
function partSrc(contentOrigin, part) {
|
|
22
|
+
const base = contentOrigin.replace(/\/+$/, "");
|
|
23
|
+
const dir = part.path.replace(/^\/+|\/+$/g, "");
|
|
24
|
+
const entry = part.entry.replace(/^\/+/g, "");
|
|
25
|
+
const path = dir === "" || dir === "." ? entry : `${dir}/${entry}`;
|
|
26
|
+
// An entry may carry its own query (`index.html?theme=dark`). It stays verbatim, and the part id is appended as one
|
|
27
|
+
// more parameter so the content port injects this part's overlay while the framed page keeps its original query.
|
|
28
|
+
const separator = path.indexOf("?");
|
|
29
|
+
const route = separator < 0 ? path : path.slice(0, separator);
|
|
30
|
+
const query = separator < 0 ? "" : path.slice(separator);
|
|
31
|
+
const url = `${base}/${route.split("/").filter(Boolean).map(encodeURIComponent).join("/")}${query}`;
|
|
32
|
+
return `${url}${query ? "&" : "?"}__design_part=${encodeURIComponent(part.id)}`;
|
|
33
|
+
}
|
|
34
|
+
export function designShellHtml(options) {
|
|
35
|
+
const parts = options.parts ?? [];
|
|
36
|
+
const sliders = options.sliders ?? [];
|
|
37
|
+
const config = scriptJson({
|
|
38
|
+
token: options.token,
|
|
39
|
+
contentOrigin: options.contentOrigin,
|
|
40
|
+
shellOrigin: options.shellOrigin,
|
|
41
|
+
parts: parts.map((p) => ({ id: p.id, kind: p.kind, path: p.path, entry: p.entry, driver: p.driver })),
|
|
42
|
+
sliders: sliders.map((s) => ({ token: s.token, label: s.label, min: s.min, max: s.max, step: s.step })),
|
|
43
|
+
tokens: options.tokens ?? {},
|
|
44
|
+
});
|
|
45
|
+
const title = escapeAttr("Design canvas");
|
|
46
|
+
// S13 · The slider panel. One slider per control; the panel shows the commit the write produced, because a
|
|
47
|
+
// slider drag is a real change to the design's files and the user must be able to see that it landed.
|
|
48
|
+
const sliderRows = sliders
|
|
49
|
+
.map((s) => {
|
|
50
|
+
const id = escapeAttr(`slider-${s.token}`);
|
|
51
|
+
return `<div class="slider">
|
|
52
|
+
<label for="${id}">${escapeText(s.label)}</label>
|
|
53
|
+
<input type="range" id="${id}" data-token="${escapeAttr(s.token)}" min="${s.min}" max="${s.max}" step="${s.step}">
|
|
54
|
+
</div>`;
|
|
55
|
+
})
|
|
56
|
+
.join("\n");
|
|
57
|
+
const frames = parts
|
|
58
|
+
.map((part) => {
|
|
59
|
+
const driver = part.driver ? ` data-driver="${escapeAttr(part.driver)}"` : "";
|
|
60
|
+
const src = escapeAttr(partSrc(options.contentOrigin, part));
|
|
61
|
+
const id = escapeAttr(part.id);
|
|
62
|
+
const label = escapeText(part.id);
|
|
63
|
+
// S15 · A play-driver composition gets a small transport in its header: the canvas drives the deck's
|
|
64
|
+
// timeline through the injected overlay, so the user can play, pause or restart without leaving the canvas.
|
|
65
|
+
// Every other driver (or none) renders nothing — there is no timeline to drive.
|
|
66
|
+
const transport = part.driver === "play"
|
|
67
|
+
? `<span class="part-transport">${["play", "pause", "restart"].map((action) => `<button type="button" data-driver-action="${action}">${action[0].toUpperCase()}${action.slice(1)}</button>`).join("")}</span>`
|
|
68
|
+
: "";
|
|
69
|
+
return `<section class="part" data-part-id="${id}">
|
|
70
|
+
<header class="part-head"><span class="part-id">${label}</span>${part.driver ? `<span class="part-driver">${escapeText(part.driver)}</span>` : ""}${transport}</header>
|
|
71
|
+
<iframe title="${id}" src="${src}" data-part-id="${id}"${driver} loading="lazy"></iframe>
|
|
72
|
+
</section>`;
|
|
73
|
+
})
|
|
74
|
+
.join("\n");
|
|
75
|
+
return `<!doctype html>
|
|
76
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
|
77
|
+
<title>${title}</title>
|
|
78
|
+
<style>
|
|
79
|
+
:root{color-scheme:dark;--bg:#111827;--panel:#1f2937;--line:#374151;--text:#f9fafb;--muted:#9ca3af;--accent:#60a5fa;--ok:#34d399}
|
|
80
|
+
*{box-sizing:border-box}html,body{margin:0;min-height:100%;background:var(--bg);color:var(--text);font:14px/1.4 system-ui,sans-serif}
|
|
81
|
+
body{padding:16px}h1{margin:0 0 4px;font-size:20px}p{margin:0}.muted{color:var(--muted)}
|
|
82
|
+
.layout{display:grid;grid-template-columns:minmax(0,1fr)320px;gap:16px;align-items:start}
|
|
83
|
+
.canvas{display:grid;gap:16px}.part{background:var(--panel);border:1px solid var(--line);border-radius:8px;overflow:hidden}
|
|
84
|
+
.part-head{display:flex;gap:8px;align-items:center;padding:8px 12px;border-bottom:1px solid var(--line);font-size:12px}
|
|
85
|
+
.part-id{font-weight:600}.part-driver{color:var(--muted);border:1px solid var(--line);border-radius:999px;padding:1px 8px}
|
|
86
|
+
.part-transport{display:inline-flex;gap:4px;margin-left:auto}
|
|
87
|
+
.part-transport button{margin-top:0;background:transparent;color:var(--muted);border:1px solid var(--line);border-radius:999px;padding:1px 8px;font:600 11px system-ui,sans-serif;cursor:pointer}
|
|
88
|
+
.part-transport button:hover{color:var(--text);border-color:var(--accent)}
|
|
89
|
+
.part-stale{color:#fbbf24;border:1px solid #fbbf24;border-radius:999px;padding:1px 8px;font-size:11px}
|
|
90
|
+
.part iframe{display:block;width:100%;height:420px;border:0;background:#fff}
|
|
91
|
+
.side{display:grid;gap:12px;position:sticky;top:16px}.card{background:var(--panel);border:1px solid var(--line);border-radius:8px;padding:12px}
|
|
92
|
+
.card h2{margin:0 0 8px;font-size:13px;color:var(--muted);text-transform:uppercase;letter-spacing:.04em}
|
|
93
|
+
#status{font-size:13px}#status.ok{color:var(--ok)}
|
|
94
|
+
.kv{display:grid;gap:6px;font-size:12px}.kv div{display:grid;gap:2px}.kv dt{color:var(--muted)}.kv dd{margin:0;word-break:break-word}
|
|
95
|
+
pre{margin:0;white-space:pre-wrap;word-break:break-word;font:12px/1.4 ui-monospace,monospace;max-height:180px;overflow:auto}
|
|
96
|
+
label{display:block;font-size:12px;color:var(--muted);margin-bottom:4px}
|
|
97
|
+
textarea{width:100%;min-height:88px;resize:vertical;background:#111827;color:var(--text);border:1px solid var(--line);border-radius:6px;padding:8px;font:13px/1.4 system-ui,sans-serif}
|
|
98
|
+
button{margin-top:8px;background:var(--accent);color:#111827;border:0;border-radius:6px;padding:8px 12px;font:600 13px system-ui,sans-serif;cursor:pointer}
|
|
99
|
+
button[disabled]{opacity:.5;cursor:default}.row{display:flex;justify-content:space-between;gap:8px;align-items:center;margin-top:8px;font-size:12px;color:var(--muted)}
|
|
100
|
+
.empty{padding:24px;border:1px dashed var(--line);border-radius:8px;color:var(--muted)}
|
|
101
|
+
.slider{display:grid;gap:4px;margin-bottom:10px}.slider input{width:100%;accent-color:var(--accent)}
|
|
102
|
+
.mono{font-family:ui-monospace,monospace}#commit{color:var(--ok)}#commit.stale{color:#f87171}
|
|
103
|
+
@media (max-width:900px){.layout{grid-template-columns:1fr}.side{position:static}}
|
|
104
|
+
</style>
|
|
105
|
+
<script>
|
|
106
|
+
(() => {
|
|
107
|
+
const cfg = ${config};
|
|
108
|
+
const headers = { authorization: 'Bearer ' + cfg.token, 'content-type': 'application/json' };
|
|
109
|
+
// S4 contract: shell reaches /api/health with the token it was handed.
|
|
110
|
+
window.__designShell = Object.freeze({
|
|
111
|
+
token: cfg.token,
|
|
112
|
+
contentOrigin: cfg.contentOrigin,
|
|
113
|
+
shellOrigin: cfg.shellOrigin,
|
|
114
|
+
parts: cfg.parts,
|
|
115
|
+
fetch: (path, init = {}) => {
|
|
116
|
+
const next = new Headers(init.headers || {});
|
|
117
|
+
next.set('authorization', 'Bearer ' + cfg.token);
|
|
118
|
+
return fetch(path, { ...init, headers: next });
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
})();
|
|
122
|
+
</script>
|
|
123
|
+
</head><body>
|
|
124
|
+
<main>
|
|
125
|
+
<h1>Design canvas</h1>
|
|
126
|
+
<p class="muted">Control origin. Select an element in a part, then describe the change.</p>
|
|
127
|
+
<div class="layout" style="margin-top:16px">
|
|
128
|
+
<section class="canvas" id="canvas" aria-label="Parts">${frames || `<div class="empty">No parts in this design session.</div>`}</section>
|
|
129
|
+
<aside class="side">
|
|
130
|
+
<section class="card" aria-labelledby="status-title"><h2 id="status-title">Status</h2><div id="status">loading…</div></section>
|
|
131
|
+
<section class="card" aria-labelledby="selection-title"><h2 id="selection-title">Selection</h2>
|
|
132
|
+
<dl class="kv">
|
|
133
|
+
<div><dt>Part</dt><dd id="sel-part">none</dd></div>
|
|
134
|
+
<div><dt>Path</dt><dd id="sel-path">none</dd></div>
|
|
135
|
+
<div><dt>Computed style</dt><dd><pre id="sel-style">none</pre></dd></div>
|
|
136
|
+
<div><dt>outerHTML</dt><dd><pre id="sel-html">none</pre></dd></div>
|
|
137
|
+
</dl>
|
|
138
|
+
</section>
|
|
139
|
+
<section class="card" aria-labelledby="tokens-title"><h2 id="tokens-title">Tokens</h2>
|
|
140
|
+
<div id="sliders">${sliderRows || `<div class="muted">No token sliders in this design.</div>`}</div>
|
|
141
|
+
<div class="row"><span id="token-state">idle</span><span id="commit" class="mono"></span></div>
|
|
142
|
+
</section>
|
|
143
|
+
<section class="card" aria-labelledby="prompt-title"><h2 id="prompt-title">Prompt</h2>
|
|
144
|
+
<label for="prompt">Intent for the selected element</label>
|
|
145
|
+
<textarea id="prompt" placeholder="Describe the change…" disabled></textarea>
|
|
146
|
+
<div class="row"><span id="queued">Queued: 0</span><span id="prompt-state">select an element</span></div>
|
|
147
|
+
<button id="send" type="button" disabled>Send intent</button>
|
|
148
|
+
</section>
|
|
149
|
+
</aside>
|
|
150
|
+
</div>
|
|
151
|
+
</main>
|
|
152
|
+
<script>
|
|
153
|
+
(() => {
|
|
154
|
+
const cfg = window.__designShell;
|
|
155
|
+
if (!cfg) return;
|
|
156
|
+
const auth = { authorization: 'Bearer ' + cfg.token };
|
|
157
|
+
const json = { 'content-type': 'application/json', authorization: 'Bearer ' + cfg.token };
|
|
158
|
+
const status = document.getElementById('status');
|
|
159
|
+
const selPart = document.getElementById('sel-part');
|
|
160
|
+
const selPath = document.getElementById('sel-path');
|
|
161
|
+
const selStyle = document.getElementById('sel-style');
|
|
162
|
+
const selHtml = document.getElementById('sel-html');
|
|
163
|
+
const prompt = document.getElementById('prompt');
|
|
164
|
+
const send = document.getElementById('send');
|
|
165
|
+
const queued = document.getElementById('queued');
|
|
166
|
+
const promptState = document.getElementById('prompt-state');
|
|
167
|
+
let selection = null;
|
|
168
|
+
// A send is confirmed until the user starts the next one: the state poll re-renders the same selection every 2 s,
|
|
169
|
+
// and without this flag its "scoped to selection" would erase the one line that says the agent was woken.
|
|
170
|
+
let justSent = false;
|
|
171
|
+
|
|
172
|
+
function esc(s) { return String(s == null ? '' : s); }
|
|
173
|
+
// The heartbeat (S8): every ping is an authorized /api/health request, and the server refreshes the one-writer
|
|
174
|
+
// lock's lastHeartbeat on it. A shell that stays open therefore keeps its session's lock live.
|
|
175
|
+
async function ping() {
|
|
176
|
+
try {
|
|
177
|
+
const res = await fetch('/api/health', { headers: auth });
|
|
178
|
+
const body = await res.json();
|
|
179
|
+
if (status && !(body && body.ok === false)) status.textContent = 'ready';
|
|
180
|
+
} catch {
|
|
181
|
+
if (status) status.textContent = 'disconnected';
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function showSelection(data) {
|
|
185
|
+
selection = data || null;
|
|
186
|
+
const has = !!(selection && selection.partId);
|
|
187
|
+
if (selPart) selPart.textContent = has ? esc(selection.partId) : 'none';
|
|
188
|
+
if (selPath) selPath.textContent = has ? esc(selection.cssPath || '(part)') : 'none';
|
|
189
|
+
if (selStyle) selStyle.textContent = has ? JSON.stringify(selection.computedStyles || {}, null, 2) : 'none';
|
|
190
|
+
if (selHtml) selHtml.textContent = has ? esc(selection.outerHTML || '') : 'none';
|
|
191
|
+
if (prompt) prompt.disabled = !has;
|
|
192
|
+
if (send) send.disabled = !has || !prompt || !prompt.value.trim();
|
|
193
|
+
if (promptState) promptState.textContent = has ? 'scoped to selection' : 'select an element';
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function refreshState() {
|
|
197
|
+
try {
|
|
198
|
+
const res = await fetch('/api/state', { headers: auth });
|
|
199
|
+
if (!res.ok) throw new Error('bad');
|
|
200
|
+
const body = await res.json();
|
|
201
|
+
if (status) {
|
|
202
|
+
status.textContent = body && body.ok === false ? 'error' : 'ready';
|
|
203
|
+
status.className = body && body.ok === false ? '' : 'ok';
|
|
204
|
+
}
|
|
205
|
+
// Two counts, because they mean different things to the user: "queued" is waiting for the agent, "working" is
|
|
206
|
+
// already drained into a turn. The poll is every 2 s, so a click that just landed is visible without a reload.
|
|
207
|
+
if (queued) {
|
|
208
|
+
const waiting = Number((body && (body.queued ?? body.pending ?? body.queue?.length)) ?? 0);
|
|
209
|
+
const working = Number((body && body.inflight) ?? 0);
|
|
210
|
+
queued.textContent = 'Queued: ' + waiting + ' · working: ' + working;
|
|
211
|
+
}
|
|
212
|
+
// S15 · A stale build: the part's source is newer than the built entry it was served from, so the canvas
|
|
213
|
+
// is showing yesterday's build. Amber rather than red — everything still works, it is just behind the files.
|
|
214
|
+
// The tag is created once and removed when the build catches up, so the poll can toggle it freely.
|
|
215
|
+
for (const part of (body && body.parts) || []) {
|
|
216
|
+
const section = part && part.id ? document.querySelector('section[data-part-id="' + String(part.id).replace(/"/g, '\\"') + '"]') : null;
|
|
217
|
+
if (!section) continue;
|
|
218
|
+
if (part.stale) {
|
|
219
|
+
section.setAttribute('data-stale', 'true');
|
|
220
|
+
let tag = section.querySelector('.part-stale');
|
|
221
|
+
if (!tag) {
|
|
222
|
+
tag = document.createElement('span');
|
|
223
|
+
tag.className = 'part-stale';
|
|
224
|
+
tag.textContent = 'stale build';
|
|
225
|
+
const id = section.querySelector('.part-id');
|
|
226
|
+
if (id && id.parentNode) id.parentNode.insertBefore(tag, id.nextSibling);
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
section.removeAttribute('data-stale');
|
|
230
|
+
const tag = section.querySelector('.part-stale');
|
|
231
|
+
if (tag) tag.remove();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (body && body.selection) {
|
|
235
|
+
showSelection(body.selection);
|
|
236
|
+
if (justSent && promptState) promptState.textContent = 'sent to the agent';
|
|
237
|
+
}
|
|
238
|
+
} catch {
|
|
239
|
+
if (status) { status.textContent = 'disconnected'; status.className = ''; }
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
window.addEventListener('message', (event) => {
|
|
244
|
+
if (event.origin !== cfg.contentOrigin) return;
|
|
245
|
+
const data = event.data;
|
|
246
|
+
if (!data || data.type !== 'design:selection') return;
|
|
247
|
+
justSent = false;
|
|
248
|
+
showSelection(data);
|
|
249
|
+
fetch('/api/selection', {
|
|
250
|
+
method: 'POST',
|
|
251
|
+
headers: json,
|
|
252
|
+
body: JSON.stringify(data),
|
|
253
|
+
}).catch(() => {});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// S6 · The reload stream. The control port pushes one reload per changed part; the shell redraws exactly that
|
|
257
|
+
// part's iframe instead of the whole page, so a commit does not cost the user their place in the canvas.
|
|
258
|
+
// EventSource cannot send an Authorization header, so the session token travels in the query — the same token the
|
|
259
|
+
// shell already carries. The frame's src carries the reload's hash as a cache-busting parameter because the
|
|
260
|
+
// content port answers no-store yet a browser may keep the old document when the src is merely reassigned.
|
|
261
|
+
const frames = {};
|
|
262
|
+
for (const frame of document.querySelectorAll('iframe[data-part-id]')) frames[frame.dataset.partId] = frame;
|
|
263
|
+
// S15 · The transport posts the clicked action to that part's frame; the injected overlay turns it into
|
|
264
|
+
// window.__player calls (or a CustomEvent a deck can listen for on document). The message targets the content
|
|
265
|
+
// origin the part is served from, and a missing frame is a no-op: the part may have left the DOM since the click.
|
|
266
|
+
document.addEventListener('click', (event) => {
|
|
267
|
+
const target = event.target;
|
|
268
|
+
const btn = target && target.closest ? target.closest('button[data-driver-action]') : null;
|
|
269
|
+
if (!btn) return;
|
|
270
|
+
const section = btn.closest('section[data-part-id]');
|
|
271
|
+
const frame = section && frames[section.dataset.partId];
|
|
272
|
+
if (!frame || !frame.contentWindow) return;
|
|
273
|
+
frame.contentWindow.postMessage({ type: 'design:driver', action: btn.dataset.driverAction }, cfg.contentOrigin);
|
|
274
|
+
});
|
|
275
|
+
const reloadTimers = Object.create(null);
|
|
276
|
+
function reloadFrame(partId, hash) {
|
|
277
|
+
const frame = frames[partId];
|
|
278
|
+
if (!frame) return;
|
|
279
|
+
let next;
|
|
280
|
+
try {
|
|
281
|
+
next = new URL(frame.src, window.location.href);
|
|
282
|
+
} catch {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
// Keep the part parameter the content port reads to inject that part's overlay; only the revision changes.
|
|
286
|
+
next.searchParams.set('__design_r', String(hash || Date.now()));
|
|
287
|
+
frame.src = next.toString();
|
|
288
|
+
}
|
|
289
|
+
// A save can produce a burst of file events, so each part has its own 200 ms debounce: the burst redraws the frame
|
|
290
|
+
// once, at its last revision. This is the client half of the S6 contract; the server debounces as well.
|
|
291
|
+
function queueReload(partId, hash) {
|
|
292
|
+
if (!partId) return;
|
|
293
|
+
if (reloadTimers[partId]) clearTimeout(reloadTimers[partId]);
|
|
294
|
+
reloadTimers[partId] = setTimeout(() => {
|
|
295
|
+
delete reloadTimers[partId];
|
|
296
|
+
reloadFrame(partId, hash);
|
|
297
|
+
}, 200);
|
|
298
|
+
}
|
|
299
|
+
(function subscribe() {
|
|
300
|
+
// The control port's own origin: the shell is served from it, so window.location.origin is the source of truth.
|
|
301
|
+
const base = (window.location && window.location.origin) || cfg.shellOrigin;
|
|
302
|
+
const stream = new EventSource(base + '/api/events?token=' + encodeURIComponent(cfg.token));
|
|
303
|
+
stream.addEventListener('reload', (event) => {
|
|
304
|
+
let data;
|
|
305
|
+
try {
|
|
306
|
+
data = JSON.parse(event.data);
|
|
307
|
+
} catch {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (!data || data.type !== 'reload' || !data.partId) return;
|
|
311
|
+
queueReload(data.partId, data.hash);
|
|
312
|
+
});
|
|
313
|
+
// EventSource reconnects on its own, so the page only reports the drop in the existing status line; closing and
|
|
314
|
+
// reopening the stream here would fight the browser's own retry.
|
|
315
|
+
stream.addEventListener('error', () => {
|
|
316
|
+
if (status) { status.textContent = 'disconnected'; status.className = ''; }
|
|
317
|
+
});
|
|
318
|
+
})();
|
|
319
|
+
|
|
320
|
+
if (prompt) prompt.addEventListener('input', () => {
|
|
321
|
+
justSent = false;
|
|
322
|
+
if (promptState && selection) promptState.textContent = prompt.value.trim() ? 'ready to send' : 'scoped to selection';
|
|
323
|
+
if (send) send.disabled = !selection || !prompt.value.trim();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
if (send) send.addEventListener('click', async () => {
|
|
327
|
+
if (!selection || !prompt || !prompt.value.trim()) return;
|
|
328
|
+
send.disabled = true;
|
|
329
|
+
if (promptState) promptState.textContent = 'sending…';
|
|
330
|
+
try {
|
|
331
|
+
const res = await fetch('/api/intent', {
|
|
332
|
+
method: 'POST',
|
|
333
|
+
headers: json,
|
|
334
|
+
body: JSON.stringify({
|
|
335
|
+
intent: prompt.value,
|
|
336
|
+
prompt: prompt.value,
|
|
337
|
+
partId: selection.partId,
|
|
338
|
+
cssPath: selection.cssPath,
|
|
339
|
+
outerHTML: selection.outerHTML,
|
|
340
|
+
computedStyles: selection.computedStyles,
|
|
341
|
+
}),
|
|
342
|
+
});
|
|
343
|
+
if (res.ok) {
|
|
344
|
+
prompt.value = '';
|
|
345
|
+
// The server has the intent; the host may already be starting the turn. Say what happened rather than
|
|
346
|
+
// "queued", which read like nothing would happen until the user typed in the terminal.
|
|
347
|
+
justSent = true;
|
|
348
|
+
if (promptState) promptState.textContent = 'sent to the agent';
|
|
349
|
+
} else {
|
|
350
|
+
if (promptState) promptState.textContent = 'error';
|
|
351
|
+
}
|
|
352
|
+
} catch {
|
|
353
|
+
if (promptState) promptState.textContent = 'error';
|
|
354
|
+
}
|
|
355
|
+
await refreshState();
|
|
356
|
+
if (send) send.disabled = !selection || !prompt || !prompt.value.trim();
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// S13 · The sliders write the design tokens. A drag is a local write, not a model call: the tokens go to the
|
|
360
|
+
// session's own server, which commits them and answers with the commit hash.
|
|
361
|
+
const sliderBox = document.getElementById('sliders');
|
|
362
|
+
const tokenState = document.getElementById('token-state');
|
|
363
|
+
const commitBox = document.getElementById('commit');
|
|
364
|
+
let tokenHash = '';
|
|
365
|
+
let tokenPart = cfg.parts && cfg.parts[0] ? cfg.parts[0].id : '';
|
|
366
|
+
let tokenValues = Object.assign({}, cfg.tokens || {});
|
|
367
|
+
|
|
368
|
+
function setCommit(text, stale) {
|
|
369
|
+
if (!commitBox) return;
|
|
370
|
+
commitBox.textContent = text || '';
|
|
371
|
+
commitBox.className = stale ? 'mono stale' : 'mono';
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async function loadTokens() {
|
|
375
|
+
if (!sliderBox || !tokenPart) return;
|
|
376
|
+
try {
|
|
377
|
+
const res = await fetch('/api/tokens?partId=' + encodeURIComponent(tokenPart), { headers: auth });
|
|
378
|
+
if (!res.ok) throw new Error('bad');
|
|
379
|
+
const body = await res.json();
|
|
380
|
+
tokenHash = (body && body.hash) || '';
|
|
381
|
+
tokenValues = Object.assign({}, (body && body.tokens) || {});
|
|
382
|
+
for (const input of sliderBox.querySelectorAll('input[type=range]')) {
|
|
383
|
+
const value = tokenValues[input.dataset.token];
|
|
384
|
+
if (typeof value === 'number') input.value = String(value);
|
|
385
|
+
}
|
|
386
|
+
if (tokenState) tokenState.textContent = 'ready';
|
|
387
|
+
} catch {
|
|
388
|
+
if (tokenState) tokenState.textContent = 'unavailable';
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// One write in flight at a time: a drag sends many events, and the server refuses a write whose hash is stale,
|
|
393
|
+
// so the panel waits for the answer before it sends the next one.
|
|
394
|
+
let writing = false;
|
|
395
|
+
let pending = null;
|
|
396
|
+
async function writeToken(token, value) {
|
|
397
|
+
if (writing) { pending = [token, value]; return; }
|
|
398
|
+
writing = true;
|
|
399
|
+
if (tokenState) tokenState.textContent = 'writing…';
|
|
400
|
+
try {
|
|
401
|
+
const res = await fetch('/api/tokens', {
|
|
402
|
+
method: 'PUT',
|
|
403
|
+
headers: json,
|
|
404
|
+
body: JSON.stringify({ partId: tokenPart, tokens: Object.assign({}, tokenValues, { [token]: value }), hash: tokenHash }),
|
|
405
|
+
});
|
|
406
|
+
const body = await res.json().catch(() => ({}));
|
|
407
|
+
if (res.ok) {
|
|
408
|
+
tokenHash = (body && body.hash) || tokenHash;
|
|
409
|
+
tokenValues[token] = value;
|
|
410
|
+
setCommit((body && body.commit) ? String(body.commit).slice(0, 12) : 'committed', false);
|
|
411
|
+
if (tokenState) tokenState.textContent = 'committed';
|
|
412
|
+
} else {
|
|
413
|
+
setCommit('refused', true);
|
|
414
|
+
if (tokenState) tokenState.textContent = 'refused';
|
|
415
|
+
}
|
|
416
|
+
} catch {
|
|
417
|
+
setCommit('error', true);
|
|
418
|
+
if (tokenState) tokenState.textContent = 'error';
|
|
419
|
+
} finally {
|
|
420
|
+
writing = false;
|
|
421
|
+
if (pending) { const next = pending; pending = null; writeToken(next[0], next[1]); }
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (sliderBox) {
|
|
426
|
+
// A drag is one change, so the panel writes when the drag ends (the change event) rather than on every step: a
|
|
427
|
+
// drag that crosses twenty values lands as one write and one commit, not twenty.
|
|
428
|
+
sliderBox.addEventListener('change', (event) => {
|
|
429
|
+
const input = event.target;
|
|
430
|
+
if (!input || input.tagName !== 'INPUT' || !input.dataset || !input.dataset.token) return;
|
|
431
|
+
writeToken(input.dataset.token, Number(input.value));
|
|
432
|
+
});
|
|
433
|
+
loadTokens();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
refreshState();
|
|
437
|
+
setInterval(refreshState, 2000);
|
|
438
|
+
ping();
|
|
439
|
+
// The heartbeat (S8): every ping is an authorized /api/health request, and the server refreshes the one-writer
|
|
440
|
+
// lock's lastHeartbeat on it. The interval sits well inside the lock's 60 s heartbeat timeout, so a shell that
|
|
441
|
+
// stays open keeps the session alive for as long as the page is up.
|
|
442
|
+
setInterval(ping, 15000);
|
|
443
|
+
})();
|
|
444
|
+
</script>
|
|
445
|
+
</main>
|
|
446
|
+
</body></html>
|
|
447
|
+
`;
|
|
448
|
+
}
|