@zhin.js/core 1.0.26 → 1.0.28

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 (68) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/lib/ai/agent.d.ts.map +1 -1
  3. package/lib/ai/agent.js +4 -0
  4. package/lib/ai/agent.js.map +1 -1
  5. package/lib/ai/bootstrap.d.ts +82 -0
  6. package/lib/ai/bootstrap.d.ts.map +1 -0
  7. package/lib/ai/bootstrap.js +199 -0
  8. package/lib/ai/bootstrap.js.map +1 -0
  9. package/lib/ai/builtin-tools.d.ts +36 -0
  10. package/lib/ai/builtin-tools.d.ts.map +1 -0
  11. package/lib/ai/builtin-tools.js +509 -0
  12. package/lib/ai/builtin-tools.js.map +1 -0
  13. package/lib/ai/compaction.d.ts +132 -0
  14. package/lib/ai/compaction.d.ts.map +1 -0
  15. package/lib/ai/compaction.js +370 -0
  16. package/lib/ai/compaction.js.map +1 -0
  17. package/lib/ai/hooks.d.ts +143 -0
  18. package/lib/ai/hooks.d.ts.map +1 -0
  19. package/lib/ai/hooks.js +108 -0
  20. package/lib/ai/hooks.js.map +1 -0
  21. package/lib/ai/index.d.ts +6 -0
  22. package/lib/ai/index.d.ts.map +1 -1
  23. package/lib/ai/index.js +6 -0
  24. package/lib/ai/index.js.map +1 -1
  25. package/lib/ai/init.d.ts.map +1 -1
  26. package/lib/ai/init.js +120 -3
  27. package/lib/ai/init.js.map +1 -1
  28. package/lib/ai/types.d.ts +2 -0
  29. package/lib/ai/types.d.ts.map +1 -1
  30. package/lib/ai/zhin-agent.d.ts +28 -1
  31. package/lib/ai/zhin-agent.d.ts.map +1 -1
  32. package/lib/ai/zhin-agent.js +196 -57
  33. package/lib/ai/zhin-agent.js.map +1 -1
  34. package/lib/built/config.d.ts +10 -0
  35. package/lib/built/config.d.ts.map +1 -1
  36. package/lib/built/config.js +54 -3
  37. package/lib/built/config.js.map +1 -1
  38. package/lib/built/tool.d.ts +6 -0
  39. package/lib/built/tool.d.ts.map +1 -1
  40. package/lib/built/tool.js +12 -0
  41. package/lib/built/tool.js.map +1 -1
  42. package/lib/cron.d.ts +0 -27
  43. package/lib/cron.d.ts.map +1 -1
  44. package/lib/cron.js +28 -27
  45. package/lib/cron.js.map +1 -1
  46. package/lib/types-generator.js +1 -1
  47. package/lib/types-generator.js.map +1 -1
  48. package/lib/types.d.ts +7 -0
  49. package/lib/types.d.ts.map +1 -1
  50. package/package.json +6 -6
  51. package/src/ai/agent.ts +6 -0
  52. package/src/ai/bootstrap.ts +263 -0
  53. package/src/ai/builtin-tools.ts +569 -0
  54. package/src/ai/compaction.ts +529 -0
  55. package/src/ai/hooks.ts +223 -0
  56. package/src/ai/index.ts +58 -0
  57. package/src/ai/init.ts +127 -3
  58. package/src/ai/types.ts +2 -0
  59. package/src/ai/zhin-agent.ts +226 -54
  60. package/src/built/config.ts +53 -3
  61. package/src/built/tool.ts +12 -0
  62. package/src/cron.ts +28 -27
  63. package/src/types-generator.ts +1 -1
  64. package/src/types.ts +8 -0
  65. package/tests/adapter.test.ts +1 -1
  66. package/tests/config.test.ts +2 -2
  67. package/test/minimal-bot.ts +0 -31
  68. package/test/stress-test.ts +0 -123
@@ -415,7 +415,7 @@ describe('Adapter Core Functionality', () => {
415
415
  name: 'dispatcher',
416
416
  description: 'mock dispatcher',
417
417
  value: {
418
- dispatch: (msg: any) => { dispatchCalled = true },
418
+ dispatch: (msg: any) => { dispatchCalled = true; return Promise.resolve() },
419
419
  },
420
420
  } as any)
421
421
 
@@ -357,8 +357,8 @@ describe('ConfigFeature', () => {
357
357
  const json = feature.toJSON()
358
358
  expect(json.name).toBe('config')
359
359
  expect(json.count).toBe(2)
360
- expect(json.items[0]).toEqual({ name: 'debug', defaultValue: false })
361
- expect(json.items[1]).toEqual({ name: 'port', defaultValue: 8080 })
360
+ expect(json.items[0]).toEqual({ name: 'debug' })
361
+ expect(json.items[1]).toEqual({ name: 'port' })
362
362
  })
363
363
 
364
364
  it('toJSON(pluginName) 应按插件过滤', () => {
@@ -1,31 +0,0 @@
1
- import { App } from '../src/app';
2
- import { LogLevel } from '@zhin.js/logger';
3
- import { fileURLToPath } from 'url';
4
- import * as path from 'path';
5
-
6
- const __filename = fileURLToPath(import.meta.url);
7
- const __dirname = path.dirname(__filename);
8
-
9
- async function runMinimalBot() {
10
- const app = new App({
11
- log_level: LogLevel.INFO,
12
- plugin_dirs: [path.join(__dirname, 'plugins')],
13
- plugins: [],
14
- bots: [],
15
- debug: true
16
- });
17
-
18
- console.log('Starting Minimal Bot...');
19
- await app.start();
20
- console.log('Minimal Bot Started');
21
-
22
- // Simulate a message
23
- // await app.receiveMessage(...)
24
-
25
- await app.stop();
26
- }
27
-
28
- if (process.argv[1] === __filename) {
29
- runMinimalBot().catch(console.error);
30
- }
31
-
@@ -1,123 +0,0 @@
1
- import { App } from '../src/app';
2
- import { LogLevel } from '@zhin.js/logger';
3
- import { fileURLToPath } from 'url';
4
- import * as path from 'path';
5
- import * as fs from 'fs';
6
- // import { Message } from '../src/message'; // Message is a type/namespace, not a class
7
-
8
- const __filename = fileURLToPath(import.meta.url);
9
- const __dirname = path.dirname(__filename);
10
-
11
- async function runStressTest() {
12
- const PLUGIN_COUNT = 50;
13
- const MSG_COUNT = 10000;
14
- const WORK_DIR = path.join(__dirname, 'stress_plugins');
15
-
16
- console.log('Starting Core Stress Test');
17
-
18
- // Setup workspace
19
- if (fs.existsSync(WORK_DIR)) fs.rmSync(WORK_DIR, { recursive: true, force: true });
20
- fs.mkdirSync(WORK_DIR, { recursive: true });
21
-
22
- // Generate plugins
23
- let loadedCount = 0;
24
- for (let i = 0; i < PLUGIN_COUNT; i++) {
25
- fs.writeFileSync(path.join(WORK_DIR, `plugin-${i}.ts`), `
26
- import { useContext } from '${path.resolve(__dirname, '../src/plugin').replace(/\\/g, '/')}';
27
-
28
- export function install(ctx) {
29
- ctx.on('message.receive', (msg) => {
30
- // console.log('Plugin ${i} received message');
31
- });
32
-
33
- ctx.middleware(async (msg, next) => {
34
- await next();
35
- });
36
- }
37
- `);
38
- }
39
-
40
- const CONFIG_FILE = path.join(WORK_DIR, 'zhin.test.yaml');
41
- // Write config to file to avoid reload overriding it with defaults
42
- const config = {
43
- log_level: LogLevel.WARN,
44
- plugin_dirs: [WORK_DIR],
45
- plugins: Array.from({ length: PLUGIN_COUNT }, (_, i) => `plugin-${i}`),
46
- bots: [],
47
- debug: false
48
- };
49
-
50
- // Simple YAML stringify since we can't require 'yaml' easily in ESM without import
51
- const yamlString = `
52
- log_level: ${config.log_level}
53
- debug: ${config.debug}
54
- plugin_dirs:
55
- ${config.plugin_dirs.map(d => ` - ${d}`).join('\n')}
56
- plugins:
57
- ${config.plugins.map(p => ` - ${p}`).join('\n')}
58
- bots: []
59
- `;
60
-
61
- fs.writeFileSync(CONFIG_FILE, yamlString);
62
-
63
- const app = new App(CONFIG_FILE);
64
-
65
- console.log('Starting App...');
66
- const startBoot = performance.now();
67
- await app.start();
68
- console.log(`App started in ${(performance.now() - startBoot).toFixed(2)}ms`);
69
-
70
- // Verify plugins loaded
71
- const loadedPlugins = app.hmrManager.dependencyList.length; // App itself + plugins
72
- console.log(`Loaded Dependencies: ${loadedPlugins}`);
73
- if (loadedPlugins < PLUGIN_COUNT) {
74
- console.warn(`WARNING: Only ${loadedPlugins} dependencies loaded (expected >= ${PLUGIN_COUNT})`);
75
- }
76
-
77
- const initialMem = process.memoryUsage();
78
- console.log(`Initial RSS: ${(initialMem.rss / 1024 / 1024).toFixed(2)} MB`);
79
-
80
- console.log(`Sending ${MSG_COUNT} messages...`);
81
- const startMsg = performance.now();
82
-
83
- // Mock message factory
84
- const createMockMsg = (id: number) => ({
85
- $id: `msg-${id}`,
86
- $adapter: 'mock',
87
- $bot: 'bot1',
88
- $content: [{ type: 'text', data: { text: 'hello' } }],
89
- $sender: { id: 'user1' },
90
- $channel: { id: 'group1', type: 'group' as const },
91
- $timestamp: Date.now(),
92
- $raw: 'hello',
93
- $reply: async () => 'reply-id',
94
- $recall: async () => {},
95
- // Custom fields usually added by adapters
96
- raw_message: 'hello',
97
- message_type: 'group',
98
- sender: { id: 'user1' },
99
- });
100
-
101
- for (let i = 0; i < MSG_COUNT; i++) {
102
- const msg = createMockMsg(i);
103
- await app.receiveMessage(msg as any);
104
-
105
- if (i % 1000 === 0) {
106
- const mem = process.memoryUsage();
107
- process.stdout.write(`\rMsgs: ${i}, RSS: ${(mem.rss / 1024 / 1024).toFixed(2)} MB`);
108
- }
109
- }
110
-
111
- console.log(`\nProcessed ${MSG_COUNT} messages in ${(performance.now() - startMsg).toFixed(2)}ms`);
112
- console.log(`Avg: ${((performance.now() - startMsg) / MSG_COUNT).toFixed(3)}ms/msg`);
113
-
114
- const finalMem = process.memoryUsage();
115
- console.log(`Final RSS: ${(finalMem.rss / 1024 / 1024).toFixed(2)} MB`);
116
- console.log(`Memory Delta: ${((finalMem.rss - initialMem.rss) / 1024 / 1024).toFixed(2)} MB`);
117
-
118
- await app.stop();
119
- fs.rmSync(WORK_DIR, { recursive: true, force: true });
120
- }
121
-
122
- runStressTest().catch(console.error);
123
-