@cotal-ai/manager 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.
Files changed (46) hide show
  1. package/LICENSE +202 -0
  2. package/dist/attach-client.d.ts +7 -0
  3. package/dist/attach-client.d.ts.map +1 -0
  4. package/dist/attach-client.js +48 -0
  5. package/dist/attach-client.js.map +1 -0
  6. package/dist/attach-endpoint.d.ts +38 -0
  7. package/dist/attach-endpoint.d.ts.map +1 -0
  8. package/dist/attach-endpoint.js +171 -0
  9. package/dist/attach-endpoint.js.map +1 -0
  10. package/dist/commands.d.ts +2 -0
  11. package/dist/commands.d.ts.map +1 -0
  12. package/dist/commands.js +157 -0
  13. package/dist/commands.js.map +1 -0
  14. package/dist/index.d.ts +4 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +3 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/manager.d.ts +43 -0
  19. package/dist/manager.d.ts.map +1 -0
  20. package/dist/manager.js +207 -0
  21. package/dist/manager.js.map +1 -0
  22. package/dist/runtime/cmux.d.ts +16 -0
  23. package/dist/runtime/cmux.d.ts.map +1 -0
  24. package/dist/runtime/cmux.js +50 -0
  25. package/dist/runtime/cmux.js.map +1 -0
  26. package/dist/runtime/index.d.ts +12 -0
  27. package/dist/runtime/index.d.ts.map +1 -0
  28. package/dist/runtime/index.js +34 -0
  29. package/dist/runtime/index.js.map +1 -0
  30. package/dist/runtime/pty.d.ts +13 -0
  31. package/dist/runtime/pty.d.ts.map +1 -0
  32. package/dist/runtime/pty.js +113 -0
  33. package/dist/runtime/pty.js.map +1 -0
  34. package/dist/runtime/tmux.d.ts +16 -0
  35. package/dist/runtime/tmux.d.ts.map +1 -0
  36. package/dist/runtime/tmux.js +77 -0
  37. package/dist/runtime/tmux.js.map +1 -0
  38. package/dist/runtime/types.d.ts +38 -0
  39. package/dist/runtime/types.d.ts.map +1 -0
  40. package/dist/runtime/types.js +2 -0
  41. package/dist/runtime/types.js.map +1 -0
  42. package/dist/ui.d.ts +10 -0
  43. package/dist/ui.d.ts.map +1 -0
  44. package/dist/ui.js +10 -0
  45. package/dist/ui.js.map +1 -0
  46. package/package.json +36 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Drive a manager's attach endpoint from the terminal: raw-mode stdin streams to
3
+ * the PTY, PTY output streams to stdout, and SIGWINCH-style resizes are forwarded.
4
+ * Ctrl-] detaches without killing the agent.
5
+ */
6
+ export declare function attachClient(url: string): Promise<void>;
7
+ //# sourceMappingURL=attach-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attach-client.d.ts","sourceRoot":"","sources":["../src/attach-client.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAuCvD"}
@@ -0,0 +1,48 @@
1
+ import WebSocket from "ws";
2
+ /** Detach key — Ctrl-] (0x1d), as in telnet/ssh escape conventions. */
3
+ const DETACH = 0x1d;
4
+ /**
5
+ * Drive a manager's attach endpoint from the terminal: raw-mode stdin streams to
6
+ * the PTY, PTY output streams to stdout, and SIGWINCH-style resizes are forwarded.
7
+ * Ctrl-] detaches without killing the agent.
8
+ */
9
+ export function attachClient(url) {
10
+ return new Promise((resolve, reject) => {
11
+ const ws = new WebSocket(url);
12
+ const stdin = process.stdin;
13
+ const wasRaw = stdin.isRaw ?? false;
14
+ const sendResize = () => ws.send(`r:${process.stdout.columns ?? 80},${process.stdout.rows ?? 24}`);
15
+ const onInput = (d) => {
16
+ if (d.length === 1 && d[0] === DETACH) {
17
+ ws.close();
18
+ return;
19
+ }
20
+ ws.send(d);
21
+ };
22
+ const cleanup = () => {
23
+ stdin.off("data", onInput);
24
+ process.stdout.off("resize", sendResize);
25
+ if (stdin.isTTY)
26
+ stdin.setRawMode(wasRaw);
27
+ stdin.pause();
28
+ };
29
+ ws.on("open", () => {
30
+ if (stdin.isTTY)
31
+ stdin.setRawMode(true);
32
+ stdin.resume();
33
+ sendResize();
34
+ process.stdout.on("resize", sendResize);
35
+ stdin.on("data", onInput);
36
+ });
37
+ ws.on("message", (data) => process.stdout.write(data));
38
+ ws.on("close", () => {
39
+ cleanup();
40
+ resolve();
41
+ });
42
+ ws.on("error", (e) => {
43
+ cleanup();
44
+ reject(e);
45
+ });
46
+ });
47
+ }
48
+ //# sourceMappingURL=attach-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attach-client.js","sourceRoot":"","sources":["../src/attach-client.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,IAAI,CAAC;AAE3B,uEAAuE;AACvE,MAAM,MAAM,GAAG,IAAI,CAAC;AAEpB;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW;IACtC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,MAAM,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;QAC9B,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC5B,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC;QAEpC,MAAM,UAAU,GAAG,GAAG,EAAE,CACtB,EAAE,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE,EAAE,CAAC,CAAC;QAC5E,MAAM,OAAO,GAAG,CAAC,CAAS,EAAE,EAAE;YAC5B,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,CAAC;gBACtC,EAAE,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;YACT,CAAC;YACD,EAAE,CAA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*/
3
+ export interface FeedEvent {
4
+ event: string;
5
+ data: unknown;
6
+ }
7
+ /**
8
+ * The manager's local HTTP + WebSocket face. It hosts the **console** (a
9
+ * lightweight xterm.js page) and bridges each agent's PTY to the browser — and to
10
+ * `cotal attach` — over a direct socket, never the mesh, so owning the terminal
11
+ * keeps the manager off the message hot path. Bound to loopback.
12
+ *
13
+ * Routes: `GET /` console page, `GET /agents` the managed roster (JSON),
14
+ * `GET /feed` the live mesh feed (SSE: presence roster + comms), static assets
15
+ * under `/assets`, and `WS /attach/<name>` the PTY stream.
16
+ *
17
+ * Attach protocol: server → client sends raw terminal bytes (binary). client →
18
+ * server: binary frames are keystrokes; a text frame `r:<cols>,<rows>` resizes.
19
+ */
20
+ export declare class AttachEndpoint {
21
+ #private;
22
+ private readonly lookup;
23
+ private readonly list;
24
+ /** Events replayed to each console as it connects to `/feed` (e.g. the current roster). */
25
+ private readonly snapshot;
26
+ constructor(lookup: (name: string) => AgentHandle | undefined, list: () => unknown,
27
+ /** Events replayed to each console as it connects to `/feed` (e.g. the current roster). */
28
+ snapshot: () => FeedEvent[], port?: number);
29
+ start(): Promise<void>;
30
+ stop(): Promise<void>;
31
+ /** Push a named event to every connected console (SSE). */
32
+ publish(event: string, data: unknown): void;
33
+ /** The ws URL a client uses to attach to `name`. */
34
+ url(name: string): string;
35
+ /** The console page URL. */
36
+ consoleUrl(): string;
37
+ }
38
+ //# sourceMappingURL=attach-endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attach-endpoint.d.ts","sourceRoot":"","sources":["../src/attach-endpoint.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAmBtD,sEAAsE;AACtE,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,cAAc;;IAQvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,2FAA2F;IAC3F,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAHR,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,WAAW,GAAG,SAAS,EACjD,IAAI,EAAE,MAAM,OAAO;IACpC,2FAA2F;IAC1E,QAAQ,EAAE,MAAM,SAAS,EAAE,EAC5C,IAAI,SAAI;IAiBJ,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAUtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ3B,2DAA2D;IAC3D,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI;IAI3C,oDAAoD;IACpD,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAIzB,4BAA4B;IAC5B,UAAU,IAAI,MAAM;CA6ErB"}
@@ -0,0 +1,171 @@
1
+ import { createServer } from "node:http";
2
+ import { createRequire } from "node:module";
3
+ import { readFileSync } from "node:fs";
4
+ import { fileURLToPath } from "node:url";
5
+ import { dirname, join } from "node:path";
6
+ import { WebSocketServer } from "ws";
7
+ const require = createRequire(import.meta.url);
8
+ const here = dirname(fileURLToPath(import.meta.url));
9
+ /** Vendored xterm.js assets (served from node_modules, resolved at startup). */
10
+ const ASSETS = {
11
+ "/assets/xterm.js": { path: require.resolve("@xterm/xterm/lib/xterm.js"), type: "text/javascript" },
12
+ "/assets/xterm.css": { path: require.resolve("@xterm/xterm/css/xterm.css"), type: "text/css" },
13
+ "/assets/addon-fit.js": { path: require.resolve("@xterm/addon-fit/lib/addon-fit.js"), type: "text/javascript" },
14
+ "/assets/addon-attach.js": { path: require.resolve("@xterm/addon-attach/lib/addon-attach.js"), type: "text/javascript" },
15
+ };
16
+ /** Anything the console page needs to render itself, served from this dir. */
17
+ const PAGE = {
18
+ "/": { path: join(here, "console/index.html"), type: "text/html" },
19
+ "/app.js": { path: join(here, "console/app.js"), type: "text/javascript" },
20
+ };
21
+ /**
22
+ * The manager's local HTTP + WebSocket face. It hosts the **console** (a
23
+ * lightweight xterm.js page) and bridges each agent's PTY to the browser — and to
24
+ * `cotal attach` — over a direct socket, never the mesh, so owning the terminal
25
+ * keeps the manager off the message hot path. Bound to loopback.
26
+ *
27
+ * Routes: `GET /` console page, `GET /agents` the managed roster (JSON),
28
+ * `GET /feed` the live mesh feed (SSE: presence roster + comms), static assets
29
+ * under `/assets`, and `WS /attach/<name>` the PTY stream.
30
+ *
31
+ * Attach protocol: server → client sends raw terminal bytes (binary). client →
32
+ * server: binary frames are keystrokes; a text frame `r:<cols>,<rows>` resizes.
33
+ */
34
+ export class AttachEndpoint {
35
+ lookup;
36
+ list;
37
+ snapshot;
38
+ #http;
39
+ #wss;
40
+ #port;
41
+ #sse = new Set();
42
+ #ping;
43
+ constructor(lookup, list,
44
+ /** Events replayed to each console as it connects to `/feed` (e.g. the current roster). */
45
+ snapshot, port = 0) {
46
+ this.lookup = lookup;
47
+ this.list = list;
48
+ this.snapshot = snapshot;
49
+ this.#port = port;
50
+ this.#http = createServer((req, res) => this.#onRequest(req, res));
51
+ this.#wss = new WebSocketServer({ noServer: true });
52
+ this.#http.on("upgrade", (req, socket, head) => {
53
+ const path = req.url ?? "/";
54
+ if (!path.startsWith("/attach/")) {
55
+ socket.destroy();
56
+ return;
57
+ }
58
+ this.#wss.handleUpgrade(req, socket, head, (ws) => this.#onConnection(ws, decodeURIComponent(path.slice("/attach/".length))));
59
+ });
60
+ }
61
+ async start() {
62
+ await new Promise((resolve) => this.#http.listen(this.#port, "127.0.0.1", resolve));
63
+ const addr = this.#http.address();
64
+ if (addr && typeof addr === "object")
65
+ this.#port = addr.port;
66
+ // Comment ping keeps idle SSE connections from being dropped; no-op with none.
67
+ this.#ping = setInterval(() => {
68
+ for (const res of this.#sse)
69
+ if (!res.writableEnded)
70
+ res.write(": ping\n\n");
71
+ }, 20_000);
72
+ }
73
+ async stop() {
74
+ if (this.#ping)
75
+ clearInterval(this.#ping);
76
+ for (const res of this.#sse)
77
+ res.end();
78
+ this.#sse.clear();
79
+ for (const ws of this.#wss.clients)
80
+ ws.terminate();
81
+ await new Promise((resolve) => this.#http.close(() => resolve()));
82
+ }
83
+ /** Push a named event to every connected console (SSE). */
84
+ publish(event, data) {
85
+ for (const res of this.#sse)
86
+ this.#writeEvent(res, event, data);
87
+ }
88
+ /** The ws URL a client uses to attach to `name`. */
89
+ url(name) {
90
+ return `ws://127.0.0.1:${this.#port}/attach/${encodeURIComponent(name)}`;
91
+ }
92
+ /** The console page URL. */
93
+ consoleUrl() {
94
+ return `http://127.0.0.1:${this.#port}/`;
95
+ }
96
+ #onRequest(req, res) {
97
+ const path = (req.url ?? "/").split("?")[0];
98
+ if (path === "/agents") {
99
+ res.writeHead(200, { "content-type": "application/json" });
100
+ res.end(JSON.stringify(this.list()));
101
+ return;
102
+ }
103
+ if (path === "/feed") {
104
+ this.#openFeed(res);
105
+ return;
106
+ }
107
+ const file = PAGE[path] ?? ASSETS[path];
108
+ if (file) {
109
+ res.writeHead(200, { "content-type": file.type });
110
+ res.end(readFileSync(file.path));
111
+ return;
112
+ }
113
+ res.writeHead(404).end("not found");
114
+ }
115
+ /** Open an SSE stream, replay the snapshot, and keep it on the broadcast set. */
116
+ #openFeed(res) {
117
+ res.writeHead(200, {
118
+ "content-type": "text/event-stream",
119
+ "cache-control": "no-cache",
120
+ connection: "keep-alive",
121
+ });
122
+ res.write("retry: 2000\n\n");
123
+ this.#sse.add(res);
124
+ try {
125
+ for (const ev of this.snapshot())
126
+ this.#writeEvent(res, ev.event, ev.data);
127
+ }
128
+ catch {
129
+ /* mesh not up yet — the console will get live events once it is */
130
+ }
131
+ res.on("close", () => this.#sse.delete(res));
132
+ }
133
+ #writeEvent(res, event, data) {
134
+ if (res.writableEnded)
135
+ return;
136
+ res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
137
+ }
138
+ #onConnection(ws, name) {
139
+ const handle = this.lookup(name);
140
+ if (!handle || handle.status() !== "running") {
141
+ ws.close();
142
+ return;
143
+ }
144
+ const session = handle.attach();
145
+ const backlog = session.backlog();
146
+ if (backlog.length)
147
+ ws.send(backlog);
148
+ const offData = session.onData((chunk) => {
149
+ if (ws.readyState === ws.OPEN)
150
+ ws.send(chunk);
151
+ });
152
+ const offExit = session.onExit(() => ws.close());
153
+ ws.on("message", (data, isBinary) => {
154
+ if (isBinary) {
155
+ session.write(data.toString("utf8"));
156
+ return;
157
+ }
158
+ const text = data.toString();
159
+ const m = /^r:(\d+),(\d+)$/.exec(text);
160
+ if (m)
161
+ session.resize(Number(m[1]), Number(m[2]));
162
+ else
163
+ session.write(text);
164
+ });
165
+ ws.on("close", () => {
166
+ offData();
167
+ offExit();
168
+ });
169
+ }
170
+ }
171
+ //# sourceMappingURL=attach-endpoint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attach-endpoint.js","sourceRoot":"","sources":["../src/attach-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA0D,MAAM,WAAW,CAAC;AACjG,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAkB,MAAM,IAAI,CAAC;AAGrD,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,IAAI,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAErD,gFAAgF;AAChF,MAAM,MAAM,GAAmD;IAC7D,kBAAkB,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,2BAA2B,CAAC,EAAE,IAAI,EAAE,iBAA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AI,CAAC;gBAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;gBAC7C,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;QACH,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;YAClB,OAAO,EAAE,CAAC;YACV,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=commands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":""}
@@ -0,0 +1,157 @@
1
+ import { parseArgs } from "node:util";
2
+ import { readFileSync } from "node:fs";
3
+ import { CotalEndpoint, isReachable, DEFAULT_SERVER, registry, } from "@cotal-ai/core";
4
+ import { attachClient } from "./attach-client.js";
5
+ import { c } from "./ui.js";
6
+ function parse(argv) {
7
+ const { values } = parseArgs({
8
+ args: argv,
9
+ allowPositionals: true,
10
+ options: {
11
+ space: { type: "string" },
12
+ server: { type: "string" },
13
+ name: { type: "string" },
14
+ role: { type: "string" },
15
+ agent: { type: "string" },
16
+ config: { type: "string" },
17
+ creds: { type: "string" },
18
+ },
19
+ });
20
+ return values;
21
+ }
22
+ /** Connect a short-lived client, send one control request to the manager, disconnect. */
23
+ async function ask(space, server, op, args, credsPath) {
24
+ const creds = credsPath ? readFileSync(credsPath, "utf8") : undefined;
25
+ if (!(await isReachable(server, { creds }))) {
26
+ console.error(c.red(`Can't reach NATS at ${server}. Run: cotal up`));
27
+ process.exit(1);
28
+ }
29
+ const ep = new CotalEndpoint({
30
+ space,
31
+ servers: server,
32
+ creds,
33
+ channels: [],
34
+ consume: false, // request/reply only — binds no consumers (and under auth has no pre-created DM durable)
35
+ registerPresence: false,
36
+ watchPresence: false,
37
+ card: { name: "cli", kind: "endpoint" },
38
+ });
39
+ ep.on("error", (e) => console.error(c.red("! " + e.message)));
40
+ await ep.start();
41
+ try {
42
+ return await ep.requestControl("manager", { op, args });
43
+ }
44
+ catch (e) {
45
+ return { ok: false, error: `no manager reachable (${e.message})` };
46
+ }
47
+ finally {
48
+ await ep.stop();
49
+ }
50
+ }
51
+ function failIfNotOk(reply) {
52
+ if (!reply.ok) {
53
+ console.error(c.red(`✗ ${reply.error ?? "error"}`));
54
+ process.exit(1);
55
+ }
56
+ }
57
+ async function start(argv) {
58
+ const v = parse(argv);
59
+ if (!v.name) {
60
+ console.error(c.red("--name is required"));
61
+ process.exit(1);
62
+ }
63
+ const reply = await ask(v.space ?? "demo", v.server ?? DEFAULT_SERVER, "start", {
64
+ name: v.name,
65
+ role: v.role,
66
+ agent: v.agent,
67
+ config: v.config,
68
+ }, v.creds);
69
+ failIfNotOk(reply);
70
+ const d = reply.data;
71
+ console.log(c.green(`✓ started ${c.bold(d.name)}`) +
72
+ c.dim(` (${d.role ?? "no role"} · ${d.agent} · ${d.mode})`));
73
+ }
74
+ async function stop(argv) {
75
+ const v = parse(argv);
76
+ if (!v.name) {
77
+ console.error(c.red("--name is required"));
78
+ process.exit(1);
79
+ }
80
+ const reply = await ask(v.space ?? "demo", v.server ?? DEFAULT_SERVER, "stop", {
81
+ name: v.name,
82
+ }, v.creds);
83
+ failIfNotOk(reply);
84
+ console.log(c.dim(`✓ stopped ${v.name}`));
85
+ }
86
+ async function ps(argv) {
87
+ const v = parse(argv);
88
+ const reply = await ask(v.space ?? "demo", v.server ?? DEFAULT_SERVER, "ps", undefined, v.creds);
89
+ failIfNotOk(reply);
90
+ const rows = reply.data ?? [];
91
+ if (!rows.length) {
92
+ console.log(c.dim("(no managed agents)"));
93
+ return;
94
+ }
95
+ for (const r of rows) {
96
+ const status = r.mesh === "absent"
97
+ ? c.yellow("starting…")
98
+ : r.mesh === "offline"
99
+ ? c.dim("offline")
100
+ : r.mesh === "working"
101
+ ? c.green("working")
102
+ : r.mesh === "waiting"
103
+ ? c.yellow("waiting")
104
+ : c.cyan(r.mesh);
105
+ console.log(`${c.bold(r.name)}${r.role ? c.dim("/" + r.role) : ""} ${c.dim(r.agent + " · " + r.mode)} ${status}`);
106
+ }
107
+ }
108
+ async function attach(argv) {
109
+ const v = parse(argv);
110
+ if (!v.name) {
111
+ console.error(c.red("--name is required"));
112
+ process.exit(1);
113
+ }
114
+ const reply = await ask(v.space ?? "demo", v.server ?? DEFAULT_SERVER, "attach", {
115
+ name: v.name,
116
+ }, v.creds);
117
+ failIfNotOk(reply);
118
+ const { ws } = reply.data;
119
+ console.error(c.dim(`attached to ${v.name} — Ctrl-] to detach`));
120
+ await attachClient(ws);
121
+ console.error(c.dim(`\ndetached from ${v.name}`));
122
+ }
123
+ /** The manager's control-plane commands — thin NATS request/reply clients that
124
+ * drive a running manager. Self-registered on import; the `cotal` binary resolves
125
+ * them from the registry. */
126
+ const managerCommands = [
127
+ {
128
+ kind: "command",
129
+ name: "start",
130
+ group: "Control plane",
131
+ summary: "ask the manager to spawn an agent — --name <n> [--role <r>] [--agent <a>] [--config <file>] (auto-discovers .cotal/agents/<n>.md)",
132
+ run: start,
133
+ },
134
+ {
135
+ kind: "command",
136
+ name: "stop",
137
+ group: "Control plane",
138
+ summary: "ask the manager to stop an agent — --name <n>",
139
+ run: stop,
140
+ },
141
+ {
142
+ kind: "command",
143
+ name: "ps",
144
+ group: "Control plane",
145
+ summary: "list managed agents + their mesh status",
146
+ run: ps,
147
+ },
148
+ {
149
+ kind: "command",
150
+ name: "attach",
151
+ group: "Control plane",
152
+ summary: "stream + drive an agent's terminal (pty runtime) — --name <n>",
153
+ run: attach,
154
+ },
155
+ ];
156
+ registry.register(...managerCommands);
157
+ //# sourceMappingURL=commands.js.map