@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
package/src/utils.ts DELETED
@@ -1,227 +0,0 @@
1
- /**
2
- * @zhin.js/core utilities.
3
- *
4
- * Generic utilities are re-exported from @zhin.js/kernel.
5
- * IM-specific utilities (compose, segment) remain here.
6
- */
7
-
8
- // ── Re-export generic utils from kernel ──
9
- export {
10
- evaluate,
11
- execute,
12
- clearEvalCache,
13
- getEvalCacheStats,
14
- getValueWithRuntime,
15
- compiler,
16
- remove,
17
- isEmpty,
18
- Time,
19
- supportedPluginExtensions,
20
- resolveEntry,
21
- sleep,
22
- } from '@zhin.js/kernel';
23
-
24
- // ── IM-specific utilities ──
25
- import {
26
- AdapterMessage,
27
- MessageElement,
28
- MessageMiddleware,
29
- RegisteredAdapter,
30
- SendContent,
31
- } from "./types.js";
32
- import { Message } from "./message.js";
33
-
34
- /**
35
- * 组合中间件,洋葱模型
36
- */
37
- export function compose<P extends RegisteredAdapter=RegisteredAdapter>(
38
- middlewares: MessageMiddleware<P>[]
39
- ) {
40
- if (middlewares.length === 0) {
41
- return () => Promise.resolve();
42
- }
43
-
44
- if (middlewares.length === 1) {
45
- return (message: Message<AdapterMessage<P>>, next: () => Promise<void> = () => Promise.resolve()) => {
46
- return middlewares[0](message, next);
47
- };
48
- }
49
-
50
- return function (
51
- message: Message<AdapterMessage<P>>,
52
- next: () => Promise<void>
53
- ) {
54
- let index = -1;
55
- const dispatch = async (i: number = 0): Promise<void> => {
56
- if (i <= index) {
57
- return Promise.reject(new Error("next() called multiple times"));
58
- }
59
- index = i;
60
- let fn = middlewares[i];
61
- if (i === middlewares.length) fn = next;
62
- if (!fn) return;
63
- try {
64
- return await fn(message, () => dispatch(i + 1));
65
- } catch (error) {
66
- console.error("Middleware error:", error);
67
- throw error;
68
- }
69
- };
70
- return dispatch(0);
71
- };
72
- }
73
-
74
- export function segment<T extends object>(type: string, data: T) {
75
- return {
76
- type,
77
- data,
78
- };
79
- }
80
- export namespace segment {
81
- export function escape<T>(text: T): T {
82
- if (typeof text !== "string") return text;
83
- return text
84
- .replace(/&/g, "&amp;")
85
- .replace(/</g, "&lt;")
86
- .replace(/>/g, "&gt;")
87
- .replace(/"/g, "&quot;")
88
- .replace(/'/g, "&#39;") as T;
89
- }
90
- export function unescape<T>(text: T): T {
91
- if (typeof text !== "string") return text;
92
- return text
93
- .replace(/&lt;/g, "<")
94
- .replace(/&gt;/g, ">")
95
- .replace(/&quot;/g, '"')
96
- .replace(/&#39;/g, "'")
97
- .replace(/&amp;/g, "&") as T;
98
- }
99
- export function text(text: string) {
100
- return segment("text", { text });
101
- }
102
- export function face(id: string, text?: string) {
103
- return segment("face", { id, text });
104
- }
105
- export function from(content: SendContent): SendContent {
106
- if (!Array.isArray(content)) content = [content];
107
- const toString = (template: string | MessageElement) => {
108
- if (typeof template !== "string") return [template];
109
-
110
- const MAX_TEMPLATE_LENGTH = 400000;
111
- if (template.length > MAX_TEMPLATE_LENGTH) {
112
- throw new Error(`Template too large: ${template.length} > ${MAX_TEMPLATE_LENGTH}`);
113
- }
114
-
115
- template = unescape(template);
116
- const result: MessageElement[] = [];
117
- const closingReg = /<(\w+)(\s+[^>]*?)?\/>/;
118
- const twinningReg = /<(\w+)(\s+[^>]*?)?>([^]*?)<\/\1>/;
119
-
120
- let iterations = 0;
121
- const MAX_ITERATIONS = 1000;
122
-
123
- while (template.length && iterations++ < MAX_ITERATIONS) {
124
- const twinMatch = template.match(twinningReg);
125
- const closeMatch = template.match(closingReg);
126
-
127
- let match: RegExpMatchArray | null = null;
128
- let isClosing = false;
129
-
130
- if (twinMatch && closeMatch) {
131
- const twinIndex = template.indexOf(twinMatch[0]);
132
- const closeIndex = template.indexOf(closeMatch[0]);
133
- if (closeIndex <= twinIndex) {
134
- match = closeMatch;
135
- isClosing = true;
136
- } else {
137
- match = twinMatch;
138
- }
139
- } else if (closeMatch) {
140
- match = closeMatch;
141
- isClosing = true;
142
- } else if (twinMatch) {
143
- match = twinMatch;
144
- }
145
-
146
- if (!match) break;
147
-
148
- const [fullMatch, type, attrStr = "", child = ""] = isClosing
149
- ? [match[0], match[1], match[2] || ""]
150
- : [match[0], match[1], match[2] || "", match[3] || ""];
151
- const index = template.indexOf(fullMatch);
152
- if (index === -1) break;
153
- const prevText = template.slice(0, index);
154
- if (prevText)
155
- result.push({
156
- type: "text",
157
- data: {
158
- text: unescape(prevText),
159
- },
160
- });
161
- template = template.slice(index + fullMatch.length);
162
- const attrArr = [
163
- ...attrStr.matchAll(/\s+([^=\s]+)=(?:"([^"]*)"|'([^']*)')/g),
164
- ];
165
- const data = Object.fromEntries(
166
- attrArr.map(([source, key, v1, v2]) => {
167
- const value = v1 || v2;
168
- try {
169
- return [key, JSON.parse(unescape(value))];
170
- } catch {
171
- return [key, unescape(value)];
172
- }
173
- })
174
- );
175
- if (child) {
176
- data.message = toString(child).map(({ type, data }) => ({
177
- type,
178
- ...data,
179
- }));
180
- }
181
- result.push({
182
- type: type,
183
- data,
184
- });
185
- }
186
- if (template.length) {
187
- result.push({
188
- type: "text",
189
- data: {
190
- text: unescape(template),
191
- },
192
- });
193
- }
194
- return result;
195
- };
196
- return content.reduce((result, item) => {
197
- result.push(...toString(item));
198
- return result;
199
- }, [] as MessageElement[]);
200
- }
201
- export function raw(content: SendContent) {
202
- if (!Array.isArray(content)) content = [content];
203
- return content
204
- .map((item) => {
205
- if (typeof item === "string") return item;
206
- const { type, data } = item;
207
- if (type === "text") return data.text;
208
- if (type === "at") return `@${data.user_id||data.qq}`;
209
- return data.text ? `{${type}}(${data.text})` : `{${type}}`;
210
- })
211
- .join("");
212
- }
213
- export function toString(content: SendContent) {
214
- if (!Array.isArray(content)) content = [content];
215
- return content
216
- .map((item) => {
217
- if (typeof item === "string") return item;
218
- let { type, data } = item;
219
- if (typeof type === "function") type = type.name;
220
- if (type === "text") return data.text;
221
- return `<${type} ${Object.keys(data)
222
- .map((key) => `${key}='${escape(JSON.stringify(data[key]))}'`)
223
- .join(" ")}/>`;
224
- })
225
- .join("");
226
- }
227
- }