@parcha/pi-browser 0.0.0-bootstrap.0 → 0.1.0-beta.13
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/CHANGELOG.md +16 -0
- package/LICENSE +202 -0
- package/NOTICE +33 -0
- package/README.md +328 -1
- package/dist/coding-agent-local.d.ts +16 -0
- package/dist/coding-agent-local.js +138 -0
- package/dist/coding-agent.d.ts +78 -0
- package/dist/coding-agent.js +446 -0
- package/dist/core/contract.d.ts +26 -0
- package/dist/core/contract.js +154 -0
- package/dist/core/custody.d.ts +111 -0
- package/dist/core/custody.js +122 -0
- package/dist/core/decisions.d.ts +43 -0
- package/dist/core/decisions.js +4 -0
- package/dist/core/downloads.d.ts +35 -0
- package/dist/core/downloads.js +131 -0
- package/dist/core/effects.d.ts +63 -0
- package/dist/core/effects.js +256 -0
- package/dist/core/evidence.d.ts +43 -0
- package/dist/core/evidence.js +6 -0
- package/dist/core/failures.d.ts +28 -0
- package/dist/core/failures.js +59 -0
- package/dist/core/find.d.ts +32 -0
- package/dist/core/find.js +179 -0
- package/dist/core/guard.d.ts +6 -0
- package/dist/core/guard.js +24 -0
- package/dist/core/host.d.ts +279 -0
- package/dist/core/host.js +2 -0
- package/dist/core/images.d.ts +37 -0
- package/dist/core/images.js +60 -0
- package/dist/core/lease.d.ts +16 -0
- package/dist/core/lease.js +30 -0
- package/dist/core/receipts.d.ts +19 -0
- package/dist/core/receipts.js +22 -0
- package/dist/core/redact.d.ts +13 -0
- package/dist/core/redact.js +83 -0
- package/dist/core/session-control.d.ts +25 -0
- package/dist/core/session-control.js +75 -0
- package/dist/core/sites.d.ts +33 -0
- package/dist/core/sites.js +102 -0
- package/dist/core/tools.d.ts +37 -0
- package/dist/core/tools.js +170 -0
- package/dist/core/uri.d.ts +11 -0
- package/dist/core/uri.js +37 -0
- package/dist/core/usage.d.ts +38 -0
- package/dist/core/usage.js +44 -0
- package/dist/core/web.d.ts +98 -0
- package/dist/core/web.js +327 -0
- package/dist/driver/stagehand.d.ts +25 -0
- package/dist/driver/stagehand.js +141 -0
- package/dist/durable-result.d.ts +6 -0
- package/dist/durable-result.js +2 -0
- package/dist/durable.d.ts +75 -0
- package/dist/durable.js +484 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +19 -0
- package/dist/providers/browserbase-net.d.ts +13 -0
- package/dist/providers/browserbase-net.js +96 -0
- package/dist/providers/browserbase.d.ts +152 -0
- package/dist/providers/browserbase.js +262 -0
- package/dist/providers/cdp-call.d.ts +6 -0
- package/dist/providers/cdp-call.js +25 -0
- package/dist/providers/cdp.d.ts +48 -0
- package/dist/providers/cdp.js +353 -0
- package/dist/providers/http.d.ts +2 -0
- package/dist/providers/http.js +21 -0
- package/dist/providers/kernel.d.ts +49 -0
- package/dist/providers/kernel.js +246 -0
- package/dist/providers/shared.d.ts +47 -0
- package/dist/providers/shared.js +82 -0
- package/dist/recorder/credential.d.ts +9 -0
- package/dist/recorder/credential.js +49 -0
- package/dist/recorder/follow.d.ts +26 -0
- package/dist/recorder/follow.js +138 -0
- package/dist/recorder/frames.d.ts +80 -0
- package/dist/recorder/frames.js +309 -0
- package/dist/recorder/index.d.ts +3 -0
- package/dist/recorder/index.js +6 -0
- package/dist/recorder/jpeg.d.ts +5 -0
- package/dist/recorder/jpeg.js +34 -0
- package/dist/store.d.ts +56 -0
- package/dist/store.js +196 -0
- package/dist/testing/backend.d.ts +231 -0
- package/dist/testing/backend.js +414 -0
- package/dist/testing/driver.d.ts +28 -0
- package/dist/testing/driver.js +32 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/pages.d.ts +3 -0
- package/dist/testing/pages.js +10 -0
- package/dist/testing/sentinels.d.ts +29 -0
- package/dist/testing/sentinels.js +38 -0
- package/dist/vendor/stagehand-facade/contract.d.ts +348 -0
- package/dist/vendor/stagehand-facade/contract.js +155 -0
- package/dist/vendor/stagehand-facade/runtime.d.ts +90 -0
- package/dist/vendor/stagehand-facade/runtime.js +1640 -0
- package/dist/vendor/stagehand-facade/screenshot-transport.d.ts +15 -0
- package/dist/vendor/stagehand-facade/screenshot-transport.js +51 -0
- package/dist/vendor/stagehand-facade/tools.d.ts +31 -0
- package/dist/vendor/stagehand-facade/tools.js +139 -0
- package/dist/web.d.ts +41 -0
- package/dist/web.js +38 -0
- package/package.json +125 -1
- package/src/vendor/stagehand-facade/UPSTREAM.json +24 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type JsonSchema = {
|
|
2
|
+
readonly [key: string]: unknown;
|
|
3
|
+
};
|
|
4
|
+
/** `read` changes nothing a rerun could double; `effect` may act on a site; `custody` opens or closes a paid session. */
|
|
5
|
+
export type EffectClass = "read" | "effect" | "custody";
|
|
6
|
+
export type ToolContract = {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly label: string;
|
|
9
|
+
readonly description: string;
|
|
10
|
+
readonly parameters: JsonSchema;
|
|
11
|
+
readonly effect: EffectClass;
|
|
12
|
+
/** After a crash a `safe` call reruns with the same arguments; an `unsafe` one is reported interrupted, never rerun. */
|
|
13
|
+
readonly replay: "safe" | "unsafe";
|
|
14
|
+
/** One sequential call makes its whole round sequential. */
|
|
15
|
+
readonly executionMode: "sequential" | "parallel";
|
|
16
|
+
readonly evidence: "none" | "receipt" | "image" | "files" | "urls";
|
|
17
|
+
readonly listedWhen: "always" | "downloads" | "fetch" | "search" | "sites";
|
|
18
|
+
};
|
|
19
|
+
export declare const BROWSER_TOOLS: readonly ToolContract[];
|
|
20
|
+
export declare const WEB_TOOLS: readonly ToolContract[];
|
|
21
|
+
export declare const BROWSER_TOOL_NAMES: readonly string[];
|
|
22
|
+
export declare const WEB_TOOL_NAMES: readonly string[];
|
|
23
|
+
/** Depends only on configuration, never live state, so every request renders the same text and keeps the prompt cache. */
|
|
24
|
+
export declare function browserSection(policy: {
|
|
25
|
+
readonly idleReleaseS: number;
|
|
26
|
+
}): string;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
const actionSchema = (op, extra = {}) => ({
|
|
2
|
+
type: "object",
|
|
3
|
+
properties: {
|
|
4
|
+
op: { const: op, description: `Action operation. Use "op": "${op}"; never use a "kind" field.` },
|
|
5
|
+
id: { type: "string", description: 'Bracketed ID copied from the latest snapshot, as a string. Use "id"; never use "ref".' },
|
|
6
|
+
...extra,
|
|
7
|
+
},
|
|
8
|
+
required: ["op", "id", ...Object.keys(extra).filter((key) => key !== "delay")],
|
|
9
|
+
additionalProperties: false,
|
|
10
|
+
});
|
|
11
|
+
const object = (properties, required = []) => ({ type: "object", additionalProperties: false, properties, ...(required.length ? { required } : {}) });
|
|
12
|
+
const snapshot = {
|
|
13
|
+
name: "snapshot",
|
|
14
|
+
label: "Browser: snapshot",
|
|
15
|
+
description: "Capture the active page's Stagehand accessibility tree and hydrate its displayed IDs for subsequent run actions. Every call replaces the active page's ID map.",
|
|
16
|
+
parameters: object({ includeIframes: { type: "boolean", default: true } }),
|
|
17
|
+
effect: "read", replay: "safe", executionMode: "sequential", evidence: "none", listedWhen: "always",
|
|
18
|
+
};
|
|
19
|
+
const run = {
|
|
20
|
+
name: "run",
|
|
21
|
+
label: "Browser: run",
|
|
22
|
+
description: 'Browse and automate websites in the persistent Stagehand browser. Navigate with JavaScript such as await page.goto("https://example.com"); there is no separate navigate or start tool. Execute either a JavaScript workflow against the Stagehand Playwright facade or a batch of actions using IDs from the latest snapshot. Provide exactly one of code or actions. Each action must use "op" (never "kind") and "id" (never "ref"). Copy the bracketed snapshot ID as a string. Examples: {"actions":[{"op":"click","id":"1-42"}]}, {"actions":[{"op":"fill","id":"2-14","value":"Miami"}]}, {"actions":[{"op":"select","id":"3-9","values":"Lowest price"}]}.',
|
|
23
|
+
// No top-level oneOf for "exactly one of code or actions": AI-SDK-based clients reject it, so the tool enforces it.
|
|
24
|
+
parameters: object({
|
|
25
|
+
code: { type: "string", minLength: 1 },
|
|
26
|
+
actions: {
|
|
27
|
+
type: "array",
|
|
28
|
+
description: 'Snapshot actions with the exact fields "op" and "id". Do not use "kind" or "ref".',
|
|
29
|
+
items: {
|
|
30
|
+
oneOf: [
|
|
31
|
+
actionSchema("click"),
|
|
32
|
+
actionSchema("hover"),
|
|
33
|
+
actionSchema("fill", { value: { type: "string" } }),
|
|
34
|
+
actionSchema("type", { text: { type: "string" }, delay: { type: "number", minimum: 0 } }),
|
|
35
|
+
actionSchema("press", { key: { type: "string" } }),
|
|
36
|
+
actionSchema("select", { values: { oneOf: [{ type: "string" }, { type: "array", items: { type: "string" }, minItems: 1 }] } }),
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
minItems: 1,
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
effect: "effect", replay: "unsafe", executionMode: "sequential", evidence: "none", listedWhen: "always",
|
|
43
|
+
};
|
|
44
|
+
const screenshot = {
|
|
45
|
+
name: "screenshot",
|
|
46
|
+
label: "Browser: screenshot",
|
|
47
|
+
description: 'Capture a screenshot of the active page. For size-constrained MCP clients, prefer a viewport JPEG: {"type":"jpeg","quality":40,"fullPage":false}.',
|
|
48
|
+
parameters: object({
|
|
49
|
+
fullPage: { type: "boolean" },
|
|
50
|
+
type: { type: "string", enum: ["png", "jpeg"] },
|
|
51
|
+
quality: { type: "number", minimum: 0, maximum: 100 },
|
|
52
|
+
ask: { type: "string", description: "What you want to know from this capture. Answered from the pixels when your model cannot see them; echoed back when it can." },
|
|
53
|
+
}),
|
|
54
|
+
effect: "read", replay: "safe", executionMode: "sequential", evidence: "image", listedWhen: "always",
|
|
55
|
+
};
|
|
56
|
+
const browserRead = {
|
|
57
|
+
name: "browser_read",
|
|
58
|
+
label: "Browser: read",
|
|
59
|
+
description: "Read the CURRENT page of the browser as evidence and persist it under evidence/ with the page URL: " +
|
|
60
|
+
"`what` = markdown (the whole page as markdown, the default for citing), text, html, url, or title. " +
|
|
61
|
+
"This is the citable door for anything you saw in the browser: snapshot, screenshot and run return values are not evidence. Cite the returned URL.",
|
|
62
|
+
parameters: object({ what: { type: "string", enum: ["markdown", "text", "html", "url", "title"], description: "Default markdown." } }),
|
|
63
|
+
effect: "read", replay: "safe", executionMode: "sequential", evidence: "receipt", listedWhen: "always",
|
|
64
|
+
};
|
|
65
|
+
const browserRelease = {
|
|
66
|
+
name: "browser_release",
|
|
67
|
+
label: "Browser: release",
|
|
68
|
+
description: "Release the browser session now. Call it as soon as you are done browsing for this task: an open session costs browser minutes whether or not you use it. " +
|
|
69
|
+
"Your next browser call launches a fresh session (a new tab, no cookies from the old one), so finish a flow before releasing.",
|
|
70
|
+
parameters: object({}),
|
|
71
|
+
effect: "custody", replay: "safe", executionMode: "sequential", evidence: "none", listedWhen: "always",
|
|
72
|
+
};
|
|
73
|
+
const browserRelaunch = {
|
|
74
|
+
name: "browser_relaunch",
|
|
75
|
+
label: "Browser: relaunch",
|
|
76
|
+
description: "Replace the browser session with a fresh one, after a site blocked it (403/429, a CAPTCHA wall, a bot-detection page, an empty page that should have content). The new session keeps the run's Verified setting unless `verified` names one: `verified: true` where the run's sessions are not Verified (a Verified browser carries a stronger identity and costs more). " +
|
|
77
|
+
"Use `geolocation` to browse as a visitor from a given place: the proxy egresses from that country (ISO alpha-2), optionally a US state and a city, so geo-aware sites (storefront region, prices, payment methods, content availability) show what a local sees. " +
|
|
78
|
+
"Page state is lost; navigate again with run.",
|
|
79
|
+
parameters: object({
|
|
80
|
+
verified: { type: "boolean", description: "true launches the new session as a Verified browser, false as one that is not. Omitted, the new session keeps the run's setting." },
|
|
81
|
+
geolocation: {
|
|
82
|
+
type: "object",
|
|
83
|
+
additionalProperties: false,
|
|
84
|
+
description: "Proxy egress location for the new session. Omit to keep the run's default.",
|
|
85
|
+
properties: {
|
|
86
|
+
country: { type: "string", description: "ISO 3166-1 alpha-2 country code, e.g. GB, US, DE." },
|
|
87
|
+
state: { type: "string", description: "US state abbreviation, United States only." },
|
|
88
|
+
city: { type: "string", description: "City name, e.g. LONDON or SAO_PAULO." },
|
|
89
|
+
},
|
|
90
|
+
required: ["country"],
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
|
+
effect: "custody", replay: "safe", executionMode: "sequential", evidence: "none", listedWhen: "always",
|
|
94
|
+
};
|
|
95
|
+
const browserDownloads = {
|
|
96
|
+
name: "browser_downloads",
|
|
97
|
+
label: "Browser: downloads",
|
|
98
|
+
description: "Copy the files this browser session downloaded into the workspace under downloads/, with downloads/manifest.json listing each file's SHA-256. " +
|
|
99
|
+
"Returns the files fetched now, those fetched before and those skipped, never their bytes; open a fetched file with read.",
|
|
100
|
+
parameters: object({}),
|
|
101
|
+
effect: "read", replay: "safe", executionMode: "sequential", evidence: "files", listedWhen: "downloads",
|
|
102
|
+
};
|
|
103
|
+
const webFetch = {
|
|
104
|
+
name: "web_fetch",
|
|
105
|
+
label: "Web fetch",
|
|
106
|
+
description: "Fetch one URL as clean markdown without a browser session: fast and cheap, and it handles most static and many dynamic pages. " +
|
|
107
|
+
"Use it for reading articles, filings, registries, PDFs-as-pages. If it returns blocked or empty content, drive the page with run/snapshot instead. " +
|
|
108
|
+
"The result is persisted as evidence with its URL.",
|
|
109
|
+
parameters: object({
|
|
110
|
+
url: { type: "string", description: "Absolute http(s) URL." },
|
|
111
|
+
format: { type: "string", enum: ["markdown", "raw"], description: "Default markdown." },
|
|
112
|
+
}, ["url"]),
|
|
113
|
+
effect: "read", replay: "safe", executionMode: "parallel", evidence: "receipt", listedWhen: "fetch",
|
|
114
|
+
};
|
|
115
|
+
const webSearch = {
|
|
116
|
+
name: "web_search",
|
|
117
|
+
label: "Web search",
|
|
118
|
+
description: "Search the web. Returns ranked results with url and title, and author and publication date where the source has them.",
|
|
119
|
+
parameters: object({ query: { type: "string" }, n: { type: "number", description: "Results to return, 1-25 (default 10)." } }, ["query"]),
|
|
120
|
+
effect: "read", replay: "safe", executionMode: "parallel", evidence: "urls", listedWhen: "search",
|
|
121
|
+
};
|
|
122
|
+
export const BROWSER_TOOLS = Object.freeze([snapshot, run, screenshot, browserRead, browserRelease, browserRelaunch, browserDownloads]);
|
|
123
|
+
export const WEB_TOOLS = Object.freeze([webFetch, webSearch]);
|
|
124
|
+
export const BROWSER_TOOL_NAMES = Object.freeze(BROWSER_TOOLS.map((tool) => tool.name));
|
|
125
|
+
export const WEB_TOOL_NAMES = Object.freeze(WEB_TOOLS.map((tool) => tool.name));
|
|
126
|
+
/** Depends only on configuration, never live state, so every request renders the same text and keeps the prompt cache. */
|
|
127
|
+
export function browserSection(policy) {
|
|
128
|
+
const idle = Math.round(policy.idleReleaseS);
|
|
129
|
+
return [
|
|
130
|
+
"You control one remote browser:",
|
|
131
|
+
"- snapshot: the active page's accessibility tree, with bracketed element IDs for run actions.",
|
|
132
|
+
"- run: snapshot actions by ID, or JavaScript against the Playwright-shaped page API. Pass exactly one of code or actions; every action uses \"op\" and \"id\", never \"kind\" or \"ref\".",
|
|
133
|
+
"- screenshot: the rendered page as an image.",
|
|
134
|
+
"- browser_read: the current page's content, filed as evidence with its URL.",
|
|
135
|
+
"- browser_relaunch: a fresh session, Verified or egressing from a given place.",
|
|
136
|
+
"- browser_release: release the session when you are done browsing.",
|
|
137
|
+
"",
|
|
138
|
+
"Snapshot IDs are valid only for the latest snapshot of the active page; snapshot again after navigation or a stale ID. Use snapshot actions for single clicks and fills, and one run code call for a multi-step workflow that returns only the values you need. Do not launch another browser; open more tabs with context.newPage() inside run code.",
|
|
139
|
+
"",
|
|
140
|
+
"The `run` page is Playwright-shaped, not Playwright. On this browser these throw: `page.waitForEvent(\"download\")`, `page.route`, `page.waitForResponse`, `context.newCDPSession`, and `page.on` for `pageerror`, `framenavigated` or `response`. Wait for what the page shows instead (`page.waitForSelector`, `page.waitForLoadState`, or poll `page.url()` or a locator's text in a short loop), read results from the page itself (a locator's text, then `browser_read` to cite it), and never intercept or rewrite requests. A file the page offers as a download cannot be collected from the browser: take the link's URL from the page and fetch it with `web_fetch` when that tool is offered, or cite the page that links it.",
|
|
141
|
+
"",
|
|
142
|
+
// A page call made while a submit's navigation commits throws -32000 or hangs; a bounded wait and one retry avoid
|
|
143
|
+
// it.
|
|
144
|
+
"After a click that loads a new page, wait for the next page with a bounded wait before reading it (for example `await page.waitForSelector(\"#result\", { timeout: 1000 })`), and if a read made while the page was changing throws \"Inspected target navigated or closed\" or \"Cannot find context with specified id\", run that read once more: it is a read, so repeating it is safe.",
|
|
145
|
+
"",
|
|
146
|
+
"Readers and actors: snapshot, screenshot and browser_read only read the page, and a read cut by an interruption runs again by itself. run acts on the site, so a run cut by an interruption is never repeated for you: you are told it was interrupted and on which page, and you check the page before acting again.",
|
|
147
|
+
"",
|
|
148
|
+
`The browser is remote. It is launched on your first browser call and released when you call browser_release, after ${idle} seconds without a browser call, or when your work ends; the call after a release opens a fresh session with no cookies or page state.`,
|
|
149
|
+
"",
|
|
150
|
+
"Evidence: browser_read and screenshot are citable, filed with the page URL. snapshot and run return values are working views, not evidence.",
|
|
151
|
+
"",
|
|
152
|
+
"Never repeat a failing call unchanged: the third identical call that failed the same way is refused. Change the code, the IDs (snapshot again), the URL or the approach.",
|
|
153
|
+
].join("\n");
|
|
154
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { BrowserFailure } from "./failures.js";
|
|
2
|
+
import type { EffectObserver } from "./effects.js";
|
|
3
|
+
import type { AttachTarget, BrowserConfig, Geo, ReleaseReason, SentEffect, SessionRecord, SessionSpec, SessionsState } from "./host.js";
|
|
4
|
+
import { type LeaseState } from "./lease.js";
|
|
5
|
+
export type BrowserDriver = {
|
|
6
|
+
close(): Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
/** A Stagehand close after a dropped and reconnected connection can hang forever; the provider ends the session anyway. */
|
|
9
|
+
export declare const DRIVER_CLOSE_MS = 5000;
|
|
10
|
+
export declare function closeDriver(driver: BrowserDriver): Promise<void>;
|
|
11
|
+
export type DriverOptions = {
|
|
12
|
+
batchTimeoutMs: number;
|
|
13
|
+
};
|
|
14
|
+
export type DriverFactory<D extends BrowserDriver = BrowserDriver> = (target: AttachTarget, signal?: AbortSignal, options?: DriverOptions) => Promise<D>;
|
|
15
|
+
export declare class BrowserFailureError extends Error {
|
|
16
|
+
readonly failure: BrowserFailure;
|
|
17
|
+
name: string;
|
|
18
|
+
constructor(failure: BrowserFailure);
|
|
19
|
+
}
|
|
20
|
+
export type Overrides = {
|
|
21
|
+
verified?: boolean;
|
|
22
|
+
geolocation?: Geo | null;
|
|
23
|
+
};
|
|
24
|
+
export type AttachedSession<D> = {
|
|
25
|
+
driver: D;
|
|
26
|
+
tag: string;
|
|
27
|
+
sessionId: string;
|
|
28
|
+
record: SessionRecord;
|
|
29
|
+
notice: string | null;
|
|
30
|
+
interrupted: SessionRecord["pendingEffect"];
|
|
31
|
+
recorded: boolean;
|
|
32
|
+
effects: Pick<EffectObserver, "record" | "flush" | "holding"> | null;
|
|
33
|
+
};
|
|
34
|
+
export interface CustodyPort<D extends BrowserDriver = BrowserDriver> {
|
|
35
|
+
/** A call rerun after an interruption runs only on its first session, or fails with `session_replaced`. */
|
|
36
|
+
session(): Promise<AttachedSession<D>>;
|
|
37
|
+
/** A rerun finds the session it launched. */
|
|
38
|
+
relaunch(overrides: Overrides): Promise<AttachedSession<D>>;
|
|
39
|
+
/** No session, or one already released, is not an error. */
|
|
40
|
+
release(): Promise<{
|
|
41
|
+
released: boolean;
|
|
42
|
+
sessionId: string | null;
|
|
43
|
+
}>;
|
|
44
|
+
/** Committed before the `run` is dispatched. */
|
|
45
|
+
dispatching(effect: {
|
|
46
|
+
callId: string;
|
|
47
|
+
codeSha: string;
|
|
48
|
+
url: string | null;
|
|
49
|
+
observed: boolean;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
journal(row: SentEffect): Promise<void>;
|
|
52
|
+
settle(): Promise<void>;
|
|
53
|
+
navigated(url: string): Promise<void>;
|
|
54
|
+
/** When the provider reports the session ended, custody records it and the next call opens a fresh one. */
|
|
55
|
+
ended(): Promise<boolean>;
|
|
56
|
+
/** The provider still runs the session: the next `session()` reconnects to it. */
|
|
57
|
+
dropped(): Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
/** `details` reach the host's UI and wraps, never the model; `usage` is pi's, for the call's spend. */
|
|
60
|
+
export type ToolOutput = {
|
|
61
|
+
content: Array<{
|
|
62
|
+
type: "text";
|
|
63
|
+
text: string;
|
|
64
|
+
} | {
|
|
65
|
+
type: "image";
|
|
66
|
+
data: string;
|
|
67
|
+
mimeType: string;
|
|
68
|
+
}>;
|
|
69
|
+
isError?: boolean;
|
|
70
|
+
details?: Record<string, unknown>;
|
|
71
|
+
usage?: Record<string, unknown>;
|
|
72
|
+
};
|
|
73
|
+
export declare const NAVIGATION_CAP = 500;
|
|
74
|
+
export declare const VIEWPORT: Readonly<{
|
|
75
|
+
width: 1288;
|
|
76
|
+
height: 711;
|
|
77
|
+
}>;
|
|
78
|
+
export declare const SESSION_REPLACED: BrowserFailure;
|
|
79
|
+
export declare const notices: {
|
|
80
|
+
kept: (url: string | null) => string;
|
|
81
|
+
lost: (url: string | null) => string;
|
|
82
|
+
ended: (url: string | null) => string;
|
|
83
|
+
released: (reason: "idle" | "close", url: string | null, idleS: number) => string;
|
|
84
|
+
};
|
|
85
|
+
export declare function currentRecord(state: SessionsState | undefined, plane: string): SessionRecord | null;
|
|
86
|
+
export declare function newSessionRecord(input: {
|
|
87
|
+
plane: string;
|
|
88
|
+
tag: string;
|
|
89
|
+
provider: string;
|
|
90
|
+
spec: SessionSpec;
|
|
91
|
+
at: string;
|
|
92
|
+
}): SessionRecord;
|
|
93
|
+
export declare function moveRecord(record: SessionRecord, next: {
|
|
94
|
+
state: LeaseState;
|
|
95
|
+
resourceId?: string;
|
|
96
|
+
reason?: ReleaseReason;
|
|
97
|
+
at: string;
|
|
98
|
+
}): void;
|
|
99
|
+
export declare function sessionSpec(config: BrowserConfig, tag: string, overrides?: Overrides): SessionSpec;
|
|
100
|
+
/** With proxies off a geolocation is refused up front: the session would browse from the provider's default place,
|
|
101
|
+
* silently. */
|
|
102
|
+
export declare function relaunchRefusal(policy: BrowserConfig["policy"], overrides: Overrides): BrowserFailure | null;
|
|
103
|
+
/** An ISO alpha-2 country, a US state abbreviation, a city in capitals; null when the country is not two letters. */
|
|
104
|
+
export declare function normalizeGeolocation(raw: unknown): Geo | null;
|
|
105
|
+
export declare const custodyTools: {
|
|
106
|
+
browser_release(_args: unknown, port: CustodyPort): Promise<ToolOutput>;
|
|
107
|
+
browser_relaunch(args: {
|
|
108
|
+
verified?: boolean;
|
|
109
|
+
geolocation?: unknown;
|
|
110
|
+
}, port: CustodyPort): Promise<ToolOutput>;
|
|
111
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { isLeaseTransition } from "./lease.js";
|
|
2
|
+
/** A Stagehand close after a dropped and reconnected connection can hang forever; the provider ends the session anyway. */
|
|
3
|
+
export const DRIVER_CLOSE_MS = 5_000;
|
|
4
|
+
export async function closeDriver(driver) {
|
|
5
|
+
let timer;
|
|
6
|
+
await Promise.race([driver.close().catch(() => undefined), new Promise((resolve) => { timer = setTimeout(resolve, DRIVER_CLOSE_MS); })]);
|
|
7
|
+
clearTimeout(timer);
|
|
8
|
+
}
|
|
9
|
+
export class BrowserFailureError extends Error {
|
|
10
|
+
failure;
|
|
11
|
+
name = "BrowserFailureError";
|
|
12
|
+
constructor(failure) {
|
|
13
|
+
super(failure.message);
|
|
14
|
+
this.failure = failure;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export const NAVIGATION_CAP = 500;
|
|
18
|
+
export const VIEWPORT = Object.freeze({ width: 1288, height: 711 });
|
|
19
|
+
export const SESSION_REPLACED = {
|
|
20
|
+
ok: false, code: "session_replaced", retryable: true, effect: "none",
|
|
21
|
+
message: "The browser session this call was using ended in an interruption, so its page is gone. Navigate again with run, or take a snapshot, to continue in a fresh session.",
|
|
22
|
+
};
|
|
23
|
+
export const notices = {
|
|
24
|
+
kept: (url) => `Your browser session survived an interruption and is still open${url ? ` on ${url}` : ""}. Take a snapshot before acting again.`,
|
|
25
|
+
lost: (url) => `Your browser session ended during an interruption. The next browser call opens a fresh session with no cookies or page state${url ? `; the last page was ${url}` : ""}.`,
|
|
26
|
+
ended: (url) => `Your previous browser session had ended (the provider closed it), so this call runs in a fresh session with no cookies or page state${url ? `; the last page was ${url}` : ""}.`,
|
|
27
|
+
released: (reason, url, idleS) => `Your previous browser session was released ${reason === "idle" ? `after ${idleS} seconds without a browser call` : "when the host closed it"}, so this call runs in a fresh session that starts on a blank page with no cookies or page state${url ? `; the last page was ${url}, so navigate there again to continue` : ""}.`,
|
|
28
|
+
};
|
|
29
|
+
export function currentRecord(state, plane) {
|
|
30
|
+
const index = state?.current[plane];
|
|
31
|
+
return index === null || index === undefined ? null : state.sessions[index] ?? null;
|
|
32
|
+
}
|
|
33
|
+
export function newSessionRecord(input) {
|
|
34
|
+
const { spec } = input;
|
|
35
|
+
return {
|
|
36
|
+
tag: input.tag, state: "creating", resourceId: null, createdAt: input.at, endedAt: null, releaseReason: null,
|
|
37
|
+
plane: input.plane, provider: input.provider,
|
|
38
|
+
settings: { proxies: spec.proxies !== false, verified: spec.verified, geolocation: typeof spec.proxies === "object" ? spec.proxies.geolocation : null, region: spec.region ?? null, contextId: spec.contextId ?? null },
|
|
39
|
+
navigation: [], lastUrl: null, pendingEffect: null, spent: { seconds: 0, usd: 0, chargedThrough: input.at },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function moveRecord(record, next) {
|
|
43
|
+
if (record.state === next.state && next.resourceId === undefined)
|
|
44
|
+
return;
|
|
45
|
+
if (!isLeaseTransition(record.state, next.state))
|
|
46
|
+
throw new Error(`session ${record.tag}: ${record.state} cannot become ${next.state}`);
|
|
47
|
+
record.state = next.state;
|
|
48
|
+
if (next.resourceId !== undefined)
|
|
49
|
+
record.resourceId = next.resourceId;
|
|
50
|
+
if (next.reason !== undefined)
|
|
51
|
+
record.releaseReason = next.reason;
|
|
52
|
+
if (next.state === "released" || next.state === "lost")
|
|
53
|
+
record.endedAt = next.at;
|
|
54
|
+
}
|
|
55
|
+
export function sessionSpec(config, tag, overrides = {}) {
|
|
56
|
+
const { policy } = config;
|
|
57
|
+
const geolocation = overrides.geolocation === undefined ? policy.geolocation : overrides.geolocation;
|
|
58
|
+
return {
|
|
59
|
+
tag, maxLifetimeS: policy.sessionTimeoutS, idleTimeoutS: policy.idleReleaseS,
|
|
60
|
+
proxies: !policy.proxies ? false : geolocation ? { geolocation } : true,
|
|
61
|
+
verified: overrides.verified ?? policy.verified, captcha: policy.captcha,
|
|
62
|
+
...(policy.region ? { region: policy.region } : {}),
|
|
63
|
+
viewport: { ...VIEWPORT },
|
|
64
|
+
...(policy.contextId ? { contextId: policy.contextId } : {}),
|
|
65
|
+
...(policy.navigation ? { navigation: policy.navigation } : {}),
|
|
66
|
+
metadata: { run: config.run, label: config.label },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** With proxies off a geolocation is refused up front: the session would browse from the provider's default place,
|
|
70
|
+
* silently. */
|
|
71
|
+
export function relaunchRefusal(policy, overrides) {
|
|
72
|
+
if (!overrides.geolocation || policy.proxies)
|
|
73
|
+
return null;
|
|
74
|
+
return { ok: false, code: "refused", retryable: false, effect: "none", message: "browser_relaunch: geolocation needs a proxy, and this run's browser policy has proxies off, so no session can browse from a chosen place. Nothing was relaunched and your session is unchanged; browser_relaunch can still change verified, with geolocation left out." };
|
|
75
|
+
}
|
|
76
|
+
/** An ISO alpha-2 country, a US state abbreviation, a city in capitals; null when the country is not two letters. */
|
|
77
|
+
export function normalizeGeolocation(raw) {
|
|
78
|
+
if (!raw || typeof raw !== "object")
|
|
79
|
+
return null;
|
|
80
|
+
const r = raw;
|
|
81
|
+
const country = String(r.country ?? "").trim().toUpperCase();
|
|
82
|
+
if (!/^[A-Z]{2}$/.test(country))
|
|
83
|
+
return null;
|
|
84
|
+
const out = { country };
|
|
85
|
+
const state = String(r.state ?? "").trim().toUpperCase();
|
|
86
|
+
if (/^[A-Z]{2}$/.test(state))
|
|
87
|
+
out.state = state;
|
|
88
|
+
const city = String(r.city ?? "").trim().toUpperCase().replace(/[\s-]+/g, "_").replace(/[^A-Z_]/g, "");
|
|
89
|
+
if (city)
|
|
90
|
+
out.city = city;
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
const text = (value, isError = false) => ({ content: [{ type: "text", text: JSON.stringify(value) }], ...(isError ? { isError } : {}) });
|
|
94
|
+
const failed = (error) => {
|
|
95
|
+
if (error instanceof BrowserFailureError)
|
|
96
|
+
return text(error.failure, true);
|
|
97
|
+
throw error;
|
|
98
|
+
};
|
|
99
|
+
export const custodyTools = {
|
|
100
|
+
async browser_release(_args, port) {
|
|
101
|
+
try {
|
|
102
|
+
const { released, sessionId } = await port.release();
|
|
103
|
+
return text({ ok: true, released, session_id: sessionId, ...(sessionId === null ? { note: "no open browser session" } : {}) });
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
return failed(error);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
async browser_relaunch(args, port) {
|
|
110
|
+
const geolocation = args.geolocation === undefined ? undefined : normalizeGeolocation(args.geolocation);
|
|
111
|
+
if (geolocation === null)
|
|
112
|
+
return text({ ok: false, code: "refused", retryable: false, effect: "none", message: 'browser_relaunch: geolocation needs a two-letter ISO country code (e.g. {country: "GB"}).' }, true);
|
|
113
|
+
try {
|
|
114
|
+
// A relaunch that names no `verified` keeps the run's policy: an omitted flag never downgrades a Verified session.
|
|
115
|
+
const { sessionId, record } = await port.relaunch({ ...(args.verified !== undefined ? { verified: args.verified === true } : {}), ...(geolocation ? { geolocation } : {}) });
|
|
116
|
+
return text({ ok: true, session: sessionId, verified: record.settings.verified, proxies: record.settings.proxies, geolocation: record.settings.geolocation });
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
return failed(error);
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export declare const WALL_CLASSES: readonly ["captcha_or_bot_check", "login_wall", "paywall", "region_or_geo_block", "consent_interstitial", "error_or_not_found", "loading_or_js_required", "empty_or_template"];
|
|
2
|
+
export type WallClass = (typeof WALL_CLASSES)[number];
|
|
3
|
+
export type PageForJudgment = {
|
|
4
|
+
url: string;
|
|
5
|
+
status: number | null;
|
|
6
|
+
contentType: string | null;
|
|
7
|
+
text: string;
|
|
8
|
+
};
|
|
9
|
+
export type PageVerdict = {
|
|
10
|
+
/** "content" files the page `ok`; a wall class files it `failed` with a typed way out. */
|
|
11
|
+
wall: "content" | WallClass | "unjudged";
|
|
12
|
+
/** Probability the page text carries instructions addressed to an AI reader; null when not asked. */
|
|
13
|
+
injection: number | null;
|
|
14
|
+
confidence: number | null;
|
|
15
|
+
/** One line filed verbatim as the receipt's `page_guard` fact, so the decision re-derives with no model call. */
|
|
16
|
+
guard?: string;
|
|
17
|
+
};
|
|
18
|
+
/** A non-GET request a `run` would send. Never a body, a header value or a field value. */
|
|
19
|
+
export type EffectRequest = {
|
|
20
|
+
method: string;
|
|
21
|
+
origin: string;
|
|
22
|
+
path: string;
|
|
23
|
+
queryKeys: string[];
|
|
24
|
+
formFields: string[];
|
|
25
|
+
pageTitle: string | null;
|
|
26
|
+
trigger: string | null;
|
|
27
|
+
task: string | null;
|
|
28
|
+
};
|
|
29
|
+
/** Probabilities, each in [0, 1]. */
|
|
30
|
+
export type EffectVerdict = {
|
|
31
|
+
irreversible: number;
|
|
32
|
+
telemetry: number;
|
|
33
|
+
queryOnly: number;
|
|
34
|
+
};
|
|
35
|
+
export type Decisions = {
|
|
36
|
+
classifyPage?(page: PageForJudgment): Promise<PageVerdict>;
|
|
37
|
+
judgeEffect?(request: EffectRequest): Promise<EffectVerdict | null>;
|
|
38
|
+
/** Under the `ask` policy: approve a held request. Absent, unanswered within `batchTimeoutMs` or cut, it is refused. */
|
|
39
|
+
approveEffect?(request: EffectRequest, verdict: EffectVerdict | null): Promise<boolean>;
|
|
40
|
+
/** One finite relevance per chunk, in order; ties go earlier. The scale must be absolute: a large text is ranked in
|
|
41
|
+
* several calls whose scores are compared. Null, a throw or a wrong count returns the text whole. */
|
|
42
|
+
rankChunks?(find: string, chunks: string[]): Promise<number[] | null>;
|
|
43
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { BrowserProvider } from "./host.js";
|
|
2
|
+
import type { LeaseRef } from "./lease.js";
|
|
3
|
+
export declare const DOWNLOADS_DIR = "downloads";
|
|
4
|
+
export declare const DOWNLOADS_MANIFEST = "downloads/manifest.json";
|
|
5
|
+
export declare const MAX_DOWNLOAD_BYTES: number;
|
|
6
|
+
export declare const MAX_DOWNLOAD_FILES = 50;
|
|
7
|
+
/** The handle `browser_release` returns for a session's recording; the host's `read` door resolves it. */
|
|
8
|
+
export declare const recordingHandle: (sessionId: string) => string;
|
|
9
|
+
export type DownloadEntry = {
|
|
10
|
+
name: string;
|
|
11
|
+
path: string;
|
|
12
|
+
sizeBytes: number;
|
|
13
|
+
sha256: string;
|
|
14
|
+
};
|
|
15
|
+
export type DownloadsResult = {
|
|
16
|
+
fetched: DownloadEntry[];
|
|
17
|
+
already_fetched: DownloadEntry[];
|
|
18
|
+
skipped: Array<{
|
|
19
|
+
name: string;
|
|
20
|
+
reason: string;
|
|
21
|
+
}>;
|
|
22
|
+
manifest: string;
|
|
23
|
+
};
|
|
24
|
+
/** A name that stays inside `downloads/`: the last path segment, no control characters, not hidden, not the manifest. */
|
|
25
|
+
export declare function safeDownloadName(remote: string): string;
|
|
26
|
+
/** Skips what the workspace already holds; one failing file is skipped with its reason. */
|
|
27
|
+
export declare function fetchDownloads(options: {
|
|
28
|
+
provider: Pick<BrowserProvider, "downloads">;
|
|
29
|
+
ref: LeaseRef;
|
|
30
|
+
session: string;
|
|
31
|
+
workspace: string;
|
|
32
|
+
maxBytes?: number;
|
|
33
|
+
maxFiles?: number;
|
|
34
|
+
now?: () => Date;
|
|
35
|
+
}): Promise<DownloadsResult>;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { lstat, mkdir, open, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const DOWNLOADS_DIR = "downloads";
|
|
5
|
+
export const DOWNLOADS_MANIFEST = "downloads/manifest.json";
|
|
6
|
+
/** Beside `downloads/`, not in it, so no downloaded file can take its name; temporary names start with a dot, which no
|
|
7
|
+
* download name does. */
|
|
8
|
+
const LOCK_FILE = ".downloads.lock";
|
|
9
|
+
export const MAX_DOWNLOAD_BYTES = 100 * 1024 * 1024;
|
|
10
|
+
export const MAX_DOWNLOAD_FILES = 50;
|
|
11
|
+
/** The handle `browser_release` returns for a session's recording; the host's `read` door resolves it. */
|
|
12
|
+
export const recordingHandle = (sessionId) => `session:${sessionId}`;
|
|
13
|
+
/** A name that stays inside `downloads/`: the last path segment, no control characters, not hidden, not the manifest. */
|
|
14
|
+
export function safeDownloadName(remote) {
|
|
15
|
+
const base = (remote.split(/[\\/]/).pop() ?? "").replace(/[\u0000-\u001f\u007f]/g, "").replace(/^\.+/, "").trim().slice(0, 200);
|
|
16
|
+
return !base || base === "manifest.json" ? "download" : base;
|
|
17
|
+
}
|
|
18
|
+
const sameRemote = (row, session, file) => row.session === session && row.remote.name === file.name && row.remote.sizeBytes === file.sizeBytes && row.remote.modifiedAt === file.modifiedAt;
|
|
19
|
+
const exists = (file) => stat(file).then((s) => s.isFile(), () => false);
|
|
20
|
+
const hashOf = async (file) => createHash("sha256").update(await readFile(file)).digest("hex");
|
|
21
|
+
const withRow = (rows, row) => [...rows.filter((r) => r.path !== row.path || (r.sha256 === row.sha256 && !(r.session === row.session && r.remote.name === row.remote.name))), row];
|
|
22
|
+
async function withLock(file, run) {
|
|
23
|
+
for (const deadline = Date.now() + 10_000;; await new Promise((r) => setTimeout(r, 25 + Math.random() * 50))) {
|
|
24
|
+
try {
|
|
25
|
+
await (await open(file, "wx")).close();
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error.code !== "EEXIST")
|
|
30
|
+
throw error;
|
|
31
|
+
if (Date.now() - (await stat(file).then((s) => s.mtimeMs, () => Date.now())) > 30_000)
|
|
32
|
+
await rm(file, { force: true });
|
|
33
|
+
else if (Date.now() > deadline)
|
|
34
|
+
throw new Error("downloads/manifest.json is locked by another fetch");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
return await run();
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
await rm(file, { force: true });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async function readManifest(file) {
|
|
45
|
+
try {
|
|
46
|
+
const parsed = JSON.parse(await readFile(file, "utf8"));
|
|
47
|
+
if (parsed?.schema === "pi-browser.downloads.v1" && Array.isArray(parsed.files))
|
|
48
|
+
return parsed;
|
|
49
|
+
}
|
|
50
|
+
catch { }
|
|
51
|
+
return { schema: "pi-browser.downloads.v1", files: [] };
|
|
52
|
+
}
|
|
53
|
+
/** Skips what the workspace already holds; one failing file is skipped with its reason. */
|
|
54
|
+
export async function fetchDownloads(options) {
|
|
55
|
+
const { provider, ref, session, workspace } = options;
|
|
56
|
+
if (!provider.downloads)
|
|
57
|
+
throw new Error("this provider cannot list downloads");
|
|
58
|
+
const maxBytes = options.maxBytes ?? MAX_DOWNLOAD_BYTES;
|
|
59
|
+
const dir = path.join(workspace, DOWNLOADS_DIR);
|
|
60
|
+
await mkdir(dir, { recursive: true });
|
|
61
|
+
if (!(await lstat(dir)).isDirectory())
|
|
62
|
+
throw new Error("downloads/ in the workspace is not a directory");
|
|
63
|
+
const manifestPath = path.join(workspace, DOWNLOADS_MANIFEST);
|
|
64
|
+
const manifest = await readManifest(manifestPath);
|
|
65
|
+
const result = { fetched: [], already_fetched: [], skipped: [], manifest: DOWNLOADS_MANIFEST };
|
|
66
|
+
const entry = (row) => ({ name: row.name, path: row.path, sizeBytes: row.sizeBytes, sha256: row.sha256 });
|
|
67
|
+
const remote = await provider.downloads.list(ref);
|
|
68
|
+
let attempted = 0;
|
|
69
|
+
for (const file of remote) {
|
|
70
|
+
const known = manifest.files.find((row) => sameRemote(row, session, file));
|
|
71
|
+
if (known && (await exists(path.join(workspace, known.path)))) {
|
|
72
|
+
result.already_fetched.push(entry(known));
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
// Counts attempts, not places in the provider's list, so files fetched before never starve the ones behind them.
|
|
76
|
+
if (attempted >= (options.maxFiles ?? MAX_DOWNLOAD_FILES)) {
|
|
77
|
+
result.skipped.push({ name: file.name, reason: "over the per-call file limit; call again" });
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
attempted += 1;
|
|
81
|
+
if (file.sizeBytes > maxBytes) {
|
|
82
|
+
result.skipped.push({ name: file.name, reason: `larger than the ${maxBytes}-byte limit` });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const part = path.join(dir, `.part-${randomBytes(6).toString("hex")}`);
|
|
86
|
+
try {
|
|
87
|
+
const hash = createHash("sha256");
|
|
88
|
+
let size = 0;
|
|
89
|
+
const out = await open(part, "wx");
|
|
90
|
+
try {
|
|
91
|
+
for await (const chunk of await provider.downloads.read(ref, file.name, maxBytes)) {
|
|
92
|
+
size += chunk.byteLength;
|
|
93
|
+
if (size > maxBytes)
|
|
94
|
+
throw new Error(`larger than the ${maxBytes}-byte limit`);
|
|
95
|
+
hash.update(chunk);
|
|
96
|
+
await out.write(chunk);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
await out.close();
|
|
101
|
+
}
|
|
102
|
+
const sha256 = hash.digest("hex");
|
|
103
|
+
// Naming, placing and recording are one step under the lock, so two writers never both find a name free; a name
|
|
104
|
+
// taken by other bytes gains this file's digest, so no rename replaces other contents.
|
|
105
|
+
const wanted = safeDownloadName(file.name);
|
|
106
|
+
const ext = path.extname(wanted);
|
|
107
|
+
const row = await withLock(path.join(workspace, LOCK_FILE), async () => {
|
|
108
|
+
let final = path.join(dir, wanted);
|
|
109
|
+
for (const digits of [8, 16, 32, 64]) {
|
|
110
|
+
if (!(await exists(final)) || (await hashOf(final)) === sha256)
|
|
111
|
+
break;
|
|
112
|
+
final = path.join(dir, `${path.basename(wanted, ext)}-${sha256.slice(0, digits)}${ext}`);
|
|
113
|
+
}
|
|
114
|
+
await rename(part, final);
|
|
115
|
+
const placed = { name: path.basename(final), path: path.posix.join(DOWNLOADS_DIR, path.basename(final)), sizeBytes: size, sha256, remote: { name: file.name, sizeBytes: file.sizeBytes, modifiedAt: file.modifiedAt }, session, fetchedAt: (options.now?.() ?? new Date()).toISOString() };
|
|
116
|
+
const latest = withRow((await readManifest(manifestPath)).files, placed);
|
|
117
|
+
const tmp = path.join(workspace, `.downloads-manifest-${randomBytes(4).toString("hex")}.tmp`);
|
|
118
|
+
await writeFile(tmp, `${JSON.stringify({ schema: "pi-browser.downloads.v1", files: latest }, null, 2)}\n`);
|
|
119
|
+
await rename(tmp, manifestPath);
|
|
120
|
+
return placed;
|
|
121
|
+
});
|
|
122
|
+
manifest.files = withRow(manifest.files, row);
|
|
123
|
+
result.fetched.push(entry(row));
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
await rm(part, { force: true });
|
|
127
|
+
result.skipped.push({ name: file.name, reason: error instanceof Error ? error.message.slice(0, 200) : "the read failed" });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|