@huyooo/ai-chat-core 0.3.6 → 0.3.8
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/dist/adapter/index.d.ts +0 -1
- package/dist/adapter/model-adapter.d.ts +0 -1
- package/dist/adapter/model-options.d.ts +0 -1
- package/dist/adapter/types.d.ts +0 -1
- package/dist/chat-runtime.d.ts +0 -1
- package/dist/constants.d.ts +0 -1
- package/dist/events.d.ts +0 -1
- package/dist/extension/index.d.ts +0 -1
- package/dist/extension/types.d.ts +0 -1
- package/dist/families/index.d.ts +0 -1
- package/dist/families/presets.d.ts +0 -1
- package/dist/families/resolver.d.ts +0 -1
- package/dist/families/types.d.ts +0 -1
- package/dist/governance/command-safety.d.ts +0 -1
- package/dist/governance/governance.d.ts +0 -1
- package/dist/governance/index.d.ts +0 -1
- package/dist/governance/types.d.ts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/internal/management-args.d.ts +0 -1
- package/dist/internal/management-results.d.ts +0 -1
- package/dist/llm-config.d.ts +0 -1
- package/dist/logger/core.d.ts +0 -1
- package/dist/logger/index.d.ts +0 -1
- package/dist/orchestrator/compression-handler.d.ts +0 -1
- package/dist/orchestrator/context-compressor.d.ts +0 -1
- package/dist/orchestrator/context-summarizer.d.ts +0 -1
- package/dist/orchestrator/index.d.ts +0 -1
- package/dist/orchestrator/orchestrator.d.ts +0 -1
- package/dist/orchestrator/types.d.ts +0 -1
- package/dist/parts/index.d.ts +0 -1
- package/dist/parts/registry.d.ts +0 -1
- package/dist/parts/summaries.d.ts +0 -1
- package/dist/parts/types.d.ts +0 -1
- package/dist/platform.d.ts +0 -1
- package/dist/protocols/anthropic.d.ts +0 -1
- package/dist/protocols/ark.d.ts +0 -1
- package/dist/protocols/deepseek.d.ts +0 -1
- package/dist/protocols/error-utils.d.ts +0 -1
- package/dist/protocols/gemini.d.ts +0 -1
- package/dist/protocols/glm.d.ts +0 -1
- package/dist/protocols/grok.d.ts +0 -1
- package/dist/protocols/index.d.ts +0 -1
- package/dist/protocols/minimax.d.ts +0 -1
- package/dist/protocols/moonshot.d.ts +0 -1
- package/dist/protocols/openai-sse.d.ts +0 -1
- package/dist/protocols/openai.d.ts +0 -1
- package/dist/protocols/qwen.d.ts +0 -1
- package/dist/protocols/responses-sse.d.ts +0 -1
- package/dist/protocols/sse-reader.d.ts +0 -1
- package/dist/protocols/tool-arguments.d.ts +0 -1
- package/dist/protocols/types.d.ts +0 -1
- package/dist/protocols/vercel-gateway.d.ts +0 -1
- package/dist/runtime.d.ts +0 -1
- package/dist/skills/index.d.ts +0 -1
- package/dist/skills/management/admin.d.ts +0 -1
- package/dist/skills/management/index.d.ts +0 -1
- package/dist/skills/management/inputs.d.ts +0 -1
- package/dist/skills/management/operations.d.ts +0 -1
- package/dist/skills/management/types.d.ts +0 -1
- package/dist/skills/registry.d.ts +0 -1
- package/dist/skills/summaries.d.ts +0 -1
- package/dist/skills/types.d.ts +0 -1
- package/dist/test-utils/mock-sse.d.ts +0 -1
- package/dist/tool-manager/define-tool.d.ts +0 -1
- package/dist/tool-manager/formats.d.ts +0 -1
- package/dist/tool-manager/identity.d.ts +0 -1
- package/dist/tool-manager/in-process-provider.d.ts +0 -1
- package/dist/tool-manager/index.d.ts +0 -1
- package/dist/tool-manager/manager.d.ts +0 -1
- package/dist/tool-manager/mcp-provider.d.ts +0 -1
- package/dist/tool-manager/summaries.d.ts +0 -1
- package/dist/tool-manager/types.d.ts +0 -1
- package/dist/types.d.ts +0 -1
- package/package.json +2 -3
- package/dist/adapter/index.d.ts.map +0 -1
- package/dist/adapter/model-adapter.d.ts.map +0 -1
- package/dist/adapter/model-options.d.ts.map +0 -1
- package/dist/adapter/types.d.ts.map +0 -1
- package/dist/chat-runtime.d.ts.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/events.d.ts.map +0 -1
- package/dist/extension/index.d.ts.map +0 -1
- package/dist/extension/types.d.ts.map +0 -1
- package/dist/families/index.d.ts.map +0 -1
- package/dist/families/presets.d.ts.map +0 -1
- package/dist/families/resolver.d.ts.map +0 -1
- package/dist/families/types.d.ts.map +0 -1
- package/dist/governance/command-safety.d.ts.map +0 -1
- package/dist/governance/governance.d.ts.map +0 -1
- package/dist/governance/index.d.ts.map +0 -1
- package/dist/governance/types.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/internal/management-args.d.ts.map +0 -1
- package/dist/internal/management-results.d.ts.map +0 -1
- package/dist/llm-config.d.ts.map +0 -1
- package/dist/logger/core.d.ts.map +0 -1
- package/dist/logger/index.d.ts.map +0 -1
- package/dist/orchestrator/compression-handler.d.ts.map +0 -1
- package/dist/orchestrator/context-compressor.d.ts.map +0 -1
- package/dist/orchestrator/context-summarizer.d.ts.map +0 -1
- package/dist/orchestrator/index.d.ts.map +0 -1
- package/dist/orchestrator/orchestrator.d.ts.map +0 -1
- package/dist/orchestrator/types.d.ts.map +0 -1
- package/dist/parts/index.d.ts.map +0 -1
- package/dist/parts/registry.d.ts.map +0 -1
- package/dist/parts/summaries.d.ts.map +0 -1
- package/dist/parts/types.d.ts.map +0 -1
- package/dist/platform.d.ts.map +0 -1
- package/dist/protocols/anthropic.d.ts.map +0 -1
- package/dist/protocols/ark.d.ts.map +0 -1
- package/dist/protocols/deepseek.d.ts.map +0 -1
- package/dist/protocols/error-utils.d.ts.map +0 -1
- package/dist/protocols/gemini.d.ts.map +0 -1
- package/dist/protocols/glm.d.ts.map +0 -1
- package/dist/protocols/grok.d.ts.map +0 -1
- package/dist/protocols/index.d.ts.map +0 -1
- package/dist/protocols/minimax.d.ts.map +0 -1
- package/dist/protocols/moonshot.d.ts.map +0 -1
- package/dist/protocols/openai-sse.d.ts.map +0 -1
- package/dist/protocols/openai.d.ts.map +0 -1
- package/dist/protocols/qwen.d.ts.map +0 -1
- package/dist/protocols/responses-sse.d.ts.map +0 -1
- package/dist/protocols/sse-reader.d.ts.map +0 -1
- package/dist/protocols/tool-arguments.d.ts.map +0 -1
- package/dist/protocols/types.d.ts.map +0 -1
- package/dist/protocols/vercel-gateway.d.ts.map +0 -1
- package/dist/runtime.d.ts.map +0 -1
- package/dist/skills/index.d.ts.map +0 -1
- package/dist/skills/management/admin.d.ts.map +0 -1
- package/dist/skills/management/index.d.ts.map +0 -1
- package/dist/skills/management/inputs.d.ts.map +0 -1
- package/dist/skills/management/operations.d.ts.map +0 -1
- package/dist/skills/management/types.d.ts.map +0 -1
- package/dist/skills/registry.d.ts.map +0 -1
- package/dist/skills/summaries.d.ts.map +0 -1
- package/dist/skills/types.d.ts.map +0 -1
- package/dist/test-utils/mock-sse.d.ts.map +0 -1
- package/dist/tool-manager/define-tool.d.ts.map +0 -1
- package/dist/tool-manager/formats.d.ts.map +0 -1
- package/dist/tool-manager/identity.d.ts.map +0 -1
- package/dist/tool-manager/in-process-provider.d.ts.map +0 -1
- package/dist/tool-manager/index.d.ts.map +0 -1
- package/dist/tool-manager/manager.d.ts.map +0 -1
- package/dist/tool-manager/mcp-provider.d.ts.map +0 -1
- package/dist/tool-manager/summaries.d.ts.map +0 -1
- package/dist/tool-manager/types.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/src/adapter/index.ts +0 -25
- package/src/adapter/model-adapter.ts +0 -196
- package/src/adapter/model-options.ts +0 -143
- package/src/adapter/types.ts +0 -41
- package/src/chat-runtime.ts +0 -515
- package/src/constants.ts +0 -32
- package/src/events.ts +0 -1084
- package/src/extension/index.ts +0 -24
- package/src/extension/types.ts +0 -49
- package/src/families/index.ts +0 -28
- package/src/families/presets.ts +0 -124
- package/src/families/resolver.ts +0 -22
- package/src/families/types.ts +0 -55
- package/src/governance/command-safety.ts +0 -224
- package/src/governance/governance.ts +0 -125
- package/src/governance/index.ts +0 -38
- package/src/governance/types.ts +0 -44
- package/src/index.ts +0 -426
- package/src/internal/management-args.ts +0 -39
- package/src/internal/management-results.ts +0 -60
- package/src/llm-config.ts +0 -137
- package/src/logger/core.ts +0 -96
- package/src/logger/index.ts +0 -8
- package/src/orchestrator/compression-handler.ts +0 -137
- package/src/orchestrator/context-compressor.ts +0 -249
- package/src/orchestrator/context-summarizer.ts +0 -123
- package/src/orchestrator/index.ts +0 -20
- package/src/orchestrator/orchestrator.ts +0 -1002
- package/src/orchestrator/types.ts +0 -70
- package/src/parts/index.ts +0 -20
- package/src/parts/registry.ts +0 -95
- package/src/parts/summaries.ts +0 -40
- package/src/parts/types.ts +0 -63
- package/src/platform.ts +0 -73
- package/src/protocols/anthropic.ts +0 -377
- package/src/protocols/ark.ts +0 -300
- package/src/protocols/deepseek.ts +0 -192
- package/src/protocols/error-utils.ts +0 -71
- package/src/protocols/gemini.ts +0 -352
- package/src/protocols/glm.ts +0 -212
- package/src/protocols/grok.ts +0 -98
- package/src/protocols/index.ts +0 -48
- package/src/protocols/minimax.ts +0 -308
- package/src/protocols/moonshot.ts +0 -186
- package/src/protocols/openai-sse.ts +0 -156
- package/src/protocols/openai.ts +0 -97
- package/src/protocols/qwen.ts +0 -358
- package/src/protocols/responses-sse.ts +0 -224
- package/src/protocols/sse-reader.ts +0 -54
- package/src/protocols/tool-arguments.ts +0 -32
- package/src/protocols/types.ts +0 -198
- package/src/protocols/vercel-gateway.ts +0 -391
- package/src/runtime.ts +0 -167
- package/src/skills/index.ts +0 -29
- package/src/skills/management/admin.ts +0 -170
- package/src/skills/management/index.ts +0 -27
- package/src/skills/management/inputs.ts +0 -79
- package/src/skills/management/operations.ts +0 -256
- package/src/skills/management/types.ts +0 -57
- package/src/skills/registry.ts +0 -120
- package/src/skills/summaries.ts +0 -48
- package/src/skills/types.ts +0 -65
- package/src/test-utils/mock-sse.ts +0 -32
- package/src/tool-manager/define-tool.ts +0 -201
- package/src/tool-manager/formats.ts +0 -146
- package/src/tool-manager/identity.ts +0 -80
- package/src/tool-manager/in-process-provider.ts +0 -164
- package/src/tool-manager/index.ts +0 -63
- package/src/tool-manager/manager.ts +0 -562
- package/src/tool-manager/mcp-provider.ts +0 -509
- package/src/tool-manager/summaries.ts +0 -136
- package/src/tool-manager/types.ts +0 -389
- package/src/types.ts +0 -1142
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* defineTool — TypeBox-only 的工具定义与执行封装
|
|
3
|
-
*
|
|
4
|
-
* - 编译 schema 缓存、外部 JSON Schema 参数校验
|
|
5
|
-
* - 统一 governance / UI 元数据到 Tool 类型
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { type Static, type TSchema, type TObject } from '@sinclair/typebox';
|
|
9
|
-
import { TypeCompiler, type TypeCheck } from '@sinclair/typebox/compiler';
|
|
10
|
-
import type {
|
|
11
|
-
AssetApprovalPolicy,
|
|
12
|
-
AssetHostDependency,
|
|
13
|
-
AssetSideEffectLevel,
|
|
14
|
-
} from '../governance/types';
|
|
15
|
-
import type { JsonSchemaObject, Tool, ToolContext, ToolExecuteResult, ToolGovernanceSnapshot, ToolUI } from '../types';
|
|
16
|
-
import { createResolvedOutputSchema, jsonSchemaToTypeBoxSchema, STANDARD_TOOL_ERROR_SCHEMA, throwToolError } from '../types';
|
|
17
|
-
import { registerToolFormats } from './formats';
|
|
18
|
-
|
|
19
|
-
// 编译后的 validator 使用 WeakMap 按 schema 对象缓存。
|
|
20
|
-
// 这样同一个 schema 只编译一次,后续工具调用直接复用,
|
|
21
|
-
// 同时又不会因为全局 Map 常驻而造成无界内存增长。
|
|
22
|
-
const validatorCache = new WeakMap<object, TypeCheck<TSchema>>();
|
|
23
|
-
// JSON Schema 需要先转成 TypeBox TObject,再编译 validator。
|
|
24
|
-
// 这里同样按 schema 对象缓存转换结果,避免重复构造。
|
|
25
|
-
const objectSchemaCache = new WeakMap<object, TObject>();
|
|
26
|
-
|
|
27
|
-
registerToolFormats();
|
|
28
|
-
|
|
29
|
-
export interface TypeBoxToolConfig<TSchema extends TObject> {
|
|
30
|
-
id?: string;
|
|
31
|
-
name: string;
|
|
32
|
-
alias?: string;
|
|
33
|
-
displayName?: string;
|
|
34
|
-
description: string;
|
|
35
|
-
inputSchema: TSchema;
|
|
36
|
-
/** 成功时 data 内的业务结构。defineTool 会生成标准 status/data/message 外围 resolvedOutputSchema。 */
|
|
37
|
-
outputSchema?: TObject;
|
|
38
|
-
/** 失败返回结构。不传时使用平台标准 ToolError schema。 */
|
|
39
|
-
errorSchema?: TObject;
|
|
40
|
-
ui?: ToolUI;
|
|
41
|
-
requiresApproval?: boolean;
|
|
42
|
-
approvalPolicy?: AssetApprovalPolicy;
|
|
43
|
-
sideEffectLevel?: AssetSideEffectLevel;
|
|
44
|
-
hostDependency?: AssetHostDependency;
|
|
45
|
-
assessInputGovernance?: (
|
|
46
|
-
args: Static<TSchema>,
|
|
47
|
-
) => ToolGovernanceSnapshot | Promise<ToolGovernanceSnapshot | undefined> | undefined;
|
|
48
|
-
category?: string;
|
|
49
|
-
tags?: string[];
|
|
50
|
-
timeout?: number;
|
|
51
|
-
execute: (args: Static<TSchema>, context: ToolContext) => Promise<ToolExecuteResult>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getCachedValidator<TSchemaValue extends TSchema>(schema: TSchemaValue): TypeCheck<TSchemaValue> {
|
|
55
|
-
let validator = validatorCache.get(schema);
|
|
56
|
-
if (!validator) {
|
|
57
|
-
validator = TypeCompiler.Compile(schema);
|
|
58
|
-
validatorCache.set(schema, validator);
|
|
59
|
-
}
|
|
60
|
-
return validator as TypeCheck<TSchemaValue>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function normalizeErrorPath(path: string): string {
|
|
64
|
-
return path.replace(/^\//, '').replace(/\//g, '.');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function formatValidationMessage(path: string, message: string): string {
|
|
68
|
-
return path ? `${path}: ${message}` : message;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function getJsonSchemaValidator(parameters: JsonSchemaObject): TypeCheck<TObject> {
|
|
72
|
-
let schema = objectSchemaCache.get(parameters);
|
|
73
|
-
if (!schema) {
|
|
74
|
-
schema = jsonSchemaToTypeBoxSchema(parameters as unknown as Record<string, unknown>) as TObject;
|
|
75
|
-
objectSchemaCache.set(parameters, schema);
|
|
76
|
-
}
|
|
77
|
-
return getCachedValidator(schema);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function throwValidationError(messages: string[]): never {
|
|
81
|
-
const message = messages.length > 0
|
|
82
|
-
? messages.join('; ')
|
|
83
|
-
: '输入不符合工具参数定义';
|
|
84
|
-
|
|
85
|
-
throwToolError({
|
|
86
|
-
message: `参数校验失败: ${message}`,
|
|
87
|
-
code: 'INVALID_PARAMS',
|
|
88
|
-
suggestion: '请根据工具的 inputSchema 修正参数类型和结构后重试',
|
|
89
|
-
retryable: true,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function validateTypeBoxArgs<TSchemaValue extends TObject>(
|
|
94
|
-
inputSchema: TSchemaValue,
|
|
95
|
-
rawArgs: Record<string, unknown>,
|
|
96
|
-
): Static<TSchemaValue> {
|
|
97
|
-
const validator = getCachedValidator(inputSchema);
|
|
98
|
-
if (validator.Check(rawArgs)) {
|
|
99
|
-
return rawArgs as Static<TSchemaValue>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const messages = Array.from(validator.Errors(rawArgs), error =>
|
|
103
|
-
formatValidationMessage(normalizeErrorPath(error.path), error.message),
|
|
104
|
-
);
|
|
105
|
-
throwValidationError(messages);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function validateJsonSchemaArgs(
|
|
109
|
-
parameters: JsonSchemaObject,
|
|
110
|
-
rawArgs: Record<string, unknown>,
|
|
111
|
-
): Record<string, unknown> {
|
|
112
|
-
const validator = getJsonSchemaValidator(parameters);
|
|
113
|
-
if (validator.Check(rawArgs)) {
|
|
114
|
-
return rawArgs;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const messages = Array.from(validator.Errors(rawArgs), error =>
|
|
118
|
-
formatValidationMessage(normalizeErrorPath(error.path), error.message),
|
|
119
|
-
);
|
|
120
|
-
throwValidationError(messages);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function getToolParameters<TSchemaValue extends TObject>(
|
|
124
|
-
config: TypeBoxToolConfig<TSchemaValue>,
|
|
125
|
-
): JsonSchemaObject {
|
|
126
|
-
return config.inputSchema as unknown as JsonSchemaObject;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function getDeclaredOutputSchema<TSchemaValue extends TObject>(
|
|
130
|
-
config: TypeBoxToolConfig<TSchemaValue>,
|
|
131
|
-
): JsonSchemaObject | undefined {
|
|
132
|
-
return config.outputSchema as unknown as JsonSchemaObject | undefined;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function getResolvedOutputSchema<TSchemaValue extends TObject>(
|
|
136
|
-
config: TypeBoxToolConfig<TSchemaValue>,
|
|
137
|
-
): JsonSchemaObject | undefined {
|
|
138
|
-
const outputSchema = getDeclaredOutputSchema(config);
|
|
139
|
-
if (outputSchema) return createResolvedOutputSchema(outputSchema);
|
|
140
|
-
return undefined;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function validateToolArgs<TSchemaValue extends TObject>(
|
|
144
|
-
config: TypeBoxToolConfig<TSchemaValue>,
|
|
145
|
-
rawArgs: Record<string, unknown>,
|
|
146
|
-
): Static<TSchemaValue> {
|
|
147
|
-
return validateTypeBoxArgs(config.inputSchema, rawArgs);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function defineTool<TSchemaValue extends TObject>(
|
|
151
|
-
config: TypeBoxToolConfig<TSchemaValue>,
|
|
152
|
-
): Tool {
|
|
153
|
-
if ('parameters' in (config as unknown as Record<string, unknown>)) {
|
|
154
|
-
throw new Error('defineTool 只支持 TypeBox inputSchema;不要使用 parameters JSON Schema');
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const parameters = getToolParameters(config);
|
|
158
|
-
|
|
159
|
-
return {
|
|
160
|
-
/** 稳定唯一 id(可选):注册表、遥测、去重或与外部系统对齐时用;缺省可由宿主按 name 生成 */
|
|
161
|
-
id: config.id,
|
|
162
|
-
/** 工具名:模型 function/tool 调用里使用的标识,需与宿主注册名一致 */
|
|
163
|
-
name: config.name,
|
|
164
|
-
/** 别名(可选):兼容旧名称或同一实现的另一入口 */
|
|
165
|
-
alias: config.alias,
|
|
166
|
-
/** 展示名(可选):UI/日志里给人看的短标题,不必与 name 相同 */
|
|
167
|
-
displayName: config.displayName,
|
|
168
|
-
/** 自然语言说明:写入模型上下文,描述工具用途、约束与典型用法 */
|
|
169
|
-
description: config.description,
|
|
170
|
-
/** 参数 JSON Schema:供模型与表单生成入参 */
|
|
171
|
-
parameters,
|
|
172
|
-
/** 成功时 data 内的业务 JSON Schema:供摘要、检索与资产元数据使用 */
|
|
173
|
-
outputSchema: getDeclaredOutputSchema(config),
|
|
174
|
-
/** 成功返回标准信封 JSON Schema:供模型在调用前理解完整结果结构 */
|
|
175
|
-
resolvedOutputSchema: getResolvedOutputSchema(config),
|
|
176
|
-
/** 失败返回 JSON Schema:默认使用平台标准错误信封 */
|
|
177
|
-
errorSchema: (config.errorSchema as unknown as JsonSchemaObject | undefined) ?? STANDARD_TOOL_ERROR_SCHEMA,
|
|
178
|
-
/** 结构化 UI 元数据(可选):如结果卡片类型、字段映射、前端 Part 渲染提示 */
|
|
179
|
-
ui: config.ui,
|
|
180
|
-
/** 是否必须经用户显式批准后才执行(与 approvalPolicy 二选一或组合,依宿主策略) */
|
|
181
|
-
requiresApproval: config.requiresApproval,
|
|
182
|
-
/** 审批策略:auto / 始终人工 / 条件人工 等,由治理层与宿主解释 */
|
|
183
|
-
approvalPolicy: config.approvalPolicy,
|
|
184
|
-
/** 副作用级别:read / write / destructive 等,用于风险提示与默认审批策略 */
|
|
185
|
-
sideEffectLevel: config.sideEffectLevel,
|
|
186
|
-
/** 宿主依赖:是否需要网络、本地 OS、特定沙箱能力等,供环境检查与提示 */
|
|
187
|
-
hostDependency: config.hostDependency,
|
|
188
|
-
/** 执行前治理快照(可选):对「已校验」的参数做风险分级等,供 UI 徽章与审批流 */
|
|
189
|
-
assessInputGovernance: config.assessInputGovernance
|
|
190
|
-
? async (rawArgs) => config.assessInputGovernance?.(validateToolArgs(config, rawArgs))
|
|
191
|
-
: undefined,
|
|
192
|
-
/** 分类(可选):设置页、工具市场或列表分组 */
|
|
193
|
-
category: config.category,
|
|
194
|
-
/** 标签(可选):检索、过滤或能力画像 */
|
|
195
|
-
tags: config.tags,
|
|
196
|
-
/** 单次执行超时毫秒(可选):超时后宿主应中止并返回可重试错误 */
|
|
197
|
-
timeout: config.timeout,
|
|
198
|
-
/** 实际执行:先将 rawArgs 用声明的 schema 校验,再传入业务 execute */
|
|
199
|
-
execute: async (rawArgs, context) => config.execute(validateToolArgs(config, rawArgs), context),
|
|
200
|
-
};
|
|
201
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 工具参数 TypeBox 自定义 format 注册表
|
|
3
|
-
*
|
|
4
|
-
* - TOOL_FORMATS:email / uuid / url 等常用约束名
|
|
5
|
-
* - 与 defineTool 中的 schema 编译共用
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { FormatRegistry } from '@sinclair/typebox';
|
|
9
|
-
|
|
10
|
-
export const TOOL_FORMATS = {
|
|
11
|
-
email: 'email',
|
|
12
|
-
uuidAny: 'uuid-any',
|
|
13
|
-
cuid: 'cuid',
|
|
14
|
-
cuid2: 'cuid2',
|
|
15
|
-
ulid: 'ulid',
|
|
16
|
-
nanoid: 'nanoid',
|
|
17
|
-
url: 'url',
|
|
18
|
-
uri: 'uri',
|
|
19
|
-
uuid: 'uuid',
|
|
20
|
-
ipv4: 'ipv4',
|
|
21
|
-
ipv6: 'ipv6',
|
|
22
|
-
ip: 'ip',
|
|
23
|
-
cidr: 'cidr',
|
|
24
|
-
hostname: 'hostname',
|
|
25
|
-
date: 'date',
|
|
26
|
-
time: 'time',
|
|
27
|
-
dateTime: 'date-time',
|
|
28
|
-
datetime: 'datetime',
|
|
29
|
-
duration: 'duration',
|
|
30
|
-
phone: 'phone',
|
|
31
|
-
phoneCn: 'phone-cn',
|
|
32
|
-
phoneE164: 'phone-e164',
|
|
33
|
-
objectId: 'objectid',
|
|
34
|
-
slug: 'slug',
|
|
35
|
-
base64: 'base64',
|
|
36
|
-
base64url: 'base64url',
|
|
37
|
-
jwt: 'jwt',
|
|
38
|
-
hexColor: 'hex-color',
|
|
39
|
-
rgbColor: 'rgb-color',
|
|
40
|
-
color: 'color',
|
|
41
|
-
emoji: 'emoji',
|
|
42
|
-
semver: 'semver',
|
|
43
|
-
creditCard: 'credit-card',
|
|
44
|
-
} as const;
|
|
45
|
-
|
|
46
|
-
export type ToolFormat = typeof TOOL_FORMATS[keyof typeof TOOL_FORMATS];
|
|
47
|
-
|
|
48
|
-
const EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
49
|
-
const UUID_ANY_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
50
|
-
const CUID_REGEX = /^c[^\s-]{8,}$/i;
|
|
51
|
-
const CUID2_REGEX = /^[0-9a-z]+$/;
|
|
52
|
-
const ULID_REGEX = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
53
|
-
const NANOID_REGEX = /^[A-Za-z0-9_-]{21}$/;
|
|
54
|
-
const URL_REGEX = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,63}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*$/;
|
|
55
|
-
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
56
|
-
const IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
57
|
-
const IPV6_REGEX = /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,7}:$|^(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}$|^(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}$|^(?:[0-9a-fA-F]{1,3}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}$|^(?:[0-9a-fA-F]{1,2}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,6}$/;
|
|
58
|
-
const CIDR_REGEX = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/;
|
|
59
|
-
const HOSTNAME_REGEX = /^(?=.{1,253}$)(?:(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)*(?!-)[a-zA-Z0-9-]{1,63}(?<!-)$/;
|
|
60
|
-
const DATE_REGEX = /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$/;
|
|
61
|
-
const TIME_REGEX = /^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\.\d{1,3})?$/;
|
|
62
|
-
const DATE_TIME_REGEX = /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\.\d{1,3})?(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])?$/;
|
|
63
|
-
const DURATION_REGEX = /^P(?:\d+Y)?(?:\d+M)?(?:\d+W)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?$/;
|
|
64
|
-
const PHONE_CN_REGEX = /^1[3-9]\d{9}$/;
|
|
65
|
-
const PHONE_E164_REGEX = /^\+[1-9]\d{6,14}$/;
|
|
66
|
-
const OBJECTID_REGEX = /^[0-9a-fA-F]{24}$/;
|
|
67
|
-
const HEX_COLOR_REGEX = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
|
|
68
|
-
const RGB_COLOR_REGEX = /^rgba?\(\s*(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s*,\s*(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s*,\s*(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)$/;
|
|
69
|
-
const BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
70
|
-
const BASE64URL_REGEX = /^[A-Za-z0-9_-]+$/;
|
|
71
|
-
const JWT_REGEX = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
|
|
72
|
-
const EMOJI_REGEX = /^[\p{Emoji}]+$/u;
|
|
73
|
-
const SLUG_REGEX = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
74
|
-
const SEMVER_REGEX = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
75
|
-
|
|
76
|
-
let registered = false;
|
|
77
|
-
|
|
78
|
-
function isValidCreditCard(value: string): boolean {
|
|
79
|
-
const digits = value.replace(/\D/g, '');
|
|
80
|
-
if (digits.length < 13 || digits.length > 19) return false;
|
|
81
|
-
|
|
82
|
-
let sum = 0;
|
|
83
|
-
let isEven = false;
|
|
84
|
-
|
|
85
|
-
for (let i = digits.length - 1; i >= 0; i--) {
|
|
86
|
-
let digit = parseInt(digits[i], 10);
|
|
87
|
-
if (isEven) {
|
|
88
|
-
digit *= 2;
|
|
89
|
-
if (digit > 9) digit -= 9;
|
|
90
|
-
}
|
|
91
|
-
sum += digit;
|
|
92
|
-
isEven = !isEven;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return sum % 10 === 0;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function registerToolFormats(): void {
|
|
99
|
-
if (registered) return;
|
|
100
|
-
registered = true;
|
|
101
|
-
|
|
102
|
-
FormatRegistry.Set(TOOL_FORMATS.email, value => EMAIL_REGEX.test(value));
|
|
103
|
-
FormatRegistry.Set(TOOL_FORMATS.uuidAny, value => UUID_ANY_REGEX.test(value));
|
|
104
|
-
FormatRegistry.Set(TOOL_FORMATS.cuid, value => CUID_REGEX.test(value));
|
|
105
|
-
FormatRegistry.Set(TOOL_FORMATS.cuid2, value => CUID2_REGEX.test(value) && value.length >= 1);
|
|
106
|
-
FormatRegistry.Set(TOOL_FORMATS.ulid, value => ULID_REGEX.test(value));
|
|
107
|
-
FormatRegistry.Set(TOOL_FORMATS.nanoid, value => NANOID_REGEX.test(value));
|
|
108
|
-
FormatRegistry.Set(TOOL_FORMATS.url, value => URL_REGEX.test(value));
|
|
109
|
-
FormatRegistry.Set(TOOL_FORMATS.uri, value => URL_REGEX.test(value));
|
|
110
|
-
FormatRegistry.Set(TOOL_FORMATS.uuid, value => UUID_REGEX.test(value));
|
|
111
|
-
FormatRegistry.Set(TOOL_FORMATS.ipv4, value => IPV4_REGEX.test(value));
|
|
112
|
-
FormatRegistry.Set(TOOL_FORMATS.ipv6, value => IPV6_REGEX.test(value));
|
|
113
|
-
FormatRegistry.Set(TOOL_FORMATS.ip, value => IPV4_REGEX.test(value) || IPV6_REGEX.test(value));
|
|
114
|
-
FormatRegistry.Set(TOOL_FORMATS.cidr, value => CIDR_REGEX.test(value));
|
|
115
|
-
FormatRegistry.Set(TOOL_FORMATS.hostname, value => HOSTNAME_REGEX.test(value));
|
|
116
|
-
FormatRegistry.Set(TOOL_FORMATS.date, value => DATE_REGEX.test(value));
|
|
117
|
-
FormatRegistry.Set(TOOL_FORMATS.time, value => TIME_REGEX.test(value));
|
|
118
|
-
FormatRegistry.Set(TOOL_FORMATS.dateTime, value => DATE_TIME_REGEX.test(value));
|
|
119
|
-
FormatRegistry.Set(TOOL_FORMATS.datetime, value => DATE_TIME_REGEX.test(value));
|
|
120
|
-
FormatRegistry.Set(TOOL_FORMATS.duration, value => DURATION_REGEX.test(value));
|
|
121
|
-
FormatRegistry.Set(TOOL_FORMATS.phone, value => PHONE_CN_REGEX.test(value));
|
|
122
|
-
FormatRegistry.Set(TOOL_FORMATS.phoneCn, value => PHONE_CN_REGEX.test(value));
|
|
123
|
-
FormatRegistry.Set(TOOL_FORMATS.phoneE164, value => PHONE_E164_REGEX.test(value));
|
|
124
|
-
FormatRegistry.Set(TOOL_FORMATS.objectId, value => OBJECTID_REGEX.test(value));
|
|
125
|
-
FormatRegistry.Set(TOOL_FORMATS.slug, value => SLUG_REGEX.test(value));
|
|
126
|
-
FormatRegistry.Set(TOOL_FORMATS.base64, value => BASE64_REGEX.test(value));
|
|
127
|
-
FormatRegistry.Set(TOOL_FORMATS.base64url, value => BASE64URL_REGEX.test(value));
|
|
128
|
-
FormatRegistry.Set(TOOL_FORMATS.jwt, value => JWT_REGEX.test(value));
|
|
129
|
-
FormatRegistry.Set(TOOL_FORMATS.hexColor, value => HEX_COLOR_REGEX.test(value));
|
|
130
|
-
FormatRegistry.Set(TOOL_FORMATS.rgbColor, value => RGB_COLOR_REGEX.test(value));
|
|
131
|
-
FormatRegistry.Set(TOOL_FORMATS.color, value => HEX_COLOR_REGEX.test(value) || RGB_COLOR_REGEX.test(value));
|
|
132
|
-
FormatRegistry.Set(TOOL_FORMATS.emoji, value => EMOJI_REGEX.test(value));
|
|
133
|
-
FormatRegistry.Set(TOOL_FORMATS.semver, value => SEMVER_REGEX.test(value));
|
|
134
|
-
FormatRegistry.Set(TOOL_FORMATS.creditCard, isValidCreditCard);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export function registerToolFormat(
|
|
138
|
-
name: string,
|
|
139
|
-
validator: (value: string) => boolean,
|
|
140
|
-
): void {
|
|
141
|
-
FormatRegistry.Set(name, validator);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function hasToolFormat(name: string): boolean {
|
|
145
|
-
return FormatRegistry.Has(name);
|
|
146
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 工具在运行时管理器中的命名空间与 identity
|
|
3
|
-
*
|
|
4
|
-
* 两段式 scoped identity:assetId / name
|
|
5
|
-
* - assetId 有值 → scoped 工具:id = source:assetId:name, alias = assetId__name
|
|
6
|
-
* - assetId 无值 → 独立工具:id = source:name, alias = name
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { STANDARD_TOOL_ERROR_SCHEMA, createResolvedOutputSchema, type Tool } from '../types';
|
|
10
|
-
import type { AssetSource } from '../governance/types';
|
|
11
|
-
import type { ToolDescriptor } from './types';
|
|
12
|
-
|
|
13
|
-
function sanitizeAliasPart(value: string): string {
|
|
14
|
-
return value.replace(/[^a-zA-Z0-9_]/g, '_');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function toScopedIdentity(tool: Tool, _providerId: string, source: AssetSource) {
|
|
18
|
-
const hasScope = !!tool.assetId;
|
|
19
|
-
const assetId = sanitizeAliasPart(tool.assetId ?? tool.name);
|
|
20
|
-
const sanitizedName = sanitizeAliasPart(tool.name);
|
|
21
|
-
const alias = tool.alias
|
|
22
|
-
?? (hasScope ? `${assetId}__${sanitizedName}` : assetId);
|
|
23
|
-
const id = tool.id
|
|
24
|
-
?? (hasScope ? `${source}:${assetId}:${sanitizedName}` : `${source}:${assetId}`);
|
|
25
|
-
|
|
26
|
-
return { id, assetId, alias };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function createToolDescriptor(
|
|
30
|
-
tool: Tool,
|
|
31
|
-
providerId: string,
|
|
32
|
-
source: AssetSource,
|
|
33
|
-
meta?: { category?: string; tags?: string[] },
|
|
34
|
-
): ToolDescriptor {
|
|
35
|
-
const scoped = toScopedIdentity(tool, providerId, source);
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
id: scoped.id,
|
|
39
|
-
assetId: scoped.assetId,
|
|
40
|
-
name: tool.name,
|
|
41
|
-
alias: scoped.alias,
|
|
42
|
-
displayName: tool.displayName ?? tool.name,
|
|
43
|
-
description: tool.description,
|
|
44
|
-
parameters: tool.parameters,
|
|
45
|
-
outputSchema: tool.outputSchema,
|
|
46
|
-
resolvedOutputSchema: tool.resolvedOutputSchema ?? (tool.outputSchema ? createResolvedOutputSchema(tool.outputSchema) : undefined),
|
|
47
|
-
errorSchema: tool.errorSchema ?? STANDARD_TOOL_ERROR_SCHEMA,
|
|
48
|
-
category: meta?.category ?? tool.category ?? 'general',
|
|
49
|
-
tags: meta?.tags ?? tool.tags ?? [],
|
|
50
|
-
source,
|
|
51
|
-
provider: providerId,
|
|
52
|
-
ui: tool.ui,
|
|
53
|
-
requiresApproval: tool.requiresApproval,
|
|
54
|
-
approvalPolicy: tool.approvalPolicy,
|
|
55
|
-
sideEffectLevel: tool.sideEffectLevel,
|
|
56
|
-
hostDependency: tool.hostDependency,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function matchesToolDescriptorRef(descriptor: ToolDescriptor, ref: string): boolean {
|
|
61
|
-
return descriptor.id === ref || descriptor.alias === ref || (!descriptor.assetId || descriptor.assetId === descriptor.name ? descriptor.assetId === ref : false);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function getToolIdentityKey(tool: Pick<Tool, 'id' | 'name' | 'assetId'>): string {
|
|
65
|
-
return tool.id
|
|
66
|
-
?? (tool.assetId
|
|
67
|
-
? `${tool.assetId}:${tool.name}`
|
|
68
|
-
: tool.name);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function createAliasedTool(tool: Tool, descriptor: ToolDescriptor): Tool {
|
|
72
|
-
return {
|
|
73
|
-
...tool,
|
|
74
|
-
id: descriptor.id,
|
|
75
|
-
assetId: descriptor.assetId,
|
|
76
|
-
alias: descriptor.alias,
|
|
77
|
-
displayName: descriptor.displayName,
|
|
78
|
-
name: descriptor.alias,
|
|
79
|
-
};
|
|
80
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* InProcessProvider — 进程内工具提供者
|
|
3
|
-
*
|
|
4
|
-
* 管理所有在主进程中直接运行的工具(builtin + user-created)。
|
|
5
|
-
*
|
|
6
|
-
* 核心能力:
|
|
7
|
-
* - 即时注册/卸载/热替换工具
|
|
8
|
-
* - 变更事件通知
|
|
9
|
-
* - 描述符缓存(避免频繁重建)
|
|
10
|
-
* - 每个工具独立 source 标记
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { Tool } from '../types';
|
|
14
|
-
import type { AssetSource } from '../governance/types';
|
|
15
|
-
import type {
|
|
16
|
-
InProcessProvider,
|
|
17
|
-
ToolDescriptor,
|
|
18
|
-
ToolChangeListener,
|
|
19
|
-
ToolChangeEvent,
|
|
20
|
-
} from './types';
|
|
21
|
-
import { createToolDescriptor, matchesToolDescriptorRef } from './identity';
|
|
22
|
-
|
|
23
|
-
/** 内部存储:工具 + 元数据 */
|
|
24
|
-
interface StoredTool {
|
|
25
|
-
tool: Tool;
|
|
26
|
-
source: AssetSource;
|
|
27
|
-
category?: string;
|
|
28
|
-
tags?: string[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function createInProcessProvider(
|
|
32
|
-
providerId: string,
|
|
33
|
-
defaultSource: AssetSource = 'bundled',
|
|
34
|
-
): InProcessProvider {
|
|
35
|
-
const store = new Map<string, StoredTool>();
|
|
36
|
-
const listeners = new Set<ToolChangeListener>();
|
|
37
|
-
let cachedDescriptors: ToolDescriptor[] = [];
|
|
38
|
-
|
|
39
|
-
function emit(event: ToolChangeEvent) {
|
|
40
|
-
for (const fn of listeners) fn(event);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function toDescriptor(stored: StoredTool): ToolDescriptor {
|
|
44
|
-
return createToolDescriptor(stored.tool, providerId, stored.source, {
|
|
45
|
-
category: stored.category,
|
|
46
|
-
tags: stored.tags,
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function getStoredKey(stored: StoredTool): string {
|
|
51
|
-
return toDescriptor(stored).id;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getStoredByRef(ref: string): StoredTool | undefined {
|
|
55
|
-
for (const stored of store.values()) {
|
|
56
|
-
if (matchesToolDescriptorRef(toDescriptor(stored), ref)) return stored;
|
|
57
|
-
}
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function getStoredEntryByRef(ref: string): [string, StoredTool] | undefined {
|
|
62
|
-
for (const entry of store.entries()) {
|
|
63
|
-
if (matchesToolDescriptorRef(toDescriptor(entry[1]), ref)) return entry;
|
|
64
|
-
}
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function rebuildCache() {
|
|
69
|
-
cachedDescriptors = Array.from(store.values()).map(toDescriptor);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const provider: InProcessProvider = {
|
|
73
|
-
id: providerId,
|
|
74
|
-
source: defaultSource,
|
|
75
|
-
|
|
76
|
-
async init() {
|
|
77
|
-
// InProcessProvider 无异步初始化
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
async destroy() {
|
|
81
|
-
store.clear();
|
|
82
|
-
cachedDescriptors = [];
|
|
83
|
-
listeners.clear();
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
getDescriptors(): ToolDescriptor[] {
|
|
87
|
-
return cachedDescriptors.slice();
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
async loadTool(ref: string): Promise<Tool | null> {
|
|
91
|
-
return getStoredByRef(ref)?.tool ?? null;
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
addChangeListener(listener: ToolChangeListener) {
|
|
95
|
-
listeners.add(listener);
|
|
96
|
-
},
|
|
97
|
-
|
|
98
|
-
removeChangeListener(listener: ToolChangeListener) {
|
|
99
|
-
listeners.delete(listener);
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
register(tool: Tool, meta?: { category?: string; tags?: string[]; source?: AssetSource }) {
|
|
103
|
-
const stored: StoredTool = {
|
|
104
|
-
tool,
|
|
105
|
-
source: meta?.source ?? defaultSource,
|
|
106
|
-
category: meta?.category,
|
|
107
|
-
tags: meta?.tags,
|
|
108
|
-
};
|
|
109
|
-
store.set(getStoredKey(stored), stored);
|
|
110
|
-
rebuildCache();
|
|
111
|
-
emit({ type: 'added', tools: [toDescriptor(stored)], provider: providerId });
|
|
112
|
-
},
|
|
113
|
-
|
|
114
|
-
registerAll(toolList: Tool[], meta?: { category?: string; tags?: string[]; source?: AssetSource }) {
|
|
115
|
-
const descriptors: ToolDescriptor[] = [];
|
|
116
|
-
for (const tool of toolList) {
|
|
117
|
-
const stored: StoredTool = {
|
|
118
|
-
tool,
|
|
119
|
-
source: meta?.source ?? defaultSource,
|
|
120
|
-
category: meta?.category,
|
|
121
|
-
tags: meta?.tags,
|
|
122
|
-
};
|
|
123
|
-
store.set(getStoredKey(stored), stored);
|
|
124
|
-
descriptors.push(toDescriptor(stored));
|
|
125
|
-
}
|
|
126
|
-
rebuildCache();
|
|
127
|
-
if (descriptors.length > 0) {
|
|
128
|
-
emit({ type: 'added', tools: descriptors, provider: providerId });
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
|
|
132
|
-
unregister(ref: string) {
|
|
133
|
-
const entry = getStoredEntryByRef(ref);
|
|
134
|
-
if (!entry) return;
|
|
135
|
-
const [key, stored] = entry;
|
|
136
|
-
store.delete(key);
|
|
137
|
-
rebuildCache();
|
|
138
|
-
emit({ type: 'removed', tools: [toDescriptor(stored)], provider: providerId });
|
|
139
|
-
},
|
|
140
|
-
|
|
141
|
-
replace(tool: Tool, meta?: { category?: string; tags?: string[]; source?: AssetSource }) {
|
|
142
|
-
const existingEntry = getStoredEntryByRef(tool.id ?? tool.alias ?? tool.name);
|
|
143
|
-
const existing = existingEntry?.[1];
|
|
144
|
-
const stored: StoredTool = {
|
|
145
|
-
tool,
|
|
146
|
-
source: meta?.source ?? existing?.source ?? defaultSource,
|
|
147
|
-
category: meta?.category ?? existing?.category,
|
|
148
|
-
tags: meta?.tags ?? existing?.tags,
|
|
149
|
-
};
|
|
150
|
-
if (existingEntry) {
|
|
151
|
-
store.delete(existingEntry[0]);
|
|
152
|
-
}
|
|
153
|
-
store.set(getStoredKey(stored), stored);
|
|
154
|
-
rebuildCache();
|
|
155
|
-
emit({ type: 'updated', tools: [toDescriptor(stored)], provider: providerId });
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
has(ref: string): boolean {
|
|
159
|
-
return !!getStoredByRef(ref);
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
return provider;
|
|
164
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 工具运行时模块导出(barrel)
|
|
3
|
-
*
|
|
4
|
-
* - types / providers / ToolRuntimeManager:多来源工具编排
|
|
5
|
-
* - summaries:管理 API 摘要
|
|
6
|
-
* - defineTool / formats:TypeBox 工具定义与外部 JSON Schema 校验
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
// ══════════════════════ 类型 ══════════════════════
|
|
10
|
-
|
|
11
|
-
export type {
|
|
12
|
-
ToolDescriptor,
|
|
13
|
-
ToolChangeEvent,
|
|
14
|
-
ToolChangeListener,
|
|
15
|
-
SessionToolsChangeEvent,
|
|
16
|
-
SessionToolsChangeListener,
|
|
17
|
-
ToolProvider,
|
|
18
|
-
InProcessProvider,
|
|
19
|
-
McpProviderExtended,
|
|
20
|
-
McpServerConfig,
|
|
21
|
-
McpConnectionStatus,
|
|
22
|
-
McpConnectionInfo,
|
|
23
|
-
ConnectionStatusEvent,
|
|
24
|
-
ConnectionStatusListener,
|
|
25
|
-
ToolMiddleware,
|
|
26
|
-
ToolExecutionStats,
|
|
27
|
-
ToolSearchQuery,
|
|
28
|
-
CategorySummary,
|
|
29
|
-
ProviderInfo,
|
|
30
|
-
ToolRuntimeManager,
|
|
31
|
-
} from './types';
|
|
32
|
-
|
|
33
|
-
// ══════════════════════ Provider 实现 ══════════════════════
|
|
34
|
-
|
|
35
|
-
export { createInProcessProvider } from './in-process-provider';
|
|
36
|
-
|
|
37
|
-
export { createMcpProvider } from './mcp-provider';
|
|
38
|
-
|
|
39
|
-
// ══════════════════════ 统一管理器 ══════════════════════
|
|
40
|
-
|
|
41
|
-
export { createToolRuntimeManager } from './manager';
|
|
42
|
-
|
|
43
|
-
// ══════════════════════ 摘要 / 详情构建 ══════════════════════
|
|
44
|
-
|
|
45
|
-
export {
|
|
46
|
-
createManagedToolSummary,
|
|
47
|
-
createManagedToolDetail,
|
|
48
|
-
createManagedMcpServerSummary,
|
|
49
|
-
createManagedMcpServerDetail,
|
|
50
|
-
} from './summaries';
|
|
51
|
-
export type {
|
|
52
|
-
ManagedToolSummary,
|
|
53
|
-
ManagedToolDetail,
|
|
54
|
-
ManagedMcpServerSummary,
|
|
55
|
-
ManagedMcpServerDetail,
|
|
56
|
-
} from './types';
|
|
57
|
-
|
|
58
|
-
// ══════════════════════ TypeBox 工具 ══════════════════════
|
|
59
|
-
|
|
60
|
-
export { defineTool, validateJsonSchemaArgs, validateTypeBoxArgs } from './define-tool';
|
|
61
|
-
export type { TypeBoxToolConfig } from './define-tool';
|
|
62
|
-
export { TOOL_FORMATS, registerToolFormats, registerToolFormat, hasToolFormat } from './formats';
|
|
63
|
-
export type { ToolFormat } from './formats';
|