@mosadd/mcp 3.0.0-alpha.15 → 3.0.0-alpha.17
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 +71 -0
- package/README.md +44 -29
- package/dist/bin/mcp.js +38 -2
- package/dist/{chunk-ZT7IBWW7.js → chunk-5ZMJB5YU.js} +551 -31
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -1
- package/package.json +3 -2
- package/src/bin/mcp.ts +45 -0
- package/src/providers/blob-upload.ts +116 -0
- package/src/server.ts +6 -0
- package/src/tools/actions.ts +162 -0
- package/src/tools/attachments.ts +328 -0
- package/src/tools/index.ts +11 -1
- package/src/tools/mail-provenance.ts +137 -0
- package/src/tools/mail.ts +5 -2
- package/src/tools/ptt-ingest.ts +91 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Symmetric voice + file attachments across channels (mDM, mROOM, mIRC).
|
|
3
|
+
*
|
|
4
|
+
* GOAL (Lane C): an AGENT must be able to participate in every channel the same
|
|
5
|
+
* way a human does — including sending VOICE notes and FILES, not just text.
|
|
6
|
+
* Today only mDM has a half-measure (`mDM_voice_note`, which takes a pre-uploaded
|
|
7
|
+
* URL and posts a control envelope, NOT a first-class `voice` message via the
|
|
8
|
+
* `message-send` attachments contract). mROOM and mIRC have no voice/file send
|
|
9
|
+
* at all from the toolkit.
|
|
10
|
+
*
|
|
11
|
+
* These tools accept the blob as base64 (MCP is JSON-only), upload it to Storage
|
|
12
|
+
* via the shared `uploadBlob` helper, then call `message-send` with
|
|
13
|
+
* `message_type: 'voice' | 'file'` and the `attachments[]` descriptor.
|
|
14
|
+
*
|
|
15
|
+
* STATUS: scaffold against the Lane A contract. Compiles and uploads, but the
|
|
16
|
+
* `message-send` call is the not-yet-final shape — see TODO(Lane A) markers.
|
|
17
|
+
* mDM voice additionally needs client-side E2EE sealing before upload (TODO).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
22
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
23
|
+
import { uploadBlob, decodeBase64Body, type AttachmentDescriptor } from "../providers/blob-upload.js";
|
|
24
|
+
|
|
25
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
26
|
+
|
|
27
|
+
// ---- Shared schema fragments ----
|
|
28
|
+
|
|
29
|
+
const Base64Body = z
|
|
30
|
+
.string()
|
|
31
|
+
.min(1)
|
|
32
|
+
.max(20_000_000)
|
|
33
|
+
.describe("Attachment bytes as base64 (or a data: URL). MCP is JSON-only, so binary is base64-encoded. Max ~15 MB decoded.");
|
|
34
|
+
|
|
35
|
+
const Mime = z.string().min(1).max(120).describe("MIME type, e.g. audio/ogg, application/pdf, image/png.");
|
|
36
|
+
const DurationMs = z.number().int().positive().optional().describe("Audio clip length in ms (voice only), if known.");
|
|
37
|
+
const Filename = z.string().min(1).max(255).optional().describe("Original filename (file only).");
|
|
38
|
+
const ReplyTo = z.string().optional().describe("Optional id of the message being replied to.");
|
|
39
|
+
|
|
40
|
+
const ContactRef = z.string().min(1).describe("Recipient identity id (UUID). Use mDM_list_contacts to find it.");
|
|
41
|
+
const RoomId = z.string().min(1).describe("Room/channel id (UUID).");
|
|
42
|
+
|
|
43
|
+
const ThreadLabel = z
|
|
44
|
+
.string()
|
|
45
|
+
.max(120)
|
|
46
|
+
.regex(/^[a-zA-Z0-9._-]+$/)
|
|
47
|
+
.optional()
|
|
48
|
+
.describe("Optional thread label within the conversation (mDM USP: multiple threads per contact).");
|
|
49
|
+
|
|
50
|
+
// ---- Helpers ----
|
|
51
|
+
|
|
52
|
+
/** Deterministic DM thread id (sorted pair) — must match mdm.ts dmThreadId. */
|
|
53
|
+
function dmThreadId(selfId: string, otherId: string, label?: string): string {
|
|
54
|
+
const [a, b] = [selfId, otherId].sort();
|
|
55
|
+
return label ? `dm:${a}:${b}:${label}` : `dm:${a}:${b}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Post an attachment message through `message-send`.
|
|
60
|
+
*
|
|
61
|
+
* TODO(Lane A): lock the exact body shape. Current best-guess from the contract:
|
|
62
|
+
* { space_id, thread_id, message_type: 'voice'|'file', attachments: [desc],
|
|
63
|
+
* encrypted_payload: <caption envelope>, protocol_version, reply_to_id }
|
|
64
|
+
* Open questions for Lane A:
|
|
65
|
+
* - is `attachments` a top-level field on message-send, or nested under the
|
|
66
|
+
* payload? (assumed top-level here)
|
|
67
|
+
* - does voice/file still require a (possibly empty) encrypted_payload caption?
|
|
68
|
+
* - mDM: who seals the blob + mints key_ref — this tool, or the EF?
|
|
69
|
+
*/
|
|
70
|
+
async function sendAttachmentMessage(
|
|
71
|
+
ctx: MosaddToolContext,
|
|
72
|
+
args: {
|
|
73
|
+
space_id: string;
|
|
74
|
+
thread_id: string;
|
|
75
|
+
message_type: "voice" | "file";
|
|
76
|
+
attachment: AttachmentDescriptor;
|
|
77
|
+
caption?: string;
|
|
78
|
+
reply_to_id?: string;
|
|
79
|
+
recipient_account_id?: string;
|
|
80
|
+
},
|
|
81
|
+
): Promise<{ message_id: string; delivered_at: string; thread_id: string }> {
|
|
82
|
+
readSupabaseEnv();
|
|
83
|
+
const captionEnvelope = {
|
|
84
|
+
v: PROTOCOL_VERSION,
|
|
85
|
+
type: args.message_type,
|
|
86
|
+
text: args.caption ?? "",
|
|
87
|
+
sent_at: new Date().toISOString(),
|
|
88
|
+
};
|
|
89
|
+
const encrypted_payload = Buffer.from(JSON.stringify(captionEnvelope), "utf8").toString("base64");
|
|
90
|
+
|
|
91
|
+
ctx.log("debug", "sendAttachmentMessage", {
|
|
92
|
+
thread_id: args.thread_id,
|
|
93
|
+
message_type: args.message_type,
|
|
94
|
+
bucket: args.attachment.bucket,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const data = await invokeFunction<{ message?: { id: string; created_at: string } }>("message-send", {
|
|
98
|
+
space_id: args.space_id,
|
|
99
|
+
thread_id: args.thread_id,
|
|
100
|
+
message_type: args.message_type,
|
|
101
|
+
attachments: [args.attachment], // TODO(Lane A): confirm field placement
|
|
102
|
+
encrypted_payload,
|
|
103
|
+
protocol_version: PROTOCOL_VERSION,
|
|
104
|
+
recipient_account_id: args.recipient_account_id ?? null,
|
|
105
|
+
reply_to_id: args.reply_to_id ?? null,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (!data?.message?.id) throw new Error("message-send returned no message id (attachment)");
|
|
109
|
+
return {
|
|
110
|
+
message_id: data.message.id,
|
|
111
|
+
delivered_at: data.message.created_at ?? new Date().toISOString(),
|
|
112
|
+
thread_id: args.thread_id,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Resolve a channel's backing space id — mirrors mirc-messages.ts. */
|
|
117
|
+
async function resolveLinkedSpaceId(channelId: string): Promise<string> {
|
|
118
|
+
type GetResponse = { channel?: { metadata?: { linked_space_id?: string } | null }; linked_space_id?: string };
|
|
119
|
+
const data = await invokeFunction<GetResponse>("channel-manage", { action: "get", channel_id: channelId });
|
|
120
|
+
const spaceId = data?.linked_space_id ?? data?.channel?.metadata?.linked_space_id;
|
|
121
|
+
if (!spaceId) throw new Error(`Channel ${channelId} has no linked space yet — run mIRC_join/mIRC_list first or pass space_id.`);
|
|
122
|
+
return spaceId;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ====================================================================
|
|
126
|
+
// mDM voice/file
|
|
127
|
+
// ====================================================================
|
|
128
|
+
|
|
129
|
+
const mDM_send_voice_input = z.object({
|
|
130
|
+
to: ContactRef,
|
|
131
|
+
audio_base64: Base64Body,
|
|
132
|
+
mime: Mime.default("audio/ogg"),
|
|
133
|
+
duration_ms: DurationMs,
|
|
134
|
+
caption: z.string().max(2_000).optional(),
|
|
135
|
+
thread_label: ThreadLabel,
|
|
136
|
+
reply_to_id: ReplyTo,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const mDM_send_file_input = z.object({
|
|
140
|
+
to: ContactRef,
|
|
141
|
+
file_base64: Base64Body,
|
|
142
|
+
filename: Filename,
|
|
143
|
+
mime: Mime,
|
|
144
|
+
caption: z.string().max(2_000).optional(),
|
|
145
|
+
thread_label: ThreadLabel,
|
|
146
|
+
reply_to_id: ReplyTo,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
async function mDM_send_voice(input: z.infer<typeof mDM_send_voice_input>, ctx: MosaddToolContext) {
|
|
150
|
+
const dm = ctx.providers.dm;
|
|
151
|
+
const selfId = await dm.selfId();
|
|
152
|
+
const thread_id = dmThreadId(selfId, input.to, input.thread_label);
|
|
153
|
+
// TODO(Lane A/E2EE): mDM is client-encrypted — seal these bytes per-recipient
|
|
154
|
+
// via @mosadd/crypto and pass the wrapped content key as key_ref BEFORE upload.
|
|
155
|
+
// Uploading plaintext here is a scaffold-only shortcut.
|
|
156
|
+
const attachment = await uploadBlob({
|
|
157
|
+
kind: "voice",
|
|
158
|
+
bytes: decodeBase64Body(input.audio_base64),
|
|
159
|
+
mime: input.mime,
|
|
160
|
+
prefix: thread_id,
|
|
161
|
+
duration_ms: input.duration_ms,
|
|
162
|
+
});
|
|
163
|
+
return sendAttachmentMessage(ctx, {
|
|
164
|
+
space_id: "dm",
|
|
165
|
+
thread_id,
|
|
166
|
+
message_type: "voice",
|
|
167
|
+
attachment,
|
|
168
|
+
caption: input.caption,
|
|
169
|
+
reply_to_id: input.reply_to_id,
|
|
170
|
+
recipient_account_id: input.to,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function mDM_send_file(input: z.infer<typeof mDM_send_file_input>, ctx: MosaddToolContext) {
|
|
175
|
+
const dm = ctx.providers.dm;
|
|
176
|
+
const selfId = await dm.selfId();
|
|
177
|
+
const thread_id = dmThreadId(selfId, input.to, input.thread_label);
|
|
178
|
+
// TODO(Lane A/E2EE): seal as above for mDM.
|
|
179
|
+
const attachment = await uploadBlob({
|
|
180
|
+
kind: "file",
|
|
181
|
+
bytes: decodeBase64Body(input.file_base64),
|
|
182
|
+
mime: input.mime,
|
|
183
|
+
prefix: thread_id,
|
|
184
|
+
filename: input.filename,
|
|
185
|
+
});
|
|
186
|
+
return sendAttachmentMessage(ctx, {
|
|
187
|
+
space_id: "dm",
|
|
188
|
+
thread_id,
|
|
189
|
+
message_type: "file",
|
|
190
|
+
attachment,
|
|
191
|
+
caption: input.caption,
|
|
192
|
+
reply_to_id: input.reply_to_id,
|
|
193
|
+
recipient_account_id: input.to,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ====================================================================
|
|
198
|
+
// mROOM voice/file
|
|
199
|
+
// ====================================================================
|
|
200
|
+
|
|
201
|
+
const mROOM_send_voice_input = z.object({
|
|
202
|
+
room_id: RoomId,
|
|
203
|
+
audio_base64: Base64Body,
|
|
204
|
+
mime: Mime.default("audio/ogg"),
|
|
205
|
+
duration_ms: DurationMs,
|
|
206
|
+
caption: z.string().max(2_000).optional(),
|
|
207
|
+
reply_to_id: ReplyTo,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const mROOM_send_file_input = z.object({
|
|
211
|
+
room_id: RoomId,
|
|
212
|
+
file_base64: Base64Body,
|
|
213
|
+
filename: Filename,
|
|
214
|
+
mime: Mime,
|
|
215
|
+
caption: z.string().max(2_000).optional(),
|
|
216
|
+
reply_to_id: ReplyTo,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
async function mROOM_send_voice(input: z.infer<typeof mROOM_send_voice_input>, ctx: MosaddToolContext) {
|
|
220
|
+
const thread_id = `privroom:${input.room_id}`;
|
|
221
|
+
const attachment = await uploadBlob({
|
|
222
|
+
kind: "voice",
|
|
223
|
+
bytes: decodeBase64Body(input.audio_base64),
|
|
224
|
+
mime: input.mime,
|
|
225
|
+
prefix: thread_id,
|
|
226
|
+
duration_ms: input.duration_ms,
|
|
227
|
+
});
|
|
228
|
+
return sendAttachmentMessage(ctx, { space_id: "rooms", thread_id, message_type: "voice", attachment, caption: input.caption, reply_to_id: input.reply_to_id });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function mROOM_send_file(input: z.infer<typeof mROOM_send_file_input>, ctx: MosaddToolContext) {
|
|
232
|
+
const thread_id = `privroom:${input.room_id}`;
|
|
233
|
+
const attachment = await uploadBlob({
|
|
234
|
+
kind: "file",
|
|
235
|
+
bytes: decodeBase64Body(input.file_base64),
|
|
236
|
+
mime: input.mime,
|
|
237
|
+
prefix: thread_id,
|
|
238
|
+
filename: input.filename,
|
|
239
|
+
});
|
|
240
|
+
return sendAttachmentMessage(ctx, { space_id: "rooms", thread_id, message_type: "file", attachment, caption: input.caption, reply_to_id: input.reply_to_id });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ====================================================================
|
|
244
|
+
// mIRC voice/file
|
|
245
|
+
// ====================================================================
|
|
246
|
+
|
|
247
|
+
const mIRC_send_voice_input = z.object({
|
|
248
|
+
channel_id: RoomId,
|
|
249
|
+
audio_base64: Base64Body,
|
|
250
|
+
mime: Mime.default("audio/ogg"),
|
|
251
|
+
duration_ms: DurationMs,
|
|
252
|
+
caption: z.string().max(2_000).optional(),
|
|
253
|
+
reply_to_id: ReplyTo,
|
|
254
|
+
space_id: z.string().optional().describe("Backing space id. Resolved automatically if omitted."),
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const mIRC_send_file_input = z.object({
|
|
258
|
+
channel_id: RoomId,
|
|
259
|
+
file_base64: Base64Body,
|
|
260
|
+
filename: Filename,
|
|
261
|
+
mime: Mime,
|
|
262
|
+
caption: z.string().max(2_000).optional(),
|
|
263
|
+
reply_to_id: ReplyTo,
|
|
264
|
+
space_id: z.string().optional(),
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
async function mIRC_send_voice(input: z.infer<typeof mIRC_send_voice_input>, ctx: MosaddToolContext) {
|
|
268
|
+
const space_id = input.space_id ?? (await resolveLinkedSpaceId(input.channel_id));
|
|
269
|
+
const thread_id = `chat:${input.channel_id}`;
|
|
270
|
+
const attachment = await uploadBlob({ kind: "voice", bytes: decodeBase64Body(input.audio_base64), mime: input.mime, prefix: thread_id, duration_ms: input.duration_ms });
|
|
271
|
+
return sendAttachmentMessage(ctx, { space_id, thread_id, message_type: "voice", attachment, caption: input.caption, reply_to_id: input.reply_to_id });
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function mIRC_send_file(input: z.infer<typeof mIRC_send_file_input>, ctx: MosaddToolContext) {
|
|
275
|
+
const space_id = input.space_id ?? (await resolveLinkedSpaceId(input.channel_id));
|
|
276
|
+
const thread_id = `chat:${input.channel_id}`;
|
|
277
|
+
const attachment = await uploadBlob({ kind: "file", bytes: decodeBase64Body(input.file_base64), mime: input.mime, prefix: thread_id, filename: input.filename });
|
|
278
|
+
return sendAttachmentMessage(ctx, { space_id, thread_id, message_type: "file", attachment, caption: input.caption, reply_to_id: input.reply_to_id });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ---- Registration ----
|
|
282
|
+
|
|
283
|
+
export const attachmentTools: MosaddTool[] = [
|
|
284
|
+
{
|
|
285
|
+
name: "mDM_send_voice",
|
|
286
|
+
requires: "network",
|
|
287
|
+
description:
|
|
288
|
+
"Send a VOICE note to a contact as a first-class mDM `voice` message (not a URL stub). Pass audio bytes as base64; the tool uploads to Storage and posts via message-send with the voice attachment descriptor. SCAFFOLD: mDM is client-encrypted — until the seal step ships, bytes are uploaded unsealed (TODO Lane A).",
|
|
289
|
+
inputSchema: mDM_send_voice_input,
|
|
290
|
+
handler: mDM_send_voice as MosaddTool["handler"],
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
name: "mDM_send_file",
|
|
294
|
+
requires: "network",
|
|
295
|
+
description:
|
|
296
|
+
"Send a FILE to a contact as a first-class mDM `file` message. Base64 bytes → Storage → message-send file attachment. SCAFFOLD: mDM E2EE seal is TODO (Lane A).",
|
|
297
|
+
inputSchema: mDM_send_file_input,
|
|
298
|
+
handler: mDM_send_file as MosaddTool["handler"],
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
name: "mROOM_send_voice",
|
|
302
|
+
requires: "network",
|
|
303
|
+
description: "Send a VOICE note into a room as a first-class `voice` message. Base64 audio → Storage → message-send. Be a room member first (mROOM_join).",
|
|
304
|
+
inputSchema: mROOM_send_voice_input,
|
|
305
|
+
handler: mROOM_send_voice as MosaddTool["handler"],
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
name: "mROOM_send_file",
|
|
309
|
+
requires: "network",
|
|
310
|
+
description: "Send a FILE into a room as a first-class `file` message. Base64 bytes → Storage → message-send.",
|
|
311
|
+
inputSchema: mROOM_send_file_input,
|
|
312
|
+
handler: mROOM_send_file as MosaddTool["handler"],
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
name: "mIRC_send_voice",
|
|
316
|
+
requires: "network",
|
|
317
|
+
description: "Send a VOICE note into a persistent channel as a first-class `voice` message. Base64 audio → Storage → message-send (backing space resolved automatically).",
|
|
318
|
+
inputSchema: mIRC_send_voice_input,
|
|
319
|
+
handler: mIRC_send_voice as MosaddTool["handler"],
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
name: "mIRC_send_file",
|
|
323
|
+
requires: "network",
|
|
324
|
+
description: "Send a FILE into a persistent channel as a first-class `file` message. Base64 bytes → Storage → message-send.",
|
|
325
|
+
inputSchema: mIRC_send_file_input,
|
|
326
|
+
handler: mIRC_send_file as MosaddTool["handler"],
|
|
327
|
+
},
|
|
328
|
+
];
|
package/src/tools/index.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tool registry. Each m* module contributes a set of tools.
|
|
3
3
|
*
|
|
4
|
-
* Live modules (6): mDM, mIRC, mROOM, mTALK, mAIL, mRAG —
|
|
4
|
+
* Live modules (6): mDM, mIRC, mROOM, mTALK, mAIL, mRAG — 70 tools + comms_capabilities.
|
|
5
|
+
* Plus action links: comms_action_create (agent → user one-link browser action,
|
|
6
|
+
* incl. screen_share) + comms_action_frame_get (see the recipient's shared screen).
|
|
5
7
|
* mURL is a consumer/brand surface (murl.mosadd.com), NOT a dev product — its tools
|
|
6
8
|
* live in tools/murl.ts but are NOT registered here (founder decision 2026-06-10).
|
|
7
9
|
* mCALL is carrier-pending (no telephony carrier live yet): its tools live in
|
|
@@ -19,9 +21,13 @@ import { mircEdgeTools } from "./mirc-edge.js";
|
|
|
19
21
|
import { mroomTools } from "./mroom.js";
|
|
20
22
|
import { mroomMessagesTools } from "./mroom-messages.js";
|
|
21
23
|
import { mailTools } from "./mail.js";
|
|
24
|
+
import { mailProvenanceTools } from "./mail-provenance.js";
|
|
22
25
|
import { mtalkTools } from "./mtalk.js";
|
|
26
|
+
import { attachmentTools } from "./attachments.js";
|
|
27
|
+
import { pttIngestTools } from "./ptt-ingest.js";
|
|
23
28
|
// import { mcallTools } from "./mcall.js"; // mCALL: carrier-pending — not registered (see header note)
|
|
24
29
|
import { knowledgeTools } from "./knowledge.js";
|
|
30
|
+
import { actionTools } from "./actions.js";
|
|
25
31
|
// import { murlTools } from "./murl.js"; // mURL: brand/consumer surface, not a dev product — not registered
|
|
26
32
|
import { makeCapabilitiesTool } from "./capabilities.js";
|
|
27
33
|
|
|
@@ -36,8 +42,12 @@ const channelTools: MosaddTool[] = [
|
|
|
36
42
|
...mroomTools,
|
|
37
43
|
...mroomMessagesTools,
|
|
38
44
|
...mailTools,
|
|
45
|
+
...mailProvenanceTools,
|
|
39
46
|
...mtalkTools,
|
|
47
|
+
...attachmentTools,
|
|
48
|
+
...pttIngestTools,
|
|
40
49
|
...knowledgeTools,
|
|
50
|
+
...actionTools, // Action links (Tier 1): agent → user one-link browser action via action-create EF
|
|
41
51
|
// ...murlTools, // mURL: brand/consumer surface (murl.mosadd.com), not a dev product
|
|
42
52
|
// ...mcallTools, // mCALL: carrier-pending (no telephony carrier live) — re-register when a carrier is configured
|
|
43
53
|
// mirlTools,
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mAIL provenance — agent-attributed email send.
|
|
3
|
+
*
|
|
4
|
+
* The plain `mAIL_send` (mail.ts) sends as the USER. When an AGENT sends mail on
|
|
5
|
+
* the user's behalf, the recipient (and the audit trail) should be able to tell
|
|
6
|
+
* it was agent-originated. Per the Lane A contract, `mp0st-send`, when called
|
|
7
|
+
* with a HUB-CLAIM JWT, stamps the row with:
|
|
8
|
+
* sent_by = 'agent', sent_by_agent_id, sent_by_task_id
|
|
9
|
+
*
|
|
10
|
+
* This tool mints/forwards that hub-claim JWT and sets the provenance fields.
|
|
11
|
+
*
|
|
12
|
+
* AUTH MODEL (differs from the rest of mAIL):
|
|
13
|
+
* - mAIL_send uses the user's Supabase session JWT (via invokeFunction).
|
|
14
|
+
* - mAIL_send_as_agent presents a HUB-CLAIM JWT (hub key → claim exchange) so
|
|
15
|
+
* mp0st-send can attribute the send to the agent/task.
|
|
16
|
+
*
|
|
17
|
+
* STATUS: scaffold. The hub-claim exchange endpoint is assumed (TODO Lane A —
|
|
18
|
+
* confirm the exact route + claim shape). Falls back to the user session JWT if
|
|
19
|
+
* no hub key is configured, with provenance fields passed through for the EF to
|
|
20
|
+
* honor once deployed.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { z } from "zod";
|
|
24
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
25
|
+
|
|
26
|
+
const DEFAULT_SUPABASE_URL = "https://rooffhgbxafyjcwmwpsy.supabase.co";
|
|
27
|
+
|
|
28
|
+
function supabaseUrl(): string {
|
|
29
|
+
return (
|
|
30
|
+
process.env.MOSADD_SUPABASE_URL?.replace(/\/$/, "") ??
|
|
31
|
+
process.env.SUPABASE_URL?.replace(/\/$/, "") ??
|
|
32
|
+
DEFAULT_SUPABASE_URL
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hubKey(): string | null {
|
|
37
|
+
return process.env.MOSADD_API_KEY ?? process.env.MOSADD_HUB_KEY ?? null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const EmailAddress = z.string().email().max(254);
|
|
41
|
+
|
|
42
|
+
const mAIL_send_as_agent_input = z.object({
|
|
43
|
+
to: z.union([EmailAddress, z.array(EmailAddress).min(1).max(50)]),
|
|
44
|
+
subject: z.string().min(1).max(998),
|
|
45
|
+
body_text: z.string().max(1_048_576).optional(),
|
|
46
|
+
body_html: z.string().max(1_048_576).optional(),
|
|
47
|
+
cc: z.array(EmailAddress).max(50).optional(),
|
|
48
|
+
bcc: z.array(EmailAddress).max(50).optional(),
|
|
49
|
+
reply_to: EmailAddress.optional(),
|
|
50
|
+
tracking: z.boolean().optional(),
|
|
51
|
+
agent_id: z
|
|
52
|
+
.string()
|
|
53
|
+
.min(1)
|
|
54
|
+
.max(120)
|
|
55
|
+
.describe("Stable id of the agent doing the send. Stamped as sent_by_agent_id for provenance."),
|
|
56
|
+
task_id: z
|
|
57
|
+
.string()
|
|
58
|
+
.min(1)
|
|
59
|
+
.max(120)
|
|
60
|
+
.optional()
|
|
61
|
+
.describe("Optional id of the task/run this send belongs to. Stamped as sent_by_task_id."),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Exchange the hub key for a hub-claim JWT that authorizes agent-attributed
|
|
66
|
+
* actions. TODO(Lane A): confirm endpoint + response. Mirrors the
|
|
67
|
+
* hub-mint-channel-token pattern in mirc-edge.ts.
|
|
68
|
+
*/
|
|
69
|
+
async function mintHubClaim(agentId: string, taskId?: string): Promise<string> {
|
|
70
|
+
const key = hubKey();
|
|
71
|
+
if (!key) {
|
|
72
|
+
throw new Error("mAIL_send_as_agent needs MOSADD_API_KEY (hub key) to mint a provenance claim.");
|
|
73
|
+
}
|
|
74
|
+
const r = await fetch(`${supabaseUrl()}/functions/v1/hub-claim-mint`, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}` },
|
|
77
|
+
body: JSON.stringify({ scope: "mail:send", agent_id: agentId, task_id: taskId ?? null }),
|
|
78
|
+
});
|
|
79
|
+
if (!r.ok) {
|
|
80
|
+
const b = await r.text().catch(() => "");
|
|
81
|
+
throw new Error(`hub-claim-mint ${r.status}: ${b.slice(0, 200)}`);
|
|
82
|
+
}
|
|
83
|
+
const data = (await r.json()) as { token?: string };
|
|
84
|
+
if (!data?.token) throw new Error("hub-claim-mint: no token in response");
|
|
85
|
+
return data.token;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function mAIL_send_as_agent(
|
|
89
|
+
input: z.infer<typeof mAIL_send_as_agent_input>,
|
|
90
|
+
ctx: MosaddToolContext,
|
|
91
|
+
): Promise<{ message_id: string; status: string; is_internal: boolean; sent_by: "agent" }> {
|
|
92
|
+
if (!input.body_text && !input.body_html) {
|
|
93
|
+
throw new Error("mAIL_send_as_agent requires at least one of body_text or body_html.");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Present the hub-claim JWT directly to mp0st-send (NOT the user session client)
|
|
97
|
+
// so the EF attributes the row to the agent. TODO(Lane A): confirm mp0st-send
|
|
98
|
+
// accepts a Bearer hub-claim and reads sent_by_agent_id / sent_by_task_id.
|
|
99
|
+
const claim = await mintHubClaim(input.agent_id, input.task_id);
|
|
100
|
+
ctx.log("debug", "mAIL_send_as_agent invoking mp0st-send with hub-claim", { agent_id: input.agent_id });
|
|
101
|
+
|
|
102
|
+
const { agent_id, task_id, ...mailFields } = input;
|
|
103
|
+
const r = await fetch(`${supabaseUrl()}/functions/v1/mp0st-send`, {
|
|
104
|
+
method: "POST",
|
|
105
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${claim}` },
|
|
106
|
+
body: JSON.stringify({
|
|
107
|
+
...mailFields,
|
|
108
|
+
sent_by: "agent",
|
|
109
|
+
sent_by_agent_id: agent_id,
|
|
110
|
+
sent_by_task_id: task_id ?? null,
|
|
111
|
+
}),
|
|
112
|
+
});
|
|
113
|
+
if (!r.ok) {
|
|
114
|
+
const b = await r.text().catch(() => "");
|
|
115
|
+
throw new Error(`mp0st-send ${r.status}: ${b.slice(0, 200)}`);
|
|
116
|
+
}
|
|
117
|
+
const res = (await r.json()) as { email_id?: string; status?: string; is_internal?: boolean; error?: string };
|
|
118
|
+
if (res.error) throw new Error(res.error);
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
message_id: res.email_id ?? "",
|
|
122
|
+
status: res.status ?? "sent",
|
|
123
|
+
is_internal: res.is_internal ?? false,
|
|
124
|
+
sent_by: "agent",
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const mailProvenanceTools: MosaddTool[] = [
|
|
129
|
+
{
|
|
130
|
+
name: "mAIL_send_as_agent",
|
|
131
|
+
requires: "network",
|
|
132
|
+
description:
|
|
133
|
+
"Send an email ATTRIBUTED TO THE AGENT (provenance-stamped). Unlike mAIL_send (sends as the user), this stamps sent_by='agent' plus your agent_id and optional task_id onto the email record and audit trail — so recipients and compliance can see the mail was agent-originated. Auth: presents a hub-claim JWT (needs MOSADD_API_KEY). SCAFFOLD: depends on mp0st-send honoring the hub-claim + provenance fields (TODO Lane A).",
|
|
134
|
+
inputSchema: mAIL_send_as_agent_input,
|
|
135
|
+
handler: mAIL_send_as_agent as MosaddTool["handler"],
|
|
136
|
+
},
|
|
137
|
+
];
|
package/src/tools/mail.ts
CHANGED
|
@@ -153,8 +153,11 @@ async function mAIL_view(
|
|
|
153
153
|
received_at: string;
|
|
154
154
|
}> {
|
|
155
155
|
readSupabaseEnv();
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
// mp0st-get (NOT mp0st-view): mp0st-view is the open-tracking pixel endpoint
|
|
157
|
+
// keyed on ?t=<trackingId> and always 400s "Missing tracking ID" on a JSON POST.
|
|
158
|
+
// mp0st-get reads the owner-scoped email by id and returns the full body.
|
|
159
|
+
ctx.log("debug", "mAIL_view invoking mp0st-get", { message_id: input.message_id });
|
|
160
|
+
return await invokeFunction("mp0st-get", { message_id: input.message_id });
|
|
158
161
|
}
|
|
159
162
|
|
|
160
163
|
async function mAIL_list(
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PTT capture → ingest → RAG transcription.
|
|
3
|
+
*
|
|
4
|
+
* mTALK (mtalk.ts) drives the live half-duplex FLOOR (who may transmit). What it
|
|
5
|
+
* does NOT do is capture a finished PTT transmission as a durable, queued message
|
|
6
|
+
* — i.e. a recorded "over" that lands in a channel's queued-PTT log AND (opt-in)
|
|
7
|
+
* gets transcribed into RAG. That's this module.
|
|
8
|
+
*
|
|
9
|
+
* Flow (per the Lane A contract):
|
|
10
|
+
* 1. agent records/has a PTT clip → base64 → uploadBlob(kind:'ptt') to
|
|
11
|
+
* 'voice-blobs'.
|
|
12
|
+
* 2. call `ptt-ingest` EF with the attachment descriptor + routing (which
|
|
13
|
+
* channel/room the "over" belongs to). The EF persists a queued-PTT message.
|
|
14
|
+
* 3. if the user has opted in to RAG, `rag-transcribe` runs and the transcript
|
|
15
|
+
* lands in user_embeddings(source_type='ptt').
|
|
16
|
+
*
|
|
17
|
+
* STATUS: scaffold — `ptt-ingest` is not yet deployed. Upload is real; the EF
|
|
18
|
+
* call shape is best-guess (TODO Lane A).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
23
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
24
|
+
import { uploadBlob, decodeBase64Body } from "../providers/blob-upload.js";
|
|
25
|
+
|
|
26
|
+
const mTALK_ingest_ptt_input = z.object({
|
|
27
|
+
audio_base64: z
|
|
28
|
+
.string()
|
|
29
|
+
.min(1)
|
|
30
|
+
.max(20_000_000)
|
|
31
|
+
.describe("Recorded PTT 'over' as base64 (or data: URL)."),
|
|
32
|
+
mime: z.string().min(1).max(120).default("audio/ogg"),
|
|
33
|
+
duration_ms: z.number().int().positive().optional(),
|
|
34
|
+
// Routing: exactly one of room_id / channel_id identifies where the over lands.
|
|
35
|
+
channel_id: z.string().optional().describe("mIRC channel id — route the queued PTT into a persistent channel's PTT log."),
|
|
36
|
+
room_id: z.string().optional().describe("mROOM/mTALK room id — route into an ephemeral room's PTT log."),
|
|
37
|
+
transcribe: z
|
|
38
|
+
.boolean()
|
|
39
|
+
.optional()
|
|
40
|
+
.describe("Request RAG transcription. Honored only if the user has opted in to rag ingest; ignored otherwise. Default true."),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
async function mTALK_ingest_ptt(
|
|
44
|
+
input: z.infer<typeof mTALK_ingest_ptt_input>,
|
|
45
|
+
ctx: MosaddToolContext,
|
|
46
|
+
): Promise<{ ptt_id: string; queued: true; transcription: "queued" | "skipped" | "opted_out" }> {
|
|
47
|
+
readSupabaseEnv();
|
|
48
|
+
if (!input.channel_id && !input.room_id) {
|
|
49
|
+
throw new Error("mTALK_ingest_ptt requires one of channel_id or room_id to route the queued PTT.");
|
|
50
|
+
}
|
|
51
|
+
const prefix = input.channel_id ? `chat:${input.channel_id}` : `room:${input.room_id}`;
|
|
52
|
+
const attachment = await uploadBlob({
|
|
53
|
+
kind: "ptt",
|
|
54
|
+
bytes: decodeBase64Body(input.audio_base64),
|
|
55
|
+
mime: input.mime,
|
|
56
|
+
prefix,
|
|
57
|
+
duration_ms: input.duration_ms,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
ctx.log("debug", "mTALK_ingest_ptt invoking ptt-ingest", { prefix, transcribe: input.transcribe ?? true });
|
|
61
|
+
|
|
62
|
+
// TODO(Lane A): `ptt-ingest` EF not yet deployed. Confirm request/response shape.
|
|
63
|
+
// Expected: persists a queued-PTT message_meta row (message_type:'ptt') with the
|
|
64
|
+
// attachment, then (if rag opt-in) enqueues rag-transcribe → user_embeddings(source_type='ptt').
|
|
65
|
+
const res = await invokeFunction<{
|
|
66
|
+
ptt_id?: string;
|
|
67
|
+
transcription?: "queued" | "skipped" | "opted_out";
|
|
68
|
+
}>("ptt-ingest", {
|
|
69
|
+
attachment,
|
|
70
|
+
channel_id: input.channel_id ?? null,
|
|
71
|
+
room_id: input.room_id ?? null,
|
|
72
|
+
transcribe: input.transcribe ?? true,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
ptt_id: res.ptt_id ?? "",
|
|
77
|
+
queued: true,
|
|
78
|
+
transcription: res.transcription ?? "queued",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const pttIngestTools: MosaddTool[] = [
|
|
83
|
+
{
|
|
84
|
+
name: "mTALK_ingest_ptt",
|
|
85
|
+
requires: "network",
|
|
86
|
+
description:
|
|
87
|
+
"Capture a finished PTT transmission (an 'over') as a durable, QUEUED PTT message in a channel (mIRC) or room (mROOM), and — if the user has opted in to RAG — transcribe it into searchable knowledge. Pass the clip as base64 + a route (channel_id OR room_id). Complements mTALK_press/release (which only manage the live floor). SCAFFOLD: depends on the not-yet-deployed `ptt-ingest` Edge Function.",
|
|
88
|
+
inputSchema: mTALK_ingest_ptt_input,
|
|
89
|
+
handler: mTALK_ingest_ptt as MosaddTool["handler"],
|
|
90
|
+
},
|
|
91
|
+
];
|