@eloxiv/daemon 0.0.0-stage → 0.1.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 +6 -2
- package/dist/Acp.d.ts +8 -0
- package/dist/Acp.d.ts.map +1 -0
- package/dist/Acp.js +268 -0
- package/dist/Acp.js.map +1 -0
- package/dist/Canonical.d.ts +3 -0
- package/dist/Canonical.d.ts.map +1 -0
- package/dist/Canonical.js +12 -0
- package/dist/Canonical.js.map +1 -0
- package/dist/Client.d.ts +100 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +474 -0
- package/dist/Client.js.map +1 -0
- package/dist/Commands.d.ts +85 -0
- package/dist/Commands.d.ts.map +1 -0
- package/dist/Commands.js +370 -0
- package/dist/Commands.js.map +1 -0
- package/dist/Config.d.ts +74 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +126 -0
- package/dist/Config.js.map +1 -0
- package/dist/Daemon.d.ts +243 -0
- package/dist/Daemon.d.ts.map +1 -0
- package/dist/Daemon.js +965 -0
- package/dist/Daemon.js.map +1 -0
- package/dist/Harness.d.ts +133 -0
- package/dist/Harness.d.ts.map +1 -0
- package/dist/Harness.js +734 -0
- package/dist/Harness.js.map +1 -0
- package/dist/Http.d.ts +15 -0
- package/dist/Http.d.ts.map +1 -0
- package/dist/Http.js +548 -0
- package/dist/Http.js.map +1 -0
- package/dist/KernelLink.d.ts +145 -0
- package/dist/KernelLink.d.ts.map +1 -0
- package/dist/KernelLink.js +109 -0
- package/dist/KernelLink.js.map +1 -0
- package/dist/LocalAuth.d.ts +99 -0
- package/dist/LocalAuth.d.ts.map +1 -0
- package/dist/LocalAuth.js +225 -0
- package/dist/LocalAuth.js.map +1 -0
- package/dist/Main.d.ts +51 -0
- package/dist/Main.d.ts.map +1 -0
- package/dist/Main.js +219 -0
- package/dist/Main.js.map +1 -0
- package/dist/Secrets.d.ts +21 -0
- package/dist/Secrets.d.ts.map +1 -0
- package/dist/Secrets.js +204 -0
- package/dist/Secrets.js.map +1 -0
- package/dist/SecureUrl.d.ts +11 -0
- package/dist/SecureUrl.d.ts.map +1 -0
- package/dist/SecureUrl.js +23 -0
- package/dist/SecureUrl.js.map +1 -0
- package/dist/State.d.ts +77 -0
- package/dist/State.d.ts.map +1 -0
- package/dist/State.js +38 -0
- package/dist/State.js.map +1 -0
- package/dist/Terminals.d.ts +49 -0
- package/dist/Terminals.d.ts.map +1 -0
- package/dist/Terminals.js +203 -0
- package/dist/Terminals.js.map +1 -0
- package/dist/bin-node.d.ts +2 -0
- package/dist/bin-node.d.ts.map +1 -0
- package/dist/bin-node.js +4 -0
- package/dist/bin-node.js.map +1 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +4 -0
- package/dist/bin.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/bun.d.ts +11 -0
- package/dist/runtime/bun.d.ts.map +1 -0
- package/dist/runtime/bun.js +20 -0
- package/dist/runtime/bun.js.map +1 -0
- package/dist/runtime/node.d.ts +11 -0
- package/dist/runtime/node.d.ts.map +1 -0
- package/dist/runtime/node.js +21 -0
- package/dist/runtime/node.js.map +1 -0
- package/dist/testing.d.ts +25 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +27 -0
- package/dist/testing.js.map +1 -0
- package/fixtures/claude-code-env-vars.txt +409 -0
- package/fixtures/fake-acp-agent.mjs +222 -0
- package/fixtures/fake-harness.mjs +299 -0
- package/fixtures/recorded/claude-code.jsonl +7 -0
- package/fixtures/recorded/codex.jsonl +6 -0
- package/package.json +64 -3
- package/scripts/build-sidecar.ts +86 -0
- package/src/Acp.ts +292 -0
- package/src/Canonical.ts +9 -0
- package/src/Client.ts +585 -0
- package/src/Commands.ts +427 -0
- package/src/Config.ts +179 -0
- package/src/Daemon.ts +1092 -0
- package/src/Harness.ts +839 -0
- package/src/Http.ts +563 -0
- package/src/KernelLink.ts +197 -0
- package/src/LocalAuth.ts +301 -0
- package/src/Main.ts +281 -0
- package/src/Secrets.ts +226 -0
- package/src/SecureUrl.ts +21 -0
- package/src/State.ts +109 -0
- package/src/Terminals.ts +227 -0
- package/src/bin-node.ts +4 -0
- package/src/bin.ts +4 -0
- package/src/index.ts +16 -0
- package/src/runtime/bun.ts +29 -0
- package/src/runtime/node.ts +30 -0
- package/src/testing.ts +43 -0
package/dist/Client.js
ADDED
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalClient v1 (decision 14; docs/contracts/local-client.md): the client core (`ClientRuntime`, from
|
|
3
|
+
* `@eloxiv/client/runtime`) served to native clients under `/client/v1` on the loopback relay, for an app that
|
|
4
|
+
* composed its sidecar with a `clientApp` (its sync schema, named queries, mutators and module contracts).
|
|
5
|
+
*
|
|
6
|
+
* The daemon holds a **person session** of its own, obtained by desktop sign-in with its own loopback callback
|
|
7
|
+
* (`/client/v1/sign-in/callback`) and kept in the secret store under its own key (`personSessionKey`), never in
|
|
8
|
+
* `state.json`; it is a second identity beside the device credential, which it never touches. Only the host and
|
|
9
|
+
* paired `desktop-app` clients may use it; no answer or event carries a kernel credential.
|
|
10
|
+
*/
|
|
11
|
+
import { ClientNotConfigured, Forbidden, InvalidInput, LOCAL_CLIENT_BASE, LOCAL_CLIENT_FEATURES, LOCAL_CLIENT_HEARTBEAT_MS, LOCAL_CLIENT_PROTOCOL, LOCAL_CLIENT_SIGN_IN_TTL_MS, LOCAL_CLIENT_VERSION, LocalClientActionCall, LocalClientError, LocalClientFollowRun, LocalClientSelectOrg, LocalClientSubscribe, LocalClientWrite, ProtocolVersion, Unauthenticated, } from "@eloxiv/contracts";
|
|
12
|
+
import { makeClientRuntime } from "@eloxiv/client/runtime";
|
|
13
|
+
import { sqliteStorage } from "@eloxiv/client/storage";
|
|
14
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
15
|
+
import { mkdirSync, rmSync } from "node:fs";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import { Schema } from "effect";
|
|
18
|
+
import { LocalAuthError } from "./LocalAuth.js";
|
|
19
|
+
import { secureUrl } from "./SecureUrl.js";
|
|
20
|
+
/** The reference web shell's approval page for a desktop sign-in (the Foldkit shell's `/desktop/sign-in`). */
|
|
21
|
+
const DESKTOP_SIGN_IN_PATH = "/desktop/sign-in";
|
|
22
|
+
/** The web shell's origin when nothing names it: its development server, a default trusted origin of the kernel. */
|
|
23
|
+
const DEFAULT_SHELL_URL = "http://localhost:5173";
|
|
24
|
+
/** The page a desktop sign-in opens: the app's `signInPage`, `ELOXIV_SIGN_IN_PAGE`, else the web shell's own. */
|
|
25
|
+
export const signInPageOf = (app, env) => app.signInPage ??
|
|
26
|
+
env["ELOXIV_SIGN_IN_PAGE"] ??
|
|
27
|
+
`${(app.shellUrl ?? env["ELOXIV_SHELL_URL"] ?? DEFAULT_SHELL_URL).replace(/\/+$/u, "")}${DESKTOP_SIGN_IN_PATH}`;
|
|
28
|
+
/** The secret-store key the person's session is kept under (per state directory, apart from the device credential). */
|
|
29
|
+
export const personSessionKey = (stateDir) => `person-session-${createHash("sha256").update(stateDir).digest("hex").slice(0, 16)}`;
|
|
30
|
+
/** Where the replica's SQLite files live (one per person and organisation). */
|
|
31
|
+
export const replicaDirFor = (stateDir) => join(stateDir, "replicas");
|
|
32
|
+
/** A failure on `/client/v1`: its status and its LocalClient error. */
|
|
33
|
+
export class ClientProblem extends Error {
|
|
34
|
+
status;
|
|
35
|
+
body;
|
|
36
|
+
constructor(status, body) {
|
|
37
|
+
super(body.message);
|
|
38
|
+
this.status = status;
|
|
39
|
+
this.body = body;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const STATUS = {
|
|
43
|
+
Unauthenticated: 401,
|
|
44
|
+
Forbidden: 403,
|
|
45
|
+
ApprovalRequired: 409,
|
|
46
|
+
ConsentRequired: 409,
|
|
47
|
+
IdempotencyConflict: 422,
|
|
48
|
+
InvalidInput: 400,
|
|
49
|
+
UnknownAction: 404,
|
|
50
|
+
ActionFailed: 500,
|
|
51
|
+
ClientOutdated: 426,
|
|
52
|
+
ClientNotConfigured: 404,
|
|
53
|
+
NotSignedIn: 409,
|
|
54
|
+
UnknownSubscription: 404,
|
|
55
|
+
UnknownQuery: 404,
|
|
56
|
+
UnknownMutator: 404,
|
|
57
|
+
StaleGeneration: 409,
|
|
58
|
+
SignInFailed: 502,
|
|
59
|
+
ProtocolVersion: 400,
|
|
60
|
+
};
|
|
61
|
+
const encodeError = Schema.encodeUnknownSync(LocalClientError);
|
|
62
|
+
/** A thrown value as a LocalClient refusal (`{ _tag, message, ...fields }`) with its status. */
|
|
63
|
+
export const problemOf = (error) => {
|
|
64
|
+
if (error instanceof ClientProblem)
|
|
65
|
+
return error;
|
|
66
|
+
const tag = error?._tag;
|
|
67
|
+
if (typeof tag === "string" && tag in STATUS) {
|
|
68
|
+
let body;
|
|
69
|
+
try {
|
|
70
|
+
body = encodeError(error);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
const message = error.message;
|
|
74
|
+
body = { ...error, _tag: tag, message: typeof message === "string" ? message : String(error) };
|
|
75
|
+
}
|
|
76
|
+
return new ClientProblem(STATUS[tag], body);
|
|
77
|
+
}
|
|
78
|
+
return new ClientProblem(500, { _tag: "ActionFailed", action: "client", message: "The daemon failed" });
|
|
79
|
+
};
|
|
80
|
+
/** The person's session in the daemon's secret store. */
|
|
81
|
+
const secretSessionStore = (secrets, key, onChange) => {
|
|
82
|
+
const store = {
|
|
83
|
+
load: async () => {
|
|
84
|
+
const token = await secrets.get(key);
|
|
85
|
+
onChange(token);
|
|
86
|
+
return token;
|
|
87
|
+
},
|
|
88
|
+
save: async (token) => {
|
|
89
|
+
await secrets.set(key, token);
|
|
90
|
+
onChange(token);
|
|
91
|
+
},
|
|
92
|
+
clear: async () => {
|
|
93
|
+
await secrets.delete(key);
|
|
94
|
+
onChange(undefined);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
return store;
|
|
98
|
+
};
|
|
99
|
+
const same = (a, b) => {
|
|
100
|
+
const left = createHash("sha256").update(a).digest();
|
|
101
|
+
const right = createHash("sha256").update(b).digest();
|
|
102
|
+
return timingSafeEqual(left, right);
|
|
103
|
+
};
|
|
104
|
+
const page = (title, text) => `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">` +
|
|
105
|
+
`<title>${title}</title></head><body style="font-family:system-ui,sans-serif;margin:3rem auto;max-width:28rem">` +
|
|
106
|
+
`<h1 style="font-size:1.25rem">${title}</h1><p>${text}</p></body></html>`;
|
|
107
|
+
const html = (status, title, text) => new Response(page(title, text), {
|
|
108
|
+
status,
|
|
109
|
+
headers: {
|
|
110
|
+
"content-type": "text/html; charset=utf-8",
|
|
111
|
+
"content-security-policy": "default-src 'none'; style-src 'unsafe-inline'",
|
|
112
|
+
"referrer-policy": "no-referrer",
|
|
113
|
+
"x-frame-options": "DENY",
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
const MAX_BODY = 1024 * 1024;
|
|
117
|
+
const readBody = async (request, schema) => {
|
|
118
|
+
const text = await request.text();
|
|
119
|
+
if (text.length > MAX_BODY)
|
|
120
|
+
throw new InvalidInput({ message: "The body is too large" });
|
|
121
|
+
let raw;
|
|
122
|
+
try {
|
|
123
|
+
raw = text.length === 0 ? {} : JSON.parse(text);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
throw new InvalidInput({ message: "Invalid JSON" });
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
return Schema.decodeUnknownSync(schema)(raw);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
throw new InvalidInput({ message: String(error) });
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
/** `/client/v1`, over one `ClientRuntime` for the daemon's person session. */
|
|
136
|
+
export class LocalClientService {
|
|
137
|
+
runtime;
|
|
138
|
+
kernelUrl;
|
|
139
|
+
#options;
|
|
140
|
+
#streams = new Map();
|
|
141
|
+
#personToken;
|
|
142
|
+
/** The device credential and any other the daemon holds, refreshed with each request (for the scrub). */
|
|
143
|
+
#credentials = [];
|
|
144
|
+
#pending;
|
|
145
|
+
#expiry;
|
|
146
|
+
#started;
|
|
147
|
+
constructor(options) {
|
|
148
|
+
this.#options = options;
|
|
149
|
+
const env = options.env ?? process.env;
|
|
150
|
+
const kernelUrl = options.app.kernelUrl ?? env["ELOXIV_KERNEL_URL"];
|
|
151
|
+
const zeroUrl = options.app.zeroUrl ?? env["ELOXIV_ZERO_URL"];
|
|
152
|
+
if (kernelUrl === undefined || zeroUrl === undefined) {
|
|
153
|
+
throw new Error("A clientApp needs its kernel and zero-cache URLs (kernelUrl, zeroUrl or ELOXIV_KERNEL_URL, ELOXIV_ZERO_URL)");
|
|
154
|
+
}
|
|
155
|
+
// Checked before the person session is loaded: it goes to both, so neither may be plain http off loopback.
|
|
156
|
+
this.kernelUrl = secureUrl(kernelUrl, "The kernel");
|
|
157
|
+
const zero = secureUrl(zeroUrl, "zero-cache");
|
|
158
|
+
let storage;
|
|
159
|
+
if (options.sqlite !== undefined) {
|
|
160
|
+
const directory = replicaDirFor(options.stateDir);
|
|
161
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
162
|
+
storage = sqliteStorage(options.sqlite, { directory, remove: (path) => rmSync(path, { force: true }) });
|
|
163
|
+
}
|
|
164
|
+
this.runtime = makeClientRuntime({
|
|
165
|
+
kernelUrl: this.kernelUrl,
|
|
166
|
+
zeroUrl: zero,
|
|
167
|
+
schema: options.app.schema,
|
|
168
|
+
queries: options.app.queries,
|
|
169
|
+
mutators: options.app.mutators,
|
|
170
|
+
modules: options.app.modules,
|
|
171
|
+
sessionStore: secretSessionStore(options.secrets, personSessionKey(options.stateDir), (token) => {
|
|
172
|
+
if (token !== undefined)
|
|
173
|
+
this.#personToken = token;
|
|
174
|
+
}),
|
|
175
|
+
storage: storage?.storage,
|
|
176
|
+
clientVersion: options.app.clientVersion,
|
|
177
|
+
log: (line) => options.log(`client: ${line}`),
|
|
178
|
+
});
|
|
179
|
+
this.runtime.onEvent((event) => this.#broadcast(event));
|
|
180
|
+
}
|
|
181
|
+
/** Check the stored person session (once, when the daemon is listening). */
|
|
182
|
+
start() {
|
|
183
|
+
this.#started ??= Promise.all([this.#refreshCredentials(), this.runtime.start()]).catch((error) => this.#options.log(`client: start failed: ${String(error)}`));
|
|
184
|
+
return this.#started;
|
|
185
|
+
}
|
|
186
|
+
async #refreshCredentials() {
|
|
187
|
+
this.#credentials = await this.#options.credentials();
|
|
188
|
+
}
|
|
189
|
+
async close() {
|
|
190
|
+
if (this.#expiry !== undefined)
|
|
191
|
+
clearTimeout(this.#expiry);
|
|
192
|
+
for (const stream of this.#streams.values())
|
|
193
|
+
stream.end();
|
|
194
|
+
this.#streams.clear();
|
|
195
|
+
await this.runtime.close();
|
|
196
|
+
}
|
|
197
|
+
/** Remove every credential from text going to a local client (defence in depth: none is expected). */
|
|
198
|
+
#scrub(text) {
|
|
199
|
+
let out = text;
|
|
200
|
+
for (const secret of [this.#personToken, ...this.#credentials]) {
|
|
201
|
+
if (secret !== undefined && secret.length > 0)
|
|
202
|
+
out = out.split(secret).join("[redacted]");
|
|
203
|
+
}
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
#broadcast(event) {
|
|
207
|
+
const { owner, ...rest } = event;
|
|
208
|
+
for (const stream of this.#streams.values()) {
|
|
209
|
+
if (owner !== undefined && owner !== stream.client)
|
|
210
|
+
continue;
|
|
211
|
+
stream.seq += 1;
|
|
212
|
+
stream.send(`data: ${this.#scrub(JSON.stringify({ seq: stream.seq, ...rest }))}\n\n`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
#json(body, status = 200) {
|
|
216
|
+
return new Response(this.#scrub(JSON.stringify(body)), {
|
|
217
|
+
status,
|
|
218
|
+
headers: { "content-type": "application/json", "cache-control": "no-store" },
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
/** A refusal as LocalClient answers it. */
|
|
222
|
+
refusal(error) {
|
|
223
|
+
const problem = problemOf(error);
|
|
224
|
+
if (problem.status === 500)
|
|
225
|
+
this.#options.log(`client: request failed: ${String(error)}`);
|
|
226
|
+
return this.#json(problem.body, problem.status);
|
|
227
|
+
}
|
|
228
|
+
/** The redirect the browser comes back to: the daemon's own callback, on 127.0.0.1 (the kernel accepts no other). */
|
|
229
|
+
#redirectUri = () => `http://127.0.0.1:${this.#options.port()}/client/v1/sign-in/callback`;
|
|
230
|
+
/** The sign-in waiting for `state`, if it is the one this daemon issued and has not expired. */
|
|
231
|
+
#pendingFor(state) {
|
|
232
|
+
const pending = this.#pending;
|
|
233
|
+
if (pending === undefined || pending.expiresAt <= Date.now() || state.length === 0)
|
|
234
|
+
return undefined;
|
|
235
|
+
return same(state, pending.login.state) ? pending : undefined;
|
|
236
|
+
}
|
|
237
|
+
/** The browser's redirect: state-checked, accepted once and within the sign-in's time; no local token. */
|
|
238
|
+
async callback(request) {
|
|
239
|
+
const url = new URL(request.url, "http://daemon");
|
|
240
|
+
const code = url.searchParams.get("code") ?? "";
|
|
241
|
+
const state = url.searchParams.get("state") ?? "";
|
|
242
|
+
const pending = this.#pendingFor(state);
|
|
243
|
+
if (pending === undefined || code.length === 0 || code.length > 512) {
|
|
244
|
+
return html(400, "This sign-in link is not valid", "Start signing in again from the app.");
|
|
245
|
+
}
|
|
246
|
+
// One use: forgotten before it is redeemed, so a second arrival (a reload, a replay) is refused.
|
|
247
|
+
this.#pending = undefined;
|
|
248
|
+
if (this.#expiry !== undefined)
|
|
249
|
+
clearTimeout(this.#expiry);
|
|
250
|
+
try {
|
|
251
|
+
await this.runtime.completeSignIn(pending.login, { code, state });
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
this.#options.log(`client: the sign-in failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
255
|
+
return html(502, "The sign-in did not complete", "Return to the app and try again.");
|
|
256
|
+
}
|
|
257
|
+
return html(200, "You are signed in", "You can return to the app.");
|
|
258
|
+
}
|
|
259
|
+
/** The routes under `/client/v1` (after the prefix), each for an authenticated host or desktop app. */
|
|
260
|
+
#routes = [
|
|
261
|
+
["GET", /^\/hello$/u, () => this.#json(this.#hello())],
|
|
262
|
+
["GET", /^\/session$/u, () => this.#json(this.runtime.session())],
|
|
263
|
+
["POST", /^\/sign-in$/u, (request) => this.#signIn(request)],
|
|
264
|
+
[
|
|
265
|
+
"POST",
|
|
266
|
+
/^\/org$/u,
|
|
267
|
+
async (request) => {
|
|
268
|
+
const input = await readBody(request, LocalClientSelectOrg);
|
|
269
|
+
return this.#json({ generation: await this.runtime.selectOrg(input.orgId) });
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
[
|
|
273
|
+
"POST",
|
|
274
|
+
/^\/sign-out$/u,
|
|
275
|
+
async (request) => {
|
|
276
|
+
await readBody(request, Schema.Struct({}));
|
|
277
|
+
this.#pending = undefined;
|
|
278
|
+
await this.runtime.signOut();
|
|
279
|
+
return this.#json({ ok: true });
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
["GET", /^\/events$/u, (request, owner) => this.#events(request, owner)],
|
|
283
|
+
[
|
|
284
|
+
"POST",
|
|
285
|
+
/^\/subscriptions$/u,
|
|
286
|
+
async (request, owner) => this.#json(await this.runtime.subscribe(await readBody(request, LocalClientSubscribe), owner)),
|
|
287
|
+
],
|
|
288
|
+
[
|
|
289
|
+
"DELETE",
|
|
290
|
+
/^\/subscriptions\/([^/]+)$/u,
|
|
291
|
+
(_request, owner, match) => {
|
|
292
|
+
this.runtime.unsubscribe(decodeURIComponent(match[1]), owner);
|
|
293
|
+
return this.#json({ ok: true });
|
|
294
|
+
},
|
|
295
|
+
],
|
|
296
|
+
[
|
|
297
|
+
"POST",
|
|
298
|
+
/^\/writes$/u,
|
|
299
|
+
async (request, owner) => this.#json(await this.runtime.write(await readBody(request, LocalClientWrite), owner)),
|
|
300
|
+
],
|
|
301
|
+
[
|
|
302
|
+
"POST",
|
|
303
|
+
/^\/runs$/u,
|
|
304
|
+
async (request, owner) => this.#json(await this.runtime.followRun(await readBody(request, LocalClientFollowRun), owner)),
|
|
305
|
+
],
|
|
306
|
+
[
|
|
307
|
+
"DELETE",
|
|
308
|
+
/^\/runs\/([^/]+)$/u,
|
|
309
|
+
(_request, owner, match) => {
|
|
310
|
+
this.runtime.unfollowRun(decodeURIComponent(match[1]), owner);
|
|
311
|
+
return this.#json({ ok: true });
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
["GET", /^\/agent\/readiness$/u, async () => this.#json(await this.runtime.agentReadiness())],
|
|
315
|
+
[
|
|
316
|
+
"POST",
|
|
317
|
+
/^\/actions\/([^/]+)$/u,
|
|
318
|
+
async (request, _owner, match) => {
|
|
319
|
+
const input = await readBody(request, LocalClientActionCall);
|
|
320
|
+
return this.#json(await this.runtime.call(decodeURIComponent(match[1]), input.input ?? {}, input.idempotencyKey, input.generation));
|
|
321
|
+
},
|
|
322
|
+
],
|
|
323
|
+
];
|
|
324
|
+
#hello() {
|
|
325
|
+
return {
|
|
326
|
+
protocol: LOCAL_CLIENT_PROTOCOL,
|
|
327
|
+
version: LOCAL_CLIENT_VERSION,
|
|
328
|
+
features: [...LOCAL_CLIENT_FEATURES],
|
|
329
|
+
app: this.runtime.app,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
/** Start a desktop sign-in: the callback is bound (this daemon's), its state kept for 60 s, once. */
|
|
333
|
+
async #signIn(request) {
|
|
334
|
+
await readBody(request, Schema.Struct({}));
|
|
335
|
+
const login = await this.runtime.beginSignIn(this.#redirectUri());
|
|
336
|
+
this.#pending = { login, expiresAt: Date.now() + LOCAL_CLIENT_SIGN_IN_TTL_MS };
|
|
337
|
+
if (this.#expiry !== undefined)
|
|
338
|
+
clearTimeout(this.#expiry);
|
|
339
|
+
this.#expiry = setTimeout(() => {
|
|
340
|
+
if (this.#pending?.login !== login)
|
|
341
|
+
return;
|
|
342
|
+
this.#pending = undefined;
|
|
343
|
+
this.runtime.cancelSignIn();
|
|
344
|
+
}, LOCAL_CLIENT_SIGN_IN_TTL_MS);
|
|
345
|
+
this.#expiry.unref?.();
|
|
346
|
+
const env = this.#options.env ?? process.env;
|
|
347
|
+
const pageUrl = signInPageOf(this.#options.app, env);
|
|
348
|
+
return this.#json({ url: `${pageUrl}${pageUrl.includes("?") ? "&" : "?"}${login.query}`, state: login.state });
|
|
349
|
+
}
|
|
350
|
+
/** Every other `/client/v1` route, for an authenticated client: only the host and desktop apps. */
|
|
351
|
+
async handle(request, path, who) {
|
|
352
|
+
if (who.kind !== "host" && who.kind !== "desktop-app") {
|
|
353
|
+
throw new Forbidden({ action: "client/v1", message: "Only the host and paired desktop apps use LocalClient" });
|
|
354
|
+
}
|
|
355
|
+
const asked = request.headers.get("x-eloxiv-local-client");
|
|
356
|
+
if (asked !== null && asked.split(".")[0] !== LOCAL_CLIENT_VERSION.split(".")[0]) {
|
|
357
|
+
throw new ProtocolVersion({
|
|
358
|
+
version: LOCAL_CLIENT_VERSION,
|
|
359
|
+
message: `This daemon speaks LocalClient ${LOCAL_CLIENT_VERSION}`,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
await this.start();
|
|
363
|
+
await this.#refreshCredentials();
|
|
364
|
+
const route = path.slice(LOCAL_CLIENT_BASE.length);
|
|
365
|
+
for (const [method, pattern, run] of this.#routes) {
|
|
366
|
+
const match = method === request.method ? pattern.exec(route) : null;
|
|
367
|
+
if (match !== null)
|
|
368
|
+
return run(request, who.id, match);
|
|
369
|
+
}
|
|
370
|
+
throw new ClientProblem(404, {
|
|
371
|
+
_tag: "UnknownAction",
|
|
372
|
+
action: route,
|
|
373
|
+
message: `No route ${request.method} ${path}`,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
/** The client's event stream: one per client (a new one closes the old); closing it ends its subscriptions. */
|
|
377
|
+
#events(request, owner) {
|
|
378
|
+
this.#streams.get(owner)?.end();
|
|
379
|
+
const encoder = new TextEncoder();
|
|
380
|
+
let heartbeat;
|
|
381
|
+
let stream;
|
|
382
|
+
const finish = () => {
|
|
383
|
+
if (heartbeat !== undefined)
|
|
384
|
+
clearInterval(heartbeat);
|
|
385
|
+
heartbeat = undefined;
|
|
386
|
+
if (stream !== undefined && this.#streams.get(owner) === stream) {
|
|
387
|
+
this.#streams.delete(owner);
|
|
388
|
+
this.runtime.unsubscribeAll(owner);
|
|
389
|
+
this.runtime.unfollowAll(owner);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
const body = new ReadableStream({
|
|
393
|
+
start: (sink) => {
|
|
394
|
+
let open = true;
|
|
395
|
+
stream = {
|
|
396
|
+
client: owner,
|
|
397
|
+
seq: 0,
|
|
398
|
+
send: (text) => {
|
|
399
|
+
if (!open)
|
|
400
|
+
return;
|
|
401
|
+
try {
|
|
402
|
+
sink.enqueue(encoder.encode(text));
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
open = false;
|
|
406
|
+
finish();
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
end: () => {
|
|
410
|
+
if (!open)
|
|
411
|
+
return;
|
|
412
|
+
open = false;
|
|
413
|
+
finish();
|
|
414
|
+
try {
|
|
415
|
+
sink.close();
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
// Already closed.
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
};
|
|
422
|
+
this.#streams.set(owner, stream);
|
|
423
|
+
// The stream opens with the current session, so a client starts from it.
|
|
424
|
+
stream.seq += 1;
|
|
425
|
+
const first = this.#scrub(JSON.stringify({ seq: stream.seq, type: "session", session: this.runtime.session() }));
|
|
426
|
+
stream.send(`retry: 2000\ndata: ${first}\n\n`);
|
|
427
|
+
heartbeat = setInterval(() => stream?.send(": heartbeat\n\n"), LOCAL_CLIENT_HEARTBEAT_MS);
|
|
428
|
+
heartbeat.unref?.();
|
|
429
|
+
request.signal?.addEventListener("abort", () => stream?.end(), { once: true });
|
|
430
|
+
},
|
|
431
|
+
cancel: () => stream?.end(),
|
|
432
|
+
});
|
|
433
|
+
return new Response(body, {
|
|
434
|
+
headers: { "content-type": "text/event-stream", "cache-control": "no-store", "x-accel-buffering": "no" },
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/** `/client/v1` on a daemon without a `clientApp`. */
|
|
439
|
+
const notConfigured = () => new ClientNotConfigured({
|
|
440
|
+
message: "This daemon was not composed with an app (clientApp): LocalClient is not served",
|
|
441
|
+
});
|
|
442
|
+
/** Whether `path` is one of LocalClient's (under `/client/v1`). */
|
|
443
|
+
export const isClientPath = (path) => path === LOCAL_CLIENT_BASE || path.startsWith(`${LOCAL_CLIENT_BASE}/`);
|
|
444
|
+
/** The one browser-facing route: no token, no Origin check, checked by its state. */
|
|
445
|
+
export const CLIENT_CALLBACK = `${LOCAL_CLIENT_BASE}/sign-in/callback`;
|
|
446
|
+
/** A refusal on `/client/v1` before the service answers (no app, the Host or Origin, the token): `{ _tag, message }`. */
|
|
447
|
+
export const clientRefusal = (error) => {
|
|
448
|
+
const problem = problemOf(error instanceof LocalAuthError
|
|
449
|
+
? error.status === 401
|
|
450
|
+
? new Unauthenticated({ message: error.message })
|
|
451
|
+
: { _tag: error.status === 403 ? "Forbidden" : "InvalidInput", action: "client/v1", message: error.message }
|
|
452
|
+
: error);
|
|
453
|
+
return Response.json(problem.body, {
|
|
454
|
+
status: problem.status,
|
|
455
|
+
headers: problem.status === 401 ? { "www-authenticate": "Bearer" } : {},
|
|
456
|
+
});
|
|
457
|
+
};
|
|
458
|
+
/**
|
|
459
|
+
* Serve one `/client/v1` request: `404 ClientNotConfigured` without a service; the callback without a token;
|
|
460
|
+
* everything else for the client `authenticate` names (it throws for a missing or unknown token).
|
|
461
|
+
*/
|
|
462
|
+
export const serveClient = async (service, request, path, authenticate) => {
|
|
463
|
+
try {
|
|
464
|
+
if (service === undefined)
|
|
465
|
+
throw notConfigured();
|
|
466
|
+
if (request.method === "GET" && path === CLIENT_CALLBACK)
|
|
467
|
+
return await service.callback(request);
|
|
468
|
+
return await service.handle(request, path, authenticate());
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
return service === undefined || error instanceof LocalAuthError ? clientRefusal(error) : service.refusal(error);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
//# sourceMappingURL=Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.js","sourceRoot":"","sources":["../src/Client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,mBAAmB,EACnB,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,qBAAqB,EACrB,yBAAyB,EACzB,qBAAqB,EACrB,2BAA2B,EAC3B,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,EACf,eAAe,GAEhB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,iBAAiB,EAAyC,MAAM,wBAAwB,CAAA;AACjG,OAAO,EAAE,aAAa,EAA2B,MAAM,wBAAwB,CAAA;AAC/E,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,cAAc,EAA4B,MAAM,gBAAgB,CAAA;AAEzE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AA6B1C,8GAA8G;AAC9G,MAAM,oBAAoB,GAAG,kBAAkB,CAAA;AAC/C,oHAAoH;AACpH,MAAM,iBAAiB,GAA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E,KAAK,CAAC,OAAO,EAAE;QAC9G,CAAC,CAAC,KAAK,CACV,CAAA;IACD,OAAO,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;QACjC,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,OAAO,EAAE,OAAO,CAAC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE;KACxE,CAAC,CAAA;AACJ,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,OAAuC,EACvC,OAAgB,EAChB,IAAY,EACZ,YAAuC,EACpB,EAAE;IACrB,IAAI,CAAC;QACH,IAAI,OAAO,KAAK,SAAS;YAAE,MAAM,aAAa,EAAE,CAAA;QAChD,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,IAAI,IAAI,KAAK,eAAe;YAAE,OAAO,MAAM,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;QAChG,OAAO,MAAM,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC,CAAA;IAC5D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,OAAO,KAAK,SAAS,IAAI,KAAK,YAAY,cAAc,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IACjH,CAAC;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device commands (DeviceControl): one operation a person, or their agent with consent, asks a paired device to
|
|
3
|
+
* run. The daemon serves the **terminal** (`terminal.exec`: one shell command) and the **files** of its workspace
|
|
4
|
+
* (`files.read`, `files.write`, `files.edit`, `files.list`, `files.search`) itself; **browser** and **computer**
|
|
5
|
+
* operations go to a companion, a local client that paired with the daemon as a `browser-extension` or a
|
|
6
|
+
* `computer-use` client and asks for work (`CompanionQueue`).
|
|
7
|
+
*
|
|
8
|
+
* Every kind is off unless the daemon was started with it (`--allow-commands`). Paths are relative to the
|
|
9
|
+
* workspace and never leave it (symbolic links included), nor reach the state directory. A shell command runs
|
|
10
|
+
* as the daemon's OS user, in the workspace, with the harnesses' environment allow-list (no credentials), in its
|
|
11
|
+
* own process group, killed at its deadline; its output is capped.
|
|
12
|
+
*/
|
|
13
|
+
import type { ClaimedCommand, CommandKind, CommandOperation } from "@eloxiv/contracts";
|
|
14
|
+
/** A command's answer: what goes back to the kernel (and from there to whoever asked). */
|
|
15
|
+
export interface CommandOutcome {
|
|
16
|
+
/** `cancelled`: the device did not act (the command was cancelled before it ran). */
|
|
17
|
+
readonly outcome: "succeeded" | "failed" | "cancelled";
|
|
18
|
+
readonly result: unknown;
|
|
19
|
+
readonly error: string | null;
|
|
20
|
+
}
|
|
21
|
+
/** The most of a command's stdout and of its stderr kept (characters each). */
|
|
22
|
+
export declare const MAX_OUTPUT: number;
|
|
23
|
+
/** The largest file `files.read` returns, and `files.search` looks into (bytes). */
|
|
24
|
+
export declare const MAX_FILE: number;
|
|
25
|
+
export declare const confine: (workspace: string, stateDir: string, path: string | undefined) => string;
|
|
26
|
+
/** What a cancellation that stopped a running command says: it was stopped, and nothing it did is undone. */
|
|
27
|
+
export declare const INTERRUPTED = "Cancelled while it ran: the command was stopped; whatever it did before it stopped was not undone";
|
|
28
|
+
/**
|
|
29
|
+
* Run a terminal or files command in `workspace` (the kernel validated its input against the operation's schema;
|
|
30
|
+
* it is checked again here, defensively, by what each operation reads). Never throws.
|
|
31
|
+
*/
|
|
32
|
+
export declare const runLocalCommand: (command: ClaimedCommand, context: {
|
|
33
|
+
readonly workspace: string;
|
|
34
|
+
readonly stateDir: string;
|
|
35
|
+
readonly signal: AbortSignal;
|
|
36
|
+
/** Aborted when the command is cancelled while it runs: a shell command is stopped. */
|
|
37
|
+
readonly cancel?: AbortSignal | undefined;
|
|
38
|
+
}) => Promise<CommandOutcome>;
|
|
39
|
+
/** The local client kind that serves each companion command kind. */
|
|
40
|
+
export declare const COMPANION_KIND: {
|
|
41
|
+
readonly browser: "browser-extension";
|
|
42
|
+
readonly computer: "computer-use";
|
|
43
|
+
};
|
|
44
|
+
export type CompanionCommandKind = keyof typeof COMPANION_KIND;
|
|
45
|
+
/** How long after its last request a companion still counts as connected (ms). */
|
|
46
|
+
export declare const COMPANION_PRESENCE_MS = 35000;
|
|
47
|
+
/**
|
|
48
|
+
* Browser and computer commands, waiting for their companion: a companion asks for work (`next`, a long poll),
|
|
49
|
+
* gets the oldest command of its kind, and reports it (`settle`); only the companion that took a command may
|
|
50
|
+
* report it. A command no companion took or reported before its deadline fails.
|
|
51
|
+
*/
|
|
52
|
+
export declare class CompanionQueue {
|
|
53
|
+
#private;
|
|
54
|
+
constructor(onChange: () => void);
|
|
55
|
+
/** Is a companion of `kind` connected (it asked for work recently)? */
|
|
56
|
+
connected(kind: CompanionCommandKind, now?: number): boolean;
|
|
57
|
+
/** The companions connected now, for the device's sessions. */
|
|
58
|
+
companions(now?: number): ({
|
|
59
|
+
client: string;
|
|
60
|
+
} & {
|
|
61
|
+
kind: CompanionCommandKind;
|
|
62
|
+
at: number;
|
|
63
|
+
origin: string | null;
|
|
64
|
+
name: string;
|
|
65
|
+
})[];
|
|
66
|
+
/**
|
|
67
|
+
* Hand `command` to a companion of `kind`, and wait for its report (or the deadline). Cancelled (`cancel`) before a
|
|
68
|
+
* companion took it: it is withdrawn and never runs. Once taken it runs to its end: the companion's report stands.
|
|
69
|
+
*/
|
|
70
|
+
run(kind: CompanionCommandKind, command: ClaimedCommand, signal: AbortSignal, cancel?: AbortSignal): Promise<CommandOutcome>;
|
|
71
|
+
/** A companion asks for work: the oldest command of its kind, waiting up to `waitMs` for one. */
|
|
72
|
+
next(client: {
|
|
73
|
+
readonly id: string;
|
|
74
|
+
readonly name: string;
|
|
75
|
+
}, kind: CompanionCommandKind, waitMs: number, origin: string | null): Promise<ClaimedCommand | null>;
|
|
76
|
+
/** A companion reports a command it took. False when it holds no such command. */
|
|
77
|
+
settle(clientId: string, commandId: string, outcome: CommandOutcome): boolean;
|
|
78
|
+
/** Forget companions that stopped asking (called when sessions are reported). */
|
|
79
|
+
prune(now?: number): void;
|
|
80
|
+
}
|
|
81
|
+
/** The kind a local client serves as a companion, if any. */
|
|
82
|
+
export declare const companionKindOf: (clientKind: string) => CompanionCommandKind | undefined;
|
|
83
|
+
export declare const isLocalKind: (kind: CommandKind) => kind is "terminal" | "files";
|
|
84
|
+
export type { CommandOperation };
|
|
85
|
+
//# sourceMappingURL=Commands.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Commands.d.ts","sourceRoot":"","sources":["../src/Commands.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAetF,0FAA0F;AAC1F,MAAM,WAAW,cAAc;IAC7B,qFAAqF;IACrF,QAAQ,CAAC,OAAO,EAAE,WAAW,GAAG,QAAQ,GAAG,WAAW,CAAA;IACtD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CAC9B;AAKD,+EAA+E;AAC/E,eAAO,MAAM,UAAU,QAAY,CAAA;AACnC,oFAAoF;AACpF,eAAO,MAAM,QAAQ,QAAc,CAAA;AA4BnC,eAAO,MAAM,OAAO,cAAe,MAAM,YAAY,MAAM,QAAQ,MAAM,GAAG,SAAS,KAAG,MAoBvF,CAAA;AAID,6GAA6G;AAC7G,eAAO,MAAM,WAAW,sGAC6E,CAAA;AA4FrG;;;GAGG;AACH,eAAO,MAAM,eAAe,YACjB,cAAc,WACd;IACP,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MA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