@tangle-network/agent-app 0.60.115 → 0.60.117
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/dist/VaultPane-OWOPGPXP.js +7 -0
- package/dist/assistant/index.js +11 -11
- package/dist/chat-react/index.js +4 -4
- package/dist/chat-routes/index.js +13 -6
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/wire.d.ts +3 -1
- package/dist/chat-store/index.js +2 -2
- package/dist/{chunk-Y4WLCFDZ.js → chunk-2FLSODOX.js} +6 -6
- package/dist/{chunk-6XE77PTT.js → chunk-2NHJNINW.js} +1 -1
- package/dist/chunk-2NHJNINW.js.map +1 -0
- package/dist/{chunk-56GPDCOJ.js → chunk-7DXRHQZO.js} +7 -7
- package/dist/chunk-BTEVD7HG.js +198 -0
- package/dist/chunk-BTEVD7HG.js.map +1 -0
- package/dist/{chunk-RQSYR4AV.js → chunk-CLB4IQEG.js} +5 -5
- package/dist/{chunk-F2FH354I.js → chunk-D52LB6UQ.js} +2 -2
- package/dist/chunk-FVI2MHUL.js +1152 -0
- package/dist/chunk-FVI2MHUL.js.map +1 -0
- package/dist/{chunk-4G7HH6WQ.js → chunk-GU7OZFU7.js} +2 -2
- package/dist/{chunk-OGDEUCNW.js → chunk-JGLK5RDK.js} +176 -1
- package/dist/chunk-JGLK5RDK.js.map +1 -0
- package/dist/{chunk-FJQUMIM6.js → chunk-JZUSVGRJ.js} +3 -3
- package/dist/{chunk-AB63T2LM.js → chunk-KBJZMR4E.js} +8 -1
- package/dist/chunk-KBJZMR4E.js.map +1 -0
- package/dist/{chunk-JCSHTJT6.js → chunk-MGON3FE6.js} +22 -3
- package/dist/chunk-MGON3FE6.js.map +1 -0
- package/dist/{chunk-RQSNTJTW.js → chunk-MO7PDQRZ.js} +526 -187
- package/dist/chunk-MO7PDQRZ.js.map +1 -0
- package/dist/{chunk-MEUNTJL5.js → chunk-N3CYCSKK.js} +6 -1
- package/dist/chunk-N3CYCSKK.js.map +1 -0
- package/dist/journeys/cli.d.ts +2 -0
- package/dist/journeys/cli.js +165 -0
- package/dist/journeys/cli.js.map +1 -0
- package/dist/journeys/config.d.ts +96 -0
- package/dist/journeys/index.d.ts +18 -0
- package/dist/journeys/index.js +731 -0
- package/dist/journeys/index.js.map +1 -0
- package/dist/journeys/journey.d.ts +173 -0
- package/dist/journeys/page-reading.d.ts +42 -0
- package/dist/journeys/report.d.ts +51 -0
- package/dist/journeys/score.d.ts +115 -0
- package/dist/journeys/scrub.d.ts +21 -0
- package/dist/journeys/send.d.ts +46 -0
- package/dist/journeys/session.d.ts +27 -0
- package/dist/journeys/trace-zip.d.ts +28 -0
- package/dist/journeys/video.d.ts +53 -0
- package/dist/public-consultation/index.d.ts +2 -2
- package/dist/public-consultation/index.js +3 -3
- package/dist/runtime/index.js +1 -1
- package/dist/runtime/router-turn.d.ts +23 -2
- package/dist/runtime/router-turn.js +1 -1
- package/dist/sandbox/index.d.ts +2 -0
- package/dist/sandbox/index.js +13 -1
- package/dist/sandbox/sandbox-exec-tool.d.ts +68 -0
- package/dist/sandbox/sandbox-run-tool.d.ts +70 -0
- package/dist/studio-react/index.js +2 -2
- package/dist/teams/index.js +10 -6
- package/dist/teams/invitations-api.js +6 -3
- package/dist/teams/invitations-api.js.map +1 -1
- package/dist/teams/invitations.d.ts +4 -0
- package/dist/teams/members-api.js +2 -1
- package/dist/teams-react/components/InviteForm.d.ts +20 -0
- package/dist/teams-react/contracts.d.ts +2 -0
- package/dist/teams-react/index.js +196 -217
- package/dist/teams-react/index.js.map +1 -1
- package/dist/vault/VaultContentResults.d.ts +29 -0
- package/dist/vault/VaultHistoryPanel.d.ts +20 -0
- package/dist/vault/VaultPane.d.ts +3 -1
- package/dist/vault/contracts.d.ts +38 -0
- package/dist/vault/index.js +1 -1
- package/dist/vault/lazy.js +1 -1
- package/dist/web-react/index.js +25 -25
- package/dist/workspace-export/index.js +4 -193
- package/dist/workspace-export/index.js.map +1 -1
- package/dist/workspace-react/index.js +9 -7
- package/dist/workspace-react/index.js.map +1 -1
- package/dist/workspace-react/workspace-list.d.ts +7 -1
- package/package.json +8 -2
- package/dist/VaultPane-JUSNGOV7.js +0 -7
- package/dist/chunk-6XE77PTT.js.map +0 -1
- package/dist/chunk-AB63T2LM.js.map +0 -1
- package/dist/chunk-JCSHTJT6.js.map +0 -1
- package/dist/chunk-MEUNTJL5.js.map +0 -1
- package/dist/chunk-OGDEUCNW.js.map +0 -1
- package/dist/chunk-RQSNTJTW.js.map +0 -1
- /package/dist/{VaultPane-JUSNGOV7.js.map → VaultPane-OWOPGPXP.js.map} +0 -0
- /package/dist/{chunk-Y4WLCFDZ.js.map → chunk-2FLSODOX.js.map} +0 -0
- /package/dist/{chunk-56GPDCOJ.js.map → chunk-7DXRHQZO.js.map} +0 -0
- /package/dist/{chunk-RQSYR4AV.js.map → chunk-CLB4IQEG.js.map} +0 -0
- /package/dist/{chunk-F2FH354I.js.map → chunk-D52LB6UQ.js.map} +0 -0
- /package/dist/{chunk-4G7HH6WQ.js.map → chunk-GU7OZFU7.js.map} +0 -0
- /package/dist/{chunk-FJQUMIM6.js.map → chunk-JZUSVGRJ.js.map} +0 -0
|
@@ -0,0 +1,1152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
crc32Update
|
|
3
|
+
} from "./chunk-BTEVD7HG.js";
|
|
4
|
+
|
|
5
|
+
// src/journeys/score.ts
|
|
6
|
+
var DEFAULT_FAILURE_TEXT = [
|
|
7
|
+
/\b[a-z]+(?:_[a-z]+)*\.[a-z]+(?:_[a-z]+)*_(?:failed|unavailable|timeout)\b/,
|
|
8
|
+
/\bAPIError \d{3}\b/,
|
|
9
|
+
/stopped before a clean completion/i,
|
|
10
|
+
/Failed to connect to Sandbox API/i,
|
|
11
|
+
/\bbilling\.balance_required\b|Add balance or upgrade your plan/i,
|
|
12
|
+
/Internal Server Error/,
|
|
13
|
+
/\bECONNREFUSED\b/,
|
|
14
|
+
/Something went wrong/i,
|
|
15
|
+
// agent-app's own sandbox failure copy (`formatSandboxProvisioningUserMessage`),
|
|
16
|
+
// which every app on the shared runtime can render as a reply. Tests drive the
|
|
17
|
+
// formatter's every branch through these, so the copy and the scorer cannot drift.
|
|
18
|
+
/unable to (?:re)?connect to the sandbox/i,
|
|
19
|
+
/sandbox service didn.t answer/i,
|
|
20
|
+
/Sandbox recovery is required before chat can continue/i,
|
|
21
|
+
/couldn.t (?:finish copying|copy) your Vault into the sandbox/i,
|
|
22
|
+
/too large (?:for the sandbox to accept|to copy into the sandbox)/i,
|
|
23
|
+
/attachment data is staged in the sandbox/i
|
|
24
|
+
];
|
|
25
|
+
var DEFAULT_STOPPED_TEXT = [
|
|
26
|
+
/interrupted by the user/i,
|
|
27
|
+
/cancell?ed by (?:the )?user/i,
|
|
28
|
+
/stopped by (?:the )?user/i
|
|
29
|
+
];
|
|
30
|
+
function normalize(line) {
|
|
31
|
+
return line.replace(/\s+/g, " ").trim();
|
|
32
|
+
}
|
|
33
|
+
function evidenceKeys(snapshot, patterns, ignore) {
|
|
34
|
+
const counts = /* @__PURE__ */ new Map();
|
|
35
|
+
const add = (key) => counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
36
|
+
for (const raw of snapshot.lines) {
|
|
37
|
+
const line = normalize(raw);
|
|
38
|
+
if (!line || ignore.has(line)) continue;
|
|
39
|
+
if (patterns.stopped.some((p) => p.test(line))) add(`stopped:${line}`);
|
|
40
|
+
else if (patterns.failure.some((p) => p.test(line))) add(`text:${line}`);
|
|
41
|
+
}
|
|
42
|
+
for (const raw of snapshot.failureNodes) {
|
|
43
|
+
const text = normalize(raw);
|
|
44
|
+
if (text && !ignore.has(text)) add(`node:${text.slice(0, 300)}`);
|
|
45
|
+
}
|
|
46
|
+
for (let i = 0; i < snapshot.retryButtons; i += 1) add("retry");
|
|
47
|
+
return counts;
|
|
48
|
+
}
|
|
49
|
+
var TurnEvidence = class {
|
|
50
|
+
constructor(patterns, prompt) {
|
|
51
|
+
this.patterns = patterns;
|
|
52
|
+
this.ignore = new Set(prompt.split("\n").map(normalize).filter(Boolean));
|
|
53
|
+
this.ignore.add(normalize(prompt));
|
|
54
|
+
}
|
|
55
|
+
patterns;
|
|
56
|
+
floor = /* @__PURE__ */ new Map();
|
|
57
|
+
peak = /* @__PURE__ */ new Map();
|
|
58
|
+
anchored = false;
|
|
59
|
+
ignore;
|
|
60
|
+
begin(snapshot) {
|
|
61
|
+
this.floor = evidenceKeys(snapshot, this.patterns, this.ignore);
|
|
62
|
+
this.peak = /* @__PURE__ */ new Map();
|
|
63
|
+
this.anchored = snapshot.anchored === true;
|
|
64
|
+
}
|
|
65
|
+
/** Read one snapshot. Returns the evidence that appeared for the first time, as display text. */
|
|
66
|
+
observe(snapshot) {
|
|
67
|
+
if (snapshot.anchored && !this.anchored) {
|
|
68
|
+
this.floor = /* @__PURE__ */ new Map();
|
|
69
|
+
this.anchored = true;
|
|
70
|
+
}
|
|
71
|
+
const now = evidenceKeys(snapshot, this.patterns, this.ignore);
|
|
72
|
+
for (const [key, floor] of this.floor) {
|
|
73
|
+
const count = now.get(key) ?? 0;
|
|
74
|
+
if (count < floor) this.floor.set(key, count);
|
|
75
|
+
}
|
|
76
|
+
const fresh = [];
|
|
77
|
+
for (const [key, count] of now) {
|
|
78
|
+
const extra = count - (this.floor.get(key) ?? 0);
|
|
79
|
+
if (extra <= (this.peak.get(key) ?? 0)) continue;
|
|
80
|
+
if (!this.peak.has(key)) fresh.push(display(key));
|
|
81
|
+
this.peak.set(key, extra);
|
|
82
|
+
}
|
|
83
|
+
return fresh;
|
|
84
|
+
}
|
|
85
|
+
/** Everything that rose above its floor during the turn, in the order it appeared. */
|
|
86
|
+
added() {
|
|
87
|
+
const failures = [];
|
|
88
|
+
const stopped = [];
|
|
89
|
+
let retryAppeared = false;
|
|
90
|
+
for (const key of this.peak.keys()) {
|
|
91
|
+
if (key === "retry") retryAppeared = true;
|
|
92
|
+
else if (key.startsWith("stopped:")) stopped.push(key.slice("stopped:".length));
|
|
93
|
+
else failures.push(key.slice(key.indexOf(":") + 1));
|
|
94
|
+
}
|
|
95
|
+
return { failures, stopped, retryAppeared };
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
function display(key) {
|
|
99
|
+
return key === "retry" ? "a Retry control appeared in the transcript" : key.slice(key.indexOf(":") + 1);
|
|
100
|
+
}
|
|
101
|
+
var STATUS_STALE_MS = 2e4;
|
|
102
|
+
function turnLooksActive(page) {
|
|
103
|
+
return page.stopVisible || page.statusLines > 0 && page.msSinceStatusChanged < STATUS_STALE_MS;
|
|
104
|
+
}
|
|
105
|
+
function scoreTurn(facts) {
|
|
106
|
+
if (facts.notSent) return { outcome: "not-sent", error: facts.notSent, failures: [] };
|
|
107
|
+
const { failures, stopped, retryAppeared } = facts.evidence;
|
|
108
|
+
const all = [...stopped, ...failures, ...retryAppeared ? ["a Retry control appeared in the transcript"] : []];
|
|
109
|
+
if (stopped.length || facts.stopPressed && !facts.timedOut) {
|
|
110
|
+
return { outcome: "stopped", error: stopped[0] ?? failures[0] ?? "stopped by the journey", failures: all };
|
|
111
|
+
}
|
|
112
|
+
if (failures.length || retryAppeared) {
|
|
113
|
+
return { outcome: "failed", error: failures[0] ?? "a Retry control appeared in the transcript", failures: all };
|
|
114
|
+
}
|
|
115
|
+
if (facts.timedOut) return { outcome: "timeout", error: "the turn was still running when the wait ended", failures: all };
|
|
116
|
+
if (facts.replyChars === 0) return { outcome: "empty", error: "the turn ended without any reply text", failures: all };
|
|
117
|
+
return { outcome: "completed", error: null, failures: [] };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/journeys/config.ts
|
|
121
|
+
import { existsSync } from "fs";
|
|
122
|
+
import { homedir } from "os";
|
|
123
|
+
import { join, resolve } from "path";
|
|
124
|
+
import { pathToFileURL } from "url";
|
|
125
|
+
var DEFAULT_CHAT_CONTRACT = {
|
|
126
|
+
transcript: "main",
|
|
127
|
+
assistant: null,
|
|
128
|
+
status: '[role="status"]',
|
|
129
|
+
failureNodes: '[role="alert"], [data-notice-tone="error"]',
|
|
130
|
+
failureText: [],
|
|
131
|
+
stoppedText: [],
|
|
132
|
+
composer: '[data-testid="composer-card"]',
|
|
133
|
+
inputName: "Message input",
|
|
134
|
+
sendName: "Send",
|
|
135
|
+
stopName: "Stop response",
|
|
136
|
+
retryName: /^Retry$/,
|
|
137
|
+
threadPath: null,
|
|
138
|
+
runningTurns: null,
|
|
139
|
+
sendPaths: [/^\/api\/(?:[^/]+\/)*chat$/]
|
|
140
|
+
};
|
|
141
|
+
var DEFAULT_AUTH_PATHS = {
|
|
142
|
+
session: "/api/auth/get-session",
|
|
143
|
+
passwordSignIn: "/api/auth/sign-in/email",
|
|
144
|
+
tangleStart: "/auth/tangle/start"
|
|
145
|
+
};
|
|
146
|
+
var CONFIG_FILE_NAMES = ["journeys.config.mjs", "journeys.config.js"];
|
|
147
|
+
function defineJourneys(config) {
|
|
148
|
+
resolveJourneysConfig(config);
|
|
149
|
+
return config;
|
|
150
|
+
}
|
|
151
|
+
var ENV_NAME = /^[A-Z_][A-Z0-9_]*$/;
|
|
152
|
+
function resolveJourneysConfig(config, env = process.env) {
|
|
153
|
+
if ("resolved" in config && config.resolved === true) return config;
|
|
154
|
+
const problems = [];
|
|
155
|
+
if (!/^[a-z0-9][a-z0-9-]{0,40}$/.test(config.app ?? "")) problems.push('app must be a short lowercase name such as "gtm"');
|
|
156
|
+
const base = (env.JOURNEYS_BASE_URL ?? config.baseUrl ?? "").replace(/\/$/, "");
|
|
157
|
+
if (!/^https?:\/\/[^/]+$/.test(base)) problems.push(`baseUrl must be an origin such as https://app.example.com, got "${base}"`);
|
|
158
|
+
const identities = config.identities ?? {};
|
|
159
|
+
if (Object.keys(identities).length === 0) problems.push("identities must name at least one test identity");
|
|
160
|
+
for (const [name, identity] of Object.entries(identities)) {
|
|
161
|
+
if (name === "anon") problems.push('"anon" is reserved for a signed-out context');
|
|
162
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name)) problems.push(`identity "${name}": use a lowercase name`);
|
|
163
|
+
for (const key of ["emailEnv", "passwordEnv"]) {
|
|
164
|
+
if (!ENV_NAME.test(identity[key] ?? "")) problems.push(`identity "${name}": ${key} must be an environment variable NAME, not a value`);
|
|
165
|
+
}
|
|
166
|
+
if (identity.signIn !== "password" && identity.signIn !== "tangle") problems.push(`identity "${name}": signIn must be "password" or "tangle"`);
|
|
167
|
+
}
|
|
168
|
+
const chat = {
|
|
169
|
+
...DEFAULT_CHAT_CONTRACT,
|
|
170
|
+
...config.chat,
|
|
171
|
+
failureText: [...DEFAULT_FAILURE_TEXT, ...config.chat?.failureText ?? []],
|
|
172
|
+
stoppedText: [...DEFAULT_STOPPED_TEXT, ...config.chat?.stoppedText ?? []]
|
|
173
|
+
};
|
|
174
|
+
if (chat.threadPath && !/\(\?<threadId>/.test(chat.threadPath.source)) problems.push("chat.threadPath needs a named group (?<threadId>\u2026)");
|
|
175
|
+
const maxTurns = config.maxTurns ?? 6;
|
|
176
|
+
if (!Number.isInteger(maxTurns) || maxTurns < 0) problems.push("maxTurns must be a whole number");
|
|
177
|
+
if (problems.length) throw new Error(`journeys config: ${problems.join("; ")}`);
|
|
178
|
+
const stateDir = resolve(env.JOURNEYS_STATE_DIR ?? config.stateDir ?? join(homedir(), ".local/state/agent-app-journeys", config.app));
|
|
179
|
+
return {
|
|
180
|
+
resolved: true,
|
|
181
|
+
app: config.app,
|
|
182
|
+
baseUrl: base,
|
|
183
|
+
identities,
|
|
184
|
+
chat,
|
|
185
|
+
auth: { ...DEFAULT_AUTH_PATHS, ...config.auth },
|
|
186
|
+
maxTurns,
|
|
187
|
+
stateDir,
|
|
188
|
+
outDir: resolve(env.JOURNEYS_OUT ?? config.outDir ?? join(stateDir, "out"))
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function threadRouteIn(url, config) {
|
|
192
|
+
if (!config.chat.threadPath) return null;
|
|
193
|
+
let parsed;
|
|
194
|
+
try {
|
|
195
|
+
parsed = new URL(url);
|
|
196
|
+
} catch {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
if (parsed.origin !== new URL(config.baseUrl).origin) return null;
|
|
200
|
+
const match = config.chat.threadPath.exec(parsed.pathname);
|
|
201
|
+
const threadId = match?.groups?.threadId;
|
|
202
|
+
if (!threadId) return null;
|
|
203
|
+
const workspaceId = match?.groups?.workspaceId;
|
|
204
|
+
return workspaceId ? { threadId, workspaceId } : { threadId };
|
|
205
|
+
}
|
|
206
|
+
function credentialNames(config) {
|
|
207
|
+
return [...new Set(Object.values(config.identities).flatMap((identity) => [identity.emailEnv, identity.passwordEnv]))];
|
|
208
|
+
}
|
|
209
|
+
async function loadJourneysConfig(dir = process.cwd(), env = process.env) {
|
|
210
|
+
const file = env.JOURNEYS_CONFIG ? resolve(dir, env.JOURNEYS_CONFIG) : CONFIG_FILE_NAMES.map((name) => join(dir, name)).find((path) => existsSync(path));
|
|
211
|
+
if (!file || !existsSync(file)) {
|
|
212
|
+
throw new Error(`no ${CONFIG_FILE_NAMES[0]} in ${dir}; add one at the app root or set JOURNEYS_CONFIG`);
|
|
213
|
+
}
|
|
214
|
+
const loaded = await import(pathToFileURL(file).href);
|
|
215
|
+
if (!loaded.default) throw new Error(`${file} must export default defineJourneys({ \u2026 })`);
|
|
216
|
+
return resolveJourneysConfig(loaded.default, env);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/journeys/scrub.ts
|
|
220
|
+
import { readdirSync, readFileSync, statSync, openSync as openSync2, closeSync as closeSync2 } from "fs";
|
|
221
|
+
import { join as join2 } from "path";
|
|
222
|
+
|
|
223
|
+
// src/journeys/trace-zip.ts
|
|
224
|
+
import { closeSync, fstatSync, openSync, readSync, writeSync } from "fs";
|
|
225
|
+
import { deflateRawSync, inflateRawSync } from "zlib";
|
|
226
|
+
var EOCD = 101010256;
|
|
227
|
+
var ZIP64_EOCD = 101075792;
|
|
228
|
+
var ZIP64_LOCATOR = 117853008;
|
|
229
|
+
var CENTRAL = 33639248;
|
|
230
|
+
var LOCAL = 67324752;
|
|
231
|
+
var U16 = 65535;
|
|
232
|
+
var U32 = 4294967295;
|
|
233
|
+
function readAt(fd, position, length) {
|
|
234
|
+
const out = Buffer.alloc(length);
|
|
235
|
+
let done = 0;
|
|
236
|
+
while (done < length) {
|
|
237
|
+
const n = readSync(fd, out, done, length - done, position + done);
|
|
238
|
+
if (n === 0) throw new Error(`zip: unexpected end of file at ${position + done}`);
|
|
239
|
+
done += n;
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
243
|
+
var u64 = (b, at) => Number(b.readBigUInt64LE(at));
|
|
244
|
+
function readCentralDirectory(fd) {
|
|
245
|
+
const size = fstatSync(fd).size;
|
|
246
|
+
const tailLength = Math.min(size, 22 + U16 + 20);
|
|
247
|
+
const tail = readAt(fd, size - tailLength, tailLength);
|
|
248
|
+
let eocd = -1;
|
|
249
|
+
for (let i = tail.length - 22; i >= 0; i -= 1) {
|
|
250
|
+
if (tail.readUInt32LE(i) === EOCD && i + 22 + tail.readUInt16LE(i + 20) === tail.length) {
|
|
251
|
+
eocd = i;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (eocd < 0) throw new Error("zip: no end-of-central-directory record");
|
|
256
|
+
let count = tail.readUInt16LE(eocd + 10);
|
|
257
|
+
let cdSize = tail.readUInt32LE(eocd + 12);
|
|
258
|
+
let cdOffset = tail.readUInt32LE(eocd + 16);
|
|
259
|
+
if (eocd >= 20 && tail.readUInt32LE(eocd - 20) === ZIP64_LOCATOR) {
|
|
260
|
+
const record = readAt(fd, u64(tail, eocd - 20 + 8), 56);
|
|
261
|
+
if (record.readUInt32LE(0) !== ZIP64_EOCD) throw new Error("zip: zip64 locator points at no zip64 record");
|
|
262
|
+
count = u64(record, 32);
|
|
263
|
+
cdSize = u64(record, 40);
|
|
264
|
+
cdOffset = u64(record, 48);
|
|
265
|
+
}
|
|
266
|
+
const cd = readAt(fd, cdOffset, cdSize);
|
|
267
|
+
const entries = [];
|
|
268
|
+
let at = 0;
|
|
269
|
+
for (let i = 0; i < count; i += 1) {
|
|
270
|
+
if (cd.readUInt32LE(at) !== CENTRAL) throw new Error(`zip: central directory is broken at entry ${i + 1} of ${count}`);
|
|
271
|
+
const nameLength = cd.readUInt16LE(at + 28);
|
|
272
|
+
const extraLength = cd.readUInt16LE(at + 30);
|
|
273
|
+
const commentLength = cd.readUInt16LE(at + 32);
|
|
274
|
+
let compressedSize = cd.readUInt32LE(at + 20);
|
|
275
|
+
let uncompressedSize = cd.readUInt32LE(at + 24);
|
|
276
|
+
let localOffset = cd.readUInt32LE(at + 42);
|
|
277
|
+
const extra = cd.subarray(at + 46 + nameLength, at + 46 + nameLength + extraLength);
|
|
278
|
+
for (let e = 0; e + 4 <= extra.length; ) {
|
|
279
|
+
const id = extra.readUInt16LE(e);
|
|
280
|
+
const len = extra.readUInt16LE(e + 2);
|
|
281
|
+
if (id === 1) {
|
|
282
|
+
let p = e + 4;
|
|
283
|
+
if (uncompressedSize === U32) {
|
|
284
|
+
uncompressedSize = u64(extra, p);
|
|
285
|
+
p += 8;
|
|
286
|
+
}
|
|
287
|
+
if (compressedSize === U32) {
|
|
288
|
+
compressedSize = u64(extra, p);
|
|
289
|
+
p += 8;
|
|
290
|
+
}
|
|
291
|
+
if (localOffset === U32) {
|
|
292
|
+
localOffset = u64(extra, p);
|
|
293
|
+
p += 8;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
e += 4 + len;
|
|
297
|
+
}
|
|
298
|
+
entries.push({
|
|
299
|
+
name: cd.toString("utf8", at + 46, at + 46 + nameLength),
|
|
300
|
+
method: cd.readUInt16LE(at + 10),
|
|
301
|
+
crc32: cd.readUInt32LE(at + 16),
|
|
302
|
+
compressedSize,
|
|
303
|
+
uncompressedSize,
|
|
304
|
+
localOffset,
|
|
305
|
+
dosTime: cd.readUInt16LE(at + 12),
|
|
306
|
+
dosDate: cd.readUInt16LE(at + 14)
|
|
307
|
+
});
|
|
308
|
+
at += 46 + nameLength + extraLength + commentLength;
|
|
309
|
+
}
|
|
310
|
+
return entries;
|
|
311
|
+
}
|
|
312
|
+
function readRaw(fd, entry) {
|
|
313
|
+
const local = readAt(fd, entry.localOffset, 30);
|
|
314
|
+
if (local.readUInt32LE(0) !== LOCAL) throw new Error(`zip: '${entry.name}' has no local header`);
|
|
315
|
+
const start = entry.localOffset + 30 + local.readUInt16LE(26) + local.readUInt16LE(28);
|
|
316
|
+
return readAt(fd, start, entry.compressedSize);
|
|
317
|
+
}
|
|
318
|
+
function inflateEntry(entry, raw) {
|
|
319
|
+
if (entry.method === 0) return raw;
|
|
320
|
+
if (entry.method === 8) return inflateRawSync(raw);
|
|
321
|
+
throw new Error(`zip: '${entry.name}' uses unsupported compression method ${entry.method}`);
|
|
322
|
+
}
|
|
323
|
+
function createZipWriter(path, opts = {}) {
|
|
324
|
+
const fd = openSync(path, "w", 384);
|
|
325
|
+
let position = 0;
|
|
326
|
+
const written = [];
|
|
327
|
+
const put = (b) => {
|
|
328
|
+
let done = 0;
|
|
329
|
+
while (done < b.length) done += writeSync(fd, b, done, b.length - done);
|
|
330
|
+
position += b.length;
|
|
331
|
+
};
|
|
332
|
+
const big = (n) => opts.forceZip64 === true || n >= U32;
|
|
333
|
+
const writeEntry = (w, data) => {
|
|
334
|
+
const zip64 = big(w.compressedSize) || big(w.uncompressedSize);
|
|
335
|
+
const header = Buffer.alloc(30 + w.name.length + (zip64 ? 20 : 0));
|
|
336
|
+
header.writeUInt32LE(LOCAL, 0);
|
|
337
|
+
header.writeUInt16LE(zip64 ? 45 : 20, 4);
|
|
338
|
+
header.writeUInt16LE(2048, 6);
|
|
339
|
+
header.writeUInt16LE(w.method, 8);
|
|
340
|
+
header.writeUInt16LE(w.dosTime, 10);
|
|
341
|
+
header.writeUInt16LE(w.dosDate, 12);
|
|
342
|
+
header.writeUInt32LE(w.crc32, 14);
|
|
343
|
+
header.writeUInt32LE(zip64 ? U32 : w.compressedSize, 18);
|
|
344
|
+
header.writeUInt32LE(zip64 ? U32 : w.uncompressedSize, 22);
|
|
345
|
+
header.writeUInt16LE(w.name.length, 26);
|
|
346
|
+
header.writeUInt16LE(zip64 ? 20 : 0, 28);
|
|
347
|
+
w.name.copy(header, 30);
|
|
348
|
+
if (zip64) {
|
|
349
|
+
const e = 30 + w.name.length;
|
|
350
|
+
header.writeUInt16LE(1, e);
|
|
351
|
+
header.writeUInt16LE(16, e + 2);
|
|
352
|
+
header.writeBigUInt64LE(BigInt(w.uncompressedSize), e + 4);
|
|
353
|
+
header.writeBigUInt64LE(BigInt(w.compressedSize), e + 12);
|
|
354
|
+
}
|
|
355
|
+
const offset = position;
|
|
356
|
+
put(header);
|
|
357
|
+
put(data);
|
|
358
|
+
written.push({ ...w, offset });
|
|
359
|
+
};
|
|
360
|
+
return {
|
|
361
|
+
add(name, content, from) {
|
|
362
|
+
const data = deflateRawSync(content);
|
|
363
|
+
writeEntry({
|
|
364
|
+
name: Buffer.from(name, "utf8"),
|
|
365
|
+
method: 8,
|
|
366
|
+
crc32: crc32Update(0, content),
|
|
367
|
+
compressedSize: data.length,
|
|
368
|
+
uncompressedSize: content.length,
|
|
369
|
+
dosTime: from?.dosTime ?? 0,
|
|
370
|
+
dosDate: from?.dosDate ?? 33
|
|
371
|
+
}, data);
|
|
372
|
+
},
|
|
373
|
+
copy(entry, raw) {
|
|
374
|
+
writeEntry({
|
|
375
|
+
name: Buffer.from(entry.name, "utf8"),
|
|
376
|
+
method: entry.method,
|
|
377
|
+
crc32: entry.crc32,
|
|
378
|
+
compressedSize: entry.compressedSize,
|
|
379
|
+
uncompressedSize: entry.uncompressedSize,
|
|
380
|
+
dosTime: entry.dosTime,
|
|
381
|
+
dosDate: entry.dosDate
|
|
382
|
+
}, raw);
|
|
383
|
+
},
|
|
384
|
+
close() {
|
|
385
|
+
const cdOffset = position;
|
|
386
|
+
for (const w of written) {
|
|
387
|
+
const fields = [];
|
|
388
|
+
if (big(w.uncompressedSize)) fields.push(BigInt(w.uncompressedSize));
|
|
389
|
+
if (big(w.compressedSize)) fields.push(BigInt(w.compressedSize));
|
|
390
|
+
if (big(w.offset)) fields.push(BigInt(w.offset));
|
|
391
|
+
const extraLength = fields.length ? 4 + 8 * fields.length : 0;
|
|
392
|
+
const h = Buffer.alloc(46 + w.name.length + extraLength);
|
|
393
|
+
h.writeUInt32LE(CENTRAL, 0);
|
|
394
|
+
h.writeUInt16LE(45, 4);
|
|
395
|
+
h.writeUInt16LE(fields.length ? 45 : 20, 6);
|
|
396
|
+
h.writeUInt16LE(2048, 8);
|
|
397
|
+
h.writeUInt16LE(w.method, 10);
|
|
398
|
+
h.writeUInt16LE(w.dosTime, 12);
|
|
399
|
+
h.writeUInt16LE(w.dosDate, 14);
|
|
400
|
+
h.writeUInt32LE(w.crc32, 16);
|
|
401
|
+
h.writeUInt32LE(big(w.compressedSize) ? U32 : w.compressedSize, 20);
|
|
402
|
+
h.writeUInt32LE(big(w.uncompressedSize) ? U32 : w.uncompressedSize, 24);
|
|
403
|
+
h.writeUInt16LE(w.name.length, 28);
|
|
404
|
+
h.writeUInt16LE(extraLength, 30);
|
|
405
|
+
h.writeUInt32LE(big(w.offset) ? U32 : w.offset, 42);
|
|
406
|
+
w.name.copy(h, 46);
|
|
407
|
+
if (fields.length) {
|
|
408
|
+
const e = 46 + w.name.length;
|
|
409
|
+
h.writeUInt16LE(1, e);
|
|
410
|
+
h.writeUInt16LE(8 * fields.length, e + 2);
|
|
411
|
+
fields.forEach((v, i) => h.writeBigUInt64LE(v, e + 4 + 8 * i));
|
|
412
|
+
}
|
|
413
|
+
put(h);
|
|
414
|
+
}
|
|
415
|
+
const cdSize = position - cdOffset;
|
|
416
|
+
const count = written.length;
|
|
417
|
+
const zip64 = opts.forceZip64 === true || count >= U16 || cdOffset >= U32 || cdSize >= U32;
|
|
418
|
+
if (zip64) {
|
|
419
|
+
const recordOffset = position;
|
|
420
|
+
const r = Buffer.alloc(56);
|
|
421
|
+
r.writeUInt32LE(ZIP64_EOCD, 0);
|
|
422
|
+
r.writeBigUInt64LE(44n, 4);
|
|
423
|
+
r.writeUInt16LE(45, 12);
|
|
424
|
+
r.writeUInt16LE(45, 14);
|
|
425
|
+
r.writeBigUInt64LE(BigInt(count), 24);
|
|
426
|
+
r.writeBigUInt64LE(BigInt(count), 32);
|
|
427
|
+
r.writeBigUInt64LE(BigInt(cdSize), 40);
|
|
428
|
+
r.writeBigUInt64LE(BigInt(cdOffset), 48);
|
|
429
|
+
put(r);
|
|
430
|
+
const l = Buffer.alloc(20);
|
|
431
|
+
l.writeUInt32LE(ZIP64_LOCATOR, 0);
|
|
432
|
+
l.writeBigUInt64LE(BigInt(recordOffset), 8);
|
|
433
|
+
l.writeUInt32LE(1, 16);
|
|
434
|
+
put(l);
|
|
435
|
+
}
|
|
436
|
+
const end = Buffer.alloc(22);
|
|
437
|
+
end.writeUInt32LE(EOCD, 0);
|
|
438
|
+
end.writeUInt16LE(zip64 ? U16 : count, 8);
|
|
439
|
+
end.writeUInt16LE(zip64 ? U16 : count, 10);
|
|
440
|
+
end.writeUInt32LE(zip64 ? U32 : cdSize, 12);
|
|
441
|
+
end.writeUInt32LE(zip64 ? U32 : cdOffset, 16);
|
|
442
|
+
put(end);
|
|
443
|
+
closeSync(fd);
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// src/journeys/scrub.ts
|
|
449
|
+
var SENSITIVE_HEADERS = /* @__PURE__ */ new Set([
|
|
450
|
+
"cookie",
|
|
451
|
+
"set-cookie",
|
|
452
|
+
"authorization",
|
|
453
|
+
"proxy-authorization",
|
|
454
|
+
"x-api-key",
|
|
455
|
+
"x-auth-token",
|
|
456
|
+
"x-stream-token",
|
|
457
|
+
"cf-access-token",
|
|
458
|
+
"x-csrf-token"
|
|
459
|
+
]);
|
|
460
|
+
var SENSITIVE_PATH = /\/api\/auth\/|\/auth\/tangle\/|token|session|\/api\/collaboration\/|oauth|\/consent|\/api-access|api-key/i;
|
|
461
|
+
var QUERY_NAMES = "token|access_token|refresh_token|id_token|code|state|key|api_key|apikey|signature|sig|jwt|ticket|secret";
|
|
462
|
+
var QUERY = new RegExp(`([?&](?:${QUERY_NAMES})=)[^&"'\\s\\\\]+`, "gi");
|
|
463
|
+
var QUERY_NAME = new RegExp(`^(?:${QUERY_NAMES})$`, "i");
|
|
464
|
+
var REDACTED = "REDACTED";
|
|
465
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
466
|
+
function scrubHeaders(headers) {
|
|
467
|
+
if (Array.isArray(headers)) {
|
|
468
|
+
for (const h of headers) {
|
|
469
|
+
if (isObject(h) && SENSITIVE_HEADERS.has(String(h.name ?? "").toLowerCase())) h.value = REDACTED;
|
|
470
|
+
}
|
|
471
|
+
} else if (isObject(headers)) {
|
|
472
|
+
for (const key of Object.keys(headers)) if (SENSITIVE_HEADERS.has(key.toLowerCase())) headers[key] = REDACTED;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
function forgetBody(holder, drop) {
|
|
476
|
+
for (const key of ["_file", "_sha1"]) {
|
|
477
|
+
const ref = holder[key];
|
|
478
|
+
if (ref) {
|
|
479
|
+
drop.add(String(ref).split("/").pop() ?? "");
|
|
480
|
+
holder[key] = null;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if ("text" in holder) holder.text = REDACTED;
|
|
484
|
+
}
|
|
485
|
+
function walk(node, drop) {
|
|
486
|
+
if (Array.isArray(node)) {
|
|
487
|
+
for (const v of node) walk(v, drop);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (!isObject(node)) return;
|
|
491
|
+
const req = node.request;
|
|
492
|
+
const res = node.response;
|
|
493
|
+
if (isObject(req) && "url" in req) {
|
|
494
|
+
const sensitive = SENSITIVE_PATH.test(String(req.url ?? "").split("?")[0] ?? "");
|
|
495
|
+
scrubHeaders(req.headers);
|
|
496
|
+
if ("cookies" in req) req.cookies = [];
|
|
497
|
+
const query = req.queryString;
|
|
498
|
+
if (Array.isArray(query)) {
|
|
499
|
+
for (const q of query) if (isObject(q) && QUERY_NAME.test(String(q.name ?? ""))) q.value = REDACTED;
|
|
500
|
+
}
|
|
501
|
+
if (sensitive && isObject(req.postData)) forgetBody(req.postData, drop);
|
|
502
|
+
if (isObject(res)) {
|
|
503
|
+
scrubHeaders(res.headers);
|
|
504
|
+
if ("cookies" in res) res.cookies = [];
|
|
505
|
+
if (sensitive && isObject(res.content)) forgetBody(res.content, drop);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
for (const [key, value] of Object.entries(node)) {
|
|
509
|
+
if (key.toLowerCase() === "cookies" && Array.isArray(value)) node[key] = [];
|
|
510
|
+
else if (key === "storageState") node[key] = null;
|
|
511
|
+
else if (SENSITIVE_HEADERS.has(key.toLowerCase()) && typeof value === "string") node[key] = REDACTED;
|
|
512
|
+
else walk(value, drop);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function scrubJsonl(text, drop) {
|
|
516
|
+
const out = [];
|
|
517
|
+
for (const line of text.split("\n")) {
|
|
518
|
+
if (!line.trim()) continue;
|
|
519
|
+
let event;
|
|
520
|
+
try {
|
|
521
|
+
event = JSON.parse(line);
|
|
522
|
+
} catch {
|
|
523
|
+
out.push(line.replace(QUERY, `$1${REDACTED}`));
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
walk(event, drop);
|
|
527
|
+
out.push(JSON.stringify(event).replace(QUERY, `$1${REDACTED}`));
|
|
528
|
+
}
|
|
529
|
+
return out.join("\n") + "\n";
|
|
530
|
+
}
|
|
531
|
+
function replaceNeedles(data, needles) {
|
|
532
|
+
let out = data;
|
|
533
|
+
for (const needle of needles) {
|
|
534
|
+
if (!needle) continue;
|
|
535
|
+
const n = Buffer.from(needle, "utf8");
|
|
536
|
+
if (out.indexOf(n) < 0) continue;
|
|
537
|
+
const parts = [];
|
|
538
|
+
let from = 0;
|
|
539
|
+
for (let at = out.indexOf(n); at >= 0; at = out.indexOf(n, from)) {
|
|
540
|
+
parts.push(out.subarray(from, at), Buffer.from(REDACTED));
|
|
541
|
+
from = at + n.length;
|
|
542
|
+
}
|
|
543
|
+
parts.push(out.subarray(from));
|
|
544
|
+
out = Buffer.concat(parts);
|
|
545
|
+
}
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
var isEvents = (name) => name.endsWith(".trace") || name.endsWith(".network");
|
|
549
|
+
function scrubTrace(src, dst, needles) {
|
|
550
|
+
const fd = openSync2(src, "r");
|
|
551
|
+
try {
|
|
552
|
+
const entries = readCentralDirectory(fd);
|
|
553
|
+
const drop = /* @__PURE__ */ new Set();
|
|
554
|
+
const events = /* @__PURE__ */ new Map();
|
|
555
|
+
for (const entry of entries) {
|
|
556
|
+
if (!isEvents(entry.name)) continue;
|
|
557
|
+
events.set(entry.name, Buffer.from(scrubJsonl(inflateEntry(entry, readRaw(fd, entry)).toString("utf8"), drop), "utf8"));
|
|
558
|
+
}
|
|
559
|
+
const writer = createZipWriter(dst);
|
|
560
|
+
let dropped = 0;
|
|
561
|
+
let rewritten = 0;
|
|
562
|
+
for (const entry of entries) {
|
|
563
|
+
const base = entry.name.split("/").pop() ?? "";
|
|
564
|
+
if (entry.name.startsWith("resources/") && drop.has(base)) {
|
|
565
|
+
dropped += 1;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
const raw = readRaw(fd, entry);
|
|
569
|
+
const content = events.get(entry.name) ?? inflateEntry(entry, raw);
|
|
570
|
+
const clean = replaceNeedles(content, needles);
|
|
571
|
+
if (!events.has(entry.name) && clean === content) {
|
|
572
|
+
writer.copy(entry, raw);
|
|
573
|
+
} else {
|
|
574
|
+
writer.add(entry.name, clean, entry);
|
|
575
|
+
rewritten += 1;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
writer.close();
|
|
579
|
+
return { entries: entries.length, dropped, rewritten };
|
|
580
|
+
} finally {
|
|
581
|
+
closeSync2(fd);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
function scanZip(path, hard, soft) {
|
|
585
|
+
const fd = openSync2(path, "r");
|
|
586
|
+
try {
|
|
587
|
+
const hits = [];
|
|
588
|
+
let softHits = 0;
|
|
589
|
+
const hardBytes = hard.filter(Boolean).map((n) => Buffer.from(n, "utf8"));
|
|
590
|
+
const softBytes = soft.filter(Boolean).map((n) => Buffer.from(n, "utf8"));
|
|
591
|
+
for (const entry of readCentralDirectory(fd)) {
|
|
592
|
+
const content = inflateEntry(entry, readRaw(fd, entry));
|
|
593
|
+
if (hardBytes.some((n) => content.includes(n))) hits.push(entry.name);
|
|
594
|
+
if (softBytes.some((n) => content.includes(n))) softHits += 1;
|
|
595
|
+
}
|
|
596
|
+
return { hard: hits, soft: softHits };
|
|
597
|
+
} finally {
|
|
598
|
+
closeSync2(fd);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function assertNoSecrets(dir, needles) {
|
|
602
|
+
const hits = [];
|
|
603
|
+
const softHits = [];
|
|
604
|
+
const hard = needles.hard.filter(Boolean).map((n) => Buffer.from(n, "utf8"));
|
|
605
|
+
const soft = needles.soft.filter(Boolean).map((n) => Buffer.from(n, "utf8"));
|
|
606
|
+
const visit = (d) => {
|
|
607
|
+
for (const name of readdirSync(d)) {
|
|
608
|
+
const path = join2(d, name);
|
|
609
|
+
if (statSync(path).isDirectory()) {
|
|
610
|
+
visit(path);
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
if (path.endsWith(".zip")) {
|
|
614
|
+
const found = scanZip(path, needles.hard, needles.soft);
|
|
615
|
+
if (found.hard.length) hits.push(`${path} (${found.hard.slice(0, 5).join(", ")})`);
|
|
616
|
+
if (found.soft) softHits.push(`${path} (${found.soft} entries)`);
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
const buf = readFileSync(path);
|
|
620
|
+
if (hard.some((n) => buf.includes(n))) hits.push(path);
|
|
621
|
+
if (soft.some((n) => buf.includes(n))) softHits.push(path);
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
visit(dir);
|
|
625
|
+
if (softHits.length) console.warn(`note: a test identity's email appears in ${softHits.join(", ")}; blur it before sharing outside the company`);
|
|
626
|
+
if (hits.length) throw new Error(`REFUSING: secret material found in ${hits.join(", ")}; do not copy ${dir}`);
|
|
627
|
+
return { softHits };
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// src/journeys/session.ts
|
|
631
|
+
import { chmodSync, existsSync as existsSync2, mkdirSync, readdirSync as readdirSync2, readFileSync as readFileSync2, renameSync, rmSync, statSync as statSync2 } from "fs";
|
|
632
|
+
import { join as join3 } from "path";
|
|
633
|
+
var SignInError = class extends Error {
|
|
634
|
+
constructor(status, message) {
|
|
635
|
+
super(message);
|
|
636
|
+
this.status = status;
|
|
637
|
+
}
|
|
638
|
+
status;
|
|
639
|
+
};
|
|
640
|
+
var sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
641
|
+
async function playwrightRequest() {
|
|
642
|
+
try {
|
|
643
|
+
return (await import("playwright")).request;
|
|
644
|
+
} catch {
|
|
645
|
+
throw new Error("journeys need the optional `playwright` peer: install playwright in the app that runs them");
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
function sessionDir(config) {
|
|
649
|
+
return join3(config.stateDir, "sessions", new URL(config.baseUrl).host);
|
|
650
|
+
}
|
|
651
|
+
function storageStatePath(config, identity) {
|
|
652
|
+
return join3(sessionDir(config), `${identity}.storage.json`);
|
|
653
|
+
}
|
|
654
|
+
function need(name) {
|
|
655
|
+
const value = process.env[name]?.trim();
|
|
656
|
+
if (!value) throw new Error(`${name} is unset: load it from the host's vault into this process (the value is never printed)`);
|
|
657
|
+
return value;
|
|
658
|
+
}
|
|
659
|
+
async function sessionUser(config, state) {
|
|
660
|
+
if (!existsSync2(state)) return null;
|
|
661
|
+
const request = await playwrightRequest();
|
|
662
|
+
const ctx = await request.newContext({ baseURL: config.baseUrl, storageState: state });
|
|
663
|
+
try {
|
|
664
|
+
const r = await ctx.get(config.auth.session, { maxRedirects: 0, timeout: 2e4 });
|
|
665
|
+
if (!r.ok()) return null;
|
|
666
|
+
const body = await r.json().catch(() => null);
|
|
667
|
+
const user = body?.user;
|
|
668
|
+
if (!user?.id) return null;
|
|
669
|
+
return { id: user.id, name: user.name ?? null, emailVerified: user.emailVerified === true };
|
|
670
|
+
} finally {
|
|
671
|
+
await ctx.dispose();
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
async function signInWithPassword(ctx, config, email, password) {
|
|
675
|
+
const r = await ctx.post(`${config.baseUrl}${config.auth.passwordSignIn}`, {
|
|
676
|
+
headers: { "content-type": "application/json", origin: config.baseUrl },
|
|
677
|
+
data: { email, password },
|
|
678
|
+
maxRedirects: 0
|
|
679
|
+
});
|
|
680
|
+
if (r.status() !== 200) throw new SignInError(r.status(), `email sign-in returned ${r.status()}`);
|
|
681
|
+
}
|
|
682
|
+
async function signInWithTangle(ctx, config, email, password) {
|
|
683
|
+
const base = config.baseUrl;
|
|
684
|
+
const start = await ctx.get(`${base}${config.auth.tangleStart}`, { maxRedirects: 0 });
|
|
685
|
+
const first = start.headers()["location"];
|
|
686
|
+
if (!first) throw new SignInError(start.status(), `SSO start returned ${start.status()} without a redirect`);
|
|
687
|
+
const authorize = new URL(first, base).toString();
|
|
688
|
+
const platform = new URL(authorize).origin;
|
|
689
|
+
const signIn = await ctx.post(`${platform}/api/auth/sign-in/email`, {
|
|
690
|
+
headers: { "content-type": "application/json", origin: platform },
|
|
691
|
+
data: { email, password },
|
|
692
|
+
maxRedirects: 0
|
|
693
|
+
});
|
|
694
|
+
if (signIn.status() !== 200) throw new SignInError(signIn.status(), `Platform sign-in returned ${signIn.status()}`);
|
|
695
|
+
const startPrefix = config.auth.tangleStart.replace(/\/start$/, "/");
|
|
696
|
+
let next = authorize;
|
|
697
|
+
for (let hop = 0; hop < 8; hop += 1) {
|
|
698
|
+
const r = await ctx.get(next, { maxRedirects: 0 });
|
|
699
|
+
const location = r.headers()["location"];
|
|
700
|
+
if (!location) throw new SignInError(r.status(), `SSO stopped at ${new URL(next).pathname} with ${r.status()}`);
|
|
701
|
+
const url = new URL(location, next);
|
|
702
|
+
if (url.origin === new URL(base).origin && !url.pathname.startsWith(startPrefix)) {
|
|
703
|
+
if (url.pathname.startsWith("/login")) throw new SignInError(401, "the app callback refused the sign-in");
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
next = url.toString();
|
|
707
|
+
}
|
|
708
|
+
throw new SignInError(0, "SSO did not return to the app within 8 redirects");
|
|
709
|
+
}
|
|
710
|
+
async function withLock(dir, work) {
|
|
711
|
+
const deadline = Date.now() + 18e4;
|
|
712
|
+
for (; ; ) {
|
|
713
|
+
try {
|
|
714
|
+
mkdirSync(dir);
|
|
715
|
+
break;
|
|
716
|
+
} catch (error) {
|
|
717
|
+
if (error.code !== "EEXIST") throw error;
|
|
718
|
+
const age = Date.now() - (statSync2(dir, { throwIfNoEntry: false })?.mtimeMs ?? Date.now());
|
|
719
|
+
if (age > 12e4) rmSync(dir, { recursive: true, force: true });
|
|
720
|
+
if (Date.now() > deadline) throw new Error(`timed out waiting for ${dir}`);
|
|
721
|
+
await sleep(500);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
try {
|
|
725
|
+
return await work();
|
|
726
|
+
} finally {
|
|
727
|
+
rmSync(dir, { recursive: true, force: true });
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
function identityOf(config, name) {
|
|
731
|
+
const identity = config.identities[name];
|
|
732
|
+
if (!identity) throw new Error(`"${name}" is not an identity in the ${config.app} journeys config (${Object.keys(config.identities).join(", ")})`);
|
|
733
|
+
return identity;
|
|
734
|
+
}
|
|
735
|
+
async function storageStateFor(config, name, opts = {}) {
|
|
736
|
+
const identity = identityOf(config, name);
|
|
737
|
+
const dir = sessionDir(config);
|
|
738
|
+
mkdirSync(dir, { recursive: true, mode: 448 });
|
|
739
|
+
const file = opts.fresh ? join3(dir, `${name}.${process.pid}.${Date.now()}.storage.json`) : storageStatePath(config, name);
|
|
740
|
+
const request = await playwrightRequest();
|
|
741
|
+
return withLock(join3(dir, `signin-${name}.lock`), async () => {
|
|
742
|
+
if (!opts.fresh) {
|
|
743
|
+
const cached = await sessionUser(config, file);
|
|
744
|
+
if (cached) return { path: file, user: cached };
|
|
745
|
+
}
|
|
746
|
+
const email = need(identity.emailEnv);
|
|
747
|
+
const password = need(identity.passwordEnv);
|
|
748
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
749
|
+
const ctx = await request.newContext({ baseURL: config.baseUrl });
|
|
750
|
+
try {
|
|
751
|
+
if (identity.signIn === "password") await signInWithPassword(ctx, config, email, password);
|
|
752
|
+
else await signInWithTangle(ctx, config, email, password);
|
|
753
|
+
const tmp = `${file}.tmp`;
|
|
754
|
+
await ctx.storageState({ path: tmp });
|
|
755
|
+
chmodSync(tmp, 384);
|
|
756
|
+
renameSync(tmp, file);
|
|
757
|
+
break;
|
|
758
|
+
} catch (error) {
|
|
759
|
+
const status = error instanceof SignInError ? error.status : 0;
|
|
760
|
+
if (status !== 429 || attempt >= 4) throw error;
|
|
761
|
+
await sleep(15e3 * attempt);
|
|
762
|
+
} finally {
|
|
763
|
+
await ctx.dispose();
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
const user = await sessionUser(config, file);
|
|
767
|
+
if (!user) throw new Error(`${name} signed in but ${config.auth.session} has no user`);
|
|
768
|
+
if (!user.emailVerified) console.warn(`${name} signed in but is not email-verified; an app that requires verification will refuse its chat`);
|
|
769
|
+
return { path: file, user };
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
function secretNeedles(config) {
|
|
773
|
+
const hard = /* @__PURE__ */ new Set();
|
|
774
|
+
const soft = /* @__PURE__ */ new Set();
|
|
775
|
+
for (const identity of Object.values(config.identities)) {
|
|
776
|
+
const password = process.env[identity.passwordEnv]?.trim();
|
|
777
|
+
if (password) hard.add(password);
|
|
778
|
+
const email = process.env[identity.emailEnv]?.trim();
|
|
779
|
+
if (email) soft.add(email);
|
|
780
|
+
}
|
|
781
|
+
const root = join3(config.stateDir, "sessions");
|
|
782
|
+
const stack = existsSync2(root) ? [root] : [];
|
|
783
|
+
while (stack.length) {
|
|
784
|
+
const dir = stack.pop();
|
|
785
|
+
for (const entry of readdirSync2(dir, { withFileTypes: true })) {
|
|
786
|
+
const path = join3(dir, entry.name);
|
|
787
|
+
if (entry.isDirectory()) stack.push(path);
|
|
788
|
+
else if (entry.name.endsWith(".storage.json")) addCookieNeedles(path, hard);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
return { hard: [...hard], soft: [...soft] };
|
|
792
|
+
}
|
|
793
|
+
function addCookieNeedles(file, hard) {
|
|
794
|
+
try {
|
|
795
|
+
const state = JSON.parse(readFileSync2(file, "utf8"));
|
|
796
|
+
for (const { value } of state.cookies ?? []) {
|
|
797
|
+
if (!value || value.length < 16) continue;
|
|
798
|
+
hard.add(value);
|
|
799
|
+
let decoded = value;
|
|
800
|
+
try {
|
|
801
|
+
decoded = decodeURIComponent(value);
|
|
802
|
+
} catch {
|
|
803
|
+
}
|
|
804
|
+
hard.add(decoded);
|
|
805
|
+
const token = decoded.split(".")[0] ?? "";
|
|
806
|
+
if (token.length >= 16) hard.add(token);
|
|
807
|
+
}
|
|
808
|
+
} catch {
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// src/journeys/video.ts
|
|
813
|
+
import { execFileSync } from "child_process";
|
|
814
|
+
import { copyFileSync, existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync } from "fs";
|
|
815
|
+
import { basename, join as join4 } from "path";
|
|
816
|
+
var W = 1440;
|
|
817
|
+
var H = 900;
|
|
818
|
+
var BAR = 84;
|
|
819
|
+
var MAX_HIGHLIGHTS_S = 178;
|
|
820
|
+
var FONT_CANDIDATES = [
|
|
821
|
+
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",
|
|
822
|
+
"/usr/share/fonts/dejavu/DejaVuSansMono.ttf",
|
|
823
|
+
"/System/Library/Fonts/Menlo.ttc"
|
|
824
|
+
];
|
|
825
|
+
function monoFont() {
|
|
826
|
+
const configured = process.env.JOURNEYS_FONT_MONO;
|
|
827
|
+
if (configured && existsSync3(configured)) return configured;
|
|
828
|
+
return FONT_CANDIDATES.find((path) => existsSync3(path)) ?? null;
|
|
829
|
+
}
|
|
830
|
+
function ff(args) {
|
|
831
|
+
execFileSync("nice", ["-n", "10", "ffmpeg", "-hide_banner", "-loglevel", "error", "-y", ...args], { stdio: ["ignore", "inherit", "inherit"] });
|
|
832
|
+
}
|
|
833
|
+
function probeSeconds(file) {
|
|
834
|
+
const out = execFileSync("ffprobe", ["-v", "error", "-show_entries", "format=duration", "-of", "default=nw=1:nk=1", file], { encoding: "utf8" }).trim();
|
|
835
|
+
const n = Number(out);
|
|
836
|
+
if (!Number.isFinite(n) || n <= 0) throw new Error(`ffprobe could not read the duration of ${file}: ${out}`);
|
|
837
|
+
return n;
|
|
838
|
+
}
|
|
839
|
+
var pad2 = (n) => String(Math.floor(n)).padStart(2, "0");
|
|
840
|
+
var mmss = (s) => `${pad2(s / 60)}:${pad2(s % 60)}`;
|
|
841
|
+
var srtTime = (s) => {
|
|
842
|
+
const ms = Math.max(0, Math.round(s * 1e3));
|
|
843
|
+
return `${pad2(ms / 36e5)}:${pad2(ms / 6e4 % 60)}:${pad2(ms / 1e3 % 60)},${String(ms % 1e3).padStart(3, "0")}`;
|
|
844
|
+
};
|
|
845
|
+
var assTime = (s) => {
|
|
846
|
+
const cs = Math.max(0, Math.round(s * 100));
|
|
847
|
+
return `${Math.floor(cs / 36e4)}:${pad2(cs / 6e3 % 60)}:${pad2(cs / 100 % 60)}.${String(cs % 100).padStart(2, "0")}`;
|
|
848
|
+
};
|
|
849
|
+
var humanSeconds = (s) => s >= 90 ? `${Math.floor(s / 60)} min ${Math.round(s % 60)} s` : `${Math.round(s)} s`;
|
|
850
|
+
function chapterCues(chapters, t0, duration) {
|
|
851
|
+
const points = chapters.map((c) => ({ ...c, s: (c.at - t0) / 1e3, e: c.until ? (c.until - t0) / 1e3 : void 0 })).filter((c) => c.s < duration).sort((a, b) => a.s - b.s);
|
|
852
|
+
const cues = [];
|
|
853
|
+
for (let i = 0; i < points.length; i += 1) {
|
|
854
|
+
const c = points[i];
|
|
855
|
+
const start = Math.max(0, c.s);
|
|
856
|
+
const end = Math.min(duration, points[i + 1]?.s ?? duration);
|
|
857
|
+
if (end - start < 0.05) continue;
|
|
858
|
+
const text = c.kind === "wait" && c.e !== void 0 ? `${c.caption} (real wait ${humanSeconds(c.e - c.s)})` : c.caption;
|
|
859
|
+
cues.push({ start, end, text });
|
|
860
|
+
}
|
|
861
|
+
return cues;
|
|
862
|
+
}
|
|
863
|
+
function srtText(cues) {
|
|
864
|
+
return cues.map((c, i) => `${i + 1}
|
|
865
|
+
${srtTime(c.start)} --> ${srtTime(c.end)}
|
|
866
|
+
${c.text}
|
|
867
|
+
`).join("\n");
|
|
868
|
+
}
|
|
869
|
+
function assText(cues) {
|
|
870
|
+
const esc = (t) => t.replace(/\\/g, "\\\\").replace(/[{}]/g, "").replace(/\n/g, "\\N");
|
|
871
|
+
const head = `[Script Info]
|
|
872
|
+
ScriptType: v4.00+
|
|
873
|
+
PlayResX: ${W}
|
|
874
|
+
PlayResY: ${H + BAR}
|
|
875
|
+
WrapStyle: 0
|
|
876
|
+
|
|
877
|
+
[V4+ Styles]
|
|
878
|
+
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
|
|
879
|
+
Style: Bar,DejaVu Sans,26,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,0,0,1,28,200,14,1
|
|
880
|
+
|
|
881
|
+
[Events]
|
|
882
|
+
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
|
883
|
+
`;
|
|
884
|
+
return head + cues.map((c) => `Dialogue: 0,${assTime(c.start)},${assTime(c.end)},Bar,,0,0,0,,${esc(c.text)}`).join("\n") + "\n";
|
|
885
|
+
}
|
|
886
|
+
function highlightPlan(chapters, t0, duration) {
|
|
887
|
+
const pts = chapters.map((c) => ({ ...c, s: Math.max(0, (c.at - t0) / 1e3), e: c.until ? (c.until - t0) / 1e3 : void 0 })).sort((x, y) => x.s - y.s);
|
|
888
|
+
const cuts = /* @__PURE__ */ new Set([0, duration]);
|
|
889
|
+
for (const p of pts) {
|
|
890
|
+
if (p.s < duration) cuts.add(Math.round(p.s * 10) / 10);
|
|
891
|
+
if (p.e !== void 0 && p.e < duration) cuts.add(Math.round(p.e * 10) / 10);
|
|
892
|
+
}
|
|
893
|
+
const bounds = [...cuts].sort((x, y) => x - y);
|
|
894
|
+
const segs = [];
|
|
895
|
+
for (let i = 0; i + 1 < bounds.length; i += 1) {
|
|
896
|
+
const a = bounds[i];
|
|
897
|
+
const b = bounds[i + 1];
|
|
898
|
+
if (b - a < 0.15) continue;
|
|
899
|
+
const wait = [...pts].reverse().find((p) => p.kind === "wait" && p.e !== void 0 && p.s <= a + 0.05 && a < p.e - 0.05);
|
|
900
|
+
const step = [...pts].reverse().find((p) => p.kind === "step" && p.s <= a + 0.05 && (!wait || p.s >= wait.s - 0.05));
|
|
901
|
+
const len = b - a;
|
|
902
|
+
const k = wait ? len > 6 ? len / 5 : 1 : len > 15 ? len / 10 : 1;
|
|
903
|
+
segs.push({ a, b, k, text: step?.caption ?? wait?.caption ?? "", wait: Boolean(wait), whole: wait?.e !== void 0 ? wait.e - wait.s : 0 });
|
|
904
|
+
}
|
|
905
|
+
const shown = segs.reduce((n, s) => n + (s.b - s.a) / s.k, 0);
|
|
906
|
+
const f = shown > MAX_HIGHLIGHTS_S ? shown / MAX_HIGHLIGHTS_S : 1;
|
|
907
|
+
return segs.map((s) => {
|
|
908
|
+
const k = s.k * f;
|
|
909
|
+
const len = s.b - s.a;
|
|
910
|
+
const x = k >= 10 ? Math.round(k) : Math.round(k * 10) / 10;
|
|
911
|
+
const speed = k > 1.05 ? ` \xBB ${x}x, ${humanSeconds(len)} real${s.wait ? ` (whole wait ${humanSeconds(s.whole)})` : ""}` : "";
|
|
912
|
+
return { a: s.a, b: s.b, k, text: `[${mmss(s.a)}] ${s.text}${speed}` };
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
function postProcessVideo(o) {
|
|
916
|
+
mkdirSync2(o.videoDir, { recursive: true });
|
|
917
|
+
const keep = join4(o.workDir, `${o.label}.webm`);
|
|
918
|
+
if (o.webm !== keep) copyFileSync(o.webm, keep);
|
|
919
|
+
writeFileSync(join4(o.workDir, `${o.label}.chapters.json`), JSON.stringify({ t0: o.t0, wallSeconds: o.wallSeconds, chapters: o.chapters }));
|
|
920
|
+
return renderAll(keep, o.workDir, o.videoDir, o.label, o.t0, o.chapters, o.speedup2x, o.wallSeconds);
|
|
921
|
+
}
|
|
922
|
+
function rebuildVideo(rawLabelDir, videoDir, label, speedup2x) {
|
|
923
|
+
const meta = JSON.parse(readFileSync3(join4(rawLabelDir, `${label}.chapters.json`), "utf8"));
|
|
924
|
+
return renderAll(join4(rawLabelDir, `${label}.webm`), rawLabelDir, videoDir, label, meta.t0, meta.chapters, speedup2x, meta.wallSeconds);
|
|
925
|
+
}
|
|
926
|
+
function renderAll(webm, work, videoDir, label, t0, chapters, speedup2x, wallSeconds) {
|
|
927
|
+
const clean = join4(work, `${label}.clean.mp4`);
|
|
928
|
+
ff(["-i", webm, "-vf", `fps=25,scale=${W}:${H}:flags=lanczos,format=yuv420p`, "-c:v", "libx264", "-preset", "veryfast", "-crf", "22", "-threads", "2", "-an", clean]);
|
|
929
|
+
const duration = probeSeconds(clean);
|
|
930
|
+
if (duration < wallSeconds - 3) console.warn(`video is ${duration.toFixed(1)} s but ${wallSeconds.toFixed(1)} s passed before recording stopped; late captions may be early`);
|
|
931
|
+
const cues = chapterCues(chapters, t0, duration);
|
|
932
|
+
const srt = join4(videoDir, `${label}.srt`);
|
|
933
|
+
writeFileSync(srt, srtText(cues));
|
|
934
|
+
const ass = join4(work, `${label}.full.ass`);
|
|
935
|
+
writeFileSync(ass, assText(cues));
|
|
936
|
+
const full = join4(videoDir, `${label}-full.mp4`);
|
|
937
|
+
const font = monoFont();
|
|
938
|
+
const clock = `drawtext=${font ? `fontfile=${font}` : "font=monospace"}:text='%{pts\\:hms}':x=w-tw-28:y=h-${BAR / 2}-th/2:fontsize=26:fontcolor=white@0.8`;
|
|
939
|
+
ff(["-i", clean, "-vf", `pad=${W}:${H + BAR}:0:0:color=0x111111,ass=${ass},${clock}`, "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-threads", "2", "-pix_fmt", "yuv420p", "-movflags", "+faststart", full]);
|
|
940
|
+
const plan = highlightPlan(chapters, t0, duration);
|
|
941
|
+
const segDir = join4(work, `${label}.segments`);
|
|
942
|
+
mkdirSync2(segDir, { recursive: true });
|
|
943
|
+
const list = [];
|
|
944
|
+
const hcues = [];
|
|
945
|
+
let at = 0;
|
|
946
|
+
plan.forEach((s, i) => {
|
|
947
|
+
const seg = join4(segDir, `${String(i).padStart(3, "0")}.mp4`);
|
|
948
|
+
ff(["-ss", s.a.toFixed(3), "-t", (s.b - s.a).toFixed(3), "-i", clean, "-vf", `setpts=PTS/${s.k.toFixed(4)},fps=25`, "-an", "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-threads", "2", "-pix_fmt", "yuv420p", seg]);
|
|
949
|
+
const len = probeSeconds(seg);
|
|
950
|
+
list.push(`file '${seg}'`);
|
|
951
|
+
hcues.push({ start: at, end: at + len, text: s.text });
|
|
952
|
+
at += len;
|
|
953
|
+
});
|
|
954
|
+
const listFile = join4(segDir, "list.txt");
|
|
955
|
+
writeFileSync(listFile, list.join("\n") + "\n");
|
|
956
|
+
const joined = join4(work, `${label}.highlights.raw.mp4`);
|
|
957
|
+
ff(["-f", "concat", "-safe", "0", "-i", listFile, "-c", "copy", joined]);
|
|
958
|
+
const hass = join4(work, `${label}.highlights.ass`);
|
|
959
|
+
writeFileSync(hass, assText(hcues));
|
|
960
|
+
const highlights = join4(videoDir, `${label}-highlights.mp4`);
|
|
961
|
+
ff(["-i", joined, "-vf", `pad=${W}:${H + BAR}:0:0:color=0x111111,ass=${hass}`, "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-threads", "2", "-pix_fmt", "yuv420p", "-movflags", "+faststart", highlights]);
|
|
962
|
+
const out = [full, srt, highlights];
|
|
963
|
+
if (speedup2x) {
|
|
964
|
+
const x2 = join4(videoDir, `${label}-x2.mp4`);
|
|
965
|
+
ff(["-i", full, "-vf", "setpts=PTS/2,fps=25", "-an", "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-threads", "2", "-pix_fmt", "yuv420p", "-movflags", "+faststart", x2]);
|
|
966
|
+
out.push(x2);
|
|
967
|
+
}
|
|
968
|
+
console.log(`video ${label}: full ${duration.toFixed(1)} s, highlights ${at.toFixed(1)} s from ${plan.length} segments`);
|
|
969
|
+
return out;
|
|
970
|
+
}
|
|
971
|
+
function frameTimes(srt, times, max = 10) {
|
|
972
|
+
let ts = [...times];
|
|
973
|
+
if (!ts.length && srt) {
|
|
974
|
+
ts = srt.split("\n").filter((l) => l.includes("-->")).map((l) => {
|
|
975
|
+
const [h = "0", m = "0", rest = "0"] = l.split(" --> ")[0].split(":");
|
|
976
|
+
return Number(h) * 3600 + Number(m) * 60 + Number(rest.replace(",", ".")) + 1.5;
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
if (ts.length > max) {
|
|
980
|
+
const step = ts.length / max;
|
|
981
|
+
ts = Array.from({ length: max }, (_, i) => ts[Math.floor(i * step)]);
|
|
982
|
+
}
|
|
983
|
+
return ts;
|
|
984
|
+
}
|
|
985
|
+
function extractFrames(video, srtFile, outDir, times, max = 10) {
|
|
986
|
+
const srt = srtFile && existsSync3(srtFile) ? readFileSync3(srtFile, "utf8") : null;
|
|
987
|
+
return frameTimes(srt, times, max).map((t, i) => {
|
|
988
|
+
const f = join4(outDir, `frame-${basename(video).replace(/-full\.mp4$/, "")}-${String(i + 1).padStart(2, "0")}-${mmss(t).replace(":", "m")}s.png`);
|
|
989
|
+
ff(["-ss", t.toFixed(2), "-i", video, "-frames:v", "1", f]);
|
|
990
|
+
return f;
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// src/journeys/report.ts
|
|
995
|
+
import { existsSync as existsSync4, readdirSync as readdirSync3, readFileSync as readFileSync4, statSync as statSync3 } from "fs";
|
|
996
|
+
import { join as join5, relative } from "path";
|
|
997
|
+
var seconds = (s) => s === null ? "?" : `${Math.round(s).toLocaleString("en-US")} s`;
|
|
998
|
+
var cell = (text) => String(text ?? "").replace(/\|/g, "\\|").replace(/\s*\n\s*/g, " ");
|
|
999
|
+
var firstLine = (text) => text.split("\n")[0].trim();
|
|
1000
|
+
function videoLabel(file) {
|
|
1001
|
+
const name = file.split("/").pop() ?? file;
|
|
1002
|
+
const m = /^(.*)-(highlights|full|x2)\.mp4$/.exec(name);
|
|
1003
|
+
return m ? { base: m[1], kind: m[2] } : { base: name.replace(/\.mp4$/, ""), kind: "other" };
|
|
1004
|
+
}
|
|
1005
|
+
function tallyTurns(journeys) {
|
|
1006
|
+
const byOutcome = {};
|
|
1007
|
+
let total = 0;
|
|
1008
|
+
for (const j of journeys) {
|
|
1009
|
+
for (const t of j.turns) {
|
|
1010
|
+
total += 1;
|
|
1011
|
+
byOutcome[t.outcome] = (byOutcome[t.outcome] ?? 0) + 1;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
return { total, byOutcome };
|
|
1015
|
+
}
|
|
1016
|
+
function renderIndex(input) {
|
|
1017
|
+
const out = [`# ${input.title}`, ""];
|
|
1018
|
+
const { critic } = input;
|
|
1019
|
+
const tally = tallyTurns(input.journeys);
|
|
1020
|
+
if (critic?.headline) out.push(`**Result.** ${critic.headline}`, "");
|
|
1021
|
+
if (tally.total) {
|
|
1022
|
+
const parts = Object.entries(tally.byOutcome).sort((a, b) => b[1] - a[1]).map(([k, n]) => `${n} ${k}`);
|
|
1023
|
+
out.push(`**Turns, as scored by the recorder:** ${parts.join(", ")}, of ${tally.total} recorded turns (source: each journey's timings.json).`, "");
|
|
1024
|
+
}
|
|
1025
|
+
const all = input.journeys.flatMap((j) => j.videos.map((v) => ({ ...v, dir: j.dir, path: j.dir ? `${j.dir}/${v.file}` : v.file })));
|
|
1026
|
+
const highlights = all.filter((v) => videoLabel(v.file).kind === "highlights");
|
|
1027
|
+
const scoreVideos = (critic?.scorecard ?? []).map((s) => s.video ?? "").filter(Boolean);
|
|
1028
|
+
const rankOf = (path) => {
|
|
1029
|
+
const i = scoreVideos.findIndex((v) => v.endsWith(path));
|
|
1030
|
+
return i < 0 ? Number.MAX_SAFE_INTEGER : i;
|
|
1031
|
+
};
|
|
1032
|
+
highlights.sort((a, b) => rankOf(a.path) - rankOf(b.path) || a.path.localeCompare(b.path));
|
|
1033
|
+
if (highlights.length) {
|
|
1034
|
+
out.push("## Start here: highlights, at most 3 minutes each", "");
|
|
1035
|
+
highlights.forEach((v, i) => {
|
|
1036
|
+
const card = critic?.scorecard?.find((s) => s.video?.endsWith(v.path));
|
|
1037
|
+
out.push(`${i + 1}. \`${v.path}\` (${seconds(v.seconds)})${card ? `. ${card.journey}: ${firstLine(card.topIssue)}` : ""}`);
|
|
1038
|
+
});
|
|
1039
|
+
out.push("");
|
|
1040
|
+
}
|
|
1041
|
+
if (critic?.scorecard?.length) {
|
|
1042
|
+
out.push("## Scorecard", "", "| Journey | Verdict | Key timing | Output | Top issue |", "|---|---|---|---|---|");
|
|
1043
|
+
for (const s of critic.scorecard) out.push(`| ${cell(s.journey)} | ${cell(s.verifiedVerdict)} | ${cell(s.keyTiming)} | ${cell(s.qualityGrade)} | ${cell(s.topIssue)} |`);
|
|
1044
|
+
out.push("");
|
|
1045
|
+
}
|
|
1046
|
+
const fixes = [...critic?.verifiedFailures ?? []].sort((a, b) => a.rank - b.rank);
|
|
1047
|
+
if (fixes.length) {
|
|
1048
|
+
out.push("## Ranked fixes (verified failures, customer impact first)", "");
|
|
1049
|
+
for (const f of fixes) {
|
|
1050
|
+
out.push(`${f.rank}. **${firstLine(f.what)}** (${f.severity}${f.journeys?.length ? `; ${f.journeys.join(", ")}` : ""})`);
|
|
1051
|
+
for (const line of f.what.split("\n").slice(1)) if (line.trim()) out.push(` ${line.trim()}`);
|
|
1052
|
+
out.push(` - Evidence: ${cell(f.evidence)}`);
|
|
1053
|
+
if (f.likelyCause) out.push(` - Likely cause: ${cell(f.likelyCause)}`);
|
|
1054
|
+
out.push(` - Fix: ${cell(f.fix)}`, ` - Number to beat: ${cell(f.numberToBeat)}`, ` - Proving test: ${cell(f.test)}`);
|
|
1055
|
+
}
|
|
1056
|
+
out.push("");
|
|
1057
|
+
}
|
|
1058
|
+
const turns = input.journeys.flatMap((j) => j.turns.map((t) => ({ ...t, dir: j.dir })));
|
|
1059
|
+
if (turns.length) {
|
|
1060
|
+
out.push("## Every recorded turn", "", "| Journey | Context | Sent | Outcome | First text | End | Reply chars | What went wrong |", "|---|---|---|---|---|---|---|---|");
|
|
1061
|
+
for (const t of turns) {
|
|
1062
|
+
out.push(`| ${cell(t.dir || ".")} | ${cell(t.label)} | ${cell(t.sentAt.slice(11, 19))}Z | ${cell(t.outcome)} | ${seconds(t.firstTextS)} | ${seconds(t.completeS)} | ${t.replyChars.toLocaleString("en-US")} | ${cell(t.error ?? "")} |`);
|
|
1063
|
+
}
|
|
1064
|
+
out.push("");
|
|
1065
|
+
}
|
|
1066
|
+
if (all.length) {
|
|
1067
|
+
out.push("## Every video", "");
|
|
1068
|
+
const dirs = [...new Set(all.map((v) => v.dir))];
|
|
1069
|
+
for (const dir of dirs) {
|
|
1070
|
+
out.push(`### \`${dir || "."}\``, "");
|
|
1071
|
+
const byBase = /* @__PURE__ */ new Map();
|
|
1072
|
+
for (const v of all.filter((x) => x.dir === dir)) {
|
|
1073
|
+
const { base } = videoLabel(v.file);
|
|
1074
|
+
byBase.set(base, [...byBase.get(base) ?? [], v]);
|
|
1075
|
+
}
|
|
1076
|
+
for (const [base, vids] of byBase) {
|
|
1077
|
+
const order = ["highlights", "full", "x2", "other"];
|
|
1078
|
+
vids.sort((a, b) => order.indexOf(videoLabel(a.file).kind) - order.indexOf(videoLabel(b.file).kind));
|
|
1079
|
+
out.push(`- ${base}: ${vids.map((v) => `\`${v.file}\` (${seconds(v.seconds)})`).join(", ")}`);
|
|
1080
|
+
}
|
|
1081
|
+
out.push("");
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
const unproven = [...critic?.refutedOrUnproven ?? [], ...critic?.coverageGaps ?? []];
|
|
1085
|
+
if (unproven.length) {
|
|
1086
|
+
out.push("## Refuted, unproven and not covered", "");
|
|
1087
|
+
for (const u of unproven) out.push(`- ${cell(u)}`);
|
|
1088
|
+
out.push("");
|
|
1089
|
+
}
|
|
1090
|
+
return out.join("\n");
|
|
1091
|
+
}
|
|
1092
|
+
function collectJourneyFiles(root, probe) {
|
|
1093
|
+
const found = [];
|
|
1094
|
+
const visit = (dir, depth) => {
|
|
1095
|
+
const timings = join5(dir, "timings.json");
|
|
1096
|
+
const videoDir = join5(dir, "video");
|
|
1097
|
+
const hasVideo = existsSync4(videoDir) && statSync3(videoDir).isDirectory();
|
|
1098
|
+
if (existsSync4(timings) || hasVideo) {
|
|
1099
|
+
const videos = hasVideo ? readdirSync3(videoDir).filter((f) => f.endsWith(".mp4")).sort().map((f) => ({ file: `video/${f}`, seconds: probe(join5(videoDir, f)) })) : [];
|
|
1100
|
+
let turns = [];
|
|
1101
|
+
if (existsSync4(timings)) {
|
|
1102
|
+
try {
|
|
1103
|
+
turns = JSON.parse(readFileSync4(timings, "utf8")).turns ?? [];
|
|
1104
|
+
} catch {
|
|
1105
|
+
turns = [];
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
found.push({ dir: relative(root, dir), videos, turns });
|
|
1109
|
+
}
|
|
1110
|
+
if (depth >= 4) return;
|
|
1111
|
+
for (const name of readdirSync3(dir).sort()) {
|
|
1112
|
+
const path = join5(dir, name);
|
|
1113
|
+
if (name === "video" || name === "turns" || name.startsWith(".")) continue;
|
|
1114
|
+
if (statSync3(path).isDirectory()) visit(path, depth + 1);
|
|
1115
|
+
}
|
|
1116
|
+
};
|
|
1117
|
+
visit(root, 0);
|
|
1118
|
+
return found;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
export {
|
|
1122
|
+
DEFAULT_FAILURE_TEXT,
|
|
1123
|
+
DEFAULT_STOPPED_TEXT,
|
|
1124
|
+
TurnEvidence,
|
|
1125
|
+
STATUS_STALE_MS,
|
|
1126
|
+
turnLooksActive,
|
|
1127
|
+
scoreTurn,
|
|
1128
|
+
DEFAULT_CHAT_CONTRACT,
|
|
1129
|
+
DEFAULT_AUTH_PATHS,
|
|
1130
|
+
CONFIG_FILE_NAMES,
|
|
1131
|
+
defineJourneys,
|
|
1132
|
+
resolveJourneysConfig,
|
|
1133
|
+
threadRouteIn,
|
|
1134
|
+
credentialNames,
|
|
1135
|
+
loadJourneysConfig,
|
|
1136
|
+
scrubTrace,
|
|
1137
|
+
assertNoSecrets,
|
|
1138
|
+
sessionUser,
|
|
1139
|
+
storageStateFor,
|
|
1140
|
+
secretNeedles,
|
|
1141
|
+
MAX_HIGHLIGHTS_S,
|
|
1142
|
+
probeSeconds,
|
|
1143
|
+
chapterCues,
|
|
1144
|
+
highlightPlan,
|
|
1145
|
+
postProcessVideo,
|
|
1146
|
+
rebuildVideo,
|
|
1147
|
+
extractFrames,
|
|
1148
|
+
tallyTurns,
|
|
1149
|
+
renderIndex,
|
|
1150
|
+
collectJourneyFiles
|
|
1151
|
+
};
|
|
1152
|
+
//# sourceMappingURL=chunk-FVI2MHUL.js.map
|