@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/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
- }