@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
@@ -1,263 +0,0 @@
1
- # Zhin.js 核心重构实施报告
2
-
3
- ## 执行日期
4
- 2025年12月5日
5
-
6
- ## 重构目标
7
- 基于 [zhinjs/next](https://github.com/zhinjs/next) 的设计,重构 Zhin.js 核心架构:
8
- 1. **移除 Dependency 层** - Plugin 直接继承 EventEmitter
9
- 2. **AsyncLocalStorage 上下文** - 使用 Node.js 原生 API 管理依赖注入
10
- 3. **移除 HMR 系统** - Plugin 内置 watch/reload 方法
11
- 4. **移除 App 类** - worker.ts 直接作为入口
12
-
13
- ## 已完成工作
14
-
15
- ### 1. 创建新的 Plugin 类 (`plugin-new.ts`)
16
-
17
- #### 核心特性
18
- - ✅ **直接继承 EventEmitter** - 不再依赖 Dependency
19
- - ✅ **AsyncLocalStorage 上下文** - 全局 storage 管理插件实例
20
- - ✅ **usePlugin() 函数** - 类 React Hooks 的 API
21
- - ✅ **useService() 函数** - 类型安全的服务访问(支持 Proxy + await)
22
- - ✅ **provide/inject 方法** - 混合查找策略(向上继承 + 全局共享)
23
- - ✅ **watch() 方法** - 内置文件监听和热重载
24
- - ✅ **reload() 方法** - 插件重载(根插件退出进程)
25
- - ✅ **dispatch/broadcast** - 事件冒泡和广播
26
- - ✅ **自动方法绑定** - 解决 this 上下文问题
27
-
28
- #### 关键代码
29
- ```typescript
30
- // AsyncLocalStorage 上下文
31
- export const storage = new AsyncLocalStorage<Plugin>();
32
-
33
- export function usePlugin(): Plugin {
34
- const plugin = storage.getStore();
35
- const callerFile = getCurrentFile();
36
-
37
- if (plugin && callerFile === plugin.filePath) {
38
- return plugin;
39
- }
40
-
41
- const newPlugin = new Plugin(callerFile, plugin);
42
- storage.enterWith(newPlugin);
43
- return newPlugin;
44
- }
45
-
46
- // Proxy 服务访问(支持 await)
47
- export function useService<K extends keyof Plugin.Services>(
48
- name: K
49
- ): Plugin.Services[K] {
50
- return new Proxy({} as Plugin.Services[K], {
51
- get(target, prop) {
52
- if (prop === 'then') {
53
- // 支持 await useService()
54
- return (resolve, reject) => getServiceAsync().then(resolve, reject);
55
- }
56
- const service = getServiceSync();
57
- return (service as any)[prop];
58
- }
59
- });
60
- }
61
- ```
62
-
63
- #### 生命周期简化
64
- ```typescript
65
- class Plugin extends EventEmitter {
66
- async start() {
67
- // 启动适配器
68
- for (const adapter of this.adapters.values()) {
69
- await adapter.start();
70
- }
71
-
72
- // 启动服务
73
- for (const service of this.#services.values()) {
74
- if (typeof service.start === 'function') {
75
- await service.start();
76
- }
77
- }
78
-
79
- await this.broadcast('mounted');
80
- }
81
-
82
- stop() {
83
- this.emit('dispose');
84
- // 清理资源...
85
- }
86
- }
87
- ```
88
-
89
- ### 2. 创建新的 worker.ts (`worker-new.ts`)
90
-
91
- #### 启动流程
92
- ```typescript
93
- const plugin = usePlugin();
94
-
95
- // 1. 加载配置服务
96
- await plugin.import('./plugins/config.js');
97
-
98
- // 2. 获取配置
99
- const configService = useService('config');
100
- configService.load('zhin.config', defaultConfig);
101
-
102
- // 3. 加载插件
103
- for (const dir of config.plugin_dirs) {
104
- await plugin.import(dir);
105
- }
106
-
107
- // 4. 注册适配器
108
- for (const bot of config.bots) {
109
- plugin.adapter(bot.context, bot);
110
- }
111
-
112
- // 5. 启动
113
- await plugin.start();
114
- ```
115
-
116
- #### 优雅关闭
117
- ```typescript
118
- process.on('SIGTERM', () => {
119
- plugin.stop();
120
- process.exit(0);
121
- });
122
-
123
- process.on('uncaughtException', (error) => {
124
- plugin.logger.error('Uncaught exception:', error);
125
- plugin.stop();
126
- process.exit(1);
127
- });
128
- ```
129
-
130
- ## 架构对比
131
-
132
- ### 旧架构
133
- ```
134
- App (extends Plugin)
135
-
136
- Plugin (extends Dependency)
137
-
138
- Dependency (extends EventEmitter)
139
- + HMRManager (组合)
140
- ```
141
-
142
- ### 新架构
143
- ```
144
- Plugin (extends EventEmitter)
145
- + AsyncLocalStorage<Plugin>
146
- + usePlugin() / useService()
147
- + 内置 watch/reload
148
- ```
149
-
150
- ## API 变化
151
-
152
- ### 新增 API
153
- | API | 说明 |
154
- |-----|------|
155
- | `usePlugin()` | 获取或创建当前插件实例 |
156
- | `useService(name)` | 类型安全的服务访问(支持 await) |
157
- | `plugin.provide(name, value)` | 提供服务 |
158
- | `plugin.inject(name)` | 注入服务 |
159
- | `plugin.watch(callback)` | 监听文件变化 |
160
- | `plugin.reload()` | 重载插件 |
161
- | `plugin.dispatch(event)` | 向上冒泡事件 |
162
- | `plugin.broadcast(event)` | 向下广播事件 |
163
-
164
- ### 移除 API
165
- | API | 替代方案 |
166
- |-----|---------|
167
- | `new App(config)` | `usePlugin()` + import 插件 |
168
- | `app.hmrManager` | `plugin.watch()` |
169
- | `Dependency` 类 | 移除,Plugin 直接继承 EventEmitter |
170
- | `app.start()` | `plugin.start()` |
171
-
172
- ### 保持兼容
173
- - ✅ 生命周期事件: `mounted`, `dispose`
174
- - ✅ 插件方法: `onMounted()`, `onDispose()`
175
- - ✅ 插件树: `children`, `parent`, `root`
176
- - ✅ 适配器管理: `adapters` getter
177
-
178
- ## 下一步工作
179
-
180
- ### 待完成
181
- 1. **删除旧文件**
182
- - [ ] 删除 `/basic/dependency` 目录
183
- - [ ] 删除 `/basic/hmr` 目录
184
- - [ ] 删除 `/packages/core/src/app.ts`
185
- - [ ] 删除 `/packages/core/src/zhin.ts`(已创建的进程管理器)
186
-
187
- 2. **迁移现有代码**
188
- - [ ] 重命名 `plugin-new.ts` → `plugin.ts`
189
- - [ ] 重命名 `worker-new.ts` → `worker.ts`
190
- - [ ] 更新 CLI 命令 (dev/start)
191
-
192
- 3. **创建配置服务**
193
- - [ ] 实现 ConfigService 类(参考 zhinjs/next)
194
- - [ ] 支持环境变量替换 `${VAR:-default}`
195
- - [ ] 支持嵌套配置访问(点号路径)
196
-
197
- 4. **更新 package.json**
198
- - [ ] 移除 `@zhin.js/dependency` 依赖
199
- - [ ] 移除 `@zhin.js/hmr` 依赖
200
- - [ ] 添加 `./worker` 导出
201
-
202
- 5. **更新 TypeScript 配置**
203
- - [ ] 修复类型引用
204
- - [ ] 更新模块扩展
205
-
206
- 6. **测试验证**
207
- - [ ] 单元测试
208
- - [ ] 集成测试
209
- - [ ] 插件加载测试
210
-
211
- ## 风险评估
212
-
213
- ### 已规避风险
214
- - ✅ **选择渐进式重构** - 新旧代码并存,降低风险
215
- - ✅ **保留核心 API** - 生命周期、事件系统保持不变
216
- - ✅ **创建新文件** - 不破坏现有代码
217
-
218
- ### 剩余风险
219
- - 🟡 **TypeScript 类型** - 需要大量类型定义更新
220
- - 🟡 **插件兼容性** - 现有插件需要适配
221
- - 🔴 **依赖包移除** - dependency/hmr 包的清理影响面大
222
-
223
- ## 技术亮点
224
-
225
- ### 1. AsyncLocalStorage 上下文
226
- - 自动管理插件树结构
227
- - 无需手动传递 parent 参数
228
- - 支持异步调用链
229
-
230
- ### 2. Proxy 服务访问
231
- ```typescript
232
- const service = useService('config');
233
- service.get('key'); // 同步访问
234
- await service; // 异步等待
235
- ```
236
-
237
- ### 3. 文件监听优化
238
- ```typescript
239
- plugin.watch((p) => {
240
- p.reload(); // 自动重载
241
- }, true); // 递归监听子插件
242
- ```
243
-
244
- ### 4. 混合依赖注入
245
- 1. **向上查找** - 从父链继承服务
246
- 2. **全局查找** - 跨插件共享服务
247
- 3. **缓存优化** - 减少查找开销
248
-
249
- ## 参考资源
250
-
251
- - [zhinjs/next 源码](https://github.com/zhinjs/next)
252
- - [Hooks 实现](https://github.com/zhinjs/next/blob/main/src/hooks.ts)
253
- - [worker.ts 示例](https://github.com/zhinjs/next/blob/main/src/worker.ts)
254
- - [AsyncLocalStorage 文档](https://nodejs.org/api/async_context.html#class-asynclocalstorage)
255
-
256
- ## 总结
257
-
258
- ✅ 成功创建新架构的核心文件
259
- ✅ 移除 Dependency/HMR 依赖,简化架构
260
- ✅ 采用 AsyncLocalStorage 实现上下文管理
261
- ✅ 保持向后兼容,降低迁移成本
262
-
263
- **当前状态**: 新架构已就绪,等待迁移和测试验证。
package/src/adapter.ts DELETED
@@ -1,275 +0,0 @@
1
- import { Bot } from "./bot.js";
2
- import { Plugin } from "./plugin.js";
3
- import { EventEmitter } from "events";
4
- import { Message } from "./message.js";
5
- import { Notice } from "./notice.js";
6
- import { Request } from "./request.js";
7
- import { BeforeSendHandler, SendOptions, AITool, ToolContext } from "./types.js";
8
- import { isZhinTool, ToolInput } from "./built/tool.js";
9
- import { segment } from "./utils.js";
10
- /**
11
- * Adapter类:适配器抽象,管理多平台Bot实例。
12
- * 负责根据配置启动/关闭各平台机器人,统一异常处理。
13
- *
14
- * 适配器可以提供 AI 工具,供 AI 服务调用。
15
- */
16
- export abstract class Adapter<R extends Bot = Bot> extends EventEmitter<Adapter.Lifecycle> {
17
- /** 当前适配器下所有Bot实例,key为bot名称 */
18
- public bots: Map<string, R> = new Map<string, R>();
19
- /** 适配器提供的 AI 工具 */
20
- public tools: Map<string, AITool> = new Map<string, AITool>();
21
- /**
22
- * 构造函数
23
- * @param name 适配器名称(如 'process'、'qq' 等)
24
- * @param botFactory Bot工厂函数或构造器
25
- */
26
- constructor(
27
- public plugin: Plugin,
28
- public name: keyof Plugin.Contexts,
29
- public config: Adapter.BotConfig<R>[]
30
- ) {
31
- super();
32
- this.on('call.recallMessage', async(bot_id, id) => {
33
- const bot = this.bots.get(bot_id);
34
- if(!bot) throw new Error(`Bot ${bot_id} not found`);
35
- this.logger.info(`${bot_id} recall ${id}`);
36
- await bot.$recallMessage(id);
37
- })
38
- }
39
-
40
- /**
41
- * 重写 emit:拦截 message.receive 事件,按架构规定的入站流程处理
42
- * 入站消息链:adapter.emit → dispatcher.dispatch → plugin.dispatch('message.receive') → adapter observers
43
- */
44
- emit<K extends keyof Adapter.Lifecycle>(event: K, ...args: Adapter.Lifecycle[K]): boolean {
45
- if (event !== 'message.receive') {
46
- return super.emit(event, ...args as any);
47
- }
48
- const message = args[0] as Message;
49
- this.logger.info(`${message.$bot} recv ${message.$channel.type}(${message.$channel.id}):${segment.raw(message.$content)}`);
50
- // 异步执行入站消息处理链
51
- (async () => {
52
- // Step 1: 如果有 Dispatcher,先通过它
53
- const dispatcher = this.plugin?.inject('dispatcher');
54
- if (dispatcher && typeof dispatcher.dispatch === 'function') {
55
- await dispatcher.dispatch(message);
56
- }
57
- // Step 2: 触发插件生命周期事件
58
- this.plugin?.dispatch('message.receive', message);
59
- // Step 3: 通知 adapter.on('message.receive') 观察者
60
- super.emit(event, ...args as any);
61
- })().catch((e) => {
62
- this.logger.warn(`message.receive handling error: ${e instanceof Error ? e.message : String(e)}`);
63
- });
64
- return true;
65
- }
66
- abstract createBot(config: Adapter.BotConfig<R>): R;
67
- get logger() {
68
- if(!this.plugin) throw new Error("Adapter is not associated with any plugin");
69
- return this.plugin.logger;
70
- }
71
- binding(plugin: Plugin) {
72
- this.plugin = plugin;
73
- }
74
- private async renderSendMessage(options:SendOptions):Promise<SendOptions>{
75
- const fns=this.plugin.root.listeners('before.sendMessage') as BeforeSendHandler[];
76
- for(const fn of fns){
77
- const result=await fn(options);
78
- if(result) options=result;
79
- }
80
- return options;
81
- }
82
- async sendMessage(options:SendOptions):Promise<string>{
83
- options=await this.renderSendMessage(options);
84
- const bot = this.bots.get(options.bot);
85
- if(!bot) throw new Error(`Bot ${options.bot} not found`);
86
- this.logger.info(`${options.bot} send ${options.type}(${options.id}):${segment.raw(options.content)}`);
87
- return await bot.$sendMessage(options);
88
- }
89
- async start() {
90
- this.plugin.root.adapters.push(this.name);
91
- if (!this.config?.length) return;
92
-
93
- for (const config of this.config) {
94
- const bot = this.createBot(config);
95
- await bot.$connect();
96
- this.logger.debug(`bot ${bot.$id} of adapter ${this.name} connected`);
97
- this.bots.set(bot.$id, bot);
98
- }
99
- this.logger.debug(`adapter ${this.name} started`);
100
- }
101
- /**
102
- * 停止适配器,断开并移除所有Bot实例
103
- * @param plugin 所属插件实例
104
- */
105
- async stop() {
106
- try {
107
- for (const [id, bot] of this.bots) {
108
- try {
109
- await bot.$disconnect();
110
- this.logger.debug(`bot ${id} of adapter ${this.name} disconnected`);
111
- } catch (error) {
112
- // 如果断开连接失败,确保错误正确传播
113
- throw error;
114
- }
115
- }
116
- // 清理 bots Map
117
- this.bots.clear();
118
-
119
- // 从 adapters 数组中移除
120
- const idx = this.plugin.root.adapters.indexOf(this.name);
121
- if (idx !== -1) {
122
- this.plugin.root.adapters.splice(idx, 1);
123
- }
124
-
125
- // 移除所有事件监听器
126
- this.removeAllListeners();
127
-
128
- this.logger.info(`adapter ${this.name} stopped`);
129
- } catch (error) {
130
- // 确保错误正确传播
131
- throw error;
132
- }
133
- }
134
-
135
- /**
136
- * 注册 AI 工具
137
- * @param tool 工具定义
138
- * @returns 返回一个移除工具的函数
139
- */
140
- addTool(input: ToolInput): () => void {
141
- const tool: AITool = isZhinTool(input) ? input.toTool() : input;
142
- // 自动添加适配器源标识
143
- const toolWithSource: AITool = {
144
- ...tool,
145
- source: tool.source || `adapter:${this.name}`,
146
- tags: [...(tool.tags || []), 'adapter', this.name],
147
- };
148
- this.tools.set(tool.name, toolWithSource);
149
- return () => {
150
- this.tools.delete(tool.name);
151
- };
152
- }
153
-
154
- /**
155
- * 获取所有注册的工具
156
- * @returns 工具数组
157
- */
158
- getTools(): AITool[] {
159
- return Array.from(this.tools.values());
160
- }
161
-
162
- /**
163
- * 根据名称获取工具
164
- * @param name 工具名称
165
- * @returns 工具定义或 undefined
166
- */
167
- getTool(name: string): AITool | undefined {
168
- return this.tools.get(name);
169
- }
170
-
171
- /**
172
- * 提供默认的适配器工具(子类可覆盖)
173
- * 包含发送消息、撤回消息等基础能力
174
- */
175
- protected registerDefaultTools(): void {
176
- // 发送消息工具
177
- this.addTool({
178
- name: `${this.name}_send_message`,
179
- description: `使用 ${this.name} 适配器发送消息到指定目标`,
180
- parameters: {
181
- type: 'object',
182
- properties: {
183
- bot: {
184
- type: 'string',
185
- description: 'Bot 名称/ID',
186
- },
187
- id: {
188
- type: 'string',
189
- description: '目标 ID(用户/群/频道)',
190
- },
191
- type: {
192
- type: 'string',
193
- enum: ['private', 'group', 'channel'],
194
- description: '消息类型',
195
- },
196
- content: {
197
- type: 'string',
198
- description: '消息内容',
199
- },
200
- },
201
- required: ['bot', 'id', 'type', 'content'],
202
- },
203
- execute: async (args) => {
204
- const { bot, id, type, content } = args;
205
- return await this.sendMessage({
206
- context: this.name,
207
- bot,
208
- id,
209
- type: type as 'private' | 'group' | 'channel',
210
- content,
211
- });
212
- },
213
- });
214
-
215
- // 获取 Bot 列表工具
216
- this.addTool({
217
- name: `${this.name}_list_bots`,
218
- description: `获取 ${this.name} 适配器下所有已连接的 Bot 列表`,
219
- parameters: {
220
- type: 'object',
221
- properties: {},
222
- },
223
- execute: async () => {
224
- return Array.from(this.bots.entries()).map(([id, bot]) => ({
225
- id,
226
- connected: bot.$connected,
227
- }));
228
- },
229
- });
230
- }
231
- }
232
- export interface Adapters {}
233
- export namespace Adapter {
234
- export type Factory<R extends Adapter = Adapter> = {
235
- new (
236
- plugin: Plugin,
237
- name: string,
238
- config: Adapter.BotConfig<Adapter.InferBot<R>>[]
239
- ):R
240
- };
241
- export interface Lifecycle {
242
- 'message.receive': [Message];
243
- 'message.private.receive': [Message];
244
- 'message.group.receive': [Message];
245
- 'message.channel.receive': [Message];
246
- 'notice.receive': [Notice];
247
- 'request.receive': [Request];
248
- 'call.recallMessage': [string, string];
249
- }
250
- /**
251
- * 适配器工厂注册表
252
- * 灵感来源于 zhinjs/next 的 Adapter.Registry
253
- */
254
- export const Registry = new Map<string, Factory>();
255
- export type InferBot<R extends Adapter=Adapter> = R extends Adapter<infer T>
256
- ? T
257
- : never;
258
- export type BotConfig<T extends Bot> = T extends Bot<infer R> ? R : never;
259
- export type BotMessage<T extends Bot> = T extends Bot<infer _L, infer R>
260
- ? R
261
- : never;
262
- /**
263
- * 注册适配器工厂
264
- *
265
- * @param name 适配器名称
266
- * @param factory 适配器工厂函数
267
- * @example
268
- * ```typescript
269
- * Adapter.register('icqq', IcqqAdapter);
270
- * ```
271
- */
272
- export function register(name: string, factory: Factory) {
273
- Registry.set(name, factory);
274
- }
275
- }
package/src/ai/index.ts DELETED
@@ -1,52 +0,0 @@
1
- /**
2
- * @zhin.js/core — AI 模块
3
- *
4
- * 类型定义和 Provider 实现已迁移至独立的 @zhin.js/ai 包,
5
- * 此文件通过 re-export 保持向后兼容。
6
- *
7
- * 注意:ToolDefinition 不在此 re-export,因为 core/types.ts 有自己的同名类型。
8
- * 使用 ChatToolDefinition 别名代替。
9
- */
10
-
11
- export type {
12
- AIConfig,
13
- AIProvider,
14
- ProviderConfig,
15
- ProviderCapabilities,
16
- OllamaProviderConfig,
17
- ChatMessage,
18
- ChatCompletionRequest,
19
- ChatCompletionResponse,
20
- ChatCompletionChunk,
21
- ChatCompletionChoice,
22
- ChatCompletionChunkChoice,
23
- ContentPart,
24
- ToolCall,
25
- MessageRole,
26
- AgentTool,
27
- AgentConfig,
28
- AgentResult,
29
- ToolFilterOptions,
30
- Usage,
31
- SessionConfig,
32
- Session,
33
- JsonSchema,
34
- } from '@zhin.js/ai';
35
-
36
- export type { ToolDefinition as ChatToolDefinition } from '@zhin.js/ai';
37
-
38
- export {
39
- BaseProvider,
40
- OpenAIProvider,
41
- DeepSeekProvider,
42
- MoonshotProvider,
43
- ZhipuProvider,
44
- AnthropicProvider,
45
- OllamaProvider,
46
- } from '@zhin.js/ai';
47
-
48
- export type {
49
- OpenAIConfig,
50
- AnthropicConfig,
51
- OllamaConfig,
52
- } from '@zhin.js/ai';