@alexsun-top/opencode-chat-room 0.8.0 → 0.8.2

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 (176) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +2 -2
  3. package/README.md +40 -8
  4. package/README.zh-CN.md +40 -8
  5. package/cli.ts +27 -1
  6. package/dist/cli.js +5406 -3088
  7. package/package.json +25 -5
  8. package/packages/chat-room-channeld/README.md +111 -33
  9. package/packages/chat-room-channeld/src/adapter.ts +179 -7
  10. package/packages/chat-room-channeld/src/config.ts +63 -1
  11. package/packages/chat-room-channeld/src/delivery.ts +6 -1
  12. package/packages/chat-room-channeld/src/host-helpers.ts +11 -0
  13. package/packages/chat-room-channeld/src/host.ts +142 -9
  14. package/packages/chat-room-channeld/src/inbound.ts +35 -0
  15. package/packages/chat-room-channeld/src/protocol.ts +52 -1
  16. package/packages/chat-room-channeld/src/rules.ts +7 -2
  17. package/packages/chat-room-channeld/src/state.ts +17 -0
  18. package/packages/chat-room-channeld/src/stats.ts +43 -1
  19. package/plugin.ts +1 -1
  20. package/server/admin-state.ts +13 -13
  21. package/server/app.ts +95 -0
  22. package/server/assets/NOTICE.txt +2 -1
  23. package/server/assets/base.css +21 -9
  24. package/server/assets/chat/admin.js +0 -2
  25. package/server/assets/chat/announcements.js +69 -8
  26. package/server/assets/chat/chat-background.js +177 -0
  27. package/server/assets/chat/confirm-dialog.js +0 -1
  28. package/server/assets/chat/create-room-dialog.js +0 -1
  29. package/server/assets/chat/diagram-viewer.js +7 -1
  30. package/server/assets/chat/doc-preview.js +251 -46
  31. package/server/assets/chat/events.js +16 -5
  32. package/server/assets/chat/files.js +53 -7
  33. package/server/assets/chat/image-zoom.js +8 -1
  34. package/server/assets/chat/index.js +6 -2
  35. package/server/assets/chat/integration-account-schema.js +99 -0
  36. package/server/assets/chat/integration-editor.js +163 -0
  37. package/server/assets/chat/integration-values.js +124 -0
  38. package/server/assets/chat/integrations.js +648 -0
  39. package/server/assets/chat/locales/de.js +40 -4
  40. package/server/assets/chat/locales/es.js +40 -4
  41. package/server/assets/chat/locales/fr.js +40 -4
  42. package/server/assets/chat/locales/it.js +40 -4
  43. package/server/assets/chat/locales/ja.js +40 -4
  44. package/server/assets/chat/locales/ko.js +40 -4
  45. package/server/assets/chat/markdown-footnotes.js +1 -1
  46. package/server/assets/chat/markdown.js +45 -8
  47. package/server/assets/chat/messages.js +6 -5
  48. package/server/assets/chat/office-preview.js +69 -0
  49. package/server/assets/chat/palette.js +6 -1
  50. package/server/assets/chat/panels.js +56 -0
  51. package/server/assets/chat/recall.js +58 -7
  52. package/server/assets/chat/receipts.js +17 -6
  53. package/server/assets/chat/reminders.js +68 -9
  54. package/server/assets/chat/replies.js +0 -6
  55. package/server/assets/chat/resource-format.js +11 -0
  56. package/server/assets/chat/rooms.js +1 -6
  57. package/server/assets/chat/settings.js +98 -4
  58. package/server/assets/chat/shortcuts.js +0 -3
  59. package/server/assets/chat/side-pane.js +34 -10
  60. package/server/assets/chat/state.js +125 -12
  61. package/server/assets/chat/system-events.js +58 -0
  62. package/server/assets/chat/system-messages.js +32 -14
  63. package/server/assets/chat/task-row.js +0 -6
  64. package/server/assets/chat/tasks.js +119 -37
  65. package/server/assets/chat/window-controls.js +37 -0
  66. package/server/assets/code-highlight.css +71 -32
  67. package/server/assets/conversation.css +174 -31
  68. package/server/assets/doc-preview.css +40 -3
  69. package/server/assets/files.css +8 -0
  70. package/server/assets/fluent-icons.svg +2 -0
  71. package/server/assets/monaco/LICENSE +21 -0
  72. package/server/assets/monaco/ThirdPartyNotices.txt +448 -0
  73. package/server/assets/monaco/codicon-jdswhhtn.ttf +0 -0
  74. package/server/assets/monaco/editor.worker.js +45 -0
  75. package/server/assets/monaco/integration-rules.css +1 -0
  76. package/server/assets/monaco/integration-rules.js +1605 -0
  77. package/server/assets/monaco/json.worker.js +77 -0
  78. package/server/assets/office/THIRD-PARTY-NOTICES.txt +11 -0
  79. package/server/assets/office/docx.bundle.js +33 -0
  80. package/server/assets/office/licenses/docx-preview-LICENSE.txt +201 -0
  81. package/server/assets/office/licenses/echarts-LICENSE.txt +222 -0
  82. package/server/assets/office/licenses/jszip-LICENSE.txt +651 -0
  83. package/server/assets/office/licenses/lodash-LICENSE.txt +47 -0
  84. package/server/assets/office/licenses/pptx-preview-LICENSE.txt +8 -0
  85. package/server/assets/office/licenses/tslib-LICENSE.txt +12 -0
  86. package/server/assets/office/licenses/uuid-LICENSE.txt +9 -0
  87. package/server/assets/office/licenses/xlsx-LICENSE.txt +201 -0
  88. package/server/assets/office/licenses/zrender-LICENSE.txt +29 -0
  89. package/server/assets/office/pptx.bundle.js +104 -0
  90. package/server/assets/office/xlsx.bundle.js +141 -0
  91. package/server/assets/recall.css +5 -4
  92. package/server/assets/responsive.css +80 -11
  93. package/server/assets/settings.css +185 -9
  94. package/server/assets/tasks.css +57 -7
  95. package/server/assets/window-controls.css +36 -0
  96. package/server/channel-attachments.ts +133 -0
  97. package/server/channel-integration.ts +572 -0
  98. package/server/channel-settings.ts +77 -0
  99. package/server/channels.ts +274 -21
  100. package/server/chat-server.ts +32 -580
  101. package/server/chat.html +234 -34
  102. package/server/http-server.ts +3 -182
  103. package/server/manifest.webmanifest +1 -0
  104. package/server/resource-routes.ts +16 -16
  105. package/server/routes/inbox.ts +100 -0
  106. package/server/routes/pages.ts +83 -0
  107. package/server/routes/questions.ts +39 -0
  108. package/server/routes/room-actions.ts +162 -0
  109. package/server/routes/rooms.ts +160 -0
  110. package/server/startup.ts +84 -0
  111. package/server/sw.js +5 -0
  112. package/server/task-routes.ts +1 -1
  113. package/src/center/center-client.ts +3 -11
  114. package/src/config/file-config.ts +7 -54
  115. package/src/platform/http-server.ts +182 -0
  116. package/src/protocol/addressing.ts +21 -0
  117. package/src/protocol/config.ts +58 -0
  118. package/src/protocol/room.ts +51 -0
  119. package/src/rooms/resources.ts +26 -16
  120. package/src/rooms/room-service.ts +2 -48
  121. package/src/rooms/storage.ts +329 -0
  122. package/src/rooms/tasks.ts +21 -5
  123. package/src/runtime/watch.ts +1 -1
  124. package/src/state/pg-database.ts +183 -0
  125. package/src/state/pg-migrate.ts +175 -0
  126. package/src/state/pg-sql.mjs +175 -0
  127. package/src/state/pg-worker.mjs +133 -0
  128. package/src/state/sqlite-announcements.ts +25 -0
  129. package/src/state/sqlite-database.ts +130 -0
  130. package/src/state/sqlite-legacy.ts +90 -0
  131. package/src/state/sqlite-members.ts +155 -0
  132. package/src/state/sqlite-messages.ts +288 -0
  133. package/src/state/sqlite-resources.ts +39 -0
  134. package/src/state/sqlite-rooms.ts +194 -0
  135. package/src/state/sqlite-schema.ts +80 -0
  136. package/src/state/sqlite-state-store.ts +34 -0
  137. package/src/state/sqlite-state.ts +15 -897
  138. package/src/state/sqlite-tasks.ts +65 -0
  139. package/src/state/sqlite-transaction.ts +13 -0
  140. package/src/state/sqlite.ts +21 -1
  141. package/src/state/store.ts +5 -1
  142. package/src/tui/app.mjs +32 -19
  143. package/src/tui/config.mjs +12 -4
  144. package/src/tui/i18n.mjs +38 -0
  145. package/src/tui/info-app.mjs +50 -51
  146. package/src/tui/info-content.mjs +79 -44
  147. package/src/tui/join-app.mjs +9 -9
  148. package/src/tui/locales/en.mjs +349 -0
  149. package/src/tui/locales/zh-CN.mjs +349 -0
  150. package/src/tui/main.mjs +85 -37
  151. package/src/tui/markdown.mjs +3 -2
  152. package/src/tui/mentions.mjs +3 -1
  153. package/src/tui/message-app.mjs +22 -21
  154. package/src/tui/message-view.mjs +2 -1
  155. package/src/tui/palette-app.mjs +9 -8
  156. package/src/tui/palette.mjs +30 -24
  157. package/src/tui/prefs.mjs +92 -0
  158. package/src/tui/reminder-app.mjs +18 -17
  159. package/src/tui/room-settings-app.mjs +30 -28
  160. package/src/tui/settings-app.mjs +153 -0
  161. package/src/tui/ui-content.mjs +65 -86
  162. package/src/tui/ui-help.mjs +73 -0
  163. package/src/tui/ui-overlay-view.mjs +23 -7
  164. package/src/tui/ui-suggest.mjs +6 -5
  165. package/src/tui/ui-theme.mjs +37 -33
  166. package/src/tui/ui-view.mjs +22 -7
  167. package/src/tui/ui.mjs +15 -3
  168. package/src/web/editor.worker.ts +1 -0
  169. package/src/web/integration-rules-editor.ts +573 -0
  170. package/src/web/integration-rules.ts +26 -0
  171. package/src/web/integration-schema.ts +229 -0
  172. package/src/web/json.worker.ts +1 -0
  173. package/src/web/office-viewer.ts +221 -0
  174. package/packages/chat-room-channeld/dist/feishu-adapter.js +0 -499
  175. package/packages/chat-room-channeld/src/feishu-adapter.ts +0 -473
  176. package/server/assets/chat/forward.js +0 -69
@@ -0,0 +1,133 @@
1
+ // Channel D attachments: chatroom resources become local files for the adapter,
2
+ // and adapter-downloaded files become chatroom resources for the room.
3
+ import { createHash } from "node:crypto";
4
+ import { createReadStream, createWriteStream } from "node:fs";
5
+ import { promises as fs } from "node:fs";
6
+ import os from "node:os";
7
+ import path from "node:path";
8
+ import { Readable } from "node:stream";
9
+ import { pipeline } from "node:stream/promises";
10
+ import type { AdapterCapabilities, AttachmentRef, IngestEvent } from "../packages/chat-room-channeld/src/protocol.ts";
11
+ import { platformLabel } from "../packages/chat-room-channeld/src/inbound.ts";
12
+ import { findResource, maxResourceBytes, openResource, uploadResource } from "../src/rooms/resources.ts";
13
+
14
+ /**
15
+ * Resources referenced by a room message marker. The message text stays the
16
+ * source of truth: an attachment that was uploaded but never mentioned is not
17
+ * forwarded.
18
+ */
19
+ export async function forwardAttachments(roomId: string, text: string): Promise<AttachmentRef[]> {
20
+ const ids = [...text.matchAll(/\[\[resource:([0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})\]\]/gi)]
21
+ .map((match) => match[1]!)
22
+ .filter((id, index, all) => all.indexOf(id) === index);
23
+ const attachments: AttachmentRef[] = [];
24
+ for (const id of ids) {
25
+ const resource = await findResource(roomId, id).catch(() => undefined);
26
+ if (!resource) {
27
+ attachments.push({ kind: "file", name: "attachment", error: "resource unavailable" });
28
+ continue;
29
+ }
30
+ attachments.push({
31
+ kind: resource.mimeType.startsWith("image/") ? "image" : "file",
32
+ name: resource.name,
33
+ mimeType: resource.mimeType,
34
+ size: resource.size,
35
+ resourceId: resource.id,
36
+ roomId,
37
+ });
38
+ }
39
+ return attachments;
40
+ }
41
+
42
+ /**
43
+ * Turn chatroom resource ids into local files for the adapter. Channel D runs
44
+ * adapters as child processes on the same host, so a path is cheaper and less
45
+ * fragile than moving megabytes through the JSON frame.
46
+ */
47
+ export async function prepareSendPayload(
48
+ payload: unknown,
49
+ capabilities?: AdapterCapabilities,
50
+ ): Promise<{ payload: unknown; cleanup?: () => Promise<void> }> {
51
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) return { payload };
52
+ const attachments = (payload as { attachments?: unknown }).attachments;
53
+ if (!Array.isArray(attachments) || attachments.length === 0) return { payload };
54
+
55
+ const directory = await fs.mkdtemp(path.join(os.tmpdir(), "chat-room-outbound-"));
56
+ const files: AttachmentRef[] = [];
57
+ for (const raw of attachments) {
58
+ if (!raw || typeof raw !== "object") continue;
59
+ const attachment = raw as AttachmentRef;
60
+ if (!attachment.resourceId || !attachment.roomId) {
61
+ files.push(attachment);
62
+ continue;
63
+ }
64
+ try {
65
+ const resource = await findResource(attachment.roomId, attachment.resourceId);
66
+ if (!resource) throw new Error("resource not found");
67
+ const limit = resource.mimeType.startsWith("image/")
68
+ ? capabilities?.attachments?.imageMaxBytes
69
+ : capabilities?.attachments?.fileMaxBytes;
70
+ if (limit !== undefined && resource.size > limit) throw new Error(`exceeds adapter attachment limit (${limit} bytes)`);
71
+ const opened = await openResource(resource);
72
+ if (!opened) throw new Error("resource content is missing");
73
+ const extension = path.extname(resource.name).toLowerCase();
74
+ const file = path.join(directory, `${resource.id}${/^\.[a-z0-9]{1,10}$/.test(extension) ? extension : ".bin"}`);
75
+ await pipeline(Readable.fromWeb(opened.stream as unknown as import("node:stream/web").ReadableStream), createWriteStream(file, { mode: 0o600 }));
76
+ files.push({ ...attachment, name: resource.name, mimeType: resource.mimeType, size: resource.size, path: file });
77
+ } catch (error) {
78
+ files.push({ ...attachment, error: error instanceof Error ? error.message : String(error) });
79
+ }
80
+ }
81
+ return {
82
+ payload: { ...(payload as Record<string, unknown>), attachments: files },
83
+ cleanup: () => fs.rm(directory, { recursive: true, force: true }),
84
+ };
85
+ }
86
+
87
+ /** Reuse one resource id per event, room, and attachment so a retry cannot duplicate it. */
88
+ function stableResourceID(eventId: string, roomId: string, index: number): string {
89
+ const hex = createHash("sha256").update(`${eventId}:${roomId}:${index}`).digest("hex");
90
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-5${hex.slice(13, 16)}-a${hex.slice(17, 20)}-${hex.slice(20, 32)}`;
91
+ }
92
+
93
+ /**
94
+ * Adapter downloads live in a private temp directory. Only accept that shape so
95
+ * an adapter cannot ask the server to import an arbitrary local file.
96
+ */
97
+ function readableAdapterFile(value: unknown): string | undefined {
98
+ if (typeof value !== "string" || !path.isAbsolute(value)) return undefined;
99
+ const prefix = path.join(os.tmpdir(), "chat-room-channeld-");
100
+ return path.resolve(value).startsWith(prefix) ? path.resolve(value) : undefined;
101
+ }
102
+
103
+ export async function inboundAttachments(event: IngestEvent, roomId: string): Promise<string[]> {
104
+ const attachments = event.content?.attachments ?? [];
105
+ const markers: string[] = [];
106
+ for (const [index, attachment] of attachments.entries()) {
107
+ if (attachment.error) {
108
+ markers.push(`[attachment unavailable: ${attachment.error}]`);
109
+ continue;
110
+ }
111
+ const file = readableAdapterFile(attachment.path);
112
+ if (!file) {
113
+ markers.push("[attachment unavailable: no local file]");
114
+ continue;
115
+ }
116
+ try {
117
+ const info = await fs.stat(file);
118
+ if (!info.isFile() || info.size > maxResourceBytes()) throw new Error("file is not a regular file within the upload limit");
119
+ const resource = await uploadResource(
120
+ roomId,
121
+ attachment.name || `${attachment.kind}-${index + 1}`,
122
+ Readable.toWeb(createReadStream(file)) as unknown as ReadableStream<Uint8Array>,
123
+ maxResourceBytes(),
124
+ { sessionID: `channel:${event.source.accountId}`, name: `${platformLabel(event.source.platform)} ${event.source.accountId}` },
125
+ stableResourceID(event.eventId, roomId, index),
126
+ );
127
+ markers.push(`[[resource:${resource.id}]]`);
128
+ } catch (error) {
129
+ markers.push(`[attachment upload failed: ${error instanceof Error ? error.message : String(error)}]`);
130
+ }
131
+ }
132
+ return markers;
133
+ }