@dsh-plugin/dsh-dynamic-tools 0.0.1 → 0.1.0-dev.43450988228
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 +111 -111
- package/docs/DETAIL.md +1 -1
- package/docs/preview.svg +25 -25
- package/icon.svg +7 -7
- package/locale/en.json +7 -7
- package/locale/zh.json +7 -7
- package/package.json +75 -75
- package/src/catalog.ts +88 -88
- package/src/disclose.ts +151 -151
- package/src/disclosure.ts +147 -147
- package/src/executor.ts +145 -145
- package/src/fold.ts +104 -104
- package/src/index.ts +178 -178
- package/src/install.ts +91 -91
- package/src/render.ts +115 -115
- package/src/source.ts +20 -20
- package/src/tool-load.ts +105 -105
package/src/render.ts
CHANGED
|
@@ -1,115 +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
|
-
}
|
|
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
CHANGED
|
@@ -1,20 +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
|
|
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
CHANGED
|
@@ -1,105 +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
|
-
|
|
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
|
+
|