@silmaril-security/sdk 0.9.0 → 0.10.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/CHANGELOG.md +48 -0
- package/README.md +200 -24
- package/dist/adapters/deepagents.cjs +255 -42
- package/dist/adapters/deepagents.cjs.map +1 -1
- package/dist/adapters/deepagents.d.cts +9 -5
- package/dist/adapters/deepagents.d.ts +9 -5
- package/dist/adapters/deepagents.js +261 -43
- package/dist/adapters/deepagents.js.map +1 -1
- package/dist/adapters/langchain.cjs +153 -32
- package/dist/adapters/langchain.cjs.map +1 -1
- package/dist/adapters/langchain.d.cts +1 -1
- package/dist/adapters/langchain.d.ts +1 -1
- package/dist/adapters/langchain.js +153 -32
- package/dist/adapters/langchain.js.map +1 -1
- package/dist/adapters/vercel-v4.cjs +700 -0
- package/dist/adapters/vercel-v4.cjs.map +1 -0
- package/dist/adapters/vercel-v4.d.cts +79 -0
- package/dist/adapters/vercel-v4.d.ts +79 -0
- package/dist/adapters/vercel-v4.js +670 -0
- package/dist/adapters/vercel-v4.js.map +1 -0
- package/dist/adapters/vercel.cjs +41 -15
- package/dist/adapters/vercel.cjs.map +1 -1
- package/dist/adapters/vercel.d.cts +1 -1
- package/dist/adapters/vercel.d.ts +1 -1
- package/dist/adapters/vercel.js +41 -15
- package/dist/adapters/vercel.js.map +1 -1
- package/dist/{firewall-Bv5y03P0.d.cts → firewall-BXM2BWEr.d.cts} +60 -1
- package/dist/{firewall-Bv5y03P0.d.ts → firewall-BXM2BWEr.d.ts} +60 -1
- package/dist/index.cjs +451 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +451 -50
- package/dist/index.js.map +1 -1
- package/package.json +21 -5
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
import { AIMessage, HumanMessage, ToolMessage } from "@langchain/core/messages";
|
|
3
3
|
import { ensureConfig } from "@langchain/core/runnables";
|
|
4
4
|
import { Command } from "@langchain/langgraph";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
createDeepAgent,
|
|
7
|
+
EMPTY_HARNESS_PROFILE,
|
|
8
|
+
GENERAL_PURPOSE_SUBAGENT,
|
|
9
|
+
getHarnessProfile
|
|
10
|
+
} from "deepagents";
|
|
6
11
|
import { createAgent, createMiddleware } from "langchain";
|
|
7
12
|
|
|
8
13
|
// src/exceptions.ts
|
|
@@ -45,7 +50,8 @@ var DEFAULT_HOOKS = /* @__PURE__ */ new Set([
|
|
|
45
50
|
FirewallHook.LLM_START,
|
|
46
51
|
FirewallHook.CHAT_MODEL_START,
|
|
47
52
|
FirewallHook.TOOL_START,
|
|
48
|
-
FirewallHook.TOOL_END
|
|
53
|
+
FirewallHook.TOOL_END,
|
|
54
|
+
FirewallHook.LLM_END
|
|
49
55
|
]);
|
|
50
56
|
var INPUT_HOOKS = /* @__PURE__ */ new Set([
|
|
51
57
|
FirewallHook.LLM_START,
|
|
@@ -81,6 +87,14 @@ function validateMode(value) {
|
|
|
81
87
|
throw new Error("Firewall: mode must be shadow, warn, or block");
|
|
82
88
|
}
|
|
83
89
|
|
|
90
|
+
// src/pending.ts
|
|
91
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
92
|
+
var retryLimit = new AsyncLocalStorage();
|
|
93
|
+
function conversationId(metadata) {
|
|
94
|
+
const value = metadata?.conversationId;
|
|
95
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
96
|
+
}
|
|
97
|
+
|
|
84
98
|
// src/adapters/thread-identity.ts
|
|
85
99
|
function record(value) {
|
|
86
100
|
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
@@ -112,6 +126,148 @@ var SAFE_OUTPUT_MESSAGE = "Silmaril Firewall blocked this response.";
|
|
|
112
126
|
var BLOCKED_TOOL_MARKER = "silmaril-firewall:v1:blocked-tool";
|
|
113
127
|
var issuedBlockedMessages = /* @__PURE__ */ new WeakSet();
|
|
114
128
|
var protectedCompiledGraphs = /* @__PURE__ */ new WeakMap();
|
|
129
|
+
var GENERAL_PURPOSE_DEFAULT_MIDDLEWARE = /* @__PURE__ */ new Set([
|
|
130
|
+
"FilesystemMiddleware",
|
|
131
|
+
"SummarizationMiddleware",
|
|
132
|
+
"PatchToolCallsMiddleware"
|
|
133
|
+
]);
|
|
134
|
+
function isCompiledMode(value) {
|
|
135
|
+
return value === "isolated" || value === "fork";
|
|
136
|
+
}
|
|
137
|
+
function recordValue(value) {
|
|
138
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
139
|
+
}
|
|
140
|
+
function modelCompatName(model) {
|
|
141
|
+
const getName = Reflect.get(model, "getName");
|
|
142
|
+
if (typeof getName !== "function") {
|
|
143
|
+
throw new Error(
|
|
144
|
+
"Protected Deep Agents model-object profiles require a callable getName()"
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
const name = Reflect.apply(getName, model, []);
|
|
148
|
+
if (typeof name !== "string" || !name) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
"Protected Deep Agents model-object getName() must return a non-empty string"
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
return name;
|
|
154
|
+
}
|
|
155
|
+
function optionalModelHint(record2, key, location) {
|
|
156
|
+
const value = Reflect.get(record2, key);
|
|
157
|
+
if (value === void 0 || value === null) return void 0;
|
|
158
|
+
if (typeof value !== "string") {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Protected Deep Agents ${location}.${key} profile hint must be a string`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return value;
|
|
164
|
+
}
|
|
165
|
+
function configurableDefaults(model) {
|
|
166
|
+
const value = Reflect.get(model, "_defaultConfig");
|
|
167
|
+
if (value === void 0 || value === null) return void 0;
|
|
168
|
+
const defaults = recordValue(value);
|
|
169
|
+
if (defaults === void 0) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
"Protected Deep Agents ConfigurableModel._defaultConfig must be an object"
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return defaults;
|
|
175
|
+
}
|
|
176
|
+
function modelProviderHint(model) {
|
|
177
|
+
const name = modelCompatName(model);
|
|
178
|
+
if (name === "ConfigurableModel") {
|
|
179
|
+
const defaults = configurableDefaults(model);
|
|
180
|
+
return defaults === void 0 ? void 0 : optionalModelHint(defaults, "modelProvider", "_defaultConfig");
|
|
181
|
+
}
|
|
182
|
+
const providers = {
|
|
183
|
+
ChatAnthropic: "anthropic",
|
|
184
|
+
ChatOpenAI: "openai",
|
|
185
|
+
ChatGoogleGenerativeAI: "google"
|
|
186
|
+
};
|
|
187
|
+
return providers[name];
|
|
188
|
+
}
|
|
189
|
+
function modelIdentifierHint(model) {
|
|
190
|
+
const name = modelCompatName(model);
|
|
191
|
+
const configurable = name === "ConfigurableModel" ? configurableDefaults(model) : void 0;
|
|
192
|
+
return (configurable === void 0 ? void 0 : optionalModelHint(configurable, "model", "_defaultConfig")) ?? optionalModelHint(model, "model_name", name) ?? optionalModelHint(model, "modelName", name);
|
|
193
|
+
}
|
|
194
|
+
function resolveProtectedHarnessProfile(model) {
|
|
195
|
+
if (typeof model === "string") {
|
|
196
|
+
return getHarnessProfile(model);
|
|
197
|
+
}
|
|
198
|
+
if (typeof model !== "object" && typeof model !== "function" || model === null) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
"Protected Deep Agents model must be a string or model object"
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
const providerHint = modelProviderHint(model);
|
|
204
|
+
const identifierHint = modelIdentifierHint(model);
|
|
205
|
+
if (providerHint && identifierHint && !identifierHint.includes(":")) {
|
|
206
|
+
const exact = getHarnessProfile(`${providerHint}:${identifierHint}`);
|
|
207
|
+
if (exact !== void 0) return exact;
|
|
208
|
+
}
|
|
209
|
+
if (identifierHint?.includes(":")) {
|
|
210
|
+
const exact = getHarnessProfile(identifierHint);
|
|
211
|
+
if (exact !== void 0) return exact;
|
|
212
|
+
}
|
|
213
|
+
if (providerHint) {
|
|
214
|
+
const provider = getHarnessProfile(providerHint);
|
|
215
|
+
if (provider !== void 0) return provider;
|
|
216
|
+
}
|
|
217
|
+
return EMPTY_HARNESS_PROFILE;
|
|
218
|
+
}
|
|
219
|
+
function applyProfileToolDescriptions(tools, profile) {
|
|
220
|
+
const overrides = profile?.toolDescriptionOverrides ?? {};
|
|
221
|
+
if (Object.keys(overrides).length === 0) return [...tools];
|
|
222
|
+
return tools.map((tool) => {
|
|
223
|
+
if (typeof tool !== "object" && typeof tool !== "function" || tool === null) {
|
|
224
|
+
return tool;
|
|
225
|
+
}
|
|
226
|
+
const name = Reflect.get(tool, "name");
|
|
227
|
+
if (typeof name !== "string" || !(name in overrides)) return tool;
|
|
228
|
+
return Object.assign(
|
|
229
|
+
Object.create(Object.getPrototypeOf(tool)),
|
|
230
|
+
tool,
|
|
231
|
+
{ description: overrides[name] }
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function generalPurposeRootOverrides(profile, middleware) {
|
|
236
|
+
const profileMiddleware = profile?.extraMiddleware ?? [];
|
|
237
|
+
if (typeof profileMiddleware === "function" && middleware.length > 0) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
"Protected Deep Agents cannot preserve root middleware overrides when the active profile uses an extraMiddleware factory; use a static profile middleware array"
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
const names = new Set(GENERAL_PURPOSE_DEFAULT_MIDDLEWARE);
|
|
243
|
+
names.add("SkillsMiddleware");
|
|
244
|
+
if (Array.isArray(profileMiddleware)) {
|
|
245
|
+
for (const entry of profileMiddleware) names.add(entry.name);
|
|
246
|
+
}
|
|
247
|
+
for (const excluded of profile?.excludedMiddleware ?? []) {
|
|
248
|
+
names.delete(excluded);
|
|
249
|
+
}
|
|
250
|
+
return middleware.filter((entry) => names.has(entry.name));
|
|
251
|
+
}
|
|
252
|
+
function applyGeneralPurposeProfile(profile) {
|
|
253
|
+
const config = profile?.generalPurposeSubagent;
|
|
254
|
+
if (config?.systemPrompt !== void 0) {
|
|
255
|
+
return {
|
|
256
|
+
description: config.description ?? GENERAL_PURPOSE_SUBAGENT.description,
|
|
257
|
+
systemPrompt: config.systemPrompt
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
let systemPrompt = profile?.baseSystemPrompt ?? GENERAL_PURPOSE_SUBAGENT.systemPrompt;
|
|
261
|
+
if (profile?.systemPromptSuffix !== void 0) {
|
|
262
|
+
systemPrompt = systemPrompt ? `${systemPrompt}
|
|
263
|
+
|
|
264
|
+
${profile.systemPromptSuffix}` : profile.systemPromptSuffix;
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
description: config?.description ?? GENERAL_PURPOSE_SUBAGENT.description,
|
|
268
|
+
systemPrompt
|
|
269
|
+
};
|
|
270
|
+
}
|
|
115
271
|
function textOf(message) {
|
|
116
272
|
return typeof message.content === "string" ? message.content : JSON.stringify(message.content);
|
|
117
273
|
}
|
|
@@ -176,50 +332,74 @@ function createDeepAgentsMiddleware(firewall, options = {}) {
|
|
|
176
332
|
}
|
|
177
333
|
const effectiveMode = validateMode(options.mode) ?? firewall.mode;
|
|
178
334
|
const notificationChecks = /* @__PURE__ */ new Map();
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
const mode = effectiveMode ?? result.mode ?? "block";
|
|
189
|
-
const blocked = result.prediction === "MALICIOUS" || result.governance?.action === "block";
|
|
190
|
-
if (options.onClassify) {
|
|
191
|
-
try {
|
|
192
|
-
options.onClassify({ hook, ...toolName === void 0 ? {} : { toolName }, text, result, blocked, mode, shadowMode: mode === "shadow" });
|
|
193
|
-
} catch {
|
|
194
|
-
}
|
|
335
|
+
const parallel = options.parallel ?? false;
|
|
336
|
+
const background = effectiveMode === "shadow" && options.failOpen !== false;
|
|
337
|
+
const decide = (result, text, hook, toolName) => {
|
|
338
|
+
const mode = effectiveMode ?? result.mode ?? "block";
|
|
339
|
+
const blocked = result.prediction === "MALICIOUS" || result.governance?.action === "block";
|
|
340
|
+
if (options.onClassify) {
|
|
341
|
+
try {
|
|
342
|
+
options.onClassify({ hook, ...toolName === void 0 ? {} : { toolName }, text, result, blocked, mode, shadowMode: mode === "shadow" });
|
|
343
|
+
} catch {
|
|
195
344
|
}
|
|
196
|
-
return { enforce: blocked && mode === "block", mode, cacheable: true };
|
|
197
|
-
} catch (error) {
|
|
198
|
-
if (isReviewCompatibilityError(error)) throw error;
|
|
199
|
-
if (options.failOpen === false) throw error;
|
|
200
|
-
return { enforce: false, mode: effectiveMode };
|
|
201
345
|
}
|
|
346
|
+
return { enforce: blocked && mode === "block", mode, cacheable: true };
|
|
347
|
+
};
|
|
348
|
+
const prepare = (text, hook, conversationId2, toolName) => {
|
|
349
|
+
if (!text.trim()) return async () => ({ enforce: false, mode: effectiveMode });
|
|
350
|
+
const request = {
|
|
351
|
+
hook,
|
|
352
|
+
...toolName === void 0 ? {} : { toolName },
|
|
353
|
+
...effectiveMode === void 0 ? {} : { mode: effectiveMode },
|
|
354
|
+
metadata: { ...conversationId2 === void 0 ? {} : { conversationId: conversationId2 }, ...remoteLineage(ensureConfig()) }
|
|
355
|
+
};
|
|
356
|
+
if (background) {
|
|
357
|
+
const observed = firewall.observe(text, request).then((result) => {
|
|
358
|
+
if (result !== void 0) decide(result, text, hook, toolName);
|
|
359
|
+
return result !== void 0;
|
|
360
|
+
});
|
|
361
|
+
return async () => ({ enforce: false, mode: effectiveMode, observed });
|
|
362
|
+
}
|
|
363
|
+
return async () => {
|
|
364
|
+
try {
|
|
365
|
+
return decide(await firewall.classify(text, request), text, hook, toolName);
|
|
366
|
+
} catch (error) {
|
|
367
|
+
if (isReviewCompatibilityError(error)) throw error;
|
|
368
|
+
if (options.failOpen === false) throw error;
|
|
369
|
+
return { enforce: false, mode: effectiveMode };
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
const classify = (text, hook, conversationId2, toolName) => {
|
|
374
|
+
const check = prepare(text, hook, conversationId2, toolName);
|
|
375
|
+
return parallel && !background ? firewall.pending.submit(check, conversationId({ conversationId: conversationId2 })) : check();
|
|
376
|
+
};
|
|
377
|
+
const rememberNotification = (key, blocked) => {
|
|
378
|
+
notificationChecks.set(key, blocked);
|
|
379
|
+
while (notificationChecks.size > 1024) notificationChecks.delete(notificationChecks.keys().next().value);
|
|
202
380
|
};
|
|
203
381
|
return createMiddleware({
|
|
204
382
|
name: "SilmarilDeepAgentsMiddleware",
|
|
205
383
|
wrapModelCall: async (request, handler) => {
|
|
206
|
-
const
|
|
384
|
+
const conversationId2 = nativeThreadId(request.runtime, void 0, void 0, ensureConfig());
|
|
207
385
|
const messages = [];
|
|
208
386
|
for (const message of request.messages) {
|
|
209
387
|
let blocked = false;
|
|
210
388
|
if (remoteNotification(message)) {
|
|
211
|
-
const key = await notificationKey(message,
|
|
389
|
+
const key = await notificationKey(message, conversationId2, effectiveMode);
|
|
212
390
|
const cached = key === void 0 ? void 0 : notificationChecks.get(key);
|
|
213
391
|
if (cached !== void 0) {
|
|
214
392
|
blocked = cached;
|
|
215
393
|
notificationChecks.delete(key);
|
|
216
394
|
notificationChecks.set(key, cached);
|
|
217
395
|
} else {
|
|
218
|
-
const decision = await classify(textOf(message), HookLabel.TOOL_RESPONSE,
|
|
396
|
+
const decision = await classify(textOf(message), HookLabel.TOOL_RESPONSE, conversationId2, "async_subagent");
|
|
219
397
|
blocked = decision.enforce;
|
|
220
|
-
if (key !== void 0 && decision.cacheable)
|
|
221
|
-
|
|
222
|
-
|
|
398
|
+
if (key !== void 0 && decision.cacheable) rememberNotification(key, blocked);
|
|
399
|
+
if (key !== void 0) {
|
|
400
|
+
void decision.observed?.then((completed) => {
|
|
401
|
+
if (completed) rememberNotification(key, false);
|
|
402
|
+
});
|
|
223
403
|
}
|
|
224
404
|
}
|
|
225
405
|
}
|
|
@@ -238,23 +418,27 @@ function createDeepAgentsMiddleware(firewall, options = {}) {
|
|
|
238
418
|
(message) => ToolMessage.isInstance(message) && message.content === SAFE_TOOL_MESSAGE && message.additional_kwargs?.silmarilBlocked === BLOCKED_TOOL_MARKER
|
|
239
419
|
).length;
|
|
240
420
|
const latestUser = [...request.messages].reverse().find(HumanMessage.isInstance);
|
|
241
|
-
const
|
|
421
|
+
const racesModel = parallel && !background && blockedCount < maxBlockedAttempts;
|
|
422
|
+
const protectedRequest = request;
|
|
423
|
+
const running = racesModel ? Promise.resolve().then(() => handler(protectedRequest)) : void 0;
|
|
424
|
+
running?.catch(() => void 0);
|
|
425
|
+
const inputDecision = latestUser === void 0 ? void 0 : await classify(textOf(latestUser), HookLabel.USER_INPUT, conversationId2);
|
|
242
426
|
if (inputDecision?.enforce) return new AIMessage(SAFE_OUTPUT_MESSAGE);
|
|
243
427
|
const capMode = effectiveMode ?? inputDecision?.mode ?? "block";
|
|
244
428
|
if (blockedCount >= maxBlockedAttempts && capMode === "block") {
|
|
245
429
|
return new AIMessage(SAFE_FINAL_MESSAGE);
|
|
246
430
|
}
|
|
247
|
-
const response = await handler(request);
|
|
248
|
-
if (response instanceof Command && (await classify(textOfStructuredValue(response.update), HookLabel.LLM_OUTPUT,
|
|
431
|
+
const response = await (running ?? handler(request));
|
|
432
|
+
if (response instanceof Command && (await classify(textOfStructuredValue(response.update), HookLabel.LLM_OUTPUT, conversationId2)).enforce) {
|
|
249
433
|
return new AIMessage(SAFE_OUTPUT_MESSAGE);
|
|
250
434
|
}
|
|
251
|
-
if (AIMessage.isInstance(response) && (await classify(textOf(response), HookLabel.LLM_OUTPUT,
|
|
435
|
+
if (AIMessage.isInstance(response) && (await classify(textOf(response), HookLabel.LLM_OUTPUT, conversationId2)).enforce) {
|
|
252
436
|
return new AIMessage(SAFE_OUTPUT_MESSAGE);
|
|
253
437
|
}
|
|
254
438
|
return response;
|
|
255
439
|
},
|
|
256
440
|
wrapToolCall: async (request, handler) => {
|
|
257
|
-
const
|
|
441
|
+
const conversationId2 = nativeThreadId(request.runtime, void 0, void 0, ensureConfig());
|
|
258
442
|
const safe = () => {
|
|
259
443
|
const message = new ToolMessage({
|
|
260
444
|
content: SAFE_TOOL_MESSAGE,
|
|
@@ -265,7 +449,7 @@ function createDeepAgentsMiddleware(firewall, options = {}) {
|
|
|
265
449
|
issuedBlockedMessages.add(message);
|
|
266
450
|
return message;
|
|
267
451
|
};
|
|
268
|
-
if ((await classify(JSON.stringify(request.toolCall.args ?? {}), HookLabel.TOOL_CALL,
|
|
452
|
+
if ((await classify(JSON.stringify(request.toolCall.args ?? {}), HookLabel.TOOL_CALL, conversationId2, request.toolCall.name)).enforce) {
|
|
269
453
|
return safe();
|
|
270
454
|
}
|
|
271
455
|
const result = await handler(request);
|
|
@@ -273,10 +457,10 @@ function createDeepAgentsMiddleware(firewall, options = {}) {
|
|
|
273
457
|
try {
|
|
274
458
|
resultText = ToolMessage.isInstance(result) ? textOf(result) : result instanceof Command ? textOfStructuredValue(result.update) : String(result);
|
|
275
459
|
} catch {
|
|
276
|
-
const decision = await classify("[uninspectable tool result]", HookLabel.TOOL_RESPONSE,
|
|
460
|
+
const decision = await classify("[uninspectable tool result]", HookLabel.TOOL_RESPONSE, conversationId2, request.toolCall.name);
|
|
277
461
|
return decision.mode === "block" ? safe() : stripAllowedMarker(result);
|
|
278
462
|
}
|
|
279
|
-
if ((await classify(resultText, HookLabel.TOOL_RESPONSE,
|
|
463
|
+
if ((await classify(resultText, HookLabel.TOOL_RESPONSE, conversationId2, request.toolCall.name)).enforce) {
|
|
280
464
|
if (["start_async_task", "check_async_task", "update_async_task", "cancel_async_task", "list_async_tasks"].includes(request.toolCall.name) && result instanceof Command && result.update && !Array.isArray(result.update) && "asyncTasks" in result.update) {
|
|
281
465
|
return new Command({ update: { messages: [safe()], asyncTasks: result.update.asyncTasks } });
|
|
282
466
|
}
|
|
@@ -288,17 +472,32 @@ function createDeepAgentsMiddleware(firewall, options = {}) {
|
|
|
288
472
|
}
|
|
289
473
|
function createProtectedCompiledSubagent(firewall, options) {
|
|
290
474
|
rejectConversationOptions(options);
|
|
291
|
-
const { name, description, silmaril, middleware = [], ...agentOptions } = options;
|
|
475
|
+
const { name, description, mode, silmaril, middleware = [], ...agentOptions } = options;
|
|
476
|
+
if (mode !== void 0 && !isCompiledMode(mode)) {
|
|
477
|
+
throw new Error("Compiled subagent mode must be isolated or fork");
|
|
478
|
+
}
|
|
292
479
|
const runnable = createAgent({
|
|
293
480
|
...agentOptions,
|
|
294
481
|
middleware: [...middleware, createDeepAgentsMiddleware(firewall, silmaril)]
|
|
295
482
|
});
|
|
296
|
-
protectedCompiledGraphs.set(runnable, firewall);
|
|
297
|
-
return {
|
|
483
|
+
protectedCompiledGraphs.set(runnable, { firewall, mode });
|
|
484
|
+
return {
|
|
485
|
+
name,
|
|
486
|
+
description,
|
|
487
|
+
runnable,
|
|
488
|
+
...mode === void 0 ? {} : { mode }
|
|
489
|
+
};
|
|
298
490
|
}
|
|
299
491
|
function createProtectedDeepAgent(firewall, options = {}) {
|
|
300
492
|
rejectConversationOptions(options);
|
|
301
493
|
const { silmaril, subagents = [], protectedCompiledSubagents = [], middleware = [], ...agentOptions } = options;
|
|
494
|
+
const model = agentOptions.model ?? "anthropic:claude-sonnet-4-6";
|
|
495
|
+
const harnessProfile = resolveProtectedHarnessProfile(model);
|
|
496
|
+
const parentTools = agentOptions.tools ?? [];
|
|
497
|
+
const effectiveParentTools = applyProfileToolDescriptions(
|
|
498
|
+
parentTools,
|
|
499
|
+
harnessProfile
|
|
500
|
+
);
|
|
302
501
|
const protectedSubagents = subagents.map((subagent) => {
|
|
303
502
|
if ("runnable" in subagent) {
|
|
304
503
|
throw new Error("Compiled subagents must use createProtectedCompiledSubagent");
|
|
@@ -306,11 +505,30 @@ function createProtectedDeepAgent(firewall, options = {}) {
|
|
|
306
505
|
if ("graphId" in subagent) return { ...subagent };
|
|
307
506
|
return { ...subagent, middleware: [...subagent.middleware ?? [], createDeepAgentsMiddleware(firewall, silmaril)] };
|
|
308
507
|
});
|
|
309
|
-
|
|
310
|
-
|
|
508
|
+
const gpConfig = harnessProfile?.generalPurposeSubagent;
|
|
509
|
+
if (gpConfig?.enabled !== false && !protectedSubagents.some(
|
|
510
|
+
(subagent) => subagent.name === GENERAL_PURPOSE_SUBAGENT.name
|
|
511
|
+
)) {
|
|
512
|
+
const inheritedMiddleware = generalPurposeRootOverrides(
|
|
513
|
+
harnessProfile,
|
|
514
|
+
middleware
|
|
515
|
+
);
|
|
516
|
+
protectedSubagents.unshift({
|
|
517
|
+
...GENERAL_PURPOSE_SUBAGENT,
|
|
518
|
+
...applyGeneralPurposeProfile(harnessProfile),
|
|
519
|
+
model,
|
|
520
|
+
tools: effectiveParentTools,
|
|
521
|
+
...agentOptions.skills === void 0 ? {} : { skills: agentOptions.skills },
|
|
522
|
+
middleware: [
|
|
523
|
+
...inheritedMiddleware,
|
|
524
|
+
createDeepAgentsMiddleware(firewall, silmaril)
|
|
525
|
+
]
|
|
526
|
+
});
|
|
311
527
|
}
|
|
312
528
|
for (const spec of protectedCompiledSubagents) {
|
|
313
|
-
|
|
529
|
+
const registration = spec.runnable ? protectedCompiledGraphs.get(spec.runnable) : void 0;
|
|
530
|
+
const candidateMode = spec.mode;
|
|
531
|
+
if (registration?.firewall !== firewall || candidateMode !== void 0 && !isCompiledMode(candidateMode) || candidateMode !== registration.mode) {
|
|
314
532
|
throw new Error("Compiled subagents must be built with createProtectedCompiledSubagent for this Firewall client");
|
|
315
533
|
}
|
|
316
534
|
}
|