@mosadd/mcp 3.0.0-alpha.54 → 3.0.0-alpha.55
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 +24 -0
- package/README.md +150 -150
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-3PUOM4R5.js → chunk-IWUGKLDX.js} +54 -20
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/__tests__/blob-upload.test.ts +93 -93
- package/src/__tests__/channel-e2ee.test.ts +62 -62
- package/src/__tests__/flota-jawnie.test.ts +244 -0
- package/src/__tests__/golden-path.test.ts +139 -139
- package/src/__tests__/integration.test.ts +182 -182
- package/src/__tests__/mdm-e2ee.test.ts +212 -212
- package/src/__tests__/mdm-mcp-readable.test.ts +390 -386
- package/src/__tests__/mirc-invite.test.ts +112 -112
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/server.test.ts +71 -71
- package/src/__tests__/tool-count-consistency.test.ts +199 -199
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -145
- package/src/config.ts +66 -66
- package/src/crypto/channel-e2ee.ts +288 -288
- package/src/crypto/mdm-session.ts +842 -842
- package/src/index.ts +19 -19
- package/src/observability/sentry.ts +79 -79
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +202 -202
- package/src/providers/supabase-voice.ts +91 -91
- package/src/server.ts +195 -195
- package/src/tools/actions.ts +166 -166
- package/src/tools/attachments.ts +276 -276
- package/src/tools/capabilities.ts +70 -70
- package/src/tools/channel-payload.ts +69 -0
- package/src/tools/embed.ts +110 -110
- package/src/tools/index.ts +181 -181
- package/src/tools/knowledge.ts +344 -344
- package/src/tools/mail-aliases.ts +25 -25
- package/src/tools/mail-provenance.ts +137 -137
- package/src/tools/mail.ts +403 -403
- package/src/tools/mdm-as-agent.ts +126 -126
- package/src/tools/mdm.ts +26 -11
- package/src/tools/mirc-edge.ts +196 -196
- package/src/tools/mirc-members.ts +287 -287
- package/src/tools/mirc-messages.ts +9 -13
- package/src/tools/mirc.ts +308 -308
- package/src/tools/mroom-messages.ts +4 -12
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +131 -131
- package/src/tools/presence.ts +442 -442
- package/src/tools/voice-format.ts +28 -28
- package/src/types.ts +121 -121
- package/src/voice/floor.ts +132 -132
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* comms_capabilities — capability discovery for consuming hosts.
|
|
3
|
-
*
|
|
4
|
-
* THE contract surface between mosadd-os and carrier-aware hosts (e.g.
|
|
5
|
-
* cymru-os, which filters tools by available transports — see cymru-os RFC
|
|
6
|
-
* 0002 D-Bus "Capability-flag bridge"). A host spawns `@mosadd/mcp` over stdio
|
|
7
|
-
* and calls `comms_capabilities` once to learn which tools are usable on which
|
|
8
|
-
* carrier, then gates the tool list accordingly (e.g. off-grid radio →
|
|
9
|
-
* only `requires: "any" | "radio"` tools are offered).
|
|
10
|
-
*
|
|
11
|
-
* `requires: "any"` here too — discovery must work on any transport, including
|
|
12
|
-
* off-grid radio, otherwise a host couldn't learn what it's allowed to call.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { z } from "zod";
|
|
16
|
-
import type { CapabilityRequirement, MosaddTool } from "../types.js";
|
|
17
|
-
|
|
18
|
-
export interface CapabilityEntry {
|
|
19
|
-
name: string;
|
|
20
|
-
requires: CapabilityRequirement;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CapabilityManifest {
|
|
24
|
-
/** Stable contract version. Bump on any enum/shape change (requires:cymru-input). */
|
|
25
|
-
contract_version: string;
|
|
26
|
-
/** The finalized capability enum, echoed so hosts can validate against it. */
|
|
27
|
-
requirements: CapabilityRequirement[];
|
|
28
|
-
/** Flat list of every tool and its transport requirement. */
|
|
29
|
-
tools: CapabilityEntry[];
|
|
30
|
-
/** Pre-grouped by requirement for cheap host-side filtering. */
|
|
31
|
-
by_requirement: Record<CapabilityRequirement, string[]>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const CONTRACT_VERSION = "1.0.0";
|
|
35
|
-
const ALL_REQUIREMENTS: CapabilityRequirement[] = ["any", "radio", "network"];
|
|
36
|
-
|
|
37
|
-
/** Build the capability manifest from a tool list. Pure, host-agnostic. */
|
|
38
|
-
export function buildCapabilityManifest(tools: MosaddTool[]): CapabilityManifest {
|
|
39
|
-
const entries: CapabilityEntry[] = tools.map((t) => ({ name: t.name, requires: t.requires }));
|
|
40
|
-
const by_requirement: Record<CapabilityRequirement, string[]> = {
|
|
41
|
-
any: [],
|
|
42
|
-
radio: [],
|
|
43
|
-
network: [],
|
|
44
|
-
};
|
|
45
|
-
for (const e of entries) by_requirement[e.requires].push(e.name);
|
|
46
|
-
return {
|
|
47
|
-
contract_version: CONTRACT_VERSION,
|
|
48
|
-
requirements: ALL_REQUIREMENTS,
|
|
49
|
-
tools: entries,
|
|
50
|
-
by_requirement,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Factory: build the comms_capabilities tool over a given tool set. The tool
|
|
56
|
-
* itself is appended to that set, so it self-reports too (requires: "any").
|
|
57
|
-
*/
|
|
58
|
-
export function makeCapabilitiesTool(channelTools: MosaddTool[]): MosaddTool {
|
|
59
|
-
const tool: MosaddTool = {
|
|
60
|
-
name: "comms_capabilities",
|
|
61
|
-
title: "List tool capabilities",
|
|
62
|
-
annotations: { readOnlyHint: true },
|
|
63
|
-
requires: "any",
|
|
64
|
-
description:
|
|
65
|
-
"Discover which mosadd tools are available and what transport each requires (any | radio | network). Carrier-aware hosts (e.g. off-grid radio devices) call this once to gate the tool list to what the current carrier supports. Returns a manifest with per-tool requirements grouped by transport.",
|
|
66
|
-
inputSchema: z.object({}),
|
|
67
|
-
handler: async () => buildCapabilityManifest([...channelTools, tool]),
|
|
68
|
-
};
|
|
69
|
-
return tool;
|
|
70
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* comms_capabilities — capability discovery for consuming hosts.
|
|
3
|
+
*
|
|
4
|
+
* THE contract surface between mosadd-os and carrier-aware hosts (e.g.
|
|
5
|
+
* cymru-os, which filters tools by available transports — see cymru-os RFC
|
|
6
|
+
* 0002 D-Bus "Capability-flag bridge"). A host spawns `@mosadd/mcp` over stdio
|
|
7
|
+
* and calls `comms_capabilities` once to learn which tools are usable on which
|
|
8
|
+
* carrier, then gates the tool list accordingly (e.g. off-grid radio →
|
|
9
|
+
* only `requires: "any" | "radio"` tools are offered).
|
|
10
|
+
*
|
|
11
|
+
* `requires: "any"` here too — discovery must work on any transport, including
|
|
12
|
+
* off-grid radio, otherwise a host couldn't learn what it's allowed to call.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import type { CapabilityRequirement, MosaddTool } from "../types.js";
|
|
17
|
+
|
|
18
|
+
export interface CapabilityEntry {
|
|
19
|
+
name: string;
|
|
20
|
+
requires: CapabilityRequirement;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CapabilityManifest {
|
|
24
|
+
/** Stable contract version. Bump on any enum/shape change (requires:cymru-input). */
|
|
25
|
+
contract_version: string;
|
|
26
|
+
/** The finalized capability enum, echoed so hosts can validate against it. */
|
|
27
|
+
requirements: CapabilityRequirement[];
|
|
28
|
+
/** Flat list of every tool and its transport requirement. */
|
|
29
|
+
tools: CapabilityEntry[];
|
|
30
|
+
/** Pre-grouped by requirement for cheap host-side filtering. */
|
|
31
|
+
by_requirement: Record<CapabilityRequirement, string[]>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const CONTRACT_VERSION = "1.0.0";
|
|
35
|
+
const ALL_REQUIREMENTS: CapabilityRequirement[] = ["any", "radio", "network"];
|
|
36
|
+
|
|
37
|
+
/** Build the capability manifest from a tool list. Pure, host-agnostic. */
|
|
38
|
+
export function buildCapabilityManifest(tools: MosaddTool[]): CapabilityManifest {
|
|
39
|
+
const entries: CapabilityEntry[] = tools.map((t) => ({ name: t.name, requires: t.requires }));
|
|
40
|
+
const by_requirement: Record<CapabilityRequirement, string[]> = {
|
|
41
|
+
any: [],
|
|
42
|
+
radio: [],
|
|
43
|
+
network: [],
|
|
44
|
+
};
|
|
45
|
+
for (const e of entries) by_requirement[e.requires].push(e.name);
|
|
46
|
+
return {
|
|
47
|
+
contract_version: CONTRACT_VERSION,
|
|
48
|
+
requirements: ALL_REQUIREMENTS,
|
|
49
|
+
tools: entries,
|
|
50
|
+
by_requirement,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Factory: build the comms_capabilities tool over a given tool set. The tool
|
|
56
|
+
* itself is appended to that set, so it self-reports too (requires: "any").
|
|
57
|
+
*/
|
|
58
|
+
export function makeCapabilitiesTool(channelTools: MosaddTool[]): MosaddTool {
|
|
59
|
+
const tool: MosaddTool = {
|
|
60
|
+
name: "comms_capabilities",
|
|
61
|
+
title: "List tool capabilities",
|
|
62
|
+
annotations: { readOnlyHint: true },
|
|
63
|
+
requires: "any",
|
|
64
|
+
description:
|
|
65
|
+
"Discover which mosadd tools are available and what transport each requires (any | radio | network). Carrier-aware hosts (e.g. off-grid radio devices) call this once to gate the tool list to what the current carrier supports. Returns a manifest with per-tool requirements grouped by transport.",
|
|
66
|
+
inputSchema: z.object({}),
|
|
67
|
+
handler: async () => buildCapabilityManifest([...channelTools, tool]),
|
|
68
|
+
};
|
|
69
|
+
return tool;
|
|
70
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* channel-payload — stored channel/room payload (base64) → the text a reader should see.
|
|
3
|
+
*
|
|
4
|
+
* ⛔ ZERO SZYFROWANIA 29.09 (rozkaz Krola 28.09: „sciagnij te jebane szyfrowanie z naszych rozmow
|
|
5
|
+
* w kazdym kanale i w kazdej tozsamosci"). #adm and #command are OPEN channels with no group key,
|
|
6
|
+
* yet every line reading them through this gateway saw the King's posts as "<ciphertext>". Nothing
|
|
7
|
+
* was encrypted: the app stores channel text as plain UTF-8 in base64 (apps/web ircApi.ts
|
|
8
|
+
* `encodePayload`), while the old decoder here understood ONLY a JSON `mosadd.chat.v1` envelope
|
|
9
|
+
* and stamped everything else "<ciphertext>". Measured 28.09–29.09 on #adm (f159c8d7, 776b3e9a).
|
|
10
|
+
*
|
|
11
|
+
* Rule now (same as mdm.ts `unpackPayload` and the mKEEPER bridge `decodePayload.mjs`):
|
|
12
|
+
* - JSON envelope with `text` → that text (toolkit posts, unchanged)
|
|
13
|
+
* - sealed envelope a reader cannot open:
|
|
14
|
+
* group-key seal {iv, ciphertext, hmac?} → "<ciphertext>"
|
|
15
|
+
* mDM ratchet envelope v: "mosadd.e2ee.*" → "<ciphertext>"
|
|
16
|
+
* - any other JSON (e.g. voice {audio, mime, dur}) → raw JSON, so `formatVoiceIfAny` can shape it
|
|
17
|
+
* - plain UTF-8 text → the text itself (what the app wrote)
|
|
18
|
+
* - bytes that are not UTF-8 / carry control characters → "<ciphertext>" (opaque, not words)
|
|
19
|
+
*
|
|
20
|
+
* Never throws.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const CIPHERTEXT_MARKER = "<ciphertext>";
|
|
24
|
+
|
|
25
|
+
const STRICT_BASE64 = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
26
|
+
// C0 controls except TAB / LF / CR, plus DEL: a "text" that carries them is bytes, not words.
|
|
27
|
+
// eslint-disable-next-line no-control-regex
|
|
28
|
+
const CONTROL_CHARS = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/;
|
|
29
|
+
|
|
30
|
+
/** A sealed envelope nobody without the key can read (group-key seal or mDM ratchet). */
|
|
31
|
+
export function isSealedEnvelope(obj: unknown): boolean {
|
|
32
|
+
if (!obj || typeof obj !== "object") return false;
|
|
33
|
+
const o = obj as Record<string, unknown>;
|
|
34
|
+
if (typeof o.iv === "string" && typeof o.ciphertext === "string") return true;
|
|
35
|
+
if (typeof o.v === "string" && o.v.startsWith("mosadd.e2ee")) return true;
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Stored payload (base64 of UTF-8, as message-list returns it) → readable text. */
|
|
40
|
+
export function decodeStoredPayloadText(payload: string | null | undefined): string {
|
|
41
|
+
if (typeof payload !== "string" || payload.length === 0) return "";
|
|
42
|
+
|
|
43
|
+
let raw: string;
|
|
44
|
+
const compact = payload.replace(/\s+/g, "");
|
|
45
|
+
if (compact.length % 4 === 0 && STRICT_BASE64.test(compact)) {
|
|
46
|
+
try {
|
|
47
|
+
raw = new TextDecoder("utf-8", { fatal: true }).decode(Buffer.from(compact, "base64"));
|
|
48
|
+
} catch {
|
|
49
|
+
return CIPHERTEXT_MARKER; // opaque bytes, not UTF-8
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
raw = payload; // a row stored as raw text, not base64
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (raw.startsWith("{")) {
|
|
56
|
+
try {
|
|
57
|
+
const obj = JSON.parse(raw) as unknown;
|
|
58
|
+
if (isSealedEnvelope(obj)) return CIPHERTEXT_MARKER;
|
|
59
|
+
const text = (obj as { text?: unknown } | null)?.text;
|
|
60
|
+
if (typeof text === "string") return text;
|
|
61
|
+
return raw;
|
|
62
|
+
} catch {
|
|
63
|
+
/* plain text that happens to start with "{" */
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (CONTROL_CHARS.test(raw)) return CIPHERTEXT_MARKER;
|
|
68
|
+
return raw;
|
|
69
|
+
}
|
package/src/tools/embed.ts
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* comms_embed — let a builder-agent drop a LIVE mosadd channel into the app/site
|
|
3
|
-
* it is generating.
|
|
4
|
-
*
|
|
5
|
-
* Mints a publishable embed key (`m_pk_live_…`) scoped to a channel + the app's
|
|
6
|
-
* domain(s) via the `embed-keys` Edge Function, and returns a paste-in HTML
|
|
7
|
-
* snippet that loads the mosadd widget from the CDN (embed.mosadd.com/v1.js).
|
|
8
|
-
*
|
|
9
|
-
* Security: the publishable key is safe to ship in HTML — it can ONLY mint
|
|
10
|
-
* channel-scoped JWTs (the widget exchanges it at runtime via `mirc-embed-token`,
|
|
11
|
-
* validated against the key's origin allow-list). Your hub key (`mosadd_sk_live_…`)
|
|
12
|
-
* never enters the browser. The action is owned by the calling user (user-JWT
|
|
13
|
-
* scoped — `mosadd login` / MOSADD_USER_JWT / hub-key exchange).
|
|
14
|
-
*
|
|
15
|
-
* Reuses the shipped embed infra (apps/embed widget + embed-keys + mirc-embed-token).
|
|
16
|
-
* RFC 0001: channel-agnostic capability → `comms_` namespace (not an m<MODULE>_ prefix).
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { z } from "zod";
|
|
20
|
-
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
21
|
-
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
22
|
-
|
|
23
|
-
/** Widget CDN base. Override with MOSADD_EMBED_CDN for self-host / dev. */
|
|
24
|
-
const EMBED_CDN = (process.env.MOSADD_EMBED_CDN ?? "https://embed.mosadd.com").replace(/\/+$/, "");
|
|
25
|
-
|
|
26
|
-
const POSITIONS = ["inline", "sidebar-left", "sidebar-right", "floating-bl", "floating-br", "fullscreen"] as const;
|
|
27
|
-
const SKINS = ["default", "retro-irc-1990", "terminal", "minimal-dark", "minimal-light"] as const;
|
|
28
|
-
|
|
29
|
-
const embed_create_input = z.object({
|
|
30
|
-
channel: z
|
|
31
|
-
.string().min(1).max(128).regex(/^[A-Za-z0-9_-]+$/)
|
|
32
|
-
.describe("The mIRC channel id everyone on the embed shares (e.g. 'acme-support'). Auto-created on first message if new."),
|
|
33
|
-
allowed_origins: z
|
|
34
|
-
.array(z.string().min(1)).min(1).max(10)
|
|
35
|
-
.describe("REQUIRED. Domain(s) where the widget will be served — the key only works from these origins. Use the host(s) of the app you're building, e.g. ['app.acme.com','*.acme.com']. For local testing use mode:'test' and e.g. 'localhost:3000'."),
|
|
36
|
-
name: z.string().max(64).optional().describe("Label for the key in the owner's dashboard. Default 'embed: <channel>'."),
|
|
37
|
-
position: z.enum(POSITIONS).optional().describe("Where the widget renders. Default 'inline'."),
|
|
38
|
-
skin: z.enum(SKINS).optional().describe("Visual skin. Default 'default' (mosadd-branded retro mIRC)."),
|
|
39
|
-
title: z.string().max(60).optional().describe("Header title shown in the widget. Default 'mIRC'."),
|
|
40
|
-
anon: z.boolean().optional().describe("Allow visitors to join with just a nick (no mosadd account). Default true."),
|
|
41
|
-
locale: z.enum(["en", "pl"]).optional().describe("Widget UI language. Default 'en'."),
|
|
42
|
-
mode: z.enum(["live", "test"]).optional().describe("Key mode — 'test' for local/dev origins, 'live' for production. Default 'live'."),
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
interface EmbedKeyResult {
|
|
46
|
-
id?: string;
|
|
47
|
-
key?: string;
|
|
48
|
-
pk_prefix?: string;
|
|
49
|
-
allowed_channels?: string[];
|
|
50
|
-
error?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface EmbedCreateResult {
|
|
54
|
-
embed_key: string;
|
|
55
|
-
channel: string;
|
|
56
|
-
allowed_origins: string[];
|
|
57
|
-
snippet: string;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** HTML-attr-safe: drop quotes/angle brackets (inputs are already length-bounded). */
|
|
61
|
-
const attrSafe = (s: string): string => s.replace(/["'<>]/g, "");
|
|
62
|
-
|
|
63
|
-
function buildSnippet(input: z.infer<typeof embed_create_input>, key: string): string {
|
|
64
|
-
const attrs = [`data-channel="${attrSafe(input.channel)}"`, `data-position="${input.position ?? "inline"}"`];
|
|
65
|
-
if (input.skin) attrs.push(`data-skin="${input.skin}"`);
|
|
66
|
-
if (input.title) attrs.push(`data-title="${attrSafe(input.title)}"`);
|
|
67
|
-
if (input.anon === false) attrs.push(`data-anon="false"`);
|
|
68
|
-
if (input.locale) attrs.push(`data-locale="${input.locale}"`);
|
|
69
|
-
return (
|
|
70
|
-
`<div id="mosadd-mirc" ${attrs.join(" ")}></div>\n` +
|
|
71
|
-
`<script src="${EMBED_CDN}/v1.js" data-key="${key}"></script>`
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async function embed_create(
|
|
76
|
-
input: z.infer<typeof embed_create_input>,
|
|
77
|
-
ctx: MosaddToolContext,
|
|
78
|
-
): Promise<EmbedCreateResult> {
|
|
79
|
-
readSupabaseEnv();
|
|
80
|
-
ctx.log("debug", "comms_embed_create minting embed key", { channel: input.channel, origins: input.allowed_origins.length });
|
|
81
|
-
|
|
82
|
-
const res = await invokeFunction<EmbedKeyResult>("embed-keys", {
|
|
83
|
-
name: input.name ?? `embed: ${input.channel}`,
|
|
84
|
-
channels: [input.channel],
|
|
85
|
-
allowed_origins: input.allowed_origins,
|
|
86
|
-
mode: input.mode ?? "live",
|
|
87
|
-
});
|
|
88
|
-
if (res.error) throw new Error(res.error);
|
|
89
|
-
if (!res.key) throw new Error("embed-keys returned no publishable key (missing `key`).");
|
|
90
|
-
|
|
91
|
-
return {
|
|
92
|
-
embed_key: res.key,
|
|
93
|
-
channel: input.channel,
|
|
94
|
-
allowed_origins: input.allowed_origins,
|
|
95
|
-
snippet: buildSnippet(input, res.key),
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export const embedTools: MosaddTool[] = [
|
|
100
|
-
{
|
|
101
|
-
name: "comms_embed_create",
|
|
102
|
-
title: "Create website embed",
|
|
103
|
-
annotations: { readOnlyHint: false },
|
|
104
|
-
requires: "network",
|
|
105
|
-
description:
|
|
106
|
-
"Embed a LIVE mosadd chat channel into an app/site you're building (great for builder-agents on Lovable/Bolt/Cursor/v0). Mints a publishable embed key scoped to a channel + the app's domain(s) and returns a ready-to-paste HTML snippet (a <div> + <script src=embed.mosadd.com/v1.js data-key=…>). Visitors of the host page chat in real time (anonymous nick or mosadd login). REQUIRES `allowed_origins` — the domain(s) the app is served from (the key only works there). The publishable key is browser-safe (mints only channel-scoped JWTs); your hub key never leaves the server. Returns { embed_key, channel, allowed_origins, snippet }.",
|
|
107
|
-
inputSchema: embed_create_input,
|
|
108
|
-
handler: embed_create as MosaddTool["handler"],
|
|
109
|
-
},
|
|
110
|
-
];
|
|
1
|
+
/**
|
|
2
|
+
* comms_embed — let a builder-agent drop a LIVE mosadd channel into the app/site
|
|
3
|
+
* it is generating.
|
|
4
|
+
*
|
|
5
|
+
* Mints a publishable embed key (`m_pk_live_…`) scoped to a channel + the app's
|
|
6
|
+
* domain(s) via the `embed-keys` Edge Function, and returns a paste-in HTML
|
|
7
|
+
* snippet that loads the mosadd widget from the CDN (embed.mosadd.com/v1.js).
|
|
8
|
+
*
|
|
9
|
+
* Security: the publishable key is safe to ship in HTML — it can ONLY mint
|
|
10
|
+
* channel-scoped JWTs (the widget exchanges it at runtime via `mirc-embed-token`,
|
|
11
|
+
* validated against the key's origin allow-list). Your hub key (`mosadd_sk_live_…`)
|
|
12
|
+
* never enters the browser. The action is owned by the calling user (user-JWT
|
|
13
|
+
* scoped — `mosadd login` / MOSADD_USER_JWT / hub-key exchange).
|
|
14
|
+
*
|
|
15
|
+
* Reuses the shipped embed infra (apps/embed widget + embed-keys + mirc-embed-token).
|
|
16
|
+
* RFC 0001: channel-agnostic capability → `comms_` namespace (not an m<MODULE>_ prefix).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
21
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
22
|
+
|
|
23
|
+
/** Widget CDN base. Override with MOSADD_EMBED_CDN for self-host / dev. */
|
|
24
|
+
const EMBED_CDN = (process.env.MOSADD_EMBED_CDN ?? "https://embed.mosadd.com").replace(/\/+$/, "");
|
|
25
|
+
|
|
26
|
+
const POSITIONS = ["inline", "sidebar-left", "sidebar-right", "floating-bl", "floating-br", "fullscreen"] as const;
|
|
27
|
+
const SKINS = ["default", "retro-irc-1990", "terminal", "minimal-dark", "minimal-light"] as const;
|
|
28
|
+
|
|
29
|
+
const embed_create_input = z.object({
|
|
30
|
+
channel: z
|
|
31
|
+
.string().min(1).max(128).regex(/^[A-Za-z0-9_-]+$/)
|
|
32
|
+
.describe("The mIRC channel id everyone on the embed shares (e.g. 'acme-support'). Auto-created on first message if new."),
|
|
33
|
+
allowed_origins: z
|
|
34
|
+
.array(z.string().min(1)).min(1).max(10)
|
|
35
|
+
.describe("REQUIRED. Domain(s) where the widget will be served — the key only works from these origins. Use the host(s) of the app you're building, e.g. ['app.acme.com','*.acme.com']. For local testing use mode:'test' and e.g. 'localhost:3000'."),
|
|
36
|
+
name: z.string().max(64).optional().describe("Label for the key in the owner's dashboard. Default 'embed: <channel>'."),
|
|
37
|
+
position: z.enum(POSITIONS).optional().describe("Where the widget renders. Default 'inline'."),
|
|
38
|
+
skin: z.enum(SKINS).optional().describe("Visual skin. Default 'default' (mosadd-branded retro mIRC)."),
|
|
39
|
+
title: z.string().max(60).optional().describe("Header title shown in the widget. Default 'mIRC'."),
|
|
40
|
+
anon: z.boolean().optional().describe("Allow visitors to join with just a nick (no mosadd account). Default true."),
|
|
41
|
+
locale: z.enum(["en", "pl"]).optional().describe("Widget UI language. Default 'en'."),
|
|
42
|
+
mode: z.enum(["live", "test"]).optional().describe("Key mode — 'test' for local/dev origins, 'live' for production. Default 'live'."),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
interface EmbedKeyResult {
|
|
46
|
+
id?: string;
|
|
47
|
+
key?: string;
|
|
48
|
+
pk_prefix?: string;
|
|
49
|
+
allowed_channels?: string[];
|
|
50
|
+
error?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface EmbedCreateResult {
|
|
54
|
+
embed_key: string;
|
|
55
|
+
channel: string;
|
|
56
|
+
allowed_origins: string[];
|
|
57
|
+
snippet: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** HTML-attr-safe: drop quotes/angle brackets (inputs are already length-bounded). */
|
|
61
|
+
const attrSafe = (s: string): string => s.replace(/["'<>]/g, "");
|
|
62
|
+
|
|
63
|
+
function buildSnippet(input: z.infer<typeof embed_create_input>, key: string): string {
|
|
64
|
+
const attrs = [`data-channel="${attrSafe(input.channel)}"`, `data-position="${input.position ?? "inline"}"`];
|
|
65
|
+
if (input.skin) attrs.push(`data-skin="${input.skin}"`);
|
|
66
|
+
if (input.title) attrs.push(`data-title="${attrSafe(input.title)}"`);
|
|
67
|
+
if (input.anon === false) attrs.push(`data-anon="false"`);
|
|
68
|
+
if (input.locale) attrs.push(`data-locale="${input.locale}"`);
|
|
69
|
+
return (
|
|
70
|
+
`<div id="mosadd-mirc" ${attrs.join(" ")}></div>\n` +
|
|
71
|
+
`<script src="${EMBED_CDN}/v1.js" data-key="${key}"></script>`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function embed_create(
|
|
76
|
+
input: z.infer<typeof embed_create_input>,
|
|
77
|
+
ctx: MosaddToolContext,
|
|
78
|
+
): Promise<EmbedCreateResult> {
|
|
79
|
+
readSupabaseEnv();
|
|
80
|
+
ctx.log("debug", "comms_embed_create minting embed key", { channel: input.channel, origins: input.allowed_origins.length });
|
|
81
|
+
|
|
82
|
+
const res = await invokeFunction<EmbedKeyResult>("embed-keys", {
|
|
83
|
+
name: input.name ?? `embed: ${input.channel}`,
|
|
84
|
+
channels: [input.channel],
|
|
85
|
+
allowed_origins: input.allowed_origins,
|
|
86
|
+
mode: input.mode ?? "live",
|
|
87
|
+
});
|
|
88
|
+
if (res.error) throw new Error(res.error);
|
|
89
|
+
if (!res.key) throw new Error("embed-keys returned no publishable key (missing `key`).");
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
embed_key: res.key,
|
|
93
|
+
channel: input.channel,
|
|
94
|
+
allowed_origins: input.allowed_origins,
|
|
95
|
+
snippet: buildSnippet(input, res.key),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const embedTools: MosaddTool[] = [
|
|
100
|
+
{
|
|
101
|
+
name: "comms_embed_create",
|
|
102
|
+
title: "Create website embed",
|
|
103
|
+
annotations: { readOnlyHint: false },
|
|
104
|
+
requires: "network",
|
|
105
|
+
description:
|
|
106
|
+
"Embed a LIVE mosadd chat channel into an app/site you're building (great for builder-agents on Lovable/Bolt/Cursor/v0). Mints a publishable embed key scoped to a channel + the app's domain(s) and returns a ready-to-paste HTML snippet (a <div> + <script src=embed.mosadd.com/v1.js data-key=…>). Visitors of the host page chat in real time (anonymous nick or mosadd login). REQUIRES `allowed_origins` — the domain(s) the app is served from (the key only works there). The publishable key is browser-safe (mints only channel-scoped JWTs); your hub key never leaves the server. Returns { embed_key, channel, allowed_origins, snippet }.",
|
|
107
|
+
inputSchema: embed_create_input,
|
|
108
|
+
handler: embed_create as MosaddTool["handler"],
|
|
109
|
+
},
|
|
110
|
+
];
|