@dsh-plugin/dsh-dynamic-tools 0.1.0-dev.43611140243 → 0.1.0-dev.43800186861
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/README.md +68 -116
- package/lib/disclose.d.ts +8 -58
- package/lib/disclose.js +53 -140
- package/lib/executor.d.ts +13 -24
- package/lib/executor.js +62 -42
- package/lib/history.d.ts +9 -0
- package/lib/history.js +22 -0
- package/lib/host.d.ts +16 -0
- package/lib/host.js +15 -0
- package/lib/index.d.ts +13 -15
- package/lib/index.js +54 -20
- package/lib/install.d.ts +4 -0
- package/lib/install.js +15 -9
- package/lib/responses.d.ts +37 -0
- package/lib/responses.js +157 -0
- package/lib/source.d.ts +14 -0
- package/lib/source.js +2 -0
- package/lib/strategy.d.ts +9 -17
- package/lib/strategy.js +11 -19
- package/lib/tool-load.d.ts +9 -19
- package/lib/tool-load.js +15 -32
- package/package.json +12 -6
- package/src/disclose.ts +51 -174
- package/src/executor.ts +48 -40
- package/src/history.ts +25 -0
- package/src/host.ts +23 -0
- package/src/index.ts +55 -22
- package/src/install.ts +13 -9
- package/src/responses.ts +154 -0
- package/src/source.ts +16 -1
- package/src/strategy.ts +14 -20
- package/src/tool-load.ts +21 -33
package/lib/responses.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { schemaBytes, toolSignature } from "./catalog.js";
|
|
2
|
+
import { renderDisclosure } from "./render.js";
|
|
3
|
+
import { SOURCE_KIND, TOOL_LOAD_NAME } from "./source.js";
|
|
4
|
+
/** 固定正文编码供持久化与发送时双向核验,不依赖可变注册表。 */
|
|
5
|
+
export function renderAddition(addition) {
|
|
6
|
+
return `[dsh-responses-tools:${addition.id}]\n${JSON.stringify(addition.tools)}`;
|
|
7
|
+
}
|
|
8
|
+
function makeAddition(schemas, id) {
|
|
9
|
+
return {
|
|
10
|
+
version: 1, id,
|
|
11
|
+
tools: schemas.map(schema => ({ type: 'function', name: schema.name, description: schema.description, parameters: schema.parameters, strict: false })),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** 声明先保存到宿主消息,随后才可计入已加载状态。 */
|
|
15
|
+
export function additionMessage(host, schemas) {
|
|
16
|
+
const addition = makeAddition(schemas, crypto.randomUUID());
|
|
17
|
+
return host.createUserMessage({
|
|
18
|
+
content: [{ type: 'text', text: renderAddition(addition) }],
|
|
19
|
+
source: { kind: SOURCE_KIND, form: 'catalog', responsesToolAddition: addition },
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** UUID 固定长度,预算包含标识与原生函数包装。 */
|
|
23
|
+
export function additionBytes(schemas) {
|
|
24
|
+
return new TextEncoder().encode(renderAddition(makeAddition(schemas, '00000000-0000-0000-0000-000000000000'))).length;
|
|
25
|
+
}
|
|
26
|
+
/** 单个定义必须同时适合 schema 预算和持久化消息预算。 */
|
|
27
|
+
export function canLoadNative(schema, limits) {
|
|
28
|
+
return schemaBytes(schema) <= limits.maxSchemaBytes && additionBytes([schema]) <= limits.maxMessageBytes;
|
|
29
|
+
}
|
|
30
|
+
/** 按最终 UTF-8 文本大小分批,定义、名字与移除通知分别记录。 */
|
|
31
|
+
export function planResponsesDisclosure(candidates, state, limits, requireLoad) {
|
|
32
|
+
const additions = [];
|
|
33
|
+
const notices = [];
|
|
34
|
+
const names = [];
|
|
35
|
+
let pending = [];
|
|
36
|
+
for (const schema of candidates) {
|
|
37
|
+
const known = state.disclosed.get(schema.name);
|
|
38
|
+
const bootstrap = schema.name === TOOL_LOAD_NAME;
|
|
39
|
+
if (known?.signature === toolSignature(schema)
|
|
40
|
+
&& (known.schema || (!bootstrap && (requireLoad || limits.mode === 'names' || !canLoadNative(schema, limits))))) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (bootstrap && !canLoadNative(schema, limits)) {
|
|
44
|
+
throw new Error('dsh-dynamic-tools: tool_load 超过声明大小限制,无法建立原生加载入口');
|
|
45
|
+
}
|
|
46
|
+
if (!bootstrap && (requireLoad || limits.mode === 'names' || !canLoadNative(schema, limits))) {
|
|
47
|
+
names.push(schema);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (pending.length && (pending.length >= limits.maxToolsPerMessage || additionBytes([...pending, schema]) > limits.maxMessageBytes)) {
|
|
51
|
+
additions.push(pending);
|
|
52
|
+
pending = [];
|
|
53
|
+
}
|
|
54
|
+
pending.push(schema);
|
|
55
|
+
}
|
|
56
|
+
if (pending.length) {
|
|
57
|
+
additions.push(pending);
|
|
58
|
+
}
|
|
59
|
+
const available = new Set(candidates.map(schema => schema.name));
|
|
60
|
+
const entries = [
|
|
61
|
+
...[...state.disclosed.keys()].filter(name => !available.has(name)).map(name => ({ name, removed: true, signature: '' })),
|
|
62
|
+
...names.map(schema => ({ name: schema.name, removed: false, signature: toolSignature(schema) })),
|
|
63
|
+
];
|
|
64
|
+
let group = [];
|
|
65
|
+
const build = (entries) => {
|
|
66
|
+
const batch = { added: [], names: entries.filter(entry => !entry.removed).map(entry => entry.name), removed: entries.filter(entry => entry.removed).map(entry => entry.name) };
|
|
67
|
+
const notice = { version: 1, added: entries.filter(entry => !entry.removed).map(({ name, signature }) => ({ name, signature })), removed: batch.removed };
|
|
68
|
+
return { batch, notice, text: renderDisclosure(batch, 'responses') };
|
|
69
|
+
};
|
|
70
|
+
const fits = (entries) => entries.length <= limits.maxToolsPerMessage && new TextEncoder().encode(build(entries).text).length <= limits.maxMessageBytes;
|
|
71
|
+
for (const entry of entries) {
|
|
72
|
+
if (group.length && !fits([...group, entry])) {
|
|
73
|
+
notices.push(build(group));
|
|
74
|
+
group = [];
|
|
75
|
+
}
|
|
76
|
+
if (!fits([entry])) {
|
|
77
|
+
throw new Error(`dsh-dynamic-tools: maxMessageBytes 无法容纳 ${entry.name} 的工具通知,请提高限制`);
|
|
78
|
+
}
|
|
79
|
+
group.push(entry);
|
|
80
|
+
}
|
|
81
|
+
if (group.length) {
|
|
82
|
+
notices.push(build(group));
|
|
83
|
+
}
|
|
84
|
+
return { additions, notices };
|
|
85
|
+
}
|
|
86
|
+
/** 只接受插件来源的版本化记录,普通文本中的 schema 从不成为声明。 */
|
|
87
|
+
export function projectResponsesRecords(messages) {
|
|
88
|
+
const records = [];
|
|
89
|
+
const ids = new Set();
|
|
90
|
+
for (const message of messages) {
|
|
91
|
+
if (message.source?.kind !== SOURCE_KIND || message.source.responsesToolAddition === undefined) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const addition = message.source.responsesToolAddition;
|
|
95
|
+
if (message.role !== 'user' || !message.id || !addition || addition.version !== 1
|
|
96
|
+
|| typeof addition.id !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/.test(addition.id) || ids.has(addition.id)
|
|
97
|
+
|| !Array.isArray(addition.tools) || addition.tools.length === 0) {
|
|
98
|
+
throw new Error('dsh-dynamic-tools: 原生声明记录损坏、标识缺失或重复');
|
|
99
|
+
}
|
|
100
|
+
const names = new Set();
|
|
101
|
+
for (const tool of addition.tools) {
|
|
102
|
+
if (tool.type !== 'function' || typeof tool.name !== 'string' || !tool.name
|
|
103
|
+
|| typeof tool.description !== 'string' || tool.strict !== false || !tool.parameters
|
|
104
|
+
|| typeof tool.parameters !== 'object' || Array.isArray(tool.parameters) || names.has(tool.name)) {
|
|
105
|
+
throw new Error(`dsh-dynamic-tools: 原生工具定义无效 ${addition.id}`);
|
|
106
|
+
}
|
|
107
|
+
names.add(tool.name);
|
|
108
|
+
}
|
|
109
|
+
const text = renderAddition(addition);
|
|
110
|
+
if (message.content.length !== 1 || message.content[0]?.type !== 'text' || message.content[0].text !== text) {
|
|
111
|
+
throw new Error(`dsh-dynamic-tools: 原生声明正文不匹配 ${addition.id}`);
|
|
112
|
+
}
|
|
113
|
+
ids.add(addition.id);
|
|
114
|
+
records.push({ messageId: message.id, sourceKind: SOURCE_KIND, text, addition });
|
|
115
|
+
}
|
|
116
|
+
return records;
|
|
117
|
+
}
|
|
118
|
+
/** 声明保留历史最新定义;移除通知只撤销可见状态,执行准入另由当步工具集决定。 */
|
|
119
|
+
export function foldResponses(messages, baseline = []) {
|
|
120
|
+
const records = new Map(projectResponsesRecords(messages).map(record => [record.messageId, record]));
|
|
121
|
+
const disclosed = new Map();
|
|
122
|
+
const definitions = new Map();
|
|
123
|
+
const loaded = new Set();
|
|
124
|
+
for (const schema of baseline) {
|
|
125
|
+
definitions.set(schema.name, schema);
|
|
126
|
+
loaded.add(schema.name);
|
|
127
|
+
disclosed.set(schema.name, { signature: toolSignature(schema), schema: true });
|
|
128
|
+
}
|
|
129
|
+
for (const message of messages) {
|
|
130
|
+
const record = message.id ? records.get(message.id) : undefined;
|
|
131
|
+
if (record) {
|
|
132
|
+
for (const tool of record.addition.tools) {
|
|
133
|
+
const schema = { name: tool.name, description: tool.description, parameters: tool.parameters };
|
|
134
|
+
definitions.set(tool.name, schema);
|
|
135
|
+
loaded.add(tool.name);
|
|
136
|
+
disclosed.set(tool.name, { signature: toolSignature(schema), schema: true });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (message.role !== 'user' || message.source?.kind !== SOURCE_KIND) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const notice = message.source.responsesToolNotice;
|
|
143
|
+
if (!notice) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (notice.version !== 1 || !Array.isArray(notice.added) || !Array.isArray(notice.removed)) {
|
|
147
|
+
throw new Error('dsh-dynamic-tools: 原生工具通知记录无效');
|
|
148
|
+
}
|
|
149
|
+
for (const name of notice.removed) {
|
|
150
|
+
disclosed.delete(name);
|
|
151
|
+
}
|
|
152
|
+
for (const entry of notice.added) {
|
|
153
|
+
disclosed.set(entry.name, { signature: entry.signature, schema: false });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { disclosed, definitions, loaded, damaged: [] };
|
|
157
|
+
}
|
package/lib/source.d.ts
CHANGED
|
@@ -5,14 +5,28 @@
|
|
|
5
5
|
* @module dsh-dynamic-tools/source
|
|
6
6
|
*/
|
|
7
7
|
import type { ContextFormed } from '@deepseek-ai/dsh-llm';
|
|
8
|
+
import type { ResponsesToolAddition } from '@dsh-plugin/dsh-loader';
|
|
9
|
+
/** 公告记录保存定义签名,使未加载的同名更新仍能与已公告版本区分。 */
|
|
10
|
+
export interface ResponsesToolNotice {
|
|
11
|
+
readonly version: 1;
|
|
12
|
+
readonly added: readonly {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly signature: string;
|
|
15
|
+
}[];
|
|
16
|
+
readonly removed: readonly string[];
|
|
17
|
+
}
|
|
8
18
|
declare module '@deepseek-ai/dsh-llm' {
|
|
9
19
|
interface MessageSourceMap {
|
|
10
20
|
'dynamic-tools': {
|
|
11
21
|
kind: 'dynamic-tools';
|
|
22
|
+
responsesToolAddition?: ResponsesToolAddition;
|
|
23
|
+
responsesToolNotice?: ResponsesToolNotice;
|
|
12
24
|
} & ContextFormed;
|
|
13
25
|
}
|
|
14
26
|
}
|
|
15
27
|
/** Message source kind used for every disclosure and loaded definition. */
|
|
16
28
|
export declare const SOURCE_KIND: "dynamic-tools";
|
|
29
|
+
/** 静态加载入口名称,原生接管已有会话时可在历史中补齐。 */
|
|
30
|
+
export declare const TOOL_LOAD_NAME = "tool_load";
|
|
17
31
|
/** Type of {@link SOURCE_KIND}. */
|
|
18
32
|
export type SourceKind = typeof SOURCE_KIND;
|
package/lib/source.js
CHANGED
package/lib/strategy.d.ts
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* How one route receives dynamic tools: at the conversation tail, or declared
|
|
3
|
-
* in the tool list after an explicit load.
|
|
4
|
-
*
|
|
5
|
-
* OpenAI Responses routes call only the functions present in the request's
|
|
6
|
-
* `tools` array, so a definition delivered as conversation text is never
|
|
7
|
-
* invoked. Those routes need the definition loaded through `tool_load` and
|
|
8
|
-
* declared on the following request. Every other route reads the tail
|
|
9
|
-
* disclosure and calls the tool directly, which needs no declaration change.
|
|
10
|
-
*
|
|
11
|
-
* Pure module: no Harness imports, so it runs under the Node type-stripping
|
|
12
|
-
* loader in tests.
|
|
13
|
-
*
|
|
14
|
-
* @module dsh-dynamic-tools/strategy
|
|
15
|
-
*/
|
|
1
|
+
/** 按显式 Responses 路由、declareOnLoad 和 tail 的顺序选择动态工具协议。 */
|
|
16
2
|
/** How one Agent's dynamic tools reach the model. */
|
|
17
|
-
export type ToolStrategy = 'tail' | 'declare';
|
|
3
|
+
export type ToolStrategy = 'tail' | 'declare' | 'responses';
|
|
4
|
+
/** provider 精确匹配;models 沿用大小写不敏感的 * 通配规则。 */
|
|
5
|
+
export interface ResponsesRoute {
|
|
6
|
+
readonly provider: string;
|
|
7
|
+
readonly models: readonly string[];
|
|
8
|
+
}
|
|
18
9
|
/** Route matching rules for the declare-on-load strategy. */
|
|
19
10
|
export interface StrategyRules {
|
|
20
11
|
/** Model-id patterns; an empty list matches every model. */
|
|
21
12
|
readonly models: readonly string[];
|
|
22
13
|
/** Provider-route patterns; an empty list matches every provider. */
|
|
23
14
|
readonly providers: readonly string[];
|
|
15
|
+
readonly responses?: readonly ResponsesRoute[];
|
|
24
16
|
}
|
|
25
17
|
/** Model families whose routes call only tools declared in the request. */
|
|
26
18
|
export declare const DEFAULT_DECLARE_MODELS: readonly string[];
|
|
@@ -44,7 +36,7 @@ export declare function patternMatches(pattern: string, value: string): boolean;
|
|
|
44
36
|
*
|
|
45
37
|
* @param selection - route the step fixed for its request, when one is recorded.
|
|
46
38
|
* @param rules - configured declare-on-load matching rules.
|
|
47
|
-
* @returns
|
|
39
|
+
* @returns 显式原生路由优先,其次按需声明,其余使用尾部文本。
|
|
48
40
|
*/
|
|
49
41
|
export declare function selectStrategy(selection: {
|
|
50
42
|
readonly provider: string;
|
package/lib/strategy.js
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* How one route receives dynamic tools: at the conversation tail, or declared
|
|
3
|
-
* in the tool list after an explicit load.
|
|
4
|
-
*
|
|
5
|
-
* OpenAI Responses routes call only the functions present in the request's
|
|
6
|
-
* `tools` array, so a definition delivered as conversation text is never
|
|
7
|
-
* invoked. Those routes need the definition loaded through `tool_load` and
|
|
8
|
-
* declared on the following request. Every other route reads the tail
|
|
9
|
-
* disclosure and calls the tool directly, which needs no declaration change.
|
|
10
|
-
*
|
|
11
|
-
* Pure module: no Harness imports, so it runs under the Node type-stripping
|
|
12
|
-
* loader in tests.
|
|
13
|
-
*
|
|
14
|
-
* @module dsh-dynamic-tools/strategy
|
|
15
|
-
*/
|
|
1
|
+
/** 按显式 Responses 路由、declareOnLoad 和 tail 的顺序选择动态工具协议。 */
|
|
16
2
|
/** Model families whose routes call only tools declared in the request. */
|
|
17
3
|
export const DEFAULT_DECLARE_MODELS = Object.freeze([
|
|
18
4
|
'gpt*',
|
|
@@ -50,14 +36,20 @@ function matchesAny(patterns, value) {
|
|
|
50
36
|
*
|
|
51
37
|
* @param selection - route the step fixed for its request, when one is recorded.
|
|
52
38
|
* @param rules - configured declare-on-load matching rules.
|
|
53
|
-
* @returns
|
|
39
|
+
* @returns 显式原生路由优先,其次按需声明,其余使用尾部文本。
|
|
54
40
|
*/
|
|
55
41
|
export function selectStrategy(selection, rules) {
|
|
56
|
-
if (selection === undefined)
|
|
42
|
+
if (selection === undefined) {
|
|
57
43
|
return 'tail';
|
|
58
|
-
|
|
44
|
+
}
|
|
45
|
+
if (rules.responses?.some(route => route.provider === selection.provider && matchesAny(route.models, selection.model))) {
|
|
46
|
+
return 'responses';
|
|
47
|
+
}
|
|
48
|
+
if (!matchesAny(rules.models, selection.model)) {
|
|
59
49
|
return 'tail';
|
|
60
|
-
|
|
50
|
+
}
|
|
51
|
+
if (!matchesAny(rules.providers, selection.provider)) {
|
|
61
52
|
return 'tail';
|
|
53
|
+
}
|
|
62
54
|
return 'declare';
|
|
63
55
|
}
|
package/lib/tool-load.d.ts
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `tool_load`: deliver the definition of a tool the conversation announced but
|
|
3
|
-
* the declaration head does not contain.
|
|
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. On a route that
|
|
7
|
-
* reads only declared tool lists, the next request also declares it.
|
|
8
|
-
*
|
|
9
|
-
* Only a name the model was actually told about is served: announcing is what
|
|
10
|
-
* makes a definition fair game. Serving any registered name would let a guessed
|
|
11
|
-
* name pull an unannounced definition into the head and keep it there.
|
|
12
|
-
*
|
|
13
|
-
* @module dsh-dynamic-tools/tool-load
|
|
14
|
-
*/
|
|
15
|
-
import type { Context } from '@deepseek-ai/cordis';
|
|
1
|
+
/** tool_load 只加载已公告且当前准入的工具;声明通过 deferContext 保存后才生效。 */
|
|
16
2
|
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
17
3
|
import type { ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
18
|
-
import {
|
|
4
|
+
import type { DisclosureLimits } from './disclosure.ts';
|
|
5
|
+
import type { DisclosureHistory } from './history.ts';
|
|
6
|
+
import type { HostBindings } from './host.ts';
|
|
19
7
|
import type { SourceKind } from './source.ts';
|
|
20
8
|
import type { ToolStrategy } from './strategy.ts';
|
|
21
9
|
/** Names this plugin owns; a deployment cannot shadow them. */
|
|
22
|
-
export
|
|
10
|
+
export { TOOL_LOAD_NAME } from './source.ts';
|
|
23
11
|
/** Settings the load tool needs at call time. */
|
|
24
12
|
export interface ToolLoadOptions {
|
|
13
|
+
readonly host: HostBindings;
|
|
14
|
+
readonly history: DisclosureHistory;
|
|
15
|
+
readonly admitted: (agent: Agent) => readonly ToolDefinition[] | undefined;
|
|
25
16
|
/** Message source kind owned by this plugin. */
|
|
26
17
|
readonly sourceKind: SourceKind;
|
|
27
18
|
/** Validated disclosure limits; `maxSchemaBytes` caps a loaded definition. */
|
|
@@ -33,8 +24,7 @@ export interface ToolLoadOptions {
|
|
|
33
24
|
}
|
|
34
25
|
/**
|
|
35
26
|
* Create the `tool_load` definition.
|
|
36
|
-
* @param ctx - plugin context, used to read the agent's visible tools.
|
|
37
27
|
* @param options - source kind, limits, and the agent's declaration facts.
|
|
38
28
|
* @returns the unregistered definition.
|
|
39
29
|
*/
|
|
40
|
-
export declare function createToolLoad(
|
|
30
|
+
export declare function createToolLoad(options: ToolLoadOptions): ToolDefinition;
|
package/lib/tool-load.js
CHANGED
|
@@ -1,35 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
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. On a route that
|
|
7
|
-
* reads only declared tool lists, the next request also declares it.
|
|
8
|
-
*
|
|
9
|
-
* Only a name the model was actually told about is served: announcing is what
|
|
10
|
-
* makes a definition fair game. Serving any registered name would let a guessed
|
|
11
|
-
* name pull an unannounced definition into the head and keep it there.
|
|
12
|
-
*
|
|
13
|
-
* @module dsh-dynamic-tools/tool-load
|
|
14
|
-
*/
|
|
15
|
-
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
16
|
-
import { defineTool, projectToolSchema, ToolContextUnavailableError } from '@deepseek-ai/dsh-tools';
|
|
17
|
-
import { schemaBytes } from "./catalog.js";
|
|
18
|
-
import { withDefinition } from "./disclosure.js";
|
|
1
|
+
/** tool_load 只加载已公告且当前准入的工具;声明通过 deferContext 保存后才生效。 */
|
|
2
|
+
import { schemaBytes, toolSignature } from "./catalog.js";
|
|
3
|
+
import { additionMessage, canLoadNative } from "./responses.js";
|
|
19
4
|
import { renderLoadedDefinition, renderLoadResult } from "./render.js";
|
|
20
|
-
import {
|
|
5
|
+
import { TOOL_LOAD_NAME } from "./source.js";
|
|
21
6
|
/** Names this plugin owns; a deployment cannot shadow them. */
|
|
22
|
-
export
|
|
7
|
+
export { TOOL_LOAD_NAME } from "./source.js";
|
|
23
8
|
/**
|
|
24
9
|
* Create the `tool_load` definition.
|
|
25
|
-
* @param ctx - plugin context, used to read the agent's visible tools.
|
|
26
10
|
* @param options - source kind, limits, and the agent's declaration facts.
|
|
27
11
|
* @returns the unregistered definition.
|
|
28
12
|
*/
|
|
29
|
-
export function createToolLoad(
|
|
13
|
+
export function createToolLoad(options) {
|
|
14
|
+
const { defineTool, projectToolSchema, ToolContextUnavailableError, createUserMessage } = options.host;
|
|
30
15
|
return defineTool({
|
|
31
16
|
name: TOOL_LOAD_NAME,
|
|
32
|
-
description: '
|
|
17
|
+
description: '加载会话中已公布的工具完整定义,传入工具原名。定义在本次调用结果之后保存;Responses 路由从该历史位置追加原生声明,后续可按原名调用。',
|
|
33
18
|
parameters: {
|
|
34
19
|
names: {
|
|
35
20
|
type: 'array',
|
|
@@ -66,8 +51,8 @@ export function createToolLoad(ctx, options) {
|
|
|
66
51
|
const alreadyAvailable = [];
|
|
67
52
|
const unavailable = [];
|
|
68
53
|
const tooLarge = [];
|
|
69
|
-
const definitions = new Map(
|
|
70
|
-
|
|
54
|
+
const definitions = new Map((options.admitted(agent) ?? []).map(definition => [definition.name, definition]));
|
|
55
|
+
const state = options.history.read(agent, strategy);
|
|
71
56
|
const wanted = [...new Set(args.names.map(name => name.trim()).filter(name => name.length > 0))];
|
|
72
57
|
for (const name of wanted) {
|
|
73
58
|
const definition = definitions.get(name);
|
|
@@ -75,8 +60,9 @@ export function createToolLoad(ctx, options) {
|
|
|
75
60
|
unavailable.push(name);
|
|
76
61
|
continue;
|
|
77
62
|
}
|
|
78
|
-
|
|
79
|
-
|
|
63
|
+
const schema = projectToolSchema(definition, true);
|
|
64
|
+
const known = state.definitions.get(name);
|
|
65
|
+
if ((strategy !== 'responses' && declared.has(name)) || (known && toolSignature(known) === toolSignature(schema))) {
|
|
80
66
|
alreadyAvailable.push(name);
|
|
81
67
|
continue;
|
|
82
68
|
}
|
|
@@ -86,19 +72,16 @@ export function createToolLoad(ctx, options) {
|
|
|
86
72
|
unavailable.push(name);
|
|
87
73
|
continue;
|
|
88
74
|
}
|
|
89
|
-
|
|
90
|
-
if (schemaBytes(schema) > options.limits.maxSchemaBytes) {
|
|
75
|
+
if (schemaBytes(schema) > options.limits.maxSchemaBytes || (strategy === 'responses' && !canLoadNative(schema, options.limits))) {
|
|
91
76
|
tooLarge.push(name);
|
|
92
77
|
continue;
|
|
93
78
|
}
|
|
94
79
|
loaded.push(name);
|
|
95
|
-
|
|
96
|
-
exec.deferContext(createUserMessage({
|
|
80
|
+
exec.deferContext(strategy === 'responses' ? additionMessage(options.host, [schema]) : createUserMessage({
|
|
97
81
|
content: [{ type: 'text', text: renderLoadedDefinition(schema, strategy) }],
|
|
98
82
|
source: { kind: options.sourceKind, form: 'catalog' },
|
|
99
83
|
}));
|
|
100
84
|
}
|
|
101
|
-
storeState(agent, state);
|
|
102
85
|
return { loaded, alreadyAvailable, unavailable, tooLarge };
|
|
103
86
|
},
|
|
104
87
|
isConcurrencySafe: () => false,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsh-plugin/dsh-dynamic-tools",
|
|
3
|
-
"version": "0.1.0-dev.
|
|
3
|
+
"version": "0.1.0-dev.43800186861",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Keeps the tool declaration head of a request frozen and discloses tools registered later as trailing context, so mid-conversation tool changes stop invalidating the cached prompt prefix; routes that call only declared tools load those definitions on demand and get them declared instead.",
|
|
6
6
|
"exports": {
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"default": "./lib/index.js"
|
|
10
10
|
},
|
|
11
11
|
"./locale/*.json": "./locale/*.json",
|
|
12
|
-
"./icon": "./icon.svg"
|
|
12
|
+
"./icon": "./icon.svg",
|
|
13
|
+
"./package.json": "./package.json"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"lib/**/*.js",
|
|
@@ -25,6 +26,7 @@
|
|
|
25
26
|
"scripts": {
|
|
26
27
|
"build": "tsc -p tsconfig.json",
|
|
27
28
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
29
|
+
"pretest": "npm run build",
|
|
28
30
|
"test": "node --test \"tests/*.test.ts\"",
|
|
29
31
|
"prepack": "npm run build"
|
|
30
32
|
},
|
|
@@ -51,7 +53,7 @@
|
|
|
51
53
|
"@deepseek-ai/dsh-session": "^0.2.2-rc.2",
|
|
52
54
|
"@deepseek-ai/dsh-system-prompt": "^0.2.2-rc.2",
|
|
53
55
|
"@deepseek-ai/dsh-tools": "^0.2.2-rc.2",
|
|
54
|
-
"@
|
|
56
|
+
"@dsh-plugin/dsh-loader": "^2.0.0-dev.43799314028"
|
|
55
57
|
},
|
|
56
58
|
"devDependencies": {
|
|
57
59
|
"@deepseek-ai/cordis": "4.1.0",
|
|
@@ -61,15 +63,19 @@
|
|
|
61
63
|
"@deepseek-ai/dsh-session": "0.2.2-rc.2",
|
|
62
64
|
"@deepseek-ai/dsh-system-prompt": "0.2.2-rc.2",
|
|
63
65
|
"@deepseek-ai/dsh-tools": "0.2.2-rc.2",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
+
"typescript": "5.9.3",
|
|
67
|
+
"@dsh-plugin/dsh-loader": "2.0.0-dev.43799314028"
|
|
66
68
|
},
|
|
67
69
|
"engines": {
|
|
68
|
-
"dsh": ">=0.2.2-rc.2 <0.3.0"
|
|
70
|
+
"dsh": ">=0.2.2-rc.2 <0.3.0",
|
|
71
|
+
"node": ">=24.9.0"
|
|
69
72
|
},
|
|
70
73
|
"dsh": {
|
|
71
74
|
"bundle": {
|
|
72
75
|
"patch": "./cordis.patch.yml"
|
|
73
76
|
}
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"schemastery": "^3.18.0"
|
|
74
80
|
}
|
|
75
81
|
}
|