@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/src/prompt.ts DELETED
@@ -1,293 +0,0 @@
1
- import {AdapterMessage, Dict, MessageMiddleware, RegisteredAdapter} from './types.js';
2
- import { Plugin } from './plugin.js';
3
- import { Message } from './message.js';
4
- import { Schema } from '@zhin.js/schema';
5
- import { Adapter } from './adapter.js';
6
- /**
7
- * Prompt类:用于实现机器人与用户的交互式提问与输入收集。
8
- * 支持文本、数字、确认、列表、选项、Schema等多种输入类型,自动处理超时、默认值、格式化等。
9
- * 典型用法:await new Prompt(plugin, event).text('请输入内容')
10
- * @template P 适配器类型
11
- */
12
- export class Prompt<P extends RegisteredAdapter> {
13
- /**
14
- * 构造函数
15
- * @param plugin 所属插件实例
16
- * @param event 当前消息事件
17
- */
18
- constructor(private plugin:Plugin,private event: Message<AdapterMessage<P>>) {}
19
- /**
20
- * 获取当前会话唯一标识(适配器-机器人-频道-用户)
21
- */
22
- private getChannelAddress<P2 extends RegisteredAdapter>(event: Message<AdapterMessage<P2>>) {
23
- return `${event.$adapter}-${event.$bot}-${event.$channel.type}:${event.$channel.id}-${event.$sender.id}`;
24
- }
25
- /**
26
- * 通用提问方法,支持自定义格式化、超时、默认值等
27
- * @param config 提问配置
28
- */
29
- private prompt<T = any>(config: Prompt.Config<T>) {
30
- return new Promise<T>(async (resolve, reject) => {
31
- const id = await this.event.$reply(config.tips);
32
- this.middleware(
33
- input => {
34
- if (input instanceof Error) {
35
- this.event.$reply(input.message);
36
- if (config.defaultValue) resolve(config.defaultValue);
37
- else reject(input);
38
- return;
39
- }
40
- const adapter = this.plugin.inject(this.event.$adapter) as Adapter;
41
- adapter.emit('call.recallMessage',this.event.$bot, id);
42
- resolve(config.format(input));
43
- },
44
- config.timeout,
45
- config.timeoutText,
46
- );
47
- });
48
- }
49
- /**
50
- * 注册一次性消息中间件,等待用户输入或超时
51
- * @param callback 输入回调
52
- * @param timeout 超时时间(默认3分钟)
53
- * @param timeoutText 超时提示
54
- */
55
- middleware(callback: (input: string | Error) => any, timeout: number = 3 * 60 * 1000, timeoutText = '输入超时') {
56
- const middleware: MessageMiddleware<P> = async (event, next) => {
57
- if (this.getChannelAddress<P>(event) !== this.getChannelAddress<P>(this.event)) return next();
58
- callback(event.$raw);
59
- dispose();
60
- clearTimeout(timer);
61
- };
62
- const dispose = this.plugin.addMiddleware(middleware);
63
- const timer = setTimeout(() => {
64
- dispose();
65
- callback(new Error(timeoutText));
66
- }, timeout);
67
- }
68
- /**
69
- * 文本输入
70
- */
71
- async text(tips: string, timeout?: number, defaultValue = '', timeoutText?: string): Promise<string> {
72
- return this.prompt<string>({
73
- tips,
74
- defaultValue,
75
- timeoutText,
76
- timeout,
77
- format: (input: string) => input,
78
- });
79
- }
80
- /**
81
- * 任意输入
82
- */
83
- async any(tips: string, timeout?: number, defaultValue = '', timeoutText?: string) {
84
- return this.prompt<string>({
85
- tips,
86
- defaultValue,
87
- timeoutText,
88
- timeout,
89
- format: (input: string) => input,
90
- });
91
- }
92
- /**
93
- * 数字输入
94
- */
95
- async number(tips: string, timeout?: number, defaultValue = 0, timeoutText?: string): Promise<number> {
96
- return this.prompt<number>({
97
- tips,
98
- defaultValue,
99
- timeoutText,
100
- timeout,
101
- format: (input: string) => +input,
102
- });
103
- }
104
- /**
105
- * 确认输入(如 yes/no)
106
- */
107
- async confirm(
108
- tips: string,
109
- condition: string = 'yes',
110
- timeout?: number,
111
- defaultValue = false,
112
- timeoutText?: string,
113
- ): Promise<boolean> {
114
- return this.prompt<boolean>({
115
- tips: `${tips}\n输入“${condition}”以确认`,
116
- defaultValue,
117
- timeout,
118
- timeoutText,
119
- format: (input: string) => input === condition,
120
- });
121
- }
122
- /**
123
- * 列表输入,支持多值分隔
124
- */
125
- async list<T extends Prompt.SingleType = 'text'>(
126
- tips: string,
127
- config: Prompt.ListConfig<T> = { type: 'text' as T },
128
- timeoutText?: string,
129
- ): Promise<Prompt.Result<T>[]> {
130
- const separator = config.separator || ',';
131
- return this.prompt<Prompt.Result<T>[]>({
132
- tips: `${tips}\n值之间使用“${separator}”分隔`,
133
- defaultValue: config.defaultValue || [],
134
- timeout: config.timeout,
135
- timeoutText,
136
- format: (input: string) =>
137
- input.split(separator).map(v => {
138
- switch (config.type) {
139
- case 'boolean':
140
- return Boolean(v);
141
- case 'number':
142
- return +v;
143
- case 'text':
144
- return v;
145
- }
146
- }) as Prompt.Result<T>[],
147
- });
148
- }
149
- /**
150
- * 返回常量值(用于Schema)
151
- */
152
- async const<T = any>(value: T): Promise<T> {
153
- return value;
154
- }
155
- /**
156
- * 选项选择,支持单选/多选
157
- */
158
- async pick<T extends Prompt.SingleType, M extends boolean = false>(
159
- tips: string,
160
- config: Prompt.PickConfig<T, M>,
161
- timeoutText?: string,
162
- ): Promise<Prompt.PickResult<T, M>> {
163
- const moreTextArr = config.options.map((o, idx) => {
164
- return `${idx + 1}.${o.label}`;
165
- });
166
- const separator = config.separator || ',';
167
- if (config.multiple) moreTextArr.push(`多选请用“${separator}”分隔`);
168
- return this.prompt<Prompt.PickResult<T, M>>({
169
- tips: `${tips}\n${moreTextArr.join('\n')}`,
170
- defaultValue: config.defaultValue,
171
- timeout: config.timeout,
172
- timeoutText,
173
- format: (input: string) => {
174
- if (!config.multiple)
175
- return config.options.find((o, idx) => {
176
- return idx + 1 === +input;
177
- })?.value as Prompt.PickResult<T, M>;
178
- const pickIdx = input.split(separator).map(Number);
179
- return config.options
180
- .filter((o, idx) => {
181
- return pickIdx.includes(idx + 1);
182
- })
183
- .map(o => o.value) as Prompt.PickResult<T, M>;
184
- },
185
- });
186
- }
187
- /**
188
- * 基于Schema的选项选择
189
- */
190
- async pickValueWithSchema<T extends Schema>(schema: T): Promise<Schema.Types<T>> {
191
- return this.pick(schema.meta.description || schema.meta.key || 'Select an option', {
192
- type: '' as any,
193
- options: schema.meta.options!.map(o => ({
194
- label: o.label,
195
- value: o.value,
196
- })),
197
- multiple: schema.meta.multiple,
198
- defaultValue: schema.meta.default,
199
- });
200
- }
201
- /**
202
- * 批量Schema输入
203
- */
204
- async getValueWithSchemas<T extends Record<string, Schema>>(schemas: T): Promise<Schema.RecordTypes<T>> {
205
- const result: Dict = {};
206
- for (const key of Object.keys(schemas)) {
207
- const schema = schemas[key];
208
- result[key] = await this.getValueWithSchema(schema);
209
- }
210
- return result as Schema.RecordTypes<T>;
211
- }
212
- /**
213
- * 单个Schema输入,自动分发到不同类型
214
- */
215
- async getValueWithSchema<T extends Schema>(schema: T): Promise<Schema.Types<T>> {
216
- if (schema.meta.options) return this.pickValueWithSchema(schema);
217
- switch (schema.meta.type) {
218
- case 'number':
219
- return (await this.number(schema.meta.description || schema.meta.key || 'Enter a number')) as Schema.Types<T>;
220
- case 'string':
221
- return (await this.text(schema.meta.description || schema.meta.key || 'Enter text')) as Schema.Types<T>;
222
- case 'boolean':
223
- return (await this.confirm(schema.meta.description || schema.meta.key || 'Confirm')) as Schema.Types<T>;
224
- case 'object':
225
- if (schema.meta.description) await this.event.$reply(schema.meta.description);
226
- if (!schema.options.object) throw new Error('Object schema missing object definition');
227
- return (await this.getValueWithSchemas(schema.options.object)) as Schema.Types<T>;
228
- case 'date':
229
- return await this.prompt({
230
- tips: schema.meta.description || schema.meta.key || 'Enter a date',
231
- defaultValue: schema.meta.default || new Date(),
232
- format: (input: string) => new Date(input) as Schema.Types<T>,
233
- });
234
- case 'regexp':
235
- return await this.prompt({
236
- tips: schema.meta.description || schema.meta.key || 'Enter a regex pattern',
237
- defaultValue: schema.meta.default || '',
238
- format: (input: string) => new RegExp(input) as Schema.Types<T>,
239
- });
240
- case 'const':
241
- return await this.const(schema.meta.default!);
242
- case 'list':
243
- const inner = schema.options.inner!;
244
- if (!['string', 'boolean', 'number'].includes(inner.meta.type))
245
- throw new Error(`unsupported inner type :${inner.meta.type}`);
246
- return (await this.list(schema.meta.description || schema.meta.key || 'Enter list items', {
247
- type: inner.meta.type === 'string' ? 'text' : (inner.meta.type as Prompt.SingleType),
248
- defaultValue: schema.meta.default,
249
- })) as Schema.Types<T>;
250
- case 'dict':
251
- default:
252
- throw new Error(`Unsupported schema input type: ${schema.meta.type}`);
253
- }
254
- }
255
- }
256
- /**
257
- * Prompt命名空间:类型辅助定义
258
- */
259
- export namespace Prompt {
260
- interface SingleMap {
261
- text: string;
262
- number: number;
263
- boolean: boolean;
264
- }
265
- export interface ListConfig<T extends SingleType> {
266
- type: T;
267
- defaultValue?: SingleMap[T][];
268
- separator?: string;
269
- timeout?: number;
270
- }
271
- export interface PickConfig<T extends SingleType = SingleType, M extends boolean = false> {
272
- type: T;
273
- defaultValue?: PickResult<T, M>;
274
- separator?: string;
275
- timeout?: number;
276
- options: PickOption<T>[];
277
- multiple?: M;
278
- }
279
- export type PickOption<T extends SingleType = 'text'> = {
280
- label: string;
281
- value: SingleMap[T];
282
- };
283
- export type PickResult<T extends SingleType, M extends boolean> = M extends true ? Result<T>[] : Result<T>;
284
- export type SingleType = keyof SingleMap;
285
- export type Result<T extends SingleType> = SingleMap[T];
286
- export type Config<R = any> = {
287
- tips: string;
288
- defaultValue?: R;
289
- timeout?: number;
290
- timeoutText?: string;
291
- format: (input: string) => R;
292
- };
293
- }
package/src/request.ts DELETED
@@ -1,95 +0,0 @@
1
- import { Adapters } from './adapter.js';
2
- import type { MessageSender, MaybePromise } from './types.js';
3
-
4
- /**
5
- * 请求类型枚举
6
- *
7
- * 常见 IM 请求事件分类:
8
- * - friend_add: 好友添加请求
9
- * - group_add: 主动申请入群
10
- * - group_invite: 邀请入群请求
11
- *
12
- * 适配器可自行扩展更多子类型
13
- */
14
- export type RequestType =
15
- | 'friend_add'
16
- | 'group_add'
17
- | 'group_invite'
18
- | (string & {}); // 允许适配器扩展自定义类型
19
-
20
- /**
21
- * 请求频道信息
22
- */
23
- export interface RequestChannel {
24
- id: string;
25
- type: 'group' | 'private' | 'channel';
26
- }
27
-
28
- /**
29
- * 请求基础结构
30
- *
31
- * 与 MessageBase / NoticeBase 同构设计。
32
- * 核心区别:Request 提供 `$approve()` 和 `$reject()` 方法,用于快速处理请求。
33
- *
34
- * @example
35
- * ```typescript
36
- * // 适配器中格式化请求
37
- * const request = Request.from(rawEvent, {
38
- * $id: rawEvent.flag,
39
- * $adapter: 'icqq',
40
- * $bot: botName,
41
- * $type: 'group_invite',
42
- * $channel: { id: groupId, type: 'group' },
43
- * $sender: { id: userId, name: '邀请者' },
44
- * $comment: '请求加群消息',
45
- * $timestamp: Date.now(),
46
- * $approve: async (remark?) => { await api.approve(flag, remark); },
47
- * $reject: async (reason?) => { await api.reject(flag, reason); },
48
- * });
49
- * this.adapter.emit('request.receive', request);
50
- * ```
51
- */
52
- export interface RequestBase {
53
- /** 请求唯一 ID / flag(平台提供的请求标识,用于后续处理) */
54
- $id: string;
55
- /** 适配器名称 */
56
- $adapter: keyof Adapters;
57
- /** Bot 名称 */
58
- $bot: string;
59
- /** 请求类型 */
60
- $type: RequestType;
61
- /** 请求子类型 */
62
- $subType?: string;
63
- /** 请求发生的频道/群/会话 */
64
- $channel: RequestChannel;
65
- /** 请求发送者 */
66
- $sender: MessageSender;
67
- /** 请求附言/验证消息 */
68
- $comment?: string;
69
- /** 请求时间戳 */
70
- $timestamp: number;
71
- /**
72
- * 同意请求
73
- * @param remark 备注信息(如好友备注)
74
- */
75
- $approve(remark?: string): MaybePromise<void>;
76
- /**
77
- * 拒绝请求
78
- * @param reason 拒绝原因
79
- */
80
- $reject(reason?: string): MaybePromise<void>;
81
- }
82
-
83
- /**
84
- * 完整请求类型,支持平台原始数据扩展
85
- */
86
- export type Request<T extends object = {}> = RequestBase & T;
87
-
88
- export namespace Request {
89
- /**
90
- * 工具方法:合并自定义字段与基础请求结构
91
- */
92
- export function from<T extends object>(input: T, format: RequestBase): Request<T> {
93
- return Object.assign(input, format);
94
- }
95
- }
@@ -1,19 +0,0 @@
1
- /**
2
- * Re-export from @zhin.js/kernel for backward compatibility.
3
- */
4
- export {
5
- Scheduler,
6
- getScheduler,
7
- setScheduler,
8
- } from '@zhin.js/kernel';
9
- export type {
10
- Schedule,
11
- JobPayload,
12
- JobState,
13
- ScheduledJob,
14
- JobStore,
15
- JobCallback,
16
- AddJobOptions,
17
- IScheduler,
18
- SchedulerOptions,
19
- } from '@zhin.js/kernel';