unbrowse 3.1.0-experiments.995f8bb → 3.1.0-experiments.9cbcb13

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.
Files changed (105) hide show
  1. package/dist/cli.js +79 -33
  2. package/dist/index.js +2 -6
  3. package/dist/mcp.js +73 -22
  4. package/dist/server.js +25994 -0
  5. package/package.json +1 -2
  6. package/vendor/kuri/manifest.json +1 -1
  7. package/runtime-src/agent-outcome.ts +0 -166
  8. package/runtime-src/analytics-session.ts +0 -55
  9. package/runtime-src/api/browse-index.ts +0 -343
  10. package/runtime-src/api/browse-session.ts +0 -572
  11. package/runtime-src/api/browse-submit-prereqs.ts +0 -48
  12. package/runtime-src/api/browse-submit.ts +0 -1184
  13. package/runtime-src/api/routes.ts +0 -1833
  14. package/runtime-src/auth/browser-cookies.ts +0 -423
  15. package/runtime-src/auth/index.ts +0 -535
  16. package/runtime-src/auth/runtime.ts +0 -116
  17. package/runtime-src/browser/index.ts +0 -659
  18. package/runtime-src/browser/types.ts +0 -41
  19. package/runtime-src/build-info.generated.ts +0 -6
  20. package/runtime-src/capture/index.ts +0 -1929
  21. package/runtime-src/capture/prefetch.ts +0 -95
  22. package/runtime-src/capture/rsc.ts +0 -45
  23. package/runtime-src/cli/shortcuts.ts +0 -273
  24. package/runtime-src/cli.ts +0 -1734
  25. package/runtime-src/client/graph-client.ts +0 -100
  26. package/runtime-src/client/index.ts +0 -1425
  27. package/runtime-src/debug-trace.ts +0 -18
  28. package/runtime-src/domain.ts +0 -38
  29. package/runtime-src/execution/index.ts +0 -3407
  30. package/runtime-src/execution/retry.ts +0 -46
  31. package/runtime-src/execution/robots.ts +0 -167
  32. package/runtime-src/execution/search-forms.ts +0 -188
  33. package/runtime-src/execution/token-resolver.ts +0 -135
  34. package/runtime-src/extraction/index.ts +0 -1507
  35. package/runtime-src/foundry/publish-bundle.ts +0 -392
  36. package/runtime-src/graph/agent-augment.ts +0 -315
  37. package/runtime-src/graph/index.ts +0 -1532
  38. package/runtime-src/graph/local-fixtures.ts +0 -393
  39. package/runtime-src/graph/local-harness.ts +0 -646
  40. package/runtime-src/graph/planner.ts +0 -411
  41. package/runtime-src/graph/session.ts +0 -294
  42. package/runtime-src/graph/trace-store.ts +0 -136
  43. package/runtime-src/impact-log.ts +0 -227
  44. package/runtime-src/index.ts +0 -24
  45. package/runtime-src/indexer/index.ts +0 -465
  46. package/runtime-src/intent-match.ts +0 -1515
  47. package/runtime-src/kuri/client.ts +0 -1839
  48. package/runtime-src/logger.ts +0 -30
  49. package/runtime-src/marketplace/index.ts +0 -111
  50. package/runtime-src/mcp.ts +0 -1911
  51. package/runtime-src/orchestrator/browser-agent.ts +0 -374
  52. package/runtime-src/orchestrator/dag-advisor.ts +0 -59
  53. package/runtime-src/orchestrator/dag-feedback.ts +0 -257
  54. package/runtime-src/orchestrator/first-pass-action.ts +0 -403
  55. package/runtime-src/orchestrator/index.ts +0 -4480
  56. package/runtime-src/orchestrator/passive-publish.ts +0 -187
  57. package/runtime-src/orchestrator/timing-economics.ts +0 -80
  58. package/runtime-src/payments/cascade.ts +0 -137
  59. package/runtime-src/payments/index.ts +0 -270
  60. package/runtime-src/payments/lobster-pay.ts +0 -182
  61. package/runtime-src/payments/wallet.ts +0 -98
  62. package/runtime-src/publish/review-context.ts +0 -93
  63. package/runtime-src/publish/sanitize.ts +0 -197
  64. package/runtime-src/publish/schema-review.ts +0 -192
  65. package/runtime-src/publish-admission.ts +0 -388
  66. package/runtime-src/ratelimit/index.ts +0 -23
  67. package/runtime-src/reverse-engineer/bundle-scanner.ts +0 -127
  68. package/runtime-src/reverse-engineer/description-prompt.ts +0 -213
  69. package/runtime-src/reverse-engineer/index.ts +0 -1551
  70. package/runtime-src/reverse-engineer/token-sources.ts +0 -379
  71. package/runtime-src/router.ts +0 -17
  72. package/runtime-src/routing-telemetry.ts +0 -395
  73. package/runtime-src/runtime/browser-access.ts +0 -11
  74. package/runtime-src/runtime/browser-auth.ts +0 -12
  75. package/runtime-src/runtime/browser-host.ts +0 -48
  76. package/runtime-src/runtime/lifecycle.ts +0 -17
  77. package/runtime-src/runtime/local-server.ts +0 -311
  78. package/runtime-src/runtime/paths.ts +0 -99
  79. package/runtime-src/runtime/setup.ts +0 -251
  80. package/runtime-src/runtime/supervisor.ts +0 -69
  81. package/runtime-src/runtime/update-hints.ts +0 -351
  82. package/runtime-src/server.ts +0 -100
  83. package/runtime-src/session-logs.ts +0 -142
  84. package/runtime-src/settings.ts +0 -221
  85. package/runtime-src/single-binary.ts +0 -143
  86. package/runtime-src/site-policy.ts +0 -54
  87. package/runtime-src/stale-cleanup-runner.ts +0 -144
  88. package/runtime-src/stale-cleanup.ts +0 -133
  89. package/runtime-src/telemetry-attribution.ts +0 -120
  90. package/runtime-src/telemetry.ts +0 -253
  91. package/runtime-src/template-params.ts +0 -141
  92. package/runtime-src/transform/drift.ts +0 -60
  93. package/runtime-src/transform/index.ts +0 -277
  94. package/runtime-src/types/index.ts +0 -1
  95. package/runtime-src/types/skill.ts +0 -931
  96. package/runtime-src/vault/index.ts +0 -196
  97. package/runtime-src/verification/auth-gate.ts +0 -8
  98. package/runtime-src/verification/candidates.ts +0 -27
  99. package/runtime-src/verification/index.ts +0 -120
  100. package/runtime-src/verification/matrix.ts +0 -30
  101. package/runtime-src/version.ts +0 -148
  102. package/runtime-src/workflow/artifact.ts +0 -161
  103. package/runtime-src/workflow/compile.ts +0 -808
  104. package/runtime-src/workflow/publish.ts +0 -225
  105. package/runtime-src/workflow/runtime.ts +0 -213
@@ -1,69 +0,0 @@
1
- export interface RuntimeSupervisor {
2
- start(): Promise<void>;
3
- stop(): Promise<void>;
4
- isRunning(): boolean;
5
- healthCheck(): Promise<{ healthy: boolean; uptime_ms: number }>;
6
- }
7
-
8
- export interface LoginUXConfig {
9
- interactive: boolean;
10
- timeout_ms: number;
11
- fallback_strategy: "skip" | "fail" | "prompt";
12
- progress_callback?: (message: string) => void;
13
- }
14
-
15
- export type HostType = "openclaw" | "mcp" | "hermes" | "elizaos" | "langchain" | "cli" | "unknown";
16
-
17
- export interface HostIntegration {
18
- type: HostType;
19
- name: string;
20
- version: string;
21
- capabilities: string[];
22
- status: "active" | "planned" | "deprecated";
23
- }
24
-
25
- export const SUPPORTED_HOSTS: HostIntegration[] = [
26
- { type: "openclaw", name: "OpenClaw", version: "1.0", capabilities: ["capture", "execute", "search"], status: "active" },
27
- { type: "mcp", name: "MCP Server", version: "1.0", capabilities: ["execute", "search"], status: "active" },
28
- { type: "hermes", name: "Hermes", version: "0.1", capabilities: ["execute"], status: "planned" },
29
- { type: "elizaos", name: "ElizaOS", version: "0.1", capabilities: ["execute"], status: "planned" },
30
- { type: "langchain", name: "LangChain", version: "0.1", capabilities: ["execute", "search"], status: "planned" },
31
- { type: "cli", name: "CLI", version: "2.0", capabilities: ["capture", "execute", "search", "publish"], status: "active" },
32
- ];
33
-
34
- export function getDefaultLoginConfig(headless: boolean): LoginUXConfig {
35
- return {
36
- interactive: !headless,
37
- timeout_ms: headless ? 30_000 : 120_000,
38
- fallback_strategy: headless ? "skip" : "prompt",
39
- };
40
- }
41
-
42
- /**
43
- * Local runtime server supervisor stub.
44
- * Manages the lifecycle of the local Unbrowse runtime server.
45
- */
46
- export class LocalSupervisor implements RuntimeSupervisor {
47
- private running = false;
48
- private startTime = 0;
49
-
50
- async start(): Promise<void> {
51
- this.running = true;
52
- this.startTime = Date.now();
53
- }
54
-
55
- async stop(): Promise<void> {
56
- this.running = false;
57
- }
58
-
59
- isRunning(): boolean {
60
- return this.running;
61
- }
62
-
63
- async healthCheck(): Promise<{ healthy: boolean; uptime_ms: number }> {
64
- return {
65
- healthy: this.running,
66
- uptime_ms: this.running ? Date.now() - this.startTime : 0,
67
- };
68
- }
69
- }
@@ -1,351 +0,0 @@
1
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { getPackageRoot } from "./paths.js";
5
-
6
- export type InstallMethod = "repo-clone" | "npm-global" | "unknown";
7
- export type InstallHost = "auto" | "codex" | "claude" | "mcp" | "off" | "unknown";
8
- export type HookAction = "installed" | "updated" | "already-installed" | "not-detected" | "skipped" | "failed";
9
-
10
- export type UpdateHookStatus = {
11
- host: "codex" | "claude";
12
- action: HookAction;
13
- config_file?: string;
14
- message?: string;
15
- };
16
-
17
- export type InstallSourceState = {
18
- method: InstallMethod;
19
- host: InstallHost;
20
- package_root: string;
21
- repo_root?: string;
22
- recorded_at: string;
23
- };
24
-
25
- type UpdateCheckState = {
26
- checked_at?: string;
27
- latest_version?: string;
28
- latest_checked_at?: string;
29
- notified_version?: string;
30
- notified_at?: string;
31
- };
32
-
33
- export type UpdateCheckResult = {
34
- installed: string;
35
- latest: string | null;
36
- has_update: boolean;
37
- install: InstallSourceState;
38
- command: string;
39
- checked_at: string;
40
- cached: boolean;
41
- };
42
-
43
- const INSTALL_SCRIPT_URL = "https://unbrowse.ai/install.sh";
44
- const DEFAULT_INTERVAL_MS = 12 * 60 * 60 * 1000;
45
- const CODEX_MARKER = "# Unbrowse update hints — managed by unbrowse setup";
46
-
47
- function getHomeDir(): string {
48
- return process.env.HOME || os.homedir();
49
- }
50
-
51
- function getConfigDir(): string {
52
- if (process.env.UNBROWSE_CONFIG_DIR) return process.env.UNBROWSE_CONFIG_DIR;
53
- return path.join(getHomeDir(), ".unbrowse");
54
- }
55
-
56
- function ensureDir(dir: string): string {
57
- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
58
- return dir;
59
- }
60
-
61
- function readJsonFile<T>(file: string): T | null {
62
- try {
63
- return JSON.parse(readFileSync(file, "utf8")) as T;
64
- } catch {
65
- return null;
66
- }
67
- }
68
-
69
- function writeJsonFile(file: string, value: unknown): void {
70
- ensureDir(path.dirname(file));
71
- writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
72
- }
73
-
74
- function getInstallSourcePath(): string {
75
- return path.join(getConfigDir(), "install-source.json");
76
- }
77
-
78
- function getUpdateCheckStatePath(): string {
79
- return path.join(getConfigDir(), "update-check.json");
80
- }
81
-
82
- function detectRepoRoot(start: string): string | undefined {
83
- let dir = path.resolve(start);
84
- const root = path.parse(dir).root;
85
- while (dir !== root) {
86
- if (existsSync(path.join(dir, ".git"))) return dir;
87
- dir = path.dirname(dir);
88
- }
89
- return undefined;
90
- }
91
-
92
- function detectInstallMethod(packageRoot: string): InstallMethod {
93
- if (process.env.UNBROWSE_SETUP_METHOD === "repo-clone") return "repo-clone";
94
- if (process.env.UNBROWSE_SETUP_METHOD === "npm-global") return "npm-global";
95
- if (packageRoot.includes(`${path.sep}node_modules${path.sep}`)) return "npm-global";
96
- return detectRepoRoot(packageRoot) ? "repo-clone" : "unknown";
97
- }
98
-
99
- function detectInstallHost(repoRoot: string | undefined): InstallHost {
100
- const explicit = process.env.UNBROWSE_SETUP_HOST as InstallHost | undefined;
101
- if (explicit) return explicit;
102
- if (!repoRoot) return "unknown";
103
-
104
- const codexHome = process.env.CODEX_HOME || path.join(getHomeDir(), ".codex");
105
- if (repoRoot === path.join(codexHome, "skills", "unbrowse")) return "codex";
106
- if (repoRoot === path.join(getHomeDir(), ".claude", "skills", "unbrowse")) return "claude";
107
- if (repoRoot === path.join(getHomeDir(), "unbrowse")) return "off";
108
- return "unknown";
109
- }
110
-
111
- export function getInstalledVersion(metaUrl: string): string {
112
- const packageRoot = getPackageRoot(metaUrl);
113
- try {
114
- const pkg = JSON.parse(readFileSync(path.join(packageRoot, "package.json"), "utf8")) as { version?: string };
115
- return pkg.version ?? "unknown";
116
- } catch {
117
- return "unknown";
118
- }
119
- }
120
-
121
- export function resolveInstallSource(metaUrl: string): InstallSourceState {
122
- const packageRoot = getPackageRoot(metaUrl);
123
- const envRepoRoot = process.env.UNBROWSE_SETUP_ROOT || undefined;
124
- const repoRoot = envRepoRoot || detectRepoRoot(packageRoot);
125
- return {
126
- method: detectInstallMethod(packageRoot),
127
- host: detectInstallHost(repoRoot),
128
- package_root: packageRoot,
129
- repo_root: repoRoot,
130
- recorded_at: new Date().toISOString(),
131
- };
132
- }
133
-
134
- export function saveInstallSource(metaUrl: string): InstallSourceState {
135
- const state = resolveInstallSource(metaUrl);
136
- writeJsonFile(getInstallSourcePath(), state);
137
- return state;
138
- }
139
-
140
- export function loadInstallSource(metaUrl: string): InstallSourceState {
141
- return readJsonFile<InstallSourceState>(getInstallSourcePath()) ?? resolveInstallSource(metaUrl);
142
- }
143
-
144
- function compareSemver(a: string, b: string): number {
145
- const parse = (value: string) => value.split("-", 1)[0].split(".").map((part) => Number.parseInt(part, 10) || 0);
146
- const left = parse(a);
147
- const right = parse(b);
148
- const max = Math.max(left.length, right.length);
149
- for (let i = 0; i < max; i++) {
150
- const diff = (left[i] ?? 0) - (right[i] ?? 0);
151
- if (diff !== 0) return diff;
152
- }
153
- return 0;
154
- }
155
-
156
- async function fetchLatestVersion(): Promise<string | null> {
157
- try {
158
- const res = await fetch("https://registry.npmjs.org/unbrowse/latest", {
159
- signal: AbortSignal.timeout(8_000),
160
- headers: { Accept: "application/json" },
161
- });
162
- if (!res.ok) return null;
163
- const body = await res.json() as { version?: string };
164
- return typeof body.version === "string" && body.version.trim() ? body.version.trim() : null;
165
- } catch {
166
- return null;
167
- }
168
- }
169
-
170
- function getUpdateIntervalMs(): number {
171
- const value = Number.parseInt(process.env.UNBROWSE_UPDATE_CHECK_INTERVAL_MS ?? "", 10);
172
- return Number.isFinite(value) && value > 0 ? value : DEFAULT_INTERVAL_MS;
173
- }
174
-
175
- export function buildUpgradeCommand(install: InstallSourceState): string {
176
- if (install.method === "repo-clone" && install.repo_root) {
177
- const host = install.host === "unknown" || install.host === "auto" ? "off" : install.host;
178
- return `cd ${install.repo_root} && git pull --ff-only && ./setup --host ${host}`;
179
- }
180
- return `curl -fsSL ${INSTALL_SCRIPT_URL} | bash`;
181
- }
182
-
183
- export async function checkForUpdates(metaUrl: string, options?: { force?: boolean }): Promise<UpdateCheckResult> {
184
- const installed = getInstalledVersion(metaUrl);
185
- const install = loadInstallSource(metaUrl);
186
- const statePath = getUpdateCheckStatePath();
187
- const state = readJsonFile<UpdateCheckState>(statePath) ?? {};
188
- const checkedAt = new Date().toISOString();
189
- const intervalMs = getUpdateIntervalMs();
190
- const lastChecked = state.latest_checked_at ? Date.parse(state.latest_checked_at) : Number.NaN;
191
- const useCache = !options?.force &&
192
- !!state.latest_version &&
193
- Number.isFinite(lastChecked) &&
194
- Date.now() - lastChecked < intervalMs;
195
- const latest = useCache ? state.latest_version ?? null : await fetchLatestVersion();
196
-
197
- if (!useCache) {
198
- writeJsonFile(statePath, {
199
- ...state,
200
- checked_at: checkedAt,
201
- latest_version: latest ?? undefined,
202
- latest_checked_at: checkedAt,
203
- } satisfies UpdateCheckState);
204
- }
205
-
206
- return {
207
- installed,
208
- latest,
209
- has_update: !!latest && compareSemver(latest, installed) > 0,
210
- install,
211
- command: buildUpgradeCommand(install),
212
- checked_at: checkedAt,
213
- cached: useCache,
214
- };
215
- }
216
-
217
- export function recordUpdateHint(latestVersion: string): void {
218
- const statePath = getUpdateCheckStatePath();
219
- const state = readJsonFile<UpdateCheckState>(statePath) ?? {};
220
- writeJsonFile(statePath, {
221
- ...state,
222
- notified_version: latestVersion,
223
- notified_at: new Date().toISOString(),
224
- } satisfies UpdateCheckState);
225
- }
226
-
227
- function commandIncludesHook(command: string | undefined, marker: string): boolean {
228
- return typeof command === "string" && command.includes(marker);
229
- }
230
-
231
- function getCodexConfigPath(): string {
232
- const codexHome = process.env.CODEX_HOME || path.join(getHomeDir(), ".codex");
233
- return path.join(codexHome, "config.toml");
234
- }
235
-
236
- function getClaudeSettingsPath(): string {
237
- return path.join(getHomeDir(), ".claude", "settings.json");
238
- }
239
-
240
- function getHookScriptPath(metaUrl: string): string {
241
- return path.join(getPackageRoot(metaUrl), "bin", "unbrowse-update-hint.mjs");
242
- }
243
-
244
- function ensureCodexHooksFeature(content: string): string {
245
- if (/\bcodex_hooks\s*=\s*true\b/.test(content)) return content;
246
- if (/\[features\]/.test(content)) {
247
- return content.replace(/\[features\]\r?\n/, (match) => `${match}codex_hooks = true\n`);
248
- }
249
- const prefix = content && !content.endsWith("\n") ? "\n" : "";
250
- return `${content}${prefix}[features]\ncodex_hooks = true\n`;
251
- }
252
-
253
- function writeCodexHook(metaUrl: string): UpdateHookStatus {
254
- const configPath = getCodexConfigPath();
255
- if (!existsSync(path.dirname(configPath))) {
256
- return { host: "codex", action: "not-detected", config_file: configPath };
257
- }
258
-
259
- try {
260
- const hookScript = getHookScriptPath(metaUrl).replace(/\\/g, "/");
261
- const fileExistsBefore = existsSync(configPath);
262
- let content = fileExistsBefore ? readFileSync(configPath, "utf8") : "";
263
- const previous = content;
264
- content = ensureCodexHooksFeature(content);
265
-
266
- if (!content.includes("unbrowse-update-hint.mjs")) {
267
- const command = `node "${hookScript}"`;
268
- const prefix = content && !content.endsWith("\n") ? "\n" : "";
269
- content += `${prefix}${CODEX_MARKER}\n[[hooks]]\nevent = "SessionStart"\ncommand = ${JSON.stringify(command)}\n`;
270
- }
271
-
272
- if (content !== previous) {
273
- writeFileSync(configPath, content, "utf8");
274
- return {
275
- host: "codex",
276
- action: fileExistsBefore ? "updated" : "installed",
277
- config_file: configPath,
278
- };
279
- }
280
-
281
- return { host: "codex", action: "already-installed", config_file: configPath };
282
- } catch (error) {
283
- return {
284
- host: "codex",
285
- action: "failed",
286
- config_file: configPath,
287
- message: error instanceof Error ? error.message : String(error),
288
- };
289
- }
290
- }
291
-
292
- type ClaudeSettings = {
293
- hooks?: {
294
- SessionStart?: Array<{
295
- matcher?: string;
296
- hooks?: Array<{ type?: string; command?: string; timeout?: number }>;
297
- }>;
298
- };
299
- };
300
-
301
- function writeClaudeHook(metaUrl: string): UpdateHookStatus {
302
- const settingsPath = getClaudeSettingsPath();
303
- if (!existsSync(path.dirname(settingsPath))) {
304
- return { host: "claude", action: "not-detected", config_file: settingsPath };
305
- }
306
-
307
- try {
308
- const hookScript = getHookScriptPath(metaUrl).replace(/\\/g, "/");
309
- const command = `node "${hookScript}"`;
310
- const fileExistsBefore = existsSync(settingsPath);
311
- const settings = readJsonFile<ClaudeSettings>(settingsPath) ?? {};
312
- settings.hooks ??= {};
313
- settings.hooks.SessionStart ??= [];
314
-
315
- const existing = settings.hooks.SessionStart.some((entry) =>
316
- Array.isArray(entry.hooks) && entry.hooks.some((hook) => commandIncludesHook(hook.command, "unbrowse-update-hint.mjs"))
317
- );
318
-
319
- if (!existing) {
320
- settings.hooks.SessionStart.push({
321
- hooks: [{ type: "command", command }],
322
- });
323
- writeJsonFile(settingsPath, settings);
324
- return {
325
- host: "claude",
326
- action: fileExistsBefore ? "updated" : "installed",
327
- config_file: settingsPath,
328
- };
329
- }
330
-
331
- return { host: "claude", action: "already-installed", config_file: settingsPath };
332
- } catch (error) {
333
- return {
334
- host: "claude",
335
- action: "failed",
336
- config_file: settingsPath,
337
- message: error instanceof Error ? error.message : String(error),
338
- };
339
- }
340
- }
341
-
342
- export function configureUpdateHintHooks(metaUrl: string, install?: InstallSourceState): UpdateHookStatus[] {
343
- if (process.env.UNBROWSE_DISABLE_UPDATE_HINTS === "1") return [];
344
- const source = install ?? loadInstallSource(metaUrl);
345
- const configuredHosts =
346
- source.host === "codex" || source.host === "claude"
347
- ? [source.host]
348
- : ["codex", "claude"];
349
-
350
- return configuredHosts.map((host) => (host === "codex" ? writeCodexHook(metaUrl) : writeClaudeHook(metaUrl)));
351
- }
@@ -1,100 +0,0 @@
1
- import { execSync } from "node:child_process";
2
- import { mkdirSync, unlinkSync, writeFileSync } from "node:fs";
3
- import path from "node:path";
4
- import Fastify, { type FastifyInstance } from "fastify";
5
- import cors from "@fastify/cors";
6
- import { registerRoutes } from "./api/routes.js";
7
- import { registerRateLimiter } from "./ratelimit/index.js";
8
- import { schedulePeriodicVerification } from "./verification/index.js";
9
- import { startBackgroundRegistration } from "./client/index.js";
10
- import { shutdownAllBrowsers } from "./capture/index.js";
11
- import * as kuri from "./kuri/client.js";
12
- import { schedulePeriodicStaleCleanup } from "./stale-cleanup-runner.js";
13
- import { CODE_HASH, PACKAGE_VERSION } from "./version.js";
14
-
15
- type StartServerOptions = {
16
- host?: string;
17
- port?: number;
18
- logger?: boolean;
19
- pidFile?: string;
20
- scheduleVerification?: boolean;
21
- };
22
-
23
- export type RunningUnbrowseServer = {
24
- app: FastifyInstance;
25
- host: string;
26
- port: number;
27
- close: (options?: { shutdownBrowsers?: boolean }) => Promise<void>;
28
- };
29
-
30
- function updatePidFile(pidFile?: string, host = "127.0.0.1", port = 6969): void {
31
- if (!pidFile) return;
32
- try {
33
- mkdirSync(path.dirname(pidFile), { recursive: true });
34
- writeFileSync(pidFile, JSON.stringify({
35
- pid: process.pid,
36
- base_url: `http://${host}:${port}`,
37
- started_at: new Date().toISOString(),
38
- version: PACKAGE_VERSION,
39
- code_hash: CODE_HASH,
40
- }, null, 2));
41
- } catch {
42
- // ignore pid-file failures
43
- }
44
- }
45
-
46
- function clearPidFile(pidFile?: string): void {
47
- if (!pidFile) return;
48
- try {
49
- unlinkSync(pidFile);
50
- } catch {
51
- // ignore pid-file failures
52
- }
53
- }
54
-
55
- export async function startUnbrowseServer(options: StartServerOptions = {}): Promise<RunningUnbrowseServer> {
56
- const host = options.host ?? process.env.HOST ?? "127.0.0.1";
57
- const port = options.port ?? Number(process.env.PORT ?? 6969);
58
- const pidFile = options.pidFile ?? process.env.UNBROWSE_PID_FILE;
59
-
60
- updatePidFile(pidFile, host, port);
61
-
62
- try {
63
- execSync("pkill -f chrome-headless-shell", { stdio: "ignore" });
64
- } catch {
65
- // no orphans
66
- }
67
-
68
- // Kuri starts on demand when browse/capture commands need it.
69
- // No eager start — avoids launching Chrome on every server restart.
70
- // Registration is allowed to finish in the background so /health is not
71
- // blocked by remote Worker latency during server bootstrap.
72
- void startBackgroundRegistration();
73
-
74
- const app = Fastify({ logger: options.logger ?? true });
75
- await app.register(cors, { origin: true });
76
- await registerRateLimiter(app);
77
- await registerRoutes(app);
78
- await app.listen({ port, host });
79
- if (options.scheduleVerification ?? true) {
80
- schedulePeriodicVerification();
81
- schedulePeriodicStaleCleanup();
82
- }
83
-
84
- return {
85
- app,
86
- host,
87
- port,
88
- async close(options?: { shutdownBrowsers?: boolean }): Promise<void> {
89
- if (options?.shutdownBrowsers ?? true) {
90
- await shutdownAllBrowsers();
91
- }
92
- await app.close();
93
- clearPidFile(pidFile);
94
- },
95
- };
96
- }
97
-
98
- export function installServerExitCleanup(pidFile?: string): void {
99
- process.on("exit", () => clearPidFile(pidFile));
100
- }
@@ -1,142 +0,0 @@
1
- import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
2
- import { join } from "node:path";
3
- import { getRegistrableDomain } from "./domain.js";
4
-
5
- export interface SessionLogEntry {
6
- file: string;
7
- kind: "execution" | "resolve" | "generation" | "debug";
8
- trace_id?: string;
9
- skill_id?: string;
10
- endpoint_id?: string;
11
- intent?: string;
12
- url?: string;
13
- started_at?: string;
14
- completed_at?: string;
15
- success?: boolean;
16
- status_code?: number;
17
- error?: string;
18
- }
19
-
20
- function normalizeDomainVariants(domain: string): string[] {
21
- const trimmed = domain.trim().toLowerCase();
22
- if (!trimmed) return [];
23
- const variants = new Set<string>([trimmed, trimmed.replace(/^www\./, "")]);
24
- try {
25
- variants.add(getRegistrableDomain(trimmed));
26
- } catch {
27
- // ignore invalid input
28
- }
29
- return [...variants].filter(Boolean);
30
- }
31
-
32
- function textContainsDomain(text: string, variants: string[]): boolean {
33
- const lower = text.toLowerCase();
34
- return variants.some((variant) => lower.includes(variant));
35
- }
36
-
37
- function findMatchingUrl(value: unknown, variants: string[], depth = 0): string | undefined {
38
- if (depth > 6 || value == null) return undefined;
39
- if (typeof value === "string") {
40
- if (/^https?:\/\//i.test(value) && textContainsDomain(value, variants)) return value;
41
- return undefined;
42
- }
43
- if (Array.isArray(value)) {
44
- for (const item of value.slice(0, 20)) {
45
- const match = findMatchingUrl(item, variants, depth + 1);
46
- if (match) return match;
47
- }
48
- return undefined;
49
- }
50
- if (typeof value === "object") {
51
- for (const next of Object.values(value as Record<string, unknown>).slice(0, 40)) {
52
- const match = findMatchingUrl(next, variants, depth + 1);
53
- if (match) return match;
54
- }
55
- }
56
- return undefined;
57
- }
58
-
59
- function inferKind(file: string, payload: Record<string, unknown>): SessionLogEntry["kind"] {
60
- if (file.includes("-resolve-")) return "resolve";
61
- if (file.includes("-generation-")) return "generation";
62
- if (typeof payload.trace_id === "string") return "execution";
63
- return "debug";
64
- }
65
-
66
- function timestampFromFilename(filePath: string): string | undefined {
67
- const match = filePath.match(/(\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z)/);
68
- if (!match) return undefined;
69
- return match[1].replace(
70
- /(\d{4}-\d{2}-\d{2}T\d{2})-(\d{2})-(\d{2})-(\d{3}Z)/,
71
- "$1:$2:$3.$4",
72
- );
73
- }
74
-
75
- function extractTimestamp(payload: Record<string, unknown>, filePath: string): string | undefined {
76
- const startedAt = typeof payload.started_at === "string" ? payload.started_at : undefined;
77
- const completedAt = typeof payload.completed_at === "string" ? payload.completed_at : undefined;
78
- if (startedAt) return startedAt;
79
- if (completedAt) return completedAt;
80
- const fileTimestamp = timestampFromFilename(filePath);
81
- if (fileTimestamp) return fileTimestamp;
82
- try {
83
- return statSync(filePath).mtime.toISOString();
84
- } catch {
85
- return undefined;
86
- }
87
- }
88
-
89
- export function listRecentSessionsForDomain(tracesDir: string, domain: string, limit = 10): SessionLogEntry[] {
90
- if (!existsSync(tracesDir)) return [];
91
- const variants = normalizeDomainVariants(domain);
92
- if (variants.length === 0) return [];
93
-
94
- const entries: Array<SessionLogEntry & { sort_key?: string }> = [];
95
- for (const name of readdirSync(tracesDir)) {
96
- if (!name.endsWith(".json")) continue;
97
- const filePath = join(tracesDir, name);
98
- let raw = "";
99
- try {
100
- raw = readFileSync(filePath, "utf-8");
101
- } catch {
102
- continue;
103
- }
104
- if (!textContainsDomain(raw, variants)) continue;
105
-
106
- let parsed: Record<string, unknown>;
107
- try {
108
- parsed = JSON.parse(raw) as Record<string, unknown>;
109
- } catch {
110
- continue;
111
- }
112
-
113
- const matchingUrl = findMatchingUrl(parsed, variants);
114
- const sortKey = extractTimestamp(parsed, filePath);
115
- entries.push({
116
- file: name,
117
- kind: inferKind(name, parsed),
118
- trace_id: typeof parsed.trace_id === "string" ? parsed.trace_id : undefined,
119
- skill_id: typeof parsed.skill_id === "string" ? parsed.skill_id : undefined,
120
- endpoint_id: typeof parsed.endpoint_id === "string"
121
- ? parsed.endpoint_id
122
- : typeof parsed.selected_endpoint_id === "string"
123
- ? parsed.selected_endpoint_id
124
- : undefined,
125
- intent: typeof parsed.intent === "string" ? parsed.intent : undefined,
126
- url: matchingUrl,
127
- started_at: typeof parsed.started_at === "string" ? parsed.started_at : sortKey,
128
- completed_at: typeof parsed.completed_at === "string" ? parsed.completed_at : undefined,
129
- success: typeof parsed.success === "boolean"
130
- ? parsed.success
131
- : parsed.outcome === "autoexec_success"
132
- ? true
133
- : undefined,
134
- status_code: typeof parsed.status_code === "number" ? parsed.status_code : undefined,
135
- error: typeof parsed.error === "string" ? parsed.error : undefined,
136
- sort_key: sortKey,
137
- });
138
- }
139
-
140
- entries.sort((lhs, rhs) => (rhs.sort_key ?? "").localeCompare(lhs.sort_key ?? ""));
141
- return entries.slice(0, Math.max(1, limit)).map(({ sort_key: _sortKey, ...entry }) => entry);
142
- }