@narumitw/pi-chat 0.1.5 → 0.1.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/README.md +47 -64
- package/dist/chunks/{chat-view-EZ24L3LM.js → chat-view-7OBZGYZM.ts} +2 -2
- package/dist/chunks/chat-view-7OBZGYZM.ts.map +7 -0
- package/dist/chunks/{chunk-ZR2JESR5.js → chunk-7FD7DDSU.ts} +6 -36
- package/dist/chunks/chunk-7FD7DDSU.ts.map +7 -0
- package/dist/chunks/{chunk-XYZZR6DW.js → chunk-EOZIFFAD.ts} +6 -23
- package/dist/chunks/chunk-EOZIFFAD.ts.map +7 -0
- package/dist/chunks/{chunk-UE3UAOJX.js → chunk-LN3NAZRC.ts} +1 -1
- package/dist/chunks/{chunk-UE3UAOJX.js.map → chunk-LN3NAZRC.ts.map} +1 -1
- package/dist/chunks/{chunk-OQLAKS7O.js → chunk-MHSYNYS3.ts} +4 -7
- package/dist/chunks/chunk-MHSYNYS3.ts.map +7 -0
- package/dist/chunks/{chunk-UQPD3L6X.js → chunk-WUCSLYAK.ts} +1 -1
- package/dist/chunks/chunk-WUCSLYAK.ts.map +7 -0
- package/dist/chunks/{directory-network-EWFZFLGK.js → directory-network-6OPGA4EH.ts} +7 -19
- package/dist/chunks/directory-network-6OPGA4EH.ts.map +7 -0
- package/dist/chunks/{menu-2XYZAYXF.js → menu-S7MZCVYM.ts} +7 -7
- package/dist/chunks/menu-S7MZCVYM.ts.map +7 -0
- package/dist/chunks/{network-MXCIGGQ2.js → network-Y4Z4JJ5J.ts} +3 -3
- package/dist/chunks/network-Y4Z4JJ5J.ts.map +7 -0
- package/dist/chunks/{widget-4OBABJDY.js → widget-QT53EBN6.ts} +4 -10
- package/dist/chunks/widget-QT53EBN6.ts.map +7 -0
- package/dist/index.ts +21 -69
- package/dist/index.ts.map +2 -2
- package/package.json +54 -58
- package/src/chat-session.ts +571 -594
- package/src/chat-view.ts +221 -226
- package/src/directory-network.ts +347 -365
- package/src/external.d.ts +56 -66
- package/src/identity.ts +74 -88
- package/src/menu.ts +535 -541
- package/src/network-contract.ts +5 -5
- package/src/network.ts +154 -157
- package/src/nickname.ts +6 -6
- package/src/pi-chat.ts +700 -777
- package/src/protocol.ts +296 -335
- package/src/public-room-directory.ts +276 -310
- package/src/room.ts +54 -58
- package/src/settings.ts +234 -259
- package/src/text.ts +57 -57
- package/src/widget.ts +56 -72
- package/dist/chunks/chat-view-EZ24L3LM.js.map +0 -7
- package/dist/chunks/chunk-OQLAKS7O.js.map +0 -7
- package/dist/chunks/chunk-UQPD3L6X.js.map +0 -7
- package/dist/chunks/chunk-XYZZR6DW.js.map +0 -7
- package/dist/chunks/chunk-ZR2JESR5.js.map +0 -7
- package/dist/chunks/directory-network-EWFZFLGK.js.map +0 -7
- package/dist/chunks/menu-2XYZAYXF.js.map +0 -7
- package/dist/chunks/network-MXCIGGQ2.js.map +0 -7
- package/dist/chunks/widget-4OBABJDY.js.map +0 -7
package/src/settings.ts
CHANGED
|
@@ -11,332 +11,307 @@ const MAX_SETTINGS_BYTES = 64 * 1024;
|
|
|
11
11
|
export type WidgetMode = "dock" | "count" | "latest" | "off";
|
|
12
12
|
|
|
13
13
|
export type RememberedRoom =
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
| { id: string; kind: "public"; slug: string }
|
|
15
|
+
| { id: string; kind: "private"; invite: string };
|
|
16
16
|
|
|
17
17
|
export interface ChatResumeSettings {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
rooms: RememberedRoom[];
|
|
19
|
+
activeRoomId: string;
|
|
20
|
+
surface: "chat" | "pi";
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export interface ChatSettings {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
nickname?: string;
|
|
25
|
+
identitySeed?: string;
|
|
26
|
+
widgetMode?: WidgetMode;
|
|
27
|
+
resume?: ChatResumeSettings;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export type ChatSettingsPatch = Partial<Omit<ChatSettings, "resume">> & {
|
|
31
|
-
|
|
31
|
+
resume?: ChatResumeSettings | null;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
export type ChatSettingsLoadResult =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
| { kind: "missing" }
|
|
36
|
+
| { kind: "invalid"; reason: string }
|
|
37
|
+
| { kind: "loaded"; settings: ChatSettings };
|
|
38
38
|
|
|
39
39
|
export interface UpdateChatSettingsOptions {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
settingsPath?: string;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
beforeRename?: (temporaryPath: string, settingsPath: string) => Promise<void>;
|
|
43
|
+
setPrivateMode?: (path: string) => Promise<void>;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
type SettingsDocument = Record<string, unknown>;
|
|
47
47
|
const queues = new Map<string, Promise<void>>();
|
|
48
48
|
|
|
49
49
|
export function chatSettingsPath(): string {
|
|
50
|
-
|
|
50
|
+
return join(getAgentDir(), CHAT_SETTINGS_FILE);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export function normalizeChatSettings(value: unknown): ChatSettings | undefined {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
54
|
+
if (!isRecord(value)) return undefined;
|
|
55
|
+
const settings: ChatSettings = {};
|
|
56
|
+
if (Object.hasOwn(value, "nickname")) {
|
|
57
|
+
const nickname = normalizeNickname(value.nickname);
|
|
58
|
+
if (!nickname) return undefined;
|
|
59
|
+
settings.nickname = nickname;
|
|
60
|
+
}
|
|
61
|
+
if (Object.hasOwn(value, "identitySeed")) {
|
|
62
|
+
if (typeof value.identitySeed !== "string" || !validSeed(value.identitySeed)) return undefined;
|
|
63
|
+
settings.identitySeed = value.identitySeed;
|
|
64
|
+
}
|
|
65
|
+
if (Object.hasOwn(value, "widgetMode")) {
|
|
66
|
+
if (
|
|
67
|
+
value.widgetMode !== "dock" &&
|
|
68
|
+
value.widgetMode !== "count" &&
|
|
69
|
+
value.widgetMode !== "latest" &&
|
|
70
|
+
value.widgetMode !== "off"
|
|
71
|
+
) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
settings.widgetMode = value.widgetMode;
|
|
75
|
+
}
|
|
76
|
+
if (Object.hasOwn(value, "resume")) {
|
|
77
|
+
const resume = normalizeResumeSettings(value.resume);
|
|
78
|
+
if (!resume || !settings.nickname || !settings.identitySeed) return undefined;
|
|
79
|
+
settings.resume = resume;
|
|
80
|
+
}
|
|
81
|
+
return settings;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
export async function readChatSettings(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return { kind: "invalid", reason: `${settingsPath}: ${safeError(error)}` };
|
|
101
|
-
}
|
|
84
|
+
export async function readChatSettings(settingsPath = chatSettingsPath()): Promise<ChatSettingsLoadResult> {
|
|
85
|
+
await queues.get(settingsPath);
|
|
86
|
+
try {
|
|
87
|
+
const contents = await readSettings(settingsPath);
|
|
88
|
+
try {
|
|
89
|
+
const settings = normalizeChatSettings(JSON.parse(contents) as unknown);
|
|
90
|
+
return settings
|
|
91
|
+
? { kind: "loaded", settings }
|
|
92
|
+
: { kind: "invalid", reason: `${settingsPath}: invalid settings shape` };
|
|
93
|
+
} catch {
|
|
94
|
+
return { kind: "invalid", reason: `${settingsPath}: invalid JSON` };
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (isNodeError(error) && error.code === "ENOENT") return { kind: "missing" };
|
|
98
|
+
return { kind: "invalid", reason: `${settingsPath}: ${safeError(error)}` };
|
|
99
|
+
}
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
export function updateChatSettings(
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
patch: ChatSettingsPatch,
|
|
104
|
+
options: UpdateChatSettingsOptions = {},
|
|
107
105
|
): Promise<ChatSettings> {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
106
|
+
const settingsPath = options.settingsPath ?? chatSettingsPath();
|
|
107
|
+
return enqueue(settingsPath, async () => {
|
|
108
|
+
options.signal?.throwIfAborted();
|
|
109
|
+
const current = await readDocumentForUpdate(settingsPath);
|
|
110
|
+
const updated: SettingsDocument = { ...current, ...patch };
|
|
111
|
+
if (Object.hasOwn(patch, "resume")) {
|
|
112
|
+
if (patch.resume === null) delete updated.resume;
|
|
113
|
+
else if (patch.resume === undefined) {
|
|
114
|
+
if (Object.hasOwn(current, "resume")) updated.resume = current.resume;
|
|
115
|
+
else delete updated.resume;
|
|
116
|
+
} else updated.resume = mergeResumeDocument(current.resume, patch.resume);
|
|
117
|
+
}
|
|
118
|
+
const settings = normalizeChatSettings(updated);
|
|
119
|
+
if (!settings) throw new Error(`Pi Chat settings at ${settingsPath} have an invalid shape.`);
|
|
120
|
+
await publish(settingsPath, updated, options);
|
|
121
|
+
return settings;
|
|
122
|
+
});
|
|
125
123
|
}
|
|
126
124
|
|
|
127
125
|
export function rememberedRoomFromDescriptor(room: RoomDescriptor): RememberedRoom {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
126
|
+
if (room.kind === "public") {
|
|
127
|
+
const slug = room.label.startsWith("#") ? room.label.slice(1) : room.label;
|
|
128
|
+
const reconstructed = createPublicRoom(slug);
|
|
129
|
+
if (reconstructed.id !== room.id) throw new Error("Public room descriptor is inconsistent.");
|
|
130
|
+
return { id: room.id, kind: "public", slug };
|
|
131
|
+
}
|
|
132
|
+
if (!room.invite || parseInvite(room.invite).id !== room.id) {
|
|
133
|
+
throw new Error("Private room descriptor is inconsistent.");
|
|
134
|
+
}
|
|
135
|
+
return { id: room.id, kind: "private", invite: room.invite };
|
|
138
136
|
}
|
|
139
137
|
|
|
140
138
|
export function descriptorFromRememberedRoom(room: RememberedRoom): RoomDescriptor {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return descriptor;
|
|
139
|
+
const descriptor = room.kind === "public" ? createPublicRoom(room.slug) : parseInvite(room.invite);
|
|
140
|
+
if (descriptor.id !== room.id) throw new Error("Remembered room is inconsistent.");
|
|
141
|
+
return descriptor;
|
|
145
142
|
}
|
|
146
143
|
|
|
147
144
|
export async function awaitChatSettingsWrites(settingsPath = chatSettingsPath()): Promise<void> {
|
|
148
|
-
|
|
145
|
+
await queues.get(settingsPath);
|
|
149
146
|
}
|
|
150
147
|
|
|
151
148
|
function enqueue<T>(path: string, operation: () => Promise<T>): Promise<T> {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
149
|
+
const previous = queues.get(path) ?? Promise.resolve();
|
|
150
|
+
const result = previous.then(operation, operation);
|
|
151
|
+
const settled = result.then(
|
|
152
|
+
() => undefined,
|
|
153
|
+
() => undefined,
|
|
154
|
+
);
|
|
155
|
+
queues.set(path, settled);
|
|
156
|
+
void settled.finally(() => {
|
|
157
|
+
if (queues.get(path) === settled) queues.delete(path);
|
|
158
|
+
});
|
|
159
|
+
return result;
|
|
163
160
|
}
|
|
164
161
|
|
|
165
162
|
async function readDocumentForUpdate(path: string): Promise<SettingsDocument> {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
163
|
+
let contents: string;
|
|
164
|
+
try {
|
|
165
|
+
contents = await readSettings(path);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (isNodeError(error) && error.code === "ENOENT") return {};
|
|
168
|
+
throw new Error(`Pi Chat settings at ${path} are invalid: ${safeError(error)}`);
|
|
169
|
+
}
|
|
170
|
+
let value: unknown;
|
|
171
|
+
try {
|
|
172
|
+
value = JSON.parse(contents) as unknown;
|
|
173
|
+
} catch {
|
|
174
|
+
throw new Error(`Pi Chat settings at ${path} are invalid: invalid JSON`);
|
|
175
|
+
}
|
|
176
|
+
if (!isRecord(value) || !normalizeChatSettings(value)) {
|
|
177
|
+
throw new Error(`Pi Chat settings at ${path} are invalid: invalid settings shape`);
|
|
178
|
+
}
|
|
179
|
+
return value;
|
|
183
180
|
}
|
|
184
181
|
|
|
185
182
|
async function readSettings(path: string): Promise<string> {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
} finally {
|
|
217
|
-
await handle.close();
|
|
218
|
-
}
|
|
183
|
+
const pathStats = await lstat(path);
|
|
184
|
+
if (pathStats.isSymbolicLink() || !pathStats.isFile()) {
|
|
185
|
+
throw new Error("settings path is not a regular file");
|
|
186
|
+
}
|
|
187
|
+
const noFollow = constants.O_NOFOLLOW ?? 0;
|
|
188
|
+
const handle = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | noFollow);
|
|
189
|
+
try {
|
|
190
|
+
const stats = await handle.stat();
|
|
191
|
+
if (!stats.isFile() || stats.dev !== pathStats.dev || stats.ino !== pathStats.ino) {
|
|
192
|
+
throw new Error("settings path changed while opening");
|
|
193
|
+
}
|
|
194
|
+
if (stats.size > MAX_SETTINGS_BYTES) {
|
|
195
|
+
throw new Error(`settings file exceeds ${MAX_SETTINGS_BYTES} bytes`);
|
|
196
|
+
}
|
|
197
|
+
const buffer = Buffer.alloc(MAX_SETTINGS_BYTES + 1);
|
|
198
|
+
let offset = 0;
|
|
199
|
+
while (offset < buffer.length) {
|
|
200
|
+
const { bytesRead } = await handle.read(buffer, offset, buffer.length - offset, offset);
|
|
201
|
+
if (bytesRead === 0) break;
|
|
202
|
+
offset += bytesRead;
|
|
203
|
+
}
|
|
204
|
+
if (offset > MAX_SETTINGS_BYTES) throw new Error(`settings file exceeds ${MAX_SETTINGS_BYTES} bytes`);
|
|
205
|
+
try {
|
|
206
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(buffer.subarray(0, offset));
|
|
207
|
+
} catch {
|
|
208
|
+
throw new Error("settings file is not valid UTF-8");
|
|
209
|
+
}
|
|
210
|
+
} finally {
|
|
211
|
+
await handle.close();
|
|
212
|
+
}
|
|
219
213
|
}
|
|
220
214
|
|
|
221
|
-
async function publish(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
} catch (error) {
|
|
249
|
-
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
250
|
-
throw error;
|
|
251
|
-
}
|
|
215
|
+
async function publish(path: string, document: SettingsDocument, options: UpdateChatSettingsOptions): Promise<void> {
|
|
216
|
+
options.signal?.throwIfAborted();
|
|
217
|
+
const contents = `${JSON.stringify(document, null, 2)}\n`;
|
|
218
|
+
if (Buffer.byteLength(contents) > MAX_SETTINGS_BYTES) {
|
|
219
|
+
throw new Error(`Pi Chat settings document exceeds ${MAX_SETTINGS_BYTES} bytes.`);
|
|
220
|
+
}
|
|
221
|
+
const directory = dirname(path);
|
|
222
|
+
await mkdir(directory, { recursive: true });
|
|
223
|
+
const temporaryPath = join(directory, `.${basename(path)}.${process.pid}.${randomUUID()}.tmp`);
|
|
224
|
+
try {
|
|
225
|
+
await writeFile(temporaryPath, contents, {
|
|
226
|
+
encoding: "utf8",
|
|
227
|
+
flag: "wx",
|
|
228
|
+
mode: 0o600,
|
|
229
|
+
signal: options.signal,
|
|
230
|
+
});
|
|
231
|
+
await options.beforeRename?.(temporaryPath, path);
|
|
232
|
+
options.signal?.throwIfAborted();
|
|
233
|
+
if (process.platform !== "win32") {
|
|
234
|
+
await (options.setPrivateMode ?? ((target) => chmod(target, 0o600)))(temporaryPath);
|
|
235
|
+
options.signal?.throwIfAborted();
|
|
236
|
+
}
|
|
237
|
+
await rename(temporaryPath, path);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
252
242
|
}
|
|
253
243
|
|
|
254
244
|
function normalizeResumeSettings(value: unknown): ChatResumeSettings | undefined {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
if (!activeRoomId || !ids.has(activeRoomId)) return undefined;
|
|
275
|
-
return { rooms, activeRoomId, surface: value.surface };
|
|
245
|
+
if (!isRecord(value) || !Array.isArray(value.rooms)) return undefined;
|
|
246
|
+
if (value.rooms.length === 0 || value.rooms.length > 16) return undefined;
|
|
247
|
+
if (typeof value.activeRoomId !== "string") return undefined;
|
|
248
|
+
if (value.surface !== "chat" && value.surface !== "pi") return undefined;
|
|
249
|
+
const rooms: RememberedRoom[] = [];
|
|
250
|
+
const ids = new Set<string>();
|
|
251
|
+
let activeRoomId: string | undefined;
|
|
252
|
+
for (const candidate of value.rooms) {
|
|
253
|
+
const room = normalizeRememberedRoom(candidate);
|
|
254
|
+
if (!room || ids.has(room.id)) return undefined;
|
|
255
|
+
ids.add(room.id);
|
|
256
|
+
rooms.push(room);
|
|
257
|
+
if (isRecord(candidate) && (candidate.id === value.activeRoomId || room.id === value.activeRoomId)) {
|
|
258
|
+
activeRoomId = room.id;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!activeRoomId || !ids.has(activeRoomId)) return undefined;
|
|
262
|
+
return { rooms, activeRoomId, surface: value.surface };
|
|
276
263
|
}
|
|
277
264
|
|
|
278
265
|
function normalizeRememberedRoom(value: unknown): RememberedRoom | undefined {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
typeof value.invite === "string" &&
|
|
294
|
-
!Object.hasOwn(value, "slug")
|
|
295
|
-
) {
|
|
296
|
-
const room = parseInvite(value.invite);
|
|
297
|
-
return isCompatibleRoomId(room, value.id)
|
|
298
|
-
? { id: room.id, kind: "private", invite: value.invite }
|
|
299
|
-
: undefined;
|
|
300
|
-
}
|
|
301
|
-
} catch {
|
|
302
|
-
return undefined;
|
|
303
|
-
}
|
|
304
|
-
return undefined;
|
|
266
|
+
if (!isRecord(value) || typeof value.id !== "string") return undefined;
|
|
267
|
+
try {
|
|
268
|
+
if (value.kind === "public" && typeof value.slug === "string" && !Object.hasOwn(value, "invite")) {
|
|
269
|
+
const room = createPublicRoom(value.slug);
|
|
270
|
+
return isCompatibleRoomId(room, value.id) ? { id: room.id, kind: "public", slug: value.slug } : undefined;
|
|
271
|
+
}
|
|
272
|
+
if (value.kind === "private" && typeof value.invite === "string" && !Object.hasOwn(value, "slug")) {
|
|
273
|
+
const room = parseInvite(value.invite);
|
|
274
|
+
return isCompatibleRoomId(room, value.id) ? { id: room.id, kind: "private", invite: value.invite } : undefined;
|
|
275
|
+
}
|
|
276
|
+
} catch {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
return undefined;
|
|
305
280
|
}
|
|
306
281
|
|
|
307
282
|
function mergeResumeDocument(current: unknown, next: ChatResumeSettings): SettingsDocument {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
283
|
+
const currentResume = isRecord(current) ? current : {};
|
|
284
|
+
const existingRooms = new Map<string, SettingsDocument>();
|
|
285
|
+
if (Array.isArray(currentResume.rooms)) {
|
|
286
|
+
for (const candidate of currentResume.rooms) {
|
|
287
|
+
const normalized = normalizeRememberedRoom(candidate);
|
|
288
|
+
if (normalized && isRecord(candidate)) existingRooms.set(normalized.id, candidate);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
...currentResume,
|
|
293
|
+
rooms: next.rooms.map((room) => ({ ...existingRooms.get(room.id), ...room })),
|
|
294
|
+
activeRoomId: next.activeRoomId,
|
|
295
|
+
surface: next.surface,
|
|
296
|
+
};
|
|
322
297
|
}
|
|
323
298
|
|
|
324
299
|
function validSeed(value: string): boolean {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
300
|
+
try {
|
|
301
|
+
return /^[A-Za-z0-9_-]{43}$/u.test(value) && Buffer.from(value, "base64url").length === 32;
|
|
302
|
+
} catch {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
330
305
|
}
|
|
331
306
|
|
|
332
307
|
function isRecord(value: unknown): value is SettingsDocument {
|
|
333
|
-
|
|
308
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
334
309
|
}
|
|
335
310
|
|
|
336
311
|
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
337
|
-
|
|
312
|
+
return error instanceof Error && "code" in error;
|
|
338
313
|
}
|
|
339
314
|
|
|
340
315
|
function safeError(error: unknown): string {
|
|
341
|
-
|
|
316
|
+
return error instanceof Error ? error.message : String(error);
|
|
342
317
|
}
|