@starsharryzhu/complete-subagent 0.1.0 → 0.2.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/AGENTS.md +19 -4
- package/README.md +61 -15
- package/lib/client.js +10 -1
- package/lib/host/delegate.d.ts +12 -3
- package/lib/host/delegate.js +29 -16
- package/lib/host/models.d.ts +25 -1
- package/lib/host/models.js +70 -6
- package/lib/host/notes-tool.d.ts +25 -0
- package/lib/host/notes-tool.js +122 -0
- package/lib/host/notes.d.ts +73 -0
- package/lib/host/notes.js +128 -0
- package/lib/host/outages.d.ts +96 -0
- package/lib/host/outages.js +154 -0
- package/lib/host/policy.js +4 -1
- package/lib/host/scope.d.ts +19 -1
- package/lib/host/scope.js +41 -2
- package/lib/host/switch-tool.d.ts +47 -0
- package/lib/host/switch-tool.js +124 -0
- package/lib/host/types.d.ts +60 -11
- package/lib/host/types.js +4 -0
- package/lib/index.d.ts +48 -1
- package/lib/index.js +204 -68
- package/lib/shared/settings.d.ts +6 -0
- package/lib/shared/settings.js +8 -0
- package/package.json +9 -1
- package/src/AGENTS.md +6 -3
- package/src/host/AGENTS.md +21 -3
- package/src/host/delegate.ts +35 -17
- package/src/host/models.ts +74 -6
- package/src/host/notes-tool.ts +143 -0
- package/src/host/notes.ts +148 -0
- package/src/host/outages.ts +181 -0
- package/src/host/policy.ts +4 -1
- package/src/host/scope.ts +43 -2
- package/src/host/switch-tool.ts +150 -0
- package/src/host/types.ts +49 -11
- package/src/index.ts +224 -51
- package/src/shared/settings.ts +9 -0
package/src/index.ts
CHANGED
|
@@ -16,24 +16,44 @@
|
|
|
16
16
|
* then see none of it; it may also set `childPreset`, the preset `delegate`
|
|
17
17
|
* children run, `ownModel: false`, which keeps those children on the listed
|
|
18
18
|
* models, and the list itself at run time through the `completeSubagent` service.
|
|
19
|
+
*
|
|
20
|
+
* A parent that may choose among listed models also gets `model_notes_set`,
|
|
21
|
+
* which fills the model notes table every parent on this machine shares; the
|
|
22
|
+
* delegation tools show each listed model's note and ask for research on the
|
|
23
|
+
* models without one (see `host/notes.ts`). It also gets `switch_child_model`:
|
|
24
|
+
* when a child stops because its model is out of quota, the parent's context
|
|
25
|
+
* says so, and the parent moves the child to another listed model, where it
|
|
26
|
+
* continues its own conversation (see `host/outages.ts`).
|
|
27
|
+
*
|
|
28
|
+
* A package that carries its own copy instead of mounting a row calls
|
|
29
|
+
* {@link embed}: that copy keeps its files in the package's own folder, provides
|
|
30
|
+
* no service, and registers its tools and prompt texts only in the contexts it
|
|
31
|
+
* is given (the package's presets), so it and an installed copy never meet.
|
|
19
32
|
* @module complete-subagent
|
|
20
33
|
*/
|
|
21
34
|
|
|
35
|
+
import { join } from 'node:path'
|
|
22
36
|
import type { Context, Volatile } from '@deepseek-ai/cordis'
|
|
23
37
|
import z from '@deepseek-ai/schemastery'
|
|
24
38
|
import type { ToolDefinition } from '@deepseek-ai/dsh-tools'
|
|
25
|
-
import { DEFAULT_TIMEOUT_SECONDS, MIN_TIMEOUT_SECONDS, PLUGIN_ID, readChildPreset, readModels, readOwnModel, readPresets, readTimeout } from './shared/settings.ts'
|
|
39
|
+
import { DEFAULT_TIMEOUT_SECONDS, MIN_TIMEOUT_SECONDS, PLUGIN_ID, modelKey, readChildPreset, readMaxDepth, readModels, readOwnModel, readPresets, readTimeout } from './shared/settings.ts'
|
|
26
40
|
import type { ChildModel, ChildModelInput } from './shared/settings.ts'
|
|
27
41
|
import { withinCall } from './host/call-scope.ts'
|
|
28
42
|
import { canStart, createDelegateTool } from './host/delegate.ts'
|
|
43
|
+
import { routeOf } from './host/models.ts'
|
|
44
|
+
import type { Route } from './host/models.ts'
|
|
45
|
+
import { createNotesTool } from './host/notes-tool.ts'
|
|
46
|
+
import { ModelNotes, defaultNotesFile } from './host/notes.ts'
|
|
47
|
+
import { ModelOutages, outageContext } from './host/outages.ts'
|
|
48
|
+
import { createSwitchTool, selectRoute } from './host/switch-tool.ts'
|
|
29
49
|
import { isLegacyHost, ToolRestrictions } from './host/legacy.ts'
|
|
30
50
|
import { Fence, patchResolve } from './host/policy.ts'
|
|
31
51
|
import { childContext, pendingContext } from './host/prompt.ts'
|
|
32
52
|
import { RequestBroker } from './host/requests.ts'
|
|
33
|
-
import { presetScope } from './host/scope.ts'
|
|
53
|
+
import { claimPresets, isClaimed, presetScope, sessionPreset } from './host/scope.ts'
|
|
34
54
|
import { BindingStore, defaultStoreDirectory } from './host/store.ts'
|
|
35
55
|
import { createAnswerTool, createGrantTool, createRequestTool, createRevokeTool } from './host/tools.ts'
|
|
36
|
-
import { ANSWER_TOOL, DELEGATE_TOOLS, GRANT_TOOL, REQUEST_TOOL, REVOKE_TOOL } from './host/types.ts'
|
|
56
|
+
import { ANSWER_TOOL, DELEGATE_TOOLS, GRANT_TOOL, NOTES_TOOL, REQUEST_TOOL, REVOKE_TOOL, SWITCH_TOOL } from './host/types.ts'
|
|
37
57
|
import type {
|
|
38
58
|
AgentLike,
|
|
39
59
|
AgentPresetsLike,
|
|
@@ -41,6 +61,9 @@ import type {
|
|
|
41
61
|
DelegateTool,
|
|
42
62
|
LlmLike,
|
|
43
63
|
SandboxPolicyServiceLike,
|
|
64
|
+
SessionControllerLike,
|
|
65
|
+
SessionEventLike,
|
|
66
|
+
SessionLike,
|
|
44
67
|
SessionProjectionsLike,
|
|
45
68
|
SubagentsLike,
|
|
46
69
|
ToolCallLike,
|
|
@@ -61,6 +84,8 @@ export interface Config {
|
|
|
61
84
|
childPreset: string
|
|
62
85
|
/** False keeps `delegate` children off the parent's own model when models are listed. */
|
|
63
86
|
ownModel: boolean
|
|
87
|
+
/** How deep delegation may go from a top-level session; 0 uses the host's subagent setting. */
|
|
88
|
+
maxDepth: number
|
|
64
89
|
}
|
|
65
90
|
|
|
66
91
|
/** `ctx.completeSubagent`: what a bundle that mounts this plugin can change at run time. */
|
|
@@ -74,6 +99,31 @@ export interface CompleteSubagentService {
|
|
|
74
99
|
setChildModels(models: readonly ChildModelInput[]): void
|
|
75
100
|
}
|
|
76
101
|
|
|
102
|
+
/** What a package that carries its own copy decides. */
|
|
103
|
+
export interface EmbedOptions {
|
|
104
|
+
/** Where the copy keeps its bindings and its model notes. */
|
|
105
|
+
readonly dataDirectory: string
|
|
106
|
+
/** The agent presets it serves; other copies in this process leave them alone. */
|
|
107
|
+
readonly presets: readonly string[]
|
|
108
|
+
/** The preset `delegate` children run. */
|
|
109
|
+
readonly childPreset: string
|
|
110
|
+
/** How deep delegation may go from a top-level session; 0 uses the host's subagent setting. */
|
|
111
|
+
readonly maxDepth?: number
|
|
112
|
+
/** Tools kept from the agents of a preset, by preset id. */
|
|
113
|
+
readonly withhold?: Readonly<Record<string, readonly string[]>>
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** An embedded copy; its children run only the models set here. */
|
|
117
|
+
export interface EmbeddedSubagents extends CompleteSubagentService {
|
|
118
|
+
/**
|
|
119
|
+
* Register the tools and prompt texts in one context, for the presets' own
|
|
120
|
+
* scope. They go with that context or with the copy, whichever ends first.
|
|
121
|
+
* @param ctx - a context with the `tools` and `systemPrompt` services.
|
|
122
|
+
* @returns takes them out again.
|
|
123
|
+
*/
|
|
124
|
+
mount(ctx: Context): () => void
|
|
125
|
+
}
|
|
126
|
+
|
|
77
127
|
export const Config: z<Config> = z.object({
|
|
78
128
|
requestTimeoutSeconds: z.natural().min(MIN_TIMEOUT_SECONDS).default(DEFAULT_TIMEOUT_SECONDS)
|
|
79
129
|
.description('Seconds a parent agent has to answer a write-access request before it counts as denied').volatile(),
|
|
@@ -89,6 +139,8 @@ export const Config: z<Config> = z.object({
|
|
|
89
139
|
.description('Agent preset that children started with delegate run; empty keeps the parent\'s. delegate_fork children always keep it'),
|
|
90
140
|
ownModel: z.boolean().default(true)
|
|
91
141
|
.description('Whether delegate children may run the parent\'s own model. Off, with models listed, they run the first listed model unless the parent names another; delegate_fork children are not affected'),
|
|
142
|
+
maxDepth: z.natural().default(0)
|
|
143
|
+
.description('How deep delegation may go from a top-level session (1: children only, 2: their children too); 0 uses the host\'s subagent setting'),
|
|
92
144
|
}) as unknown as z<Config>
|
|
93
145
|
|
|
94
146
|
/** `AssembleContext` fields the prompt providers read. */
|
|
@@ -116,6 +168,7 @@ interface HostContext {
|
|
|
116
168
|
get(name: 'agentPresets'): AgentPresetsLike | undefined
|
|
117
169
|
get(name: 'sessionProjections'): SessionProjectionsLike | undefined
|
|
118
170
|
get(name: 'agents'): AgentsLike | undefined
|
|
171
|
+
get(name: 'sessionController'): SessionControllerLike | undefined
|
|
119
172
|
provide(name: 'completeSubagent', value: CompleteSubagentService): () => void
|
|
120
173
|
// oxlint-disable-next-line typescript/no-explicit-any -- host events are typed by packages this plugin does not import.
|
|
121
174
|
on(name: string, listener: (...args: any[]) => unknown): () => void
|
|
@@ -133,24 +186,70 @@ function live(value: { get(): unknown } | undefined): unknown {
|
|
|
133
186
|
return value?.get()
|
|
134
187
|
}
|
|
135
188
|
|
|
189
|
+
/** One context the tools and prompt texts are registered in. */
|
|
190
|
+
interface Site {
|
|
191
|
+
readonly host: HostContext
|
|
192
|
+
/** The model list its delegation tools were built for. */
|
|
193
|
+
key?: string
|
|
194
|
+
tools: (() => void)[]
|
|
195
|
+
readonly rest: (() => void)[]
|
|
196
|
+
}
|
|
197
|
+
|
|
136
198
|
/**
|
|
137
199
|
* Install the fence, the delegation tools, the write-access tools, and the
|
|
138
|
-
* guidance.
|
|
200
|
+
* guidance, and provide the `completeSubagent` service.
|
|
139
201
|
* @param ctx - the plugin context; the services in {@link inject} are present.
|
|
140
202
|
* @param config - loaded settings.
|
|
141
203
|
*/
|
|
142
204
|
export function apply(ctx: Context, config: Config): void {
|
|
205
|
+
const copy = start(ctx, config)
|
|
206
|
+
if (copy === undefined) return
|
|
207
|
+
copy.mount(ctx)
|
|
208
|
+
try {
|
|
209
|
+
(ctx as unknown as HostContext).provide('completeSubagent', { setChildModels: list => copy.setChildModels(list) })
|
|
210
|
+
} catch (error) {
|
|
211
|
+
// Another loaded copy already holds this name; that is a known limit, not a reason to stop.
|
|
212
|
+
ctx.logger.warn(`${PLUGIN_ID}: providing the service failed: ${String(error)}`)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Run a copy of the plugin for a package's own presets: the fence, requests, and
|
|
218
|
+
* model switching start at once; the tools and prompt texts appear where
|
|
219
|
+
* {@link EmbeddedSubagents.mount} puts them.
|
|
220
|
+
* @param ctx - a context with the services in {@link inject}; its end stops the copy.
|
|
221
|
+
* @param options - what the package decides.
|
|
222
|
+
* @returns the copy, or undefined when this host cannot carry the fence.
|
|
223
|
+
*/
|
|
224
|
+
export function embed(ctx: Context, options: EmbedOptions): EmbeddedSubagents | undefined {
|
|
225
|
+
const { presets, childPreset, maxDepth = 0 } = options
|
|
226
|
+
return start(ctx, { presets: [...presets], childPreset, maxDepth, ownModel: false }, options)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Start everything but the registrations.
|
|
231
|
+
* @param ctx - the copy's context.
|
|
232
|
+
* @param config - the settings; a missing one has its default.
|
|
233
|
+
* @param own - set for a copy a package carries.
|
|
234
|
+
* @returns the running copy, or undefined when it stays disabled.
|
|
235
|
+
*/
|
|
236
|
+
function start(ctx: Context, config: Partial<Config> | undefined, own?: EmbedOptions): EmbeddedSubagents | undefined {
|
|
143
237
|
const host = ctx as unknown as HostContext
|
|
144
238
|
const timeoutMs = (): number => readTimeout(live(config?.requestTimeoutSeconds)) * 1000
|
|
145
239
|
const childPreset = readChildPreset(config?.childPreset)
|
|
240
|
+
const maxDepth = readMaxDepth(config?.maxDepth)
|
|
146
241
|
let bundleModels: ChildModel[] | undefined
|
|
147
242
|
const models = (): ChildModel[] => bundleModels ?? readModels(live(config?.models))
|
|
148
|
-
const
|
|
243
|
+
const presetSources = {
|
|
149
244
|
agentPresets: () => host.get('agentPresets'),
|
|
150
245
|
sessionProjections: () => host.get('sessionProjections'),
|
|
151
|
-
}
|
|
246
|
+
}
|
|
247
|
+
const serves = presetScope(readPresets(config?.presets), presetSources)
|
|
152
248
|
const legacy = isLegacyHost(host.tools)
|
|
153
|
-
const store = new BindingStore(defaultStoreDirectory())
|
|
249
|
+
const store = new BindingStore(own === undefined ? defaultStoreDirectory() : join(own.dataDirectory, 'children'))
|
|
250
|
+
const notes = new ModelNotes(own === undefined ? defaultNotesFile() : join(own.dataDirectory, 'model-notes.json'))
|
|
251
|
+
const outages = new ModelOutages()
|
|
252
|
+
const pendingRoutes = new Map<string, Route>()
|
|
154
253
|
const broker = new RequestBroker()
|
|
155
254
|
const resolvePolicy = (session: AgentLike['session']) => host.sandboxPolicy.resolve({ session })
|
|
156
255
|
const warn = (message: string): void => {
|
|
@@ -177,9 +276,10 @@ export function apply(ctx: Context, config: Config): void {
|
|
|
177
276
|
restore = patchResolve(host.sandboxPolicy, (policy, request) => fence.narrow(policy, request))
|
|
178
277
|
} catch (error) {
|
|
179
278
|
warn(`${PLUGIN_ID} is disabled: ${String(error)}`)
|
|
180
|
-
return
|
|
279
|
+
return undefined
|
|
181
280
|
}
|
|
182
281
|
ctx.effect(() => restore)
|
|
282
|
+
if (own !== undefined) ctx.effect(() => claimPresets(own.presets))
|
|
183
283
|
// This listener adds no failure of its own; what `next` throws is the host's pipeline and stays its to handle.
|
|
184
284
|
host.on('tools/execute', (call: ToolCallLike, next: () => Promise<unknown>) => withinCall(call, next))
|
|
185
285
|
|
|
@@ -196,14 +296,42 @@ export function apply(ctx: Context, config: Config): void {
|
|
|
196
296
|
if (!guarded('reading the parent preset', true, () => serves((host.get('agents')?.get(parentSession) ?? agent).session))) return
|
|
197
297
|
store.set({ version: 1, child: agent.id, parent: parentSession, workspace: null, grants: [], inherited: true })
|
|
198
298
|
})
|
|
299
|
+
// A child the host put away comes back for the parent's message: give it the model the parent switched it to before its first step.
|
|
300
|
+
const route = pendingRoutes.get(agent.id)
|
|
301
|
+
if (route !== undefined) {
|
|
302
|
+
pendingRoutes.delete(agent.id)
|
|
303
|
+
guarded('switching a returning subagent\'s model', undefined, () => selectRoute(agent, route, host.get('sessionController')))
|
|
304
|
+
}
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
// A child of this plugin whose turn ended in an error waits for its parent, which is told what the error was.
|
|
308
|
+
host.on('session/event', (session: SessionLike, event: SessionEventLike) => {
|
|
309
|
+
if (event.type !== 'turn/end' && event.type !== 'turn/start') return
|
|
310
|
+
guarded('following a subagent\'s turn', undefined, () => {
|
|
311
|
+
const binding = store.get(session.id)
|
|
312
|
+
if (binding === undefined || binding.inherited === true) return
|
|
313
|
+
if (event.type === 'turn/start') {
|
|
314
|
+
outages.resolve(session.id)
|
|
315
|
+
return
|
|
316
|
+
}
|
|
317
|
+
const reason = event.data?.reason
|
|
318
|
+
if (reason?.kind === 'completed') outages.finished(session.id)
|
|
319
|
+
if (reason?.kind !== 'error' || reason.error === undefined) return
|
|
320
|
+
const agent = host.get('agents')?.get(session.id)
|
|
321
|
+
const config = session.requestHeader?.()?.config
|
|
322
|
+
const route = agent === undefined ? undefined : routeOf(agent)
|
|
323
|
+
const model = outages.runsOn(session.id) ?? (route !== undefined ? modelKey(route)
|
|
324
|
+
: config?.provider !== undefined && config.model !== undefined ? modelKey({ provider: config.provider, model: config.model }) : 'its model')
|
|
325
|
+
outages.record({ child: session.id, parent: binding.parent, model, failure: reason.error, at: Date.now() })
|
|
326
|
+
})
|
|
199
327
|
})
|
|
200
328
|
|
|
201
329
|
const tools = Object.keys(DELEGATE_TOOLS) as DelegateTool[]
|
|
202
330
|
const canDelegate = (agent: AgentLike): boolean => serves(agent.session)
|
|
203
|
-
&& tools.some(tool => host.tools.get(tool, agent) !== undefined && canStart(host.subagents, tool, agent))
|
|
331
|
+
&& tools.some(tool => host.tools.get(tool, agent) !== undefined && canStart(host.subagents, tool, agent, maxDepth))
|
|
204
332
|
const conditions = {
|
|
205
|
-
delegate: (agent: AgentLike): boolean => serves(agent.session) && canStart(host.subagents, 'delegate', agent),
|
|
206
|
-
delegate_fork: (agent: AgentLike): boolean => serves(agent.session) && canStart(host.subagents, 'delegate_fork', agent),
|
|
333
|
+
delegate: (agent: AgentLike): boolean => serves(agent.session) && canStart(host.subagents, 'delegate', agent, maxDepth),
|
|
334
|
+
delegate_fork: (agent: AgentLike): boolean => serves(agent.session) && canStart(host.subagents, 'delegate_fork', agent, maxDepth),
|
|
207
335
|
[REQUEST_TOOL]: (agent: AgentLike): boolean => {
|
|
208
336
|
const binding = store.get(agent.id)
|
|
209
337
|
return binding !== undefined && binding.inherited !== true && serves(agent.session)
|
|
@@ -211,11 +339,15 @@ export function apply(ctx: Context, config: Config): void {
|
|
|
211
339
|
[ANSWER_TOOL]: (agent: AgentLike): boolean => canDelegate(agent) || (broker.pendingFor(agent.id).length > 0 && serves(agent.session)),
|
|
212
340
|
[GRANT_TOOL]: canDelegate,
|
|
213
341
|
[REVOKE_TOOL]: canDelegate,
|
|
342
|
+
[NOTES_TOOL]: (agent: AgentLike): boolean => models().length > 0 && canDelegate(agent),
|
|
343
|
+
[SWITCH_TOOL]: (agent: AgentLike): boolean => models().length > 0 && canDelegate(agent),
|
|
214
344
|
}
|
|
215
345
|
type ToolName = keyof typeof conditions
|
|
216
|
-
const
|
|
346
|
+
const withheld = (tool: string, agent: AgentLike): boolean => own?.withhold !== undefined
|
|
347
|
+
&& own.withhold[sessionPreset(agent.session, presetSources) ?? '']?.includes(tool) === true
|
|
348
|
+
const visible = (tool: ToolName, agent: AgentLike): boolean => guarded(`the visibility check of ${tool}`, false, () => conditions[tool](agent) && !withheld(tool, agent))
|
|
217
349
|
|
|
218
|
-
// The
|
|
350
|
+
// The schemas list the configured models, so settings changes rebuild the tools; notes are read live.
|
|
219
351
|
const deps = {
|
|
220
352
|
store,
|
|
221
353
|
fence,
|
|
@@ -227,52 +359,78 @@ export function apply(ctx: Context, config: Config): void {
|
|
|
227
359
|
llm: () => host.get('llm'),
|
|
228
360
|
childPreset,
|
|
229
361
|
ownModel: readOwnModel(config?.ownModel),
|
|
362
|
+
notes,
|
|
363
|
+
maxDepth,
|
|
230
364
|
}
|
|
231
|
-
|
|
232
|
-
|
|
365
|
+
const sites = new Set<Site>()
|
|
366
|
+
let disposed = false
|
|
367
|
+
const buildDelegation = (list: readonly ChildModel[]): [ToolName, ToolDefinition][] => [
|
|
368
|
+
...tools.map(tool => [tool, createDelegateTool(tool, list, deps)] as [ToolName, ToolDefinition]),
|
|
369
|
+
[NOTES_TOOL, createNotesTool(list, { notes, llm: deps.llm })],
|
|
370
|
+
[SWITCH_TOOL, createSwitchTool(list, {
|
|
371
|
+
store,
|
|
372
|
+
outages,
|
|
373
|
+
subagents: host.subagents,
|
|
374
|
+
llm: deps.llm,
|
|
375
|
+
agents: () => host.get('agents'),
|
|
376
|
+
sessionController: () => host.get('sessionController'),
|
|
377
|
+
pendingRoutes,
|
|
378
|
+
})],
|
|
379
|
+
]
|
|
380
|
+
const register = (site: Site, tool: ToolName, definition: ToolDefinition): (() => void) =>
|
|
381
|
+
site.host.tools.register(definition, { when: ({ agent }) => visible(tool, agent) })
|
|
382
|
+
/** Bring the delegation tools of every site up to the current model list. */
|
|
383
|
+
const refresh = (): void => {
|
|
233
384
|
const list = models()
|
|
234
385
|
const key = JSON.stringify(list)
|
|
235
|
-
|
|
386
|
+
const stale = [...sites].filter(site => site.key !== key)
|
|
387
|
+
if (stale.length === 0) return
|
|
236
388
|
// Build first, so a definition that fails to build leaves the old tools in place.
|
|
237
|
-
const definitions =
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
389
|
+
const definitions = buildDelegation(list)
|
|
390
|
+
for (const site of stale) {
|
|
391
|
+
for (const dispose of site.tools.splice(0)) dispose()
|
|
392
|
+
site.key = key
|
|
393
|
+
for (const [tool, definition] of definitions) site.tools.push(register(site, tool, definition))
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const remove = (site: Site): void => {
|
|
397
|
+
if (!sites.delete(site)) return
|
|
398
|
+
for (const dispose of [...site.tools.splice(0), ...site.rest.splice(0)]) dispose()
|
|
399
|
+
}
|
|
400
|
+
const mount = (target: Context): (() => void) => {
|
|
401
|
+
if (disposed) return () => {}
|
|
402
|
+
const site: Site = { host: target as unknown as HostContext, tools: [], rest: [] }
|
|
403
|
+
sites.add(site)
|
|
404
|
+
target.effect(() => () => remove(site))
|
|
405
|
+
refresh()
|
|
406
|
+
site.rest.push(
|
|
407
|
+
register(site, REQUEST_TOOL, createRequestTool(deps)),
|
|
408
|
+
register(site, ANSWER_TOOL, createAnswerTool(deps)),
|
|
409
|
+
register(site, GRANT_TOOL, createGrantTool(deps)),
|
|
410
|
+
register(site, REVOKE_TOOL, createRevokeTool(deps)),
|
|
411
|
+
...mountContexts(site.host),
|
|
412
|
+
)
|
|
413
|
+
if (legacy) for (const agent of guarded('listing the live agents', [], () => host.get('agents')?.list?.() ?? [])) sync(agent)
|
|
414
|
+
return () => remove(site)
|
|
245
415
|
}
|
|
246
|
-
mount()
|
|
247
|
-
let disposed = false
|
|
248
416
|
ctx.effect(() => () => {
|
|
249
417
|
disposed = true
|
|
250
|
-
|
|
251
|
-
mounted = undefined
|
|
418
|
+
for (const site of sites) remove(site)
|
|
252
419
|
})
|
|
253
420
|
host.on('loader/volatile-update', () => {
|
|
254
|
-
guarded('rebuilding the delegation tools', undefined,
|
|
421
|
+
guarded('rebuilding the delegation tools', undefined, refresh)
|
|
255
422
|
})
|
|
256
|
-
// Another loaded copy already holds this name; that is a known limit, not a reason to stop.
|
|
257
|
-
guarded('providing the service', undefined, () => host.provide('completeSubagent', {
|
|
258
|
-
setChildModels(list) {
|
|
259
|
-
if (childPreset === undefined || disposed) return
|
|
260
|
-
bundleModels = readModels(list)
|
|
261
|
-
guarded('rebuilding the delegation tools', undefined, mount)
|
|
262
|
-
},
|
|
263
|
-
}))
|
|
264
|
-
|
|
265
|
-
ctx.effect(() => host.tools.register(createRequestTool(deps), { when: ({ agent }) => visible(REQUEST_TOOL, agent) }))
|
|
266
|
-
ctx.effect(() => host.tools.register(createAnswerTool(deps), { when: ({ agent }) => visible(ANSWER_TOOL, agent) }))
|
|
267
|
-
ctx.effect(() => host.tools.register(createGrantTool(deps), { when: ({ agent }) => visible(GRANT_TOOL, agent) }))
|
|
268
|
-
ctx.effect(() => host.tools.register(createRevokeTool(deps), { when: ({ agent }) => visible(REVOKE_TOOL, agent) }))
|
|
269
423
|
|
|
270
424
|
// This host drops `when`, so the same conditions become per-agent filters. A
|
|
271
425
|
// step reads its tools before `agent/pre-step`, so a change seen there takes
|
|
272
426
|
// effect on the agent's next step; a new agent is filtered before its first.
|
|
427
|
+
// An embedded copy's tools exist only for the agents it serves, and those of a
|
|
428
|
+
// preset it claimed are not for any other copy to filter.
|
|
429
|
+
let sync = (_agent: AgentLike): void => {}
|
|
273
430
|
if (legacy) {
|
|
274
431
|
const restrictions = new ToolRestrictions(Object.keys(conditions), (tool, agent) => visible(tool as ToolName, agent))
|
|
275
|
-
|
|
432
|
+
sync = (agent) => {
|
|
433
|
+
if (sites.size === 0 || (own === undefined ? isClaimed(agent.session, presetSources) : !serves(agent.session))) return
|
|
276
434
|
guarded('updating the tool filter', undefined, () => restrictions.sync(agent))
|
|
277
435
|
}
|
|
278
436
|
ctx.effect(() => () => {
|
|
@@ -288,13 +446,11 @@ export function apply(ctx: Context, config: Config): void {
|
|
|
288
446
|
if (agent !== undefined) sync(agent)
|
|
289
447
|
})
|
|
290
448
|
host.on('agent/disposed', ({ agent }: { agent: AgentLike }) => restrictions.forget(agent))
|
|
291
|
-
// Agents that were already running when the plugin loaded fire no `agent/created`.
|
|
292
|
-
for (const agent of guarded('listing the live agents', [], () => host.get('agents')?.list?.() ?? [])) sync(agent)
|
|
293
449
|
}
|
|
294
450
|
|
|
295
|
-
|
|
451
|
+
const mountContexts = (site: HostContext): (() => void)[] => [site.systemPrompt.context({
|
|
296
452
|
name: `${name}:child`,
|
|
297
|
-
order:
|
|
453
|
+
order: site.systemPrompt.getContextOrder('SUBAGENT_DELEGATION') + 1,
|
|
298
454
|
interpolate: false,
|
|
299
455
|
required: true,
|
|
300
456
|
text: context => guarded('the child context', '', () => {
|
|
@@ -302,15 +458,32 @@ export function apply(ctx: Context, config: Config): void {
|
|
|
302
458
|
const binding = agent === undefined || !serves(agent.session) ? undefined : fence.bindingOf(agent.session)
|
|
303
459
|
return binding === undefined ? '' : childContext(binding, !legacy)
|
|
304
460
|
}),
|
|
305
|
-
})
|
|
306
|
-
ctx.effect(() => host.systemPrompt.context({
|
|
461
|
+
}), site.systemPrompt.context({
|
|
307
462
|
name: `${name}:requests`,
|
|
308
|
-
order:
|
|
463
|
+
order: site.systemPrompt.getContextOrder('SUBAGENT_DELEGATION') + 2,
|
|
309
464
|
interpolate: false,
|
|
310
465
|
required: true,
|
|
311
466
|
text: context => guarded('the requests context', '', () => {
|
|
312
467
|
const agent = agentOf(context)
|
|
313
468
|
return agent === undefined || !serves(agent.session) ? '' : pendingContext(broker.pendingFor(agent.id))
|
|
314
469
|
}),
|
|
315
|
-
})
|
|
470
|
+
}), site.systemPrompt.context({
|
|
471
|
+
name: `${name}:outages`,
|
|
472
|
+
order: site.systemPrompt.getContextOrder('SUBAGENT_DELEGATION') + 3,
|
|
473
|
+
interpolate: false,
|
|
474
|
+
required: true,
|
|
475
|
+
text: context => guarded('the outages context', '', () => {
|
|
476
|
+
const agent = agentOf(context)
|
|
477
|
+
return agent === undefined || !serves(agent.session) ? '' : outageContext(outages.pendingFor(agent.id), models(), child => outages.refused(child))
|
|
478
|
+
}),
|
|
479
|
+
})]
|
|
480
|
+
|
|
481
|
+
return {
|
|
482
|
+
mount,
|
|
483
|
+
setChildModels(list) {
|
|
484
|
+
if (childPreset === undefined || disposed) return
|
|
485
|
+
bundleModels = readModels(list)
|
|
486
|
+
guarded('rebuilding the delegation tools', undefined, refresh)
|
|
487
|
+
},
|
|
488
|
+
}
|
|
316
489
|
}
|
package/src/shared/settings.ts
CHANGED
|
@@ -85,6 +85,15 @@ export function readChildPreset(value: unknown): string | undefined {
|
|
|
85
85
|
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Read a stored depth limit.
|
|
90
|
+
* @param value - the stored `maxDepth` field.
|
|
91
|
+
* @returns a whole number above 0, or undefined when the host's own limit applies.
|
|
92
|
+
*/
|
|
93
|
+
export function readMaxDepth(value: unknown): number | undefined {
|
|
94
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? value : undefined
|
|
95
|
+
}
|
|
96
|
+
|
|
88
97
|
/**
|
|
89
98
|
* Read the stored `ownModel` flag.
|
|
90
99
|
* @param value - the stored field.
|