@x-tech-laboratories/x-app 0.129.0 → 0.131.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/README.md +12 -3
- package/dist/agent/claude_stream.d.ts +1 -22
- package/dist/agent/claude_stream.js +2 -85
- package/dist/agent/client.d.ts +1 -35
- package/dist/agent/client.js +2 -169
- package/dist/agent/commands.d.ts +1 -22
- package/dist/agent/commands.js +2 -80
- package/dist/agent/desktop_notify.d.ts +1 -54
- package/dist/agent/desktop_notify.js +2 -229
- package/dist/agent/firefox_bookmarks.d.ts +1 -29
- package/dist/agent/firefox_bookmarks.js +2 -147
- package/dist/agent/git.d.ts +1 -37
- package/dist/agent/git.js +2 -121
- package/dist/agent/github.d.ts +1 -81
- package/dist/agent/github.js +2 -185
- package/dist/agent/inhibit.d.ts +1 -32
- package/dist/agent/inhibit.js +2 -141
- package/dist/agent/keys.d.ts +1 -26
- package/dist/agent/keys.js +2 -161
- package/dist/agent/local_socket.d.ts +1 -18
- package/dist/agent/local_socket.js +2 -106
- package/dist/agent/long_runs.d.ts +1 -45
- package/dist/agent/long_runs.js +2 -419
- package/dist/agent/programs.d.ts +1 -60
- package/dist/agent/programs.js +2 -237
- package/dist/agent/protocol.d.ts +1 -172
- package/dist/agent/protocol.js +2 -249
- package/dist/agent/scheduled_notices.d.ts +1 -21
- package/dist/agent/scheduled_notices.js +2 -38
- package/dist/agent/session.d.ts +1 -61
- package/dist/agent/session.js +2 -335
- package/dist/agent/ssh_config.d.ts +1 -12
- package/dist/agent/ssh_config.js +2 -72
- package/dist/agent/stalls.d.ts +1 -44
- package/dist/agent/stalls.js +2 -176
- package/dist/agent/tasks.d.ts +1 -17
- package/dist/agent/tasks.js +2 -15
- package/dist/agent/wake.d.ts +1 -14
- package/dist/agent/wake.js +2 -88
- package/dist/agent/worktree_files.d.ts +1 -20
- package/dist/agent/worktree_files.js +2 -69
- package/dist/app/define.d.ts +3 -0
- package/dist/app/desktop.js +21 -8
- package/dist/app/server.js +1 -0
- package/dist/daemon_link.d.ts +2 -1
- package/dist/daemon_link.js +4 -2
- package/dist/durations.d.ts +1 -15
- package/dist/durations.js +2 -52
- package/package.json +5 -3
package/dist/agent/programs.js
CHANGED
|
@@ -1,237 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
|
-
import { constants as fs_constants } from "node:fs";
|
|
6
|
-
import { access } from "node:fs/promises";
|
|
7
|
-
import { constants } from "node:os";
|
|
8
|
-
import { StringDecoder } from "node:string_decoder";
|
|
9
|
-
// ── Settings ─────────────────────────────────────────────────────────────────
|
|
10
|
-
// Give a stopped program this long to exit before it is killed outright.
|
|
11
|
-
export const kill_grace_in_milliseconds = 5000;
|
|
12
|
-
// A run keeps at most this much stdout, since it travels whole in one message.
|
|
13
|
-
export const stdout_limit_in_bytes = 32 * 1024 * 1024;
|
|
14
|
-
// Stderr is only ever an error message, so this much is plenty.
|
|
15
|
-
export const stderr_limit_in_bytes = 64 * 1024;
|
|
16
|
-
// The exit code a program that is not installed answers with, as a shell does.
|
|
17
|
-
export const not_installed_exit_code = 127;
|
|
18
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
19
|
-
// Sends the signal to a process group, which may have ended already.
|
|
20
|
-
export function signal_group(pid, signal) {
|
|
21
|
-
if (pid === undefined)
|
|
22
|
-
return;
|
|
23
|
-
try {
|
|
24
|
-
process.kill(-pid, signal);
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
// The process group is already gone.
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
// Collects chunks up to a size, noting whether any were dropped.
|
|
31
|
-
export class BoundedBuffer {
|
|
32
|
-
chunks = [];
|
|
33
|
-
size = 0;
|
|
34
|
-
truncated = false;
|
|
35
|
-
limit;
|
|
36
|
-
constructor(limit) {
|
|
37
|
-
this.limit = limit;
|
|
38
|
-
}
|
|
39
|
-
write(chunk) {
|
|
40
|
-
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
41
|
-
const room = this.limit - this.size;
|
|
42
|
-
if (room <= 0) {
|
|
43
|
-
this.truncated = bytes.length > 0 || this.truncated;
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
const kept = bytes.length > room ? bytes.subarray(0, room) : bytes;
|
|
47
|
-
if (kept !== bytes)
|
|
48
|
-
this.truncated = true;
|
|
49
|
-
this.chunks.push(kept);
|
|
50
|
-
this.size += kept.length;
|
|
51
|
-
}
|
|
52
|
-
text() {
|
|
53
|
-
return Buffer.concat(this.chunks).toString("utf8");
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const shows = (use) => use === "show" || use === "both";
|
|
57
|
-
const keeps = (use) => use === "keep" || use === "both";
|
|
58
|
-
// Where a program is on the PATH, or null when it is not there.
|
|
59
|
-
export async function on_path(name) {
|
|
60
|
-
const folders = name.includes("/") ? [""] : (process.env.PATH ?? "").split(":").filter(Boolean);
|
|
61
|
-
for (const folder of folders) {
|
|
62
|
-
const path = folder === "" ? name : `${folder}/${name}`;
|
|
63
|
-
try {
|
|
64
|
-
await access(path, fs_constants.X_OK);
|
|
65
|
-
return path;
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
// Not in this folder.
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
// Passes on only whole lines, so programs printing side by side never split each other's; flush passes on the rest.
|
|
74
|
-
export function whole_lines(sink) {
|
|
75
|
-
const decoder = new StringDecoder("utf8");
|
|
76
|
-
let carry = "";
|
|
77
|
-
return {
|
|
78
|
-
write(chunk) {
|
|
79
|
-
const text = carry + (typeof chunk === "string" ? chunk : decoder.write(chunk));
|
|
80
|
-
const end = text.lastIndexOf("\n") + 1;
|
|
81
|
-
carry = text.slice(end);
|
|
82
|
-
if (end > 0)
|
|
83
|
-
sink.write(text.slice(0, end));
|
|
84
|
-
},
|
|
85
|
-
flush() {
|
|
86
|
-
const rest = carry + decoder.end();
|
|
87
|
-
carry = "";
|
|
88
|
-
if (rest !== "")
|
|
89
|
-
sink.write(`${rest}\n`);
|
|
90
|
-
},
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
// ── The computer's programs ──────────────────────────────────────────────────
|
|
94
|
-
// Whether this process runs in a Flatpak's sandbox, which has its own programs rather than the computer's.
|
|
95
|
-
export function in_flatpak(variables = process.env) {
|
|
96
|
-
return Boolean(variables.FLATPAK_ID);
|
|
97
|
-
}
|
|
98
|
-
// The command that runs a program on the computer itself: through flatpak-spawn --host from inside a Flatpak, in the
|
|
99
|
-
// folder given, which the Flatpak needs --talk-name=org.freedesktop.Flatpak for; the program as given otherwise.
|
|
100
|
-
export function host_argv(argv, cwd, variables = process.env) {
|
|
101
|
-
if (!in_flatpak(variables))
|
|
102
|
-
return [...argv];
|
|
103
|
-
return ["flatpak-spawn", "--host", "--watch-bus", ...(cwd ? [`--directory=${cwd}`] : []), ...argv];
|
|
104
|
-
}
|
|
105
|
-
// What to start, and where: the program as asked, or flatpak-spawn starting it on the computer, which takes the folder itself.
|
|
106
|
-
function spawned(argv, on_host, cwd) {
|
|
107
|
-
if (!on_host || !in_flatpak())
|
|
108
|
-
return { command: [...argv], cwd };
|
|
109
|
-
return { command: host_argv(argv, cwd), cwd: undefined };
|
|
110
|
-
}
|
|
111
|
-
// ── Running ──────────────────────────────────────────────────────────────────
|
|
112
|
-
// Runs the program to its end and never rejects: a program that cannot start answers 127 or -1 with why.
|
|
113
|
-
export function run_program(argv, options = {}) {
|
|
114
|
-
return new Promise((resolve) => {
|
|
115
|
-
const stdout_use = options.stdout ?? "keep";
|
|
116
|
-
const stderr_use = options.stderr ?? "keep";
|
|
117
|
-
if (options.signal?.aborted) {
|
|
118
|
-
resolve({ exit_code: -1, stdout: "", stderr: "Stopped before it started" });
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
const start = spawned(argv, options.on_host, options.cwd);
|
|
122
|
-
const child = spawn(start.command[0], start.command.slice(1), {
|
|
123
|
-
detached: options.own_group ?? false,
|
|
124
|
-
stdio: [options.stdin == null ? "ignore" : "pipe", "pipe", "pipe"],
|
|
125
|
-
env: options.env,
|
|
126
|
-
cwd: start.cwd,
|
|
127
|
-
});
|
|
128
|
-
if (options.stdin != null) {
|
|
129
|
-
child.stdin.on("error", () => { });
|
|
130
|
-
child.stdin.end(options.stdin);
|
|
131
|
-
}
|
|
132
|
-
const stdout = new BoundedBuffer(stdout_limit_in_bytes);
|
|
133
|
-
const stderr = new BoundedBuffer(stderr_limit_in_bytes);
|
|
134
|
-
child.stdout.on("data", (chunk) => {
|
|
135
|
-
if (shows(stdout_use))
|
|
136
|
-
options.out?.write(chunk);
|
|
137
|
-
if (keeps(stdout_use))
|
|
138
|
-
stdout.write(chunk);
|
|
139
|
-
});
|
|
140
|
-
child.stderr.on("data", (chunk) => {
|
|
141
|
-
if (shows(stderr_use))
|
|
142
|
-
options.err?.write(chunk);
|
|
143
|
-
if (keeps(stderr_use))
|
|
144
|
-
stderr.write(chunk);
|
|
145
|
-
});
|
|
146
|
-
let kill_timer;
|
|
147
|
-
const stop = () => {
|
|
148
|
-
const signal = (name) => (options.own_group ? signal_group(child.pid, name) : child.kill(name));
|
|
149
|
-
signal("SIGTERM");
|
|
150
|
-
kill_timer = setTimeout(() => signal("SIGKILL"), kill_grace_in_milliseconds);
|
|
151
|
-
};
|
|
152
|
-
options.signal?.addEventListener("abort", stop, { once: true });
|
|
153
|
-
let settled = false;
|
|
154
|
-
const settle = (result) => {
|
|
155
|
-
if (settled)
|
|
156
|
-
return;
|
|
157
|
-
settled = true;
|
|
158
|
-
clearTimeout(kill_timer);
|
|
159
|
-
options.signal?.removeEventListener("abort", stop);
|
|
160
|
-
resolve(result);
|
|
161
|
-
};
|
|
162
|
-
child.on("error", (error) => {
|
|
163
|
-
const missing = error.code === "ENOENT";
|
|
164
|
-
const message = missing ? `${start.command[0]} is not installed on this host` : `Could not start ${start.command[0]}: ${error.message}`;
|
|
165
|
-
if (shows(stderr_use))
|
|
166
|
-
options.err?.write(`${message}\n`);
|
|
167
|
-
settle({ exit_code: missing ? not_installed_exit_code : -1, stdout: "", stderr: message });
|
|
168
|
-
});
|
|
169
|
-
child.on("close", (code, signal) => {
|
|
170
|
-
const exit_code = code ?? 128 + (signal === null ? 0 : constants.signals[signal]);
|
|
171
|
-
settle({ exit_code, stdout: stdout.text(), stderr: stderr.text() });
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
// Runs a program that talks in lines both ways, in a process group of its own: each line it prints is handed on as it
|
|
176
|
-
// arrives, whole even across chunks, lines can be written to it until its input closes, and the signal stops it.
|
|
177
|
-
export function start_line_program(argv, options) {
|
|
178
|
-
if (options.signal?.aborted)
|
|
179
|
-
return { write_line: () => undefined, end_input: () => undefined, is_open: () => false, finished: Promise.resolve({ exit_code: -1, stderr: "Stopped before it started" }) };
|
|
180
|
-
const start = spawned(argv, options.on_host, options.cwd);
|
|
181
|
-
const child = spawn(start.command[0], start.command.slice(1), { detached: true, stdio: ["pipe", "pipe", "pipe"], env: options.env, cwd: start.cwd });
|
|
182
|
-
let open = true;
|
|
183
|
-
child.stdin.on("error", () => (open = false));
|
|
184
|
-
const stderr = new BoundedBuffer(stderr_limit_in_bytes);
|
|
185
|
-
const decoder = new TextDecoder();
|
|
186
|
-
let pending = "";
|
|
187
|
-
child.stdout.on("data", (chunk) => {
|
|
188
|
-
pending += decoder.decode(chunk, { stream: true });
|
|
189
|
-
const lines = pending.split("\n");
|
|
190
|
-
pending = lines.pop() ?? "";
|
|
191
|
-
for (const line of lines)
|
|
192
|
-
options.on_line(line);
|
|
193
|
-
});
|
|
194
|
-
child.stderr.on("data", (chunk) => stderr.write(chunk));
|
|
195
|
-
let kill_timer;
|
|
196
|
-
const stop = () => {
|
|
197
|
-
signal_group(child.pid, "SIGTERM");
|
|
198
|
-
kill_timer = setTimeout(() => signal_group(child.pid, "SIGKILL"), kill_grace_in_milliseconds);
|
|
199
|
-
};
|
|
200
|
-
options.signal?.addEventListener("abort", stop, { once: true });
|
|
201
|
-
const finished = new Promise((resolve) => {
|
|
202
|
-
let settled = false;
|
|
203
|
-
const settle = (exit) => {
|
|
204
|
-
if (settled)
|
|
205
|
-
return;
|
|
206
|
-
settled = true;
|
|
207
|
-
open = false;
|
|
208
|
-
clearTimeout(kill_timer);
|
|
209
|
-
options.signal?.removeEventListener("abort", stop);
|
|
210
|
-
resolve(exit);
|
|
211
|
-
};
|
|
212
|
-
child.on("error", (error) => {
|
|
213
|
-
const missing = error.code === "ENOENT";
|
|
214
|
-
settle({ exit_code: missing ? not_installed_exit_code : -1, stderr: missing ? `${argv[0]} is not installed on this host` : `Could not start ${argv[0]}: ${error.message}` });
|
|
215
|
-
});
|
|
216
|
-
child.on("close", (code, signal) => {
|
|
217
|
-
pending += decoder.decode();
|
|
218
|
-
if (pending)
|
|
219
|
-
options.on_line(pending);
|
|
220
|
-
settle({ exit_code: code ?? 128 + (signal === null ? 0 : constants.signals[signal]), stderr: stderr.text() });
|
|
221
|
-
});
|
|
222
|
-
});
|
|
223
|
-
return {
|
|
224
|
-
write_line: (line) => {
|
|
225
|
-
if (open)
|
|
226
|
-
child.stdin.write(`${line}\n`);
|
|
227
|
-
},
|
|
228
|
-
end_input: () => {
|
|
229
|
-
if (!open)
|
|
230
|
-
return;
|
|
231
|
-
open = false;
|
|
232
|
-
child.stdin.end();
|
|
233
|
-
},
|
|
234
|
-
is_open: () => open,
|
|
235
|
-
finished,
|
|
236
|
-
};
|
|
237
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/programs";
|
package/dist/agent/protocol.d.ts
CHANGED
|
@@ -1,172 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
name: string;
|
|
3
|
-
protocol_version: number;
|
|
4
|
-
task_field: string;
|
|
5
|
-
is_task(value: unknown): value is Task;
|
|
6
|
-
}
|
|
7
|
-
export declare function shell_app(name: string, protocol_version?: number): AgentApp<string>;
|
|
8
|
-
export interface RunResult {
|
|
9
|
-
exit_code: number;
|
|
10
|
-
stdout: string;
|
|
11
|
-
stderr: string;
|
|
12
|
-
}
|
|
13
|
-
export type LongRunState = "running" | "exited" | "stopped" | "lost";
|
|
14
|
-
export interface LongRun {
|
|
15
|
-
run_id: string;
|
|
16
|
-
pid: number;
|
|
17
|
-
started_epoch: number;
|
|
18
|
-
state: LongRunState;
|
|
19
|
-
exit_code: number | null;
|
|
20
|
-
}
|
|
21
|
-
export interface LongRunPoll {
|
|
22
|
-
state: LongRunState;
|
|
23
|
-
exit_code: number | null;
|
|
24
|
-
alive: boolean;
|
|
25
|
-
size: number;
|
|
26
|
-
new_output: string;
|
|
27
|
-
next_offset: number;
|
|
28
|
-
}
|
|
29
|
-
export interface AppMessage {
|
|
30
|
-
type: string;
|
|
31
|
-
[field: string]: unknown;
|
|
32
|
-
}
|
|
33
|
-
export interface Requests<Task> {
|
|
34
|
-
run: {
|
|
35
|
-
body: {
|
|
36
|
-
task: Task;
|
|
37
|
-
stdin: string | null;
|
|
38
|
-
timeout_in_seconds: number;
|
|
39
|
-
};
|
|
40
|
-
reply: RunResult;
|
|
41
|
-
};
|
|
42
|
-
start: {
|
|
43
|
-
body: {
|
|
44
|
-
run_id: string;
|
|
45
|
-
task: Task;
|
|
46
|
-
stdin: string | null;
|
|
47
|
-
};
|
|
48
|
-
reply: LongRun;
|
|
49
|
-
};
|
|
50
|
-
poll: {
|
|
51
|
-
body: {
|
|
52
|
-
run_id: string;
|
|
53
|
-
offset: number;
|
|
54
|
-
};
|
|
55
|
-
reply: LongRunPoll;
|
|
56
|
-
};
|
|
57
|
-
stop: {
|
|
58
|
-
body: {
|
|
59
|
-
run_id: string;
|
|
60
|
-
};
|
|
61
|
-
reply: LongRun;
|
|
62
|
-
};
|
|
63
|
-
forget: {
|
|
64
|
-
body: {
|
|
65
|
-
run_id: string;
|
|
66
|
-
};
|
|
67
|
-
reply: null;
|
|
68
|
-
};
|
|
69
|
-
runs: {
|
|
70
|
-
body: Record<never, never>;
|
|
71
|
-
reply: LongRun[];
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
export type RequestType = keyof Requests<unknown>;
|
|
75
|
-
export type RequestMessage<Task> = {
|
|
76
|
-
[K in RequestType]: {
|
|
77
|
-
type: K;
|
|
78
|
-
id: string;
|
|
79
|
-
} & Requests<Task>[K]["body"];
|
|
80
|
-
}[RequestType];
|
|
81
|
-
export type ServerMessage<Task> = RequestMessage<Task> | {
|
|
82
|
-
type: "stream";
|
|
83
|
-
id: string;
|
|
84
|
-
task: Task;
|
|
85
|
-
stdin: string | null;
|
|
86
|
-
window: number;
|
|
87
|
-
} | {
|
|
88
|
-
type: "attach";
|
|
89
|
-
id: string;
|
|
90
|
-
run_id: string;
|
|
91
|
-
offset: number;
|
|
92
|
-
window: number;
|
|
93
|
-
} | {
|
|
94
|
-
type: "credit";
|
|
95
|
-
id: string;
|
|
96
|
-
bytes: number;
|
|
97
|
-
} | {
|
|
98
|
-
type: "cancel";
|
|
99
|
-
id: string;
|
|
100
|
-
} | {
|
|
101
|
-
type: "ping";
|
|
102
|
-
epoch_ms: number;
|
|
103
|
-
} | {
|
|
104
|
-
type: "pong";
|
|
105
|
-
epoch_ms: number;
|
|
106
|
-
} | {
|
|
107
|
-
type: "challenge";
|
|
108
|
-
nonce: string;
|
|
109
|
-
server_key?: string;
|
|
110
|
-
server_signature?: string;
|
|
111
|
-
};
|
|
112
|
-
export type AgentMessage = {
|
|
113
|
-
type: "hello";
|
|
114
|
-
protocol: number;
|
|
115
|
-
host: string;
|
|
116
|
-
agent_version: string;
|
|
117
|
-
runs: LongRun[];
|
|
118
|
-
signature?: string;
|
|
119
|
-
agent_nonce?: string;
|
|
120
|
-
} | {
|
|
121
|
-
type: "reply";
|
|
122
|
-
id: string;
|
|
123
|
-
value: unknown;
|
|
124
|
-
} | {
|
|
125
|
-
type: "failed";
|
|
126
|
-
id: string;
|
|
127
|
-
error: string;
|
|
128
|
-
} | {
|
|
129
|
-
type: "stream_end";
|
|
130
|
-
id: string;
|
|
131
|
-
exit_code: number;
|
|
132
|
-
stderr: string;
|
|
133
|
-
} | {
|
|
134
|
-
type: "ping";
|
|
135
|
-
epoch_ms: number;
|
|
136
|
-
} | {
|
|
137
|
-
type: "pong";
|
|
138
|
-
epoch_ms: number;
|
|
139
|
-
};
|
|
140
|
-
export declare function parse_server_message<Task>(text: string, app: AgentApp<Task>): ServerMessage<Task> | AppMessage | null;
|
|
141
|
-
export declare function parse_agent_message(text: string, app: AgentApp<unknown>): AgentMessage | AppMessage | null;
|
|
142
|
-
export declare function is_server_message<Task>(message: ServerMessage<Task> | AppMessage): message is ServerMessage<Task>;
|
|
143
|
-
export declare function is_agent_message(message: AgentMessage | AppMessage): message is AgentMessage;
|
|
144
|
-
export declare function encode_message<Task>(message: ServerMessage<Task> | AgentMessage | AppMessage, app: AgentApp<Task>): string;
|
|
145
|
-
export declare const most_runs_in_hello = 512;
|
|
146
|
-
export declare const most_runs_in_reply = 10000;
|
|
147
|
-
export declare const most_hello_text_length = 256;
|
|
148
|
-
export declare function clip_text(text: string, length: number): string;
|
|
149
|
-
export declare function parse_long_run(value: unknown): LongRun | null;
|
|
150
|
-
export declare function parse_long_runs(value: unknown, most: number): LongRun[] | null;
|
|
151
|
-
export declare function parse_long_run_poll(value: unknown): LongRunPoll | null;
|
|
152
|
-
export declare function parse_run_result(value: unknown): RunResult | null;
|
|
153
|
-
export declare function clip_hello<Hello extends Extract<AgentMessage, {
|
|
154
|
-
type: "hello";
|
|
155
|
-
}>>(hello: Hello): Hello;
|
|
156
|
-
export declare const signed_message_type = "signed";
|
|
157
|
-
export interface SignedMessage {
|
|
158
|
-
type: typeof signed_message_type;
|
|
159
|
-
counter: number;
|
|
160
|
-
message: string;
|
|
161
|
-
signature: string;
|
|
162
|
-
}
|
|
163
|
-
export declare function encode_signed_message(message_text: string, counter: number, signature: string): string;
|
|
164
|
-
export declare function parse_signed_message(text: string): SignedMessage | null;
|
|
165
|
-
export declare function encode_chunk(id: string, data: Uint8Array): Buffer<ArrayBuffer>;
|
|
166
|
-
export declare function decode_chunk(frame: Uint8Array): {
|
|
167
|
-
id: string;
|
|
168
|
-
data: Buffer;
|
|
169
|
-
} | null;
|
|
170
|
-
export declare function key_host_header(app_name: string): string;
|
|
171
|
-
export declare function is_run_id(value: string): boolean;
|
|
172
|
-
export declare function unknown_run_message(run_id: string): string;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/protocol";
|
package/dist/agent/protocol.js
CHANGED
|
@@ -1,249 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// Every message but a stream's output is one JSON text frame with a `type`. A request from the server carries an `id`
|
|
4
|
-
// the agent answers with one `reply` or `failed` of the same id. A stream or an attach sends its output as binary
|
|
5
|
-
// frames tagged with its id, only as far as the credit the server has granted, then one `stream_end`. Either side
|
|
6
|
-
// sends `ping` while idle and drops a connection that has been silent too long.
|
|
7
|
-
//
|
|
8
|
-
// A run, a long run and a stream each carry the app's task: a shell command for one app, a typed operation for another.
|
|
9
|
-
// The app names the field its task travels in and checks it, so each app keeps its own wire format and version.
|
|
10
|
-
//
|
|
11
|
-
// An agent logging in with its host's key, rather than a token, names the host in the upgrade; the server's first
|
|
12
|
-
// message is then a `challenge`, and the agent's hello carries its signature of that nonce (see keys.ts).
|
|
13
|
-
//
|
|
14
|
-
// A server with a key of its own signs its challenge, to every agent and with either login. An agent that lists the
|
|
15
|
-
// server's public key checks that signature before it signs anything, adds a nonce of its own to its hello, and from
|
|
16
|
-
// then on takes only `signed` messages: each wraps the exact text of one message the server sends, with a counter one
|
|
17
|
-
// more than the last and the server's signature over both nonces, the counter and that text. An agent that lists no
|
|
18
|
-
// server key sends no nonce, so the server sends it plain messages as before.
|
|
19
|
-
//
|
|
20
|
-
// An app adds its own messages beside these: any other `type` is handed to the app, and one carrying a string `id` is
|
|
21
|
-
// a request the app's handler answers like any other.
|
|
22
|
-
// An app whose tasks are shell command lines, sent in a `command` field.
|
|
23
|
-
export function shell_app(name, protocol_version = 1) {
|
|
24
|
-
return { name, protocol_version, task_field: "command", is_task: (value) => typeof value === "string" };
|
|
25
|
-
}
|
|
26
|
-
const id_fields = { id: "string" };
|
|
27
|
-
const run_fields = { ...id_fields, run_id: "string" };
|
|
28
|
-
const server_fields = {
|
|
29
|
-
run: { ...id_fields, task: "task", stdin: "string_or_null", timeout_in_seconds: "number" },
|
|
30
|
-
start: { ...run_fields, task: "task", stdin: "string_or_null" },
|
|
31
|
-
poll: { ...run_fields, offset: "number" },
|
|
32
|
-
stop: run_fields,
|
|
33
|
-
forget: run_fields,
|
|
34
|
-
runs: id_fields,
|
|
35
|
-
stream: { ...id_fields, task: "task", stdin: "string_or_null", window: "number" },
|
|
36
|
-
attach: { ...run_fields, offset: "number", window: "number" },
|
|
37
|
-
credit: { ...id_fields, bytes: "number" },
|
|
38
|
-
cancel: id_fields,
|
|
39
|
-
ping: { epoch_ms: "number" },
|
|
40
|
-
pong: { epoch_ms: "number" },
|
|
41
|
-
challenge: { nonce: "string" },
|
|
42
|
-
};
|
|
43
|
-
const agent_fields = {
|
|
44
|
-
hello: { protocol: "number", host: "string", agent_version: "string", runs: "array" },
|
|
45
|
-
reply: { ...id_fields, value: "any" },
|
|
46
|
-
failed: { ...id_fields, error: "string" },
|
|
47
|
-
stream_end: { ...id_fields, exit_code: "number", stderr: "string" },
|
|
48
|
-
ping: { epoch_ms: "number" },
|
|
49
|
-
pong: { epoch_ms: "number" },
|
|
50
|
-
};
|
|
51
|
-
function has_kind(value, kind, app) {
|
|
52
|
-
if (kind === "any")
|
|
53
|
-
return value !== undefined;
|
|
54
|
-
if (kind === "task")
|
|
55
|
-
return app.is_task(value);
|
|
56
|
-
if (kind === "array")
|
|
57
|
-
return Array.isArray(value);
|
|
58
|
-
if (kind === "string_or_null")
|
|
59
|
-
return value === null || typeof value === "string";
|
|
60
|
-
if (kind === "number")
|
|
61
|
-
return typeof value === "number" && Number.isFinite(value);
|
|
62
|
-
return typeof value === kind;
|
|
63
|
-
}
|
|
64
|
-
// A known message with every field it needs, an app's message for any other type, or null for one that is neither.
|
|
65
|
-
function parse_with(text, fields, app) {
|
|
66
|
-
let message;
|
|
67
|
-
try {
|
|
68
|
-
message = JSON.parse(text);
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
if (typeof message !== "object" || message === null || Array.isArray(message))
|
|
74
|
-
return null;
|
|
75
|
-
const record = message;
|
|
76
|
-
if (typeof record.type !== "string" || record.type === "")
|
|
77
|
-
return null;
|
|
78
|
-
if (!Object.hasOwn(fields, record.type))
|
|
79
|
-
return record;
|
|
80
|
-
// The task arrives in the app's own field and is read as `task`.
|
|
81
|
-
const known_fields = fields[record.type];
|
|
82
|
-
if (Object.hasOwn(known_fields, "task") && app.task_field !== "task") {
|
|
83
|
-
record.task = record[app.task_field];
|
|
84
|
-
delete record[app.task_field];
|
|
85
|
-
}
|
|
86
|
-
for (const [name, kind] of Object.entries(known_fields)) {
|
|
87
|
-
if (!has_kind(record[name], kind, app))
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
return message;
|
|
91
|
-
}
|
|
92
|
-
// A message from the server, or null for one that is not a message at all.
|
|
93
|
-
export function parse_server_message(text, app) {
|
|
94
|
-
return parse_with(text, server_fields, app);
|
|
95
|
-
}
|
|
96
|
-
// A message from an agent, or null for one that is not a message at all.
|
|
97
|
-
export function parse_agent_message(text, app) {
|
|
98
|
-
return parse_with(text, agent_fields, app);
|
|
99
|
-
}
|
|
100
|
-
// Whether a parsed message is one of this protocol's own rather than the app's.
|
|
101
|
-
export function is_server_message(message) {
|
|
102
|
-
return Object.hasOwn(server_fields, message.type);
|
|
103
|
-
}
|
|
104
|
-
export function is_agent_message(message) {
|
|
105
|
-
return Object.hasOwn(agent_fields, message.type);
|
|
106
|
-
}
|
|
107
|
-
// The message as one text frame, with any task in the app's own field.
|
|
108
|
-
export function encode_message(message, app) {
|
|
109
|
-
if (!Object.hasOwn(server_fields, message.type) || !("task" in message))
|
|
110
|
-
return JSON.stringify(message);
|
|
111
|
-
const { task, ...rest } = message;
|
|
112
|
-
return JSON.stringify({ ...rest, [app.task_field]: task });
|
|
113
|
-
}
|
|
114
|
-
// ── Checking what an agent sends ─────────────────────────────────────────────
|
|
115
|
-
// A hello lists at most this many long runs, the newest, so it stays small enough to arrive before login.
|
|
116
|
-
export const most_runs_in_hello = 512;
|
|
117
|
-
// A runs reply lists at most this many long runs, the newest.
|
|
118
|
-
export const most_runs_in_reply = 10_000;
|
|
119
|
-
// The host and version a hello names are cut to this many characters.
|
|
120
|
-
export const most_hello_text_length = 256;
|
|
121
|
-
const long_run_states = ["running", "exited", "stopped", "lost"];
|
|
122
|
-
function is_count(value) {
|
|
123
|
-
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
124
|
-
}
|
|
125
|
-
function is_exit_code(value) {
|
|
126
|
-
return value === null || (typeof value === "number" && Number.isSafeInteger(value));
|
|
127
|
-
}
|
|
128
|
-
function is_state(value) {
|
|
129
|
-
return typeof value === "string" && long_run_states.includes(value);
|
|
130
|
-
}
|
|
131
|
-
// The text cut to a length, so an agent cannot make the server keep an unbounded string.
|
|
132
|
-
export function clip_text(text, length) {
|
|
133
|
-
return text.length <= length ? text : text.slice(0, length);
|
|
134
|
-
}
|
|
135
|
-
// A long run record with only its own fields, or null for one that is not a well-formed record.
|
|
136
|
-
export function parse_long_run(value) {
|
|
137
|
-
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
138
|
-
return null;
|
|
139
|
-
const record = value;
|
|
140
|
-
if (typeof record.run_id !== "string" || !is_run_id(record.run_id))
|
|
141
|
-
return null;
|
|
142
|
-
if (typeof record.pid !== "number" || !Number.isSafeInteger(record.pid))
|
|
143
|
-
return null;
|
|
144
|
-
if (typeof record.started_epoch !== "number" || !Number.isFinite(record.started_epoch))
|
|
145
|
-
return null;
|
|
146
|
-
if (!is_state(record.state) || !is_exit_code(record.exit_code))
|
|
147
|
-
return null;
|
|
148
|
-
return { run_id: record.run_id, pid: record.pid, started_epoch: record.started_epoch, state: record.state, exit_code: record.exit_code };
|
|
149
|
-
}
|
|
150
|
-
// The well-formed records in a list of long runs, at most the given number of them, the last; null for a value that is not a list.
|
|
151
|
-
export function parse_long_runs(value, most) {
|
|
152
|
-
if (!Array.isArray(value))
|
|
153
|
-
return null;
|
|
154
|
-
const runs = [];
|
|
155
|
-
for (const each of value.slice(-most)) {
|
|
156
|
-
const run = parse_long_run(each);
|
|
157
|
-
if (run !== null)
|
|
158
|
-
runs.push(run);
|
|
159
|
-
}
|
|
160
|
-
return runs;
|
|
161
|
-
}
|
|
162
|
-
// A poll reply with only its own fields, or null for one whose fields are missing, of the wrong kind or inconsistent.
|
|
163
|
-
export function parse_long_run_poll(value) {
|
|
164
|
-
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
165
|
-
return null;
|
|
166
|
-
const record = value;
|
|
167
|
-
if (!is_state(record.state) || !is_exit_code(record.exit_code) || typeof record.alive !== "boolean")
|
|
168
|
-
return null;
|
|
169
|
-
if (!is_count(record.size) || !is_count(record.next_offset) || record.next_offset > record.size)
|
|
170
|
-
return null;
|
|
171
|
-
if (typeof record.new_output !== "string")
|
|
172
|
-
return null;
|
|
173
|
-
return {
|
|
174
|
-
state: record.state, exit_code: record.exit_code, alive: record.alive, size: record.size, new_output: record.new_output, next_offset: record.next_offset,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
// A run reply with only its own fields, or null for one that is not well formed.
|
|
178
|
-
export function parse_run_result(value) {
|
|
179
|
-
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
180
|
-
return null;
|
|
181
|
-
const record = value;
|
|
182
|
-
if (typeof record.exit_code !== "number" || !Number.isSafeInteger(record.exit_code))
|
|
183
|
-
return null;
|
|
184
|
-
if (typeof record.stdout !== "string" || typeof record.stderr !== "string")
|
|
185
|
-
return null;
|
|
186
|
-
return { exit_code: record.exit_code, stdout: record.stdout, stderr: record.stderr };
|
|
187
|
-
}
|
|
188
|
-
// A hello whose texts are cut to length and whose runs are the well-formed ones, at most `most_runs_in_hello` of them.
|
|
189
|
-
export function clip_hello(hello) {
|
|
190
|
-
return {
|
|
191
|
-
...hello,
|
|
192
|
-
host: clip_text(hello.host, most_hello_text_length),
|
|
193
|
-
agent_version: clip_text(hello.agent_version, most_hello_text_length),
|
|
194
|
-
runs: parse_long_runs(hello.runs, most_runs_in_hello) ?? [],
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
// ── Signed messages ──────────────────────────────────────────────────────────
|
|
198
|
-
// The type of the frame that wraps each message a server with a key sends an agent that asked for signed messages.
|
|
199
|
-
export const signed_message_type = "signed";
|
|
200
|
-
// The frame that wraps a message's text with its counter and signature.
|
|
201
|
-
export function encode_signed_message(message_text, counter, signature) {
|
|
202
|
-
return JSON.stringify({ type: signed_message_type, counter, message: message_text, signature });
|
|
203
|
-
}
|
|
204
|
-
// A signed message with its fields, or null for a frame that is not one.
|
|
205
|
-
export function parse_signed_message(text) {
|
|
206
|
-
let value;
|
|
207
|
-
try {
|
|
208
|
-
value = JSON.parse(text);
|
|
209
|
-
}
|
|
210
|
-
catch {
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
214
|
-
return null;
|
|
215
|
-
const record = value;
|
|
216
|
-
if (record.type !== signed_message_type || typeof record.message !== "string" || typeof record.signature !== "string")
|
|
217
|
-
return null;
|
|
218
|
-
if (typeof record.counter !== "number" || !Number.isSafeInteger(record.counter) || record.counter < 1)
|
|
219
|
-
return null;
|
|
220
|
-
return { type: signed_message_type, counter: record.counter, message: record.message, signature: record.signature };
|
|
221
|
-
}
|
|
222
|
-
// ── Output frames ────────────────────────────────────────────────────────────
|
|
223
|
-
// A binary frame is one byte giving the id's length, the id, then the output bytes.
|
|
224
|
-
export function encode_chunk(id, data) {
|
|
225
|
-
const id_bytes = Buffer.from(id, "utf8");
|
|
226
|
-
if (id_bytes.length > 255)
|
|
227
|
-
throw new Error("A stream id is at most 255 bytes.");
|
|
228
|
-
return Buffer.concat([Buffer.from([id_bytes.length]), id_bytes, data]);
|
|
229
|
-
}
|
|
230
|
-
export function decode_chunk(frame) {
|
|
231
|
-
const bytes = Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
232
|
-
if (bytes.length < 1 || bytes.length < 1 + bytes[0])
|
|
233
|
-
return null;
|
|
234
|
-
return { id: bytes.subarray(1, 1 + bytes[0]).toString("utf8"), data: bytes.subarray(1 + bytes[0]) };
|
|
235
|
-
}
|
|
236
|
-
// ── Key login ────────────────────────────────────────────────────────────────
|
|
237
|
-
// The upgrade header an agent logging in with its key names its host in, such as "x-nix-agent-host".
|
|
238
|
-
export function key_host_header(app_name) {
|
|
239
|
-
return `${app_name}-agent-host`;
|
|
240
|
-
}
|
|
241
|
-
// ── Long run ids ─────────────────────────────────────────────────────────────
|
|
242
|
-
// A long run's id names its folder on the agent, so it is kept to safe characters.
|
|
243
|
-
export function is_run_id(value) {
|
|
244
|
-
return /^[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}$/.test(value);
|
|
245
|
-
}
|
|
246
|
-
// What an agent answers for a run it does not know, which tells the server the run is gone.
|
|
247
|
-
export function unknown_run_message(run_id) {
|
|
248
|
-
return `No run named '${run_id}'.`;
|
|
249
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/protocol";
|
|
@@ -1,21 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
key: string;
|
|
3
|
-
title: string;
|
|
4
|
-
body: string;
|
|
5
|
-
url?: string;
|
|
6
|
-
icon?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface NoticeShower {
|
|
9
|
-
show(notice: DueNotice): unknown;
|
|
10
|
-
}
|
|
11
|
-
export interface ScheduledNoticesOptions {
|
|
12
|
-
notifier: NoticeShower;
|
|
13
|
-
due: (after: number, until: number) => DueNotice[];
|
|
14
|
-
read_checked_until?: () => number | null;
|
|
15
|
-
write_checked_until?: (moment: number) => void;
|
|
16
|
-
check_ms?: number;
|
|
17
|
-
most_late_ms?: number;
|
|
18
|
-
now?: () => number;
|
|
19
|
-
on_error?: (error: unknown) => void;
|
|
20
|
-
}
|
|
21
|
-
export declare function keep_giving_scheduled_notices(options: ScheduledNoticesOptions): () => void;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/scheduled_notices";
|