@av-pi-studio/server 0.0.117 → 0.0.119
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 +8 -6
- package/dist/agent/agent-service.d.ts +1 -1
- package/dist/agent/agent-service.js +5 -4
- package/dist/agent/session-operations.d.ts +1 -1
- package/dist/agent/session-operations.js +2 -1
- package/dist/daemon/bootstrap.js +6 -2
- package/dist/daemon/dev-bootstrap.js +30 -4
- package/dist/files/file-explorer.d.ts +0 -3
- package/dist/files/file-explorer.js +0 -2
- package/dist/files/file-transfer.d.ts +80 -16
- package/dist/files/file-transfer.js +439 -90
- package/dist/files/index.d.ts +0 -1
- package/dist/files/index.js +0 -1
- package/dist/observability/daemon-status-service.js +4 -0
- package/dist/test-support/daemon-test-client.d.ts +2 -0
- package/dist/test-support/daemon-test-client.js +7 -1
- package/dist/test-support/external-fixtures-scenario.js +106 -0
- package/dist/ws/router.d.ts +7 -0
- package/dist/ws/router.js +28 -9
- package/dist/ws/session.d.ts +21 -0
- package/dist/ws/session.js +34 -0
- package/dist/ws/ws-server.js +2 -0
- package/package.json +3 -3
- package/dist/files/download-token-store.d.ts +0 -14
- package/dist/files/download-token-store.js +0 -33
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
1
4
|
import { PassThrough } from "node:stream";
|
|
5
|
+
import { encodeFileTransferFrame } from "@av-pi-studio/protocol";
|
|
2
6
|
import { MOCK_REBIND_ENTRY_ID } from "../agent/providers/mock/mock-provider.js";
|
|
3
7
|
import { LogTap } from "../logging/log-tap.js";
|
|
4
8
|
import { createLogger } from "../logging/logger.js";
|
|
@@ -53,6 +57,8 @@ export function variantKey(frame) {
|
|
|
53
57
|
const payload = m.payload;
|
|
54
58
|
return payload.ok ? "agent_ui_respond_response.ok" : "agent_ui_respond_response.not_found";
|
|
55
59
|
}
|
|
60
|
+
case "file_download_response":
|
|
61
|
+
return m.ok === true ? type : "file_download_response.rejected";
|
|
56
62
|
case "pong":
|
|
57
63
|
case "daemon_status_response":
|
|
58
64
|
case "daemon_status_subscribe_response":
|
|
@@ -70,6 +76,16 @@ export function variantKey(frame) {
|
|
|
70
76
|
case "interrupt_response":
|
|
71
77
|
case "agent_ui_list_response":
|
|
72
78
|
case "rpc_error":
|
|
79
|
+
case "file_download_cancel_response":
|
|
80
|
+
case "file_upload_response":
|
|
81
|
+
case "file_upload_complete":
|
|
82
|
+
case "create_terminal_response":
|
|
83
|
+
case "subscribe_terminal_response":
|
|
84
|
+
case "list_terminals_response":
|
|
85
|
+
case "rename_terminal_response":
|
|
86
|
+
case "capture_terminal_response":
|
|
87
|
+
case "kill_terminal_response":
|
|
88
|
+
case "terminals_update":
|
|
73
89
|
return type;
|
|
74
90
|
default:
|
|
75
91
|
return undefined;
|
|
@@ -86,6 +102,94 @@ const isType = (type, extra = () => true) => (frame) => {
|
|
|
86
102
|
const m = messageOf(frame);
|
|
87
103
|
return m.type === type && extra(m);
|
|
88
104
|
};
|
|
105
|
+
/** Text of the file the scenario downloads; the fixtures only pin its shape, never its bytes. */
|
|
106
|
+
const SCENARIO_DOWNLOAD_TEXT = "synthetic fixture download body\n";
|
|
107
|
+
/** `Begin → Chunk → End` upload frames (client → daemon) for `stream`, with fake literal content. */
|
|
108
|
+
function uploadFrames(stream, transferId) {
|
|
109
|
+
return [
|
|
110
|
+
encodeFileTransferFrame({ opcode: "Begin", stream, meta: { transferId, fileName: "up.txt" } }),
|
|
111
|
+
encodeFileTransferFrame({
|
|
112
|
+
opcode: "Chunk",
|
|
113
|
+
stream,
|
|
114
|
+
data: new TextEncoder().encode("synthetic fixture upload body\n"),
|
|
115
|
+
}),
|
|
116
|
+
encodeFileTransferFrame({ opcode: "End", stream, ok: true }),
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* File transfer, all inside a temp dir: a whole-file download, a guarded ranged one, a rejected
|
|
121
|
+
* one and a cancel for an idle stream; upload with `ack: true` driven as a client
|
|
122
|
+
* (Begin/Chunk/End), then the `file_upload_complete` push.
|
|
123
|
+
*/
|
|
124
|
+
async function runFileTransferSteps(client) {
|
|
125
|
+
const dir = await mkdtemp(join(tmpdir(), "pi-studio-fixture-"));
|
|
126
|
+
try {
|
|
127
|
+
const downloadPath = join(dir, "download.txt");
|
|
128
|
+
await writeFile(downloadPath, SCENARIO_DOWNLOAD_TEXT);
|
|
129
|
+
const first = await client.rpc("file_download_request", { path: downloadPath });
|
|
130
|
+
// A guarded range over the file's real size and mtime, as a resuming client sends it.
|
|
131
|
+
const ranged = await client.rpc("file_download_request", {
|
|
132
|
+
path: downloadPath,
|
|
133
|
+
offset: 8,
|
|
134
|
+
length: 16,
|
|
135
|
+
ifUnchanged: { size: first.totalBytes, mtimeMs: first.mtimeMs },
|
|
136
|
+
});
|
|
137
|
+
if (ranged.ok !== true)
|
|
138
|
+
throw new Error(`ranged download was rejected: ${String(ranged.error)}`);
|
|
139
|
+
// A rejection (no such file) and a cancel for a stream nothing runs on (the cancel outcome
|
|
140
|
+
// that is the same every run; `ok: true` needs a download still in flight).
|
|
141
|
+
await client.rpc("file_download_request", { path: join(dir, "missing.txt") });
|
|
142
|
+
await client.rpc("file_download_cancel_request", { stream: 200 });
|
|
143
|
+
const transferId = "scenario-upload";
|
|
144
|
+
const uploadPath = join(dir, "upload.txt");
|
|
145
|
+
const upload = await client.rpc("file_upload_request", {
|
|
146
|
+
path: uploadPath,
|
|
147
|
+
transferId,
|
|
148
|
+
ack: true,
|
|
149
|
+
});
|
|
150
|
+
for (const frame of uploadFrames(Number(upload.stream), transferId))
|
|
151
|
+
client.ws.send(frame);
|
|
152
|
+
await client.waitFor(isType("file_upload_complete"));
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
await rm(dir, { recursive: true, force: true });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Terminal control RPCs against one real PTY running a plain `/bin/sh` (never the user's shell or
|
|
160
|
+
* its rc files). The terminal is always killed, even when a step fails.
|
|
161
|
+
*/
|
|
162
|
+
async function runTerminalSteps(client) {
|
|
163
|
+
const dir = await mkdtemp(join(tmpdir(), "pi-studio-fixture-"));
|
|
164
|
+
let slot;
|
|
165
|
+
try {
|
|
166
|
+
const created = await client.rpc("create_terminal_request", {
|
|
167
|
+
workspaceId: "workspace-1",
|
|
168
|
+
cwd: dir,
|
|
169
|
+
shell: "/bin/sh",
|
|
170
|
+
name: "terminal-1",
|
|
171
|
+
cols: 80,
|
|
172
|
+
rows: 24,
|
|
173
|
+
});
|
|
174
|
+
const terminal = created.terminal;
|
|
175
|
+
if (typeof terminal !== "object" || terminal === null || !("slot" in terminal)) {
|
|
176
|
+
throw new Error("create_terminal_response carries no terminal slot");
|
|
177
|
+
}
|
|
178
|
+
slot = Number(terminal.slot);
|
|
179
|
+
await client.waitFor(isType("terminals_update"));
|
|
180
|
+
await client.rpc("subscribe_terminal_request", { slot, cols: 80, rows: 24 });
|
|
181
|
+
await client.rpc("list_terminals_request");
|
|
182
|
+
await client.rpc("rename_terminal_request", { slot, name: "scenario-renamed" });
|
|
183
|
+
await client.rpc("capture_terminal_request", { slot });
|
|
184
|
+
await client.rpc("kill_terminal_request", { slot });
|
|
185
|
+
slot = undefined;
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
if (slot !== undefined)
|
|
189
|
+
await client.rpc("kill_terminal_request", { slot });
|
|
190
|
+
await rm(dir, { recursive: true, force: true });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
89
193
|
export async function runExternalFixtureScenario(dev, client) {
|
|
90
194
|
const { rpc, waitFor } = client;
|
|
91
195
|
const createAgent = async (initialPrompt) => {
|
|
@@ -204,6 +308,8 @@ export async function runExternalFixtureScenario(dev, client) {
|
|
|
204
308
|
dev.logger.info("external fixture live line");
|
|
205
309
|
await waitFor(isType("daemon_log_lines"), 2000);
|
|
206
310
|
await rpc("daemon_logs_unsubscribe_request");
|
|
311
|
+
await runFileTransferSteps(client);
|
|
312
|
+
await runTerminalSteps(client);
|
|
207
313
|
const captured = new Map();
|
|
208
314
|
for (const frame of client.frames) {
|
|
209
315
|
const key = variantKey(frame);
|
package/dist/ws/router.d.ts
CHANGED
|
@@ -14,6 +14,13 @@ export interface RpcHandlerContext {
|
|
|
14
14
|
session: Session;
|
|
15
15
|
message: Record<string, unknown>;
|
|
16
16
|
requestId?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Run `fn` once the router has sent this handler's reply. A handler that must put frames on the
|
|
19
|
+
* wire strictly after its answer (a download's binary stream) schedules them here instead of
|
|
20
|
+
* sending them from inside the awaited handler. Not run when the handler throws; a throwing `fn`
|
|
21
|
+
* is logged and never reaches the client as an `rpc_error`.
|
|
22
|
+
*/
|
|
23
|
+
afterReply(fn: () => void): void;
|
|
17
24
|
}
|
|
18
25
|
/** A handler returns a response message (wrapped + correlated by the router) or `undefined`. */
|
|
19
26
|
export type RpcHandler = (ctx: RpcHandlerContext) => unknown | Promise<unknown>;
|
package/dist/ws/router.js
CHANGED
|
@@ -44,16 +44,35 @@ async function dispatchSessionMessage(session, message, registry) {
|
|
|
44
44
|
}
|
|
45
45
|
const startedAt = Date.now();
|
|
46
46
|
try {
|
|
47
|
-
const
|
|
47
|
+
const afterReply = [];
|
|
48
|
+
const result = await handler({
|
|
49
|
+
session,
|
|
50
|
+
message,
|
|
51
|
+
requestId,
|
|
52
|
+
afterReply: (fn) => afterReply.push(fn),
|
|
53
|
+
});
|
|
48
54
|
registry.logger?.debug({ type, requestId, clientId: session.clientId, durationMs: Date.now() - startedAt }, "rpc ok");
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
try {
|
|
56
|
+
if (result !== undefined) {
|
|
57
|
+
const response = requestId &&
|
|
58
|
+
typeof result === "object" &&
|
|
59
|
+
result !== null &&
|
|
60
|
+
result.requestId === undefined
|
|
61
|
+
? { ...result, requestId }
|
|
62
|
+
: result;
|
|
63
|
+
session.send({ type: "session", message: response });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
// Hooks run even when the send threw (a closed socket) so they can release what they hold.
|
|
68
|
+
for (const fn of afterReply) {
|
|
69
|
+
try {
|
|
70
|
+
fn();
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
registry.logger?.warn({ type, requestId, clientId: session.clientId, err: error?.message }, "rpc after-reply hook failed");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
57
76
|
}
|
|
58
77
|
}
|
|
59
78
|
catch (error) {
|
package/dist/ws/session.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { WebSocket } from "ws";
|
|
2
|
+
/** What a connection receives of `agent_stream` frames (`hello.agentStreams`). */
|
|
3
|
+
export type AgentStreamMode = "all" | "none";
|
|
4
|
+
/**
|
|
5
|
+
* The sessions that may receive an `agent_stream` frame: everyone except `"none"` sessions. Every
|
|
6
|
+
* place that sends one MUST pass its audience through here (an absent mode counts as `"all"`).
|
|
7
|
+
*/
|
|
8
|
+
export declare function agentStreamRecipients(sessions: Iterable<Session>): Session[];
|
|
2
9
|
/**
|
|
3
10
|
* A per-connection session (architecture/websocket-protocol.md § Behavior). Holds the client
|
|
4
11
|
* identity, the (rehydrated) capability set, and the socket. The wire boundary asks exactly one
|
|
@@ -8,6 +15,10 @@ export declare class Session {
|
|
|
8
15
|
readonly id: string;
|
|
9
16
|
readonly clientId: string;
|
|
10
17
|
readonly clientType: string;
|
|
18
|
+
/** The hello's `platform` (e.g. `"desktop"`), verbatim when the client sent one. Informational. */
|
|
19
|
+
readonly platform: string | undefined;
|
|
20
|
+
/** The hello's `agentStreams` mode; fixed for the life of the connection. */
|
|
21
|
+
readonly agentStreams: AgentStreamMode;
|
|
11
22
|
capabilities: Record<string, boolean>;
|
|
12
23
|
/** Epoch ms of the handshake. A wall-clock fact for the connected-clients list. */
|
|
13
24
|
readonly connectedAt: number;
|
|
@@ -21,6 +32,8 @@ export declare class Session {
|
|
|
21
32
|
id: string;
|
|
22
33
|
clientId: string;
|
|
23
34
|
clientType: string;
|
|
35
|
+
platform?: string;
|
|
36
|
+
agentStreams?: AgentStreamMode;
|
|
24
37
|
capabilities: Record<string, boolean>;
|
|
25
38
|
socket: WebSocket;
|
|
26
39
|
connectedAt?: number;
|
|
@@ -34,6 +47,14 @@ export declare class Session {
|
|
|
34
47
|
send(envelope: unknown): void;
|
|
35
48
|
/** Send raw bytes as a binary frame (terminal/file streams). */
|
|
36
49
|
sendBinary(bytes: Uint8Array): void;
|
|
50
|
+
/**
|
|
51
|
+
* Bytes queued on the socket and not yet flushed, or `undefined` when the socket cannot say (the
|
|
52
|
+
* relay's stand-in socket forwards each frame into an encrypted channel and has no queue to
|
|
53
|
+
* read). Bulk senders pace themselves on it; `undefined` means "do not pace".
|
|
54
|
+
*/
|
|
55
|
+
get bufferedAmount(): number | undefined;
|
|
56
|
+
/** False once the socket is closing or closed. A socket with no `readyState` (relay) is open. */
|
|
57
|
+
get isOpen(): boolean;
|
|
37
58
|
close(code?: number, reason?: string): void;
|
|
38
59
|
}
|
|
39
60
|
//# sourceMappingURL=session.d.ts.map
|
package/dist/ws/session.js
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import { supports } from "@av-pi-studio/protocol";
|
|
2
|
+
/** `WebSocket.OPEN` (the `ws` package's readyState for an open socket). */
|
|
3
|
+
const WS_OPEN = 1;
|
|
4
|
+
/**
|
|
5
|
+
* The sessions that may receive an `agent_stream` frame: everyone except `"none"` sessions. Every
|
|
6
|
+
* place that sends one MUST pass its audience through here (an absent mode counts as `"all"`).
|
|
7
|
+
*/
|
|
8
|
+
export function agentStreamRecipients(sessions) {
|
|
9
|
+
const out = [];
|
|
10
|
+
for (const session of sessions) {
|
|
11
|
+
if (session.agentStreams !== "none")
|
|
12
|
+
out.push(session);
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
2
16
|
/**
|
|
3
17
|
* A per-connection session (architecture/websocket-protocol.md § Behavior). Holds the client
|
|
4
18
|
* identity, the (rehydrated) capability set, and the socket. The wire boundary asks exactly one
|
|
@@ -8,6 +22,10 @@ export class Session {
|
|
|
8
22
|
id;
|
|
9
23
|
clientId;
|
|
10
24
|
clientType;
|
|
25
|
+
/** The hello's `platform` (e.g. `"desktop"`), verbatim when the client sent one. Informational. */
|
|
26
|
+
platform;
|
|
27
|
+
/** The hello's `agentStreams` mode; fixed for the life of the connection. */
|
|
28
|
+
agentStreams;
|
|
11
29
|
capabilities;
|
|
12
30
|
/** Epoch ms of the handshake. A wall-clock fact for the connected-clients list. */
|
|
13
31
|
connectedAt;
|
|
@@ -21,6 +39,8 @@ export class Session {
|
|
|
21
39
|
this.id = opts.id;
|
|
22
40
|
this.clientId = opts.clientId;
|
|
23
41
|
this.clientType = opts.clientType;
|
|
42
|
+
this.platform = opts.platform;
|
|
43
|
+
this.agentStreams = opts.agentStreams ?? "all";
|
|
24
44
|
this.capabilities = opts.capabilities;
|
|
25
45
|
this.connectedAt = opts.connectedAt ?? Date.now();
|
|
26
46
|
this.appVersion = opts.appVersion;
|
|
@@ -40,6 +60,20 @@ export class Session {
|
|
|
40
60
|
sendBinary(bytes) {
|
|
41
61
|
this.socket.send(bytes);
|
|
42
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Bytes queued on the socket and not yet flushed, or `undefined` when the socket cannot say (the
|
|
65
|
+
* relay's stand-in socket forwards each frame into an encrypted channel and has no queue to
|
|
66
|
+
* read). Bulk senders pace themselves on it; `undefined` means "do not pace".
|
|
67
|
+
*/
|
|
68
|
+
get bufferedAmount() {
|
|
69
|
+
const queued = this.socket.bufferedAmount;
|
|
70
|
+
return typeof queued === "number" ? queued : undefined;
|
|
71
|
+
}
|
|
72
|
+
/** False once the socket is closing or closed. A socket with no `readyState` (relay) is open. */
|
|
73
|
+
get isOpen() {
|
|
74
|
+
const state = this.socket.readyState;
|
|
75
|
+
return typeof state !== "number" || state === WS_OPEN;
|
|
76
|
+
}
|
|
43
77
|
close(code, reason) {
|
|
44
78
|
this.socket.close(code, reason);
|
|
45
79
|
}
|
package/dist/ws/ws-server.js
CHANGED
|
@@ -71,6 +71,8 @@ export function createWebSocketServer(httpServer, deps) {
|
|
|
71
71
|
id: randomUUID(),
|
|
72
72
|
clientId: hello.data.clientId,
|
|
73
73
|
clientType: hello.data.clientType,
|
|
74
|
+
platform: hello.data.platform,
|
|
75
|
+
agentStreams: hello.data.agentStreams,
|
|
74
76
|
capabilities,
|
|
75
77
|
socket: ws,
|
|
76
78
|
appVersion: hello.data.appVersion,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@av-pi-studio/server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.119",
|
|
4
4
|
"bin": {
|
|
5
5
|
"pi-studio-daemon": "dist/daemon/main.js"
|
|
6
6
|
},
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"clean": "rm -rf dist *.tsbuildinfo"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@av-pi-studio/protocol": "^0.0.
|
|
28
|
-
"@av-pi-studio/relay": "^0.0.
|
|
27
|
+
"@av-pi-studio/protocol": "^0.0.119",
|
|
28
|
+
"@av-pi-studio/relay": "^0.0.119",
|
|
29
29
|
"@earendil-works/pi-coding-agent": "^1.4.0",
|
|
30
30
|
"@xterm/addon-serialize": "^0.14.0",
|
|
31
31
|
"@xterm/headless": "^6.0.0",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare class DownloadTokenStore {
|
|
2
|
-
private readonly ttlMs;
|
|
3
|
-
private readonly now;
|
|
4
|
-
private readonly tokens;
|
|
5
|
-
constructor(ttlMs?: number, now?: () => number, maxTokens?: number);
|
|
6
|
-
/** Issue a single-use token for `path`. */
|
|
7
|
-
issue(path: string): {
|
|
8
|
-
token: string;
|
|
9
|
-
expiresAt: number;
|
|
10
|
-
};
|
|
11
|
-
/** Validate + consume a token. Returns the path, or null if invalid/expired/already used. */
|
|
12
|
-
consume(token: string): string | null;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=download-token-store.d.ts.map
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { LRUCache } from "lru-cache";
|
|
3
|
-
export class DownloadTokenStore {
|
|
4
|
-
ttlMs;
|
|
5
|
-
now;
|
|
6
|
-
tokens;
|
|
7
|
-
constructor(ttlMs = 10 * 60_000, now = () => Date.now(), maxTokens = 10_000) {
|
|
8
|
-
this.ttlMs = ttlMs;
|
|
9
|
-
this.now = now;
|
|
10
|
-
this.tokens = new LRUCache({ max: maxTokens });
|
|
11
|
-
}
|
|
12
|
-
/** Issue a single-use token for `path`. */
|
|
13
|
-
issue(path) {
|
|
14
|
-
const token = randomUUID();
|
|
15
|
-
const expiresAt = this.now() + this.ttlMs;
|
|
16
|
-
this.tokens.set(token, { path, expiresAt, used: false });
|
|
17
|
-
return { token, expiresAt };
|
|
18
|
-
}
|
|
19
|
-
/** Validate + consume a token. Returns the path, or null if invalid/expired/already used. */
|
|
20
|
-
consume(token) {
|
|
21
|
-
const entry = this.tokens.get(token);
|
|
22
|
-
if (!entry)
|
|
23
|
-
return null;
|
|
24
|
-
if (entry.used || entry.expiresAt < this.now()) {
|
|
25
|
-
this.tokens.delete(token);
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
entry.used = true;
|
|
29
|
-
this.tokens.delete(token); // single-use
|
|
30
|
-
return entry.path;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=download-token-store.js.map
|