@zhin.js/ai 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,346 @@
1
+ /**
2
+ * 内置工具测试
3
+ *
4
+ * 测试内容:
5
+ * 1. 计算器工具
6
+ * 2. 时间工具
7
+ * 3. 搜索工具
8
+ * 4. 代码执行工具
9
+ * 5. HTTP 请求工具
10
+ */
11
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
12
+ import {
13
+ calculatorTool,
14
+ timeTool,
15
+ searchTool,
16
+ codeRunnerTool,
17
+ httpTool,
18
+ getBuiltinTools,
19
+ } from '../src/tools.js';
20
+ import { ZhinTool } from '@zhin.js/core';
21
+
22
+ describe('内置工具', () => {
23
+ describe('计算器工具', () => {
24
+ const calculator = calculatorTool.toTool();
25
+
26
+ it('应该有正确的元数据', () => {
27
+ expect(calculator.name).toBe('calculator');
28
+ expect(calculator.description).toContain('计算');
29
+ expect(calculator.parameters.properties).toHaveProperty('expression');
30
+ expect(calculator.parameters.required).toContain('expression');
31
+ });
32
+
33
+ it('应该计算简单加法', async () => {
34
+ const result = await calculator.execute({ expression: '2 + 3' });
35
+ expect(result.result).toBe(5);
36
+ expect(result.expression).toBe('2 + 3');
37
+ });
38
+
39
+ it('应该计算简单减法', async () => {
40
+ const result = await calculator.execute({ expression: '10 - 4' });
41
+ expect(result.result).toBe(6);
42
+ });
43
+
44
+ it('应该计算简单乘法', async () => {
45
+ const result = await calculator.execute({ expression: '6 * 7' });
46
+ expect(result.result).toBe(42);
47
+ });
48
+
49
+ it('应该计算简单除法', async () => {
50
+ const result = await calculator.execute({ expression: '20 / 4' });
51
+ expect(result.result).toBe(5);
52
+ });
53
+
54
+ it('应该计算复杂表达式', async () => {
55
+ const result = await calculator.execute({ expression: '(10 + 5) * 2' });
56
+ expect(result.result).toBe(30);
57
+ });
58
+
59
+ it('应该支持幂运算 (^)', async () => {
60
+ const result = await calculator.execute({ expression: '2 ^ 3' });
61
+ expect(result.result).toBe(8);
62
+ });
63
+
64
+ it('应该支持 sqrt 函数', async () => {
65
+ const result = await calculator.execute({ expression: 'sqrt(16)' });
66
+ expect(result.result).toBe(4);
67
+ });
68
+
69
+ it('应该支持 sin 函数', async () => {
70
+ const result = await calculator.execute({ expression: 'sin(0)' });
71
+ expect(result.result).toBe(0);
72
+ });
73
+
74
+ it('应该支持 cos 函数', async () => {
75
+ const result = await calculator.execute({ expression: 'cos(0)' });
76
+ expect(result.result).toBe(1);
77
+ });
78
+
79
+ it('应该支持 tan 函数', async () => {
80
+ const result = await calculator.execute({ expression: 'tan(0)' });
81
+ expect(result.result).toBe(0);
82
+ });
83
+
84
+ it('应该支持 log 函数', async () => {
85
+ const result = await calculator.execute({ expression: 'log(1)' });
86
+ expect(result.result).toBe(0);
87
+ });
88
+
89
+ it('应该支持 abs 函数', async () => {
90
+ const result = await calculator.execute({ expression: 'abs(-5)' });
91
+ expect(result.result).toBe(5);
92
+ });
93
+
94
+ it('应该支持 pow 函数(通过 ^ 运算符)', async () => {
95
+ // pow() 函数的逗号会被过滤,所以使用 ^ 运算符
96
+ const result = await calculator.execute({ expression: '2 ^ 10' });
97
+ expect(result.result).toBe(1024);
98
+ });
99
+
100
+ it('应该支持 PI 常量', async () => {
101
+ const result = await calculator.execute({ expression: 'PI' });
102
+ expect(result.result).toBeCloseTo(Math.PI);
103
+ });
104
+
105
+ it('应该支持 E 常量', async () => {
106
+ const result = await calculator.execute({ expression: 'E' });
107
+ expect(result.result).toBeCloseTo(Math.E);
108
+ });
109
+
110
+ it('应该处理无效表达式', async () => {
111
+ const result = await calculator.execute({ expression: 'invalid_expression' });
112
+ expect(result.error).toBeDefined();
113
+ });
114
+
115
+ it('应该处理除零', async () => {
116
+ const result = await calculator.execute({ expression: '1 / 0' });
117
+ expect(result.result).toBe(Infinity);
118
+ });
119
+
120
+ it('应该处理空表达式', async () => {
121
+ const result = await calculator.execute({ expression: '' });
122
+ // 空表达式会返回 undefined 或 error
123
+ expect(result.error || result.result === undefined).toBeTruthy();
124
+ });
125
+ });
126
+
127
+ describe('时间工具', () => {
128
+ const timeToolObj = timeTool.toTool();
129
+
130
+ it('应该有正确的元数据', () => {
131
+ expect(timeToolObj.name).toBe('get_time');
132
+ expect(timeToolObj.description).toContain('时间');
133
+ expect(timeToolObj.parameters.properties).toHaveProperty('timezone');
134
+ expect(timeToolObj.parameters.properties).toHaveProperty('format');
135
+ });
136
+
137
+ it('应该返回当前时间 (默认格式)', async () => {
138
+ const result = await timeToolObj.execute({});
139
+
140
+ expect(result).toHaveProperty('timestamp');
141
+ expect(result).toHaveProperty('formatted');
142
+ expect(result).toHaveProperty('iso');
143
+ expect(typeof result.timestamp).toBe('number');
144
+ expect(typeof result.formatted).toBe('string');
145
+ expect(typeof result.iso).toBe('string');
146
+ });
147
+
148
+ it('应该支持 full 格式', async () => {
149
+ const result = await timeToolObj.execute({ format: 'full' });
150
+
151
+ expect(result).toHaveProperty('formatted');
152
+ expect(result).toHaveProperty('timestamp');
153
+ });
154
+
155
+ it('应该支持 date 格式', async () => {
156
+ const result = await timeToolObj.execute({ format: 'date' });
157
+
158
+ expect(result).toHaveProperty('formatted');
159
+ expect(result).toHaveProperty('timestamp');
160
+ });
161
+
162
+ it('应该支持 time 格式', async () => {
163
+ const result = await timeToolObj.execute({ format: 'time' });
164
+
165
+ expect(result).toHaveProperty('formatted');
166
+ expect(result).toHaveProperty('timestamp');
167
+ });
168
+
169
+ it('应该支持 timestamp 格式', async () => {
170
+ const result = await timeToolObj.execute({ format: 'timestamp' });
171
+
172
+ expect(result).toHaveProperty('timestamp');
173
+ expect(result).toHaveProperty('iso');
174
+ expect(result).not.toHaveProperty('formatted');
175
+ });
176
+
177
+ it('应该支持指定时区', async () => {
178
+ const result = await timeToolObj.execute({ timezone: 'UTC' });
179
+
180
+ expect(result).toHaveProperty('formatted');
181
+ expect(result).toHaveProperty('timestamp');
182
+ });
183
+
184
+ it('时间戳应该是有效的数字', async () => {
185
+ const result = await timeToolObj.execute({});
186
+ const now = Date.now();
187
+
188
+ // 时间戳应该在合理范围内(前后 1 秒)
189
+ expect(result.timestamp).toBeGreaterThan(now - 1000);
190
+ expect(result.timestamp).toBeLessThan(now + 1000);
191
+ });
192
+ });
193
+
194
+ describe('搜索工具', () => {
195
+ it('应该有正确的元数据', () => {
196
+ const searchToolObj = searchTool.toTool();
197
+
198
+ expect(searchToolObj.name).toBe('web_search');
199
+ expect(searchToolObj.description).toContain('搜索');
200
+ expect(searchToolObj.parameters.properties).toHaveProperty('query');
201
+ expect(searchToolObj.parameters.required).toContain('query');
202
+ });
203
+
204
+ it('未配置搜索函数时应返回错误', async () => {
205
+ const searchToolObj = searchTool.toTool();
206
+ const result = await searchToolObj.execute({ query: 'test' });
207
+
208
+ expect(result.error).toBeDefined();
209
+ expect(result.query).toBe('test');
210
+ });
211
+ });
212
+
213
+ describe('代码执行工具', () => {
214
+ const codeRunnerObj = codeRunnerTool.toTool();
215
+
216
+ it('应该有正确的元数据', () => {
217
+ expect(codeRunnerObj.name).toBe('run_code');
218
+ expect(codeRunnerObj.description).toContain('JavaScript');
219
+ expect(codeRunnerObj.parameters.properties).toHaveProperty('code');
220
+ expect(codeRunnerObj.parameters.required).toContain('code');
221
+ });
222
+
223
+ it('应该执行简单表达式', async () => {
224
+ const result = await codeRunnerObj.execute({ code: 'return 1 + 2' });
225
+
226
+ expect(result.success).toBe(true);
227
+ expect(result.result).toBe('3');
228
+ });
229
+
230
+ it('应该执行字符串操作', async () => {
231
+ const result = await codeRunnerObj.execute({ code: 'return "hello".toUpperCase()' });
232
+
233
+ expect(result.success).toBe(true);
234
+ expect(result.result).toBe('HELLO');
235
+ });
236
+
237
+ it('应该执行数组操作', async () => {
238
+ const result = await codeRunnerObj.execute({ code: 'return [1,2,3].map(x => x * 2)' });
239
+
240
+ expect(result.success).toBe(true);
241
+ // 数组转字符串时会变成 "2,4,6"
242
+ expect(result.result).toContain('2,4,6');
243
+ });
244
+
245
+ it('应该处理 undefined 返回值', async () => {
246
+ const result = await codeRunnerObj.execute({ code: 'let x = 1' });
247
+
248
+ expect(result.success).toBe(true);
249
+ expect(result.result).toBe('undefined');
250
+ });
251
+
252
+ it('应该处理语法错误', async () => {
253
+ const result = await codeRunnerObj.execute({ code: 'return {{{' });
254
+
255
+ expect(result.success).toBe(false);
256
+ expect(result.error).toBeDefined();
257
+ });
258
+
259
+ it('应该处理运行时错误', async () => {
260
+ const result = await codeRunnerObj.execute({ code: 'throw new Error("test error")' });
261
+
262
+ expect(result.success).toBe(false);
263
+ expect(result.error).toContain('test error');
264
+ });
265
+ });
266
+
267
+ describe('HTTP 请求工具', () => {
268
+ const httpToolObj = httpTool.toTool();
269
+
270
+ it('应该有正确的元数据', () => {
271
+ expect(httpToolObj.name).toBe('http_request');
272
+ expect(httpToolObj.description).toContain('HTTP');
273
+ expect(httpToolObj.parameters.properties).toHaveProperty('url');
274
+ expect(httpToolObj.parameters.properties).toHaveProperty('method');
275
+ expect(httpToolObj.parameters.required).toContain('url');
276
+ });
277
+
278
+ it('应该处理无效 URL', async () => {
279
+ const result = await httpToolObj.execute({ url: 'invalid-url' });
280
+
281
+ expect(result.error).toBeDefined();
282
+ });
283
+
284
+ it('应该支持 method 参数', () => {
285
+ const methodProp = httpToolObj.parameters.properties?.method;
286
+
287
+ expect(methodProp).toBeDefined();
288
+ expect(methodProp?.type).toBe('string');
289
+ // 描述中应该包含支持的方法
290
+ expect(methodProp?.description).toContain('GET');
291
+ expect(methodProp?.description).toContain('POST');
292
+ expect(methodProp?.description).toContain('PUT');
293
+ expect(methodProp?.description).toContain('DELETE');
294
+ });
295
+ });
296
+
297
+ describe('getBuiltinTools', () => {
298
+ it('应该返回内置工具列表', () => {
299
+ const tools = getBuiltinTools();
300
+
301
+ expect(Array.isArray(tools)).toBe(true);
302
+ expect(tools.length).toBeGreaterThan(0);
303
+
304
+ // tools 现在是 ZhinTool 实例
305
+ const names = tools.map(t => t.name);
306
+ expect(names).toContain('calculator');
307
+ expect(names).toContain('get_time');
308
+ });
309
+
310
+ it('所有工具应该是 ZhinTool 实例', () => {
311
+ const tools = getBuiltinTools();
312
+
313
+ for (const tool of tools) {
314
+ expect(tool).toBeInstanceOf(ZhinTool);
315
+ expect(tool.name).toBeDefined();
316
+ expect(tool.description).toBeDefined();
317
+ }
318
+ });
319
+
320
+ it('工具名称应该唯一', () => {
321
+ const tools = getBuiltinTools();
322
+ const names = tools.map(t => t.name);
323
+ const uniqueNames = [...new Set(names)];
324
+
325
+ expect(names.length).toBe(uniqueNames.length);
326
+ });
327
+
328
+ it('所有工具应该能转换为 Tool 对象', () => {
329
+ const tools = getBuiltinTools();
330
+
331
+ for (const tool of tools) {
332
+ const toolObj = tool.toTool();
333
+
334
+ expect(toolObj).toHaveProperty('name');
335
+ expect(toolObj).toHaveProperty('description');
336
+ expect(toolObj).toHaveProperty('parameters');
337
+ expect(toolObj).toHaveProperty('execute');
338
+ expect(typeof toolObj.name).toBe('string');
339
+ expect(typeof toolObj.description).toBe('string');
340
+ expect(typeof toolObj.execute).toBe('function');
341
+ expect(toolObj.parameters).toHaveProperty('type');
342
+ expect(toolObj.parameters.type).toBe('object');
343
+ }
344
+ });
345
+ });
346
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "outDir": "./lib",
7
+ "rootDir": "./src",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "allowSyntheticDefaultImports": true,
15
+ "experimentalDecorators": true,
16
+ "emitDecoratorMetadata": true,
17
+ "declaration": true,
18
+ "declarationMap": true,
19
+ "sourceMap": true
20
+ },
21
+ "include": ["src/**/*"],
22
+ "exclude": ["lib", "node_modules", "client", "dist"]
23
+ }