@mosadd/mcp 3.0.0-alpha.5 → 3.0.0-alpha.50
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 +309 -0
- package/LICENSE +201 -201
- package/README.md +150 -126
- package/dist/bin/mcp.js +42 -3
- package/dist/chunk-L4PB6A6Z.js +3800 -0
- package/dist/index.d.ts +118 -5
- package/dist/index.js +7 -3
- package/package.json +10 -7
- package/src/__tests__/channel-e2ee.test.ts +62 -0
- package/src/__tests__/golden-path.test.ts +139 -137
- package/src/__tests__/integration.test.ts +182 -178
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -178
- package/src/__tests__/mirc-invite.test.ts +112 -0
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +199 -0
- package/src/__tests__/server.test.ts +71 -67
- package/src/__tests__/serverJsonVersion.test.ts +45 -0
- package/src/__tests__/tool-count-consistency.test.ts +199 -0
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -97
- package/src/config.ts +66 -66
- package/src/crypto/channel-e2ee.ts +288 -0
- package/src/crypto/mdm-session.ts +502 -403
- package/src/index.ts +19 -17
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +116 -0
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +122 -117
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +231 -96
- package/src/server.ts +162 -105
- package/src/tools/__tests__/ragDeleteFoldsSourceId.test.ts +67 -0
- package/src/tools/actions.ts +166 -0
- package/src/tools/attachments.ts +276 -0
- package/src/tools/capabilities.ts +70 -68
- package/src/tools/embed.ts +110 -0
- package/src/tools/index.ts +181 -51
- package/src/tools/knowledge-graph.ts +186 -0
- package/src/tools/knowledge.ts +344 -132
- package/src/tools/mail-agentbox.ts +155 -0
- package/src/tools/mail-aliases.ts +25 -0
- package/src/tools/mail-provenance.ts +137 -0
- package/src/tools/mail.ts +403 -157
- package/src/tools/mcall.ts +216 -216
- package/src/tools/mdm-as-agent.ts +126 -0
- package/src/tools/mdm-voice.ts +220 -206
- package/src/tools/mdm.ts +73 -26
- package/src/tools/mirc-edge.ts +196 -190
- package/src/tools/mirc-members.ts +287 -177
- package/src/tools/mirc-messages.ts +76 -27
- package/src/tools/mirc.ts +308 -170
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +131 -121
- package/src/tools/murl.ts +314 -0
- package/src/tools/presence.ts +442 -0
- package/src/tools/ptt-ingest.ts +99 -0
- package/src/tools/threat.ts +123 -0
- package/src/tools/voice-format.ts +28 -0
- package/src/types.ts +121 -85
- package/src/version.ts +8 -0
- package/src/voice/floor.ts +132 -132
- package/dist/chunk-4C36JDTJ.js +0 -1836
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* voice-format — W4 (rozkaz Króla 16.09): głos do agenta ma czytać się jak notka,
|
|
3
|
+
* nie jak 500 KB base64 w polu text.
|
|
4
|
+
*
|
|
5
|
+
* Wiadomości voice/ptt (message_type "voice"/"ptt") niosą payload w kształcie
|
|
6
|
+
* { audio, mime, dur } (base64 webm/mp4). Do tej pory mDM_list/mIRC_list_messages
|
|
7
|
+
* zwracały ten surowy JSON jako text — agent widział śmieć zamiast nagrania.
|
|
8
|
+
* Teraz dostaje zwięzły marker z czasem trwania. Audio zostaje w payloadzie;
|
|
9
|
+
* kto chce odsłuchać, sięga po narzędzie głosowe (mDM_voice_note / transkrypcję).
|
|
10
|
+
*/
|
|
11
|
+
export function formatVoiceIfAny(text: string): string {
|
|
12
|
+
try {
|
|
13
|
+
const obj = JSON.parse(text) as unknown;
|
|
14
|
+
if (
|
|
15
|
+
obj &&
|
|
16
|
+
typeof obj === "object" &&
|
|
17
|
+
typeof (obj as { audio?: unknown }).audio === "string" &&
|
|
18
|
+
(obj as { audio: string }).audio.length > 1000 &&
|
|
19
|
+
typeof (obj as { dur?: unknown }).dur === "number"
|
|
20
|
+
) {
|
|
21
|
+
const s = Math.max(1, Math.round((obj as { dur: number }).dur / 1000));
|
|
22
|
+
return `[voice note — ${s}s]`;
|
|
23
|
+
}
|
|
24
|
+
} catch {
|
|
25
|
+
// nie JSON — zwykły tekst, zwróć bez zmian
|
|
26
|
+
}
|
|
27
|
+
return text;
|
|
28
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,85 +1,121 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal types for the mosadd MCP server.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { z } from "zod";
|
|
6
|
-
import type { DmProvider, VoiceProvider } from "@mosadd/providers";
|
|
7
|
-
import type { MdmKeyStore } from "./crypto/mdm-session.js";
|
|
8
|
-
|
|
9
|
-
export type Mode = "cloud" | "local" | "self-host";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Transport backends per channel. The server fills any missing entry with its
|
|
13
|
-
* default network adapter; a host (e.g. cymru-os) injects alternatives —
|
|
14
|
-
* notably a radio DmProvider — via `MosaddServerOptions.providers`. This is
|
|
15
|
-
* the DI seam that keeps mosadd-os transport-agnostic.
|
|
16
|
-
*
|
|
17
|
-
* `keys` is LOCAL crypto custody (identity + prekeys + ratchet sessions) for
|
|
18
|
-
* mDM E2EE — distinct from transport. Defaults to an in-process keystore; a
|
|
19
|
-
* host may inject a file/keychain-backed one.
|
|
20
|
-
*/
|
|
21
|
-
export interface ProviderRegistry {
|
|
22
|
-
dm: DmProvider;
|
|
23
|
-
keys: MdmKeyStore;
|
|
24
|
-
/**
|
|
25
|
-
* mTALK push-to-talk transport. Defaults to the network provider (LiveKit
|
|
26
|
-
* media + authoritative floor state). A radio host injects a provider that
|
|
27
|
-
* maps the floor onto a physical RF channel.
|
|
28
|
-
*/
|
|
29
|
-
voice: VoiceProvider;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface MosaddServerOptions {
|
|
33
|
-
/** API key for hosted mode. Required if mode === "cloud". */
|
|
34
|
-
apiKey?: string;
|
|
35
|
-
/** Override the default hub URL (https://mcp.mosadd.com). */
|
|
36
|
-
hubUrl?: string;
|
|
37
|
-
/** Operating mode. Defaults to "cloud" if apiKey is present, else "local". */
|
|
38
|
-
mode?: Mode;
|
|
39
|
-
/** Log level. */
|
|
40
|
-
logLevel?: "debug" | "info" | "warn" | "error";
|
|
41
|
-
/**
|
|
42
|
-
* Host-injected channel providers. Any omitted channel uses the default
|
|
43
|
-
* network adapter. A carrier-aware host supplies e.g. a radio DmProvider here.
|
|
44
|
-
*/
|
|
45
|
-
providers?: Partial<ProviderRegistry>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Internal types for the mosadd MCP server.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { z } from "zod";
|
|
6
|
+
import type { DmProvider, VoiceProvider } from "@mosadd/providers";
|
|
7
|
+
import type { MdmKeyStore } from "./crypto/mdm-session.js";
|
|
8
|
+
|
|
9
|
+
export type Mode = "cloud" | "local" | "self-host";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Transport backends per channel. The server fills any missing entry with its
|
|
13
|
+
* default network adapter; a host (e.g. cymru-os) injects alternatives —
|
|
14
|
+
* notably a radio DmProvider — via `MosaddServerOptions.providers`. This is
|
|
15
|
+
* the DI seam that keeps mosadd-os transport-agnostic.
|
|
16
|
+
*
|
|
17
|
+
* `keys` is LOCAL crypto custody (identity + prekeys + ratchet sessions) for
|
|
18
|
+
* mDM E2EE — distinct from transport. Defaults to an in-process keystore; a
|
|
19
|
+
* host may inject a file/keychain-backed one.
|
|
20
|
+
*/
|
|
21
|
+
export interface ProviderRegistry {
|
|
22
|
+
dm: DmProvider;
|
|
23
|
+
keys: MdmKeyStore;
|
|
24
|
+
/**
|
|
25
|
+
* mTALK push-to-talk transport. Defaults to the network provider (LiveKit
|
|
26
|
+
* media + authoritative floor state). A radio host injects a provider that
|
|
27
|
+
* maps the floor onto a physical RF channel.
|
|
28
|
+
*/
|
|
29
|
+
voice: VoiceProvider;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MosaddServerOptions {
|
|
33
|
+
/** API key for hosted mode. Required if mode === "cloud". */
|
|
34
|
+
apiKey?: string;
|
|
35
|
+
/** Override the default hub URL (https://mcp.mosadd.com). */
|
|
36
|
+
hubUrl?: string;
|
|
37
|
+
/** Operating mode. Defaults to "cloud" if apiKey is present, else "local". */
|
|
38
|
+
mode?: Mode;
|
|
39
|
+
/** Log level. */
|
|
40
|
+
logLevel?: "debug" | "info" | "warn" | "error";
|
|
41
|
+
/**
|
|
42
|
+
* Host-injected channel providers. Any omitted channel uses the default
|
|
43
|
+
* network adapter. A carrier-aware host supplies e.g. a radio DmProvider here.
|
|
44
|
+
*/
|
|
45
|
+
providers?: Partial<ProviderRegistry>;
|
|
46
|
+
/**
|
|
47
|
+
* When true, publish this identity's mDM prekey bundle once on startup
|
|
48
|
+
* (best-effort, non-fatal) so peers can open an E2EE session without the user
|
|
49
|
+
* manually running mDM_publish_keys first. Fixes the cold-start where mDM_send
|
|
50
|
+
* fails for every recipient because nobody has published keys. Used by the
|
|
51
|
+
* stdio binary; the hosted per-request transport leaves this off (it publishes
|
|
52
|
+
* per authenticated session instead).
|
|
53
|
+
*/
|
|
54
|
+
autoPublishKeys?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface MosaddToolContext {
|
|
58
|
+
options: Required<Pick<MosaddServerOptions, "mode" | "hubUrl">> & MosaddServerOptions;
|
|
59
|
+
/** Resolved channel providers (defaults merged with host-injected ones). */
|
|
60
|
+
providers: ProviderRegistry;
|
|
61
|
+
/** Per-request logger. */
|
|
62
|
+
log: (level: "debug" | "info" | "warn" | "error", msg: string, extra?: unknown) => void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Transport capability a tool requires to function. This is the ONE contract
|
|
67
|
+
* string shared with consuming hosts. e.g. cymru-os gates tools by carrier
|
|
68
|
+
* availability via this flag (cymru-os RFC 0002 D-Bus "Capability-flag bridge").
|
|
69
|
+
* The enum is FINAL and normalized with the cymru-os host; changing these
|
|
70
|
+
* values must be coordinated (Linear label `requires:cymru-input`).
|
|
71
|
+
*
|
|
72
|
+
* - "any" works over any transport, incl. off-grid radio (small,
|
|
73
|
+
* packet-friendly payloads). e.g. text DM.
|
|
74
|
+
* - "radio" radio-specific path (e.g. Codec2 audio frames). Off-grid OK.
|
|
75
|
+
* - "network" needs IP connectivity (server-hosted channels, email, public
|
|
76
|
+
* join links, bridges). NOT available off-grid.
|
|
77
|
+
*/
|
|
78
|
+
export type CapabilityRequirement = "any" | "radio" | "network";
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* MCP tool annotations (spec `ToolAnnotations`, minus `title` which lives on the
|
|
82
|
+
* tool itself). Connector UIs — Claude's included — group and gate tools by these
|
|
83
|
+
* hints: `readOnlyHint: true` lands the tool in the read-only bucket (bulk-approvable),
|
|
84
|
+
* `destructiveHint: true` marks it as destroying/overwriting user data at approval
|
|
85
|
+
* time, and tools with NO annotations all fall into one flat "Other tools" bucket —
|
|
86
|
+
* which is exactly how the mosADD connector looked until 2026-08-26 (84 tools, one
|
|
87
|
+
* undifferentiated list; founder order: fix it).
|
|
88
|
+
*/
|
|
89
|
+
export interface MosaddToolAnnotations {
|
|
90
|
+
/** Tool does not mutate anything. list/get/search/state/metrics class. */
|
|
91
|
+
readOnlyHint?: boolean;
|
|
92
|
+
/** Tool destroys or irreversibly overwrites data (delete/ban/revoke/edit-in-place). */
|
|
93
|
+
destructiveHint?: boolean;
|
|
94
|
+
/** Calling twice with the same args has no additional effect. */
|
|
95
|
+
idempotentHint?: boolean;
|
|
96
|
+
/** Tool reaches outside the mosADD closed world (e.g. sends external email). */
|
|
97
|
+
openWorldHint?: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface MosaddTool<TInput = unknown, TOutput = unknown> {
|
|
101
|
+
/** Tool name. MUST follow the m{MODULE}_{operation} convention. */
|
|
102
|
+
name: string;
|
|
103
|
+
/**
|
|
104
|
+
* Human title for connector UIs (e.g. "Send encrypted DM"). The `name` stays the
|
|
105
|
+
* model-facing handle; this is what a PERSON sees in the permissions list.
|
|
106
|
+
*/
|
|
107
|
+
title?: string;
|
|
108
|
+
/** Short human-readable description. Shown to the model. */
|
|
109
|
+
description: string;
|
|
110
|
+
/** MCP annotations; see MosaddToolAnnotations. Every registered tool SHOULD set them. */
|
|
111
|
+
annotations?: MosaddToolAnnotations;
|
|
112
|
+
/**
|
|
113
|
+
* Transport this tool requires. Consuming hosts read this to gate tools by
|
|
114
|
+
* available carriers. Every tool MUST declare it explicitly.
|
|
115
|
+
*/
|
|
116
|
+
requires: CapabilityRequirement;
|
|
117
|
+
/** Zod schema for input. */
|
|
118
|
+
inputSchema: z.ZodType<TInput>;
|
|
119
|
+
/** Tool handler. */
|
|
120
|
+
handler: (input: TInput, ctx: MosaddToolContext) => Promise<TOutput>;
|
|
121
|
+
}
|
package/src/version.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Single source of truth for the package version at RUNTIME.
|
|
2
|
+
// ⛔ Never hand-type the version anywhere else (server.ts carried a hardcoded
|
|
3
|
+
// "3.0.0-alpha.32" while package.json said alpha.34 — serverInfo lied to every
|
|
4
|
+
// connected client). tsup bundles this JSON import, so the constant always
|
|
5
|
+
// equals the version that was actually published.
|
|
6
|
+
import pkg from "../package.json" with { type: "json" };
|
|
7
|
+
|
|
8
|
+
export const PKG_VERSION: string = (pkg as { version: string }).version;
|
package/src/voice/floor.ts
CHANGED
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mTALK floor control — the half-duplex push-to-talk state machine.
|
|
3
|
-
*
|
|
4
|
-
* This is the kill-feature logic: a walkie-talkie discipline over a real-time
|
|
5
|
-
* room. At most ONE participant transmits at a time (holds "the floor"); others
|
|
6
|
-
* who press PTT are queued FIFO and promoted when the holder releases. An
|
|
7
|
-
* anti-hog timeout auto-releases a holder who never lets go, so one participant
|
|
8
|
-
* can't lock everyone else out.
|
|
9
|
-
*
|
|
10
|
-
* It is a PURE, deterministic reducer over plain data — no I/O, no clock of its
|
|
11
|
-
* own (the caller passes `now`). That makes it:
|
|
12
|
-
* - the reference implementation the authoritative server (mtalk-floor edge
|
|
13
|
-
* function) runs per room, and
|
|
14
|
-
* - trivially unit-testable, and
|
|
15
|
-
* - reusable by an in-memory provider for local/off-grid and for tests.
|
|
16
|
-
*
|
|
17
|
-
* A radio host does NOT use this — there the floor is the physical RF channel
|
|
18
|
-
* (carrier sense + the hardware PTT button). This models the NETWORK floor.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import type { FloorSnapshot } from "@mosadd/providers";
|
|
22
|
-
|
|
23
|
-
/** Serializable floor state for one room. `since` is epoch ms (number) here; the snapshot exposes ISO. */
|
|
24
|
-
export interface FloorState {
|
|
25
|
-
roomId: string;
|
|
26
|
-
holder: string | null;
|
|
27
|
-
/** Epoch ms when the holder acquired the floor; null when idle. */
|
|
28
|
-
sinceMs: number | null;
|
|
29
|
-
queue: string[];
|
|
30
|
-
/**
|
|
31
|
-
* Max ms a holder may keep the floor before auto-release (anti-hog). 0/undefined
|
|
32
|
-
* disables the timeout. Default applied by `createFloorState`.
|
|
33
|
-
*/
|
|
34
|
-
maxHoldMs: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Default anti-hog ceiling: 30s of continuous transmission, like a real PTT radio. */
|
|
38
|
-
export const DEFAULT_MAX_HOLD_MS = 30_000;
|
|
39
|
-
|
|
40
|
-
export function createFloorState(roomId: string, maxHoldMs: number = DEFAULT_MAX_HOLD_MS): FloorState {
|
|
41
|
-
return { roomId, holder: null, sinceMs: null, queue: [], maxHoldMs };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function toSnapshot(state: FloorState): FloorSnapshot {
|
|
45
|
-
return {
|
|
46
|
-
roomId: state.roomId,
|
|
47
|
-
holder: state.holder,
|
|
48
|
-
since: state.sinceMs === null ? null : new Date(state.sinceMs).toISOString(),
|
|
49
|
-
queue: [...state.queue],
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Auto-release the holder if they've exceeded the anti-hog ceiling, promoting
|
|
55
|
-
* the next queued participant. Idempotent; call before any decision so a stale
|
|
56
|
-
* holder never blocks a fresh request. Mutates and returns `state`.
|
|
57
|
-
*/
|
|
58
|
-
export function expireStale(state: FloorState, now: number): FloorState {
|
|
59
|
-
if (
|
|
60
|
-
state.holder !== null &&
|
|
61
|
-
state.maxHoldMs > 0 &&
|
|
62
|
-
state.sinceMs !== null &&
|
|
63
|
-
now - state.sinceMs >= state.maxHoldMs
|
|
64
|
-
) {
|
|
65
|
-
promoteNext(state, now);
|
|
66
|
-
}
|
|
67
|
-
return state;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** Internal: clear the holder and promote the head of the queue (if any). */
|
|
71
|
-
function promoteNext(state: FloorState, now: number): void {
|
|
72
|
-
const next = state.queue.shift();
|
|
73
|
-
if (next !== undefined) {
|
|
74
|
-
state.holder = next;
|
|
75
|
-
state.sinceMs = now;
|
|
76
|
-
} else {
|
|
77
|
-
state.holder = null;
|
|
78
|
-
state.sinceMs = null;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface RequestOutcome {
|
|
83
|
-
granted: boolean;
|
|
84
|
-
/** 1-based queue position when not granted; 0 when granted. */
|
|
85
|
-
position: number;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Press PTT. Grants the floor if idle (or refreshes if the caller already holds
|
|
90
|
-
* it); otherwise enqueues FIFO and reports the caller's position. Mutates state.
|
|
91
|
-
*/
|
|
92
|
-
export function requestFloor(state: FloorState, participant: string, now: number): RequestOutcome {
|
|
93
|
-
expireStale(state, now);
|
|
94
|
-
|
|
95
|
-
if (state.holder === null) {
|
|
96
|
-
state.holder = participant;
|
|
97
|
-
state.sinceMs = now;
|
|
98
|
-
// Acquiring clears any prior queued entry for this participant.
|
|
99
|
-
state.queue = state.queue.filter((p) => p !== participant);
|
|
100
|
-
return { granted: true, position: 0 };
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (state.holder === participant) {
|
|
104
|
-
// Holding PTT down — refresh the hold so a long legitimate transmission
|
|
105
|
-
// isn't cut by the anti-hog timer mid-sentence.
|
|
106
|
-
state.sinceMs = now;
|
|
107
|
-
return { granted: true, position: 0 };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Someone else holds the floor → queue (idempotent: no duplicate entries).
|
|
111
|
-
const existing = state.queue.indexOf(participant);
|
|
112
|
-
if (existing !== -1) return { granted: false, position: existing + 1 };
|
|
113
|
-
state.queue.push(participant);
|
|
114
|
-
return { granted: false, position: state.queue.length };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Release PTT. If the caller holds the floor, it's freed and the next queued
|
|
119
|
-
* participant is promoted. If the caller is only queued, their request is
|
|
120
|
-
* cancelled. No-op otherwise. Mutates state.
|
|
121
|
-
*/
|
|
122
|
-
export function releaseFloor(state: FloorState, participant: string, now: number): FloorState {
|
|
123
|
-
expireStale(state, now);
|
|
124
|
-
|
|
125
|
-
if (state.holder === participant) {
|
|
126
|
-
promoteNext(state, now);
|
|
127
|
-
return state;
|
|
128
|
-
}
|
|
129
|
-
// Not holding — cancel a pending queued request if present.
|
|
130
|
-
state.queue = state.queue.filter((p) => p !== participant);
|
|
131
|
-
return state;
|
|
132
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* mTALK floor control — the half-duplex push-to-talk state machine.
|
|
3
|
+
*
|
|
4
|
+
* This is the kill-feature logic: a walkie-talkie discipline over a real-time
|
|
5
|
+
* room. At most ONE participant transmits at a time (holds "the floor"); others
|
|
6
|
+
* who press PTT are queued FIFO and promoted when the holder releases. An
|
|
7
|
+
* anti-hog timeout auto-releases a holder who never lets go, so one participant
|
|
8
|
+
* can't lock everyone else out.
|
|
9
|
+
*
|
|
10
|
+
* It is a PURE, deterministic reducer over plain data — no I/O, no clock of its
|
|
11
|
+
* own (the caller passes `now`). That makes it:
|
|
12
|
+
* - the reference implementation the authoritative server (mtalk-floor edge
|
|
13
|
+
* function) runs per room, and
|
|
14
|
+
* - trivially unit-testable, and
|
|
15
|
+
* - reusable by an in-memory provider for local/off-grid and for tests.
|
|
16
|
+
*
|
|
17
|
+
* A radio host does NOT use this — there the floor is the physical RF channel
|
|
18
|
+
* (carrier sense + the hardware PTT button). This models the NETWORK floor.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { FloorSnapshot } from "@mosadd/providers";
|
|
22
|
+
|
|
23
|
+
/** Serializable floor state for one room. `since` is epoch ms (number) here; the snapshot exposes ISO. */
|
|
24
|
+
export interface FloorState {
|
|
25
|
+
roomId: string;
|
|
26
|
+
holder: string | null;
|
|
27
|
+
/** Epoch ms when the holder acquired the floor; null when idle. */
|
|
28
|
+
sinceMs: number | null;
|
|
29
|
+
queue: string[];
|
|
30
|
+
/**
|
|
31
|
+
* Max ms a holder may keep the floor before auto-release (anti-hog). 0/undefined
|
|
32
|
+
* disables the timeout. Default applied by `createFloorState`.
|
|
33
|
+
*/
|
|
34
|
+
maxHoldMs: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Default anti-hog ceiling: 30s of continuous transmission, like a real PTT radio. */
|
|
38
|
+
export const DEFAULT_MAX_HOLD_MS = 30_000;
|
|
39
|
+
|
|
40
|
+
export function createFloorState(roomId: string, maxHoldMs: number = DEFAULT_MAX_HOLD_MS): FloorState {
|
|
41
|
+
return { roomId, holder: null, sinceMs: null, queue: [], maxHoldMs };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function toSnapshot(state: FloorState): FloorSnapshot {
|
|
45
|
+
return {
|
|
46
|
+
roomId: state.roomId,
|
|
47
|
+
holder: state.holder,
|
|
48
|
+
since: state.sinceMs === null ? null : new Date(state.sinceMs).toISOString(),
|
|
49
|
+
queue: [...state.queue],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Auto-release the holder if they've exceeded the anti-hog ceiling, promoting
|
|
55
|
+
* the next queued participant. Idempotent; call before any decision so a stale
|
|
56
|
+
* holder never blocks a fresh request. Mutates and returns `state`.
|
|
57
|
+
*/
|
|
58
|
+
export function expireStale(state: FloorState, now: number): FloorState {
|
|
59
|
+
if (
|
|
60
|
+
state.holder !== null &&
|
|
61
|
+
state.maxHoldMs > 0 &&
|
|
62
|
+
state.sinceMs !== null &&
|
|
63
|
+
now - state.sinceMs >= state.maxHoldMs
|
|
64
|
+
) {
|
|
65
|
+
promoteNext(state, now);
|
|
66
|
+
}
|
|
67
|
+
return state;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Internal: clear the holder and promote the head of the queue (if any). */
|
|
71
|
+
function promoteNext(state: FloorState, now: number): void {
|
|
72
|
+
const next = state.queue.shift();
|
|
73
|
+
if (next !== undefined) {
|
|
74
|
+
state.holder = next;
|
|
75
|
+
state.sinceMs = now;
|
|
76
|
+
} else {
|
|
77
|
+
state.holder = null;
|
|
78
|
+
state.sinceMs = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface RequestOutcome {
|
|
83
|
+
granted: boolean;
|
|
84
|
+
/** 1-based queue position when not granted; 0 when granted. */
|
|
85
|
+
position: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Press PTT. Grants the floor if idle (or refreshes if the caller already holds
|
|
90
|
+
* it); otherwise enqueues FIFO and reports the caller's position. Mutates state.
|
|
91
|
+
*/
|
|
92
|
+
export function requestFloor(state: FloorState, participant: string, now: number): RequestOutcome {
|
|
93
|
+
expireStale(state, now);
|
|
94
|
+
|
|
95
|
+
if (state.holder === null) {
|
|
96
|
+
state.holder = participant;
|
|
97
|
+
state.sinceMs = now;
|
|
98
|
+
// Acquiring clears any prior queued entry for this participant.
|
|
99
|
+
state.queue = state.queue.filter((p) => p !== participant);
|
|
100
|
+
return { granted: true, position: 0 };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (state.holder === participant) {
|
|
104
|
+
// Holding PTT down — refresh the hold so a long legitimate transmission
|
|
105
|
+
// isn't cut by the anti-hog timer mid-sentence.
|
|
106
|
+
state.sinceMs = now;
|
|
107
|
+
return { granted: true, position: 0 };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Someone else holds the floor → queue (idempotent: no duplicate entries).
|
|
111
|
+
const existing = state.queue.indexOf(participant);
|
|
112
|
+
if (existing !== -1) return { granted: false, position: existing + 1 };
|
|
113
|
+
state.queue.push(participant);
|
|
114
|
+
return { granted: false, position: state.queue.length };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Release PTT. If the caller holds the floor, it's freed and the next queued
|
|
119
|
+
* participant is promoted. If the caller is only queued, their request is
|
|
120
|
+
* cancelled. No-op otherwise. Mutates state.
|
|
121
|
+
*/
|
|
122
|
+
export function releaseFloor(state: FloorState, participant: string, now: number): FloorState {
|
|
123
|
+
expireStale(state, now);
|
|
124
|
+
|
|
125
|
+
if (state.holder === participant) {
|
|
126
|
+
promoteNext(state, now);
|
|
127
|
+
return state;
|
|
128
|
+
}
|
|
129
|
+
// Not holding — cancel a pending queued request if present.
|
|
130
|
+
state.queue = state.queue.filter((p) => p !== participant);
|
|
131
|
+
return state;
|
|
132
|
+
}
|