@getmateo/client 0.0.0-stage → 0.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +211 -0
- package/README.md +43 -2
- package/dist/compat/normalize-provider-models.d.ts +11 -0
- package/dist/compat/normalize-provider-models.js +51 -0
- package/dist/connection/index.d.ts +51 -0
- package/dist/connection/index.js +113 -0
- package/dist/connection/legacy-workspaces.d.ts +19 -0
- package/dist/connection/legacy-workspaces.js +161 -0
- package/dist/connection/legacy.d.ts +31 -0
- package/dist/connection/legacy.js +245 -0
- package/dist/connection/owned.d.ts +36 -0
- package/dist/connection/owned.js +210 -0
- package/dist/creation/index.d.ts +40 -0
- package/dist/creation/index.js +197 -0
- package/dist/daemon-client-relay-e2ee-transport.d.ts +10 -0
- package/dist/daemon-client-relay-e2ee-transport.js +190 -0
- package/dist/daemon-client-runtime-metrics.d.ts +39 -0
- package/dist/daemon-client-runtime-metrics.js +173 -0
- package/dist/daemon-client-transport-types.d.ts +43 -0
- package/dist/daemon-client-transport-types.js +2 -0
- package/dist/daemon-client-transport-utils.d.ts +14 -0
- package/dist/daemon-client-transport-utils.js +127 -0
- package/dist/daemon-client-transport.d.ts +5 -0
- package/dist/daemon-client-transport.js +4 -0
- package/dist/daemon-client-websocket-transport.d.ts +15 -0
- package/dist/daemon-client-websocket-transport.js +183 -0
- package/dist/daemon-client.d.ts +1507 -0
- package/dist/daemon-client.js +5465 -0
- package/dist/fleet/index.d.ts +29 -0
- package/dist/fleet/index.js +81 -0
- package/dist/hosts/index.d.ts +16 -0
- package/dist/hosts/index.js +201 -0
- package/dist/index.d.ts +430 -0
- package/dist/index.js +583 -0
- package/dist/legacy-usage-icons.d.ts +2 -0
- package/dist/legacy-usage-icons.js +16 -0
- package/dist/node/buffering.d.ts +10 -0
- package/dist/node/buffering.js +28 -0
- package/dist/node/index.d.ts +17 -0
- package/dist/node/index.js +191 -0
- package/dist/node/ssh-tunnel.d.ts +9 -0
- package/dist/node/ssh-tunnel.js +81 -0
- package/dist/provider-snapshots/index.d.ts +21 -0
- package/dist/provider-snapshots/index.js +56 -0
- package/dist/terminal-stream-router.d.ts +25 -0
- package/dist/terminal-stream-router.js +62 -0
- package/dist/terminals/index.d.ts +80 -0
- package/dist/terminals/index.js +120 -0
- package/dist/timeline-subscription/index.d.ts +25 -0
- package/dist/timeline-subscription/index.js +56 -0
- package/package.json +59 -4
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
export { createHostChannelTransport } from "../hosts/index.js";
|
|
2
|
+
import { HostTransportBudget } from "./buffering.js";
|
|
3
|
+
export { HostTransportBudget } from "./buffering.js";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { WebSocket } from "ws";
|
|
6
|
+
import { parseHostTarget } from "@getmateo/protocol/host-target";
|
|
7
|
+
import { createSshTunnel } from "./ssh-tunnel.js";
|
|
8
|
+
import { createWebSocketTransportFactory } from "../daemon-client-websocket-transport.js";
|
|
9
|
+
import { createRelayE2eeTransportFactory } from "../daemon-client-relay-e2ee-transport.js";
|
|
10
|
+
import { DaemonClient } from "../daemon-client.js";
|
|
11
|
+
export { parseHostTarget };
|
|
12
|
+
export { createSshTunnel, resolveSshFailureDetail } from "./ssh-tunnel.js";
|
|
13
|
+
/** A factory owns one attempt, including a fresh SSH process and its teardown. */
|
|
14
|
+
export function createHostTransportFactory(connection, budget = new HostTransportBudget(), options = {}) {
|
|
15
|
+
const target = parseHostTarget(connection);
|
|
16
|
+
return (attemptOptions) => {
|
|
17
|
+
const opens = new Set();
|
|
18
|
+
const closes = new Set();
|
|
19
|
+
const errors = new Set();
|
|
20
|
+
const messages = new Set();
|
|
21
|
+
let closed = false;
|
|
22
|
+
let closeEvent;
|
|
23
|
+
let transport;
|
|
24
|
+
let teardown = () => { };
|
|
25
|
+
let disposeSocket = () => { };
|
|
26
|
+
let assertOutboundCapacity = (bytes) => budget.assertCapacity(bytes);
|
|
27
|
+
let sshTunnel = null;
|
|
28
|
+
const close = (code, reason) => {
|
|
29
|
+
if (closed)
|
|
30
|
+
return;
|
|
31
|
+
closed = true;
|
|
32
|
+
try {
|
|
33
|
+
transport?.close(code, reason);
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
try {
|
|
37
|
+
disposeSocket();
|
|
38
|
+
teardown();
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
for (const handler of closes)
|
|
42
|
+
handler(closeEvent);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
void (async () => {
|
|
47
|
+
let socketTarget;
|
|
48
|
+
if (target.kind === "ssh") {
|
|
49
|
+
const tunnel = await createSshTunnel(target.ssh);
|
|
50
|
+
sshTunnel = tunnel;
|
|
51
|
+
teardown = () => tunnel.close();
|
|
52
|
+
socketTarget = parseHostTarget(tunnel.endpoint);
|
|
53
|
+
}
|
|
54
|
+
else
|
|
55
|
+
socketTarget = target;
|
|
56
|
+
if (closed) {
|
|
57
|
+
teardown();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const socketPath = socketTarget.socketPath?.replace(/^~(?=\/)/, homedir());
|
|
61
|
+
let factory = createWebSocketTransportFactory((url, socketOptions) => {
|
|
62
|
+
const socket = new WebSocket(socketPath && url.startsWith("ws+unix:") ? `ws+unix://${socketPath}:/ws` : url, socketOptions?.protocols, {
|
|
63
|
+
headers: socketOptions?.headers,
|
|
64
|
+
agent: socketPath ? undefined : options.webSocketAgent?.(url),
|
|
65
|
+
...(socketPath ? { socketPath } : {}),
|
|
66
|
+
maxPayload: 100 * 1024 * 1024,
|
|
67
|
+
});
|
|
68
|
+
disposeSocket = () => socket.terminate();
|
|
69
|
+
assertOutboundCapacity = (bytes) => {
|
|
70
|
+
if (socket.bufferedAmount + bytes > 128 * 1024 * 1024) {
|
|
71
|
+
throw Object.assign(new Error("Host socket buffer limit reached"), {
|
|
72
|
+
code: "resource_limit",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
budget.assertCapacity(bytes);
|
|
76
|
+
};
|
|
77
|
+
const pending = new Set();
|
|
78
|
+
const send = socket.send.bind(socket);
|
|
79
|
+
socket.send = (data, ...args) => {
|
|
80
|
+
const bytes = typeof data === "string"
|
|
81
|
+
? Buffer.byteLength(data)
|
|
82
|
+
: Buffer.byteLength(data);
|
|
83
|
+
assertOutboundCapacity(bytes);
|
|
84
|
+
const release = budget.reserve(bytes);
|
|
85
|
+
pending.add(release);
|
|
86
|
+
const callback = args.find((value) => typeof value === "function");
|
|
87
|
+
const sendOptions = args.find((value) => value !== null && typeof value === "object");
|
|
88
|
+
try {
|
|
89
|
+
send(data, sendOptions ?? {}, (error) => {
|
|
90
|
+
release();
|
|
91
|
+
pending.delete(release);
|
|
92
|
+
callback?.(error);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
release();
|
|
97
|
+
pending.delete(release);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
socket.once("close", () => {
|
|
102
|
+
for (const release of pending)
|
|
103
|
+
release();
|
|
104
|
+
pending.clear();
|
|
105
|
+
});
|
|
106
|
+
return socket;
|
|
107
|
+
});
|
|
108
|
+
if (socketTarget.offer)
|
|
109
|
+
factory = createRelayE2eeTransportFactory({
|
|
110
|
+
baseFactory: factory,
|
|
111
|
+
daemonPublicKeyB64: socketTarget.offer.daemonPublicKeyB64,
|
|
112
|
+
logger: { warn() { } },
|
|
113
|
+
assertOutboundCapacity: (bytes) => assertOutboundCapacity(bytes),
|
|
114
|
+
});
|
|
115
|
+
transport = factory({ ...attemptOptions, url: socketTarget.url });
|
|
116
|
+
transport.onOpen(() => {
|
|
117
|
+
if (!closed)
|
|
118
|
+
for (const handler of opens)
|
|
119
|
+
handler();
|
|
120
|
+
});
|
|
121
|
+
transport.onClose((event) => {
|
|
122
|
+
closeEvent = event;
|
|
123
|
+
close();
|
|
124
|
+
});
|
|
125
|
+
transport.onError((error) => {
|
|
126
|
+
if (closed)
|
|
127
|
+
return;
|
|
128
|
+
const detail = sshTunnel?.failureDetail() ?? "";
|
|
129
|
+
const failure = /permission denied|authentication failed|host key verification failed|REMOTE HOST IDENTIFICATION HAS CHANGED/i.test(detail)
|
|
130
|
+
? Object.assign(new Error("SSH authentication or host verification failed. Check the SSH configuration on this daemon."), { code: "authentication_failed" })
|
|
131
|
+
: error;
|
|
132
|
+
for (const handler of errors)
|
|
133
|
+
handler(failure);
|
|
134
|
+
close();
|
|
135
|
+
});
|
|
136
|
+
transport.onMessage((data, binary) => {
|
|
137
|
+
if (!closed)
|
|
138
|
+
for (const handler of messages)
|
|
139
|
+
handler(data, binary);
|
|
140
|
+
});
|
|
141
|
+
})().catch(() => {
|
|
142
|
+
for (const handler of errors)
|
|
143
|
+
handler(new Error("Host transport failed"));
|
|
144
|
+
close();
|
|
145
|
+
});
|
|
146
|
+
return {
|
|
147
|
+
get supportsDrain() {
|
|
148
|
+
return transport?.supportsDrain;
|
|
149
|
+
},
|
|
150
|
+
get maxFrameBytes() {
|
|
151
|
+
return transport?.maxFrameBytes;
|
|
152
|
+
},
|
|
153
|
+
send(data) {
|
|
154
|
+
if (closed || !transport)
|
|
155
|
+
throw new Error("Host transport is unavailable");
|
|
156
|
+
return transport.send(data);
|
|
157
|
+
},
|
|
158
|
+
close,
|
|
159
|
+
onOpen(handler) {
|
|
160
|
+
opens.add(handler);
|
|
161
|
+
return () => opens.delete(handler);
|
|
162
|
+
},
|
|
163
|
+
onClose(handler) {
|
|
164
|
+
closes.add(handler);
|
|
165
|
+
return () => closes.delete(handler);
|
|
166
|
+
},
|
|
167
|
+
onError(handler) {
|
|
168
|
+
errors.add(handler);
|
|
169
|
+
return () => errors.delete(handler);
|
|
170
|
+
},
|
|
171
|
+
onMessage(handler) {
|
|
172
|
+
messages.add(handler);
|
|
173
|
+
return () => messages.delete(handler);
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
export function createHostClient(connection, config, budget) {
|
|
179
|
+
const target = parseHostTarget(connection);
|
|
180
|
+
const { webSocketAgent, transportFactory, ...clientConfig } = config;
|
|
181
|
+
return new DaemonClient({
|
|
182
|
+
...clientConfig,
|
|
183
|
+
url: "ws://host-connector/ws",
|
|
184
|
+
...(target.password ? { password: target.password } : {}),
|
|
185
|
+
...(target.kind === "socket" && target.peerCredential
|
|
186
|
+
? { peerCredential: target.peerCredential }
|
|
187
|
+
: {}),
|
|
188
|
+
transportFactory: transportFactory ?? createHostTransportFactory(connection, budget, { webSocketAgent }),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type SshTransportTarget } from "@getmateo/protocol/ssh-transport";
|
|
2
|
+
export interface SshTunnel {
|
|
3
|
+
endpoint: string;
|
|
4
|
+
close(): void;
|
|
5
|
+
failureDetail(): string | null;
|
|
6
|
+
}
|
|
7
|
+
export declare function resolveSshFailureDetail(failure: string | null, stderr: string): string | null;
|
|
8
|
+
export declare function createSshTunnel(target: SshTransportTarget): Promise<SshTunnel>;
|
|
9
|
+
//# sourceMappingURL=ssh-tunnel.d.ts.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createServer } from "node:net";
|
|
3
|
+
import { buildSshTunnelArgs } from "@getmateo/protocol/ssh-transport";
|
|
4
|
+
const SSH_STDERR_LIMIT = 8192;
|
|
5
|
+
function formatSshFailure(stderr, code, signal) {
|
|
6
|
+
const detail = stderr.trim();
|
|
7
|
+
if (detail)
|
|
8
|
+
return detail;
|
|
9
|
+
if (signal)
|
|
10
|
+
return `ssh exited with signal ${signal}`;
|
|
11
|
+
return `ssh exited with code ${code ?? "unknown"}`;
|
|
12
|
+
}
|
|
13
|
+
export function resolveSshFailureDetail(failure, stderr) {
|
|
14
|
+
return failure ?? (stderr.trim() || null);
|
|
15
|
+
}
|
|
16
|
+
export function createSshTunnel(target) {
|
|
17
|
+
let server = null;
|
|
18
|
+
let socket = null;
|
|
19
|
+
let child = null;
|
|
20
|
+
let stderr = "";
|
|
21
|
+
let failure = null;
|
|
22
|
+
function close() {
|
|
23
|
+
server?.close();
|
|
24
|
+
server = null;
|
|
25
|
+
socket?.destroy();
|
|
26
|
+
socket = null;
|
|
27
|
+
if (child && !child.killed)
|
|
28
|
+
child.kill();
|
|
29
|
+
child = null;
|
|
30
|
+
}
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
server = createServer((acceptedSocket) => {
|
|
33
|
+
socket = acceptedSocket;
|
|
34
|
+
server?.close();
|
|
35
|
+
server = null;
|
|
36
|
+
child = spawn("ssh", buildSshTunnelArgs(target), {
|
|
37
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
38
|
+
windowsHide: true,
|
|
39
|
+
});
|
|
40
|
+
child.stderr.on("data", (chunk) => {
|
|
41
|
+
stderr = `${stderr}${chunk.toString()}`.slice(-SSH_STDERR_LIMIT);
|
|
42
|
+
});
|
|
43
|
+
const fail = (error) => {
|
|
44
|
+
failure = error.message;
|
|
45
|
+
acceptedSocket.destroy(error);
|
|
46
|
+
};
|
|
47
|
+
child.on("error", fail);
|
|
48
|
+
child.stdin.on("error", fail);
|
|
49
|
+
child.on("exit", (code, signal) => {
|
|
50
|
+
if (code !== 0 || signal)
|
|
51
|
+
failure = formatSshFailure(stderr, code, signal);
|
|
52
|
+
acceptedSocket.destroy(failure ? new Error(failure) : undefined);
|
|
53
|
+
});
|
|
54
|
+
acceptedSocket.on("error", () => undefined);
|
|
55
|
+
acceptedSocket.on("close", () => {
|
|
56
|
+
if (child && !child.killed)
|
|
57
|
+
child.kill();
|
|
58
|
+
});
|
|
59
|
+
acceptedSocket.pipe(child.stdin);
|
|
60
|
+
child.stdout.pipe(acceptedSocket);
|
|
61
|
+
});
|
|
62
|
+
server.once("error", (error) => {
|
|
63
|
+
close();
|
|
64
|
+
reject(error);
|
|
65
|
+
});
|
|
66
|
+
server.listen(0, "127.0.0.1", () => {
|
|
67
|
+
const address = server?.address();
|
|
68
|
+
if (!address || typeof address === "string") {
|
|
69
|
+
close();
|
|
70
|
+
reject(new Error("Failed to allocate the SSH tunnel port"));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
resolve({
|
|
74
|
+
endpoint: `127.0.0.1:${address.port}`,
|
|
75
|
+
close,
|
|
76
|
+
failureDetail: () => resolveSshFailureDetail(failure, stderr),
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ssh-tunnel.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { GetProvidersSnapshotResponseMessage, SessionOutboundMessage } from "@getmateo/protocol/messages";
|
|
2
|
+
type Update = Extract<SessionOutboundMessage, {
|
|
3
|
+
type: "providers_snapshot_update";
|
|
4
|
+
}>;
|
|
5
|
+
type Snapshot = GetProvidersSnapshotResponseMessage["payload"];
|
|
6
|
+
/** Resolve announcements only while observed, coalescing changes during a fetch. */
|
|
7
|
+
export declare class ProviderSnapshotUpdates {
|
|
8
|
+
private readonly host;
|
|
9
|
+
private pending;
|
|
10
|
+
constructor(host: {
|
|
11
|
+
active(update: Update): boolean;
|
|
12
|
+
fetch(cwd: string | undefined): Promise<Snapshot>;
|
|
13
|
+
emit(update: Update): void;
|
|
14
|
+
failed(error: unknown): void;
|
|
15
|
+
});
|
|
16
|
+
clear(): void;
|
|
17
|
+
receive(update: Update): void;
|
|
18
|
+
private resolve;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Resolve announcements only while observed, coalescing changes during a fetch. */
|
|
2
|
+
export class ProviderSnapshotUpdates {
|
|
3
|
+
constructor(host) {
|
|
4
|
+
this.host = host;
|
|
5
|
+
this.pending = new Map();
|
|
6
|
+
}
|
|
7
|
+
clear() {
|
|
8
|
+
this.pending.clear();
|
|
9
|
+
}
|
|
10
|
+
receive(update) {
|
|
11
|
+
if (!this.host.active(update))
|
|
12
|
+
return;
|
|
13
|
+
const key = JSON.stringify([update.payload.subscriptionId, update.payload.cwd]);
|
|
14
|
+
const pending = this.pending.get(key) ?? { latest: update, running: false };
|
|
15
|
+
pending.latest = update;
|
|
16
|
+
this.pending.set(key, pending);
|
|
17
|
+
if (pending.running)
|
|
18
|
+
return;
|
|
19
|
+
pending.running = true;
|
|
20
|
+
void this.resolve(key, pending);
|
|
21
|
+
}
|
|
22
|
+
async resolve(key, pending) {
|
|
23
|
+
try {
|
|
24
|
+
while (this.pending.get(key) === pending) {
|
|
25
|
+
const announced = pending.latest;
|
|
26
|
+
if (!this.host.active(announced))
|
|
27
|
+
return;
|
|
28
|
+
const snapshot = await this.host.fetch(announced.payload.cwd);
|
|
29
|
+
if (!this.host.active(announced))
|
|
30
|
+
return;
|
|
31
|
+
if (this.pending.get(key) !== pending)
|
|
32
|
+
return;
|
|
33
|
+
// A change arriving while the body was in flight needs the current body.
|
|
34
|
+
// If that response already covers it, no second fetch is necessary.
|
|
35
|
+
if (pending.latest !== announced &&
|
|
36
|
+
pending.latest.payload.snapshotHash !== snapshot.snapshotHash)
|
|
37
|
+
continue;
|
|
38
|
+
this.pending.delete(key);
|
|
39
|
+
this.host.emit({
|
|
40
|
+
type: "providers_snapshot_update",
|
|
41
|
+
payload: { ...snapshot, subscriptionId: announced.payload.subscriptionId },
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (this.pending.get(key) === pending)
|
|
48
|
+
this.host.failed(error);
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
if (this.pending.get(key) === pending)
|
|
52
|
+
this.pending.delete(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type TerminalStreamFrame } from "@getmateo/protocol/binary-frames/index";
|
|
2
|
+
import type { TerminalState } from "@getmateo/protocol/messages";
|
|
3
|
+
export type TerminalStreamEvent = {
|
|
4
|
+
terminalId: string;
|
|
5
|
+
subscriptionId: string;
|
|
6
|
+
} & ({
|
|
7
|
+
type: "output";
|
|
8
|
+
data: Uint8Array;
|
|
9
|
+
} | {
|
|
10
|
+
type: "snapshot";
|
|
11
|
+
state: TerminalState;
|
|
12
|
+
} | {
|
|
13
|
+
type: "restore";
|
|
14
|
+
data: Uint8Array;
|
|
15
|
+
});
|
|
16
|
+
export declare class TerminalStreamRouter {
|
|
17
|
+
private readonly slots;
|
|
18
|
+
private readonly listeners;
|
|
19
|
+
/** Passive wire diagnostics. App state consumes the registration's callback. */
|
|
20
|
+
onEvent(handler: (event: TerminalStreamEvent) => void): () => void;
|
|
21
|
+
attach(subscriptionId: string, terminalId: string, slot: number, receive: (event: TerminalStreamEvent) => void): () => void;
|
|
22
|
+
clearSlots(): void;
|
|
23
|
+
handleFrame(frame: TerminalStreamFrame): void;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=terminal-stream-router.d.ts.map
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { decodeTerminalSnapshotPayload, TerminalStreamOpcode, } from "@getmateo/protocol/binary-frames/index";
|
|
2
|
+
export class TerminalStreamRouter {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.slots = new Map();
|
|
5
|
+
this.listeners = new Set();
|
|
6
|
+
}
|
|
7
|
+
/** Passive wire diagnostics. App state consumes the registration's callback. */
|
|
8
|
+
onEvent(handler) {
|
|
9
|
+
this.listeners.add(handler);
|
|
10
|
+
return () => {
|
|
11
|
+
this.listeners.delete(handler);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
attach(subscriptionId, terminalId, slot, receive) {
|
|
15
|
+
const registration = { subscriptionId, terminalId, receive };
|
|
16
|
+
const registrations = this.slots.get(slot) ?? new Set();
|
|
17
|
+
registrations.add(registration);
|
|
18
|
+
this.slots.set(slot, registrations);
|
|
19
|
+
return () => {
|
|
20
|
+
registrations.delete(registration);
|
|
21
|
+
if (registrations.size === 0)
|
|
22
|
+
this.slots.delete(slot);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
clearSlots() {
|
|
26
|
+
this.slots.clear();
|
|
27
|
+
}
|
|
28
|
+
handleFrame(frame) {
|
|
29
|
+
const registrations = this.slots.get(frame.slot);
|
|
30
|
+
if (!registrations)
|
|
31
|
+
return;
|
|
32
|
+
for (const registration of registrations) {
|
|
33
|
+
const identity = {
|
|
34
|
+
subscriptionId: registration.subscriptionId,
|
|
35
|
+
terminalId: registration.terminalId,
|
|
36
|
+
};
|
|
37
|
+
let event;
|
|
38
|
+
if (frame.opcode === TerminalStreamOpcode.Output)
|
|
39
|
+
event = { ...identity, type: "output", data: frame.payload };
|
|
40
|
+
else if (frame.opcode === TerminalStreamOpcode.Restore)
|
|
41
|
+
event = { ...identity, type: "restore", data: frame.payload };
|
|
42
|
+
else if (frame.opcode === TerminalStreamOpcode.Snapshot) {
|
|
43
|
+
const state = decodeTerminalSnapshotPayload(frame.payload);
|
|
44
|
+
if (!state)
|
|
45
|
+
return;
|
|
46
|
+
event = { ...identity, type: "snapshot", state };
|
|
47
|
+
}
|
|
48
|
+
else
|
|
49
|
+
return;
|
|
50
|
+
registration.receive(event);
|
|
51
|
+
for (const listener of this.listeners) {
|
|
52
|
+
try {
|
|
53
|
+
listener(event);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
/* Passive diagnostics cannot interrupt delivery. */
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=terminal-stream-router.js.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { DaemonClient } from "../daemon-client.js";
|
|
3
|
+
declare const TerminalSchema: z.ZodObject<{
|
|
4
|
+
id: z.ZodString;
|
|
5
|
+
workspaceId: z.ZodString;
|
|
6
|
+
cwd: z.ZodString;
|
|
7
|
+
name: z.ZodString;
|
|
8
|
+
}, z.core.$strip>;
|
|
9
|
+
export type MateoTerminal = z.infer<typeof TerminalSchema>;
|
|
10
|
+
export interface MateoTerminalCreateOptions {
|
|
11
|
+
workspaceId: string;
|
|
12
|
+
/** Process working directory; defaults to the workspace directory. */
|
|
13
|
+
cwd?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
command?: string;
|
|
16
|
+
args?: string[];
|
|
17
|
+
size?: {
|
|
18
|
+
rows: number;
|
|
19
|
+
cols: number;
|
|
20
|
+
};
|
|
21
|
+
requestId?: string;
|
|
22
|
+
}
|
|
23
|
+
export type MateoGlobalTerminalCreateOptions = Omit<MateoTerminalCreateOptions, "workspaceId" | "cwd"> & {
|
|
24
|
+
/** Working directory on the host; `~` and `~/...` resolve against the daemon user's home. */
|
|
25
|
+
cwd?: string;
|
|
26
|
+
};
|
|
27
|
+
export interface MateoTerminalListOptions {
|
|
28
|
+
/** Ownership filter. When supplied, cwd does not restrict the results. */
|
|
29
|
+
workspaceId?: string;
|
|
30
|
+
/** Workspace root directory filter for unscoped listings. */
|
|
31
|
+
cwd?: string;
|
|
32
|
+
requestId?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface MateoTerminalListResult {
|
|
35
|
+
entries: MateoTerminal[];
|
|
36
|
+
requestId: string;
|
|
37
|
+
}
|
|
38
|
+
export interface MateoTerminalCaptureOptions {
|
|
39
|
+
start?: number;
|
|
40
|
+
end?: number;
|
|
41
|
+
stripAnsi?: boolean;
|
|
42
|
+
requestId?: string;
|
|
43
|
+
}
|
|
44
|
+
export type MateoTerminalCaptureResult = Awaited<ReturnType<DaemonClient["captureTerminal"]>>;
|
|
45
|
+
export interface MateoTerminalHandle {
|
|
46
|
+
readonly id: string;
|
|
47
|
+
current(): MateoTerminal | null;
|
|
48
|
+
refresh(options?: {
|
|
49
|
+
requestId?: string;
|
|
50
|
+
}): Promise<MateoTerminal | null>;
|
|
51
|
+
/** Sends literal input and returns its UTF-16 length. Does not await command execution. */
|
|
52
|
+
write(data: string): number;
|
|
53
|
+
/** Expands CLI key tokens; other strings are literal. Returns the input's UTF-16 length. */
|
|
54
|
+
sendKeys(keys: readonly string[]): number;
|
|
55
|
+
capture(options?: MateoTerminalCaptureOptions): Promise<MateoTerminalCaptureResult>;
|
|
56
|
+
kill(requestId?: string): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
export interface MateoTerminalActions {
|
|
59
|
+
create(options: MateoTerminalCreateOptions): Promise<MateoTerminalHandle>;
|
|
60
|
+
list(options?: MateoTerminalListOptions): Promise<MateoTerminalListResult>;
|
|
61
|
+
ref(terminal: string | MateoTerminal): MateoTerminalHandle;
|
|
62
|
+
/** Whether the host runs terminals owned by no workspace (`createGlobal`, `listGlobal`). */
|
|
63
|
+
supportsGlobal(): boolean;
|
|
64
|
+
/** Starts a terminal owned by no workspace, in `~` unless `cwd` says otherwise. */
|
|
65
|
+
createGlobal(options?: MateoGlobalTerminalCreateOptions): Promise<MateoTerminalHandle>;
|
|
66
|
+
listGlobal(options?: {
|
|
67
|
+
requestId?: string;
|
|
68
|
+
}): Promise<MateoTerminalListResult>;
|
|
69
|
+
}
|
|
70
|
+
export interface MateoWorkspaceTerminalActions {
|
|
71
|
+
create(options?: Omit<MateoTerminalCreateOptions, "workspaceId">): Promise<MateoTerminalHandle>;
|
|
72
|
+
list(options?: {
|
|
73
|
+
requestId?: string;
|
|
74
|
+
}): Promise<MateoTerminalListResult>;
|
|
75
|
+
}
|
|
76
|
+
type TerminalClient = Pick<DaemonClient, "ensureConnected" | "getLastServerInfoMessage" | "createTerminal" | "listTerminals" | "sendTerminalInput" | "captureTerminal" | "killTerminal">;
|
|
77
|
+
/** @package */
|
|
78
|
+
export declare function createTerminalActions(daemonClient: TerminalClient, resolveWorkspaceDirectory: (workspaceId: string) => Promise<string>): MateoTerminalActions;
|
|
79
|
+
export {};
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { GLOBAL_TERMINAL_WORKSPACE_ID } from "@getmateo/protocol/global-terminal";
|
|
3
|
+
const TerminalSchema = z.object({
|
|
4
|
+
id: z.string(),
|
|
5
|
+
workspaceId: z.string(),
|
|
6
|
+
cwd: z.string(),
|
|
7
|
+
name: z.string(),
|
|
8
|
+
});
|
|
9
|
+
/** @package */
|
|
10
|
+
export function createTerminalActions(daemonClient, resolveWorkspaceDirectory) {
|
|
11
|
+
function client() {
|
|
12
|
+
daemonClient.ensureConnected();
|
|
13
|
+
// COMPAT(workspaceTerminals): added in v0.7.3, remove gate after 2027-09-05.
|
|
14
|
+
if (daemonClient.getLastServerInfoMessage()?.features?.workspaceTerminals !== true) {
|
|
15
|
+
throw new Error("Update the host to use workspace terminals through the SDK.");
|
|
16
|
+
}
|
|
17
|
+
return daemonClient;
|
|
18
|
+
}
|
|
19
|
+
const list = async (options = {}) => {
|
|
20
|
+
const result = await client().listTerminals(options.cwd, options.requestId, {
|
|
21
|
+
workspaceId: options.workspaceId,
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
entries: result.terminals.map((terminal) => TerminalSchema.parse(terminal)),
|
|
25
|
+
requestId: result.requestId,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
const ref = (terminal) => {
|
|
29
|
+
const id = typeof terminal === "string" ? terminal : terminal.id;
|
|
30
|
+
let current = typeof terminal === "string" ? null : terminal;
|
|
31
|
+
const write = (data) => {
|
|
32
|
+
client().sendTerminalInput(id, { type: "input", data });
|
|
33
|
+
return data.length;
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
current: () => current,
|
|
38
|
+
refresh: async (options) => {
|
|
39
|
+
const result = await list(options);
|
|
40
|
+
current = result.entries.find((entry) => entry.id === id) ?? null;
|
|
41
|
+
return current;
|
|
42
|
+
},
|
|
43
|
+
write,
|
|
44
|
+
sendKeys: (keys) => write(keys.map(resolveKeyToken).join("")),
|
|
45
|
+
capture: (options = {}) => {
|
|
46
|
+
const { requestId, ...captureOptions } = options;
|
|
47
|
+
return client().captureTerminal(id, captureOptions, requestId);
|
|
48
|
+
},
|
|
49
|
+
kill: async (requestId) => {
|
|
50
|
+
const result = await client().killTerminal(id, requestId);
|
|
51
|
+
if (!result.success)
|
|
52
|
+
throw new Error(`Failed to kill terminal ${id}`);
|
|
53
|
+
current = null;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
create: async ({ workspaceId, cwd, name, requestId, ...options }) => {
|
|
59
|
+
const driver = client();
|
|
60
|
+
if (!workspaceId)
|
|
61
|
+
throw new Error("workspaceId is required");
|
|
62
|
+
const directory = cwd ?? (await resolveWorkspaceDirectory(workspaceId));
|
|
63
|
+
const result = await driver.createTerminal(directory, name, requestId, {
|
|
64
|
+
...options,
|
|
65
|
+
workspaceId,
|
|
66
|
+
});
|
|
67
|
+
if (result.error || !result.terminal) {
|
|
68
|
+
throw new Error(result.error ?? "The daemon did not create a terminal");
|
|
69
|
+
}
|
|
70
|
+
return ref(TerminalSchema.parse(result.terminal));
|
|
71
|
+
},
|
|
72
|
+
list,
|
|
73
|
+
ref,
|
|
74
|
+
supportsGlobal: () => daemonClient.getLastServerInfoMessage()?.features?.globalTerminals === true,
|
|
75
|
+
createGlobal: async ({ cwd = "~", name, requestId, ...options } = {}) => {
|
|
76
|
+
const driver = client();
|
|
77
|
+
if (driver.getLastServerInfoMessage()?.features?.globalTerminals !== true) {
|
|
78
|
+
throw new Error("Update the host to run terminals outside a workspace.");
|
|
79
|
+
}
|
|
80
|
+
const result = await driver.createTerminal(cwd, name, requestId, {
|
|
81
|
+
...options,
|
|
82
|
+
workspaceId: GLOBAL_TERMINAL_WORKSPACE_ID,
|
|
83
|
+
});
|
|
84
|
+
if (result.error || !result.terminal) {
|
|
85
|
+
throw new Error(result.error ?? "The daemon did not create a terminal");
|
|
86
|
+
}
|
|
87
|
+
return ref(TerminalSchema.parse(result.terminal));
|
|
88
|
+
},
|
|
89
|
+
listGlobal: (options = {}) => list({ ...options, workspaceId: GLOBAL_TERMINAL_WORKSPACE_ID }),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function resolveKeyToken(key) {
|
|
93
|
+
switch (key) {
|
|
94
|
+
case "Enter":
|
|
95
|
+
return "\r";
|
|
96
|
+
case "Tab":
|
|
97
|
+
return "\t";
|
|
98
|
+
case "Escape":
|
|
99
|
+
return "\u001b";
|
|
100
|
+
case "Space":
|
|
101
|
+
return " ";
|
|
102
|
+
case "BSpace":
|
|
103
|
+
return "\u007f";
|
|
104
|
+
case "C-c":
|
|
105
|
+
return "\u0003";
|
|
106
|
+
case "C-d":
|
|
107
|
+
return "\u0004";
|
|
108
|
+
case "C-z":
|
|
109
|
+
return "\u001a";
|
|
110
|
+
case "C-l":
|
|
111
|
+
return "\u000c";
|
|
112
|
+
case "C-a":
|
|
113
|
+
return "\u0001";
|
|
114
|
+
case "C-e":
|
|
115
|
+
return "\u0005";
|
|
116
|
+
default:
|
|
117
|
+
return key;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SessionOutboundMessage } from "@getmateo/protocol/messages";
|
|
2
|
+
import type { OwnedSubscription, TimelineSubscription } from "../connection/index.js";
|
|
3
|
+
type TimelineUpdate = Extract<SessionOutboundMessage, {
|
|
4
|
+
type: "agent_stream" | "agent.timeline.replacement";
|
|
5
|
+
}>;
|
|
6
|
+
/** Local SDK lifecycle messages; these are not additional wire RPCs. */
|
|
7
|
+
export type TimelineMessage = TimelineUpdate | {
|
|
8
|
+
type: "agent.timeline.subscription_restored";
|
|
9
|
+
payload: {
|
|
10
|
+
agentId: string;
|
|
11
|
+
subscriptionId: string;
|
|
12
|
+
};
|
|
13
|
+
} | {
|
|
14
|
+
type: "agent.timeline.error";
|
|
15
|
+
payload: {
|
|
16
|
+
agentId: string;
|
|
17
|
+
error: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
/** Restores live delivery; the consumer owns history reads and gap recovery. */
|
|
21
|
+
export declare function subscribeTimeline(agentId: string, observation: OwnedSubscription<{
|
|
22
|
+
agentIds: string[];
|
|
23
|
+
}>, handler: (message: TimelineMessage) => void, reportError: (error: unknown) => void): TimelineSubscription;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|