@alexsun-top/opencode-chat-room 0.7.8 → 0.8.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 (67) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/LICENSE +21 -0
  3. package/README.md +45 -36
  4. package/README.zh-CN.md +50 -41
  5. package/dist/cli.js +2629 -350
  6. package/package.json +13 -3
  7. package/packages/chat-room-channeld/README.md +242 -0
  8. package/packages/chat-room-channeld/dist/feishu-adapter.js +499 -0
  9. package/packages/chat-room-channeld/dist/mock-adapter.js +295 -0
  10. package/packages/chat-room-channeld/src/adapter.ts +475 -0
  11. package/packages/chat-room-channeld/src/cli.ts +124 -0
  12. package/packages/chat-room-channeld/src/config.ts +207 -0
  13. package/packages/chat-room-channeld/src/delivery.ts +158 -0
  14. package/packages/chat-room-channeld/src/feishu-adapter.ts +473 -0
  15. package/packages/chat-room-channeld/src/framing.ts +128 -0
  16. package/packages/chat-room-channeld/src/host-helpers.ts +41 -0
  17. package/packages/chat-room-channeld/src/host.ts +509 -0
  18. package/packages/chat-room-channeld/src/ingest-gate.ts +108 -0
  19. package/packages/chat-room-channeld/src/log.ts +26 -0
  20. package/packages/chat-room-channeld/src/mock-adapter.ts +206 -0
  21. package/packages/chat-room-channeld/src/protocol.ts +75 -0
  22. package/packages/chat-room-channeld/src/published.ts +37 -0
  23. package/packages/chat-room-channeld/src/rules.ts +127 -0
  24. package/packages/chat-room-channeld/src/state.ts +485 -0
  25. package/packages/chat-room-channeld/src/stats.ts +167 -0
  26. package/plugin.ts +22 -5
  27. package/server/addressing-routes.ts +130 -0
  28. package/server/assets/NOTICE.txt +2 -2
  29. package/server/assets/SHIKI-LICENSE +22 -0
  30. package/server/assets/base.css +9 -1
  31. package/server/assets/chat/doc-preview.js +3 -4
  32. package/server/assets/chat/forward.js +69 -0
  33. package/server/assets/chat/index.js +2 -0
  34. package/server/assets/chat/locales/de.js +5 -0
  35. package/server/assets/chat/locales/es.js +5 -0
  36. package/server/assets/chat/locales/fr.js +5 -0
  37. package/server/assets/chat/locales/it.js +5 -0
  38. package/server/assets/chat/locales/ja.js +5 -0
  39. package/server/assets/chat/locales/ko.js +5 -0
  40. package/server/assets/chat/markdown-footnotes.js +115 -0
  41. package/server/assets/chat/markdown.js +24 -35
  42. package/server/assets/chat/palette.js +1 -1
  43. package/server/assets/chat/replies.js +7 -1
  44. package/server/assets/chat/shiki.js +87 -0
  45. package/server/assets/chat/side-pane.js +8 -1
  46. package/server/assets/chat/state.js +14 -1
  47. package/server/assets/chat/system-messages.js +2 -0
  48. package/server/assets/code-highlight.css +20 -31
  49. package/server/assets/conversation.css +3 -2
  50. package/server/assets/doc-preview.css +2 -1
  51. package/server/assets/responsive.css +5 -0
  52. package/server/assets/shiki.bundle.js +152 -0
  53. package/server/channels.ts +314 -0
  54. package/server/chat-server.ts +27 -4
  55. package/server/chat.html +10 -3
  56. package/server/sw.js +1 -2
  57. package/skills/chatroom/SKILL.md +1 -1
  58. package/src/center/center-client.ts +79 -1
  59. package/src/config/file-config.ts +34 -0
  60. package/src/runtime/v2-plugin.ts +8 -2
  61. package/src/runtime/watch.ts +3 -2
  62. package/src/state/sqlite-state.ts +42 -11
  63. package/src/state/store.ts +9 -3
  64. package/src/tui/http.mjs +16 -2
  65. package/src/tui/main.mjs +1 -1
  66. package/server/assets/HIGHLIGHT-LICENSE +0 -29
  67. package/server/assets/highlight-10.7.3.min.js +0 -1358
@@ -0,0 +1,295 @@
1
+ // packages/chat-room-channeld/src/mock-adapter.ts
2
+ import path from "node:path";
3
+ import { pathToFileURL } from "node:url";
4
+
5
+ // packages/chat-room-channeld/src/framing.ts
6
+ import { StringDecoder } from "node:string_decoder";
7
+
8
+ // packages/chat-room-channeld/src/protocol.ts
9
+ var PROTOCOL_VERSION = 1;
10
+ var EVENT_SCHEMA_VERSION = 1;
11
+ var MAX_FRAME_BYTES = 1048576;
12
+ var ADAPTER_ID_PREFIX = "adapter-";
13
+ var RPC_METHOD_NOT_FOUND = -32601;
14
+ function adapterRequestId(sequence) {
15
+ return `${ADAPTER_ID_PREFIX}${sequence}`;
16
+ }
17
+ function rpcRequest(id, method, params) {
18
+ return params === undefined ? { jsonrpc: "2.0", id, method } : { jsonrpc: "2.0", id, method, params };
19
+ }
20
+ function rpcResult(id, result) {
21
+ return { jsonrpc: "2.0", id, result };
22
+ }
23
+ function rpcError(id, code, message, data) {
24
+ const error = data === undefined ? { code, message } : { code, message, data };
25
+ return { jsonrpc: "2.0", id, error };
26
+ }
27
+
28
+ // packages/chat-room-channeld/src/framing.ts
29
+ function parseFrame(line) {
30
+ const text = line.trim();
31
+ if (!text)
32
+ throw new Error("empty frame");
33
+ let value;
34
+ try {
35
+ value = JSON.parse(text);
36
+ } catch (error) {
37
+ throw new Error(`invalid JSON frame: ${error instanceof Error ? error.message : String(error)}`);
38
+ }
39
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
40
+ throw new Error("frame must be a JSON object");
41
+ }
42
+ return value;
43
+ }
44
+ function createFrameReader(onMessage, onError) {
45
+ const decoder = new StringDecoder("utf8");
46
+ let parts = [];
47
+ let bytes = 0;
48
+ let discarding = false;
49
+ const dispatch = () => {
50
+ const text = discarding ? "" : parts.join("");
51
+ const wasDiscarding = discarding;
52
+ parts = [];
53
+ bytes = 0;
54
+ discarding = false;
55
+ if (wasDiscarding || !text.trim())
56
+ return;
57
+ try {
58
+ onMessage(parseFrame(text));
59
+ } catch (error) {
60
+ onError(error instanceof Error ? error : new Error(String(error)));
61
+ }
62
+ };
63
+ const consume = (segment) => {
64
+ const decoded = decoder.write(segment);
65
+ if (discarding)
66
+ return;
67
+ if (bytes + segment.length > MAX_FRAME_BYTES) {
68
+ parts = [];
69
+ bytes = 0;
70
+ discarding = true;
71
+ onError(new Error(`frame exceeds MAX_FRAME_BYTES (${MAX_FRAME_BYTES} bytes)`));
72
+ return;
73
+ }
74
+ bytes += segment.length;
75
+ parts.push(decoded);
76
+ };
77
+ return (chunk) => {
78
+ let start = 0;
79
+ while (start <= chunk.length) {
80
+ const newline = chunk.indexOf(10, start);
81
+ const end = newline === -1 ? chunk.length : newline;
82
+ consume(chunk.subarray(start, end));
83
+ if (newline === -1)
84
+ return;
85
+ dispatch();
86
+ start = newline + 1;
87
+ }
88
+ };
89
+ }
90
+
91
+ class AdapterGoneError extends Error {
92
+ code = "ADAPTER_GONE";
93
+ constructor(message) {
94
+ super(message);
95
+ this.name = "AdapterGoneError";
96
+ }
97
+ }
98
+ var GONE_CODES = new Set(["EPIPE", "ERR_STREAM_DESTROYED", "ERR_STREAM_WRITE_AFTER_END"]);
99
+ function writeError(error) {
100
+ const code = error?.code;
101
+ if (code && GONE_CODES.has(code))
102
+ return new AdapterGoneError(`adapter stream is gone (${code})`);
103
+ return error instanceof Error ? error : new Error(String(error));
104
+ }
105
+ function writeFrame(stream, value) {
106
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
107
+ throw new TypeError("frame must be a JSON object");
108
+ }
109
+ const writable = stream;
110
+ if (writable.destroyed || writable.writableEnded) {
111
+ throw new AdapterGoneError("adapter stream is closed");
112
+ }
113
+ const line = `${JSON.stringify(value)}
114
+ `;
115
+ if (Buffer.byteLength(line, "utf8") > MAX_FRAME_BYTES) {
116
+ throw new RangeError(`frame exceeds MAX_FRAME_BYTES (${MAX_FRAME_BYTES} bytes)`);
117
+ }
118
+ return new Promise((resolve, reject) => {
119
+ const fail = (error) => reject(writeError(error));
120
+ try {
121
+ stream.write(line, (error) => {
122
+ if (error)
123
+ fail(error);
124
+ else
125
+ resolve();
126
+ });
127
+ } catch (error) {
128
+ fail(error);
129
+ }
130
+ });
131
+ }
132
+
133
+ // packages/chat-room-channeld/src/mock-adapter.ts
134
+ async function handleRequest(message, options, state) {
135
+ const rawId = message.id;
136
+ if (rawId === undefined) {
137
+ const failure = message.error;
138
+ if (failure)
139
+ console.error(`mock adapter ${options.accountId}: request failed: ${failure.message ?? "error"}`);
140
+ return;
141
+ }
142
+ if (typeof rawId !== "string" && typeof rawId !== "number") {
143
+ console.error(`mock adapter ${options.accountId}: request id must be a string or number`);
144
+ return;
145
+ }
146
+ const id = rawId;
147
+ const method = message.method;
148
+ if (typeof method !== "string")
149
+ return;
150
+ if (method === "initialize") {
151
+ const accounts = {
152
+ [options.accountId]: {
153
+ ingress: "durable_outbox",
154
+ ack: "after_host_commit",
155
+ send: { text: true, reply: "none", idempotency: "persistent_delivery_key" }
156
+ }
157
+ };
158
+ await writeFrame(process.stdout, rpcResult(id, {
159
+ version: PROTOCOL_VERSION,
160
+ eventSchema: EVENT_SCHEMA_VERSION,
161
+ maxFrameBytes: MAX_FRAME_BYTES,
162
+ accounts
163
+ }));
164
+ return;
165
+ }
166
+ if (method === "start") {
167
+ state.started = true;
168
+ await writeFrame(process.stdout, rpcResult(id, { ready: true }));
169
+ if (process.env.CHANNELD_MOCK_EMIT === "1" && !state.emitted) {
170
+ state.emitted = true;
171
+ setTimeout(() => {
172
+ emitIngest(options, state);
173
+ }, 50);
174
+ }
175
+ return;
176
+ }
177
+ if (method === "send") {
178
+ const params = message.params ?? {};
179
+ const target = params.target;
180
+ const nativeMessageId = `mock-${params.attemptId}`;
181
+ console.error(`mock adapter ${options.accountId}: send attempt=${params.attemptId} target=${target?.platform}/${target?.roomId}`);
182
+ const result = rpcResult(id, {
183
+ state: "sent",
184
+ providerMessage: { endpoint: target, nativeMessageId }
185
+ });
186
+ if (process.env.CHANNELD_MOCK_ECHO !== "1" || !target) {
187
+ await writeFrame(process.stdout, result);
188
+ return;
189
+ }
190
+ const echo = rpcRequest(adapterRequestId(++state.sequence), "ingest", {
191
+ schemaVersion: EVENT_SCHEMA_VERSION,
192
+ eventId: `mock-echo-${nativeMessageId}`,
193
+ kind: "message.created",
194
+ source: {
195
+ platform: target.platform,
196
+ accountId: target.accountId,
197
+ roomId: target.roomId
198
+ },
199
+ actor: { platform: target.platform, identityNamespace: "mock", nativeUserId: "mock-user" },
200
+ providerEventId: nativeMessageId,
201
+ sourceDedupKey: `mock-echo:${target.platform}/${target.accountId}/${target.roomId}:${nativeMessageId}`,
202
+ message: { endpoint: target, nativeMessageId },
203
+ receivedAt: new Date().toISOString(),
204
+ content: { text: "mock echo" }
205
+ });
206
+ const delay = Number(process.env.CHANNELD_MOCK_ECHO_DELAY_MS ?? 0);
207
+ if (delay > 0) {
208
+ await writeFrame(process.stdout, echo);
209
+ await new Promise((resolve) => setTimeout(resolve, delay));
210
+ await writeFrame(process.stdout, result);
211
+ return;
212
+ }
213
+ await writeLines([result, echo]);
214
+ return;
215
+ }
216
+ if (method === "ping") {
217
+ await writeFrame(process.stdout, rpcResult(id, { pong: true }));
218
+ return;
219
+ }
220
+ if (method === "shutdown") {
221
+ await writeFrame(process.stdout, rpcResult(id, {}));
222
+ process.exit(0);
223
+ }
224
+ await writeFrame(process.stdout, rpcError(id, RPC_METHOD_NOT_FOUND, `method not found: ${method}`));
225
+ }
226
+ function writeLines(values) {
227
+ const text = `${values.map((value) => JSON.stringify(value)).join(`
228
+ `)}
229
+ `;
230
+ return new Promise((resolve, reject) => {
231
+ process.stdout.write(text, (error) => {
232
+ if (error)
233
+ reject(error);
234
+ else
235
+ resolve();
236
+ });
237
+ });
238
+ }
239
+ async function emitIngest(options, state) {
240
+ if (!state.started)
241
+ return;
242
+ const source = { platform: "mock", accountId: options.accountId, roomId: "in" };
243
+ const event = {
244
+ schemaVersion: EVENT_SCHEMA_VERSION,
245
+ eventId: `mock-event-${options.accountId}`,
246
+ kind: "message.created",
247
+ source,
248
+ actor: { platform: "mock", identityNamespace: "mock", nativeUserId: "mock-user" },
249
+ providerEventId: `mock-provider-${options.accountId}`,
250
+ sourceDedupKey: `mock:${options.accountId}:hello-from-mock`,
251
+ message: { endpoint: source, nativeMessageId: `mock-native-${options.accountId}` },
252
+ receivedAt: new Date().toISOString(),
253
+ content: { text: "hello from mock" }
254
+ };
255
+ try {
256
+ await writeFrame(process.stdout, rpcRequest(adapterRequestId(++state.sequence), "ingest", event));
257
+ console.error(`mock adapter ${options.accountId}: ingest emitted`);
258
+ } catch (error) {
259
+ console.error(`mock adapter ${options.accountId}: ingest failed: ${error instanceof Error ? error.message : String(error)}`);
260
+ }
261
+ }
262
+ function runMockAdapter() {
263
+ const options = { accountId: process.env.CHANNELD_ACCOUNT_ID ?? "mock" };
264
+ const state = { started: false, emitted: false, sequence: 0 };
265
+ const read = createFrameReader((message) => {
266
+ handleRequest(message, options, state).catch((error) => {
267
+ console.error(`mock adapter ${options.accountId}: handler failed: ${error instanceof Error ? error.message : String(error)}`);
268
+ });
269
+ }, (error) => console.error(`mock adapter ${options.accountId}: frame error: ${error.message}`));
270
+ process.stdin.on("data", (chunk) => read(chunk));
271
+ console.error(`mock adapter ${options.accountId}: ready on stdio`);
272
+ return new Promise((resolve) => {
273
+ process.stdin.once("end", resolve);
274
+ process.stdin.once("close", resolve);
275
+ });
276
+ }
277
+ function isDirectRun() {
278
+ const entry = process.argv[1];
279
+ if (!entry)
280
+ return false;
281
+ try {
282
+ return pathToFileURL(path.resolve(entry)).href === import.meta.url;
283
+ } catch {
284
+ return false;
285
+ }
286
+ }
287
+ if (isDirectRun()) {
288
+ runMockAdapter().catch((error) => {
289
+ console.error(`mock adapter failed: ${error instanceof Error ? error.message : String(error)}`);
290
+ process.exit(1);
291
+ });
292
+ }
293
+ export {
294
+ runMockAdapter
295
+ };