@alexsun-top/opencode-chat-room 0.7.7 → 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.
- package/CHANGELOG.md +32 -0
- package/LICENSE +21 -0
- package/README.md +45 -36
- package/README.zh-CN.md +50 -41
- package/dist/cli.js +2696 -365
- package/package.json +14 -4
- package/packages/chat-room-channeld/README.md +242 -0
- package/packages/chat-room-channeld/dist/feishu-adapter.js +499 -0
- package/packages/chat-room-channeld/dist/mock-adapter.js +295 -0
- package/packages/chat-room-channeld/src/adapter.ts +475 -0
- package/packages/chat-room-channeld/src/cli.ts +124 -0
- package/packages/chat-room-channeld/src/config.ts +207 -0
- package/packages/chat-room-channeld/src/delivery.ts +158 -0
- package/packages/chat-room-channeld/src/feishu-adapter.ts +473 -0
- package/packages/chat-room-channeld/src/framing.ts +128 -0
- package/packages/chat-room-channeld/src/host-helpers.ts +41 -0
- package/packages/chat-room-channeld/src/host.ts +509 -0
- package/packages/chat-room-channeld/src/ingest-gate.ts +108 -0
- package/packages/chat-room-channeld/src/log.ts +26 -0
- package/packages/chat-room-channeld/src/mock-adapter.ts +206 -0
- package/packages/chat-room-channeld/src/protocol.ts +75 -0
- package/packages/chat-room-channeld/src/published.ts +37 -0
- package/packages/chat-room-channeld/src/rules.ts +127 -0
- package/packages/chat-room-channeld/src/state.ts +485 -0
- package/packages/chat-room-channeld/src/stats.ts +167 -0
- package/plugin.ts +22 -5
- package/server/addressing-routes.ts +130 -0
- package/server/assets/NOTICE.txt +2 -2
- package/server/assets/SHIKI-LICENSE +22 -0
- package/server/assets/base.css +9 -1
- package/server/assets/chat/announcements.js +5 -2
- package/server/assets/chat/clipboard.js +30 -0
- package/server/assets/chat/copy-action.js +48 -0
- package/server/assets/chat/diagram-viewer.js +253 -0
- package/server/assets/chat/doc-preview.js +149 -12
- package/server/assets/chat/events.js +25 -4
- package/server/assets/chat/files.js +26 -5
- package/server/assets/chat/forward.js +69 -0
- package/server/assets/chat/history-loader.js +3 -1
- package/server/assets/chat/index.js +11 -1
- package/server/assets/chat/join-requests.js +6 -3
- package/server/assets/chat/locales/de.js +7 -2
- package/server/assets/chat/locales/es.js +7 -2
- package/server/assets/chat/locales/fr.js +7 -2
- package/server/assets/chat/locales/it.js +7 -2
- package/server/assets/chat/locales/ja.js +7 -2
- package/server/assets/chat/locales/ko.js +7 -2
- package/server/assets/chat/markdown-footnotes.js +115 -0
- package/server/assets/chat/markdown.js +65 -113
- package/server/assets/chat/messages.js +6 -3
- package/server/assets/chat/palette.js +33 -16
- package/server/assets/chat/pwa.js +2 -0
- package/server/assets/chat/questions.js +5 -0
- package/server/assets/chat/replies.js +7 -1
- package/server/assets/chat/resource-format.js +74 -0
- package/server/assets/chat/rooms.js +3 -0
- package/server/assets/chat/shiki.js +87 -0
- package/server/assets/chat/side-pane.js +143 -0
- package/server/assets/chat/state.js +63 -9
- package/server/assets/chat/system-messages.js +2 -0
- package/server/assets/chat/task-row.js +2 -1
- package/server/assets/chat/tasks.js +29 -2
- package/server/assets/chat/viewport.js +5 -0
- package/server/assets/code-highlight.css +20 -31
- package/server/assets/conversation.css +27 -2
- package/server/assets/diagram-viewer.css +141 -0
- package/server/assets/doc-preview.css +26 -6
- package/server/assets/responsive.css +87 -13
- package/server/assets/shiki.bundle.js +152 -0
- package/server/assets/tasks.css +1 -4
- package/server/channels.ts +314 -0
- package/server/chat-server.ts +36 -8
- package/server/chat.html +33 -5
- package/server/resource-routes.ts +3 -1
- package/server/sw.js +236 -4
- package/skills/chatroom/SKILL.md +1 -1
- package/src/agent/chatroom-skill.ts +8 -2
- package/src/center/center-client.ts +79 -1
- package/src/config/file-config.ts +34 -0
- package/src/rooms/resources.ts +2 -2
- package/src/runtime/v2-plugin.ts +8 -2
- package/src/runtime/watch.ts +41 -7
- package/src/state/sqlite-state.ts +67 -18
- package/src/state/store.ts +11 -5
- package/src/tui/app.mjs +10 -2
- package/src/tui/http.mjs +16 -2
- package/src/tui/info-app.mjs +24 -15
- package/src/tui/join-app.mjs +3 -3
- package/src/tui/main.mjs +1 -1
- package/src/tui/markdown.mjs +5 -3
- package/src/tui/mentions.mjs +3 -3
- package/src/tui/message-app.mjs +14 -11
- package/src/tui/palette-app.mjs +10 -7
- package/src/tui/reminder-app.mjs +14 -10
- package/src/tui/ui-content.mjs +3 -1
- package/server/assets/HIGHLIGHT-LICENSE +0 -29
- package/server/assets/highlight-10.7.3.min.js +0 -1358
package/server/assets/tasks.css
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
#tasksDialog.is-pane {
|
|
25
25
|
position: relative;
|
|
26
26
|
inset: auto;
|
|
27
|
-
grid-column:
|
|
27
|
+
grid-column: 4;
|
|
28
28
|
grid-row: 1 / -1;
|
|
29
29
|
width: auto;
|
|
30
30
|
height: auto;
|
|
@@ -54,9 +54,6 @@
|
|
|
54
54
|
#tasksDialog.is-pane .task-edit-form { grid-template-columns: minmax(0, 1fr); }
|
|
55
55
|
#tasksDialog.is-pane .task-create-due, #tasksDialog.is-pane .task-create-assignee,
|
|
56
56
|
#tasksDialog.is-pane .task-edit-due-field, #tasksDialog.is-pane .task-edit-assignee-field { grid-column: 1; }
|
|
57
|
-
@media (min-width: 1100px) and (min-height: 560px) {
|
|
58
|
-
#chatView.has-tasks { grid-template-columns: 252px minmax(0, 1fr) clamp(380px, 30vw, 460px); }
|
|
59
|
-
}
|
|
60
57
|
|
|
61
58
|
.tasks-head {
|
|
62
59
|
display: flex;
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// Channel D integration: opt-in forwarding from a room message to an external
|
|
2
|
+
// IM endpoint through the channeld relay host. The host keeps its own SQLite
|
|
3
|
+
// file, so disabling the feature (or deleting channeld.sqlite) is a full rollback.
|
|
4
|
+
import { mkdirSync, readFileSync } from "node:fs";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { parse as parseYaml } from "yaml";
|
|
8
|
+
import type { ServerSettings } from "../src/config/file-config.ts";
|
|
9
|
+
import { databasePath, writeState } from "../src/state/store.ts";
|
|
10
|
+
import { openStateDatabase } from "../src/state/sqlite-state.ts";
|
|
11
|
+
import {
|
|
12
|
+
parseConfig,
|
|
13
|
+
type ChannelConfig,
|
|
14
|
+
type RuleTarget,
|
|
15
|
+
} from "../packages/chat-room-channeld/src/config.ts";
|
|
16
|
+
import { createHost, type ChannelHost, type SpawnAdapter } from "../packages/chat-room-channeld/src/host.ts";
|
|
17
|
+
import { EVENT_SCHEMA_VERSION, type Endpoint, type IngestEvent } from "../packages/chat-room-channeld/src/protocol.ts";
|
|
18
|
+
import { matchRules, targetKey } from "../packages/chat-room-channeld/src/rules.ts";
|
|
19
|
+
import {
|
|
20
|
+
isTerminalDeliveryState,
|
|
21
|
+
readChannelStats,
|
|
22
|
+
type DeliveryRow,
|
|
23
|
+
type ReceiptRow,
|
|
24
|
+
} from "../packages/chat-room-channeld/src/state.ts";
|
|
25
|
+
import { jsonObject } from "./json-body.ts";
|
|
26
|
+
|
|
27
|
+
export type ChannelsRuntime = {
|
|
28
|
+
host: ChannelHost;
|
|
29
|
+
config: ChannelConfig;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type ChannelDeps = {
|
|
33
|
+
spawnAdapter?: SpawnAdapter;
|
|
34
|
+
deliveryIntervalMs?: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// The center accepts an optional receipt because an account disabled mid-send
|
|
38
|
+
// records a terminal `unknown` without one; the message must not invent one.
|
|
39
|
+
export type DeliveryResultCallback = (
|
|
40
|
+
event: IngestEvent | undefined,
|
|
41
|
+
delivery: DeliveryRow,
|
|
42
|
+
receipt: ReceiptRow | undefined,
|
|
43
|
+
) => void;
|
|
44
|
+
|
|
45
|
+
const json = (body: unknown, status = 200) => Response.json(body, { status });
|
|
46
|
+
const IDENTIFIER = /^[A-Za-z0-9._-]{1,128}$/;
|
|
47
|
+
const MAX_SESSION = 128;
|
|
48
|
+
const MAX_REQUEST_ID = 200;
|
|
49
|
+
|
|
50
|
+
/** Load a channeld YAML config, with the center owning the state file path. */
|
|
51
|
+
function loadChannelConfig(file: string, stateFile: string): ChannelConfig {
|
|
52
|
+
let text: string;
|
|
53
|
+
try {
|
|
54
|
+
text = readFileSync(file, "utf8");
|
|
55
|
+
} catch {
|
|
56
|
+
throw new Error(`channeld config: cannot read ${file}`);
|
|
57
|
+
}
|
|
58
|
+
let raw: unknown;
|
|
59
|
+
try {
|
|
60
|
+
raw = parseYaml(text);
|
|
61
|
+
} catch {
|
|
62
|
+
throw new Error(`channeld config: ${file} is not valid YAML/JSON`);
|
|
63
|
+
}
|
|
64
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
65
|
+
throw new Error(`channeld config: ${file} must contain a mapping`);
|
|
66
|
+
}
|
|
67
|
+
// Override any YAML stateFile: a stale path must never point at the room database.
|
|
68
|
+
return parseConfig({ ...(raw as Record<string, unknown>), stateFile });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function resolveChannelStateFile(stateDir: string, configured: string | undefined): string {
|
|
72
|
+
const file = configured
|
|
73
|
+
? (path.isAbsolute(configured) ? configured : path.resolve(stateDir, configured))
|
|
74
|
+
: path.join(stateDir, "channeld.sqlite");
|
|
75
|
+
if (path.resolve(file) === path.join(stateDir, "chat-room.sqlite")) {
|
|
76
|
+
throw new Error("chatroom config: server.channeld.stateFile must not be the room database");
|
|
77
|
+
}
|
|
78
|
+
return file;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function deliverySystemMessage(delivery: DeliveryRow, receipt: ReceiptRow | undefined): string {
|
|
82
|
+
if (delivery.state === "sent") return `Forwarded to ${delivery.target}`;
|
|
83
|
+
const raw = receipt?.detail || delivery.lastError || receipt?.state || delivery.state;
|
|
84
|
+
// Keep the system row inside the 5000-char message limit even for a huge adapter detail.
|
|
85
|
+
const reason = String(raw).replace(/[\r\n;]+/g, " ").trim().slice(0, 500) || delivery.state;
|
|
86
|
+
return `Forward to ${delivery.target} failed: ${reason}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Build the terminal-delivery publisher. One system message per delivery, and
|
|
91
|
+
* only for a forward event that came from this center.
|
|
92
|
+
*/
|
|
93
|
+
export function createDeliveryResultHandler(): DeliveryResultCallback {
|
|
94
|
+
const notified = new Set<string>();
|
|
95
|
+
return (event, delivery, receipt) => {
|
|
96
|
+
if (!isTerminalDeliveryState(delivery.state)) return;
|
|
97
|
+
if (notified.has(delivery.deliveryId)) return;
|
|
98
|
+
const source = event?.source;
|
|
99
|
+
if (
|
|
100
|
+
event?.kind !== "message.forward_requested"
|
|
101
|
+
|| source?.platform !== "chatroom"
|
|
102
|
+
|| source.accountId !== "center"
|
|
103
|
+
) return;
|
|
104
|
+
notified.add(delivery.deliveryId);
|
|
105
|
+
const text = deliverySystemMessage(delivery, receipt);
|
|
106
|
+
void writeState((svc) => {
|
|
107
|
+
svc.addEvent(source.roomId, text);
|
|
108
|
+
}).catch((error: unknown) => {
|
|
109
|
+
console.warn(`[chat-room] could not write the channeld result message: ${error instanceof Error ? error.message : String(error)}`);
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Start the relay host when `server.channeld.enabled` is on. A missing or
|
|
116
|
+
* invalid config fails startup; an adapter start failure only disables channels.
|
|
117
|
+
*/
|
|
118
|
+
export async function startChannels(
|
|
119
|
+
settings: ServerSettings,
|
|
120
|
+
deps: ChannelDeps = {},
|
|
121
|
+
): Promise<ChannelsRuntime | undefined> {
|
|
122
|
+
if (!settings.channeld?.enabled) return undefined;
|
|
123
|
+
const configPath = settings.channeld.config;
|
|
124
|
+
if (!configPath) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
"chatroom config: server.channeld is enabled but no config file is set (server.channeld.config or CHAT_ROOM_CHANNELD_CONFIG)",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
const stateDir = path.dirname(databasePath());
|
|
130
|
+
const stateFile = resolveChannelStateFile(stateDir, settings.channeld.stateFile);
|
|
131
|
+
const config = loadChannelConfig(configPath, stateFile);
|
|
132
|
+
mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
133
|
+
const host = createHost(config, {
|
|
134
|
+
spawnAdapter: deps.spawnAdapter,
|
|
135
|
+
deliveryIntervalMs: deps.deliveryIntervalMs,
|
|
136
|
+
onDeliveryResult: createDeliveryResultHandler(),
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
await host.start();
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.warn(`[chat-room] channeld disabled: ${error instanceof Error ? error.message : String(error)}`);
|
|
142
|
+
// A partially started host may still own adapters or the state file.
|
|
143
|
+
await stopChannels({ host, config });
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
return { host, config };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function stopChannels(runtime: ChannelsRuntime | undefined): Promise<void> {
|
|
150
|
+
if (!runtime) return;
|
|
151
|
+
try {
|
|
152
|
+
await runtime.host.stop();
|
|
153
|
+
} catch (error) {
|
|
154
|
+
console.warn(`[chat-room] channeld stop failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function parseTarget(value: unknown): RuleTarget | undefined {
|
|
159
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
160
|
+
const raw = value as Record<string, unknown>;
|
|
161
|
+
if (
|
|
162
|
+
typeof raw.platform !== "string" || !IDENTIFIER.test(raw.platform)
|
|
163
|
+
|| typeof raw.account !== "string" || !IDENTIFIER.test(raw.account)
|
|
164
|
+
|| typeof raw.room !== "string" || !IDENTIFIER.test(raw.room)
|
|
165
|
+
) return undefined;
|
|
166
|
+
return { platform: raw.platform, account: raw.account, room: raw.room };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
type ForwardableMessage = { text: string; createdAt: number };
|
|
170
|
+
|
|
171
|
+
/** A malformed percent escape must not become a 500. */
|
|
172
|
+
function decodeSegment(segment: string): string | undefined {
|
|
173
|
+
try {
|
|
174
|
+
return decodeURIComponent(segment);
|
|
175
|
+
} catch {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Registry + participant join: a stale registry row after a kick is not a member. */
|
|
181
|
+
async function memberName(roomId: string, sessionID: string): Promise<string | undefined> {
|
|
182
|
+
const db = await openStateDatabase(databasePath());
|
|
183
|
+
const row = db.query(
|
|
184
|
+
`SELECT g.name FROM registry g
|
|
185
|
+
JOIN rooms r ON r.id = g.room_id
|
|
186
|
+
JOIN participants p ON p.room_id = g.room_id
|
|
187
|
+
AND p.id = CASE WHEN r.kind = 'direct' THEN g.session_id ELSE g.name END
|
|
188
|
+
WHERE g.room_id = ? AND g.session_id = ?`,
|
|
189
|
+
).get(roomId, sessionID) as { name: string } | null;
|
|
190
|
+
return row?.name;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Member + live message check; undefined covers missing, recalled, and non-member. */
|
|
194
|
+
async function readForwardableMessage(
|
|
195
|
+
roomId: string,
|
|
196
|
+
messageId: string,
|
|
197
|
+
sessionID: string,
|
|
198
|
+
): Promise<ForwardableMessage | undefined> {
|
|
199
|
+
if (!await memberName(roomId, sessionID)) return undefined;
|
|
200
|
+
const db = await openStateDatabase(databasePath());
|
|
201
|
+
const row = db.query("SELECT text, created_at, recalled_at FROM messages WHERE room_id = ? AND id = ?")
|
|
202
|
+
.get(roomId, messageId) as { text: string; created_at: number; recalled_at: number | null } | null;
|
|
203
|
+
if (!row || row.recalled_at !== null) return undefined;
|
|
204
|
+
return { text: row.text, createdAt: Number(row.created_at) };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function isRoomMember(roomId: string, sessionID: string): Promise<boolean> {
|
|
208
|
+
return Boolean(await memberName(roomId, sessionID));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Targets of rules whose source is this room's forward requests and whose account is ready. */
|
|
212
|
+
function forwardTargets(runtime: ChannelsRuntime, roomId: string): RuleTarget[] {
|
|
213
|
+
const probe: IngestEvent = {
|
|
214
|
+
schemaVersion: EVENT_SCHEMA_VERSION,
|
|
215
|
+
eventId: "forward-targets",
|
|
216
|
+
kind: "message.forward_requested",
|
|
217
|
+
source: { platform: "chatroom", accountId: "center", roomId },
|
|
218
|
+
actor: { platform: "chatroom", identityNamespace: "session", nativeUserId: "" },
|
|
219
|
+
sourceDedupKey: "forward-targets",
|
|
220
|
+
receivedAt: new Date(0).toISOString(),
|
|
221
|
+
};
|
|
222
|
+
const config = runtime.config;
|
|
223
|
+
const seen = new Set<string>();
|
|
224
|
+
const targets: RuleTarget[] = [];
|
|
225
|
+
for (const rule of matchRules(config, probe)) {
|
|
226
|
+
for (const target of rule.to) {
|
|
227
|
+
if (runtime.host.targetStatus(target.platform, target.account) !== "ready") continue;
|
|
228
|
+
const key = `${target.platform}/${target.account}/${target.room}`;
|
|
229
|
+
if (seen.has(key)) continue;
|
|
230
|
+
seen.add(key);
|
|
231
|
+
targets.push(target);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return targets;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* POST /rooms/:roomId/forward and GET /rooms/:roomId/forward-targets.
|
|
239
|
+
* No runtime means the feature is off: the route stays absent.
|
|
240
|
+
*/
|
|
241
|
+
export async function channelRoutes(
|
|
242
|
+
req: Request,
|
|
243
|
+
url: URL,
|
|
244
|
+
runtime: ChannelsRuntime | undefined,
|
|
245
|
+
): Promise<Response | undefined> {
|
|
246
|
+
if (!runtime) return undefined;
|
|
247
|
+
|
|
248
|
+
if (url.pathname === "/channeld/status") {
|
|
249
|
+
if (req.method !== "GET") return json({ error: "method not allowed" }, 405);
|
|
250
|
+
try {
|
|
251
|
+
return json(readChannelStats(runtime.config.stateFile));
|
|
252
|
+
} catch {
|
|
253
|
+
return json({ error: "channel state unavailable" }, 503);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const forward = url.pathname.match(/^\/rooms\/([^/]+)\/forward$/);
|
|
258
|
+
if (forward) {
|
|
259
|
+
if (req.method !== "POST") return json({ error: "method not allowed" }, 405);
|
|
260
|
+
const roomId = decodeSegment(forward[1]!);
|
|
261
|
+
if (!roomId) return json({ error: "message not found" }, 404);
|
|
262
|
+
const body = await jsonObject<Record<string, unknown>>(req);
|
|
263
|
+
if (!body) return json({ error: "JSON object body is required" }, 400);
|
|
264
|
+
const sessionID = typeof body.sessionID === "string" ? body.sessionID.trim() : "";
|
|
265
|
+
const messageId = typeof body.messageId === "string" ? body.messageId.trim() : "";
|
|
266
|
+
if (!sessionID || sessionID.length > MAX_SESSION || !messageId || messageId.length > MAX_SESSION) {
|
|
267
|
+
return json({ error: "sessionID and messageId are required" }, 400);
|
|
268
|
+
}
|
|
269
|
+
const target = parseTarget(body.target);
|
|
270
|
+
if (!target) return json({ error: "target must contain platform, account, and room identifiers" }, 400);
|
|
271
|
+
const requestId = body.requestId === undefined
|
|
272
|
+
? undefined
|
|
273
|
+
: typeof body.requestId === "string" && body.requestId.trim() && body.requestId.trim().length <= MAX_REQUEST_ID
|
|
274
|
+
? body.requestId.trim()
|
|
275
|
+
: null;
|
|
276
|
+
if (requestId === null) return json({ error: `requestId must be a non-empty string (max ${MAX_REQUEST_ID})` }, 400);
|
|
277
|
+
|
|
278
|
+
const message = await readForwardableMessage(roomId, messageId, sessionID);
|
|
279
|
+
if (!message) return json({ error: "message not found" }, 404);
|
|
280
|
+
if (runtime.host.targetStatus(target.platform, target.account) !== "ready") {
|
|
281
|
+
return json({ error: "forward target is unavailable" }, 409);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const source: Endpoint = { platform: "chatroom", accountId: "center", roomId };
|
|
285
|
+
const requestedTarget: Endpoint = { platform: target.platform, accountId: target.account, roomId: target.room };
|
|
286
|
+
const event: IngestEvent = {
|
|
287
|
+
schemaVersion: EVENT_SCHEMA_VERSION,
|
|
288
|
+
eventId: `fwd-${randomUUID()}`,
|
|
289
|
+
kind: "message.forward_requested",
|
|
290
|
+
source,
|
|
291
|
+
actor: { platform: "chatroom", identityNamespace: "session", nativeUserId: sessionID },
|
|
292
|
+
...(requestId ? { providerEventId: requestId } : {}),
|
|
293
|
+
// The same message and target always reuse this key, so a retry is one event.
|
|
294
|
+
sourceDedupKey: `forward:${messageId}:${targetKey(requestedTarget)}`,
|
|
295
|
+
selectedMessage: { endpoint: source, nativeMessageId: messageId, text: message.text },
|
|
296
|
+
requestedTarget,
|
|
297
|
+
occurredAt: new Date(message.createdAt).toISOString(),
|
|
298
|
+
receivedAt: new Date().toISOString(),
|
|
299
|
+
content: { text: message.text },
|
|
300
|
+
};
|
|
301
|
+
const result = await runtime.host.ingest(event);
|
|
302
|
+
return json({ eventId: result.eventId ?? event.eventId }, 202);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const targets = url.pathname.match(/^\/rooms\/([^/]+)\/forward-targets$/);
|
|
306
|
+
if (!targets) return undefined;
|
|
307
|
+
if (req.method !== "GET") return json({ error: "method not allowed" }, 405);
|
|
308
|
+
const roomId = decodeSegment(targets[1]!);
|
|
309
|
+
if (!roomId) return json({ error: "room not found" }, 404);
|
|
310
|
+
const sessionID = (url.searchParams.get("sessionID") ?? "").trim();
|
|
311
|
+
if (!sessionID || sessionID.length > MAX_SESSION) return json({ error: "sessionID is required" }, 400);
|
|
312
|
+
if (!await isRoomMember(roomId, sessionID)) return json({ error: "room not found" }, 404);
|
|
313
|
+
return json({ targets: forwardTargets(runtime, roomId) });
|
|
314
|
+
}
|
package/server/chat-server.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { readInbox } from "./inbox.ts";
|
|
|
20
20
|
import { isReceiverOwnerID } from "../src/state/receiver-owner.ts";
|
|
21
21
|
import { claimWake, touchActive, wakeMentionedSessions } from "./wake.ts";
|
|
22
22
|
import { historyRoute } from "./history-route.ts";
|
|
23
|
+
import { addressingRoutes } from "./addressing-routes.ts";
|
|
23
24
|
import { messageRecallRoutes } from "./message-recall-routes.ts";
|
|
24
25
|
import { roomPayload } from "./room-payload.ts";
|
|
25
26
|
import { isUser, settleAdmins, settleOwner } from "./room-owner.ts";
|
|
@@ -32,6 +33,7 @@ import { adminRoutes } from "./admin-routes.ts";
|
|
|
32
33
|
import { joinRequestRoutes } from "./join-requests.ts";
|
|
33
34
|
import { joinRoomMember } from "./room-join.ts";
|
|
34
35
|
import { roomReminderRoutes, startRoomReminderScheduler } from "./room-reminders.ts";
|
|
36
|
+
import { channelRoutes, startChannels, stopChannels, type ChannelsRuntime } from "./channels.ts";
|
|
35
37
|
import { loadRoomConfig, resolveServerSettings } from "../src/config/file-config.ts";
|
|
36
38
|
import { startServer } from "./http-server.ts";
|
|
37
39
|
|
|
@@ -60,6 +62,8 @@ const hostname = settings.host;
|
|
|
60
62
|
const displayHostname = hostname.includes(":") ? `[${hostname}]` : hostname;
|
|
61
63
|
const port = parsePort(settings.port);
|
|
62
64
|
const token = settings.token;
|
|
65
|
+
// Set before the request handler runs; the forward routes stay absent when off.
|
|
66
|
+
let channels: ChannelsRuntime | undefined;
|
|
63
67
|
|
|
64
68
|
// The state directory and upload limit are read lazily from the environment;
|
|
65
69
|
// publish the file values before the first database or upload call.
|
|
@@ -104,7 +108,7 @@ async function handle(req: Request): Promise<Response> {
|
|
|
104
108
|
return json({ error: "chat page not found" }, 404);
|
|
105
109
|
}
|
|
106
110
|
const version = await chatVersionTag();
|
|
107
|
-
return new Response(html.replaceAll("{{version}}", version), {
|
|
111
|
+
return new Response(html.replaceAll("{{version}}", version).replaceAll("{{channeld}}", channels ? "on" : "off"), {
|
|
108
112
|
headers: {
|
|
109
113
|
"Content-Type": "text/html; charset=utf-8",
|
|
110
114
|
"Cache-Control": "no-store",
|
|
@@ -163,8 +167,12 @@ async function handle(req: Request): Promise<Response> {
|
|
|
163
167
|
if (resource) return resource;
|
|
164
168
|
const history = await historyRoute(url);
|
|
165
169
|
if (history) return history;
|
|
170
|
+
const addressing = await addressingRoutes(req, url);
|
|
171
|
+
if (addressing) return addressing;
|
|
166
172
|
const recall = await messageRecallRoutes(req, url);
|
|
167
173
|
if (recall) return recall;
|
|
174
|
+
const channel = await channelRoutes(req, url, channels);
|
|
175
|
+
if (channel) return channel;
|
|
168
176
|
if (req.method === "GET" && p === "/rooms/summary") {
|
|
169
177
|
// ponytail: 轻量房间摘要(不含消息)——wakeSessions 每 10s 用它计算
|
|
170
178
|
// latest,避免下载全部房间历史(orc-1 #5)
|
|
@@ -443,22 +451,30 @@ async function handle(req: Request): Promise<Response> {
|
|
|
443
451
|
if (text.length > 5000) {
|
|
444
452
|
return json({ error: "text too long (max 5000)" }, 400);
|
|
445
453
|
}
|
|
446
|
-
|
|
447
|
-
|
|
454
|
+
// parentMessageId is the addressing alias of the web client's reply field.
|
|
455
|
+
const replyTo = (body as Record<string, unknown>).replyToMessageID ?? (body as Record<string, unknown>).parentMessageId;
|
|
456
|
+
if (replyTo !== undefined && (typeof replyTo !== "string" || !replyTo)) return json({ error: "parentMessageId must be a non-empty message ID" }, 400);
|
|
457
|
+
// Optional retry key: a lost response may be retried without creating a twin.
|
|
458
|
+
const clientMessageID = (body as Record<string, unknown>).clientMessageID;
|
|
459
|
+
if (clientMessageID !== undefined && (typeof clientMessageID !== "string" || !clientMessageID || clientMessageID.length > 100)) {
|
|
460
|
+
return json({ error: "clientMessageID must be a non-empty string (max 100)" }, 400);
|
|
461
|
+
}
|
|
448
462
|
if (replyTo !== undefined) {
|
|
449
|
-
const result = await sendReply(roomId, replyTo, sessionID, text);
|
|
463
|
+
const result = await sendReply(roomId, replyTo, sessionID, text, clientMessageID);
|
|
450
464
|
if (result.status !== "ok") {
|
|
451
465
|
if (result.status === "missing") return json({ error: "room not found" }, 404);
|
|
452
466
|
if (result.status === "target") return json({ error: "reply target not found in room" }, 404);
|
|
467
|
+
if (result.status === "recalled") return json({ error: "reply target was recalled" }, 409);
|
|
453
468
|
if (result.status === "question") return json({ error: "use the question answers endpoint to reply to a question" }, 409);
|
|
454
|
-
|
|
469
|
+
// Reply addressing is member-only and uniformly missing otherwise.
|
|
470
|
+
return json({ error: "not found" }, 404);
|
|
455
471
|
}
|
|
456
472
|
touchActive(sessionID);
|
|
457
473
|
const room = await read((svc) => svc.getRoom(roomId));
|
|
458
|
-
void wakeMentionedSessions(serveWakeUrl, roomId, room.name, text, sessionID, result.message);
|
|
474
|
+
if (!result.duplicate) void wakeMentionedSessions(serveWakeUrl, roomId, room.name, text, sessionID, result.message);
|
|
459
475
|
return json(await roomPayload(room), 201);
|
|
460
476
|
}
|
|
461
|
-
const result = await appendRoomMessage(roomId, sessionID, text, isQuestion === true);
|
|
477
|
+
const result = await appendRoomMessage(roomId, sessionID, text, isQuestion === true, clientMessageID);
|
|
462
478
|
if (result.status !== "ok") {
|
|
463
479
|
if (result.status === "missing") return json({ error: "room not found" }, 404);
|
|
464
480
|
return json({ error: "session is not a room member" }, 409);
|
|
@@ -466,7 +482,7 @@ async function handle(req: Request): Promise<Response> {
|
|
|
466
482
|
touchActive(sessionID); // 发送者活跃
|
|
467
483
|
const room = await read((svc) => svc.getRoom(roomId));
|
|
468
484
|
// ponytail: 即时唤起——仅 @ 点名成员时(核心需求)
|
|
469
|
-
void wakeMentionedSessions(serveWakeUrl, roomId, room.name, text, sessionID, result.message);
|
|
485
|
+
if (!result.duplicate) void wakeMentionedSessions(serveWakeUrl, roomId, room.name, text, sessionID, result.message);
|
|
470
486
|
return json(await roomPayload(room), 201);
|
|
471
487
|
}
|
|
472
488
|
}
|
|
@@ -553,6 +569,18 @@ const config = {
|
|
|
553
569
|
|
|
554
570
|
export default config;
|
|
555
571
|
|
|
572
|
+
// Channel D is opt-in. A missing config aborts startup; a failed adapter start
|
|
573
|
+
// only logs and leaves the forward routes absent.
|
|
574
|
+
channels = await startChannels(settings);
|
|
575
|
+
if (channels) {
|
|
576
|
+
let stopping: Promise<void> | undefined;
|
|
577
|
+
const stop = () => {
|
|
578
|
+
stopping ??= stopChannels(channels).finally(() => process.exit(0));
|
|
579
|
+
};
|
|
580
|
+
process.once("SIGINT", stop);
|
|
581
|
+
process.once("SIGTERM", stop);
|
|
582
|
+
}
|
|
583
|
+
|
|
556
584
|
// Bun auto-serves the default export when this file is the entrypoint. Node and
|
|
557
585
|
// Deno have no such convention, so they start the shared adapter explicitly.
|
|
558
586
|
if (import.meta.main && typeof Bun === "undefined") await startServer(config);
|
package/server/chat.html
CHANGED
|
@@ -33,8 +33,9 @@
|
|
|
33
33
|
<link rel="stylesheet" href="/assets/files.css?v={{version}}">
|
|
34
34
|
<link rel="stylesheet" href="/assets/tasks.css?v={{version}}">
|
|
35
35
|
<link rel="stylesheet" href="/assets/doc-preview.css?v={{version}}">
|
|
36
|
+
<link rel="stylesheet" href="/assets/diagram-viewer.css?v={{version}}">
|
|
36
37
|
</head>
|
|
37
|
-
<body>
|
|
38
|
+
<body data-channeld="{{channeld}}">
|
|
38
39
|
|
|
39
40
|
<main id="app">
|
|
40
41
|
<section id="restoreView" class="restore-view" role="status" aria-live="polite">
|
|
@@ -604,20 +605,23 @@
|
|
|
604
605
|
<p id="draftCount" class="draft-count">0 / 5000</p>
|
|
605
606
|
</div>
|
|
606
607
|
</footer>
|
|
608
|
+
<!-- Drag or arrow-key resizes the preview/task pane; one separator serves both. -->
|
|
609
|
+
<div id="sidePaneHandle" class="side-pane-handle" role="separator" aria-orientation="vertical" aria-label="Resize side pane" data-i18n-aria-label="sidePaneResize" aria-valuemin="360" aria-valuemax="960" aria-valuenow="380" tabindex="0"></div>
|
|
607
610
|
<dialog id="docPreview" class="doc-preview" aria-labelledby="docPreviewTitle">
|
|
608
611
|
<header class="doc-preview-head">
|
|
609
612
|
<div class="doc-preview-heading">
|
|
610
|
-
<p class="doc-preview-eyebrow"><svg class="fluent-icon doc-preview-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-document-24"></use></svg><span data-i18n="docPreviewEyebrow">Markdown preview</span></p>
|
|
613
|
+
<p class="doc-preview-eyebrow"><svg class="fluent-icon doc-preview-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-document-24"></use></svg><span id="docPreviewEyebrow" data-i18n="docPreviewEyebrow">Markdown preview</span></p>
|
|
611
614
|
<h2 id="docPreviewTitle" tabindex="-1"></h2>
|
|
612
|
-
<p class="doc-preview-meta"><span id="docPreviewSize"></span><span id="docPreviewStatus" role="status" aria-live="polite"></span></p>
|
|
615
|
+
<p class="doc-preview-meta"><span id="docPreviewSize"></span><span id="docPreviewLanguage" class="doc-preview-language" hidden></span><span id="docPreviewStatus" role="status" aria-live="polite"></span></p>
|
|
613
616
|
</div>
|
|
614
617
|
<div class="doc-preview-actions">
|
|
615
|
-
<button id="btnDocPreviewDownload" class="btn ghost small doc-preview-download" type="button" data-i18n="resourceDownload">Download</button>
|
|
618
|
+
<button id="btnDocPreviewDownload" class="btn ghost small doc-preview-download" type="button" data-i18n="resourceDownload"><svg class="fluent-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-arrow_download-24"></use></svg><span class="i18n-label">Download</span></button>
|
|
616
619
|
<button id="btnCloseDocPreview" class="reader-close" type="button" aria-label="Close" data-i18n-aria-label="close"><svg class="fluent-icon close-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-dismiss-24"></use></svg></button>
|
|
617
620
|
</div>
|
|
618
621
|
</header>
|
|
619
622
|
<div class="doc-preview-scroll">
|
|
620
623
|
<article id="docPreviewBody" class="doc-preview-body msg-text"></article>
|
|
624
|
+
<p id="docPreviewNotice" class="hint doc-preview-empty" role="status" hidden></p>
|
|
621
625
|
<p id="docPreviewEmpty" class="hint doc-preview-empty" data-i18n="docPreviewEmpty" hidden>This document is empty.</p>
|
|
622
626
|
</div>
|
|
623
627
|
</dialog>
|
|
@@ -635,6 +639,23 @@
|
|
|
635
639
|
<img id="imageDialogImage" alt="">
|
|
636
640
|
</dialog>
|
|
637
641
|
|
|
642
|
+
<!-- A rendered Mermaid diagram opens large enough to read, with zoom and pan. -->
|
|
643
|
+
<dialog id="diagramViewer" class="diagram-viewer" aria-labelledby="diagramViewerTitle">
|
|
644
|
+
<header class="diagram-viewer-toolbar">
|
|
645
|
+
<h2 id="diagramViewerTitle" data-i18n="diagramViewerTitle">Diagram</h2>
|
|
646
|
+
<div class="diagram-viewer-tools" role="group" aria-label="Diagram zoom" data-i18n-aria-label="diagramZoomControls">
|
|
647
|
+
<button id="btnDiagramZoomOut" class="diagram-viewer-tool" type="button" aria-label="Zoom out" data-i18n-aria-label="zoomOut"></button>
|
|
648
|
+
<span id="diagramZoomLevel" class="diagram-viewer-level" aria-live="polite">100%</span>
|
|
649
|
+
<button id="btnDiagramZoomIn" class="diagram-viewer-tool" type="button" aria-label="Zoom in" data-i18n-aria-label="zoomIn"></button>
|
|
650
|
+
<button id="btnDiagramFit" class="diagram-viewer-tool diagram-viewer-fit" type="button" data-i18n="diagramFit">Fit</button>
|
|
651
|
+
</div>
|
|
652
|
+
<button id="btnCloseDiagram" class="reader-close" type="button" aria-label="Close" data-i18n-aria-label="close"><svg class="fluent-icon close-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-dismiss-24"></use></svg></button>
|
|
653
|
+
</header>
|
|
654
|
+
<div id="diagramViewerBody" class="diagram-viewer-body">
|
|
655
|
+
<div id="diagramViewerStage" class="diagram-viewer-stage"></div>
|
|
656
|
+
</div>
|
|
657
|
+
</dialog>
|
|
658
|
+
|
|
638
659
|
<!-- Shown when someone who is not the room owner reaches for the settings. -->
|
|
639
660
|
<dialog id="ownerOnlyDialog" class="admin-dialog" aria-labelledby="ownerOnlyHeading">
|
|
640
661
|
<header class="admin-dialog-head"><h2 id="ownerOnlyHeading" data-i18n="roomOwnerOnlyTitle">Owner or administrator only</h2><div class="admin-dialog-actions"><button id="btnCloseOwnerOnly" class="reader-close" type="button" aria-label="Close" data-i18n-aria-label="close"><svg class="fluent-icon close-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-dismiss-24"></use></svg></button></div></header>
|
|
@@ -651,6 +672,14 @@
|
|
|
651
672
|
</div>
|
|
652
673
|
</dialog>
|
|
653
674
|
|
|
675
|
+
<dialog id="forwardDialog" class="admin-dialog" aria-labelledby="forwardHeading">
|
|
676
|
+
<header class="admin-dialog-head"><h2 id="forwardHeading" data-i18n="forwardTitle">Forward message</h2><div class="admin-dialog-actions"><button id="btnCloseForward" class="reader-close" type="button" aria-label="Close" data-i18n-aria-label="close"><svg class="fluent-icon close-icon" aria-hidden="true" viewBox="0 0 24 24"><use href="/assets/fluent-icons.svg#fluent-dismiss-24"></use></svg></button></div></header>
|
|
677
|
+
<p class="hint" data-i18n="forwardHint">Choose where to forward this message.</p>
|
|
678
|
+
<div id="forwardTargets" class="forward-targets"></div>
|
|
679
|
+
<p id="forwardStatus" class="hint" role="status" aria-live="polite"></p>
|
|
680
|
+
<div class="announcement-actions"><button id="btnCancelForward" class="btn ghost small" type="button" data-i18n="cancel">Cancel</button></div>
|
|
681
|
+
</dialog>
|
|
682
|
+
|
|
654
683
|
<!-- Alt+Space: message search and common commands in one keyboard-first surface. -->
|
|
655
684
|
<dialog id="paletteDialog" class="admin-dialog palette-dialog" aria-labelledby="paletteHeading" aria-describedby="paletteHint">
|
|
656
685
|
<header class="palette-head">
|
|
@@ -741,7 +770,6 @@
|
|
|
741
770
|
|
|
742
771
|
<script src="/assets/marked-18.0.4.js?v={{version}}"></script>
|
|
743
772
|
<script src="/assets/dompurify-3.4.7.js?v={{version}}"></script>
|
|
744
|
-
<script src="/assets/highlight-10.7.3.min.js?v={{version}}"></script>
|
|
745
773
|
<script src="/assets/mermaid-11.12.2.min.js?v={{version}}" defer></script>
|
|
746
774
|
<script type="module" src="/assets/chat/index.js"></script>
|
|
747
775
|
</body>
|
|
@@ -33,7 +33,9 @@ export async function resourceRoutes(req: Request, url: URL): Promise<Response |
|
|
|
33
33
|
const rawOffset = Number(url.searchParams.get("offset") ?? 0);
|
|
34
34
|
const limit = Number.isSafeInteger(rawLimit) ? Math.min(1000, Math.max(1, rawLimit)) : 500;
|
|
35
35
|
const offset = Number.isSafeInteger(rawOffset) ? Math.max(0, rawOffset) : 0;
|
|
36
|
-
|
|
36
|
+
// A cursor keeps paging stable when a new upload shifts the offset window.
|
|
37
|
+
const before = url.searchParams.get("before");
|
|
38
|
+
const page = await listRoomResources(roomID, limit, offset, before && uuid.test(before) ? before : undefined);
|
|
37
39
|
return json({ resources: page.items, total: page.total, limit, offset }, 200);
|
|
38
40
|
}
|
|
39
41
|
if (upload) {
|