@dsh-plugin/dsh-dynamic-tools 0.0.0-stage → 0.0.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/LICENSE +165 -0
- package/README.md +111 -3
- package/cordis.patch.yml +8 -0
- package/docs/DETAIL.md +1 -0
- package/docs/preview.svg +25 -0
- package/icon.svg +7 -0
- package/lib/catalog.d.ts +53 -0
- package/lib/catalog.js +66 -0
- package/lib/disclose.d.ts +69 -0
- package/lib/disclose.js +115 -0
- package/lib/disclosure.d.ts +64 -0
- package/lib/disclosure.js +99 -0
- package/lib/executor.d.ts +86 -0
- package/lib/executor.js +112 -0
- package/lib/fold.d.ts +28 -0
- package/lib/fold.js +97 -0
- package/lib/index.d.ts +60 -0
- package/lib/index.js +126 -0
- package/lib/install.d.ts +39 -0
- package/lib/install.js +68 -0
- package/lib/render.d.ts +55 -0
- package/lib/render.js +107 -0
- package/lib/source.d.ts +18 -0
- package/lib/source.js +8 -0
- package/lib/tool-load.d.ts +29 -0
- package/lib/tool-load.js +91 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +75 -6
- package/src/catalog.ts +88 -0
- package/src/disclose.ts +151 -0
- package/src/disclosure.ts +147 -0
- package/src/executor.ts +145 -0
- package/src/fold.ts +104 -0
- package/src/index.ts +178 -0
- package/src/install.ts +91 -0
- package/src/render.ts +115 -0
- package/src/source.ts +20 -0
- package/src/tool-load.ts +105 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,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 { createToolLoad, TOOL_LOAD_NAME } from './tool-load.ts'
|
|
24
|
+
|
|
25
|
+
/** Cordis plugin name. */
|
|
26
|
+
export const name = '@dsh-plugin/dsh-dynamic-tools'
|
|
27
|
+
|
|
28
|
+
/** Services the plugin registers into. */
|
|
29
|
+
export const inject = ['tools', 'systemPrompt']
|
|
30
|
+
|
|
31
|
+
/** Tool-selection and disclosure settings. */
|
|
32
|
+
export interface Config {
|
|
33
|
+
/** Install for agents that already exist when this plugin loads. */
|
|
34
|
+
installExistingAgents: boolean
|
|
35
|
+
/** Preset ids left untouched, so their own executor keeps the declaration path. */
|
|
36
|
+
skipPresets: string[]
|
|
37
|
+
dynamic: {
|
|
38
|
+
/** Exact tool names disclosed at the conversation tail. */
|
|
39
|
+
names: string[]
|
|
40
|
+
/** Tool-name prefixes disclosed at the conversation tail. */
|
|
41
|
+
prefixes: string[]
|
|
42
|
+
/** Exact names forced into the frozen head. */
|
|
43
|
+
inlineNames: string[]
|
|
44
|
+
}
|
|
45
|
+
disclosure: {
|
|
46
|
+
/** Attach definitions to the disclosure, or announce names for `tool_load`. */
|
|
47
|
+
mode: 'schema' | 'names'
|
|
48
|
+
/** Require `tool_load` before a dynamic tool may run. */
|
|
49
|
+
requireLoad: boolean
|
|
50
|
+
/** Maximum tools announced in one message. */
|
|
51
|
+
maxToolsPerMessage: number
|
|
52
|
+
/** Maximum bytes of one attached definition. */
|
|
53
|
+
maxSchemaBytes: number
|
|
54
|
+
/** Maximum bytes of one disclosure message. */
|
|
55
|
+
maxMessageBytes: number
|
|
56
|
+
/** Announce tools that stop being available. */
|
|
57
|
+
announceRemovals: boolean
|
|
58
|
+
}
|
|
59
|
+
/** Model-facing reason reported when a declared tool is not admitted. */
|
|
60
|
+
unavailableReasons: Record<string, string>
|
|
61
|
+
/** Concurrency used when the loop supplies no limit. */
|
|
62
|
+
maxParallelCalls: number
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Schemastery validation; invalid values fail at load. */
|
|
66
|
+
export const Config: z<Config> = z.object({
|
|
67
|
+
installExistingAgents: z.boolean().default(true)
|
|
68
|
+
.description('Install for agents that already exist when this plugin loads.'),
|
|
69
|
+
skipPresets: z.array(z.string()).default(['ptc'])
|
|
70
|
+
.description('Preset ids whose own tool executor keeps the declaration path.'),
|
|
71
|
+
dynamic: z.object({
|
|
72
|
+
names: z.array(z.string()).default(['exit_plan_mode'])
|
|
73
|
+
.description('Exact tool names disclosed at the conversation tail.'),
|
|
74
|
+
prefixes: z.array(z.string()).default([])
|
|
75
|
+
.description('Tool-name prefixes disclosed at the conversation tail.'),
|
|
76
|
+
inlineNames: z.array(z.string()).default([])
|
|
77
|
+
.description('Exact names forced into the frozen head.'),
|
|
78
|
+
}).default({}),
|
|
79
|
+
disclosure: z.object({
|
|
80
|
+
mode: z.union(['schema', 'names']).default('schema')
|
|
81
|
+
.description('Attach definitions to the disclosure, or announce names for tool_load.'),
|
|
82
|
+
requireLoad: z.boolean().default(false)
|
|
83
|
+
.description('Require tool_load before a dynamic tool may run.'),
|
|
84
|
+
maxToolsPerMessage: z.number().min(1).default(8)
|
|
85
|
+
.description('Maximum tools announced in one message.'),
|
|
86
|
+
maxSchemaBytes: z.number().min(1).default(6000)
|
|
87
|
+
.description('Maximum bytes of one attached definition.'),
|
|
88
|
+
maxMessageBytes: z.number().min(1).default(20000)
|
|
89
|
+
.description('Maximum bytes of one disclosure message.'),
|
|
90
|
+
announceRemovals: z.boolean().default(true)
|
|
91
|
+
.description('Announce tools that stop being available.'),
|
|
92
|
+
}).default({}),
|
|
93
|
+
unavailableReasons: z.dict(z.string()).default({
|
|
94
|
+
exit_plan_mode: 'Plan mode is not active in this session. Ask the user to switch to plan mode, then call it again.',
|
|
95
|
+
}).description('Model-facing reason reported when a declared tool is not admitted.'),
|
|
96
|
+
maxParallelCalls: z.number().min(1).default(10)
|
|
97
|
+
.description('Concurrency used when the loop supplies no limit.'),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Compose validated settings into the two halves the plugin registers.
|
|
102
|
+
* @param config - validated plugin configuration.
|
|
103
|
+
* @returns classification, execution, and disclosure settings.
|
|
104
|
+
*/
|
|
105
|
+
function resolveSettings(config: Config): {
|
|
106
|
+
rules: ClassificationRules
|
|
107
|
+
install: Omit<InstallOptions, 'executors' | 'reported'>
|
|
108
|
+
disclose: Omit<DiscloseOptions, 'catalogNames' | 'admitted'>
|
|
109
|
+
} {
|
|
110
|
+
const rules: ClassificationRules = {
|
|
111
|
+
dynamicNames: config.dynamic.names,
|
|
112
|
+
dynamicPrefixes: config.dynamic.prefixes,
|
|
113
|
+
inlineNames: config.dynamic.inlineNames,
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
rules,
|
|
117
|
+
install: {
|
|
118
|
+
sourceKind: SOURCE_KIND,
|
|
119
|
+
rules,
|
|
120
|
+
executor: {
|
|
121
|
+
requireLoad: config.disclosure.requireLoad,
|
|
122
|
+
unavailableReasons: config.unavailableReasons,
|
|
123
|
+
maxParallelCalls: config.maxParallelCalls,
|
|
124
|
+
},
|
|
125
|
+
skipPresets: config.skipPresets,
|
|
126
|
+
},
|
|
127
|
+
disclose: {
|
|
128
|
+
sourceKind: SOURCE_KIND,
|
|
129
|
+
limits: {
|
|
130
|
+
mode: config.disclosure.mode,
|
|
131
|
+
maxToolsPerMessage: config.disclosure.maxToolsPerMessage,
|
|
132
|
+
maxSchemaBytes: config.disclosure.maxSchemaBytes,
|
|
133
|
+
maxMessageBytes: config.disclosure.maxMessageBytes,
|
|
134
|
+
},
|
|
135
|
+
announceRemovals: config.disclosure.announceRemovals,
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Register the load tool, the frozen catalog per agent, and tail disclosure.
|
|
142
|
+
* @param ctx - plugin context owning every registration.
|
|
143
|
+
* @param config - validated plugin configuration.
|
|
144
|
+
*/
|
|
145
|
+
export function apply(ctx: Context, config: Config): void {
|
|
146
|
+
const settings = resolveSettings(config)
|
|
147
|
+
const executors = new WeakMap<Agent, StableCatalogExecutor>()
|
|
148
|
+
const release = new WeakMap<Agent, () => void>()
|
|
149
|
+
const reported = new WeakSet<Agent>()
|
|
150
|
+
const installOptions: InstallOptions = { ...settings.install, executors, reported }
|
|
151
|
+
|
|
152
|
+
ctx.effect(() => ctx.tools.register(createToolLoad(ctx, {
|
|
153
|
+
sourceKind: SOURCE_KIND,
|
|
154
|
+
limits: settings.disclose.limits,
|
|
155
|
+
})), `dsh-dynamic-tools: register ${TOOL_LOAD_NAME}`)
|
|
156
|
+
|
|
157
|
+
ctx.on('agent/created', ({ agent }) => {
|
|
158
|
+
installAgent(ctx, agent, installOptions, release)
|
|
159
|
+
return undefined
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// An agent's executor belongs to that agent: release it with the agent instead
|
|
163
|
+
// of retaining every closed session's executor until this plugin unloads.
|
|
164
|
+
ctx.on('agent/disposed', ({ agent }) => {
|
|
165
|
+
release.get(agent)?.()
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
if (config.installExistingAgents) {
|
|
169
|
+
const agents = ctx.get('agents')
|
|
170
|
+
for (const agent of agents?.list() ?? []) installAgent(ctx, agent, installOptions, release)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
registerDisclosure(ctx, {
|
|
174
|
+
...settings.disclose,
|
|
175
|
+
catalogNames: agent => executors.get(agent)?.declaredNames() ?? new Set(),
|
|
176
|
+
admitted: agent => executors.get(agent)?.admittedDefinitions(),
|
|
177
|
+
})
|
|
178
|
+
}
|
package/src/install.ts
ADDED
|
@@ -0,0 +1,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
|
+
}
|
|
69
|
+
const executor = new StableCatalogExecutor(ctx, agent, options.rules, options.executor, holder)
|
|
70
|
+
try {
|
|
71
|
+
const dispose = ctx.effect(() => {
|
|
72
|
+
const uninstall = agent.ctx.tools.setExecutor(executor)
|
|
73
|
+
options.executors.set(agent, executor)
|
|
74
|
+
return () => {
|
|
75
|
+
options.executors.delete(agent)
|
|
76
|
+
uninstall()
|
|
77
|
+
}
|
|
78
|
+
}, 'dsh-dynamic-tools: install frozen-catalog executor')
|
|
79
|
+
let released = false
|
|
80
|
+
release.set(agent, () => {
|
|
81
|
+
if (released) return
|
|
82
|
+
released = true
|
|
83
|
+
release.delete(agent)
|
|
84
|
+
dispose()
|
|
85
|
+
})
|
|
86
|
+
} catch (error: unknown) {
|
|
87
|
+
if (options.reported.has(agent)) return
|
|
88
|
+
options.reported.add(agent)
|
|
89
|
+
ctx.logger.warn('dsh-dynamic-tools: cannot install for agent "%s": %o', agent.id, error)
|
|
90
|
+
}
|
|
91
|
+
}
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-visible text for disclosure messages and the tool-load notice.
|
|
3
|
+
* The tags are the fold's input, so both sides change together.
|
|
4
|
+
*
|
|
5
|
+
* @module dsh-dynamic-tools/render
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ToolSchemaLike } from './catalog.ts'
|
|
9
|
+
import type { DisclosureBatch } from './disclosure.ts'
|
|
10
|
+
|
|
11
|
+
const CLOSING_TAG = /<\//g
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Keep tool prose from closing a wrapper or block early. JSON accepts the
|
|
15
|
+
* resulting backslash-slash escape, so a definition body stays parseable.
|
|
16
|
+
* @param text - arbitrary tool-authored text.
|
|
17
|
+
* @returns the text with every closing tag escape neutralized.
|
|
18
|
+
*/
|
|
19
|
+
export function escapeReminderText(text: string): string {
|
|
20
|
+
return text.replace(CLOSING_TAG, '<\\/')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Collapse text to one announcement line, keeping the name list parseable.
|
|
25
|
+
* @param text - arbitrary tool-authored text.
|
|
26
|
+
* @returns the text with line breaks and repeated whitespace collapsed.
|
|
27
|
+
*/
|
|
28
|
+
export function oneLine(text: string): string {
|
|
29
|
+
return text.replace(/\s+/g, ' ').trim()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Render one disclosure batch.
|
|
34
|
+
* @param batch - definitions, name-only entries, and removals.
|
|
35
|
+
* @returns the complete user-message text.
|
|
36
|
+
*/
|
|
37
|
+
export function renderDisclosure(batch: DisclosureBatch): string {
|
|
38
|
+
const sections: string[] = []
|
|
39
|
+
const announced: string[] = []
|
|
40
|
+
for (const schema of batch.added) announced.push(`- ${schema.name}: ${oneLine(escapeReminderText(schema.description))}`)
|
|
41
|
+
for (const name of batch.names) announced.push(`- ${name}`)
|
|
42
|
+
if (announced.length > 0) sections.push(`<tools_added>\n${announced.join('\n')}\n</tools_added>`)
|
|
43
|
+
for (const schema of batch.added) {
|
|
44
|
+
sections.push(`<tool_definition name="${schema.name}">\n${escapeReminderText(JSON.stringify(schema))}\n</tool_definition>`)
|
|
45
|
+
}
|
|
46
|
+
if (batch.names.length > 0) {
|
|
47
|
+
sections.push(`<tools_load_required>\n${batch.names.map(name => `- ${name}`).join('\n')}\n</tools_load_required>`)
|
|
48
|
+
}
|
|
49
|
+
if (batch.removed.length > 0) {
|
|
50
|
+
sections.push(`<tools_removed>\n${batch.removed.map(name => `- ${name}`).join('\n')}\n</tools_removed>`)
|
|
51
|
+
}
|
|
52
|
+
const trailers: string[] = []
|
|
53
|
+
if (batch.added.length > 0) {
|
|
54
|
+
trailers.push('These tools are callable directly by name even though they are not in your tool list; match the definitions above.')
|
|
55
|
+
}
|
|
56
|
+
if (batch.names.length > 0) {
|
|
57
|
+
trailers.push('Call tool_load with the names under tools_load_required before calling those tools.')
|
|
58
|
+
}
|
|
59
|
+
if (batch.removed.length > 0) {
|
|
60
|
+
trailers.push('Removed tools are unavailable; they fail if called.')
|
|
61
|
+
}
|
|
62
|
+
sections.push(...trailers)
|
|
63
|
+
return `<system-reminder>\n${sections.join('\n\n')}\n</system-reminder>`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Render the definitions `tool_load` returns for one tool.
|
|
68
|
+
* @param schema - the definition to deliver.
|
|
69
|
+
* @returns the complete user-message text.
|
|
70
|
+
*/
|
|
71
|
+
export function renderLoadedDefinition(schema: ToolSchemaLike): string {
|
|
72
|
+
return `<system-reminder>\n<tool_definition name="${schema.name}">\n${escapeReminderText(JSON.stringify(schema))}\n</tool_definition>\nThese tools are callable directly by name even though they are not in your tool list; match the definitions above.\n</system-reminder>`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Render the model-facing result of one `tool_load` call.
|
|
77
|
+
* @param loaded - names whose definitions were delivered.
|
|
78
|
+
* @param alreadyAvailable - names that are declared in the tool list already.
|
|
79
|
+
* @param unavailable - names no longer registered or not admitted.
|
|
80
|
+
* @param tooLarge - names too large to deliver.
|
|
81
|
+
* @returns plain-text result content.
|
|
82
|
+
*/
|
|
83
|
+
export function renderLoadResult(
|
|
84
|
+
loaded: readonly string[],
|
|
85
|
+
alreadyAvailable: readonly string[],
|
|
86
|
+
unavailable: readonly string[],
|
|
87
|
+
tooLarge: readonly string[],
|
|
88
|
+
): string {
|
|
89
|
+
const lines: string[] = []
|
|
90
|
+
if (loaded.length > 0) lines.push(`Loaded: ${loaded.join(', ')}`)
|
|
91
|
+
if (alreadyAvailable.length > 0) lines.push(`Already available: ${alreadyAvailable.join(', ')}`)
|
|
92
|
+
if (unavailable.length > 0) lines.push(`Unavailable: ${unavailable.join(', ')}`)
|
|
93
|
+
if (tooLarge.length > 0) lines.push(`Definition too large to load: ${tooLarge.join(', ')}`)
|
|
94
|
+
if (lines.length === 0) return 'No tool names were supplied.'
|
|
95
|
+
return lines.join('\n')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The reason reported when a catalog tool is not admitted for this step.
|
|
100
|
+
* @param name - tool name the model called.
|
|
101
|
+
* @param configured - configured reason for this name, when any.
|
|
102
|
+
* @returns the model-facing denial reason.
|
|
103
|
+
*/
|
|
104
|
+
export function unavailableReason(name: string, configured: string | undefined): string {
|
|
105
|
+
return configured ?? `Tool "${name}" is registered but not available in the current session state; ask the user to enable it, then retry.`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The reason reported when a loaded-on-demand tool has not been loaded.
|
|
110
|
+
* @param name - tool name the model called.
|
|
111
|
+
* @returns the model-facing denial reason.
|
|
112
|
+
*/
|
|
113
|
+
export function loadFirstReason(name: string): string {
|
|
114
|
+
return `Tool "${name}" is available but its definition is not loaded. Call tool_load with {"names":["${name}"]} first, then call it again.`
|
|
115
|
+
}
|
package/src/source.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The message source this plugin records, declared once so every module and the
|
|
3
|
+
* fold agree on it.
|
|
4
|
+
*
|
|
5
|
+
* @module dsh-dynamic-tools/source
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ContextFormed } from '@deepseek-ai/dsh-llm'
|
|
9
|
+
|
|
10
|
+
declare module '@deepseek-ai/dsh-llm' {
|
|
11
|
+
interface MessageSourceMap {
|
|
12
|
+
'dynamic-tools': { kind: 'dynamic-tools' } & ContextFormed
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Message source kind used for every disclosure and loaded definition. */
|
|
17
|
+
export const SOURCE_KIND = 'dynamic-tools' as const
|
|
18
|
+
|
|
19
|
+
/** Type of {@link SOURCE_KIND}. */
|
|
20
|
+
export type SourceKind = typeof SOURCE_KIND
|
package/src/tool-load.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `tool_load`: deliver the definition of a tool the model was told about but
|
|
3
|
+
* whose schema it cannot see in the declaration head.
|
|
4
|
+
*
|
|
5
|
+
* The definition rides `exec.deferContext()`, so the loop logs it after this
|
|
6
|
+
* tool's result and the next request carries it at the tail.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-dynamic-tools/tool-load
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
12
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
|
13
|
+
import { defineTool, projectToolSchema, ToolContextUnavailableError } from '@deepseek-ai/dsh-tools'
|
|
14
|
+
import type { ToolDefinition } from '@deepseek-ai/dsh-tools'
|
|
15
|
+
import { schemaBytes } from './catalog.ts'
|
|
16
|
+
import type { DisclosureLimits } from './disclosure.ts'
|
|
17
|
+
import { renderLoadedDefinition, renderLoadResult } from './render.ts'
|
|
18
|
+
import { currentState, storeState } from './disclose.ts'
|
|
19
|
+
import type { SourceKind } from './source.ts'
|
|
20
|
+
|
|
21
|
+
/** Names this plugin owns; a deployment cannot shadow them. */
|
|
22
|
+
export const TOOL_LOAD_NAME = 'tool_load'
|
|
23
|
+
|
|
24
|
+
/** Settings the load tool needs at call time. */
|
|
25
|
+
export interface ToolLoadOptions {
|
|
26
|
+
/** Message source kind owned by this plugin. */
|
|
27
|
+
readonly sourceKind: SourceKind
|
|
28
|
+
/** Validated disclosure limits; `maxSchemaBytes` caps a loaded definition. */
|
|
29
|
+
readonly limits: DisclosureLimits
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create the `tool_load` definition.
|
|
34
|
+
* @param ctx - plugin context, used to read the agent's visible tools.
|
|
35
|
+
* @param options - source kind and limits.
|
|
36
|
+
* @returns the unregistered definition.
|
|
37
|
+
*/
|
|
38
|
+
export function createToolLoad(ctx: Context, options: ToolLoadOptions): ToolDefinition {
|
|
39
|
+
return defineTool({
|
|
40
|
+
name: TOOL_LOAD_NAME,
|
|
41
|
+
description: 'Read the full definition of tools that are announced in this conversation but absent from your tool list. Pass their exact names. Definitions arrive as a user message after this result; call the tool in a later step.',
|
|
42
|
+
parameters: {
|
|
43
|
+
names: {
|
|
44
|
+
type: 'array',
|
|
45
|
+
items: { type: 'string' },
|
|
46
|
+
required: true,
|
|
47
|
+
description: 'Exact tool names taken from the latest <tools_added> or <tools_load_required> announcement.',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
output: {
|
|
51
|
+
schema: {
|
|
52
|
+
type: 'object',
|
|
53
|
+
additionalProperties: false,
|
|
54
|
+
properties: {
|
|
55
|
+
loaded: { type: 'array', items: { type: 'string' }, required: true },
|
|
56
|
+
alreadyAvailable: { type: 'array', items: { type: 'string' }, required: true },
|
|
57
|
+
unavailable: { type: 'array', items: { type: 'string' }, required: true },
|
|
58
|
+
tooLarge: { type: 'array', items: { type: 'string' }, required: true },
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
render: (_args, value) => [{
|
|
62
|
+
type: 'text',
|
|
63
|
+
text: renderLoadResult(value.loaded, value.alreadyAvailable, value.unavailable, value.tooLarge),
|
|
64
|
+
}],
|
|
65
|
+
},
|
|
66
|
+
async execute(args, exec) {
|
|
67
|
+
exec.signal.throwIfAborted()
|
|
68
|
+
const agent = exec.agent
|
|
69
|
+
if (agent === undefined) {
|
|
70
|
+
throw new ToolContextUnavailableError(`${TOOL_LOAD_NAME} requires a calling Agent`)
|
|
71
|
+
}
|
|
72
|
+
const loaded: string[] = []
|
|
73
|
+
const alreadyAvailable: string[] = []
|
|
74
|
+
const unavailable: string[] = []
|
|
75
|
+
const tooLarge: string[] = []
|
|
76
|
+
const definitions = new Map(ctx.tools.definitions(agent).map(definition => [definition.name, definition]))
|
|
77
|
+
const state = currentState(agent, options.sourceKind)
|
|
78
|
+
const nextLoaded = new Set(state.loaded)
|
|
79
|
+
const wanted = [...new Set(args.names.map(name => name.trim()).filter(name => name.length > 0))]
|
|
80
|
+
for (const name of wanted) {
|
|
81
|
+
const definition = definitions.get(name)
|
|
82
|
+
if (definition === undefined) {
|
|
83
|
+
unavailable.push(name)
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
const schema = projectToolSchema(definition, true)
|
|
87
|
+
if (schemaBytes(schema) > options.limits.maxSchemaBytes) {
|
|
88
|
+
tooLarge.push(name)
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
if (!state.disclosed.has(name)) alreadyAvailable.push(name)
|
|
92
|
+
else loaded.push(name)
|
|
93
|
+
nextLoaded.add(name)
|
|
94
|
+
exec.deferContext(createUserMessage({
|
|
95
|
+
content: [{ type: 'text', text: renderLoadedDefinition(schema) }],
|
|
96
|
+
source: { kind: options.sourceKind, form: 'catalog' },
|
|
97
|
+
}))
|
|
98
|
+
}
|
|
99
|
+
storeState(agent, { disclosed: state.disclosed, loaded: nextLoaded })
|
|
100
|
+
return { loaded, alreadyAvailable, unavailable, tooLarge }
|
|
101
|
+
},
|
|
102
|
+
isConcurrencySafe: () => false,
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|