@radheim/plugin-browser 0.0.0-stage → 2.4.3
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/README.md +144 -3
- package/dist/connection.d.ts +516 -0
- package/dist/connection.js +432 -0
- package/dist/egress-policy.d.ts +65 -0
- package/dist/egress-policy.js +446 -0
- package/dist/files.d.ts +75 -0
- package/dist/files.js +200 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -0
- package/dist/machine.d.ts +231 -0
- package/dist/machine.js +435 -0
- package/dist/preview-path.d.ts +7 -0
- package/dist/preview-path.js +28 -0
- package/dist/proxy-machine.d.ts +33 -0
- package/dist/proxy-machine.js +30 -0
- package/dist/proxy.d.ts +29 -0
- package/dist/proxy.js +438 -0
- package/dist/rpc.d.ts +2739 -0
- package/dist/rpc.js +502 -0
- package/dist/skill.d.ts +11 -0
- package/dist/skill.js +93 -0
- package/dist/stream-mux.d.ts +72 -0
- package/dist/stream-mux.js +1129 -0
- package/dist/stream-wire.d.ts +92 -0
- package/dist/stream-wire.js +273 -0
- package/dist/stream.d.ts +4 -0
- package/dist/stream.js +4 -0
- package/dist/tools.d.ts +534 -0
- package/dist/tools.js +451 -0
- package/dist/tunnel-host.d.ts +65 -0
- package/dist/tunnel-host.js +55 -0
- package/dist/tunnel-machine.d.ts +84 -0
- package/dist/tunnel-machine.js +127 -0
- package/dist/tunnel.d.ts +17 -0
- package/dist/tunnel.js +181 -0
- package/dist/wildcard.d.ts +11 -0
- package/dist/wildcard.js +21 -0
- package/dist/workspace.d.ts +3 -0
- package/dist/workspace.js +19 -0
- package/package.json +66 -4
package/dist/files.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
export * as BrowserFiles from "./files.js";
|
|
2
|
+
import { Browser } from "./rpc.js";
|
|
3
|
+
import { Tool } from "@radheim/schema/tool";
|
|
4
|
+
import { Effect } from "effect";
|
|
5
|
+
import { FSUtil } from "@radheim/util/fs-util";
|
|
6
|
+
import { Global } from "@radheim/util/global";
|
|
7
|
+
import { workspaceTarget } from "./workspace.js";
|
|
8
|
+
import { previewPath } from "./preview-path.js";
|
|
9
|
+
import { open, realpath, stat } from "node:fs/promises";
|
|
10
|
+
import { basename, dirname, extname, join, parse, relative, resolve } from "node:path";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
// Match core FileAccess's lexical resources; the plugin SDK exposes permission.assert, not FileAccess.Service.
|
|
14
|
+
export const authorize = Effect.fn("BrowserFiles.authorize")(function* (ctx, tool, action, paths) {
|
|
15
|
+
if (ctx.location.workspaceID)
|
|
16
|
+
return yield* new Tool.Error({ message: "Browser file transfers require a server-local workspace." });
|
|
17
|
+
const directory = ctx.location.directory;
|
|
18
|
+
const project = ctx.location.project.directory;
|
|
19
|
+
const outside = paths.filter((file) => !FSUtil.contains(directory, file) && (project === parse(project).root || !FSUtil.contains(project, file)));
|
|
20
|
+
const invocation = { sessionID: tool.sessionID, agent: tool.agent, source: { type: "tool", messageID: tool.messageID, id: tool.id }, metadata: { input: { paths } } };
|
|
21
|
+
if (outside.length)
|
|
22
|
+
yield* ctx.permission.assert({ ...invocation, action: "external_directory", resources: [...new Set(outside.map((file) => join(dirname(file), "*").replaceAll("\\", "/")))], save: [] });
|
|
23
|
+
const resources = paths.map((file) => (outside.includes(file) ? file : relative(directory, file) || ".").replaceAll("\\", "/"));
|
|
24
|
+
yield* ctx.permission.assert({ ...invocation, action, resources, save: outside.length || resources.some((r) => /[*?]/.test(r)) ? [] : resources });
|
|
25
|
+
});
|
|
26
|
+
export function uploadPath(directory, input) {
|
|
27
|
+
const path = FSUtil.windowsPath(input);
|
|
28
|
+
return resolve(directory, path === "~" ? Global.Path.home : /^~\//.test(path) || (process.platform === "win32" && path.startsWith("~\\")) ? join(Global.Path.home, path.slice(2)) : path);
|
|
29
|
+
}
|
|
30
|
+
export const physical = (paths) => Effect.tryPromise({ try: () => Promise.all(paths.map((path) => realpath(path))), catch: (error) => failure("read", error) });
|
|
31
|
+
// Files cross machines as bytes. Only this endpoint interprets its local paths.
|
|
32
|
+
export const read = Effect.fn("BrowserFiles.read")((paths, names) => Effect.tryPromise({
|
|
33
|
+
try: async () => {
|
|
34
|
+
const files = [];
|
|
35
|
+
let remaining = Browser.MAX_FILE_BYTES;
|
|
36
|
+
for (const [index, input] of paths.entries()) {
|
|
37
|
+
const file = await open(input, "r");
|
|
38
|
+
try {
|
|
39
|
+
const info = await file.stat();
|
|
40
|
+
if (!info.isFile())
|
|
41
|
+
throw new Error("Upload paths must name regular files.");
|
|
42
|
+
if (info.size > remaining)
|
|
43
|
+
throw new Error("The selected upload files exceed 5 MiB in total.");
|
|
44
|
+
const data = Buffer.alloc(info.size);
|
|
45
|
+
let offset = 0;
|
|
46
|
+
while (offset < data.length) {
|
|
47
|
+
const { bytesRead } = await file.read(data, offset, data.length - offset, offset);
|
|
48
|
+
if (!bytesRead)
|
|
49
|
+
throw new Error("The upload file changed while it was being read.");
|
|
50
|
+
offset += bytesRead;
|
|
51
|
+
}
|
|
52
|
+
remaining -= data.length;
|
|
53
|
+
const name = basename(names[index] ?? input);
|
|
54
|
+
files.push({
|
|
55
|
+
id: Browser.FileID.make(`file_${crypto.randomUUID()}`),
|
|
56
|
+
name,
|
|
57
|
+
mime: types[extname(name).toLowerCase()] ?? "application/octet-stream",
|
|
58
|
+
data,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
await file.close();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return files;
|
|
66
|
+
},
|
|
67
|
+
catch: (error) => failure("read", error),
|
|
68
|
+
}));
|
|
69
|
+
/**
|
|
70
|
+
* The workspace entry a path or `file:` address names, decided from its text alone, with no look at the disk: the
|
|
71
|
+
* address and its percent-encoding decoded, a line suffix dropped, `.` and `..` resolved. `entry` is the result
|
|
72
|
+
* relative to the workspace, with forward slashes. Undefined for an address that is no file. `unresolved` for text
|
|
73
|
+
* that is no path, `outside` for one that leaves the workspace, `invalid` for a name the desktop cannot open.
|
|
74
|
+
*/
|
|
75
|
+
export function entry(path, directory) {
|
|
76
|
+
const value = path.trim();
|
|
77
|
+
if (address(value) && !/^file:/i.test(value))
|
|
78
|
+
return undefined;
|
|
79
|
+
const local = localPath(value);
|
|
80
|
+
if (local === undefined)
|
|
81
|
+
return { unresolved: true };
|
|
82
|
+
if (local === "")
|
|
83
|
+
return { absolute: value, outside: true };
|
|
84
|
+
const plain = local.replace(/(?:#L\d+(?:C\d+)?(?:-L?\d+(?:C\d+)?)?|:\d+(?::\d+)?(?:-\d+(?::\d+)?)?:?)$/i, "");
|
|
85
|
+
const absolute = resolve(directory, plain);
|
|
86
|
+
if (!FSUtil.contains(directory, absolute))
|
|
87
|
+
return { absolute, outside: true };
|
|
88
|
+
const name = relative(directory, absolute).replaceAll("\\", "/");
|
|
89
|
+
if (name && !previewPath(name, process.platform === "win32"))
|
|
90
|
+
return { absolute, invalid: true };
|
|
91
|
+
return { absolute, entry: name };
|
|
92
|
+
}
|
|
93
|
+
// A `file:` address as a local path, with forward slashes. Empty for one that names another host; undefined for text
|
|
94
|
+
// that is no address or path at all.
|
|
95
|
+
function localPath(value) {
|
|
96
|
+
try {
|
|
97
|
+
const url = /^file:/i.test(value) ? new URL(value) : undefined;
|
|
98
|
+
if (url?.hostname)
|
|
99
|
+
return "";
|
|
100
|
+
return (url ? fileURLToPath(url) : value).replaceAll("\\", "/");
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Resolve and authorize the exact entry. A missing agent path must not trigger a search of unapproved directories. */
|
|
107
|
+
export const locate = Effect.fn("BrowserFiles.locate")(function* (path, directory, approve) {
|
|
108
|
+
const resolved = entry(path, directory);
|
|
109
|
+
if (!resolved)
|
|
110
|
+
return undefined;
|
|
111
|
+
const invalid = () => new Tool.Error({ metadata: { code: "unresolved_path" }, message: "This path does not resolve to a file inside the workspace." });
|
|
112
|
+
if ("unresolved" in resolved)
|
|
113
|
+
return yield* invalid();
|
|
114
|
+
const absolute = resolved.absolute;
|
|
115
|
+
const missing = { absolute, found: false, directory: false };
|
|
116
|
+
// Outside and invalid names can be refused lexically, without querying the filesystem or prompting to read them.
|
|
117
|
+
if ("outside" in resolved)
|
|
118
|
+
return { ...missing, outside: true };
|
|
119
|
+
if ("invalid" in resolved)
|
|
120
|
+
return { ...missing, invalid: true };
|
|
121
|
+
yield* approve(absolute);
|
|
122
|
+
const target = yield* Effect.tryPromise({ try: () => workspaceTarget(absolute, [directory]), catch: cause => cause instanceof Error && "code" in cause && (cause.code === "ENOENT" || cause.code === "ENOTDIR") ? "missing" : invalid() }).pipe(Effect.catchIf(error => error === "missing", () => Effect.succeed(null)));
|
|
123
|
+
if (target === null)
|
|
124
|
+
return missing;
|
|
125
|
+
if (!target)
|
|
126
|
+
return { ...missing, outside: true };
|
|
127
|
+
// The physical file, named under the workspace as the session spells it: a workspace reached through a symlink
|
|
128
|
+
// (macOS's /var is /private/var) keeps its own path, and only a link inside it to another entry needs approval.
|
|
129
|
+
const root = yield* Effect.tryPromise({ try: () => realpath(directory), catch: invalid });
|
|
130
|
+
const physical = resolve(directory, relative(root, target));
|
|
131
|
+
if (physical !== absolute)
|
|
132
|
+
yield* approve(physical);
|
|
133
|
+
const info = yield* Effect.tryPromise({ try: () => stat(target), catch: invalid });
|
|
134
|
+
return { absolute: physical, found: info.isFile(), directory: info.isDirectory() };
|
|
135
|
+
});
|
|
136
|
+
/** Whether a preview path is an address with a scheme, such as https:, skill: or file:. A drive letter is no scheme. */
|
|
137
|
+
export function address(path) {
|
|
138
|
+
return /^[a-z][a-z\d+.-]+:/i.test(path.trim());
|
|
139
|
+
}
|
|
140
|
+
const types = {
|
|
141
|
+
".txt": "text/plain",
|
|
142
|
+
".csv": "text/csv",
|
|
143
|
+
".json": "application/json",
|
|
144
|
+
".html": "text/html",
|
|
145
|
+
".png": "image/png",
|
|
146
|
+
".jpg": "image/jpeg",
|
|
147
|
+
".jpeg": "image/jpeg",
|
|
148
|
+
".webp": "image/webp",
|
|
149
|
+
".gif": "image/gif",
|
|
150
|
+
".svg": "image/svg+xml",
|
|
151
|
+
".pdf": "application/pdf",
|
|
152
|
+
".zip": "application/zip",
|
|
153
|
+
".gz": "application/gzip",
|
|
154
|
+
};
|
|
155
|
+
/** A session owns one private cache tree. Neither file names nor session IDs are interpreted as directory paths. */
|
|
156
|
+
export function exportRoot(sessionID) {
|
|
157
|
+
return join(Global.Path.cache, "browser-exports", createHash("sha256").update(sessionID).digest("hex"));
|
|
158
|
+
}
|
|
159
|
+
export function exportPaths(files, root) {
|
|
160
|
+
const directory = join(root, crypto.randomUUID());
|
|
161
|
+
return { directory, paths: files.map((file, index) => join(directory, String(index), captureName(file.name))) };
|
|
162
|
+
}
|
|
163
|
+
export const save = Effect.fn("BrowserFiles.save")((files, plan) => Effect.tryPromise({
|
|
164
|
+
try: async () => {
|
|
165
|
+
if (files.length === 0)
|
|
166
|
+
return [];
|
|
167
|
+
if (files.reduce((size, file) => size + file.data.byteLength, 0) > Browser.MAX_FILE_BYTES)
|
|
168
|
+
throw new Error("Capture files exceed the 5 MiB total transfer limit. Use a smaller screenshot, a shorter trace/profile, or a smaller page for heap capture; do not retry the identical capture.");
|
|
169
|
+
const { mkdir, writeFile } = await import("node:fs/promises");
|
|
170
|
+
await mkdir(dirname(plan.directory), { recursive: true, mode: 0o700 });
|
|
171
|
+
await mkdir(plan.directory, { mode: 0o700 });
|
|
172
|
+
return Promise.all(files.map(async (file, index) => {
|
|
173
|
+
const path = plan.paths[index];
|
|
174
|
+
await mkdir(dirname(path), { mode: 0o700 });
|
|
175
|
+
await writeFile(path, file.data, { flag: "wx", mode: 0o600 });
|
|
176
|
+
return { id: file.id, name: file.name, mime: file.mime, bytes: file.data.byteLength, path };
|
|
177
|
+
}));
|
|
178
|
+
},
|
|
179
|
+
catch: (error) => failure("save", error),
|
|
180
|
+
}));
|
|
181
|
+
// `.`/`..` escape the per-file directory and Windows resolves device names such as CON.txt regardless of directory.
|
|
182
|
+
export function captureName(name) {
|
|
183
|
+
const sanitized = name.replace(/[^a-zA-Z0-9._-]/g, "_").slice(-160);
|
|
184
|
+
if (!sanitized || /^\.{1,2}$/.test(sanitized) || /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i.test(sanitized))
|
|
185
|
+
return "capture";
|
|
186
|
+
return sanitized;
|
|
187
|
+
}
|
|
188
|
+
function failure(operation, error) {
|
|
189
|
+
const detail = error instanceof Error ? error.message.slice(0, 400) : String(error).slice(0, 400);
|
|
190
|
+
const code = error instanceof Error && "code" in error && typeof error.code === "string" && !detail.startsWith(error.code)
|
|
191
|
+
? `${error.code}: `
|
|
192
|
+
: "";
|
|
193
|
+
const recovery = operation === "save"
|
|
194
|
+
? "The browser may have completed the capture, but no server-local export is confirmed. Check free space and write access on the server. Use browser.files.list({tabID}) and browser.files.get({tabID,fileID}) to retrieve an existing completed capture instead of repeating its browser action."
|
|
195
|
+
: "Upload paths are on the server, not the desktop. Check that each path exists, is a file, and is readable on the server; correct paths or select smaller files before retrying.";
|
|
196
|
+
return new Tool.Error({
|
|
197
|
+
message: `Cannot ${operation} browser files on the server. ${recovery} Details: ${code}${detail}`,
|
|
198
|
+
error,
|
|
199
|
+
});
|
|
200
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Plugin } from "@radheim/plugin/effect";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { BrowserConnection } from "./connection.js";
|
|
4
|
+
import { BrowserTools } from "./tools.js";
|
|
5
|
+
export default Plugin.define({
|
|
6
|
+
id: "opencode.browser",
|
|
7
|
+
effect: (ctx) => Effect.gen(function* () {
|
|
8
|
+
const connection = yield* BrowserConnection.make(ctx);
|
|
9
|
+
yield* BrowserTools.register(ctx, connection);
|
|
10
|
+
}),
|
|
11
|
+
});
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
export * as BrowserMachine from "./machine.js";
|
|
2
|
+
import type { Permission } from "@radheim/schema/permission";
|
|
3
|
+
import type { Session } from "@radheim/schema/session";
|
|
4
|
+
import { Tool } from "@radheim/schema/tool";
|
|
5
|
+
import type { Browser } from "./rpc.js";
|
|
6
|
+
/** How long one permission sync (`session.get` and, when needed, `session.update`) may take. The desktop waits 15 s. */
|
|
7
|
+
export declare const PERMISSION_SYNC_TIMEOUT_MS = 10000;
|
|
8
|
+
/** How long a dispatched browser command may run before it times out and is cancelled on the desktop. */
|
|
9
|
+
export declare const REQUEST_TIMEOUT_MS = 60000;
|
|
10
|
+
/** The browser tools and their skill, which an attachment allows together. */
|
|
11
|
+
export declare const ACCESS: readonly [{
|
|
12
|
+
readonly action: "browser";
|
|
13
|
+
readonly resource: "*";
|
|
14
|
+
}, {
|
|
15
|
+
readonly action: "skill";
|
|
16
|
+
readonly resource: string & import("effect/Brand").Brand<"Skill.ID">;
|
|
17
|
+
}];
|
|
18
|
+
/** The one `session.permissions` sync in flight for a session: grant (`allow`) or revoke the browser rules. */
|
|
19
|
+
export type PermissionSync = {
|
|
20
|
+
readonly attempt: number;
|
|
21
|
+
readonly allow: boolean;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* One session's attachment. A session absent from `sessions` has no attachment and no sync in flight.
|
|
25
|
+
*
|
|
26
|
+
* - `attaching`: the desktop called `attach` as `attempt`. Its grant runs after any revoke in flight (`sync`); until it
|
|
27
|
+
* succeeds, no `attached` handshake is sent and no command is dispatched.
|
|
28
|
+
* - `attached`: the session allows the browser and the desktop received `attached`; commands run.
|
|
29
|
+
* - `revoking`: no attachment; `sync` settles, then a revoke follows unless the session was `deleted`.
|
|
30
|
+
*/
|
|
31
|
+
export type SessionState = {
|
|
32
|
+
readonly status: "attaching";
|
|
33
|
+
readonly attempt: number;
|
|
34
|
+
readonly connectionID: string;
|
|
35
|
+
readonly sync: PermissionSync;
|
|
36
|
+
} | {
|
|
37
|
+
readonly status: "attached";
|
|
38
|
+
readonly attempt: number;
|
|
39
|
+
readonly connectionID: string;
|
|
40
|
+
readonly browserState: Browser.State;
|
|
41
|
+
readonly pending: ReadonlyMap<string, Browser.Command>;
|
|
42
|
+
} | {
|
|
43
|
+
readonly status: "revoking";
|
|
44
|
+
readonly sync: PermissionSync;
|
|
45
|
+
readonly deleted: boolean;
|
|
46
|
+
};
|
|
47
|
+
export type RevokingSession = Extract<SessionState, {
|
|
48
|
+
readonly status: "revoking";
|
|
49
|
+
}>;
|
|
50
|
+
/** Every attachment of the plugin. Once `disposed`, only revokes still draining remain. */
|
|
51
|
+
export type ConnectionState = {
|
|
52
|
+
readonly status: "active";
|
|
53
|
+
readonly nextAttempt: number;
|
|
54
|
+
readonly sessions: ReadonlyMap<Session.ID, SessionState>;
|
|
55
|
+
} | {
|
|
56
|
+
readonly status: "disposed";
|
|
57
|
+
readonly nextAttempt: number;
|
|
58
|
+
readonly sessions: ReadonlyMap<Session.ID, RevokingSession>;
|
|
59
|
+
};
|
|
60
|
+
/** How an `attach` call ends: its result, or `unavailable` when the grant failed. */
|
|
61
|
+
export type AttachmentOutcome = "closed" | "replaced" | "unavailable";
|
|
62
|
+
export type RequestSettlement = {
|
|
63
|
+
readonly ok: true;
|
|
64
|
+
readonly result: Browser.Result;
|
|
65
|
+
} | {
|
|
66
|
+
readonly ok: false;
|
|
67
|
+
readonly error: Tool.Error;
|
|
68
|
+
};
|
|
69
|
+
export type ConnectionEvent =
|
|
70
|
+
/** `attempt` is the state's `nextAttempt`, read when the driver registered the attachment's handle. */
|
|
71
|
+
{
|
|
72
|
+
readonly type: "attach.started";
|
|
73
|
+
readonly sessionID: Session.ID;
|
|
74
|
+
readonly attempt: number;
|
|
75
|
+
readonly connectionID: string;
|
|
76
|
+
}
|
|
77
|
+
/** The `attach` call ended on the desktop's side (disconnect or interrupt). */
|
|
78
|
+
| {
|
|
79
|
+
readonly type: "attach.closed";
|
|
80
|
+
readonly sessionID: Session.ID;
|
|
81
|
+
readonly attempt: number;
|
|
82
|
+
} | {
|
|
83
|
+
readonly type: "permissions.synced";
|
|
84
|
+
readonly sessionID: Session.ID;
|
|
85
|
+
readonly attempt: number;
|
|
86
|
+
} | {
|
|
87
|
+
readonly type: "permissions.failed";
|
|
88
|
+
readonly sessionID: Session.ID;
|
|
89
|
+
readonly attempt: number;
|
|
90
|
+
readonly error: unknown;
|
|
91
|
+
} | {
|
|
92
|
+
readonly type: "permissions.timed-out";
|
|
93
|
+
readonly sessionID: Session.ID;
|
|
94
|
+
readonly attempt: number;
|
|
95
|
+
} | {
|
|
96
|
+
readonly type: "state.updated";
|
|
97
|
+
readonly sessionID: Session.ID;
|
|
98
|
+
readonly connectionID: string;
|
|
99
|
+
readonly browserState: Browser.State;
|
|
100
|
+
} | {
|
|
101
|
+
readonly type: "request.started";
|
|
102
|
+
readonly sessionID: Session.ID;
|
|
103
|
+
readonly connectionID: string;
|
|
104
|
+
readonly requestID: string;
|
|
105
|
+
readonly command: Browser.Command;
|
|
106
|
+
} | {
|
|
107
|
+
readonly type: "request.dispatch-failed";
|
|
108
|
+
readonly sessionID: Session.ID;
|
|
109
|
+
readonly connectionID: string;
|
|
110
|
+
readonly requestID: string;
|
|
111
|
+
readonly error: unknown;
|
|
112
|
+
} | {
|
|
113
|
+
readonly type: "request.settled";
|
|
114
|
+
readonly sessionID: Session.ID;
|
|
115
|
+
readonly connectionID: string;
|
|
116
|
+
readonly requestID: string;
|
|
117
|
+
readonly outcome: Browser.Outcome;
|
|
118
|
+
} | {
|
|
119
|
+
readonly type: "request.timed-out";
|
|
120
|
+
readonly sessionID: Session.ID;
|
|
121
|
+
readonly connectionID: string;
|
|
122
|
+
readonly requestID: string;
|
|
123
|
+
}
|
|
124
|
+
/** The tool call that started the request was interrupted. */
|
|
125
|
+
| {
|
|
126
|
+
readonly type: "request.cancelled";
|
|
127
|
+
readonly sessionID: Session.ID;
|
|
128
|
+
readonly connectionID: string;
|
|
129
|
+
readonly requestID: string;
|
|
130
|
+
} | {
|
|
131
|
+
readonly type: "session.moved";
|
|
132
|
+
readonly sessionID: Session.ID;
|
|
133
|
+
} | {
|
|
134
|
+
readonly type: "session.deleted";
|
|
135
|
+
readonly sessionID: Session.ID;
|
|
136
|
+
} | {
|
|
137
|
+
readonly type: "disposed";
|
|
138
|
+
};
|
|
139
|
+
export type ConnectionEffect = {
|
|
140
|
+
readonly type: "sync-permissions";
|
|
141
|
+
readonly sessionID: Session.ID;
|
|
142
|
+
readonly attempt: number;
|
|
143
|
+
readonly allow: boolean;
|
|
144
|
+
}
|
|
145
|
+
/** Disposes the attachment's tunnels and ends its `attach` call with `outcome`. */
|
|
146
|
+
| {
|
|
147
|
+
readonly type: "close-attachment";
|
|
148
|
+
readonly attempt: number;
|
|
149
|
+
readonly outcome: AttachmentOutcome;
|
|
150
|
+
} | {
|
|
151
|
+
readonly type: "emit-control";
|
|
152
|
+
readonly control: Browser.Control;
|
|
153
|
+
}
|
|
154
|
+
/** Emits the `command` control; a failure comes back as `request.dispatch-failed`. */
|
|
155
|
+
| {
|
|
156
|
+
readonly type: "dispatch-command";
|
|
157
|
+
readonly sessionID: Session.ID;
|
|
158
|
+
readonly connectionID: string;
|
|
159
|
+
readonly requestID: string;
|
|
160
|
+
} | {
|
|
161
|
+
readonly type: "settle-request";
|
|
162
|
+
readonly requestID: string;
|
|
163
|
+
readonly settlement: RequestSettlement;
|
|
164
|
+
} | {
|
|
165
|
+
readonly type: "warn";
|
|
166
|
+
readonly message: string;
|
|
167
|
+
readonly error: unknown;
|
|
168
|
+
}
|
|
169
|
+
/** The disposed plugin has no revoke left in flight. */
|
|
170
|
+
| {
|
|
171
|
+
readonly type: "notify-drained";
|
|
172
|
+
};
|
|
173
|
+
export type ConnectionTransition = {
|
|
174
|
+
readonly state: ConnectionState;
|
|
175
|
+
readonly effects: readonly ConnectionEffect[];
|
|
176
|
+
};
|
|
177
|
+
export declare function createConnectionState(): ConnectionState;
|
|
178
|
+
export declare function transitionConnection(state: ConnectionState, event: ConnectionEvent): ConnectionTransition;
|
|
179
|
+
/**
|
|
180
|
+
* The session's permissions with exactly one set of browser allows (`allow`) or none, every other rule kept in order.
|
|
181
|
+
* Undefined when they already are, so a replace writes nothing.
|
|
182
|
+
*/
|
|
183
|
+
export declare function reconcileBrowserPermissions(current: readonly Permission.Rule[] | undefined, allow: boolean): Permission.Rule[] | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* One of the allows an attachment grants. A grant that holds only the browser allow, as some versions wrote it, is
|
|
186
|
+
* recognised too, so a revoke or the startup sweep removes it. A user rule of the same shape cannot be told apart
|
|
187
|
+
* from it.
|
|
188
|
+
*/
|
|
189
|
+
export declare function isGrant(rule: Permission.Rule): boolean;
|
|
190
|
+
/**
|
|
191
|
+
* The rule that decides `browser` on `operation`, picked as core's `Permission.evaluate` picks it: the last rule whose
|
|
192
|
+
* action and resource patterns both match. Undefined when none matches.
|
|
193
|
+
*/
|
|
194
|
+
export declare function selectBrowserRule(rules: readonly Permission.Rule[], operation: string): Permission.Rule | undefined;
|
|
195
|
+
export declare function isConnectionDisposed(state: ConnectionState): state is {
|
|
196
|
+
readonly status: "disposed";
|
|
197
|
+
readonly nextAttempt: number;
|
|
198
|
+
readonly sessions: ReadonlyMap<Session.ID, RevokingSession>;
|
|
199
|
+
};
|
|
200
|
+
/** Whether the session has an attachment or a permission sync in flight, either of which owns its browser rules. */
|
|
201
|
+
export declare function hasSession(state: ConnectionState, sessionID: Session.ID): boolean;
|
|
202
|
+
/** The attempt the next `attach` gets; the driver keys its handle by it before dispatching `attach.started`. */
|
|
203
|
+
export declare function selectNextAttempt(state: ConnectionState): number;
|
|
204
|
+
/** The attempt of the session's attachment with `connectionID`, attaching or attached. */
|
|
205
|
+
export declare function selectAttachment(state: ConnectionState, sessionID: Session.ID, connectionID: string): number | undefined;
|
|
206
|
+
export declare function isAttachedConnection(state: ConnectionState, sessionID: Session.ID, connectionID: string): boolean;
|
|
207
|
+
export declare function selectPendingCommand(state: ConnectionState, sessionID: Session.ID, connectionID: string, requestID: string): Browser.Command | undefined;
|
|
208
|
+
/**
|
|
209
|
+
* The session's attached connection, or the error a tool call returns while it has none. Reads nothing about its tabs,
|
|
210
|
+
* so it may answer before the call is authorized.
|
|
211
|
+
*/
|
|
212
|
+
export declare function selectConnection(state: ConnectionState, sessionID: Session.ID): {
|
|
213
|
+
readonly ok: true;
|
|
214
|
+
readonly connectionID: string;
|
|
215
|
+
readonly tabs: readonly Browser.Tab[];
|
|
216
|
+
} | {
|
|
217
|
+
readonly ok: false;
|
|
218
|
+
readonly error: Tool.Error;
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* The attachment and tab a browser tool call targets, or the error the tool returns. The error for an unknown tab lists
|
|
222
|
+
* the session's tabs, so this runs only once the call is authorized.
|
|
223
|
+
*/
|
|
224
|
+
export declare function selectTarget(state: ConnectionState, sessionID: Session.ID, action: Browser.Action): {
|
|
225
|
+
readonly ok: true;
|
|
226
|
+
readonly connectionID: string;
|
|
227
|
+
readonly tab: Browser.Tab | undefined;
|
|
228
|
+
} | {
|
|
229
|
+
readonly ok: false;
|
|
230
|
+
readonly error: Tool.Error;
|
|
231
|
+
};
|