@alexsun-top/opencode-chat-room 0.8.5 → 0.8.7

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 (126) hide show
  1. package/CHANGELOG.md +23 -2
  2. package/README.md +1 -1
  3. package/README.zh-CN.md +1 -1
  4. package/cli.ts +19 -0
  5. package/dist/cli.js +4058 -1531
  6. package/package.json +7 -1
  7. package/packages/chat-room-channeld/README.md +13 -8
  8. package/packages/chat-room-channeld/src/adapter.ts +52 -0
  9. package/packages/chat-room-channeld/src/config.ts +6 -1
  10. package/packages/chat-room-channeld/src/delivery.ts +0 -4
  11. package/packages/chat-room-channeld/src/host.ts +66 -44
  12. package/packages/chat-room-channeld/src/inbound.ts +14 -1
  13. package/packages/chat-room-channeld/src/pg/database.ts +141 -49
  14. package/packages/chat-room-channeld/src/pg/worker.mjs +46 -5
  15. package/packages/chat-room-channeld/src/pg/worker.mts +41 -6
  16. package/packages/chat-room-channeld/src/protocol.ts +38 -1
  17. package/packages/chat-room-channeld/src/state-rows.ts +132 -0
  18. package/packages/chat-room-channeld/src/state.ts +129 -157
  19. package/packages/chat-room-channeld/src/stats.ts +4 -0
  20. package/plugin.ts +26 -5
  21. package/server/app.ts +15 -0
  22. package/server/assets/base.css +22 -2
  23. package/server/assets/chat/admin.js +13 -0
  24. package/server/assets/chat/announcements.js +76 -34
  25. package/server/assets/chat/confirm-dialog.js +3 -0
  26. package/server/assets/chat/create-room-dialog.js +7 -0
  27. package/server/assets/chat/diagram-viewer.js +4 -8
  28. package/server/assets/chat/doc-preview.js +70 -30
  29. package/server/assets/chat/editor.js +38 -3
  30. package/server/assets/chat/events.js +11 -2
  31. package/server/assets/chat/files.js +28 -26
  32. package/server/assets/chat/history-loader.js +31 -15
  33. package/server/assets/chat/integration-editor.js +25 -1
  34. package/server/assets/chat/integrations.js +107 -15
  35. package/server/assets/chat/join-requests.js +39 -1
  36. package/server/assets/chat/locales/de.js +5 -3
  37. package/server/assets/chat/locales/es.js +5 -3
  38. package/server/assets/chat/locales/fr.js +5 -3
  39. package/server/assets/chat/locales/it.js +5 -3
  40. package/server/assets/chat/locales/ja.js +5 -3
  41. package/server/assets/chat/locales/ko.js +5 -3
  42. package/server/assets/chat/markdown-footnotes.js +19 -8
  43. package/server/assets/chat/markdown.js +0 -7
  44. package/server/assets/chat/members.js +31 -19
  45. package/server/assets/chat/message-cache.js +8 -2
  46. package/server/assets/chat/message-polling.js +8 -2
  47. package/server/assets/chat/message-window.js +57 -0
  48. package/server/assets/chat/messages.js +79 -40
  49. package/server/assets/chat/operator.js +624 -0
  50. package/server/assets/chat/palette.js +2 -7
  51. package/server/assets/chat/panels.js +31 -8
  52. package/server/assets/chat/questions.js +72 -56
  53. package/server/assets/chat/recall.js +14 -5
  54. package/server/assets/chat/receipts.js +63 -7
  55. package/server/assets/chat/refresh-timing.js +6 -0
  56. package/server/assets/chat/reminders.js +44 -13
  57. package/server/assets/chat/replies.js +33 -22
  58. package/server/assets/chat/resource-format.js +4 -1
  59. package/server/assets/chat/room-list.js +202 -3
  60. package/server/assets/chat/room-watcher.js +3 -1
  61. package/server/assets/chat/rooms.js +22 -3
  62. package/server/assets/chat/settings.js +33 -3
  63. package/server/assets/chat/state.js +21 -7
  64. package/server/assets/chat/tasks.js +33 -11
  65. package/server/assets/chat/window-controls.js +25 -16
  66. package/server/assets/code-highlight.css +0 -13
  67. package/server/assets/conversation.css +30 -2
  68. package/server/assets/doc-preview.css +3 -0
  69. package/server/assets/files.css +4 -0
  70. package/server/assets/monaco/integration-rules.js +1 -1
  71. package/server/assets/responsive.css +12 -2
  72. package/server/assets/settings.css +3 -1
  73. package/server/assets/tasks.css +6 -1
  74. package/server/assets/window-controls.css +3 -1
  75. package/server/channels.ts +24 -6
  76. package/server/chat-server.ts +4 -0
  77. package/server/chat.html +7 -2
  78. package/server/inbox.ts +3 -1
  79. package/server/join-requests.ts +8 -1
  80. package/server/mcp-http.ts +88 -0
  81. package/server/member-routes.ts +16 -2
  82. package/server/newrooms.ts +2 -13
  83. package/server/operator-auth.ts +21 -0
  84. package/server/operator-routes.ts +150 -0
  85. package/server/operator-state.ts +64 -0
  86. package/server/operator.css +139 -0
  87. package/server/room-owner.ts +17 -0
  88. package/server/routes/inbox.ts +15 -5
  89. package/server/routes/pages.ts +12 -2
  90. package/server/routes/room-actions.ts +4 -3
  91. package/server/system.html +109 -0
  92. package/server/wake.ts +1 -1
  93. package/skills/chatroom/SKILL.md +19 -3
  94. package/src/acp/client.ts +254 -0
  95. package/src/acp/protocol.ts +300 -0
  96. package/src/acp/watch.ts +498 -0
  97. package/src/agent/agent-receipts.ts +87 -5
  98. package/src/center/center-client.ts +18 -1
  99. package/src/center/center-inbox.ts +54 -7
  100. package/src/center/center-wake.ts +57 -3
  101. package/src/center/forward-client.ts +48 -0
  102. package/src/center/mentions.ts +16 -3
  103. package/src/center/notification-delay.ts +139 -0
  104. package/src/center/queue.ts +3 -0
  105. package/src/config/file-config.ts +25 -0
  106. package/src/mcp/server.ts +407 -0
  107. package/src/protocol/config.ts +7 -0
  108. package/src/runtime/plugin-notifications.ts +43 -9
  109. package/src/runtime/v2-plugin.ts +20 -4
  110. package/src/runtime/v2-receiver.ts +10 -2
  111. package/src/runtime/watch.ts +5 -1
  112. package/src/state/pg-database.ts +133 -49
  113. package/src/state/pg-worker.mjs +46 -5
  114. package/src/state/pg-worker.mts +41 -6
  115. package/src/state/receiver-state.ts +47 -0
  116. package/src/state/sqlite-database.ts +6 -2
  117. package/src/state/sqlite-members.ts +48 -4
  118. package/src/state/sqlite-messages.ts +25 -12
  119. package/src/state/sqlite-schema.ts +4 -1
  120. package/src/state/sqlite-state.ts +1 -1
  121. package/src/state/store.ts +10 -4
  122. package/src/tui/app.mjs +57 -10
  123. package/src/tui/app.mts +54 -11
  124. package/src/tui/room-settings-app.mjs +27 -9
  125. package/src/tui/room-settings-app.mts +26 -9
  126. package/src/web/integration-rules-editor.ts +25 -2
@@ -0,0 +1,300 @@
1
+ // SDK-free ACP v1 JSON-RPC over the agent's stdin/stdout. One instance owns
2
+ // one trusted local child and enforces frame, output and lifetime bounds.
3
+ import type { ChildProcess } from "node:child_process";
4
+ import { StringDecoder } from "node:string_decoder";
5
+
6
+ export const MAX_FRAME_CHARS = 8 * 1024 * 1024;
7
+ export const MAX_OUTPUT_CHARS = 2 * 1024 * 1024;
8
+ const EOF_GRACE_MS = 100;
9
+ const TERM_GRACE_MS = 1_500;
10
+ const KILL_GRACE_MS = 1_500;
11
+
12
+ export class AcpTimeoutError extends Error {}
13
+ export class AcpClosedError extends Error {}
14
+ export class AcpProtocolError extends Error {}
15
+
16
+ type Pending = { resolve: (value: unknown) => void; reject: (reason: Error) => void };
17
+ type RpcMessage = {
18
+ jsonrpc?: unknown;
19
+ id?: unknown;
20
+ result?: unknown;
21
+ error?: unknown;
22
+ method?: unknown;
23
+ params?: unknown;
24
+ };
25
+
26
+ function record(value: unknown): Record<string, unknown> | undefined {
27
+ return value && typeof value === "object" && !Array.isArray(value)
28
+ ? value as Record<string, unknown>
29
+ : undefined;
30
+ }
31
+
32
+ function waitClose(child: ChildProcess, ms: number): Promise<boolean> {
33
+ return new Promise((resolve) => {
34
+ if (child.exitCode !== null || child.signalCode !== null) {
35
+ resolve(true);
36
+ return;
37
+ }
38
+ const timer = setTimeout(() => {
39
+ child.off("close", onClose);
40
+ resolve(false);
41
+ }, ms);
42
+ const onClose = () => {
43
+ clearTimeout(timer);
44
+ resolve(true);
45
+ };
46
+ child.once("close", onClose);
47
+ });
48
+ }
49
+
50
+ export class AcpConnection {
51
+ private readonly child: ChildProcess;
52
+ private readonly decoder = new StringDecoder("utf8");
53
+ private readonly pending = new Map<number, Pending>();
54
+ private buffer = "";
55
+ private nextId = 1;
56
+ private closed = false;
57
+ private failing = false;
58
+ private sessionId = "";
59
+ private promptActive = false;
60
+ private text = "";
61
+ private timer: ReturnType<typeof setTimeout> | undefined;
62
+ private shutdownPromise: Promise<void> | undefined;
63
+
64
+ constructor(child: ChildProcess, timeoutMs?: number) {
65
+ this.child = child;
66
+ child.stdout?.on("data", (chunk: Buffer) => this.read(chunk));
67
+ child.stdin?.on("error", () => this.fail(new AcpClosedError("agent write failed")));
68
+ child.stdout?.on("error", () => this.fail(new AcpClosedError("agent stream failed")));
69
+ child.once("error", () => this.fail(new AcpClosedError("cannot start agent")));
70
+ child.once("close", () => this.fail(new AcpClosedError("agent exited")));
71
+ if (timeoutMs !== undefined) this.armTimeout(timeoutMs);
72
+ }
73
+
74
+ get output(): string {
75
+ return this.text;
76
+ }
77
+
78
+ get isClosed(): boolean {
79
+ return this.closed;
80
+ }
81
+
82
+ resetOutput(): void {
83
+ this.text = "";
84
+ }
85
+
86
+ armTimeout(timeoutMs: number): void {
87
+ if (this.closed) return;
88
+ this.disarmTimeout();
89
+ this.timer = setTimeout(() => this.fail(new AcpTimeoutError("timed out")), timeoutMs);
90
+ }
91
+
92
+ disarmTimeout(): void {
93
+ if (this.timer) clearTimeout(this.timer);
94
+ this.timer = undefined;
95
+ }
96
+
97
+ setSessionId(sessionId: string): void {
98
+ this.sessionId = sessionId;
99
+ }
100
+
101
+ markPromptActive(): void {
102
+ this.promptActive = true;
103
+ }
104
+
105
+ markPromptDone(): void {
106
+ this.promptActive = false;
107
+ }
108
+
109
+ request(method: string, params: unknown): Promise<unknown> {
110
+ if (this.closed) return Promise.reject(new AcpClosedError("connection closed"));
111
+ const id = this.nextId;
112
+ this.nextId += 1;
113
+ return new Promise((resolve, reject) => {
114
+ this.pending.set(id, { resolve, reject });
115
+ this.send({ jsonrpc: "2.0", id, method, params });
116
+ });
117
+ }
118
+
119
+ private read(chunk: Buffer): void {
120
+ if (this.closed) return;
121
+ this.buffer += this.decoder.write(chunk);
122
+ if (this.buffer.length > MAX_FRAME_CHARS) {
123
+ this.fail(new AcpProtocolError("agent frame exceeded limit"));
124
+ return;
125
+ }
126
+ let index = this.buffer.indexOf("\n");
127
+ while (index >= 0) {
128
+ const line = this.buffer.slice(0, index).trim();
129
+ this.buffer = this.buffer.slice(index + 1);
130
+ if (line) this.handleLine(line);
131
+ if (this.closed) return;
132
+ index = this.buffer.indexOf("\n");
133
+ }
134
+ }
135
+
136
+ private handleLine(line: string): void {
137
+ let parsed: unknown;
138
+ try {
139
+ parsed = JSON.parse(line);
140
+ } catch {
141
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
142
+ return;
143
+ }
144
+ const message = record(parsed);
145
+ if (!message) {
146
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
147
+ return;
148
+ }
149
+ const rpc = message as RpcMessage;
150
+ if (rpc.jsonrpc !== "2.0") {
151
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
152
+ return;
153
+ }
154
+ if (rpc.method !== undefined) {
155
+ if (typeof rpc.method !== "string") {
156
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
157
+ return;
158
+ }
159
+ if (rpc.id === undefined) {
160
+ this.handleNotification(rpc.method, rpc.params);
161
+ return;
162
+ }
163
+ if (typeof rpc.id !== "number" && typeof rpc.id !== "string") {
164
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
165
+ return;
166
+ }
167
+ this.handleRequest(rpc.id, rpc.method, rpc.params);
168
+ return;
169
+ }
170
+ if (typeof rpc.id !== "number" || !Number.isFinite(rpc.id)) {
171
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
172
+ return;
173
+ }
174
+ const hasResult = Object.hasOwn(rpc, "result");
175
+ const hasError = Object.hasOwn(rpc, "error");
176
+ if (hasResult === hasError) {
177
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
178
+ return;
179
+ }
180
+ const pending = this.pending.get(rpc.id);
181
+ if (!pending) {
182
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
183
+ return;
184
+ }
185
+ if (hasError) {
186
+ const error = record(rpc.error);
187
+ if (!error || typeof error.code !== "number") {
188
+ this.fail(new AcpProtocolError("agent sent invalid protocol data"));
189
+ return;
190
+ }
191
+ this.pending.delete(rpc.id);
192
+ pending.reject(new Error(`agent returned JSON-RPC error ${error.code}`));
193
+ return;
194
+ }
195
+ this.pending.delete(rpc.id);
196
+ pending.resolve(rpc.result);
197
+ }
198
+
199
+ private handleNotification(method: string, params: unknown): void {
200
+ if (method !== "session/update") return;
201
+ const input = record(params);
202
+ if (!input || input.sessionId !== this.sessionId) return;
203
+ const update = record(input.update);
204
+ if (!update) return;
205
+ if (update.sessionUpdate === "agent_message_chunk") {
206
+ const content = record(update.content);
207
+ if (content?.type !== "text" || typeof content.text !== "string") return;
208
+ const next = this.text + content.text;
209
+ if (next.length > MAX_OUTPUT_CHARS) {
210
+ this.fail(new AcpProtocolError("agent output exceeded limit"));
211
+ return;
212
+ }
213
+ this.text = next;
214
+ }
215
+ }
216
+
217
+ private handleRequest(id: number | string, method: string, params: unknown): void {
218
+ if (method === "session/request_permission") {
219
+ const options = record(params)?.options;
220
+ const option = Array.isArray(options) ? options.find((entry) => {
221
+ const kind = record(entry)?.kind;
222
+ return kind === "reject_once" || kind === "reject_always";
223
+ }) : undefined;
224
+ const optionId = record(option)?.optionId;
225
+ this.respond(id, typeof optionId === "string"
226
+ ? { outcome: { outcome: "selected", optionId } }
227
+ : { outcome: { outcome: "cancelled" } });
228
+ return;
229
+ }
230
+ this.respondError(id, -32601);
231
+ }
232
+
233
+ private respond(id: unknown, result: unknown): void {
234
+ this.send({ jsonrpc: "2.0", id, result });
235
+ }
236
+
237
+ private respondError(id: unknown, code: number): void {
238
+ this.send({ jsonrpc: "2.0", id, error: { code, message: "Method not found" } });
239
+ }
240
+
241
+ private send(message: unknown): void {
242
+ if (this.closed) return;
243
+ try {
244
+ this.child.stdin?.write(`${JSON.stringify(message)}\n`);
245
+ } catch {
246
+ this.fail(new AcpClosedError("agent write failed"));
247
+ }
248
+ }
249
+
250
+ private fail(reason: Error): void {
251
+ if (this.closed || this.failing) return;
252
+ this.failing = true;
253
+ if (this.promptActive && this.sessionId) {
254
+ this.send({ jsonrpc: "2.0", method: "session/cancel", params: { sessionId: this.sessionId } });
255
+ }
256
+ this.closed = true;
257
+ this.rejectAll(reason);
258
+ void this.finishChild();
259
+ }
260
+
261
+ private rejectAll(reason: Error): void {
262
+ for (const pending of this.pending.values()) pending.reject(reason);
263
+ this.pending.clear();
264
+ }
265
+
266
+ async shutdown(): Promise<void> {
267
+ return this.finishChild();
268
+ }
269
+
270
+ private finishChild(): Promise<void> {
271
+ if (this.shutdownPromise) return this.shutdownPromise;
272
+ this.shutdownPromise = this.terminate();
273
+ return this.shutdownPromise;
274
+ }
275
+
276
+ private async terminate(): Promise<void> {
277
+ if (this.timer) clearTimeout(this.timer);
278
+ this.timer = undefined;
279
+ if (!this.closed) {
280
+ if (this.promptActive && this.sessionId) {
281
+ this.send({ jsonrpc: "2.0", method: "session/cancel", params: { sessionId: this.sessionId } });
282
+ }
283
+ this.closed = true;
284
+ this.rejectAll(new AcpClosedError("connection closed"));
285
+ }
286
+ const stdin = this.child.stdin;
287
+ if (stdin && !stdin.destroyed) stdin.end();
288
+ if (!(await waitClose(this.child, EOF_GRACE_MS))) {
289
+ this.child.kill("SIGTERM");
290
+ if (!(await waitClose(this.child, TERM_GRACE_MS))) {
291
+ this.child.kill("SIGKILL");
292
+ await waitClose(this.child, KILL_GRACE_MS);
293
+ }
294
+ }
295
+ stdin?.destroy();
296
+ this.child.stdout?.destroy();
297
+ this.child.removeAllListeners();
298
+ }
299
+ }
300
+