@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.
- package/lib/adapter.d.ts +1 -26
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +20 -117
- package/lib/adapter.js.map +1 -1
- package/lib/built/adapter-process.d.ts +0 -4
- package/lib/built/adapter-process.d.ts.map +1 -1
- package/lib/built/adapter-process.js +0 -95
- package/lib/built/adapter-process.js.map +1 -1
- package/lib/built/agent-preset.d.ts +2 -0
- package/lib/built/agent-preset.d.ts.map +1 -1
- package/lib/built/agent-preset.js +4 -0
- package/lib/built/agent-preset.js.map +1 -1
- package/lib/built/command.d.ts +4 -0
- package/lib/built/command.d.ts.map +1 -1
- package/lib/built/command.js +6 -0
- package/lib/built/command.js.map +1 -1
- package/lib/built/component.d.ts.map +1 -1
- package/lib/built/component.js +1 -0
- package/lib/built/component.js.map +1 -1
- package/lib/built/dispatcher.d.ts.map +1 -1
- package/lib/built/dispatcher.js +0 -13
- package/lib/built/dispatcher.js.map +1 -1
- package/lib/built/message-filter.d.ts +2 -0
- package/lib/built/message-filter.d.ts.map +1 -1
- package/lib/built/message-filter.js +5 -0
- package/lib/built/message-filter.js.map +1 -1
- package/lib/built/skill.d.ts +11 -0
- package/lib/built/skill.d.ts.map +1 -1
- package/lib/built/skill.js +14 -0
- package/lib/built/skill.js.map +1 -1
- package/lib/built/tool.d.ts +11 -44
- package/lib/built/tool.d.ts.map +1 -1
- package/lib/built/tool.js +14 -353
- package/lib/built/tool.js.map +1 -1
- package/lib/plugin.d.ts +1 -25
- package/lib/plugin.d.ts.map +1 -1
- package/lib/plugin.js +1 -77
- package/lib/plugin.js.map +1 -1
- package/lib/types.d.ts +0 -25
- package/lib/types.d.ts.map +1 -1
- package/package.json +10 -7
- package/CHANGELOG.md +0 -561
- package/REFACTORING_COMPLETE.md +0 -178
- package/REFACTORING_STATUS.md +0 -263
- package/src/adapter.ts +0 -275
- package/src/ai/index.ts +0 -55
- package/src/ai/providers/anthropic.ts +0 -379
- package/src/ai/providers/base.ts +0 -175
- package/src/ai/providers/index.ts +0 -13
- package/src/ai/providers/ollama.ts +0 -302
- package/src/ai/providers/openai.ts +0 -174
- package/src/ai/types.ts +0 -348
- package/src/bot.ts +0 -37
- package/src/built/adapter-process.ts +0 -177
- package/src/built/agent-preset.ts +0 -136
- package/src/built/ai-trigger.ts +0 -259
- package/src/built/command.ts +0 -108
- package/src/built/common-adapter-tools.ts +0 -242
- package/src/built/component.ts +0 -130
- package/src/built/config.ts +0 -335
- package/src/built/cron.ts +0 -156
- package/src/built/database.ts +0 -134
- package/src/built/dispatcher.ts +0 -496
- package/src/built/login-assist.ts +0 -131
- package/src/built/message-filter.ts +0 -390
- package/src/built/permission.ts +0 -151
- package/src/built/schema-feature.ts +0 -190
- package/src/built/skill.ts +0 -221
- package/src/built/tool.ts +0 -948
- package/src/command.ts +0 -87
- package/src/component.ts +0 -565
- package/src/cron.ts +0 -4
- package/src/errors.ts +0 -46
- package/src/feature.ts +0 -7
- package/src/index.ts +0 -53
- package/src/jsx-dev-runtime.ts +0 -2
- package/src/jsx-runtime.ts +0 -12
- package/src/jsx.ts +0 -135
- package/src/message.ts +0 -48
- package/src/models/system-log.ts +0 -20
- package/src/models/user.ts +0 -15
- package/src/notice.ts +0 -98
- package/src/plugin.ts +0 -896
- package/src/prompt.ts +0 -293
- package/src/request.ts +0 -95
- package/src/scheduler/index.ts +0 -19
- package/src/scheduler/scheduler.ts +0 -372
- package/src/scheduler/types.ts +0 -74
- package/src/tool-zod.ts +0 -115
- package/src/types-generator.ts +0 -78
- package/src/types.ts +0 -505
- package/src/utils.ts +0 -227
- package/tests/adapter.test.ts +0 -638
- package/tests/ai/ai-trigger.test.ts +0 -368
- package/tests/ai/providers.integration.test.ts +0 -227
- package/tests/ai/setup.ts +0 -308
- package/tests/ai/tool.test.ts +0 -800
- package/tests/bot.test.ts +0 -151
- package/tests/command.test.ts +0 -737
- package/tests/component-new.test.ts +0 -361
- package/tests/config.test.ts +0 -372
- package/tests/cron.test.ts +0 -82
- package/tests/dispatcher.test.ts +0 -293
- package/tests/errors.test.ts +0 -21
- package/tests/expression-evaluation.test.ts +0 -258
- package/tests/features-builtin.test.ts +0 -191
- package/tests/jsx-runtime.test.ts +0 -45
- package/tests/jsx.test.ts +0 -319
- package/tests/message-filter.test.ts +0 -566
- package/tests/message.test.ts +0 -402
- package/tests/notice.test.ts +0 -198
- package/tests/plugin.test.ts +0 -779
- package/tests/prompt.test.ts +0 -78
- package/tests/redos-protection.test.ts +0 -198
- package/tests/request.test.ts +0 -221
- package/tests/schema.test.ts +0 -248
- package/tests/skill-feature.test.ts +0 -179
- package/tests/test-utils.ts +0 -59
- package/tests/tool-feature.test.ts +0 -254
- package/tests/types.test.ts +0 -162
- package/tests/utils.test.ts +0 -135
- 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, "&")
|
|
85
|
-
.replace(/</g, "<")
|
|
86
|
-
.replace(/>/g, ">")
|
|
87
|
-
.replace(/"/g, """)
|
|
88
|
-
.replace(/'/g, "'") as T;
|
|
89
|
-
}
|
|
90
|
-
export function unescape<T>(text: T): T {
|
|
91
|
-
if (typeof text !== "string") return text;
|
|
92
|
-
return text
|
|
93
|
-
.replace(/</g, "<")
|
|
94
|
-
.replace(/>/g, ">")
|
|
95
|
-
.replace(/"/g, '"')
|
|
96
|
-
.replace(/'/g, "'")
|
|
97
|
-
.replace(/&/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
|
-
}
|