@parcha/pi-browser 0.1.0-beta.34 → 0.1.0-beta.340

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 (78) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/NOTICE +6 -1
  3. package/README.md +70 -60
  4. package/dist/core/contract.d.ts +9 -6
  5. package/dist/core/contract.js +12 -30
  6. package/dist/core/custody.d.ts +6 -0
  7. package/dist/core/custody.js +15 -3
  8. package/dist/core/downloads.d.ts +37 -3
  9. package/dist/core/downloads.js +81 -57
  10. package/dist/core/driving-guard.d.ts +1 -1
  11. package/dist/core/driving-guard.js +1 -1
  12. package/dist/core/evidence.d.ts +1 -1
  13. package/dist/core/files.d.ts +20 -2
  14. package/dist/core/files.js +161 -10
  15. package/dist/core/host.d.ts +18 -75
  16. package/dist/core/images.d.ts +32 -4
  17. package/dist/core/images.js +81 -16
  18. package/dist/core/lease.d.ts +2 -1
  19. package/dist/core/page-scan.d.ts +81 -2
  20. package/dist/core/page-scan.js +487 -13
  21. package/dist/core/public-fetch.d.ts +70 -9
  22. package/dist/core/public-fetch.js +348 -51
  23. package/dist/core/read-page-worker.js +5 -4
  24. package/dist/core/read-page.d.ts +23 -8
  25. package/dist/core/read-page.js +64 -20
  26. package/dist/core/redact.d.ts +0 -2
  27. package/dist/core/redact.js +0 -32
  28. package/dist/core/session-control.d.ts +0 -2
  29. package/dist/core/session-control.js +0 -5
  30. package/dist/core/tools.d.ts +23 -1
  31. package/dist/core/tools.js +46 -4
  32. package/dist/core/usage.d.ts +4 -3
  33. package/dist/core/usage.js +5 -4
  34. package/dist/core/vault.d.ts +0 -10
  35. package/dist/core/vault.js +0 -13
  36. package/dist/core/web.d.ts +2 -111
  37. package/dist/core/web.js +0 -443
  38. package/dist/driver/cdp.js +3 -3
  39. package/dist/driver/page-driver.js +7 -3
  40. package/dist/driver/stagehand.d.ts +3 -1
  41. package/dist/driver/stagehand.js +4 -2
  42. package/dist/durable/store.d.ts +14 -3
  43. package/dist/durable/store.js +204 -51
  44. package/dist/durable.d.ts +32 -16
  45. package/dist/durable.js +68 -95
  46. package/dist/index.d.ts +3 -3
  47. package/dist/index.js +5 -3
  48. package/dist/live-view/index.d.ts +3 -1
  49. package/dist/live-view/index.js +4 -0
  50. package/dist/live-view/protocol.d.ts +1 -1
  51. package/dist/live-view/server.d.ts +30 -0
  52. package/dist/live-view/server.js +152 -25
  53. package/dist/providers/cdp.js +2 -2
  54. package/dist/providers/profiles.d.ts +1 -15
  55. package/dist/providers/profiles.js +1 -64
  56. package/dist/providers/shared.d.ts +1 -47
  57. package/dist/providers/shared.js +1 -82
  58. package/dist/providers/worker-extension.d.ts +14 -1
  59. package/dist/providers/worker-extension.js +69 -3
  60. package/dist/testing/backend.d.ts +6 -24
  61. package/dist/testing/backend.js +7 -30
  62. package/dist/vault-session.d.ts +4 -7
  63. package/dist/vault-session.js +26 -81
  64. package/dist/vendor/browser-use-pi/page.d.ts +3 -12
  65. package/dist/vendor/browser-use-pi/page.js +6 -19
  66. package/package.json +9 -22
  67. package/dist/core/sites.d.ts +0 -33
  68. package/dist/core/sites.js +0 -104
  69. package/dist/durable/web.d.ts +0 -47
  70. package/dist/durable/web.js +0 -41
  71. package/dist/providers/browserbase-net.d.ts +0 -12
  72. package/dist/providers/browserbase-net.js +0 -95
  73. package/dist/providers/browserbase.d.ts +0 -169
  74. package/dist/providers/browserbase.js +0 -319
  75. package/dist/providers/kernel.d.ts +0 -58
  76. package/dist/providers/kernel.js +0 -291
  77. /package/dist/{providers → core}/cdp-call.d.ts +0 -0
  78. /package/dist/{providers → core}/cdp-call.js +0 -0
@@ -1,47 +0,0 @@
1
- import { defineExtension, type ConversationId, type ToolRegistration } from "@earendil-works/pi-durable";
2
- import type { Decisions } from "../core/decisions.js";
3
- import type { EvidenceSink } from "../core/evidence.js";
4
- import type { FetchRequest, FetchResult, PriceTable, SearchRequest, SearchResult } from "../core/host.js";
5
- import { type FetchSource, type RenderedPage, type SearchSource, type WebBackup, type WebToolsOptions } from "../core/web.js";
6
- export type WebExtensionOptions = {
7
- providers: {
8
- /** Names the source in `via` (`<name>_fetch`, `<name>_search`); default "provider". */
9
- name?: string;
10
- fetch?: (request: FetchRequest, signal?: AbortSignal) => Promise<FetchResult>;
11
- search?: (request: SearchRequest, signal?: AbortSignal) => Promise<SearchResult>;
12
- };
13
- /** Searches tried before `providers.search`, in order (see `SearchSource`). */
14
- searchSources?: readonly SearchSource[];
15
- /** Readers tried before `providers.fetch` for a markdown read, in order (see `FetchSource`). */
16
- fetchSources?: readonly FetchSource[];
17
- evidence: EvidenceSink;
18
- /** Per use: evidence label and proxies. Default: label "web", proxies on. */
19
- scope?: (conversationId: ConversationId) => {
20
- label: string;
21
- proxies: boolean;
22
- } | Promise<{
23
- label: string;
24
- proxies: boolean;
25
- }>;
26
- prices?: Partial<PriceTable>;
27
- redact?: {
28
- values: () => readonly string[];
29
- };
30
- /** The host's fallback fetchers, tried in order when the provider cannot fetch a page. */
31
- backups?: ReadonlyArray<WebBackup>;
32
- decisions?: Pick<Decisions, "classifyPage">;
33
- render?: {
34
- enabled: boolean;
35
- read(url: string, signal?: AbortSignal): Promise<RenderedPage>;
36
- };
37
- pdf?: WebToolsOptions["pdf"];
38
- /** How long the screening of a search answer's snippets may take in all (see `WebToolsOptions`). */
39
- snippetScreenMs?: number;
40
- section?: {
41
- addendum?: string;
42
- };
43
- };
44
- export declare function createWebExtension(options: WebExtensionOptions): {
45
- extension: ReturnType<typeof defineExtension>;
46
- tools: readonly ToolRegistration[];
47
- };
@@ -1,41 +0,0 @@
1
- import { defineExtension, defineTool } from "@earendil-works/pi-durable";
2
- import { WEB_TOOLS } from "../core/contract.js";
3
- import { providerKeyValues } from "../core/session-control.js";
4
- import { toolUsage } from "../core/usage.js";
5
- import { createWebTools } from "../core/web.js";
6
- import { forModel } from "./result.js";
7
- export function createWebExtension(options) {
8
- const tools = createWebTools({
9
- name: options.providers.name ?? "provider",
10
- fetch: options.providers.fetch,
11
- search: options.providers.search,
12
- searchSources: options.searchSources,
13
- fetchSources: options.fetchSources,
14
- evidence: options.evidence,
15
- scope: options.scope ?? (() => ({ label: "web", proxies: true })),
16
- prices: options.prices,
17
- redact: { values: () => providerKeyValues(process.env, options.redact?.values) },
18
- backups: options.backups,
19
- classifyPage: options.decisions?.classifyPage,
20
- render: options.render,
21
- pdf: options.pdf,
22
- snippetScreenMs: options.snippetScreenMs,
23
- });
24
- const run = { web_fetch: tools.fetch, web_search: tools.search };
25
- const result = ({ text, isError, details, usd }) => ({ ...forModel([{ type: "text", text }]), isError, details: details, usage: toolUsage(usd).usage });
26
- const registrations = WEB_TOOLS
27
- .filter((tool) => (tool.listedWhen === "fetch" ? options.providers.fetch || options.render?.enabled || options.backups?.length || options.fetchSources?.length : options.providers.search || options.searchSources?.length))
28
- .map((tool) => defineTool({
29
- name: tool.name,
30
- description: tool.description,
31
- parameters: tool.parameters,
32
- replay: tool.replay,
33
- executionMode: tool.executionMode,
34
- execute: async (args, api, context) => result(await run[tool.name](api.conversationId, args, context.abortSignal)),
35
- }));
36
- // Matched by name: another extension's tool registered under the same name would carry the text too.
37
- const names = new Set(registrations.map((tool) => tool.name));
38
- const addendum = options.section?.addendum;
39
- const sections = addendum ? [{ key: "web", render: async (input) => (input.agent.tools.some((tool) => names.has(tool.name)) ? addendum : undefined) }] : [];
40
- return { extension: defineExtension({ name: "web", tools: registrations, sections }), tools: registrations };
41
- }
@@ -1,12 +0,0 @@
1
- /** Rewrite a Browserbase connect URL onto the credential proxy: only `*.browserbase.com` hosts, the signing query kept. */
2
- export declare function rewriteConnectUrl(connectUrl: string, connectBaseUrl: string | undefined | null): string;
3
- /** Direct, never through the HTTP(S) env proxy: it rejects CONNECT and the CDP WebSocket cannot ride it. Under `node
4
- * --use-env-proxy` NO_PROXY is re-read per request, so listing these hosts bypasses it. */
5
- export declare function browserbaseNoProxyHosts(env: NodeJS.ProcessEnv): string[];
6
- export declare function applyBrowserbaseNoProxy(env?: NodeJS.ProcessEnv): string[];
7
- /** A second provider binding the same URL to a different proxy is refused, never switched: the signed URL would go to
8
- * it. */
9
- export declare function bindConnectDial(connectUrl: string, connectBaseUrl: string | null): void;
10
- export declare function unbindConnectDial(connectUrl: string): void;
11
- export declare function connectDialUrl(url: string): string;
12
- export declare function installWebSocketWrapper(): void;
@@ -1,95 +0,0 @@
1
- import { browserbaseHoldings } from "./shared.js";
2
- /** Rewrite a Browserbase connect URL onto the credential proxy: only `*.browserbase.com` hosts, the signing query kept. */
3
- export function rewriteConnectUrl(connectUrl, connectBaseUrl) {
4
- if (!connectUrl || !connectBaseUrl)
5
- return connectUrl;
6
- try {
7
- const target = new URL(connectBaseUrl);
8
- const u = new URL(connectUrl);
9
- if (!/(?:^|\.)browserbase\.com$/i.test(u.hostname))
10
- return connectUrl;
11
- u.protocol = target.protocol;
12
- u.host = target.host;
13
- u.pathname = target.pathname.replace(/\/$/, "") + u.pathname;
14
- return u.toString();
15
- }
16
- catch {
17
- return connectUrl;
18
- }
19
- }
20
- /** Direct, never through the HTTP(S) env proxy: it rejects CONNECT and the CDP WebSocket cannot ride it. Under `node
21
- * --use-env-proxy` NO_PROXY is re-read per request, so listing these hosts bypasses it. */
22
- export function browserbaseNoProxyHosts(env) {
23
- const hosts = new Set();
24
- for (const key of ["BROWSERBASE_BASE_URL", "BROWSERBASE_CONNECT_BASE_URL"]) {
25
- const raw = String(env[key] || "").trim();
26
- if (!raw)
27
- continue;
28
- try {
29
- hosts.add(new URL(raw).hostname);
30
- }
31
- catch { }
32
- }
33
- if (!String(env.BROWSERBASE_CONNECT_BASE_URL || "").trim())
34
- hosts.add(".browserbase.com");
35
- return [...hosts];
36
- }
37
- export function applyBrowserbaseNoProxy(env = process.env) {
38
- const hosts = browserbaseNoProxyHosts(env);
39
- if (!hosts.length)
40
- return hosts;
41
- for (const target of env === process.env ? [env] : [env, process.env]) {
42
- for (const key of ["NO_PROXY", "no_proxy"]) {
43
- const merged = String(target[key] || "").split(",").map((h) => h.trim()).filter(Boolean);
44
- for (const h of hosts)
45
- if (!merged.includes(h))
46
- merged.push(h);
47
- target[key] = merged.join(",");
48
- }
49
- }
50
- return hosts;
51
- }
52
- /** The driver keeps the raw connect URL (the extension in the cloud browser dials it back and reaches only
53
- * Browserbase); this process's socket dials the proxy, so the rewrite happens at dial time, never in a record. */
54
- /** Per-socket targets, so two providers with different proxies never send one's signed URL to the other's proxy. */
55
- const { boundDials } = browserbaseHoldings;
56
- /** A second provider binding the same URL to a different proxy is refused, never switched: the signed URL would go to
57
- * it. */
58
- export function bindConnectDial(connectUrl, connectBaseUrl) {
59
- const target = connectBaseUrl?.trim() ?? "";
60
- const bound = boundDials.get(connectUrl);
61
- if (bound !== undefined && bound !== target)
62
- throw new Error("this Browserbase session is already attached through another credential proxy");
63
- boundDials.set(connectUrl, target);
64
- }
65
- export function unbindConnectDial(connectUrl) {
66
- boundDials.delete(connectUrl);
67
- if (![...boundDials.values()].some(Boolean))
68
- restoreWebSocket();
69
- }
70
- export function connectDialUrl(url) {
71
- const bound = boundDials.get(url);
72
- if (bound !== undefined)
73
- return bound ? rewriteConnectUrl(url, bound) : url;
74
- return url;
75
- }
76
- /** Wraps the process WebSocket only while a session is bound to a proxy; the native one returns with the last binding. A wrapper
77
- * installed on top cannot be removed, so ours stays under it and, with no bindings, passes every URL through. */
78
- let wrapped = null;
79
- export function installWebSocketWrapper() {
80
- if (typeof globalThis.WebSocket !== "function" || (wrapped && globalThis.WebSocket === wrapped.wrapper))
81
- return;
82
- const original = globalThis.WebSocket;
83
- const wrapper = function (url, protocols) { return new original(connectDialUrl(String(url)), protocols); };
84
- wrapper.prototype = original.prototype;
85
- for (const k of ["CONNECTING", "OPEN", "CLOSING", "CLOSED"])
86
- wrapper[k] = original[k];
87
- globalThis.WebSocket = wrapper;
88
- wrapped = { original, wrapper };
89
- }
90
- function restoreWebSocket() {
91
- if (!wrapped || globalThis.WebSocket !== wrapped.wrapper)
92
- return;
93
- globalThis.WebSocket = wrapped.original;
94
- wrapped = null;
95
- }
@@ -1,169 +0,0 @@
1
- import type { BrowserProvider, Geo, PriceTable, SessionSpec } from "../core/host.js";
2
- import { type CdpSession } from "./cdp-call.js";
3
- import { type ProfileStore } from "./profiles.js";
4
- export { rewriteConnectUrl, browserbaseNoProxyHosts, applyBrowserbaseNoProxy } from "./browserbase-net.js";
5
- export { fileProfileStore, memoryProfileStore, type ProfileStore } from "./profiles.js";
6
- export declare const SEARCH_QUERY_MAX = 200;
7
- export declare const SEARCH_RESULTS_MAX = 25;
8
- export type BrowserbaseSdk = {
9
- sessions: {
10
- create(body: BrowserbaseSessionBody, options?: RequestOptions): Promise<{
11
- id: string;
12
- connectUrl: string;
13
- }>;
14
- list(query: {
15
- q?: string;
16
- status?: string;
17
- }, options?: RequestOptions): Promise<Array<{
18
- id: string;
19
- status?: string;
20
- userMetadata?: Record<string, unknown>;
21
- }>>;
22
- retrieve(id: string, options?: RequestOptions): Promise<{
23
- id: string;
24
- status?: string;
25
- connectUrl?: string;
26
- }>;
27
- update(id: string, body: {
28
- status: "REQUEST_RELEASE";
29
- }, options?: RequestOptions): Promise<unknown>;
30
- debug(id: string, options?: RequestOptions): Promise<{
31
- debuggerFullscreenUrl?: string;
32
- debuggerUrl?: string;
33
- pages?: Array<{
34
- id?: string;
35
- url?: string;
36
- title?: string;
37
- debuggerFullscreenUrl?: string;
38
- }>;
39
- }>;
40
- recording?: {
41
- downloads: {
42
- create(id: string): Promise<{
43
- downloads?: RecordingDownload[];
44
- }>;
45
- list(id: string): Promise<{
46
- downloads?: RecordingDownload[];
47
- }>;
48
- };
49
- };
50
- };
51
- extensions: {
52
- create(body: {
53
- file: unknown;
54
- }, options?: RequestOptions): Promise<{
55
- id: string;
56
- }>;
57
- };
58
- contexts: {
59
- create(body: {
60
- projectId?: string;
61
- }, options?: RequestOptions): Promise<{
62
- id: string;
63
- }>;
64
- };
65
- fetchAPI: {
66
- create(body: {
67
- url: string;
68
- allowRedirects?: boolean;
69
- proxies?: boolean;
70
- format?: string;
71
- }, options?: RequestOptions): Promise<FetchApiResult>;
72
- };
73
- search: {
74
- web(body: {
75
- query: string;
76
- numResults?: number;
77
- }, options?: RequestOptions): Promise<SearchApiResult>;
78
- };
79
- get<T = unknown>(path: string, options?: RequestOptions & {
80
- query?: Record<string, unknown>;
81
- headers?: Record<string, string>;
82
- __binaryResponse?: boolean;
83
- }): Promise<T>;
84
- };
85
- type FetchApiResult = {
86
- statusCode?: unknown;
87
- contentType?: unknown;
88
- content?: unknown;
89
- } | null | undefined;
90
- type SearchRow = {
91
- url?: unknown;
92
- title?: string | null;
93
- author?: string | null;
94
- publishedDate?: string | null;
95
- published?: string | null;
96
- };
97
- type SearchApiResult = {
98
- results?: SearchRow[];
99
- } | null | undefined;
100
- type BrowserbaseSessionBody = {
101
- proxies: boolean | Array<Record<string, unknown>>;
102
- timeout: number;
103
- keepAlive: boolean;
104
- browserSettings: {
105
- solveCaptchas: boolean;
106
- viewport: SessionSpec["viewport"];
107
- verified?: boolean;
108
- context?: {
109
- id: string;
110
- persist: boolean;
111
- };
112
- allowedDomains?: string[];
113
- };
114
- userMetadata: Record<string, string>;
115
- region?: string;
116
- extensionId?: string;
117
- projectId?: string;
118
- };
119
- type RequestOptions = {
120
- maxRetries?: number;
121
- timeout?: number;
122
- signal?: AbortSignal;
123
- };
124
- type RecordingDownload = {
125
- pageId?: string;
126
- status?: string;
127
- downloadUrl?: string;
128
- };
129
- export type BrowserbaseOptions = {
130
- /** Reads BROWSERBASE_API_KEY (or BB_API_KEY), _BASE_URL, _CONNECT_BASE_URL, _PROJECT_ID. Default: process.env. */
131
- env?: NodeJS.ProcessEnv;
132
- client?: BrowserbaseSdk;
133
- /** The Stagehand extension's Browserbase id. Default: uploaded once per process. */
134
- extensionId?: () => Promise<string>;
135
- prices?: Partial<PriceTable>;
136
- downloadBehavior?: (dialUrl: string) => Promise<DownloadSetting | void>;
137
- confirm?: {
138
- polls: number;
139
- intervalMs: number;
140
- };
141
- sleep?: (ms: number) => Promise<void>;
142
- /** Where profile names find their Browserbase contexts. Default: a file under the user's config directory. */
143
- profiles?: ProfileStore;
144
- /** How long a release that saved a profile waits for Browserbase to write the context back. */
145
- profileSettleMs?: number;
146
- };
147
- /** `Browser.setDownloadBehavior` lasts only as long as the socket that sent it, so the socket stays open until
148
- * `close()`. */
149
- export type DownloadSetting = Pick<CdpSession, "close" | "open">;
150
- /** Into `downloadPath` (default `downloads`, Browserbase's synced-downloads rule); the socket stays open. */
151
- export declare const setDownloadBehavior: (url: string, options?: {
152
- downloadPath?: string;
153
- timeoutMs?: number;
154
- }) => Promise<DownloadSetting>;
155
- export type BrowserbaseProvider = BrowserProvider & {
156
- readonly name: "browserbase";
157
- };
158
- /** ISO alpha-2 country, US state abbreviation, upper-case city; no two-letter country, no geolocation. */
159
- export declare function browserbaseGeolocation(raw: Partial<Geo> | null | undefined): Geo | null;
160
- export declare function proxiesField(proxies: SessionSpec["proxies"]): boolean | Array<Record<string, unknown>>;
161
- export declare function sessionCreateBody(spec: SessionSpec, extra?: {
162
- extensionId?: string;
163
- projectId?: string;
164
- context?: {
165
- id: string;
166
- persist: boolean;
167
- };
168
- }): BrowserbaseSessionBody;
169
- export declare function browserbaseProvider(options?: BrowserbaseOptions): BrowserbaseProvider;