@starsharryzhu/complete-subagent 0.0.0-stage → 0.1.1
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 +58 -0
- package/LICENSE +21 -0
- package/README.md +168 -2
- package/cordis.patch.yml +11 -0
- package/lib/client.js +439 -0
- package/lib/client.modules.txt +8 -0
- package/lib/host/access.d.ts +46 -0
- package/lib/host/access.js +54 -0
- package/lib/host/call-scope.d.ts +21 -0
- package/lib/host/call-scope.js +26 -0
- package/lib/host/delegate.d.ts +47 -0
- package/lib/host/delegate.js +133 -0
- package/lib/host/grants.d.ts +51 -0
- package/lib/host/grants.js +62 -0
- package/lib/host/legacy.d.ts +57 -0
- package/lib/host/legacy.js +85 -0
- package/lib/host/models.d.ts +59 -0
- package/lib/host/models.js +139 -0
- package/lib/host/paths.d.ts +51 -0
- package/lib/host/paths.js +100 -0
- package/lib/host/policy.d.ts +72 -0
- package/lib/host/policy.js +169 -0
- package/lib/host/prompt.d.ts +41 -0
- package/lib/host/prompt.js +100 -0
- package/lib/host/requests.d.ts +82 -0
- package/lib/host/requests.js +109 -0
- package/lib/host/scope.d.ts +33 -0
- package/lib/host/scope.js +46 -0
- package/lib/host/store.d.ts +89 -0
- package/lib/host/store.js +128 -0
- package/lib/host/tools.d.ts +58 -0
- package/lib/host/tools.js +298 -0
- package/lib/host/types.d.ts +184 -0
- package/lib/host/types.js +26 -0
- package/lib/index.d.ts +55 -0
- package/lib/index.js +245 -0
- package/lib/shared/settings.d.ts +69 -0
- package/lib/shared/settings.js +86 -0
- package/locale/AGENTS.md +22 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +95 -4
- package/src/AGENTS.md +26 -0
- package/src/client/AGENTS.md +28 -0
- package/src/client/css.d.ts +5 -0
- package/src/client/host.ts +82 -0
- package/src/client/index.ts +81 -0
- package/src/client/locales.ts +61 -0
- package/src/client/settings.tsx +222 -0
- package/src/client/styles.css +44 -0
- package/src/host/AGENTS.md +53 -0
- package/src/host/access.ts +82 -0
- package/src/host/call-scope.ts +31 -0
- package/src/host/delegate.ts +166 -0
- package/src/host/grants.ts +82 -0
- package/src/host/legacy.ts +86 -0
- package/src/host/models.ts +155 -0
- package/src/host/paths.ts +100 -0
- package/src/host/policy.ts +169 -0
- package/src/host/prompt.ts +113 -0
- package/src/host/requests.ts +149 -0
- package/src/host/scope.ts +54 -0
- package/src/host/store.ts +159 -0
- package/src/host/tools.ts +318 -0
- package/src/host/types.ts +192 -0
- package/src/index.ts +316 -0
- package/src/shared/AGENTS.md +20 -0
- package/src/shared/settings.ts +104 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural mirrors of the host services this plugin reads.
|
|
3
|
+
*
|
|
4
|
+
* The plugin imports only `@deepseek-ai/cordis`, `@deepseek-ai/schemastery`, and
|
|
5
|
+
* `@deepseek-ai/dsh-tools` at runtime, so these types mirror only the fields it
|
|
6
|
+
* uses from `@deepseek-ai/dsh-session` (header), `@deepseek-ai/dsh-agent`,
|
|
7
|
+
* `@deepseek-ai/dsh-agent-preset-registry`, `@deepseek-ai/dsh-session-projection`,
|
|
8
|
+
* `@deepseek-ai/dsh-sandbox-policy`, `@deepseek-ai/dsh-working-directory`,
|
|
9
|
+
* `@deepseek-ai/dsh-subagent`, and `@deepseek-ai/dsh-llm`. Each mirror names its
|
|
10
|
+
* source for the next reader, and says so when a field is missing from the
|
|
11
|
+
* oldest supported host, 0.2.1-rc.5.
|
|
12
|
+
* @module complete-subagent/host/types
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** `SessionHeader`: the immutable storage metadata of one session. */
|
|
16
|
+
export interface SessionHeaderLike {
|
|
17
|
+
readonly id: string
|
|
18
|
+
/** Absolute directory the session was created in; the sandbox workspace root. */
|
|
19
|
+
readonly cwd?: string
|
|
20
|
+
/** Direct creator of a subagent session. */
|
|
21
|
+
readonly parentSession?: string
|
|
22
|
+
/** `'subagent'` for a delegated child. */
|
|
23
|
+
readonly origin?: 'subagent'
|
|
24
|
+
/** Persisted delegation depth of a child (`delegationDepthOf`). */
|
|
25
|
+
readonly delegationDepth?: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The provider, model, and effort of one LLM route (`AgentOptions` / `LlmCallConfig`). */
|
|
29
|
+
export interface RouteLike {
|
|
30
|
+
readonly provider?: string
|
|
31
|
+
readonly model?: string
|
|
32
|
+
readonly reasoningEffort?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `Session`: identity, header, and the route of its next request. */
|
|
36
|
+
export interface SessionLike {
|
|
37
|
+
readonly id: string
|
|
38
|
+
readonly header: SessionHeaderLike
|
|
39
|
+
/** The latest request header; its `config` is the session's current route. */
|
|
40
|
+
requestHeader?(): { readonly config?: RouteLike } | undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `Agent`: a live agent; its id is its session id. */
|
|
44
|
+
export interface AgentLike {
|
|
45
|
+
readonly id: string
|
|
46
|
+
readonly session: SessionLike
|
|
47
|
+
/** Creation options: the route before the first request, and a runtime depth. */
|
|
48
|
+
readonly options?: RouteLike & { readonly subagentDepth?: number }
|
|
49
|
+
/** The agent's scoped context; a restriction made through it covers this agent only. */
|
|
50
|
+
readonly ctx?: {
|
|
51
|
+
readonly tools?: { restrict(filter: { readonly deny: readonly string[] }): () => void }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `ctx.agents` (`AgentRegistry` of `@deepseek-ai/dsh-agent`): live agents by id. */
|
|
56
|
+
export interface AgentsLike {
|
|
57
|
+
get(id: string): AgentLike | undefined
|
|
58
|
+
/** All live agents. */
|
|
59
|
+
list?(): readonly AgentLike[]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `ctx.agentPresets` (`AgentPresetRegistry` of `@deepseek-ai/dsh-agent-preset-registry`). */
|
|
63
|
+
export interface AgentPresetsLike {
|
|
64
|
+
/** The preset a session runs: its header's, or a later selection; null when none was recorded. Missing from 0.2.1-rc.5. */
|
|
65
|
+
presetForSession?(session: SessionLike): string | null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `ctx.sessionProjections` (`SessionProjectionRegistry` of `@deepseek-ai/dsh-session-projection`). */
|
|
69
|
+
export interface SessionProjectionsLike {
|
|
70
|
+
/** One projection's current state, or undefined when its key is not registered. */
|
|
71
|
+
stateOf(session: SessionLike, key: string): unknown
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `SandboxMode` of `@deepseek-ai/dsh-sandbox`. */
|
|
75
|
+
export type SandboxMode = 'read-only' | 'workspace-write' | 'danger-full-access'
|
|
76
|
+
|
|
77
|
+
/** `SandboxExecutionPolicy`: what one confined capability call may write. */
|
|
78
|
+
export interface SandboxPolicy {
|
|
79
|
+
readonly mode: SandboxMode
|
|
80
|
+
readonly workspaceRoot: string
|
|
81
|
+
readonly additionalWritableRoots?: readonly string[]
|
|
82
|
+
readonly sessionId?: string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `SandboxPolicyRequest`. */
|
|
86
|
+
export interface SandboxPolicyRequest {
|
|
87
|
+
readonly session?: SessionLike
|
|
88
|
+
readonly mode?: SandboxMode
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** `ctx.sandboxPolicy` (`SandboxPolicyService`). */
|
|
92
|
+
export interface SandboxPolicyServiceLike {
|
|
93
|
+
resolve(request?: SandboxPolicyRequest): SandboxPolicy
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** `ctx.workingDirectory` (`WorkingDirectoryService`). */
|
|
97
|
+
export interface WorkingDirectoryLike {
|
|
98
|
+
/** Effective directory of a session, without repairing a vanished one. */
|
|
99
|
+
get(session: SessionLike): string
|
|
100
|
+
/** Effective directory of an agent, restoring the header cwd when it vanished. */
|
|
101
|
+
ensure(agent: AgentLike, signal?: AbortSignal): Promise<string>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** One text content block, the only kind this plugin authors. */
|
|
105
|
+
export interface TextBlock {
|
|
106
|
+
readonly type: 'text'
|
|
107
|
+
readonly text: string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** `SubagentStartRequest`: the task part of a start, as this plugin fills it. */
|
|
111
|
+
export interface StartRequestLike {
|
|
112
|
+
readonly cwd?: string
|
|
113
|
+
readonly prompt: TextBlock[]
|
|
114
|
+
readonly parent: AgentLike
|
|
115
|
+
/** Preset the child runs instead of the parent's; only a backend that does not inherit the parent's context accepts it. */
|
|
116
|
+
readonly agentPreset?: string
|
|
117
|
+
readonly agentOptions?: RouteLike
|
|
118
|
+
readonly maxDepth?: number
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `SubagentActivationSpec` with a reserved child id, as this plugin fills it.
|
|
123
|
+
* Hosts from 0.2.2-rc.2 read the task at the top level; older ones read it
|
|
124
|
+
* under `request` and ignore the top-level copy, and newer ones the reverse,
|
|
125
|
+
* so the plugin sends both.
|
|
126
|
+
*/
|
|
127
|
+
export interface ActivationSpecLike extends StartRequestLike {
|
|
128
|
+
/** Registered backend: `spawn` (fresh child) or `fork` (inherits the conversation). */
|
|
129
|
+
readonly provider: string
|
|
130
|
+
/** Short task label shown with the child. */
|
|
131
|
+
readonly label: string
|
|
132
|
+
/** Reserved child session id, so the child is bound before it exists. */
|
|
133
|
+
readonly childId: string
|
|
134
|
+
/** The same task, for hosts before 0.2.2-rc.2. */
|
|
135
|
+
readonly request: StartRequestLike
|
|
136
|
+
readonly signal: AbortSignal
|
|
137
|
+
/** `parent`: the runtime notifies the parent model when the child finishes. */
|
|
138
|
+
readonly delivery: 'parent'
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** `ctx.subagents` (`SubagentRuntime`). */
|
|
142
|
+
export interface SubagentsLike {
|
|
143
|
+
/** Adjacent-agent messaging, for write-access requests. */
|
|
144
|
+
sendMessage(sender: AgentLike, targetId: string, content: TextBlock[], options: { signal: AbortSignal }): Promise<unknown>
|
|
145
|
+
/** Start a managed child; resolves once it is published. */
|
|
146
|
+
startActivation(spec: ActivationSpecLike): Promise<{ readonly childId: string }>
|
|
147
|
+
/** The user's delegation depth limit, or undefined when there is none. */
|
|
148
|
+
resolveMaxDepth(): number | undefined
|
|
149
|
+
/** A registered backend, or undefined. */
|
|
150
|
+
getProvider(name: string): unknown
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** `LlmResolvedModelInfo`, reduced to its reasoning levels. */
|
|
154
|
+
export interface ModelInfoLike {
|
|
155
|
+
readonly reasoning?: {
|
|
156
|
+
readonly efforts: readonly { readonly id: string }[]
|
|
157
|
+
readonly defaultEffort?: string
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** `ctx.llm` (`LlmRuntime`). */
|
|
162
|
+
export interface LlmLike {
|
|
163
|
+
resolveModelInfo(provider: string, model: string, signal?: AbortSignal): Promise<ModelInfoLike>
|
|
164
|
+
resolveCallConfig(config: RouteLike, signal?: AbortSignal): Promise<unknown>
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** `ToolExecution`: the fields the pipeline listeners read. */
|
|
168
|
+
export interface ToolCallLike {
|
|
169
|
+
readonly callId: string
|
|
170
|
+
readonly name: string
|
|
171
|
+
readonly arguments: unknown
|
|
172
|
+
readonly agent?: AgentLike
|
|
173
|
+
readonly signal: AbortSignal
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** This plugin's delegation tools and the backend each starts children on. */
|
|
177
|
+
export const DELEGATE_TOOLS = {
|
|
178
|
+
delegate: 'spawn',
|
|
179
|
+
delegate_fork: 'fork',
|
|
180
|
+
} as const
|
|
181
|
+
|
|
182
|
+
/** A delegation tool name. */
|
|
183
|
+
export type DelegateTool = keyof typeof DELEGATE_TOOLS
|
|
184
|
+
|
|
185
|
+
/** The child's tool for asking its parent. */
|
|
186
|
+
export const REQUEST_TOOL = 'request_write_access'
|
|
187
|
+
/** The parent's tool for answering a request. */
|
|
188
|
+
export const ANSWER_TOOL = 'answer_write_request'
|
|
189
|
+
/** The parent's tool for granting without a request. */
|
|
190
|
+
export const GRANT_TOOL = 'grant_write_access'
|
|
191
|
+
/** The parent's tool for taking grants back. */
|
|
192
|
+
export const REVOKE_TOOL = 'revoke_write_access'
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* complete-subagent host plugin.
|
|
3
|
+
*
|
|
4
|
+
* Adds its own delegation tools beside the official ones: `delegate` (a fresh
|
|
5
|
+
* child) and `delegate_fork` (a child that inherits the conversation). The
|
|
6
|
+
* `cwd` a parent passes becomes the child's sub-workspace, `writable_paths`
|
|
7
|
+
* grants more up front, and the child may modify nothing else (besides temp
|
|
8
|
+
* directories). For anything else it asks with `request_write_access`; the
|
|
9
|
+
* parent answers with `answer_write_request`, and can grant or revoke at any
|
|
10
|
+
* time with `grant_write_access` / `revoke_write_access`. A parent may also
|
|
11
|
+
* pick the child's model and reasoning effort among those the user lists in
|
|
12
|
+
* the settings. Children run on the host's subagent runtime; the fence narrows
|
|
13
|
+
* the sandbox policy the host already enforces, and anything a confined agent
|
|
14
|
+
* starts by other means is confined the same way. A bundle that mounts the
|
|
15
|
+
* plugin for its own agent preset sets `presets`, and agents of other presets
|
|
16
|
+
* then see none of it; it may also set `childPreset`, the preset `delegate`
|
|
17
|
+
* children run, `ownModel: false`, which keeps those children on the listed
|
|
18
|
+
* models, and the list itself at run time through the `completeSubagent` service.
|
|
19
|
+
* @module complete-subagent
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { Context, Volatile } from '@deepseek-ai/cordis'
|
|
23
|
+
import z from '@deepseek-ai/schemastery'
|
|
24
|
+
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'
|
|
26
|
+
import type { ChildModel, ChildModelInput } from './shared/settings.ts'
|
|
27
|
+
import { withinCall } from './host/call-scope.ts'
|
|
28
|
+
import { canStart, createDelegateTool } from './host/delegate.ts'
|
|
29
|
+
import { isLegacyHost, ToolRestrictions } from './host/legacy.ts'
|
|
30
|
+
import { Fence, patchResolve } from './host/policy.ts'
|
|
31
|
+
import { childContext, pendingContext } from './host/prompt.ts'
|
|
32
|
+
import { RequestBroker } from './host/requests.ts'
|
|
33
|
+
import { presetScope } from './host/scope.ts'
|
|
34
|
+
import { BindingStore, defaultStoreDirectory } from './host/store.ts'
|
|
35
|
+
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'
|
|
37
|
+
import type {
|
|
38
|
+
AgentLike,
|
|
39
|
+
AgentPresetsLike,
|
|
40
|
+
AgentsLike,
|
|
41
|
+
DelegateTool,
|
|
42
|
+
LlmLike,
|
|
43
|
+
SandboxPolicyServiceLike,
|
|
44
|
+
SessionProjectionsLike,
|
|
45
|
+
SubagentsLike,
|
|
46
|
+
ToolCallLike,
|
|
47
|
+
WorkingDirectoryLike,
|
|
48
|
+
} from './host/types.ts'
|
|
49
|
+
|
|
50
|
+
export const name = PLUGIN_ID
|
|
51
|
+
export const inject = ['tools', 'subagents', 'sandboxPolicy', 'workingDirectory', 'systemPrompt']
|
|
52
|
+
|
|
53
|
+
export interface Config {
|
|
54
|
+
/** Seconds a parent has to answer a write-access request before it counts as denied. */
|
|
55
|
+
requestTimeoutSeconds: Volatile<number>
|
|
56
|
+
/** Models a parent may choose for a child, with the efforts each allows. */
|
|
57
|
+
models: Volatile<ChildModel[]>
|
|
58
|
+
/** Agent presets whose agents the plugin serves; empty serves every agent. */
|
|
59
|
+
presets: string[]
|
|
60
|
+
/** Agent preset `delegate` children run; empty keeps the parent's. */
|
|
61
|
+
childPreset: string
|
|
62
|
+
/** False keeps `delegate` children off the parent's own model when models are listed. */
|
|
63
|
+
ownModel: boolean
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** `ctx.completeSubagent`: what a bundle that mounts this plugin can change at run time. */
|
|
67
|
+
export interface CompleteSubagentService {
|
|
68
|
+
/**
|
|
69
|
+
* Replace the `models` setting until the row reloads, so call it whenever the
|
|
70
|
+
* service appears. Does nothing on a copy without `childPreset`: that is a
|
|
71
|
+
* standalone install, whose list belongs to the user's normal sessions.
|
|
72
|
+
* @param models - the models a parent may choose for a child, in order.
|
|
73
|
+
*/
|
|
74
|
+
setChildModels(models: readonly ChildModelInput[]): void
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const Config: z<Config> = z.object({
|
|
78
|
+
requestTimeoutSeconds: z.natural().min(MIN_TIMEOUT_SECONDS).default(DEFAULT_TIMEOUT_SECONDS)
|
|
79
|
+
.description('Seconds a parent agent has to answer a write-access request before it counts as denied').volatile(),
|
|
80
|
+
models: z.array(z.object({
|
|
81
|
+
provider: z.string().min(1).required(),
|
|
82
|
+
model: z.string().min(1).required(),
|
|
83
|
+
reasoningEfforts: z.array(z.string().min(1)).default([]),
|
|
84
|
+
})).default([])
|
|
85
|
+
.description('Models a parent may choose for a child; an empty effort list allows every effort the model supports').volatile(),
|
|
86
|
+
presets: z.array(z.string().min(1)).default([])
|
|
87
|
+
.description('Agent presets whose agents get the tools and prompt texts; empty means every preset'),
|
|
88
|
+
childPreset: z.string().default('')
|
|
89
|
+
.description('Agent preset that children started with delegate run; empty keeps the parent\'s. delegate_fork children always keep it'),
|
|
90
|
+
ownModel: z.boolean().default(true)
|
|
91
|
+
.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'),
|
|
92
|
+
}) as unknown as z<Config>
|
|
93
|
+
|
|
94
|
+
/** `AssembleContext` fields the prompt providers read. */
|
|
95
|
+
interface AssemblyLike {
|
|
96
|
+
readonly agent?: AgentLike
|
|
97
|
+
readonly scope?: object
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The services this plugin reads, beyond what cordis types. */
|
|
101
|
+
interface HostContext {
|
|
102
|
+
readonly tools: {
|
|
103
|
+
register(definition: ToolDefinition, options?: { when?: (context: { agent: AgentLike }) => boolean }): () => void
|
|
104
|
+
get(name: string, scope?: object): ToolDefinition | undefined
|
|
105
|
+
/** Checks each `when` before a step; missing from a host that ignores `when`. */
|
|
106
|
+
readonly resolve?: unknown
|
|
107
|
+
}
|
|
108
|
+
readonly subagents: SubagentsLike
|
|
109
|
+
readonly sandboxPolicy: SandboxPolicyServiceLike
|
|
110
|
+
readonly workingDirectory: WorkingDirectoryLike
|
|
111
|
+
readonly systemPrompt: {
|
|
112
|
+
context(context: { name: string; order: number; text: (context: AssemblyLike) => string; interpolate?: boolean; required?: boolean }): () => void
|
|
113
|
+
getContextOrder(name: 'SUBAGENT_DELEGATION'): number
|
|
114
|
+
}
|
|
115
|
+
get(name: 'llm'): LlmLike | undefined
|
|
116
|
+
get(name: 'agentPresets'): AgentPresetsLike | undefined
|
|
117
|
+
get(name: 'sessionProjections'): SessionProjectionsLike | undefined
|
|
118
|
+
get(name: 'agents'): AgentsLike | undefined
|
|
119
|
+
provide(name: 'completeSubagent', value: CompleteSubagentService): () => void
|
|
120
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- host events are typed by packages this plugin does not import.
|
|
121
|
+
on(name: string, listener: (...args: any[]) => unknown): () => void
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The agent an assembly is for: `agent` when the host sets it, else the agent scope key. */
|
|
125
|
+
function agentOf(context: AssemblyLike): AgentLike | undefined {
|
|
126
|
+
if (context.agent !== undefined) return context.agent
|
|
127
|
+
const scope = context.scope as Partial<AgentLike> | undefined
|
|
128
|
+
return typeof scope?.id === 'string' && scope.session !== undefined ? scope as AgentLike : undefined
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** A live config value, or undefined when `apply` was called without the loader. */
|
|
132
|
+
function live(value: { get(): unknown } | undefined): unknown {
|
|
133
|
+
return value?.get()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Install the fence, the delegation tools, the write-access tools, and the
|
|
138
|
+
* guidance.
|
|
139
|
+
* @param ctx - the plugin context; the services in {@link inject} are present.
|
|
140
|
+
* @param config - loaded settings.
|
|
141
|
+
*/
|
|
142
|
+
export function apply(ctx: Context, config: Config): void {
|
|
143
|
+
const host = ctx as unknown as HostContext
|
|
144
|
+
const timeoutMs = (): number => readTimeout(live(config?.requestTimeoutSeconds)) * 1000
|
|
145
|
+
const childPreset = readChildPreset(config?.childPreset)
|
|
146
|
+
let bundleModels: ChildModel[] | undefined
|
|
147
|
+
const models = (): ChildModel[] => bundleModels ?? readModels(live(config?.models))
|
|
148
|
+
const serves = presetScope(readPresets(config?.presets), {
|
|
149
|
+
agentPresets: () => host.get('agentPresets'),
|
|
150
|
+
sessionProjections: () => host.get('sessionProjections'),
|
|
151
|
+
})
|
|
152
|
+
const legacy = isLegacyHost(host.tools)
|
|
153
|
+
const store = new BindingStore(defaultStoreDirectory())
|
|
154
|
+
const broker = new RequestBroker()
|
|
155
|
+
const resolvePolicy = (session: AgentLike['session']) => host.sandboxPolicy.resolve({ session })
|
|
156
|
+
const warn = (message: string): void => {
|
|
157
|
+
try {
|
|
158
|
+
ctx.logger.warn(message)
|
|
159
|
+
} catch {
|
|
160
|
+
// A host call must not fail because its warning could not be logged.
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// The host calls these for every session of every preset, and a throw would
|
|
164
|
+
// fail a step or stop an agent from starting. A plugin failure stays here.
|
|
165
|
+
const guarded = <T>(label: string, fallback: T, run: () => T): T => {
|
|
166
|
+
try {
|
|
167
|
+
return run()
|
|
168
|
+
} catch (error) {
|
|
169
|
+
warn(`${PLUGIN_ID}: ${label} failed: ${String(error)}`)
|
|
170
|
+
return fallback
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const fence = new Fence({ store, workingDirectory: host.workingDirectory, warn })
|
|
174
|
+
|
|
175
|
+
let restore: () => void
|
|
176
|
+
try {
|
|
177
|
+
restore = patchResolve(host.sandboxPolicy, (policy, request) => fence.narrow(policy, request))
|
|
178
|
+
} catch (error) {
|
|
179
|
+
warn(`${PLUGIN_ID} is disabled: ${String(error)}`)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
ctx.effect(() => restore)
|
|
183
|
+
// This listener adds no failure of its own; what `next` throws is the host's pipeline and stays its to handle.
|
|
184
|
+
host.on('tools/execute', (call: ToolCallLike, next: () => Promise<unknown>) => withinCall(call, next))
|
|
185
|
+
|
|
186
|
+
// A child that a confined agent starts by other means (the official tools,
|
|
187
|
+
// a workflow) shares that agent's access instead of the whole project. Only
|
|
188
|
+
// a served parent passes it on; a child judged without its live parent is
|
|
189
|
+
// judged by its own preset, which it took from that parent. When the parent's
|
|
190
|
+
// preset cannot be read, the child is confined rather than freed.
|
|
191
|
+
host.on('agent/created', ({ agent }: { agent: AgentLike }) => {
|
|
192
|
+
guarded('storing an inherited binding', undefined, () => {
|
|
193
|
+
const { origin, parentSession } = agent.session.header
|
|
194
|
+
if (origin !== 'subagent' || parentSession === undefined) return
|
|
195
|
+
if (store.get(agent.id) !== undefined || store.get(parentSession) === undefined) return
|
|
196
|
+
if (!guarded('reading the parent preset', true, () => serves((host.get('agents')?.get(parentSession) ?? agent).session))) return
|
|
197
|
+
store.set({ version: 1, child: agent.id, parent: parentSession, workspace: null, grants: [], inherited: true })
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
const tools = Object.keys(DELEGATE_TOOLS) as DelegateTool[]
|
|
202
|
+
const canDelegate = (agent: AgentLike): boolean => serves(agent.session)
|
|
203
|
+
&& tools.some(tool => host.tools.get(tool, agent) !== undefined && canStart(host.subagents, tool, agent))
|
|
204
|
+
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),
|
|
207
|
+
[REQUEST_TOOL]: (agent: AgentLike): boolean => {
|
|
208
|
+
const binding = store.get(agent.id)
|
|
209
|
+
return binding !== undefined && binding.inherited !== true && serves(agent.session)
|
|
210
|
+
},
|
|
211
|
+
[ANSWER_TOOL]: (agent: AgentLike): boolean => canDelegate(agent) || (broker.pendingFor(agent.id).length > 0 && serves(agent.session)),
|
|
212
|
+
[GRANT_TOOL]: canDelegate,
|
|
213
|
+
[REVOKE_TOOL]: canDelegate,
|
|
214
|
+
}
|
|
215
|
+
type ToolName = keyof typeof conditions
|
|
216
|
+
const visible = (tool: ToolName, agent: AgentLike): boolean => guarded(`the visibility check of ${tool}`, false, () => conditions[tool](agent))
|
|
217
|
+
|
|
218
|
+
// The schema lists the configured models, so settings changes rebuild the tools.
|
|
219
|
+
const deps = {
|
|
220
|
+
store,
|
|
221
|
+
fence,
|
|
222
|
+
broker,
|
|
223
|
+
subagents: host.subagents,
|
|
224
|
+
workingDirectory: host.workingDirectory,
|
|
225
|
+
resolvePolicy,
|
|
226
|
+
timeoutMs,
|
|
227
|
+
llm: () => host.get('llm'),
|
|
228
|
+
childPreset,
|
|
229
|
+
ownModel: readOwnModel(config?.ownModel),
|
|
230
|
+
}
|
|
231
|
+
let mounted: { key: string; dispose: () => void } | undefined
|
|
232
|
+
const mount = (): void => {
|
|
233
|
+
const list = models()
|
|
234
|
+
const key = JSON.stringify(list)
|
|
235
|
+
if (mounted?.key === key) return
|
|
236
|
+
// Build first, so a definition that fails to build leaves the old tools in place.
|
|
237
|
+
const definitions = tools.map(tool => createDelegateTool(tool, list, deps))
|
|
238
|
+
mounted?.dispose()
|
|
239
|
+
const disposers: (() => void)[] = []
|
|
240
|
+
mounted = { key, dispose: () => { for (const dispose of disposers) dispose() } }
|
|
241
|
+
definitions.forEach((definition, index) => {
|
|
242
|
+
const tool = tools[index] as DelegateTool
|
|
243
|
+
disposers.push(host.tools.register(definition, { when: ({ agent }) => visible(tool, agent) }))
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
mount()
|
|
247
|
+
let disposed = false
|
|
248
|
+
ctx.effect(() => () => {
|
|
249
|
+
disposed = true
|
|
250
|
+
mounted?.dispose()
|
|
251
|
+
mounted = undefined
|
|
252
|
+
})
|
|
253
|
+
host.on('loader/volatile-update', () => {
|
|
254
|
+
guarded('rebuilding the delegation tools', undefined, mount)
|
|
255
|
+
})
|
|
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
|
+
|
|
270
|
+
// This host drops `when`, so the same conditions become per-agent filters. A
|
|
271
|
+
// step reads its tools before `agent/pre-step`, so a change seen there takes
|
|
272
|
+
// effect on the agent's next step; a new agent is filtered before its first.
|
|
273
|
+
if (legacy) {
|
|
274
|
+
const restrictions = new ToolRestrictions(Object.keys(conditions), (tool, agent) => visible(tool as ToolName, agent))
|
|
275
|
+
const sync = (agent: AgentLike): void => {
|
|
276
|
+
guarded('updating the tool filter', undefined, () => restrictions.sync(agent))
|
|
277
|
+
}
|
|
278
|
+
ctx.effect(() => () => {
|
|
279
|
+
guarded('lifting the tool filters', undefined, () => restrictions.dispose())
|
|
280
|
+
})
|
|
281
|
+
host.on('agent/created', ({ agent }: { agent: AgentLike }) => sync(agent))
|
|
282
|
+
host.on('agent/pre-step', ({ agent }: { agent: AgentLike }, next: () => Promise<unknown>) => {
|
|
283
|
+
sync(agent)
|
|
284
|
+
return next()
|
|
285
|
+
})
|
|
286
|
+
host.on('agent-preset/selected', (sessionId: string) => {
|
|
287
|
+
const agent = guarded('finding the agent of a preset selection', undefined, () => host.get('agents')?.get(sessionId))
|
|
288
|
+
if (agent !== undefined) sync(agent)
|
|
289
|
+
})
|
|
290
|
+
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
|
+
}
|
|
294
|
+
|
|
295
|
+
ctx.effect(() => host.systemPrompt.context({
|
|
296
|
+
name: `${name}:child`,
|
|
297
|
+
order: host.systemPrompt.getContextOrder('SUBAGENT_DELEGATION') + 1,
|
|
298
|
+
interpolate: false,
|
|
299
|
+
required: true,
|
|
300
|
+
text: context => guarded('the child context', '', () => {
|
|
301
|
+
const agent = agentOf(context)
|
|
302
|
+
const binding = agent === undefined || !serves(agent.session) ? undefined : fence.bindingOf(agent.session)
|
|
303
|
+
return binding === undefined ? '' : childContext(binding, !legacy)
|
|
304
|
+
}),
|
|
305
|
+
}))
|
|
306
|
+
ctx.effect(() => host.systemPrompt.context({
|
|
307
|
+
name: `${name}:requests`,
|
|
308
|
+
order: host.systemPrompt.getContextOrder('SUBAGENT_DELEGATION') + 2,
|
|
309
|
+
interpolate: false,
|
|
310
|
+
required: true,
|
|
311
|
+
text: context => guarded('the requests context', '', () => {
|
|
312
|
+
const agent = agentOf(context)
|
|
313
|
+
return agent === undefined || !serves(agent.session) ? '' : pendingContext(broker.pendingFor(agent.id))
|
|
314
|
+
}),
|
|
315
|
+
}))
|
|
316
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
## 1. Role
|
|
4
|
+
|
|
5
|
+
What both halves agree on: the package name, the plugin id (the bundle row id and settings form id, kept from the original spelling), and the shape of the live settings: the models a parent may choose for a child with their allowed reasoning efforts plus the write-access answer time, the agent presets the plugin serves, the preset `delegate` children run, and whether they may run the parent's own model. The host half reads these values when it builds the delegation tools and checks a call; the browser half reads and writes the models and the answer time on the settings page, and never the other three fields, which a bundle sets in its patch.
|
|
6
|
+
|
|
7
|
+
## 2. Constraints
|
|
8
|
+
|
|
9
|
+
- Pure data and functions with no imports, so it compiles into both halves.
|
|
10
|
+
- The readers accept anything and return clean values: malformed entries, repeated models, repeated efforts, and repeated or empty presets are dropped, a blank child preset reads as none, `ownModel` is off only when it is literally false, and an invalid answer time falls back to the default.
|
|
11
|
+
|
|
12
|
+
## 3. Contents
|
|
13
|
+
|
|
14
|
+
- `settings.ts` — the plugin id, the child model shapes, the `provider/model` key, and the readers for the five settings fields.
|
|
15
|
+
|
|
16
|
+
## 4. Progress
|
|
17
|
+
|
|
18
|
+
- Done: used by the host entry, the model choice, and the settings page.
|
|
19
|
+
- Open: nothing.
|
|
20
|
+
- Goal later: nothing planned.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The settings both halves read: which models a parent may choose for a child,
|
|
3
|
+
* which reasoning efforts each allows, and how long a parent has to answer.
|
|
4
|
+
* The host half declares them as live config fields; the browser half edits
|
|
5
|
+
* them on the Plugins page. Only the host half reads `presets`, `childPreset`,
|
|
6
|
+
* and `ownModel`, which a bundle that mounts this plugin sets in its patch.
|
|
7
|
+
* @module complete-subagent/shared/settings
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The npm package name: the browser bundle's registration id and the Plugins page key. */
|
|
11
|
+
export const PACKAGE_NAME = '@starsharryzhu/complete-subagent'
|
|
12
|
+
|
|
13
|
+
/** Bundle row id, settings form id, and plugin name; it keeps the original spelling. */
|
|
14
|
+
export const PLUGIN_ID = 'complete-subagent'
|
|
15
|
+
|
|
16
|
+
/** One model a parent may choose for a child. */
|
|
17
|
+
export interface ChildModel {
|
|
18
|
+
/** Registered LLM provider id. */
|
|
19
|
+
readonly provider: string
|
|
20
|
+
/** Exact model id within that provider. */
|
|
21
|
+
readonly model: string
|
|
22
|
+
/** Efforts a parent may choose for it; empty allows every effort the model advertises. */
|
|
23
|
+
readonly reasoningEfforts: readonly string[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A model as a bundle hands it to the plugin at run time: an omitted effort list allows every effort. */
|
|
27
|
+
export type ChildModelInput = Omit<ChildModel, 'reasoningEfforts'> & { readonly reasoningEfforts?: readonly string[] }
|
|
28
|
+
|
|
29
|
+
/** Seconds a parent has to answer a write-access request, unless the settings say otherwise. */
|
|
30
|
+
export const DEFAULT_TIMEOUT_SECONDS = 600
|
|
31
|
+
|
|
32
|
+
/** The shortest answer time the settings accept. */
|
|
33
|
+
export const MIN_TIMEOUT_SECONDS = 10
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The id a parent passes as `model`.
|
|
37
|
+
* @param route - provider and model.
|
|
38
|
+
* @returns `provider/model`.
|
|
39
|
+
*/
|
|
40
|
+
export function modelKey(route: { readonly provider: string; readonly model: string }): string {
|
|
41
|
+
return `${route.provider}/${route.model}`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Read a stored model list, dropping malformed entries, repeated models, and
|
|
46
|
+
* repeated efforts.
|
|
47
|
+
* @param value - the stored `models` field.
|
|
48
|
+
* @returns the usable entries in their stored order.
|
|
49
|
+
*/
|
|
50
|
+
export function readModels(value: unknown): ChildModel[] {
|
|
51
|
+
if (!Array.isArray(value)) return []
|
|
52
|
+
const seen = new Set<string>()
|
|
53
|
+
const models: ChildModel[] = []
|
|
54
|
+
for (const item of value as unknown[]) {
|
|
55
|
+
if (typeof item !== 'object' || item === null) continue
|
|
56
|
+
const { provider, model, reasoningEfforts } = item as Record<string, unknown>
|
|
57
|
+
if (typeof provider !== 'string' || provider === '' || typeof model !== 'string' || model === '') continue
|
|
58
|
+
const key = modelKey({ provider, model })
|
|
59
|
+
if (seen.has(key)) continue
|
|
60
|
+
seen.add(key)
|
|
61
|
+
const efforts = Array.isArray(reasoningEfforts)
|
|
62
|
+
? [...new Set((reasoningEfforts as unknown[]).filter((effort): effort is string => typeof effort === 'string' && effort !== ''))]
|
|
63
|
+
: []
|
|
64
|
+
models.push({ provider, model, reasoningEfforts: efforts })
|
|
65
|
+
}
|
|
66
|
+
return models
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Read a stored preset list, dropping anything but non-empty strings and repeats.
|
|
71
|
+
* @param value - the stored `presets` field.
|
|
72
|
+
* @returns the preset ids in their stored order; empty means every preset.
|
|
73
|
+
*/
|
|
74
|
+
export function readPresets(value: unknown): string[] {
|
|
75
|
+
if (!Array.isArray(value)) return []
|
|
76
|
+
return [...new Set((value as unknown[]).filter((preset): preset is string => typeof preset === 'string' && preset !== ''))]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Read a stored child preset.
|
|
81
|
+
* @param value - the stored `childPreset` field.
|
|
82
|
+
* @returns the preset id, or undefined when none is set.
|
|
83
|
+
*/
|
|
84
|
+
export function readChildPreset(value: unknown): string | undefined {
|
|
85
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Read the stored `ownModel` flag.
|
|
90
|
+
* @param value - the stored field.
|
|
91
|
+
* @returns false only when the field says so.
|
|
92
|
+
*/
|
|
93
|
+
export function readOwnModel(value: unknown): boolean {
|
|
94
|
+
return value !== false
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Read a stored answer time.
|
|
99
|
+
* @param value - the stored `requestTimeoutSeconds` field.
|
|
100
|
+
* @returns whole seconds, at least {@link MIN_TIMEOUT_SECONDS}.
|
|
101
|
+
*/
|
|
102
|
+
export function readTimeout(value: unknown): number {
|
|
103
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= MIN_TIMEOUT_SECONDS ? value : DEFAULT_TIMEOUT_SECONDS
|
|
104
|
+
}
|