@alexsun-top/opencode-chat-room 0.8.6 → 0.8.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/CHANGELOG.md +20 -0
- package/dist/cli.js +1029 -415
- package/package.json +2 -1
- package/packages/chat-room-channeld/src/adapter.ts +52 -0
- package/packages/chat-room-channeld/src/config.ts +10 -2
- package/packages/chat-room-channeld/src/host.ts +26 -8
- package/packages/chat-room-channeld/src/inbound.ts +14 -1
- package/packages/chat-room-channeld/src/protocol.ts +38 -1
- package/packages/chat-room-channeld/src/stats.ts +4 -0
- package/plugin.ts +7 -2
- package/server/addressing-routes.ts +5 -1
- package/server/app.ts +6 -0
- package/server/assets/chat/events.js +5 -1
- package/server/assets/chat/operator-api.js +131 -0
- package/server/assets/chat/operator-i18n.js +219 -0
- package/server/assets/chat/operator-manage.js +221 -0
- package/server/assets/chat/operator.js +87 -130
- package/server/assets/chat/room-list.js +102 -38
- package/server/assets/conversation.css +10 -11
- package/server/channels.ts +60 -7
- package/server/chat-server.ts +4 -0
- package/server/mcp-http.ts +88 -0
- package/server/operator-routes.ts +195 -3
- package/server/operator-state.ts +72 -8
- package/server/operator.css +29 -0
- package/server/system.html +75 -31
- package/skills/chatroom/SKILL.md +20 -3
- package/src/center/center-client.ts +14 -2
- package/src/mcp/server.ts +177 -102
- package/src/runtime/v2-plugin.ts +7 -2
- package/src/state/sqlite-schema.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexsun-top/opencode-chat-room",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.8",
|
|
4
4
|
"description": "A chat-room server, web client, terminal client, and OpenCode/Codex/dsh plugins.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"server/routes",
|
|
42
42
|
"server/app.ts",
|
|
43
43
|
"server/channels.ts",
|
|
44
|
+
"server/mcp-http.ts",
|
|
44
45
|
"server/channel-attachments.ts",
|
|
45
46
|
"server/channel-settings.ts",
|
|
46
47
|
"server/channel-integration.ts",
|
|
@@ -13,9 +13,11 @@ import {
|
|
|
13
13
|
rpcError,
|
|
14
14
|
rpcRequest,
|
|
15
15
|
rpcResult,
|
|
16
|
+
FEATURE_VERSION_PATTERN,
|
|
16
17
|
type Endpoint,
|
|
17
18
|
type AccountField,
|
|
18
19
|
type AdapterCapabilities,
|
|
20
|
+
type FeatureDeclaration,
|
|
19
21
|
type IngestEvent,
|
|
20
22
|
type JsonRpcId,
|
|
21
23
|
type MessageRef,
|
|
@@ -157,9 +159,50 @@ function readCapabilities(result: unknown, accountId: string): AdapterCapabiliti
|
|
|
157
159
|
const value = (send as Record<string, unknown>).idempotency;
|
|
158
160
|
if (typeof value === "string" && value.trim()) capabilities.send = { idempotency: value.trim() };
|
|
159
161
|
}
|
|
162
|
+
const features = readFeatures(entry.features);
|
|
163
|
+
if (features) capabilities.features = features;
|
|
160
164
|
return Object.keys(capabilities).length ? capabilities : undefined;
|
|
161
165
|
}
|
|
162
166
|
|
|
167
|
+
const FEATURE_ID_PATTERN = /^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$/;
|
|
168
|
+
|
|
169
|
+
function readFeatures(raw: unknown): Record<string, FeatureDeclaration[]> | undefined {
|
|
170
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
171
|
+
const features: Record<string, FeatureDeclaration[]> = {};
|
|
172
|
+
for (const [id, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
173
|
+
if (!FEATURE_ID_PATTERN.test(id)) continue;
|
|
174
|
+
const declared = (Array.isArray(value) ? value : [value])
|
|
175
|
+
.map(readFeature)
|
|
176
|
+
.filter((feature): feature is FeatureDeclaration => feature !== undefined);
|
|
177
|
+
if (declared.length) features[id] = declared;
|
|
178
|
+
}
|
|
179
|
+
return Object.keys(features).length ? features : undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function readFeature(raw: unknown): FeatureDeclaration | undefined {
|
|
183
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
184
|
+
const entry = raw as Record<string, unknown>;
|
|
185
|
+
if (typeof entry.version !== "string" || !FEATURE_VERSION_PATTERN.test(entry.version)) return undefined;
|
|
186
|
+
const feature: FeatureDeclaration = { version: entry.version };
|
|
187
|
+
if (entry.direction === "receive" || entry.direction === "send" || entry.direction === "both") {
|
|
188
|
+
feature.direction = entry.direction;
|
|
189
|
+
}
|
|
190
|
+
const limits = readLimits(entry.limits);
|
|
191
|
+
if (limits) feature.limits = limits;
|
|
192
|
+
return feature;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function readLimits(raw: unknown): Record<string, string | number | boolean> | undefined {
|
|
196
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
197
|
+
const limits: Record<string, string | number | boolean> = {};
|
|
198
|
+
for (const [key, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
199
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
200
|
+
limits[key] = value;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return Object.keys(limits).length ? limits : undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
163
206
|
export class AdapterProcess {
|
|
164
207
|
readonly #account: AccountConfig;
|
|
165
208
|
readonly #onIngest: AdapterContext["onIngest"];
|
|
@@ -244,6 +287,15 @@ export class AdapterProcess {
|
|
|
244
287
|
log.info("adapter started", { account: this.#account.id });
|
|
245
288
|
}
|
|
246
289
|
|
|
290
|
+
/**
|
|
291
|
+
* Platform operation declared by the adapter (history, members, ...).
|
|
292
|
+
* The adapter rejects ops it did not declare or lacks permission for.
|
|
293
|
+
*/
|
|
294
|
+
async op(op: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
|
295
|
+
if (!this.#started) throw new Error(`adapter ${this.#account.id} is not started`);
|
|
296
|
+
return this.#call("op", { op, ...params }, this.#sendTimeoutMs);
|
|
297
|
+
}
|
|
298
|
+
|
|
247
299
|
/**
|
|
248
300
|
* Read-only handshake used when a plugin is uploaded: initialize only, then
|
|
249
301
|
* kill. It does not start the account and does not need credentials.
|
|
@@ -47,6 +47,8 @@ export interface ChannelConfig {
|
|
|
47
47
|
rules: RuleConfig[];
|
|
48
48
|
/** Room notifications for inbound events; never matched by outbound rules. */
|
|
49
49
|
inbound: InboundRoute[];
|
|
50
|
+
/** Optional room that receives inbound events with no matching route. */
|
|
51
|
+
inboundFallback?: string;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
type RecordValue = Record<string, unknown>;
|
|
@@ -99,7 +101,10 @@ function parseTarget(value: unknown, where: string): RuleTarget {
|
|
|
99
101
|
return {
|
|
100
102
|
platform: identifierValue(raw.platform, `${where}.platform`),
|
|
101
103
|
account: identifierValue(raw.account, `${where}.account`),
|
|
102
|
-
|
|
104
|
+
// A wildcard room marks a rule that delivers to the requested target on
|
|
105
|
+
// this platform and account instead of one fixed chat. Inbound routes never
|
|
106
|
+
// use parseTarget, so "*" stays limited to outbound rules.
|
|
107
|
+
room: raw.room === "*" ? "*" : identifierValue(raw.room, `${where}.room`),
|
|
103
108
|
};
|
|
104
109
|
}
|
|
105
110
|
|
|
@@ -244,7 +249,10 @@ export function parseConfig(value: unknown): ChannelConfig {
|
|
|
244
249
|
if (ruleKeys.has(key)) throw new ConfigError(`config: duplicate rule "${rule.id}" version ${rule.version}`);
|
|
245
250
|
ruleKeys.add(key);
|
|
246
251
|
}
|
|
247
|
-
|
|
252
|
+
const inboundFallback = raw.inboundFallback === undefined
|
|
253
|
+
? undefined
|
|
254
|
+
: stringValue(raw.inboundFallback, "inboundFallback");
|
|
255
|
+
return { stateFile, accounts, rules, inbound, ...(inboundFallback ? { inboundFallback } : {}) };
|
|
248
256
|
}
|
|
249
257
|
|
|
250
258
|
/** Load a config file. YAML parsing also accepts JSON documents. */
|
|
@@ -23,6 +23,8 @@ export interface AdapterHandle {
|
|
|
23
23
|
target: Endpoint,
|
|
24
24
|
payload: unknown,
|
|
25
25
|
): Promise<SendReceipt>;
|
|
26
|
+
/** Optional platform operation declared through capabilities. */
|
|
27
|
+
op?(op: string, params: Record<string, unknown>): Promise<unknown>;
|
|
26
28
|
stop(): Promise<void>;
|
|
27
29
|
}
|
|
28
30
|
|
|
@@ -115,6 +117,14 @@ export class ChannelHost {
|
|
|
115
117
|
}
|
|
116
118
|
|
|
117
119
|
/** Read-only view for routes: unknown ids or platform mismatches are missing. */
|
|
120
|
+
/** Run a capability-declared platform operation on a started adapter. */
|
|
121
|
+
async runOp(accountId: string, op: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
|
122
|
+
const adapter = this.#adapters.get(accountId);
|
|
123
|
+
if (!adapter) throw new Error(`adapter ${accountId} is not running`);
|
|
124
|
+
if (!adapter.op) throw new Error(`adapter ${accountId} does not support operations`);
|
|
125
|
+
return adapter.op(op, params);
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
targetStatus(platform: string, accountId: string): "ready" | "disabled" | "missing" {
|
|
119
129
|
const account = this.#config.accounts.find((item) => item.id === accountId);
|
|
120
130
|
if (!account || account.platform !== platform) return "missing";
|
|
@@ -331,7 +341,14 @@ export class ChannelHost {
|
|
|
331
341
|
const planned: PlannedDelivery[] = [];
|
|
332
342
|
for (const rule of matchRules(this.#config, event)) {
|
|
333
343
|
for (const target of rule.to) {
|
|
334
|
-
|
|
344
|
+
let endpoint = toEndpoint(this.#config, rule.id, target);
|
|
345
|
+
if (target.room === "*") {
|
|
346
|
+
// A wildcard rule delivers to the requested chat on the same platform
|
|
347
|
+
// and account; without a requested target it has nowhere to go.
|
|
348
|
+
if (!requestedTarget) continue;
|
|
349
|
+
if (requestedTarget.platform !== endpoint.platform || requestedTarget.accountId !== endpoint.accountId) continue;
|
|
350
|
+
endpoint = requestedTarget;
|
|
351
|
+
}
|
|
335
352
|
if (requestedTarget && !sameEndpoint(endpoint, requestedTarget)) continue;
|
|
336
353
|
const plan = planPayload(rule, event, endpoint);
|
|
337
354
|
if ("dropped" in plan) {
|
|
@@ -519,13 +536,12 @@ export class ChannelHost {
|
|
|
519
536
|
if (this.#disabled.has(accountId)) return;
|
|
520
537
|
const account = this.#config.accounts.find((item) => item.id === accountId);
|
|
521
538
|
if (!account) return;
|
|
522
|
-
const key = `${account.platform}/${account.id}`;
|
|
523
539
|
if (status.inbound === "failed") {
|
|
524
540
|
const detail = sanitizeAccountError(`inbound failed: ${status.detail ?? "unknown"}`);
|
|
525
|
-
this.#
|
|
541
|
+
this.#setAccountState(account, "started", detail);
|
|
526
542
|
log.warn(detail, { account: accountId });
|
|
527
543
|
} else if (status.inbound === "ready") {
|
|
528
|
-
this.#
|
|
544
|
+
this.#setAccountState(account, "started");
|
|
529
545
|
}
|
|
530
546
|
}
|
|
531
547
|
|
|
@@ -534,10 +550,12 @@ export class ChannelHost {
|
|
|
534
550
|
state: ChannelAccountRuntime["state"],
|
|
535
551
|
lastError?: string,
|
|
536
552
|
): void {
|
|
537
|
-
this.#
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
553
|
+
const features = this.#adapters.get(account.id)?.capabilities?.features;
|
|
554
|
+
this.#accountStates.set(`${account.platform}/${account.id}`, {
|
|
555
|
+
state,
|
|
556
|
+
...(lastError ? { lastError } : {}),
|
|
557
|
+
...(features ? { features } : {}),
|
|
558
|
+
});
|
|
541
559
|
}
|
|
542
560
|
|
|
543
561
|
#disableAccount(accountId: string, reason: string): void {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ChannelConfig, InboundRoute } from "./config.ts";
|
|
2
2
|
import type { IngestEvent } from "./protocol.ts";
|
|
3
3
|
|
|
4
|
-
const MAX_RELAY_TEXT =
|
|
4
|
+
const MAX_RELAY_TEXT = 5000;
|
|
5
5
|
|
|
6
6
|
/** Inbound routes that turn one committed external event into a room notification. */
|
|
7
7
|
export function matchInboundRoutes(config: ChannelConfig, event: IngestEvent): InboundRoute[] {
|
|
@@ -13,6 +13,19 @@ export function matchInboundRoutes(config: ChannelConfig, event: IngestEvent): I
|
|
|
13
13
|
);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/** Inbound message kinds; action events such as message.forward_requested are internal. */
|
|
17
|
+
const INBOUND_KINDS = new Set(["message.created", "message"]);
|
|
18
|
+
|
|
19
|
+
/** Matched routes, or one fallback route for an unmatched inbound source. */
|
|
20
|
+
export function inboundRoutes(config: ChannelConfig, event: IngestEvent): InboundRoute[] {
|
|
21
|
+
const matched = matchInboundRoutes(config, event);
|
|
22
|
+
if (matched.length > 0 || !config.inboundFallback || !INBOUND_KINDS.has(event.kind)) return matched;
|
|
23
|
+
return [{
|
|
24
|
+
from: { platform: event.source.platform, account: event.source.accountId, room: event.source.roomId },
|
|
25
|
+
to: { room: config.inboundFallback },
|
|
26
|
+
}];
|
|
27
|
+
}
|
|
28
|
+
|
|
16
29
|
export function platformLabel(platform: string): string {
|
|
17
30
|
const value = platform.trim();
|
|
18
31
|
return value ? value[0]!.toUpperCase() + value.slice(1) : "Channel";
|
|
@@ -57,14 +57,46 @@ export type AccountCapabilities = {
|
|
|
57
57
|
env?: AccountField[];
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
+
/** A feature contract version such as `v1`. A `domain/` prefix means a different contract; matching is exact. */
|
|
61
|
+
export const FEATURE_VERSION_PATTERN = /^(?:[A-Za-z0-9][A-Za-z0-9.-]*\/)?v[1-9]\d*$/;
|
|
62
|
+
|
|
63
|
+
export type FeatureDirection = "receive" | "send" | "both";
|
|
64
|
+
|
|
65
|
+
/** One capability, versioned on its own so features can evolve independently. */
|
|
66
|
+
export type FeatureDeclaration = {
|
|
67
|
+
version: string;
|
|
68
|
+
direction?: FeatureDirection;
|
|
69
|
+
limits?: Record<string, string | number | boolean>;
|
|
70
|
+
};
|
|
71
|
+
|
|
60
72
|
/** Adapter-declared platform facts the generic host must not hardcode. */
|
|
61
73
|
export type AdapterCapabilities = {
|
|
62
74
|
displayName?: string;
|
|
63
75
|
attachments?: AttachmentCapabilities;
|
|
64
76
|
account?: AccountCapabilities;
|
|
65
77
|
send?: { idempotency?: string };
|
|
78
|
+
/**
|
|
79
|
+
* Stable feature id to every version the adapter implements. An adapter may
|
|
80
|
+
* declare several versions of one feature; the host picks the highest it supports.
|
|
81
|
+
*/
|
|
82
|
+
features?: Record<string, FeatureDeclaration[]>;
|
|
66
83
|
};
|
|
67
84
|
|
|
85
|
+
/** Return the first host-supported version of a feature, or undefined when none match. */
|
|
86
|
+
export function selectFeature(
|
|
87
|
+
capabilities: AdapterCapabilities | undefined,
|
|
88
|
+
id: string,
|
|
89
|
+
hostVersions: string[],
|
|
90
|
+
): FeatureDeclaration | undefined {
|
|
91
|
+
const declared = capabilities?.features?.[id];
|
|
92
|
+
if (!declared?.length) return undefined;
|
|
93
|
+
for (const version of hostVersions) {
|
|
94
|
+
const match = declared.find((feature) => feature.version === version);
|
|
95
|
+
if (match) return match;
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
68
100
|
export interface IngestEvent {
|
|
69
101
|
schemaVersion: number;
|
|
70
102
|
eventId: string;
|
|
@@ -77,7 +109,12 @@ export interface IngestEvent {
|
|
|
77
109
|
parent?: MessageRef;
|
|
78
110
|
occurredAt?: string;
|
|
79
111
|
receivedAt: string;
|
|
80
|
-
content?: {
|
|
112
|
+
content?: {
|
|
113
|
+
text?: string;
|
|
114
|
+
attachmentRefs?: string[];
|
|
115
|
+
attachments?: AttachmentRef[];
|
|
116
|
+
mentions?: Array<{ id: string; name: string }>;
|
|
117
|
+
};
|
|
81
118
|
selectedMessage?: MessageRef;
|
|
82
119
|
requestedTarget?: Endpoint;
|
|
83
120
|
cause?: { rootEventId: string; visitedEndpoints: Endpoint[]; hopCount: number };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PgDatabase } from "./pg/database.ts";
|
|
2
2
|
import { Database, type SQLQueryBindings, type StateDatabase } from "./pg/sqlite.ts";
|
|
3
3
|
import { PUBLISHED_RESULT_PREFIX } from "./published.ts";
|
|
4
|
+
import type { FeatureDeclaration } from "./protocol.ts";
|
|
4
5
|
|
|
5
6
|
export type ChannelAccountState = "started" | "failed" | "disabled" | "unknown";
|
|
6
7
|
|
|
@@ -8,12 +9,14 @@ export type ChannelAccountState = "started" | "failed" | "disabled" | "unknown";
|
|
|
8
9
|
export interface ChannelAccountRuntime {
|
|
9
10
|
state: ChannelAccountState;
|
|
10
11
|
lastError?: string;
|
|
12
|
+
features?: Record<string, FeatureDeclaration[]>;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export interface ChannelAccountStats {
|
|
14
16
|
account: string;
|
|
15
17
|
state: ChannelAccountState;
|
|
16
18
|
lastError?: string;
|
|
19
|
+
features?: Record<string, FeatureDeclaration[]>;
|
|
17
20
|
queueDepth: number;
|
|
18
21
|
oldestBacklogAt: number | null;
|
|
19
22
|
unknown: number;
|
|
@@ -183,6 +186,7 @@ function buildStats(
|
|
|
183
186
|
account.state = live.state;
|
|
184
187
|
const lastError = live.lastError ? sanitizeAccountError(live.lastError) : "";
|
|
185
188
|
if (lastError) account.lastError = lastError;
|
|
189
|
+
if (live.features) account.features = live.features;
|
|
186
190
|
accounts.set(key, account);
|
|
187
191
|
}
|
|
188
192
|
|
package/plugin.ts
CHANGED
|
@@ -281,10 +281,14 @@ export const roomArgs = z.object({
|
|
|
281
281
|
platform: z.string().regex(/^[A-Za-z0-9._-]{1,128}$/).describe("Target platform identifier from forward_targets."),
|
|
282
282
|
account: z.string().regex(/^[A-Za-z0-9._-]{1,128}$/).describe("Target account identifier from forward_targets."),
|
|
283
283
|
room: z.string().regex(/^[A-Za-z0-9._-]{1,128}$/).describe("Target room identifier from forward_targets."),
|
|
284
|
-
}).optional().describe("For forward: one exact ready target from forward_targets; tool arguments cannot choose credentials."),
|
|
284
|
+
}).optional().describe("For forward and send: one exact ready target from forward_targets; tool arguments cannot choose credentials."),
|
|
285
|
+
forward: z.object({
|
|
286
|
+
platform: z.string().regex(/^[A-Za-z0-9._-]{1,128}$/).describe("Target platform identifier from forward_targets."),
|
|
287
|
+
account: z.string().regex(/^[A-Za-z0-9._-]{1,128}$/).describe("Target account identifier from forward_targets."),
|
|
288
|
+
room: z.string().regex(/^[A-Za-z0-9._-]{1,128}$/).describe("Target room identifier from forward_targets."),
|
|
289
|
+
}).optional().describe("For send: queue the new reply to this exact target in the same call; sent first, then forwarded. Use forward_targets to read valid values."),
|
|
285
290
|
resourceId: z.string().optional().describe("Resource UUID returned by upload_resource. read_resource and delete_resource also require roomId."),
|
|
286
291
|
filePath: z.string().optional().describe("Absolute path to a regular local file to upload; central mode only; requires host read permission."),
|
|
287
|
-
text: z.string().max(5000).optional().describe("Message text, at most 5000 characters; required for send."),
|
|
288
292
|
isQuestion: z.boolean().optional().describe("For send: mark the message as a question awaiting an answer."),
|
|
289
293
|
parentMessageId: z.string().optional().describe("For send: the ID of the message this one answers or replies to."),
|
|
290
294
|
replyToMessageID: z.string().optional().describe("Legacy alias of parentMessageId for send."),
|
|
@@ -306,6 +310,7 @@ export const roomArgs = z.object({
|
|
|
306
310
|
schedule: z.discriminatedUnion("type", [z.object({ type: z.literal("once"), at: z.string(), timezone: z.string().refine((value) => { try { new Intl.DateTimeFormat("en", { timeZone: value }); return true; } catch { return false; } }, "timezone must be an IANA timezone").optional() }), z.object({ type: z.literal("cron"), expression: z.string().regex(/^\S+\s+\S+\s+\S+\s+\S+\s+\S+$/), timezone: z.string().refine((value) => { try { new Intl.DateTimeFormat("en", { timeZone: value }); return true; } catch { return false; } }, "timezone must be an IANA timezone").optional() })]).optional(),
|
|
307
311
|
paused: z.boolean().optional(),
|
|
308
312
|
joinPolicy: z.enum(["open", "approval"]).optional().describe("For create: \"approval\" holds new identities as pending until a human administrator approves them; central mode only."),
|
|
313
|
+
text: z.string().max(5000).optional().describe("Message text, at most 5000 characters; required for send."),
|
|
309
314
|
}).superRefine((args, ctx) => {
|
|
310
315
|
if (args.action === "join" && !args.name?.trim()) {
|
|
311
316
|
ctx.addIssue({ code: "custom", path: ["name"], message: "name is required to join a room; pass your participant display name" });
|
|
@@ -30,9 +30,13 @@ async function memberContext(roomID: string, sessionID: string): Promise<MemberC
|
|
|
30
30
|
const entry = sessionID && sessionID.length <= MAX_SESSION ? registry[roomID]?.[sessionID] : undefined;
|
|
31
31
|
// Pending and kicked sessions have no registry entry; a stale entry without
|
|
32
32
|
// a participant is not a membership either.
|
|
33
|
+
// Registry entries carry only the display name, while participants are keyed
|
|
34
|
+
// by id; a client may join with an id that differs from its name.
|
|
35
|
+
const participants = [...room?.participants.values() ?? []];
|
|
36
|
+
const joined = Boolean(entry) && participants.some((participant) => participant.id === entry!.name || participant.name === entry!.name);
|
|
33
37
|
const allowed = Boolean(room && entry) && (room!.kind === "direct"
|
|
34
38
|
? Boolean(room!.directSessionIDs?.includes(sessionID))
|
|
35
|
-
:
|
|
39
|
+
: joined);
|
|
36
40
|
return {
|
|
37
41
|
allowed,
|
|
38
42
|
name: entry?.name ?? "",
|
package/server/app.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { addressingRoutes } from "./addressing-routes.ts";
|
|
|
5
5
|
import type { ChannelController } from "./channels.ts";
|
|
6
6
|
import { historyRoute } from "./history-route.ts";
|
|
7
7
|
import { joinRequestRoutes } from "./join-requests.ts";
|
|
8
|
+
import { mcpHttpRoutes } from "./mcp-http.ts";
|
|
8
9
|
import { memberRoutes } from "./member-routes.ts";
|
|
9
10
|
import { messageRecallRoutes } from "./message-recall-routes.ts";
|
|
10
11
|
import { roomManagement } from "./newrooms.ts";
|
|
@@ -24,6 +25,7 @@ export type AppOptions = {
|
|
|
24
25
|
operatorToken?: string;
|
|
25
26
|
serveWakeUrl: string;
|
|
26
27
|
channeld: () => ChannelController | undefined;
|
|
28
|
+
mcp?: { center: string; sessionID: string };
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
const json = (body: unknown, status = 200) =>
|
|
@@ -43,6 +45,10 @@ export function createApp(options: AppOptions): (req: Request) => Promise<Respon
|
|
|
43
45
|
if (p === "/system" || p.startsWith("/system/")) {
|
|
44
46
|
return operatorRoutes(req, url, { here: options.here, token: operatorToken });
|
|
45
47
|
}
|
|
48
|
+
if (options.mcp) {
|
|
49
|
+
const mcp = await mcpHttpRoutes(req, url, { ...options.mcp, token: options.token });
|
|
50
|
+
if (mcp) return mcp;
|
|
51
|
+
}
|
|
46
52
|
const page = await pageRoutes(req, url, options.here, options.channeld()?.enabled ?? false);
|
|
47
53
|
if (page) return page;
|
|
48
54
|
// ponytail: F1 — reject non-JSON POSTs; a text/plain simple request from a
|
|
@@ -589,7 +589,11 @@ export function initEvents(app) {
|
|
|
589
589
|
app.closePanels();
|
|
590
590
|
$("#btnRoomOptions").focus();
|
|
591
591
|
}
|
|
592
|
-
|
|
592
|
+
const composer = $("#msgInput");
|
|
593
|
+
const composerEmpty = !!composer && event.target === composer && !composer.draftValue.trim();
|
|
594
|
+
// An empty composer has no history to walk, so plain arrows switch rooms.
|
|
595
|
+
const plainSwitch = composerEmpty && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey;
|
|
596
|
+
if ((event.altKey || plainSwitch) && !event.ctrlKey && !event.metaKey && !event.shiftKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
|
|
593
597
|
// Same switch as a rail row click on a joined room, in the order the rail renders.
|
|
594
598
|
const joined = (app.state.availableRooms || []).filter((room) => app.state.rooms.has(room.id));
|
|
595
599
|
const index = joined.findIndex((room) => room.id === app.state.room?.id);
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
export function createApi(token) {
|
|
2
|
+
return {
|
|
3
|
+
async request(path, init = {}) {
|
|
4
|
+
const headers = new Headers(init.headers);
|
|
5
|
+
const key = token();
|
|
6
|
+
if (key)
|
|
7
|
+
headers.set("Authorization", `Bearer ${key}`);
|
|
8
|
+
if (init.body)
|
|
9
|
+
headers.set("Content-Type", "application/json");
|
|
10
|
+
return fetch(path, {
|
|
11
|
+
...init,
|
|
12
|
+
headers,
|
|
13
|
+
cache: "no-store",
|
|
14
|
+
credentials: "omit",
|
|
15
|
+
redirect: "error",
|
|
16
|
+
referrerPolicy: "no-referrer",
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
async readJson(response) {
|
|
20
|
+
try {
|
|
21
|
+
return await response.json();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function parseRooms(value) {
|
|
30
|
+
if (!value || typeof value !== "object")
|
|
31
|
+
return null;
|
|
32
|
+
const list = value.rooms;
|
|
33
|
+
if (!Array.isArray(list))
|
|
34
|
+
return null;
|
|
35
|
+
const result = [];
|
|
36
|
+
for (const item of list) {
|
|
37
|
+
if (!item || typeof item !== "object")
|
|
38
|
+
continue;
|
|
39
|
+
const room = item;
|
|
40
|
+
if (typeof room.id !== "string" || typeof room.name !== "string")
|
|
41
|
+
continue;
|
|
42
|
+
result.push({
|
|
43
|
+
id: room.id,
|
|
44
|
+
name: room.name,
|
|
45
|
+
ownerId: typeof room.ownerId === "string" ? room.ownerId : "",
|
|
46
|
+
kind: typeof room.kind === "string" ? room.kind : "group",
|
|
47
|
+
memberCount: typeof room.memberCount === "number" ? room.memberCount : 0,
|
|
48
|
+
agentCount: typeof room.agentCount === "number" ? room.agentCount : 0,
|
|
49
|
+
lastActiveAt: typeof room.lastActiveAt === "number" ? room.lastActiveAt : 0,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
export function parseDetail(value, roomId) {
|
|
55
|
+
if (!value || typeof value !== "object")
|
|
56
|
+
return null;
|
|
57
|
+
const root = value;
|
|
58
|
+
const raw = root.room;
|
|
59
|
+
if (!raw || typeof raw !== "object")
|
|
60
|
+
return null;
|
|
61
|
+
const room = raw;
|
|
62
|
+
if (typeof room.id !== "string" || typeof room.name !== "string" || room.id !== roomId)
|
|
63
|
+
return null;
|
|
64
|
+
const candidates = [];
|
|
65
|
+
if (Array.isArray(root.candidates))
|
|
66
|
+
for (const item of root.candidates) {
|
|
67
|
+
if (!item || typeof item !== "object")
|
|
68
|
+
continue;
|
|
69
|
+
const candidate = item;
|
|
70
|
+
if (typeof candidate.name === "string" && typeof candidate.membershipID === "string" && candidate.membershipID) {
|
|
71
|
+
candidates.push({ name: candidate.name, membershipID: candidate.membershipID });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const audit = [];
|
|
75
|
+
if (Array.isArray(root.audit))
|
|
76
|
+
for (const item of root.audit) {
|
|
77
|
+
if (!item || typeof item !== "object")
|
|
78
|
+
continue;
|
|
79
|
+
const entry = item;
|
|
80
|
+
audit.push({
|
|
81
|
+
createdAt: typeof entry.createdAt === "number" ? entry.createdAt : 0,
|
|
82
|
+
previousOwner: typeof entry.previousOwner === "string" ? entry.previousOwner : "",
|
|
83
|
+
targetName: typeof entry.targetName === "string" ? entry.targetName : "",
|
|
84
|
+
reason: typeof entry.reason === "string" ? entry.reason : "",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const admins = Array.isArray(room.admins) ? room.admins.filter((name) => typeof name === "string") : [];
|
|
88
|
+
const members = [];
|
|
89
|
+
if (Array.isArray(room.members))
|
|
90
|
+
for (const item of room.members) {
|
|
91
|
+
if (!item || typeof item !== "object")
|
|
92
|
+
continue;
|
|
93
|
+
const member = item;
|
|
94
|
+
if (typeof member.name !== "string")
|
|
95
|
+
continue;
|
|
96
|
+
const role = member.role === "owner" || member.role === "admin" ? member.role : "member";
|
|
97
|
+
members.push({
|
|
98
|
+
name: member.name,
|
|
99
|
+
role,
|
|
100
|
+
isAgent: member.isAgent === true,
|
|
101
|
+
lastSeenAt: typeof member.lastSeenAt === "number" ? member.lastSeenAt : 0,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const actions = [];
|
|
105
|
+
if (Array.isArray(root.actions))
|
|
106
|
+
for (const item of root.actions) {
|
|
107
|
+
if (!item || typeof item !== "object")
|
|
108
|
+
continue;
|
|
109
|
+
const entry = item;
|
|
110
|
+
actions.push({
|
|
111
|
+
createdAt: typeof entry.createdAt === "number" ? entry.createdAt : 0,
|
|
112
|
+
action: typeof entry.action === "string" ? entry.action : "",
|
|
113
|
+
target: typeof entry.target === "string" ? entry.target : "",
|
|
114
|
+
detail: typeof entry.detail === "string" ? entry.detail : "",
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
room: {
|
|
119
|
+
id: room.id,
|
|
120
|
+
name: room.name,
|
|
121
|
+
ownerId: typeof room.ownerId === "string" ? room.ownerId : "",
|
|
122
|
+
ownerMembershipID: typeof room.ownerMembershipID === "string" ? room.ownerMembershipID : null,
|
|
123
|
+
admins,
|
|
124
|
+
members,
|
|
125
|
+
pendingJoins: typeof room.pendingJoins === "number" ? room.pendingJoins : 0,
|
|
126
|
+
},
|
|
127
|
+
candidates,
|
|
128
|
+
audit,
|
|
129
|
+
actions,
|
|
130
|
+
};
|
|
131
|
+
}
|