@narumitw/pi-chat 0.1.6 → 0.1.8
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 +17 -34
- 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-2KEDM6NJ.js → menu-S7MZCVYM.ts} +5 -5
- 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 +20 -66
- package/dist/index.ts.map +2 -2
- package/package.json +54 -55
- 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 -774
- 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-2KEDM6NJ.js.map +0 -7
- package/dist/chunks/network-MXCIGGQ2.js.map +0 -7
- package/dist/chunks/widget-4OBABJDY.js.map +0 -7
package/src/pi-chat.ts
CHANGED
|
@@ -1,53 +1,44 @@
|
|
|
1
1
|
import { randomBytes as nodeRandomBytes } from "node:crypto";
|
|
2
|
-
import type {
|
|
3
|
-
ExtensionAPI,
|
|
4
|
-
ExtensionCommandContext,
|
|
5
|
-
ExtensionContext,
|
|
6
|
-
} from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
7
3
|
import type { ChatSnapshot, ChatTransport } from "./chat-session.js";
|
|
8
4
|
import { ChatSession } from "./chat-session.js";
|
|
9
5
|
import type { PublicRoomDirectory } from "./directory-network.js";
|
|
10
|
-
import {
|
|
11
|
-
type ChatIdentity,
|
|
12
|
-
createIdentity,
|
|
13
|
-
deriveScopedIdentity,
|
|
14
|
-
formatIdentityLabel,
|
|
15
|
-
} from "./identity.js";
|
|
6
|
+
import { type ChatIdentity, createIdentity, deriveScopedIdentity, formatIdentityLabel } from "./identity.js";
|
|
16
7
|
import type { ChatMenuSource, JoinRoomOptions } from "./menu.js";
|
|
17
8
|
import { type HyperswarmTransportOptions, MAX_DIRECT_NEIGHBORS } from "./network-contract.js";
|
|
18
9
|
import { createPrivateRoom, createPublicRoom, parseInvite, type RoomDescriptor } from "./room.js";
|
|
19
10
|
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
awaitChatSettingsWrites,
|
|
12
|
+
type ChatSettings,
|
|
13
|
+
chatSettingsPath,
|
|
14
|
+
descriptorFromRememberedRoom,
|
|
15
|
+
readChatSettings,
|
|
16
|
+
rememberedRoomFromDescriptor,
|
|
17
|
+
updateChatSettings as updateChatSettingsFile,
|
|
18
|
+
type WidgetMode,
|
|
28
19
|
} from "./settings.js";
|
|
29
20
|
import { sanitizeSingleLine } from "./text.js";
|
|
30
21
|
|
|
31
22
|
const CHAT_UI_KEY = "chat";
|
|
32
23
|
const USAGE = "Usage: /chat, /chat <pichat:v1-or-v2:invite>, or /chat #<public-slug>";
|
|
33
24
|
const PRIVATE_ROOM_OPTIONS = [
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
25
|
+
"Join and remember — Save the bearer invite privately and restore this room",
|
|
26
|
+
"Join once — Do not save the bearer invite",
|
|
27
|
+
"Cancel",
|
|
37
28
|
] as const;
|
|
38
29
|
const DISPLAY_OPTIONS: ReadonlyArray<{ label: string; mode: WidgetMode }> = [
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
30
|
+
{
|
|
31
|
+
label: "Room dock — Show up to three recent messages above the Pi editor",
|
|
32
|
+
mode: "dock",
|
|
33
|
+
},
|
|
34
|
+
{ label: "Latest message — Show one recent message above the Pi editor", mode: "latest" },
|
|
35
|
+
{ label: "Status only — Hide message text", mode: "count" },
|
|
36
|
+
{ label: "Hidden — Show no persistent room widget", mode: "off" },
|
|
46
37
|
];
|
|
47
38
|
|
|
48
39
|
export interface PiChatDirectoryOptions {
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
identity: ChatIdentity;
|
|
41
|
+
advertisedSlug?: string;
|
|
51
42
|
}
|
|
52
43
|
|
|
53
44
|
type Awaitable<Value> = Value | Promise<Value>;
|
|
@@ -56,778 +47,713 @@ type ChatViewModule = Pick<typeof import("./chat-view.js"), "ChatView">;
|
|
|
56
47
|
type ChatWidgetModule = Pick<typeof import("./widget.js"), "renderChatWidget">;
|
|
57
48
|
|
|
58
49
|
export interface PiChatDependencies {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
50
|
+
settingsPath: string;
|
|
51
|
+
randomBytes(size: number): Buffer;
|
|
52
|
+
createTransport(options: HyperswarmTransportOptions): Awaitable<ChatTransport>;
|
|
53
|
+
createDirectory(options: PiChatDirectoryOptions): Awaitable<PublicRoomDirectory>;
|
|
54
|
+
updateSettings: typeof updateChatSettingsFile;
|
|
55
|
+
loadChatMenu(): Promise<ChatMenuModule>;
|
|
56
|
+
loadChatView(): Promise<ChatViewModule>;
|
|
57
|
+
loadChatWidget(): Promise<ChatWidgetModule>;
|
|
67
58
|
}
|
|
68
59
|
|
|
69
|
-
export function createPiChatExtension(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
loadChatWidget: cachedModuleLoader(
|
|
97
|
-
dependencies.loadChatWidget ?? (() => import("./widget.js")),
|
|
98
|
-
),
|
|
99
|
-
};
|
|
100
|
-
const updateChatSettings = deps.updateSettings;
|
|
60
|
+
export function createPiChatExtension(dependencies: Partial<PiChatDependencies> = {}): (pi: ExtensionAPI) => void {
|
|
61
|
+
const loadNetwork = cachedModuleLoader(() => import("./network.js"));
|
|
62
|
+
const loadDirectoryNetwork = cachedModuleLoader(() => import("./directory-network.js"));
|
|
63
|
+
const deps: PiChatDependencies = {
|
|
64
|
+
settingsPath: dependencies.settingsPath ?? chatSettingsPath(),
|
|
65
|
+
randomBytes: dependencies.randomBytes ?? nodeRandomBytes,
|
|
66
|
+
createTransport:
|
|
67
|
+
dependencies.createTransport ??
|
|
68
|
+
(async (options) => {
|
|
69
|
+
const { HyperswarmTransport } = await loadNetwork();
|
|
70
|
+
return new HyperswarmTransport(options);
|
|
71
|
+
}),
|
|
72
|
+
createDirectory:
|
|
73
|
+
dependencies.createDirectory ??
|
|
74
|
+
(async (options) => {
|
|
75
|
+
const { HyperswarmDirectoryTransport, PublicRoomDirectorySession } = await loadDirectoryNetwork();
|
|
76
|
+
return new PublicRoomDirectorySession({
|
|
77
|
+
...options,
|
|
78
|
+
transport: new HyperswarmDirectoryTransport({ identity: options.identity }),
|
|
79
|
+
});
|
|
80
|
+
}),
|
|
81
|
+
updateSettings: dependencies.updateSettings ?? updateChatSettingsFile,
|
|
82
|
+
loadChatMenu: cachedModuleLoader(dependencies.loadChatMenu ?? (() => import("./menu.js"))),
|
|
83
|
+
loadChatView: cachedModuleLoader(dependencies.loadChatView ?? (() => import("./chat-view.js"))),
|
|
84
|
+
loadChatWidget: cachedModuleLoader(dependencies.loadChatWidget ?? (() => import("./widget.js"))),
|
|
85
|
+
};
|
|
86
|
+
const updateChatSettings = deps.updateSettings;
|
|
101
87
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
88
|
+
return function piChatExtension(pi: ExtensionAPI): void {
|
|
89
|
+
let generation = 0;
|
|
90
|
+
let controller = new AbortController();
|
|
91
|
+
let activeSessionManager: unknown;
|
|
92
|
+
let activeContext: ExtensionContext | undefined;
|
|
93
|
+
let settings: ChatSettings = {};
|
|
94
|
+
let chatSession: ChatSession | undefined;
|
|
95
|
+
let roomDirectory: PublicRoomDirectory | undefined;
|
|
96
|
+
let latestSnapshot: ChatSnapshot | undefined;
|
|
97
|
+
let chatDraft = "";
|
|
98
|
+
let restoreError: string | undefined;
|
|
99
|
+
let restoringRoomId: string | undefined;
|
|
100
|
+
let joinOwner: { sessionManager: unknown; generation: number; token: symbol } | undefined;
|
|
101
|
+
const ownedTasks = new Set<Promise<unknown>>();
|
|
102
|
+
let renderChatWidget: ChatWidgetModule["renderChatWidget"] | undefined;
|
|
103
|
+
let widgetRender: (() => void) | undefined;
|
|
104
|
+
let widgetInstalled = false;
|
|
119
105
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
generation === ownerGeneration &&
|
|
123
|
-
!controller.signal.aborted;
|
|
106
|
+
const isCurrent = (owner: unknown, ownerGeneration: number) =>
|
|
107
|
+
activeSessionManager === owner && generation === ownerGeneration && !controller.signal.aborted;
|
|
124
108
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
109
|
+
const trackTask = <T>(task: Promise<T>): Promise<T> => {
|
|
110
|
+
ownedTasks.add(task);
|
|
111
|
+
void task.then(
|
|
112
|
+
() => ownedTasks.delete(task),
|
|
113
|
+
() => ownedTasks.delete(task),
|
|
114
|
+
);
|
|
115
|
+
return task;
|
|
116
|
+
};
|
|
133
117
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
118
|
+
const drainOwnedTasks = async (): Promise<void> => {
|
|
119
|
+
while (ownedTasks.size > 0) await Promise.allSettled([...ownedTasks]);
|
|
120
|
+
};
|
|
137
121
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
122
|
+
const clearUi = (ctx: ExtensionContext, ownerGeneration = generation): void => {
|
|
123
|
+
try {
|
|
124
|
+
ctx.ui.setWidget(CHAT_UI_KEY, undefined);
|
|
125
|
+
ctx.ui.setStatus(CHAT_UI_KEY, undefined);
|
|
126
|
+
} catch {
|
|
127
|
+
// Session replacement may invalidate an old UI immediately.
|
|
128
|
+
}
|
|
129
|
+
if (ctx.sessionManager !== activeSessionManager || generation !== ownerGeneration) return;
|
|
130
|
+
renderChatWidget = undefined;
|
|
131
|
+
widgetRender = undefined;
|
|
132
|
+
widgetInstalled = false;
|
|
133
|
+
};
|
|
150
134
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (widgetRender === requestRender) widgetRender = undefined;
|
|
197
|
-
},
|
|
198
|
-
};
|
|
199
|
-
});
|
|
200
|
-
widgetInstalled = true;
|
|
201
|
-
}
|
|
202
|
-
widgetRender?.();
|
|
203
|
-
} catch {
|
|
204
|
-
// UI updates are best-effort after replacement.
|
|
205
|
-
}
|
|
206
|
-
};
|
|
135
|
+
const updateUi = (ctx: ExtensionContext, snapshot?: ChatSnapshot): void => {
|
|
136
|
+
if (ctx.sessionManager !== activeSessionManager) return;
|
|
137
|
+
latestSnapshot = snapshot;
|
|
138
|
+
try {
|
|
139
|
+
ctx.ui.setStatus(
|
|
140
|
+
CHAT_UI_KEY,
|
|
141
|
+
snapshot?.state === "connecting"
|
|
142
|
+
? restoringRoomId === snapshot.room.id
|
|
143
|
+
? `chat: restoring ${sanitizeSingleLine(snapshot.room.label)}`
|
|
144
|
+
: "chat: joining"
|
|
145
|
+
: snapshot?.state === "degraded"
|
|
146
|
+
? "chat: reconnecting"
|
|
147
|
+
: undefined,
|
|
148
|
+
);
|
|
149
|
+
const mode = settings.widgetMode ?? "count";
|
|
150
|
+
if (ctx.mode !== "tui" || !snapshot || snapshot.state === "disconnected" || mode === "off") {
|
|
151
|
+
ctx.ui.setWidget(CHAT_UI_KEY, undefined);
|
|
152
|
+
widgetInstalled = false;
|
|
153
|
+
widgetRender = undefined;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const renderWidget = renderChatWidget;
|
|
157
|
+
if (!renderWidget) return;
|
|
158
|
+
if (!widgetInstalled) {
|
|
159
|
+
ctx.ui.setWidget(CHAT_UI_KEY, (tui, theme) => {
|
|
160
|
+
const requestRender = () => tui.requestRender();
|
|
161
|
+
widgetRender = requestRender;
|
|
162
|
+
return {
|
|
163
|
+
render: (width: number) =>
|
|
164
|
+
latestSnapshot
|
|
165
|
+
? renderWidget(latestSnapshot, settings.widgetMode ?? "count", width, theme, tui.terminal.rows)
|
|
166
|
+
: [],
|
|
167
|
+
invalidate() {},
|
|
168
|
+
dispose() {
|
|
169
|
+
if (widgetRender === requestRender) widgetRender = undefined;
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
widgetInstalled = true;
|
|
174
|
+
}
|
|
175
|
+
widgetRender?.();
|
|
176
|
+
} catch {
|
|
177
|
+
// UI updates are best-effort after replacement.
|
|
178
|
+
}
|
|
179
|
+
};
|
|
207
180
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
181
|
+
const disconnectRoom = async (): Promise<void> => {
|
|
182
|
+
const owned = chatSession;
|
|
183
|
+
const ownedDirectory = roomDirectory;
|
|
184
|
+
const uiContext = activeContext;
|
|
185
|
+
const uiGeneration = generation;
|
|
186
|
+
chatSession = undefined;
|
|
187
|
+
roomDirectory = undefined;
|
|
188
|
+
latestSnapshot = undefined;
|
|
189
|
+
chatDraft = "";
|
|
190
|
+
await Promise.allSettled([owned?.leave(), ownedDirectory?.stop()]);
|
|
191
|
+
if (uiContext) clearUi(uiContext, uiGeneration);
|
|
192
|
+
};
|
|
220
193
|
|
|
221
|
-
|
|
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
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (!isCurrent(owner, ownerGeneration)) return undefined;
|
|
275
|
-
settings = updated;
|
|
276
|
-
}
|
|
277
|
-
return createIdentity(Buffer.from(seed, "base64url"));
|
|
278
|
-
};
|
|
194
|
+
const ensureIdentity = async (
|
|
195
|
+
ctx: ExtensionContext,
|
|
196
|
+
signal: AbortSignal,
|
|
197
|
+
owner: unknown,
|
|
198
|
+
ownerGeneration: number,
|
|
199
|
+
): Promise<ChatIdentity | undefined> => {
|
|
200
|
+
let nickname = settings.nickname;
|
|
201
|
+
if (!nickname) {
|
|
202
|
+
const entered = await ctx.ui.input(
|
|
203
|
+
"Choose a Pi Chat nickname",
|
|
204
|
+
"Visible to room peers; no OS or Git identity is used",
|
|
205
|
+
{ signal },
|
|
206
|
+
);
|
|
207
|
+
if (!isCurrent(owner, ownerGeneration) || signal.aborted || !entered) return undefined;
|
|
208
|
+
nickname = entered;
|
|
209
|
+
}
|
|
210
|
+
let seed = settings.identitySeed;
|
|
211
|
+
let widgetMode = settings.widgetMode;
|
|
212
|
+
if (!seed) {
|
|
213
|
+
const candidate = createIdentity(deps.randomBytes(32));
|
|
214
|
+
if (!widgetMode) {
|
|
215
|
+
const selected = await ctx.ui.select(
|
|
216
|
+
"Joined room display",
|
|
217
|
+
DISPLAY_OPTIONS.map(({ label }) => label),
|
|
218
|
+
{ signal },
|
|
219
|
+
);
|
|
220
|
+
if (!selected || !isCurrent(owner, ownerGeneration) || signal.aborted) return undefined;
|
|
221
|
+
widgetMode = DISPLAY_OPTIONS.find(({ label }) => label === selected)?.mode;
|
|
222
|
+
if (!widgetMode) return undefined;
|
|
223
|
+
}
|
|
224
|
+
const display = DISPLAY_OPTIONS.find(({ mode }) => mode === widgetMode)?.label ?? widgetMode;
|
|
225
|
+
const confirmed = await ctx.ui.confirm(
|
|
226
|
+
"Create Pi Chat identity?",
|
|
227
|
+
`${formatIdentityLabel(nickname, candidate.publicKey)}\nDisplay: ${display}\nThis pseudonymous fingerprint is not real-world identity verification.`,
|
|
228
|
+
{ signal },
|
|
229
|
+
);
|
|
230
|
+
if (!confirmed || !isCurrent(owner, ownerGeneration) || signal.aborted) return undefined;
|
|
231
|
+
seed = candidate.seed;
|
|
232
|
+
}
|
|
233
|
+
if (nickname !== settings.nickname || seed !== settings.identitySeed || widgetMode !== settings.widgetMode) {
|
|
234
|
+
const updated = await updateChatSettings(
|
|
235
|
+
{
|
|
236
|
+
nickname,
|
|
237
|
+
identitySeed: seed,
|
|
238
|
+
...(widgetMode ? { widgetMode } : {}),
|
|
239
|
+
},
|
|
240
|
+
{ settingsPath: deps.settingsPath, signal },
|
|
241
|
+
);
|
|
242
|
+
if (!isCurrent(owner, ownerGeneration)) return undefined;
|
|
243
|
+
settings = updated;
|
|
244
|
+
}
|
|
245
|
+
return createIdentity(Buffer.from(seed, "base64url"));
|
|
246
|
+
};
|
|
279
247
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}
|
|
326
|
-
})(),
|
|
327
|
-
);
|
|
328
|
-
};
|
|
248
|
+
const startPublicRoomDirectory = (
|
|
249
|
+
room: RoomDescriptor,
|
|
250
|
+
identity: ChatIdentity,
|
|
251
|
+
session: ChatSession,
|
|
252
|
+
ctx: ExtensionContext,
|
|
253
|
+
owner: unknown,
|
|
254
|
+
ownerGeneration: number,
|
|
255
|
+
): void => {
|
|
256
|
+
if (room.kind !== "public" || !room.slug) return;
|
|
257
|
+
void trackTask(
|
|
258
|
+
(async () => {
|
|
259
|
+
let directory: PublicRoomDirectory | undefined;
|
|
260
|
+
try {
|
|
261
|
+
directory = await deps.createDirectory({
|
|
262
|
+
identity: deriveScopedIdentity(identity, `directory:#${room.slug}`),
|
|
263
|
+
advertisedSlug: room.slug,
|
|
264
|
+
});
|
|
265
|
+
if (!isCurrent(owner, ownerGeneration) || controller.signal.aborted || chatSession !== session) {
|
|
266
|
+
await directory.stop();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
roomDirectory = directory;
|
|
270
|
+
await directory.start(controller.signal);
|
|
271
|
+
} catch (error) {
|
|
272
|
+
const ownsDirectory = !directory || roomDirectory === directory;
|
|
273
|
+
if (directory && roomDirectory === directory) roomDirectory = undefined;
|
|
274
|
+
if (directory) await Promise.allSettled([directory.stop()]);
|
|
275
|
+
if (
|
|
276
|
+
ownsDirectory &&
|
|
277
|
+
isCurrent(owner, ownerGeneration) &&
|
|
278
|
+
chatSession === session &&
|
|
279
|
+
session.snapshot().room.id === room.id
|
|
280
|
+
) {
|
|
281
|
+
notifySafely(
|
|
282
|
+
ctx,
|
|
283
|
+
`Pi Chat public-room discovery is unavailable: ${sanitizeSingleLine(
|
|
284
|
+
error instanceof Error ? error.message : String(error),
|
|
285
|
+
)}. Chat remains connected.`,
|
|
286
|
+
"warning",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
})(),
|
|
291
|
+
);
|
|
292
|
+
};
|
|
329
293
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
clearUi(ctx, ownerGeneration);
|
|
429
|
-
if (!signal.aborted && isCurrent(owner, ownerGeneration)) throw error;
|
|
430
|
-
return false;
|
|
431
|
-
}
|
|
432
|
-
};
|
|
294
|
+
const joinRoomOwned = async (
|
|
295
|
+
room: RoomDescriptor,
|
|
296
|
+
ctx: ExtensionContext,
|
|
297
|
+
signal: AbortSignal,
|
|
298
|
+
options?: JoinRoomOptions,
|
|
299
|
+
): Promise<boolean> => {
|
|
300
|
+
if (chatSession && latestSnapshot?.state !== "disconnected") {
|
|
301
|
+
throw new Error("Leave the current Pi Chat room before joining another.");
|
|
302
|
+
}
|
|
303
|
+
const owner = ctx.sessionManager;
|
|
304
|
+
const ownerGeneration = generation;
|
|
305
|
+
if (!isCurrent(owner, ownerGeneration)) throw new Error("Pi Chat session is stale.");
|
|
306
|
+
let remember = options?.remember ?? false;
|
|
307
|
+
if (room.kind === "private" && options === undefined) {
|
|
308
|
+
const selected = await ctx.ui.select(
|
|
309
|
+
"Private bearer invite — restore this room after restarting Pi?",
|
|
310
|
+
[...PRIVATE_ROOM_OPTIONS],
|
|
311
|
+
{ signal },
|
|
312
|
+
);
|
|
313
|
+
if (!selected || selected === PRIVATE_ROOM_OPTIONS[2] || !isCurrent(owner, ownerGeneration) || signal.aborted) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
remember = selected === PRIVATE_ROOM_OPTIONS[0];
|
|
317
|
+
}
|
|
318
|
+
const identity = await ensureIdentity(ctx, signal, owner, ownerGeneration);
|
|
319
|
+
if (!identity || !isCurrent(owner, ownerGeneration) || signal.aborted) return false;
|
|
320
|
+
const nickname = settings.nickname;
|
|
321
|
+
if (!nickname) throw new Error("Pi Chat nickname is unavailable.");
|
|
322
|
+
const widgetModule = await loadOwnedModule(
|
|
323
|
+
deps.loadChatWidget,
|
|
324
|
+
() => isCurrent(owner, ownerGeneration) && !signal.aborted,
|
|
325
|
+
);
|
|
326
|
+
if (!widgetModule) return false;
|
|
327
|
+
let transport: ChatTransport;
|
|
328
|
+
try {
|
|
329
|
+
transport = await deps.createTransport({
|
|
330
|
+
room,
|
|
331
|
+
identity,
|
|
332
|
+
maxPeers: MAX_DIRECT_NEIGHBORS,
|
|
333
|
+
});
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (!isCurrent(owner, ownerGeneration) || signal.aborted) return false;
|
|
336
|
+
throw error;
|
|
337
|
+
}
|
|
338
|
+
if (!isCurrent(owner, ownerGeneration) || signal.aborted) {
|
|
339
|
+
await transport.stop();
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
renderChatWidget = widgetModule.renderChatWidget;
|
|
343
|
+
const session = new ChatSession({
|
|
344
|
+
room,
|
|
345
|
+
identity,
|
|
346
|
+
nickname,
|
|
347
|
+
transport,
|
|
348
|
+
onChange: (snapshot) => {
|
|
349
|
+
if (chatSession !== session || !isCurrent(owner, ownerGeneration)) return;
|
|
350
|
+
updateUi(ctx, snapshot);
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
chatSession = session;
|
|
354
|
+
ctx.ui.setStatus(CHAT_UI_KEY, "chat: joining");
|
|
355
|
+
try {
|
|
356
|
+
await session.start(AbortSignal.any([signal, controller.signal]));
|
|
357
|
+
if (!isCurrent(owner, ownerGeneration) || chatSession !== session) {
|
|
358
|
+
await session.leave();
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
if (remember) {
|
|
362
|
+
const rememberedRoom = rememberedRoomFromDescriptor(room);
|
|
363
|
+
const next = await updateChatSettings(
|
|
364
|
+
{
|
|
365
|
+
resume: {
|
|
366
|
+
rooms: [rememberedRoom],
|
|
367
|
+
activeRoomId: rememberedRoom.id,
|
|
368
|
+
surface: "chat",
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
{ settingsPath: deps.settingsPath, signal },
|
|
372
|
+
);
|
|
373
|
+
if (!isCurrent(owner, ownerGeneration) || chatSession !== session) {
|
|
374
|
+
await session.leave();
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
settings = next;
|
|
378
|
+
}
|
|
379
|
+
restoreError = undefined;
|
|
380
|
+
latestSnapshot = session.snapshot();
|
|
381
|
+
updateUi(ctx, latestSnapshot);
|
|
382
|
+
startPublicRoomDirectory(room, identity, session, ctx, owner, ownerGeneration);
|
|
383
|
+
return true;
|
|
384
|
+
} catch (error) {
|
|
385
|
+
if (chatSession === session) chatSession = undefined;
|
|
386
|
+
await session.leave();
|
|
387
|
+
clearUi(ctx, ownerGeneration);
|
|
388
|
+
if (!signal.aborted && isCurrent(owner, ownerGeneration)) throw error;
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
};
|
|
433
392
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
393
|
+
const joinRoom: typeof joinRoomOwned = async (room, ctx, signal, options) => {
|
|
394
|
+
const existing = joinOwner;
|
|
395
|
+
if (existing?.sessionManager === ctx.sessionManager && existing.generation === generation) {
|
|
396
|
+
throw new Error("A Pi Chat room join is already in progress.");
|
|
397
|
+
}
|
|
398
|
+
const token = Symbol("chat-join");
|
|
399
|
+
joinOwner = { sessionManager: ctx.sessionManager, generation, token };
|
|
400
|
+
try {
|
|
401
|
+
return await joinRoomOwned(room, ctx, signal, options);
|
|
402
|
+
} finally {
|
|
403
|
+
if (joinOwner?.token === token) joinOwner = undefined;
|
|
404
|
+
}
|
|
405
|
+
};
|
|
447
406
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
};
|
|
407
|
+
const updateRememberedSurface = async (
|
|
408
|
+
surface: "chat" | "pi",
|
|
409
|
+
ctx: ExtensionContext,
|
|
410
|
+
owner: unknown,
|
|
411
|
+
ownerGeneration: number,
|
|
412
|
+
session: ChatSession,
|
|
413
|
+
): Promise<void> => {
|
|
414
|
+
const resume = settings.resume;
|
|
415
|
+
if (!resume || resume.activeRoomId !== session.snapshot().room.id || resume.surface === surface) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
const next = await updateChatSettings(
|
|
420
|
+
{ resume: { ...resume, surface } },
|
|
421
|
+
{ settingsPath: deps.settingsPath, signal: controller.signal },
|
|
422
|
+
);
|
|
423
|
+
if (isCurrent(owner, ownerGeneration) && chatSession === session) settings = next;
|
|
424
|
+
} catch (error) {
|
|
425
|
+
if (isCurrent(owner, ownerGeneration) && chatSession === session) {
|
|
426
|
+
notifySafely(
|
|
427
|
+
ctx,
|
|
428
|
+
`Pi Chat could not save the restart view: ${sanitizeSingleLine(error instanceof Error ? error.message : String(error))}.`,
|
|
429
|
+
"error",
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
};
|
|
479
434
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
};
|
|
435
|
+
const openChat = async (ctx: ExtensionContext): Promise<void> => {
|
|
436
|
+
const session = chatSession;
|
|
437
|
+
if (!session || session.snapshot().state === "disconnected") {
|
|
438
|
+
throw new Error("Join a Pi Chat room first.");
|
|
439
|
+
}
|
|
440
|
+
const owner = ctx.sessionManager;
|
|
441
|
+
const ownerGeneration = generation;
|
|
442
|
+
const ownerSignal = controller.signal;
|
|
443
|
+
await updateRememberedSurface("chat", ctx, owner, ownerGeneration, session);
|
|
444
|
+
if (!isCurrent(owner, ownerGeneration) || chatSession !== session) return;
|
|
445
|
+
const viewModule = await loadOwnedModule(
|
|
446
|
+
deps.loadChatView,
|
|
447
|
+
() => isCurrent(owner, ownerGeneration) && chatSession === session,
|
|
448
|
+
);
|
|
449
|
+
if (!viewModule) return;
|
|
450
|
+
const { ChatView } = viewModule;
|
|
451
|
+
await ctx.ui.custom<void>(
|
|
452
|
+
(tui, theme, _keybindings, done) =>
|
|
453
|
+
new ChatView({
|
|
454
|
+
tui,
|
|
455
|
+
theme,
|
|
456
|
+
getSnapshot: () => session.snapshot(),
|
|
457
|
+
send: (text) => session.send(text),
|
|
458
|
+
initialDraft: chatDraft,
|
|
459
|
+
onDraftChange: (text) => {
|
|
460
|
+
if (isCurrent(owner, ownerGeneration) && chatSession === session) chatDraft = text;
|
|
461
|
+
},
|
|
462
|
+
setViewOpen: (open) => session.setViewOpen(open),
|
|
463
|
+
subscribe: (listener) => session.subscribe(listener),
|
|
464
|
+
signal: ownerSignal,
|
|
465
|
+
onReturnToPi: () => {
|
|
466
|
+
void trackTask(updateRememberedSurface("pi", ctx, owner, ownerGeneration, session));
|
|
467
|
+
},
|
|
468
|
+
onClose: () => done(undefined),
|
|
469
|
+
}),
|
|
470
|
+
);
|
|
471
|
+
if (isCurrent(owner, ownerGeneration) && chatSession === session) updateUi(ctx, session.snapshot());
|
|
472
|
+
};
|
|
519
473
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
474
|
+
const restoreRememberedRoom = async (
|
|
475
|
+
ctx: ExtensionContext,
|
|
476
|
+
ownerGeneration: number,
|
|
477
|
+
signal: AbortSignal = controller.signal,
|
|
478
|
+
): Promise<void> => {
|
|
479
|
+
const resume = settings.resume;
|
|
480
|
+
if (!resume || !settings.nickname || !settings.identitySeed || ctx.mode !== "tui") return;
|
|
481
|
+
const remembered = resume.rooms.find(({ id }) => id === resume.activeRoomId);
|
|
482
|
+
if (!remembered || !isCurrent(ctx.sessionManager, ownerGeneration)) return;
|
|
483
|
+
let room: RoomDescriptor;
|
|
484
|
+
try {
|
|
485
|
+
room = descriptorFromRememberedRoom(remembered);
|
|
486
|
+
} catch {
|
|
487
|
+
notifySafely(ctx, "Pi Chat could not restore the saved room; fix pi-chat.json.", "error");
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
restoreError = undefined;
|
|
491
|
+
restoringRoomId = room.id;
|
|
492
|
+
try {
|
|
493
|
+
ctx.ui.setStatus(CHAT_UI_KEY, `chat: restoring ${sanitizeSingleLine(room.label)}`);
|
|
494
|
+
const joined = await joinRoom(room, ctx, signal, { remember: false });
|
|
495
|
+
if (!joined || signal.aborted || !isCurrent(ctx.sessionManager, ownerGeneration)) return;
|
|
496
|
+
if (resume.surface === "chat") await openChat(ctx);
|
|
497
|
+
} catch (error) {
|
|
498
|
+
if (isCurrent(ctx.sessionManager, ownerGeneration)) {
|
|
499
|
+
restoreError = sanitizeSingleLine(error instanceof Error ? error.message : String(error));
|
|
500
|
+
ctx.ui.setStatus(CHAT_UI_KEY, "chat: restore failed");
|
|
501
|
+
notifySafely(
|
|
502
|
+
ctx,
|
|
503
|
+
`Pi Chat could not restore ${sanitizeSingleLine(room.label)}: ${restoreError}. Use /chat to retry or forget it.`,
|
|
504
|
+
"error",
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
} finally {
|
|
508
|
+
if (restoringRoomId === room.id) restoringRoomId = undefined;
|
|
509
|
+
}
|
|
510
|
+
};
|
|
557
511
|
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
: "not created";
|
|
669
|
-
return {
|
|
670
|
-
current,
|
|
671
|
-
next: formatIdentityLabel(settings.nickname ?? "anonymous", resetIdentity.publicKey),
|
|
672
|
-
};
|
|
673
|
-
},
|
|
674
|
-
async resetIdentity(signal) {
|
|
675
|
-
const next = await updateChatSettings(
|
|
676
|
-
{ identitySeed: resetIdentity.seed, resume: null },
|
|
677
|
-
{ settingsPath: deps.settingsPath, signal },
|
|
678
|
-
);
|
|
679
|
-
if (!ownsMenu()) return;
|
|
680
|
-
settings = next;
|
|
681
|
-
await disconnectRoom();
|
|
682
|
-
},
|
|
683
|
-
};
|
|
684
|
-
};
|
|
512
|
+
const menuSource = (ctx: ExtensionCommandContext): ChatMenuSource => {
|
|
513
|
+
const resetIdentity = createIdentity(deps.randomBytes(32));
|
|
514
|
+
const owner = ctx.sessionManager;
|
|
515
|
+
const ownerGeneration = generation;
|
|
516
|
+
const ownsMenu = () => isCurrent(owner, ownerGeneration);
|
|
517
|
+
return {
|
|
518
|
+
settingsPath: deps.settingsPath,
|
|
519
|
+
getSettings: () => settings,
|
|
520
|
+
getSnapshot: () => chatSession?.snapshot(),
|
|
521
|
+
getRestoreError: () => restoreError,
|
|
522
|
+
createPrivateRoom: () => createPrivateRoom(deps.randomBytes(32)),
|
|
523
|
+
browsePublicRooms: async (signal) => {
|
|
524
|
+
if (!ownsMenu()) throw new Error("Pi Chat session is stale.");
|
|
525
|
+
const activeDirectory = roomDirectory;
|
|
526
|
+
const directory =
|
|
527
|
+
activeDirectory ?? (await deps.createDirectory({ identity: createIdentity(deps.randomBytes(32)) }));
|
|
528
|
+
const temporary = activeDirectory ? undefined : directory;
|
|
529
|
+
const browseSignal = AbortSignal.any([signal, controller.signal]);
|
|
530
|
+
if (!ownsMenu() || browseSignal.aborted) {
|
|
531
|
+
if (temporary) await temporary.stop();
|
|
532
|
+
throw browseSignal.reason ?? new DOMException("Aborted", "AbortError");
|
|
533
|
+
}
|
|
534
|
+
try {
|
|
535
|
+
const result = await directory.browse(browseSignal);
|
|
536
|
+
if (!ownsMenu() || browseSignal.aborted) {
|
|
537
|
+
throw browseSignal.reason ?? new DOMException("Aborted", "AbortError");
|
|
538
|
+
}
|
|
539
|
+
return result;
|
|
540
|
+
} finally {
|
|
541
|
+
if (temporary) await temporary.stop();
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
joinRoom,
|
|
545
|
+
openChat: async (commandCtx) => {
|
|
546
|
+
if (ownsMenu()) await openChat(commandCtx);
|
|
547
|
+
},
|
|
548
|
+
retryRememberedRoom: async (commandCtx, signal) => {
|
|
549
|
+
if (!ownsMenu() || signal.aborted) return;
|
|
550
|
+
await restoreRememberedRoom(commandCtx, ownerGeneration, AbortSignal.any([signal, controller.signal]));
|
|
551
|
+
if (restoreError) throw new Error(restoreError);
|
|
552
|
+
},
|
|
553
|
+
forgetRememberedRoom: async (signal) => {
|
|
554
|
+
if (!ownsMenu()) return;
|
|
555
|
+
if (settings.resume) {
|
|
556
|
+
const next = await updateChatSettings({ resume: null }, { settingsPath: deps.settingsPath, signal });
|
|
557
|
+
if (!ownsMenu()) return;
|
|
558
|
+
settings = next;
|
|
559
|
+
}
|
|
560
|
+
restoreError = undefined;
|
|
561
|
+
clearUi(ctx);
|
|
562
|
+
},
|
|
563
|
+
leaveRoom: async (signal) => {
|
|
564
|
+
if (!ownsMenu()) return;
|
|
565
|
+
const currentRoomId = chatSession?.snapshot().room.id;
|
|
566
|
+
if (settings.resume && settings.resume.activeRoomId === currentRoomId) {
|
|
567
|
+
const next = await updateChatSettings({ resume: null }, { settingsPath: deps.settingsPath, signal });
|
|
568
|
+
if (!ownsMenu()) return;
|
|
569
|
+
settings = next;
|
|
570
|
+
}
|
|
571
|
+
restoreError = undefined;
|
|
572
|
+
await disconnectRoom();
|
|
573
|
+
},
|
|
574
|
+
toggleMute(publicKey) {
|
|
575
|
+
if (!ownsMenu() || !/^[0-9a-f]{64}$/u.test(publicKey)) return undefined;
|
|
576
|
+
const muted = chatSession?.toggleMute(Buffer.from(publicKey, "hex"));
|
|
577
|
+
if (chatSession) updateUi(ctx, chatSession.snapshot());
|
|
578
|
+
return muted;
|
|
579
|
+
},
|
|
580
|
+
async updateNickname(value, signal) {
|
|
581
|
+
const previous = settings;
|
|
582
|
+
const next = await updateChatSettings({ nickname: value }, { settingsPath: deps.settingsPath, signal });
|
|
583
|
+
if (!ownsMenu()) return;
|
|
584
|
+
settings = next;
|
|
585
|
+
try {
|
|
586
|
+
chatSession?.updateNickname(value);
|
|
587
|
+
} catch (error) {
|
|
588
|
+
settings = previous;
|
|
589
|
+
throw error;
|
|
590
|
+
}
|
|
591
|
+
if (chatSession) updateUi(ctx, chatSession.snapshot());
|
|
592
|
+
},
|
|
593
|
+
async updateWidgetMode(value, signal) {
|
|
594
|
+
const next = await updateChatSettings({ widgetMode: value }, { settingsPath: deps.settingsPath, signal });
|
|
595
|
+
if (!ownsMenu()) return;
|
|
596
|
+
settings = next;
|
|
597
|
+
updateUi(ctx, chatSession?.snapshot());
|
|
598
|
+
},
|
|
599
|
+
getIdentityResetPreview: () => {
|
|
600
|
+
const current = settings.identitySeed
|
|
601
|
+
? formatIdentityLabel(
|
|
602
|
+
settings.nickname ?? "anonymous",
|
|
603
|
+
createIdentity(Buffer.from(settings.identitySeed, "base64url")).publicKey,
|
|
604
|
+
)
|
|
605
|
+
: "not created";
|
|
606
|
+
return {
|
|
607
|
+
current,
|
|
608
|
+
next: formatIdentityLabel(settings.nickname ?? "anonymous", resetIdentity.publicKey),
|
|
609
|
+
};
|
|
610
|
+
},
|
|
611
|
+
async resetIdentity(signal) {
|
|
612
|
+
const next = await updateChatSettings(
|
|
613
|
+
{ identitySeed: resetIdentity.seed, resume: null },
|
|
614
|
+
{ settingsPath: deps.settingsPath, signal },
|
|
615
|
+
);
|
|
616
|
+
if (!ownsMenu()) return;
|
|
617
|
+
settings = next;
|
|
618
|
+
await disconnectRoom();
|
|
619
|
+
},
|
|
620
|
+
};
|
|
621
|
+
};
|
|
685
622
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
!controller.signal.aborted &&
|
|
748
|
-
isCurrent(owner, ownerGeneration)
|
|
749
|
-
) {
|
|
750
|
-
await openChat(ctx);
|
|
751
|
-
}
|
|
752
|
-
},
|
|
753
|
-
});
|
|
623
|
+
pi.registerCommand("chat", {
|
|
624
|
+
description: "Open peer-to-peer developer chat",
|
|
625
|
+
handler: async (args, ctx) => {
|
|
626
|
+
if (ctx.mode !== "tui") throw new Error("Pi Chat requires Pi TUI mode.");
|
|
627
|
+
const owner = ctx.sessionManager;
|
|
628
|
+
const ownerGeneration = generation;
|
|
629
|
+
if (!isCurrent(owner, ownerGeneration)) throw new Error("Pi Chat session is stale.");
|
|
630
|
+
const input = args.trim();
|
|
631
|
+
if (!input) {
|
|
632
|
+
const menuModule = await loadOwnedModule(deps.loadChatMenu, () => isCurrent(owner, ownerGeneration));
|
|
633
|
+
if (!menuModule) return;
|
|
634
|
+
const { showChatMenu } = menuModule;
|
|
635
|
+
await showChatMenu(ctx, menuSource(ctx), {
|
|
636
|
+
signal: controller.signal,
|
|
637
|
+
isCurrent: () => isCurrent(owner, ownerGeneration),
|
|
638
|
+
});
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
if (/\s/u.test(input)) {
|
|
642
|
+
notifySafely(ctx, USAGE, "warning");
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
let room: RoomDescriptor;
|
|
646
|
+
let openAfterJoin = false;
|
|
647
|
+
if (input.startsWith("pichat:")) {
|
|
648
|
+
openAfterJoin = true;
|
|
649
|
+
try {
|
|
650
|
+
room = parseInvite(input);
|
|
651
|
+
} catch {
|
|
652
|
+
notifySafely(ctx, USAGE, "warning");
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
} else if (input.startsWith("#")) {
|
|
656
|
+
openAfterJoin = true;
|
|
657
|
+
try {
|
|
658
|
+
room = createPublicRoom(input.slice(1));
|
|
659
|
+
} catch {
|
|
660
|
+
notifySafely(ctx, USAGE, "warning");
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
const confirmed = await ctx.ui.confirm(
|
|
664
|
+
"Join public room?",
|
|
665
|
+
"Anyone can join or record it. DHT and direct peers may observe network metadata.",
|
|
666
|
+
{ signal: controller.signal },
|
|
667
|
+
);
|
|
668
|
+
if (!confirmed || !isCurrent(owner, ownerGeneration)) return;
|
|
669
|
+
} else {
|
|
670
|
+
notifySafely(ctx, USAGE, "warning");
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
const joined = await joinRoom(
|
|
674
|
+
room,
|
|
675
|
+
ctx,
|
|
676
|
+
controller.signal,
|
|
677
|
+
room.kind === "public" ? { remember: true } : undefined,
|
|
678
|
+
);
|
|
679
|
+
if (joined && openAfterJoin && !controller.signal.aborted && isCurrent(owner, ownerGeneration)) {
|
|
680
|
+
await openChat(ctx);
|
|
681
|
+
}
|
|
682
|
+
},
|
|
683
|
+
});
|
|
754
684
|
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
685
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
686
|
+
controller.abort(new DOMException("Pi Chat session replaced", "AbortError"));
|
|
687
|
+
await disconnectRoom();
|
|
688
|
+
await drainOwnedTasks();
|
|
689
|
+
controller = new AbortController();
|
|
690
|
+
generation += 1;
|
|
691
|
+
activeSessionManager = ctx.sessionManager;
|
|
692
|
+
activeContext = ctx;
|
|
693
|
+
restoreError = undefined;
|
|
694
|
+
clearUi(ctx);
|
|
695
|
+
const ownerGeneration = generation;
|
|
696
|
+
const loaded = await readChatSettings(deps.settingsPath);
|
|
697
|
+
if (generation !== ownerGeneration || activeSessionManager !== ctx.sessionManager) return;
|
|
698
|
+
if (loaded.kind === "loaded") settings = loaded.settings;
|
|
699
|
+
else if (loaded.kind === "missing") settings = {};
|
|
700
|
+
else {
|
|
701
|
+
settings = {};
|
|
702
|
+
notifySafely(ctx, "Pi Chat settings are invalid; fix the file before saving.", "error");
|
|
703
|
+
}
|
|
704
|
+
if (settings.resume && ctx.mode === "tui") {
|
|
705
|
+
void trackTask(restoreRememberedRoom(ctx, ownerGeneration));
|
|
706
|
+
}
|
|
707
|
+
});
|
|
778
708
|
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
709
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
710
|
+
const owner = ctx.sessionManager;
|
|
711
|
+
if (owner !== activeSessionManager) return;
|
|
712
|
+
controller.abort(new DOMException("Pi Chat session shut down", "AbortError"));
|
|
713
|
+
const shutdownGeneration = ++generation;
|
|
714
|
+
await disconnectRoom();
|
|
715
|
+
await drainOwnedTasks();
|
|
716
|
+
await awaitChatSettingsWrites(deps.settingsPath);
|
|
717
|
+
if (activeSessionManager !== owner || generation !== shutdownGeneration) return;
|
|
718
|
+
clearUi(ctx);
|
|
719
|
+
activeSessionManager = undefined;
|
|
720
|
+
activeContext = undefined;
|
|
721
|
+
});
|
|
722
|
+
};
|
|
793
723
|
}
|
|
794
724
|
|
|
795
|
-
function notifySafely(
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
ctx.ui.notify(sanitizeSingleLine(message), level);
|
|
802
|
-
} catch {
|
|
803
|
-
// Stale UI errors must not escape lifecycle cleanup.
|
|
804
|
-
}
|
|
725
|
+
function notifySafely(ctx: ExtensionContext, message: string, level: "info" | "warning" | "error"): void {
|
|
726
|
+
try {
|
|
727
|
+
ctx.ui.notify(sanitizeSingleLine(message), level);
|
|
728
|
+
} catch {
|
|
729
|
+
// Stale UI errors must not escape lifecycle cleanup.
|
|
730
|
+
}
|
|
805
731
|
}
|
|
806
732
|
|
|
807
733
|
async function loadOwnedModule<Module>(
|
|
808
|
-
|
|
809
|
-
|
|
734
|
+
load: () => Promise<Module>,
|
|
735
|
+
isCurrent: () => boolean,
|
|
810
736
|
): Promise<Module | undefined> {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
737
|
+
try {
|
|
738
|
+
const module = await load();
|
|
739
|
+
return isCurrent() ? module : undefined;
|
|
740
|
+
} catch (error) {
|
|
741
|
+
if (!isCurrent()) return undefined;
|
|
742
|
+
throw error;
|
|
743
|
+
}
|
|
818
744
|
}
|
|
819
745
|
|
|
820
746
|
function cachedModuleLoader<Module>(load: () => Promise<Module>): () => Promise<Module> {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
747
|
+
let pending: Promise<Module> | undefined;
|
|
748
|
+
return () => {
|
|
749
|
+
if (!pending) {
|
|
750
|
+
pending = load().catch((error) => {
|
|
751
|
+
pending = undefined;
|
|
752
|
+
throw error;
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
return pending;
|
|
756
|
+
};
|
|
831
757
|
}
|
|
832
758
|
|
|
833
759
|
export default createPiChatExtension();
|