@celljs/ta-host 0.0.0-stage → 4.5.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.
Files changed (155) hide show
  1. package/README.md +33 -2
  2. package/cell.schema.json +33 -0
  3. package/cell.yml +14 -0
  4. package/esm/node/client-channel.d.ts +31 -0
  5. package/esm/node/client-channel.js +84 -0
  6. package/esm/node/client-channel.js.map +1 -0
  7. package/esm/node/event-log.d.ts +28 -0
  8. package/esm/node/event-log.js +48 -0
  9. package/esm/node/event-log.js.map +1 -0
  10. package/esm/node/host-protocol.d.ts +27 -0
  11. package/esm/node/host-protocol.js +4 -0
  12. package/esm/node/host-protocol.js.map +1 -0
  13. package/esm/node/index.d.ts +13 -0
  14. package/esm/node/index.js +14 -0
  15. package/esm/node/index.js.map +1 -0
  16. package/esm/node/local-exec-environment-factory.d.ts +4 -0
  17. package/esm/node/local-exec-environment-factory.js +18 -0
  18. package/esm/node/local-exec-environment-factory.js.map +1 -0
  19. package/esm/node/module.d.ts +3 -0
  20. package/esm/node/module.js +4 -0
  21. package/esm/node/module.js.map +1 -0
  22. package/esm/node/receipt-book.d.ts +12 -0
  23. package/esm/node/receipt-book.js +38 -0
  24. package/esm/node/receipt-book.js.map +1 -0
  25. package/esm/node/remote-ui-context.d.ts +63 -0
  26. package/esm/node/remote-ui-context.js +130 -0
  27. package/esm/node/remote-ui-context.js.map +1 -0
  28. package/esm/node/ta-access.d.ts +30 -0
  29. package/esm/node/ta-access.js +107 -0
  30. package/esm/node/ta-access.js.map +1 -0
  31. package/esm/node/ta-errors.d.ts +11 -0
  32. package/esm/node/ta-errors.js +44 -0
  33. package/esm/node/ta-errors.js.map +1 -0
  34. package/esm/node/ta-event-util.d.ts +11 -0
  35. package/esm/node/ta-event-util.js +20 -0
  36. package/esm/node/ta-event-util.js.map +1 -0
  37. package/esm/node/ta-server-impl.d.ts +50 -0
  38. package/esm/node/ta-server-impl.js +226 -0
  39. package/esm/node/ta-server-impl.js.map +1 -0
  40. package/esm/node/ta-session-factory.d.ts +32 -0
  41. package/esm/node/ta-session-factory.js +151 -0
  42. package/esm/node/ta-session-factory.js.map +1 -0
  43. package/esm/node/ta-session-registry.d.ts +20 -0
  44. package/esm/node/ta-session-registry.js +85 -0
  45. package/esm/node/ta-session-registry.js.map +1 -0
  46. package/esm/node/ta-session.d.ts +53 -0
  47. package/esm/node/ta-session.js +157 -0
  48. package/esm/node/ta-session.js.map +1 -0
  49. package/esm/package.json +3 -0
  50. package/lib/node/client-channel.d.ts +32 -0
  51. package/lib/node/client-channel.d.ts.map +1 -0
  52. package/lib/node/client-channel.js +88 -0
  53. package/lib/node/client-channel.js.map +1 -0
  54. package/lib/node/client-channel.spec.d.ts +2 -0
  55. package/lib/node/client-channel.spec.d.ts.map +1 -0
  56. package/lib/node/client-channel.spec.js +106 -0
  57. package/lib/node/client-channel.spec.js.map +1 -0
  58. package/lib/node/event-log.d.ts +29 -0
  59. package/lib/node/event-log.d.ts.map +1 -0
  60. package/lib/node/event-log.js +52 -0
  61. package/lib/node/event-log.js.map +1 -0
  62. package/lib/node/event-log.spec.d.ts +2 -0
  63. package/lib/node/event-log.spec.d.ts.map +1 -0
  64. package/lib/node/event-log.spec.js +48 -0
  65. package/lib/node/event-log.spec.js.map +1 -0
  66. package/lib/node/host-protocol.d.ts +28 -0
  67. package/lib/node/host-protocol.d.ts.map +1 -0
  68. package/lib/node/host-protocol.js +7 -0
  69. package/lib/node/host-protocol.js.map +1 -0
  70. package/lib/node/index.d.ts +14 -0
  71. package/lib/node/index.d.ts.map +1 -0
  72. package/lib/node/index.js +30 -0
  73. package/lib/node/index.js.map +1 -0
  74. package/lib/node/local-exec-environment-factory.d.ts +5 -0
  75. package/lib/node/local-exec-environment-factory.d.ts.map +1 -0
  76. package/lib/node/local-exec-environment-factory.js +21 -0
  77. package/lib/node/local-exec-environment-factory.js.map +1 -0
  78. package/lib/node/module.d.ts +4 -0
  79. package/lib/node/module.d.ts.map +1 -0
  80. package/lib/node/module.js +6 -0
  81. package/lib/node/module.js.map +1 -0
  82. package/lib/node/receipt-book.d.ts +13 -0
  83. package/lib/node/receipt-book.d.ts.map +1 -0
  84. package/lib/node/receipt-book.js +42 -0
  85. package/lib/node/receipt-book.js.map +1 -0
  86. package/lib/node/receipt-book.spec.d.ts +2 -0
  87. package/lib/node/receipt-book.spec.d.ts.map +1 -0
  88. package/lib/node/receipt-book.spec.js +39 -0
  89. package/lib/node/receipt-book.spec.js.map +1 -0
  90. package/lib/node/remote-ui-context.d.ts +64 -0
  91. package/lib/node/remote-ui-context.d.ts.map +1 -0
  92. package/lib/node/remote-ui-context.js +134 -0
  93. package/lib/node/remote-ui-context.js.map +1 -0
  94. package/lib/node/remote-ui-context.spec.d.ts +2 -0
  95. package/lib/node/remote-ui-context.spec.d.ts.map +1 -0
  96. package/lib/node/remote-ui-context.spec.js +62 -0
  97. package/lib/node/remote-ui-context.spec.js.map +1 -0
  98. package/lib/node/ta-access.d.ts +31 -0
  99. package/lib/node/ta-access.d.ts.map +1 -0
  100. package/lib/node/ta-access.js +110 -0
  101. package/lib/node/ta-access.js.map +1 -0
  102. package/lib/node/ta-access.spec.d.ts +2 -0
  103. package/lib/node/ta-access.spec.d.ts.map +1 -0
  104. package/lib/node/ta-access.spec.js +25 -0
  105. package/lib/node/ta-access.spec.js.map +1 -0
  106. package/lib/node/ta-errors.d.ts +12 -0
  107. package/lib/node/ta-errors.d.ts.map +1 -0
  108. package/lib/node/ta-errors.js +47 -0
  109. package/lib/node/ta-errors.js.map +1 -0
  110. package/lib/node/ta-event-util.d.ts +12 -0
  111. package/lib/node/ta-event-util.d.ts.map +1 -0
  112. package/lib/node/ta-event-util.js +23 -0
  113. package/lib/node/ta-event-util.js.map +1 -0
  114. package/lib/node/ta-event-util.spec.d.ts +2 -0
  115. package/lib/node/ta-event-util.spec.d.ts.map +1 -0
  116. package/lib/node/ta-event-util.spec.js +28 -0
  117. package/lib/node/ta-event-util.spec.js.map +1 -0
  118. package/lib/node/ta-server-impl.d.ts +51 -0
  119. package/lib/node/ta-server-impl.d.ts.map +1 -0
  120. package/lib/node/ta-server-impl.js +229 -0
  121. package/lib/node/ta-server-impl.js.map +1 -0
  122. package/lib/node/ta-session-factory.d.ts +33 -0
  123. package/lib/node/ta-session-factory.d.ts.map +1 -0
  124. package/lib/node/ta-session-factory.js +154 -0
  125. package/lib/node/ta-session-factory.js.map +1 -0
  126. package/lib/node/ta-session-registry.d.ts +21 -0
  127. package/lib/node/ta-session-registry.d.ts.map +1 -0
  128. package/lib/node/ta-session-registry.js +86 -0
  129. package/lib/node/ta-session-registry.js.map +1 -0
  130. package/lib/node/ta-session.d.ts +54 -0
  131. package/lib/node/ta-session.d.ts.map +1 -0
  132. package/lib/node/ta-session.js +161 -0
  133. package/lib/node/ta-session.js.map +1 -0
  134. package/package.json +80 -4
  135. package/src/node/client-channel.spec.ts +117 -0
  136. package/src/node/client-channel.ts +94 -0
  137. package/src/node/event-log.spec.ts +52 -0
  138. package/src/node/event-log.ts +59 -0
  139. package/src/node/host-protocol.ts +32 -0
  140. package/src/node/index.ts +13 -0
  141. package/src/node/local-exec-environment-factory.ts +9 -0
  142. package/src/node/module.ts +4 -0
  143. package/src/node/receipt-book.spec.ts +41 -0
  144. package/src/node/receipt-book.ts +40 -0
  145. package/src/node/remote-ui-context.spec.ts +68 -0
  146. package/src/node/remote-ui-context.ts +168 -0
  147. package/src/node/ta-access.spec.ts +27 -0
  148. package/src/node/ta-access.ts +98 -0
  149. package/src/node/ta-errors.ts +55 -0
  150. package/src/node/ta-event-util.spec.ts +29 -0
  151. package/src/node/ta-event-util.ts +23 -0
  152. package/src/node/ta-server-impl.ts +264 -0
  153. package/src/node/ta-session-factory.ts +156 -0
  154. package/src/node/ta-session-registry.ts +75 -0
  155. package/src/node/ta-session.ts +193 -0
@@ -0,0 +1,130 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ const TEXT_STYLES = new Set(['fg', 'bg', 'bold', 'italic', 'underline', 'strikethrough', 'inverse']);
3
+ /** Clients draw their own UI: an extension's styled text reaches them without ANSI codes. */
4
+ const PLAIN_THEME = new Proxy({}, {
5
+ get: (_target, name) => (TEXT_STYLES.has(String(name)) ? (...args) => args[args.length - 1] : () => ''),
6
+ });
7
+ /**
8
+ * pi's extension UI for clients that are not this process. It has the shape of pi's RPC mode, with one
9
+ * difference: a dialog goes to every attached client, the first answer counts, and the others are told.
10
+ * A dialog nobody answers ends at its timeout, as the extension asked.
11
+ */
12
+ export class RemoteUiContext {
13
+ constructor(publish) {
14
+ this.publish = publish;
15
+ this.pending = new Map();
16
+ this.theme = PLAIN_THEME;
17
+ }
18
+ get pendingRequests() {
19
+ return [...this.pending.values()].map(dialog => dialog.request);
20
+ }
21
+ /** False when the dialog was answered before or ended. */
22
+ respond(response) {
23
+ const dialog = this.pending.get(response.id);
24
+ dialog === null || dialog === void 0 ? void 0 : dialog.settle(response);
25
+ return dialog !== undefined;
26
+ }
27
+ /** Ends the open dialogs as if they timed out: the extensions go on, with their defaults. */
28
+ cancelAll() {
29
+ for (const dialog of [...this.pending.values()]) {
30
+ dialog.settle(undefined);
31
+ }
32
+ }
33
+ select(title, options, opts) {
34
+ return this.dialog({ method: 'select', title, options, timeout: opts === null || opts === void 0 ? void 0 : opts.timeout }, opts, undefined, r => ('value' in r ? r.value : undefined));
35
+ }
36
+ confirm(title, message, opts) {
37
+ return this.dialog({ method: 'confirm', title, message, timeout: opts === null || opts === void 0 ? void 0 : opts.timeout }, opts, false, r => ('confirmed' in r ? r.confirmed : false));
38
+ }
39
+ input(title, placeholder, opts) {
40
+ return this.dialog({ method: 'input', title, placeholder, timeout: opts === null || opts === void 0 ? void 0 : opts.timeout }, opts, undefined, r => ('value' in r ? r.value : undefined));
41
+ }
42
+ editor(title, prefill) {
43
+ return this.dialog({ method: 'editor', title, prefill }, undefined, undefined, r => ('value' in r ? r.value : undefined));
44
+ }
45
+ notify(message, type) {
46
+ this.tell({ method: 'notify', message, notifyType: type });
47
+ }
48
+ setStatus(key, text) {
49
+ this.tell({ method: 'setStatus', statusKey: key, statusText: text });
50
+ }
51
+ setWidget(key, content, options) {
52
+ // Clients cannot run a component factory; pi's RPC mode ignores those too.
53
+ if (content === undefined || Array.isArray(content)) {
54
+ this.tell({ method: 'setWidget', widgetKey: key, widgetLines: content, widgetPlacement: options === null || options === void 0 ? void 0 : options.placement });
55
+ }
56
+ }
57
+ setTitle(title) {
58
+ this.tell({ method: 'setTitle', title });
59
+ }
60
+ setEditorText(text) {
61
+ this.tell({ method: 'set_editor_text', text });
62
+ }
63
+ pasteToEditor(text) {
64
+ this.setEditorText(text);
65
+ }
66
+ getEditorText() {
67
+ return '';
68
+ }
69
+ // What needs a terminal in this process does nothing, as in pi's RPC mode.
70
+ onTerminalInput() {
71
+ return () => undefined;
72
+ }
73
+ setWorkingMessage() { }
74
+ setWorkingVisible() { }
75
+ setWorkingIndicator() { }
76
+ setHiddenThinkingLabel() { }
77
+ setFooter() { }
78
+ setHeader() { }
79
+ addAutocompleteProvider() { }
80
+ setEditorComponent() { }
81
+ getEditorComponent() {
82
+ return undefined;
83
+ }
84
+ async custom() {
85
+ return undefined;
86
+ }
87
+ getAllThemes() {
88
+ return [];
89
+ }
90
+ getTheme() {
91
+ return undefined;
92
+ }
93
+ setTheme() {
94
+ return { success: false, error: 'Clients of ta serve choose their own theme.' };
95
+ }
96
+ getToolsExpanded() {
97
+ return false;
98
+ }
99
+ setToolsExpanded() { }
100
+ tell(request) {
101
+ this.publish({ type: 'extension_ui_request', id: randomUUID(), ...request });
102
+ }
103
+ dialog(request, options, fallback, parse) {
104
+ var _a;
105
+ if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
106
+ return Promise.resolve(fallback);
107
+ }
108
+ const id = randomUUID();
109
+ return new Promise(resolve => {
110
+ var _a;
111
+ const settle = (response) => {
112
+ var _a;
113
+ clearTimeout(timer);
114
+ (_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', onAbort);
115
+ this.pending.delete(id);
116
+ this.publish({ type: 'ui_request_resolved', id });
117
+ resolve(!response || ('cancelled' in response && response.cancelled)
118
+ ? fallback
119
+ : parse(response));
120
+ };
121
+ const onAbort = () => settle(undefined);
122
+ const timer = (options === null || options === void 0 ? void 0 : options.timeout) ? setTimeout(onAbort, options.timeout) : undefined;
123
+ (_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', onAbort, { once: true });
124
+ const full = { type: 'extension_ui_request', id, ...request };
125
+ this.pending.set(id, { request: full, settle });
126
+ this.publish(full);
127
+ });
128
+ }
129
+ }
130
+ //# sourceMappingURL=remote-ui-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remote-ui-context.js","sourceRoot":"","sources":["../../src/node/remote-ui-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAczC,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,CAAC;AAErG,6FAA6F;AAC7F,MAAM,WAAW,GAAG,IAAI,KAAK,CACzB,EAAE,EACF;IACI,GAAG,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAc,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC;CACpH,CACK,CAAC;AAEX;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAKxB,YAA+B,OAAiC;QAAjC,YAAO,GAAP,OAAO,CAA0B;QAJ7C,YAAO,GAAG,IAAI,GAAG,EA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AO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,EAAC,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YAClF,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,0CAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YACpE,MAAM,IAAI,GAAG,EAAE,IAAI,EAAE,sBAAsB,EAAE,EAAE,EAAE,GAAG,OAAO,EAA2B,CAAC;YACvF,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;YAChD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;IACP,CAAC;CACJ"}
@@ -0,0 +1,30 @@
1
+ import type { IncomingMessage, Server } from 'node:http';
2
+ import { type Application, ApplicationLifecycle } from '@celljs/core';
3
+ import { RpcWebSocketGuard } from '@celljs/rpc/lib/node';
4
+ import { type Context, Middleware, ServerAware } from '@celljs/web/lib/node';
5
+ import { TaSessionRegistry } from './host-protocol.js';
6
+ export declare namespace TaAccessUtil {
7
+ /** From `Authorization: Bearer`, or `?token=` for browsers, whose WebSocket API cannot set headers. */
8
+ function tokenOf(request: IncomingMessage): string | undefined;
9
+ function equals(a: string, b: string): boolean;
10
+ /** A page that rebinds its domain to 127.0.0.1 reaches the server with its own name in `Host`. */
11
+ function isLoopbackHost(host: string | undefined): boolean;
12
+ }
13
+ /**
14
+ * The token of this server: `TA_SERVE_TOKEN`, which a parent process (the desktop app) can choose, or a
15
+ * random one. It lets in connections and requests, and is published in a file only this user can read.
16
+ */
17
+ export declare class TaAccess implements RpcWebSocketGuard, Middleware, ServerAware, ApplicationLifecycle<Application> {
18
+ readonly priority = 10000;
19
+ readonly token: string;
20
+ protected readonly host?: string;
21
+ protected readonly sessions: TaSessionRegistry;
22
+ protected readonly infoFile: string;
23
+ allows(request: IncomingMessage): boolean;
24
+ /** Calls over a WebSocket were checked when it opened; this covers plain HTTP. */
25
+ handle(ctx: Context, next: () => Promise<void>): Promise<void>;
26
+ setServer(server: Server): Promise<void>;
27
+ onStop(): Promise<void>;
28
+ protected acceptsHost(request: IncomingMessage): boolean;
29
+ protected publish(server: Server): void;
30
+ }
@@ -0,0 +1,107 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { randomBytes, timingSafeEqual } from 'node:crypto';
11
+ import { chmodSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
12
+ import { join } from 'node:path';
13
+ import { ApplicationLifecycle, Autowired, Component, Value } from '@celljs/core';
14
+ import { RpcContext, RpcWebSocketGuard } from '@celljs/rpc/lib/node';
15
+ import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
16
+ import { TA_SERVE_FILE, TA_WEBSOCKET_PATH } from '@celljs/ta-protocol';
17
+ import { Middleware, ServerAware } from '@celljs/web/lib/node';
18
+ import { TaSessionRegistry } from './host-protocol.js';
19
+ const LOOPBACK_NAMES = new Set(['127.0.0.1', 'localhost', '[::1]']);
20
+ /** Runs before everything else that handles a request. */
21
+ const ACCESS_MIDDLEWARE_PRIORITY = 10000;
22
+ export var TaAccessUtil;
23
+ (function (TaAccessUtil) {
24
+ /** From `Authorization: Bearer`, or `?token=` for browsers, whose WebSocket API cannot set headers. */
25
+ function tokenOf(request) {
26
+ var _a, _b;
27
+ const header = request.headers.authorization;
28
+ if (header === null || header === void 0 ? void 0 : header.startsWith('Bearer ')) {
29
+ return header.slice('Bearer '.length);
30
+ }
31
+ return (_b = new URL((_a = request.url) !== null && _a !== void 0 ? _a : '/', 'http://localhost').searchParams.get('token')) !== null && _b !== void 0 ? _b : undefined;
32
+ }
33
+ TaAccessUtil.tokenOf = tokenOf;
34
+ function equals(a, b) {
35
+ const [left, right] = [Buffer.from(a), Buffer.from(b)];
36
+ return left.length === right.length && timingSafeEqual(left, right);
37
+ }
38
+ TaAccessUtil.equals = equals;
39
+ /** A page that rebinds its domain to 127.0.0.1 reaches the server with its own name in `Host`. */
40
+ function isLoopbackHost(host) {
41
+ return LOOPBACK_NAMES.has((host !== null && host !== void 0 ? host : '').replace(/:\d+$/, ''));
42
+ }
43
+ TaAccessUtil.isLoopbackHost = isLoopbackHost;
44
+ })(TaAccessUtil || (TaAccessUtil = {}));
45
+ /**
46
+ * The token of this server: `TA_SERVE_TOKEN`, which a parent process (the desktop app) can choose, or a
47
+ * random one. It lets in connections and requests, and is published in a file only this user can read.
48
+ */
49
+ let TaAccess = class TaAccess {
50
+ constructor() {
51
+ this.priority = ACCESS_MIDDLEWARE_PRIORITY;
52
+ this.token = process.env.TA_SERVE_TOKEN || randomBytes(32).toString('base64url');
53
+ this.infoFile = join(TaBrand.agentDir(), TA_SERVE_FILE);
54
+ }
55
+ allows(request) {
56
+ const token = TaAccessUtil.tokenOf(request);
57
+ return token !== undefined && TaAccessUtil.equals(token, this.token) && this.acceptsHost(request);
58
+ }
59
+ /** Calls over a WebSocket were checked when it opened; this covers plain HTTP. */
60
+ async handle(ctx, next) {
61
+ if (RpcContext.connection() || this.allows(ctx.request)) {
62
+ await next();
63
+ return;
64
+ }
65
+ ctx.response.statusCode = 401;
66
+ ctx.response.end('Unauthorized');
67
+ }
68
+ async setServer(server) {
69
+ const publish = () => this.publish(server);
70
+ if (server.listening) {
71
+ publish();
72
+ }
73
+ else {
74
+ server.once('listening', publish);
75
+ }
76
+ }
77
+ async onStop() {
78
+ rmSync(this.infoFile, { force: true });
79
+ await this.sessions.closeAll();
80
+ }
81
+ acceptsHost(request) {
82
+ return !TaAccessUtil.isLoopbackHost(this.host) || TaAccessUtil.isLoopbackHost(request.headers.host);
83
+ }
84
+ publish(server) {
85
+ var _a;
86
+ const { port } = server.address();
87
+ const info = { url: `ws://${(_a = this.host) !== null && _a !== void 0 ? _a : '127.0.0.1'}:${port}${TA_WEBSOCKET_PATH}`, token: this.token, pid: process.pid };
88
+ mkdirSync(join(this.infoFile, '..'), { recursive: true });
89
+ writeFileSync(this.infoFile, JSON.stringify(info), { mode: 0o600 });
90
+ // The mode above only applies to a new file.
91
+ chmodSync(this.infoFile, 0o600);
92
+ console.log(`ta serve: ${info.url} (token in ${this.infoFile})`);
93
+ }
94
+ };
95
+ __decorate([
96
+ Value('server.host'),
97
+ __metadata("design:type", String)
98
+ ], TaAccess.prototype, "host", void 0);
99
+ __decorate([
100
+ Autowired(TaSessionRegistry),
101
+ __metadata("design:type", Object)
102
+ ], TaAccess.prototype, "sessions", void 0);
103
+ TaAccess = __decorate([
104
+ Component([RpcWebSocketGuard, Middleware, ServerAware, ApplicationLifecycle])
105
+ ], TaAccess);
106
+ export { TaAccess };
107
+ //# sourceMappingURL=ta-access.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ta-access.js","sourceRoot":"","sources":["../../src/node/ta-access.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGtE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAoB,oBAAoB,E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I,EAAE,KAAK,EAAE,CAAC,CAAC;QACpE,6CAA6C;QAC7C,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QAChC,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,CAAC,GAAG,cAAc,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;IACrE,CAAC;CACJ,CAAA;AAjDsB;IADlB,KAAK,CAAC,aAAa,CAAC;;sCACY;AAGd;IADlB,SAAS,CAAC,iBAAiB,CAAC;;0CACkB;AARtC,QAAQ;IADpB,SAAS,CAAC,CAAC,iBAAiB,EAAE,UAAU,EAAE,WAAW,EAAE,oBAAoB,CAAC,CAAC;GACjE,QAAQ,CAsDpB"}
@@ -0,0 +1,11 @@
1
+ /** The failures of {@link TaServer} calls, which reach the client with their message and code. */
2
+ export declare namespace TaErrors {
3
+ function sessionNotFound(sessionId: string): Error;
4
+ function sessionLocked(sessionFile: string): Error;
5
+ function tooManySessions(max: number): Error;
6
+ function messageConflict(clientMessageId: string): Error;
7
+ function modelNotFound(model: string): Error;
8
+ function invalidArgument(message: string, hint: string): Error;
9
+ /** A command that pi refused or that failed. Other errors would lose their message on the way out. */
10
+ function commandFailed(cause: unknown): Error;
11
+ }
@@ -0,0 +1,44 @@
1
+ import { ApplicationError } from '@celljs/core';
2
+ import { TA_RPC_ERROR_CODE, TaErrorCode } from '@celljs/ta-protocol';
3
+ const TaError = ApplicationError.declare(TA_RPC_ERROR_CODE, (code, message, hint) => ({
4
+ message,
5
+ data: { code, hint },
6
+ }));
7
+ /** The failures of {@link TaServer} calls, which reach the client with their message and code. */
8
+ export var TaErrors;
9
+ (function (TaErrors) {
10
+ function sessionNotFound(sessionId) {
11
+ return TaError(TaErrorCode.SESSION_NOT_FOUND, `No open session has the id ${sessionId}.`, 'Open it again with openSession and its session file: idle sessions are closed, their files stay.');
12
+ }
13
+ TaErrors.sessionNotFound = sessionNotFound;
14
+ function sessionLocked(sessionFile) {
15
+ return TaError(TaErrorCode.SESSION_LOCKED, `The session ${sessionFile} is open elsewhere.`, 'Close it there first: a session has one writer.');
16
+ }
17
+ TaErrors.sessionLocked = sessionLocked;
18
+ function tooManySessions(max) {
19
+ return TaError(TaErrorCode.TOO_MANY_SESSIONS, `${max} sessions are open, the limit of this server.`, 'Close a session, or raise ta.host.maxSessions.');
20
+ }
21
+ TaErrors.tooManySessions = tooManySessions;
22
+ function messageConflict(clientMessageId) {
23
+ return TaError(TaErrorCode.MESSAGE_CONFLICT, `The message id ${clientMessageId} was used for a different message.`, 'Use a new clientMessageId for each message.');
24
+ }
25
+ TaErrors.messageConflict = messageConflict;
26
+ function modelNotFound(model) {
27
+ return TaError(TaErrorCode.MODEL_NOT_FOUND, `The model ${model} is not known or has no credentials.`, 'Use <provider>/<model id>; getAvailableModels lists the ones that work.');
28
+ }
29
+ TaErrors.modelNotFound = modelNotFound;
30
+ function invalidArgument(message, hint) {
31
+ return TaError(TaErrorCode.INVALID_ARGUMENT, message, hint);
32
+ }
33
+ TaErrors.invalidArgument = invalidArgument;
34
+ /** A command that pi refused or that failed. Other errors would lose their message on the way out. */
35
+ function commandFailed(cause) {
36
+ if (ApplicationError.is(cause)) {
37
+ return cause;
38
+ }
39
+ const message = cause instanceof Error ? cause.message : String(cause);
40
+ return TaError(TaErrorCode.COMMAND_FAILED, message, 'The message comes from the agent; fix what it says and send the command again.');
41
+ }
42
+ TaErrors.commandFailed = commandFailed;
43
+ })(TaErrors || (TaErrors = {}));
44
+ //# sourceMappingURL=ta-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ta-errors.js","sourceRoot":"","sources":["../../src/node/ta-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAoB,MAAM,qBAAqB,CAAC;AAEvF,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAsB,iBAAiB,EAAE,CAAC,IAAiB,EAAE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sGAAsG;IACtG,SAAgB,aAAa,CAAC,KAAc;QACxC,IAAI,gBAAgB,CAAC,EAAE,CAAC,KAAe,CAAC,EAAE,CAAC;YACvC,OAAO,KAAc,CAAC;QAC1B,CAAC;QACD,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,OAAO,CAAC,WAAW,CAAC,cAAc,EAAE,OAAO,EAAE,gFAAgF,CAAC,CAAC;IAC1I,CAAC;IANe,SAAA,aAAa,gBAM5B,CAAA;AACL,CAAC,EA7CgB,QAAQ,KAAR,QAAQ,QA6CxB"}
@@ -0,0 +1,11 @@
1
+ import type { TaEvent } from '@celljs/ta-protocol';
2
+ type AgentSessionEvent = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).AgentSessionEvent;
3
+ export declare namespace TaEventUtil {
4
+ /**
5
+ * An event of pi as it goes on the wire, the JSON form of pi's RPC and JSON modes (pi does not export
6
+ * its function). pi's `message_update` holds the whole message so far: sent as is, a reply would cost
7
+ * the square of its length.
8
+ */
9
+ function toWire(event: AgentSessionEvent): TaEvent;
10
+ }
11
+ export {};
@@ -0,0 +1,20 @@
1
+ export var TaEventUtil;
2
+ (function (TaEventUtil) {
3
+ /**
4
+ * An event of pi as it goes on the wire, the JSON form of pi's RPC and JSON modes (pi does not export
5
+ * its function). pi's `message_update` holds the whole message so far: sent as is, a reply would cost
6
+ * the square of its length.
7
+ */
8
+ function toWire(event) {
9
+ var _a;
10
+ if (event.type !== 'message_update') {
11
+ return event;
12
+ }
13
+ const { partial, ...delta } = event.assistantMessageEvent;
14
+ const block = delta.type === 'toolcall_start' ? partial === null || partial === void 0 ? void 0 : partial.content[(_a = delta.contentIndex) !== null && _a !== void 0 ? _a : -1] : undefined;
15
+ const assistantMessageEvent = block ? { ...delta, id: block.id, toolName: block.name } : delta;
16
+ return { type: 'message_update', usage: event.message.usage, assistantMessageEvent };
17
+ }
18
+ TaEventUtil.toWire = toWire;
19
+ })(TaEventUtil || (TaEventUtil = {}));
20
+ //# sourceMappingURL=ta-event-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ta-event-util.js","sourceRoot":"","sources":["../../src/node/ta-event-util.ts"],"names":[],"mappings":"AAIA,MAAM,KAAW,WAAW,CAkB3B;AAlBD,WAAiB,WAAW;IACxB;;;;OAIG;IACH,SAAgB,MAAM,CAAC,KAAwB;;QAC3C,IAAI,KAAK,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YAClC,OAAO,KAAgB,CAAC;QAC5B,CAAC;QACD,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,EAAE,GAAG,KAAK,CAAC,qBAGnC,CAAC;QACF,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,KAAK,gBAAgB,CAAC,CAAC,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,OAAC,KAAK,CAAC,YAAY,mCAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACvG,MAAM,qBAAqB,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,QAAQ,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;QAC/F,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,KAAK,EAAG,KAAK,CAAC,OAA+B,CAAC,KAAK,EAAE,qBAAqB,EAAa,CAAC;IAC7H,CAAC;IAXe,YAAA,MAAM,SAWrB,CAAA;AACL,CAAC,EAlBgB,WAAW,KAAX,WAAW,QAkB3B"}
@@ -0,0 +1,50 @@
1
+ import { type RpcExtensionUIResponse, type TaAttachResult, type TaClientToolResult, type TaCommandResult, type TaOpenSessionOptions, type TaPromptInput, type TaPromptReceipt, TaServer, type TaSessionInfo, type ThinkingLevel } from '@celljs/ta-protocol';
2
+ import { TaSessionRegistry } from './host-protocol.js';
3
+ import type { TaSession } from './ta-session.js';
4
+ /** Each command does what the command of the same name does in pi's RPC mode. */
5
+ export declare class TaServerImpl implements TaServer {
6
+ protected readonly sessions: TaSessionRegistry;
7
+ openSession(options: TaOpenSessionOptions): Promise<TaSessionInfo>;
8
+ closeSession(sessionId: string): Promise<void>;
9
+ listSessions(): Promise<TaSessionInfo[]>;
10
+ attach(sessionId: string, afterSeq?: number): Promise<TaAttachResult>;
11
+ detach(sessionId: string): Promise<void>;
12
+ respondUi(sessionId: string, response: RpcExtensionUIResponse): Promise<boolean>;
13
+ /** Client tools are declared in the protocol and not run yet: nobody can be waiting. */
14
+ respondClientTool(_sessionId: string, _result: TaClientToolResult): Promise<boolean>;
15
+ prompt(sessionId: string, input: TaPromptInput): Promise<TaPromptReceipt>;
16
+ steer(sessionId: string, input: TaPromptInput): Promise<TaPromptReceipt>;
17
+ followUp(sessionId: string, input: TaPromptInput): Promise<TaPromptReceipt>;
18
+ abort(sessionId: string): Promise<void>;
19
+ clearQueue(sessionId: string): Promise<TaCommandResult<'clear_queue'>>;
20
+ newSession(sessionId: string, parentSession?: string): Promise<TaCommandResult<'new_session'>>;
21
+ getState(sessionId: string): Promise<TaCommandResult<'get_state'>>;
22
+ setModel(sessionId: string, provider: string, modelId: string): Promise<TaCommandResult<'set_model'>>;
23
+ cycleModel(sessionId: string): Promise<TaCommandResult<'cycle_model'>>;
24
+ getAvailableModels(sessionId: string): Promise<TaCommandResult<'get_available_models'>>;
25
+ setThinkingLevel(sessionId: string, level: ThinkingLevel): Promise<void>;
26
+ cycleThinkingLevel(sessionId: string): Promise<TaCommandResult<'cycle_thinking_level'>>;
27
+ getAvailableThinkingLevels(sessionId: string): Promise<TaCommandResult<'get_available_thinking_levels'>>;
28
+ setSteeringMode(sessionId: string, mode: 'all' | 'one-at-a-time'): Promise<void>;
29
+ setFollowUpMode(sessionId: string, mode: 'all' | 'one-at-a-time'): Promise<void>;
30
+ compact(sessionId: string, customInstructions?: string): Promise<TaCommandResult<'compact'>>;
31
+ setAutoCompaction(sessionId: string, enabled: boolean): Promise<void>;
32
+ setAutoRetry(sessionId: string, enabled: boolean): Promise<void>;
33
+ abortRetry(sessionId: string): Promise<void>;
34
+ bash(sessionId: string, command: string, excludeFromContext?: boolean): Promise<TaCommandResult<'bash'>>;
35
+ abortBash(sessionId: string): Promise<void>;
36
+ getSessionStats(sessionId: string): Promise<TaCommandResult<'get_session_stats'>>;
37
+ exportHtml(sessionId: string, outputPath?: string): Promise<TaCommandResult<'export_html'>>;
38
+ switchSession(sessionId: string, sessionPath: string): Promise<TaCommandResult<'switch_session'>>;
39
+ fork(sessionId: string, entryId: string): Promise<TaCommandResult<'fork'>>;
40
+ clone(sessionId: string): Promise<TaCommandResult<'clone'>>;
41
+ getForkMessages(sessionId: string): Promise<TaCommandResult<'get_fork_messages'>>;
42
+ getEntries(sessionId: string, since?: string): Promise<TaCommandResult<'get_entries'>>;
43
+ getTree(sessionId: string): Promise<TaCommandResult<'get_tree'>>;
44
+ getLastAssistantText(sessionId: string): Promise<TaCommandResult<'get_last_assistant_text'>>;
45
+ setSessionName(sessionId: string, name: string): Promise<void>;
46
+ getMessages(sessionId: string): Promise<TaCommandResult<'get_messages'>>;
47
+ getCommands(sessionId: string): Promise<TaCommandResult<'get_commands'>>;
48
+ protected on<T>(sessionId: string, command: (session: TaSession) => T | Promise<T>): Promise<T>;
49
+ protected guard<T>(command: () => Promise<T>): Promise<T>;
50
+ }