send-to-paseo 1.3.0
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/LICENSE +21 -0
- package/README.md +879 -0
- package/client/hosts.ts +70 -0
- package/client/settings.tsx +1231 -0
- package/index.client.tsx +23 -0
- package/index.server.ts +157 -0
- package/package.json +51 -0
- package/paseo-plugin.json +6 -0
- package/server/bridge.ts +725 -0
- package/server/cache.ts +23 -0
- package/server/daemon-password.ts +44 -0
- package/server/daemon.ts +287 -0
- package/server/deps.ts +432 -0
- package/server/gh.ts +864 -0
- package/server/git.ts +298 -0
- package/server/main-agent.ts +110 -0
- package/server/peers.ts +404 -0
- package/server/resolve.ts +1041 -0
- package/server/send.ts +463 -0
- package/server/settings.ts +288 -0
- package/server/target.ts +6 -0
- package/shared/contracts.ts +766 -0
- package/shared/host-target.ts +8 -0
package/server/cache.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Add one value while pruning expired entries and enforcing a hard key limit. */
|
|
2
|
+
export function setBoundedCache<Key, Value>(
|
|
3
|
+
cache: Map<Key, Value>,
|
|
4
|
+
key: Key,
|
|
5
|
+
value: Value,
|
|
6
|
+
options: {
|
|
7
|
+
maxEntries: number;
|
|
8
|
+
expired?: (value: Value) => boolean;
|
|
9
|
+
},
|
|
10
|
+
): void {
|
|
11
|
+
if (options.expired) {
|
|
12
|
+
for (const [candidate, cached] of cache) {
|
|
13
|
+
if (options.expired(cached)) cache.delete(candidate);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
cache.delete(key);
|
|
17
|
+
while (cache.size >= options.maxEntries) {
|
|
18
|
+
const oldest = cache.keys().next().value;
|
|
19
|
+
if (oldest === undefined) break;
|
|
20
|
+
cache.delete(oldest);
|
|
21
|
+
}
|
|
22
|
+
cache.set(key, value);
|
|
23
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
const PASSWORD_ENV_KEYS = ["SEND_TO_PASEO_DAEMON_PASSWORD", "PASEO_PASSWORD"] as const;
|
|
6
|
+
const PASSWORD_FILE_PARTS = ["paseo-hub", "secrets", "daemon-password"] as const;
|
|
7
|
+
|
|
8
|
+
interface PasswordSourceOptions {
|
|
9
|
+
env?: NodeJS.ProcessEnv;
|
|
10
|
+
home?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The VM convention for a daemon password shared with local automation. */
|
|
14
|
+
export function daemonPasswordFile(home: string = homedir()): string {
|
|
15
|
+
return join(home, ...PASSWORD_FILE_PARTS);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolve a daemon password without ever logging or returning source metadata.
|
|
20
|
+
*
|
|
21
|
+
* Explicit process configuration wins, followed by the VM secret file, then
|
|
22
|
+
* the plugin's existing settings value. Missing, unreadable and blank secret
|
|
23
|
+
* files are deliberately indistinguishable and fall through to settings.
|
|
24
|
+
*/
|
|
25
|
+
export async function resolveDaemonPassword(
|
|
26
|
+
readSettingsPassword: () => Promise<string | null | undefined>,
|
|
27
|
+
options: PasswordSourceOptions = {},
|
|
28
|
+
): Promise<string | undefined> {
|
|
29
|
+
const env = options.env ?? process.env;
|
|
30
|
+
for (const key of PASSWORD_ENV_KEYS) {
|
|
31
|
+
const value = env[key];
|
|
32
|
+
if (value !== undefined && value !== "") return value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const value = (await readFile(daemonPasswordFile(options.home), "utf8")).trim();
|
|
37
|
+
if (value !== "") return value;
|
|
38
|
+
} catch {
|
|
39
|
+
// A host without the VM secret convention keeps using plugin settings.
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const stored = await readSettingsPassword().catch(() => null);
|
|
43
|
+
return stored === null || stored === undefined || stored === "" ? undefined : stored;
|
|
44
|
+
}
|
package/server/daemon.ts
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import type { PluginHandlerContext } from "@getpaseo/plugin/server";
|
|
6
|
+
import { BridgeError } from "../shared/contracts";
|
|
7
|
+
import { resolveDaemonPassword } from "./daemon-password";
|
|
8
|
+
import { settings } from "./settings";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Short-lived Paseo SDK connections for the HTTP bridge.
|
|
12
|
+
*
|
|
13
|
+
* Only RPC handlers receive a `paseo` handle, and the bridge serves requests
|
|
14
|
+
* that arrive long before any surface is opened, so it opens its own SDK
|
|
15
|
+
* connection per request and closes it again. That is deliberate on two counts:
|
|
16
|
+
* a long-lived reconnecting socket in this subprocess would keep the event loop
|
|
17
|
+
* alive and hang Paseo's "Stopping plugin" step, and a per-request connection
|
|
18
|
+
* cannot go stale between sends.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const CONNECT_TIMEOUT_MS = 10_000;
|
|
22
|
+
const STATUS_TIMEOUT_MS = 2_000;
|
|
23
|
+
const STATUS_TTL_MS = 15_000;
|
|
24
|
+
|
|
25
|
+
function timeoutSignal(ms: number): AbortSignal {
|
|
26
|
+
return (
|
|
27
|
+
AbortSignal as typeof AbortSignal & {
|
|
28
|
+
timeout(milliseconds: number): AbortSignal;
|
|
29
|
+
}
|
|
30
|
+
).timeout(ms);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type PaseoApi = PluginHandlerContext["paseo"];
|
|
34
|
+
|
|
35
|
+
interface ClientModule {
|
|
36
|
+
createPaseoClient(options: {
|
|
37
|
+
url: string;
|
|
38
|
+
clientId: string;
|
|
39
|
+
reconnect: { enabled: boolean };
|
|
40
|
+
connectTimeoutMs: number;
|
|
41
|
+
suppressSendErrors: boolean;
|
|
42
|
+
password?: string;
|
|
43
|
+
}): PaseoApi & {
|
|
44
|
+
connect(): Promise<void>;
|
|
45
|
+
close(): Promise<void>;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let clientModule: ClientModule | null = null;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Borrows Paseo's own SDK from the host at runtime.
|
|
53
|
+
*
|
|
54
|
+
* The specifier is assembled rather than written as a literal so the plugin
|
|
55
|
+
* compiler cannot resolve it at build time. That keeps the plugin installable
|
|
56
|
+
* from a directory or from Git without a package manager step, and it removes
|
|
57
|
+
* any chance of a protocol version skew between a bundled copy of the SDK and
|
|
58
|
+
* the daemon actually running.
|
|
59
|
+
*/
|
|
60
|
+
function loadClientModule(): ClientModule {
|
|
61
|
+
if (clientModule !== null) return clientModule;
|
|
62
|
+
const specifier = ["@getpaseo", "client"].join("/");
|
|
63
|
+
// Paseo compiles plugin backends to CommonJS, so `require` is normally right
|
|
64
|
+
// here. `createRequire` keeps this working if a host ever loads the module as
|
|
65
|
+
// real ESM instead, where `require` is not defined.
|
|
66
|
+
const load: NodeRequire =
|
|
67
|
+
typeof require === "function" ? require : createRequire(join(process.cwd(), "noop.cjs"));
|
|
68
|
+
try {
|
|
69
|
+
clientModule = load(specifier) as ClientModule;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new BridgeError(
|
|
72
|
+
"daemon_unreachable",
|
|
73
|
+
`This Paseo host does not expose its client SDK (${specifier}), which the Send to Paseo bridge needs: ${
|
|
74
|
+
error instanceof Error ? error.message : String(error)
|
|
75
|
+
}`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return clientModule;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let cachedListen: string | null = null;
|
|
82
|
+
|
|
83
|
+
/** `host:port` the daemon listens on, from config or the documented default. */
|
|
84
|
+
async function resolveListen(): Promise<string> {
|
|
85
|
+
if (cachedListen !== null) return cachedListen;
|
|
86
|
+
const home = process.env.PASEO_HOME ?? join(homedir(), ".paseo");
|
|
87
|
+
let listen: string | undefined;
|
|
88
|
+
try {
|
|
89
|
+
const raw = JSON.parse(await readFile(join(home, "config.json"), "utf8")) as {
|
|
90
|
+
daemon?: { listen?: string };
|
|
91
|
+
};
|
|
92
|
+
listen = raw.daemon?.listen;
|
|
93
|
+
} catch {
|
|
94
|
+
// Fall through to the documented default.
|
|
95
|
+
}
|
|
96
|
+
cachedListen = listen ?? "127.0.0.1:6767";
|
|
97
|
+
return cachedListen;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function resolveUrl(): Promise<string> {
|
|
101
|
+
const fromEnv = process.env.PASEO_DAEMON_URL;
|
|
102
|
+
if (fromEnv !== undefined && fromEnv !== "") return fromEnv;
|
|
103
|
+
return `ws://${await resolveListen()}/ws`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The daemon password, when this daemon requires one.
|
|
108
|
+
*
|
|
109
|
+
* A daemon that listens on anything other than loopback turns on
|
|
110
|
+
* `daemon.auth.password`, and then it rejects an unauthenticated WebSocket with
|
|
111
|
+
* `Password required` and answers `/api/status` with `401` — so without this the
|
|
112
|
+
* plugin lists no providers, no modes, reports the daemon unreachable, and every
|
|
113
|
+
* send fails. Measured against a daemon on `0.0.0.0:6767`.
|
|
114
|
+
*
|
|
115
|
+
* It cannot be recovered from `config.json`: what is stored there is a bcrypt
|
|
116
|
+
* hash (`$2b$12$…`), by design. The plaintext has to be supplied. Four sources,
|
|
117
|
+
* in order:
|
|
118
|
+
*
|
|
119
|
+
* 1. `SEND_TO_PASEO_DAEMON_PASSWORD` — this plugin's own override.
|
|
120
|
+
* 2. `PASEO_PASSWORD` — the standard Paseo variable, the same one the `paseo`
|
|
121
|
+
* CLI reads. Honoured for the same reason `PASEO_HOME` and
|
|
122
|
+
* `PASEO_DAEMON_URL` are: a machine already configured for the CLI should
|
|
123
|
+
* not need configuring again for this.
|
|
124
|
+
* 3. `~/paseo-hub/secrets/daemon-password` — the Paseo VM convention.
|
|
125
|
+
* 4. `daemonPassword` in the plugin's own `settings.json`.
|
|
126
|
+
*
|
|
127
|
+
* The env vars are checked first, but the two files are reloadable: the plugin
|
|
128
|
+
* subprocess inherits the daemon's environment, which is fixed at daemon
|
|
129
|
+
* start, and restarting the daemon kills running agents. A file change is
|
|
130
|
+
* picked up by `paseo plugin reload`.
|
|
131
|
+
*
|
|
132
|
+
* Password values are never logged, echoed into an error, or included in a
|
|
133
|
+
* status payload — the same treatment as the pairing token.
|
|
134
|
+
*/
|
|
135
|
+
async function resolvePassword(): Promise<string | undefined> {
|
|
136
|
+
return resolveDaemonPassword(async () => (await settings.read()).daemonPassword);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Runs `work` against an SDK connection that is always closed before returning. */
|
|
140
|
+
export async function withPaseo<T>(work: (paseo: PaseoApi) => Promise<T>): Promise<T> {
|
|
141
|
+
const { createPaseoClient } = loadClientModule();
|
|
142
|
+
const password = await resolvePassword();
|
|
143
|
+
const client = createPaseoClient({
|
|
144
|
+
url: await resolveUrl(),
|
|
145
|
+
clientId: "send-to-paseo-bridge",
|
|
146
|
+
reconnect: { enabled: false },
|
|
147
|
+
connectTimeoutMs: CONNECT_TIMEOUT_MS,
|
|
148
|
+
suppressSendErrors: true,
|
|
149
|
+
// Omitted rather than passed as "" when there is none, so a daemon without
|
|
150
|
+
// auth is unaffected.
|
|
151
|
+
...(password === undefined ? {} : { password }),
|
|
152
|
+
});
|
|
153
|
+
try {
|
|
154
|
+
await client.connect();
|
|
155
|
+
} catch (error) {
|
|
156
|
+
await client.close().catch(() => undefined);
|
|
157
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
158
|
+
// A daemon that wants a password is REACHABLE and refusing, which is a
|
|
159
|
+
// different fix from "start Paseo". The hint has to say which.
|
|
160
|
+
if (/password/i.test(detail)) {
|
|
161
|
+
throw new BridgeError(
|
|
162
|
+
"daemon_unreachable",
|
|
163
|
+
"The Paseo daemon requires a password and the plugin does not have it.",
|
|
164
|
+
"Set SEND_TO_PASEO_DAEMON_PASSWORD, ~/paseo-hub/secrets/daemon-password, or daemonPassword in the plugin settings, then run: paseo plugin reload send-to-paseo",
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
throw new BridgeError(
|
|
168
|
+
"daemon_unreachable",
|
|
169
|
+
"The Paseo daemon is not reachable from the plugin.",
|
|
170
|
+
detail,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
return await work(client);
|
|
175
|
+
} finally {
|
|
176
|
+
// Teardown must not mask the original result or error.
|
|
177
|
+
await client.close().catch(() => undefined);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface DaemonStatus {
|
|
182
|
+
reachable: boolean;
|
|
183
|
+
version: string | null;
|
|
184
|
+
serverId: string | null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let statusCache: { at: number; status: DaemonStatus } | null = null;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* This machine's `serverId`, straight off disk.
|
|
191
|
+
*
|
|
192
|
+
* `$PASEO_HOME/server-id` is the daemon's own copy of the value agent deep links
|
|
193
|
+
* are keyed on, and reading it needs no daemon and no credential. That matters:
|
|
194
|
+
* `/api/status` is behind auth on a daemon that has a password, so a deep link
|
|
195
|
+
* used to be unbuildable there even though the id was sitting in a file the
|
|
196
|
+
* plugin can already read.
|
|
197
|
+
*
|
|
198
|
+
* Returns null rather than throwing; the caller falls back to `/api/status`.
|
|
199
|
+
*/
|
|
200
|
+
async function readServerIdFile(): Promise<string | null> {
|
|
201
|
+
const home = process.env.PASEO_HOME ?? join(homedir(), ".paseo");
|
|
202
|
+
try {
|
|
203
|
+
const raw = (await readFile(join(home, "server-id"), "utf8")).trim();
|
|
204
|
+
return raw === "" ? null : raw;
|
|
205
|
+
} catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Daemon identity over its plain HTTP status endpoint.
|
|
212
|
+
*
|
|
213
|
+
* `/api/status` is far cheaper than a WebSocket handshake, and `serverId` from
|
|
214
|
+
* it is what agent deep links are keyed on. Cached briefly because `/v1/ping`
|
|
215
|
+
* is polled by the extension's options page.
|
|
216
|
+
*
|
|
217
|
+
* Two things this deliberately gets right, both measured against a daemon with
|
|
218
|
+
* `daemon.auth.password` set (which is what any non-loopback `listen` turns on):
|
|
219
|
+
*
|
|
220
|
+
* - **`401` is not `unreachable`.** That endpoint requires auth on such a
|
|
221
|
+
* daemon, and reporting the daemon as down because we were refused is simply
|
|
222
|
+
* a false statement — the options page said "Paseo daemon unreachable" about
|
|
223
|
+
* a daemon that was running perfectly. `/api/health` is unauthenticated, so
|
|
224
|
+
* it is what liveness is actually decided by.
|
|
225
|
+
* - **`serverId` does not depend on being authorized**, because it is on disk.
|
|
226
|
+
*/
|
|
227
|
+
export async function readDaemonStatus(): Promise<DaemonStatus> {
|
|
228
|
+
if (statusCache !== null && Date.now() - statusCache.at < STATUS_TTL_MS) {
|
|
229
|
+
return statusCache.status;
|
|
230
|
+
}
|
|
231
|
+
const base = `http://${await resolveListen()}`;
|
|
232
|
+
let status: DaemonStatus = { reachable: false, version: null, serverId: null };
|
|
233
|
+
try {
|
|
234
|
+
const response = await fetch(`${base}/api/status`, {
|
|
235
|
+
signal: timeoutSignal(STATUS_TIMEOUT_MS),
|
|
236
|
+
});
|
|
237
|
+
if (response.ok) {
|
|
238
|
+
const body = (await response.json()) as { version?: unknown; serverId?: unknown };
|
|
239
|
+
status = {
|
|
240
|
+
reachable: true,
|
|
241
|
+
version: typeof body.version === "string" ? body.version : null,
|
|
242
|
+
serverId: typeof body.serverId === "string" ? body.serverId : null,
|
|
243
|
+
};
|
|
244
|
+
} else if (response.status === 401 || response.status === 403) {
|
|
245
|
+
// It answered. It just would not tell us. Liveness comes from the
|
|
246
|
+
// unauthenticated endpoint, and the id from disk.
|
|
247
|
+
const health = await fetch(`${base}/api/health`, {
|
|
248
|
+
signal: timeoutSignal(STATUS_TIMEOUT_MS),
|
|
249
|
+
}).catch(() => null);
|
|
250
|
+
status = {
|
|
251
|
+
reachable: health?.ok === true,
|
|
252
|
+
version: null,
|
|
253
|
+
serverId: await readServerIdFile(),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
} catch {
|
|
257
|
+
// Unreachable is a reported state, not an error: /v1/ping still returns 200.
|
|
258
|
+
}
|
|
259
|
+
if (status.serverId === null) status.serverId = await readServerIdFile();
|
|
260
|
+
statusCache = { at: Date.now(), status };
|
|
261
|
+
return status;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The daemon's `serverId`, which an agent deep link cannot be built without.
|
|
266
|
+
* Bypasses the cache when it is missing so a send does not fail just because a
|
|
267
|
+
* stale "unreachable" answer is still warm.
|
|
268
|
+
*/
|
|
269
|
+
export async function requireServerId(): Promise<string> {
|
|
270
|
+
let status = await readDaemonStatus();
|
|
271
|
+
if (status.serverId === null) {
|
|
272
|
+
statusCache = null;
|
|
273
|
+
status = await readDaemonStatus();
|
|
274
|
+
}
|
|
275
|
+
if (status.serverId === null) {
|
|
276
|
+
throw new BridgeError(
|
|
277
|
+
"daemon_unreachable",
|
|
278
|
+
"The Paseo daemon did not report a server id, so an agent link cannot be built.",
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
return status.serverId;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function clearDaemonCaches(): void {
|
|
285
|
+
statusCache = null;
|
|
286
|
+
cachedListen = null;
|
|
287
|
+
}
|