@deepraxis/sdk 0.1.1-dev.48a97c5 → 0.1.1-dev.4b420a8
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 +89 -1
- package/dist/core/errors.d.ts +6 -0
- package/dist/core/errors.js +8 -0
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +1 -1
- package/dist/generated/sdk.gen.d.ts +133 -6
- package/dist/generated/sdk.gen.js +336 -5
- package/dist/generated/types.gen.d.ts +2453 -28
- package/dist/helpers/browser-connect.d.ts +12 -0
- package/dist/helpers/browser-connect.js +108 -22
- package/dist/helpers/browser-decisions.d.ts +29 -0
- package/dist/helpers/browser-decisions.js +39 -0
- package/dist/operations.generated.d.ts +644 -9
- package/dist/operations.generated.js +807 -8
- package/dist/resources.generated.d.ts +53 -1
- package/dist/resources.generated.js +52 -0
- package/package.json +1 -1
|
@@ -47,6 +47,7 @@ interface ContextsApi {
|
|
|
47
47
|
query?: {
|
|
48
48
|
limit?: number;
|
|
49
49
|
cursor?: string;
|
|
50
|
+
name?: string;
|
|
50
51
|
};
|
|
51
52
|
}): Promise<{
|
|
52
53
|
data: (ContextLike & {
|
|
@@ -61,6 +62,15 @@ interface ContextsApi {
|
|
|
61
62
|
};
|
|
62
63
|
}): Promise<ContextLike>;
|
|
63
64
|
}
|
|
65
|
+
/** The playwright-core module shape the helper uses. */
|
|
66
|
+
export interface PlaywrightModule {
|
|
67
|
+
chromium: {
|
|
68
|
+
connectOverCDP(url: string, options: {
|
|
69
|
+
headers: Record<string, string>;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
}): Promise<unknown>;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
64
74
|
/** What connect() needs from the platform client and the process. All of it is injectable for tests. */
|
|
65
75
|
export interface BrowserConnectDeps {
|
|
66
76
|
contexts: ContextsApi;
|
|
@@ -68,6 +78,8 @@ export interface BrowserConnectDeps {
|
|
|
68
78
|
/** process.getBuiltinModule; undefined on runtimes without it. */
|
|
69
79
|
builtin: ((name: string) => unknown) | undefined;
|
|
70
80
|
now?: () => number;
|
|
81
|
+
/** An already-loaded playwright-core; the real loader finds or installs one and imports it. */
|
|
82
|
+
playwright?: PlaywrightModule;
|
|
71
83
|
}
|
|
72
84
|
export declare const unavailable: (message: string) => PraxisError;
|
|
73
85
|
/** Same signal the plugin installer and child_process use: the light machine asks to be upgraded and the program runs again. */
|
|
@@ -8,7 +8,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
8
8
|
};
|
|
9
9
|
import { PraxisError } from "../core/errors.js";
|
|
10
10
|
import { BROWSER_DOCUMENTATION } from "./browser-documentation.js";
|
|
11
|
-
import { agentContextName, buildConnectUrl, decideTabReuse, pickAgentContext } from "./browser-decisions.js";
|
|
11
|
+
import { agentContextName, buildConnectUrl, decideTabReuse, isDuplicateNameError, matchTargetsToPages, pickAgentContext, pickContextByName } from "./browser-decisions.js";
|
|
12
12
|
/** Version of the Playwright client library: preinstalled in the base image, installed on demand elsewhere. */
|
|
13
13
|
export const PLAYWRIGHT_CORE_VERSION = '1.63.0';
|
|
14
14
|
const GLOBAL_PLAYWRIGHT = '/usr/local/lib/node_modules/playwright-core';
|
|
@@ -33,6 +33,8 @@ function node(deps, name) {
|
|
|
33
33
|
throw needsFullMachine();
|
|
34
34
|
}
|
|
35
35
|
async function loadPlaywright(deps, home) {
|
|
36
|
+
if (deps.playwright)
|
|
37
|
+
return deps.playwright;
|
|
36
38
|
node(deps, 'node:child_process'); // fails fast on a light machine, before any network work
|
|
37
39
|
const fs = node(deps, 'node:fs');
|
|
38
40
|
const entry = (root) => `${root}/index.mjs`;
|
|
@@ -49,20 +51,49 @@ async function loadPlaywright(deps, home) {
|
|
|
49
51
|
}
|
|
50
52
|
return await import(__rewriteRelativeImportExtension(/* @vite-ignore */ `file://${entry(root)}`));
|
|
51
53
|
}
|
|
54
|
+
/** The agent's own context, in one name-filtered request; the paged scan is only the pre-`name` server fallback. */
|
|
52
55
|
async function ensureContext(deps, agentId) {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
const name = agentContextName(agentId);
|
|
57
|
+
const byNameQuery = () => deps.contexts.list({ query: { name, limit: 1 } });
|
|
58
|
+
// A server that knows `name` answers with this agent's context or nothing; a non-empty answer of other rows means
|
|
59
|
+
// it ignored the filter, and only then is the paged scan worth its round trips.
|
|
60
|
+
const paged = async () => {
|
|
61
|
+
const seen = [];
|
|
62
|
+
let cursor;
|
|
63
|
+
for (let page = 0; page < 20; page++) {
|
|
64
|
+
const result = await deps.contexts.list({ query: { limit: 100, ...(cursor ? { cursor } : {}) } });
|
|
65
|
+
seen.push(...result.data);
|
|
66
|
+
const own = pickAgentContext(seen, agentId);
|
|
67
|
+
if (own)
|
|
68
|
+
return own.id;
|
|
69
|
+
if (!result.nextCursor)
|
|
70
|
+
break;
|
|
71
|
+
cursor = result.nextCursor;
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
};
|
|
75
|
+
const find = async () => {
|
|
76
|
+
const answer = (await byNameQuery()).data;
|
|
77
|
+
const byName = pickContextByName(answer, agentId);
|
|
78
|
+
if (byName)
|
|
79
|
+
return byName.id;
|
|
80
|
+
return answer.length > 0 ? paged() : undefined;
|
|
81
|
+
};
|
|
82
|
+
const existing = await find();
|
|
83
|
+
if (existing)
|
|
84
|
+
return existing;
|
|
85
|
+
try {
|
|
86
|
+
return (await deps.contexts.create({ body: { name, labels: { agent: agentId } } })).id;
|
|
87
|
+
}
|
|
88
|
+
catch (cause) {
|
|
89
|
+
// Names are unique per account, so this is a concurrent run winning the create: reuse what it made.
|
|
90
|
+
if (!isDuplicateNameError(cause))
|
|
91
|
+
throw cause;
|
|
92
|
+
const raced = await find();
|
|
93
|
+
if (raced)
|
|
94
|
+
return raced;
|
|
95
|
+
throw cause;
|
|
64
96
|
}
|
|
65
|
-
return (await deps.contexts.create({ body: { name: agentContextName(agentId), labels: { agent: agentId } } })).id;
|
|
66
97
|
}
|
|
67
98
|
const bounded = (work, ms) => Promise.race([work, new Promise((_, reject) => setTimeout(() => reject(new Error('timed out')), ms))]);
|
|
68
99
|
export async function connectBrowser(deps, options = {}) {
|
|
@@ -86,6 +117,33 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
86
117
|
const now = deps.now ?? Date.now;
|
|
87
118
|
const pages = () => browser.contexts().flatMap((c) => c.pages());
|
|
88
119
|
const ids = new WeakMap();
|
|
120
|
+
/** One browser-level CDP session for the connection: `Target.getTargets` answers for every tab in one round trip. */
|
|
121
|
+
let cdp;
|
|
122
|
+
const browserSession = async () => {
|
|
123
|
+
if (!browser.newBrowserCDPSession)
|
|
124
|
+
return undefined; // older playwright-core: every page keeps the per-page lookup
|
|
125
|
+
if (!cdp) {
|
|
126
|
+
try {
|
|
127
|
+
cdp = await browser.newBrowserCDPSession();
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
} // a browser that refuses the session lists tabs the old way, it does not fail
|
|
132
|
+
}
|
|
133
|
+
return cdp;
|
|
134
|
+
};
|
|
135
|
+
/** CDP's page targets, or nothing when the browser session is unavailable — never a thrown listing. */
|
|
136
|
+
const pageTargets = async () => {
|
|
137
|
+
const session = await browserSession();
|
|
138
|
+
if (!session)
|
|
139
|
+
return undefined;
|
|
140
|
+
try {
|
|
141
|
+
return ((await session.send('Target.getTargets'))?.targetInfos ?? []);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
89
147
|
const idOf = async (page) => {
|
|
90
148
|
const known = ids.get(page);
|
|
91
149
|
if (known)
|
|
@@ -100,6 +158,25 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
100
158
|
await session.detach().catch(() => { });
|
|
101
159
|
}
|
|
102
160
|
};
|
|
161
|
+
/**
|
|
162
|
+
* Every page with its target id: the one `Target.getTargets` covers the pages it names unambiguously, and only
|
|
163
|
+
* the rest (another tab on the same url, a page it does not name) pay for their own `Target.getTargetInfo`.
|
|
164
|
+
* Matching itself makes no request, so `get` reads the map for free and only a miss asks the page again.
|
|
165
|
+
*/
|
|
166
|
+
const observed = async () => {
|
|
167
|
+
const all = pages();
|
|
168
|
+
const urls = all.map(page => page.url());
|
|
169
|
+
const targets = await pageTargets();
|
|
170
|
+
const { matched } = matchTargetsToPages(targets ?? [], urls);
|
|
171
|
+
return all.map((page, index) => {
|
|
172
|
+
const target = matched.get(index);
|
|
173
|
+
if (target)
|
|
174
|
+
ids.set(page, target.targetId);
|
|
175
|
+
return { page, id: target?.targetId, url: target?.url ?? urls[index] ?? '', title: target?.title };
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
/** A page's target id, from the map when it named it or from its own CDP session otherwise. */
|
|
179
|
+
const idOfObserved = async (tab) => tab.id ?? await bounded(idOf(tab.page), 2000).catch(() => '');
|
|
103
180
|
const wrap = async (page) => {
|
|
104
181
|
const id = await idOf(page);
|
|
105
182
|
return {
|
|
@@ -121,17 +198,20 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
121
198
|
},
|
|
122
199
|
};
|
|
123
200
|
};
|
|
124
|
-
const list = async () => Promise.all(
|
|
125
|
-
id: await
|
|
126
|
-
title
|
|
127
|
-
|
|
201
|
+
const list = async () => Promise.all((await observed()).map(async (tab) => ({
|
|
202
|
+
id: await idOfObserved(tab), url: tab.url,
|
|
203
|
+
// A matched target already carries the title; an unmapped page keeps asking its own page, as before.
|
|
204
|
+
title: tab.title ?? await bounded(tab.page.title(), 2000).catch(() => ''),
|
|
205
|
+
active: await bounded(tab.page.evaluate(() => document.visibilityState === 'visible'), 2000).catch(() => false),
|
|
128
206
|
})));
|
|
129
207
|
const get = async (id) => {
|
|
130
|
-
for (const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
208
|
+
for (const tab of await observed()) {
|
|
209
|
+
const known = await idOfObserved(tab);
|
|
210
|
+
if (known === id && known) {
|
|
211
|
+
await tab.page.bringToFront().catch(() => { });
|
|
212
|
+
return wrap(tab.page);
|
|
134
213
|
}
|
|
214
|
+
}
|
|
135
215
|
throw new PraxisError({ status: 404, code: 'TAB_NOT_FOUND', message: `No tab with id ${id}; call browser.tabs.list() for the current tabs.` });
|
|
136
216
|
};
|
|
137
217
|
const create = async () => {
|
|
@@ -156,6 +236,12 @@ export async function connectBrowser(deps, options = {}) {
|
|
|
156
236
|
return tab;
|
|
157
237
|
},
|
|
158
238
|
documentation: () => BROWSER_DOCUMENTATION,
|
|
159
|
-
|
|
239
|
+
/** Drops this connection only; the cloud session and its tabs stay until it idles out. */
|
|
240
|
+
disconnect: async () => {
|
|
241
|
+
// browser.close() drops the whole CDP connection, the browser-level session with it; detaching
|
|
242
|
+
// it first only added one more cross-region round trip (+0.6–0.8 s on the #829 LAB).
|
|
243
|
+
cdp = undefined;
|
|
244
|
+
await browser.close();
|
|
245
|
+
},
|
|
160
246
|
};
|
|
161
247
|
}
|
|
@@ -42,3 +42,32 @@ export declare function pickAgentContext<T extends ContextLike>(contexts: readon
|
|
|
42
42
|
export declare const agentContextName: (agentId: string) => string;
|
|
43
43
|
/** Start pages are expendable only on Console hosts, never arbitrary sites' /start pages. */
|
|
44
44
|
export declare function isBlankTab(value: string): boolean;
|
|
45
|
+
/** One entry of a browser-level `Target.getTargets` answer, kept structural. */
|
|
46
|
+
export interface CdpPageTarget {
|
|
47
|
+
targetId: string;
|
|
48
|
+
type?: string;
|
|
49
|
+
url: string;
|
|
50
|
+
title?: string;
|
|
51
|
+
}
|
|
52
|
+
export interface TargetMatch<T extends CdpPageTarget> {
|
|
53
|
+
/** Page index → its CDP target, for every page a single target names unambiguously. */
|
|
54
|
+
matched: Map<number, T>;
|
|
55
|
+
/** Page indexes that still need their own `Target.getTargetInfo`: no target, or a url shared by several tabs. */
|
|
56
|
+
unmatched: number[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Matches the browser's page targets (a raw `Target.getTargets` answer) to the pages Playwright holds, by url.
|
|
60
|
+
* Non-page targets name no tab. A url naming more than one target, or more than one page, identifies nothing:
|
|
61
|
+
* both sides fall back to the per-page lookup, because a wrong tab id is worse than one extra round trip.
|
|
62
|
+
*/
|
|
63
|
+
export declare function matchTargetsToPages<T extends CdpPageTarget>(targets: readonly T[], pageUrls: readonly string[]): TargetMatch<T>;
|
|
64
|
+
/**
|
|
65
|
+
* The context a name-filtered lookup returned for this agent, or nothing when the entry is not it.
|
|
66
|
+
* An old server ignores `name` and answers with unrelated rows; adopting one of those would hand this run
|
|
67
|
+
* another agent's stored sign-ins.
|
|
68
|
+
*/
|
|
69
|
+
export declare function pickContextByName<T extends ContextLike & {
|
|
70
|
+
name?: unknown;
|
|
71
|
+
}>(contexts: readonly T[], agentId: string): T | undefined;
|
|
72
|
+
/** A context name is unique per account, so a create that loses a race answers INVALID_PARAMS with this phrase. */
|
|
73
|
+
export declare function isDuplicateNameError(error: unknown): boolean;
|
|
@@ -82,3 +82,42 @@ export function isBlankTab(value) {
|
|
|
82
82
|
return false;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Matches the browser's page targets (a raw `Target.getTargets` answer) to the pages Playwright holds, by url.
|
|
87
|
+
* Non-page targets name no tab. A url naming more than one target, or more than one page, identifies nothing:
|
|
88
|
+
* both sides fall back to the per-page lookup, because a wrong tab id is worse than one extra round trip.
|
|
89
|
+
*/
|
|
90
|
+
export function matchTargetsToPages(targets, pageUrls) {
|
|
91
|
+
const byUrl = new Map();
|
|
92
|
+
for (const target of targets)
|
|
93
|
+
if (target.type === 'page')
|
|
94
|
+
byUrl.set(target.url, [...(byUrl.get(target.url) ?? []), target]);
|
|
95
|
+
const pagesPerUrl = new Map();
|
|
96
|
+
for (const url of pageUrls)
|
|
97
|
+
pagesPerUrl.set(url, (pagesPerUrl.get(url) ?? 0) + 1);
|
|
98
|
+
const matched = new Map();
|
|
99
|
+
const unmatched = [];
|
|
100
|
+
pageUrls.forEach((url, index) => {
|
|
101
|
+
const candidates = byUrl.get(url);
|
|
102
|
+
const target = candidates?.length === 1 && pagesPerUrl.get(url) === 1 ? candidates[0] : undefined;
|
|
103
|
+
if (target)
|
|
104
|
+
matched.set(index, target);
|
|
105
|
+
else
|
|
106
|
+
unmatched.push(index);
|
|
107
|
+
});
|
|
108
|
+
return { matched, unmatched };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The context a name-filtered lookup returned for this agent, or nothing when the entry is not it.
|
|
112
|
+
* An old server ignores `name` and answers with unrelated rows; adopting one of those would hand this run
|
|
113
|
+
* another agent's stored sign-ins.
|
|
114
|
+
*/
|
|
115
|
+
export function pickContextByName(contexts, agentId) {
|
|
116
|
+
const wanted = agentContextName(agentId);
|
|
117
|
+
return contexts.find(context => context.name === wanted && context.labels?.agent === agentId);
|
|
118
|
+
}
|
|
119
|
+
/** A context name is unique per account, so a create that loses a race answers INVALID_PARAMS with this phrase. */
|
|
120
|
+
export function isDuplicateNameError(error) {
|
|
121
|
+
const problem = error;
|
|
122
|
+
return problem?.code === 'INVALID_PARAMS' && typeof problem.message === 'string' && problem.message.includes('already exists');
|
|
123
|
+
}
|