@mosadd/mcp 3.0.0-alpha.32 → 3.0.0-alpha.33
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 +74 -0
- package/README.md +11 -11
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-KX34DDJB.js → chunk-IWLN5EO5.js} +614 -134
- package/dist/index.js +1 -1
- package/package.json +15 -15
- package/src/__tests__/golden-path.test.ts +137 -137
- package/src/__tests__/integration.test.ts +179 -179
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -212
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +141 -0
- package/src/__tests__/server.test.ts +71 -71
- package/src/__tests__/tool-count-consistency.test.ts +121 -1
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -145
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +502 -502
- package/src/index.ts +19 -19
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +116 -116
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +122 -122
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +148 -148
- package/src/tools/actions.ts +162 -162
- package/src/tools/attachments.ts +268 -268
- package/src/tools/capabilities.ts +68 -68
- package/src/tools/embed.ts +108 -108
- package/src/tools/index.ts +173 -104
- package/src/tools/knowledge-graph.ts +178 -0
- package/src/tools/knowledge.ts +319 -191
- package/src/tools/mail-agentbox.ts +147 -0
- package/src/tools/mail-aliases.ts +25 -25
- package/src/tools/mail-provenance.ts +135 -135
- package/src/tools/mail.ts +381 -381
- package/src/tools/mdm.ts +476 -479
- package/src/tools/mirc-edge.ts +190 -190
- package/src/tools/mirc-members.ts +2 -2
- package/src/tools/mirc-messages.ts +155 -155
- package/src/tools/mirc.ts +2 -2
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +121 -121
- package/src/tools/murl.ts +4 -2
- package/src/tools/presence.ts +208 -0
- package/src/types.ts +94 -94
- package/src/voice/floor.ts +132 -132
- package/LICENSE +0 -201
- package/dist/bin/login.d.ts +0 -13
- package/dist/bin/mcp.d.ts +0 -1
- package/dist/index.d.ts +0 -266
package/src/types.ts
CHANGED
|
@@ -1,94 +1,94 @@
|
|
|
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
|
-
export interface MosaddTool<TInput = unknown, TOutput = unknown> {
|
|
81
|
-
/** Tool name. MUST follow the m{MODULE}_{operation} convention. */
|
|
82
|
-
name: string;
|
|
83
|
-
/** Short human-readable description. Shown to the model. */
|
|
84
|
-
description: string;
|
|
85
|
-
/**
|
|
86
|
-
* Transport this tool requires. Consuming hosts read this to gate tools by
|
|
87
|
-
* available carriers. Every tool MUST declare it explicitly.
|
|
88
|
-
*/
|
|
89
|
-
requires: CapabilityRequirement;
|
|
90
|
-
/** Zod schema for input. */
|
|
91
|
-
inputSchema: z.ZodType<TInput>;
|
|
92
|
-
/** Tool handler. */
|
|
93
|
-
handler: (input: TInput, ctx: MosaddToolContext) => Promise<TOutput>;
|
|
94
|
-
}
|
|
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
|
+
export interface MosaddTool<TInput = unknown, TOutput = unknown> {
|
|
81
|
+
/** Tool name. MUST follow the m{MODULE}_{operation} convention. */
|
|
82
|
+
name: string;
|
|
83
|
+
/** Short human-readable description. Shown to the model. */
|
|
84
|
+
description: string;
|
|
85
|
+
/**
|
|
86
|
+
* Transport this tool requires. Consuming hosts read this to gate tools by
|
|
87
|
+
* available carriers. Every tool MUST declare it explicitly.
|
|
88
|
+
*/
|
|
89
|
+
requires: CapabilityRequirement;
|
|
90
|
+
/** Zod schema for input. */
|
|
91
|
+
inputSchema: z.ZodType<TInput>;
|
|
92
|
+
/** Tool handler. */
|
|
93
|
+
handler: (input: TInput, ctx: MosaddToolContext) => Promise<TOutput>;
|
|
94
|
+
}
|
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
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
Apache License
|
|
2
|
-
Version 2.0, January 2004
|
|
3
|
-
http://www.apache.org/licenses/
|
|
4
|
-
|
|
5
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
-
|
|
7
|
-
1. Definitions.
|
|
8
|
-
|
|
9
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
-
|
|
12
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
-
the copyright owner that is granting the License.
|
|
14
|
-
|
|
15
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
-
other entities that control, are controlled by, or are under common
|
|
17
|
-
control with that entity. For the purposes of this definition,
|
|
18
|
-
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
-
direction or management of such entity, whether by contract or
|
|
20
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
-
|
|
23
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
-
exercising permissions granted by this License.
|
|
25
|
-
|
|
26
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
-
including but not limited to software source code, documentation
|
|
28
|
-
source, and configuration files.
|
|
29
|
-
|
|
30
|
-
"Object" form shall mean any form resulting from mechanical
|
|
31
|
-
transformation or translation of a Source form, including but
|
|
32
|
-
not limited to compiled object code, generated documentation,
|
|
33
|
-
and conversions to other media types.
|
|
34
|
-
|
|
35
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
-
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
-
(an example is provided in the Appendix below).
|
|
39
|
-
|
|
40
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
-
form, that is based on (or derived from) the Work and for which the
|
|
42
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
-
of this License, Derivative Works shall not include works that remain
|
|
45
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
-
the Work and Derivative Works thereof.
|
|
47
|
-
|
|
48
|
-
"Contribution" shall mean any work of authorship, including
|
|
49
|
-
the original version of the Work and any modifications or additions
|
|
50
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
-
means any form of electronic, verbal, or written communication sent
|
|
55
|
-
to the Licensor or its representatives, including but not limited to
|
|
56
|
-
communication on electronic mailing lists, source code control systems,
|
|
57
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
-
excluding communication that is conspicuously marked or otherwise
|
|
60
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
-
|
|
62
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
-
subsequently incorporated within the Work.
|
|
65
|
-
|
|
66
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
-
Work and such Derivative Works in Source or Object form.
|
|
72
|
-
|
|
73
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
-
(except as stated in this section) patent license to make, have made,
|
|
77
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
-
where such license applies only to those patent claims licensable
|
|
79
|
-
by such Contributor that are necessarily infringed by their
|
|
80
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
-
institute patent litigation against any entity (including a
|
|
83
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
-
or contributory patent infringement, then any patent licenses
|
|
86
|
-
granted to You under this License for that Work shall terminate
|
|
87
|
-
as of the date such litigation is filed.
|
|
88
|
-
|
|
89
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
-
modifications, and in Source or Object form, provided that You
|
|
92
|
-
meet the following conditions:
|
|
93
|
-
|
|
94
|
-
(a) You must give any other recipients of the Work or
|
|
95
|
-
Derivative Works a copy of this License; and
|
|
96
|
-
|
|
97
|
-
(b) You must cause any modified files to carry prominent notices
|
|
98
|
-
stating that You changed the files; and
|
|
99
|
-
|
|
100
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
-
that You distribute, all copyright, patent, trademark, and
|
|
102
|
-
attribution notices from the Source form of the Work,
|
|
103
|
-
excluding those notices that do not pertain to any part of
|
|
104
|
-
the Derivative Works; and
|
|
105
|
-
|
|
106
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
-
distribution, then any Derivative Works that You distribute must
|
|
108
|
-
include a readable copy of the attribution notices contained
|
|
109
|
-
within such NOTICE file, excluding those notices that do not
|
|
110
|
-
pertain to any part of the Derivative Works, in at least one
|
|
111
|
-
of the following places: within a NOTICE text file distributed
|
|
112
|
-
as part of the Derivative Works; within the Source form or
|
|
113
|
-
documentation, if provided along with the Derivative Works; or,
|
|
114
|
-
within a display generated by the Derivative Works, if and
|
|
115
|
-
wherever such third-party notices normally appear. The contents
|
|
116
|
-
of the NOTICE file are for informational purposes only and
|
|
117
|
-
do not modify the License. You may add Your own attribution
|
|
118
|
-
notices within Derivative Works that You distribute, alongside
|
|
119
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
-
that such additional attribution notices cannot be construed
|
|
121
|
-
as modifying the License.
|
|
122
|
-
|
|
123
|
-
You may add Your own copyright statement to Your modifications and
|
|
124
|
-
may provide additional or different license terms and conditions
|
|
125
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
-
the conditions stated in this License.
|
|
129
|
-
|
|
130
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
-
this License, without any additional terms or conditions.
|
|
134
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
-
the terms of any separate license agreement you may have executed
|
|
136
|
-
with Licensor regarding such Contributions.
|
|
137
|
-
|
|
138
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
-
except as required for describing the origin of the Work and
|
|
141
|
-
reproducing the content of the NOTICE file.
|
|
142
|
-
|
|
143
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
-
implied, including, without limitation, any warranties or conditions
|
|
148
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
-
appropriateness of using or redistributing the Work and assume any
|
|
151
|
-
risks associated with Your exercise of permissions under this License.
|
|
152
|
-
|
|
153
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
-
unless required by applicable law (such as deliberate and grossly
|
|
156
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
-
liable to You for damages, including any direct, indirect, special,
|
|
158
|
-
incidental, or consequential damages of any character arising as a
|
|
159
|
-
result of this License or out of the use or inability to use the
|
|
160
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
-
other commercial damages or losses), even if such Contributor
|
|
163
|
-
has been advised of the possibility of such damages.
|
|
164
|
-
|
|
165
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
-
the Work or Derivative Works thereof, You may accept and charge a
|
|
167
|
-
fee for acceptance of support, warranty, indemnity, or other liability
|
|
168
|
-
obligations and/or rights consistent with this License. However, in
|
|
169
|
-
accepting such obligations, You may act only on Your own behalf and
|
|
170
|
-
on Your sole responsibility, not on behalf of any other Contributor,
|
|
171
|
-
and only if You agree to indemnify, defend, and hold each Contributor
|
|
172
|
-
harmless for any liability incurred by, or claims asserted against,
|
|
173
|
-
such Contributor by reason of your accepting any such warranty or
|
|
174
|
-
additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
|
177
|
-
|
|
178
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
-
|
|
180
|
-
To apply the Apache License to your work, attach the following
|
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
-
replaced with your own identifying information. (Don't include
|
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
-
comment syntax for the file format. We also recommend that a
|
|
185
|
-
file or class name and description of purpose be included on the
|
|
186
|
-
same "printed page" as the copyright notice for easier
|
|
187
|
-
identification within third-party archives.
|
|
188
|
-
|
|
189
|
-
Copyright 2026 mosadd contributors
|
|
190
|
-
|
|
191
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
-
you may not use this file except in compliance with the License.
|
|
193
|
-
You may obtain a copy of the License at
|
|
194
|
-
|
|
195
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
-
|
|
197
|
-
Unless required by applicable law or agreed to in writing, software
|
|
198
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
-
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|