@alexsun-top/opencode-chat-room 0.7.5 → 0.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +5 -5
- package/README.zh-CN.md +5 -5
- package/cli.ts +20 -0
- package/dist/cli.js +753 -164
- package/package.json +2 -1
- package/plugin.ts +15 -3
- package/server/assets/base.css +0 -2
- package/server/assets/chat/doc-preview.js +122 -0
- package/server/assets/chat/editor.js +10 -7
- package/server/assets/chat/events.js +7 -1
- package/server/assets/chat/files.js +14 -6
- package/server/assets/chat/index.js +4 -0
- package/server/assets/chat/locales/de.js +21 -3
- package/server/assets/chat/locales/es.js +21 -3
- package/server/assets/chat/locales/fr.js +21 -3
- package/server/assets/chat/locales/it.js +21 -3
- package/server/assets/chat/locales/ja.js +21 -3
- package/server/assets/chat/locales/ko.js +21 -3
- package/server/assets/chat/markdown.js +39 -5
- package/server/assets/chat/messages.js +1 -0
- package/server/assets/chat/palette.js +18 -9
- package/server/assets/chat/questions.js +1 -0
- package/server/assets/chat/rooms.js +6 -1
- package/server/assets/chat/shortcuts.js +14 -3
- package/server/assets/chat/state.js +42 -6
- package/server/assets/chat/system-messages.js +9 -1
- package/server/assets/chat/task-form.js +197 -0
- package/server/assets/chat/task-row.js +169 -0
- package/server/assets/chat/tasks.js +494 -0
- package/server/assets/doc-preview.css +158 -0
- package/server/assets/edit-room.css +8 -0
- package/server/assets/responsive.css +26 -1
- package/server/assets/settings.css +9 -2
- package/server/assets/tasks.css +422 -0
- package/server/chat.html +129 -33
- package/server/member-routes.ts +3 -0
- package/server/sw.js +13 -2
- package/server/task-routes.ts +53 -0
- package/skills/chatroom/SKILL.md +3 -1
- package/src/README.md +3 -3
- package/src/agent/task-tool.ts +155 -0
- package/src/center/center-client.ts +44 -0
- package/src/rooms/tasks.ts +246 -0
- package/src/runtime/v2-plugin.ts +11 -2
- package/src/state/sqlite-state.ts +67 -0
- package/src/tui/app.mjs +21 -4
- package/src/tui/http.mjs +109 -10
- package/src/tui/info-app.mjs +435 -0
- package/src/tui/info-content.mjs +198 -0
- package/src/tui/join-app.mjs +71 -0
- package/src/tui/main.mjs +23 -4
- package/src/tui/markdown.mjs +293 -0
- package/src/tui/mentions.mjs +218 -0
- package/src/tui/message-app.mjs +178 -0
- package/src/tui/message-view.mjs +128 -0
- package/src/tui/palette-app.mjs +91 -8
- package/src/tui/palette.mjs +17 -2
- package/src/tui/reminder-app.mjs +189 -0
- package/src/tui/room-settings-app.mjs +249 -0
- package/src/tui/ui-content.mjs +144 -94
- package/src/tui/ui-mentions.mjs +44 -0
- package/src/tui/ui-overlay-view.mjs +255 -0
- package/src/tui/ui-suggest-view.mjs +63 -0
- package/src/tui/ui-suggest.mjs +197 -0
- package/src/tui/ui-theme.mjs +2 -1
- package/src/tui/ui-view.mjs +4 -190
- package/src/tui/ui.mjs +111 -1
package/src/tui/http.mjs
CHANGED
|
@@ -35,20 +35,24 @@ function query(params) {
|
|
|
35
35
|
export function createApi({ url, token, requestTimeoutMs = 10000 }) {
|
|
36
36
|
const base = String(url ?? "").replace(/\/+$/, "");
|
|
37
37
|
|
|
38
|
-
async function call(route, { method = "GET", body, signal } = {}) {
|
|
39
|
-
const headers = {};
|
|
38
|
+
async function call(route, { method = "GET", body, signal, timeoutMs = requestTimeoutMs, headers: extra = {}, parseJson = true } = {}) {
|
|
39
|
+
const headers = { ...extra };
|
|
40
40
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
41
|
-
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
42
|
-
const timeout = AbortSignal.timeout(
|
|
41
|
+
if (body !== undefined && typeof body !== "string" && !(body instanceof Uint8Array)) headers["Content-Type"] = "application/json";
|
|
42
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
43
43
|
const res = await fetch(`${base}${route}`, {
|
|
44
44
|
method,
|
|
45
45
|
headers,
|
|
46
|
-
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
46
|
+
...(body === undefined ? {} : { body: typeof body === "string" || body instanceof Uint8Array ? body : JSON.stringify(body) }),
|
|
47
47
|
signal: signal ? AbortSignal.any([timeout, signal]) : timeout,
|
|
48
48
|
});
|
|
49
|
-
|
|
49
|
+
// Binary downloads must not be cloned and JSON-parsed; that would duplicate the
|
|
50
|
+
// whole payload in memory before the caller reads it.
|
|
51
|
+
const data = parseJson || !res.ok
|
|
52
|
+
? await res.clone().json().catch(() => ({}))
|
|
53
|
+
: {};
|
|
50
54
|
if (!res.ok) throw apiError(res.status, res.statusText, data);
|
|
51
|
-
return { status: res.status, data };
|
|
55
|
+
return { status: res.status, data, response: res };
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
const room = (roomId) => `/rooms/${encodeURIComponent(roomId)}`;
|
|
@@ -83,21 +87,116 @@ export function createApi({ url, token, requestTimeoutMs = 10000 }) {
|
|
|
83
87
|
async leave(roomId, sessionID) {
|
|
84
88
|
await call(`${room(roomId)}/leave`, { method: "POST", body: { sessionID } });
|
|
85
89
|
},
|
|
86
|
-
async send(roomId, sessionID, text) {
|
|
87
|
-
return (await call(`${room(roomId)}/messages`, {
|
|
90
|
+
async send(roomId, sessionID, text, { replyToMessageID, isQuestion } = {}) {
|
|
91
|
+
return (await call(`${room(roomId)}/messages`, {
|
|
92
|
+
method: "POST",
|
|
93
|
+
body: { sessionID, text, ...(replyToMessageID ? { replyToMessageID } : {}), ...(isQuestion ? { isQuestion: true } : {}) },
|
|
94
|
+
})).data;
|
|
88
95
|
},
|
|
89
96
|
async poll(roomId, sessionID, afterTs, { signal, timeoutMs = 20000 } = {}) {
|
|
90
97
|
const wait = Number.isFinite(timeoutMs) ? Math.max(0, Math.floor(timeoutMs)) : 20000;
|
|
91
98
|
const after = Number.isFinite(afterTs) && afterTs >= 0 ? afterTs : undefined;
|
|
92
99
|
const params = { sessionID, roomId, excludeSelf: 0, timeout: wait, afterTs: after };
|
|
93
|
-
|
|
100
|
+
// The inbox holds the request for up to `wait`; the normal request timeout
|
|
101
|
+
// must not cut that long poll short and report a false disconnect.
|
|
102
|
+
const longPollTimeout = Math.max(requestTimeoutMs, wait + 5000);
|
|
103
|
+
return (await call(`/inbox?${query(params)}`, { signal, timeoutMs: longPollTimeout })).data;
|
|
94
104
|
},
|
|
95
105
|
async markRead(roomId, sessionID, ts) {
|
|
96
106
|
await call("/read", { method: "POST", body: { roomId, sessionID, ts } });
|
|
97
107
|
},
|
|
108
|
+
async members(roomId) {
|
|
109
|
+
return (await call(`${room(roomId)}/members`)).data.members ?? [];
|
|
110
|
+
},
|
|
111
|
+
async announcements(roomId) {
|
|
112
|
+
return (await call(`${room(roomId)}/announcements`)).data.announcements ?? [];
|
|
113
|
+
},
|
|
114
|
+
async saveAnnouncement(roomId, name, content, sessionID) {
|
|
115
|
+
return (await call(`${room(roomId)}/announcements/${encodeURIComponent(name)}`, {
|
|
116
|
+
method: "PUT", body: { sessionID, content },
|
|
117
|
+
})).data.announcement;
|
|
118
|
+
},
|
|
119
|
+
async deleteAnnouncement(roomId, name, sessionID) {
|
|
120
|
+
await call(`${room(roomId)}/announcements/${encodeURIComponent(name)}`, { method: "DELETE", body: { sessionID } });
|
|
121
|
+
},
|
|
122
|
+
async questions(roomId) {
|
|
123
|
+
return (await call(`${room(roomId)}/questions`)).data.questions ?? [];
|
|
124
|
+
},
|
|
125
|
+
async answerQuestion(roomId, questionId, sessionID, text) {
|
|
126
|
+
return (await call(`${room(roomId)}/questions/${encodeURIComponent(questionId)}/answers`, {
|
|
127
|
+
method: "POST", body: { sessionID, text },
|
|
128
|
+
})).data;
|
|
129
|
+
},
|
|
130
|
+
async reminders(roomId, sessionID) {
|
|
131
|
+
return (await call(`${room(roomId)}/reminders?${query({ sessionID })}`)).data.reminders ?? [];
|
|
132
|
+
},
|
|
133
|
+
async createReminder(roomId, sessionID, text, schedule) {
|
|
134
|
+
return (await call(`${room(roomId)}/reminders`, { method: "POST", body: { sessionID, text, schedule } })).data.reminder;
|
|
135
|
+
},
|
|
136
|
+
async updateReminder(roomId, reminderId, sessionID, patch) {
|
|
137
|
+
return (await call(`${room(roomId)}/reminders/${encodeURIComponent(reminderId)}`, {
|
|
138
|
+
method: "PATCH", body: { sessionID, ...patch },
|
|
139
|
+
})).data.reminder;
|
|
140
|
+
},
|
|
141
|
+
async deleteReminder(roomId, reminderId, sessionID) {
|
|
142
|
+
await call(`${room(roomId)}/reminders/${encodeURIComponent(reminderId)}`, { method: "DELETE", body: { sessionID } });
|
|
143
|
+
},
|
|
144
|
+
async recall(roomId, messageId, sessionID) {
|
|
145
|
+
return (await call(`${room(roomId)}/messages/${encodeURIComponent(messageId)}/recall`, {
|
|
146
|
+
method: "POST", body: { sessionID },
|
|
147
|
+
})).data;
|
|
148
|
+
},
|
|
149
|
+
async reveal(roomId, messageId, sessionID) {
|
|
150
|
+
return (await call(`${room(roomId)}/messages/${encodeURIComponent(messageId)}/content?${query({ sessionID })}`)).data;
|
|
151
|
+
},
|
|
152
|
+
async resources(roomId, sessionID, limit = 500, offset = 0) {
|
|
153
|
+
return (await call(`${room(roomId)}/resources?${query({ limit, offset })}`, {
|
|
154
|
+
headers: { "x-session-id": sessionID },
|
|
155
|
+
})).data;
|
|
156
|
+
},
|
|
157
|
+
async resourceContent(roomId, resourceId) {
|
|
158
|
+
const { response } = await call(`${room(roomId)}/resources/${encodeURIComponent(resourceId)}/content`, {
|
|
159
|
+
parseJson: false,
|
|
160
|
+
});
|
|
161
|
+
return { bytes: new Uint8Array(await response.arrayBuffer()), contentType: response.headers.get("content-type") ?? "" };
|
|
162
|
+
},
|
|
163
|
+
async deleteResource(roomId, resourceId, sessionID) {
|
|
164
|
+
const { data } = await call(`${room(roomId)}/resources/${encodeURIComponent(resourceId)}`, {
|
|
165
|
+
method: "DELETE",
|
|
166
|
+
body: {},
|
|
167
|
+
headers: { "x-session-id": sessionID },
|
|
168
|
+
});
|
|
169
|
+
return data;
|
|
170
|
+
},
|
|
171
|
+
async joinRequests(roomId, sessionID) {
|
|
172
|
+
const { status, data } = await call(`${room(roomId)}/join-requests?${query({ sessionID })}`);
|
|
173
|
+
return { status, requests: Array.isArray(data) ? data : [] };
|
|
174
|
+
},
|
|
175
|
+
async decideJoin(roomId, requestSessionID, sessionID, decision) {
|
|
176
|
+
const { data } = await call(`${room(roomId)}/join-requests/${encodeURIComponent(requestSessionID)}/${decision}`, {
|
|
177
|
+
method: "POST",
|
|
178
|
+
body: { sessionID },
|
|
179
|
+
});
|
|
180
|
+
return data;
|
|
181
|
+
},
|
|
182
|
+
async uploadResource(roomId, sessionID, filename, bytes) {
|
|
183
|
+
const { data } = await call(`${room(roomId)}/resources`, {
|
|
184
|
+
method: "POST",
|
|
185
|
+
body: bytes,
|
|
186
|
+
headers: { "x-session-id": sessionID, "x-file-name": encodeURIComponent(filename), "Content-Type": "application/octet-stream" },
|
|
187
|
+
});
|
|
188
|
+
return data.resource;
|
|
189
|
+
},
|
|
98
190
|
async createRoom(name, sessionID, ownerId, joinPolicy = "open") {
|
|
99
191
|
const body = { name, sessionID, ownerId, joinPolicy };
|
|
100
192
|
return (await call("/rooms", { method: "POST", body })).data;
|
|
101
193
|
},
|
|
194
|
+
async updateRoom(roomId, sessionID, patch) {
|
|
195
|
+
const body = { sessionID, ...patch };
|
|
196
|
+
return (await call(`${room(roomId)}`, { method: "PATCH", body })).data;
|
|
197
|
+
},
|
|
198
|
+
async deleteRoom(roomId, sessionID) {
|
|
199
|
+
return (await call(`${room(roomId)}`, { method: "DELETE", body: { sessionID } })).data;
|
|
200
|
+
},
|
|
102
201
|
};
|
|
103
202
|
}
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
// CLI parity commands: read-only member/question/file/reminder views plus
|
|
2
|
+
// announcement and question actions. Reuses the palette overlay as a scrollable
|
|
3
|
+
// info/list/detail surface instead of introducing another panel.
|
|
4
|
+
import {
|
|
5
|
+
announcementRows,
|
|
6
|
+
fileRows,
|
|
7
|
+
memberRows,
|
|
8
|
+
questionRows,
|
|
9
|
+
settingsMarkdown,
|
|
10
|
+
} from "./info-content.mjs";
|
|
11
|
+
import { sanitizeText } from "./messages.mjs";
|
|
12
|
+
|
|
13
|
+
const errorText = (error) => (error instanceof Error ? error.message : String(error ?? "unknown error"));
|
|
14
|
+
export const MAX_UPLOAD_BYTES = 100 * 1024 * 1024;
|
|
15
|
+
export const ANNOUNCEMENT_HINT = "n new · d delete · Enter read · Esc close";
|
|
16
|
+
|
|
17
|
+
export function createInfoPalette(kind, title, subtitle) {
|
|
18
|
+
return {
|
|
19
|
+
mode: "info",
|
|
20
|
+
kind,
|
|
21
|
+
title,
|
|
22
|
+
subtitle,
|
|
23
|
+
roomId: "",
|
|
24
|
+
items: [],
|
|
25
|
+
selected: 0,
|
|
26
|
+
error: "",
|
|
27
|
+
loading: false,
|
|
28
|
+
hint: "",
|
|
29
|
+
query: "",
|
|
30
|
+
scope: "room",
|
|
31
|
+
results: [],
|
|
32
|
+
searching: false,
|
|
33
|
+
prompt: null,
|
|
34
|
+
confirm: null,
|
|
35
|
+
detail: null,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function attachInfo(Class) {
|
|
40
|
+
Object.assign(Class.prototype, {
|
|
41
|
+
currentInfoRoom() {
|
|
42
|
+
if (this.state.current && this.state.joined) return this.state.current;
|
|
43
|
+
this.state.palette = null;
|
|
44
|
+
this.setStatus("open a joined room first", "error");
|
|
45
|
+
return null;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async openInfo(kind, title, hint, load) {
|
|
49
|
+
const room = this.currentInfoRoom();
|
|
50
|
+
if (!room) return;
|
|
51
|
+
const palette = createInfoPalette(kind, title, `#${room.name}`);
|
|
52
|
+
palette.roomId = room.id;
|
|
53
|
+
palette.hint = hint;
|
|
54
|
+
palette.loading = true;
|
|
55
|
+
this.state.palette = palette;
|
|
56
|
+
this.touch();
|
|
57
|
+
try {
|
|
58
|
+
const items = await load(room);
|
|
59
|
+
if (this.state.palette !== palette) return;
|
|
60
|
+
palette.items = Array.isArray(items) ? items : [];
|
|
61
|
+
palette.loading = false;
|
|
62
|
+
palette.selected = Math.min(palette.selected, Math.max(0, palette.items.length - 1));
|
|
63
|
+
this.touch();
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (this.state.palette !== palette) return;
|
|
66
|
+
palette.loading = false;
|
|
67
|
+
palette.error = errorText(error);
|
|
68
|
+
this.touch();
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
openMembers() {
|
|
73
|
+
return this.openInfo("members", "Members", "Read-only · Esc close", async (room) =>
|
|
74
|
+
memberRows(await this.api.members(room.id), this.sessionID));
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
openAnnouncements() {
|
|
78
|
+
return this.openInfo("announcements", "Announcements", ANNOUNCEMENT_HINT, async (room) =>
|
|
79
|
+
announcementRows(await this.api.announcements(room.id)));
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
openQuestions() {
|
|
83
|
+
return this.openInfo("questions", "Questions", "Enter answer · Esc close", async (room) =>
|
|
84
|
+
questionRows(await this.api.questions(room.id), this.state.messages));
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
openFiles() {
|
|
88
|
+
return this.openInfo("files", "Files", "Enter download · u upload · d delete · Esc close", async (room) => {
|
|
89
|
+
const result = await this.api.resources(room.id, this.sessionID);
|
|
90
|
+
return fileRows(Array.isArray(result) ? result : result?.resources);
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
openSettings() {
|
|
95
|
+
const text = settingsMarkdown({
|
|
96
|
+
...this.settings,
|
|
97
|
+
participant: this.participant,
|
|
98
|
+
theme: this.state.theme,
|
|
99
|
+
token: this.token,
|
|
100
|
+
});
|
|
101
|
+
this.state.help = false;
|
|
102
|
+
this.state.palette = {
|
|
103
|
+
...createInfoPalette("settings", "Settings", "read-only"),
|
|
104
|
+
mode: "detail",
|
|
105
|
+
detail: { title: "Settings", markdown: text, kind: "settings" },
|
|
106
|
+
hint: "↑↓ scroll · Esc close",
|
|
107
|
+
};
|
|
108
|
+
this.touch();
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
async runInfoSelection() {
|
|
112
|
+
const palette = this.state.palette;
|
|
113
|
+
if (!palette || palette.mode !== "info" || palette.loading) return;
|
|
114
|
+
const item = this.paletteItems()[palette.selected];
|
|
115
|
+
if (!item) return;
|
|
116
|
+
if (palette.kind === "announcements") {
|
|
117
|
+
palette.mode = "detail";
|
|
118
|
+
palette.detail = {
|
|
119
|
+
title: String(item.value),
|
|
120
|
+
markdown: String(item.content ?? "").trim() || "(empty announcement)",
|
|
121
|
+
kind: "announcements",
|
|
122
|
+
};
|
|
123
|
+
palette.hint = "d delete · Esc back";
|
|
124
|
+
this.touch();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (palette.kind === "questions" && item.open) {
|
|
128
|
+
palette.mode = "prompt";
|
|
129
|
+
palette.prompt = {
|
|
130
|
+
label: `Answer "${sanitizeText(item.text).slice(0, 40)}"`,
|
|
131
|
+
value: "",
|
|
132
|
+
action: "question-answer",
|
|
133
|
+
data: { id: String(item.value) },
|
|
134
|
+
};
|
|
135
|
+
palette.hint = "Enter post · Esc cancel";
|
|
136
|
+
this.touch();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (palette.kind === "join-requests") {
|
|
140
|
+
this.askJoinDecision("approve");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (palette.kind === "files") await this.downloadFile(item);
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
startAnnouncementCreate() {
|
|
147
|
+
const palette = this.state.palette;
|
|
148
|
+
if (!palette || palette.mode !== "info" || palette.kind !== "announcements") return;
|
|
149
|
+
palette.mode = "prompt";
|
|
150
|
+
palette.prompt = { label: "Announcement name (existing name replaces)", value: "", action: "announcement-name", data: {} };
|
|
151
|
+
palette.error = "";
|
|
152
|
+
palette.hint = "Enter next · Esc cancel";
|
|
153
|
+
this.touch();
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
askDeleteFile() {
|
|
157
|
+
const palette = this.state.palette;
|
|
158
|
+
if (!palette || palette.mode !== "info" || palette.kind !== "files") return;
|
|
159
|
+
const item = this.paletteItems()[palette.selected];
|
|
160
|
+
if (!item?.resource) return;
|
|
161
|
+
palette.mode = "confirm";
|
|
162
|
+
palette.previous = { mode: "info", hint: palette.hint };
|
|
163
|
+
palette.confirm = {
|
|
164
|
+
text: `Delete file "${sanitizeText(item.resource.name)}"?`,
|
|
165
|
+
action: "file-delete",
|
|
166
|
+
data: { id: item.resource.id, name: item.resource.name },
|
|
167
|
+
};
|
|
168
|
+
palette.error = "";
|
|
169
|
+
palette.hint = "Enter or y deletes · n or Esc cancels";
|
|
170
|
+
this.touch();
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
async runFileDeleteConfirm(confirm) {
|
|
174
|
+
const palette = this.state.palette;
|
|
175
|
+
const room = this.state.current;
|
|
176
|
+
if (!palette || !room) return;
|
|
177
|
+
palette.searching = true;
|
|
178
|
+
palette.error = "";
|
|
179
|
+
this.touch();
|
|
180
|
+
try {
|
|
181
|
+
await this.api.deleteResource(room.id, confirm.data.id, this.sessionID);
|
|
182
|
+
this.setStatus(`deleted ${confirm.data.name}`, "success");
|
|
183
|
+
await this.openFiles();
|
|
184
|
+
} catch (error) {
|
|
185
|
+
if (this.state.palette === palette) {
|
|
186
|
+
palette.mode = "info";
|
|
187
|
+
palette.confirm = null;
|
|
188
|
+
palette.previous = null;
|
|
189
|
+
palette.searching = false;
|
|
190
|
+
palette.hint = "Enter download · u upload · d delete · Esc close";
|
|
191
|
+
palette.error = errorText(error);
|
|
192
|
+
this.touch();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
startFileUpload() {
|
|
198
|
+
const palette = this.state.palette;
|
|
199
|
+
if (!palette || palette.mode !== "info" || palette.kind !== "files") return;
|
|
200
|
+
palette.mode = "prompt";
|
|
201
|
+
palette.prompt = { label: "Absolute file path (max 100 MiB)", value: "", action: "file-upload", data: {} };
|
|
202
|
+
palette.error = "";
|
|
203
|
+
palette.hint = "Enter upload · Esc cancel";
|
|
204
|
+
this.touch();
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
askDeleteAnnouncement(name) {
|
|
208
|
+
const palette = this.state.palette;
|
|
209
|
+
const selected = name ?? palette?.items?.[palette.selected]?.value;
|
|
210
|
+
if (!palette || !selected) return;
|
|
211
|
+
palette.mode = "confirm";
|
|
212
|
+
palette.error = "";
|
|
213
|
+
palette.previous = { mode: "info", hint: palette.hint };
|
|
214
|
+
palette.confirm = {
|
|
215
|
+
text: `Delete announcement "${sanitizeText(selected)}"?`,
|
|
216
|
+
action: "announcement-delete",
|
|
217
|
+
data: { name: String(selected) },
|
|
218
|
+
};
|
|
219
|
+
palette.hint = "Enter or y deletes · n or Esc cancels";
|
|
220
|
+
this.touch();
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
async runConfirm() {
|
|
224
|
+
const palette = this.state.palette;
|
|
225
|
+
const confirm = palette?.confirm;
|
|
226
|
+
if (!palette || !confirm) return;
|
|
227
|
+
if (typeof confirm.action === "string" && confirm.action.startsWith("message-")) {
|
|
228
|
+
await this.runMessageConfirm(confirm);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (typeof confirm.action === "string" && confirm.action.startsWith("reminder-")) {
|
|
232
|
+
await this.runReminderConfirm(confirm);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (typeof confirm.action === "string" && confirm.action.startsWith("room-")) {
|
|
236
|
+
await this.runRoomSettingsConfirm(confirm);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (typeof confirm.action === "string" && confirm.action.startsWith("join-")) {
|
|
240
|
+
await this.runJoinConfirm(confirm);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (typeof confirm.action === "string" && confirm.action.startsWith("file-")) {
|
|
244
|
+
await this.runFileDeleteConfirm(confirm);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (confirm.action === "announcement-delete") {
|
|
248
|
+
palette.searching = true;
|
|
249
|
+
this.touch();
|
|
250
|
+
try {
|
|
251
|
+
await this.api.deleteAnnouncement(palette.roomId, confirm.data.name, this.sessionID);
|
|
252
|
+
this.setStatus(`deleted announcement "${confirm.data.name}"`, "success");
|
|
253
|
+
await this.openAnnouncements();
|
|
254
|
+
} catch (error) {
|
|
255
|
+
if (this.state.palette === palette) {
|
|
256
|
+
palette.searching = false;
|
|
257
|
+
palette.error = errorText(error);
|
|
258
|
+
this.touch();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
cancelConfirm() {
|
|
265
|
+
const palette = this.state.palette;
|
|
266
|
+
if (!palette) return;
|
|
267
|
+
const previous = palette.previous;
|
|
268
|
+
palette.previous = null;
|
|
269
|
+
palette.confirm = null;
|
|
270
|
+
palette.error = "";
|
|
271
|
+
if (previous?.mode) {
|
|
272
|
+
palette.mode = previous.mode;
|
|
273
|
+
palette.hint = previous.hint ?? "";
|
|
274
|
+
} else {
|
|
275
|
+
palette.mode = "info";
|
|
276
|
+
palette.hint = ANNOUNCEMENT_HINT;
|
|
277
|
+
}
|
|
278
|
+
this.touch();
|
|
279
|
+
},
|
|
280
|
+
|
|
281
|
+
backFromDetail() {
|
|
282
|
+
const palette = this.state.palette;
|
|
283
|
+
if (!palette) return;
|
|
284
|
+
if (palette.kind !== "announcements") {
|
|
285
|
+
this.closePalette();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
palette.mode = "info";
|
|
289
|
+
palette.detail = null;
|
|
290
|
+
palette.hint = ANNOUNCEMENT_HINT;
|
|
291
|
+
this.touch();
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
async downloadFile(item) {
|
|
295
|
+
const room = this.state.current;
|
|
296
|
+
if (!room || !item?.resource) return;
|
|
297
|
+
const palette = this.state.palette;
|
|
298
|
+
if (palette) palette.searching = true;
|
|
299
|
+
this.touch();
|
|
300
|
+
try {
|
|
301
|
+
const payload = await this.api.resourceContent(room.id, item.resource.id);
|
|
302
|
+
const [{ mkdir, writeFile, access }, path] = await Promise.all([
|
|
303
|
+
import("node:fs/promises"),
|
|
304
|
+
import("node:path"),
|
|
305
|
+
]);
|
|
306
|
+
await mkdir(this.downloadDir, { recursive: true });
|
|
307
|
+
const base = path.basename(String(item.resource.name || "download")).replace(/[\0\r\n]/g, "_");
|
|
308
|
+
const filename = !base || base === "." || base === ".." ? "download" : base;
|
|
309
|
+
let target = path.join(this.downloadDir, filename);
|
|
310
|
+
try {
|
|
311
|
+
await access(target);
|
|
312
|
+
const extension = path.extname(filename);
|
|
313
|
+
target = path.join(this.downloadDir, `${path.basename(filename, extension)}-${Date.now()}${extension}`);
|
|
314
|
+
} catch {
|
|
315
|
+
// Target does not exist yet.
|
|
316
|
+
}
|
|
317
|
+
await writeFile(target, payload.bytes ?? payload);
|
|
318
|
+
this.closePalette();
|
|
319
|
+
this.setStatus(`saved ${target}`, "success");
|
|
320
|
+
} catch (error) {
|
|
321
|
+
if (this.state.palette === palette) {
|
|
322
|
+
palette.searching = false;
|
|
323
|
+
palette.error = errorText(error);
|
|
324
|
+
this.touch();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
|
|
329
|
+
async submitCommandPrompt(prompt, value) {
|
|
330
|
+
const palette = this.state.palette;
|
|
331
|
+
if (!palette || palette.mode !== "prompt") return;
|
|
332
|
+
if (typeof prompt.action === "string" && prompt.action.startsWith("reminder-")) {
|
|
333
|
+
await this.submitReminderPrompt(prompt, value);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (typeof prompt.action === "string" && prompt.action.startsWith("room-")) {
|
|
337
|
+
await this.submitRoomSettingsPrompt(prompt, value);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (typeof prompt.action === "string" && prompt.action.startsWith("message-")) {
|
|
341
|
+
await this.submitMessagePrompt(prompt, value);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const room = this.state.current;
|
|
345
|
+
if (!room) {
|
|
346
|
+
palette.error = "open a joined room first";
|
|
347
|
+
this.touch();
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (prompt.action === "announcement-name") {
|
|
351
|
+
const name = String(value ?? "").trim();
|
|
352
|
+
if (!name) {
|
|
353
|
+
palette.error = "enter an announcement name";
|
|
354
|
+
this.touch();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
palette.prompt = { label: `Content for "${sanitizeText(name)}"`, value: "", action: "announcement-content", data: { name } };
|
|
358
|
+
palette.error = "";
|
|
359
|
+
this.touch();
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (prompt.action === "announcement-content") {
|
|
363
|
+
const name = prompt.data?.name;
|
|
364
|
+
if (!name) return;
|
|
365
|
+
palette.searching = true;
|
|
366
|
+
this.touch();
|
|
367
|
+
try {
|
|
368
|
+
await this.api.saveAnnouncement(room.id, name, String(value ?? ""), this.sessionID);
|
|
369
|
+
this.setStatus(`saved announcement "${name}"`, "success");
|
|
370
|
+
await this.openAnnouncements();
|
|
371
|
+
} catch (error) {
|
|
372
|
+
if (this.state.palette === palette) {
|
|
373
|
+
palette.searching = false;
|
|
374
|
+
palette.error = errorText(error);
|
|
375
|
+
this.touch();
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (prompt.action === "question-answer") {
|
|
381
|
+
const answer = String(value ?? "").trim();
|
|
382
|
+
if (!answer) {
|
|
383
|
+
palette.error = "answer text is required";
|
|
384
|
+
this.touch();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
palette.searching = true;
|
|
388
|
+
this.touch();
|
|
389
|
+
try {
|
|
390
|
+
await this.api.answerQuestion(room.id, prompt.data?.id, this.sessionID, answer);
|
|
391
|
+
this.setStatus("answer posted", "success");
|
|
392
|
+
await this.openQuestions();
|
|
393
|
+
} catch (error) {
|
|
394
|
+
if (this.state.palette === palette) {
|
|
395
|
+
palette.searching = false;
|
|
396
|
+
palette.error = errorText(error);
|
|
397
|
+
this.touch();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (prompt.action === "file-upload") {
|
|
403
|
+
const file = String(value ?? "").trim();
|
|
404
|
+
const path = await import("node:path");
|
|
405
|
+
if (!file || !path.isAbsolute(file)) {
|
|
406
|
+
palette.error = "enter an absolute file path";
|
|
407
|
+
this.touch();
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
palette.searching = true;
|
|
411
|
+
this.touch();
|
|
412
|
+
try {
|
|
413
|
+
const fs = await import("node:fs/promises");
|
|
414
|
+
const info = await fs.stat(file);
|
|
415
|
+
if (info.size > MAX_UPLOAD_BYTES) {
|
|
416
|
+
palette.searching = false;
|
|
417
|
+
palette.error = "file is too large for terminal upload; limit 100 MiB";
|
|
418
|
+
this.touch();
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const bytes = new Uint8Array(await fs.readFile(file));
|
|
422
|
+
await this.api.uploadResource(room.id, this.sessionID, path.basename(file), bytes);
|
|
423
|
+
this.setStatus(`uploaded ${path.basename(file)}`, "success");
|
|
424
|
+
await this.openFiles();
|
|
425
|
+
} catch (error) {
|
|
426
|
+
if (this.state.palette === palette) {
|
|
427
|
+
palette.searching = false;
|
|
428
|
+
palette.error = errorText(error);
|
|
429
|
+
this.touch();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
}
|