@alexsun-top/opencode-chat-room 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +17 -12
- package/README.zh-CN.md +17 -12
- package/index.ts +2 -0
- package/package.json +9 -4
- package/plugin.ts +129 -321
- package/src/center-client.ts +10 -572
- package/src/center-inbox.ts +106 -0
- package/src/center-wake.ts +118 -0
- package/src/plugin-notifications.ts +163 -0
- package/src/queue-transport.ts +129 -0
- package/src/store.ts +1 -0
- package/src/v1-plugin.ts +55 -0
- package/src/v2-plugin.ts +120 -0
package/plugin.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { tool
|
|
1
|
+
import { tool } from "@opencode-ai/plugin";
|
|
2
|
+
import type { ToolContext as V1ToolContext } from "@opencode-ai/plugin";
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
4
|
import { z } from "zod";
|
|
3
|
-
import { promises as fs } from "node:fs";
|
|
4
|
-
import path from "node:path";
|
|
5
5
|
import RoomService, { RoomError, type Message } from "./src/room-service";
|
|
6
6
|
import {
|
|
7
7
|
enqueue,
|
|
8
|
-
formatNotificationLines,
|
|
9
8
|
formatRoom,
|
|
10
9
|
identity,
|
|
11
10
|
loadRegistry,
|
|
@@ -14,43 +13,38 @@ import {
|
|
|
14
13
|
saveRegistry,
|
|
15
14
|
saveRooms,
|
|
16
15
|
senderName,
|
|
17
|
-
statePath,
|
|
18
16
|
timeOf,
|
|
19
17
|
type Registry,
|
|
20
18
|
type RegistryEntry,
|
|
21
19
|
} from "./src/store";
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
sendQueue,
|
|
27
|
-
wakeSessions,
|
|
28
|
-
type QueueTransport,
|
|
29
|
-
} from "./src/center-client";
|
|
20
|
+
import { centerAction } from "./src/center-client";
|
|
21
|
+
import { createQueueTransport, type QueueTransport } from "./src/queue-transport";
|
|
22
|
+
import { checkCenterInbox } from "./src/center-inbox";
|
|
23
|
+
import { wakeSessions } from "./src/center-wake";
|
|
30
24
|
import {
|
|
31
25
|
clearPendingAdmission,
|
|
32
|
-
getOrCreatePendingAdmission,
|
|
33
|
-
markPendingAdmissionAdmitted,
|
|
34
|
-
pendingAdmission,
|
|
35
|
-
rejectPendingAdmission,
|
|
36
26
|
} from "./src/queue";
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
let selfDirectory = "";
|
|
41
|
-
let queueTransport: QueueTransport = createQueueTransport();
|
|
42
|
-
|
|
43
|
-
// ponytail: central-mode in-process wake timer (see server() below)
|
|
44
|
-
let wakeTimer: ReturnType<typeof setInterval> | null = null;
|
|
27
|
+
import { createNotifier } from "./src/plugin-notifications";
|
|
28
|
+
import { createV2Setup, type V2Runtime } from "./src/v2-plugin";
|
|
29
|
+
import { createV1Server } from "./src/v1-plugin";
|
|
45
30
|
|
|
46
31
|
// ponytail: CHAT_ROOM_SERVER_URL switches to central mode (all state on a
|
|
47
32
|
// central server); unset = standalone local-file mode
|
|
48
33
|
const centerUrl = process.env.CHAT_ROOM_SERVER_URL ?? "";
|
|
49
34
|
const centerToken = process.env.CHAT_ROOM_SERVER_TOKEN ?? "";
|
|
50
|
-
|
|
51
|
-
// ponytail: optional常驻 opencode serve 地址——配置了才能对已退出 session
|
|
52
|
-
// 做进程内唤起;未配置则不唤起(用户要求)
|
|
35
|
+
// A configured OpenCode serve URL is required to wake exited sessions.
|
|
53
36
|
const serveWakeUrl = process.env.CHAT_ROOM_WAKE_SERVE_URL ?? "";
|
|
37
|
+
type PluginRuntime = V2Runtime;
|
|
38
|
+
|
|
39
|
+
const runtimeContext = new AsyncLocalStorage<PluginRuntime>();
|
|
40
|
+
const defaultRuntime: PluginRuntime = {
|
|
41
|
+
selfServerUrl: "",
|
|
42
|
+
selfDirectory: "",
|
|
43
|
+
protocol: "v1",
|
|
44
|
+
transport: createQueueTransport(),
|
|
45
|
+
center: { center: centerUrl, token: centerToken, directory: "" },
|
|
46
|
+
};
|
|
47
|
+
const currentRuntime = (): PluginRuntime => runtimeContext.getStore() ?? defaultRuntime;
|
|
54
48
|
|
|
55
49
|
async function withRooms<T>(
|
|
56
50
|
fn: (svc: RoomService) => T,
|
|
@@ -79,6 +73,7 @@ async function registerSelf(
|
|
|
79
73
|
sessionID: string,
|
|
80
74
|
name = identity(),
|
|
81
75
|
): Promise<void> {
|
|
76
|
+
const runtime = currentRuntime();
|
|
82
77
|
return enqueue(async () => {
|
|
83
78
|
try {
|
|
84
79
|
const reg = await loadRegistry();
|
|
@@ -88,10 +83,11 @@ async function registerSelf(
|
|
|
88
83
|
const regRoom = (reg[roomId] =
|
|
89
84
|
reg[roomId] ?? Object.create(null) as Record<string, RegistryEntry>);
|
|
90
85
|
regRoom[sessionID] = {
|
|
91
|
-
serverUrl: selfServerUrl,
|
|
92
|
-
apiPrefix: "/api",
|
|
86
|
+
serverUrl: runtime.selfServerUrl,
|
|
87
|
+
apiPrefix: runtime.protocol === "v1" ? "" : "/api",
|
|
88
|
+
protocol: runtime.protocol,
|
|
93
89
|
name,
|
|
94
|
-
directory: selfDirectory || undefined,
|
|
90
|
+
directory: runtime.selfDirectory || undefined,
|
|
95
91
|
// ponytail: re-join must not rewind the read watermark (unread
|
|
96
92
|
// messages before the re-join would be silently dropped); a first
|
|
97
93
|
// join starts reading at join time
|
|
@@ -107,6 +103,27 @@ async function registerSelf(
|
|
|
107
103
|
});
|
|
108
104
|
}
|
|
109
105
|
|
|
106
|
+
async function refreshSelf(runtime: PluginRuntime, sessionID: string): Promise<void> {
|
|
107
|
+
await enqueue(async () => {
|
|
108
|
+
try {
|
|
109
|
+
const registry = await loadRegistry();
|
|
110
|
+
let changed = false;
|
|
111
|
+
for (const members of Object.values(registry)) {
|
|
112
|
+
const entry = members[sessionID];
|
|
113
|
+
if (!entry) continue;
|
|
114
|
+
entry.serverUrl = runtime.selfServerUrl;
|
|
115
|
+
entry.apiPrefix = runtime.protocol === "v1" ? "" : "/api";
|
|
116
|
+
entry.protocol = runtime.protocol;
|
|
117
|
+
entry.directory = runtime.selfDirectory || undefined;
|
|
118
|
+
changed = true;
|
|
119
|
+
}
|
|
120
|
+
if (changed) await saveRegistry(registry);
|
|
121
|
+
} catch {
|
|
122
|
+
// Existing watermarks and identities remain untouched if refresh fails.
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
110
127
|
async function unregisterSelf(
|
|
111
128
|
roomId: string,
|
|
112
129
|
sessionID: string,
|
|
@@ -127,28 +144,6 @@ async function unregisterSelf(
|
|
|
127
144
|
});
|
|
128
145
|
}
|
|
129
146
|
|
|
130
|
-
function notifyPath(): string {
|
|
131
|
-
return path.join(path.dirname(statePath()), "notify.log");
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async function logNotify(
|
|
135
|
-
roomId: string,
|
|
136
|
-
results: ReadonlyArray<PromiseSettledResult<{ url: string; status: number }>>,
|
|
137
|
-
): Promise<void> {
|
|
138
|
-
const lines = results.map((r) => {
|
|
139
|
-
const head = `[${timeOf(Date.now())}] room=${roomId}`;
|
|
140
|
-
if (r.status === "rejected") {
|
|
141
|
-
return `${head} FAIL ${r.reason instanceof Error ? r.reason.message : String(r.reason)}`;
|
|
142
|
-
}
|
|
143
|
-
return r.value.status >= 200 && r.value.status < 300
|
|
144
|
-
? `${head} OK ${r.value.status} ${r.value.url}`
|
|
145
|
-
: `${head} FAIL ${r.value.status} ${r.value.url}`;
|
|
146
|
-
});
|
|
147
|
-
await fs
|
|
148
|
-
.appendFile(notifyPath(), lines.join("\n") + "\n", "utf8")
|
|
149
|
-
.catch(() => {});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
147
|
// ponytail: push is best-effort; poll remains the fallback; log every attempt for debugging
|
|
153
148
|
async function bumpLastRead(
|
|
154
149
|
roomId: string,
|
|
@@ -196,232 +191,60 @@ async function bumpLastQueued(
|
|
|
196
191
|
});
|
|
197
192
|
}
|
|
198
193
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
roomName: string;
|
|
205
|
-
messages: Message[];
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
type NotifyAttempt = { url: string; status: number };
|
|
209
|
-
|
|
210
|
-
type NotifyTargetQueue = {
|
|
211
|
-
latest: NotifyBatch | undefined;
|
|
212
|
-
latestTs: number;
|
|
213
|
-
running: Promise<NotifyAttempt[]>;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const notifyQueues = new Map<string, NotifyTargetQueue>();
|
|
217
|
-
|
|
218
|
-
function notifyBatchMaxTs(batch: NotifyBatch): number {
|
|
219
|
-
return batch.messages.reduce(
|
|
220
|
-
(maxTs, message) => Math.max(maxTs, message.createdAt),
|
|
221
|
-
0,
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async function notifyTarget(
|
|
226
|
-
sessionID: string,
|
|
227
|
-
batch: NotifyBatch,
|
|
228
|
-
): Promise<NotifyAttempt | undefined> {
|
|
229
|
-
const reg = await loadRegistry();
|
|
230
|
-
const entry = reg[batch.roomId]?.[sessionID];
|
|
231
|
-
if (!entry?.serverUrl) {
|
|
232
|
-
return undefined;
|
|
233
|
-
}
|
|
234
|
-
const unread = batch.messages.filter(
|
|
235
|
-
(m) =>
|
|
236
|
-
m.createdAt >
|
|
237
|
-
Math.max(entry.lastReadTs ?? 0, entry.lastQueuedTs ?? 0) &&
|
|
238
|
-
(m.senderSessionID !== undefined
|
|
239
|
-
? m.senderSessionID !== sessionID
|
|
240
|
-
: m.senderId !== entry.name),
|
|
241
|
-
);
|
|
242
|
-
const currentMaxTs = unread.reduce(
|
|
243
|
-
(mx, message) => Math.max(mx, message.createdAt),
|
|
244
|
-
0,
|
|
245
|
-
);
|
|
246
|
-
const existing = pendingAdmission(sessionID, batch.roomId);
|
|
247
|
-
const deltaItems =
|
|
248
|
-
existing?.state === "admitted" && currentMaxTs > existing.maxTs
|
|
249
|
-
? unread.filter((message) => message.createdAt > existing.maxTs)
|
|
250
|
-
: unread;
|
|
251
|
-
const base = new URL(entry.serverUrl);
|
|
252
|
-
const prefix = (entry.apiPrefix || "/api").replace(/^\/+|\/+$/g, "");
|
|
253
|
-
const url = `${base.origin}/${prefix}/session/${sessionID}/prompt`;
|
|
254
|
-
if (
|
|
255
|
-
existing?.state === "admitted" &&
|
|
256
|
-
(currentMaxTs <= existing.maxTs || deltaItems.length === 0)
|
|
257
|
-
) {
|
|
258
|
-
if (existing.maxTs > 0) {
|
|
259
|
-
await bumpLastQueued(batch.roomId, sessionID, existing.maxTs);
|
|
260
|
-
}
|
|
261
|
-
return { url, status: 200 };
|
|
262
|
-
}
|
|
263
|
-
const { lines, capped } = formatNotificationLines(deltaItems);
|
|
264
|
-
const prompt =
|
|
265
|
-
deltaItems.length > 0
|
|
266
|
-
? `<notification>\nNew messages in room ${batch.roomName}:\n${lines.join("\n")}\n${NOTIFY_INSTRUCTION}\n</notification>`
|
|
267
|
-
: `<notification>\n${NOTIFY_INSTRUCTION}\n</notification>`;
|
|
268
|
-
const maxTs = deltaItems.reduce(
|
|
269
|
-
(mx, message) => Math.max(mx, message.createdAt),
|
|
270
|
-
0,
|
|
271
|
-
);
|
|
272
|
-
const admissionItems =
|
|
273
|
-
deltaItems.length > 0 ? deltaItems : batch.messages.slice(-1);
|
|
274
|
-
const admission = getOrCreatePendingAdmission(
|
|
275
|
-
sessionID,
|
|
276
|
-
batch.roomId,
|
|
277
|
-
admissionItems,
|
|
278
|
-
prompt,
|
|
279
|
-
maxTs,
|
|
280
|
-
capped,
|
|
281
|
-
);
|
|
282
|
-
if (admission.state === "admitted" && maxTs <= admission.maxTs) {
|
|
283
|
-
if (admission.maxTs <= 0) {
|
|
284
|
-
return { url, status: 200 };
|
|
285
|
-
}
|
|
286
|
-
await bumpLastQueued(batch.roomId, sessionID, admission.maxTs);
|
|
287
|
-
return { url, status: 200 };
|
|
288
|
-
}
|
|
289
|
-
const result = await sendQueue(
|
|
290
|
-
{
|
|
291
|
-
...queueTransport,
|
|
292
|
-
serverUrl: entry.serverUrl,
|
|
293
|
-
directory: entry.directory ?? queueTransport.directory,
|
|
294
|
-
},
|
|
295
|
-
sessionID,
|
|
296
|
-
admission.prompt,
|
|
297
|
-
admission.id,
|
|
298
|
-
);
|
|
299
|
-
if (result !== "delivered") {
|
|
300
|
-
if (result === "rejected") {
|
|
301
|
-
rejectPendingAdmission(sessionID, batch.roomId, admission.id);
|
|
302
|
-
}
|
|
303
|
-
return { url, status: result === "unreachable" ? 0 : 400 };
|
|
304
|
-
}
|
|
305
|
-
markPendingAdmissionAdmitted(sessionID, batch.roomId, admission.id);
|
|
306
|
-
if (admission.maxTs > 0) {
|
|
307
|
-
await bumpLastQueued(batch.roomId, sessionID, admission.maxTs);
|
|
308
|
-
}
|
|
309
|
-
return { url, status: 200 };
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function queueNotifyTarget(
|
|
313
|
-
sessionID: string,
|
|
314
|
-
batch: NotifyBatch,
|
|
315
|
-
): Promise<NotifyAttempt[]> {
|
|
316
|
-
const key = JSON.stringify([batch.roomId, sessionID]);
|
|
317
|
-
const existing = notifyQueues.get(key);
|
|
318
|
-
if (existing) {
|
|
319
|
-
const maxTs = notifyBatchMaxTs(batch);
|
|
320
|
-
if (maxTs > existing.latestTs) {
|
|
321
|
-
existing.latest = batch;
|
|
322
|
-
existing.latestTs = maxTs;
|
|
323
|
-
}
|
|
324
|
-
return existing.running;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
const state: NotifyTargetQueue = {
|
|
328
|
-
latest: batch,
|
|
329
|
-
latestTs: notifyBatchMaxTs(batch),
|
|
330
|
-
running: Promise.resolve([] as NotifyAttempt[]),
|
|
331
|
-
};
|
|
332
|
-
const running = (async (): Promise<NotifyAttempt[]> => {
|
|
333
|
-
const attempts: NotifyAttempt[] = [];
|
|
334
|
-
let failed = false;
|
|
335
|
-
let firstError: unknown;
|
|
336
|
-
while (state.latest) {
|
|
337
|
-
const current = state.latest;
|
|
338
|
-
state.latest = undefined;
|
|
339
|
-
try {
|
|
340
|
-
const attempt = await notifyTarget(sessionID, current);
|
|
341
|
-
if (attempt) {
|
|
342
|
-
attempts.push(attempt);
|
|
343
|
-
}
|
|
344
|
-
} catch (error) {
|
|
345
|
-
if (!failed) {
|
|
346
|
-
firstError = error;
|
|
347
|
-
failed = true;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
if (failed) {
|
|
352
|
-
throw firstError;
|
|
353
|
-
}
|
|
354
|
-
return attempts;
|
|
355
|
-
})();
|
|
356
|
-
state.running = running;
|
|
357
|
-
notifyQueues.set(key, state);
|
|
358
|
-
const cleanup = () => {
|
|
359
|
-
if (notifyQueues.get(key) === state) {
|
|
360
|
-
notifyQueues.delete(key);
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
void running.then(cleanup, cleanup);
|
|
364
|
-
return running;
|
|
365
|
-
}
|
|
194
|
+
const notifier = createNotifier({
|
|
195
|
+
transport: () => currentRuntime().transport,
|
|
196
|
+
bumpQueued: (roomId, sessionID, ts) => bumpLastQueued(roomId, sessionID, ts),
|
|
197
|
+
});
|
|
198
|
+
const { notifyRoom, notifyTarget } = notifier;
|
|
366
199
|
|
|
367
|
-
async function
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
selfSessionID: string,
|
|
372
|
-
messages: Message[],
|
|
200
|
+
export async function pollKnownSessions(
|
|
201
|
+
sessions: ReadonlySet<string>,
|
|
202
|
+
transport: QueueTransport,
|
|
203
|
+
opts: PluginRuntime["center"],
|
|
373
204
|
): Promise<void> {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const results = await Promise.allSettled(
|
|
380
|
-
targets.map(([sessionID]) => queueNotifyTarget(sessionID, batch)),
|
|
381
|
-
);
|
|
382
|
-
const logResults: PromiseSettledResult<NotifyAttempt>[] = [];
|
|
383
|
-
let failed = false;
|
|
384
|
-
let firstError: unknown;
|
|
385
|
-
for (const result of results) {
|
|
386
|
-
if (result.status === "fulfilled") {
|
|
387
|
-
for (const attempt of result.value) {
|
|
388
|
-
logResults.push({ status: "fulfilled", value: attempt });
|
|
389
|
-
}
|
|
390
|
-
} else {
|
|
391
|
-
logResults.push({ status: "rejected", reason: result.reason });
|
|
392
|
-
if (!failed) {
|
|
393
|
-
firstError = result.reason;
|
|
394
|
-
failed = true;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
205
|
+
if (opts.center) {
|
|
206
|
+
await Promise.all([...sessions].map((sessionID) =>
|
|
207
|
+
checkCenterInbox(opts, sessionID, transport),
|
|
208
|
+
));
|
|
209
|
+
return;
|
|
397
210
|
}
|
|
398
|
-
await
|
|
399
|
-
|
|
400
|
-
|
|
211
|
+
const [rooms, registry] = await Promise.all([loadRooms(), loadRegistry()]);
|
|
212
|
+
for (const sessionID of sessions) {
|
|
213
|
+
for (const room of rooms) {
|
|
214
|
+
if (!registry[room.id]?.[sessionID]) continue;
|
|
215
|
+
await notifyTarget(sessionID, {
|
|
216
|
+
roomId: room.id,
|
|
217
|
+
roomName: room.name,
|
|
218
|
+
messages: room.messages,
|
|
219
|
+
}, transport);
|
|
220
|
+
}
|
|
401
221
|
}
|
|
402
222
|
}
|
|
403
223
|
|
|
404
224
|
const READ_ONLY_ACTIONS = new Set(["list", "members", "poll"]);
|
|
405
225
|
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
226
|
+
const roomArgs = z.object({
|
|
227
|
+
action: z.enum(["create", "join", "leave", "list", "send", "poll", "members"]),
|
|
228
|
+
name: z.string().max(64).optional(),
|
|
229
|
+
roomId: z.string().optional(),
|
|
230
|
+
text: z.string().max(2000).optional(),
|
|
231
|
+
});
|
|
232
|
+
type RoomArguments = z.infer<typeof roomArgs>;
|
|
233
|
+
|
|
234
|
+
const roomExecute = async (
|
|
235
|
+
args: RoomArguments,
|
|
236
|
+
context: Pick<V1ToolContext, "sessionID">,
|
|
237
|
+
) => {
|
|
416
238
|
try {
|
|
417
|
-
|
|
239
|
+
const runtime = currentRuntime();
|
|
240
|
+
if (runtime.center.center) {
|
|
418
241
|
// ponytail: central mode — self-pull inbox first, then act via HTTP
|
|
419
242
|
// (skipped for poll: poll itself pulls the inbox, so racing the
|
|
420
243
|
// fire-and-forget check would read the position it just advanced)
|
|
421
244
|
if (args.action !== "poll") {
|
|
422
|
-
void checkCenterInbox(
|
|
245
|
+
void checkCenterInbox(runtime.center, context.sessionID, runtime.transport);
|
|
423
246
|
}
|
|
424
|
-
return await centerAction(
|
|
247
|
+
return await centerAction(runtime.center, args, context.sessionID);
|
|
425
248
|
}
|
|
426
249
|
// ponytail: act as the identity this session registered with on join
|
|
427
250
|
let selfName = identity();
|
|
@@ -436,7 +259,7 @@ const room = tool({
|
|
|
436
259
|
selfName = reg[args.roomId]?.[context.sessionID]?.name ?? selfName;
|
|
437
260
|
}
|
|
438
261
|
if (args.action === "leave") {
|
|
439
|
-
//
|
|
262
|
+
// Keep the participant while another session still uses this name.
|
|
440
263
|
leaveNameShared = Object.entries(reg[args.roomId] ?? {}).some(
|
|
441
264
|
([sid, e]) => sid !== context.sessionID && e.name === selfName,
|
|
442
265
|
);
|
|
@@ -514,7 +337,7 @@ const room = tool({
|
|
|
514
337
|
// ponytail: ora-2 M2 — mirror the join guard: if another session
|
|
515
338
|
// still holds this name, remove only the registry entry, not the
|
|
516
339
|
// participant (or the sibling window breaks with NOT_JOINED);
|
|
517
|
-
//
|
|
340
|
+
// Do not emit a false leave event while the participant remains.
|
|
518
341
|
if (!leaveNameShared) {
|
|
519
342
|
svc.leaveRoom(args.roomId, selfName);
|
|
520
343
|
const room = svc.getRoom(args.roomId);
|
|
@@ -623,60 +446,45 @@ const room = tool({
|
|
|
623
446
|
} catch (err) {
|
|
624
447
|
return `error: ${err instanceof Error ? err.message : String(err)}`;
|
|
625
448
|
}
|
|
626
|
-
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const room = tool({
|
|
452
|
+
description: "Chat room operations: create, join, leave and list rooms; send and poll messages; list members",
|
|
453
|
+
args: roomArgs.shape,
|
|
454
|
+
execute: roomExecute,
|
|
627
455
|
});
|
|
628
456
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
}
|
|
637
|
-
|
|
457
|
+
export const server = createV1Server({
|
|
458
|
+
runtime: (input, transport) => ({
|
|
459
|
+
protocol: "v1",
|
|
460
|
+
selfServerUrl: transport.serverUrl ?? "",
|
|
461
|
+
selfDirectory: input.directory,
|
|
462
|
+
transport,
|
|
463
|
+
center: { center: centerUrl, token: centerToken, directory: input.directory },
|
|
464
|
+
}),
|
|
465
|
+
run: (runtime, callback) => runtimeContext.run(runtime, callback),
|
|
466
|
+
room,
|
|
467
|
+
observe: refreshSelf,
|
|
468
|
+
checkInbox: (runtime, sessionID) => checkCenterInbox(runtime.center, sessionID, runtime.transport),
|
|
469
|
+
wake: (runtime, sessions) => wakeSessions(runtime.center, runtime.transport, serveWakeUrl, sessions),
|
|
470
|
+
central: Boolean(centerUrl),
|
|
471
|
+
});
|
|
638
472
|
|
|
639
|
-
export
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
void wakeSessions(centerOpts, queueTransport, serveWakeUrl);
|
|
658
|
-
}, 10000);
|
|
659
|
-
}
|
|
660
|
-
return {
|
|
661
|
-
tool: { room },
|
|
662
|
-
config,
|
|
663
|
-
// ponytail: central mode has no cross-host push, so each session pulls
|
|
664
|
-
// its inbox on chat messages and self-pushes queue notifications
|
|
665
|
-
// (SDK signature: (input, output) — sessionID is on the first arg)
|
|
666
|
-
...(centerUrl
|
|
667
|
-
? {
|
|
668
|
-
"chat.message": async (
|
|
669
|
-
input: { sessionID: string },
|
|
670
|
-
_output: unknown,
|
|
671
|
-
) => {
|
|
672
|
-
void checkCenterInbox(
|
|
673
|
-
centerOpts,
|
|
674
|
-
input.sessionID,
|
|
675
|
-
queueTransport,
|
|
676
|
-
);
|
|
677
|
-
},
|
|
678
|
-
}
|
|
679
|
-
: {}),
|
|
680
|
-
};
|
|
681
|
-
},
|
|
682
|
-
};
|
|
473
|
+
export const setup = createV2Setup({
|
|
474
|
+
runtime: (directory, transport) => ({
|
|
475
|
+
protocol: "v2",
|
|
476
|
+
selfServerUrl: "",
|
|
477
|
+
selfDirectory: directory,
|
|
478
|
+
transport,
|
|
479
|
+
center: { center: centerUrl, token: centerToken, directory },
|
|
480
|
+
}),
|
|
481
|
+
run: (runtime, callback) => runtimeContext.run(runtime, callback),
|
|
482
|
+
execute: (args, sessionID) => roomExecute(args, { sessionID }),
|
|
483
|
+
observe: refreshSelf,
|
|
484
|
+
poll: pollKnownSessions,
|
|
485
|
+
wake: (center, transport, sessions) => wakeSessions(center, transport, serveWakeUrl, sessions),
|
|
486
|
+
central: Boolean(centerUrl),
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
export const id = "chat-room";
|
|
490
|
+
export default { id, server, setup };
|