@mosadd/mcp 3.0.0-alpha.2 → 3.0.0-alpha.20
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 +120 -0
- package/README.md +141 -116
- package/dist/bin/mcp.js +70 -3
- package/dist/chunk-FZNAGICW.js +2353 -0
- package/dist/index.d.ts +62 -4
- package/dist/index.js +5 -3
- package/package.json +10 -5
- package/src/__tests__/golden-path.test.ts +137 -132
- package/src/__tests__/integration.test.ts +178 -175
- package/src/__tests__/mdm-e2ee.test.ts +212 -149
- package/src/__tests__/mtalk.test.ts +122 -0
- package/src/__tests__/server.test.ts +71 -67
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -55
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +403 -378
- 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 -0
- package/src/providers/supabase-dm.ts +122 -117
- package/src/providers/supabase-voice.ts +91 -0
- package/src/providers/supabase.ts +120 -96
- package/src/server.ts +123 -103
- package/src/tools/actions.ts +162 -0
- package/src/tools/attachments.ts +268 -0
- package/src/tools/capabilities.ts +68 -68
- package/src/tools/embed.ts +108 -0
- package/src/tools/index.ts +72 -37
- package/src/tools/knowledge.ts +191 -0
- package/src/tools/mail-provenance.ts +137 -0
- package/src/tools/mail.ts +378 -89
- package/src/tools/mcall.ts +216 -0
- package/src/tools/mdm-voice.ts +206 -0
- package/src/tools/mdm.ts +451 -379
- package/src/tools/mirc-edge.ts +190 -0
- package/src/tools/mirc-members.ts +188 -177
- package/src/tools/mirc-messages.ts +159 -0
- package/src/tools/mirc.ts +174 -170
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -189
- package/src/tools/mtalk.ts +121 -0
- package/src/tools/murl.ts +195 -0
- package/src/tools/ptt-ingest.ts +91 -0
- package/src/tools/threat.ts +92 -0
- package/src/types.ts +94 -79
- package/src/voice/floor.ts +132 -0
- package/dist/chunk-HD5RLE5K.js +0 -1193
package/src/index.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @mosadd/mcp — Model Context Protocol server for mosadd
|
|
3
|
-
*
|
|
4
|
-
* Exposes mosadd OS modules (m*) as MCP tools to any agent runtime
|
|
5
|
-
* (Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, etc.).
|
|
6
|
-
*
|
|
7
|
-
* License: Apache-2.0
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export { createMosaddServer, defaultProviders } from "./server.js";
|
|
11
|
-
export type {
|
|
12
|
-
MosaddServerOptions,
|
|
13
|
-
MosaddTool,
|
|
14
|
-
MosaddToolContext,
|
|
15
|
-
ProviderRegistry,
|
|
16
|
-
} from "./types.js";
|
|
17
|
-
export { allTools } from "./tools/index.js";
|
|
1
|
+
/**
|
|
2
|
+
* @mosadd/mcp — Model Context Protocol server for mosadd
|
|
3
|
+
*
|
|
4
|
+
* Exposes mosadd OS modules (m*) as MCP tools to any agent runtime
|
|
5
|
+
* (Claude Code, Cursor, Windsurf, Cline, ChatGPT Apps, Lovable, etc.).
|
|
6
|
+
*
|
|
7
|
+
* License: Apache-2.0
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export { createMosaddServer, defaultProviders } from "./server.js";
|
|
11
|
+
export type {
|
|
12
|
+
MosaddServerOptions,
|
|
13
|
+
MosaddTool,
|
|
14
|
+
MosaddToolContext,
|
|
15
|
+
ProviderRegistry,
|
|
16
|
+
} from "./types.js";
|
|
17
|
+
export { allTools } from "./tools/index.js";
|
|
18
|
+
// Per-request session context for the hosted HTTP gateway (mcp.mosadd.com).
|
|
19
|
+
export { runWithSupabaseEnv, type SupabaseEnv } from "./providers/supabase.js";
|
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sentry init for @mosadd/mcp.
|
|
3
|
-
*
|
|
4
|
-
* Optional. The MCP server runs entirely as user-supplied stdio — most
|
|
5
|
-
* operators won't want their tool errors phoning home. We expose an opt-in
|
|
6
|
-
* env var (MOSADD_SENTRY_DSN) so power users / hub-operators can attach.
|
|
7
|
-
*
|
|
8
|
-
* Setup (one-time, hub operators):
|
|
9
|
-
* 1. Create a project in https://papugai.sentry.io named "mosadd-mcp"
|
|
10
|
-
* with platform = Node.
|
|
11
|
-
* 2. Export MOSADD_SENTRY_DSN before running the server.
|
|
12
|
-
*
|
|
13
|
-
* Without the env var, this module is an inert no-op.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const DSN = process.env.MOSADD_SENTRY_DSN;
|
|
17
|
-
const ENV =
|
|
18
|
-
process.env.MOSADD_SENTRY_ENVIRONMENT ??
|
|
19
|
-
process.env.NODE_ENV ??
|
|
20
|
-
"production";
|
|
21
|
-
const RELEASE = process.env.MOSADD_SENTRY_RELEASE;
|
|
22
|
-
|
|
23
|
-
let initialized = false;
|
|
24
|
-
// @sentry/node is an OPTIONAL peer — operators install it only if they opt in
|
|
25
|
-
// via MOSADD_SENTRY_DSN. Typed as `any` + loaded via a non-literal specifier so
|
|
26
|
-
// the package compiles and ships without the dependency present.
|
|
27
|
-
let sentryRef: any = null;
|
|
28
|
-
|
|
29
|
-
export async function initSentry(): Promise<void> {
|
|
30
|
-
if (initialized || !DSN) return;
|
|
31
|
-
try {
|
|
32
|
-
// Lazy, optional import (variable specifier => tsc won't require the module).
|
|
33
|
-
const spec = "@sentry/node";
|
|
34
|
-
const Sentry: any = await import(spec).catch(() => null);
|
|
35
|
-
if (!Sentry) return;
|
|
36
|
-
Sentry.init({
|
|
37
|
-
dsn: DSN,
|
|
38
|
-
environment: ENV,
|
|
39
|
-
release: RELEASE,
|
|
40
|
-
tracesSampleRate: 0.1,
|
|
41
|
-
sendDefaultPii: false,
|
|
42
|
-
beforeSend(event: any) {
|
|
43
|
-
// Strip Authorization header / BYOK env values if they sneak in.
|
|
44
|
-
if (event.extra) {
|
|
45
|
-
for (const k of Object.keys(event.extra)) {
|
|
46
|
-
if (/auth|token|key|secret/i.test(k)) {
|
|
47
|
-
event.extra[k] = "[redacted]";
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return event;
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
sentryRef = Sentry;
|
|
55
|
-
initialized = true;
|
|
56
|
-
} catch {
|
|
57
|
-
// Init failures are non-fatal. MCP servers must keep serving.
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Capture an exception with optional tool context. Safe to call before init —
|
|
63
|
-
* no-ops when DSN is unset.
|
|
64
|
-
*/
|
|
65
|
-
export function captureToolError(
|
|
66
|
-
err: unknown,
|
|
67
|
-
ctx: { tool?: string; user_id?: string } = {}
|
|
68
|
-
): void {
|
|
69
|
-
if (!sentryRef) return;
|
|
70
|
-
try {
|
|
71
|
-
sentryRef.withScope((scope: any) => {
|
|
72
|
-
if (ctx.tool) scope.setTag("tool", ctx.tool);
|
|
73
|
-
if (ctx.user_id) scope.setUser({ id: ctx.user_id });
|
|
74
|
-
sentryRef!.captureException(err);
|
|
75
|
-
});
|
|
76
|
-
} catch {
|
|
77
|
-
// ignore
|
|
78
|
-
}
|
|
79
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Sentry init for @mosadd/mcp.
|
|
3
|
+
*
|
|
4
|
+
* Optional. The MCP server runs entirely as user-supplied stdio — most
|
|
5
|
+
* operators won't want their tool errors phoning home. We expose an opt-in
|
|
6
|
+
* env var (MOSADD_SENTRY_DSN) so power users / hub-operators can attach.
|
|
7
|
+
*
|
|
8
|
+
* Setup (one-time, hub operators):
|
|
9
|
+
* 1. Create a project in https://papugai.sentry.io named "mosadd-mcp"
|
|
10
|
+
* with platform = Node.
|
|
11
|
+
* 2. Export MOSADD_SENTRY_DSN before running the server.
|
|
12
|
+
*
|
|
13
|
+
* Without the env var, this module is an inert no-op.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const DSN = process.env.MOSADD_SENTRY_DSN;
|
|
17
|
+
const ENV =
|
|
18
|
+
process.env.MOSADD_SENTRY_ENVIRONMENT ??
|
|
19
|
+
process.env.NODE_ENV ??
|
|
20
|
+
"production";
|
|
21
|
+
const RELEASE = process.env.MOSADD_SENTRY_RELEASE;
|
|
22
|
+
|
|
23
|
+
let initialized = false;
|
|
24
|
+
// @sentry/node is an OPTIONAL peer — operators install it only if they opt in
|
|
25
|
+
// via MOSADD_SENTRY_DSN. Typed as `any` + loaded via a non-literal specifier so
|
|
26
|
+
// the package compiles and ships without the dependency present.
|
|
27
|
+
let sentryRef: any = null;
|
|
28
|
+
|
|
29
|
+
export async function initSentry(): Promise<void> {
|
|
30
|
+
if (initialized || !DSN) return;
|
|
31
|
+
try {
|
|
32
|
+
// Lazy, optional import (variable specifier => tsc won't require the module).
|
|
33
|
+
const spec = "@sentry/node";
|
|
34
|
+
const Sentry: any = await import(spec).catch(() => null);
|
|
35
|
+
if (!Sentry) return;
|
|
36
|
+
Sentry.init({
|
|
37
|
+
dsn: DSN,
|
|
38
|
+
environment: ENV,
|
|
39
|
+
release: RELEASE,
|
|
40
|
+
tracesSampleRate: 0.1,
|
|
41
|
+
sendDefaultPii: false,
|
|
42
|
+
beforeSend(event: any) {
|
|
43
|
+
// Strip Authorization header / BYOK env values if they sneak in.
|
|
44
|
+
if (event.extra) {
|
|
45
|
+
for (const k of Object.keys(event.extra)) {
|
|
46
|
+
if (/auth|token|key|secret/i.test(k)) {
|
|
47
|
+
event.extra[k] = "[redacted]";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return event;
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
sentryRef = Sentry;
|
|
55
|
+
initialized = true;
|
|
56
|
+
} catch {
|
|
57
|
+
// Init failures are non-fatal. MCP servers must keep serving.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Capture an exception with optional tool context. Safe to call before init —
|
|
63
|
+
* no-ops when DSN is unset.
|
|
64
|
+
*/
|
|
65
|
+
export function captureToolError(
|
|
66
|
+
err: unknown,
|
|
67
|
+
ctx: { tool?: string; user_id?: string } = {}
|
|
68
|
+
): void {
|
|
69
|
+
if (!sentryRef) return;
|
|
70
|
+
try {
|
|
71
|
+
sentryRef.withScope((scope: any) => {
|
|
72
|
+
if (ctx.tool) scope.setTag("tool", ctx.tool);
|
|
73
|
+
if (ctx.user_id) scope.setUser({ id: ctx.user_id });
|
|
74
|
+
sentryRef!.captureException(err);
|
|
75
|
+
});
|
|
76
|
+
} catch {
|
|
77
|
+
// ignore
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Voice/file blob upload helper.
|
|
3
|
+
*
|
|
4
|
+
* The `message-send` contract carries voice/ptt/file bodies via an
|
|
5
|
+
* `attachments[]` array of `{ kind, bucket, path, mime, duration_ms, key_ref }`
|
|
6
|
+
* descriptors. The actual bytes must be uploaded to Supabase Storage FIRST; the
|
|
7
|
+
* message row only references them by `{ bucket, path }`.
|
|
8
|
+
*
|
|
9
|
+
* This module centralises that upload so every channel's voice/file/ptt tool
|
|
10
|
+
* (mDM, mROOM, mIRC) produces an identical attachment descriptor.
|
|
11
|
+
*
|
|
12
|
+
* STATUS: scaffold. The Storage upload path below is real (supabase-js
|
|
13
|
+
* `storage.from(bucket).upload`), but two things are TODO and gated on the
|
|
14
|
+
* Lane A / CTO backend contract:
|
|
15
|
+
* - TODO(Lane A): confirm bucket names (`voice-blobs` for voice/ptt) and the
|
|
16
|
+
* RLS/policy that lets an agent JWT or hub-claim JWT write to them.
|
|
17
|
+
* - TODO(Lane A): mDM voice is CLIENT-ENCRYPTED — the bytes must be sealed
|
|
18
|
+
* (per-recipient, via @mosadd/crypto) before upload and `key_ref` must point
|
|
19
|
+
* at the wrapped content key. Today this helper uploads the bytes as-is and
|
|
20
|
+
* sets key_ref:null. Do NOT use for mDM voice until the seal step lands.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { getSupabase, readSupabaseEnv } from "./supabase.js";
|
|
24
|
+
|
|
25
|
+
export type AttachmentKind = "voice" | "file" | "ptt";
|
|
26
|
+
|
|
27
|
+
/** The descriptor shape `message-send` expects inside `attachments[]`. */
|
|
28
|
+
export interface AttachmentDescriptor {
|
|
29
|
+
kind: AttachmentKind;
|
|
30
|
+
bucket: string;
|
|
31
|
+
path: string;
|
|
32
|
+
mime: string;
|
|
33
|
+
duration_ms?: number | null;
|
|
34
|
+
/** Reference to the wrapped content-encryption key (E2EE channels only). */
|
|
35
|
+
key_ref?: string | null;
|
|
36
|
+
/** Original filename, for `kind:'file'`. */
|
|
37
|
+
filename?: string | null;
|
|
38
|
+
size_bytes?: number | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface UploadBlobArgs {
|
|
42
|
+
kind: AttachmentKind;
|
|
43
|
+
/** Raw bytes to upload. Caller is responsible for any E2EE sealing first. */
|
|
44
|
+
bytes: Uint8Array;
|
|
45
|
+
mime: string;
|
|
46
|
+
/** Storage bucket. Defaults: voice/ptt → 'voice-blobs', file → 'file-blobs'. */
|
|
47
|
+
bucket?: string;
|
|
48
|
+
/** Sub-path prefix inside the bucket (e.g. the thread id). */
|
|
49
|
+
prefix?: string;
|
|
50
|
+
duration_ms?: number | null;
|
|
51
|
+
filename?: string | null;
|
|
52
|
+
/** Reference to the wrapped content key, when the caller pre-sealed the bytes. */
|
|
53
|
+
key_ref?: string | null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function defaultBucket(kind: AttachmentKind): string {
|
|
57
|
+
// TODO(Lane A): confirm canonical bucket names + write policy for agent JWTs.
|
|
58
|
+
return kind === "file" ? "file-blobs" : "voice-blobs";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function randomId(): string {
|
|
62
|
+
// crypto.randomUUID is available on Node >=18 (package engines floor).
|
|
63
|
+
return globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Upload bytes to Supabase Storage and return the attachment descriptor to embed
|
|
68
|
+
* in `message-send`'s `attachments[]`.
|
|
69
|
+
*/
|
|
70
|
+
export async function uploadBlob(args: UploadBlobArgs): Promise<AttachmentDescriptor> {
|
|
71
|
+
readSupabaseEnv();
|
|
72
|
+
const sb = getSupabase();
|
|
73
|
+
const bucket = args.bucket ?? defaultBucket(args.kind);
|
|
74
|
+
const ext = args.filename?.split(".").pop() ?? mimeExt(args.mime);
|
|
75
|
+
const path = `${args.prefix ? args.prefix.replace(/[^a-zA-Z0-9._/-]/g, "-") + "/" : ""}${randomId()}${ext ? "." + ext : ""}`;
|
|
76
|
+
|
|
77
|
+
const { error } = await sb.storage.from(bucket).upload(path, args.bytes, {
|
|
78
|
+
contentType: args.mime,
|
|
79
|
+
upsert: false,
|
|
80
|
+
});
|
|
81
|
+
if (error) {
|
|
82
|
+
throw new Error(`blob upload to ${bucket}/${path} failed: ${error.message}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
kind: args.kind,
|
|
87
|
+
bucket,
|
|
88
|
+
path,
|
|
89
|
+
mime: args.mime,
|
|
90
|
+
duration_ms: args.duration_ms ?? null,
|
|
91
|
+
key_ref: args.key_ref ?? null,
|
|
92
|
+
filename: args.filename ?? null,
|
|
93
|
+
size_bytes: args.bytes.byteLength,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function mimeExt(mime: string): string {
|
|
98
|
+
const map: Record<string, string> = {
|
|
99
|
+
"audio/ogg": "ogg",
|
|
100
|
+
"audio/mpeg": "mp3",
|
|
101
|
+
"audio/mp4": "m4a",
|
|
102
|
+
"audio/wav": "wav",
|
|
103
|
+
"audio/webm": "webm",
|
|
104
|
+
};
|
|
105
|
+
return map[mime] ?? "";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Decode a base64 / data-URL string into bytes. Agents hand us audio/file
|
|
110
|
+
* content as base64 (MCP is JSON-only — no binary channel), so every voice/file
|
|
111
|
+
* tool funnels through this.
|
|
112
|
+
*/
|
|
113
|
+
export function decodeBase64Body(b64: string): Uint8Array {
|
|
114
|
+
const cleaned = b64.includes(",") && b64.startsWith("data:") ? b64.slice(b64.indexOf(",") + 1) : b64;
|
|
115
|
+
return new Uint8Array(Buffer.from(cleaned, "base64"));
|
|
116
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InMemoryVoiceProvider — a self-contained mTALK provider for local mode,
|
|
3
|
+
* off-grid single-process use, and tests. It runs the reference floor-control
|
|
4
|
+
* FSM (floor.ts) in process, so the PTT discipline is fully exercised without
|
|
5
|
+
* any media server. It does NOT mint real media credentials (joinTicket returns
|
|
6
|
+
* a local stub) — real audio transport is the network/radio provider's job.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
VoiceProvider,
|
|
11
|
+
FloorSnapshot,
|
|
12
|
+
FloorRequestResult,
|
|
13
|
+
VoiceJoinTicket,
|
|
14
|
+
} from "@mosadd/providers";
|
|
15
|
+
import {
|
|
16
|
+
createFloorState,
|
|
17
|
+
requestFloor as fsmRequest,
|
|
18
|
+
releaseFloor as fsmRelease,
|
|
19
|
+
expireStale,
|
|
20
|
+
toSnapshot,
|
|
21
|
+
type FloorState,
|
|
22
|
+
} from "../voice/floor.js";
|
|
23
|
+
|
|
24
|
+
export class InMemoryVoiceProvider implements VoiceProvider {
|
|
25
|
+
private readonly rooms = new Map<string, FloorState>();
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly myId: string,
|
|
29
|
+
/** Injectable clock for deterministic tests; defaults to wall time. */
|
|
30
|
+
private readonly clock: () => number = () => Date.now(),
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
async selfId(): Promise<string> {
|
|
34
|
+
return this.myId;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async createRoom(label?: string): Promise<{ roomId: string }> {
|
|
38
|
+
const roomId = label ? `mtalk:${label}` : `mtalk:${this.myId}`;
|
|
39
|
+
if (!this.rooms.has(roomId)) this.rooms.set(roomId, createFloorState(roomId));
|
|
40
|
+
return { roomId };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async joinTicket(roomId: string): Promise<VoiceJoinTicket> {
|
|
44
|
+
this.ensure(roomId);
|
|
45
|
+
return { token: `local:${this.myId}`, url: "local://mtalk", identity: this.myId, roomId };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async requestFloor(roomId: string): Promise<FloorRequestResult> {
|
|
49
|
+
const state = this.ensure(roomId);
|
|
50
|
+
const outcome = fsmRequest(state, this.myId, this.clock());
|
|
51
|
+
return { granted: outcome.granted, position: outcome.position, snapshot: toSnapshot(state) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async releaseFloor(roomId: string): Promise<FloorSnapshot> {
|
|
55
|
+
const state = this.ensure(roomId);
|
|
56
|
+
fsmRelease(state, this.myId, this.clock());
|
|
57
|
+
return toSnapshot(state);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async floorState(roomId: string): Promise<FloorSnapshot> {
|
|
61
|
+
const state = this.ensure(roomId);
|
|
62
|
+
expireStale(state, this.clock());
|
|
63
|
+
return toSnapshot(state);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Shared backend handle so multiple participants' providers see one room. */
|
|
67
|
+
shareRoomsWith(other: InMemoryVoiceProvider): void {
|
|
68
|
+
(other as unknown as { rooms: Map<string, FloorState> }).rooms = this.rooms;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private ensure(roomId: string): FloorState {
|
|
72
|
+
let state = this.rooms.get(roomId);
|
|
73
|
+
if (!state) {
|
|
74
|
+
state = createFloorState(roomId);
|
|
75
|
+
this.rooms.set(roomId, state);
|
|
76
|
+
}
|
|
77
|
+
return state;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -1,117 +1,122 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SupabaseDmProvider — the default (network) DmProvider.
|
|
3
|
-
*
|
|
4
|
-
* Implements the transport-agnostic `DmProvider` contract from
|
|
5
|
-
* `@mosadd/providers` over the
|
|
6
|
-
* (`message-send` / `message-list`). This is the strangler-fig network path;
|
|
7
|
-
* a carrier-aware host injects a different DmProvider for radio.
|
|
8
|
-
*
|
|
9
|
-
* The provider receives OPAQUE bytes and base64-wraps them into the
|
|
10
|
-
* `encrypted_payload` string the Edge Function expects — it does not know or
|
|
11
|
-
* care that the bytes are (for alpha) a plaintext `mosadd.chat.v1` envelope.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type {
|
|
15
|
-
DmProvider,
|
|
16
|
-
DmSendArgs,
|
|
17
|
-
DmSendResult,
|
|
18
|
-
DmListArgs,
|
|
19
|
-
DmListResult,
|
|
20
|
-
} from "@mosadd/providers";
|
|
21
|
-
import { getSupabase, invokeFunction, readSupabaseEnv } from "./supabase.js";
|
|
22
|
-
|
|
23
|
-
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
24
|
-
|
|
25
|
-
export class SupabaseDmProvider implements DmProvider {
|
|
26
|
-
private cachedSelfId: string | null = null;
|
|
27
|
-
|
|
28
|
-
async selfId(): Promise<string> {
|
|
29
|
-
if (this.cachedSelfId) return this.cachedSelfId;
|
|
30
|
-
readSupabaseEnv(); // fail fast with an actionable error if BYOK env missing
|
|
31
|
-
const sb = getSupabase();
|
|
32
|
-
const { data: u, error: ue } = await sb.auth.getUser();
|
|
33
|
-
if (ue || !u?.user) {
|
|
34
|
-
throw new Error(
|
|
35
|
-
"Unable to resolve current user. Ensure MOSADD_USER_JWT is set to a valid Supabase session token.",
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
const { data: identity, error } = await sb
|
|
39
|
-
.from("identities")
|
|
40
|
-
.select("id")
|
|
41
|
-
.eq("user_id", u.user.id)
|
|
42
|
-
.maybeSingle();
|
|
43
|
-
if (error || !identity) {
|
|
44
|
-
throw new Error("Current user has no mosadd identity row. Sign in to mosadd.com first.");
|
|
45
|
-
}
|
|
46
|
-
this.cachedSelfId = identity.id as string;
|
|
47
|
-
return this.cachedSelfId;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async send(args: DmSendArgs): Promise<DmSendResult> {
|
|
51
|
-
const encrypted_payload = Buffer.from(args.payload).toString("base64");
|
|
52
|
-
type MessageSendResponse = { message?: { id: string; created_at: string } };
|
|
53
|
-
const data = await invokeFunction<MessageSendResponse>("message-send", {
|
|
54
|
-
space_id: "dm",
|
|
55
|
-
thread_id: args.threadId,
|
|
56
|
-
encrypted_payload,
|
|
57
|
-
message_type: "text",
|
|
58
|
-
protocol_version: PROTOCOL_VERSION,
|
|
59
|
-
recipient_account_id: args.to,
|
|
60
|
-
reply_to_id: args.replyToId ?? null,
|
|
61
|
-
});
|
|
62
|
-
if (!data?.message?.id) {
|
|
63
|
-
throw new Error("message-send returned no message id");
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
id: data.message.id,
|
|
67
|
-
deliveredAt: data.message.created_at ?? new Date().toISOString(),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async publishPrekeyBundle(bundle: Uint8Array): Promise<void> {
|
|
72
|
-
// Network key directory. Server-side fn (`prekey-bundle-publish`) is the
|
|
73
|
-
//
|
|
74
|
-
// caller's identity (resolved from the JWT). mosadd-os just ships bytes.
|
|
75
|
-
await invokeFunction<{ ok: boolean }>("prekey-bundle-publish", {
|
|
76
|
-
bundle: Buffer.from(bundle).toString("base64"),
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async fetchPrekeyBundle(peerId: string): Promise<Uint8Array | null> {
|
|
81
|
-
type FetchResponse = { bundle?: string | null };
|
|
82
|
-
const data = await invokeFunction<FetchResponse>("prekey-bundle-fetch", {
|
|
83
|
-
peer_id: peerId,
|
|
84
|
-
});
|
|
85
|
-
if (!data?.bundle) return null;
|
|
86
|
-
return new Uint8Array(Buffer.from(data.bundle, "base64"));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async list(args: DmListArgs): Promise<DmListResult> {
|
|
90
|
-
type MessageListResponse = {
|
|
91
|
-
messages?: Array<{
|
|
92
|
-
id: string;
|
|
93
|
-
sender_identity_id: string;
|
|
94
|
-
thread_id: string;
|
|
95
|
-
encrypted_payload: string;
|
|
96
|
-
created_at: string;
|
|
97
|
-
}>;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* SupabaseDmProvider — the default (network) DmProvider.
|
|
3
|
+
*
|
|
4
|
+
* Implements the transport-agnostic `DmProvider` contract from
|
|
5
|
+
* `@mosadd/providers` over the mosadd Supabase Edge Functions
|
|
6
|
+
* (`message-send` / `message-list`). This is the strangler-fig network path;
|
|
7
|
+
* a carrier-aware host injects a different DmProvider for radio.
|
|
8
|
+
*
|
|
9
|
+
* The provider receives OPAQUE bytes and base64-wraps them into the
|
|
10
|
+
* `encrypted_payload` string the Edge Function expects — it does not know or
|
|
11
|
+
* care that the bytes are (for alpha) a plaintext `mosadd.chat.v1` envelope.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
DmProvider,
|
|
16
|
+
DmSendArgs,
|
|
17
|
+
DmSendResult,
|
|
18
|
+
DmListArgs,
|
|
19
|
+
DmListResult,
|
|
20
|
+
} from "@mosadd/providers";
|
|
21
|
+
import { getSupabase, invokeFunction, readSupabaseEnv } from "./supabase.js";
|
|
22
|
+
|
|
23
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
24
|
+
|
|
25
|
+
export class SupabaseDmProvider implements DmProvider {
|
|
26
|
+
private cachedSelfId: string | null = null;
|
|
27
|
+
|
|
28
|
+
async selfId(): Promise<string> {
|
|
29
|
+
if (this.cachedSelfId) return this.cachedSelfId;
|
|
30
|
+
readSupabaseEnv(); // fail fast with an actionable error if BYOK env missing
|
|
31
|
+
const sb = getSupabase();
|
|
32
|
+
const { data: u, error: ue } = await sb.auth.getUser();
|
|
33
|
+
if (ue || !u?.user) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
"Unable to resolve current user. Ensure MOSADD_USER_JWT is set to a valid Supabase session token.",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
const { data: identity, error } = await sb
|
|
39
|
+
.from("identities")
|
|
40
|
+
.select("id")
|
|
41
|
+
.eq("user_id", u.user.id)
|
|
42
|
+
.maybeSingle();
|
|
43
|
+
if (error || !identity) {
|
|
44
|
+
throw new Error("Current user has no mosadd identity row. Sign in to mosadd.com first.");
|
|
45
|
+
}
|
|
46
|
+
this.cachedSelfId = identity.id as string;
|
|
47
|
+
return this.cachedSelfId;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async send(args: DmSendArgs): Promise<DmSendResult> {
|
|
51
|
+
const encrypted_payload = Buffer.from(args.payload).toString("base64");
|
|
52
|
+
type MessageSendResponse = { message?: { id: string; created_at: string } };
|
|
53
|
+
const data = await invokeFunction<MessageSendResponse>("message-send", {
|
|
54
|
+
space_id: "dm",
|
|
55
|
+
thread_id: args.threadId,
|
|
56
|
+
encrypted_payload,
|
|
57
|
+
message_type: "text",
|
|
58
|
+
protocol_version: PROTOCOL_VERSION,
|
|
59
|
+
recipient_account_id: args.to,
|
|
60
|
+
reply_to_id: args.replyToId ?? null,
|
|
61
|
+
});
|
|
62
|
+
if (!data?.message?.id) {
|
|
63
|
+
throw new Error("message-send returned no message id");
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
id: data.message.id,
|
|
67
|
+
deliveredAt: data.message.created_at ?? new Date().toISOString(),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async publishPrekeyBundle(bundle: Uint8Array): Promise<void> {
|
|
72
|
+
// Network key directory. Server-side fn (`prekey-bundle-publish`) is the
|
|
73
|
+
// the mosadd hub's responsibility — it stores the opaque bundle keyed by the
|
|
74
|
+
// caller's identity (resolved from the JWT). mosadd-os just ships bytes.
|
|
75
|
+
await invokeFunction<{ ok: boolean }>("prekey-bundle-publish", {
|
|
76
|
+
bundle: Buffer.from(bundle).toString("base64"),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async fetchPrekeyBundle(peerId: string): Promise<Uint8Array | null> {
|
|
81
|
+
type FetchResponse = { bundle?: string | null };
|
|
82
|
+
const data = await invokeFunction<FetchResponse>("prekey-bundle-fetch", {
|
|
83
|
+
peer_id: peerId,
|
|
84
|
+
});
|
|
85
|
+
if (!data?.bundle) return null;
|
|
86
|
+
return new Uint8Array(Buffer.from(data.bundle, "base64"));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async list(args: DmListArgs): Promise<DmListResult> {
|
|
90
|
+
type MessageListResponse = {
|
|
91
|
+
messages?: Array<{
|
|
92
|
+
id: string;
|
|
93
|
+
sender_identity_id: string;
|
|
94
|
+
thread_id: string;
|
|
95
|
+
encrypted_payload: string;
|
|
96
|
+
created_at: string;
|
|
97
|
+
}>;
|
|
98
|
+
cursor?: string | null;
|
|
99
|
+
has_more?: boolean;
|
|
100
|
+
next_before?: string | null;
|
|
101
|
+
};
|
|
102
|
+
// message-list paginates history via `before` (a created_at marker) and returns
|
|
103
|
+
// the next marker as `cursor` — NOT `cursor` in / `next_cursor` out.
|
|
104
|
+
const data = await invokeFunction<MessageListResponse>("message-list", {
|
|
105
|
+
space_id: "dm",
|
|
106
|
+
thread_id: args.threadId,
|
|
107
|
+
limit: args.limit ?? 50,
|
|
108
|
+
before: args.cursor,
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
messages: (data?.messages ?? []).map((m) => ({
|
|
112
|
+
id: m.id,
|
|
113
|
+
senderId: m.sender_identity_id,
|
|
114
|
+
payload: new Uint8Array(Buffer.from(m.encrypted_payload, "base64")),
|
|
115
|
+
timestamp: m.created_at,
|
|
116
|
+
threadId: m.thread_id,
|
|
117
|
+
})),
|
|
118
|
+
// history mode (`before`) returns `next_before`; incremental (`since`) returns `cursor`+`has_more`.
|
|
119
|
+
nextCursor: data?.next_before ?? (data?.has_more ? (data?.cursor ?? null) : null),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|