@alexsun-top/opencode-chat-room 0.8.0 → 0.8.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.
- package/CHANGELOG.md +29 -0
- package/LICENSE +2 -2
- package/README.md +38 -6
- package/README.zh-CN.md +38 -6
- package/cli.ts +27 -1
- package/dist/cli.js +5074 -3212
- package/package.json +23 -3
- package/packages/chat-room-channeld/README.md +98 -25
- package/packages/chat-room-channeld/dist/feishu-adapter.js +912 -149
- package/packages/chat-room-channeld/src/adapter.ts +36 -2
- package/packages/chat-room-channeld/src/config.ts +63 -1
- package/packages/chat-room-channeld/src/delivery.ts +6 -1
- package/packages/chat-room-channeld/src/feishu-adapter.ts +122 -292
- package/packages/chat-room-channeld/src/feishu-card.ts +27 -0
- package/packages/chat-room-channeld/src/feishu-inbound.ts +158 -0
- package/packages/chat-room-channeld/src/feishu-live.ts +355 -0
- package/packages/chat-room-channeld/src/feishu-send.ts +342 -0
- package/packages/chat-room-channeld/src/feishu-ws-frames.ts +173 -0
- package/packages/chat-room-channeld/src/host-helpers.ts +11 -0
- package/packages/chat-room-channeld/src/host.ts +120 -2
- package/packages/chat-room-channeld/src/inbound.ts +26 -0
- package/packages/chat-room-channeld/src/state.ts +17 -0
- package/packages/chat-room-channeld/src/stats.ts +43 -1
- package/server/admin-state.ts +13 -13
- package/server/app.ts +95 -0
- package/server/assets/NOTICE.txt +2 -1
- package/server/assets/base.css +15 -9
- package/server/assets/chat/announcements.js +70 -7
- package/server/assets/chat/doc-preview.js +184 -35
- package/server/assets/chat/events.js +3 -0
- package/server/assets/chat/files.js +46 -4
- package/server/assets/chat/index.js +2 -0
- package/server/assets/chat/integration-editor.js +163 -0
- package/server/assets/chat/integration-values.js +124 -0
- package/server/assets/chat/integrations.js +498 -0
- package/server/assets/chat/locales/de.js +40 -4
- package/server/assets/chat/locales/es.js +40 -4
- package/server/assets/chat/locales/fr.js +40 -4
- package/server/assets/chat/locales/it.js +40 -4
- package/server/assets/chat/locales/ja.js +40 -4
- package/server/assets/chat/locales/ko.js +40 -4
- package/server/assets/chat/markdown.js +22 -8
- package/server/assets/chat/messages.js +5 -5
- package/server/assets/chat/office-preview.js +69 -0
- package/server/assets/chat/panels.js +36 -0
- package/server/assets/chat/recall.js +58 -7
- package/server/assets/chat/receipts.js +15 -3
- package/server/assets/chat/reminders.js +69 -8
- package/server/assets/chat/resource-format.js +11 -0
- package/server/assets/chat/settings.js +30 -2
- package/server/assets/chat/side-pane.js +1 -2
- package/server/assets/chat/state.js +93 -10
- package/server/assets/chat/system-events.js +32 -0
- package/server/assets/chat/system-messages.js +23 -14
- package/server/assets/chat/task-row.js +0 -6
- package/server/assets/chat/tasks.js +121 -33
- package/server/assets/code-highlight.css +55 -32
- package/server/assets/conversation.css +123 -23
- package/server/assets/doc-preview.css +35 -3
- package/server/assets/files.css +5 -0
- package/server/assets/fluent-icons.svg +2 -0
- package/server/assets/monaco/LICENSE +21 -0
- package/server/assets/monaco/ThirdPartyNotices.txt +448 -0
- package/server/assets/monaco/codicon-jdswhhtn.ttf +0 -0
- package/server/assets/monaco/editor.worker.js +45 -0
- package/server/assets/monaco/integration-rules.css +1 -0
- package/server/assets/monaco/integration-rules.js +1297 -0
- package/server/assets/monaco/json.worker.js +77 -0
- package/server/assets/office/THIRD-PARTY-NOTICES.txt +11 -0
- package/server/assets/office/docx.bundle.js +33 -0
- package/server/assets/office/licenses/docx-preview-LICENSE.txt +201 -0
- package/server/assets/office/licenses/echarts-LICENSE.txt +222 -0
- package/server/assets/office/licenses/jszip-LICENSE.txt +651 -0
- package/server/assets/office/licenses/lodash-LICENSE.txt +47 -0
- package/server/assets/office/licenses/pptx-preview-LICENSE.txt +8 -0
- package/server/assets/office/licenses/tslib-LICENSE.txt +12 -0
- package/server/assets/office/licenses/uuid-LICENSE.txt +9 -0
- package/server/assets/office/licenses/xlsx-LICENSE.txt +201 -0
- package/server/assets/office/licenses/zrender-LICENSE.txt +29 -0
- package/server/assets/office/pptx.bundle.js +104 -0
- package/server/assets/office/xlsx.bundle.js +141 -0
- package/server/assets/recall.css +5 -4
- package/server/assets/responsive.css +50 -11
- package/server/assets/settings.css +73 -1
- package/server/assets/tasks.css +57 -7
- package/server/channel-integration.ts +519 -0
- package/server/channel-settings.ts +77 -0
- package/server/channels.ts +261 -18
- package/server/chat-server.ts +32 -580
- package/server/chat.html +187 -21
- package/server/http-server.ts +3 -182
- package/server/resource-routes.ts +16 -16
- package/server/routes/inbox.ts +100 -0
- package/server/routes/pages.ts +83 -0
- package/server/routes/questions.ts +39 -0
- package/server/routes/room-actions.ts +162 -0
- package/server/routes/rooms.ts +160 -0
- package/server/startup.ts +84 -0
- package/server/sw.js +4 -0
- package/server/task-routes.ts +1 -1
- package/src/center/center-client.ts +2 -10
- package/src/config/file-config.ts +7 -54
- package/src/platform/http-server.ts +182 -0
- package/src/protocol/addressing.ts +21 -0
- package/src/protocol/config.ts +58 -0
- package/src/protocol/room.ts +51 -0
- package/src/rooms/resources.ts +26 -16
- package/src/rooms/room-service.ts +2 -48
- package/src/rooms/storage.ts +329 -0
- package/src/rooms/tasks.ts +21 -5
- package/src/runtime/watch.ts +1 -1
- package/src/state/pg-database.ts +183 -0
- package/src/state/pg-migrate.ts +175 -0
- package/src/state/pg-sql.mjs +175 -0
- package/src/state/pg-worker.mjs +148 -0
- package/src/state/sqlite-announcements.ts +25 -0
- package/src/state/sqlite-database.ts +121 -0
- package/src/state/sqlite-legacy.ts +90 -0
- package/src/state/sqlite-members.ts +155 -0
- package/src/state/sqlite-messages.ts +288 -0
- package/src/state/sqlite-resources.ts +39 -0
- package/src/state/sqlite-rooms.ts +87 -0
- package/src/state/sqlite-schema.ts +80 -0
- package/src/state/sqlite-state-store.ts +33 -0
- package/src/state/sqlite-state.ts +15 -897
- package/src/state/sqlite-tasks.ts +65 -0
- package/src/state/sqlite-transaction.ts +13 -0
- package/src/state/sqlite.ts +21 -1
- package/src/state/store.ts +5 -1
- package/src/tui/app.mjs +32 -19
- package/src/tui/config.mjs +12 -4
- package/src/tui/i18n.mjs +38 -0
- package/src/tui/info-app.mjs +50 -51
- package/src/tui/info-content.mjs +79 -44
- package/src/tui/join-app.mjs +9 -9
- package/src/tui/locales/en.mjs +349 -0
- package/src/tui/locales/zh-CN.mjs +349 -0
- package/src/tui/main.mjs +85 -37
- package/src/tui/markdown.mjs +3 -2
- package/src/tui/mentions.mjs +3 -1
- package/src/tui/message-app.mjs +22 -21
- package/src/tui/message-view.mjs +2 -1
- package/src/tui/palette-app.mjs +9 -8
- package/src/tui/palette.mjs +30 -24
- package/src/tui/prefs.mjs +92 -0
- package/src/tui/reminder-app.mjs +18 -17
- package/src/tui/room-settings-app.mjs +30 -28
- package/src/tui/settings-app.mjs +153 -0
- package/src/tui/ui-content.mjs +65 -86
- package/src/tui/ui-help.mjs +73 -0
- package/src/tui/ui-overlay-view.mjs +23 -7
- package/src/tui/ui-suggest.mjs +6 -5
- package/src/tui/ui-theme.mjs +37 -33
- package/src/tui/ui-view.mjs +22 -7
- package/src/tui/ui.mjs +15 -3
- package/src/web/editor.worker.ts +1 -0
- package/src/web/integration-rules-editor.ts +439 -0
- package/src/web/integration-rules.ts +22 -0
- package/src/web/integration-schema.ts +228 -0
- package/src/web/json.worker.ts +1 -0
- package/src/web/office-viewer.ts +221 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type StateDatabase } from "./sqlite.ts";
|
|
2
|
+
|
|
3
|
+
// --- Room tasks (group rooms only; HTTP permissions live in rooms/tasks.ts) ---
|
|
4
|
+
export const ROOM_TASK_STATUSES = ["todo", "doing", "done"] as const;
|
|
5
|
+
export type RoomTaskStatus = (typeof ROOM_TASK_STATUSES)[number];
|
|
6
|
+
export type RoomTaskStep = { text: string; done: boolean };
|
|
7
|
+
export type RoomTask = {
|
|
8
|
+
id: string; roomId: string; title: string; description: string; assignee: string;
|
|
9
|
+
status: RoomTaskStatus; creatorName: string; creatorSessionID: string;
|
|
10
|
+
createdAt: number; updatedAt: number; dueDate: string; participants: string[];
|
|
11
|
+
conclusion: string; steps: RoomTaskStep[];
|
|
12
|
+
};
|
|
13
|
+
type RoomTaskRow = Omit<RoomTask, "status" | "participants" | "steps"> & { status: string; participants: string; steps: string };
|
|
14
|
+
const ROOM_TASK_COLUMNS = "id,room_id AS roomId,title,description,assignee,status,creator_name AS creatorName,creator_session_id AS creatorSessionID,created_at AS createdAt,updated_at AS updatedAt,due_date AS dueDate,participants,conclusion,steps";
|
|
15
|
+
const jsonArray = <T>(value: string): T[] => {
|
|
16
|
+
try { const parsed = JSON.parse(value) as unknown; return Array.isArray(parsed) ? parsed as T[] : []; }
|
|
17
|
+
catch { return []; }
|
|
18
|
+
};
|
|
19
|
+
const roomTask = (row: RoomTaskRow): RoomTask => ({
|
|
20
|
+
...row, status: row.status as RoomTaskStatus,
|
|
21
|
+
participants: jsonArray<string>(row.participants), steps: jsonArray<RoomTaskStep>(row.steps),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export function listRoomTasks(db: StateDatabase, roomID: string): RoomTask[] {
|
|
25
|
+
return (db.query(`SELECT ${ROOM_TASK_COLUMNS} FROM room_tasks WHERE room_id=? ORDER BY created_at,id`).all(roomID) as RoomTaskRow[]).map(roomTask);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function countRoomTasks(db: StateDatabase, roomID: string): number {
|
|
29
|
+
return Number((db.query("SELECT COUNT(*) AS count FROM room_tasks WHERE room_id=?").get(roomID) as { count: number } | null)?.count ?? 0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function readRoomTask(db: StateDatabase, roomID: string, id: string): RoomTask | undefined {
|
|
33
|
+
const row = db.query(`SELECT ${ROOM_TASK_COLUMNS} FROM room_tasks WHERE room_id=? AND id=?`).get(roomID, id) as RoomTaskRow | null;
|
|
34
|
+
return row ? roomTask(row) : undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function insertRoomTask(db: StateDatabase, task: RoomTask): void {
|
|
38
|
+
db.query("INSERT INTO room_tasks(id,room_id,title,description,assignee,status,creator_name,creator_session_id,created_at,updated_at,due_date,participants,conclusion,steps) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
|
|
39
|
+
.run(task.id, task.roomId, task.title, task.description, task.assignee, task.status, task.creatorName, task.creatorSessionID,
|
|
40
|
+
task.createdAt, task.updatedAt, task.dueDate, JSON.stringify(task.participants), task.conclusion, JSON.stringify(task.steps));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type RoomTaskPatch = Partial<Pick<RoomTask, "title" | "description" | "assignee" | "status" | "dueDate" | "participants" | "conclusion" | "steps">>;
|
|
44
|
+
|
|
45
|
+
export function patchRoomTask(db: StateDatabase, roomID: string, id: string, patch: RoomTaskPatch, updatedAt: number): RoomTask | undefined {
|
|
46
|
+
const sets: string[] = [], values: string[] = [];
|
|
47
|
+
for (const [column, value] of [["title", patch.title], ["description", patch.description], ["assignee", patch.assignee], ["status", patch.status], ["due_date", patch.dueDate], ["conclusion", patch.conclusion]] as const) {
|
|
48
|
+
if (value !== undefined) { sets.push(`${column}=?`); values.push(value); }
|
|
49
|
+
}
|
|
50
|
+
if (patch.participants !== undefined) { sets.push("participants=?"); values.push(JSON.stringify(patch.participants)); }
|
|
51
|
+
if (patch.steps !== undefined) { sets.push("steps=?"); values.push(JSON.stringify(patch.steps)); }
|
|
52
|
+
db.query(`UPDATE room_tasks SET ${sets.map((set) => `${set},`).join("")}updated_at=? WHERE room_id=? AND id=?`)
|
|
53
|
+
.run(...values, updatedAt, roomID, id);
|
|
54
|
+
return readRoomTask(db, roomID, id);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function deleteRoomTask(db: StateDatabase, roomID: string, id: string): boolean {
|
|
58
|
+
return db.query("DELETE FROM room_tasks WHERE room_id=? AND id=?").run(roomID, id).changes > 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function deleteCompletedRoomTasks(db: StateDatabase, roomID: string, creatorSessionID?: string): number {
|
|
62
|
+
return creatorSessionID === undefined
|
|
63
|
+
? db.query("DELETE FROM room_tasks WHERE room_id=? AND status='done'").run(roomID).changes
|
|
64
|
+
: db.query("DELETE FROM room_tasks WHERE room_id=? AND status='done' AND creator_session_id=?").run(roomID, creatorSessionID).changes;
|
|
65
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isBusyError, type StateDatabase } from "./sqlite.ts";
|
|
2
|
+
|
|
3
|
+
export const pause = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
4
|
+
|
|
5
|
+
export async function immediate<T>(db: StateDatabase, fn: () => T): Promise<T> {
|
|
6
|
+
for (let attempt = 0; ; attempt++) {
|
|
7
|
+
try { return db.transaction(fn).immediate(); }
|
|
8
|
+
catch (error) {
|
|
9
|
+
if (!isBusyError(error) || attempt >= 7) throw error;
|
|
10
|
+
await pause(Math.min(20 * 2 ** attempt, 250));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/state/sqlite.ts
CHANGED
|
@@ -37,7 +37,23 @@ export class Statement {
|
|
|
37
37
|
|
|
38
38
|
export type Transaction<T> = (() => T) & { deferred(): T; immediate(): T };
|
|
39
39
|
|
|
40
|
-
export
|
|
40
|
+
export type QueryStatement = {
|
|
41
|
+
all(...params: SQLQueryBindings[]): unknown[];
|
|
42
|
+
get(...params: SQLQueryBindings[]): unknown;
|
|
43
|
+
run(...params: SQLQueryBindings[]): RunResult;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// The synchronous surface used by the state layer. Implemented by the
|
|
47
|
+
// node:sqlite adapter and by PgDatabase's worker-thread bridge.
|
|
48
|
+
export interface StateDatabase {
|
|
49
|
+
query(sql: string): QueryStatement;
|
|
50
|
+
prepare(sql: string): QueryStatement;
|
|
51
|
+
exec(sql: string): void;
|
|
52
|
+
transaction<T>(fn: () => T): Transaction<T>;
|
|
53
|
+
close(): void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class Database implements StateDatabase {
|
|
41
57
|
readonly #db: DatabaseSync;
|
|
42
58
|
#savepoints = 0;
|
|
43
59
|
|
|
@@ -47,6 +63,7 @@ export class Database {
|
|
|
47
63
|
}
|
|
48
64
|
|
|
49
65
|
query(sql: string): Statement { return new Statement(this.#db.prepare(sql)); }
|
|
66
|
+
prepare(sql: string): Statement { return this.query(sql); }
|
|
50
67
|
exec(sql: string): void { this.#db.exec(sql); }
|
|
51
68
|
|
|
52
69
|
transaction<T>(fn: () => T): Transaction<T> {
|
|
@@ -75,6 +92,9 @@ export class Database {
|
|
|
75
92
|
export function isBusyError(error: unknown): boolean {
|
|
76
93
|
const err = error as { code?: string; errcode?: number; message?: string } | null;
|
|
77
94
|
if (!err) return false;
|
|
95
|
+
// PostgreSQL serialization failure and deadlock detected. The existing
|
|
96
|
+
// immediate() retry loop treats these like SQLITE_BUSY/SQLITE_LOCKED.
|
|
97
|
+
if (err.code === "40001" || err.code === "40P01") return true;
|
|
78
98
|
if (typeof err.errcode === "number" && ((err.errcode & 0xff) === 5 || (err.errcode & 0xff) === 6)) return true;
|
|
79
99
|
return /SQLITE_(BUSY|LOCKED)/.test(err.code ?? "") || /database (?:is|table is) locked/i.test(err.message ?? "");
|
|
80
100
|
}
|
package/src/state/store.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Buffer } from "node:buffer";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import os from "node:os";
|
|
7
7
|
import RoomService, { type Message, type Room } from "../rooms/room-service.ts";
|
|
8
|
-
import { acknowledgeQueued as sqliteAcknowledgeQueued, acknowledgeRead as sqliteAcknowledgeRead, answerQuestion as sqliteAnswerQuestion, appendRoomMessage as sqliteAppendRoomMessage, sendReply as sqliteSendReply, readHistoryPage, readMessage as sqliteReadMessage, readOpenQuestionRefs as sqliteReadOpenQuestionRefs, MAX_OPEN_QUESTION_REFS, readInboxPage as sqliteInboxPage, readMessageReceipts, readQuestionExcerpt as sqliteQuestionExcerpt, readQuestions as sqliteReadQuestions, countOpenQuestions as sqliteCountOpenQuestions, readSqliteState, readUnreadPage, recallMessage as sqliteRecallMessage, revealMessageContent as sqliteRevealMessageContent, isMessageRecalled, recalledMessageIDs as sqliteRecalledMessageIDs, writeMessageReceipts, writePresenceState, writeSqliteState, type HistoryMessage, type JoinRequest, listJoinRequests as sqliteListJoinRequests, readJoinRequest as sqliteReadJoinRequest, type RecallMessageResult, type RevealMessageResult } from "./sqlite-state.ts";
|
|
8
|
+
import { acknowledgeQueued as sqliteAcknowledgeQueued, acknowledgeRead as sqliteAcknowledgeRead, answerQuestion as sqliteAnswerQuestion, appendRoomMessage as sqliteAppendRoomMessage, sendReply as sqliteSendReply, readHistoryPage, readMessage as sqliteReadMessage, readOpenQuestionRefs as sqliteReadOpenQuestionRefs, MAX_OPEN_QUESTION_REFS, readInboxPage as sqliteInboxPage, readMessageReceipts, readQuestionExcerpt as sqliteQuestionExcerpt, readQuestions as sqliteReadQuestions, countOpenQuestions as sqliteCountOpenQuestions, readSqliteRoomSummaries, readSqliteState, readUnreadPage, recallMessage as sqliteRecallMessage, revealMessageContent as sqliteRevealMessageContent, isMessageRecalled, recalledMessageIDs as sqliteRecalledMessageIDs, writeMessageReceipts, writePresenceState, writeSqliteState, type HistoryMessage, type JoinRequest, listJoinRequests as sqliteListJoinRequests, readJoinRequest as sqliteReadJoinRequest, type RecallMessageResult, type RevealMessageResult, type RoomSummary } from "./sqlite-state.ts";
|
|
9
9
|
export type { JoinRequest, RecallMessageResult, RevealMessageResult } from "./sqlite-state.ts";
|
|
10
10
|
|
|
11
11
|
export function statePath(): string {
|
|
@@ -73,6 +73,10 @@ export async function loadRooms(): Promise<Room[]> {
|
|
|
73
73
|
return (await readSqliteState(databasePath())).rooms;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
export function readRoomSummaries(): Promise<RoomSummary[]> {
|
|
77
|
+
return readSqliteRoomSummaries(databasePath());
|
|
78
|
+
}
|
|
79
|
+
|
|
76
80
|
export async function readReceipts(roomID: string, messageIDs?: string[]): Promise<MessageReceipt[] | undefined> {
|
|
77
81
|
return readMessageReceipts(databasePath(), roomID, messageIDs);
|
|
78
82
|
}
|
package/src/tui/app.mjs
CHANGED
|
@@ -3,12 +3,15 @@
|
|
|
3
3
|
// from palette-app.mjs.
|
|
4
4
|
import { attachPalette } from "./palette-app.mjs";
|
|
5
5
|
import { attachInfo } from "./info-app.mjs";
|
|
6
|
+
import { attachSettings } from "./settings-app.mjs";
|
|
6
7
|
import { attachReminders } from "./reminder-app.mjs";
|
|
7
8
|
import { attachRoomSettings } from "./room-settings-app.mjs";
|
|
8
9
|
import { attachMessageActions } from "./message-app.mjs";
|
|
9
10
|
import { attachJoinRequests } from "./join-app.mjs";
|
|
10
11
|
import { canonicalizeMentions } from "./mentions.mjs";
|
|
11
12
|
import { mergeMessages, messageStamp, oldestSeq } from "./messages.mjs";
|
|
13
|
+
import { normalizeLanguage, setLanguage, t } from "./i18n.mjs";
|
|
14
|
+
import { normalizeTheme } from "./prefs.mjs";
|
|
12
15
|
|
|
13
16
|
const BACKOFF_MS = [1000, 2000, 4000, 8000, 15000];
|
|
14
17
|
|
|
@@ -22,15 +25,23 @@ const normalizeRoom = (room) => ({
|
|
|
22
25
|
});
|
|
23
26
|
|
|
24
27
|
export class ChatApp {
|
|
25
|
-
constructor({ api, sessionID, participant, host = "", autoPoll = true, settings = {}, downloadDir = process.cwd(), schedule = (fn, ms) => setTimeout(fn, ms) }) {
|
|
28
|
+
constructor({ api, sessionID, participant, host = "", autoPoll = true, settings = {}, savePrefs = null, downloadDir = process.cwd(), schedule = (fn, ms) => setTimeout(fn, ms) }) {
|
|
26
29
|
this.api = api;
|
|
27
30
|
this.sessionID = sessionID;
|
|
28
31
|
this.participant = participant;
|
|
29
32
|
this.autoPoll = autoPoll;
|
|
30
33
|
this.schedule = schedule;
|
|
34
|
+
this.savePrefs = typeof savePrefs === "function" ? savePrefs : null;
|
|
31
35
|
this.settings = { ...settings };
|
|
32
36
|
this.token = this.settings.token ?? "";
|
|
33
37
|
delete this.settings.token;
|
|
38
|
+
this.prefs = {
|
|
39
|
+
language: normalizeLanguage(this.settings.language),
|
|
40
|
+
theme: normalizeTheme(this.settings.theme),
|
|
41
|
+
participant: this.settings.participant ?? participant,
|
|
42
|
+
url: this.settings.server ?? "",
|
|
43
|
+
};
|
|
44
|
+
setLanguage(this.prefs.language);
|
|
34
45
|
this.downloadDir = downloadDir;
|
|
35
46
|
this.closed = false;
|
|
36
47
|
this.pollToken = 0;
|
|
@@ -45,7 +56,8 @@ export class ChatApp {
|
|
|
45
56
|
this.state = {
|
|
46
57
|
host,
|
|
47
58
|
participant,
|
|
48
|
-
theme:
|
|
59
|
+
theme: this.prefs.theme,
|
|
60
|
+
language: this.prefs.language,
|
|
49
61
|
connection: "connecting",
|
|
50
62
|
rooms: [],
|
|
51
63
|
selected: 0,
|
|
@@ -105,14 +117,14 @@ export class ChatApp {
|
|
|
105
117
|
this.state.selected = found >= 0 ? found : Math.min(this.state.selected, Math.max(0, rooms.length - 1));
|
|
106
118
|
this.state.connection = "online";
|
|
107
119
|
if (!quiet) {
|
|
108
|
-
this.state.status = "
|
|
120
|
+
this.state.status = t("status.refreshed");
|
|
109
121
|
this.state.statusKind = "info";
|
|
110
122
|
}
|
|
111
123
|
this.touch();
|
|
112
124
|
return true;
|
|
113
125
|
} catch (error) {
|
|
114
126
|
this.state.connection = "offline";
|
|
115
|
-
this.state.status =
|
|
127
|
+
this.state.status = t("status.refreshFailed", { error: errorText(error) });
|
|
116
128
|
this.state.statusKind = "error";
|
|
117
129
|
this.touch();
|
|
118
130
|
return false;
|
|
@@ -138,7 +150,7 @@ export class ChatApp {
|
|
|
138
150
|
this.state.messages = [];
|
|
139
151
|
this.state.pending = null;
|
|
140
152
|
this.state.highlight = null;
|
|
141
|
-
this.state.status =
|
|
153
|
+
this.state.status = t("status.opening", { name: summary.name });
|
|
142
154
|
this.state.statusKind = "info";
|
|
143
155
|
this.touch();
|
|
144
156
|
if (summary.joined) await this.loadRoom(summary.id);
|
|
@@ -150,7 +162,7 @@ export class ChatApp {
|
|
|
150
162
|
const result = await this.api.join(roomID, this.sessionID, this.participant);
|
|
151
163
|
if (!this.isCurrent(roomID)) return;
|
|
152
164
|
if (result?.pending) {
|
|
153
|
-
const message = String(result.message ?? "
|
|
165
|
+
const message = String(result.message ?? t("status.joinPending"));
|
|
154
166
|
this.state.pending = { roomId: roomID, message };
|
|
155
167
|
this.state.joined = false;
|
|
156
168
|
this.state.status = message;
|
|
@@ -161,7 +173,7 @@ export class ChatApp {
|
|
|
161
173
|
}
|
|
162
174
|
this.enterRoom(result?.room ?? result, roomID, true);
|
|
163
175
|
} catch (error) {
|
|
164
|
-
if (this.isCurrent(roomID)) this.setStatus(
|
|
176
|
+
if (this.isCurrent(roomID)) this.setStatus(t("status.joinFailed", { error: errorText(error) }), "error");
|
|
165
177
|
}
|
|
166
178
|
}
|
|
167
179
|
|
|
@@ -173,7 +185,7 @@ export class ChatApp {
|
|
|
173
185
|
const status = typeof result === "string" ? result : result?.status;
|
|
174
186
|
if (status === "joined") {
|
|
175
187
|
this.state.pending = null;
|
|
176
|
-
this.setStatus("
|
|
188
|
+
this.setStatus(t("status.joinApproved"), "success");
|
|
177
189
|
await this.loadRoom(roomID);
|
|
178
190
|
return;
|
|
179
191
|
}
|
|
@@ -182,7 +194,7 @@ export class ChatApp {
|
|
|
182
194
|
return;
|
|
183
195
|
}
|
|
184
196
|
this.state.pending = null;
|
|
185
|
-
this.setStatus("
|
|
197
|
+
this.setStatus(t("status.joinStale"), "error");
|
|
186
198
|
} catch {
|
|
187
199
|
this.later(() => this.checkJoin(roomID), 1500);
|
|
188
200
|
}
|
|
@@ -193,18 +205,18 @@ export class ChatApp {
|
|
|
193
205
|
const room = await this.api.getRoom(roomID);
|
|
194
206
|
if (!this.isCurrent(roomID) || !room) return;
|
|
195
207
|
if (room.joined === false) {
|
|
196
|
-
this.notJoined(roomID, "
|
|
208
|
+
this.notJoined(roomID, t("status.notMember"));
|
|
197
209
|
return;
|
|
198
210
|
}
|
|
199
211
|
this.enterRoom(room, roomID, false);
|
|
200
212
|
} catch (error) {
|
|
201
|
-
if (this.isCurrent(roomID)) this.setStatus(
|
|
213
|
+
if (this.isCurrent(roomID)) this.setStatus(t("status.openFailed", { error: errorText(error) }), "error");
|
|
202
214
|
}
|
|
203
215
|
}
|
|
204
216
|
|
|
205
217
|
enterRoom(room, roomID, joinedNow) {
|
|
206
218
|
if (!room || typeof room !== "object") {
|
|
207
|
-
this.setStatus("
|
|
219
|
+
this.setStatus(t("status.roomMissing"), "error");
|
|
208
220
|
return;
|
|
209
221
|
}
|
|
210
222
|
const summary = this.state.rooms.find((item) => item.id === roomID);
|
|
@@ -224,7 +236,7 @@ export class ChatApp {
|
|
|
224
236
|
this.state.pending = null;
|
|
225
237
|
this.state.messages = cache;
|
|
226
238
|
this.state.connection = "online";
|
|
227
|
-
this.state.status = joinedNow ?
|
|
239
|
+
this.state.status = joinedNow ? t("status.joined", { name: this.state.current.name }) : "";
|
|
228
240
|
this.state.statusKind = "success";
|
|
229
241
|
if (typeof room.hasMore === "boolean") this.historyMore.set(roomID, room.hasMore);
|
|
230
242
|
const oldest = oldestSeq(cache);
|
|
@@ -266,14 +278,14 @@ export class ChatApp {
|
|
|
266
278
|
);
|
|
267
279
|
if (!this.isCurrent(roomID)) return false;
|
|
268
280
|
if (!result || result.member === false) {
|
|
269
|
-
this.notJoined(roomID, "
|
|
281
|
+
this.notJoined(roomID, t("status.memberLost"));
|
|
270
282
|
return false;
|
|
271
283
|
}
|
|
272
284
|
const wasOffline = this.state.connection !== "online";
|
|
273
285
|
this.merge(result.items);
|
|
274
286
|
this.state.connection = "online";
|
|
275
287
|
if (wasOffline) {
|
|
276
|
-
this.state.status = "reconnected";
|
|
288
|
+
this.state.status = t("status.reconnected");
|
|
277
289
|
this.state.statusKind = "success";
|
|
278
290
|
}
|
|
279
291
|
this.touch();
|
|
@@ -282,7 +294,7 @@ export class ChatApp {
|
|
|
282
294
|
} catch (error) {
|
|
283
295
|
if (controller?.signal.aborted) return false;
|
|
284
296
|
this.state.connection = "reconnecting";
|
|
285
|
-
this.state.status =
|
|
297
|
+
this.state.status = t("status.connectionLost", { error: errorText(error) });
|
|
286
298
|
this.state.statusKind = "error";
|
|
287
299
|
this.touch();
|
|
288
300
|
return false;
|
|
@@ -334,7 +346,7 @@ export class ChatApp {
|
|
|
334
346
|
if (!raw) return;
|
|
335
347
|
const room = this.state.current;
|
|
336
348
|
if (!room || !this.state.joined) {
|
|
337
|
-
this.setStatus("
|
|
349
|
+
this.setStatus(t("status.joinBeforeSend"), "error");
|
|
338
350
|
return;
|
|
339
351
|
}
|
|
340
352
|
const text = canonicalizeMentions(raw, room.participants);
|
|
@@ -354,12 +366,12 @@ export class ChatApp {
|
|
|
354
366
|
else if (result?.id) this.merge([result]);
|
|
355
367
|
this.state.highlight = null;
|
|
356
368
|
this.state.connection = "online";
|
|
357
|
-
this.setStatus("sent", "success");
|
|
369
|
+
this.setStatus(t("status.sent"), "success");
|
|
358
370
|
this.markRead();
|
|
359
371
|
} catch (error) {
|
|
360
372
|
// Keep text typed while the request was in flight; dropping it loses work.
|
|
361
373
|
this.state.draft = this.state.draft ? `${raw}\n${this.state.draft}` : raw;
|
|
362
|
-
this.setStatus(
|
|
374
|
+
this.setStatus(t("status.sendFailed", { error: errorText(error) }), "error");
|
|
363
375
|
}
|
|
364
376
|
}
|
|
365
377
|
|
|
@@ -432,6 +444,7 @@ export class ChatApp {
|
|
|
432
444
|
|
|
433
445
|
attachPalette(ChatApp);
|
|
434
446
|
attachInfo(ChatApp);
|
|
447
|
+
attachSettings(ChatApp);
|
|
435
448
|
attachReminders(ChatApp);
|
|
436
449
|
attachRoomSettings(ChatApp);
|
|
437
450
|
attachMessageActions(ChatApp);
|
package/src/tui/config.mjs
CHANGED
|
@@ -44,6 +44,8 @@ function parseClient(value) {
|
|
|
44
44
|
url: optionalString(raw.url, "client.url"),
|
|
45
45
|
token: optionalString(raw.token, "client.token"),
|
|
46
46
|
participant: optionalString(raw.participant, "client.participant"),
|
|
47
|
+
language: optionalString(raw.language, "client.language"),
|
|
48
|
+
theme: optionalString(raw.theme, "client.theme"),
|
|
47
49
|
requestTimeoutMs,
|
|
48
50
|
};
|
|
49
51
|
}
|
|
@@ -54,6 +56,8 @@ function parseConfig(value) {
|
|
|
54
56
|
return {
|
|
55
57
|
url: optionalString(raw.url, "url"),
|
|
56
58
|
token: optionalString(raw.token, "token"),
|
|
59
|
+
language: optionalString(raw.language, "language"),
|
|
60
|
+
theme: optionalString(raw.theme, "theme"),
|
|
57
61
|
client: parseClient(raw.client),
|
|
58
62
|
};
|
|
59
63
|
}
|
|
@@ -174,16 +178,20 @@ function stateFile(env, cwd, home) {
|
|
|
174
178
|
return path.join(stateHome, "chatroom-tui", "session.json");
|
|
175
179
|
}
|
|
176
180
|
|
|
177
|
-
|
|
178
|
-
|
|
181
|
+
/** Resolve startup config. extra = { flags, prefs, project } keeps the paths
|
|
182
|
+
* used by main.mjs testable; callers without extra behave as before. */
|
|
183
|
+
export function resolveTuiConfig(env = process.env, cwd = process.cwd(), extra = {}) {
|
|
184
|
+
const file = extra.project === undefined ? loadTuiConfig(env, cwd) : extra.project;
|
|
185
|
+
const flags = extra.flags ?? {};
|
|
186
|
+
const prefs = extra.prefs ?? {};
|
|
179
187
|
const home = env.HOME || os.homedir();
|
|
180
|
-
const url = (envValue(env, "CHAT_ROOM_SERVER_URL") ?? file?.client?.url ?? file?.url ?? "")
|
|
188
|
+
const url = (flags.server ?? envValue(env, "CHAT_ROOM_SERVER_URL") ?? prefs.url ?? file?.client?.url ?? file?.url ?? "")
|
|
181
189
|
.trim().replace(/\/+$/, "");
|
|
182
190
|
if (!url) throw new Error(`chatroom TUI: server URL is not set; ${URL_HINT}`);
|
|
183
191
|
if (!/^https?:\/\/[^/]+/i.test(url)) {
|
|
184
192
|
throw new Error(`chatroom TUI: server URL must be an http(s) URL; ${URL_HINT}`);
|
|
185
193
|
}
|
|
186
|
-
const name = envValue(env, "CHAT_ROOM_TUI_NAME") ?? file?.client?.participant ?? DEFAULT_PARTICIPANT;
|
|
194
|
+
const name = flags.name ?? envValue(env, "CHAT_ROOM_TUI_NAME") ?? prefs.participant ?? file?.client?.participant ?? DEFAULT_PARTICIPANT;
|
|
187
195
|
return {
|
|
188
196
|
url,
|
|
189
197
|
token: envValue(env, "CHAT_ROOM_SERVER_TOKEN") ?? file?.client?.token ?? file?.token ?? "",
|
package/src/tui/i18n.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Terminal UI language layer. Modules call t("key", vars); a missing key falls
|
|
2
|
+
// back to English and then to the key itself, so a language pack can never
|
|
3
|
+
// blank the screen. Dictionaries live in locales/en.mjs and locales/zh-CN.mjs.
|
|
4
|
+
import EN from "./locales/en.mjs";
|
|
5
|
+
import ZH from "./locales/zh-CN.mjs";
|
|
6
|
+
|
|
7
|
+
export const LANGUAGES = ["en", "zh-CN"];
|
|
8
|
+
export const DEFAULT_LANGUAGE = "en";
|
|
9
|
+
|
|
10
|
+
export function normalizeLanguage(value) {
|
|
11
|
+
const text = String(value ?? "").trim().toLowerCase().replace("_", "-");
|
|
12
|
+
if (text === "zh" || text === "zh-cn") return "zh-CN";
|
|
13
|
+
return DEFAULT_LANGUAGE;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const DICTIONARIES = { en: EN, "zh-CN": ZH };
|
|
17
|
+
let current = DEFAULT_LANGUAGE;
|
|
18
|
+
|
|
19
|
+
export function setLanguage(value) {
|
|
20
|
+
current = normalizeLanguage(value);
|
|
21
|
+
return current;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getLanguage() {
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function messageKeys(language = DEFAULT_LANGUAGE) {
|
|
29
|
+
return Object.keys(DICTIONARIES[normalizeLanguage(language)]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Look up a visible string. Vars replace {name} placeholders. */
|
|
33
|
+
export function t(key, vars) {
|
|
34
|
+
const text = DICTIONARIES[current]?.[key] ?? EN[key] ?? key;
|
|
35
|
+
if (!vars) return text;
|
|
36
|
+
return String(text).replace(/\{(\w+)\}/g, (match, name) =>
|
|
37
|
+
vars[name] === undefined ? match : String(vars[name]));
|
|
38
|
+
}
|