@mosadd/mcp 3.0.0-alpha.3 → 3.0.0-alpha.4
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/README.md +116 -116
- package/dist/bin/mcp.js +1 -1
- package/dist/{chunk-HD5RLE5K.js → chunk-YDFU6S5E.js} +326 -48
- package/dist/index.d.ts +22 -1
- package/dist/index.js +1 -1
- package/package.json +6 -6
- package/src/__tests__/golden-path.test.ts +137 -132
- package/src/__tests__/integration.test.ts +178 -175
- package/src/__tests__/mdm-e2ee.test.ts +178 -149
- package/src/__tests__/mtalk.test.ts +122 -0
- package/src/__tests__/server.test.ts +67 -67
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +97 -97
- package/src/config.ts +66 -66
- package/src/crypto/mdm-session.ts +403 -378
- package/src/index.ts +17 -17
- package/src/observability/sentry.ts +79 -79
- package/src/providers/memory-voice.ts +79 -0
- package/src/providers/supabase-dm.ts +117 -117
- package/src/providers/supabase-voice.ts +91 -0
- package/src/providers/supabase.ts +96 -96
- package/src/server.ts +105 -103
- package/src/tools/capabilities.ts +68 -68
- package/src/tools/index.ts +43 -37
- package/src/tools/knowledge.ts +91 -0
- package/src/tools/mail.ts +89 -89
- package/src/tools/mdm.ts +392 -379
- package/src/tools/mirc-members.ts +177 -177
- package/src/tools/mirc-messages.ts +151 -0
- package/src/tools/mirc.ts +170 -170
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +189 -189
- package/src/tools/mtalk.ts +121 -0
- package/src/types.ts +85 -79
- package/src/voice/floor.ts +132 -0
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mIRC channel members — join, leave, kick, ban, set-role, set-ptt.
|
|
3
|
-
*
|
|
4
|
-
* Wraps the m0ssad-3 `channel-members-manage` Edge Function (audit ab6c885f),
|
|
5
|
-
* which dispatches on the `action` body field.
|
|
6
|
-
*
|
|
7
|
-
* Split from packages/mcp/src/tools/mirc.ts because the channel-management
|
|
8
|
-
* surface and the member-management surface have different ergonomics — a
|
|
9
|
-
* model that handles roster ops shouldn't have to read past 5 'channel CRUD'
|
|
10
|
-
* tools to find them.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { z } from "zod";
|
|
14
|
-
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
15
|
-
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
16
|
-
|
|
17
|
-
// ---- Schemas ----
|
|
18
|
-
|
|
19
|
-
const ChannelId = z
|
|
20
|
-
.string()
|
|
21
|
-
.min(1)
|
|
22
|
-
.max(120)
|
|
23
|
-
.describe("Channel id (UUID). From mIRC_list / mIRC_create.");
|
|
24
|
-
|
|
25
|
-
const MemberIdentity = z
|
|
26
|
-
.string()
|
|
27
|
-
.min(1)
|
|
28
|
-
.describe("Target user's identity id (UUID). From mDM_list_contacts or radar logs.");
|
|
29
|
-
|
|
30
|
-
const Role = z
|
|
31
|
-
.enum(["owner", "admin", "moderator", "member", "guest"])
|
|
32
|
-
.describe("Member role within the channel.");
|
|
33
|
-
|
|
34
|
-
const mIRC_join_input = z.object({
|
|
35
|
-
channel_id: ChannelId,
|
|
36
|
-
password: z.string().min(1).optional().describe("Required if the channel is access_mode=private."),
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const mIRC_request_access_input = z.object({
|
|
40
|
-
channel_id: ChannelId,
|
|
41
|
-
message: z.string().max(500).optional().describe("Optional note for the channel owner explaining why."),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const mIRC_leave_input = z.object({
|
|
45
|
-
channel_id: ChannelId,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const mIRC_approve_request_input = z.object({
|
|
49
|
-
channel_id: ChannelId,
|
|
50
|
-
request_id: z.string().min(1).describe("Pending request id."),
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const mIRC_reject_request_input = mIRC_approve_request_input;
|
|
54
|
-
|
|
55
|
-
const mIRC_kick_input = z.object({
|
|
56
|
-
channel_id: ChannelId,
|
|
57
|
-
identity_id: MemberIdentity,
|
|
58
|
-
reason: z.string().max(500).optional(),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
const mIRC_ban_input = z.object({
|
|
62
|
-
channel_id: ChannelId,
|
|
63
|
-
identity_id: MemberIdentity,
|
|
64
|
-
reason: z.string().max(500).optional(),
|
|
65
|
-
/** Optional unban deadline (ISO-8601). Omit for permanent ban. */
|
|
66
|
-
until: z.string().optional(),
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
const mIRC_unban_input = z.object({
|
|
70
|
-
channel_id: ChannelId,
|
|
71
|
-
identity_id: MemberIdentity,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const mIRC_set_role_input = z.object({
|
|
75
|
-
channel_id: ChannelId,
|
|
76
|
-
identity_id: MemberIdentity,
|
|
77
|
-
role: Role,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const mIRC_set_ptt_input = z.object({
|
|
81
|
-
channel_id: ChannelId,
|
|
82
|
-
identity_id: MemberIdentity,
|
|
83
|
-
ptt_enabled: z.boolean().describe("Allow this member to hold the PTT floor in voice mode."),
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// ---- Handlers ----
|
|
87
|
-
|
|
88
|
-
async function invokeChannelMembers(
|
|
89
|
-
action: string,
|
|
90
|
-
body: Record<string, unknown>,
|
|
91
|
-
ctx: MosaddToolContext,
|
|
92
|
-
): Promise<unknown> {
|
|
93
|
-
readSupabaseEnv();
|
|
94
|
-
ctx.log("debug", `mIRC.${action}`, body);
|
|
95
|
-
return await invokeFunction("channel-members-manage", { action, ...body });
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// ---- Registration ----
|
|
99
|
-
|
|
100
|
-
export const mircMembersTools: MosaddTool[] = [
|
|
101
|
-
{
|
|
102
|
-
name: "mIRC_join",
|
|
103
|
-
requires: "network",
|
|
104
|
-
description:
|
|
105
|
-
"Join a channel as the current user. For private channels pass `password`. Returns a membership row or 409 if already a member.",
|
|
106
|
-
inputSchema: mIRC_join_input,
|
|
107
|
-
handler: ((input, ctx) => invokeChannelMembers("join", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
name: "mIRC_request_access",
|
|
111
|
-
requires: "network",
|
|
112
|
-
description:
|
|
113
|
-
"Request access to an invite_only channel. Owner / admins get a notification with the optional message.",
|
|
114
|
-
inputSchema: mIRC_request_access_input,
|
|
115
|
-
handler: ((input, ctx) => invokeChannelMembers("request-access", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
name: "mIRC_leave",
|
|
119
|
-
requires: "network",
|
|
120
|
-
description: "Leave a channel as the current user. Owner cannot leave without transferring ownership first.",
|
|
121
|
-
inputSchema: mIRC_leave_input,
|
|
122
|
-
handler: ((input, ctx) => invokeChannelMembers("leave", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
123
|
-
},
|
|
124
|
-
{
|
|
125
|
-
name: "mIRC_approve_request",
|
|
126
|
-
requires: "network",
|
|
127
|
-
description: "Approve a pending access request. Admins / moderators / owner only.",
|
|
128
|
-
inputSchema: mIRC_approve_request_input,
|
|
129
|
-
handler: ((input, ctx) => invokeChannelMembers("approve-request", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
name: "mIRC_reject_request",
|
|
133
|
-
requires: "network",
|
|
134
|
-
description: "Reject a pending access request. Admins / moderators / owner only.",
|
|
135
|
-
inputSchema: mIRC_reject_request_input,
|
|
136
|
-
handler: ((input, ctx) => invokeChannelMembers("reject-request", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
name: "mIRC_kick",
|
|
140
|
-
requires: "network",
|
|
141
|
-
description:
|
|
142
|
-
"Remove a member from the channel. They can rejoin if not banned. Requires moderator+. `reason` is logged to channel-event-log.",
|
|
143
|
-
inputSchema: mIRC_kick_input,
|
|
144
|
-
handler: ((input, ctx) => invokeChannelMembers("kick", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
name: "mIRC_ban",
|
|
148
|
-
requires: "network",
|
|
149
|
-
description:
|
|
150
|
-
"Ban a member from the channel. They cannot rejoin until unbanned. `until` (ISO-8601) for time-bound ban; omit for permanent. Requires admin+.",
|
|
151
|
-
inputSchema: mIRC_ban_input,
|
|
152
|
-
handler: ((input, ctx) => invokeChannelMembers("ban", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
name: "mIRC_unban",
|
|
156
|
-
requires: "network",
|
|
157
|
-
description: "Lift a ban. Requires admin+.",
|
|
158
|
-
inputSchema: mIRC_unban_input,
|
|
159
|
-
handler: ((input, ctx) => invokeChannelMembers("unban", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
160
|
-
},
|
|
161
|
-
{
|
|
162
|
-
name: "mIRC_set_role",
|
|
163
|
-
requires: "network",
|
|
164
|
-
description:
|
|
165
|
-
"Promote / demote a member. Roles: owner > admin > moderator > member > guest. Only owner can promote to admin; only admin+ can promote to moderator.",
|
|
166
|
-
inputSchema: mIRC_set_role_input,
|
|
167
|
-
handler: ((input, ctx) => invokeChannelMembers("set-role", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
168
|
-
},
|
|
169
|
-
{
|
|
170
|
-
name: "mIRC_set_ptt",
|
|
171
|
-
requires: "network",
|
|
172
|
-
description:
|
|
173
|
-
"Grant or revoke PTT floor permission for a member in voice-capable channels (capabilities.ptt=true). Requires moderator+.",
|
|
174
|
-
inputSchema: mIRC_set_ptt_input,
|
|
175
|
-
handler: ((input, ctx) => invokeChannelMembers("set-ptt", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
176
|
-
},
|
|
177
|
-
];
|
|
1
|
+
/**
|
|
2
|
+
* mIRC channel members — join, leave, kick, ban, set-role, set-ptt.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the m0ssad-3 `channel-members-manage` Edge Function (audit ab6c885f),
|
|
5
|
+
* which dispatches on the `action` body field.
|
|
6
|
+
*
|
|
7
|
+
* Split from packages/mcp/src/tools/mirc.ts because the channel-management
|
|
8
|
+
* surface and the member-management surface have different ergonomics — a
|
|
9
|
+
* model that handles roster ops shouldn't have to read past 5 'channel CRUD'
|
|
10
|
+
* tools to find them.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
15
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
16
|
+
|
|
17
|
+
// ---- Schemas ----
|
|
18
|
+
|
|
19
|
+
const ChannelId = z
|
|
20
|
+
.string()
|
|
21
|
+
.min(1)
|
|
22
|
+
.max(120)
|
|
23
|
+
.describe("Channel id (UUID). From mIRC_list / mIRC_create.");
|
|
24
|
+
|
|
25
|
+
const MemberIdentity = z
|
|
26
|
+
.string()
|
|
27
|
+
.min(1)
|
|
28
|
+
.describe("Target user's identity id (UUID). From mDM_list_contacts or radar logs.");
|
|
29
|
+
|
|
30
|
+
const Role = z
|
|
31
|
+
.enum(["owner", "admin", "moderator", "member", "guest"])
|
|
32
|
+
.describe("Member role within the channel.");
|
|
33
|
+
|
|
34
|
+
const mIRC_join_input = z.object({
|
|
35
|
+
channel_id: ChannelId,
|
|
36
|
+
password: z.string().min(1).optional().describe("Required if the channel is access_mode=private."),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const mIRC_request_access_input = z.object({
|
|
40
|
+
channel_id: ChannelId,
|
|
41
|
+
message: z.string().max(500).optional().describe("Optional note for the channel owner explaining why."),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const mIRC_leave_input = z.object({
|
|
45
|
+
channel_id: ChannelId,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const mIRC_approve_request_input = z.object({
|
|
49
|
+
channel_id: ChannelId,
|
|
50
|
+
request_id: z.string().min(1).describe("Pending request id."),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const mIRC_reject_request_input = mIRC_approve_request_input;
|
|
54
|
+
|
|
55
|
+
const mIRC_kick_input = z.object({
|
|
56
|
+
channel_id: ChannelId,
|
|
57
|
+
identity_id: MemberIdentity,
|
|
58
|
+
reason: z.string().max(500).optional(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const mIRC_ban_input = z.object({
|
|
62
|
+
channel_id: ChannelId,
|
|
63
|
+
identity_id: MemberIdentity,
|
|
64
|
+
reason: z.string().max(500).optional(),
|
|
65
|
+
/** Optional unban deadline (ISO-8601). Omit for permanent ban. */
|
|
66
|
+
until: z.string().optional(),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const mIRC_unban_input = z.object({
|
|
70
|
+
channel_id: ChannelId,
|
|
71
|
+
identity_id: MemberIdentity,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const mIRC_set_role_input = z.object({
|
|
75
|
+
channel_id: ChannelId,
|
|
76
|
+
identity_id: MemberIdentity,
|
|
77
|
+
role: Role,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const mIRC_set_ptt_input = z.object({
|
|
81
|
+
channel_id: ChannelId,
|
|
82
|
+
identity_id: MemberIdentity,
|
|
83
|
+
ptt_enabled: z.boolean().describe("Allow this member to hold the PTT floor in voice mode."),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ---- Handlers ----
|
|
87
|
+
|
|
88
|
+
async function invokeChannelMembers(
|
|
89
|
+
action: string,
|
|
90
|
+
body: Record<string, unknown>,
|
|
91
|
+
ctx: MosaddToolContext,
|
|
92
|
+
): Promise<unknown> {
|
|
93
|
+
readSupabaseEnv();
|
|
94
|
+
ctx.log("debug", `mIRC.${action}`, body);
|
|
95
|
+
return await invokeFunction("channel-members-manage", { action, ...body });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ---- Registration ----
|
|
99
|
+
|
|
100
|
+
export const mircMembersTools: MosaddTool[] = [
|
|
101
|
+
{
|
|
102
|
+
name: "mIRC_join",
|
|
103
|
+
requires: "network",
|
|
104
|
+
description:
|
|
105
|
+
"Join a channel as the current user. For private channels pass `password`. Returns a membership row or 409 if already a member.",
|
|
106
|
+
inputSchema: mIRC_join_input,
|
|
107
|
+
handler: ((input, ctx) => invokeChannelMembers("join", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "mIRC_request_access",
|
|
111
|
+
requires: "network",
|
|
112
|
+
description:
|
|
113
|
+
"Request access to an invite_only channel. Owner / admins get a notification with the optional message.",
|
|
114
|
+
inputSchema: mIRC_request_access_input,
|
|
115
|
+
handler: ((input, ctx) => invokeChannelMembers("request-access", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "mIRC_leave",
|
|
119
|
+
requires: "network",
|
|
120
|
+
description: "Leave a channel as the current user. Owner cannot leave without transferring ownership first.",
|
|
121
|
+
inputSchema: mIRC_leave_input,
|
|
122
|
+
handler: ((input, ctx) => invokeChannelMembers("leave", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "mIRC_approve_request",
|
|
126
|
+
requires: "network",
|
|
127
|
+
description: "Approve a pending access request. Admins / moderators / owner only.",
|
|
128
|
+
inputSchema: mIRC_approve_request_input,
|
|
129
|
+
handler: ((input, ctx) => invokeChannelMembers("approve-request", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: "mIRC_reject_request",
|
|
133
|
+
requires: "network",
|
|
134
|
+
description: "Reject a pending access request. Admins / moderators / owner only.",
|
|
135
|
+
inputSchema: mIRC_reject_request_input,
|
|
136
|
+
handler: ((input, ctx) => invokeChannelMembers("reject-request", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: "mIRC_kick",
|
|
140
|
+
requires: "network",
|
|
141
|
+
description:
|
|
142
|
+
"Remove a member from the channel. They can rejoin if not banned. Requires moderator+. `reason` is logged to channel-event-log.",
|
|
143
|
+
inputSchema: mIRC_kick_input,
|
|
144
|
+
handler: ((input, ctx) => invokeChannelMembers("kick", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
name: "mIRC_ban",
|
|
148
|
+
requires: "network",
|
|
149
|
+
description:
|
|
150
|
+
"Ban a member from the channel. They cannot rejoin until unbanned. `until` (ISO-8601) for time-bound ban; omit for permanent. Requires admin+.",
|
|
151
|
+
inputSchema: mIRC_ban_input,
|
|
152
|
+
handler: ((input, ctx) => invokeChannelMembers("ban", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "mIRC_unban",
|
|
156
|
+
requires: "network",
|
|
157
|
+
description: "Lift a ban. Requires admin+.",
|
|
158
|
+
inputSchema: mIRC_unban_input,
|
|
159
|
+
handler: ((input, ctx) => invokeChannelMembers("unban", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: "mIRC_set_role",
|
|
163
|
+
requires: "network",
|
|
164
|
+
description:
|
|
165
|
+
"Promote / demote a member. Roles: owner > admin > moderator > member > guest. Only owner can promote to admin; only admin+ can promote to moderator.",
|
|
166
|
+
inputSchema: mIRC_set_role_input,
|
|
167
|
+
handler: ((input, ctx) => invokeChannelMembers("set-role", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: "mIRC_set_ptt",
|
|
171
|
+
requires: "network",
|
|
172
|
+
description:
|
|
173
|
+
"Grant or revoke PTT floor permission for a member in voice-capable channels (capabilities.ptt=true). Requires moderator+.",
|
|
174
|
+
inputSchema: mIRC_set_ptt_input,
|
|
175
|
+
handler: ((input, ctx) => invokeChannelMembers("set-ptt", input as Record<string, unknown>, ctx)) as MosaddTool["handler"],
|
|
176
|
+
},
|
|
177
|
+
];
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mIRC text mode — post + list messages inside a persistent channel.
|
|
3
|
+
*
|
|
4
|
+
* Channel messages route through the same m0ssad-3 `message-send` / `message-list`
|
|
5
|
+
* Edge Functions as DMs and rooms, but with `thread_id: 'chat:<channelId>'` so the
|
|
6
|
+
* RLS / access checks apply channel-membership rules. Each channel is linked to a
|
|
7
|
+
* backing space (`metadata.linked_space_id`); message-send needs that space id, so
|
|
8
|
+
* these tools resolve it from the channel automatically (or accept an override).
|
|
9
|
+
*
|
|
10
|
+
* Until now mIRC exposed only channel *management* (create/join/moderate) — there
|
|
11
|
+
* was no way to actually post or read a channel's messages from MCP. These two
|
|
12
|
+
* tools close that gap (the mROOM channels had it; mIRC did not).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import type { MosaddTool, MosaddToolContext } from "../types.js";
|
|
17
|
+
import { invokeFunction, readSupabaseEnv } from "../providers/supabase.js";
|
|
18
|
+
|
|
19
|
+
const PROTOCOL_VERSION = "mosadd.chat.v1";
|
|
20
|
+
|
|
21
|
+
const ChannelId = z.string().min(1).max(120).describe("Channel id (UUID). From mIRC_create / mIRC_list.");
|
|
22
|
+
|
|
23
|
+
const mIRC_post_message_input = z.object({
|
|
24
|
+
channel_id: ChannelId,
|
|
25
|
+
text: z.string().min(1).max(64_000).describe("Message body (UTF-8). Max 64 KB."),
|
|
26
|
+
reply_to_id: z.string().optional().describe("Optional id of the message being replied to."),
|
|
27
|
+
space_id: z
|
|
28
|
+
.string()
|
|
29
|
+
.optional()
|
|
30
|
+
.describe("Optional backing space id. Resolved automatically from the channel if omitted."),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const mIRC_list_messages_input = z.object({
|
|
34
|
+
channel_id: ChannelId,
|
|
35
|
+
limit: z.number().int().min(1).max(200).default(50).optional(),
|
|
36
|
+
cursor: z.string().optional().describe("Opaque pagination cursor from a prior list response."),
|
|
37
|
+
space_id: z.string().optional().describe("Optional backing space id. Resolved automatically if omitted."),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
function packPlaintextPayload(text: string, replyToId?: string): string {
|
|
41
|
+
const envelope = {
|
|
42
|
+
v: PROTOCOL_VERSION,
|
|
43
|
+
type: "text",
|
|
44
|
+
text,
|
|
45
|
+
reply_to: replyToId ?? null,
|
|
46
|
+
sent_at: new Date().toISOString(),
|
|
47
|
+
};
|
|
48
|
+
return Buffer.from(JSON.stringify(envelope), "utf8").toString("base64");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function unpackPayload(payload: string): { text: string } {
|
|
52
|
+
try {
|
|
53
|
+
const obj = JSON.parse(Buffer.from(payload, "base64").toString("utf8"));
|
|
54
|
+
if (typeof obj?.text === "string") return obj;
|
|
55
|
+
} catch {
|
|
56
|
+
/* fall through */
|
|
57
|
+
}
|
|
58
|
+
return { text: "<ciphertext>" };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Resolve a channel's backing space id (metadata.linked_space_id) via channel-manage. */
|
|
62
|
+
async function resolveLinkedSpaceId(channelId: string): Promise<string> {
|
|
63
|
+
type GetResponse = {
|
|
64
|
+
channel?: { metadata?: { linked_space_id?: string } | null };
|
|
65
|
+
linked_space_id?: string;
|
|
66
|
+
};
|
|
67
|
+
const data = await invokeFunction<GetResponse>("channel-manage", { action: "get", channel_id: channelId });
|
|
68
|
+
const spaceId = data?.linked_space_id ?? data?.channel?.metadata?.linked_space_id;
|
|
69
|
+
if (!spaceId) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Channel ${channelId} has no linked space yet. Run mIRC_list (which repairs linked spaces) or mIRC_join first, ` +
|
|
72
|
+
`or pass space_id explicitly.`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return spaceId;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function mIRC_post_message(
|
|
79
|
+
input: z.infer<typeof mIRC_post_message_input>,
|
|
80
|
+
ctx: MosaddToolContext,
|
|
81
|
+
): Promise<{ message_id: string; delivered_at: string; thread_id: string }> {
|
|
82
|
+
readSupabaseEnv();
|
|
83
|
+
const space_id = input.space_id ?? (await resolveLinkedSpaceId(input.channel_id));
|
|
84
|
+
const thread_id = `chat:${input.channel_id}`;
|
|
85
|
+
ctx.log("debug", "mIRC_post_message", { thread_id, space_id });
|
|
86
|
+
|
|
87
|
+
const data = await invokeFunction<{ message?: { id: string; created_at: string } }>("message-send", {
|
|
88
|
+
space_id,
|
|
89
|
+
thread_id,
|
|
90
|
+
encrypted_payload: packPlaintextPayload(input.text, input.reply_to_id),
|
|
91
|
+
message_type: "txt",
|
|
92
|
+
protocol_version: PROTOCOL_VERSION,
|
|
93
|
+
reply_to_id: input.reply_to_id ?? null,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (!data?.message?.id) throw new Error("message-send returned no message id");
|
|
97
|
+
return { message_id: data.message.id, delivered_at: data.message.created_at ?? new Date().toISOString(), thread_id };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function mIRC_list_messages(
|
|
101
|
+
input: z.infer<typeof mIRC_list_messages_input>,
|
|
102
|
+
ctx: MosaddToolContext,
|
|
103
|
+
): Promise<{
|
|
104
|
+
messages: Array<{ id: string; sender_identity_id: string; text: string; timestamp: string }>;
|
|
105
|
+
next_cursor: string | null;
|
|
106
|
+
}> {
|
|
107
|
+
readSupabaseEnv();
|
|
108
|
+
const space_id = input.space_id ?? (await resolveLinkedSpaceId(input.channel_id));
|
|
109
|
+
const thread_id = `chat:${input.channel_id}`;
|
|
110
|
+
ctx.log("debug", "mIRC_list_messages", { thread_id, space_id });
|
|
111
|
+
|
|
112
|
+
type MessageListResponse = {
|
|
113
|
+
messages?: Array<{ id: string; sender_identity_id: string; encrypted_payload: string; created_at: string }>;
|
|
114
|
+
next_cursor?: string | null;
|
|
115
|
+
};
|
|
116
|
+
const data = await invokeFunction<MessageListResponse>("message-list", {
|
|
117
|
+
space_id,
|
|
118
|
+
thread_id,
|
|
119
|
+
limit: input.limit ?? 50,
|
|
120
|
+
cursor: input.cursor,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
messages: (data?.messages ?? []).map((m) => ({
|
|
125
|
+
id: m.id,
|
|
126
|
+
sender_identity_id: m.sender_identity_id,
|
|
127
|
+
text: unpackPayload(m.encrypted_payload).text,
|
|
128
|
+
timestamp: m.created_at,
|
|
129
|
+
})),
|
|
130
|
+
next_cursor: data?.next_cursor ?? null,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export const mircMessagesTools: MosaddTool[] = [
|
|
135
|
+
{
|
|
136
|
+
name: "mIRC_post_message",
|
|
137
|
+
requires: "network",
|
|
138
|
+
description:
|
|
139
|
+
"Post a text message into a persistent channel (mIRC). Pass channel_id from mIRC_create / mIRC_list; the backing space is resolved automatically. You must be a member of private channels (use mIRC_join first). This is how an agent actually talks in a channel — the other mIRC_* tools only manage it.",
|
|
140
|
+
inputSchema: mIRC_post_message_input,
|
|
141
|
+
handler: mIRC_post_message as MosaddTool["handler"],
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
name: "mIRC_list_messages",
|
|
145
|
+
requires: "network",
|
|
146
|
+
description:
|
|
147
|
+
"List recent text messages in a persistent channel (mIRC), newest first, cursor-paginated. Pass channel_id; the backing space is resolved automatically.",
|
|
148
|
+
inputSchema: mIRC_list_messages_input,
|
|
149
|
+
handler: mIRC_list_messages as MosaddTool["handler"],
|
|
150
|
+
},
|
|
151
|
+
];
|