@zhin.js/core 1.1.0 → 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 (122) 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/built/adapter-process.d.ts +0 -4
  6. package/lib/built/adapter-process.d.ts.map +1 -1
  7. package/lib/built/adapter-process.js +0 -95
  8. package/lib/built/adapter-process.js.map +1 -1
  9. package/lib/built/agent-preset.d.ts +2 -0
  10. package/lib/built/agent-preset.d.ts.map +1 -1
  11. package/lib/built/agent-preset.js +4 -0
  12. package/lib/built/agent-preset.js.map +1 -1
  13. package/lib/built/command.d.ts +4 -0
  14. package/lib/built/command.d.ts.map +1 -1
  15. package/lib/built/command.js +6 -0
  16. package/lib/built/command.js.map +1 -1
  17. package/lib/built/component.d.ts.map +1 -1
  18. package/lib/built/component.js +1 -0
  19. package/lib/built/component.js.map +1 -1
  20. package/lib/built/dispatcher.d.ts.map +1 -1
  21. package/lib/built/dispatcher.js +0 -13
  22. package/lib/built/dispatcher.js.map +1 -1
  23. package/lib/built/message-filter.d.ts +2 -0
  24. package/lib/built/message-filter.d.ts.map +1 -1
  25. package/lib/built/message-filter.js +5 -0
  26. package/lib/built/message-filter.js.map +1 -1
  27. package/lib/built/skill.d.ts +11 -0
  28. package/lib/built/skill.d.ts.map +1 -1
  29. package/lib/built/skill.js +14 -0
  30. package/lib/built/skill.js.map +1 -1
  31. package/lib/built/tool.d.ts +11 -44
  32. package/lib/built/tool.d.ts.map +1 -1
  33. package/lib/built/tool.js +14 -353
  34. package/lib/built/tool.js.map +1 -1
  35. package/lib/plugin.d.ts +1 -25
  36. package/lib/plugin.d.ts.map +1 -1
  37. package/lib/plugin.js +1 -77
  38. package/lib/plugin.js.map +1 -1
  39. package/lib/types.d.ts +0 -25
  40. package/lib/types.d.ts.map +1 -1
  41. package/package.json +10 -7
  42. package/CHANGELOG.md +0 -561
  43. package/REFACTORING_COMPLETE.md +0 -178
  44. package/REFACTORING_STATUS.md +0 -263
  45. package/src/adapter.ts +0 -275
  46. package/src/ai/index.ts +0 -55
  47. package/src/ai/providers/anthropic.ts +0 -379
  48. package/src/ai/providers/base.ts +0 -175
  49. package/src/ai/providers/index.ts +0 -13
  50. package/src/ai/providers/ollama.ts +0 -302
  51. package/src/ai/providers/openai.ts +0 -174
  52. package/src/ai/types.ts +0 -348
  53. package/src/bot.ts +0 -37
  54. package/src/built/adapter-process.ts +0 -177
  55. package/src/built/agent-preset.ts +0 -136
  56. package/src/built/ai-trigger.ts +0 -259
  57. package/src/built/command.ts +0 -108
  58. package/src/built/common-adapter-tools.ts +0 -242
  59. package/src/built/component.ts +0 -130
  60. package/src/built/config.ts +0 -335
  61. package/src/built/cron.ts +0 -156
  62. package/src/built/database.ts +0 -134
  63. package/src/built/dispatcher.ts +0 -496
  64. package/src/built/login-assist.ts +0 -131
  65. package/src/built/message-filter.ts +0 -390
  66. package/src/built/permission.ts +0 -151
  67. package/src/built/schema-feature.ts +0 -190
  68. package/src/built/skill.ts +0 -221
  69. package/src/built/tool.ts +0 -948
  70. package/src/command.ts +0 -87
  71. package/src/component.ts +0 -565
  72. package/src/cron.ts +0 -4
  73. package/src/errors.ts +0 -46
  74. package/src/feature.ts +0 -7
  75. package/src/index.ts +0 -53
  76. package/src/jsx-dev-runtime.ts +0 -2
  77. package/src/jsx-runtime.ts +0 -12
  78. package/src/jsx.ts +0 -135
  79. package/src/message.ts +0 -48
  80. package/src/models/system-log.ts +0 -20
  81. package/src/models/user.ts +0 -15
  82. package/src/notice.ts +0 -98
  83. package/src/plugin.ts +0 -896
  84. package/src/prompt.ts +0 -293
  85. package/src/request.ts +0 -95
  86. package/src/scheduler/index.ts +0 -19
  87. package/src/scheduler/scheduler.ts +0 -372
  88. package/src/scheduler/types.ts +0 -74
  89. package/src/tool-zod.ts +0 -115
  90. package/src/types-generator.ts +0 -78
  91. package/src/types.ts +0 -505
  92. package/src/utils.ts +0 -227
  93. package/tests/adapter.test.ts +0 -638
  94. package/tests/ai/ai-trigger.test.ts +0 -368
  95. package/tests/ai/providers.integration.test.ts +0 -227
  96. package/tests/ai/setup.ts +0 -308
  97. package/tests/ai/tool.test.ts +0 -800
  98. package/tests/bot.test.ts +0 -151
  99. package/tests/command.test.ts +0 -737
  100. package/tests/component-new.test.ts +0 -361
  101. package/tests/config.test.ts +0 -372
  102. package/tests/cron.test.ts +0 -82
  103. package/tests/dispatcher.test.ts +0 -293
  104. package/tests/errors.test.ts +0 -21
  105. package/tests/expression-evaluation.test.ts +0 -258
  106. package/tests/features-builtin.test.ts +0 -191
  107. package/tests/jsx-runtime.test.ts +0 -45
  108. package/tests/jsx.test.ts +0 -319
  109. package/tests/message-filter.test.ts +0 -566
  110. package/tests/message.test.ts +0 -402
  111. package/tests/notice.test.ts +0 -198
  112. package/tests/plugin.test.ts +0 -779
  113. package/tests/prompt.test.ts +0 -78
  114. package/tests/redos-protection.test.ts +0 -198
  115. package/tests/request.test.ts +0 -221
  116. package/tests/schema.test.ts +0 -248
  117. package/tests/skill-feature.test.ts +0 -179
  118. package/tests/test-utils.ts +0 -59
  119. package/tests/tool-feature.test.ts +0 -254
  120. package/tests/types.test.ts +0 -162
  121. package/tests/utils.test.ts +0 -135
  122. package/tsconfig.json +0 -24
@@ -1,361 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest'
2
- import {
3
- defineComponent,
4
- createComponentContext,
5
- renderComponents,
6
- getProps,
7
- Component,
8
- ComponentContext,
9
- Fragment,
10
- Fetch
11
- } from '../src/component'
12
- import { Message } from '../src/message'
13
- import { SendOptions, SendContent } from '../src/types'
14
- import { segment } from '../src/utils'
15
-
16
- // Helper to convert content to string for assertions
17
- function contentToString(content: SendContent): string {
18
- if (typeof content === 'string') return content
19
- if (Array.isArray(content)) {
20
- return content.map(c => typeof c === 'string' ? c : segment.toString(c)).join('')
21
- }
22
- return segment.toString(content)
23
- }
24
-
25
- // Mock utils functions
26
- vi.mock('../src/utils', () => ({
27
- getValueWithRuntime: vi.fn((expression, context) => {
28
- // Simple mock implementation for testing
29
- if (typeof expression === 'string' && context) {
30
- // Handle simple variable access
31
- if (expression in context) {
32
- return context[expression]
33
- }
34
- // Handle object property access like 'user.name'
35
- if (expression.includes('.')) {
36
- const [obj, prop] = expression.split('.')
37
- return context[obj]?.[prop]
38
- }
39
- // Handle simple expressions
40
- try {
41
- return eval(expression)
42
- } catch {
43
- return expression
44
- }
45
- }
46
- return expression
47
- }),
48
- compiler: vi.fn((template, context) => template),
49
- segment: {
50
- toString: vi.fn((content) => typeof content === 'string' ? content : JSON.stringify(content)),
51
- from: vi.fn((content) => content),
52
- escape: vi.fn((content) => content.replace(/</g, '&lt;').replace(/>/g, '&gt;'))
53
- }
54
- }))
55
-
56
- describe('函数式组件系统测试', () => {
57
- let mockContext: ComponentContext
58
- let mockMessage: Message
59
-
60
- beforeEach(() => {
61
- mockMessage = {
62
- $id: '1',
63
- $adapter: 'test',
64
- $bot: 'test-bot',
65
- $content: [],
66
- $sender: { id: 'user1', name: 'User' },
67
- $reply: vi.fn(),
68
- $channel: { id: 'channel1', type: 'private' },
69
- $timestamp: Date.now(),
70
- $raw: 'test'
71
- }
72
-
73
- mockContext = createComponentContext(
74
- { user: { name: 'John', age: 25 } },
75
- undefined,
76
- 'test template'
77
- )
78
- })
79
-
80
- describe('defineComponent 函数测试', () => {
81
- it('应该正确创建函数式组件', () => {
82
- const TestComponent = defineComponent(async function TestComponent(props: { name: string }, context: ComponentContext) {
83
- return `Hello ${props.name}`
84
- }, 'test-component')
85
-
86
- expect(TestComponent).toBeInstanceOf(Function)
87
- expect(TestComponent.name).toBe('test-component')
88
- })
89
-
90
- it('应该支持异步组件', async () => {
91
- const AsyncComponent = defineComponent(async function AsyncComponent(props: { delay: number }, context: ComponentContext) {
92
- await new Promise(resolve => setTimeout(resolve, props.delay))
93
- return `Delayed: ${props.delay}ms`
94
- }, 'async-component')
95
-
96
- const result = await AsyncComponent({ delay: 10 }, mockContext)
97
- expect(result).toBe('Delayed: 10ms')
98
- })
99
- })
100
-
101
- describe('getProps 函数测试', () => {
102
- it('应该正确解析简单属性', () => {
103
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
104
- return 'test'
105
- }, 'test')
106
-
107
- const template = '<test name="John" age={25} active={true} />'
108
- const props = getProps(TestComponent, template, mockContext)
109
-
110
- expect(props.name).toBe('John')
111
- expect(props.age).toBe(25)
112
- expect(props.active).toBe(true)
113
- })
114
-
115
- it('应该正确解析表达式属性', () => {
116
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
117
- return 'test'
118
- }, 'test')
119
-
120
- const template = '<test sum={1+1} />'
121
- const props = getProps(TestComponent, template, mockContext)
122
-
123
- expect(props.sum).toBe(2)
124
- })
125
-
126
- it('应该正确处理 kebab-case 到 camelCase 转换', () => {
127
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
128
- return 'test'
129
- }, 'test')
130
-
131
- const template = '<test user-name="John" user-age={25} is-active={true} />'
132
- const props = getProps(TestComponent, template, mockContext)
133
-
134
- expect(props.userName).toBe('John')
135
- expect(props.userAge).toBe(25)
136
- expect(props.isActive).toBe(true)
137
- })
138
-
139
- it('应该正确处理 children 属性', () => {
140
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
141
- return 'test'
142
- }, 'test')
143
-
144
- const template = '<test>Hello World</test>'
145
- const props = getProps(TestComponent, template, mockContext)
146
-
147
- expect(props.children).toBe('Hello World')
148
- })
149
-
150
- it('应该正确处理自闭合标签', () => {
151
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
152
- return 'test'
153
- }, 'test')
154
-
155
- const template = '<test name="John" />'
156
- const props = getProps(TestComponent, template, mockContext)
157
-
158
- expect(props.name).toBe('John')
159
- expect(props.children).toBeUndefined()
160
- })
161
- })
162
-
163
- describe('createComponentContext 函数测试', () => {
164
- it('应该正确创建组件上下文', () => {
165
- const context = createComponentContext(
166
- { user: { name: 'John' } },
167
- undefined,
168
- 'test template'
169
- )
170
-
171
- expect(context.props).toEqual({ user: { name: 'John' } })
172
- expect(context.root).toBe('test template')
173
- expect(context.parent).toBeUndefined()
174
- expect(context.children).toBeUndefined()
175
- expect(typeof context.render).toBe('function')
176
- expect(typeof context.getValue).toBe('function')
177
- expect(typeof context.compile).toBe('function')
178
- })
179
-
180
- it('应该正确处理父上下文', () => {
181
- const parentContext = createComponentContext({ parent: 'data' })
182
- const childContext = createComponentContext(
183
- { child: 'data' },
184
- parentContext,
185
- 'child template'
186
- )
187
-
188
- expect(childContext.parent).toBe(parentContext)
189
- expect(childContext.props).toEqual({ child: 'data' })
190
- })
191
- })
192
-
193
- describe('内置组件测试', () => {
194
- it('Fragment 组件应该正确渲染 children', async () => {
195
- const result = await Fragment({ children: 'Hello World' }, mockContext)
196
- expect(result).toBe('Hello World')
197
- })
198
-
199
- it('Fetch 组件应该正确获取远程内容', async () => {
200
- // Mock fetch
201
- global.fetch = vi.fn().mockResolvedValue({
202
- text: () => Promise.resolve('Remote content')
203
- })
204
-
205
- const result = await Fetch({ url: 'https://example.com' }, mockContext)
206
- expect(result).toBe('Remote content')
207
- })
208
- })
209
-
210
- describe('renderComponents 函数测试', () => {
211
- it('应该正确渲染单个组件', async () => {
212
- const TestComponent = defineComponent(async function TestComponent(props: { name: string }, context: ComponentContext) {
213
- return `Hello ${props.name}`
214
- }, 'test')
215
-
216
- const componentMap = new Map([['test', TestComponent]])
217
- const options: SendOptions = {
218
- content: '<test name="John" />',
219
- type: 'text',
220
- context: 'test',
221
- bot: 'test'
222
- }
223
-
224
- const result = await renderComponents(componentMap, options)
225
- expect(contentToString(result.content)).toContain('Hello John')
226
- })
227
-
228
- it('应该正确渲染多个组件', async () => {
229
- const Component1 = defineComponent(async function Component1(props: { text: string }, context: ComponentContext) {
230
- return `[${props.text}]`
231
- }, 'comp1')
232
-
233
- const Component2 = defineComponent(async function Component2(props: { number: number }, context: ComponentContext) {
234
- return `{${props.number}}`
235
- }, 'comp2')
236
-
237
- const componentMap = new Map([
238
- ['comp1', Component1],
239
- ['comp2', Component2]
240
- ])
241
-
242
- const options: SendOptions = {
243
- content: '<comp1 text="Hello" /> <comp2 number={42} />',
244
- type: 'text',
245
- context: 'test',
246
- bot: 'test'
247
- }
248
-
249
- const result = await renderComponents(componentMap, options)
250
- const contentStr = contentToString(result.content)
251
- expect(contentStr).toContain('[Hello]')
252
- expect(contentStr).toContain('{42}')
253
- })
254
-
255
- it('应该正确处理嵌套组件', async () => {
256
- const OuterComponent = defineComponent(async function OuterComponent(props: { title: string }, context: ComponentContext) {
257
- return `标题: ${props.title}`
258
- }, 'outer')
259
-
260
- const InnerComponent = defineComponent(async function InnerComponent(props: { content: string }, context: ComponentContext) {
261
- return `Content: ${props.content}`
262
- }, 'inner')
263
-
264
- const componentMap = new Map([
265
- ['outer', OuterComponent],
266
- ['inner', InnerComponent]
267
- ])
268
-
269
- const options: SendOptions = {
270
- content: '<outer title="Test"><inner content="Nested" /></outer>',
271
- type: 'text',
272
- context: 'test',
273
- bot: 'test'
274
- }
275
-
276
- const result = await renderComponents(componentMap, options)
277
- // 现在嵌套组件渲染应该工作了
278
- expect(contentToString(result.content)).toContain('标题: Test')
279
- })
280
- })
281
-
282
- describe('表达式求值测试', () => {
283
- it('应该正确计算数学表达式', () => {
284
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
285
- return 'test'
286
- }, 'test')
287
-
288
- const template = '<test sum={1+2+3} product={2*3*4} />'
289
- const props = getProps(TestComponent, template, mockContext)
290
-
291
- expect(props.sum).toBe(6)
292
- expect(props.product).toBe(24)
293
- })
294
-
295
- it('应该正确处理比较表达式', () => {
296
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
297
- return 'test'
298
- }, 'test')
299
-
300
- const template = '<test greater={5>3} equal={2==2} />'
301
- const props = getProps(TestComponent, template, mockContext)
302
-
303
- expect(props.greater).toBe(true)
304
- expect(props.equal).toBe(true)
305
- })
306
-
307
- it('应该正确处理三元运算符', () => {
308
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
309
- return 'test'
310
- }, 'test')
311
-
312
- const template = '<test result={5>3 ? "yes" : "no"} />'
313
- const props = getProps(TestComponent, template, mockContext)
314
-
315
- expect(props.result).toBe('yes')
316
- })
317
-
318
- it('应该正确处理数组和对象表达式', () => {
319
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
320
- return 'test'
321
- }, 'test')
322
-
323
- const template = '<test items={[1,2,3]} config={{name:"test",value:42}} />'
324
- const props = getProps(TestComponent, template, mockContext)
325
-
326
- expect(props.items).toEqual([1, 2, 3])
327
- expect(props.config).toEqual({ name: 'test', value: 42 })
328
- })
329
- })
330
-
331
- describe('错误处理测试', () => {
332
- it('应该正确处理无效的组件模板', () => {
333
- const TestComponent = defineComponent(async function TestComponent(props: any, context: ComponentContext) {
334
- return 'test'
335
- }, 'test')
336
-
337
- const template = 'invalid template'
338
- const props = getProps(TestComponent, template, mockContext)
339
-
340
- expect(props).toEqual({})
341
- })
342
-
343
- it('应该正确处理组件渲染错误', async () => {
344
- const ErrorComponent = defineComponent(async function ErrorComponent(props: any, context: ComponentContext) {
345
- throw new Error('Test error')
346
- }, 'error')
347
-
348
- const componentMap = new Map([['error', ErrorComponent]])
349
- const options: SendOptions = {
350
- content: '<error />',
351
- type: 'text',
352
- context: 'test',
353
- bot: 'test'
354
- }
355
-
356
- const result = await renderComponents(componentMap, options)
357
- // 错误会被捕获并转换为错误消息
358
- expect(contentToString(result.content)).toMatch(/\[error Error: Test error\]/)
359
- })
360
- })
361
- })