@deepraxis/sdk 0.1.1-dev.972fea6 → 0.1.1-dev.98b74fb
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 +45 -1
- package/dist/core/binary-operations.d.ts +3 -0
- package/dist/core/binary-operations.js +14 -0
- 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 +77 -19
- package/dist/generated/sdk.gen.js +172 -18
- package/dist/generated/types.gen.d.ts +2991 -124
- package/dist/helpers/browser-connect-error.d.ts +28 -0
- package/dist/helpers/browser-connect-error.js +74 -0
- package/dist/helpers/browser-connect.js +5 -33
- package/dist/operations.generated.js +383 -32
- package/dist/resources.generated.d.ts +821 -232
- package/dist/resources.generated.js +23 -0
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -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";
|
|
@@ -124,37 +125,6 @@ async function ensureContext(deps, agentId) {
|
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
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
128
|
/**
|
|
159
129
|
* The light machine's browser: every call is one page action over HTTP, so nothing here loads playwright-core,
|
|
160
130
|
* reads `node:child_process`, or keeps a connection open. Tabs are addressed by the CDP target id the service
|
|
@@ -258,8 +228,10 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
258
228
|
browser = await playwright.chromium.connectOverCDP(url, { headers: { Authorization: `Bearer ${token}` }, timeout: 60_000 });
|
|
259
229
|
}
|
|
260
230
|
catch (cause) {
|
|
261
|
-
// A refusal that named itself travels as itself
|
|
262
|
-
|
|
231
|
+
// A refusal that named itself travels as itself (ADR-024: a locked context is 409 CONTEXT_LOCKED, not a 503);
|
|
232
|
+
// only a real 503, or a failure with no status to read, is the outage it looks like (ticket-834, ticket-3252).
|
|
233
|
+
const refusal = classifyConnectRefusal(cause instanceof Error ? cause.message : String(cause));
|
|
234
|
+
throw new PraxisError({ status: refusal.status, code: refusal.code, message: refusal.message, raw: refusal.raw });
|
|
263
235
|
}
|
|
264
236
|
const fs = node(deps, 'node:fs');
|
|
265
237
|
const pages = () => browser.contexts().flatMap((c) => c.pages());
|