@zhin.js/core 1.0.57 → 1.1.2

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.
Files changed (126) hide show
  1. package/lib/adapter.d.ts +1 -26
  2. package/lib/adapter.d.ts.map +1 -1
  3. package/lib/adapter.js +20 -117
  4. package/lib/adapter.js.map +1 -1
  5. package/lib/ai/index.d.ts +2 -0
  6. package/lib/ai/index.d.ts.map +1 -1
  7. package/lib/ai/index.js +1 -0
  8. package/lib/ai/index.js.map +1 -1
  9. package/lib/built/adapter-process.d.ts +0 -4
  10. package/lib/built/adapter-process.d.ts.map +1 -1
  11. package/lib/built/adapter-process.js +0 -95
  12. package/lib/built/adapter-process.js.map +1 -1
  13. package/lib/built/agent-preset.d.ts +2 -0
  14. package/lib/built/agent-preset.d.ts.map +1 -1
  15. package/lib/built/agent-preset.js +4 -0
  16. package/lib/built/agent-preset.js.map +1 -1
  17. package/lib/built/command.d.ts +4 -0
  18. package/lib/built/command.d.ts.map +1 -1
  19. package/lib/built/command.js +6 -0
  20. package/lib/built/command.js.map +1 -1
  21. package/lib/built/component.d.ts.map +1 -1
  22. package/lib/built/component.js +1 -0
  23. package/lib/built/component.js.map +1 -1
  24. package/lib/built/dispatcher.d.ts.map +1 -1
  25. package/lib/built/dispatcher.js +0 -13
  26. package/lib/built/dispatcher.js.map +1 -1
  27. package/lib/built/message-filter.d.ts +2 -0
  28. package/lib/built/message-filter.d.ts.map +1 -1
  29. package/lib/built/message-filter.js +5 -0
  30. package/lib/built/message-filter.js.map +1 -1
  31. package/lib/built/skill.d.ts +11 -0
  32. package/lib/built/skill.d.ts.map +1 -1
  33. package/lib/built/skill.js +14 -0
  34. package/lib/built/skill.js.map +1 -1
  35. package/lib/built/tool.d.ts +11 -44
  36. package/lib/built/tool.d.ts.map +1 -1
  37. package/lib/built/tool.js +14 -353
  38. package/lib/built/tool.js.map +1 -1
  39. package/lib/plugin.d.ts +1 -25
  40. package/lib/plugin.d.ts.map +1 -1
  41. package/lib/plugin.js +1 -77
  42. package/lib/plugin.js.map +1 -1
  43. package/lib/types.d.ts +0 -25
  44. package/lib/types.d.ts.map +1 -1
  45. package/package.json +10 -7
  46. package/CHANGELOG.md +0 -538
  47. package/REFACTORING_COMPLETE.md +0 -178
  48. package/REFACTORING_STATUS.md +0 -263
  49. package/src/adapter.ts +0 -275
  50. package/src/ai/index.ts +0 -52
  51. package/src/ai/providers/anthropic.ts +0 -379
  52. package/src/ai/providers/base.ts +0 -175
  53. package/src/ai/providers/index.ts +0 -13
  54. package/src/ai/providers/ollama.ts +0 -302
  55. package/src/ai/providers/openai.ts +0 -174
  56. package/src/ai/types.ts +0 -348
  57. package/src/bot.ts +0 -37
  58. package/src/built/adapter-process.ts +0 -177
  59. package/src/built/agent-preset.ts +0 -136
  60. package/src/built/ai-trigger.ts +0 -259
  61. package/src/built/command.ts +0 -108
  62. package/src/built/common-adapter-tools.ts +0 -242
  63. package/src/built/component.ts +0 -130
  64. package/src/built/config.ts +0 -335
  65. package/src/built/cron.ts +0 -156
  66. package/src/built/database.ts +0 -134
  67. package/src/built/dispatcher.ts +0 -496
  68. package/src/built/login-assist.ts +0 -131
  69. package/src/built/message-filter.ts +0 -390
  70. package/src/built/permission.ts +0 -151
  71. package/src/built/schema-feature.ts +0 -190
  72. package/src/built/skill.ts +0 -221
  73. package/src/built/tool.ts +0 -948
  74. package/src/command.ts +0 -87
  75. package/src/component.ts +0 -565
  76. package/src/cron.ts +0 -4
  77. package/src/errors.ts +0 -46
  78. package/src/feature.ts +0 -7
  79. package/src/index.ts +0 -53
  80. package/src/jsx-dev-runtime.ts +0 -2
  81. package/src/jsx-runtime.ts +0 -12
  82. package/src/jsx.ts +0 -135
  83. package/src/message.ts +0 -48
  84. package/src/models/system-log.ts +0 -20
  85. package/src/models/user.ts +0 -15
  86. package/src/notice.ts +0 -98
  87. package/src/plugin.ts +0 -896
  88. package/src/prompt.ts +0 -293
  89. package/src/request.ts +0 -95
  90. package/src/scheduler/index.ts +0 -19
  91. package/src/scheduler/scheduler.ts +0 -372
  92. package/src/scheduler/types.ts +0 -74
  93. package/src/tool-zod.ts +0 -115
  94. package/src/types-generator.ts +0 -78
  95. package/src/types.ts +0 -505
  96. package/src/utils.ts +0 -227
  97. package/tests/adapter.test.ts +0 -638
  98. package/tests/ai/ai-trigger.test.ts +0 -368
  99. package/tests/ai/providers.integration.test.ts +0 -227
  100. package/tests/ai/setup.ts +0 -308
  101. package/tests/ai/tool.test.ts +0 -800
  102. package/tests/bot.test.ts +0 -151
  103. package/tests/command.test.ts +0 -737
  104. package/tests/component-new.test.ts +0 -361
  105. package/tests/config.test.ts +0 -372
  106. package/tests/cron.test.ts +0 -82
  107. package/tests/dispatcher.test.ts +0 -293
  108. package/tests/errors.test.ts +0 -21
  109. package/tests/expression-evaluation.test.ts +0 -258
  110. package/tests/features-builtin.test.ts +0 -191
  111. package/tests/jsx-runtime.test.ts +0 -45
  112. package/tests/jsx.test.ts +0 -319
  113. package/tests/message-filter.test.ts +0 -566
  114. package/tests/message.test.ts +0 -402
  115. package/tests/notice.test.ts +0 -198
  116. package/tests/plugin.test.ts +0 -779
  117. package/tests/prompt.test.ts +0 -78
  118. package/tests/redos-protection.test.ts +0 -198
  119. package/tests/request.test.ts +0 -221
  120. package/tests/schema.test.ts +0 -248
  121. package/tests/skill-feature.test.ts +0 -179
  122. package/tests/test-utils.ts +0 -59
  123. package/tests/tool-feature.test.ts +0 -254
  124. package/tests/types.test.ts +0 -162
  125. package/tests/utils.test.ts +0 -135
  126. package/tsconfig.json +0 -24
package/tests/ai/setup.ts DELETED
@@ -1,308 +0,0 @@
1
- /**
2
- * AI 模块测试环境设置
3
- *
4
- * 提供:
5
- * - Mock 对象工厂
6
- * - 测试辅助函数
7
- * - 公共测试配置
8
- */
9
- import { vi } from 'vitest';
10
- import type { Message, MessageElement, Tool, ToolContext } from '@zhin.js/core';
11
- import type { AIConfig, ChatMessage } from '@zhin.js/core';
12
-
13
- // ============================================================================
14
- // Logger Mock
15
- // ============================================================================
16
-
17
- export const createMockLogger = () => ({
18
- debug: vi.fn(),
19
- info: vi.fn(),
20
- warn: vi.fn(),
21
- error: vi.fn(),
22
- });
23
-
24
- // ============================================================================
25
- // Plugin Mock
26
- // ============================================================================
27
-
28
- export const createMockPlugin = (name = 'test-plugin') => ({
29
- name,
30
- root: {
31
- inject: vi.fn(),
32
- contexts: new Map(),
33
- middleware: vi.fn(),
34
- },
35
- logger: createMockLogger(),
36
- onDispose: vi.fn(),
37
- collectAllTools: vi.fn(() => []),
38
- provide: vi.fn(),
39
- useContext: vi.fn(),
40
- addMiddleware: vi.fn(),
41
- });
42
-
43
- // ============================================================================
44
- // Message Mock
45
- // ============================================================================
46
-
47
- export interface MockMessageOptions {
48
- content?: string;
49
- elements?: MessageElement[];
50
- platform?: string;
51
- channelType?: 'group' | 'private' | 'guild';
52
- channelId?: string;
53
- senderId?: string;
54
- senderPermissions?: string[];
55
- senderRole?: string;
56
- botId?: string;
57
- }
58
-
59
- export const createMockMessage = (options: MockMessageOptions = {}): Partial<Message> => {
60
- const {
61
- content = '测试消息',
62
- elements,
63
- platform = 'test',
64
- channelType = 'group',
65
- channelId = 'channel-1',
66
- senderId = 'user-1',
67
- senderPermissions = [],
68
- senderRole,
69
- botId = 'bot-1',
70
- } = options;
71
-
72
- const $content: MessageElement[] = elements || [
73
- { type: 'text', data: { text: content } },
74
- ];
75
-
76
- return {
77
- $content,
78
- $bot: botId,
79
- $adapter: platform,
80
- $channel: {
81
- type: channelType,
82
- id: channelId,
83
- name: 'Test Channel',
84
- },
85
- $sender: {
86
- id: senderId,
87
- name: 'Test User',
88
- permissions: senderPermissions,
89
- role: senderRole,
90
- },
91
- $reply: vi.fn().mockResolvedValue(undefined),
92
- $quote: vi.fn().mockResolvedValue(undefined),
93
- };
94
- };
95
-
96
- // ============================================================================
97
- // Tool Mock
98
- // ============================================================================
99
-
100
- export interface MockToolOptions {
101
- name: string;
102
- description?: string;
103
- parameters?: Record<string, { type: string; description?: string }>;
104
- required?: string[];
105
- platforms?: string[];
106
- scopes?: ('group' | 'private' | 'guild')[];
107
- permissionLevel?: 'user' | 'group_admin' | 'group_owner' | 'bot_admin' | 'owner';
108
- tags?: string[];
109
- keywords?: string[];
110
- executeResult?: any;
111
- executeError?: Error;
112
- }
113
-
114
- export const createMockTool = (options: MockToolOptions): Tool => {
115
- const {
116
- name,
117
- description = `${name} 工具`,
118
- parameters = {},
119
- required = [],
120
- platforms,
121
- scopes,
122
- permissionLevel,
123
- tags = [],
124
- keywords = [],
125
- executeResult = 'success',
126
- executeError,
127
- } = options;
128
-
129
- const properties: Record<string, any> = {};
130
- for (const [key, value] of Object.entries(parameters)) {
131
- properties[key] = {
132
- type: value.type,
133
- description: value.description,
134
- };
135
- }
136
-
137
- const tool: any = {
138
- name,
139
- description,
140
- parameters: {
141
- type: 'object',
142
- properties,
143
- required,
144
- },
145
- platforms,
146
- scopes,
147
- permissionLevel,
148
- tags,
149
- execute: executeError
150
- ? vi.fn().mockRejectedValue(executeError)
151
- : vi.fn().mockResolvedValue(executeResult),
152
- };
153
- if (keywords.length > 0) tool.keywords = keywords;
154
- return tool;
155
- };
156
-
157
- // ============================================================================
158
- // AI Provider Mock
159
- // ============================================================================
160
-
161
- export interface MockProviderOptions {
162
- name?: string;
163
- response?: string | AsyncGenerator<string>;
164
- toolCalls?: Array<{ name: string; arguments: Record<string, any> }>;
165
- error?: Error;
166
- }
167
-
168
- export const createMockProvider = (options: MockProviderOptions = {}) => {
169
- const {
170
- name = 'mock',
171
- response = '这是 AI 的回复',
172
- toolCalls = [],
173
- error,
174
- } = options;
175
-
176
- if (error) {
177
- return {
178
- name,
179
- chat: vi.fn().mockRejectedValue(error),
180
- healthCheck: vi.fn().mockResolvedValue(false),
181
- };
182
- }
183
-
184
- const generateResponse = async function* (): AsyncGenerator<{
185
- content?: string;
186
- toolCalls?: Array<{ name: string; arguments: Record<string, any> }>;
187
- done: boolean;
188
- }> {
189
- if (toolCalls.length > 0) {
190
- yield { toolCalls, done: false };
191
- }
192
-
193
- if (typeof response === 'string') {
194
- yield { content: response, done: true };
195
- } else {
196
- for await (const chunk of response) {
197
- yield { content: chunk, done: false };
198
- }
199
- yield { done: true };
200
- }
201
- };
202
-
203
- return {
204
- name,
205
- chat: vi.fn().mockImplementation(() => generateResponse()),
206
- healthCheck: vi.fn().mockResolvedValue(true),
207
- };
208
- };
209
-
210
- // ============================================================================
211
- // Context Factory
212
- // ============================================================================
213
-
214
- export const createToolContext = (options: Partial<ToolContext> = {}): ToolContext => ({
215
- platform: 'test',
216
- scope: 'group',
217
- permissionLevel: 'user',
218
- ...options,
219
- });
220
-
221
- // ============================================================================
222
- // AI Config Factory
223
- // ============================================================================
224
-
225
- export const createMockAIConfig = (overrides: Partial<AIConfig> = {}): AIConfig => ({
226
- defaultProvider: 'mock',
227
- sessions: {
228
- maxHistory: 10,
229
- expireMs: 300000,
230
- },
231
- context: {
232
- enabled: false,
233
- maxSize: 100,
234
- },
235
- trigger: {
236
- enabled: true,
237
- prefixes: ['#'],
238
- ignorePrefixes: ['/'],
239
- allowAtBot: true,
240
- allowPrivateChat: true,
241
- },
242
- ...overrides,
243
- });
244
-
245
- // ============================================================================
246
- // Test Utilities
247
- // ============================================================================
248
-
249
- /**
250
- * 等待 Promise 解决或超时
251
- */
252
- export const waitFor = async <T>(
253
- promise: Promise<T>,
254
- timeout = 5000
255
- ): Promise<T> => {
256
- return Promise.race([
257
- promise,
258
- new Promise<T>((_, reject) =>
259
- setTimeout(() => reject(new Error('Timeout')), timeout)
260
- ),
261
- ]);
262
- };
263
-
264
- /**
265
- * 收集 AsyncGenerator 的所有值
266
- */
267
- export const collectAsyncGenerator = async <T>(
268
- generator: AsyncGenerator<T>
269
- ): Promise<T[]> => {
270
- const results: T[] = [];
271
- for await (const item of generator) {
272
- results.push(item);
273
- }
274
- return results;
275
- };
276
-
277
- /**
278
- * 创建延迟 Promise
279
- */
280
- export const delay = (ms: number): Promise<void> =>
281
- new Promise((resolve) => setTimeout(resolve, ms));
282
-
283
- /**
284
- * 创建带有所有字段的 ChatMessage
285
- */
286
- export const createChatMessage = (
287
- role: 'user' | 'assistant' | 'system',
288
- content: string
289
- ): ChatMessage => ({
290
- role,
291
- content,
292
- });
293
-
294
- /**
295
- * 验证工具参数结构
296
- */
297
- export const assertToolParameters = (
298
- tool: Tool,
299
- expectedProperties: string[],
300
- expectedRequired: string[] = []
301
- ) => {
302
- const props = Object.keys(tool.parameters.properties || {});
303
- expect(props).toEqual(expect.arrayContaining(expectedProperties));
304
-
305
- if (expectedRequired.length > 0) {
306
- expect(tool.parameters.required).toEqual(expect.arrayContaining(expectedRequired));
307
- }
308
- };