@mosadd/mcp 3.0.0-alpha.4 → 3.0.0-alpha.41
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 +298 -0
- package/README.md +70 -36
- package/dist/bin/mcp.js +42 -3
- package/dist/chunk-SS6UVKSB.js +3513 -0
- package/dist/index.d.ts +115 -6
- package/dist/index.js +7 -3
- package/package.json +8 -5
- package/src/__tests__/golden-path.test.ts +139 -137
- package/src/__tests__/integration.test.ts +179 -178
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -178
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +178 -0
- package/src/__tests__/server.test.ts +71 -67
- package/src/__tests__/tool-count-consistency.test.ts +164 -0
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -97
- package/src/config.ts +66 -66
- 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 +155 -96
- package/src/server.ts +66 -9
- 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 +177 -43
- package/src/tools/knowledge-graph.ts +186 -0
- package/src/tools/knowledge.ts +327 -91
- 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 -89
- package/src/tools/mcall.ts +216 -0
- package/src/tools/mdm-as-agent.ts +126 -0
- package/src/tools/mdm-voice.ts +220 -0
- package/src/tools/mdm.ts +494 -392
- package/src/tools/mirc-edge.ts +196 -0
- package/src/tools/mirc-members.ts +61 -20
- package/src/tools/mirc-messages.ts +185 -151
- package/src/tools/mirc.ts +104 -10
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -189
- package/src/tools/mtalk.ts +131 -121
- package/src/tools/murl.ts +314 -0
- package/src/tools/presence.ts +399 -0
- package/src/tools/ptt-ingest.ts +99 -0
- package/src/tools/threat.ts +123 -0
- package/src/types.ts +121 -85
- package/src/version.ts +8 -0
- package/src/voice/floor.ts +132 -132
- package/dist/chunk-YDFU6S5E.js +0 -1471
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mCALL — Outbound PSTN calls.
|
|
3
|
+
*
|
|
4
|
+
* Place a real phone call (E.164) from an agent. Control plane is MCP; audio
|
|
5
|
+
* never flows through the tool call — it is bridged over LiveKit SIP (primary)
|
|
6
|
+
* or a Telnyx fallback to the carrier, with an optional voice vocoder.
|
|
7
|
+
*
|
|
8
|
+
* Wired to the m0ssad-3 `call-start-pstn` / `call-end-pstn` Edge Functions
|
|
9
|
+
* (deployed). A carrier must be wired server-side (LIVEKIT_SIP_TRUNK_ID, or
|
|
10
|
+
* TELNYX_API_KEY + TELNYX_CONNECTION_ID + TELNYX_FROM_NUMBER); until then the
|
|
11
|
+
* call session is created and metered but no audio leg is bridged.
|
|
12
|
+
*
|
|
13
|
+
* NOTE: mCALL is the telephone (PSTN). For 1:1 in-app voice use mDM; for
|
|
14
|
+
* walkie-talkie / floor-controlled voice use mTALK.
|
|
15
|
+
*
|
|
16
|
+
* Burner numbers: mCALL_acquire_number leases an ephemeral DID (~10 min) that
|
|
17
|
+
* becomes your outbound caller-ID and can receive inbound calls. Inbound rings
|
|
18
|
+
* are delivered out-of-band (Supabase Realtime broadcast on
|
|
19
|
+
* `mcall-inbound-<identity_id>`, or an integrator webhook) carrying a room_name;
|
|
20
|
+
* mCALL_answer mints the LiveKit token to join and bridge that call. Pair with
|
|
21
|
+
* the `vocoder` flag / a published vocoder track to mask the caller's voice.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { z } from "zod";
|
|
25
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
26
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
27
|
+
|
|
28
|
+
// ---- Schemas ----
|
|
29
|
+
|
|
30
|
+
const E164 = z
|
|
31
|
+
.string()
|
|
32
|
+
.regex(/^\+\d{7,15}$/)
|
|
33
|
+
.describe("Destination phone number in E.164 format, e.g. +14155550123.");
|
|
34
|
+
|
|
35
|
+
const mCALL_start_pstn_input = z.object({
|
|
36
|
+
to: E164,
|
|
37
|
+
vocoder: z
|
|
38
|
+
.boolean()
|
|
39
|
+
.optional()
|
|
40
|
+
.describe("Disguise the caller's voice with a vocoder (anonymous calling). Default false."),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const mCALL_end_pstn_input = z.object({
|
|
44
|
+
session_id: z.string().min(1).describe("Session id returned by mCALL_start_pstn."),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// ---- Burner (ephemeral number) schemas ----
|
|
48
|
+
|
|
49
|
+
const mCALL_acquire_number_input = z.object({}).describe(
|
|
50
|
+
"No parameters. Leases the next available number for ~10 minutes (idempotent: returns your live lease if you already hold one).",
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const mCALL_my_numbers_input = z.object({}).describe("No parameters.");
|
|
54
|
+
|
|
55
|
+
const mCALL_extend_number_input = z.object({
|
|
56
|
+
number_id: z
|
|
57
|
+
.string()
|
|
58
|
+
.optional()
|
|
59
|
+
.describe("Pool id of the number to extend. Omit to extend your current lease."),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const mCALL_release_number_input = z.object({
|
|
63
|
+
number_id: z
|
|
64
|
+
.string()
|
|
65
|
+
.optional()
|
|
66
|
+
.describe("Pool id of the number to release. Omit to release all numbers you hold."),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const mCALL_answer_input = z.object({
|
|
70
|
+
room_name: z
|
|
71
|
+
.string()
|
|
72
|
+
.min(1)
|
|
73
|
+
.describe("room_name from an inbound_call event (Realtime broadcast / integrator webhook)."),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// ---- Handlers ----
|
|
77
|
+
|
|
78
|
+
async function mCALL_start_pstn(
|
|
79
|
+
input: z.infer<typeof mCALL_start_pstn_input>,
|
|
80
|
+
ctx: MosaddToolContext,
|
|
81
|
+
): Promise<{
|
|
82
|
+
session_id: string;
|
|
83
|
+
room_name: string;
|
|
84
|
+
destination_number: string;
|
|
85
|
+
vocoder_enabled: boolean;
|
|
86
|
+
minutes_remaining: number;
|
|
87
|
+
zone: string;
|
|
88
|
+
zone_label: string;
|
|
89
|
+
}> {
|
|
90
|
+
readSupabaseEnv();
|
|
91
|
+
ctx.log("debug", "mCALL_start_pstn invoking call-start-pstn", { to: input.to });
|
|
92
|
+
return await invokeFunction("call-start-pstn", {
|
|
93
|
+
destination_number: input.to,
|
|
94
|
+
vocoder_enabled: input.vocoder ?? false,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function mCALL_end_pstn(
|
|
99
|
+
input: z.infer<typeof mCALL_end_pstn_input>,
|
|
100
|
+
ctx: MosaddToolContext,
|
|
101
|
+
): Promise<{ duration_ms: number; cost_credits: number }> {
|
|
102
|
+
readSupabaseEnv();
|
|
103
|
+
ctx.log("debug", "mCALL_end_pstn invoking call-end-pstn", { session_id: input.session_id });
|
|
104
|
+
return await invokeFunction("call-end-pstn", { session_id: input.session_id });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---- Burner (ephemeral number) handlers ----
|
|
108
|
+
|
|
109
|
+
interface AcquiredNumber {
|
|
110
|
+
did: { id: string; phone_number: string; hold_until: string | null };
|
|
111
|
+
reused: boolean;
|
|
112
|
+
hold_until: string;
|
|
113
|
+
seconds_remaining: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function mCALL_acquire_number(
|
|
117
|
+
_input: z.infer<typeof mCALL_acquire_number_input>,
|
|
118
|
+
ctx: MosaddToolContext,
|
|
119
|
+
): Promise<AcquiredNumber> {
|
|
120
|
+
readSupabaseEnv();
|
|
121
|
+
ctx.log("debug", "mCALL_acquire_number invoking call-did-pool acquire");
|
|
122
|
+
return await invokeFunction("call-did-pool", { action: "acquire" });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function mCALL_my_numbers(
|
|
126
|
+
_input: z.infer<typeof mCALL_my_numbers_input>,
|
|
127
|
+
ctx: MosaddToolContext,
|
|
128
|
+
): Promise<{ numbers: Array<{ id: string; phone_number: string; seconds_remaining: number | null }> }> {
|
|
129
|
+
readSupabaseEnv();
|
|
130
|
+
ctx.log("debug", "mCALL_my_numbers invoking call-did-pool mine");
|
|
131
|
+
return await invokeFunction("call-did-pool", { action: "mine" });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function mCALL_extend_number(
|
|
135
|
+
input: z.infer<typeof mCALL_extend_number_input>,
|
|
136
|
+
ctx: MosaddToolContext,
|
|
137
|
+
): Promise<{ did: unknown; hold_until: string; seconds_remaining: number }> {
|
|
138
|
+
readSupabaseEnv();
|
|
139
|
+
ctx.log("debug", "mCALL_extend_number invoking call-did-pool extend", { number_id: input.number_id });
|
|
140
|
+
return await invokeFunction("call-did-pool", { action: "extend", did_id: input.number_id });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function mCALL_release_number(
|
|
144
|
+
input: z.infer<typeof mCALL_release_number_input>,
|
|
145
|
+
ctx: MosaddToolContext,
|
|
146
|
+
): Promise<{ released: number; dids: unknown[] }> {
|
|
147
|
+
readSupabaseEnv();
|
|
148
|
+
ctx.log("debug", "mCALL_release_number invoking call-did-pool release_mine", { number_id: input.number_id });
|
|
149
|
+
return await invokeFunction("call-did-pool", { action: "release_mine", did_id: input.number_id });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function mCALL_answer(
|
|
153
|
+
input: z.infer<typeof mCALL_answer_input>,
|
|
154
|
+
ctx: MosaddToolContext,
|
|
155
|
+
): Promise<{ token: string; url: string; roomName: string; identity: string }> {
|
|
156
|
+
readSupabaseEnv();
|
|
157
|
+
ctx.log("debug", "mCALL_answer minting livekit token", { room_name: input.room_name });
|
|
158
|
+
return await invokeFunction("livekit-token", { roomName: input.room_name });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ---- Registration ----
|
|
162
|
+
|
|
163
|
+
export const mcallTools: MosaddTool[] = [
|
|
164
|
+
{
|
|
165
|
+
name: "mCALL_start_pstn",
|
|
166
|
+
requires: "network",
|
|
167
|
+
description:
|
|
168
|
+
"Place an outbound PSTN phone call to an E.164 number. Bridges audio over an encrypted SIP relay (LiveKit SIP / Telnyx); optional voice vocoder for anonymous calling. Returns session_id, room_name, zone + per-minute cost, and minutes_remaining. Requires PSTN minutes in the wallet.",
|
|
169
|
+
inputSchema: mCALL_start_pstn_input,
|
|
170
|
+
handler: mCALL_start_pstn as MosaddTool["handler"],
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: "mCALL_end_pstn",
|
|
174
|
+
requires: "network",
|
|
175
|
+
description: "End an active PSTN call by session_id. Returns call duration and credits spent.",
|
|
176
|
+
inputSchema: mCALL_end_pstn_input,
|
|
177
|
+
handler: mCALL_end_pstn as MosaddTool["handler"],
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: "mCALL_acquire_number",
|
|
181
|
+
requires: "network",
|
|
182
|
+
description:
|
|
183
|
+
"Lease an ephemeral 'burner' phone number for ~10 minutes. While held, it is presented as your caller-ID on mCALL_start_pstn and can receive inbound calls; it auto-recycles when the lease lapses (unless on a live call). Idempotent — returns your existing lease if you already hold one. Returns the number, hold_until and seconds_remaining.",
|
|
184
|
+
inputSchema: mCALL_acquire_number_input,
|
|
185
|
+
handler: mCALL_acquire_number as MosaddTool["handler"],
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "mCALL_my_numbers",
|
|
189
|
+
requires: "network",
|
|
190
|
+
description: "List the ephemeral numbers currently leased to you, each with its seconds_remaining.",
|
|
191
|
+
inputSchema: mCALL_my_numbers_input,
|
|
192
|
+
handler: mCALL_my_numbers as MosaddTool["handler"],
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: "mCALL_extend_number",
|
|
196
|
+
requires: "network",
|
|
197
|
+
description: "Extend the lease on a held ephemeral number by another ~10 minutes (by number_id, or your current lease).",
|
|
198
|
+
inputSchema: mCALL_extend_number_input,
|
|
199
|
+
handler: mCALL_extend_number as MosaddTool["handler"],
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
name: "mCALL_release_number",
|
|
203
|
+
requires: "network",
|
|
204
|
+
description: "Release an ephemeral number back to the pool before its lease expires (by number_id, or all you hold).",
|
|
205
|
+
inputSchema: mCALL_release_number_input,
|
|
206
|
+
handler: mCALL_release_number as MosaddTool["handler"],
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: "mCALL_answer",
|
|
210
|
+
requires: "network",
|
|
211
|
+
description:
|
|
212
|
+
"Answer an inbound call to your burner number. Pass the room_name from the inbound_call event; returns a LiveKit token + url so your client joins the room and bridges audio (publish a vocoder track to mask your voice on answer).",
|
|
213
|
+
inputSchema: mCALL_answer_input,
|
|
214
|
+
handler: mCALL_answer as MosaddTool["handler"],
|
|
215
|
+
},
|
|
216
|
+
];
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mDM agent-attributed send — a person's session speaks AS one of THEIR agents.
|
|
3
|
+
*
|
|
4
|
+
* ── THE PROBLEM THIS SOLVES ──────────────────────────────────────────────────
|
|
5
|
+
* An MCP key is bound to a USER, and the hosted gateway exchanges it for THAT
|
|
6
|
+
* user's session. So when someone adds the connector while signed in as
|
|
7
|
+
* themselves, their agent runtime comes up holding the OWNER'S identity. The
|
|
8
|
+
* founder hit exactly this on 2026-08-17: his "Dispatch" agent authenticated as
|
|
9
|
+
* him, went looking for him among his own contacts, did not find him, and
|
|
10
|
+
* reported the agent↔agent guard as the cause. The real cost was that his mDM
|
|
11
|
+
* had no living 1:1 thread with his dispatcher.
|
|
12
|
+
*
|
|
13
|
+
* With this tool the same session can post AS `dispatcher@mosadd.com`, so the
|
|
14
|
+
* DYSPOZYTOR thread fills with messages FROM the dispatcher.
|
|
15
|
+
*
|
|
16
|
+
* ── AUTHORIZATION ────────────────────────────────────────────────────────────
|
|
17
|
+
* Enforced SERVER-SIDE by the `mdm-send-as-agent` Edge Function, never here:
|
|
18
|
+
* the named identity must be `kind='agent'`, owned by the CALLER
|
|
19
|
+
* (`owner_user_id`), and not retired. A client-side check would be decoration —
|
|
20
|
+
* the tool arguments are attacker-controlled by definition.
|
|
21
|
+
*
|
|
22
|
+
* Shape follows the accepted `mAYL_send_as_agent` precedent (owner's credential
|
|
23
|
+
* in, agent-attributed message out). Its AUTH model was deliberately not copied:
|
|
24
|
+
* measured 2026-08-17, `hub-claim-mint` accepts any `agent_id` string and never
|
|
25
|
+
* compares it to an owner, so the agent name in a mAYL provenance stamp is
|
|
26
|
+
* caller-asserted text. That gap is filed as LINEAR-5625; this tool does not
|
|
27
|
+
* reproduce it.
|
|
28
|
+
*
|
|
29
|
+
* Auth transport also differs on purpose: mAYL_send_as_agent reads
|
|
30
|
+
* `process.env.MOSADD_API_KEY`, which does not exist on the hosted multi-tenant
|
|
31
|
+
* gateway (the key is per-request there, not process-wide). This tool goes
|
|
32
|
+
* through `invokeFunction`, i.e. the caller's own session — so it works
|
|
33
|
+
* identically over stdio and over mcp.mosadd.com.
|
|
34
|
+
*
|
|
35
|
+
* Agent DMs travel as plaintext base64 on the established agent lane (the same
|
|
36
|
+
* path agent-dm-responder uses); this tool does not change that property.
|
|
37
|
+
* LINEAR-5620.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import { z } from "zod";
|
|
41
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
42
|
+
import { invokeFunction } from "../providers/supabase.js";
|
|
43
|
+
|
|
44
|
+
const mDM_send_as_agent_input = z.object({
|
|
45
|
+
agent: z
|
|
46
|
+
.string()
|
|
47
|
+
.min(1)
|
|
48
|
+
.max(200)
|
|
49
|
+
.describe(
|
|
50
|
+
"Which of YOUR agents to speak as — its mosADD address (dispatcher@mosadd.com), " +
|
|
51
|
+
"its identity id, or its display name. Must be an agent you own.",
|
|
52
|
+
),
|
|
53
|
+
text: z.string().min(1).max(8000).describe("The message body, as the agent would write it."),
|
|
54
|
+
to: z
|
|
55
|
+
.string()
|
|
56
|
+
.max(200)
|
|
57
|
+
.optional()
|
|
58
|
+
.describe(
|
|
59
|
+
"Recipient identity id or mosADD address. Defaults to YOU (the key's owner) — " +
|
|
60
|
+
"the common case is an agent reporting to its owner. Must be a person, not another agent.",
|
|
61
|
+
),
|
|
62
|
+
silent: z
|
|
63
|
+
.boolean()
|
|
64
|
+
.optional()
|
|
65
|
+
.describe("Deliver without waking a phone: the message still arrives and shows unread, only the OS push is skipped."),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const mDM_list_my_agents_input = z.object({});
|
|
69
|
+
|
|
70
|
+
type SendResult = {
|
|
71
|
+
ok: boolean;
|
|
72
|
+
sent_as: { identity_id: string; address: string | null; display_name: string | null };
|
|
73
|
+
to: { identity_id: string; display_name: string | null };
|
|
74
|
+
thread_id: string;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
async function mDM_send_as_agent(
|
|
78
|
+
input: z.infer<typeof mDM_send_as_agent_input>,
|
|
79
|
+
ctx: MosaddToolContext,
|
|
80
|
+
): Promise<SendResult> {
|
|
81
|
+
ctx.log("debug", "mDM_send_as_agent", { agent: input.agent });
|
|
82
|
+
return await invokeFunction<SendResult>("mdm-send-as-agent", {
|
|
83
|
+
agent: input.agent,
|
|
84
|
+
text: input.text,
|
|
85
|
+
...(input.to ? { to: input.to } : {}),
|
|
86
|
+
...(input.silent ? { silent: true } : {}),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function mDM_list_my_agents(
|
|
91
|
+
_input: z.infer<typeof mDM_list_my_agents_input>,
|
|
92
|
+
ctx: MosaddToolContext,
|
|
93
|
+
): Promise<{ agents: Array<{ identity_id: string; address: string | null; display_name: string | null }> }> {
|
|
94
|
+
ctx.log("debug", "mDM_list_my_agents");
|
|
95
|
+
return await invokeFunction("mdm-send-as-agent", { action: "list_agents" });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const mdmAsAgentTools: MosaddTool[] = [
|
|
99
|
+
{
|
|
100
|
+
name: "mDM_send_as_agent",
|
|
101
|
+
title: "Send DM as your agent",
|
|
102
|
+
annotations: { readOnlyHint: false },
|
|
103
|
+
requires: "network",
|
|
104
|
+
description:
|
|
105
|
+
"Send a direct message AS ONE OF YOUR OWN AGENTS instead of as yourself. Use this when you are an " +
|
|
106
|
+
"agent runtime (Dispatch, Hermes, a CTO agent) connected with your owner's key: without it every " +
|
|
107
|
+
"message you send appears to come FROM the owner, so the owner has no thread with you. Names the " +
|
|
108
|
+
"agent by mosADD address, identity id or display name; `to` defaults to the key's owner. The server " +
|
|
109
|
+
"verifies you OWN that agent — speaking as someone else's agent, or as another person, is refused. " +
|
|
110
|
+
"Recipient must be a person: agent-to-agent DM stays blocked.",
|
|
111
|
+
inputSchema: mDM_send_as_agent_input,
|
|
112
|
+
handler: mDM_send_as_agent as MosaddTool["handler"],
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: "mDM_list_my_agents",
|
|
116
|
+
title: "List your agents",
|
|
117
|
+
annotations: { readOnlyHint: true },
|
|
118
|
+
requires: "network",
|
|
119
|
+
description:
|
|
120
|
+
"List the agents you are allowed to speak as with mDM_send_as_agent — every live agent owned by " +
|
|
121
|
+
"the account behind this key, with its identity id, mosADD address and display name. Call this " +
|
|
122
|
+
"first when you are unsure which agent identity you hold.",
|
|
123
|
+
inputSchema: mDM_list_my_agents_input,
|
|
124
|
+
handler: mDM_list_my_agents as MosaddTool["handler"],
|
|
125
|
+
},
|
|
126
|
+
];
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mDM — 1:1 voice (full-duplex call + voice note).
|
|
3
|
+
*
|
|
4
|
+
* mDM is the PRIVATE channel: 1:1 / threaded text (mdm.ts) PLUS real-time 1:1
|
|
5
|
+
* voice here. This is the ordinary phone-style call between two people — NOT
|
|
6
|
+
* walkie-talkie (that's mTALK, half-duplex floor control) and NOT the telephone
|
|
7
|
+
* network (that's mCALL / PSTN). Audio rides the same media transport the
|
|
8
|
+
* VoiceProvider points at (LiveKit by default); these tools coordinate the room
|
|
9
|
+
* and the call invite/hangup signalling over the DM channel.
|
|
10
|
+
*
|
|
11
|
+
* Flow:
|
|
12
|
+
* caller: mDM_call_start({ to }) → creates a room, returns join creds, AND
|
|
13
|
+
* drops a `call_invite` control message in the DM thread.
|
|
14
|
+
* callee: sees the invite (mDM_list), then mDM_call_answer({ room_id }) → creds.
|
|
15
|
+
* either: mDM_call_end({ room_id, to }) → drops a `call_end` control message.
|
|
16
|
+
*
|
|
17
|
+
* No new backend: rooms + tokens come from ctx.providers.voice (same minting
|
|
18
|
+
* path mTALK uses); signalling reuses ctx.providers.dm.send.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
23
|
+
|
|
24
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
25
|
+
|
|
26
|
+
const ContactRef = z
|
|
27
|
+
.string()
|
|
28
|
+
.min(1)
|
|
29
|
+
.describe("Recipient identity id (UUID). Use mDM_list_contacts to find it.");
|
|
30
|
+
|
|
31
|
+
const RoomId = z.string().min(1).describe("Voice room id (from mDM_call_start's call_invite).");
|
|
32
|
+
|
|
33
|
+
const mDM_call_start_input = z.object({
|
|
34
|
+
to: ContactRef,
|
|
35
|
+
video: z.boolean().optional().describe("Request a video call instead of audio-only. Default false."),
|
|
36
|
+
thread_label: z
|
|
37
|
+
.string()
|
|
38
|
+
.max(120)
|
|
39
|
+
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
40
|
+
.optional()
|
|
41
|
+
.describe("Optional thread label to place the call invite in. Defaults to the contact's main thread."),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const mDM_call_answer_input = z.object({ room_id: RoomId });
|
|
45
|
+
|
|
46
|
+
const mDM_call_end_input = z.object({
|
|
47
|
+
room_id: RoomId,
|
|
48
|
+
to: ContactRef.optional().describe("If set, also drop a call_end control message in this contact's DM thread."),
|
|
49
|
+
thread_label: z.string().max(120).regex(/^[a-zA-Z0-9._-]+$/).optional(),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const mDM_voice_note_input = z.object({
|
|
53
|
+
to: ContactRef,
|
|
54
|
+
audio_url: z.string().url().describe("URL of the recorded audio clip (already uploaded by the client)."),
|
|
55
|
+
duration_ms: z.number().int().positive().optional().describe("Clip length in milliseconds, if known."),
|
|
56
|
+
mime: z.string().max(120).optional().describe("Audio MIME type, e.g. audio/ogg, audio/mpeg. Default audio/ogg."),
|
|
57
|
+
thread_label: z.string().max(120).regex(/^[a-zA-Z0-9._-]+$/).optional(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// ---- Helpers ----
|
|
61
|
+
|
|
62
|
+
/** Deterministic DM thread id (sorted pair) — must match mdm.ts dmThreadId. */
|
|
63
|
+
function dmThreadId(selfId: string, otherId: string, label?: string): string {
|
|
64
|
+
const [a, b] = [selfId, otherId].sort();
|
|
65
|
+
return label ? `dm:${a}:${b}:${label}` : `dm:${a}:${b}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Pack a mosadd.chat.v1 control/voice envelope into the opaque payload bytes. */
|
|
69
|
+
function packEnvelope(obj: Record<string, unknown>): Uint8Array {
|
|
70
|
+
const envelope = { v: PROTOCOL_VERSION, sent_at: new Date().toISOString(), ...obj };
|
|
71
|
+
return new Uint8Array(Buffer.from(JSON.stringify(envelope), "utf8"));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function signalDm(
|
|
75
|
+
ctx: MosaddToolContext,
|
|
76
|
+
to: string,
|
|
77
|
+
threadLabel: string | undefined,
|
|
78
|
+
envelope: Record<string, unknown>,
|
|
79
|
+
): Promise<{ message_id?: string; thread_id: string }> {
|
|
80
|
+
const dm = ctx.providers.dm;
|
|
81
|
+
const selfId = await dm.selfId();
|
|
82
|
+
const threadId = dmThreadId(selfId, to, threadLabel);
|
|
83
|
+
// DmProvider.send returns DmSendResult { id, deliveredAt } — read `id` (the old
|
|
84
|
+
// `message_id` lookup was always undefined, so the echoed id never populated).
|
|
85
|
+
const res = await dm.send({ to, threadId, payload: packEnvelope(envelope) });
|
|
86
|
+
return { message_id: (res as { id?: string })?.id, thread_id: threadId };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ---- Handlers ----
|
|
90
|
+
|
|
91
|
+
async function mDM_call_start(
|
|
92
|
+
input: z.infer<typeof mDM_call_start_input>,
|
|
93
|
+
ctx: MosaddToolContext,
|
|
94
|
+
): Promise<{
|
|
95
|
+
room_id: string;
|
|
96
|
+
token: string;
|
|
97
|
+
url: string;
|
|
98
|
+
identity: string;
|
|
99
|
+
mode: "audio" | "video";
|
|
100
|
+
invited: boolean;
|
|
101
|
+
thread_id: string;
|
|
102
|
+
}> {
|
|
103
|
+
const dm = ctx.providers.dm;
|
|
104
|
+
const selfId = await dm.selfId();
|
|
105
|
+
const mode = input.video ? "video" : "audio";
|
|
106
|
+
|
|
107
|
+
// Stable room label tied to the DM PAIR (no thread_label) so re-dials reuse one
|
|
108
|
+
// room AND the name stays short enough that both participants' full identity ids
|
|
109
|
+
// are always present — the livekit-token gate authorizes by full-id inclusion, so
|
|
110
|
+
// truncating an id would lock a real participant out. (thread_label still routes
|
|
111
|
+
// the call_invite message below, just not the room id.)
|
|
112
|
+
const label = `dmcall-${dmThreadId(selfId, input.to).replace(/[^a-zA-Z0-9._-]/g, "-")}`.slice(0, 120);
|
|
113
|
+
const { roomId } = await ctx.providers.voice.createRoom(label);
|
|
114
|
+
const ticket = await ctx.providers.voice.joinTicket(roomId);
|
|
115
|
+
|
|
116
|
+
let invited = false;
|
|
117
|
+
let thread_id = dmThreadId(selfId, input.to, input.thread_label);
|
|
118
|
+
try {
|
|
119
|
+
const sig = await signalDm(ctx, input.to, input.thread_label, {
|
|
120
|
+
type: "call_invite",
|
|
121
|
+
room_id: roomId,
|
|
122
|
+
mode,
|
|
123
|
+
from: selfId,
|
|
124
|
+
});
|
|
125
|
+
thread_id = sig.thread_id;
|
|
126
|
+
invited = true;
|
|
127
|
+
} catch (e) {
|
|
128
|
+
ctx.log("warn", "mDM_call_start: invite signal failed (room still usable)", {
|
|
129
|
+
error: (e as Error).message,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
ctx.log("debug", "mDM_call_start", { room_id: roomId, mode, invited });
|
|
134
|
+
return { room_id: roomId, token: ticket.token, url: ticket.url, identity: ticket.identity, mode, invited, thread_id };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function mDM_call_answer(
|
|
138
|
+
input: z.infer<typeof mDM_call_answer_input>,
|
|
139
|
+
ctx: MosaddToolContext,
|
|
140
|
+
): Promise<{ room_id: string; token: string; url: string; identity: string }> {
|
|
141
|
+
const ticket = await ctx.providers.voice.joinTicket(input.room_id);
|
|
142
|
+
ctx.log("debug", "mDM_call_answer", { room_id: ticket.roomId, identity: ticket.identity });
|
|
143
|
+
return { room_id: ticket.roomId, token: ticket.token, url: ticket.url, identity: ticket.identity };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function mDM_call_end(
|
|
147
|
+
input: z.infer<typeof mDM_call_end_input>,
|
|
148
|
+
ctx: MosaddToolContext,
|
|
149
|
+
): Promise<{ room_id: string; signalled: boolean }> {
|
|
150
|
+
let signalled = false;
|
|
151
|
+
if (input.to) {
|
|
152
|
+
try {
|
|
153
|
+
await signalDm(ctx, input.to, input.thread_label, { type: "call_end", room_id: input.room_id });
|
|
154
|
+
signalled = true;
|
|
155
|
+
} catch (e) {
|
|
156
|
+
ctx.log("warn", "mDM_call_end: hangup signal failed", { error: (e as Error).message });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
ctx.log("debug", "mDM_call_end", { room_id: input.room_id, signalled });
|
|
160
|
+
return { room_id: input.room_id, signalled };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function mDM_voice_note(
|
|
164
|
+
input: z.infer<typeof mDM_voice_note_input>,
|
|
165
|
+
ctx: MosaddToolContext,
|
|
166
|
+
): Promise<{ message_id?: string; thread_id: string }> {
|
|
167
|
+
const sig = await signalDm(ctx, input.to, input.thread_label, {
|
|
168
|
+
type: "voice_note",
|
|
169
|
+
audio_url: input.audio_url,
|
|
170
|
+
duration_ms: input.duration_ms ?? null,
|
|
171
|
+
mime: input.mime ?? "audio/ogg",
|
|
172
|
+
});
|
|
173
|
+
ctx.log("debug", "mDM_voice_note", { thread_id: sig.thread_id });
|
|
174
|
+
return sig;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ---- Registration ----
|
|
178
|
+
|
|
179
|
+
export const mdmVoiceTools: MosaddTool[] = [
|
|
180
|
+
{
|
|
181
|
+
name: "mDM_call_start",
|
|
182
|
+
title: "Start a voice call",
|
|
183
|
+
annotations: { readOnlyHint: false },
|
|
184
|
+
requires: "network",
|
|
185
|
+
description:
|
|
186
|
+
"Start a 1:1 real-time voice (or video) call with a contact — the ordinary full-duplex phone-style call (NOT walkie-talkie mTALK, NOT PSTN mCALL). Creates a room, returns join credentials (token + media url), and drops a `call_invite` control message in the DM thread so the contact can answer. Set video:true for video.",
|
|
187
|
+
inputSchema: mDM_call_start_input,
|
|
188
|
+
handler: mDM_call_start as MosaddTool["handler"],
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: "mDM_call_answer",
|
|
192
|
+
title: "Answer a voice call",
|
|
193
|
+
annotations: { readOnlyHint: false },
|
|
194
|
+
requires: "network",
|
|
195
|
+
description:
|
|
196
|
+
"Answer an incoming 1:1 call by its room_id (from the call_invite seen via mDM_list). Returns join credentials (token + media url) for the same room.",
|
|
197
|
+
inputSchema: mDM_call_answer_input,
|
|
198
|
+
handler: mDM_call_answer as MosaddTool["handler"],
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
name: "mDM_call_end",
|
|
202
|
+
title: "End a voice call",
|
|
203
|
+
annotations: { readOnlyHint: false },
|
|
204
|
+
requires: "network",
|
|
205
|
+
description:
|
|
206
|
+
"End a 1:1 call. The audio leg ends when clients disconnect; pass `to` to also drop a `call_end` (hangup) control message in the contact's DM thread.",
|
|
207
|
+
inputSchema: mDM_call_end_input,
|
|
208
|
+
handler: mDM_call_end as MosaddTool["handler"],
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: "mDM_voice_note",
|
|
212
|
+
title: "Leave a voice note",
|
|
213
|
+
annotations: { readOnlyHint: false },
|
|
214
|
+
requires: "network",
|
|
215
|
+
description:
|
|
216
|
+
"Send an asynchronous voice note to a contact: references an already-uploaded audio clip (audio_url) and posts it as a `voice_note` message in the DM thread. Use for fire-and-forget voice instead of a live call.",
|
|
217
|
+
inputSchema: mDM_voice_note_input,
|
|
218
|
+
handler: mDM_voice_note as MosaddTool["handler"],
|
|
219
|
+
},
|
|
220
|
+
];
|