webmcp-nexus-core 0.1.9

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.
@@ -0,0 +1,129 @@
1
+ import { Type } from 'ts-morph';
2
+
3
+ /**
4
+ * 将 TypeScript 类型信息转换为 JSON Schema。
5
+ * 支持的映射规则见设计文档 3.2 节。
6
+ */
7
+ interface JsonSchema {
8
+ type: string;
9
+ description?: string;
10
+ properties?: Record<string, JsonSchema>;
11
+ required?: string[];
12
+ items?: JsonSchema;
13
+ enum?: string[];
14
+ }
15
+ interface PropertyInfo {
16
+ name: string;
17
+ type: string;
18
+ description?: string;
19
+ required: boolean;
20
+ enumValues?: string[];
21
+ itemType?: string;
22
+ properties?: PropertyInfo[];
23
+ }
24
+ /**
25
+ * 从提取到的属性信息生成 JSON Schema
26
+ */
27
+ declare function generateSchema(properties: PropertyInfo[], description?: string): JsonSchema;
28
+ /**
29
+ * 生成 __webmcpSchema 属性注入代码。
30
+ *
31
+ * 输出形如:
32
+ * ```
33
+ * target.__webmcpSchema = { description: "...", inputSchema: {...}, readOnly: false };
34
+ * ```
35
+ *
36
+ * @param injectionTarget - 注入目标表达式(如 "searchInPanel" 或 "userApi.getUser")
37
+ * @param description - 工具描述
38
+ * @param properties - 参数属性列表
39
+ * @param readOnly - 是否只读
40
+ * @returns 注入代码字符串
41
+ */
42
+ declare function generateSchemaInjectionCode(injectionTarget: string, description: string, properties: PropertyInfo[], readOnly: boolean): string;
43
+ declare function mapTypeToSchema(prop: PropertyInfo): JsonSchema;
44
+
45
+ /**
46
+ * TypeScript 类型提取器(v2 — 逆向追踪机制)。
47
+ *
48
+ * 从 registerGlobalTools() / useWebMcpTools() 调用向上追踪到函数定义,
49
+ * 使用 ts-morph 解析参数类型 + JSDoc 注释,提取工具元数据。
50
+ *
51
+ * 支持两种参数形式:
52
+ * 1. 对象字面量:{ fn1, fn2 } → 从 key 追踪到函数定义
53
+ * 2. Namespace import:import * as api from './module' → 解析源模块所有导出函数
54
+ */
55
+
56
+ /** 提取出的工具元数据 */
57
+ interface ExtractedTool {
58
+ /** 工具名(对象 key 或导出函数名) */
59
+ name: string;
60
+ /** JSDoc 描述 */
61
+ description: string;
62
+ /** 参数属性列表 */
63
+ properties: PropertyInfo[];
64
+ /** 是否为只读工具(@readonly 标签) */
65
+ readOnly: boolean;
66
+ /** 来源文件路径(用于调试) */
67
+ sourceFile: string;
68
+ /**
69
+ * 注入目标表达式。
70
+ * 对于对象字面量参数中的本地变量:变量名,如 "searchInPanel"
71
+ * 对于 namespace import:namespace.exportName,如 "userApi.getUser"
72
+ */
73
+ injectionTarget: string;
74
+ }
75
+ /** 注册调用的提取结果 */
76
+ interface ExtractionResult {
77
+ /** 提取到的工具列表 */
78
+ tools: ExtractedTool[];
79
+ /** 注册调用的位置信息(用于在调用前注入代码) */
80
+ registrationCalls: {
81
+ /** 调用类型 */
82
+ type: 'registerGlobalTools' | 'useWebMcpTools';
83
+ /** 调用在源码中的起始位置(字符偏移量) */
84
+ start: number;
85
+ }[];
86
+ }
87
+ /**
88
+ * 模块路径别名映射(兼容 webpack / vite 的 alias 配置)。
89
+ * key 为别名前缀(可用 `$` 后缀表示精确匹配),value 为目标绝对路径或相对 projectRoot 的路径。
90
+ */
91
+ type AliasMap = Record<string, string>;
92
+ /**
93
+ * 根据 alias 映射解析模块说明符。
94
+ * 支持 webpack 风格:
95
+ * - 精确匹配:key 以 `$` 结尾(如 `xyz$`),仅在 spec 完全等于 `xyz` 时命中
96
+ * - 前缀匹配:spec 等于 key 或以 `key + '/'` 开头
97
+ * 最长前缀优先。命中时返回替换后的路径,未命中返回 null。
98
+ */
99
+ declare function resolveWithAlias(spec: string, alias?: AliasMap): string | null;
100
+ /**
101
+ * 将 ts-morph Type 映射为简化的类型字符串(JSON Schema type)
102
+ */
103
+ declare function mapType(type: Type): string;
104
+ /** 从一个 Type 提取所有属性信息 */
105
+ declare function extractProperties(type: Type, depth?: number): PropertyInfo[];
106
+ /**
107
+ * 从源文件中提取所有 registerGlobalTools / useWebMcpTools 调用引用的工具
108
+ *
109
+ * @param fileContent - 源文件内容
110
+ * @param filePath - 源文件绝对路径
111
+ * @returns 提取结果,包含工具列表和注册调用位置信息
112
+ */
113
+ declare function extractToolsFromFile(fileContent: string, filePath: string, projectRoot?: string, alias?: AliasMap): ExtractionResult | null;
114
+
115
+ interface TransformOptions {
116
+ projectRoot?: string;
117
+ /**
118
+ * 构建工具的模块路径 alias 映射(已归一化为 prefix → 目标路径)。
119
+ * 用于解析 `import * as api from '@alias/xxx'` 类形式的模块说明符。
120
+ */
121
+ alias?: AliasMap;
122
+ }
123
+ interface TransformResult {
124
+ code: string;
125
+ transformed: boolean;
126
+ }
127
+ declare function transformCode(code: string, filePath: string, options?: TransformOptions): TransformResult;
128
+
129
+ export { type AliasMap, type ExtractedTool, type ExtractionResult, type JsonSchema, type PropertyInfo, type TransformOptions, type TransformResult, extractProperties, extractToolsFromFile, generateSchema, generateSchemaInjectionCode, mapType, mapTypeToSchema, resolveWithAlias, transformCode };
@@ -0,0 +1,129 @@
1
+ import { Type } from 'ts-morph';
2
+
3
+ /**
4
+ * 将 TypeScript 类型信息转换为 JSON Schema。
5
+ * 支持的映射规则见设计文档 3.2 节。
6
+ */
7
+ interface JsonSchema {
8
+ type: string;
9
+ description?: string;
10
+ properties?: Record<string, JsonSchema>;
11
+ required?: string[];
12
+ items?: JsonSchema;
13
+ enum?: string[];
14
+ }
15
+ interface PropertyInfo {
16
+ name: string;
17
+ type: string;
18
+ description?: string;
19
+ required: boolean;
20
+ enumValues?: string[];
21
+ itemType?: string;
22
+ properties?: PropertyInfo[];
23
+ }
24
+ /**
25
+ * 从提取到的属性信息生成 JSON Schema
26
+ */
27
+ declare function generateSchema(properties: PropertyInfo[], description?: string): JsonSchema;
28
+ /**
29
+ * 生成 __webmcpSchema 属性注入代码。
30
+ *
31
+ * 输出形如:
32
+ * ```
33
+ * target.__webmcpSchema = { description: "...", inputSchema: {...}, readOnly: false };
34
+ * ```
35
+ *
36
+ * @param injectionTarget - 注入目标表达式(如 "searchInPanel" 或 "userApi.getUser")
37
+ * @param description - 工具描述
38
+ * @param properties - 参数属性列表
39
+ * @param readOnly - 是否只读
40
+ * @returns 注入代码字符串
41
+ */
42
+ declare function generateSchemaInjectionCode(injectionTarget: string, description: string, properties: PropertyInfo[], readOnly: boolean): string;
43
+ declare function mapTypeToSchema(prop: PropertyInfo): JsonSchema;
44
+
45
+ /**
46
+ * TypeScript 类型提取器(v2 — 逆向追踪机制)。
47
+ *
48
+ * 从 registerGlobalTools() / useWebMcpTools() 调用向上追踪到函数定义,
49
+ * 使用 ts-morph 解析参数类型 + JSDoc 注释,提取工具元数据。
50
+ *
51
+ * 支持两种参数形式:
52
+ * 1. 对象字面量:{ fn1, fn2 } → 从 key 追踪到函数定义
53
+ * 2. Namespace import:import * as api from './module' → 解析源模块所有导出函数
54
+ */
55
+
56
+ /** 提取出的工具元数据 */
57
+ interface ExtractedTool {
58
+ /** 工具名(对象 key 或导出函数名) */
59
+ name: string;
60
+ /** JSDoc 描述 */
61
+ description: string;
62
+ /** 参数属性列表 */
63
+ properties: PropertyInfo[];
64
+ /** 是否为只读工具(@readonly 标签) */
65
+ readOnly: boolean;
66
+ /** 来源文件路径(用于调试) */
67
+ sourceFile: string;
68
+ /**
69
+ * 注入目标表达式。
70
+ * 对于对象字面量参数中的本地变量:变量名,如 "searchInPanel"
71
+ * 对于 namespace import:namespace.exportName,如 "userApi.getUser"
72
+ */
73
+ injectionTarget: string;
74
+ }
75
+ /** 注册调用的提取结果 */
76
+ interface ExtractionResult {
77
+ /** 提取到的工具列表 */
78
+ tools: ExtractedTool[];
79
+ /** 注册调用的位置信息(用于在调用前注入代码) */
80
+ registrationCalls: {
81
+ /** 调用类型 */
82
+ type: 'registerGlobalTools' | 'useWebMcpTools';
83
+ /** 调用在源码中的起始位置(字符偏移量) */
84
+ start: number;
85
+ }[];
86
+ }
87
+ /**
88
+ * 模块路径别名映射(兼容 webpack / vite 的 alias 配置)。
89
+ * key 为别名前缀(可用 `$` 后缀表示精确匹配),value 为目标绝对路径或相对 projectRoot 的路径。
90
+ */
91
+ type AliasMap = Record<string, string>;
92
+ /**
93
+ * 根据 alias 映射解析模块说明符。
94
+ * 支持 webpack 风格:
95
+ * - 精确匹配:key 以 `$` 结尾(如 `xyz$`),仅在 spec 完全等于 `xyz` 时命中
96
+ * - 前缀匹配:spec 等于 key 或以 `key + '/'` 开头
97
+ * 最长前缀优先。命中时返回替换后的路径,未命中返回 null。
98
+ */
99
+ declare function resolveWithAlias(spec: string, alias?: AliasMap): string | null;
100
+ /**
101
+ * 将 ts-morph Type 映射为简化的类型字符串(JSON Schema type)
102
+ */
103
+ declare function mapType(type: Type): string;
104
+ /** 从一个 Type 提取所有属性信息 */
105
+ declare function extractProperties(type: Type, depth?: number): PropertyInfo[];
106
+ /**
107
+ * 从源文件中提取所有 registerGlobalTools / useWebMcpTools 调用引用的工具
108
+ *
109
+ * @param fileContent - 源文件内容
110
+ * @param filePath - 源文件绝对路径
111
+ * @returns 提取结果,包含工具列表和注册调用位置信息
112
+ */
113
+ declare function extractToolsFromFile(fileContent: string, filePath: string, projectRoot?: string, alias?: AliasMap): ExtractionResult | null;
114
+
115
+ interface TransformOptions {
116
+ projectRoot?: string;
117
+ /**
118
+ * 构建工具的模块路径 alias 映射(已归一化为 prefix → 目标路径)。
119
+ * 用于解析 `import * as api from '@alias/xxx'` 类形式的模块说明符。
120
+ */
121
+ alias?: AliasMap;
122
+ }
123
+ interface TransformResult {
124
+ code: string;
125
+ transformed: boolean;
126
+ }
127
+ declare function transformCode(code: string, filePath: string, options?: TransformOptions): TransformResult;
128
+
129
+ export { type AliasMap, type ExtractedTool, type ExtractionResult, type JsonSchema, type PropertyInfo, type TransformOptions, type TransformResult, extractProperties, extractToolsFromFile, generateSchema, generateSchemaInjectionCode, mapType, mapTypeToSchema, resolveWithAlias, transformCode };