@deepraxis/sdk 0.1.1-dev.1b3a943 → 0.1.1-dev.1b9ba47
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/METHODS.md +237 -20
- package/dist/core/binary-operations.d.ts +3 -0
- package/dist/core/binary-operations.js +14 -0
- package/dist/core/credentials.d.ts +16 -6
- package/dist/core/credentials.js +24 -5
- package/dist/core/transport.js +8 -4
- package/dist/generated/binary-operations.gen.d.ts +3 -0
- package/dist/generated/binary-operations.gen.js +14 -0
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +1 -1
- package/dist/generated/sdk.gen.d.ts +443 -65
- package/dist/generated/sdk.gen.js +1432 -377
- package/dist/generated/types.gen.d.ts +34899 -24816
- package/dist/helpers/application-publish.js +16 -2
- package/dist/helpers/browser-connect-error.d.ts +28 -0
- package/dist/helpers/browser-connect-error.js +74 -0
- package/dist/helpers/browser-connect.d.ts +2 -0
- package/dist/helpers/browser-connect.js +31 -38
- package/dist/operations.generated.js +2724 -760
- package/dist/resources.generated.d.ts +1844 -307
- package/dist/resources.generated.js +149 -12
- package/package.json +1 -1
|
@@ -140,14 +140,28 @@ export async function publish(client, input) {
|
|
|
140
140
|
// The service contract accepts jwt instead of manifest. Its generated OpenAPI
|
|
141
141
|
// type currently omits jwt; keep this explicit shape until that schema catches up.
|
|
142
142
|
const versionAssets = { jwt, ...(input.assets_config ? { config: input.assets_config } : {}) };
|
|
143
|
-
|
|
143
|
+
// #3232: a large build takes minutes (one serial object write per module, ~1 s each), so the caller may be cut
|
|
144
|
+
// off by a codemode runtime limit or a gateway before the answer arrives while the service keeps writing. The key
|
|
145
|
+
// is chosen here, before the request: the same publish that is retried answers the version it already created
|
|
146
|
+
// instead of writing a second one, and a create still in progress answers 409 IDEMPOTENCY_IN_FLIGHT.
|
|
147
|
+
// A fresh key per publish() call is deliberate — publishing the same build twice on purpose is two versions.
|
|
148
|
+
// It is reused by the transport's own retries, which replay the same request (headers included).
|
|
149
|
+
const idempotencyKey = `publish-${crypto.randomUUID()}`;
|
|
150
|
+
const result = await workers.versions.create({
|
|
151
|
+
path,
|
|
152
|
+
headers: { 'Idempotency-Key': idempotencyKey },
|
|
153
|
+
// deploy stays true: the create and its pending deployment are one write to one Durable Object, and a replay
|
|
154
|
+
// returns that same deployment. Splitting it into two calls would add a second, unkeyed write whose timeout
|
|
155
|
+
// leaves the version created but not deployed.
|
|
156
|
+
body: {
|
|
144
157
|
deploy: true,
|
|
145
158
|
compatibility_date: input.compatibility_date ?? new Date().toISOString().slice(0, 10),
|
|
146
159
|
assets: versionAssets,
|
|
147
160
|
...(input.compatibility_flags ? { compatibility_flags: input.compatibility_flags } : {}),
|
|
148
161
|
...(input.bindings ? { bindings: input.bindings } : {}),
|
|
149
162
|
...(entryModules ? { main_module: mainModule, modules: entryModules } : {}),
|
|
150
|
-
}
|
|
163
|
+
},
|
|
164
|
+
});
|
|
151
165
|
let deployment = result.deployment;
|
|
152
166
|
if (!deployment)
|
|
153
167
|
throw new PraxisError({ code: 'INVALID_RESPONSE', message: 'Version creation did not return a deployment' });
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ticket-3252 — the function core for a refused CDP connect (ADR-024): one pure classification, the code the
|
|
3
|
+
* server named travels unchanged, and no refusal collapses into a generic one on the way out.
|
|
4
|
+
*
|
|
5
|
+
* Playwright's transport rejects with `WebSocket error: <url> <status> <statusWord>\n<body>` when the CDP upgrade
|
|
6
|
+
* is answered (ticket-834), and the body is the server's problem document — but the body does not always survive
|
|
7
|
+
* the handshake (`WebSocket error: wss://…/browser/v1/connect 409 Conflict`). The old fallback read only the body,
|
|
8
|
+
* so every body-less refusal became a 503 BROWSER_UNAVAILABLE: an agent met the retryable CONTEXT_LOCKED the
|
|
9
|
+
* service was holding (services/browser/src/api/rpc-contract.ts:208) as an outage it cannot act on.
|
|
10
|
+
*
|
|
11
|
+
* Owner of the status -> code mapping is the domain exit (services/browser/src/api/rpc-contract.ts), which the
|
|
12
|
+
* SDK mirrors by status only where the status is unambiguous on the connect path: 409 there is CONTEXT_LOCKED
|
|
13
|
+
* alone, 503 alone is SERVICE_UNAVAILABLE (:219). A body that names a code always wins over that mirror.
|
|
14
|
+
*/
|
|
15
|
+
/** A refusal read as the fields the SDK raises it with. */
|
|
16
|
+
export interface ConnectRefusal {
|
|
17
|
+
readonly status: number;
|
|
18
|
+
readonly code: string;
|
|
19
|
+
readonly message: string;
|
|
20
|
+
/** The refusal body, when the handshake carried a readable one. */
|
|
21
|
+
readonly raw?: unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The refusal a Playwright connect failure stands for. Total and pure: the same message always yields the same
|
|
25
|
+
* classification. The body's own code wins (the domain code crosses the boundary unchanged); with no readable
|
|
26
|
+
* body the status line names it, so a 409 arrives as CONTEXT_LOCKED rather than a 503.
|
|
27
|
+
*/
|
|
28
|
+
export declare function classifyConnectRefusal(message: string): ConnectRefusal;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ticket-3252 — the function core for a refused CDP connect (ADR-024): one pure classification, the code the
|
|
3
|
+
* server named travels unchanged, and no refusal collapses into a generic one on the way out.
|
|
4
|
+
*
|
|
5
|
+
* Playwright's transport rejects with `WebSocket error: <url> <status> <statusWord>\n<body>` when the CDP upgrade
|
|
6
|
+
* is answered (ticket-834), and the body is the server's problem document — but the body does not always survive
|
|
7
|
+
* the handshake (`WebSocket error: wss://…/browser/v1/connect 409 Conflict`). The old fallback read only the body,
|
|
8
|
+
* so every body-less refusal became a 503 BROWSER_UNAVAILABLE: an agent met the retryable CONTEXT_LOCKED the
|
|
9
|
+
* service was holding (services/browser/src/api/rpc-contract.ts:208) as an outage it cannot act on.
|
|
10
|
+
*
|
|
11
|
+
* Owner of the status -> code mapping is the domain exit (services/browser/src/api/rpc-contract.ts), which the
|
|
12
|
+
* SDK mirrors by status only where the status is unambiguous on the connect path: 409 there is CONTEXT_LOCKED
|
|
13
|
+
* alone, 503 alone is SERVICE_UNAVAILABLE (:219). A body that names a code always wins over that mirror.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* What a status line alone names, when the refusal body carried no code. Only statuses that the connect path
|
|
17
|
+
* answers with one unambiguous domain code belong here; anything else stays the outage.
|
|
18
|
+
*/
|
|
19
|
+
const CODE_BY_STATUS = {
|
|
20
|
+
400: 'INVALID_PARAMS',
|
|
21
|
+
401: 'UNAUTHENTICATED',
|
|
22
|
+
402: 'QUOTA_EXHAUSTED',
|
|
23
|
+
403: 'FORBIDDEN',
|
|
24
|
+
404: 'NOT_FOUND',
|
|
25
|
+
409: 'CONTEXT_LOCKED',
|
|
26
|
+
429: 'RATE_LIMITED',
|
|
27
|
+
};
|
|
28
|
+
/** The only useful thing to say about a locked sign-in context: wait for the lease, or leave it alone. */
|
|
29
|
+
const CONTEXT_LOCKED_MESSAGE = "The agent's saved sign-in context is locked by another session; retry shortly, or mount it read-only (persist:false).";
|
|
30
|
+
/** 503 is the outage: the service answered 503, or the SDK holds nothing that names the failure. */
|
|
31
|
+
const outage = (message) => ({ status: 503, code: 'BROWSER_UNAVAILABLE', message });
|
|
32
|
+
/** The object a body text parses to, or undefined when it is not a JSON object. */
|
|
33
|
+
function jsonObject(text) {
|
|
34
|
+
try {
|
|
35
|
+
const value = JSON.parse(text);
|
|
36
|
+
return value !== null && typeof value === 'object' ? value : undefined;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** The status line and body a handshake answer carries, or null when the message is not that shape (a DNS or connect error, a timeout). */
|
|
43
|
+
function refusalFromMessage(message) {
|
|
44
|
+
const line = /WebSocket error: \S+ (\d{3})(?:[^\n]*)/.exec(message);
|
|
45
|
+
if (!line)
|
|
46
|
+
return null;
|
|
47
|
+
const status = Number(line[1]);
|
|
48
|
+
if (!Number.isInteger(status))
|
|
49
|
+
return null;
|
|
50
|
+
const at = message.indexOf('\n');
|
|
51
|
+
return { status, body: at < 0 ? undefined : jsonObject(message.slice(at + 1)) };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The refusal a Playwright connect failure stands for. Total and pure: the same message always yields the same
|
|
55
|
+
* classification. The body's own code wins (the domain code crosses the boundary unchanged); with no readable
|
|
56
|
+
* body the status line names it, so a 409 arrives as CONTEXT_LOCKED rather than a 503.
|
|
57
|
+
*/
|
|
58
|
+
export function classifyConnectRefusal(message) {
|
|
59
|
+
const firstLine = message.split('\n')[0] ?? message;
|
|
60
|
+
const refusal = refusalFromMessage(message);
|
|
61
|
+
if (!refusal)
|
|
62
|
+
return outage(`Connecting to the cloud browser failed: ${firstLine}`);
|
|
63
|
+
const body = refusal.body;
|
|
64
|
+
const code = typeof body?.code === 'string' ? body.code : undefined;
|
|
65
|
+
if (code !== undefined && body) {
|
|
66
|
+
const retryAfter = typeof body.retryAfter === 'number' && Number.isFinite(body.retryAfter) ? body.retryAfter : null;
|
|
67
|
+
const text = typeof body.message === 'string' ? body.message : undefined;
|
|
68
|
+
return { status: refusal.status, code, message: `${text ?? code}${retryAfter === null ? '' : ` Retry after ${retryAfter} s.`}`, raw: body };
|
|
69
|
+
}
|
|
70
|
+
const byStatus = CODE_BY_STATUS[refusal.status];
|
|
71
|
+
if (byStatus === undefined)
|
|
72
|
+
return outage(`Connecting to the cloud browser failed: ${firstLine}`);
|
|
73
|
+
return { status: refusal.status, code: byStatus, message: refusal.status === 409 ? CONTEXT_LOCKED_MESSAGE : firstLine };
|
|
74
|
+
}
|
|
@@ -101,6 +101,8 @@ export interface BrowserConnectDeps {
|
|
|
101
101
|
fetch?: typeof globalThis.fetch;
|
|
102
102
|
}
|
|
103
103
|
export declare const unavailable: (message: string) => PraxisError;
|
|
104
|
+
/** The validated tab id, or INVALID_PARAMS naming the method, before any round trip. */
|
|
105
|
+
export declare const requireTabId: (method: string, id: unknown) => string;
|
|
104
106
|
/** Same signal the plugin installer and child_process use: the light machine asks to be upgraded and the program runs again. */
|
|
105
107
|
export declare const needsFullMachine: () => Error & {
|
|
106
108
|
code: string;
|
|
@@ -7,6 +7,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
9
|
import { PraxisError } from "../core/errors.js";
|
|
10
|
+
import { classifyConnectRefusal } from "./browser-connect-error.js";
|
|
10
11
|
import { BROWSER_DOCUMENTATION } from "./browser-documentation.js";
|
|
11
12
|
import { createBrowserActions } from "./browser-actions.js";
|
|
12
13
|
import { agentContextName, buildConnectUrl, decideBrowserDriver, decideTabReuse, isDuplicateNameError, matchTargetsToPages, pickAgentContext, pickContextByName } from "./browser-decisions.js";
|
|
@@ -17,6 +18,25 @@ const IDLE_MS = 300_000;
|
|
|
17
18
|
/** A scroll without a distance moves this far, the server's own default (PAGE_ACTION_LIMITS.scrollPixelsDefault). */
|
|
18
19
|
const SCROLL_PIXELS_DEFAULT = 800;
|
|
19
20
|
export const unavailable = (message) => new PraxisError({ status: 503, code: 'BROWSER_UNAVAILABLE', message });
|
|
21
|
+
/** The shape of a tab-id argument that is not a non-empty string, in the words the message uses. */
|
|
22
|
+
const argumentShape = (value) => typeof value === 'string' ? 'an empty string'
|
|
23
|
+
: value === null ? 'null'
|
|
24
|
+
: Array.isArray(value) ? 'an array'
|
|
25
|
+
: typeof value === 'object' ? 'an object'
|
|
26
|
+
: typeof value;
|
|
27
|
+
/** A tab method takes the id string. A wrong argument shape is the caller's mistake (400 INVALID_PARAMS),
|
|
28
|
+
* reported as the shape it is — never as a missing tab (404 TAB_NOT_FOUND), which is what passing the object
|
|
29
|
+
* `{ id }` / `{ tabId }` used to look like (ticket #3330). */
|
|
30
|
+
function tabIdArgument(method, value) {
|
|
31
|
+
const inner = value !== null && typeof value === 'object' ? value : undefined;
|
|
32
|
+
const named = typeof inner?.id === 'string' && inner.id ? inner.id : typeof inner?.tabId === 'string' && inner.tabId ? inner.tabId : undefined;
|
|
33
|
+
return new PraxisError({
|
|
34
|
+
status: 400, code: 'INVALID_PARAMS',
|
|
35
|
+
message: `${method}(id: string) takes the tab id string, e.g. ${method}(tab.id); got ${argumentShape(value)}${named ? `; pass ${method}(${JSON.stringify(named)})` : ''}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** The validated tab id, or INVALID_PARAMS naming the method, before any round trip. */
|
|
39
|
+
export const requireTabId = (method, id) => typeof id === 'string' && id.length > 0 ? id : (() => { throw tabIdArgument(method, id); })();
|
|
20
40
|
/** Same signal the plugin installer and child_process use: the light machine asks to be upgraded and the program runs again. */
|
|
21
41
|
export const needsFullMachine = () => Object.assign(new Error('The cloud browser needs a full sandbox — upgrade this instance'), { code: 'PROGRAM_NEEDS_FULL_MACHINE' });
|
|
22
42
|
/** Reads a Node builtin; a light machine's isolate refuses child_process, which is the signal to upgrade. */
|
|
@@ -124,37 +144,6 @@ async function ensureContext(deps, agentId) {
|
|
|
124
144
|
}
|
|
125
145
|
}
|
|
126
146
|
const bounded = (work, ms) => Promise.race([work, new Promise((_, reject) => setTimeout(() => reject(new Error('timed out')), ms))]);
|
|
127
|
-
/**
|
|
128
|
-
* ticket-834 — an agent that is refused a browser must be able to tell "come back in 7 s" from "your key is
|
|
129
|
-
* wrong". Playwright's transport rejects with `WebSocket error: <url> <status> <statusWord>\n<body>` when the
|
|
130
|
-
* CDP upgrade is answered, wrapped by `browserType.connectOverCDP: `; the body is the only part that says
|
|
131
|
-
* which refusal it was and how long to wait, so parse it instead of reporting the first line as an outage.
|
|
132
|
-
*/
|
|
133
|
-
function refusedConnect(cause) {
|
|
134
|
-
if (!(cause instanceof Error))
|
|
135
|
-
return null;
|
|
136
|
-
const match = /WebSocket error: \S+ (\d{3}) [^\n]*\n([\s\S]+)$/.exec(cause.message);
|
|
137
|
-
if (!match)
|
|
138
|
-
return null;
|
|
139
|
-
const status = Number(match[1]);
|
|
140
|
-
let body;
|
|
141
|
-
try {
|
|
142
|
-
body = JSON.parse(match[2].trim());
|
|
143
|
-
}
|
|
144
|
-
catch {
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
if (typeof body?.code !== 'string')
|
|
148
|
-
return null;
|
|
149
|
-
const retryAfter = typeof body.retryAfter === 'number' && Number.isFinite(body.retryAfter) ? body.retryAfter : null;
|
|
150
|
-
const message = typeof body.message === 'string' ? body.message : undefined;
|
|
151
|
-
return new PraxisError({
|
|
152
|
-
status,
|
|
153
|
-
code: body.code,
|
|
154
|
-
message: `${message ?? body.code}${retryAfter === null ? '' : ` Retry after ${retryAfter} s.`}`,
|
|
155
|
-
raw: body,
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
147
|
/**
|
|
159
148
|
* The light machine's browser: every call is one page action over HTTP, so nothing here loads playwright-core,
|
|
160
149
|
* reads `node:child_process`, or keeps a connection open. Tabs are addressed by the CDP target id the service
|
|
@@ -222,9 +211,10 @@ async function connectActions(deps, settings) {
|
|
|
222
211
|
tabs: {
|
|
223
212
|
list: () => client.list(),
|
|
224
213
|
get: async (id) => {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
214
|
+
const tabId = requireTabId('tabs.get', id);
|
|
215
|
+
if (!(await ids()).includes(tabId))
|
|
216
|
+
throw new PraxisError({ status: 404, code: 'TAB_NOT_FOUND', message: `No tab with id ${tabId}; call browser.tabs.list() for the current tabs.` });
|
|
217
|
+
return await wrap(tabId);
|
|
228
218
|
},
|
|
229
219
|
new: async () => {
|
|
230
220
|
// `open` with `newTab` is the only way the actions face creates a page.
|
|
@@ -258,8 +248,10 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
258
248
|
browser = await playwright.chromium.connectOverCDP(url, { headers: { Authorization: `Bearer ${token}` }, timeout: 60_000 });
|
|
259
249
|
}
|
|
260
250
|
catch (cause) {
|
|
261
|
-
// A refusal that named itself travels as itself
|
|
262
|
-
|
|
251
|
+
// A refusal that named itself travels as itself (ADR-024: a locked context is 409 CONTEXT_LOCKED, not a 503);
|
|
252
|
+
// only a real 503, or a failure with no status to read, is the outage it looks like (ticket-834, ticket-3252).
|
|
253
|
+
const refusal = classifyConnectRefusal(cause instanceof Error ? cause.message : String(cause));
|
|
254
|
+
throw new PraxisError({ status: refusal.status, code: refusal.code, message: refusal.message, raw: refusal.raw });
|
|
263
255
|
}
|
|
264
256
|
const fs = node(deps, 'node:fs');
|
|
265
257
|
const pages = () => browser.contexts().flatMap((c) => c.pages());
|
|
@@ -360,14 +352,15 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
360
352
|
active: await bounded(tab.page.evaluate(() => document.visibilityState === 'visible'), 2000).catch(() => false),
|
|
361
353
|
})));
|
|
362
354
|
const get = async (id) => {
|
|
355
|
+
const tabId = requireTabId('tabs.get', id);
|
|
363
356
|
for (const tab of await observed()) {
|
|
364
357
|
const known = await idOfObserved(tab);
|
|
365
|
-
if (known ===
|
|
358
|
+
if (known === tabId && known) {
|
|
366
359
|
await tab.page.bringToFront().catch(() => { });
|
|
367
360
|
return wrap(tab.page);
|
|
368
361
|
}
|
|
369
362
|
}
|
|
370
|
-
throw new PraxisError({ status: 404, code: 'TAB_NOT_FOUND', message: `No tab with id ${
|
|
363
|
+
throw new PraxisError({ status: 404, code: 'TAB_NOT_FOUND', message: `No tab with id ${tabId}; call browser.tabs.list() for the current tabs.` });
|
|
371
364
|
};
|
|
372
365
|
const create = async () => {
|
|
373
366
|
const owner = browser.contexts()[0];
|