@omercnet/paseo-shared-browser 4.1.12 → 4.2.0-next.537.1
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/OVERVIEW.md +1 -1
- package/README.md +40 -2
- package/client/agent-access-settings.tsx +157 -0
- package/client/browser-canvas-keyboard-web.test.ts +185 -2
- package/client/browser-canvas-keyboard-web.ts +48 -8
- package/client/browser-input-queue.test.ts +32 -0
- package/client/browser-input-queue.ts +12 -0
- package/index.client.tsx +8 -0
- package/index.server.ts +145 -29
- package/npm-shrinkwrap.json +44 -40
- package/package.json +5 -5
- package/paseo-plugin.json +1 -1
- package/server/supervisor.ts +28 -0
- package/server/workspace-list.ts +42 -0
- package/shared/agent-access.ts +204 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which new agents get the Shared Browser tool.
|
|
3
|
+
*
|
|
4
|
+
* "all" injects the adapter into every new agent (the behaviour before this setting existed, and the
|
|
5
|
+
* default). "listed" injects it only when the new agent matches an entry of one of four lists: its
|
|
6
|
+
* provider id, the id of its workspace, the id of its workspace's project, or one of its labels. An empty configuration
|
|
7
|
+
* means no agent. Agents that already exist are never changed, except that an agent chosen by label gets the tool in
|
|
8
|
+
* every interactive session it opens, resumed ones included.
|
|
9
|
+
*
|
|
10
|
+
* `agent.create` carries the provider and the working directory but no labels and no workspace id, so
|
|
11
|
+
* the workspace is found by matching the directory against the daemon's workspaces.
|
|
12
|
+
*/
|
|
13
|
+
import { defineSettings } from "@getpaseo/plugin";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
export const AGENT_ACCESS_MODES = ["all", "listed"] as const;
|
|
17
|
+
export type AgentAccessMode = (typeof AGENT_ACCESS_MODES)[number];
|
|
18
|
+
|
|
19
|
+
const MAX_ENTRIES = 200;
|
|
20
|
+
const MAX_ENTRY_LENGTH = 256;
|
|
21
|
+
const entries = z.array(z.string().min(1).max(MAX_ENTRY_LENGTH)).max(MAX_ENTRIES).default([]);
|
|
22
|
+
|
|
23
|
+
export const agentAccessSchema = z.object({
|
|
24
|
+
mode: z.enum(AGENT_ACCESS_MODES).default("all"),
|
|
25
|
+
providers: entries,
|
|
26
|
+
workspaces: entries,
|
|
27
|
+
projects: entries,
|
|
28
|
+
labels: entries,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export type AgentAccess = z.output<typeof agentAccessSchema>;
|
|
32
|
+
|
|
33
|
+
export const agentAccessSettings = defineSettings({
|
|
34
|
+
id: "agent-access",
|
|
35
|
+
scope: "host",
|
|
36
|
+
version: 1,
|
|
37
|
+
schema: agentAccessSchema,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** Used when the setting cannot be read or fails validation. */
|
|
41
|
+
export const DEFAULT_AGENT_ACCESS: AgentAccess = {
|
|
42
|
+
mode: "all",
|
|
43
|
+
providers: [],
|
|
44
|
+
workspaces: [],
|
|
45
|
+
projects: [],
|
|
46
|
+
labels: [],
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export function accessFromState(
|
|
50
|
+
state: { status: "ready"; values: AgentAccess } | { status: string } | undefined,
|
|
51
|
+
): AgentAccess {
|
|
52
|
+
return state?.status === "ready" && "values" in state ? state.values : DEFAULT_AGENT_ACCESS;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface NewAgent {
|
|
56
|
+
provider: string;
|
|
57
|
+
cwd: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface WorkspaceRef {
|
|
61
|
+
id: string;
|
|
62
|
+
projectId: string;
|
|
63
|
+
workspaceDirectory?: string | undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A directory in one lexical canonical form: backslashes become slashes, repeated slashes collapse, and
|
|
68
|
+
* `.` and `..` segments resolve. An absolute path (a leading slash, or a drive such as `C:`) cannot
|
|
69
|
+
* climb above its root, which is `/` or `C:/`. It is the same idea as posix normalize, written out because
|
|
70
|
+
* shared code does not import Node modules. Nothing on disk is consulted: symlinks are not resolved and
|
|
71
|
+
* letter case is not folded.
|
|
72
|
+
*/
|
|
73
|
+
export function normalizeDirectory(path: string): string {
|
|
74
|
+
const slashed = path.replace(/\\/g, "/");
|
|
75
|
+
const drive = /^[A-Za-z]:(?=\/|$)/.exec(slashed)?.[0] ?? "";
|
|
76
|
+
const rest = slashed.slice(drive.length);
|
|
77
|
+
const absolute = drive !== "" || rest.startsWith("/");
|
|
78
|
+
const kept: string[] = [];
|
|
79
|
+
for (const segment of rest.split("/")) {
|
|
80
|
+
if (segment === "" || segment === ".") continue;
|
|
81
|
+
if (segment !== "..") {
|
|
82
|
+
kept.push(segment);
|
|
83
|
+
} else if (kept.length > 0 && kept[kept.length - 1] !== "..") {
|
|
84
|
+
kept.pop();
|
|
85
|
+
} else if (!absolute) {
|
|
86
|
+
kept.push("..");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const joined = kept.join("/");
|
|
90
|
+
return absolute ? `${drive}/${joined}` : joined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** True for a normalized path that starts at a root (`/` or a drive such as `C:/`). A relative path never matches a workspace. */
|
|
94
|
+
export function isAbsoluteDirectory(normalized: string): boolean {
|
|
95
|
+
return normalized.startsWith("/") || /^[A-Za-z]:\//.test(normalized);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The workspaces whose directory is the agent's directory, or the deepest directory that contains it.
|
|
100
|
+
* Several workspaces can share one directory, so this returns all of them.
|
|
101
|
+
*/
|
|
102
|
+
export function workspacesForDirectory(
|
|
103
|
+
cwd: string,
|
|
104
|
+
workspaces: readonly WorkspaceRef[],
|
|
105
|
+
): WorkspaceRef[] {
|
|
106
|
+
const target = normalizeDirectory(cwd);
|
|
107
|
+
// Early exit only: a relative target can never equal or sit under an absolute workspace directory, so the loop below would find nothing.
|
|
108
|
+
if (!isAbsoluteDirectory(target)) return [];
|
|
109
|
+
let depth = -1;
|
|
110
|
+
let found: WorkspaceRef[] = [];
|
|
111
|
+
for (const workspace of workspaces) {
|
|
112
|
+
if (!workspace.workspaceDirectory) continue;
|
|
113
|
+
const directory = normalizeDirectory(workspace.workspaceDirectory);
|
|
114
|
+
if (!isAbsoluteDirectory(directory)) continue;
|
|
115
|
+
const prefix = directory.endsWith("/") ? directory : `${directory}/`;
|
|
116
|
+
if (target !== directory && !target.startsWith(prefix)) continue;
|
|
117
|
+
if (directory.length > depth) {
|
|
118
|
+
depth = directory.length;
|
|
119
|
+
found = [workspace];
|
|
120
|
+
} else if (directory.length === depth) {
|
|
121
|
+
found.push(workspace);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return found;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* True when a new agent gets the browser tool. `listWorkspaces` is called only when a workspace or
|
|
129
|
+
* project is listed and the provider did not already match; if it throws, those lists do not match.
|
|
130
|
+
*/
|
|
131
|
+
export async function agentGetsBrowser(
|
|
132
|
+
access: AgentAccess,
|
|
133
|
+
agent: NewAgent,
|
|
134
|
+
listWorkspaces: () => Promise<readonly WorkspaceRef[]>,
|
|
135
|
+
): Promise<boolean> {
|
|
136
|
+
if (access.mode === "all") return true;
|
|
137
|
+
if (access.providers.includes(agent.provider)) return true;
|
|
138
|
+
if (access.workspaces.length === 0 && access.projects.length === 0) return false;
|
|
139
|
+
try {
|
|
140
|
+
return workspacesForDirectory(agent.cwd, await listWorkspaces()).some(
|
|
141
|
+
(workspace) =>
|
|
142
|
+
access.workspaces.includes(workspace.id) || access.projects.includes(workspace.projectId),
|
|
143
|
+
);
|
|
144
|
+
} catch {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* True when one entry of `entries` matches the agent's labels. An entry is a label key (`team`: the agent has that
|
|
151
|
+
* label, whatever its value) or `key=value` (the agent has that label with exactly that value). Matching is exact and
|
|
152
|
+
* case-sensitive. An entry with an empty key never matches.
|
|
153
|
+
*/
|
|
154
|
+
export function labelMatches(
|
|
155
|
+
entries: readonly string[],
|
|
156
|
+
labels: Readonly<Record<string, string>>,
|
|
157
|
+
): boolean {
|
|
158
|
+
return entries.some((entry) => {
|
|
159
|
+
const separator = entry.indexOf("=");
|
|
160
|
+
const key = separator < 0 ? entry : entry.slice(0, separator);
|
|
161
|
+
if (key === "" || !Object.hasOwn(labels, key)) return false;
|
|
162
|
+
return separator < 0 || labels[key] === entry.slice(separator + 1);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* True when an agent gets the browser tool because of its labels. Labels are only on the session-open request, not on
|
|
168
|
+
* agent.create, so this runs there. It applies to the "listed" mode only; in "all" every new agent already has the tool.
|
|
169
|
+
*/
|
|
170
|
+
export function agentGetsBrowserByLabel(
|
|
171
|
+
access: AgentAccess,
|
|
172
|
+
labels: Readonly<Record<string, string>>,
|
|
173
|
+
): boolean {
|
|
174
|
+
return access.mode === "listed" && labelMatches(access.labels, labels);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const MAX_LIST_ENTRIES = MAX_ENTRIES;
|
|
178
|
+
export const MAX_LIST_ENTRY_LENGTH = MAX_ENTRY_LENGTH;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The comma or line separated names typed in the settings screen. Repeats are dropped. A name over the
|
|
182
|
+
* length limit or a list over the entry limit is reported in `error` instead of being cut off.
|
|
183
|
+
*/
|
|
184
|
+
export function parseEntries(text: string): { entries: string[]; error: string | null } {
|
|
185
|
+
const entries: string[] = [];
|
|
186
|
+
for (const piece of text.split(/[,\n]/)) {
|
|
187
|
+
const name = piece.trim();
|
|
188
|
+
if (name === "") continue;
|
|
189
|
+
if (name.length > MAX_ENTRY_LENGTH) {
|
|
190
|
+
return {
|
|
191
|
+
entries: [],
|
|
192
|
+
error: `An entry is longer than ${MAX_ENTRY_LENGTH} characters: ${name.slice(0, 24)}...`,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (!entries.includes(name)) entries.push(name);
|
|
196
|
+
}
|
|
197
|
+
if (entries.length > MAX_ENTRIES) {
|
|
198
|
+
return {
|
|
199
|
+
entries: [],
|
|
200
|
+
error: `A list can hold ${MAX_ENTRIES} entries; this one has ${entries.length}.`,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return { entries, error: null };
|
|
204
|
+
}
|