@open-qa/qa-stage 0.0.0-stage → 0.1.0-alpha.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 +204 -0
- package/NOTICE +7 -0
- package/THIRD_PARTY.md +13 -0
- package/THIRD_PARTY_NOTICES.md +11 -0
- package/package.json +68 -4
- package/scripts/bundle.d.ts +7 -0
- package/scripts/bundle.js +78 -0
- package/src/auth.d.ts +10 -0
- package/src/auth.js +26 -0
- package/src/blackboard-tools.d.ts +2 -0
- package/src/blackboard-tools.js +46 -0
- package/src/config.d.ts +61 -0
- package/src/config.js +64 -0
- package/src/discord-api.d.ts +47 -0
- package/src/discord-api.js +151 -0
- package/src/discord-surface.d.ts +4 -0
- package/src/discord-surface.js +218 -0
- package/src/drivers.d.ts +21 -0
- package/src/drivers.js +12 -0
- package/src/email-message.d.ts +22 -0
- package/src/email-message.js +114 -0
- package/src/email-surface.d.ts +4 -0
- package/src/email-surface.js +140 -0
- package/src/event-log.d.ts +18 -0
- package/src/event-log.js +46 -0
- package/src/expect-engine.d.ts +15 -0
- package/src/expect-engine.js +114 -0
- package/src/expect-evidence.d.ts +7 -0
- package/src/expect-evidence.js +68 -0
- package/src/flow-tools.d.ts +2 -0
- package/src/flow-tools.js +26 -0
- package/src/http-expect.d.ts +4 -0
- package/src/http-expect.js +68 -0
- package/src/index.d.ts +13 -0
- package/src/index.js +37 -0
- package/src/main.d.ts +1 -0
- package/src/main.js +51 -0
- package/src/mcp.d.ts +14 -0
- package/src/mcp.js +23 -0
- package/src/observation-tools.d.ts +3 -0
- package/src/observation-tools.js +39 -0
- package/src/page-diagnostics.d.ts +19 -0
- package/src/page-diagnostics.js +53 -0
- package/src/page-pixels.d.ts +35 -0
- package/src/page-pixels.js +73 -0
- package/src/page-policy.d.ts +48 -0
- package/src/page-policy.js +125 -0
- package/src/page-recording.d.ts +39 -0
- package/src/page-recording.js +532 -0
- package/src/page-replay.d.ts +7 -0
- package/src/page-replay.js +69 -0
- package/src/page-views.d.ts +85 -0
- package/src/page-views.js +338 -0
- package/src/rendered.d.ts +6 -0
- package/src/rendered.js +135 -0
- package/src/replay-artifacts.d.ts +2 -0
- package/src/replay-artifacts.js +100 -0
- package/src/replay-assets.d.ts +21 -0
- package/src/replay-assets.js +106 -0
- package/src/replay-budget.d.ts +22 -0
- package/src/replay-budget.js +47 -0
- package/src/replay-files.d.ts +3 -0
- package/src/replay-files.js +18 -0
- package/src/replay-privacy.d.ts +10 -0
- package/src/replay-privacy.js +48 -0
- package/src/replay-script.d.ts +2 -0
- package/src/replay-script.js +65 -0
- package/src/replay-warnings.d.ts +6 -0
- package/src/replay-warnings.js +16 -0
- package/src/replay-writer.d.ts +24 -0
- package/src/replay-writer.js +92 -0
- package/src/screen-tools.d.ts +5 -0
- package/src/screen-tools.js +168 -0
- package/src/server.d.ts +50 -0
- package/src/server.js +537 -0
- package/src/signed-artifacts.d.ts +21 -0
- package/src/signed-artifacts.js +127 -0
- package/src/sqlite-parser.d.ts +3 -0
- package/src/stage-clock.d.ts +2 -0
- package/src/stage-clock.js +7 -0
- package/src/surface-adapter.d.ts +65 -0
- package/src/surface-adapter.js +0 -0
- package/src/system-common.d.ts +34 -0
- package/src/system-common.js +123 -0
- package/src/system-db.d.ts +9 -0
- package/src/system-db.js +143 -0
- package/src/system-github.d.ts +6 -0
- package/src/system-github.js +73 -0
- package/src/system-http.d.ts +4 -0
- package/src/system-http.js +86 -0
- package/src/system-logs.d.ts +6 -0
- package/src/system-logs.js +78 -0
- package/src/telegram-api.d.ts +162 -0
- package/src/telegram-api.js +170 -0
- package/src/telegram-markup.d.ts +4 -0
- package/src/telegram-markup.js +129 -0
- package/src/telegram-surface.d.ts +39 -0
- package/src/telegram-surface.js +448 -0
- package/src/tool-gateway.d.ts +41 -0
- package/src/tool-gateway.js +696 -0
- package/src/tool-limits.d.ts +12 -0
- package/src/tool-limits.js +41 -0
- package/src/tool-secrets.d.ts +8 -0
- package/src/tool-secrets.js +38 -0
- package/src/vendor/gmail.d.ts +45 -0
- package/src/vendor/gmail.js +193 -0
- package/src/watch.d.ts +11 -0
- package/src/watch.js +30 -0
- package/src/web-expect.d.ts +5 -0
- package/src/web-expect.js +130 -0
- package/src/web-tools.d.ts +15 -0
- package/src/web-tools.js +340 -0
- package/README.md +0 -3
package/src/server.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import type { Clock } from "@open-qa/qa-engine";
|
|
3
|
+
import { type Principal } from "./auth.js";
|
|
4
|
+
import type { StageConfig } from "./config.js";
|
|
5
|
+
import { type StageDrivers } from "./drivers.js";
|
|
6
|
+
import { EventLog } from "./event-log.js";
|
|
7
|
+
import { type RegisterTools } from "./mcp.js";
|
|
8
|
+
import { type ToolGatewayOptions } from "./tool-gateway.js";
|
|
9
|
+
import type { TelegramApiOptions } from "./telegram-api.js";
|
|
10
|
+
export declare const MAX_EVENT_STREAMS = 16;
|
|
11
|
+
export interface StageExtensions {
|
|
12
|
+
telegram?: TelegramApiOptions;
|
|
13
|
+
discordFetch?: typeof fetch;
|
|
14
|
+
emailFetch?: typeof fetch;
|
|
15
|
+
gateway?: ToolGatewayOptions;
|
|
16
|
+
drivers?: Partial<StageDrivers>;
|
|
17
|
+
registerTools?: RegisterTools;
|
|
18
|
+
clock?: Clock;
|
|
19
|
+
/** Values to mask, never exposed through agent secret references. */
|
|
20
|
+
redactions?: readonly string[];
|
|
21
|
+
watch?: {
|
|
22
|
+
token: string;
|
|
23
|
+
frames: (signal: AbortSignal) => Promise<{
|
|
24
|
+
view: string;
|
|
25
|
+
png: string;
|
|
26
|
+
}[]>;
|
|
27
|
+
};
|
|
28
|
+
onRedactions?: (values: readonly string[]) => void;
|
|
29
|
+
}
|
|
30
|
+
export interface StageAppOptions extends StageExtensions {
|
|
31
|
+
tokens: Pick<StageConfig, "conductorToken" | "actorTokens">;
|
|
32
|
+
log: EventLog;
|
|
33
|
+
clock: Clock;
|
|
34
|
+
}
|
|
35
|
+
export interface StageApp {
|
|
36
|
+
app: Hono<{
|
|
37
|
+
Variables: {
|
|
38
|
+
principal: Principal;
|
|
39
|
+
};
|
|
40
|
+
}>;
|
|
41
|
+
addRedactions(values: readonly string[]): void;
|
|
42
|
+
close(): void;
|
|
43
|
+
}
|
|
44
|
+
export declare function createStageApp(options: StageAppOptions): StageApp;
|
|
45
|
+
export interface RunningStage {
|
|
46
|
+
url: string;
|
|
47
|
+
log: EventLog;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export declare function startStageServer(config: StageConfig, extensions?: StageExtensions): Promise<RunningStage>;
|
package/src/server.js
ADDED
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import { registerWatch } from "./watch.js";
|
|
2
|
+
import { GmailInbox } from "./vendor/gmail.js";
|
|
3
|
+
import { emailSurface } from "./email-surface.js";
|
|
4
|
+
import { mkdir } from "node:fs/promises";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { serve } from "@hono/node-server";
|
|
7
|
+
import { Hono } from "hono";
|
|
8
|
+
import { bodyLimit } from "hono/body-limit";
|
|
9
|
+
import { HTTPException } from "hono/http-exception";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { controlSchemas } from "@open-qa/qa-engine/protocol";
|
|
12
|
+
import { createAuthenticator } from "./auth.js";
|
|
13
|
+
import { StageError } from "./drivers.js";
|
|
14
|
+
import { EventLog } from "./event-log.js";
|
|
15
|
+
import { handleMcpRequest } from "./mcp.js";
|
|
16
|
+
import { createStageClock } from "./stage-clock.js";
|
|
17
|
+
import { ToolGateway } from "./tool-gateway.js";
|
|
18
|
+
import { PageViews } from "./page-views.js";
|
|
19
|
+
import { ExpectEngine } from "./expect-engine.js";
|
|
20
|
+
import { redactExpectEvidence } from "./expect-evidence.js";
|
|
21
|
+
import { TelegramSurface } from "./telegram-surface.js";
|
|
22
|
+
import { assertPageUrl } from "./page-policy.js";
|
|
23
|
+
import { DiscordApi } from "./discord-api.js";
|
|
24
|
+
import { discordSurface } from "./discord-surface.js";
|
|
25
|
+
import { httpSurface } from "./system-http.js";
|
|
26
|
+
import { dbSurface, libsqlQuery } from "./system-db.js";
|
|
27
|
+
import { logsSurface } from "./system-logs.js";
|
|
28
|
+
import { githubSurface } from "./system-github.js";
|
|
29
|
+
import { PageRecording } from "./page-recording.js";
|
|
30
|
+
import { RENDERED_CSP, RENDERED_PATH, renderedShell } from "./rendered.js";
|
|
31
|
+
const MAX_BODY_BYTES = 10 * 1024 * 1024;
|
|
32
|
+
const HEARTBEAT_MS = 15e3;
|
|
33
|
+
const STREAM_BATCH_EVENTS = 100;
|
|
34
|
+
const STREAM_BATCH_CHARS = 256 * 1024;
|
|
35
|
+
const MAX_EVENT_STREAMS = 16;
|
|
36
|
+
function fail(c, status, code, message) {
|
|
37
|
+
return c.json({ error: { code, message } }, status);
|
|
38
|
+
}
|
|
39
|
+
function createStageApp(options) {
|
|
40
|
+
const { log, clock, drivers = {} } = options;
|
|
41
|
+
const authenticate = createAuthenticator(options.tokens);
|
|
42
|
+
const streams = /* @__PURE__ */ new Set();
|
|
43
|
+
const secrets = [
|
|
44
|
+
options.watch?.token ?? "",
|
|
45
|
+
options.tokens.conductorToken,
|
|
46
|
+
...Object.values(options.tokens.actorTokens),
|
|
47
|
+
...Object.values(options.gateway?.secrets ?? {}),
|
|
48
|
+
...options.redactions ?? []
|
|
49
|
+
].filter(Boolean).sort((a, b) => b.length - a.length);
|
|
50
|
+
const gateway = new ToolGateway({
|
|
51
|
+
...options.gateway,
|
|
52
|
+
clock,
|
|
53
|
+
tokens: secrets,
|
|
54
|
+
redactor: log.redactor
|
|
55
|
+
});
|
|
56
|
+
log.redactor.add(secrets);
|
|
57
|
+
const addRedactions = (values) => {
|
|
58
|
+
log.redactor.add(values);
|
|
59
|
+
options.onRedactions?.(values);
|
|
60
|
+
};
|
|
61
|
+
let status = "ready";
|
|
62
|
+
const appendEvent = (event) => {
|
|
63
|
+
try {
|
|
64
|
+
return log.append(event);
|
|
65
|
+
} catch {
|
|
66
|
+
status = "stopping";
|
|
67
|
+
throw new Error("Event recording failed");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const app = new Hono();
|
|
71
|
+
app.onError((err, c) => {
|
|
72
|
+
if (err instanceof StageError) return fail(c, err.status, err.code, err.message);
|
|
73
|
+
if (err instanceof HTTPException) return err.getResponse();
|
|
74
|
+
console.error("Stage request failed:", log.redactor.text(String(err.stack ?? err)));
|
|
75
|
+
return fail(c, 500, "internal", "Internal stage error");
|
|
76
|
+
});
|
|
77
|
+
app.get(
|
|
78
|
+
"/health",
|
|
79
|
+
(c) => c.json(controlSchemas.health.response.parse({ status, t: clock.now() }))
|
|
80
|
+
);
|
|
81
|
+
app.get(RENDERED_PATH, (c) => {
|
|
82
|
+
c.header("content-security-policy", RENDERED_CSP);
|
|
83
|
+
c.header("cache-control", "no-store");
|
|
84
|
+
return c.html(renderedShell);
|
|
85
|
+
});
|
|
86
|
+
registerWatch(app, options.watch?.token, options.watch?.frames ?? (async () => []));
|
|
87
|
+
app.use(async (c, next) => {
|
|
88
|
+
const principal = authenticate(c.req.header("authorization"));
|
|
89
|
+
if (!principal) return fail(c, 401, "unauthorized", "Missing or unknown bearer token");
|
|
90
|
+
c.set("principal", principal);
|
|
91
|
+
await next();
|
|
92
|
+
});
|
|
93
|
+
app.use(bodyLimit({ maxSize: MAX_BODY_BYTES }));
|
|
94
|
+
const mcp = app.basePath("/mcp");
|
|
95
|
+
mcp.use(async (c, next) => {
|
|
96
|
+
if (c.get("principal").role !== "actor") {
|
|
97
|
+
return fail(c, 403, "forbidden", "MCP requires an actor token");
|
|
98
|
+
}
|
|
99
|
+
if (status !== "ready") return fail(c, 503, "stage_stopping", "Stage is stopping");
|
|
100
|
+
await next();
|
|
101
|
+
});
|
|
102
|
+
mcp.post("/", (c) => {
|
|
103
|
+
const principal = c.get("principal");
|
|
104
|
+
return handleMcpRequest(
|
|
105
|
+
c.req.raw,
|
|
106
|
+
principal.actor,
|
|
107
|
+
{ append: appendEvent },
|
|
108
|
+
options.registerTools ?? gateway.registerTools
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
mcp.on(["GET", "DELETE"], "/", (c) => {
|
|
112
|
+
c.header("allow", "POST");
|
|
113
|
+
return fail(c, 405, "method_not_allowed", "Only POST is supported on /mcp");
|
|
114
|
+
});
|
|
115
|
+
const control = new Hono();
|
|
116
|
+
control.use(async (c, next) => {
|
|
117
|
+
if (c.get("principal").role !== "conductor") {
|
|
118
|
+
return fail(c, 403, "forbidden", "The control API requires the conductor token");
|
|
119
|
+
}
|
|
120
|
+
await next();
|
|
121
|
+
});
|
|
122
|
+
function route(path, name, schemas, driver) {
|
|
123
|
+
control.post(path, async (c) => {
|
|
124
|
+
if (status !== "ready" && [
|
|
125
|
+
"openView",
|
|
126
|
+
"renderView",
|
|
127
|
+
"expect",
|
|
128
|
+
"recordStart",
|
|
129
|
+
"setStep",
|
|
130
|
+
"replay",
|
|
131
|
+
"executeTools",
|
|
132
|
+
"captureEvidence",
|
|
133
|
+
"fire"
|
|
134
|
+
].includes(name))
|
|
135
|
+
return fail(c, 503, "stage_stopping", "Stage is stopping");
|
|
136
|
+
let body;
|
|
137
|
+
try {
|
|
138
|
+
body = await c.req.json();
|
|
139
|
+
} catch {
|
|
140
|
+
return fail(c, 400, "invalid_json", "Request body must be JSON");
|
|
141
|
+
}
|
|
142
|
+
const request = schemas.request.safeParse(body);
|
|
143
|
+
if (!request.success) {
|
|
144
|
+
return fail(c, 400, "invalid_request", z.prettifyError(request.error));
|
|
145
|
+
}
|
|
146
|
+
if (name === "openView") {
|
|
147
|
+
const parsed = controlSchemas.openView.request.parse(request.data);
|
|
148
|
+
if (parsed.type === "rendered" && parsed.telegram) addRedactions([parsed.telegram.token]);
|
|
149
|
+
}
|
|
150
|
+
if (!driver) return fail(c, 501, "not_implemented", `${name} is not implemented yet`);
|
|
151
|
+
const response = await driver(request.data, {
|
|
152
|
+
emit: appendEvent,
|
|
153
|
+
signal: c.req.raw.signal
|
|
154
|
+
});
|
|
155
|
+
return c.json(schemas.response.parse(response));
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
route(
|
|
159
|
+
"/evidence",
|
|
160
|
+
"captureEvidence",
|
|
161
|
+
controlSchemas.captureEvidence,
|
|
162
|
+
(request, context) => gateway.captureEvidence(request, context)
|
|
163
|
+
);
|
|
164
|
+
route("/redactions", "addRedactions", controlSchemas.addRedactions, async ({ values }) => {
|
|
165
|
+
addRedactions(values);
|
|
166
|
+
return { ok: true };
|
|
167
|
+
});
|
|
168
|
+
route(
|
|
169
|
+
"/tools/execute",
|
|
170
|
+
"executeTools",
|
|
171
|
+
controlSchemas.executeTools,
|
|
172
|
+
async ({ calls }, context) => {
|
|
173
|
+
if (calls.some(({ actor }) => !Object.hasOwn(options.tokens.actorTokens, actor)))
|
|
174
|
+
throw new StageError(400, "invalid_actor", "Unknown tool actor");
|
|
175
|
+
const results = await Promise.all(
|
|
176
|
+
calls.map(
|
|
177
|
+
({ actor, tool, args }) => gateway.call(
|
|
178
|
+
{
|
|
179
|
+
actor,
|
|
180
|
+
signal: context.signal,
|
|
181
|
+
emit: (event) => context.emit({ ...event, actor, by: "program" })
|
|
182
|
+
},
|
|
183
|
+
tool,
|
|
184
|
+
args
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
);
|
|
188
|
+
return { results: results.map(({ image: _image, ...result }) => result) };
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
route("/replay", "replay", controlSchemas.replay, (request, context) => {
|
|
192
|
+
if (!Object.hasOwn(options.tokens.actorTokens, request.actor))
|
|
193
|
+
throw new StageError(403, "actor_forbidden", "Unknown actor");
|
|
194
|
+
if (!drivers.expect) throw new StageError(501, "not_implemented", "Expect is not implemented");
|
|
195
|
+
return gateway.replay(request, context, drivers.expect);
|
|
196
|
+
});
|
|
197
|
+
route("/fire", "fire", controlSchemas.fire, (request, context) => gateway.fire(request, context));
|
|
198
|
+
route("/actor/step", "setStep", controlSchemas.setStep, async (request, context) => {
|
|
199
|
+
if (!Object.hasOwn(options.tokens.actorTokens, request.actor))
|
|
200
|
+
throw new StageError(403, "actor_forbidden", "Unknown actor");
|
|
201
|
+
context.signal.throwIfAborted();
|
|
202
|
+
await drivers.setStep?.(request, context);
|
|
203
|
+
context.signal.throwIfAborted();
|
|
204
|
+
return gateway.setStep(request);
|
|
205
|
+
});
|
|
206
|
+
route("/views/render", "renderView", controlSchemas.renderView, drivers.renderView);
|
|
207
|
+
route("/views", "openView", controlSchemas.openView, drivers.openView);
|
|
208
|
+
route("/views/close", "closeView", controlSchemas.closeView, drivers.closeView);
|
|
209
|
+
route("/expect", "expect", controlSchemas.expect, drivers.expect);
|
|
210
|
+
route("/record/start", "recordStart", controlSchemas.recordStart, drivers.recordStart);
|
|
211
|
+
route("/record/stop", "recordStop", controlSchemas.recordStop, drivers.recordStop);
|
|
212
|
+
route(
|
|
213
|
+
"/artifacts/flush",
|
|
214
|
+
"flushArtifacts",
|
|
215
|
+
controlSchemas.flushArtifacts,
|
|
216
|
+
drivers.flushArtifacts
|
|
217
|
+
);
|
|
218
|
+
control.get("/events", (c) => {
|
|
219
|
+
const raw = c.req.header("last-event-id") ?? c.req.query("afterSeq");
|
|
220
|
+
const request = controlSchemas.events.request.safeParse(
|
|
221
|
+
raw === void 0 ? {} : { afterSeq: raw.trim() === "" ? Number.NaN : Number(raw) }
|
|
222
|
+
);
|
|
223
|
+
if (!request.success) {
|
|
224
|
+
return fail(c, 400, "invalid_request", z.prettifyError(request.error));
|
|
225
|
+
}
|
|
226
|
+
if (streams.size >= MAX_EVENT_STREAMS) {
|
|
227
|
+
return fail(
|
|
228
|
+
c,
|
|
229
|
+
503,
|
|
230
|
+
"too_many_streams",
|
|
231
|
+
`At most ${MAX_EVENT_STREAMS} event streams may be open`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
return streamEvents(c.req.raw.signal, request.data.afterSeq);
|
|
235
|
+
});
|
|
236
|
+
app.route("/", control);
|
|
237
|
+
function streamEvents(signal, afterSeq) {
|
|
238
|
+
const encoder = new TextEncoder();
|
|
239
|
+
let cursor = afterSeq;
|
|
240
|
+
let wake;
|
|
241
|
+
let end = () => {
|
|
242
|
+
};
|
|
243
|
+
const body = new ReadableStream(
|
|
244
|
+
{
|
|
245
|
+
start(controller) {
|
|
246
|
+
const unsubscribe = log.subscribe(() => wake?.());
|
|
247
|
+
const heartbeat = setInterval(() => {
|
|
248
|
+
if ((controller.desiredSize ?? 0) > 0)
|
|
249
|
+
controller.enqueue(encoder.encode(": heartbeat\n\n"));
|
|
250
|
+
}, HEARTBEAT_MS);
|
|
251
|
+
end = () => {
|
|
252
|
+
if (!streams.delete(end)) return;
|
|
253
|
+
unsubscribe();
|
|
254
|
+
clearInterval(heartbeat);
|
|
255
|
+
wake?.();
|
|
256
|
+
try {
|
|
257
|
+
controller.close();
|
|
258
|
+
} catch {
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
streams.add(end);
|
|
262
|
+
signal.addEventListener("abort", () => end(), { once: true });
|
|
263
|
+
},
|
|
264
|
+
async pull(controller) {
|
|
265
|
+
while (streams.has(end)) {
|
|
266
|
+
const batch = log.after(cursor, STREAM_BATCH_EVENTS);
|
|
267
|
+
if (batch.length > 0) {
|
|
268
|
+
let text = "";
|
|
269
|
+
for (const event of batch) {
|
|
270
|
+
text += `id: ${event.seq}
|
|
271
|
+
data: ${JSON.stringify(event)}
|
|
272
|
+
|
|
273
|
+
`;
|
|
274
|
+
cursor = event.seq;
|
|
275
|
+
if (text.length >= STREAM_BATCH_CHARS) break;
|
|
276
|
+
}
|
|
277
|
+
controller.enqueue(encoder.encode(text));
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
await new Promise((resolve) => wake = resolve);
|
|
281
|
+
wake = void 0;
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
cancel() {
|
|
285
|
+
end();
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
{ highWaterMark: 1 }
|
|
289
|
+
);
|
|
290
|
+
return new Response(body, {
|
|
291
|
+
headers: {
|
|
292
|
+
"content-type": "text/event-stream",
|
|
293
|
+
"cache-control": "no-cache",
|
|
294
|
+
"x-accel-buffering": "no"
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
app,
|
|
300
|
+
addRedactions,
|
|
301
|
+
close() {
|
|
302
|
+
status = "stopping";
|
|
303
|
+
for (const end of [...streams]) end();
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
async function startStageServer(config, extensions = {}) {
|
|
308
|
+
await mkdir(config.dir, { recursive: true });
|
|
309
|
+
const clock = extensions.clock ?? createStageClock();
|
|
310
|
+
const log = new EventLog(join(config.dir, "events.ndjson"), clock);
|
|
311
|
+
const systemSecrets = config.systems?.secrets ?? {};
|
|
312
|
+
const redactions = [
|
|
313
|
+
...config.redactions ?? [],
|
|
314
|
+
...extensions.redactions ?? [],
|
|
315
|
+
...Object.values(systemSecrets),
|
|
316
|
+
...config.systems?.db ? [config.systems.db.token] : [],
|
|
317
|
+
...config.systems?.logs ? [config.systems.logs.accessToken] : [],
|
|
318
|
+
...config.systems?.github ? [config.systems.github.token] : [],
|
|
319
|
+
...config.discord ? [config.discord.botToken] : [],
|
|
320
|
+
...config.email ? Object.values(config.email) : [],
|
|
321
|
+
...config.page?.access ? [config.page.access.clientId, config.page.access.clientSecret] : []
|
|
322
|
+
];
|
|
323
|
+
const recording = config.page ? new PageRecording(config.dir, clock, [
|
|
324
|
+
...redactions,
|
|
325
|
+
config.conductorToken,
|
|
326
|
+
...Object.values(config.actorTokens),
|
|
327
|
+
...Object.values(extensions.gateway?.secrets ?? {})
|
|
328
|
+
]) : void 0;
|
|
329
|
+
const pages = config.page ? new PageViews(
|
|
330
|
+
{ ...config.page, headed: config.headed ?? config.page.headed },
|
|
331
|
+
config.dir,
|
|
332
|
+
Object.keys(config.actorTokens),
|
|
333
|
+
void 0,
|
|
334
|
+
recording
|
|
335
|
+
) : void 0;
|
|
336
|
+
const telegram = pages ? new TelegramSurface(pages, pages.drivers.renderView, pages.drivers.openView, {
|
|
337
|
+
...extensions.telegram,
|
|
338
|
+
closeView: pages.drivers.closeView,
|
|
339
|
+
navigate: async (view, url, context) => {
|
|
340
|
+
assertPageUrl(url, pages.config);
|
|
341
|
+
await pages.run(view, context, async (page, bounded) => {
|
|
342
|
+
await page.goto(url, { waitUntil: "domcontentloaded", signal: bounded.signal });
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}) : void 0;
|
|
346
|
+
const discord = pages && config.discord ? discordSurface(pages, new DiscordApi(config.discord.botToken, extensions.discordFetch), [
|
|
347
|
+
...redactions,
|
|
348
|
+
config.conductorToken,
|
|
349
|
+
...Object.values(config.actorTokens),
|
|
350
|
+
...Object.values(extensions.gateway?.secrets ?? {})
|
|
351
|
+
]) : void 0;
|
|
352
|
+
const email = pages && config.email ? emailSurface(
|
|
353
|
+
pages,
|
|
354
|
+
new GmailInbox(config.email, extensions.emailFetch),
|
|
355
|
+
redactions,
|
|
356
|
+
(values) => {
|
|
357
|
+
const fresh = values.filter((value) => value && !redactions.includes(value));
|
|
358
|
+
redactions.push(...fresh);
|
|
359
|
+
pages.addRedactions(fresh);
|
|
360
|
+
recording?.addRedactions(fresh);
|
|
361
|
+
stage.addRedactions(fresh);
|
|
362
|
+
}
|
|
363
|
+
) : void 0;
|
|
364
|
+
const web = pages && {
|
|
365
|
+
...pages.surface,
|
|
366
|
+
views: {
|
|
367
|
+
list: () => pages.surface.views.list().filter((view) => !telegram.owns(view.view) && !pages.entry(view.view).email),
|
|
368
|
+
observe: pages.observe.bind(pages),
|
|
369
|
+
snapshot: pages.snapshot.bind(pages),
|
|
370
|
+
screenshot: pages.screenshot.bind(pages),
|
|
371
|
+
text: pages.text.bind(pages)
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
const surfaces = [
|
|
375
|
+
...web ? [web] : [],
|
|
376
|
+
...telegram ? [telegram.surface] : [],
|
|
377
|
+
...discord ? [discord] : [],
|
|
378
|
+
...email ? [email] : [],
|
|
379
|
+
...config.page ? [httpSurface(config.page)] : [],
|
|
380
|
+
...config.systems?.db ? [dbSurface(config.systems.target, libsqlQuery(config.systems.db))] : [],
|
|
381
|
+
...config.systems?.logs ? [logsSurface(config.systems.logs)] : [],
|
|
382
|
+
...config.systems?.github ? [githubSurface(config.systems.github)] : [],
|
|
383
|
+
...extensions.gateway?.surfaces ?? []
|
|
384
|
+
];
|
|
385
|
+
const expectEngine = new ExpectEngine({
|
|
386
|
+
surfaces,
|
|
387
|
+
clock,
|
|
388
|
+
redact: (value, options) => {
|
|
389
|
+
const secrets = [
|
|
390
|
+
...redactions,
|
|
391
|
+
config.conductorToken,
|
|
392
|
+
...Object.values(config.actorTokens),
|
|
393
|
+
...Object.values(extensions.gateway?.secrets ?? {})
|
|
394
|
+
];
|
|
395
|
+
return pages ? pages.redactEvidence(value, secrets, options) : redactExpectEvidence(value, secrets, options);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
const stage = createStageApp({
|
|
399
|
+
...extensions,
|
|
400
|
+
tokens: config,
|
|
401
|
+
watch: config.watchToken ? {
|
|
402
|
+
token: config.watchToken,
|
|
403
|
+
frames: async (signal) => {
|
|
404
|
+
const frames = [];
|
|
405
|
+
for (const entry of pages?.list() ?? []) {
|
|
406
|
+
const page = pages.entry(entry.view, entry.actor).page;
|
|
407
|
+
const png = await page.screenshot({ type: "png", signal });
|
|
408
|
+
if (png.length > 4 * 1048576) throw new Error("Screen too large");
|
|
409
|
+
frames.push({ view: entry.view, png: png.toString("base64") });
|
|
410
|
+
}
|
|
411
|
+
return frames;
|
|
412
|
+
}
|
|
413
|
+
} : void 0,
|
|
414
|
+
log,
|
|
415
|
+
clock,
|
|
416
|
+
drivers: {
|
|
417
|
+
...surfaces.length ? { expect: expectEngine.expect } : {},
|
|
418
|
+
...pages?.drivers,
|
|
419
|
+
...recording?.drivers,
|
|
420
|
+
...extensions.drivers,
|
|
421
|
+
...pages && telegram ? {
|
|
422
|
+
openView: async (request, context) => {
|
|
423
|
+
if (request.type === "rendered" && request.source && (!discord || request.template !== "chat"))
|
|
424
|
+
throw new StageError(
|
|
425
|
+
400,
|
|
426
|
+
"discord_unavailable",
|
|
427
|
+
"Discord observer is not configured"
|
|
428
|
+
);
|
|
429
|
+
if (request.type === "rendered" && request.email) {
|
|
430
|
+
if (!email || request.template !== "email" || request.source || request.telegram)
|
|
431
|
+
throw new StageError(
|
|
432
|
+
400,
|
|
433
|
+
"email_config",
|
|
434
|
+
"Email requires a configured email view"
|
|
435
|
+
);
|
|
436
|
+
if (pages.list().some(
|
|
437
|
+
(view) => view.actor !== request.actor && pages.entry(view.view).email?.address.toLowerCase() === request.email.address.toLowerCase()
|
|
438
|
+
))
|
|
439
|
+
throw new StageError(
|
|
440
|
+
400,
|
|
441
|
+
"email_shared",
|
|
442
|
+
"Email inboxes must be private to one actor"
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
if (request.type === "rendered" && request.template === "telegram") {
|
|
446
|
+
if (!Object.hasOwn(config.actorTokens, request.actor))
|
|
447
|
+
throw new StageError(403, "actor_forbidden", "Unknown actor");
|
|
448
|
+
if (config.page?.target === "prod")
|
|
449
|
+
throw new StageError(
|
|
450
|
+
403,
|
|
451
|
+
"prod_forbidden",
|
|
452
|
+
"Telegram requires a non-production target"
|
|
453
|
+
);
|
|
454
|
+
if (request.telegram) {
|
|
455
|
+
redactions.push(request.telegram.token);
|
|
456
|
+
recording?.addRedactions([request.telegram.token]);
|
|
457
|
+
}
|
|
458
|
+
return telegram.open(request, context);
|
|
459
|
+
}
|
|
460
|
+
if (request.type === "rendered" && request.telegram)
|
|
461
|
+
throw new StageError(
|
|
462
|
+
400,
|
|
463
|
+
"telegram_config",
|
|
464
|
+
"Private Telegram configuration requires a Telegram view"
|
|
465
|
+
);
|
|
466
|
+
return (extensions.drivers?.openView ?? pages.drivers.openView)(request, context);
|
|
467
|
+
},
|
|
468
|
+
closeView: async (request, context) => {
|
|
469
|
+
const response = await (extensions.drivers?.closeView ?? pages.drivers.closeView)(
|
|
470
|
+
request,
|
|
471
|
+
context
|
|
472
|
+
);
|
|
473
|
+
telegram.forget(request.view);
|
|
474
|
+
return response;
|
|
475
|
+
}
|
|
476
|
+
} : {},
|
|
477
|
+
setStep: async (request, context) => {
|
|
478
|
+
await pages?.drivers.setStep?.(request, context);
|
|
479
|
+
await recording?.drivers.setStep(request, context);
|
|
480
|
+
await extensions.drivers?.setStep?.(request, context);
|
|
481
|
+
return { ...request };
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
redactions,
|
|
485
|
+
onRedactions: (values) => {
|
|
486
|
+
redactions.push(...values.filter((value) => !redactions.includes(value)));
|
|
487
|
+
pages?.addRedactions(values);
|
|
488
|
+
recording?.addRedactions(values);
|
|
489
|
+
extensions.onRedactions?.(values);
|
|
490
|
+
},
|
|
491
|
+
gateway: {
|
|
492
|
+
permissions: config.page?.permissions,
|
|
493
|
+
secrets: systemSecrets,
|
|
494
|
+
...extensions.gateway,
|
|
495
|
+
surfaces
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
const server = await new Promise((resolve, reject) => {
|
|
499
|
+
const listening = serve(
|
|
500
|
+
{ fetch: stage.app.fetch, hostname: config.host, port: config.port },
|
|
501
|
+
() => resolve(listening)
|
|
502
|
+
);
|
|
503
|
+
listening.once("error", reject);
|
|
504
|
+
});
|
|
505
|
+
const { port } = server.address();
|
|
506
|
+
const host = config.host.includes(":") ? `[${config.host}]` : config.host;
|
|
507
|
+
pages?.setRenderedOrigin(`http://${host}:${port}`);
|
|
508
|
+
return {
|
|
509
|
+
url: `http://${host}:${port}`,
|
|
510
|
+
log,
|
|
511
|
+
close: async () => {
|
|
512
|
+
stage.close();
|
|
513
|
+
const failures = [];
|
|
514
|
+
try {
|
|
515
|
+
await pages?.close();
|
|
516
|
+
} catch (error) {
|
|
517
|
+
failures.push(error);
|
|
518
|
+
}
|
|
519
|
+
try {
|
|
520
|
+
await recording?.close();
|
|
521
|
+
} catch (error) {
|
|
522
|
+
failures.push(error);
|
|
523
|
+
}
|
|
524
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
525
|
+
await new Promise((resolve, reject) => {
|
|
526
|
+
server.close((err) => err ? reject(err) : resolve());
|
|
527
|
+
server.closeAllConnections();
|
|
528
|
+
});
|
|
529
|
+
if (failures.length) throw new AggregateError(failures, "Stage recording cleanup failed");
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
export {
|
|
534
|
+
MAX_EVENT_STREAMS,
|
|
535
|
+
createStageApp,
|
|
536
|
+
startStageServer
|
|
537
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class SignedArtifactSnapshots {
|
|
2
|
+
private current?;
|
|
3
|
+
private uploading;
|
|
4
|
+
clear(): void;
|
|
5
|
+
prepare(source: string): {
|
|
6
|
+
snapshot: string;
|
|
7
|
+
artifacts: {
|
|
8
|
+
path: string;
|
|
9
|
+
contentType: string;
|
|
10
|
+
bytes: number;
|
|
11
|
+
}[];
|
|
12
|
+
};
|
|
13
|
+
upload(snapshot: string | undefined, urls: Record<string, string>, signal: AbortSignal, send?: typeof fetch): Promise<{
|
|
14
|
+
snapshot: string;
|
|
15
|
+
artifacts: {
|
|
16
|
+
path: string;
|
|
17
|
+
contentType: string;
|
|
18
|
+
bytes: number;
|
|
19
|
+
}[];
|
|
20
|
+
}>;
|
|
21
|
+
}
|