@dsh-plugin/dsh-dynamic-tools 0.0.1 → 0.1.0-dev.43582542989
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 +165 -165
- package/README.md +151 -111
- package/docs/DETAIL.md +1 -1
- package/docs/preview.svg +25 -25
- package/icon.svg +7 -7
- package/lib/catalog.d.ts +11 -0
- package/lib/catalog.js +21 -0
- package/lib/disclose.d.ts +9 -6
- package/lib/disclose.js +51 -16
- package/lib/disclosure.d.ts +34 -0
- package/lib/disclosure.js +50 -3
- package/lib/executor.d.ts +53 -3
- package/lib/executor.js +87 -13
- package/lib/fold.d.ts +2 -2
- package/lib/fold.js +32 -21
- package/lib/index.d.ts +6 -0
- package/lib/index.js +26 -2
- package/lib/install.js +1 -0
- package/lib/render.d.ts +10 -2
- package/lib/render.js +20 -7
- package/lib/strategy.d.ts +52 -0
- package/lib/strategy.js +63 -0
- package/lib/tool-load.d.ts +16 -5
- package/lib/tool-load.js +29 -14
- package/locale/en.json +7 -7
- package/locale/zh.json +7 -7
- package/package.json +75 -75
- package/src/catalog.ts +112 -88
- package/src/disclose.ts +199 -151
- package/src/disclosure.ts +212 -147
- package/src/executor.ts +234 -145
- package/src/fold.ts +114 -104
- package/src/index.ts +209 -178
- package/src/install.ts +92 -91
- package/src/render.ts +129 -115
- package/src/source.ts +20 -20
- package/src/strategy.ts +79 -0
- package/src/tool-load.ts +126 -105
package/src/index.ts
CHANGED
|
@@ -1,178 +1,209 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Freeze the tool declaration head and disclose later tools at the conversation
|
|
3
|
-
* tail.
|
|
4
|
-
*
|
|
5
|
-
* A model request whose tool list changes forces the agent loop to resend the
|
|
6
|
-
* complete system prompt and declarations, which discards the provider's cached
|
|
7
|
-
* prefix. This plugin keeps the head constant: the first step's declaration set
|
|
8
|
-
* is captured, later admissions are delivered as recorded user messages, and a
|
|
9
|
-
* call to a tool the step does not admit fails through an executable denial
|
|
10
|
-
* instead of running.
|
|
11
|
-
*
|
|
12
|
-
* @module dsh-dynamic-tools
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import z from '@deepseek-ai/schemastery'
|
|
16
|
-
import type { Context } from '@deepseek-ai/cordis'
|
|
17
|
-
import type { Agent } from '@deepseek-ai/dsh-agent'
|
|
18
|
-
import type { ClassificationRules } from './catalog.ts'
|
|
19
|
-
import { registerDisclosure, type DiscloseOptions } from './disclose.ts'
|
|
20
|
-
import type { StableCatalogExecutor } from './executor.ts'
|
|
21
|
-
import { installAgent, type InstallOptions } from './install.ts'
|
|
22
|
-
import { SOURCE_KIND } from './source.ts'
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
names
|
|
40
|
-
|
|
41
|
-
prefixes
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Freeze the tool declaration head and disclose later tools at the conversation
|
|
3
|
+
* tail.
|
|
4
|
+
*
|
|
5
|
+
* A model request whose tool list changes forces the agent loop to resend the
|
|
6
|
+
* complete system prompt and declarations, which discards the provider's cached
|
|
7
|
+
* prefix. This plugin keeps the head constant: the first step's declaration set
|
|
8
|
+
* is captured, later admissions are delivered as recorded user messages, and a
|
|
9
|
+
* call to a tool the step does not admit fails through an executable denial
|
|
10
|
+
* instead of running.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-dynamic-tools
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import z from '@deepseek-ai/schemastery'
|
|
16
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
17
|
+
import type { Agent } from '@deepseek-ai/dsh-agent'
|
|
18
|
+
import type { ClassificationRules } from './catalog.ts'
|
|
19
|
+
import { registerDisclosure, type DiscloseOptions } from './disclose.ts'
|
|
20
|
+
import type { StableCatalogExecutor } from './executor.ts'
|
|
21
|
+
import { installAgent, type InstallOptions } from './install.ts'
|
|
22
|
+
import { SOURCE_KIND } from './source.ts'
|
|
23
|
+
import { DEFAULT_DECLARE_MODELS, type StrategyRules, type ToolStrategy } from './strategy.ts'
|
|
24
|
+
import { createToolLoad, TOOL_LOAD_NAME } from './tool-load.ts'
|
|
25
|
+
|
|
26
|
+
/** Cordis plugin name. */
|
|
27
|
+
export const name = '@dsh-plugin/dsh-dynamic-tools'
|
|
28
|
+
|
|
29
|
+
/** Services the plugin registers into. */
|
|
30
|
+
export const inject = ['tools', 'systemPrompt']
|
|
31
|
+
|
|
32
|
+
/** Tool-selection and disclosure settings. */
|
|
33
|
+
export interface Config {
|
|
34
|
+
/** Install for agents that already exist when this plugin loads. */
|
|
35
|
+
installExistingAgents: boolean
|
|
36
|
+
/** Preset ids left untouched, so their own executor keeps the declaration path. */
|
|
37
|
+
skipPresets: string[]
|
|
38
|
+
dynamic: {
|
|
39
|
+
/** Exact tool names disclosed at the conversation tail. */
|
|
40
|
+
names: string[]
|
|
41
|
+
/** Tool-name prefixes disclosed at the conversation tail. */
|
|
42
|
+
prefixes: string[]
|
|
43
|
+
/** Exact names forced into the frozen head. */
|
|
44
|
+
inlineNames: string[]
|
|
45
|
+
}
|
|
46
|
+
disclosure: {
|
|
47
|
+
/** Attach definitions to the disclosure, or announce names for `tool_load`. */
|
|
48
|
+
mode: 'schema' | 'names'
|
|
49
|
+
/** Require `tool_load` before a dynamic tool may run. */
|
|
50
|
+
requireLoad: boolean
|
|
51
|
+
/** Maximum tools announced in one message. */
|
|
52
|
+
maxToolsPerMessage: number
|
|
53
|
+
/** Maximum bytes of one attached definition. */
|
|
54
|
+
maxSchemaBytes: number
|
|
55
|
+
/** Maximum bytes of one disclosure message. */
|
|
56
|
+
maxMessageBytes: number
|
|
57
|
+
/** Announce tools that stop being available. */
|
|
58
|
+
announceRemovals: boolean
|
|
59
|
+
}
|
|
60
|
+
declareOnLoad: {
|
|
61
|
+
/** Model-id patterns whose routes declare loaded tools instead of reading the tail. */
|
|
62
|
+
models: string[]
|
|
63
|
+
/** Provider-route patterns restricted to that handling; empty matches every provider. */
|
|
64
|
+
providers: string[]
|
|
65
|
+
}
|
|
66
|
+
/** Model-facing reason reported when a declared tool is not admitted. */
|
|
67
|
+
unavailableReasons: Record<string, string>
|
|
68
|
+
/** Concurrency used when the loop supplies no limit. */
|
|
69
|
+
maxParallelCalls: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Schemastery validation; invalid values fail at load. */
|
|
73
|
+
export const Config: z<Config> = z.object({
|
|
74
|
+
installExistingAgents: z.boolean().default(true)
|
|
75
|
+
.description('Install for agents that already exist when this plugin loads.'),
|
|
76
|
+
skipPresets: z.array(z.string()).default(['ptc'])
|
|
77
|
+
.description('Preset ids whose own tool executor keeps the declaration path.'),
|
|
78
|
+
dynamic: z.object({
|
|
79
|
+
names: z.array(z.string()).default(['exit_plan_mode'])
|
|
80
|
+
.description('Exact tool names disclosed at the conversation tail.'),
|
|
81
|
+
prefixes: z.array(z.string()).default([])
|
|
82
|
+
.description('Tool-name prefixes disclosed at the conversation tail.'),
|
|
83
|
+
inlineNames: z.array(z.string()).default([])
|
|
84
|
+
.description('Exact names forced into the frozen head.'),
|
|
85
|
+
}).default({}),
|
|
86
|
+
disclosure: z.object({
|
|
87
|
+
mode: z.union(['schema', 'names']).default('schema')
|
|
88
|
+
.description('Attach definitions to the disclosure, or announce names for tool_load.'),
|
|
89
|
+
requireLoad: z.boolean().default(false)
|
|
90
|
+
.description('Require tool_load before a dynamic tool may run.'),
|
|
91
|
+
maxToolsPerMessage: z.number().min(1).default(8)
|
|
92
|
+
.description('Maximum tools announced in one message.'),
|
|
93
|
+
maxSchemaBytes: z.number().min(1).default(6000)
|
|
94
|
+
.description('Maximum bytes of one attached definition.'),
|
|
95
|
+
maxMessageBytes: z.number().min(1).default(20000)
|
|
96
|
+
.description('Maximum bytes of one disclosure message.'),
|
|
97
|
+
announceRemovals: z.boolean().default(true)
|
|
98
|
+
.description('Announce tools that stop being available.'),
|
|
99
|
+
}).default({}),
|
|
100
|
+
declareOnLoad: z.object({
|
|
101
|
+
models: z.array(z.string()).default([...DEFAULT_DECLARE_MODELS])
|
|
102
|
+
.description('Model-id patterns whose routes call only declared tools; dynamic tools are loaded on demand there.'),
|
|
103
|
+
providers: z.array(z.string()).default([])
|
|
104
|
+
.description('Provider-route patterns restricted to that handling; empty matches every provider.'),
|
|
105
|
+
}).default({}),
|
|
106
|
+
unavailableReasons: z.dict(z.string()).default({
|
|
107
|
+
exit_plan_mode: 'Plan mode is not active in this session. Ask the user to switch to plan mode, then call it again.',
|
|
108
|
+
}).description('Model-facing reason reported when a declared tool is not admitted.'),
|
|
109
|
+
maxParallelCalls: z.number().min(1).default(10)
|
|
110
|
+
.description('Concurrency used when the loop supplies no limit.'),
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Compose validated settings into the two halves the plugin registers.
|
|
115
|
+
* @param config - validated plugin configuration.
|
|
116
|
+
* @returns classification, execution, and disclosure settings.
|
|
117
|
+
*/
|
|
118
|
+
function resolveSettings(config: Config): {
|
|
119
|
+
rules: ClassificationRules
|
|
120
|
+
strategyRules: StrategyRules
|
|
121
|
+
install: Omit<InstallOptions, 'executors' | 'reported'>
|
|
122
|
+
disclose: Omit<DiscloseOptions, 'strategy' | 'catalogNames' | 'admitted'>
|
|
123
|
+
} {
|
|
124
|
+
const strategyRules: StrategyRules = {
|
|
125
|
+
models: config.declareOnLoad.models,
|
|
126
|
+
providers: config.declareOnLoad.providers,
|
|
127
|
+
}
|
|
128
|
+
// A route that declares loaded tools needs the loader in its head, so a
|
|
129
|
+
// deployment cannot classify it onto the tail and lose the only path in.
|
|
130
|
+
const inlineNames = config.dynamic.inlineNames.includes(TOOL_LOAD_NAME)
|
|
131
|
+
? config.dynamic.inlineNames
|
|
132
|
+
: [...config.dynamic.inlineNames, TOOL_LOAD_NAME]
|
|
133
|
+
const rules: ClassificationRules = {
|
|
134
|
+
dynamicNames: config.dynamic.names,
|
|
135
|
+
dynamicPrefixes: config.dynamic.prefixes,
|
|
136
|
+
inlineNames,
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
rules,
|
|
140
|
+
strategyRules,
|
|
141
|
+
install: {
|
|
142
|
+
sourceKind: SOURCE_KIND,
|
|
143
|
+
rules,
|
|
144
|
+
executor: {
|
|
145
|
+
requireLoad: config.disclosure.requireLoad,
|
|
146
|
+
unavailableReasons: config.unavailableReasons,
|
|
147
|
+
maxParallelCalls: config.maxParallelCalls,
|
|
148
|
+
strategyRules,
|
|
149
|
+
loadToolName: TOOL_LOAD_NAME,
|
|
150
|
+
},
|
|
151
|
+
skipPresets: config.skipPresets,
|
|
152
|
+
},
|
|
153
|
+
disclose: {
|
|
154
|
+
sourceKind: SOURCE_KIND,
|
|
155
|
+
limits: {
|
|
156
|
+
mode: config.disclosure.mode,
|
|
157
|
+
maxToolsPerMessage: config.disclosure.maxToolsPerMessage,
|
|
158
|
+
maxSchemaBytes: config.disclosure.maxSchemaBytes,
|
|
159
|
+
maxMessageBytes: config.disclosure.maxMessageBytes,
|
|
160
|
+
},
|
|
161
|
+
announceRemovals: config.disclosure.announceRemovals,
|
|
162
|
+
},
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Register the load tool, the frozen catalog per agent, and tail disclosure.
|
|
168
|
+
* @param ctx - plugin context owning every registration.
|
|
169
|
+
* @param config - validated plugin configuration.
|
|
170
|
+
*/
|
|
171
|
+
export function apply(ctx: Context, config: Config): void {
|
|
172
|
+
const settings = resolveSettings(config)
|
|
173
|
+
const executors = new WeakMap<Agent, StableCatalogExecutor>()
|
|
174
|
+
const release = new WeakMap<Agent, () => void>()
|
|
175
|
+
const reported = new WeakSet<Agent>()
|
|
176
|
+
const installOptions: InstallOptions = { ...settings.install, executors, reported }
|
|
177
|
+
const strategyOf = (agent: Agent): ToolStrategy | undefined => executors.get(agent)?.planStrategy()
|
|
178
|
+
const declaredNamesOf = (agent: Agent): ReadonlySet<string> => executors.get(agent)?.declaredNames() ?? new Set()
|
|
179
|
+
|
|
180
|
+
ctx.effect(() => ctx.tools.register(createToolLoad(ctx, {
|
|
181
|
+
sourceKind: SOURCE_KIND,
|
|
182
|
+
limits: settings.disclose.limits,
|
|
183
|
+
strategy: strategyOf,
|
|
184
|
+
declaredNames: declaredNamesOf,
|
|
185
|
+
})), `dsh-dynamic-tools: register ${TOOL_LOAD_NAME}`)
|
|
186
|
+
|
|
187
|
+
ctx.on('agent/created', ({ agent }) => {
|
|
188
|
+
installAgent(ctx, agent, installOptions, release)
|
|
189
|
+
return undefined
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
// An agent's executor belongs to that agent: release it with the agent instead
|
|
193
|
+
// of retaining every closed session's executor until this plugin unloads.
|
|
194
|
+
ctx.on('agent/disposed', ({ agent }) => {
|
|
195
|
+
release.get(agent)?.()
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
if (config.installExistingAgents) {
|
|
199
|
+
const agents = ctx.get('agents')
|
|
200
|
+
for (const agent of agents?.list() ?? []) installAgent(ctx, agent, installOptions, release)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
registerDisclosure(ctx, {
|
|
204
|
+
...settings.disclose,
|
|
205
|
+
strategy: strategyOf,
|
|
206
|
+
catalogNames: declaredNamesOf,
|
|
207
|
+
admitted: agent => executors.get(agent)?.admittedDefinitions(),
|
|
208
|
+
})
|
|
209
|
+
}
|
package/src/install.ts
CHANGED
|
@@ -1,91 +1,92 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Per-Agent installation of the frozen-catalog executor.
|
|
3
|
-
*
|
|
4
|
-
* Every installation is one plugin-owned effect, so unload releases exactly the
|
|
5
|
-
* registrations this plugin made, and an agent's disposal releases its executor
|
|
6
|
-
* immediately instead of retaining it until the plugin unloads. A scope that
|
|
7
|
-
* already selected another executor keeps it: this plugin then stays out of the
|
|
8
|
-
* declaration path instead of replacing that selection.
|
|
9
|
-
*
|
|
10
|
-
* @module dsh-dynamic-tools/install
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { Context } from '@deepseek-ai/cordis'
|
|
14
|
-
import type { Agent } from '@deepseek-ai/dsh-agent'
|
|
15
|
-
import type { ClassificationRules } from './catalog.ts'
|
|
16
|
-
import { StableCatalogExecutor, type ExecutorOptions } from './executor.ts'
|
|
17
|
-
import { currentState } from './disclose.ts'
|
|
18
|
-
import type { SourceKind } from './source.ts'
|
|
19
|
-
|
|
20
|
-
/** Settings shared by every installed agent. */
|
|
21
|
-
export interface InstallOptions {
|
|
22
|
-
/** Message source kind owned by this plugin. */
|
|
23
|
-
readonly sourceKind: SourceKind
|
|
24
|
-
/** Classification rules for the frozen head. */
|
|
25
|
-
readonly rules: ClassificationRules
|
|
26
|
-
/** Execution settings. */
|
|
27
|
-
readonly executor: ExecutorOptions
|
|
28
|
-
/** Preset ids this plugin leaves alone. */
|
|
29
|
-
readonly skipPresets: readonly string[]
|
|
30
|
-
/** Executors by agent, shared with the disclosure listener. */
|
|
31
|
-
readonly executors: WeakMap<Agent, StableCatalogExecutor>
|
|
32
|
-
/** Agents already reported, so each diagnostic appears once. */
|
|
33
|
-
readonly reported: WeakSet<Agent>
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Read the preset id an agent runs under, when a registry is composed. */
|
|
37
|
-
function presetOf(ctx: Context, agent: Agent): string | undefined {
|
|
38
|
-
const registry = ctx.get('agentPresets')
|
|
39
|
-
if (registry === undefined) return undefined
|
|
40
|
-
return registry.presetForSession(agent.session) ?? undefined
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Install the frozen-catalog executor for one agent.
|
|
45
|
-
* @param ctx - plugin context that owns the registration.
|
|
46
|
-
* @param agent - agent whose scope receives the executor.
|
|
47
|
-
* @param options - installation settings.
|
|
48
|
-
* @param release - disposer registry, so agent disposal can release the executor.
|
|
49
|
-
*/
|
|
50
|
-
export function installAgent(
|
|
51
|
-
ctx: Context,
|
|
52
|
-
agent: Agent,
|
|
53
|
-
options: InstallOptions,
|
|
54
|
-
release: WeakMap<Agent, () => void>,
|
|
55
|
-
): void {
|
|
56
|
-
if (options.executors.has(agent)) return
|
|
57
|
-
const preset = presetOf(ctx, agent)
|
|
58
|
-
if (preset !== undefined && options.skipPresets.includes(preset)) {
|
|
59
|
-
if (!options.reported.has(agent)) {
|
|
60
|
-
options.reported.add(agent)
|
|
61
|
-
ctx.logger.info('dsh-dynamic-tools: leaving preset "%s" alone; tool declarations stay as composed', preset)
|
|
62
|
-
}
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
const holder = {
|
|
66
|
-
disclosed: () => currentState(agent, options.sourceKind).disclosed,
|
|
67
|
-
loaded: () => currentState(agent, options.sourceKind).loaded,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
released
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
options.reported.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Per-Agent installation of the frozen-catalog executor.
|
|
3
|
+
*
|
|
4
|
+
* Every installation is one plugin-owned effect, so unload releases exactly the
|
|
5
|
+
* registrations this plugin made, and an agent's disposal releases its executor
|
|
6
|
+
* immediately instead of retaining it until the plugin unloads. A scope that
|
|
7
|
+
* already selected another executor keeps it: this plugin then stays out of the
|
|
8
|
+
* declaration path instead of replacing that selection.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-dynamic-tools/install
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
14
|
+
import type { Agent } from '@deepseek-ai/dsh-agent'
|
|
15
|
+
import type { ClassificationRules } from './catalog.ts'
|
|
16
|
+
import { StableCatalogExecutor, type ExecutorOptions } from './executor.ts'
|
|
17
|
+
import { currentState } from './disclose.ts'
|
|
18
|
+
import type { SourceKind } from './source.ts'
|
|
19
|
+
|
|
20
|
+
/** Settings shared by every installed agent. */
|
|
21
|
+
export interface InstallOptions {
|
|
22
|
+
/** Message source kind owned by this plugin. */
|
|
23
|
+
readonly sourceKind: SourceKind
|
|
24
|
+
/** Classification rules for the frozen head. */
|
|
25
|
+
readonly rules: ClassificationRules
|
|
26
|
+
/** Execution settings. */
|
|
27
|
+
readonly executor: ExecutorOptions
|
|
28
|
+
/** Preset ids this plugin leaves alone. */
|
|
29
|
+
readonly skipPresets: readonly string[]
|
|
30
|
+
/** Executors by agent, shared with the disclosure listener. */
|
|
31
|
+
readonly executors: WeakMap<Agent, StableCatalogExecutor>
|
|
32
|
+
/** Agents already reported, so each diagnostic appears once. */
|
|
33
|
+
readonly reported: WeakSet<Agent>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Read the preset id an agent runs under, when a registry is composed. */
|
|
37
|
+
function presetOf(ctx: Context, agent: Agent): string | undefined {
|
|
38
|
+
const registry = ctx.get('agentPresets')
|
|
39
|
+
if (registry === undefined) return undefined
|
|
40
|
+
return registry.presetForSession(agent.session) ?? undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Install the frozen-catalog executor for one agent.
|
|
45
|
+
* @param ctx - plugin context that owns the registration.
|
|
46
|
+
* @param agent - agent whose scope receives the executor.
|
|
47
|
+
* @param options - installation settings.
|
|
48
|
+
* @param release - disposer registry, so agent disposal can release the executor.
|
|
49
|
+
*/
|
|
50
|
+
export function installAgent(
|
|
51
|
+
ctx: Context,
|
|
52
|
+
agent: Agent,
|
|
53
|
+
options: InstallOptions,
|
|
54
|
+
release: WeakMap<Agent, () => void>,
|
|
55
|
+
): void {
|
|
56
|
+
if (options.executors.has(agent)) return
|
|
57
|
+
const preset = presetOf(ctx, agent)
|
|
58
|
+
if (preset !== undefined && options.skipPresets.includes(preset)) {
|
|
59
|
+
if (!options.reported.has(agent)) {
|
|
60
|
+
options.reported.add(agent)
|
|
61
|
+
ctx.logger.info('dsh-dynamic-tools: leaving preset "%s" alone; tool declarations stay as composed', preset)
|
|
62
|
+
}
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
const holder = {
|
|
66
|
+
disclosed: () => currentState(agent, options.sourceKind).disclosed,
|
|
67
|
+
loaded: () => currentState(agent, options.sourceKind).loaded,
|
|
68
|
+
definitions: () => currentState(agent, options.sourceKind).definitions,
|
|
69
|
+
}
|
|
70
|
+
const executor = new StableCatalogExecutor(ctx, agent, options.rules, options.executor, holder)
|
|
71
|
+
try {
|
|
72
|
+
const dispose = ctx.effect(() => {
|
|
73
|
+
const uninstall = agent.ctx.tools.setExecutor(executor)
|
|
74
|
+
options.executors.set(agent, executor)
|
|
75
|
+
return () => {
|
|
76
|
+
options.executors.delete(agent)
|
|
77
|
+
uninstall()
|
|
78
|
+
}
|
|
79
|
+
}, 'dsh-dynamic-tools: install frozen-catalog executor')
|
|
80
|
+
let released = false
|
|
81
|
+
release.set(agent, () => {
|
|
82
|
+
if (released) return
|
|
83
|
+
released = true
|
|
84
|
+
release.delete(agent)
|
|
85
|
+
dispose()
|
|
86
|
+
})
|
|
87
|
+
} catch (error: unknown) {
|
|
88
|
+
if (options.reported.has(agent)) return
|
|
89
|
+
options.reported.add(agent)
|
|
90
|
+
ctx.logger.warn('dsh-dynamic-tools: cannot install for agent "%s": %o', agent.id, error)
|
|
91
|
+
}
|
|
92
|
+
}
|