@mosadd/mcp 3.0.0-alpha.46 → 3.0.0-alpha.48

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.
@@ -1,220 +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
- ];
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
+ ];