ocean-crew 0.0.0-stage → 0.4.0
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/LICENSE +21 -0
- package/README.md +156 -2
- package/THIRD_PARTY_NOTICES.md +159 -0
- package/client.js +31389 -0
- package/cordis.patch.yml +6 -0
- package/dist/host/chat/btw.js +436 -0
- package/dist/host/chat/channel.js +1241 -0
- package/dist/host/chat/chat-index.js +344 -0
- package/dist/host/chat/delivery.js +231 -0
- package/dist/host/chat/feed.js +165 -0
- package/dist/host/chat/handoffs.js +625 -0
- package/dist/host/chat/parts.js +2033 -0
- package/dist/host/chat/requests.js +155 -0
- package/dist/host/chat/resume.js +364 -0
- package/dist/host/chat/side.js +457 -0
- package/dist/host/chat/steer.js +346 -0
- package/dist/host/chat/turns.js +1457 -0
- package/dist/host/connectors/catalog.js +331 -0
- package/dist/host/connectors/gateway.js +339 -0
- package/dist/host/connectors/index.js +1235 -0
- package/dist/host/connectors/mail/index.js +912 -0
- package/dist/host/connectors/mail/presets.js +53 -0
- package/dist/host/connectors/mail/transport.js +519 -0
- package/dist/host/connectors/oauth.js +337 -0
- package/dist/host/core/constants.js +123 -0
- package/dist/host/core/diagnostics.js +310 -0
- package/dist/host/core/dsh-update.js +212 -0
- package/dist/host/core/events.js +16 -0
- package/dist/host/core/files.js +37 -0
- package/dist/host/core/folders.js +283 -0
- package/dist/host/core/home-lock.js +137 -0
- package/dist/host/core/links.js +52 -0
- package/dist/host/core/runtime.js +5 -0
- package/dist/host/core/store.js +662 -0
- package/dist/host/core/text.js +59 -0
- package/dist/host/core/thinking.js +60 -0
- package/dist/host/core/time-context.js +69 -0
- package/dist/host/core/update.js +466 -0
- package/dist/host/groups/calls.js +684 -0
- package/dist/host/groups/index.js +79 -0
- package/dist/host/groups/log.js +334 -0
- package/dist/host/groups/prompt.js +124 -0
- package/dist/host/groups/relay.js +489 -0
- package/dist/host/groups/round.js +992 -0
- package/dist/host/groups/tasks.js +217 -0
- package/dist/host/groups/tools.js +478 -0
- package/dist/host/groups/types.js +8 -0
- package/dist/host/im/format.js +228 -0
- package/dist/host/im/index.js +2461 -0
- package/dist/host/im/platforms/feishu.js +1134 -0
- package/dist/host/im/platforms/index.js +6 -0
- package/dist/host/im/platforms/media-type.js +70 -0
- package/dist/host/im/platforms/weixin.js +1117 -0
- package/dist/host/im/reaction.js +51 -0
- package/dist/host/im/supervisor.js +56 -0
- package/dist/host/team/agents.js +161 -0
- package/dist/host/team/models.js +427 -0
- package/dist/host/team/permissions.js +263 -0
- package/dist/host/team/privacy.js +108 -0
- package/dist/host/team/prompt.js +86 -0
- package/dist/host/team/providers.js +461 -0
- package/dist/host/team/roster.js +1011 -0
- package/dist/host/team/tools.js +1638 -0
- package/dist/host/work/approvals.js +290 -0
- package/dist/host/work/avatar-library.js +155 -0
- package/dist/host/work/avatar.js +132 -0
- package/dist/host/work/capabilities.js +862 -0
- package/dist/host/work/companion.js +306 -0
- package/dist/host/work/goal-skills.js +91 -0
- package/dist/host/work/goals.js +1337 -0
- package/dist/host/work/images.js +283 -0
- package/dist/host/work/learning.js +1021 -0
- package/dist/host/work/memory-pending.js +108 -0
- package/dist/host/work/memory.js +3237 -0
- package/dist/host/work/notify.js +146 -0
- package/dist/host/work/reset.js +43 -0
- package/dist/host/work/schedules.js +309 -0
- package/dist/host/work/secrets.js +702 -0
- package/dist/host/work/steward.js +166 -0
- package/dist/host/work/tts.js +192 -0
- package/dist/host/work/usage.js +84 -0
- package/dist/index.js +137 -0
- package/dist/shared/api.js +32 -0
- package/dist/shared/companion.js +110 -0
- package/dist/shared/config.js +3 -0
- package/dist/shared/coordination.js +6 -0
- package/dist/shared/group-replies.js +95 -0
- package/dist/shared/im-platforms.js +35 -0
- package/dist/shared/im.js +3 -0
- package/dist/shared/mentions.js +85 -0
- package/dist/shared/state.js +25 -0
- package/package.json +81 -5
|
@@ -0,0 +1,862 @@
|
|
|
1
|
+
// Capabilities: the shared MCP servers, skills and packs in the team library, their
|
|
2
|
+
// per-Session health, and the mounts that give each Bot Session its tools and skills.
|
|
3
|
+
// The MCP client is loaded through DSH's own loader; a mounted fiber lives in the
|
|
4
|
+
// Agent's own context, so this module keeps it reachable and disposes it on unload.
|
|
5
|
+
//
|
|
6
|
+
// The capability tools (list/save/delete/attach/detach) are registered here — the
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
9
|
+
import { own } from "../core/store.js";
|
|
10
|
+
import { encodeUrlComponent } from "./secrets.js";
|
|
11
|
+
const MCP_CLIENT = "@deepseek-ai/dsh-mcp-client";
|
|
12
|
+
const NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
13
|
+
const SECRET_REFERENCE = /\$DSH_SECRET_([A-Z][A-Z0-9_]*)/g;
|
|
14
|
+
const ENSURE_TIMEOUT_MS = 15_000;
|
|
15
|
+
// Connector ids are reserved: a capability named like one would collide with its
|
|
16
|
+
// mcp__<id>__* tool prefix. The connectors module registers its ids here.
|
|
17
|
+
export const CONNECTOR_IDS = new Set(["github", "mail"]);
|
|
18
|
+
export function validateCapabilityName(value, reserved = CONNECTOR_IDS) {
|
|
19
|
+
const name = String(value ?? "").trim();
|
|
20
|
+
if (name.length > 32 || !NAME.test(name) || name === "github" || reserved.has(name)) {
|
|
21
|
+
throw new Error("Use a capability name with lowercase letters, digits, and single hyphens; connector names are reserved.");
|
|
22
|
+
}
|
|
23
|
+
return name;
|
|
24
|
+
}
|
|
25
|
+
export function expandCapabilityNames(capabilities, library) {
|
|
26
|
+
const expanded = new Set();
|
|
27
|
+
for (const name of capabilities ?? []) {
|
|
28
|
+
const item = own(library, name);
|
|
29
|
+
if (!item)
|
|
30
|
+
throw new Error(`No capability named ${name}.`);
|
|
31
|
+
if (item.kind === "pack") {
|
|
32
|
+
for (const childName of item.items ?? []) {
|
|
33
|
+
const child = own(library, childName);
|
|
34
|
+
if (!child || !["mcp", "skill"].includes(child.kind))
|
|
35
|
+
throw new Error(`Pack ${name} refers to an unavailable capability ${childName}.`);
|
|
36
|
+
expanded.add(childName);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else if (item.kind === "mcp" || item.kind === "skill") {
|
|
40
|
+
expanded.add(name);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
throw new Error(`Capability ${name} has an unknown kind.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return expanded;
|
|
47
|
+
}
|
|
48
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
49
|
+
const strings = (value) => {
|
|
50
|
+
if (value === undefined)
|
|
51
|
+
return [];
|
|
52
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
|
|
53
|
+
throw new Error("Arguments must be a list of text values.");
|
|
54
|
+
return [...value];
|
|
55
|
+
};
|
|
56
|
+
const stringMap = (value, label) => {
|
|
57
|
+
if (value === undefined)
|
|
58
|
+
return {};
|
|
59
|
+
if (value === null ||
|
|
60
|
+
typeof value !== "object" ||
|
|
61
|
+
Array.isArray(value) ||
|
|
62
|
+
Object.values(value).some((item) => typeof item !== "string"))
|
|
63
|
+
throw new Error(`${label} must contain text values.`);
|
|
64
|
+
return Object.fromEntries(Object.entries(value));
|
|
65
|
+
};
|
|
66
|
+
const output = {
|
|
67
|
+
schema: { type: "string" },
|
|
68
|
+
render: (_args, value) => [
|
|
69
|
+
{ type: "text", text: String(value) },
|
|
70
|
+
],
|
|
71
|
+
};
|
|
72
|
+
const tool = (options) => defineTool({ ...options, output });
|
|
73
|
+
export function install(rt) {
|
|
74
|
+
const { ctx, state } = rt;
|
|
75
|
+
const agents = new Map();
|
|
76
|
+
// Mounted fibers live in each Agent's own context, so they outlive this plugin unless
|
|
77
|
+
// its cleanup disposes them; a strong map keeps them reachable for that.
|
|
78
|
+
const mounts = new Map();
|
|
79
|
+
// One reconcile chain per Agent: a slow MCP server holds up only its own Agent.
|
|
80
|
+
const chains = new WeakMap();
|
|
81
|
+
const queued = new WeakSet();
|
|
82
|
+
const sessionStatuses = new Map();
|
|
83
|
+
let pluginPromise;
|
|
84
|
+
let scheduled = null;
|
|
85
|
+
let loadingSecrets = 0;
|
|
86
|
+
let stopped = false;
|
|
87
|
+
const library = () => state.capabilities.items;
|
|
88
|
+
const ensureLibrary = () => {
|
|
89
|
+
const team = state;
|
|
90
|
+
team.capabilities ??= { items: {} };
|
|
91
|
+
const items = team.capabilities.items;
|
|
92
|
+
return items;
|
|
93
|
+
};
|
|
94
|
+
const writeCheck = () => {
|
|
95
|
+
const readOnly = rt.readOnly();
|
|
96
|
+
if (readOnly !== undefined)
|
|
97
|
+
throw new Error(readOnly);
|
|
98
|
+
};
|
|
99
|
+
const allowedToEdit = (item, botId) => rt.isMain(botId) || item.createdBy === botId;
|
|
100
|
+
const usedByOthers = (name, botId) => Object.values(state.bots).some((bot) => bot.id !== botId &&
|
|
101
|
+
(bot.capabilities ?? []).some((attached) => {
|
|
102
|
+
const item = own(library(), attached);
|
|
103
|
+
return attached === name || (item?.kind === "pack" && (item.items ?? []).includes(name));
|
|
104
|
+
}));
|
|
105
|
+
const editCheck = (item, botId, verb) => {
|
|
106
|
+
if (!allowedToEdit(item, botId))
|
|
107
|
+
throw new Error(`Only ${item.createdBy ? "its creator or a Main Bot" : "a Main Bot"} can ${verb} ${item.name}.`);
|
|
108
|
+
if (!rt.isMain(botId) && usedByOthers(item.name, botId))
|
|
109
|
+
throw new Error(`Another Bot uses ${item.name}, so only a Main Bot can ${verb} it.`);
|
|
110
|
+
};
|
|
111
|
+
const itemInput = (body, { botId, ui }) => {
|
|
112
|
+
const name = validateCapabilityName(body.name);
|
|
113
|
+
const kind = body.kind;
|
|
114
|
+
if (!["mcp", "skill", "pack"].includes(kind))
|
|
115
|
+
throw new Error("Choose an MCP server, skill, or pack.");
|
|
116
|
+
const items = ensureLibrary();
|
|
117
|
+
const previous = own(items, name);
|
|
118
|
+
if (previous && !ui)
|
|
119
|
+
editCheck(previous, botId, "change");
|
|
120
|
+
if (previous && previous.kind !== kind)
|
|
121
|
+
throw new Error("A capability cannot change its kind. Delete it and save a new one.");
|
|
122
|
+
const now = Date.now();
|
|
123
|
+
const item = {
|
|
124
|
+
name,
|
|
125
|
+
kind,
|
|
126
|
+
description: String(body.description ?? "")
|
|
127
|
+
.trim()
|
|
128
|
+
.slice(0, 2000),
|
|
129
|
+
createdBy: previous?.createdBy ?? (ui ? null : (botId ?? null)),
|
|
130
|
+
createdAt: previous?.createdAt ?? now,
|
|
131
|
+
updatedAt: now,
|
|
132
|
+
};
|
|
133
|
+
if (kind === "mcp") {
|
|
134
|
+
const transport = body.transport === "streamable-http" ? "streamable-http" : "stdio";
|
|
135
|
+
if (transport === "stdio") {
|
|
136
|
+
const command = String(body.command ?? "").trim();
|
|
137
|
+
if (command === "")
|
|
138
|
+
throw new Error("Enter a command for the MCP server.");
|
|
139
|
+
const args = strings(body.args);
|
|
140
|
+
const env = stringMap(body.env, "Environment variables");
|
|
141
|
+
const changed = !previous ||
|
|
142
|
+
previous.kind !== "mcp" ||
|
|
143
|
+
previous.transport !== "stdio" ||
|
|
144
|
+
previous.command !== command ||
|
|
145
|
+
!same(previous.args ?? [], args) ||
|
|
146
|
+
!same(previous.env ?? {}, env);
|
|
147
|
+
Object.assign(item, {
|
|
148
|
+
transport,
|
|
149
|
+
command,
|
|
150
|
+
args,
|
|
151
|
+
env,
|
|
152
|
+
approved: ui ? true : changed ? false : previous.approved === true,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const url = String(body.url ?? "").trim();
|
|
157
|
+
try {
|
|
158
|
+
const parsed = new URL(url);
|
|
159
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:")
|
|
160
|
+
throw new Error();
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
throw new Error("Enter a valid HTTP or HTTPS URL.");
|
|
164
|
+
}
|
|
165
|
+
Object.assign(item, {
|
|
166
|
+
transport,
|
|
167
|
+
url,
|
|
168
|
+
headers: stringMap(body.headers, "Headers"),
|
|
169
|
+
approved: true,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else if (kind === "skill") {
|
|
174
|
+
item.content = String(body.content ?? "").trim();
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const names = [...new Set(strings(body.items))];
|
|
178
|
+
for (const childName of names) {
|
|
179
|
+
const child = own(items, childName);
|
|
180
|
+
if (!child || !["mcp", "skill"].includes(child.kind))
|
|
181
|
+
throw new Error(`Packs can include only saved MCP servers and skills. ${childName} is not one.`);
|
|
182
|
+
}
|
|
183
|
+
item.items = names;
|
|
184
|
+
}
|
|
185
|
+
items[name] = item;
|
|
186
|
+
return item;
|
|
187
|
+
};
|
|
188
|
+
const capabilitySave = async (body, options = {}) => {
|
|
189
|
+
await rt.load();
|
|
190
|
+
writeCheck();
|
|
191
|
+
const item = itemInput(body ?? {}, options);
|
|
192
|
+
await rt.save();
|
|
193
|
+
return item;
|
|
194
|
+
};
|
|
195
|
+
const capabilityDelete = async (name, { botId, ui } = {}) => {
|
|
196
|
+
await rt.load();
|
|
197
|
+
writeCheck();
|
|
198
|
+
const items = ensureLibrary();
|
|
199
|
+
const item = own(items, String(name ?? ""));
|
|
200
|
+
if (!item)
|
|
201
|
+
throw new Error(`No capability named ${name}.`);
|
|
202
|
+
if (!ui)
|
|
203
|
+
editCheck(item, botId, "delete");
|
|
204
|
+
delete items[item.name];
|
|
205
|
+
for (const pack of Object.values(items))
|
|
206
|
+
if (pack.kind === "pack")
|
|
207
|
+
pack.items = (pack.items ?? []).filter((child) => child !== item.name);
|
|
208
|
+
for (const bot of Object.values(state.bots))
|
|
209
|
+
bot.capabilities = (bot.capabilities ?? []).filter((attached) => attached !== item.name);
|
|
210
|
+
await rt.save();
|
|
211
|
+
return true;
|
|
212
|
+
};
|
|
213
|
+
// Called with the Bot being deleted, after it left the team. The shared list belongs
|
|
214
|
+
// to the user, so only a local MCP this Bot added, nobody approved and no other Bot or
|
|
215
|
+
// pack uses goes: no one is left to ask for it, and approving it would start a server
|
|
216
|
+
// for no one. Anything a pack lists stays.
|
|
217
|
+
const dropOrphanCapabilities = (bot) => {
|
|
218
|
+
const items = library();
|
|
219
|
+
const packed = (name) => Object.values(items).some((item) => item.kind === "pack" && (item.items ?? []).includes(name));
|
|
220
|
+
for (const name of bot.capabilities ?? []) {
|
|
221
|
+
const item = own(items, name);
|
|
222
|
+
if (item === undefined || item.kind !== "mcp" || item.transport !== "stdio" || item.approved === true)
|
|
223
|
+
continue;
|
|
224
|
+
if (item.createdBy !== bot.id || usedByOthers(name, bot.id) || packed(name))
|
|
225
|
+
continue;
|
|
226
|
+
delete items[item.name];
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
const changeAttachment = async (endpoint, body, callerId) => {
|
|
230
|
+
await rt.load();
|
|
231
|
+
writeCheck();
|
|
232
|
+
const caller = rt.botOf(callerId);
|
|
233
|
+
if (!caller)
|
|
234
|
+
throw new Error("Only Bots on the team can change capabilities.");
|
|
235
|
+
const target = body.bot ? rt.findBot(body.bot) : caller;
|
|
236
|
+
if (!target)
|
|
237
|
+
throw new Error(`No Bot named ${body.bot}.`);
|
|
238
|
+
if (target.id !== caller.id && !rt.isMain(callerId))
|
|
239
|
+
throw new Error("Only a Main Bot can change another Bot’s capabilities.");
|
|
240
|
+
const item = own(ensureLibrary(), String(body.name ?? ""));
|
|
241
|
+
if (!item)
|
|
242
|
+
throw new Error(`No capability named ${body.name}.`);
|
|
243
|
+
await setAttached(target, item.name, endpoint === "attach");
|
|
244
|
+
return { bot: target.name, attached: endpoint === "attach" };
|
|
245
|
+
};
|
|
246
|
+
// Changes one name against the saved list, so two quick changes both land.
|
|
247
|
+
const setAttached = async (bot, name, attached) => {
|
|
248
|
+
bot.capabilities ??= [];
|
|
249
|
+
const wasEmpty = bot.capabilities.length === 0;
|
|
250
|
+
if (attached && !bot.capabilities.includes(name))
|
|
251
|
+
bot.capabilities.push(name);
|
|
252
|
+
if (!attached)
|
|
253
|
+
bot.capabilities = bot.capabilities.filter((entry) => entry !== name);
|
|
254
|
+
await rt.save();
|
|
255
|
+
if (wasEmpty && bot.capabilities.length > 0) {
|
|
256
|
+
await trackBotAgents(bot);
|
|
257
|
+
void scheduleReconcile();
|
|
258
|
+
}
|
|
259
|
+
return bot.capabilities;
|
|
260
|
+
};
|
|
261
|
+
const statusSet = (botId, itemName, sessionId, status) => {
|
|
262
|
+
let byItem = sessionStatuses.get(botId);
|
|
263
|
+
if (!byItem) {
|
|
264
|
+
byItem = new Map();
|
|
265
|
+
sessionStatuses.set(botId, byItem);
|
|
266
|
+
}
|
|
267
|
+
let bySession = byItem.get(itemName);
|
|
268
|
+
if (!bySession) {
|
|
269
|
+
bySession = new Map();
|
|
270
|
+
byItem.set(itemName, bySession);
|
|
271
|
+
}
|
|
272
|
+
bySession.set(sessionId, status);
|
|
273
|
+
};
|
|
274
|
+
const clearStatus = (botId, itemName, sessionId) => {
|
|
275
|
+
const byItem = sessionStatuses.get(botId);
|
|
276
|
+
const bySession = byItem?.get(itemName);
|
|
277
|
+
bySession?.delete(sessionId);
|
|
278
|
+
if (bySession?.size === 0)
|
|
279
|
+
byItem?.delete(itemName);
|
|
280
|
+
if (byItem?.size === 0)
|
|
281
|
+
sessionStatuses.delete(botId);
|
|
282
|
+
};
|
|
283
|
+
const clearSession = (sessionId) => {
|
|
284
|
+
for (const [botId, byItem] of sessionStatuses) {
|
|
285
|
+
for (const [name, bySession] of byItem) {
|
|
286
|
+
bySession.delete(sessionId);
|
|
287
|
+
if (bySession.size === 0)
|
|
288
|
+
byItem.delete(name);
|
|
289
|
+
}
|
|
290
|
+
if (byItem.size === 0)
|
|
291
|
+
sessionStatuses.delete(botId);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
const statusFor = (botId, name) => {
|
|
295
|
+
const values = [...(sessionStatuses.get(botId)?.get(name)?.values() ?? [])];
|
|
296
|
+
if (values.some((status) => status.state === "error"))
|
|
297
|
+
return values.find((status) => status.state === "error");
|
|
298
|
+
if (values.some((status) => status.state === "needs-approval"))
|
|
299
|
+
return { state: "needs-approval" };
|
|
300
|
+
if (values.some((status) => status.state === "connecting"))
|
|
301
|
+
return { state: "connecting" };
|
|
302
|
+
return values.length > 0 ? { state: "ready" } : undefined;
|
|
303
|
+
};
|
|
304
|
+
const capabilityStatusesView = () => {
|
|
305
|
+
const result = {};
|
|
306
|
+
for (const bot of Object.values(state.bots)) {
|
|
307
|
+
const statuses = {};
|
|
308
|
+
for (const name of bot.capabilities ?? []) {
|
|
309
|
+
const item = own(library(), name);
|
|
310
|
+
if (!item)
|
|
311
|
+
continue;
|
|
312
|
+
if (item.kind !== "pack") {
|
|
313
|
+
const status = statusFor(bot.id, name);
|
|
314
|
+
if (status)
|
|
315
|
+
statuses[name] = status;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const children = (item.items ?? [])
|
|
319
|
+
.map((child) => statusFor(bot.id, child))
|
|
320
|
+
.filter((status) => Boolean(status));
|
|
321
|
+
const status = children.find((entry) => entry.state === "error") ??
|
|
322
|
+
children.find((entry) => entry.state === "needs-approval") ??
|
|
323
|
+
children.find((entry) => entry.state === "connecting") ??
|
|
324
|
+
children[0];
|
|
325
|
+
if (status)
|
|
326
|
+
statuses[name] = status;
|
|
327
|
+
}
|
|
328
|
+
result[bot.id] = statuses;
|
|
329
|
+
}
|
|
330
|
+
return result;
|
|
331
|
+
};
|
|
332
|
+
const libraryView = () => Object.values(library()).map((item) => ({
|
|
333
|
+
...item,
|
|
334
|
+
...(item.env ? { env: { ...item.env } } : {}),
|
|
335
|
+
...(item.headers ? { headers: { ...item.headers } } : {}),
|
|
336
|
+
...(item.items ? { items: [...item.items] } : {}),
|
|
337
|
+
}));
|
|
338
|
+
async function loadMcpPlugin() {
|
|
339
|
+
if (!pluginPromise) {
|
|
340
|
+
pluginPromise = (async () => {
|
|
341
|
+
const loader = ctx.get("loader");
|
|
342
|
+
if (typeof loader?.import !== "function")
|
|
343
|
+
throw new Error("This DSH cannot load MCP servers.");
|
|
344
|
+
const exports = await loader.import(MCP_CLIENT);
|
|
345
|
+
return loader.unwrapExports?.(exports) ?? exports?.default ?? exports;
|
|
346
|
+
})();
|
|
347
|
+
}
|
|
348
|
+
return pluginPromise;
|
|
349
|
+
}
|
|
350
|
+
function secretFor(name, botId) {
|
|
351
|
+
const value = rt.canUseSecret(name, botId) ? rt.secretValue(name) : undefined;
|
|
352
|
+
if (value === undefined)
|
|
353
|
+
throw new Error(`Secret ${name} is missing or unavailable for this Bot.`);
|
|
354
|
+
return value;
|
|
355
|
+
}
|
|
356
|
+
function resolveValues(values, botId) {
|
|
357
|
+
const resolved = {};
|
|
358
|
+
for (const [key, raw] of Object.entries(values ?? {})) {
|
|
359
|
+
resolved[key] = String(raw).replace(SECRET_REFERENCE, (_match, name) => secretFor(name, botId));
|
|
360
|
+
}
|
|
361
|
+
return resolved;
|
|
362
|
+
}
|
|
363
|
+
const resolveUrl = (url, botId) => String(url).replace(SECRET_REFERENCE, (_match, name) => encodeUrlComponent(secretFor(name, botId)));
|
|
364
|
+
const resolveList = (list, botId) => (list ?? []).map((raw) => String(raw).replace(SECRET_REFERENCE, (_match, name) => secretFor(name, botId)));
|
|
365
|
+
const configFor = (item, botId) => {
|
|
366
|
+
if (item.kind === "skill")
|
|
367
|
+
return { name: item.name, description: item.description, content: item.content };
|
|
368
|
+
if (item.transport === "stdio")
|
|
369
|
+
return {
|
|
370
|
+
transport: "stdio",
|
|
371
|
+
serverName: item.name,
|
|
372
|
+
command: item.command,
|
|
373
|
+
args: resolveList(item.args, botId),
|
|
374
|
+
env: resolveValues(item.env, botId),
|
|
375
|
+
failOnStartupError: true,
|
|
376
|
+
};
|
|
377
|
+
return {
|
|
378
|
+
transport: "streamable-http",
|
|
379
|
+
serverName: item.name,
|
|
380
|
+
url: resolveUrl(item.url ?? "", botId),
|
|
381
|
+
headers: resolveValues(item.headers, botId),
|
|
382
|
+
failOnStartupError: true,
|
|
383
|
+
};
|
|
384
|
+
};
|
|
385
|
+
const disposeMount = async (mount) => {
|
|
386
|
+
if (mount?.fiber)
|
|
387
|
+
await Promise.resolve()
|
|
388
|
+
.then(() => mount.fiber.dispose?.())
|
|
389
|
+
.catch(() => undefined);
|
|
390
|
+
};
|
|
391
|
+
const mountOne = async (agent, bot, agentMounts, name) => {
|
|
392
|
+
const sessionId = agent.session.id;
|
|
393
|
+
const item = own(library(), name);
|
|
394
|
+
const drop = async () => {
|
|
395
|
+
await disposeMount(agentMounts.get(name));
|
|
396
|
+
agentMounts.delete(name);
|
|
397
|
+
};
|
|
398
|
+
if (item.kind === "mcp" && item.transport === "stdio" && item.approved !== true) {
|
|
399
|
+
await drop();
|
|
400
|
+
statusSet(bot.id, name, sessionId, { state: "needs-approval" });
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
let config;
|
|
404
|
+
try {
|
|
405
|
+
config = configFor(item, bot.id);
|
|
406
|
+
}
|
|
407
|
+
catch (error) {
|
|
408
|
+
await drop();
|
|
409
|
+
statusSet(bot.id, name, sessionId, {
|
|
410
|
+
state: "error",
|
|
411
|
+
error: rt.redactSecrets(error.message).slice(0, 300),
|
|
412
|
+
});
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const key = `${name}:${createHash("sha256").update(JSON.stringify(config)).digest("hex")}`;
|
|
416
|
+
if (agentMounts.get(name)?.key === key)
|
|
417
|
+
return;
|
|
418
|
+
await drop();
|
|
419
|
+
statusSet(bot.id, name, sessionId, { state: "connecting" });
|
|
420
|
+
try {
|
|
421
|
+
const plugin = item.kind === "skill"
|
|
422
|
+
? {
|
|
423
|
+
name: `ocean-crew-capability-skill-${name}`,
|
|
424
|
+
inject: ["skills"],
|
|
425
|
+
apply(c) {
|
|
426
|
+
c.effect(() => c.skills.register({
|
|
427
|
+
name: config.name,
|
|
428
|
+
description: config.description,
|
|
429
|
+
source: "runtime",
|
|
430
|
+
content: config.content,
|
|
431
|
+
}));
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
: await loadMcpPlugin();
|
|
435
|
+
if (stopped)
|
|
436
|
+
return;
|
|
437
|
+
const fiber = item.kind === "skill" ? agent.ctx.plugin(plugin) : agent.ctx.plugin(plugin, config);
|
|
438
|
+
agentMounts.set(name, { key, fiber });
|
|
439
|
+
await fiber.await?.();
|
|
440
|
+
if (stopped || agents.get(sessionId)?.deref() !== agent)
|
|
441
|
+
return;
|
|
442
|
+
statusSet(bot.id, name, sessionId, { state: "ready" });
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
await drop();
|
|
446
|
+
const cause = error.cause;
|
|
447
|
+
const message = rt
|
|
448
|
+
.redactSecrets([error, cause]
|
|
449
|
+
.filter(Boolean)
|
|
450
|
+
.map((part) => String(part?.message ?? part))
|
|
451
|
+
.join(": ") || "Connection failed")
|
|
452
|
+
.slice(0, 300);
|
|
453
|
+
statusSet(bot.id, name, sessionId, { state: "error", error: message });
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
const reconcileAgent = async (agent, bot) => {
|
|
457
|
+
const sessionId = agent.session.id;
|
|
458
|
+
const desired = bot ? expandCapabilityNames(bot.capabilities, library()) : new Set();
|
|
459
|
+
const agentMounts = mounts.get(agent) ?? new Map();
|
|
460
|
+
if (desired.size > 0)
|
|
461
|
+
mounts.set(agent, agentMounts);
|
|
462
|
+
if (bot) {
|
|
463
|
+
for (const name of sessionStatuses.get(bot.id)?.keys() ?? []) {
|
|
464
|
+
if (!desired.has(name))
|
|
465
|
+
clearStatus(bot.id, name, sessionId);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
clearSession(sessionId);
|
|
470
|
+
}
|
|
471
|
+
for (const [name, mount] of agentMounts) {
|
|
472
|
+
if (desired.has(name))
|
|
473
|
+
continue;
|
|
474
|
+
await disposeMount(mount);
|
|
475
|
+
agentMounts.delete(name);
|
|
476
|
+
}
|
|
477
|
+
await Promise.all([...desired].map((name) => mountOne(agent, bot, agentMounts, name)));
|
|
478
|
+
if (agentMounts.size === 0 && mounts.get(agent) === agentMounts)
|
|
479
|
+
mounts.delete(agent);
|
|
480
|
+
};
|
|
481
|
+
const botOfSession = (sessionId) => rt.isBotSession(sessionId) ? rt.botOf(rt.selfOf(sessionId)) : undefined;
|
|
482
|
+
// At most one run waits behind the running one; it reads the latest state when it starts.
|
|
483
|
+
const reconcileOne = (agent) => {
|
|
484
|
+
if (queued.has(agent))
|
|
485
|
+
return chains.get(agent) ?? Promise.resolve();
|
|
486
|
+
queued.add(agent);
|
|
487
|
+
const run = (chains.get(agent) ?? Promise.resolve())
|
|
488
|
+
.then(async () => {
|
|
489
|
+
queued.delete(agent);
|
|
490
|
+
const sessionId = agent.session.id;
|
|
491
|
+
if (stopped || agents.get(sessionId)?.deref() !== agent)
|
|
492
|
+
return;
|
|
493
|
+
await reconcileAgent(agent, botOfSession(sessionId));
|
|
494
|
+
})
|
|
495
|
+
.catch((error) => rt.warn("ocean-crew: capability reconcile for %s failed: %s", agent.session?.id, error));
|
|
496
|
+
chains.set(agent, run);
|
|
497
|
+
return run;
|
|
498
|
+
};
|
|
499
|
+
const loadInputs = async () => {
|
|
500
|
+
await rt.load();
|
|
501
|
+
loadingSecrets += 1;
|
|
502
|
+
try {
|
|
503
|
+
await rt.loadSecrets();
|
|
504
|
+
}
|
|
505
|
+
finally {
|
|
506
|
+
loadingSecrets -= 1;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
async function reconcile() {
|
|
510
|
+
if (stopped)
|
|
511
|
+
return;
|
|
512
|
+
await loadInputs();
|
|
513
|
+
const runs = [];
|
|
514
|
+
for (const [sessionId, reference] of agents) {
|
|
515
|
+
const agent = reference.deref();
|
|
516
|
+
if (agent) {
|
|
517
|
+
runs.push(reconcileOne(agent));
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
agents.delete(sessionId);
|
|
521
|
+
clearSession(sessionId);
|
|
522
|
+
}
|
|
523
|
+
await Promise.all(runs);
|
|
524
|
+
}
|
|
525
|
+
const scheduleReconcile = () => {
|
|
526
|
+
if (!scheduled) {
|
|
527
|
+
scheduled = new Promise((resolve) => {
|
|
528
|
+
queueMicrotask(() => {
|
|
529
|
+
scheduled = null;
|
|
530
|
+
resolve(reconcile().catch((error) => rt.warn("ocean-crew: capability reconcile failed: %s", error)));
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
return scheduled;
|
|
535
|
+
};
|
|
536
|
+
// A run that outlasts the limit goes on in the background, and later requests for the
|
|
537
|
+
// same Agent do not wait for it again.
|
|
538
|
+
const stalled = new WeakSet();
|
|
539
|
+
const prepare = async (agent, sessionId) => {
|
|
540
|
+
const run = loadInputs()
|
|
541
|
+
.then(() => reconcileOne(agent))
|
|
542
|
+
.then(() => true, (error) => {
|
|
543
|
+
rt.warn("ocean-crew: capability mount for %s failed: %s", sessionId, error);
|
|
544
|
+
return true;
|
|
545
|
+
});
|
|
546
|
+
if (stalled.has(agent))
|
|
547
|
+
return;
|
|
548
|
+
const timeoutMs = rt.capabilityEnsureTimeoutMs ?? ENSURE_TIMEOUT_MS;
|
|
549
|
+
let timer;
|
|
550
|
+
const completed = await Promise.race([
|
|
551
|
+
run,
|
|
552
|
+
new Promise((resolve) => {
|
|
553
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
554
|
+
}),
|
|
555
|
+
]);
|
|
556
|
+
if (timer !== undefined)
|
|
557
|
+
clearTimeout(timer);
|
|
558
|
+
if (completed)
|
|
559
|
+
return;
|
|
560
|
+
stalled.add(agent);
|
|
561
|
+
void run.then(() => stalled.delete(agent));
|
|
562
|
+
rt.warn("ocean-crew: capability mount for %s did not finish within %dms; continuing", sessionId, timeoutMs);
|
|
563
|
+
};
|
|
564
|
+
async function ensureCapabilities(sessionId) {
|
|
565
|
+
await rt.load();
|
|
566
|
+
const bot = botOfSession(sessionId);
|
|
567
|
+
if (!bot)
|
|
568
|
+
return;
|
|
569
|
+
if ((bot.capabilities ?? []).length === 0 && !mounts.get(agents.get(sessionId)?.deref())?.size)
|
|
570
|
+
return;
|
|
571
|
+
let agent;
|
|
572
|
+
try {
|
|
573
|
+
agent = (await rt.resolveAgent(sessionId));
|
|
574
|
+
}
|
|
575
|
+
catch (error) {
|
|
576
|
+
rt.warn("ocean-crew: capability Agent for %s could not be resolved: %s", sessionId, rt.redactSecrets(String(error)));
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
trackAgent(agent);
|
|
580
|
+
await prepare(agent, sessionId);
|
|
581
|
+
}
|
|
582
|
+
const trackAgent = (agent) => {
|
|
583
|
+
const sessionId = agent?.session?.id;
|
|
584
|
+
if (typeof sessionId !== "string" || !agent?.ctx)
|
|
585
|
+
return;
|
|
586
|
+
if (agents.get(sessionId)?.deref() === agent)
|
|
587
|
+
return;
|
|
588
|
+
agents.set(sessionId, new WeakRef(agent));
|
|
589
|
+
agent.ctx.effect(() => () => {
|
|
590
|
+
if (agents.get(sessionId)?.deref() === agent)
|
|
591
|
+
agents.delete(sessionId);
|
|
592
|
+
clearSession(sessionId);
|
|
593
|
+
for (const mount of mounts.get(agent)?.values() ?? [])
|
|
594
|
+
void disposeMount(mount);
|
|
595
|
+
mounts.delete(agent);
|
|
596
|
+
}, `ocean-crew: capabilities ${sessionId}`);
|
|
597
|
+
};
|
|
598
|
+
const trackBotAgents = async (bot) => {
|
|
599
|
+
const sessionIds = new Set();
|
|
600
|
+
try {
|
|
601
|
+
const sessionId = rt.chatOf(bot.id);
|
|
602
|
+
if (typeof sessionId === "string")
|
|
603
|
+
sessionIds.add(sessionId);
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
rt.warn("ocean-crew: capability chat session for %s could not be read: %s", bot.id, error);
|
|
607
|
+
}
|
|
608
|
+
try {
|
|
609
|
+
for (const sessionId of rt.groupSessionsOf(bot.id) ?? []) {
|
|
610
|
+
if (typeof sessionId === "string")
|
|
611
|
+
sessionIds.add(sessionId);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
catch (error) {
|
|
615
|
+
rt.warn("ocean-crew: capability group sessions for %s could not be read: %s", bot.id, error);
|
|
616
|
+
}
|
|
617
|
+
for (const sessionId of sessionIds) {
|
|
618
|
+
try {
|
|
619
|
+
trackAgent((await rt.resolveAgent(sessionId)));
|
|
620
|
+
}
|
|
621
|
+
catch (error) {
|
|
622
|
+
rt.warn("ocean-crew: capability Agent for %s could not be resolved: %s", sessionId, error);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
async function capabilityEndpoint(endpoint, body = {}) {
|
|
627
|
+
if (endpoint === "capability-save")
|
|
628
|
+
return capabilitySave(body, { ui: true });
|
|
629
|
+
if (endpoint === "capability-delete")
|
|
630
|
+
return capabilityDelete(body.name, { ui: true });
|
|
631
|
+
if (endpoint === "capability-approve") {
|
|
632
|
+
await rt.load();
|
|
633
|
+
writeCheck();
|
|
634
|
+
const item = own(library(), String(body.name ?? ""));
|
|
635
|
+
if (!item || item.kind !== "mcp" || item.transport !== "stdio")
|
|
636
|
+
throw new Error(`No unapproved stdio MCP named ${body.name}.`);
|
|
637
|
+
item.approved = true;
|
|
638
|
+
item.updatedAt = Date.now();
|
|
639
|
+
await rt.save();
|
|
640
|
+
return item;
|
|
641
|
+
}
|
|
642
|
+
if (endpoint === "bot-capability") {
|
|
643
|
+
await rt.load();
|
|
644
|
+
writeCheck();
|
|
645
|
+
const bot = rt.botOf(String(body.botId ?? ""));
|
|
646
|
+
if (!bot)
|
|
647
|
+
throw new Error("Unknown Bot");
|
|
648
|
+
const name = String(body.name ?? "");
|
|
649
|
+
if (!own(library(), name))
|
|
650
|
+
throw new Error(`No capability named ${name}.`);
|
|
651
|
+
return setAttached(bot, name, body.attached === true);
|
|
652
|
+
}
|
|
653
|
+
throw new Error("Unknown capability operation.");
|
|
654
|
+
}
|
|
655
|
+
const secretsChanged = rt.secretsChanged;
|
|
656
|
+
rt.secretsChanged = () => {
|
|
657
|
+
secretsChanged?.();
|
|
658
|
+
if (loadingSecrets === 0)
|
|
659
|
+
void scheduleReconcile();
|
|
660
|
+
};
|
|
661
|
+
rt.capabilitiesStateSaved = () => {
|
|
662
|
+
void scheduleReconcile();
|
|
663
|
+
};
|
|
664
|
+
// Creation waits for these listeners, so a new Agent's first request already has its
|
|
665
|
+
// tools and skill catalog. agent/pre-step is too late: a step assembles its tools first.
|
|
666
|
+
ctx.on("agent/created", async function prepareCapabilities({ agent }) {
|
|
667
|
+
trackAgent(agent);
|
|
668
|
+
try {
|
|
669
|
+
await rt.load();
|
|
670
|
+
const bot = botOfSession(agent.session.id);
|
|
671
|
+
if ((bot?.capabilities ?? []).length > 0)
|
|
672
|
+
await prepare(agent, agent.session.id);
|
|
673
|
+
}
|
|
674
|
+
catch (error) {
|
|
675
|
+
rt.warn("ocean-crew: capability load failed: %s", error);
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
ctx.effect(() => {
|
|
679
|
+
void rt
|
|
680
|
+
.load()
|
|
681
|
+
.then(async () => {
|
|
682
|
+
for (const bot of Object.values(state.bots)) {
|
|
683
|
+
if ((bot.capabilities ?? []).length > 0)
|
|
684
|
+
await trackBotAgents(bot);
|
|
685
|
+
}
|
|
686
|
+
await scheduleReconcile();
|
|
687
|
+
})
|
|
688
|
+
.catch((error) => rt.warn("ocean-crew: capability startup failed: %s", error));
|
|
689
|
+
return async () => {
|
|
690
|
+
stopped = true;
|
|
691
|
+
const live = [...mounts.values()];
|
|
692
|
+
mounts.clear();
|
|
693
|
+
agents.clear();
|
|
694
|
+
sessionStatuses.clear();
|
|
695
|
+
await Promise.all(live.flatMap((agentMounts) => [...agentMounts.values()].map(disposeMount)));
|
|
696
|
+
};
|
|
697
|
+
}, "ocean-crew: capabilities");
|
|
698
|
+
// The capability tools are model-visible contract: names, parameters and the texts
|
|
699
|
+
// module's late-tool path (or directly when it is not installed, as in unit tests).
|
|
700
|
+
const addTool = (definition) => {
|
|
701
|
+
if (typeof rt.addTeamTool === "function")
|
|
702
|
+
rt.addTeamTool(definition);
|
|
703
|
+
else
|
|
704
|
+
rt.teamTools.push(definition);
|
|
705
|
+
};
|
|
706
|
+
const callerOf = (exec) => rt.selfOf(exec.agent?.id);
|
|
707
|
+
addTool(tool({
|
|
708
|
+
name: "list_capabilities",
|
|
709
|
+
description: "List shared MCP servers, skills, and packs, plus the capabilities attached to you.",
|
|
710
|
+
parameters: {},
|
|
711
|
+
execute: async (_args, exec) => {
|
|
712
|
+
await rt.load();
|
|
713
|
+
const self = callerOf(exec);
|
|
714
|
+
const bot = rt.botOf(self);
|
|
715
|
+
if (!bot)
|
|
716
|
+
return "Only Bots on the team can use list_capabilities.";
|
|
717
|
+
const items = Object.values(library());
|
|
718
|
+
if (items.length === 0)
|
|
719
|
+
return "No shared capabilities are saved yet.";
|
|
720
|
+
const statuses = capabilityStatusesView()[bot.id] ?? {};
|
|
721
|
+
const lines = items.map((item) => {
|
|
722
|
+
const extra = item.kind === "pack"
|
|
723
|
+
? `; includes ${(item.items ?? []).join(", ") || "nothing"}`
|
|
724
|
+
: item.kind === "mcp" && item.transport === "stdio"
|
|
725
|
+
? `; ${item.approved ? "allowed" : "needs user approval"}`
|
|
726
|
+
: "";
|
|
727
|
+
const itemStatus = statuses[item.name];
|
|
728
|
+
const status = itemStatus
|
|
729
|
+
? ` (${itemStatus.state}${itemStatus.error ? `: ${itemStatus.error}` : ""})`
|
|
730
|
+
: "";
|
|
731
|
+
const attached = bot.capabilities?.includes(item.name) ? `; attached to you${status}` : "";
|
|
732
|
+
return `- ${item.name} (${item.kind})${item.description ? ` — ${item.description}` : ""}${extra}${attached}`;
|
|
733
|
+
});
|
|
734
|
+
return ["Shared capabilities:", ...lines].join("\n");
|
|
735
|
+
},
|
|
736
|
+
}));
|
|
737
|
+
addTool(tool({
|
|
738
|
+
name: "save_capability",
|
|
739
|
+
description: "Create or update a shared MCP server, skill, or pack. Changes apply from your next message.",
|
|
740
|
+
parameters: {
|
|
741
|
+
name: {
|
|
742
|
+
type: "string",
|
|
743
|
+
required: true,
|
|
744
|
+
description: 'Lowercase letters, digits, and single hyphens, up to 32 characters; "github" is reserved',
|
|
745
|
+
},
|
|
746
|
+
kind: { type: "string", required: true, enum: ["mcp", "skill", "pack"] },
|
|
747
|
+
description: { type: "string", description: "What this capability provides" },
|
|
748
|
+
transport: {
|
|
749
|
+
type: "string",
|
|
750
|
+
enum: ["stdio", "streamable-http"],
|
|
751
|
+
description: "How to reach an MCP server: a local command or an HTTP URL",
|
|
752
|
+
},
|
|
753
|
+
command: { type: "string", description: "Program that starts a local MCP server" },
|
|
754
|
+
args: { type: "array", items: { type: "string" }, description: "Arguments for command" },
|
|
755
|
+
env: {
|
|
756
|
+
type: "object",
|
|
757
|
+
additionalProperties: true,
|
|
758
|
+
description: "Environment variables for command; write $DSH_SECRET_<NAME> to insert a saved secret instead of the value",
|
|
759
|
+
},
|
|
760
|
+
url: {
|
|
761
|
+
type: "string",
|
|
762
|
+
description: "MCP server URL; write $DSH_SECRET_<NAME> in the path or query to insert a saved secret, URL-encoded. Prefer headers for keys",
|
|
763
|
+
},
|
|
764
|
+
headers: {
|
|
765
|
+
type: "object",
|
|
766
|
+
additionalProperties: true,
|
|
767
|
+
description: "HTTP headers; write $DSH_SECRET_<NAME> to insert a saved secret instead of the value",
|
|
768
|
+
},
|
|
769
|
+
content: { type: "string", description: "Skill instructions" },
|
|
770
|
+
items: {
|
|
771
|
+
type: "array",
|
|
772
|
+
items: { type: "string" },
|
|
773
|
+
description: "MCP server and skill names in the pack",
|
|
774
|
+
},
|
|
775
|
+
},
|
|
776
|
+
execute: async (args, exec) => {
|
|
777
|
+
await rt.load();
|
|
778
|
+
const self = callerOf(exec);
|
|
779
|
+
if (!rt.botOf(self))
|
|
780
|
+
return "Only Bots on the team can use save_capability.";
|
|
781
|
+
try {
|
|
782
|
+
const item = await capabilitySave(args, { botId: self });
|
|
783
|
+
const approval = item.kind === "mcp" && item.transport === "stdio" && !item.approved
|
|
784
|
+
? " The user must allow this MCP server in Ocean Crew settings before it can work."
|
|
785
|
+
: "";
|
|
786
|
+
return `Saved ${item.name}. Bots with it attached use the change from their next message.${approval}`;
|
|
787
|
+
}
|
|
788
|
+
catch (error) {
|
|
789
|
+
return error.message;
|
|
790
|
+
}
|
|
791
|
+
},
|
|
792
|
+
}));
|
|
793
|
+
addTool(tool({
|
|
794
|
+
name: "delete_capability",
|
|
795
|
+
description: "Remove a shared capability and detach it from every Bot and pack.",
|
|
796
|
+
parameters: { name: { type: "string", required: true } },
|
|
797
|
+
execute: async (args, exec) => {
|
|
798
|
+
await rt.load();
|
|
799
|
+
const self = callerOf(exec);
|
|
800
|
+
if (!rt.botOf(self))
|
|
801
|
+
return "Only Bots on the team can use delete_capability.";
|
|
802
|
+
try {
|
|
803
|
+
await capabilityDelete(args.name, { botId: self });
|
|
804
|
+
return `Deleted ${args.name}; it was removed from every Bot and pack. The change applies from their next message.`;
|
|
805
|
+
}
|
|
806
|
+
catch (error) {
|
|
807
|
+
return error.message;
|
|
808
|
+
}
|
|
809
|
+
},
|
|
810
|
+
}));
|
|
811
|
+
for (const [name, endpoint, verb] of [
|
|
812
|
+
["attach_capability", "attach", "Attached"],
|
|
813
|
+
["detach_capability", "detach", "Detached"],
|
|
814
|
+
]) {
|
|
815
|
+
addTool(tool({
|
|
816
|
+
name,
|
|
817
|
+
description: `${verb} a shared capability to yourself, or to another Bot if you are a Main Bot.`,
|
|
818
|
+
parameters: {
|
|
819
|
+
name: { type: "string", required: true, description: "Capability name from list_capabilities" },
|
|
820
|
+
bot: { type: "string", description: "Optional Bot name; only a Main Bot may change another Bot" },
|
|
821
|
+
},
|
|
822
|
+
execute: async (args, exec) => {
|
|
823
|
+
await rt.load();
|
|
824
|
+
const self = callerOf(exec);
|
|
825
|
+
if (!rt.botOf(self))
|
|
826
|
+
return `Only Bots on the team can use ${name}.`;
|
|
827
|
+
try {
|
|
828
|
+
const result = await changeAttachment(endpoint, args, self);
|
|
829
|
+
const attached = own(library(), args.name);
|
|
830
|
+
const members = attached?.kind === "pack"
|
|
831
|
+
? (attached.items ?? [])
|
|
832
|
+
.map((child) => own(library(), child))
|
|
833
|
+
.filter((item) => Boolean(item))
|
|
834
|
+
: attached
|
|
835
|
+
? [attached]
|
|
836
|
+
: [];
|
|
837
|
+
const approvals = members
|
|
838
|
+
.filter((item) => item.kind === "mcp" && item.transport === "stdio" && item.approved !== true)
|
|
839
|
+
.map((item) => ` The user must allow ${item.name} in Ocean Crew settings before it can work.`)
|
|
840
|
+
.join("");
|
|
841
|
+
return `${verb} ${args.name} ${args.bot ? `to ${result.bot}` : "to yourself"}. Changes apply from ${result.bot}'s next message.${endpoint === "attach" ? approvals : ""}`;
|
|
842
|
+
}
|
|
843
|
+
catch (error) {
|
|
844
|
+
return error.message;
|
|
845
|
+
}
|
|
846
|
+
},
|
|
847
|
+
}));
|
|
848
|
+
}
|
|
849
|
+
Object.assign(rt, {
|
|
850
|
+
capabilitySave,
|
|
851
|
+
capabilityDelete,
|
|
852
|
+
dropOrphanCapabilities,
|
|
853
|
+
capabilityEndpoint,
|
|
854
|
+
capabilityLibraryView: libraryView,
|
|
855
|
+
capabilityStatusesView,
|
|
856
|
+
reconcileCapabilities: reconcile,
|
|
857
|
+
scheduleCapabilitiesReconcile: scheduleReconcile,
|
|
858
|
+
ensureCapabilities,
|
|
859
|
+
expandCapabilities: (names) => expandCapabilityNames(names, library()),
|
|
860
|
+
changeCapabilityAttachment: changeAttachment,
|
|
861
|
+
});
|
|
862
|
+
}
|