@alexsun-top/opencode-chat-room 0.1.2 → 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 +147 -301
- package/server/chat-server.ts +128 -41
- package/src/center-client.ts +24 -528
- 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/queue.ts +14 -9
- package/src/store.ts +2 -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,42 +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
|
-
rejectPendingAdmission,
|
|
35
26
|
} from "./src/queue";
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let selfDirectory = "";
|
|
40
|
-
let queueTransport: QueueTransport = createQueueTransport();
|
|
41
|
-
|
|
42
|
-
// ponytail: central-mode in-process wake timer (see server() below)
|
|
43
|
-
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";
|
|
44
30
|
|
|
45
31
|
// ponytail: CHAT_ROOM_SERVER_URL switches to central mode (all state on a
|
|
46
32
|
// central server); unset = standalone local-file mode
|
|
47
33
|
const centerUrl = process.env.CHAT_ROOM_SERVER_URL ?? "";
|
|
48
34
|
const centerToken = process.env.CHAT_ROOM_SERVER_TOKEN ?? "";
|
|
49
|
-
|
|
50
|
-
// ponytail: optional常驻 opencode serve 地址——配置了才能对已退出 session
|
|
51
|
-
// 做进程内唤起;未配置则不唤起(用户要求)
|
|
35
|
+
// A configured OpenCode serve URL is required to wake exited sessions.
|
|
52
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;
|
|
53
48
|
|
|
54
49
|
async function withRooms<T>(
|
|
55
50
|
fn: (svc: RoomService) => T,
|
|
@@ -78,6 +73,7 @@ async function registerSelf(
|
|
|
78
73
|
sessionID: string,
|
|
79
74
|
name = identity(),
|
|
80
75
|
): Promise<void> {
|
|
76
|
+
const runtime = currentRuntime();
|
|
81
77
|
return enqueue(async () => {
|
|
82
78
|
try {
|
|
83
79
|
const reg = await loadRegistry();
|
|
@@ -87,16 +83,18 @@ async function registerSelf(
|
|
|
87
83
|
const regRoom = (reg[roomId] =
|
|
88
84
|
reg[roomId] ?? Object.create(null) as Record<string, RegistryEntry>);
|
|
89
85
|
regRoom[sessionID] = {
|
|
90
|
-
serverUrl: selfServerUrl,
|
|
91
|
-
apiPrefix: "/api",
|
|
86
|
+
serverUrl: runtime.selfServerUrl,
|
|
87
|
+
apiPrefix: runtime.protocol === "v1" ? "" : "/api",
|
|
88
|
+
protocol: runtime.protocol,
|
|
92
89
|
name,
|
|
93
|
-
directory: selfDirectory || undefined,
|
|
90
|
+
directory: runtime.selfDirectory || undefined,
|
|
94
91
|
// ponytail: re-join must not rewind the read watermark (unread
|
|
95
92
|
// messages before the re-join would be silently dropped); a first
|
|
96
93
|
// join starts reading at join time
|
|
97
94
|
lastReadTs:
|
|
98
95
|
regRoom[sessionID]?.lastReadTs ??
|
|
99
96
|
Math.max(Date.now(), latestMessageTs),
|
|
97
|
+
lastQueuedTs: regRoom[sessionID]?.lastQueuedTs,
|
|
100
98
|
};
|
|
101
99
|
await saveRegistry(reg);
|
|
102
100
|
} catch {
|
|
@@ -105,6 +103,27 @@ async function registerSelf(
|
|
|
105
103
|
});
|
|
106
104
|
}
|
|
107
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
|
+
|
|
108
127
|
async function unregisterSelf(
|
|
109
128
|
roomId: string,
|
|
110
129
|
sessionID: string,
|
|
@@ -125,28 +144,6 @@ async function unregisterSelf(
|
|
|
125
144
|
});
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
function notifyPath(): string {
|
|
129
|
-
return path.join(path.dirname(statePath()), "notify.log");
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async function logNotify(
|
|
133
|
-
roomId: string,
|
|
134
|
-
results: ReadonlyArray<PromiseSettledResult<{ url: string; status: number }>>,
|
|
135
|
-
): Promise<void> {
|
|
136
|
-
const lines = results.map((r) => {
|
|
137
|
-
const head = `[${timeOf(Date.now())}] room=${roomId}`;
|
|
138
|
-
if (r.status === "rejected") {
|
|
139
|
-
return `${head} FAIL ${r.reason instanceof Error ? r.reason.message : String(r.reason)}`;
|
|
140
|
-
}
|
|
141
|
-
return r.value.status >= 200 && r.value.status < 300
|
|
142
|
-
? `${head} OK ${r.value.status} ${r.value.url}`
|
|
143
|
-
: `${head} FAIL ${r.value.status} ${r.value.url}`;
|
|
144
|
-
});
|
|
145
|
-
await fs
|
|
146
|
-
.appendFile(notifyPath(), lines.join("\n") + "\n", "utf8")
|
|
147
|
-
.catch(() => {});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
147
|
// ponytail: push is best-effort; poll remains the fallback; log every attempt for debugging
|
|
151
148
|
async function bumpLastRead(
|
|
152
149
|
roomId: string,
|
|
@@ -172,218 +169,82 @@ async function bumpLastRead(
|
|
|
172
169
|
});
|
|
173
170
|
}
|
|
174
171
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// whose earlier push failed. Event-only pushes (empty messages) don't bump.
|
|
178
|
-
type NotifyBatch = {
|
|
179
|
-
roomId: string;
|
|
180
|
-
roomName: string;
|
|
181
|
-
messages: Message[];
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
type NotifyAttempt = { url: string; status: number };
|
|
185
|
-
|
|
186
|
-
type NotifyTargetQueue = {
|
|
187
|
-
latest: NotifyBatch | undefined;
|
|
188
|
-
latestTs: number;
|
|
189
|
-
running: Promise<NotifyAttempt[]>;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const notifyQueues = new Map<string, NotifyTargetQueue>();
|
|
193
|
-
|
|
194
|
-
function notifyBatchMaxTs(batch: NotifyBatch): number {
|
|
195
|
-
return batch.messages.reduce(
|
|
196
|
-
(maxTs, message) => Math.max(maxTs, message.createdAt),
|
|
197
|
-
0,
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async function notifyTarget(
|
|
172
|
+
async function bumpLastQueued(
|
|
173
|
+
roomId: string,
|
|
202
174
|
sessionID: string,
|
|
203
|
-
|
|
204
|
-
): Promise<
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
? m.senderSessionID !== sessionID
|
|
215
|
-
: m.senderId !== entry.name),
|
|
216
|
-
);
|
|
217
|
-
const { lines, capped } = formatNotificationLines(unread);
|
|
218
|
-
const prompt =
|
|
219
|
-
unread.length > 0
|
|
220
|
-
? `<notification>\nNew messages in room ${batch.roomName}:\n${lines.join("\n")}\n${NOTIFY_INSTRUCTION}\n</notification>`
|
|
221
|
-
: `<notification>\n${NOTIFY_INSTRUCTION}\n</notification>`;
|
|
222
|
-
const maxTs = unread.reduce(
|
|
223
|
-
(mx, message) => Math.max(mx, message.createdAt),
|
|
224
|
-
0,
|
|
225
|
-
);
|
|
226
|
-
const admissionItems = unread.length > 0 ? unread : batch.messages.slice(-1);
|
|
227
|
-
const admission = getOrCreatePendingAdmission(
|
|
228
|
-
sessionID,
|
|
229
|
-
batch.roomId,
|
|
230
|
-
admissionItems,
|
|
231
|
-
prompt,
|
|
232
|
-
maxTs,
|
|
233
|
-
capped,
|
|
234
|
-
);
|
|
235
|
-
const base = new URL(entry.serverUrl);
|
|
236
|
-
const prefix = (entry.apiPrefix || "/api").replace(/^\/+|\/+$/g, "");
|
|
237
|
-
const url = `${base.origin}/${prefix}/session/${sessionID}/prompt`;
|
|
238
|
-
if (admission.state === "admitted") {
|
|
239
|
-
if (admission.capped) {
|
|
240
|
-
if (maxTs <= admission.maxTs) {
|
|
241
|
-
return { url, status: 200 };
|
|
242
|
-
}
|
|
243
|
-
} else {
|
|
244
|
-
if (admission.maxTs <= 0) {
|
|
245
|
-
return { url, status: 200 };
|
|
175
|
+
ts: number,
|
|
176
|
+
): Promise<boolean> {
|
|
177
|
+
return enqueue(async () => {
|
|
178
|
+
try {
|
|
179
|
+
const reg = await loadRegistry();
|
|
180
|
+
const entry = reg[roomId]?.[sessionID];
|
|
181
|
+
if (entry) {
|
|
182
|
+
entry.lastQueuedTs = Math.max(entry.lastQueuedTs ?? 0, ts);
|
|
183
|
+
await saveRegistry(reg);
|
|
184
|
+
clearPendingAdmission(sessionID, roomId, ts);
|
|
185
|
+
return true;
|
|
246
186
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
const result = await sendQueue(
|
|
252
|
-
{
|
|
253
|
-
...queueTransport,
|
|
254
|
-
serverUrl: entry.serverUrl,
|
|
255
|
-
directory: entry.directory ?? queueTransport.directory,
|
|
256
|
-
},
|
|
257
|
-
sessionID,
|
|
258
|
-
admission.prompt,
|
|
259
|
-
admission.id,
|
|
260
|
-
);
|
|
261
|
-
if (result !== "delivered") {
|
|
262
|
-
if (result === "rejected") {
|
|
263
|
-
rejectPendingAdmission(sessionID, batch.roomId, admission.id);
|
|
187
|
+
return false;
|
|
188
|
+
} catch {
|
|
189
|
+
return false;
|
|
264
190
|
}
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
markPendingAdmissionAdmitted(sessionID, batch.roomId, admission.id);
|
|
268
|
-
if (admission.maxTs > 0 && !admission.capped) {
|
|
269
|
-
await bumpLastRead(batch.roomId, sessionID, admission.maxTs);
|
|
270
|
-
}
|
|
271
|
-
return { url, status: 200 };
|
|
191
|
+
});
|
|
272
192
|
}
|
|
273
193
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
)
|
|
278
|
-
|
|
279
|
-
const existing = notifyQueues.get(key);
|
|
280
|
-
if (existing) {
|
|
281
|
-
const maxTs = notifyBatchMaxTs(batch);
|
|
282
|
-
if (maxTs > existing.latestTs) {
|
|
283
|
-
existing.latest = batch;
|
|
284
|
-
existing.latestTs = maxTs;
|
|
285
|
-
}
|
|
286
|
-
return existing.running;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const state: NotifyTargetQueue = {
|
|
290
|
-
latest: batch,
|
|
291
|
-
latestTs: notifyBatchMaxTs(batch),
|
|
292
|
-
running: Promise.resolve([] as NotifyAttempt[]),
|
|
293
|
-
};
|
|
294
|
-
const running = (async (): Promise<NotifyAttempt[]> => {
|
|
295
|
-
const attempts: NotifyAttempt[] = [];
|
|
296
|
-
let failed = false;
|
|
297
|
-
let firstError: unknown;
|
|
298
|
-
while (state.latest) {
|
|
299
|
-
const current = state.latest;
|
|
300
|
-
state.latest = undefined;
|
|
301
|
-
try {
|
|
302
|
-
const attempt = await notifyTarget(sessionID, current);
|
|
303
|
-
if (attempt) {
|
|
304
|
-
attempts.push(attempt);
|
|
305
|
-
}
|
|
306
|
-
} catch (error) {
|
|
307
|
-
if (!failed) {
|
|
308
|
-
firstError = error;
|
|
309
|
-
failed = true;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
if (failed) {
|
|
314
|
-
throw firstError;
|
|
315
|
-
}
|
|
316
|
-
return attempts;
|
|
317
|
-
})();
|
|
318
|
-
state.running = running;
|
|
319
|
-
notifyQueues.set(key, state);
|
|
320
|
-
const cleanup = () => {
|
|
321
|
-
if (notifyQueues.get(key) === state) {
|
|
322
|
-
notifyQueues.delete(key);
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
void running.then(cleanup, cleanup);
|
|
326
|
-
return running;
|
|
327
|
-
}
|
|
194
|
+
const notifier = createNotifier({
|
|
195
|
+
transport: () => currentRuntime().transport,
|
|
196
|
+
bumpQueued: (roomId, sessionID, ts) => bumpLastQueued(roomId, sessionID, ts),
|
|
197
|
+
});
|
|
198
|
+
const { notifyRoom, notifyTarget } = notifier;
|
|
328
199
|
|
|
329
|
-
async function
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
selfSessionID: string,
|
|
334
|
-
messages: Message[],
|
|
200
|
+
export async function pollKnownSessions(
|
|
201
|
+
sessions: ReadonlySet<string>,
|
|
202
|
+
transport: QueueTransport,
|
|
203
|
+
opts: PluginRuntime["center"],
|
|
335
204
|
): Promise<void> {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const results = await Promise.allSettled(
|
|
342
|
-
targets.map(([sessionID]) => queueNotifyTarget(sessionID, batch)),
|
|
343
|
-
);
|
|
344
|
-
const logResults: PromiseSettledResult<NotifyAttempt>[] = [];
|
|
345
|
-
let failed = false;
|
|
346
|
-
let firstError: unknown;
|
|
347
|
-
for (const result of results) {
|
|
348
|
-
if (result.status === "fulfilled") {
|
|
349
|
-
for (const attempt of result.value) {
|
|
350
|
-
logResults.push({ status: "fulfilled", value: attempt });
|
|
351
|
-
}
|
|
352
|
-
} else {
|
|
353
|
-
logResults.push({ status: "rejected", reason: result.reason });
|
|
354
|
-
if (!failed) {
|
|
355
|
-
firstError = result.reason;
|
|
356
|
-
failed = true;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
205
|
+
if (opts.center) {
|
|
206
|
+
await Promise.all([...sessions].map((sessionID) =>
|
|
207
|
+
checkCenterInbox(opts, sessionID, transport),
|
|
208
|
+
));
|
|
209
|
+
return;
|
|
359
210
|
}
|
|
360
|
-
await
|
|
361
|
-
|
|
362
|
-
|
|
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
|
+
}
|
|
363
221
|
}
|
|
364
222
|
}
|
|
365
223
|
|
|
366
224
|
const READ_ONLY_ACTIONS = new Set(["list", "members", "poll"]);
|
|
367
225
|
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
+
) => {
|
|
378
238
|
try {
|
|
379
|
-
|
|
239
|
+
const runtime = currentRuntime();
|
|
240
|
+
if (runtime.center.center) {
|
|
380
241
|
// ponytail: central mode — self-pull inbox first, then act via HTTP
|
|
381
242
|
// (skipped for poll: poll itself pulls the inbox, so racing the
|
|
382
243
|
// fire-and-forget check would read the position it just advanced)
|
|
383
244
|
if (args.action !== "poll") {
|
|
384
|
-
void checkCenterInbox(
|
|
245
|
+
void checkCenterInbox(runtime.center, context.sessionID, runtime.transport);
|
|
385
246
|
}
|
|
386
|
-
return await centerAction(
|
|
247
|
+
return await centerAction(runtime.center, args, context.sessionID);
|
|
387
248
|
}
|
|
388
249
|
// ponytail: act as the identity this session registered with on join
|
|
389
250
|
let selfName = identity();
|
|
@@ -398,7 +259,7 @@ const room = tool({
|
|
|
398
259
|
selfName = reg[args.roomId]?.[context.sessionID]?.name ?? selfName;
|
|
399
260
|
}
|
|
400
261
|
if (args.action === "leave") {
|
|
401
|
-
//
|
|
262
|
+
// Keep the participant while another session still uses this name.
|
|
402
263
|
leaveNameShared = Object.entries(reg[args.roomId] ?? {}).some(
|
|
403
264
|
([sid, e]) => sid !== context.sessionID && e.name === selfName,
|
|
404
265
|
);
|
|
@@ -476,7 +337,7 @@ const room = tool({
|
|
|
476
337
|
// ponytail: ora-2 M2 — mirror the join guard: if another session
|
|
477
338
|
// still holds this name, remove only the registry entry, not the
|
|
478
339
|
// participant (or the sibling window breaks with NOT_JOINED);
|
|
479
|
-
//
|
|
340
|
+
// Do not emit a false leave event while the participant remains.
|
|
480
341
|
if (!leaveNameShared) {
|
|
481
342
|
svc.leaveRoom(args.roomId, selfName);
|
|
482
343
|
const room = svc.getRoom(args.roomId);
|
|
@@ -585,60 +446,45 @@ const room = tool({
|
|
|
585
446
|
} catch (err) {
|
|
586
447
|
return `error: ${err instanceof Error ? err.message : String(err)}`;
|
|
587
448
|
}
|
|
588
|
-
|
|
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,
|
|
589
455
|
});
|
|
590
456
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
}
|
|
599
|
-
|
|
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
|
+
});
|
|
600
472
|
|
|
601
|
-
export
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
void wakeSessions(centerOpts, queueTransport, serveWakeUrl);
|
|
620
|
-
}, 10000);
|
|
621
|
-
}
|
|
622
|
-
return {
|
|
623
|
-
tool: { room },
|
|
624
|
-
config,
|
|
625
|
-
// ponytail: central mode has no cross-host push, so each session pulls
|
|
626
|
-
// its inbox on chat messages and self-pushes queue notifications
|
|
627
|
-
// (SDK signature: (input, output) — sessionID is on the first arg)
|
|
628
|
-
...(centerUrl
|
|
629
|
-
? {
|
|
630
|
-
"chat.message": async (
|
|
631
|
-
input: { sessionID: string },
|
|
632
|
-
_output: unknown,
|
|
633
|
-
) => {
|
|
634
|
-
void checkCenterInbox(
|
|
635
|
-
centerOpts,
|
|
636
|
-
input.sessionID,
|
|
637
|
-
queueTransport,
|
|
638
|
-
);
|
|
639
|
-
},
|
|
640
|
-
}
|
|
641
|
-
: {}),
|
|
642
|
-
};
|
|
643
|
-
},
|
|
644
|
-
};
|
|
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 };
|