@isdk/ai-tool 0.0.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/README.cn.md +261 -0
- package/README.md +3 -0
- package/dist/chunk-EZFPVSGF.mjs +22 -0
- package/dist/chunk-PKECBABP.mjs +5085 -0
- package/dist/funcs.d.mts +6 -0
- package/dist/funcs.d.ts +6 -0
- package/dist/funcs.js +5016 -0
- package/dist/funcs.mjs +37 -0
- package/dist/index-D4KjfAKl.d.mts +606 -0
- package/dist/index-D4KjfAKl.d.ts +606 -0
- package/dist/index.d.mts +1097 -0
- package/dist/index.d.ts +1097 -0
- package/dist/index.js +5239 -0
- package/dist/index.mjs +399 -0
- package/dist/test/util.d.mts +7 -0
- package/dist/test/util.d.ts +7 -0
- package/dist/test/util.js +98 -0
- package/dist/test/util.mjs +61 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +5 -0
- package/docs/classes/AbortError.md +359 -0
- package/docs/classes/AlreadyExistsError.md +339 -0
- package/docs/classes/BaseError.md +346 -0
- package/docs/classes/ClientTools.md +2705 -0
- package/docs/classes/CommonError.md +368 -0
- package/docs/classes/ConfigFile.md +346 -0
- package/docs/classes/EventClient.md +3183 -0
- package/docs/classes/EventServer.md +3138 -0
- package/docs/classes/EventToolFunc.md +2498 -0
- package/docs/classes/FStringPromptTemplate.md +1236 -0
- package/docs/classes/FewShotPromptTemplate.md +1286 -0
- package/docs/classes/GolangPromptTemplate.md +1236 -0
- package/docs/classes/HFEnvironment.md +212 -0
- package/docs/classes/HFInterpreter.md +386 -0
- package/docs/classes/HFTemplate.md +79 -0
- package/docs/classes/HfPromptTemplate.md +1236 -0
- package/docs/classes/NotFoundError.md +339 -0
- package/docs/classes/NotImplementationError.md +339 -0
- package/docs/classes/PromptExampleSelector.md +207 -0
- package/docs/classes/PromptTemplate.md +1163 -0
- package/docs/classes/ReadableStreamError.md +359 -0
- package/docs/classes/ResClientTools.md +2859 -0
- package/docs/classes/ResServerTools.md +2783 -0
- package/docs/classes/SSEChannel.md +289 -0
- package/docs/classes/ServerTools.md +2633 -0
- package/docs/classes/ToolFunc.md +2124 -0
- package/docs/enums/ErrorCode.md +217 -0
- package/docs/enums/XXHashAlgorithm.md +30 -0
- package/docs/interfaces/AIChatAssistantMessageParam.md +84 -0
- package/docs/interfaces/AIChatContentPartImage.md +36 -0
- package/docs/interfaces/AIChatContentPartText.md +30 -0
- package/docs/interfaces/AIChatMessageParamBase.md +35 -0
- package/docs/interfaces/AIChatMessageToolCall.md +48 -0
- package/docs/interfaces/AIChatSystemMessageParam.md +51 -0
- package/docs/interfaces/AIChatToolMessageParam.md +62 -0
- package/docs/interfaces/AIChatUserMessageParam.md +95 -0
- package/docs/interfaces/AIResult.md +65 -0
- package/docs/interfaces/AIStreamCallbacksAndOptions.md +158 -0
- package/docs/interfaces/AIStreamParser.md +33 -0
- package/docs/interfaces/AIStreamParserOptions.md +23 -0
- package/docs/interfaces/BaseFunc.md +145 -0
- package/docs/interfaces/BaseFuncItem.md +99 -0
- package/docs/interfaces/ClientFuncItem.md +181 -0
- package/docs/interfaces/DotenvExpandOptions.md +51 -0
- package/docs/interfaces/DotenvExpandOutput.md +30 -0
- package/docs/interfaces/DotenvParseInput.md +7 -0
- package/docs/interfaces/DotenvParseOutput.md +7 -0
- package/docs/interfaces/DotenvPopulateInput.md +7 -0
- package/docs/interfaces/EventClientFuncParams.md +66 -0
- package/docs/interfaces/EventServerFuncParams.md +79 -0
- package/docs/interfaces/FewShotPromptTemplateOptions.md +179 -0
- package/docs/interfaces/FuncItem.md +130 -0
- package/docs/interfaces/FuncParam.md +67 -0
- package/docs/interfaces/FuncParams.md +7 -0
- package/docs/interfaces/Funcs.md +7 -0
- package/docs/interfaces/IOptions.md +30 -0
- package/docs/interfaces/JsonFilter.md +7 -0
- package/docs/interfaces/PromptExampleSelectorOptions.md +39 -0
- package/docs/interfaces/PromptTemplateOptions.md +84 -0
- package/docs/interfaces/RemoteFuncItem.md +167 -0
- package/docs/interfaces/ResClientFuncParams.md +34 -0
- package/docs/interfaces/ResServerFuncParams.md +79 -0
- package/docs/interfaces/SanitizeFilenameOptions.md +30 -0
- package/docs/interfaces/ServerFuncItem.md +192 -0
- package/docs/interfaces/ServerFuncParams.md +44 -0
- package/docs/modules.md +2707 -0
- package/package.json +112 -0
package/dist/funcs.js
ADDED
|
@@ -0,0 +1,5016 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/funcs/index.ts
|
|
32
|
+
var funcs_exports = {};
|
|
33
|
+
__export(funcs_exports, {
|
|
34
|
+
EventClient: () => EventClient,
|
|
35
|
+
EventServer: () => EventServer,
|
|
36
|
+
EventToolFunc: () => EventToolFunc,
|
|
37
|
+
_lrucache: () => _lrucache,
|
|
38
|
+
createLRUCache: () => createLRUCache,
|
|
39
|
+
event: () => event,
|
|
40
|
+
eventClient: () => eventClient,
|
|
41
|
+
eventServer: () => eventServer,
|
|
42
|
+
getDefaultSimilarPassingScore: () => getDefaultSimilarPassingScore,
|
|
43
|
+
isSimilar: () => isSimilar,
|
|
44
|
+
lrucache: () => lrucache,
|
|
45
|
+
mergeSegments: () => mergeSegments,
|
|
46
|
+
registerCoreTools: () => registerCoreTools,
|
|
47
|
+
segments: () => segments,
|
|
48
|
+
similarity: () => similarity,
|
|
49
|
+
splitSegments: () => splitSegments
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(funcs_exports);
|
|
52
|
+
|
|
53
|
+
// src/funcs/event.ts
|
|
54
|
+
var import_events_ex = require("events-ex");
|
|
55
|
+
|
|
56
|
+
// src/tool-func.ts
|
|
57
|
+
var import_property_manager = require("property-manager");
|
|
58
|
+
var import_util_ex = require("util-ex");
|
|
59
|
+
|
|
60
|
+
// src/utils/base-error.ts
|
|
61
|
+
var import_abstract_error = require("abstract-error");
|
|
62
|
+
var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
|
|
63
|
+
ErrorCode2[ErrorCode2["OK"] = 200] = "OK";
|
|
64
|
+
ErrorCode2[ErrorCode2["BadRequest"] = 400] = "BadRequest";
|
|
65
|
+
ErrorCode2[ErrorCode2["InvalidArgument"] = 400] = "InvalidArgument";
|
|
66
|
+
ErrorCode2[ErrorCode2["Unauthorized"] = 401] = "Unauthorized";
|
|
67
|
+
ErrorCode2[ErrorCode2["PaymentRequired"] = 402] = "PaymentRequired";
|
|
68
|
+
ErrorCode2[ErrorCode2["Forbidden"] = 403] = "Forbidden";
|
|
69
|
+
ErrorCode2[ErrorCode2["NotFound"] = 404] = "NotFound";
|
|
70
|
+
ErrorCode2[ErrorCode2["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
71
|
+
ErrorCode2[ErrorCode2["NotAcceptable"] = 406] = "NotAcceptable";
|
|
72
|
+
ErrorCode2[ErrorCode2["RequestTimeout"] = 408] = "RequestTimeout";
|
|
73
|
+
ErrorCode2[ErrorCode2["Conflict"] = 409] = "Conflict";
|
|
74
|
+
ErrorCode2[ErrorCode2["Corruption"] = 409] = "Corruption";
|
|
75
|
+
ErrorCode2[ErrorCode2["Aborted"] = 499] = "Aborted";
|
|
76
|
+
ErrorCode2[ErrorCode2["InternalError"] = 500] = "InternalError";
|
|
77
|
+
ErrorCode2[ErrorCode2["NotImplemented"] = 501] = "NotImplemented";
|
|
78
|
+
ErrorCode2[ErrorCode2["NotSupported"] = 501] = "NotSupported";
|
|
79
|
+
ErrorCode2[ErrorCode2["BadGateway"] = 502] = "BadGateway";
|
|
80
|
+
ErrorCode2[ErrorCode2["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
81
|
+
ErrorCode2[ErrorCode2["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
82
|
+
return ErrorCode2;
|
|
83
|
+
})(ErrorCode || {});
|
|
84
|
+
var InternalErrorCode = 500 /* InternalError */;
|
|
85
|
+
var NotImplementedErrorCode = 501 /* NotImplemented */;
|
|
86
|
+
var NotFoundErrorCode = 404 /* NotFound */;
|
|
87
|
+
var AlreadyExistsErrorCode = 409 /* Conflict */;
|
|
88
|
+
var AbortErrorCode = 499 /* Aborted */;
|
|
89
|
+
var BaseError = class _BaseError extends import_abstract_error.AbstractError {
|
|
90
|
+
static createErrorClass(aType, aErrorCode, ParentErrorClass = _BaseError) {
|
|
91
|
+
return (0, import_abstract_error.createErrorClass)(aType, aErrorCode, ParentErrorClass);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Constructs a new BaseError instance.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} message - The error message.
|
|
97
|
+
* @param {ErrorCodeType} [code] - The error code.
|
|
98
|
+
* @param {string|object} [name] - The error name or additional properties.
|
|
99
|
+
*/
|
|
100
|
+
constructor(message, code, name) {
|
|
101
|
+
super(message, code);
|
|
102
|
+
const ctor = this.constructor;
|
|
103
|
+
if (code != null) {
|
|
104
|
+
this.code = code;
|
|
105
|
+
} else if (ctor.code) {
|
|
106
|
+
this.code = ctor.code;
|
|
107
|
+
} else {
|
|
108
|
+
this.code = ctor.name;
|
|
109
|
+
}
|
|
110
|
+
if (name) {
|
|
111
|
+
if (typeof name === "string") {
|
|
112
|
+
this.name = name;
|
|
113
|
+
} else {
|
|
114
|
+
Object.assign(this, name);
|
|
115
|
+
}
|
|
116
|
+
} else if (typeof this.code === "string") {
|
|
117
|
+
this.name = this.code;
|
|
118
|
+
} else {
|
|
119
|
+
this.name = ctor.name;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns a JSON representation of the error.
|
|
124
|
+
*
|
|
125
|
+
* @returns {object} A JSON representation of the error.
|
|
126
|
+
*/
|
|
127
|
+
toJSON() {
|
|
128
|
+
const result = {
|
|
129
|
+
name: this.name,
|
|
130
|
+
code: this.code,
|
|
131
|
+
data: this.data,
|
|
132
|
+
caller: this.caller,
|
|
133
|
+
error: this.message,
|
|
134
|
+
stack: this.stack
|
|
135
|
+
};
|
|
136
|
+
Object.keys(result).forEach((key) => result[key] === void 0 && delete result[key]);
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Creates a new BaseError instance from a JSON representation.
|
|
141
|
+
*
|
|
142
|
+
* @param {object} json - A JSON representation of the error.
|
|
143
|
+
* @returns {BaseError} A new BaseError instance.
|
|
144
|
+
*/
|
|
145
|
+
fromJSON(json) {
|
|
146
|
+
const e = new _BaseError(json.message);
|
|
147
|
+
e.code = json.code;
|
|
148
|
+
e.data = json.data;
|
|
149
|
+
e.caller = json.caller;
|
|
150
|
+
e.message = json.error;
|
|
151
|
+
e.stack = json.stack;
|
|
152
|
+
return e;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
var CommonError = class extends BaseError {
|
|
156
|
+
constructor(message, name, status = InternalErrorCode) {
|
|
157
|
+
super(message, status, name);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var NotImplementationError = class extends CommonError {
|
|
161
|
+
constructor(msg, name) {
|
|
162
|
+
if (!msg) {
|
|
163
|
+
msg = `Not Implementation.`;
|
|
164
|
+
}
|
|
165
|
+
super(msg, name, 501 /* NotImplemented */);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
CommonError[NotImplementedErrorCode] = NotImplementationError;
|
|
169
|
+
var NotFoundError = class extends CommonError {
|
|
170
|
+
static {
|
|
171
|
+
this.code = NotFoundErrorCode;
|
|
172
|
+
}
|
|
173
|
+
constructor(what, name) {
|
|
174
|
+
super(`Could not find ${what}.`, name, NotFoundErrorCode);
|
|
175
|
+
this.data = { what };
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
CommonError[NotFoundErrorCode] = NotFoundError;
|
|
179
|
+
var AlreadyExistsError = class extends CommonError {
|
|
180
|
+
static {
|
|
181
|
+
this.code = AlreadyExistsErrorCode;
|
|
182
|
+
}
|
|
183
|
+
constructor(what, name) {
|
|
184
|
+
super(`The ${what} already exists.`, name, AlreadyExistsErrorCode);
|
|
185
|
+
this.data = { what };
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
CommonError[AlreadyExistsErrorCode] = AlreadyExistsError;
|
|
189
|
+
var AbortError = class extends CommonError {
|
|
190
|
+
static {
|
|
191
|
+
this.code = AbortErrorCode;
|
|
192
|
+
}
|
|
193
|
+
constructor(what, name) {
|
|
194
|
+
const message = what ? `The operation was aborted for ${what}.` : `The operation was aborted.`;
|
|
195
|
+
super(message, name, AbortErrorCode);
|
|
196
|
+
if (what) {
|
|
197
|
+
this.data = { what };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
CommonError[AbortErrorCode] = AbortError;
|
|
202
|
+
function createError(message, name, status = InternalErrorCode) {
|
|
203
|
+
const ErrorClass = CommonError[status] || CommonError;
|
|
204
|
+
const error = new ErrorClass(message, name, status);
|
|
205
|
+
if (typeof error.code !== "number")
|
|
206
|
+
error.code = status;
|
|
207
|
+
return error;
|
|
208
|
+
}
|
|
209
|
+
function throwError2(message, name, status = InternalErrorCode) {
|
|
210
|
+
const error = createError(message, name, status);
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/tool-func.ts
|
|
215
|
+
var ToolFunc = class _ToolFunc extends import_property_manager.AdvancePropertyManager {
|
|
216
|
+
static {
|
|
217
|
+
this.items = {};
|
|
218
|
+
}
|
|
219
|
+
static get(name) {
|
|
220
|
+
return this.items[name];
|
|
221
|
+
}
|
|
222
|
+
static list() {
|
|
223
|
+
return this.items;
|
|
224
|
+
}
|
|
225
|
+
static getByTag(tagName) {
|
|
226
|
+
let result;
|
|
227
|
+
for (const name in this.list()) {
|
|
228
|
+
const item = this.get(name);
|
|
229
|
+
let tags = item.tags;
|
|
230
|
+
if (typeof tags === "string") {
|
|
231
|
+
if (tags === tagName) {
|
|
232
|
+
result = item;
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
} else if (Array.isArray(tags)) {
|
|
236
|
+
if (tags.indexOf(tagName) >= 0) {
|
|
237
|
+
result = item;
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
static getAllByTag(tagName) {
|
|
245
|
+
let result = [];
|
|
246
|
+
for (const name in this.list()) {
|
|
247
|
+
const item = this.get(name);
|
|
248
|
+
let tags = item.tags;
|
|
249
|
+
if (typeof tags === "string") {
|
|
250
|
+
if (tags === tagName) {
|
|
251
|
+
result.push(item);
|
|
252
|
+
}
|
|
253
|
+
} else if (Array.isArray(tags)) {
|
|
254
|
+
if (tags.indexOf(tagName) >= 0) {
|
|
255
|
+
result.push(item);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
static async run(name, params) {
|
|
262
|
+
const func = this.get(name);
|
|
263
|
+
if (func) {
|
|
264
|
+
return await func.run(params);
|
|
265
|
+
}
|
|
266
|
+
throw new NotFoundError(`${name} to run`, this.name);
|
|
267
|
+
}
|
|
268
|
+
static runSync(name, params) {
|
|
269
|
+
const func = this.get(name);
|
|
270
|
+
if (func) {
|
|
271
|
+
return func.runSync(params);
|
|
272
|
+
}
|
|
273
|
+
throw new NotFoundError(`${name} to run`, this.name);
|
|
274
|
+
}
|
|
275
|
+
static getFunc(name) {
|
|
276
|
+
const func = this.get(name);
|
|
277
|
+
return func?.getFunc();
|
|
278
|
+
}
|
|
279
|
+
static async runWithPos(name, ...params) {
|
|
280
|
+
const func = this.get(name);
|
|
281
|
+
if (func) {
|
|
282
|
+
return await func.runWithPos(...params);
|
|
283
|
+
}
|
|
284
|
+
throw new NotFoundError(`${name} to run`, this.name);
|
|
285
|
+
}
|
|
286
|
+
static runWithPosSync(name, ...params) {
|
|
287
|
+
const func = this.get(name);
|
|
288
|
+
if (func) {
|
|
289
|
+
return func.runWithPosSync(...params);
|
|
290
|
+
}
|
|
291
|
+
throw new NotFoundError(`${name} to run`, this.name);
|
|
292
|
+
}
|
|
293
|
+
static getFuncWithPos(name) {
|
|
294
|
+
const func = this.get(name);
|
|
295
|
+
return func?.getFuncWithPos();
|
|
296
|
+
}
|
|
297
|
+
static register(name, options = {}) {
|
|
298
|
+
switch (typeof name) {
|
|
299
|
+
case "string":
|
|
300
|
+
options.name = name;
|
|
301
|
+
break;
|
|
302
|
+
case "function":
|
|
303
|
+
options.func = name;
|
|
304
|
+
break;
|
|
305
|
+
case "object":
|
|
306
|
+
options = name;
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
name = options.name;
|
|
310
|
+
let result = !!this.get(name);
|
|
311
|
+
if (!result) {
|
|
312
|
+
if (!(options instanceof _ToolFunc)) {
|
|
313
|
+
result = new this(options);
|
|
314
|
+
return result.register();
|
|
315
|
+
}
|
|
316
|
+
this.items[name] = options;
|
|
317
|
+
result = options;
|
|
318
|
+
} else {
|
|
319
|
+
result = false;
|
|
320
|
+
}
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
static unregister(name) {
|
|
324
|
+
const result = this.items[name];
|
|
325
|
+
if (result) {
|
|
326
|
+
delete this.items[name];
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
constructor(name, options = {}) {
|
|
331
|
+
super();
|
|
332
|
+
switch (typeof name) {
|
|
333
|
+
case "string":
|
|
334
|
+
options.name = name;
|
|
335
|
+
break;
|
|
336
|
+
case "function":
|
|
337
|
+
options.func = name;
|
|
338
|
+
break;
|
|
339
|
+
case "object":
|
|
340
|
+
options = name;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
this.name = name = options.name;
|
|
344
|
+
if (options.scope) {
|
|
345
|
+
this.scope = options.scope;
|
|
346
|
+
}
|
|
347
|
+
if (typeof options.setup === "function") {
|
|
348
|
+
options.setup.call(this, options);
|
|
349
|
+
}
|
|
350
|
+
this.initialize(options);
|
|
351
|
+
}
|
|
352
|
+
register() {
|
|
353
|
+
const Tools = this.constructor;
|
|
354
|
+
const depends = this.depends;
|
|
355
|
+
if (depends) {
|
|
356
|
+
const keys = Object.keys(depends);
|
|
357
|
+
for (const k of keys) {
|
|
358
|
+
const dep = depends[k];
|
|
359
|
+
if (dep instanceof _ToolFunc) {
|
|
360
|
+
dep.register();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return Tools.register(this);
|
|
365
|
+
}
|
|
366
|
+
unregister() {
|
|
367
|
+
return this.constructor.unregister(this.name);
|
|
368
|
+
}
|
|
369
|
+
arr2ObjParams(params) {
|
|
370
|
+
if (this.params && (params.length > 1 || Array.isArray(params[0]) || params[0] && typeof params[0] !== "object")) {
|
|
371
|
+
const _p = {};
|
|
372
|
+
const keys = Object.keys(this.params);
|
|
373
|
+
let len = Math.min(keys.length, params.length);
|
|
374
|
+
for (let i = 0; i < len; i++) {
|
|
375
|
+
_p[keys[i]] = params[i];
|
|
376
|
+
}
|
|
377
|
+
params = [_p];
|
|
378
|
+
}
|
|
379
|
+
return params;
|
|
380
|
+
}
|
|
381
|
+
obj2ArrParams(params) {
|
|
382
|
+
const result = [];
|
|
383
|
+
if (params && this.params && Array.isArray(this.params)) {
|
|
384
|
+
const keys = Object.keys(params);
|
|
385
|
+
let len = Math.min(keys.length, this.params.length);
|
|
386
|
+
for (let i = 0; i < len; i++) {
|
|
387
|
+
result.push(params[keys[i]]);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return result;
|
|
391
|
+
}
|
|
392
|
+
runSync(params) {
|
|
393
|
+
const isPosParams = this.params && Array.isArray(this.params);
|
|
394
|
+
if (Array.isArray(params)) {
|
|
395
|
+
if (isPosParams)
|
|
396
|
+
return this.func(...params);
|
|
397
|
+
throwError2("the function is not support array params, the params must be object!", this.name);
|
|
398
|
+
}
|
|
399
|
+
if (isPosParams) {
|
|
400
|
+
params = this.obj2ArrParams(params);
|
|
401
|
+
console.warn('Warning:Use runWithPos() instead of run() for the "' + this.name + '" is function with position params');
|
|
402
|
+
return this.func(...params);
|
|
403
|
+
}
|
|
404
|
+
return this.func(params);
|
|
405
|
+
}
|
|
406
|
+
async run(params) {
|
|
407
|
+
return await this.runSync(params);
|
|
408
|
+
}
|
|
409
|
+
async runAs(name, params) {
|
|
410
|
+
const result = await this.constructor.run(name, params);
|
|
411
|
+
return result;
|
|
412
|
+
}
|
|
413
|
+
runAsSync(name, params) {
|
|
414
|
+
const result = this.constructor.runSync(name, params);
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
417
|
+
getFunc(name) {
|
|
418
|
+
const result = name ? this.constructor.getFunc(name) : this.runSync.bind(this);
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
runWithPosSync(...params) {
|
|
422
|
+
if (this.params && !Array.isArray(this.params)) {
|
|
423
|
+
params = this.arr2ObjParams(params);
|
|
424
|
+
}
|
|
425
|
+
return this.func(...params);
|
|
426
|
+
}
|
|
427
|
+
runWithPosAsSync(name, ...params) {
|
|
428
|
+
return this.constructor.runWithPosSync(name, ...params);
|
|
429
|
+
}
|
|
430
|
+
async runWithPos(...params) {
|
|
431
|
+
const result = await this.runWithPosSync(...params);
|
|
432
|
+
return result;
|
|
433
|
+
}
|
|
434
|
+
async runWithPosAs(name, ...params) {
|
|
435
|
+
const result = await this.constructor.runWithPos(name, ...params);
|
|
436
|
+
return result;
|
|
437
|
+
}
|
|
438
|
+
getFuncWithPos(name) {
|
|
439
|
+
const result = name ? this.constructor.getFuncWithPos(name) : this.runWithPosSync.bind(this);
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
var ToolFuncSchema = {
|
|
444
|
+
name: { type: "string" },
|
|
445
|
+
description: { type: "string" },
|
|
446
|
+
func: {
|
|
447
|
+
type: "function",
|
|
448
|
+
assign(value, dest, src, name, options) {
|
|
449
|
+
let result = value;
|
|
450
|
+
const valueType = typeof value;
|
|
451
|
+
const isExported = options.isExported;
|
|
452
|
+
if (isExported) {
|
|
453
|
+
result = valueType === "function" ? value.toString() : value;
|
|
454
|
+
} else if (value) {
|
|
455
|
+
if (valueType !== "string") {
|
|
456
|
+
value = value.toString();
|
|
457
|
+
}
|
|
458
|
+
result = (0, import_util_ex._createFunction)(value, dest.scope);
|
|
459
|
+
}
|
|
460
|
+
return result;
|
|
461
|
+
}
|
|
462
|
+
},
|
|
463
|
+
params: { type: "object" },
|
|
464
|
+
result: { type: "any" },
|
|
465
|
+
setup: { type: "function" },
|
|
466
|
+
depends: { type: "object", exported: false },
|
|
467
|
+
tags: { type: ["array", "string"] }
|
|
468
|
+
};
|
|
469
|
+
ToolFunc.defineProperties(ToolFunc, ToolFuncSchema);
|
|
470
|
+
|
|
471
|
+
// src/utils/event/event-ability.ts
|
|
472
|
+
var import_custom_ability = require("custom-ability");
|
|
473
|
+
var EventName = "event";
|
|
474
|
+
var EventBusName = "event-bus";
|
|
475
|
+
function getEventableClass(Backend) {
|
|
476
|
+
class BackendEventable {
|
|
477
|
+
static get emitter() {
|
|
478
|
+
if (!this._emitter) {
|
|
479
|
+
const emitter = this.get(EventBusName)?.emitter;
|
|
480
|
+
if (emitter) {
|
|
481
|
+
this._emitter = emitter;
|
|
482
|
+
this.prototype._events = emitter._events;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return this._emitter;
|
|
486
|
+
}
|
|
487
|
+
get emitter() {
|
|
488
|
+
const ctor = this.constructor;
|
|
489
|
+
return ctor._emitter;
|
|
490
|
+
}
|
|
491
|
+
on(eventName, listener) {
|
|
492
|
+
const emitter = Backend.emitter;
|
|
493
|
+
if (!emitter) {
|
|
494
|
+
throw new TypeError("EventBackend required");
|
|
495
|
+
}
|
|
496
|
+
return emitter.on(eventName, listener);
|
|
497
|
+
}
|
|
498
|
+
once(eventName, listener) {
|
|
499
|
+
const emitter = Backend.emitter;
|
|
500
|
+
if (!emitter) {
|
|
501
|
+
throw new TypeError("EventBackend required");
|
|
502
|
+
}
|
|
503
|
+
return emitter.once(eventName, listener);
|
|
504
|
+
}
|
|
505
|
+
off(type, listener) {
|
|
506
|
+
const emitter = Backend.emitter;
|
|
507
|
+
if (!emitter) {
|
|
508
|
+
throw new TypeError("EventBackend required");
|
|
509
|
+
}
|
|
510
|
+
return emitter.off(type, listener);
|
|
511
|
+
}
|
|
512
|
+
emit(eventName, ...args) {
|
|
513
|
+
const emitter = Backend.emitter;
|
|
514
|
+
if (!emitter) {
|
|
515
|
+
throw new TypeError("EventBackend required");
|
|
516
|
+
}
|
|
517
|
+
if (eventName === "error") {
|
|
518
|
+
return this.emitError.apply(this, args);
|
|
519
|
+
}
|
|
520
|
+
return emitter.emit.call(this, eventName, this.name, ...args);
|
|
521
|
+
}
|
|
522
|
+
emitError(err, ...args) {
|
|
523
|
+
const emitter = Backend.emitter;
|
|
524
|
+
const eventName = "error";
|
|
525
|
+
if (!emitter) {
|
|
526
|
+
throw new TypeError("EventBackend required");
|
|
527
|
+
}
|
|
528
|
+
return emitter.emit.call(this, eventName, err, this.name, ...args);
|
|
529
|
+
}
|
|
530
|
+
async emitAsync(eventName, ...args) {
|
|
531
|
+
const emitter = Backend.emitter;
|
|
532
|
+
if (!emitter) {
|
|
533
|
+
throw new TypeError("EventBackend required");
|
|
534
|
+
}
|
|
535
|
+
return emitter.emitAsync.call(this, eventName, this.name, ...args);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return BackendEventable;
|
|
539
|
+
}
|
|
540
|
+
var backendEventable = (0, import_custom_ability.createAbilityInjector)(getEventableClass, true);
|
|
541
|
+
|
|
542
|
+
// src/utils/event/sse-channel.ts
|
|
543
|
+
function hasEventMatch(subscriptionList, eventName) {
|
|
544
|
+
return !subscriptionList || subscriptionList.some((pat) => pat instanceof RegExp ? pat.test(eventName) : pat === eventName);
|
|
545
|
+
}
|
|
546
|
+
var SSEChannelAlreadyClosedErrCode = 498;
|
|
547
|
+
var SSEChannel = class {
|
|
548
|
+
get active() {
|
|
549
|
+
return this._active;
|
|
550
|
+
}
|
|
551
|
+
set active(v) {
|
|
552
|
+
if (v !== this._active) {
|
|
553
|
+
if (v) {
|
|
554
|
+
if (this.pingTimer) {
|
|
555
|
+
clearInterval(this.pingTimer);
|
|
556
|
+
this.pingTimer = void 0;
|
|
557
|
+
}
|
|
558
|
+
if (this.options.pingInterval > 0) {
|
|
559
|
+
this.pingTimer = setInterval(() => this.publish(), this.options.pingInterval);
|
|
560
|
+
}
|
|
561
|
+
} else {
|
|
562
|
+
if (this.clients.size) {
|
|
563
|
+
this.clearClients();
|
|
564
|
+
}
|
|
565
|
+
if (this.pingTimer) {
|
|
566
|
+
clearInterval(this.pingTimer);
|
|
567
|
+
this.pingTimer = void 0;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
this._active = v;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Creates a new SSE channel.
|
|
575
|
+
* @param options - The options for the SSE channel.
|
|
576
|
+
*/
|
|
577
|
+
constructor(options) {
|
|
578
|
+
this.options = Object.assign(
|
|
579
|
+
{},
|
|
580
|
+
{
|
|
581
|
+
pingInterval: 3e3,
|
|
582
|
+
maxStreamDuration: 3e4,
|
|
583
|
+
clientRetryInterval: 1e3,
|
|
584
|
+
startId: 1,
|
|
585
|
+
historySize: 100,
|
|
586
|
+
rewind: 0,
|
|
587
|
+
cors: false
|
|
588
|
+
},
|
|
589
|
+
options
|
|
590
|
+
);
|
|
591
|
+
this.nextID = this.options.startId;
|
|
592
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
593
|
+
this.messages = [];
|
|
594
|
+
this.active = true;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Publishes a message to the SSE channel.
|
|
598
|
+
* @param {string | Record<string, any>} data - The data to send. It can be a string or an object.
|
|
599
|
+
* @param {string} eventName - The name of the event.
|
|
600
|
+
* @returns {number | undefined} - Returns the ID of the message.
|
|
601
|
+
* @throws Will throw an error if the channel is closed.
|
|
602
|
+
*/
|
|
603
|
+
publish(data, eventName) {
|
|
604
|
+
if (!this.active)
|
|
605
|
+
throwError2("Channel closed", "SSEChannel", SSEChannelAlreadyClosedErrCode);
|
|
606
|
+
let output;
|
|
607
|
+
let id;
|
|
608
|
+
let _eventName = eventName;
|
|
609
|
+
if (!data && !eventName) {
|
|
610
|
+
if (!this.clients.size)
|
|
611
|
+
return;
|
|
612
|
+
output = "data: \n\n";
|
|
613
|
+
} else {
|
|
614
|
+
id = this.nextID++;
|
|
615
|
+
if (typeof data === "object") {
|
|
616
|
+
if (data.event) {
|
|
617
|
+
_eventName = data.event;
|
|
618
|
+
if (data.data !== void 0)
|
|
619
|
+
data = JSON.stringify(data.data);
|
|
620
|
+
} else {
|
|
621
|
+
data = JSON.stringify(data);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
data = data ? data.split(/[\r\n]+/).map((str) => "data: " + str).join("\n") : "";
|
|
625
|
+
output = "id: " + id + "\n" + (eventName ? "event: " + eventName + "\n" : "") + (data || "data: ") + "\n\n";
|
|
626
|
+
this.messages.push({ id, _eventName, output });
|
|
627
|
+
}
|
|
628
|
+
;
|
|
629
|
+
[...this.clients].filter((c) => !_eventName || hasEventMatch(c.events, _eventName)).forEach((c, i) => {
|
|
630
|
+
c.res.write(output);
|
|
631
|
+
});
|
|
632
|
+
while (this.messages.length > this.options.historySize) {
|
|
633
|
+
this.messages.shift();
|
|
634
|
+
}
|
|
635
|
+
return id;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Subscribes a client to the SSE channel.
|
|
639
|
+
* @param {IncomingMessage} req - The request object.
|
|
640
|
+
* @param {ServerResponse} res - The response object.
|
|
641
|
+
* @param {Events} events - The events to subscribe to.
|
|
642
|
+
* @returns {Client} - Returns the client object.
|
|
643
|
+
* @throws Will throw an error if the channel is closed.
|
|
644
|
+
*/
|
|
645
|
+
subscribe(req, res, events) {
|
|
646
|
+
if (!this.active)
|
|
647
|
+
throwError2("Channel closed", "SSEChannel", SSEChannelAlreadyClosedErrCode);
|
|
648
|
+
const c = { req, res, events };
|
|
649
|
+
const maxStreamDuration = this.options.maxStreamDuration;
|
|
650
|
+
let cacheControl = "max-age=0, stale-while-revalidate=0, stale-if-error=0, no-transform";
|
|
651
|
+
if (maxStreamDuration > 0) {
|
|
652
|
+
cacheControl += ", s-maxage=" + (Math.floor(maxStreamDuration / 1e3) - 1);
|
|
653
|
+
}
|
|
654
|
+
const headers = {
|
|
655
|
+
"Content-Type": "text/event-stream",
|
|
656
|
+
"Cache-Control": cacheControl,
|
|
657
|
+
"Connection": "keep-alive"
|
|
658
|
+
};
|
|
659
|
+
if (this.options.cors) {
|
|
660
|
+
headers["access-control-allow-origin"] = "*";
|
|
661
|
+
}
|
|
662
|
+
c.req.socket.setNoDelay(true);
|
|
663
|
+
c.res.writeHead(200, headers);
|
|
664
|
+
let body = "retry: " + this.options.clientRetryInterval + "\n\n";
|
|
665
|
+
const lastID = Number.parseInt(req.headers["last-event-id"], 10);
|
|
666
|
+
const rewind = !Number.isNaN(lastID) ? this.nextID - 1 - lastID : this.options.rewind;
|
|
667
|
+
if (rewind) {
|
|
668
|
+
this.messages.filter((m) => hasEventMatch(c.events, m.eventName)).slice(0 - rewind).forEach((m) => {
|
|
669
|
+
body += m.output;
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
c.res.write(body);
|
|
673
|
+
this.clients.add(c);
|
|
674
|
+
if (maxStreamDuration > 0) {
|
|
675
|
+
setTimeout(() => {
|
|
676
|
+
if (!c.res.writableEnded) {
|
|
677
|
+
this.unsubscribe(c);
|
|
678
|
+
}
|
|
679
|
+
}, maxStreamDuration);
|
|
680
|
+
}
|
|
681
|
+
c.res.on("close", () => this.unsubscribe(c));
|
|
682
|
+
return c;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Unsubscribes a client from the SSE channel.
|
|
686
|
+
* @param {Client} c - The client to unsubscribe.
|
|
687
|
+
*/
|
|
688
|
+
unsubscribe(c) {
|
|
689
|
+
c.res.end();
|
|
690
|
+
this.clients.delete(c);
|
|
691
|
+
}
|
|
692
|
+
clearClients() {
|
|
693
|
+
this.clients.forEach((c) => c.res.end());
|
|
694
|
+
this.clients.clear();
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Lists the clients connected to the SSE channel grouped by IP address.
|
|
698
|
+
* @returns - Returns an object where the keys are the IP addresses and the values are the number of clients connected from each IP.
|
|
699
|
+
*/
|
|
700
|
+
listClients() {
|
|
701
|
+
const rollupByIP = {};
|
|
702
|
+
this.clients.forEach((c) => {
|
|
703
|
+
const ip = c.req.socket.remoteAddress ?? "";
|
|
704
|
+
if (!(ip in rollupByIP)) {
|
|
705
|
+
rollupByIP[ip] = 0;
|
|
706
|
+
}
|
|
707
|
+
rollupByIP[ip]++;
|
|
708
|
+
});
|
|
709
|
+
return rollupByIP;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Gets the number of clients subscribed to the SSE channel.
|
|
713
|
+
* @returns - Returns the number of clients.
|
|
714
|
+
*/
|
|
715
|
+
getSubscriberCount() {
|
|
716
|
+
return this.clients.size;
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
// src/funcs/event.ts
|
|
721
|
+
var EventToolFunc = class extends ToolFunc {
|
|
722
|
+
constructor() {
|
|
723
|
+
super(...arguments);
|
|
724
|
+
// static _emitter = wrapEventEmitter()
|
|
725
|
+
this._emitter = new import_events_ex.EventEmitter();
|
|
726
|
+
// wrapEventEmitter()
|
|
727
|
+
this.description = "Return event bus";
|
|
728
|
+
this.result = "event";
|
|
729
|
+
}
|
|
730
|
+
get emitter() {
|
|
731
|
+
return this._emitter;
|
|
732
|
+
}
|
|
733
|
+
func() {
|
|
734
|
+
return this.emitter;
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
var event = new EventToolFunc(EventBusName);
|
|
738
|
+
|
|
739
|
+
// src/utils/consts.ts
|
|
740
|
+
var PASSING_SCORE = 0.618;
|
|
741
|
+
var ActionNames = ["get", "post", "put", "delete", "patch", "list", "res"];
|
|
742
|
+
var RemoteToolFuncSchema = {
|
|
743
|
+
apiRoot: {
|
|
744
|
+
type: "string",
|
|
745
|
+
get() {
|
|
746
|
+
return this._apiRoot ?? this.constructor.apiRoot;
|
|
747
|
+
},
|
|
748
|
+
set(value) {
|
|
749
|
+
this._apiRoot = value;
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
action: {
|
|
753
|
+
type: "string",
|
|
754
|
+
assign(value, dest, src, name, options) {
|
|
755
|
+
return value || "post";
|
|
756
|
+
}
|
|
757
|
+
},
|
|
758
|
+
fetchOptions: { type: "object" },
|
|
759
|
+
allowExportFunc: { type: "boolean" },
|
|
760
|
+
stream: { type: "boolean" }
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
// src/server-tools.ts
|
|
764
|
+
var ServerTools = class _ServerTools extends ToolFunc {
|
|
765
|
+
static get apiRoot() {
|
|
766
|
+
return _ServerTools._apiRoot;
|
|
767
|
+
}
|
|
768
|
+
static setApiRoot(v) {
|
|
769
|
+
_ServerTools._apiRoot = v;
|
|
770
|
+
}
|
|
771
|
+
// static get(name: string) {
|
|
772
|
+
// return this.items[name] ?? ToolFunc.get(name)
|
|
773
|
+
// }
|
|
774
|
+
// static list() {
|
|
775
|
+
// return {...ToolFunc.list(), ...this.items}
|
|
776
|
+
// }
|
|
777
|
+
// static items = ServerToolItems;
|
|
778
|
+
static toJSON() {
|
|
779
|
+
const result = {};
|
|
780
|
+
for (const name in this.items) {
|
|
781
|
+
let item = this.items[name];
|
|
782
|
+
if (item instanceof _ServerTools) {
|
|
783
|
+
if (!item.allowExportFunc) {
|
|
784
|
+
item = item.toJSON();
|
|
785
|
+
delete item.func;
|
|
786
|
+
}
|
|
787
|
+
result[name] = item;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
return result;
|
|
791
|
+
}
|
|
792
|
+
isStream(params) {
|
|
793
|
+
return this.stream;
|
|
794
|
+
}
|
|
795
|
+
func({}) {
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
var ServerToolFuncSchema = { ...RemoteToolFuncSchema };
|
|
799
|
+
ServerTools.defineProperties(ServerTools, ServerToolFuncSchema);
|
|
800
|
+
|
|
801
|
+
// src/funcs/similarity.ts
|
|
802
|
+
var import_transformers = require("@xenova/transformers");
|
|
803
|
+
|
|
804
|
+
// src/funcs/lrucache.ts
|
|
805
|
+
var import_secondary_cache = require("secondary-cache");
|
|
806
|
+
function _lrucache({ key, value, options } = {}) {
|
|
807
|
+
if (key === void 0) {
|
|
808
|
+
if (options)
|
|
809
|
+
this.cache.setDefaultOptions(options);
|
|
810
|
+
} else if (value === null) {
|
|
811
|
+
return this.cache.del(key);
|
|
812
|
+
} else if (value !== void 0) {
|
|
813
|
+
return this.cache.set(key, value, options);
|
|
814
|
+
} else {
|
|
815
|
+
return this.cache.get(key);
|
|
816
|
+
}
|
|
817
|
+
return this.cache;
|
|
818
|
+
}
|
|
819
|
+
function createLRUCache(name, options) {
|
|
820
|
+
let result = ToolFunc.get(name);
|
|
821
|
+
if (!result) {
|
|
822
|
+
result = new ToolFunc(name, {
|
|
823
|
+
func: _lrucache,
|
|
824
|
+
description: "get/set LRU cache or return the LRUCache object",
|
|
825
|
+
params: {
|
|
826
|
+
key: { name: "key", type: "string", description: "the key is undefined means change the default cache options" },
|
|
827
|
+
value: { name: "value", type: "any", description: "the value to store, if value is null means remove the key" },
|
|
828
|
+
options: { name: "options", type: "object|number", description: "the optional cache options:{capacity,expires,cleanInterval} or expires" }
|
|
829
|
+
},
|
|
830
|
+
result: "any"
|
|
831
|
+
});
|
|
832
|
+
result.cache = new import_secondary_cache.Cache(options);
|
|
833
|
+
}
|
|
834
|
+
return result;
|
|
835
|
+
}
|
|
836
|
+
var lrucache = createLRUCache("lrucache");
|
|
837
|
+
|
|
838
|
+
// src/funcs/similarity.ts
|
|
839
|
+
var ModelsCache = createLRUCache("ModelsCache", { capacity: 2, expires: 6 * 60 * 1e3 });
|
|
840
|
+
var cache = ModelsCache.runWithPosSync();
|
|
841
|
+
function average(arr) {
|
|
842
|
+
if (arr.length === 0) {
|
|
843
|
+
throw new NotFoundError("Array must not be empty");
|
|
844
|
+
}
|
|
845
|
+
const sum = arr.reduce((a, b) => a + b, 0);
|
|
846
|
+
return sum / arr.length;
|
|
847
|
+
}
|
|
848
|
+
async function _similarity({ query, texts, model } = {}) {
|
|
849
|
+
if (!model) {
|
|
850
|
+
model = this.modelId;
|
|
851
|
+
}
|
|
852
|
+
let extractor = cache.get(model);
|
|
853
|
+
if (!extractor) {
|
|
854
|
+
extractor = await pipeline("feature-extraction", model);
|
|
855
|
+
cache.set(model, extractor);
|
|
856
|
+
}
|
|
857
|
+
if (texts && query) {
|
|
858
|
+
if (!Array.isArray(texts)) {
|
|
859
|
+
texts = [texts];
|
|
860
|
+
}
|
|
861
|
+
const embeddings = await extractor(texts, { pooling: "cls", normalize: true });
|
|
862
|
+
const query_embeddings = await extractor(query, { pooling: "cls", normalize: true });
|
|
863
|
+
const scores = embeddings.tolist().map(
|
|
864
|
+
(embedding, i) => cos_sim(query_embeddings.data, embedding)
|
|
865
|
+
);
|
|
866
|
+
const result = average(scores);
|
|
867
|
+
return result;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
var similarity = new ServerTools("similarity", {
|
|
871
|
+
func: _similarity,
|
|
872
|
+
description: "Calculate the similarity between the query and the texts.",
|
|
873
|
+
params: {
|
|
874
|
+
query: { name: "query", type: "string", required: true },
|
|
875
|
+
texts: { name: "texts", type: ["string", "array"], required: true },
|
|
876
|
+
model: { name: "model", type: "string", description: "the embedding model name used" }
|
|
877
|
+
// {name: 'maxExtractors', type: 'number', description: 'the max cached embedding model count'},
|
|
878
|
+
},
|
|
879
|
+
result: "number",
|
|
880
|
+
scope: { env: import_transformers.env, pipeline: import_transformers.pipeline, cos_sim: import_transformers.cos_sim, average, cache },
|
|
881
|
+
depends: { ModelsCache },
|
|
882
|
+
setup() {
|
|
883
|
+
this.modelId = "Xenova/distiluse-base-multilingual-cased-v2";
|
|
884
|
+
}
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
// src/utils/trunc-to.ts
|
|
888
|
+
function truncTo2(n, dec = 2, up = 0.5) {
|
|
889
|
+
const d = 10 ** dec;
|
|
890
|
+
return Math.trunc((n + Number.EPSILON + up / d) * d) / d;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// src/funcs/is-similar.ts
|
|
894
|
+
function getDefaultSimilarPassingScore(modelId) {
|
|
895
|
+
const i = modelId.lastIndexOf("/");
|
|
896
|
+
let result = 0.618;
|
|
897
|
+
if (i > 0) {
|
|
898
|
+
modelId = modelId.substring(i + 1);
|
|
899
|
+
}
|
|
900
|
+
if (modelId.startsWith("stella-")) {
|
|
901
|
+
result = 0.818;
|
|
902
|
+
} else if (modelId.startsWith("Dmeta-")) {
|
|
903
|
+
result = 0.65;
|
|
904
|
+
}
|
|
905
|
+
return result;
|
|
906
|
+
}
|
|
907
|
+
async function _isSimilar({ query, texts, model, passingScore } = {}) {
|
|
908
|
+
if (passingScore == null && model) {
|
|
909
|
+
passingScore = this.getDefaultPassingScore(model);
|
|
910
|
+
}
|
|
911
|
+
const similarity2 = this.depends.similarity.getFuncWithPos();
|
|
912
|
+
const average_score = truncTo(await similarity2(query, texts, model || this.modelId));
|
|
913
|
+
if (passingScore == null) {
|
|
914
|
+
passingScore = this.passingScore;
|
|
915
|
+
}
|
|
916
|
+
return average_score >= passingScore;
|
|
917
|
+
}
|
|
918
|
+
var isSimilar = new ServerTools("isSimilar", {
|
|
919
|
+
func: _isSimilar,
|
|
920
|
+
description: "merge Segments in same topics simply",
|
|
921
|
+
params: {
|
|
922
|
+
query: { name: "query", type: "string", required: true },
|
|
923
|
+
texts: { name: "texts", type: ["string", "array"], required: true },
|
|
924
|
+
model: { name: "model", type: "string", description: "the embedding model name used" },
|
|
925
|
+
passingScore: { name: "passingScore", type: "number", description: "the min passing score" }
|
|
926
|
+
},
|
|
927
|
+
result: "boolean",
|
|
928
|
+
scope: { truncTo: truncTo2 },
|
|
929
|
+
depends: { similarity },
|
|
930
|
+
setup() {
|
|
931
|
+
this.getDefaultPassingScore = getDefaultSimilarPassingScore;
|
|
932
|
+
Object.defineProperty(this, "modelId", {
|
|
933
|
+
get() {
|
|
934
|
+
return this._modelId;
|
|
935
|
+
},
|
|
936
|
+
set(value) {
|
|
937
|
+
if (value != this.modelId) {
|
|
938
|
+
this._modelId = value;
|
|
939
|
+
this.passingScore = this.getDefaultPassingScore(value);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
this.modelId = "Xenova/distiluse-base-multilingual-cased-v2";
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
|
|
947
|
+
// src/funcs/merge-segments.ts
|
|
948
|
+
async function _mergeSegments({ segments: segments2, model } = {}) {
|
|
949
|
+
if (!model) {
|
|
950
|
+
model = this.modelId;
|
|
951
|
+
}
|
|
952
|
+
if (!segments2 || segments2.length === 0) {
|
|
953
|
+
return [];
|
|
954
|
+
}
|
|
955
|
+
const isSimilar2 = this.depends.isSimilar.getFuncWithPos();
|
|
956
|
+
let i = 0;
|
|
957
|
+
const result = [];
|
|
958
|
+
while (i < segments2.length) {
|
|
959
|
+
const item = segments2[i++];
|
|
960
|
+
if (item.length === 1 && i < segments2.length) {
|
|
961
|
+
const query = item[0];
|
|
962
|
+
const segment = segments2[i++];
|
|
963
|
+
if (await isSimilar2(query, segment, model)) {
|
|
964
|
+
segment.unshift(query);
|
|
965
|
+
result.push(segment);
|
|
966
|
+
} else {
|
|
967
|
+
result.push(item);
|
|
968
|
+
result.push(segment);
|
|
969
|
+
}
|
|
970
|
+
} else {
|
|
971
|
+
result.push(item);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
return result;
|
|
975
|
+
}
|
|
976
|
+
var mergeSegments = new ServerTools("mergeSegments", {
|
|
977
|
+
func: _mergeSegments,
|
|
978
|
+
description: "merge Segments in same topics simply",
|
|
979
|
+
params: {
|
|
980
|
+
segments: { name: "segments", type: "array", required: true, description: "the segments to merge" },
|
|
981
|
+
model: { name: "model", type: "string", description: "the embedding model name used" }
|
|
982
|
+
},
|
|
983
|
+
result: "array",
|
|
984
|
+
depends: { isSimilar },
|
|
985
|
+
setup() {
|
|
986
|
+
this.modelId = "Xenova/distiluse-base-multilingual-cased-v2";
|
|
987
|
+
}
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
// src/utils/split-sentence.ts
|
|
991
|
+
var SEPARATOR = "@";
|
|
992
|
+
var RE_SENTENCE = /(\S.+?[.!?])(?=\s+|$)|(\S.+?)(?=[\n]|$)/g;
|
|
993
|
+
var AB_SENIOR = /([A-Z][a-z]{1,2}\.)\s(\w)/g;
|
|
994
|
+
var AB_ACRONYM = /(\.[a-zA-Z]\.)\s(\w)/g;
|
|
995
|
+
var UNDO_AB_SENIOR = new RegExp("([A-Z][a-z]{1,2}\\.)" + SEPARATOR + "(\\w)", "g");
|
|
996
|
+
var UNDO_AB_ACRONYM = new RegExp("(\\.[a-zA-Z]\\.)" + SEPARATOR + "(\\w)", "g");
|
|
997
|
+
function replaceWithSeparator(text, separator, regexs) {
|
|
998
|
+
const replacement = "$1" + separator + "$2";
|
|
999
|
+
let result = text;
|
|
1000
|
+
for (let i = 0; i < regexs.length; i++) {
|
|
1001
|
+
result = result.replace(regexs[i], replacement);
|
|
1002
|
+
}
|
|
1003
|
+
return result;
|
|
1004
|
+
}
|
|
1005
|
+
function splitSentence2(text, best = true) {
|
|
1006
|
+
text = text.replace(/([。!??])([^”’])/g, "$1\n$2");
|
|
1007
|
+
text = text.replace(/(\.{6})([^”’])/g, "$1\n$2");
|
|
1008
|
+
text = text.replace(/(…{2})([^”’])/g, "$1\n$2");
|
|
1009
|
+
text = text.replace(/([。!??][”’])([^,。!??])/g, "$1\n$2");
|
|
1010
|
+
const chunks = text.split("\n");
|
|
1011
|
+
let result = [];
|
|
1012
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
1013
|
+
let chunk = chunks[i].trim();
|
|
1014
|
+
if (!chunk) {
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
if (!best) {
|
|
1018
|
+
result.push(chunk);
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
let processed = replaceWithSeparator(chunk, SEPARATOR, [AB_SENIOR, AB_ACRONYM]);
|
|
1022
|
+
let sents = Array.from(processed.matchAll(RE_SENTENCE));
|
|
1023
|
+
if (!sents.length) {
|
|
1024
|
+
result.push(chunk);
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
for (let j = 0; j < sents.length; j++) {
|
|
1028
|
+
let sentence = replaceWithSeparator(sents[j][0], " ", [UNDO_AB_SENIOR, UNDO_AB_ACRONYM]);
|
|
1029
|
+
result.push(sentence);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return result;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
// src/funcs/split-segments.ts
|
|
1036
|
+
async function _splitSegments({ texts, model } = {}) {
|
|
1037
|
+
if (typeof texts === "string") {
|
|
1038
|
+
texts = splitSentence(texts);
|
|
1039
|
+
} else if (!Array.isArray(texts)) {
|
|
1040
|
+
throwError("the texts parameter should be a string or string[]", "segments");
|
|
1041
|
+
}
|
|
1042
|
+
if (!model) {
|
|
1043
|
+
model = this.modelId;
|
|
1044
|
+
}
|
|
1045
|
+
const isSimilar2 = this.depends.isSimilar.getFuncWithPos();
|
|
1046
|
+
const segment = [];
|
|
1047
|
+
const segments2 = [];
|
|
1048
|
+
let i = 0;
|
|
1049
|
+
while (i < texts.length) {
|
|
1050
|
+
segment.push(texts[i++]);
|
|
1051
|
+
if (i >= texts.length) {
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
const query = texts[i];
|
|
1055
|
+
if (query) {
|
|
1056
|
+
if (!await isSimilar2(query, segment, model || this.modelId)) {
|
|
1057
|
+
segments2.push([...segment]);
|
|
1058
|
+
segment.length = 0;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
if (segment.length)
|
|
1063
|
+
segments2.push([...segment]);
|
|
1064
|
+
return segments2;
|
|
1065
|
+
}
|
|
1066
|
+
var splitSegments = new ServerTools("splitSegments", {
|
|
1067
|
+
func: _splitSegments,
|
|
1068
|
+
description: "Segmenting the text according to the topics simply",
|
|
1069
|
+
params: {
|
|
1070
|
+
texts: { name: "texts", type: ["string", "array"], required: true, description: "the texts to segment" },
|
|
1071
|
+
model: { name: "model", type: "string", description: "the embedding model name used" }
|
|
1072
|
+
},
|
|
1073
|
+
result: "array",
|
|
1074
|
+
scope: { splitSentence: splitSentence2, throwError: throwError2 },
|
|
1075
|
+
depends: { isSimilar },
|
|
1076
|
+
setup() {
|
|
1077
|
+
this.modelId = "Xenova/distiluse-base-multilingual-cased-v2";
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
|
|
1081
|
+
// src/funcs/segments.ts
|
|
1082
|
+
async function _segments({ texts, model = "Xenova/distiluse-base-multilingual-cased-v2" } = {}) {
|
|
1083
|
+
const splitSegments2 = this.depends.splitSegments.getFuncWithPos();
|
|
1084
|
+
const mergeSegments2 = this.depends.mergeSegments.getFuncWithPos();
|
|
1085
|
+
const segments2 = await mergeSegments2(await splitSegments2(texts, model), model);
|
|
1086
|
+
return segments2;
|
|
1087
|
+
}
|
|
1088
|
+
var segments = new ServerTools("segments", {
|
|
1089
|
+
func: _segments,
|
|
1090
|
+
description: "Segmenting the text according to the topics",
|
|
1091
|
+
params: {
|
|
1092
|
+
texts: { name: "texts", type: ["string", "array"], required: true },
|
|
1093
|
+
model: { name: "model", type: "string", description: "the embedding model name used" }
|
|
1094
|
+
},
|
|
1095
|
+
depends: { splitSegments, mergeSegments },
|
|
1096
|
+
result: "array"
|
|
1097
|
+
});
|
|
1098
|
+
|
|
1099
|
+
// src/utils/index.ts
|
|
1100
|
+
var utils_exports = {};
|
|
1101
|
+
__export(utils_exports, {
|
|
1102
|
+
AIChatRoles: () => AIChatRoles,
|
|
1103
|
+
AIMessageTypes: () => AIMessageTypes,
|
|
1104
|
+
AIStream: () => AIStream,
|
|
1105
|
+
AITextGenerationFinishReasons: () => AITextGenerationFinishReasons,
|
|
1106
|
+
AbortError: () => AbortError,
|
|
1107
|
+
AbortErrorCode: () => AbortErrorCode,
|
|
1108
|
+
ActionNames: () => ActionNames,
|
|
1109
|
+
AlreadyExistsError: () => AlreadyExistsError,
|
|
1110
|
+
AlreadyExistsErrorCode: () => AlreadyExistsErrorCode,
|
|
1111
|
+
BaseError: () => BaseError,
|
|
1112
|
+
CommonError: () => CommonError,
|
|
1113
|
+
ConfigFile: () => import_load_config_file.Config,
|
|
1114
|
+
ErrorCode: () => ErrorCode,
|
|
1115
|
+
EventBusName: () => EventBusName,
|
|
1116
|
+
EventName: () => EventName,
|
|
1117
|
+
FStringPromptTemplate: () => FStringPromptTemplate,
|
|
1118
|
+
FewShotPromptTemplate: () => FewShotPromptTemplate,
|
|
1119
|
+
FilenameReservedRegex: () => FilenameReservedRegex,
|
|
1120
|
+
GolangPromptTemplate: () => GolangPromptTemplate,
|
|
1121
|
+
HFBuiltins: () => builtins,
|
|
1122
|
+
HFEnvironment: () => EnvironmentEx,
|
|
1123
|
+
HFInterpreter: () => Interpreter,
|
|
1124
|
+
HFTemplate: () => Template,
|
|
1125
|
+
HfPromptTemplate: () => HfPromptTemplate,
|
|
1126
|
+
InternalErrorCode: () => InternalErrorCode,
|
|
1127
|
+
NotFoundError: () => NotFoundError,
|
|
1128
|
+
NotFoundErrorCode: () => NotFoundErrorCode,
|
|
1129
|
+
NotImplementationError: () => NotImplementationError,
|
|
1130
|
+
NotImplementedErrorCode: () => NotImplementedErrorCode,
|
|
1131
|
+
PASSING_SCORE: () => PASSING_SCORE,
|
|
1132
|
+
PromptExampleSelector: () => PromptExampleSelector,
|
|
1133
|
+
PromptTemplate: () => PromptTemplate,
|
|
1134
|
+
PromptTemplateTypes: () => PromptTemplateTypes,
|
|
1135
|
+
PromptTypes: () => PromptTypes,
|
|
1136
|
+
RStreamErrCode: () => RStreamErrCode,
|
|
1137
|
+
ReadableStreamError: () => ReadableStreamError,
|
|
1138
|
+
RemoteToolFuncSchema: () => RemoteToolFuncSchema,
|
|
1139
|
+
ResponseRStreamErrCode: () => ResponseRStreamErrCode,
|
|
1140
|
+
SSEChannel: () => SSEChannel,
|
|
1141
|
+
SSEChannelAlreadyClosedErrCode: () => SSEChannelAlreadyClosedErrCode,
|
|
1142
|
+
WindowsReservedNameRegex: () => WindowsReservedNameRegex,
|
|
1143
|
+
XXHashAlgorithm: () => XXHashAlgorithm,
|
|
1144
|
+
backendEventable: () => backendEventable,
|
|
1145
|
+
base32768: () => base327682,
|
|
1146
|
+
createCallbacksTransformer: () => createCallbacksTransformer,
|
|
1147
|
+
createEmptyReadableStream: () => createEmptyReadableStream,
|
|
1148
|
+
createEndWithRepetitionDetector: () => createEndWithRepetitionDetector,
|
|
1149
|
+
createError: () => createError,
|
|
1150
|
+
createEventStreamTransformer: () => createEventStreamTransformer,
|
|
1151
|
+
createHfValueFunc: () => createHfValueFunc,
|
|
1152
|
+
defaultTemplateFormat: () => defaultTemplateFormat,
|
|
1153
|
+
expandEnv: () => expandEnv,
|
|
1154
|
+
expandObjEnv: () => expandObjEnv,
|
|
1155
|
+
extNameLevel: () => extNameLevel,
|
|
1156
|
+
filenameReservedRegex: () => filenameReservedRegex,
|
|
1157
|
+
getAllEnumKeys: () => getAllEnumKeys,
|
|
1158
|
+
getConfigFileNames: () => getConfigFileNames,
|
|
1159
|
+
getConfigs: () => getConfigs,
|
|
1160
|
+
getKeysPath: () => getKeysPath,
|
|
1161
|
+
getMultiLevelExtname: () => getMultiLevelExtname,
|
|
1162
|
+
getResponseErrorReadableStream: () => getResponseErrorReadableStream,
|
|
1163
|
+
hfParse: () => parse,
|
|
1164
|
+
hfTokenize: () => tokenize,
|
|
1165
|
+
interpolateEnv: () => interpolateEnv,
|
|
1166
|
+
interpolateFString: () => interpolateFString,
|
|
1167
|
+
interpolateGolangTemplate: () => interpolateGolangTemplate,
|
|
1168
|
+
isModelNameMatched: () => isModelNameMatched,
|
|
1169
|
+
isValidFilename: () => isValidFilename,
|
|
1170
|
+
isValidFilepath: () => isValidFilepath,
|
|
1171
|
+
jsonFilterToWhere: () => jsonFilterToWhere,
|
|
1172
|
+
loadFileFromPaths: () => loadFileFromPaths,
|
|
1173
|
+
loadTextFromPaths: () => loadTextFromPaths,
|
|
1174
|
+
mimeType: () => import_with_db.mimeType,
|
|
1175
|
+
parseFString: () => parseFString,
|
|
1176
|
+
parseJsJson: () => parseJsJson,
|
|
1177
|
+
parseYaml: () => parseYaml,
|
|
1178
|
+
reControlCharsRegex: () => reControlCharsRegex,
|
|
1179
|
+
readableFromAsyncIterable: () => readableFromAsyncIterable,
|
|
1180
|
+
registerYamlTag: () => registerYamlTag,
|
|
1181
|
+
sanitizeFilename: () => sanitizeFilename,
|
|
1182
|
+
sanitizeFilepath: () => sanitizeFilepath,
|
|
1183
|
+
saveConfigFile: () => saveConfigFile,
|
|
1184
|
+
sortedValues: () => sortedValues,
|
|
1185
|
+
splitSentence: () => splitSentence2,
|
|
1186
|
+
stringifyYaml: () => import_yaml2.stringify,
|
|
1187
|
+
throwError: () => throwError2,
|
|
1188
|
+
trimStartOfStreamHelper: () => trimStartOfStreamHelper,
|
|
1189
|
+
truncTo: () => truncTo2,
|
|
1190
|
+
uuid: () => uuid,
|
|
1191
|
+
uuidNIL: () => import_uuid2.NIL,
|
|
1192
|
+
uuidParse: () => import_uuid2.parse,
|
|
1193
|
+
uuidStringify: () => import_uuid2.stringify,
|
|
1194
|
+
uuidValidate: () => import_uuid2.validate,
|
|
1195
|
+
uuidVersion: () => import_uuid2.version,
|
|
1196
|
+
uuidv1: () => import_uuid2.v1,
|
|
1197
|
+
uuidv4: () => import_uuid2.v4,
|
|
1198
|
+
uuidv5: () => import_uuid2.v5,
|
|
1199
|
+
wait: () => wait,
|
|
1200
|
+
xxhash: () => xxhash,
|
|
1201
|
+
xxhash32: () => xxhash32,
|
|
1202
|
+
xxhash64: () => xxhash64,
|
|
1203
|
+
xxhashAsStr: () => xxhashAsStr
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
// src/utils/chat.ts
|
|
1207
|
+
var AITextGenerationFinishReasons = [
|
|
1208
|
+
"stop",
|
|
1209
|
+
// model generated stop sequence
|
|
1210
|
+
"length",
|
|
1211
|
+
// model generated maximum number of tokens
|
|
1212
|
+
"content-filter",
|
|
1213
|
+
// content filter violation stopped the model
|
|
1214
|
+
"tool-calls",
|
|
1215
|
+
// model triggered tool calls
|
|
1216
|
+
"abort",
|
|
1217
|
+
// aborted by user or timeout for stream
|
|
1218
|
+
"error",
|
|
1219
|
+
// model stopped because of an error
|
|
1220
|
+
"other",
|
|
1221
|
+
null
|
|
1222
|
+
// model stopped for other reasons
|
|
1223
|
+
];
|
|
1224
|
+
var AIMessageTypes = ["human", "ai", "generic", "system", "tool"];
|
|
1225
|
+
var AIChatRoles = ["user", "assistant", "system", "tool", "tool_calls"];
|
|
1226
|
+
|
|
1227
|
+
// src/utils/prompt/consts.ts
|
|
1228
|
+
var PromptTypes = [
|
|
1229
|
+
"chat",
|
|
1230
|
+
"char",
|
|
1231
|
+
"plan"
|
|
1232
|
+
];
|
|
1233
|
+
var PromptTemplateTypes = [
|
|
1234
|
+
"internal",
|
|
1235
|
+
"hf",
|
|
1236
|
+
"fill"
|
|
1237
|
+
];
|
|
1238
|
+
|
|
1239
|
+
// src/utils/prompt/prompt-template.ts
|
|
1240
|
+
var import_custom_factory = require("custom-factory");
|
|
1241
|
+
|
|
1242
|
+
// src/utils/filterNullOrUndefined.ts
|
|
1243
|
+
function filterNullOrUndefined(obj) {
|
|
1244
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
1245
|
+
const value = obj[key];
|
|
1246
|
+
if (value != null) {
|
|
1247
|
+
acc[key] = value;
|
|
1248
|
+
}
|
|
1249
|
+
return acc;
|
|
1250
|
+
}, {});
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
// src/utils/prompt/prompt-template.ts
|
|
1254
|
+
var defaultTemplateFormat = "default";
|
|
1255
|
+
var PromptTemplate = class _PromptTemplate extends import_custom_factory.BaseFactory {
|
|
1256
|
+
static from(template, options) {
|
|
1257
|
+
return new this(template, options);
|
|
1258
|
+
}
|
|
1259
|
+
static async format(options) {
|
|
1260
|
+
const template = new this(options);
|
|
1261
|
+
return template.format();
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* If the given options.template is the template, perform formatting using that template.
|
|
1265
|
+
* @param options - The options object to check for being a template and to format.
|
|
1266
|
+
* @returns A Promise that resolves to the formatted result if options is a template; otherwise, no value is returned.
|
|
1267
|
+
*/
|
|
1268
|
+
static async formatIf(options) {
|
|
1269
|
+
if (this.isTemplate(options)) {
|
|
1270
|
+
const template = new this(options);
|
|
1271
|
+
return template.format();
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
static isTemplate(templateOpt) {
|
|
1275
|
+
if (templateOpt?.template) {
|
|
1276
|
+
const templateType = templateOpt.templateFormat || defaultTemplateFormat;
|
|
1277
|
+
const Template2 = _PromptTemplate.get(templateType);
|
|
1278
|
+
return Template2.isTemplate !== _PromptTemplate.isTemplate && Template2.isTemplate(templateOpt);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Validate/filter the data in inputVariables
|
|
1283
|
+
* @param data
|
|
1284
|
+
* @returns
|
|
1285
|
+
*/
|
|
1286
|
+
filterData(data) {
|
|
1287
|
+
if (Array.isArray(this.inputVariables)) {
|
|
1288
|
+
data = Object.fromEntries(Object.entries(data).filter(([key]) => this.inputVariables.includes(key)));
|
|
1289
|
+
}
|
|
1290
|
+
return data;
|
|
1291
|
+
}
|
|
1292
|
+
constructor(template, options) {
|
|
1293
|
+
if (typeof template === "string") {
|
|
1294
|
+
if (!options) {
|
|
1295
|
+
options = {};
|
|
1296
|
+
}
|
|
1297
|
+
options.template = template;
|
|
1298
|
+
} else if (template) {
|
|
1299
|
+
options = template;
|
|
1300
|
+
template = options.template;
|
|
1301
|
+
}
|
|
1302
|
+
const { templateFormat: templateType } = options || {};
|
|
1303
|
+
super(options);
|
|
1304
|
+
if (this.constructor === _PromptTemplate) {
|
|
1305
|
+
const TemplateClass = _PromptTemplate.get(templateType || defaultTemplateFormat);
|
|
1306
|
+
if (TemplateClass) {
|
|
1307
|
+
return Reflect.construct(TemplateClass, arguments);
|
|
1308
|
+
} else {
|
|
1309
|
+
throw new CommonError(`Prompt template type ${templateType} not found`, "PromptTemplate", 400 /* InvalidArgument */);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
_initialize(options) {
|
|
1314
|
+
throw new NotImplementationError("Not implemented", "PromptTemplate");
|
|
1315
|
+
}
|
|
1316
|
+
initialize(options) {
|
|
1317
|
+
if (this.constructor !== _PromptTemplate) {
|
|
1318
|
+
Object.assign(this, this.toJSON(options));
|
|
1319
|
+
if (!options?.ignoreInitialize) {
|
|
1320
|
+
this._initialize(options);
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
_format(data) {
|
|
1325
|
+
throw new NotImplementationError("Not implemented", "PromptTemplate");
|
|
1326
|
+
}
|
|
1327
|
+
async format(data) {
|
|
1328
|
+
const partialData = this.data;
|
|
1329
|
+
data = { ...partialData, ...data };
|
|
1330
|
+
if (partialData) {
|
|
1331
|
+
for (const [key, value] of Object.entries(partialData)) {
|
|
1332
|
+
if (data[key] === value && typeof value === "function") {
|
|
1333
|
+
delete data[key];
|
|
1334
|
+
data[key] = await value(data);
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
for (const [key, value] of Object.entries(data)) {
|
|
1339
|
+
if (value instanceof _PromptTemplate) {
|
|
1340
|
+
delete data[key];
|
|
1341
|
+
data[key] = await value.format(data);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
return this._format(data);
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* it can make sense to "partial" a prompt template - eg pass in a subset of the required values, as to create a new prompt template which expects only the remaining subset of values.
|
|
1348
|
+
* @param data the partial data
|
|
1349
|
+
* @returns the new partial PromptTemplate instance
|
|
1350
|
+
*/
|
|
1351
|
+
partial(data) {
|
|
1352
|
+
data = { ...this.data, ...data };
|
|
1353
|
+
const options = this.toJSON();
|
|
1354
|
+
options.data = data;
|
|
1355
|
+
options.ignoreInitialize = true;
|
|
1356
|
+
return new this.constructor(options);
|
|
1357
|
+
}
|
|
1358
|
+
toJSON(options = this) {
|
|
1359
|
+
let result = {
|
|
1360
|
+
template: options.template,
|
|
1361
|
+
data: options.data,
|
|
1362
|
+
inputVariables: options.inputVariables,
|
|
1363
|
+
compiledTemplate: options.compiledTemplate
|
|
1364
|
+
};
|
|
1365
|
+
if (options.templateFormat && _PromptTemplate.get(options.templateFormat) !== this.constructor) {
|
|
1366
|
+
result.templateFormat = options.templateFormat;
|
|
1367
|
+
}
|
|
1368
|
+
result = filterNullOrUndefined(result);
|
|
1369
|
+
return result;
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
// src/utils/iterator-wrap.ts
|
|
1374
|
+
async function* asyncFromSync(iterable) {
|
|
1375
|
+
const it = iterable[Symbol.iterator]();
|
|
1376
|
+
let needToClose;
|
|
1377
|
+
try {
|
|
1378
|
+
for (; ; ) {
|
|
1379
|
+
needToClose = false;
|
|
1380
|
+
const rec = it.next();
|
|
1381
|
+
needToClose = true;
|
|
1382
|
+
if (rec.done) {
|
|
1383
|
+
needToClose = false;
|
|
1384
|
+
return await rec.value;
|
|
1385
|
+
}
|
|
1386
|
+
yield await rec.value;
|
|
1387
|
+
}
|
|
1388
|
+
} finally {
|
|
1389
|
+
if (needToClose) {
|
|
1390
|
+
await it.return?.().value;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
function asyncWrap(iterable) {
|
|
1395
|
+
return isIterable(iterable) ? asyncFromSync(iterable) : iterable;
|
|
1396
|
+
}
|
|
1397
|
+
function isIterable(value) {
|
|
1398
|
+
return !!value && typeof value[Symbol.iterator] === "function";
|
|
1399
|
+
}
|
|
1400
|
+
function iteratorWrap(iterable) {
|
|
1401
|
+
return engineValid ? iterable : asyncWrap(iterable);
|
|
1402
|
+
}
|
|
1403
|
+
var engineValid = false;
|
|
1404
|
+
isEngineValid().then((valid) => {
|
|
1405
|
+
engineValid = valid;
|
|
1406
|
+
});
|
|
1407
|
+
async function isEngineValid() {
|
|
1408
|
+
let finallyCalled = false;
|
|
1409
|
+
let catchCalled = false;
|
|
1410
|
+
try {
|
|
1411
|
+
for await (const _x of function* () {
|
|
1412
|
+
try {
|
|
1413
|
+
yield Promise.reject();
|
|
1414
|
+
} finally {
|
|
1415
|
+
finallyCalled = true;
|
|
1416
|
+
}
|
|
1417
|
+
}()) {
|
|
1418
|
+
throw new Error("impossible");
|
|
1419
|
+
}
|
|
1420
|
+
} catch {
|
|
1421
|
+
catchCalled = true;
|
|
1422
|
+
}
|
|
1423
|
+
return catchCalled && finallyCalled;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
// src/utils/prompt/prompt-example-selector.ts
|
|
1427
|
+
var PromptExampleSelector = class {
|
|
1428
|
+
/**
|
|
1429
|
+
* Constructs a new `PromptExampleSelector` instance with the given examples and options.
|
|
1430
|
+
* @param examples - The prompt examples to select from.
|
|
1431
|
+
* @param options - An optional configuration object.
|
|
1432
|
+
*/
|
|
1433
|
+
constructor(examples, options) {
|
|
1434
|
+
this.initialize(examples, options);
|
|
1435
|
+
}
|
|
1436
|
+
/**
|
|
1437
|
+
* Initializes the selector with examples and options.
|
|
1438
|
+
* @param examples - The prompt examples to select from.
|
|
1439
|
+
* @param options - An optional configuration object.
|
|
1440
|
+
*/
|
|
1441
|
+
initialize(examples, options) {
|
|
1442
|
+
const maxLength = options?.maxLength;
|
|
1443
|
+
if (maxLength > 0) {
|
|
1444
|
+
this.maxLength = maxLength;
|
|
1445
|
+
}
|
|
1446
|
+
if (options?.threshold !== void 0) {
|
|
1447
|
+
if (options.threshold === true) {
|
|
1448
|
+
this.threshold = 0.5;
|
|
1449
|
+
} else if (options.threshold !== false) {
|
|
1450
|
+
this.threshold = options.threshold;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
this.examples = iteratorWrap(examples);
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Select an example. Can overwrite this in subclasses.
|
|
1457
|
+
* @param example - The example to potentially select.
|
|
1458
|
+
* @param [threshold] - report the current probability [0-1) for the selecting example.
|
|
1459
|
+
* @returns The selected example, or `undefined` if not selected.
|
|
1460
|
+
*/
|
|
1461
|
+
selectExample(example, threshold) {
|
|
1462
|
+
if (threshold === void 0 || threshold <= this.threshold) {
|
|
1463
|
+
return example;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* Asynchronously selects examples from the given examples, applying the configured threshold (if any).
|
|
1468
|
+
* @param examples - The examples to select from. Defaults to the selector's examples if not provided.
|
|
1469
|
+
* @returns An asynchronous iterator yielding selected examples.
|
|
1470
|
+
*/
|
|
1471
|
+
async *selectExamples(examples = this.examples) {
|
|
1472
|
+
let count = this.maxLength || Infinity;
|
|
1473
|
+
for await (const example of examples) {
|
|
1474
|
+
const threshold = typeof this.threshold === "number" ? Math.random() : void 0;
|
|
1475
|
+
const ex = this.selectExample(example, threshold);
|
|
1476
|
+
if (ex) {
|
|
1477
|
+
if (--count < 0) {
|
|
1478
|
+
break;
|
|
1479
|
+
}
|
|
1480
|
+
yield example;
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
/**
|
|
1485
|
+
* Returns an asynchronous iterator for the selected examples.
|
|
1486
|
+
* @returns An asynchronous iterator over the selected examples.
|
|
1487
|
+
*/
|
|
1488
|
+
async *[Symbol.asyncIterator]() {
|
|
1489
|
+
yield* this.selectExamples();
|
|
1490
|
+
}
|
|
1491
|
+
};
|
|
1492
|
+
|
|
1493
|
+
// src/utils/prompt/template/jinja/src/lexer.ts
|
|
1494
|
+
var TOKEN_TYPES = Object.freeze({
|
|
1495
|
+
Text: "Text",
|
|
1496
|
+
// The text between Jinja statements or expressions
|
|
1497
|
+
NumericLiteral: "NumericLiteral",
|
|
1498
|
+
// e.g., 123
|
|
1499
|
+
BooleanLiteral: "BooleanLiteral",
|
|
1500
|
+
// true or false
|
|
1501
|
+
StringLiteral: "StringLiteral",
|
|
1502
|
+
// 'string'
|
|
1503
|
+
Identifier: "Identifier",
|
|
1504
|
+
// Variables, functions, etc.
|
|
1505
|
+
Equals: "Equals",
|
|
1506
|
+
// =
|
|
1507
|
+
OpenParen: "OpenParen",
|
|
1508
|
+
// (
|
|
1509
|
+
CloseParen: "CloseParen",
|
|
1510
|
+
// )
|
|
1511
|
+
OpenStatement: "OpenStatement",
|
|
1512
|
+
// {%
|
|
1513
|
+
CloseStatement: "CloseStatement",
|
|
1514
|
+
// %}
|
|
1515
|
+
OpenExpression: "OpenExpression",
|
|
1516
|
+
// {{
|
|
1517
|
+
CloseExpression: "CloseExpression",
|
|
1518
|
+
// }}
|
|
1519
|
+
OpenSquareBracket: "OpenSquareBracket",
|
|
1520
|
+
// [
|
|
1521
|
+
CloseSquareBracket: "CloseSquareBracket",
|
|
1522
|
+
// ]
|
|
1523
|
+
OpenCurlyBracket: "OpenCurlyBracket",
|
|
1524
|
+
// {
|
|
1525
|
+
CloseCurlyBracket: "CloseCurlyBracket",
|
|
1526
|
+
// }
|
|
1527
|
+
Comma: "Comma",
|
|
1528
|
+
// ,
|
|
1529
|
+
Dot: "Dot",
|
|
1530
|
+
// .
|
|
1531
|
+
Colon: "Colon",
|
|
1532
|
+
// :
|
|
1533
|
+
Pipe: "Pipe",
|
|
1534
|
+
// |
|
|
1535
|
+
CallOperator: "CallOperator",
|
|
1536
|
+
// ()
|
|
1537
|
+
AdditiveBinaryOperator: "AdditiveBinaryOperator",
|
|
1538
|
+
// + -
|
|
1539
|
+
MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator",
|
|
1540
|
+
// * / %
|
|
1541
|
+
ComparisonBinaryOperator: "ComparisonBinaryOperator",
|
|
1542
|
+
// < > <= >= == !=
|
|
1543
|
+
UnaryOperator: "UnaryOperator",
|
|
1544
|
+
// ! - +
|
|
1545
|
+
// Keywords
|
|
1546
|
+
Set: "Set",
|
|
1547
|
+
If: "If",
|
|
1548
|
+
For: "For",
|
|
1549
|
+
In: "In",
|
|
1550
|
+
Is: "Is",
|
|
1551
|
+
NotIn: "NotIn",
|
|
1552
|
+
Else: "Else",
|
|
1553
|
+
EndIf: "EndIf",
|
|
1554
|
+
ElseIf: "ElseIf",
|
|
1555
|
+
EndFor: "EndFor",
|
|
1556
|
+
And: "And",
|
|
1557
|
+
Or: "Or",
|
|
1558
|
+
Not: "UnaryOperator"
|
|
1559
|
+
});
|
|
1560
|
+
var KEYWORDS = Object.freeze({
|
|
1561
|
+
set: TOKEN_TYPES.Set,
|
|
1562
|
+
for: TOKEN_TYPES.For,
|
|
1563
|
+
in: TOKEN_TYPES.In,
|
|
1564
|
+
is: TOKEN_TYPES.Is,
|
|
1565
|
+
if: TOKEN_TYPES.If,
|
|
1566
|
+
else: TOKEN_TYPES.Else,
|
|
1567
|
+
endif: TOKEN_TYPES.EndIf,
|
|
1568
|
+
elif: TOKEN_TYPES.ElseIf,
|
|
1569
|
+
endfor: TOKEN_TYPES.EndFor,
|
|
1570
|
+
and: TOKEN_TYPES.And,
|
|
1571
|
+
or: TOKEN_TYPES.Or,
|
|
1572
|
+
not: TOKEN_TYPES.Not,
|
|
1573
|
+
"not in": TOKEN_TYPES.NotIn,
|
|
1574
|
+
// Literals
|
|
1575
|
+
true: TOKEN_TYPES.BooleanLiteral,
|
|
1576
|
+
false: TOKEN_TYPES.BooleanLiteral
|
|
1577
|
+
});
|
|
1578
|
+
var Token = class {
|
|
1579
|
+
/**
|
|
1580
|
+
* Constructs a new Token.
|
|
1581
|
+
* @param {string} value The raw value as seen inside the source code.
|
|
1582
|
+
* @param {TokenType} type The type of token.
|
|
1583
|
+
*/
|
|
1584
|
+
constructor(value, type) {
|
|
1585
|
+
this.value = value;
|
|
1586
|
+
this.type = type;
|
|
1587
|
+
}
|
|
1588
|
+
};
|
|
1589
|
+
function isWord(char) {
|
|
1590
|
+
return /\w/.test(char);
|
|
1591
|
+
}
|
|
1592
|
+
function isInteger(char) {
|
|
1593
|
+
return /[0-9]/.test(char);
|
|
1594
|
+
}
|
|
1595
|
+
var ORDERED_MAPPING_TABLE = [
|
|
1596
|
+
// Control sequences
|
|
1597
|
+
["{%", TOKEN_TYPES.OpenStatement],
|
|
1598
|
+
["%}", TOKEN_TYPES.CloseStatement],
|
|
1599
|
+
["{{", TOKEN_TYPES.OpenExpression],
|
|
1600
|
+
["}}", TOKEN_TYPES.CloseExpression],
|
|
1601
|
+
// Single character tokens
|
|
1602
|
+
["(", TOKEN_TYPES.OpenParen],
|
|
1603
|
+
[")", TOKEN_TYPES.CloseParen],
|
|
1604
|
+
["{", TOKEN_TYPES.OpenCurlyBracket],
|
|
1605
|
+
["}", TOKEN_TYPES.CloseCurlyBracket],
|
|
1606
|
+
["[", TOKEN_TYPES.OpenSquareBracket],
|
|
1607
|
+
["]", TOKEN_TYPES.CloseSquareBracket],
|
|
1608
|
+
[",", TOKEN_TYPES.Comma],
|
|
1609
|
+
[".", TOKEN_TYPES.Dot],
|
|
1610
|
+
[":", TOKEN_TYPES.Colon],
|
|
1611
|
+
["|", TOKEN_TYPES.Pipe],
|
|
1612
|
+
// Comparison operators
|
|
1613
|
+
["<=", TOKEN_TYPES.ComparisonBinaryOperator],
|
|
1614
|
+
[">=", TOKEN_TYPES.ComparisonBinaryOperator],
|
|
1615
|
+
["==", TOKEN_TYPES.ComparisonBinaryOperator],
|
|
1616
|
+
["!=", TOKEN_TYPES.ComparisonBinaryOperator],
|
|
1617
|
+
["<", TOKEN_TYPES.ComparisonBinaryOperator],
|
|
1618
|
+
[">", TOKEN_TYPES.ComparisonBinaryOperator],
|
|
1619
|
+
// Arithmetic operators
|
|
1620
|
+
["+", TOKEN_TYPES.AdditiveBinaryOperator],
|
|
1621
|
+
["-", TOKEN_TYPES.AdditiveBinaryOperator],
|
|
1622
|
+
["*", TOKEN_TYPES.MultiplicativeBinaryOperator],
|
|
1623
|
+
["/", TOKEN_TYPES.MultiplicativeBinaryOperator],
|
|
1624
|
+
["%", TOKEN_TYPES.MultiplicativeBinaryOperator],
|
|
1625
|
+
// Assignment operator
|
|
1626
|
+
["=", TOKEN_TYPES.Equals]
|
|
1627
|
+
];
|
|
1628
|
+
var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([
|
|
1629
|
+
["n", "\n"],
|
|
1630
|
+
// New line
|
|
1631
|
+
["t", " "],
|
|
1632
|
+
// Horizontal tab
|
|
1633
|
+
["r", "\r"],
|
|
1634
|
+
// Carriage return
|
|
1635
|
+
["b", "\b"],
|
|
1636
|
+
// Backspace
|
|
1637
|
+
["f", "\f"],
|
|
1638
|
+
// Form feed
|
|
1639
|
+
["v", "\v"],
|
|
1640
|
+
// Vertical tab
|
|
1641
|
+
["'", "'"],
|
|
1642
|
+
// Single quote
|
|
1643
|
+
['"', '"'],
|
|
1644
|
+
// Double quote
|
|
1645
|
+
["\\", "\\"]
|
|
1646
|
+
// Backslash
|
|
1647
|
+
]);
|
|
1648
|
+
function preprocess(template, options = {}) {
|
|
1649
|
+
if (template.endsWith("\n")) {
|
|
1650
|
+
template = template.slice(0, -1);
|
|
1651
|
+
}
|
|
1652
|
+
template = template.replace(/{#.*?#}/gs, "{##}");
|
|
1653
|
+
if (options.lstrip_blocks) {
|
|
1654
|
+
template = template.replace(/^[ \t]*({[#%])/gm, "$1");
|
|
1655
|
+
}
|
|
1656
|
+
if (options.trim_blocks) {
|
|
1657
|
+
template = template.replace(/([#%]})\n/g, "$1");
|
|
1658
|
+
}
|
|
1659
|
+
return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{");
|
|
1660
|
+
}
|
|
1661
|
+
function tokenize(source, options = {}) {
|
|
1662
|
+
const tokens = [];
|
|
1663
|
+
const src = preprocess(source, options);
|
|
1664
|
+
let cursorPosition = 0;
|
|
1665
|
+
const consumeWhile = (predicate) => {
|
|
1666
|
+
let str = "";
|
|
1667
|
+
while (predicate(src[cursorPosition])) {
|
|
1668
|
+
if (src[cursorPosition] === "\\") {
|
|
1669
|
+
++cursorPosition;
|
|
1670
|
+
if (cursorPosition >= src.length)
|
|
1671
|
+
throw new SyntaxError("Unexpected end of input");
|
|
1672
|
+
const escaped = src[cursorPosition++];
|
|
1673
|
+
const unescaped = ESCAPE_CHARACTERS.get(escaped);
|
|
1674
|
+
if (unescaped === void 0) {
|
|
1675
|
+
throw new SyntaxError(`Unexpected escaped character: ${escaped}`);
|
|
1676
|
+
}
|
|
1677
|
+
str += unescaped;
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
str += src[cursorPosition++];
|
|
1681
|
+
if (cursorPosition >= src.length)
|
|
1682
|
+
throw new SyntaxError("Unexpected end of input");
|
|
1683
|
+
}
|
|
1684
|
+
return str;
|
|
1685
|
+
};
|
|
1686
|
+
main:
|
|
1687
|
+
while (cursorPosition < src.length) {
|
|
1688
|
+
const lastTokenType = tokens.at(-1)?.type;
|
|
1689
|
+
if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) {
|
|
1690
|
+
let text = "";
|
|
1691
|
+
while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression
|
|
1692
|
+
!(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) {
|
|
1693
|
+
text += src[cursorPosition++];
|
|
1694
|
+
}
|
|
1695
|
+
if (text.length > 0) {
|
|
1696
|
+
tokens.push(new Token(text, TOKEN_TYPES.Text));
|
|
1697
|
+
continue;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
consumeWhile((char2) => /\s/.test(char2));
|
|
1701
|
+
const char = src[cursorPosition];
|
|
1702
|
+
if (char === "-" || char === "+") {
|
|
1703
|
+
const lastTokenType2 = tokens.at(-1)?.type;
|
|
1704
|
+
if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) {
|
|
1705
|
+
throw new SyntaxError(`Unexpected character: ${char}`);
|
|
1706
|
+
}
|
|
1707
|
+
switch (lastTokenType2) {
|
|
1708
|
+
case TOKEN_TYPES.Identifier:
|
|
1709
|
+
case TOKEN_TYPES.NumericLiteral:
|
|
1710
|
+
case TOKEN_TYPES.BooleanLiteral:
|
|
1711
|
+
case TOKEN_TYPES.StringLiteral:
|
|
1712
|
+
case TOKEN_TYPES.CloseParen:
|
|
1713
|
+
case TOKEN_TYPES.CloseSquareBracket:
|
|
1714
|
+
break;
|
|
1715
|
+
default: {
|
|
1716
|
+
++cursorPosition;
|
|
1717
|
+
const num = consumeWhile(isInteger);
|
|
1718
|
+
tokens.push(
|
|
1719
|
+
new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator)
|
|
1720
|
+
);
|
|
1721
|
+
continue;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
for (const [char2, token] of ORDERED_MAPPING_TABLE) {
|
|
1726
|
+
const slice2 = src.slice(cursorPosition, cursorPosition + char2.length);
|
|
1727
|
+
if (slice2 === char2) {
|
|
1728
|
+
tokens.push(new Token(char2, token));
|
|
1729
|
+
cursorPosition += char2.length;
|
|
1730
|
+
continue main;
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
if (char === "'" || char === '"') {
|
|
1734
|
+
++cursorPosition;
|
|
1735
|
+
const str = consumeWhile((c) => c !== char);
|
|
1736
|
+
tokens.push(new Token(str, TOKEN_TYPES.StringLiteral));
|
|
1737
|
+
++cursorPosition;
|
|
1738
|
+
continue;
|
|
1739
|
+
}
|
|
1740
|
+
if (isInteger(char)) {
|
|
1741
|
+
const num = consumeWhile(isInteger);
|
|
1742
|
+
tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral));
|
|
1743
|
+
continue;
|
|
1744
|
+
}
|
|
1745
|
+
if (isWord(char)) {
|
|
1746
|
+
const word = consumeWhile(isWord);
|
|
1747
|
+
const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier;
|
|
1748
|
+
if (type === TOKEN_TYPES.In && tokens.at(-1)?.type === TOKEN_TYPES.Not) {
|
|
1749
|
+
tokens.pop();
|
|
1750
|
+
tokens.push(new Token("not in", TOKEN_TYPES.NotIn));
|
|
1751
|
+
} else {
|
|
1752
|
+
tokens.push(new Token(word, type));
|
|
1753
|
+
}
|
|
1754
|
+
continue;
|
|
1755
|
+
}
|
|
1756
|
+
throw new SyntaxError(`Unexpected character: ${char}`);
|
|
1757
|
+
}
|
|
1758
|
+
return tokens;
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
// src/utils/prompt/template/jinja/src/ast.ts
|
|
1762
|
+
var Statement = class {
|
|
1763
|
+
constructor() {
|
|
1764
|
+
this.type = "Statement";
|
|
1765
|
+
}
|
|
1766
|
+
};
|
|
1767
|
+
var Program = class extends Statement {
|
|
1768
|
+
constructor(body) {
|
|
1769
|
+
super();
|
|
1770
|
+
this.body = body;
|
|
1771
|
+
this.type = "Program";
|
|
1772
|
+
}
|
|
1773
|
+
};
|
|
1774
|
+
var If = class extends Statement {
|
|
1775
|
+
constructor(test, body, alternate) {
|
|
1776
|
+
super();
|
|
1777
|
+
this.test = test;
|
|
1778
|
+
this.body = body;
|
|
1779
|
+
this.alternate = alternate;
|
|
1780
|
+
this.type = "If";
|
|
1781
|
+
}
|
|
1782
|
+
};
|
|
1783
|
+
var For = class extends Statement {
|
|
1784
|
+
constructor(loopvar, iterable, body) {
|
|
1785
|
+
super();
|
|
1786
|
+
this.loopvar = loopvar;
|
|
1787
|
+
this.iterable = iterable;
|
|
1788
|
+
this.body = body;
|
|
1789
|
+
this.type = "For";
|
|
1790
|
+
}
|
|
1791
|
+
};
|
|
1792
|
+
var SetStatement = class extends Statement {
|
|
1793
|
+
constructor(assignee, value) {
|
|
1794
|
+
super();
|
|
1795
|
+
this.assignee = assignee;
|
|
1796
|
+
this.value = value;
|
|
1797
|
+
this.type = "Set";
|
|
1798
|
+
}
|
|
1799
|
+
};
|
|
1800
|
+
var Expression = class extends Statement {
|
|
1801
|
+
constructor() {
|
|
1802
|
+
super(...arguments);
|
|
1803
|
+
this.type = "Expression";
|
|
1804
|
+
}
|
|
1805
|
+
};
|
|
1806
|
+
var MemberExpression = class extends Expression {
|
|
1807
|
+
constructor(object, property, computed) {
|
|
1808
|
+
super();
|
|
1809
|
+
this.object = object;
|
|
1810
|
+
this.property = property;
|
|
1811
|
+
this.computed = computed;
|
|
1812
|
+
this.type = "MemberExpression";
|
|
1813
|
+
}
|
|
1814
|
+
};
|
|
1815
|
+
var CallExpression = class extends Expression {
|
|
1816
|
+
constructor(callee, args) {
|
|
1817
|
+
super();
|
|
1818
|
+
this.callee = callee;
|
|
1819
|
+
this.args = args;
|
|
1820
|
+
this.type = "CallExpression";
|
|
1821
|
+
}
|
|
1822
|
+
};
|
|
1823
|
+
var Identifier = class extends Expression {
|
|
1824
|
+
/**
|
|
1825
|
+
* @param {string} value The name of the identifier
|
|
1826
|
+
*/
|
|
1827
|
+
constructor(value) {
|
|
1828
|
+
super();
|
|
1829
|
+
this.value = value;
|
|
1830
|
+
this.type = "Identifier";
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
var Literal = class extends Expression {
|
|
1834
|
+
constructor(value) {
|
|
1835
|
+
super();
|
|
1836
|
+
this.value = value;
|
|
1837
|
+
this.type = "Literal";
|
|
1838
|
+
}
|
|
1839
|
+
};
|
|
1840
|
+
var NumericLiteral = class extends Literal {
|
|
1841
|
+
constructor() {
|
|
1842
|
+
super(...arguments);
|
|
1843
|
+
this.type = "NumericLiteral";
|
|
1844
|
+
}
|
|
1845
|
+
};
|
|
1846
|
+
var StringLiteral = class extends Literal {
|
|
1847
|
+
constructor() {
|
|
1848
|
+
super(...arguments);
|
|
1849
|
+
this.type = "StringLiteral";
|
|
1850
|
+
}
|
|
1851
|
+
};
|
|
1852
|
+
var BooleanLiteral = class extends Literal {
|
|
1853
|
+
constructor() {
|
|
1854
|
+
super(...arguments);
|
|
1855
|
+
this.type = "BooleanLiteral";
|
|
1856
|
+
}
|
|
1857
|
+
};
|
|
1858
|
+
var ArrayLiteral = class extends Literal {
|
|
1859
|
+
constructor() {
|
|
1860
|
+
super(...arguments);
|
|
1861
|
+
this.type = "ArrayLiteral";
|
|
1862
|
+
}
|
|
1863
|
+
};
|
|
1864
|
+
var TupleLiteral = class extends Literal {
|
|
1865
|
+
constructor() {
|
|
1866
|
+
super(...arguments);
|
|
1867
|
+
this.type = "TupleLiteral";
|
|
1868
|
+
}
|
|
1869
|
+
};
|
|
1870
|
+
var ObjectLiteral = class extends Literal {
|
|
1871
|
+
constructor() {
|
|
1872
|
+
super(...arguments);
|
|
1873
|
+
this.type = "ObjectLiteral";
|
|
1874
|
+
}
|
|
1875
|
+
};
|
|
1876
|
+
var BinaryExpression = class extends Expression {
|
|
1877
|
+
constructor(operator, left, right) {
|
|
1878
|
+
super();
|
|
1879
|
+
this.operator = operator;
|
|
1880
|
+
this.left = left;
|
|
1881
|
+
this.right = right;
|
|
1882
|
+
this.type = "BinaryExpression";
|
|
1883
|
+
}
|
|
1884
|
+
};
|
|
1885
|
+
var FilterExpression = class extends Expression {
|
|
1886
|
+
constructor(operand, filter) {
|
|
1887
|
+
super();
|
|
1888
|
+
this.operand = operand;
|
|
1889
|
+
this.filter = filter;
|
|
1890
|
+
this.type = "FilterExpression";
|
|
1891
|
+
}
|
|
1892
|
+
};
|
|
1893
|
+
var TestExpression = class extends Expression {
|
|
1894
|
+
constructor(operand, negate, test) {
|
|
1895
|
+
super();
|
|
1896
|
+
this.operand = operand;
|
|
1897
|
+
this.negate = negate;
|
|
1898
|
+
this.test = test;
|
|
1899
|
+
this.type = "TestExpression";
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
var UnaryExpression = class extends Expression {
|
|
1903
|
+
constructor(operator, argument) {
|
|
1904
|
+
super();
|
|
1905
|
+
this.operator = operator;
|
|
1906
|
+
this.argument = argument;
|
|
1907
|
+
this.type = "UnaryExpression";
|
|
1908
|
+
}
|
|
1909
|
+
};
|
|
1910
|
+
var SliceExpression = class extends Expression {
|
|
1911
|
+
constructor(start = void 0, stop = void 0, step = void 0) {
|
|
1912
|
+
super();
|
|
1913
|
+
this.start = start;
|
|
1914
|
+
this.stop = stop;
|
|
1915
|
+
this.step = step;
|
|
1916
|
+
this.type = "SliceExpression";
|
|
1917
|
+
}
|
|
1918
|
+
};
|
|
1919
|
+
var KeywordArgumentExpression = class extends Expression {
|
|
1920
|
+
constructor(key, value) {
|
|
1921
|
+
super();
|
|
1922
|
+
this.key = key;
|
|
1923
|
+
this.value = value;
|
|
1924
|
+
this.type = "KeywordArgumentExpression";
|
|
1925
|
+
}
|
|
1926
|
+
};
|
|
1927
|
+
|
|
1928
|
+
// src/utils/prompt/template/jinja/src/parser.ts
|
|
1929
|
+
function parse(tokens) {
|
|
1930
|
+
const program = new Program([]);
|
|
1931
|
+
let current = 0;
|
|
1932
|
+
function expect(type, error) {
|
|
1933
|
+
const prev = tokens[current++];
|
|
1934
|
+
if (!prev || prev.type !== type) {
|
|
1935
|
+
throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`);
|
|
1936
|
+
}
|
|
1937
|
+
return prev;
|
|
1938
|
+
}
|
|
1939
|
+
function parseAny() {
|
|
1940
|
+
let result;
|
|
1941
|
+
switch (tokens[current].type) {
|
|
1942
|
+
case TOKEN_TYPES.Text:
|
|
1943
|
+
result = parseText();
|
|
1944
|
+
break;
|
|
1945
|
+
case TOKEN_TYPES.OpenStatement:
|
|
1946
|
+
result = parseJinjaStatement();
|
|
1947
|
+
result.isStatement = true;
|
|
1948
|
+
break;
|
|
1949
|
+
case TOKEN_TYPES.OpenExpression:
|
|
1950
|
+
result = parseJinjaExpression();
|
|
1951
|
+
result.isStatement = true;
|
|
1952
|
+
break;
|
|
1953
|
+
default:
|
|
1954
|
+
throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`);
|
|
1955
|
+
}
|
|
1956
|
+
return result;
|
|
1957
|
+
}
|
|
1958
|
+
function not(...types) {
|
|
1959
|
+
return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type);
|
|
1960
|
+
}
|
|
1961
|
+
function is(...types) {
|
|
1962
|
+
return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type);
|
|
1963
|
+
}
|
|
1964
|
+
function parseText() {
|
|
1965
|
+
return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value);
|
|
1966
|
+
}
|
|
1967
|
+
function parseJinjaStatement() {
|
|
1968
|
+
expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token");
|
|
1969
|
+
let result;
|
|
1970
|
+
switch (tokens[current].type) {
|
|
1971
|
+
case TOKEN_TYPES.Set:
|
|
1972
|
+
++current;
|
|
1973
|
+
result = parseSetStatement();
|
|
1974
|
+
expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token");
|
|
1975
|
+
break;
|
|
1976
|
+
case TOKEN_TYPES.If:
|
|
1977
|
+
++current;
|
|
1978
|
+
result = parseIfStatement();
|
|
1979
|
+
expect(TOKEN_TYPES.OpenStatement, "Expected {% token");
|
|
1980
|
+
expect(TOKEN_TYPES.EndIf, "Expected endif token");
|
|
1981
|
+
expect(TOKEN_TYPES.CloseStatement, "Expected %} token");
|
|
1982
|
+
break;
|
|
1983
|
+
case TOKEN_TYPES.For:
|
|
1984
|
+
++current;
|
|
1985
|
+
result = parseForStatement();
|
|
1986
|
+
expect(TOKEN_TYPES.OpenStatement, "Expected {% token");
|
|
1987
|
+
expect(TOKEN_TYPES.EndFor, "Expected endfor token");
|
|
1988
|
+
expect(TOKEN_TYPES.CloseStatement, "Expected %} token");
|
|
1989
|
+
break;
|
|
1990
|
+
default:
|
|
1991
|
+
throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`);
|
|
1992
|
+
}
|
|
1993
|
+
return result;
|
|
1994
|
+
}
|
|
1995
|
+
function parseJinjaExpression() {
|
|
1996
|
+
expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token");
|
|
1997
|
+
const result = parseExpression();
|
|
1998
|
+
expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token");
|
|
1999
|
+
return result;
|
|
2000
|
+
}
|
|
2001
|
+
function parseSetStatement() {
|
|
2002
|
+
const left = parseExpression();
|
|
2003
|
+
if (is(TOKEN_TYPES.Equals)) {
|
|
2004
|
+
++current;
|
|
2005
|
+
const value = parseSetStatement();
|
|
2006
|
+
return new SetStatement(left, value);
|
|
2007
|
+
}
|
|
2008
|
+
return left;
|
|
2009
|
+
}
|
|
2010
|
+
function parseIfStatement() {
|
|
2011
|
+
const test = parseExpression();
|
|
2012
|
+
expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token");
|
|
2013
|
+
const body = [];
|
|
2014
|
+
const alternate = [];
|
|
2015
|
+
while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && (tokens[current + 1]?.type === TOKEN_TYPES.ElseIf || tokens[current + 1]?.type === TOKEN_TYPES.Else || tokens[current + 1]?.type === TOKEN_TYPES.EndIf))) {
|
|
2016
|
+
body.push(parseAny());
|
|
2017
|
+
}
|
|
2018
|
+
if (tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type !== TOKEN_TYPES.EndIf) {
|
|
2019
|
+
++current;
|
|
2020
|
+
if (is(TOKEN_TYPES.ElseIf)) {
|
|
2021
|
+
expect(TOKEN_TYPES.ElseIf, "Expected elseif token");
|
|
2022
|
+
alternate.push(parseIfStatement());
|
|
2023
|
+
} else {
|
|
2024
|
+
expect(TOKEN_TYPES.Else, "Expected else token");
|
|
2025
|
+
expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token");
|
|
2026
|
+
while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndIf)) {
|
|
2027
|
+
alternate.push(parseAny());
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
return new If(test, body, alternate);
|
|
2032
|
+
}
|
|
2033
|
+
function parseExpressionSequence(primary = false) {
|
|
2034
|
+
const fn = primary ? parsePrimaryExpression : parseExpression;
|
|
2035
|
+
const expressions = [fn()];
|
|
2036
|
+
const isTuple = is(TOKEN_TYPES.Comma);
|
|
2037
|
+
while (isTuple) {
|
|
2038
|
+
++current;
|
|
2039
|
+
expressions.push(fn());
|
|
2040
|
+
if (!is(TOKEN_TYPES.Comma)) {
|
|
2041
|
+
break;
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
return isTuple ? new TupleLiteral(expressions) : expressions[0];
|
|
2045
|
+
}
|
|
2046
|
+
function parseForStatement() {
|
|
2047
|
+
const loopVariable = parseExpressionSequence(true);
|
|
2048
|
+
if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) {
|
|
2049
|
+
throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`);
|
|
2050
|
+
}
|
|
2051
|
+
expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable");
|
|
2052
|
+
const iterable = parseExpression();
|
|
2053
|
+
expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token");
|
|
2054
|
+
const body = [];
|
|
2055
|
+
while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) {
|
|
2056
|
+
body.push(parseAny());
|
|
2057
|
+
}
|
|
2058
|
+
return new For(loopVariable, iterable, body);
|
|
2059
|
+
}
|
|
2060
|
+
function parseExpression() {
|
|
2061
|
+
return parseTernaryExpression();
|
|
2062
|
+
}
|
|
2063
|
+
function parseTernaryExpression() {
|
|
2064
|
+
const a = parseLogicalOrExpression();
|
|
2065
|
+
if (is(TOKEN_TYPES.If)) {
|
|
2066
|
+
++current;
|
|
2067
|
+
const predicate = parseLogicalOrExpression();
|
|
2068
|
+
expect(TOKEN_TYPES.Else, "Expected else token");
|
|
2069
|
+
const b = parseLogicalOrExpression();
|
|
2070
|
+
return new If(predicate, [a], [b]);
|
|
2071
|
+
}
|
|
2072
|
+
return a;
|
|
2073
|
+
}
|
|
2074
|
+
function parseLogicalOrExpression() {
|
|
2075
|
+
let left = parseLogicalAndExpression();
|
|
2076
|
+
while (is(TOKEN_TYPES.Or)) {
|
|
2077
|
+
const operator = tokens[current];
|
|
2078
|
+
++current;
|
|
2079
|
+
const right = parseLogicalAndExpression();
|
|
2080
|
+
left = new BinaryExpression(operator, left, right);
|
|
2081
|
+
}
|
|
2082
|
+
return left;
|
|
2083
|
+
}
|
|
2084
|
+
function parseLogicalAndExpression() {
|
|
2085
|
+
let left = parseLogicalNegationExpression();
|
|
2086
|
+
while (is(TOKEN_TYPES.And)) {
|
|
2087
|
+
const operator = tokens[current];
|
|
2088
|
+
++current;
|
|
2089
|
+
const right = parseLogicalNegationExpression();
|
|
2090
|
+
left = new BinaryExpression(operator, left, right);
|
|
2091
|
+
}
|
|
2092
|
+
return left;
|
|
2093
|
+
}
|
|
2094
|
+
function parseLogicalNegationExpression() {
|
|
2095
|
+
let right;
|
|
2096
|
+
while (is(TOKEN_TYPES.Not)) {
|
|
2097
|
+
const operator = tokens[current];
|
|
2098
|
+
++current;
|
|
2099
|
+
const arg = parseLogicalNegationExpression();
|
|
2100
|
+
right = new UnaryExpression(operator, arg);
|
|
2101
|
+
}
|
|
2102
|
+
return right ?? parseComparisonExpression();
|
|
2103
|
+
}
|
|
2104
|
+
function parseComparisonExpression() {
|
|
2105
|
+
let left = parseAdditiveExpression();
|
|
2106
|
+
while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) {
|
|
2107
|
+
const operator = tokens[current];
|
|
2108
|
+
++current;
|
|
2109
|
+
const right = parseAdditiveExpression();
|
|
2110
|
+
left = new BinaryExpression(operator, left, right);
|
|
2111
|
+
}
|
|
2112
|
+
return left;
|
|
2113
|
+
}
|
|
2114
|
+
function parseAdditiveExpression() {
|
|
2115
|
+
let left = parseMultiplicativeExpression();
|
|
2116
|
+
while (is(TOKEN_TYPES.AdditiveBinaryOperator)) {
|
|
2117
|
+
const operator = tokens[current];
|
|
2118
|
+
++current;
|
|
2119
|
+
const right = parseMultiplicativeExpression();
|
|
2120
|
+
left = new BinaryExpression(operator, left, right);
|
|
2121
|
+
}
|
|
2122
|
+
return left;
|
|
2123
|
+
}
|
|
2124
|
+
function parseCallMemberExpression() {
|
|
2125
|
+
const member = parseMemberExpression();
|
|
2126
|
+
if (is(TOKEN_TYPES.OpenParen)) {
|
|
2127
|
+
return parseCallExpression(member);
|
|
2128
|
+
}
|
|
2129
|
+
return member;
|
|
2130
|
+
}
|
|
2131
|
+
function parseCallExpression(callee) {
|
|
2132
|
+
let callExpression = new CallExpression(callee, parseArgs());
|
|
2133
|
+
if (is(TOKEN_TYPES.OpenParen)) {
|
|
2134
|
+
callExpression = parseCallExpression(callExpression);
|
|
2135
|
+
}
|
|
2136
|
+
return callExpression;
|
|
2137
|
+
}
|
|
2138
|
+
function parseArgs() {
|
|
2139
|
+
expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list");
|
|
2140
|
+
const args = parseArgumentsList();
|
|
2141
|
+
expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list");
|
|
2142
|
+
return args;
|
|
2143
|
+
}
|
|
2144
|
+
function parseArgumentsList() {
|
|
2145
|
+
const args = [];
|
|
2146
|
+
while (!is(TOKEN_TYPES.CloseParen)) {
|
|
2147
|
+
let argument = parseExpression();
|
|
2148
|
+
if (is(TOKEN_TYPES.Equals)) {
|
|
2149
|
+
++current;
|
|
2150
|
+
if (!(argument instanceof Identifier)) {
|
|
2151
|
+
throw new SyntaxError(`Expected identifier for keyword argument`);
|
|
2152
|
+
}
|
|
2153
|
+
const value = parseExpression();
|
|
2154
|
+
argument = new KeywordArgumentExpression(argument, value);
|
|
2155
|
+
}
|
|
2156
|
+
args.push(argument);
|
|
2157
|
+
if (is(TOKEN_TYPES.Comma)) {
|
|
2158
|
+
++current;
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
return args;
|
|
2162
|
+
}
|
|
2163
|
+
function parseMemberExpressionArgumentsList() {
|
|
2164
|
+
const slices = [];
|
|
2165
|
+
let isSlice = false;
|
|
2166
|
+
while (!is(TOKEN_TYPES.CloseSquareBracket)) {
|
|
2167
|
+
if (is(TOKEN_TYPES.Colon)) {
|
|
2168
|
+
slices.push(void 0);
|
|
2169
|
+
++current;
|
|
2170
|
+
isSlice = true;
|
|
2171
|
+
} else {
|
|
2172
|
+
slices.push(parseExpression());
|
|
2173
|
+
if (is(TOKEN_TYPES.Colon)) {
|
|
2174
|
+
++current;
|
|
2175
|
+
isSlice = true;
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
if (slices.length === 0) {
|
|
2180
|
+
throw new SyntaxError(`Expected at least one argument for member/slice expression`);
|
|
2181
|
+
}
|
|
2182
|
+
if (isSlice) {
|
|
2183
|
+
if (slices.length > 3) {
|
|
2184
|
+
throw new SyntaxError(`Expected 0-3 arguments for slice expression`);
|
|
2185
|
+
}
|
|
2186
|
+
return new SliceExpression(...slices);
|
|
2187
|
+
}
|
|
2188
|
+
return slices[0];
|
|
2189
|
+
}
|
|
2190
|
+
function parseMemberExpression() {
|
|
2191
|
+
let object = parsePrimaryExpression();
|
|
2192
|
+
while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) {
|
|
2193
|
+
const operator = tokens[current];
|
|
2194
|
+
++current;
|
|
2195
|
+
let property;
|
|
2196
|
+
const computed = operator.type !== TOKEN_TYPES.Dot;
|
|
2197
|
+
if (computed) {
|
|
2198
|
+
property = parseMemberExpressionArgumentsList();
|
|
2199
|
+
expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket");
|
|
2200
|
+
} else {
|
|
2201
|
+
property = parsePrimaryExpression();
|
|
2202
|
+
if (property.type !== "Identifier") {
|
|
2203
|
+
throw new SyntaxError(`Expected identifier following dot operator`);
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
object = new MemberExpression(object, property, computed);
|
|
2207
|
+
}
|
|
2208
|
+
return object;
|
|
2209
|
+
}
|
|
2210
|
+
function parseMultiplicativeExpression() {
|
|
2211
|
+
let left = parseTestExpression();
|
|
2212
|
+
while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) {
|
|
2213
|
+
const operator = tokens[current];
|
|
2214
|
+
++current;
|
|
2215
|
+
const right = parseTestExpression();
|
|
2216
|
+
left = new BinaryExpression(operator, left, right);
|
|
2217
|
+
}
|
|
2218
|
+
return left;
|
|
2219
|
+
}
|
|
2220
|
+
function parseTestExpression() {
|
|
2221
|
+
let operand = parseFilterExpression();
|
|
2222
|
+
while (is(TOKEN_TYPES.Is)) {
|
|
2223
|
+
++current;
|
|
2224
|
+
const negate = is(TOKEN_TYPES.Not);
|
|
2225
|
+
if (negate) {
|
|
2226
|
+
++current;
|
|
2227
|
+
}
|
|
2228
|
+
let filter = parsePrimaryExpression();
|
|
2229
|
+
if (filter instanceof BooleanLiteral) {
|
|
2230
|
+
filter = new Identifier(filter.value.toString());
|
|
2231
|
+
}
|
|
2232
|
+
if (!(filter instanceof Identifier)) {
|
|
2233
|
+
throw new SyntaxError(`Expected identifier for the test`);
|
|
2234
|
+
}
|
|
2235
|
+
operand = new TestExpression(operand, negate, filter);
|
|
2236
|
+
}
|
|
2237
|
+
return operand;
|
|
2238
|
+
}
|
|
2239
|
+
function parseFilterExpression() {
|
|
2240
|
+
let operand = parseCallMemberExpression();
|
|
2241
|
+
while (is(TOKEN_TYPES.Pipe)) {
|
|
2242
|
+
++current;
|
|
2243
|
+
let filter = parsePrimaryExpression();
|
|
2244
|
+
if (!(filter instanceof Identifier)) {
|
|
2245
|
+
throw new SyntaxError(`Expected identifier for the filter`);
|
|
2246
|
+
}
|
|
2247
|
+
if (is(TOKEN_TYPES.OpenParen)) {
|
|
2248
|
+
filter = parseCallExpression(filter);
|
|
2249
|
+
}
|
|
2250
|
+
operand = new FilterExpression(operand, filter);
|
|
2251
|
+
}
|
|
2252
|
+
return operand;
|
|
2253
|
+
}
|
|
2254
|
+
function parsePrimaryExpression() {
|
|
2255
|
+
const token = tokens[current];
|
|
2256
|
+
switch (token.type) {
|
|
2257
|
+
case TOKEN_TYPES.NumericLiteral:
|
|
2258
|
+
++current;
|
|
2259
|
+
return new NumericLiteral(Number(token.value));
|
|
2260
|
+
case TOKEN_TYPES.StringLiteral:
|
|
2261
|
+
++current;
|
|
2262
|
+
return new StringLiteral(token.value);
|
|
2263
|
+
case TOKEN_TYPES.BooleanLiteral:
|
|
2264
|
+
++current;
|
|
2265
|
+
return new BooleanLiteral(token.value === "true");
|
|
2266
|
+
case TOKEN_TYPES.Identifier:
|
|
2267
|
+
++current;
|
|
2268
|
+
return new Identifier(token.value);
|
|
2269
|
+
case TOKEN_TYPES.OpenParen: {
|
|
2270
|
+
++current;
|
|
2271
|
+
const expression = parseExpressionSequence();
|
|
2272
|
+
if (tokens[current].type !== TOKEN_TYPES.CloseParen) {
|
|
2273
|
+
throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`);
|
|
2274
|
+
}
|
|
2275
|
+
++current;
|
|
2276
|
+
return expression;
|
|
2277
|
+
}
|
|
2278
|
+
case TOKEN_TYPES.OpenSquareBracket: {
|
|
2279
|
+
++current;
|
|
2280
|
+
const values = [];
|
|
2281
|
+
while (!is(TOKEN_TYPES.CloseSquareBracket)) {
|
|
2282
|
+
values.push(parseExpression());
|
|
2283
|
+
if (is(TOKEN_TYPES.Comma)) {
|
|
2284
|
+
++current;
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
++current;
|
|
2288
|
+
return new ArrayLiteral(values);
|
|
2289
|
+
}
|
|
2290
|
+
case TOKEN_TYPES.OpenCurlyBracket: {
|
|
2291
|
+
++current;
|
|
2292
|
+
const values = /* @__PURE__ */ new Map();
|
|
2293
|
+
while (!is(TOKEN_TYPES.CloseCurlyBracket)) {
|
|
2294
|
+
const key = parseExpression();
|
|
2295
|
+
expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal");
|
|
2296
|
+
const value = parseExpression();
|
|
2297
|
+
values.set(key, value);
|
|
2298
|
+
if (is(TOKEN_TYPES.Comma)) {
|
|
2299
|
+
++current;
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
++current;
|
|
2303
|
+
return new ObjectLiteral(values);
|
|
2304
|
+
}
|
|
2305
|
+
default:
|
|
2306
|
+
throw new SyntaxError(`Unexpected token: ${token.type}`);
|
|
2307
|
+
}
|
|
2308
|
+
}
|
|
2309
|
+
while (current < tokens.length) {
|
|
2310
|
+
program.body.push(parseAny());
|
|
2311
|
+
}
|
|
2312
|
+
return program;
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
// src/utils/prompt/template/jinja/src/utils.ts
|
|
2316
|
+
function range(start, stop, step = 1) {
|
|
2317
|
+
if (stop === void 0) {
|
|
2318
|
+
stop = start;
|
|
2319
|
+
start = 0;
|
|
2320
|
+
}
|
|
2321
|
+
const result = [];
|
|
2322
|
+
for (let i = start; i < stop; i += step) {
|
|
2323
|
+
result.push(i);
|
|
2324
|
+
}
|
|
2325
|
+
return result;
|
|
2326
|
+
}
|
|
2327
|
+
function slice(array, start, stop, step = 1) {
|
|
2328
|
+
const direction = Math.sign(step);
|
|
2329
|
+
if (direction >= 0) {
|
|
2330
|
+
start = (start ??= 0) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length);
|
|
2331
|
+
stop = (stop ??= array.length) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length);
|
|
2332
|
+
} else {
|
|
2333
|
+
start = (start ??= array.length - 1) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1);
|
|
2334
|
+
stop = (stop ??= -1) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1);
|
|
2335
|
+
}
|
|
2336
|
+
const result = [];
|
|
2337
|
+
for (let i = start; direction * i < direction * stop; i += step) {
|
|
2338
|
+
result.push(array[i]);
|
|
2339
|
+
}
|
|
2340
|
+
return result;
|
|
2341
|
+
}
|
|
2342
|
+
function titleCase(value) {
|
|
2343
|
+
return value.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
// src/utils/prompt/template/jinja/src/runtime.ts
|
|
2347
|
+
var RuntimeValue = class {
|
|
2348
|
+
/**
|
|
2349
|
+
* Creates a new RuntimeValue.
|
|
2350
|
+
*/
|
|
2351
|
+
constructor(value = void 0) {
|
|
2352
|
+
this.type = "RuntimeValue";
|
|
2353
|
+
/**
|
|
2354
|
+
* A collection of built-in functions for this type.
|
|
2355
|
+
*/
|
|
2356
|
+
this.builtins = /* @__PURE__ */ new Map();
|
|
2357
|
+
this.value = value;
|
|
2358
|
+
}
|
|
2359
|
+
/**
|
|
2360
|
+
* Determines truthiness or falsiness of the runtime value.
|
|
2361
|
+
* This function should be overridden by subclasses if it has custom truthiness criteria.
|
|
2362
|
+
* @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise.
|
|
2363
|
+
*/
|
|
2364
|
+
__bool__() {
|
|
2365
|
+
return new BooleanValue(!!this.value);
|
|
2366
|
+
}
|
|
2367
|
+
toString() {
|
|
2368
|
+
return "" + this.value;
|
|
2369
|
+
}
|
|
2370
|
+
toJSON() {
|
|
2371
|
+
return this.value;
|
|
2372
|
+
}
|
|
2373
|
+
};
|
|
2374
|
+
var NumericValue = class extends RuntimeValue {
|
|
2375
|
+
constructor() {
|
|
2376
|
+
super(...arguments);
|
|
2377
|
+
this.type = "NumericValue";
|
|
2378
|
+
}
|
|
2379
|
+
};
|
|
2380
|
+
var StringValue = class _StringValue extends RuntimeValue {
|
|
2381
|
+
constructor() {
|
|
2382
|
+
super(...arguments);
|
|
2383
|
+
this.type = "StringValue";
|
|
2384
|
+
this.builtins = /* @__PURE__ */ new Map([
|
|
2385
|
+
[
|
|
2386
|
+
"upper",
|
|
2387
|
+
new FunctionValue(() => {
|
|
2388
|
+
return new _StringValue(this.value.toUpperCase());
|
|
2389
|
+
})
|
|
2390
|
+
],
|
|
2391
|
+
[
|
|
2392
|
+
"lower",
|
|
2393
|
+
new FunctionValue(() => {
|
|
2394
|
+
return new _StringValue(this.value.toLowerCase());
|
|
2395
|
+
})
|
|
2396
|
+
],
|
|
2397
|
+
[
|
|
2398
|
+
"strip",
|
|
2399
|
+
new FunctionValue(() => {
|
|
2400
|
+
return new _StringValue(this.value.trim());
|
|
2401
|
+
})
|
|
2402
|
+
],
|
|
2403
|
+
[
|
|
2404
|
+
"title",
|
|
2405
|
+
new FunctionValue(() => {
|
|
2406
|
+
return new _StringValue(titleCase(this.value));
|
|
2407
|
+
})
|
|
2408
|
+
],
|
|
2409
|
+
["length", new NumericValue(this.value.length)],
|
|
2410
|
+
[
|
|
2411
|
+
"rstrip",
|
|
2412
|
+
new FunctionValue(() => {
|
|
2413
|
+
return new _StringValue(this.value.trimEnd());
|
|
2414
|
+
})
|
|
2415
|
+
],
|
|
2416
|
+
[
|
|
2417
|
+
"lstrip",
|
|
2418
|
+
new FunctionValue(() => {
|
|
2419
|
+
return new _StringValue(this.value.trimStart());
|
|
2420
|
+
})
|
|
2421
|
+
]
|
|
2422
|
+
]);
|
|
2423
|
+
}
|
|
2424
|
+
};
|
|
2425
|
+
var BooleanValue = class extends RuntimeValue {
|
|
2426
|
+
constructor() {
|
|
2427
|
+
super(...arguments);
|
|
2428
|
+
this.type = "BooleanValue";
|
|
2429
|
+
}
|
|
2430
|
+
};
|
|
2431
|
+
var ObjectValue = class extends RuntimeValue {
|
|
2432
|
+
constructor(value, orgValue) {
|
|
2433
|
+
super(value);
|
|
2434
|
+
this.orgValue = orgValue;
|
|
2435
|
+
this.type = "ObjectValue";
|
|
2436
|
+
this.builtins = /* @__PURE__ */ new Map([
|
|
2437
|
+
[
|
|
2438
|
+
"get",
|
|
2439
|
+
new FunctionValue(([key, defaultValue]) => {
|
|
2440
|
+
if (!(key instanceof StringValue)) {
|
|
2441
|
+
throw new Error(`Object key must be a string: got ${key.type}`);
|
|
2442
|
+
}
|
|
2443
|
+
return this.value.get(key.value) ?? defaultValue ?? new NullValue();
|
|
2444
|
+
})
|
|
2445
|
+
],
|
|
2446
|
+
[
|
|
2447
|
+
"items",
|
|
2448
|
+
new FunctionValue(() => {
|
|
2449
|
+
return new ArrayValue(
|
|
2450
|
+
Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value]))
|
|
2451
|
+
);
|
|
2452
|
+
})
|
|
2453
|
+
]
|
|
2454
|
+
]);
|
|
2455
|
+
}
|
|
2456
|
+
toString() {
|
|
2457
|
+
let result;
|
|
2458
|
+
if (this.orgValue && this.orgValue.toString !== Object.prototype.toString) {
|
|
2459
|
+
result = this.orgValue.toString();
|
|
2460
|
+
} else {
|
|
2461
|
+
result = JSON.stringify(Object.fromEntries(this.value.entries()));
|
|
2462
|
+
}
|
|
2463
|
+
return result;
|
|
2464
|
+
}
|
|
2465
|
+
toJSON() {
|
|
2466
|
+
if (this.orgValue) {
|
|
2467
|
+
return this.orgValue;
|
|
2468
|
+
}
|
|
2469
|
+
return Object.fromEntries(this.value.entries());
|
|
2470
|
+
}
|
|
2471
|
+
/**
|
|
2472
|
+
* NOTE: necessary to override since all JavaScript arrays are considered truthy,
|
|
2473
|
+
* while only non-empty Python arrays are consider truthy.
|
|
2474
|
+
*
|
|
2475
|
+
* e.g.,
|
|
2476
|
+
* - JavaScript: {} && 5 -> 5
|
|
2477
|
+
* - Python: {} and 5 -> {}
|
|
2478
|
+
*/
|
|
2479
|
+
__bool__() {
|
|
2480
|
+
return new BooleanValue(this.value.size > 0);
|
|
2481
|
+
}
|
|
2482
|
+
};
|
|
2483
|
+
var ArrayValue = class extends RuntimeValue {
|
|
2484
|
+
constructor() {
|
|
2485
|
+
super(...arguments);
|
|
2486
|
+
this.type = "ArrayValue";
|
|
2487
|
+
this.builtins = /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]]);
|
|
2488
|
+
}
|
|
2489
|
+
/**
|
|
2490
|
+
* NOTE: necessary to override since all JavaScript arrays are considered truthy,
|
|
2491
|
+
* while only non-empty Python arrays are consider truthy.
|
|
2492
|
+
*
|
|
2493
|
+
* e.g.,
|
|
2494
|
+
* - JavaScript: [] && 5 -> 5
|
|
2495
|
+
* - Python: [] and 5 -> []
|
|
2496
|
+
*/
|
|
2497
|
+
__bool__() {
|
|
2498
|
+
return new BooleanValue(this.value.length > 0);
|
|
2499
|
+
}
|
|
2500
|
+
};
|
|
2501
|
+
var TupleValue = class extends ArrayValue {
|
|
2502
|
+
constructor() {
|
|
2503
|
+
super(...arguments);
|
|
2504
|
+
this.type = "TupleValue";
|
|
2505
|
+
}
|
|
2506
|
+
};
|
|
2507
|
+
var FunctionValue = class extends RuntimeValue {
|
|
2508
|
+
constructor() {
|
|
2509
|
+
super(...arguments);
|
|
2510
|
+
this.type = "FunctionValue";
|
|
2511
|
+
}
|
|
2512
|
+
};
|
|
2513
|
+
var NullValue = class extends RuntimeValue {
|
|
2514
|
+
constructor() {
|
|
2515
|
+
super(...arguments);
|
|
2516
|
+
this.type = "NullValue";
|
|
2517
|
+
}
|
|
2518
|
+
};
|
|
2519
|
+
var UndefinedValue = class extends RuntimeValue {
|
|
2520
|
+
constructor() {
|
|
2521
|
+
super(...arguments);
|
|
2522
|
+
this.type = "UndefinedValue";
|
|
2523
|
+
}
|
|
2524
|
+
};
|
|
2525
|
+
var Environment = class {
|
|
2526
|
+
constructor(parent) {
|
|
2527
|
+
this.parent = parent;
|
|
2528
|
+
/**
|
|
2529
|
+
* The variables declared in this environment.
|
|
2530
|
+
*/
|
|
2531
|
+
this.variables = /* @__PURE__ */ new Map([
|
|
2532
|
+
[
|
|
2533
|
+
"namespace",
|
|
2534
|
+
new FunctionValue((args) => {
|
|
2535
|
+
if (args.length === 0) {
|
|
2536
|
+
return new ObjectValue(/* @__PURE__ */ new Map());
|
|
2537
|
+
}
|
|
2538
|
+
if (args.length !== 1 || !(args[0] instanceof ObjectValue)) {
|
|
2539
|
+
throw new Error("`namespace` expects either zero arguments or a single object argument");
|
|
2540
|
+
}
|
|
2541
|
+
return args[0];
|
|
2542
|
+
})
|
|
2543
|
+
]
|
|
2544
|
+
]);
|
|
2545
|
+
/**
|
|
2546
|
+
* The tests available in this environment.
|
|
2547
|
+
*/
|
|
2548
|
+
this.tests = /* @__PURE__ */ new Map([
|
|
2549
|
+
["boolean", (operand) => operand.type === "BooleanValue"],
|
|
2550
|
+
["callable", (operand) => operand instanceof FunctionValue],
|
|
2551
|
+
[
|
|
2552
|
+
"odd",
|
|
2553
|
+
(operand) => {
|
|
2554
|
+
if (operand.type !== "NumericValue") {
|
|
2555
|
+
throw new Error(`Cannot apply test "odd" to type: ${operand.type}`);
|
|
2556
|
+
}
|
|
2557
|
+
return operand.value % 2 !== 0;
|
|
2558
|
+
}
|
|
2559
|
+
],
|
|
2560
|
+
[
|
|
2561
|
+
"even",
|
|
2562
|
+
(operand) => {
|
|
2563
|
+
if (operand.type !== "NumericValue") {
|
|
2564
|
+
throw new Error(`Cannot apply test "even" to type: ${operand.type}`);
|
|
2565
|
+
}
|
|
2566
|
+
return operand.value % 2 === 0;
|
|
2567
|
+
}
|
|
2568
|
+
],
|
|
2569
|
+
["false", (operand) => operand.type === "BooleanValue" && !operand.value],
|
|
2570
|
+
["true", (operand) => operand.type === "BooleanValue" && operand.value],
|
|
2571
|
+
["number", (operand) => operand.type === "NumericValue"],
|
|
2572
|
+
["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)],
|
|
2573
|
+
["iterable", (operand) => operand instanceof ArrayValue || operand instanceof StringValue],
|
|
2574
|
+
[
|
|
2575
|
+
"lower",
|
|
2576
|
+
(operand) => {
|
|
2577
|
+
const str = operand.value;
|
|
2578
|
+
return operand.type === "StringValue" && str === str.toLowerCase();
|
|
2579
|
+
}
|
|
2580
|
+
],
|
|
2581
|
+
[
|
|
2582
|
+
"upper",
|
|
2583
|
+
(operand) => {
|
|
2584
|
+
const str = operand.value;
|
|
2585
|
+
return operand.type === "StringValue" && str === str.toUpperCase();
|
|
2586
|
+
}
|
|
2587
|
+
],
|
|
2588
|
+
["none", (operand) => operand.type === "NullValue"],
|
|
2589
|
+
["defined", (operand) => operand.type !== "UndefinedValue"],
|
|
2590
|
+
["undefined", (operand) => operand.type === "UndefinedValue"],
|
|
2591
|
+
["equalto", (a, b) => a.value === b.value]
|
|
2592
|
+
]);
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Set the value of a variable in the current environment.
|
|
2596
|
+
*/
|
|
2597
|
+
set(name, value) {
|
|
2598
|
+
return this.declareVariable(name, convertToRuntimeValues(value));
|
|
2599
|
+
}
|
|
2600
|
+
declareVariable(name, value) {
|
|
2601
|
+
if (this.variables.has(name)) {
|
|
2602
|
+
throw new SyntaxError(`Variable already declared: ${name}`);
|
|
2603
|
+
}
|
|
2604
|
+
this.variables.set(name, value);
|
|
2605
|
+
return value;
|
|
2606
|
+
}
|
|
2607
|
+
// private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue {
|
|
2608
|
+
// const env = this.resolve(name);
|
|
2609
|
+
// env.variables.set(name, value);
|
|
2610
|
+
// return value;
|
|
2611
|
+
// }
|
|
2612
|
+
/**
|
|
2613
|
+
* Set variable in the current scope.
|
|
2614
|
+
* See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information.
|
|
2615
|
+
*/
|
|
2616
|
+
setVariable(name, value) {
|
|
2617
|
+
this.variables.set(name, value);
|
|
2618
|
+
return value;
|
|
2619
|
+
}
|
|
2620
|
+
/**
|
|
2621
|
+
* Resolve the environment in which the variable is declared.
|
|
2622
|
+
* @param {string} name The name of the variable.
|
|
2623
|
+
* @returns {Environment} The environment in which the variable is declared.
|
|
2624
|
+
*/
|
|
2625
|
+
resolve(name) {
|
|
2626
|
+
if (this.variables.has(name)) {
|
|
2627
|
+
return this;
|
|
2628
|
+
}
|
|
2629
|
+
if (this.parent) {
|
|
2630
|
+
return this.parent.resolve(name);
|
|
2631
|
+
}
|
|
2632
|
+
throw new Error(`Unknown variable: ${name}`);
|
|
2633
|
+
}
|
|
2634
|
+
lookupVariable(name) {
|
|
2635
|
+
try {
|
|
2636
|
+
return this.resolve(name).variables.get(name) ?? new UndefinedValue();
|
|
2637
|
+
} catch {
|
|
2638
|
+
return new UndefinedValue();
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2641
|
+
};
|
|
2642
|
+
var Interpreter = class {
|
|
2643
|
+
constructor(env) {
|
|
2644
|
+
this.global = env ?? new Environment();
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* Run the program.
|
|
2648
|
+
*/
|
|
2649
|
+
run(program) {
|
|
2650
|
+
return this.evaluate(program, this.global);
|
|
2651
|
+
}
|
|
2652
|
+
/**
|
|
2653
|
+
* Evaluates expressions following the binary operation type.
|
|
2654
|
+
*/
|
|
2655
|
+
evaluateBinaryExpression(node, environment) {
|
|
2656
|
+
const left = this.evaluate(node.left, environment);
|
|
2657
|
+
switch (node.operator.value) {
|
|
2658
|
+
case "and":
|
|
2659
|
+
return left.__bool__().value ? this.evaluate(node.right, environment) : left;
|
|
2660
|
+
case "or":
|
|
2661
|
+
return left.__bool__().value ? left : this.evaluate(node.right, environment);
|
|
2662
|
+
}
|
|
2663
|
+
const right = this.evaluate(node.right, environment);
|
|
2664
|
+
switch (node.operator.value) {
|
|
2665
|
+
case "==":
|
|
2666
|
+
return new BooleanValue(left.value == right.value);
|
|
2667
|
+
case "!=":
|
|
2668
|
+
return new BooleanValue(left.value != right.value);
|
|
2669
|
+
}
|
|
2670
|
+
if (left instanceof UndefinedValue || right instanceof UndefinedValue) {
|
|
2671
|
+
throw new Error("Cannot perform operation on undefined values");
|
|
2672
|
+
} else if (left instanceof NullValue || right instanceof NullValue) {
|
|
2673
|
+
throw new Error("Cannot perform operation on null values");
|
|
2674
|
+
} else if (left instanceof NumericValue && right instanceof NumericValue) {
|
|
2675
|
+
switch (node.operator.value) {
|
|
2676
|
+
case "+":
|
|
2677
|
+
return new NumericValue(left.value + right.value);
|
|
2678
|
+
case "-":
|
|
2679
|
+
return new NumericValue(left.value - right.value);
|
|
2680
|
+
case "*":
|
|
2681
|
+
return new NumericValue(left.value * right.value);
|
|
2682
|
+
case "/":
|
|
2683
|
+
return new NumericValue(left.value / right.value);
|
|
2684
|
+
case "%":
|
|
2685
|
+
return new NumericValue(left.value % right.value);
|
|
2686
|
+
case "<":
|
|
2687
|
+
return new BooleanValue(left.value < right.value);
|
|
2688
|
+
case ">":
|
|
2689
|
+
return new BooleanValue(left.value > right.value);
|
|
2690
|
+
case ">=":
|
|
2691
|
+
return new BooleanValue(left.value >= right.value);
|
|
2692
|
+
case "<=":
|
|
2693
|
+
return new BooleanValue(left.value <= right.value);
|
|
2694
|
+
}
|
|
2695
|
+
} else if (left instanceof ArrayValue && right instanceof ArrayValue) {
|
|
2696
|
+
switch (node.operator.value) {
|
|
2697
|
+
case "+":
|
|
2698
|
+
return new ArrayValue(left.value.concat(right.value));
|
|
2699
|
+
}
|
|
2700
|
+
} else if (right instanceof ArrayValue) {
|
|
2701
|
+
const member = right.value.find((x) => x.value === left.value) !== void 0;
|
|
2702
|
+
switch (node.operator.value) {
|
|
2703
|
+
case "in":
|
|
2704
|
+
return new BooleanValue(member);
|
|
2705
|
+
case "not in":
|
|
2706
|
+
return new BooleanValue(!member);
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
if (left instanceof StringValue || right instanceof StringValue) {
|
|
2710
|
+
switch (node.operator.value) {
|
|
2711
|
+
case "+":
|
|
2712
|
+
return new StringValue(left.toString() + right.toString());
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
if (left instanceof StringValue && right instanceof StringValue) {
|
|
2716
|
+
switch (node.operator.value) {
|
|
2717
|
+
case "in":
|
|
2718
|
+
return new BooleanValue(right.value.includes(left.value));
|
|
2719
|
+
case "not in":
|
|
2720
|
+
return new BooleanValue(!right.value.includes(left.value));
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
if (left instanceof StringValue && right instanceof ObjectValue) {
|
|
2724
|
+
switch (node.operator.value) {
|
|
2725
|
+
case "in":
|
|
2726
|
+
return new BooleanValue(right.value.has(left.value));
|
|
2727
|
+
case "not in":
|
|
2728
|
+
return new BooleanValue(!right.value.has(left.value));
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`);
|
|
2732
|
+
}
|
|
2733
|
+
/**
|
|
2734
|
+
* Evaluates expressions following the filter operation type.
|
|
2735
|
+
*/
|
|
2736
|
+
evaluateFilterExpression(node, environment) {
|
|
2737
|
+
const operand = this.evaluate(node.operand, environment);
|
|
2738
|
+
if (node.filter.type === "Identifier") {
|
|
2739
|
+
const filter = node.filter;
|
|
2740
|
+
if (operand instanceof ArrayValue) {
|
|
2741
|
+
switch (filter.value) {
|
|
2742
|
+
case "list":
|
|
2743
|
+
return operand;
|
|
2744
|
+
case "first":
|
|
2745
|
+
return operand.value[0];
|
|
2746
|
+
case "last":
|
|
2747
|
+
return operand.value[operand.value.length - 1];
|
|
2748
|
+
case "length":
|
|
2749
|
+
return new NumericValue(operand.value.length);
|
|
2750
|
+
case "reverse":
|
|
2751
|
+
return new ArrayValue(operand.value.reverse());
|
|
2752
|
+
case "sort":
|
|
2753
|
+
return new ArrayValue(
|
|
2754
|
+
operand.value.sort((a, b) => {
|
|
2755
|
+
if (a.type !== b.type) {
|
|
2756
|
+
throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`);
|
|
2757
|
+
}
|
|
2758
|
+
switch (a.type) {
|
|
2759
|
+
case "NumericValue":
|
|
2760
|
+
return a.value - b.value;
|
|
2761
|
+
case "StringValue":
|
|
2762
|
+
return a.value.localeCompare(b.value);
|
|
2763
|
+
default:
|
|
2764
|
+
throw new Error(`Cannot compare type: ${a.type}`);
|
|
2765
|
+
}
|
|
2766
|
+
})
|
|
2767
|
+
);
|
|
2768
|
+
case "tojson":
|
|
2769
|
+
return new StringValue(JSON.stringify(operand.value));
|
|
2770
|
+
default:
|
|
2771
|
+
throw new Error(`Unknown ArrayValue filter: ${filter.value}`);
|
|
2772
|
+
}
|
|
2773
|
+
} else if (operand instanceof StringValue) {
|
|
2774
|
+
switch (filter.value) {
|
|
2775
|
+
case "length":
|
|
2776
|
+
return new NumericValue(operand.value.length);
|
|
2777
|
+
case "upper":
|
|
2778
|
+
return new StringValue(operand.value.toUpperCase());
|
|
2779
|
+
case "lower":
|
|
2780
|
+
return new StringValue(operand.value.toLowerCase());
|
|
2781
|
+
case "title":
|
|
2782
|
+
return new StringValue(titleCase(operand.value));
|
|
2783
|
+
case "capitalize":
|
|
2784
|
+
return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1));
|
|
2785
|
+
case "trim":
|
|
2786
|
+
return new StringValue(operand.value.trim());
|
|
2787
|
+
case "trimStart":
|
|
2788
|
+
return new StringValue(operand.value.trimStart());
|
|
2789
|
+
case "trimEnd":
|
|
2790
|
+
return new StringValue(operand.value.trimEnd());
|
|
2791
|
+
default:
|
|
2792
|
+
throw new Error(`Unknown StringValue filter: ${filter.value}`);
|
|
2793
|
+
}
|
|
2794
|
+
} else if (operand instanceof NumericValue) {
|
|
2795
|
+
switch (filter.value) {
|
|
2796
|
+
case "abs":
|
|
2797
|
+
return new NumericValue(Math.abs(operand.value));
|
|
2798
|
+
default:
|
|
2799
|
+
throw new Error(`Unknown NumericValue filter: ${filter.value}`);
|
|
2800
|
+
}
|
|
2801
|
+
} else if (operand instanceof ObjectValue) {
|
|
2802
|
+
switch (filter.value) {
|
|
2803
|
+
case "items":
|
|
2804
|
+
return new ArrayValue(
|
|
2805
|
+
Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value]))
|
|
2806
|
+
);
|
|
2807
|
+
case "length":
|
|
2808
|
+
return new NumericValue(operand.value.size);
|
|
2809
|
+
case "tojson":
|
|
2810
|
+
return new StringValue(JSON.stringify(operand));
|
|
2811
|
+
case "string":
|
|
2812
|
+
return new StringValue(operand.toString());
|
|
2813
|
+
default:
|
|
2814
|
+
throw new Error(`Unknown ObjectValue filter: ${filter.value}`);
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
2817
|
+
throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`);
|
|
2818
|
+
} else if (node.filter.type === "CallExpression") {
|
|
2819
|
+
const filter = node.filter;
|
|
2820
|
+
if (filter.callee.type !== "Identifier") {
|
|
2821
|
+
throw new Error(`Unknown filter: ${filter.callee.type}`);
|
|
2822
|
+
}
|
|
2823
|
+
const filterName = filter.callee.value;
|
|
2824
|
+
const filterFn = environment.lookupVariable(filterName);
|
|
2825
|
+
if (filterFn instanceof FunctionValue) {
|
|
2826
|
+
return filterFn.value([operand, ...filter.args.map((x) => this.evaluate(x, environment))], environment);
|
|
2827
|
+
}
|
|
2828
|
+
if (operand instanceof ArrayValue) {
|
|
2829
|
+
switch (filterName) {
|
|
2830
|
+
case "selectattr": {
|
|
2831
|
+
if (operand.value.some((x) => !(x instanceof ObjectValue))) {
|
|
2832
|
+
throw new Error("`selectattr` can only be applied to array of objects");
|
|
2833
|
+
}
|
|
2834
|
+
if (filter.args.some((x) => x.type !== "StringLiteral")) {
|
|
2835
|
+
throw new Error("arguments of `selectattr` must be strings");
|
|
2836
|
+
}
|
|
2837
|
+
const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment));
|
|
2838
|
+
let testFunction;
|
|
2839
|
+
if (testName) {
|
|
2840
|
+
const test = environment.tests.get(testName.value);
|
|
2841
|
+
if (!test) {
|
|
2842
|
+
throw new Error(`Unknown test: ${testName.value}`);
|
|
2843
|
+
}
|
|
2844
|
+
testFunction = test;
|
|
2845
|
+
} else {
|
|
2846
|
+
testFunction = (...x) => x[0].__bool__().value;
|
|
2847
|
+
}
|
|
2848
|
+
const filtered = operand.value.filter((item) => {
|
|
2849
|
+
const a = item.value.get(attr.value);
|
|
2850
|
+
if (a) {
|
|
2851
|
+
return testFunction(a, value);
|
|
2852
|
+
}
|
|
2853
|
+
return false;
|
|
2854
|
+
});
|
|
2855
|
+
return new ArrayValue(filtered);
|
|
2856
|
+
}
|
|
2857
|
+
}
|
|
2858
|
+
throw new Error(`Unknown ArrayValue filter: ${filterName}`);
|
|
2859
|
+
} else {
|
|
2860
|
+
throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`);
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
throw new Error(`Unknown filter: ${node.filter.type}`);
|
|
2864
|
+
}
|
|
2865
|
+
/**
|
|
2866
|
+
* Evaluates expressions following the test operation type.
|
|
2867
|
+
*/
|
|
2868
|
+
evaluateTestExpression(node, environment) {
|
|
2869
|
+
const operand = this.evaluate(node.operand, environment);
|
|
2870
|
+
const test = environment.tests.get(node.test.value);
|
|
2871
|
+
if (!test) {
|
|
2872
|
+
throw new Error(`Unknown test: ${node.test.value}`);
|
|
2873
|
+
}
|
|
2874
|
+
const result = test(operand);
|
|
2875
|
+
return new BooleanValue(node.negate ? !result : result);
|
|
2876
|
+
}
|
|
2877
|
+
/**
|
|
2878
|
+
* Evaluates expressions following the unary operation type.
|
|
2879
|
+
*/
|
|
2880
|
+
evaluateUnaryExpression(node, environment) {
|
|
2881
|
+
const argument = this.evaluate(node.argument, environment);
|
|
2882
|
+
switch (node.operator.value) {
|
|
2883
|
+
case "not":
|
|
2884
|
+
return new BooleanValue(!argument.value);
|
|
2885
|
+
default:
|
|
2886
|
+
throw new SyntaxError(`Unknown operator: ${node.operator.value}`);
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
evalProgram(program, environment) {
|
|
2890
|
+
return this.evaluateBlock(program.body, environment);
|
|
2891
|
+
}
|
|
2892
|
+
evaluateBlock(statements, environment) {
|
|
2893
|
+
let result = "";
|
|
2894
|
+
for (const statement of statements) {
|
|
2895
|
+
const lastEvaluated = this.evaluate(statement, environment);
|
|
2896
|
+
if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") {
|
|
2897
|
+
result += lastEvaluated;
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
return new StringValue(result);
|
|
2901
|
+
}
|
|
2902
|
+
evaluateIdentifier(node, environment) {
|
|
2903
|
+
return environment.lookupVariable(node.value);
|
|
2904
|
+
}
|
|
2905
|
+
evaluateCallExpression(expr, environment) {
|
|
2906
|
+
const args = [];
|
|
2907
|
+
const kwargs = /* @__PURE__ */ new Map();
|
|
2908
|
+
for (const argument of expr.args) {
|
|
2909
|
+
if (argument.type === "KeywordArgumentExpression") {
|
|
2910
|
+
const kwarg = argument;
|
|
2911
|
+
kwargs.set(kwarg.key.value, this.evaluate(kwarg.value, environment));
|
|
2912
|
+
} else {
|
|
2913
|
+
args.push(this.evaluate(argument, environment));
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
if (kwargs.size > 0) {
|
|
2917
|
+
args.push(new ObjectValue(kwargs));
|
|
2918
|
+
}
|
|
2919
|
+
const fn = this.evaluate(expr.callee, environment);
|
|
2920
|
+
if (fn.type !== "FunctionValue") {
|
|
2921
|
+
throw new Error(`Cannot call something that is not a function: got ${fn.type}`);
|
|
2922
|
+
}
|
|
2923
|
+
return fn.value(args, environment);
|
|
2924
|
+
}
|
|
2925
|
+
evaluateSliceExpression(object, expr, environment) {
|
|
2926
|
+
if (!(object instanceof ArrayValue || object instanceof StringValue)) {
|
|
2927
|
+
throw new Error("Slice object must be an array or string");
|
|
2928
|
+
}
|
|
2929
|
+
const start = this.evaluate(expr.start, environment);
|
|
2930
|
+
const stop = this.evaluate(expr.stop, environment);
|
|
2931
|
+
const step = this.evaluate(expr.step, environment);
|
|
2932
|
+
if (!(start instanceof NumericValue || start instanceof UndefinedValue)) {
|
|
2933
|
+
throw new Error("Slice start must be numeric or undefined");
|
|
2934
|
+
}
|
|
2935
|
+
if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) {
|
|
2936
|
+
throw new Error("Slice stop must be numeric or undefined");
|
|
2937
|
+
}
|
|
2938
|
+
if (!(step instanceof NumericValue || step instanceof UndefinedValue)) {
|
|
2939
|
+
throw new Error("Slice step must be numeric or undefined");
|
|
2940
|
+
}
|
|
2941
|
+
if (object instanceof ArrayValue) {
|
|
2942
|
+
return new ArrayValue(slice(object.value, start.value, stop.value, step.value));
|
|
2943
|
+
} else {
|
|
2944
|
+
return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join(""));
|
|
2945
|
+
}
|
|
2946
|
+
}
|
|
2947
|
+
evaluateMemberExpression(expr, environment) {
|
|
2948
|
+
const object = this.evaluate(expr.object, environment);
|
|
2949
|
+
let property;
|
|
2950
|
+
if (expr.computed) {
|
|
2951
|
+
if (expr.property.type === "SliceExpression") {
|
|
2952
|
+
return this.evaluateSliceExpression(object, expr.property, environment);
|
|
2953
|
+
} else {
|
|
2954
|
+
property = this.evaluate(expr.property, environment);
|
|
2955
|
+
}
|
|
2956
|
+
} else {
|
|
2957
|
+
property = new StringValue(expr.property.value);
|
|
2958
|
+
}
|
|
2959
|
+
let value;
|
|
2960
|
+
if (object instanceof ObjectValue) {
|
|
2961
|
+
if (!(property instanceof StringValue)) {
|
|
2962
|
+
throw new Error(`Cannot access property with non-string: got ${property.type}`);
|
|
2963
|
+
}
|
|
2964
|
+
value = object.value.get(property.value) ?? object.builtins.get(property.value);
|
|
2965
|
+
} else if (object instanceof ArrayValue || object instanceof StringValue) {
|
|
2966
|
+
if (property instanceof NumericValue) {
|
|
2967
|
+
value = object.value.at(property.value);
|
|
2968
|
+
if (object instanceof StringValue) {
|
|
2969
|
+
value = new StringValue(object.value.at(property.value));
|
|
2970
|
+
}
|
|
2971
|
+
} else if (property instanceof StringValue) {
|
|
2972
|
+
value = object.builtins.get(property.value);
|
|
2973
|
+
} else {
|
|
2974
|
+
throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`);
|
|
2975
|
+
}
|
|
2976
|
+
} else {
|
|
2977
|
+
if (!(property instanceof StringValue)) {
|
|
2978
|
+
throw new Error(`Cannot access property with non-string: got ${property.type}`);
|
|
2979
|
+
}
|
|
2980
|
+
value = object.builtins.get(property.value);
|
|
2981
|
+
}
|
|
2982
|
+
return value instanceof RuntimeValue ? value : new UndefinedValue();
|
|
2983
|
+
}
|
|
2984
|
+
evaluateSet(node, environment) {
|
|
2985
|
+
const rhs = this.evaluate(node.value, environment);
|
|
2986
|
+
if (node.assignee.type === "Identifier") {
|
|
2987
|
+
const variableName = node.assignee.value;
|
|
2988
|
+
environment.setVariable(variableName, rhs);
|
|
2989
|
+
} else if (node.assignee.type === "MemberExpression") {
|
|
2990
|
+
const member = node.assignee;
|
|
2991
|
+
const object = this.evaluate(member.object, environment);
|
|
2992
|
+
if (!(object instanceof ObjectValue)) {
|
|
2993
|
+
throw new Error("Cannot assign to member of non-object");
|
|
2994
|
+
}
|
|
2995
|
+
if (member.property.type !== "Identifier") {
|
|
2996
|
+
throw new Error("Cannot assign to member with non-identifier property");
|
|
2997
|
+
}
|
|
2998
|
+
object.value.set(member.property.value, rhs);
|
|
2999
|
+
} else {
|
|
3000
|
+
throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`);
|
|
3001
|
+
}
|
|
3002
|
+
return new NullValue();
|
|
3003
|
+
}
|
|
3004
|
+
evaluateIf(node, environment) {
|
|
3005
|
+
const test = this.evaluate(node.test, environment);
|
|
3006
|
+
return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment);
|
|
3007
|
+
}
|
|
3008
|
+
evaluateFor(node, environment) {
|
|
3009
|
+
const scope = new Environment(environment);
|
|
3010
|
+
const iterable = this.evaluate(node.iterable, scope);
|
|
3011
|
+
if (!(iterable instanceof ArrayValue)) {
|
|
3012
|
+
throw new Error(`Expected iterable type in for loop: got ${iterable.type}`);
|
|
3013
|
+
}
|
|
3014
|
+
let result = "";
|
|
3015
|
+
for (let i = 0; i < iterable.value.length; ++i) {
|
|
3016
|
+
const loop = /* @__PURE__ */ new Map([
|
|
3017
|
+
["index", new NumericValue(i + 1)],
|
|
3018
|
+
["index0", new NumericValue(i)],
|
|
3019
|
+
["revindex", new NumericValue(iterable.value.length - i)],
|
|
3020
|
+
["revindex0", new NumericValue(iterable.value.length - i - 1)],
|
|
3021
|
+
["first", new BooleanValue(i === 0)],
|
|
3022
|
+
["last", new BooleanValue(i === iterable.value.length - 1)],
|
|
3023
|
+
["length", new NumericValue(iterable.value.length)],
|
|
3024
|
+
["previtem", i > 0 ? iterable.value[i - 1] : new UndefinedValue()],
|
|
3025
|
+
["nextitem", i < iterable.value.length - 1 ? iterable.value[i + 1] : new UndefinedValue()]
|
|
3026
|
+
]);
|
|
3027
|
+
scope.setVariable("loop", new ObjectValue(loop));
|
|
3028
|
+
const current = iterable.value[i];
|
|
3029
|
+
if (node.loopvar.type === "Identifier") {
|
|
3030
|
+
scope.setVariable(node.loopvar.value, current);
|
|
3031
|
+
} else if (node.loopvar.type === "TupleLiteral") {
|
|
3032
|
+
const loopvar = node.loopvar;
|
|
3033
|
+
if (current.type !== "ArrayValue") {
|
|
3034
|
+
throw new Error(`Cannot unpack non-iterable type: ${current.type}`);
|
|
3035
|
+
}
|
|
3036
|
+
const c = current;
|
|
3037
|
+
if (loopvar.value.length !== c.value.length) {
|
|
3038
|
+
throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`);
|
|
3039
|
+
}
|
|
3040
|
+
for (let j = 0; j < loopvar.value.length; ++j) {
|
|
3041
|
+
if (loopvar.value[j].type !== "Identifier") {
|
|
3042
|
+
throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`);
|
|
3043
|
+
}
|
|
3044
|
+
scope.setVariable(loopvar.value[j].value, c.value[j]);
|
|
3045
|
+
}
|
|
3046
|
+
}
|
|
3047
|
+
const evaluated = this.evaluateBlock(node.body, scope);
|
|
3048
|
+
result += evaluated.value;
|
|
3049
|
+
}
|
|
3050
|
+
return new StringValue(result);
|
|
3051
|
+
}
|
|
3052
|
+
evaluate(statement, environment) {
|
|
3053
|
+
if (statement === void 0)
|
|
3054
|
+
return new UndefinedValue();
|
|
3055
|
+
switch (statement.type) {
|
|
3056
|
+
case "Program":
|
|
3057
|
+
return this.evalProgram(statement, environment);
|
|
3058
|
+
case "Set":
|
|
3059
|
+
return this.evaluateSet(statement, environment);
|
|
3060
|
+
case "If":
|
|
3061
|
+
return this.evaluateIf(statement, environment);
|
|
3062
|
+
case "For":
|
|
3063
|
+
return this.evaluateFor(statement, environment);
|
|
3064
|
+
case "NumericLiteral":
|
|
3065
|
+
return new NumericValue(Number(statement.value));
|
|
3066
|
+
case "StringLiteral":
|
|
3067
|
+
return new StringValue(statement.value);
|
|
3068
|
+
case "BooleanLiteral":
|
|
3069
|
+
return new BooleanValue(statement.value);
|
|
3070
|
+
case "ArrayLiteral":
|
|
3071
|
+
return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment)));
|
|
3072
|
+
case "TupleLiteral":
|
|
3073
|
+
return new TupleValue(statement.value.map((x) => this.evaluate(x, environment)));
|
|
3074
|
+
case "ObjectLiteral": {
|
|
3075
|
+
const mapping = /* @__PURE__ */ new Map();
|
|
3076
|
+
for (const [key, value] of statement.value) {
|
|
3077
|
+
const evaluatedKey = this.evaluate(key, environment);
|
|
3078
|
+
if (!(evaluatedKey instanceof StringValue)) {
|
|
3079
|
+
throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`);
|
|
3080
|
+
}
|
|
3081
|
+
mapping.set(evaluatedKey.value, this.evaluate(value, environment));
|
|
3082
|
+
}
|
|
3083
|
+
return new ObjectValue(mapping, statement.value);
|
|
3084
|
+
}
|
|
3085
|
+
case "Identifier":
|
|
3086
|
+
return this.evaluateIdentifier(statement, environment);
|
|
3087
|
+
case "CallExpression":
|
|
3088
|
+
return this.evaluateCallExpression(statement, environment);
|
|
3089
|
+
case "MemberExpression":
|
|
3090
|
+
return this.evaluateMemberExpression(statement, environment);
|
|
3091
|
+
case "UnaryExpression":
|
|
3092
|
+
return this.evaluateUnaryExpression(statement, environment);
|
|
3093
|
+
case "BinaryExpression":
|
|
3094
|
+
return this.evaluateBinaryExpression(statement, environment);
|
|
3095
|
+
case "FilterExpression":
|
|
3096
|
+
return this.evaluateFilterExpression(statement, environment);
|
|
3097
|
+
case "TestExpression":
|
|
3098
|
+
return this.evaluateTestExpression(statement, environment);
|
|
3099
|
+
default:
|
|
3100
|
+
throw new SyntaxError(`Unknown node type: ${statement.type}`);
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
};
|
|
3104
|
+
function convertToRuntimeValues(input) {
|
|
3105
|
+
switch (typeof input) {
|
|
3106
|
+
case "number":
|
|
3107
|
+
return new NumericValue(input);
|
|
3108
|
+
case "string":
|
|
3109
|
+
return new StringValue(input);
|
|
3110
|
+
case "boolean":
|
|
3111
|
+
return new BooleanValue(input);
|
|
3112
|
+
case "object":
|
|
3113
|
+
if (input === null) {
|
|
3114
|
+
return new NullValue();
|
|
3115
|
+
} else if (Array.isArray(input)) {
|
|
3116
|
+
return new ArrayValue(input.map(convertToRuntimeValues));
|
|
3117
|
+
} else {
|
|
3118
|
+
return new ObjectValue(
|
|
3119
|
+
new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])),
|
|
3120
|
+
input
|
|
3121
|
+
);
|
|
3122
|
+
}
|
|
3123
|
+
case "function":
|
|
3124
|
+
return new FunctionValue((args, _scope) => {
|
|
3125
|
+
const _args = args.map((arg) => {
|
|
3126
|
+
return parseRuntimeValue(arg);
|
|
3127
|
+
});
|
|
3128
|
+
const result = input(..._args) ?? null;
|
|
3129
|
+
return convertToRuntimeValues(result);
|
|
3130
|
+
});
|
|
3131
|
+
default:
|
|
3132
|
+
throw new Error(`Cannot convert to runtime value: ${input}`);
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
function parseRuntimeValue(arg) {
|
|
3136
|
+
let result = arg;
|
|
3137
|
+
if (Array.isArray(arg)) {
|
|
3138
|
+
result = arg.map((item) => parseRuntimeValue(item));
|
|
3139
|
+
} else if (arg instanceof Map) {
|
|
3140
|
+
result = {};
|
|
3141
|
+
arg.forEach((item, key) => {
|
|
3142
|
+
result[key] = parseRuntimeValue(item);
|
|
3143
|
+
});
|
|
3144
|
+
} else if (arg.type === "ObjectValue") {
|
|
3145
|
+
if (arg.orgValue) {
|
|
3146
|
+
result = arg.orgValue;
|
|
3147
|
+
} else {
|
|
3148
|
+
result = {};
|
|
3149
|
+
arg.forEach((item, key) => {
|
|
3150
|
+
result[key] = parseRuntimeValue(item);
|
|
3151
|
+
});
|
|
3152
|
+
}
|
|
3153
|
+
} else if (arg.type === "ArrayValue") {
|
|
3154
|
+
result = arg.value.map((item) => parseRuntimeValue(item));
|
|
3155
|
+
} else if (arg.type) {
|
|
3156
|
+
result = arg.value;
|
|
3157
|
+
}
|
|
3158
|
+
return result;
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
// src/utils/prompt/template/jinja/src/builtins.ts
|
|
3162
|
+
function randomInt(to, from = 0) {
|
|
3163
|
+
return Math.floor(Math.random() * (to - from + 1)) + from;
|
|
3164
|
+
}
|
|
3165
|
+
function select(obj, index) {
|
|
3166
|
+
if (Array.isArray(obj) || typeof obj === "string") {
|
|
3167
|
+
if (index === void 0) {
|
|
3168
|
+
index = randomInt(obj.length - 1);
|
|
3169
|
+
} else if (typeof index === "string") {
|
|
3170
|
+
index = parseInt(index);
|
|
3171
|
+
}
|
|
3172
|
+
if (index < 0) {
|
|
3173
|
+
index = obj.length + index;
|
|
3174
|
+
}
|
|
3175
|
+
return obj[index];
|
|
3176
|
+
} else if (obj && typeof obj === "object") {
|
|
3177
|
+
if (index === void 0) {
|
|
3178
|
+
const keys = Object.keys(obj);
|
|
3179
|
+
index = keys[randomInt(keys.length - 1)];
|
|
3180
|
+
}
|
|
3181
|
+
return obj[index];
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
var builtins = {
|
|
3185
|
+
randomInt,
|
|
3186
|
+
select
|
|
3187
|
+
};
|
|
3188
|
+
|
|
3189
|
+
// src/utils/prompt/template/jinja/src/index.ts
|
|
3190
|
+
var EnvironmentEx = class extends Environment {
|
|
3191
|
+
constructor(parent) {
|
|
3192
|
+
super(parent);
|
|
3193
|
+
this.parent = parent;
|
|
3194
|
+
}
|
|
3195
|
+
assign(items) {
|
|
3196
|
+
for (const [key, value] of Object.entries(items)) {
|
|
3197
|
+
this.set(key, value);
|
|
3198
|
+
}
|
|
3199
|
+
}
|
|
3200
|
+
clear() {
|
|
3201
|
+
this.variables.clear();
|
|
3202
|
+
}
|
|
3203
|
+
};
|
|
3204
|
+
var Template = class _Template {
|
|
3205
|
+
static {
|
|
3206
|
+
this.global = new EnvironmentEx();
|
|
3207
|
+
}
|
|
3208
|
+
/**
|
|
3209
|
+
* @param {string} template The template string
|
|
3210
|
+
*/
|
|
3211
|
+
constructor(template, options = {}) {
|
|
3212
|
+
options = {
|
|
3213
|
+
lstrip_blocks: true,
|
|
3214
|
+
trim_blocks: true,
|
|
3215
|
+
...options
|
|
3216
|
+
};
|
|
3217
|
+
const tokens = tokenize(template, options);
|
|
3218
|
+
this.parsed = parse(tokens);
|
|
3219
|
+
}
|
|
3220
|
+
render(items) {
|
|
3221
|
+
const env = new Environment(_Template.global);
|
|
3222
|
+
env.set("false", false);
|
|
3223
|
+
env.set("true", true);
|
|
3224
|
+
env.set("raise_exception", (args) => {
|
|
3225
|
+
throw new Error(args);
|
|
3226
|
+
});
|
|
3227
|
+
env.set("range", range);
|
|
3228
|
+
for (const [key, value] of Object.entries(items)) {
|
|
3229
|
+
env.set(key, value);
|
|
3230
|
+
}
|
|
3231
|
+
const interpreter = new Interpreter(env);
|
|
3232
|
+
const result = interpreter.run(this.parsed);
|
|
3233
|
+
return result.toString();
|
|
3234
|
+
}
|
|
3235
|
+
};
|
|
3236
|
+
Template.global.assign(builtins);
|
|
3237
|
+
|
|
3238
|
+
// src/utils/prompt/hf-prompt-template.ts
|
|
3239
|
+
function getVariable(statement, internalVars) {
|
|
3240
|
+
let result;
|
|
3241
|
+
switch (statement.type) {
|
|
3242
|
+
case "Identifier": {
|
|
3243
|
+
result = statement.value;
|
|
3244
|
+
break;
|
|
3245
|
+
}
|
|
3246
|
+
case "MemberExpression": {
|
|
3247
|
+
result = getVariable(statement.object, internalVars);
|
|
3248
|
+
break;
|
|
3249
|
+
}
|
|
3250
|
+
case "FilterExpression": {
|
|
3251
|
+
result = getVariable(statement.operand, internalVars);
|
|
3252
|
+
break;
|
|
3253
|
+
}
|
|
3254
|
+
case "Set": {
|
|
3255
|
+
if (!internalVars) {
|
|
3256
|
+
internalVars = [];
|
|
3257
|
+
}
|
|
3258
|
+
const assignee = getVariable(statement.assignee);
|
|
3259
|
+
if (assignee) {
|
|
3260
|
+
internalVars.push(assignee);
|
|
3261
|
+
}
|
|
3262
|
+
result = getVariable(statement.value, internalVars);
|
|
3263
|
+
break;
|
|
3264
|
+
}
|
|
3265
|
+
}
|
|
3266
|
+
if (result && internalVars?.includes(result)) {
|
|
3267
|
+
result = void 0;
|
|
3268
|
+
}
|
|
3269
|
+
return result;
|
|
3270
|
+
}
|
|
3271
|
+
function isTemplate(statement) {
|
|
3272
|
+
let result;
|
|
3273
|
+
switch (statement.type) {
|
|
3274
|
+
case "Program": {
|
|
3275
|
+
result = statement.body.some((item) => isTemplate(item));
|
|
3276
|
+
break;
|
|
3277
|
+
}
|
|
3278
|
+
default: {
|
|
3279
|
+
result = statement.isStatement;
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
return result;
|
|
3283
|
+
}
|
|
3284
|
+
function getVariables(statement, internalVars) {
|
|
3285
|
+
let result = [];
|
|
3286
|
+
if (Array.isArray(statement)) {
|
|
3287
|
+
result = statement.map((item) => getVariables(item, internalVars)).filter(Boolean).flat();
|
|
3288
|
+
} else {
|
|
3289
|
+
switch (statement.type) {
|
|
3290
|
+
case "Program": {
|
|
3291
|
+
result = statement.body.map((item) => getVariables(item, internalVars)).filter(Boolean).flat();
|
|
3292
|
+
break;
|
|
3293
|
+
}
|
|
3294
|
+
case "If": {
|
|
3295
|
+
result = [getVariable(statement.test, internalVars), ...getVariables(statement.body, internalVars), ...getVariables(statement.alternate, internalVars)].filter(Boolean);
|
|
3296
|
+
break;
|
|
3297
|
+
}
|
|
3298
|
+
case "BinaryExpression": {
|
|
3299
|
+
result = [getVariable(statement.left, internalVars), getVariable(statement.right, internalVars)].filter(Boolean);
|
|
3300
|
+
break;
|
|
3301
|
+
}
|
|
3302
|
+
case "For": {
|
|
3303
|
+
const loopVar = getVariable(statement.loopvar);
|
|
3304
|
+
if (loopVar) {
|
|
3305
|
+
if (!internalVars) {
|
|
3306
|
+
internalVars = [];
|
|
3307
|
+
}
|
|
3308
|
+
internalVars.push(loopVar);
|
|
3309
|
+
}
|
|
3310
|
+
result = [getVariable(statement.iterable, internalVars), ...getVariables(statement.body, internalVars)].filter(Boolean);
|
|
3311
|
+
break;
|
|
3312
|
+
}
|
|
3313
|
+
case "CallExpression": {
|
|
3314
|
+
result = [getVariable(statement.callee, internalVars), ...getVariables(statement.args, internalVars)].filter(Boolean);
|
|
3315
|
+
break;
|
|
3316
|
+
}
|
|
3317
|
+
default: {
|
|
3318
|
+
result = [getVariable(statement, internalVars)].filter(Boolean);
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
}
|
|
3322
|
+
return result;
|
|
3323
|
+
}
|
|
3324
|
+
var HfPromptTemplate = class extends PromptTemplate {
|
|
3325
|
+
static isTemplate(templateOpt) {
|
|
3326
|
+
let compiledTemplate;
|
|
3327
|
+
let template;
|
|
3328
|
+
let result = false;
|
|
3329
|
+
if (typeof templateOpt === "object") {
|
|
3330
|
+
template = templateOpt.template;
|
|
3331
|
+
compiledTemplate = templateOpt.compiledTemplate;
|
|
3332
|
+
} else {
|
|
3333
|
+
template = templateOpt;
|
|
3334
|
+
}
|
|
3335
|
+
if (!compiledTemplate && template) {
|
|
3336
|
+
try {
|
|
3337
|
+
compiledTemplate = new Template(template);
|
|
3338
|
+
} catch (error) {
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3341
|
+
if (compiledTemplate) {
|
|
3342
|
+
result = isTemplate(compiledTemplate.parsed);
|
|
3343
|
+
}
|
|
3344
|
+
return result;
|
|
3345
|
+
}
|
|
3346
|
+
getVariables(template = this.compiledTemplate) {
|
|
3347
|
+
const internalVars = [];
|
|
3348
|
+
const result = getVariables(template.parsed, internalVars).filter((item, index, self) => self.indexOf(item) === index);
|
|
3349
|
+
return result;
|
|
3350
|
+
}
|
|
3351
|
+
_initialize(options) {
|
|
3352
|
+
const template = options?.template;
|
|
3353
|
+
if (typeof template !== "string") {
|
|
3354
|
+
throw new CommonError("Prompt template must be a string", "PromptTemplate", 400 /* InvalidArgument */);
|
|
3355
|
+
}
|
|
3356
|
+
this.compiledTemplate = new Template(template);
|
|
3357
|
+
this.inputVariables = Array.isArray(options?.inputVariables) ? options.inputVariables : this.getVariables();
|
|
3358
|
+
}
|
|
3359
|
+
_format(data) {
|
|
3360
|
+
return this.compiledTemplate.render(data);
|
|
3361
|
+
}
|
|
3362
|
+
};
|
|
3363
|
+
PromptTemplate.register(HfPromptTemplate, { name: "hf", aliases: ["huggingface", "internal", "default"] });
|
|
3364
|
+
function createHfValueFunc(fn) {
|
|
3365
|
+
return function(_data) {
|
|
3366
|
+
return fn;
|
|
3367
|
+
};
|
|
3368
|
+
}
|
|
3369
|
+
|
|
3370
|
+
// src/utils/prompt/template/python.ts
|
|
3371
|
+
function parseFString(template) {
|
|
3372
|
+
const chars = template.split("");
|
|
3373
|
+
const nodes = [];
|
|
3374
|
+
const nextBracket = (bracket, start) => {
|
|
3375
|
+
for (let i2 = start; i2 < chars.length; i2 += 1) {
|
|
3376
|
+
if (bracket.includes(chars[i2])) {
|
|
3377
|
+
return i2;
|
|
3378
|
+
}
|
|
3379
|
+
}
|
|
3380
|
+
return -1;
|
|
3381
|
+
};
|
|
3382
|
+
let i = 0;
|
|
3383
|
+
while (i < chars.length) {
|
|
3384
|
+
if (chars[i] === "{" && i + 1 < chars.length && chars[i + 1] === "{") {
|
|
3385
|
+
nodes.push({ type: "literal", text: "{" });
|
|
3386
|
+
i += 2;
|
|
3387
|
+
} else if (chars[i] === "}" && i + 1 < chars.length && chars[i + 1] === "}") {
|
|
3388
|
+
nodes.push({ type: "literal", text: "}" });
|
|
3389
|
+
i += 2;
|
|
3390
|
+
} else if (chars[i] === "{") {
|
|
3391
|
+
const j = nextBracket("}", i);
|
|
3392
|
+
if (j < 0) {
|
|
3393
|
+
throw new Error("Unclosed '{' in template.");
|
|
3394
|
+
}
|
|
3395
|
+
nodes.push({
|
|
3396
|
+
type: "variable",
|
|
3397
|
+
name: chars.slice(i + 1, j).join("")
|
|
3398
|
+
});
|
|
3399
|
+
i = j + 1;
|
|
3400
|
+
} else if (chars[i] === "}") {
|
|
3401
|
+
throw new Error("Single '}' in template.");
|
|
3402
|
+
} else {
|
|
3403
|
+
const next = nextBracket("{}", i);
|
|
3404
|
+
const text = (next < 0 ? chars.slice(i) : chars.slice(i, next)).join("");
|
|
3405
|
+
nodes.push({ type: "literal", text });
|
|
3406
|
+
i = next < 0 ? chars.length : next;
|
|
3407
|
+
}
|
|
3408
|
+
}
|
|
3409
|
+
return nodes;
|
|
3410
|
+
}
|
|
3411
|
+
function interpolateFString(nodes, values) {
|
|
3412
|
+
return nodes.reduce((res, node) => {
|
|
3413
|
+
let result;
|
|
3414
|
+
if (node.type === "variable") {
|
|
3415
|
+
if (node.name in values) {
|
|
3416
|
+
result = res + values[node.name];
|
|
3417
|
+
} else {
|
|
3418
|
+
result = res;
|
|
3419
|
+
}
|
|
3420
|
+
} else {
|
|
3421
|
+
result = res + node.text;
|
|
3422
|
+
}
|
|
3423
|
+
return result;
|
|
3424
|
+
}, "");
|
|
3425
|
+
}
|
|
3426
|
+
|
|
3427
|
+
// src/utils/prompt/f-string-prompt-template.ts
|
|
3428
|
+
function getVariables2(template) {
|
|
3429
|
+
const result = /* @__PURE__ */ new Set();
|
|
3430
|
+
template.forEach((node) => {
|
|
3431
|
+
if (node.type === "variable") {
|
|
3432
|
+
result.add(node.name);
|
|
3433
|
+
}
|
|
3434
|
+
});
|
|
3435
|
+
return [...result];
|
|
3436
|
+
}
|
|
3437
|
+
var FStringPromptTemplate = class extends PromptTemplate {
|
|
3438
|
+
static isTemplate(templateOpt) {
|
|
3439
|
+
let compiledTemplate;
|
|
3440
|
+
let template;
|
|
3441
|
+
let result = false;
|
|
3442
|
+
if (typeof templateOpt === "object") {
|
|
3443
|
+
template = templateOpt.template;
|
|
3444
|
+
compiledTemplate = templateOpt.compiledTemplate;
|
|
3445
|
+
} else {
|
|
3446
|
+
template = templateOpt;
|
|
3447
|
+
}
|
|
3448
|
+
if (!compiledTemplate && template) {
|
|
3449
|
+
try {
|
|
3450
|
+
compiledTemplate = parseFString(template);
|
|
3451
|
+
} catch (_err) {
|
|
3452
|
+
}
|
|
3453
|
+
}
|
|
3454
|
+
if (compiledTemplate) {
|
|
3455
|
+
const vars = getVariables2(compiledTemplate);
|
|
3456
|
+
result = vars.length > 0;
|
|
3457
|
+
}
|
|
3458
|
+
return result;
|
|
3459
|
+
}
|
|
3460
|
+
getVariables(template = this.compiledTemplate) {
|
|
3461
|
+
return getVariables2(template);
|
|
3462
|
+
}
|
|
3463
|
+
_initialize(options) {
|
|
3464
|
+
const template = options?.template;
|
|
3465
|
+
if (typeof template !== "string") {
|
|
3466
|
+
throw new CommonError("Prompt template must be a string", "PromptTemplate", 400 /* InvalidArgument */);
|
|
3467
|
+
}
|
|
3468
|
+
this.compiledTemplate = parseFString(template);
|
|
3469
|
+
this.inputVariables = Array.isArray(options?.inputVariables) ? options.inputVariables : this.getVariables();
|
|
3470
|
+
}
|
|
3471
|
+
_format(data) {
|
|
3472
|
+
return interpolateFString(this.compiledTemplate, data);
|
|
3473
|
+
}
|
|
3474
|
+
};
|
|
3475
|
+
PromptTemplate.register(FStringPromptTemplate, { name: "fstring", aliases: ["python", "f-string", "langchain"] });
|
|
3476
|
+
|
|
3477
|
+
// src/utils/prompt/template/util.ts
|
|
3478
|
+
function getValueByPath(object, path4) {
|
|
3479
|
+
if (typeof path4 === "string") {
|
|
3480
|
+
path4 = path4.split(".").filter((key) => key.length);
|
|
3481
|
+
}
|
|
3482
|
+
return path4.reduce((dive, key) => dive?.[key], object);
|
|
3483
|
+
}
|
|
3484
|
+
|
|
3485
|
+
// src/utils/prompt/template/golang.ts
|
|
3486
|
+
function reReplace(template, variables, initVars) {
|
|
3487
|
+
const regex = /{{\s*re_replace\s+\.(.+?)\s+(["'])([^\2]*?)\2\s+(["'])([^\4]*?)\4\s*}}/g;
|
|
3488
|
+
return template.replace(regex, (match, prop, _regDelimiter, regexp2, _newValueDelimiter, newValue) => {
|
|
3489
|
+
if (initVars) {
|
|
3490
|
+
variables[prop] = null;
|
|
3491
|
+
return match;
|
|
3492
|
+
}
|
|
3493
|
+
const replaceRegex = new RegExp(regexp2, "g");
|
|
3494
|
+
const input = getValueByPath(variables, prop);
|
|
3495
|
+
const expanded = input.replace(replaceRegex, newValue);
|
|
3496
|
+
return expanded;
|
|
3497
|
+
});
|
|
3498
|
+
}
|
|
3499
|
+
function joinReplace(template, variables, initVars) {
|
|
3500
|
+
const regex = /{{\s*join\s+\.(.+?)\s+(["'])([^\2]*?)\2\s*}}/g;
|
|
3501
|
+
return template.replace(regex, (match, prop, _quote, delimiter) => {
|
|
3502
|
+
if (initVars) {
|
|
3503
|
+
variables[prop] = null;
|
|
3504
|
+
return match;
|
|
3505
|
+
}
|
|
3506
|
+
const input = getValueByPath(variables, prop);
|
|
3507
|
+
const expanded = input.join(delimiter);
|
|
3508
|
+
return expanded;
|
|
3509
|
+
});
|
|
3510
|
+
}
|
|
3511
|
+
function ifElseReplace(template, variables, initVars) {
|
|
3512
|
+
const regex = /{{\s*if\s*(\S+?)\s*}}([^{]*)({{\s*else\s*}}([^{]*))?{{\s*end\s*}}/g;
|
|
3513
|
+
return template.replace(regex, (match, condition, onTrue, _elseMarker, onFalse) => {
|
|
3514
|
+
if (initVars) {
|
|
3515
|
+
if (condition.startsWith(".")) {
|
|
3516
|
+
condition = condition.substring(1);
|
|
3517
|
+
}
|
|
3518
|
+
variables[condition] = null;
|
|
3519
|
+
return match;
|
|
3520
|
+
}
|
|
3521
|
+
let conditionResult;
|
|
3522
|
+
if (condition.startsWith(".")) {
|
|
3523
|
+
const value = getValueByPath(variables, condition.substring(1));
|
|
3524
|
+
if (value === null || value === void 0) {
|
|
3525
|
+
conditionResult = onFalse ?? "";
|
|
3526
|
+
} else if (typeof value === "string") {
|
|
3527
|
+
conditionResult = value.length > 0 ? onTrue : onFalse ?? "";
|
|
3528
|
+
} else if (Array.isArray(value)) {
|
|
3529
|
+
conditionResult = value.length > 0 ? onTrue : onFalse ?? "";
|
|
3530
|
+
} else if (typeof value === "boolean") {
|
|
3531
|
+
conditionResult = value ? onTrue : onFalse ?? "";
|
|
3532
|
+
} else {
|
|
3533
|
+
throw new Error(`Unexpected type for variable ${condition}: ${typeof value}`);
|
|
3534
|
+
}
|
|
3535
|
+
} else {
|
|
3536
|
+
throw new Error("Functionality not implemented");
|
|
3537
|
+
}
|
|
3538
|
+
return conditionResult;
|
|
3539
|
+
});
|
|
3540
|
+
}
|
|
3541
|
+
function rangeReplace(template, variables, initVars) {
|
|
3542
|
+
const regex = /{{\s*range\s*[.$]([^{}\s]+?)\s*}}([^{]*?){{\.}}([^{]*?){{\s*end\s*}}/g;
|
|
3543
|
+
return template.replace(regex, (match, prop, prefix, postfix) => {
|
|
3544
|
+
if (initVars) {
|
|
3545
|
+
variables[prop] = null;
|
|
3546
|
+
return match;
|
|
3547
|
+
}
|
|
3548
|
+
const arr = getValueByPath(variables, prop);
|
|
3549
|
+
if (Array.isArray(arr)) {
|
|
3550
|
+
let expanded = "";
|
|
3551
|
+
for (const value of arr) {
|
|
3552
|
+
expanded += `${prefix}${value}${postfix}`;
|
|
3553
|
+
}
|
|
3554
|
+
return expanded;
|
|
3555
|
+
}
|
|
3556
|
+
return match;
|
|
3557
|
+
});
|
|
3558
|
+
}
|
|
3559
|
+
function variableReplace(template, variables, initVars) {
|
|
3560
|
+
const regex = /{{\s*\.([^{}\s]+?)\s*}}/g;
|
|
3561
|
+
return template.replace(regex, (match, key) => {
|
|
3562
|
+
if (initVars) {
|
|
3563
|
+
variables[key] = null;
|
|
3564
|
+
return match;
|
|
3565
|
+
}
|
|
3566
|
+
return getValueByPath(variables, key) || "";
|
|
3567
|
+
});
|
|
3568
|
+
}
|
|
3569
|
+
function indexReplace(template, variables, initVars) {
|
|
3570
|
+
const regex = /{{\s*index\s*\.(.+?)\s+(.+?)\s*}}/g;
|
|
3571
|
+
return template.replace(regex, (match, prop, index) => {
|
|
3572
|
+
if (initVars) {
|
|
3573
|
+
variables[prop] = null;
|
|
3574
|
+
return match;
|
|
3575
|
+
}
|
|
3576
|
+
const top = getValueByPath(variables, prop);
|
|
3577
|
+
let value;
|
|
3578
|
+
if (!isNaN(index)) {
|
|
3579
|
+
value = top[parseInt(index, 10)];
|
|
3580
|
+
} else {
|
|
3581
|
+
const quotedIndex = index.substring(1, index.length - 1);
|
|
3582
|
+
value = top[quotedIndex];
|
|
3583
|
+
}
|
|
3584
|
+
return value || "";
|
|
3585
|
+
});
|
|
3586
|
+
}
|
|
3587
|
+
function interpolateGolangTemplate(str, variables, initVars) {
|
|
3588
|
+
let result = reReplace(str, variables, initVars);
|
|
3589
|
+
result = joinReplace(result, variables, initVars);
|
|
3590
|
+
result = ifElseReplace(result, variables, initVars);
|
|
3591
|
+
result = rangeReplace(result, variables, initVars);
|
|
3592
|
+
result = variableReplace(result, variables, initVars);
|
|
3593
|
+
result = indexReplace(result, variables, initVars);
|
|
3594
|
+
return result;
|
|
3595
|
+
}
|
|
3596
|
+
|
|
3597
|
+
// src/utils/prompt/golang-prompt-template.ts
|
|
3598
|
+
var GolangPromptTemplate = class extends PromptTemplate {
|
|
3599
|
+
static isTemplate(templateOpt) {
|
|
3600
|
+
const template = typeof templateOpt === "string" ? templateOpt : templateOpt.template;
|
|
3601
|
+
return template?.includes("{{") && template.includes("}}");
|
|
3602
|
+
}
|
|
3603
|
+
getVariables(template) {
|
|
3604
|
+
const result = {};
|
|
3605
|
+
interpolateGolangTemplate(template, result, true);
|
|
3606
|
+
return [...Object.keys(result)];
|
|
3607
|
+
}
|
|
3608
|
+
_initialize(options) {
|
|
3609
|
+
const template = options?.template;
|
|
3610
|
+
if (typeof template !== "string") {
|
|
3611
|
+
throw new CommonError("Prompt template must be a string", "PromptTemplate", 400 /* InvalidArgument */);
|
|
3612
|
+
}
|
|
3613
|
+
this.inputVariables = Array.isArray(options?.inputVariables) ? options.inputVariables : this.getVariables(template);
|
|
3614
|
+
}
|
|
3615
|
+
_format(data) {
|
|
3616
|
+
return interpolateGolangTemplate(this.template, data);
|
|
3617
|
+
}
|
|
3618
|
+
};
|
|
3619
|
+
PromptTemplate.register(GolangPromptTemplate, { name: "golang", aliases: ["localai", "ollama"] });
|
|
3620
|
+
|
|
3621
|
+
// src/utils/prompt/few-shot-prompt-template.ts
|
|
3622
|
+
function sortedValues(values) {
|
|
3623
|
+
return Object.keys(values).sort().map((key) => values[key]);
|
|
3624
|
+
}
|
|
3625
|
+
var FewShotPromptTemplate = class extends PromptTemplate {
|
|
3626
|
+
static from(options) {
|
|
3627
|
+
return new this(options);
|
|
3628
|
+
}
|
|
3629
|
+
constructor(options) {
|
|
3630
|
+
super(options);
|
|
3631
|
+
}
|
|
3632
|
+
_initialize(options) {
|
|
3633
|
+
if (options) {
|
|
3634
|
+
if (options.suffix) {
|
|
3635
|
+
this.suffix = options.suffix;
|
|
3636
|
+
}
|
|
3637
|
+
if (options.exampleSeparator) {
|
|
3638
|
+
this.exampleSeparator = options.exampleSeparator;
|
|
3639
|
+
}
|
|
3640
|
+
if (options.prefix) {
|
|
3641
|
+
this.prefix = options.prefix;
|
|
3642
|
+
}
|
|
3643
|
+
if (options.examplePrompt) {
|
|
3644
|
+
this.examplePrompt = options.examplePrompt instanceof PromptTemplate ? options.examplePrompt : PromptTemplate.from(options.examplePrompt);
|
|
3645
|
+
}
|
|
3646
|
+
if (options.examples) {
|
|
3647
|
+
this.examples = options.examples;
|
|
3648
|
+
}
|
|
3649
|
+
}
|
|
3650
|
+
if (!this.prefix) {
|
|
3651
|
+
this.prefix = "";
|
|
3652
|
+
}
|
|
3653
|
+
if (!this.exampleSeparator) {
|
|
3654
|
+
this.exampleSeparator = "\n\n";
|
|
3655
|
+
}
|
|
3656
|
+
if (!this.suffix) {
|
|
3657
|
+
this.suffix = "";
|
|
3658
|
+
}
|
|
3659
|
+
}
|
|
3660
|
+
async _format(data) {
|
|
3661
|
+
const exampleStrings = [];
|
|
3662
|
+
for await (const example of this.examples) {
|
|
3663
|
+
if (this.examplePrompt) {
|
|
3664
|
+
const ex = await this.examplePrompt.format(example);
|
|
3665
|
+
exampleStrings.push(ex);
|
|
3666
|
+
} else {
|
|
3667
|
+
if (typeof example === "string") {
|
|
3668
|
+
exampleStrings.push(example);
|
|
3669
|
+
} else if (example) {
|
|
3670
|
+
const ex = sortedValues(example);
|
|
3671
|
+
if (ex.length > 0) {
|
|
3672
|
+
exampleStrings.push(...ex);
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
3676
|
+
}
|
|
3677
|
+
const template = [this.prefix, ...exampleStrings, this.suffix].join(
|
|
3678
|
+
this.exampleSeparator
|
|
3679
|
+
);
|
|
3680
|
+
const templateFormat = this.templateFormat;
|
|
3681
|
+
const result = await PromptTemplate.format({ template, data, templateFormat });
|
|
3682
|
+
return result;
|
|
3683
|
+
}
|
|
3684
|
+
toJSON(options = this) {
|
|
3685
|
+
const result = super.toJSON(options);
|
|
3686
|
+
if (options.suffix) {
|
|
3687
|
+
result.suffix = options.suffix;
|
|
3688
|
+
}
|
|
3689
|
+
if (options.exampleSeparator) {
|
|
3690
|
+
result.exampleSeparator = options.exampleSeparator;
|
|
3691
|
+
}
|
|
3692
|
+
if (options.prefix) {
|
|
3693
|
+
result.prefix = options.prefix;
|
|
3694
|
+
}
|
|
3695
|
+
if (options.examplePrompt) {
|
|
3696
|
+
result.examplePrompt = options.examplePrompt;
|
|
3697
|
+
}
|
|
3698
|
+
if (options.examples) {
|
|
3699
|
+
result.examples = options.examples;
|
|
3700
|
+
}
|
|
3701
|
+
return result;
|
|
3702
|
+
}
|
|
3703
|
+
};
|
|
3704
|
+
PromptTemplate.register(FewShotPromptTemplate, { name: "fewshot", aliases: ["few_shot"] });
|
|
3705
|
+
|
|
3706
|
+
// src/utils/prompt/template/env.ts
|
|
3707
|
+
var DOTENV_SUBSTITUTION_REGEX = /(\\)?(\$)(?!\()(\{?)([\w.]+)(?::?-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))?(\}?)/gi;
|
|
3708
|
+
function _resolveEscapeSequences(value) {
|
|
3709
|
+
return value.replace(/\\\$/g, "$");
|
|
3710
|
+
}
|
|
3711
|
+
function interpolateEnv(value, processEnv, parsed) {
|
|
3712
|
+
return value.replace(DOTENV_SUBSTITUTION_REGEX, (match, escaped, dollarSign, openBrace, key, defaultValue, closeBrace) => {
|
|
3713
|
+
if (escaped === "\\") {
|
|
3714
|
+
return match.slice(1);
|
|
3715
|
+
} else {
|
|
3716
|
+
if (processEnv[key]) {
|
|
3717
|
+
if (processEnv[key] === parsed[key]) {
|
|
3718
|
+
return processEnv[key];
|
|
3719
|
+
} else {
|
|
3720
|
+
return interpolateEnv(processEnv[key], processEnv, parsed);
|
|
3721
|
+
}
|
|
3722
|
+
}
|
|
3723
|
+
if (parsed[key]) {
|
|
3724
|
+
if (parsed[key] === value) {
|
|
3725
|
+
return parsed[key];
|
|
3726
|
+
} else {
|
|
3727
|
+
return interpolateEnv(parsed[key], processEnv, parsed);
|
|
3728
|
+
}
|
|
3729
|
+
}
|
|
3730
|
+
if (defaultValue) {
|
|
3731
|
+
if (defaultValue.startsWith("$")) {
|
|
3732
|
+
return interpolateEnv(defaultValue, processEnv, parsed);
|
|
3733
|
+
} else {
|
|
3734
|
+
return defaultValue;
|
|
3735
|
+
}
|
|
3736
|
+
}
|
|
3737
|
+
return "";
|
|
3738
|
+
}
|
|
3739
|
+
});
|
|
3740
|
+
}
|
|
3741
|
+
function expandEnv(options) {
|
|
3742
|
+
let processEnv = process.env;
|
|
3743
|
+
if (options && options.processEnv != null) {
|
|
3744
|
+
processEnv = options.processEnv;
|
|
3745
|
+
}
|
|
3746
|
+
for (const key in options.parsed) {
|
|
3747
|
+
let value = options.parsed[key];
|
|
3748
|
+
if (!value) {
|
|
3749
|
+
continue;
|
|
3750
|
+
}
|
|
3751
|
+
const inProcessEnv = Object.prototype.hasOwnProperty.call(processEnv, key);
|
|
3752
|
+
if (inProcessEnv) {
|
|
3753
|
+
if (processEnv[key] === options.parsed[key]) {
|
|
3754
|
+
value = interpolateEnv(value, processEnv, options.parsed);
|
|
3755
|
+
} else {
|
|
3756
|
+
value = processEnv[key];
|
|
3757
|
+
}
|
|
3758
|
+
} else {
|
|
3759
|
+
value = interpolateEnv(value, processEnv, options.parsed);
|
|
3760
|
+
}
|
|
3761
|
+
options.parsed[key] = value ? _resolveEscapeSequences(value) : value;
|
|
3762
|
+
}
|
|
3763
|
+
for (const processKey in options.parsed) {
|
|
3764
|
+
processEnv[processKey] = options.parsed[processKey];
|
|
3765
|
+
}
|
|
3766
|
+
return options;
|
|
3767
|
+
}
|
|
3768
|
+
function expandObjEnv(obj, options = {}, parsedObjs) {
|
|
3769
|
+
if (!parsedObjs) {
|
|
3770
|
+
parsedObjs = /* @__PURE__ */ new WeakSet();
|
|
3771
|
+
}
|
|
3772
|
+
if (!options.processEnv) {
|
|
3773
|
+
options.processEnv = { ...process.env };
|
|
3774
|
+
}
|
|
3775
|
+
if (!options.parsed) {
|
|
3776
|
+
options.parsed = options.processEnv;
|
|
3777
|
+
}
|
|
3778
|
+
switch (typeof obj) {
|
|
3779
|
+
case "string": {
|
|
3780
|
+
const processEnv = options.processEnv;
|
|
3781
|
+
obj = interpolateEnv(obj, processEnv, options.parsed);
|
|
3782
|
+
break;
|
|
3783
|
+
}
|
|
3784
|
+
case "object": {
|
|
3785
|
+
if (parsedObjs.has(obj)) {
|
|
3786
|
+
return obj;
|
|
3787
|
+
}
|
|
3788
|
+
parsedObjs.add(obj);
|
|
3789
|
+
if (Array.isArray(obj)) {
|
|
3790
|
+
for (let i = 0; i < obj.length; i++) {
|
|
3791
|
+
const value = obj[i];
|
|
3792
|
+
if (typeof value === "string" || typeof value === "object") {
|
|
3793
|
+
obj[i] = expandObjEnv(obj[i], options, parsedObjs);
|
|
3794
|
+
}
|
|
3795
|
+
}
|
|
3796
|
+
} else {
|
|
3797
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
3798
|
+
if (typeof value === "string" || typeof value === "object") {
|
|
3799
|
+
obj[key] = expandObjEnv(value, options, parsedObjs);
|
|
3800
|
+
}
|
|
3801
|
+
}
|
|
3802
|
+
}
|
|
3803
|
+
break;
|
|
3804
|
+
}
|
|
3805
|
+
}
|
|
3806
|
+
return obj;
|
|
3807
|
+
}
|
|
3808
|
+
|
|
3809
|
+
// src/utils/wait.ts
|
|
3810
|
+
async function wait(ms) {
|
|
3811
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3812
|
+
}
|
|
3813
|
+
|
|
3814
|
+
// src/utils/stream/error-readable-stream.ts
|
|
3815
|
+
var RStreamErrCode = 600;
|
|
3816
|
+
var ResponseRStreamErrCode = 601;
|
|
3817
|
+
var ReadableStreamError = class extends BaseError {
|
|
3818
|
+
constructor(msg, code = RStreamErrCode) {
|
|
3819
|
+
super(msg, 500);
|
|
3820
|
+
}
|
|
3821
|
+
};
|
|
3822
|
+
function getResponseErrorReadableStream(body) {
|
|
3823
|
+
if (body) {
|
|
3824
|
+
const reader = body.getReader();
|
|
3825
|
+
return new ReadableStream({
|
|
3826
|
+
async start(controller) {
|
|
3827
|
+
const { done, value } = await reader.read();
|
|
3828
|
+
if (!done) {
|
|
3829
|
+
const errorText = new TextDecoder().decode(value);
|
|
3830
|
+
controller.error(new ReadableStreamError(`Response error: ${errorText}`, ResponseRStreamErrCode));
|
|
3831
|
+
}
|
|
3832
|
+
}
|
|
3833
|
+
});
|
|
3834
|
+
} else {
|
|
3835
|
+
return new ReadableStream({
|
|
3836
|
+
start(controller) {
|
|
3837
|
+
controller.error(new ReadableStreamError("Response error: No response body", ResponseRStreamErrCode));
|
|
3838
|
+
}
|
|
3839
|
+
});
|
|
3840
|
+
}
|
|
3841
|
+
}
|
|
3842
|
+
|
|
3843
|
+
// src/utils/stream/create-empty-stream.ts
|
|
3844
|
+
function createEmptyReadableStream() {
|
|
3845
|
+
return new ReadableStream({
|
|
3846
|
+
start(controller) {
|
|
3847
|
+
controller.close();
|
|
3848
|
+
}
|
|
3849
|
+
});
|
|
3850
|
+
}
|
|
3851
|
+
|
|
3852
|
+
// src/utils/stream/ai-stream.ts
|
|
3853
|
+
var import_eventsource_parser = require("eventsource-parser");
|
|
3854
|
+
function createEventStreamTransformer(customParser) {
|
|
3855
|
+
const textDecoder = new TextDecoder();
|
|
3856
|
+
let eventSourceParser;
|
|
3857
|
+
return new TransformStream({
|
|
3858
|
+
async start(controller) {
|
|
3859
|
+
eventSourceParser = (0, import_eventsource_parser.createParser)(
|
|
3860
|
+
(event2) => {
|
|
3861
|
+
if ("data" in event2 && event2.type === "event" && event2.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
|
|
3862
|
+
// @see https://replicate.com/docs/streaming
|
|
3863
|
+
event2.event === "done") {
|
|
3864
|
+
controller.terminate();
|
|
3865
|
+
return;
|
|
3866
|
+
}
|
|
3867
|
+
if ("data" in event2) {
|
|
3868
|
+
const parsedMessage = customParser ? customParser(event2.data, {
|
|
3869
|
+
event: event2.event
|
|
3870
|
+
}) : event2.data;
|
|
3871
|
+
if (parsedMessage)
|
|
3872
|
+
controller.enqueue(parsedMessage);
|
|
3873
|
+
}
|
|
3874
|
+
}
|
|
3875
|
+
);
|
|
3876
|
+
},
|
|
3877
|
+
transform(chunk) {
|
|
3878
|
+
eventSourceParser.feed(textDecoder.decode(chunk));
|
|
3879
|
+
}
|
|
3880
|
+
});
|
|
3881
|
+
}
|
|
3882
|
+
function createCallbacksTransformer(cb) {
|
|
3883
|
+
const textEncoder = new TextEncoder();
|
|
3884
|
+
let aggregatedResponse = "";
|
|
3885
|
+
const callbacks = cb || {};
|
|
3886
|
+
return new TransformStream({
|
|
3887
|
+
async start() {
|
|
3888
|
+
if (callbacks.onStart)
|
|
3889
|
+
await callbacks.onStart();
|
|
3890
|
+
},
|
|
3891
|
+
async transform(message, controller) {
|
|
3892
|
+
const content = typeof message === "string" ? message : message.content;
|
|
3893
|
+
controller.enqueue(textEncoder.encode(content));
|
|
3894
|
+
aggregatedResponse += content;
|
|
3895
|
+
if (callbacks.onToken)
|
|
3896
|
+
await callbacks.onToken(content);
|
|
3897
|
+
if (callbacks.onText && typeof message === "string") {
|
|
3898
|
+
await callbacks.onText(message);
|
|
3899
|
+
}
|
|
3900
|
+
},
|
|
3901
|
+
async flush() {
|
|
3902
|
+
if (callbacks.onCompletion) {
|
|
3903
|
+
await callbacks.onCompletion(aggregatedResponse);
|
|
3904
|
+
}
|
|
3905
|
+
if (callbacks.onFinal) {
|
|
3906
|
+
await callbacks.onFinal(aggregatedResponse);
|
|
3907
|
+
}
|
|
3908
|
+
}
|
|
3909
|
+
});
|
|
3910
|
+
}
|
|
3911
|
+
function trimStartOfStreamHelper() {
|
|
3912
|
+
let isStreamStart = true;
|
|
3913
|
+
return (text) => {
|
|
3914
|
+
if (isStreamStart) {
|
|
3915
|
+
text = text.trimStart();
|
|
3916
|
+
if (text)
|
|
3917
|
+
isStreamStart = false;
|
|
3918
|
+
}
|
|
3919
|
+
return text;
|
|
3920
|
+
};
|
|
3921
|
+
}
|
|
3922
|
+
function AIStream(response, customParser) {
|
|
3923
|
+
if (!response.ok) {
|
|
3924
|
+
return getResponseErrorReadableStream(response.body);
|
|
3925
|
+
}
|
|
3926
|
+
let responseBodyStream = response.body || createEmptyReadableStream2();
|
|
3927
|
+
if (!(responseBodyStream instanceof ReadableStream)) {
|
|
3928
|
+
responseBodyStream = ReadableStream.from(responseBodyStream);
|
|
3929
|
+
}
|
|
3930
|
+
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser));
|
|
3931
|
+
}
|
|
3932
|
+
function createEmptyReadableStream2() {
|
|
3933
|
+
return new ReadableStream({
|
|
3934
|
+
start(controller) {
|
|
3935
|
+
controller.close();
|
|
3936
|
+
}
|
|
3937
|
+
});
|
|
3938
|
+
}
|
|
3939
|
+
function readableFromAsyncIterable(iterable) {
|
|
3940
|
+
let it = iterable[Symbol.asyncIterator]();
|
|
3941
|
+
return new ReadableStream({
|
|
3942
|
+
async pull(controller) {
|
|
3943
|
+
const { done, value } = await it.next();
|
|
3944
|
+
if (done)
|
|
3945
|
+
controller.close();
|
|
3946
|
+
else
|
|
3947
|
+
controller.enqueue(value);
|
|
3948
|
+
},
|
|
3949
|
+
async cancel(reason) {
|
|
3950
|
+
await it.return?.(reason);
|
|
3951
|
+
}
|
|
3952
|
+
});
|
|
3953
|
+
}
|
|
3954
|
+
|
|
3955
|
+
// src/utils/hash/index.ts
|
|
3956
|
+
var hash_exports = {};
|
|
3957
|
+
__export(hash_exports, {
|
|
3958
|
+
XXHashAlgorithm: () => XXHashAlgorithm,
|
|
3959
|
+
base32768: () => base327682,
|
|
3960
|
+
uuid: () => uuid,
|
|
3961
|
+
uuidNIL: () => import_uuid2.NIL,
|
|
3962
|
+
uuidParse: () => import_uuid2.parse,
|
|
3963
|
+
uuidStringify: () => import_uuid2.stringify,
|
|
3964
|
+
uuidValidate: () => import_uuid2.validate,
|
|
3965
|
+
uuidVersion: () => import_uuid2.version,
|
|
3966
|
+
uuidv1: () => import_uuid2.v1,
|
|
3967
|
+
uuidv4: () => import_uuid2.v4,
|
|
3968
|
+
uuidv5: () => import_uuid2.v5,
|
|
3969
|
+
xxhash: () => xxhash,
|
|
3970
|
+
xxhash32: () => xxhash32,
|
|
3971
|
+
xxhash64: () => xxhash64,
|
|
3972
|
+
xxhashAsStr: () => xxhashAsStr
|
|
3973
|
+
});
|
|
3974
|
+
|
|
3975
|
+
// src/utils/hash/uuid.ts
|
|
3976
|
+
var import_uuid = require("uuid");
|
|
3977
|
+
var base32768 = __toESM(require("base32768"));
|
|
3978
|
+
var import_uuid2 = require("uuid");
|
|
3979
|
+
var uuids = { 1: import_uuid.v1, 4: import_uuid.v4, 5: import_uuid.v5 };
|
|
3980
|
+
function uuid(ver = 1, encode3) {
|
|
3981
|
+
const exec = uuids[ver];
|
|
3982
|
+
return encode3 ? base32768.encode(exec(null, [])) : exec();
|
|
3983
|
+
}
|
|
3984
|
+
|
|
3985
|
+
// src/utils/hash/xxhash.ts
|
|
3986
|
+
var xxhash_exports = {};
|
|
3987
|
+
__export(xxhash_exports, {
|
|
3988
|
+
XXHashAlgorithm: () => XXHashAlgorithm,
|
|
3989
|
+
base32768: () => base327682,
|
|
3990
|
+
xxhash: () => xxhash,
|
|
3991
|
+
xxhash32: () => xxhash32,
|
|
3992
|
+
xxhash64: () => xxhash64,
|
|
3993
|
+
xxhashAsStr: () => xxhashAsStr
|
|
3994
|
+
});
|
|
3995
|
+
var import_xxhashjs = __toESM(require("xxhashjs"));
|
|
3996
|
+
var _base32768 = __toESM(require("base32768"));
|
|
3997
|
+
var import_json_canonicalize = require("json-canonicalize");
|
|
3998
|
+
__reExport(xxhash_exports, require("json-canonicalize"));
|
|
3999
|
+
var base327682 = _base32768;
|
|
4000
|
+
var { h32, h64 } = import_xxhashjs.default;
|
|
4001
|
+
var SEED = 588213;
|
|
4002
|
+
function xxhash32(value, radix = 16) {
|
|
4003
|
+
if (typeof value === "object") {
|
|
4004
|
+
value = (0, import_json_canonicalize.canonicalize)(value);
|
|
4005
|
+
}
|
|
4006
|
+
return h32(value, SEED).toString(radix);
|
|
4007
|
+
}
|
|
4008
|
+
function xxhash64(value, radix = 16) {
|
|
4009
|
+
if (typeof value === "object") {
|
|
4010
|
+
value = (0, import_json_canonicalize.canonicalize)(value);
|
|
4011
|
+
}
|
|
4012
|
+
return h64(value, SEED).toString(radix);
|
|
4013
|
+
}
|
|
4014
|
+
var XXHashAlgorithm = /* @__PURE__ */ ((XXHashAlgorithm2) => {
|
|
4015
|
+
XXHashAlgorithm2[XXHashAlgorithm2["xxhash64"] = 111] = "xxhash64";
|
|
4016
|
+
XXHashAlgorithm2[XXHashAlgorithm2["xxhash32"] = 112] = "xxhash32";
|
|
4017
|
+
return XXHashAlgorithm2;
|
|
4018
|
+
})(XXHashAlgorithm || {});
|
|
4019
|
+
function xxhash(value, hashAlgo = 111 /* xxhash64 */, seed = SEED) {
|
|
4020
|
+
if (value.buffer)
|
|
4021
|
+
value = value.buffer;
|
|
4022
|
+
let result;
|
|
4023
|
+
if (hashAlgo !== 112 /* xxhash32 */) {
|
|
4024
|
+
result = h64(value, seed);
|
|
4025
|
+
result = new Uint16Array([result._a00, result._a16, result._a32, result._a48]);
|
|
4026
|
+
result = new Uint8Array(result.buffer);
|
|
4027
|
+
} else {
|
|
4028
|
+
result = h32(value, seed);
|
|
4029
|
+
result = new Uint16Array([result._low, result._high]);
|
|
4030
|
+
result = new Uint8Array(result.buffer);
|
|
4031
|
+
}
|
|
4032
|
+
return result;
|
|
4033
|
+
}
|
|
4034
|
+
function xxhashAsStr(value, hashAlgo = 111 /* xxhash64 */, seed = SEED) {
|
|
4035
|
+
return _base32768.encode(xxhash(value, hashAlgo, seed));
|
|
4036
|
+
}
|
|
4037
|
+
|
|
4038
|
+
// src/utils/hash/index.ts
|
|
4039
|
+
__reExport(hash_exports, xxhash_exports);
|
|
4040
|
+
|
|
4041
|
+
// src/utils/index.ts
|
|
4042
|
+
__reExport(utils_exports, hash_exports);
|
|
4043
|
+
|
|
4044
|
+
// src/utils/filename.ts
|
|
4045
|
+
var import_path = __toESM(require("path"));
|
|
4046
|
+
var FilenameReservedRegex = /[<>:"/\\|?*\u0000-\u001F]/;
|
|
4047
|
+
var WindowsReservedNameRegex = /^(con|prn|aux|nul|com\d|lpt\d)$/i;
|
|
4048
|
+
var MAX_FILENAME_LENGTH = 100;
|
|
4049
|
+
var ReRelativePathRegex = /^\.+(\\|\/)|^\.+$/;
|
|
4050
|
+
var ReTrailingPeriodsRegex = /\.+$/;
|
|
4051
|
+
var ReControlCharsRegex = /[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;
|
|
4052
|
+
function filenameReservedRegex() {
|
|
4053
|
+
return new RegExp(FilenameReservedRegex.source, "g");
|
|
4054
|
+
}
|
|
4055
|
+
function reControlCharsRegex() {
|
|
4056
|
+
return new RegExp(ReControlCharsRegex.source, "g");
|
|
4057
|
+
}
|
|
4058
|
+
function isValidFilename(filename) {
|
|
4059
|
+
return filename && !(FilenameReservedRegex.test(filename) || reControlCharsRegex().test(filename) || ReRelativePathRegex.test(filename) || ReTrailingPeriodsRegex.test(filename));
|
|
4060
|
+
}
|
|
4061
|
+
function isValidFilepath(filepath) {
|
|
4062
|
+
const parts = filepath.split(import_path.default.sep);
|
|
4063
|
+
if (filepath[0] === "/" || parts[0] && import_path.default.dirname(parts[0]) === parts[0]) {
|
|
4064
|
+
parts.shift();
|
|
4065
|
+
}
|
|
4066
|
+
return parts.every(isValidFilename);
|
|
4067
|
+
}
|
|
4068
|
+
function sanitizeFilename(filename, options = {}) {
|
|
4069
|
+
const replacement = options.replacement || "!";
|
|
4070
|
+
if (FilenameReservedRegex.test(replacement) || ReControlCharsRegex.test(replacement)) {
|
|
4071
|
+
throwError2("Replacement string cannot contain reserved filename characters", "sanitizeFilename", 400 /* InvalidArgument */);
|
|
4072
|
+
}
|
|
4073
|
+
if (replacement.length > 0) {
|
|
4074
|
+
const ReRepeatedReservedCharactersRegex = /([<>:"/\\|?*\u0000-\u001F]){2,}/;
|
|
4075
|
+
filename = filename.replace(ReRepeatedReservedCharactersRegex, "$1");
|
|
4076
|
+
}
|
|
4077
|
+
filename = filename.normalize("NFD");
|
|
4078
|
+
filename = filename.replace(ReRelativePathRegex, replacement);
|
|
4079
|
+
filename = filename.replace(filenameReservedRegex(), replacement);
|
|
4080
|
+
filename = filename.replace(reControlCharsRegex(), replacement);
|
|
4081
|
+
filename = filename.replace(ReTrailingPeriodsRegex, "");
|
|
4082
|
+
if (replacement.length > 0) {
|
|
4083
|
+
const startedWithDot = filename[0] === ".";
|
|
4084
|
+
if (!startedWithDot && filename[0] === ".") {
|
|
4085
|
+
filename = replacement + filename;
|
|
4086
|
+
}
|
|
4087
|
+
if (filename[filename.length - 1] === ".") {
|
|
4088
|
+
filename += replacement;
|
|
4089
|
+
}
|
|
4090
|
+
}
|
|
4091
|
+
filename = WindowsReservedNameRegex.test(filename) ? filename + replacement : filename;
|
|
4092
|
+
const allowedLength = typeof options.maxLength === "number" ? options.maxLength : MAX_FILENAME_LENGTH;
|
|
4093
|
+
if (filename.length > allowedLength) {
|
|
4094
|
+
const extensionIndex = filename.lastIndexOf(".");
|
|
4095
|
+
if (extensionIndex === -1) {
|
|
4096
|
+
filename = filename.slice(0, allowedLength);
|
|
4097
|
+
} else {
|
|
4098
|
+
const str = filename.slice(0, extensionIndex);
|
|
4099
|
+
const extension = filename.slice(extensionIndex);
|
|
4100
|
+
filename = str.slice(0, Math.max(1, allowedLength - extension.length)) + extension;
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
return filename;
|
|
4104
|
+
}
|
|
4105
|
+
function sanitizeFilepath(filepath, options = {}) {
|
|
4106
|
+
const parts = filepath.split(import_path.default.sep);
|
|
4107
|
+
let root;
|
|
4108
|
+
if (filepath[0] === "/" || parts[0] && import_path.default.dirname(parts[0]) === parts[0]) {
|
|
4109
|
+
root = parts.shift();
|
|
4110
|
+
}
|
|
4111
|
+
const result = parts.map((p) => sanitizeFilename(p, options));
|
|
4112
|
+
if (root !== void 0) {
|
|
4113
|
+
result.unshift(root);
|
|
4114
|
+
}
|
|
4115
|
+
return result.join(import_path.default.sep);
|
|
4116
|
+
}
|
|
4117
|
+
function getMultiLevelExtname(filename, level = 1) {
|
|
4118
|
+
let result = "";
|
|
4119
|
+
while (level--) {
|
|
4120
|
+
const extname = import_path.default.extname(filename);
|
|
4121
|
+
if (extname) {
|
|
4122
|
+
result = extname + result;
|
|
4123
|
+
filename = import_path.default.basename(filename, extname);
|
|
4124
|
+
} else {
|
|
4125
|
+
break;
|
|
4126
|
+
}
|
|
4127
|
+
}
|
|
4128
|
+
return result;
|
|
4129
|
+
}
|
|
4130
|
+
function extNameLevel(extName) {
|
|
4131
|
+
return extName.split(".").length - 1;
|
|
4132
|
+
}
|
|
4133
|
+
|
|
4134
|
+
// src/utils/is-model-name-matched.ts
|
|
4135
|
+
var import_util_ex2 = require("util-ex");
|
|
4136
|
+
function isModelNameMatched(modelName, rule) {
|
|
4137
|
+
if (typeof modelName !== "string") {
|
|
4138
|
+
throw new CommonError("modelName must be a string", "isModelNameMatched", 400 /* InvalidArgument */);
|
|
4139
|
+
}
|
|
4140
|
+
switch (typeof rule) {
|
|
4141
|
+
case "string":
|
|
4142
|
+
if ((0, import_util_ex2.isRegExpStr)(rule)) {
|
|
4143
|
+
rule = (0, import_util_ex2.toRegExp)(rule);
|
|
4144
|
+
const match = rule.exec(modelName);
|
|
4145
|
+
if (match) {
|
|
4146
|
+
return match;
|
|
4147
|
+
}
|
|
4148
|
+
} else if (modelName.toLowerCase() === rule.toLowerCase()) {
|
|
4149
|
+
return rule;
|
|
4150
|
+
}
|
|
4151
|
+
break;
|
|
4152
|
+
case "object":
|
|
4153
|
+
if (Array.isArray(rule)) {
|
|
4154
|
+
for (const item of rule) {
|
|
4155
|
+
if (typeof item === "string") {
|
|
4156
|
+
if ((0, import_util_ex2.isRegExpStr)(item)) {
|
|
4157
|
+
rule = (0, import_util_ex2.toRegExp)(item);
|
|
4158
|
+
const match = rule.exec(modelName);
|
|
4159
|
+
if (match) {
|
|
4160
|
+
return match;
|
|
4161
|
+
}
|
|
4162
|
+
} else if (modelName.toLowerCase() === item.toLowerCase()) {
|
|
4163
|
+
return item;
|
|
4164
|
+
}
|
|
4165
|
+
} else if (item instanceof RegExp) {
|
|
4166
|
+
const match = item.exec(modelName);
|
|
4167
|
+
if (match) {
|
|
4168
|
+
return match;
|
|
4169
|
+
}
|
|
4170
|
+
} else if (typeof item === "function") {
|
|
4171
|
+
const result2 = item.call(this, modelName);
|
|
4172
|
+
if (result2) {
|
|
4173
|
+
return result2;
|
|
4174
|
+
}
|
|
4175
|
+
}
|
|
4176
|
+
}
|
|
4177
|
+
} else if (rule instanceof RegExp) {
|
|
4178
|
+
const match = rule.exec(modelName);
|
|
4179
|
+
if (match) {
|
|
4180
|
+
return match;
|
|
4181
|
+
}
|
|
4182
|
+
}
|
|
4183
|
+
break;
|
|
4184
|
+
case "function":
|
|
4185
|
+
const result = rule.call(this, modelName);
|
|
4186
|
+
if (result) {
|
|
4187
|
+
return result;
|
|
4188
|
+
}
|
|
4189
|
+
break;
|
|
4190
|
+
}
|
|
4191
|
+
}
|
|
4192
|
+
|
|
4193
|
+
// src/utils/get-all-enum-keys.ts
|
|
4194
|
+
function getAllEnumKeys(enumType) {
|
|
4195
|
+
return Object.keys(enumType).filter((k) => Number.isNaN(Number(k)));
|
|
4196
|
+
}
|
|
4197
|
+
|
|
4198
|
+
// src/utils/parse-js-json.ts
|
|
4199
|
+
var import_util_ex3 = require("util-ex");
|
|
4200
|
+
function parseJsJson(input) {
|
|
4201
|
+
const fn = (0, import_util_ex3.newFunction)("expression", [], `return ${input}`);
|
|
4202
|
+
return fn();
|
|
4203
|
+
}
|
|
4204
|
+
|
|
4205
|
+
// src/utils/get-keys-path.ts
|
|
4206
|
+
function isObject(value) {
|
|
4207
|
+
return !!value && value.constructor === Object;
|
|
4208
|
+
}
|
|
4209
|
+
function getKeysPath(value) {
|
|
4210
|
+
if (!value)
|
|
4211
|
+
return [];
|
|
4212
|
+
return getKeys(value, [], "");
|
|
4213
|
+
}
|
|
4214
|
+
function getKeys(obj, paths, dot = ".") {
|
|
4215
|
+
if (Array.isArray(obj)) {
|
|
4216
|
+
return obj.flatMap((item, i) => getKeys(item, [...paths, `[${i}]`]));
|
|
4217
|
+
}
|
|
4218
|
+
if (isObject(obj)) {
|
|
4219
|
+
return Object.entries(obj).flatMap(
|
|
4220
|
+
([k, v]) => getKeys(v, [...paths, dot + k])
|
|
4221
|
+
);
|
|
4222
|
+
}
|
|
4223
|
+
return [paths.join("")];
|
|
4224
|
+
}
|
|
4225
|
+
|
|
4226
|
+
// src/utils/end-with-repeated-sequence.ts
|
|
4227
|
+
function createEndWithRepetitionDetector(repetitionThreshold) {
|
|
4228
|
+
if (repetitionThreshold < 2) {
|
|
4229
|
+
throw new CommonError("repetitionThreshold must be greater than 1", "createEndWithRepetitionDetector", 400 /* InvalidArgument */);
|
|
4230
|
+
}
|
|
4231
|
+
const regex = new RegExp(`([\\S\\s]+)(\\1{${repetitionThreshold - 1},})$`);
|
|
4232
|
+
return function endWithRepeatedSequence(value) {
|
|
4233
|
+
return regex.test(value);
|
|
4234
|
+
};
|
|
4235
|
+
}
|
|
4236
|
+
|
|
4237
|
+
// src/utils/load-file-from-paths.ts
|
|
4238
|
+
var import_path2 = __toESM(require("path"));
|
|
4239
|
+
var fs = __toESM(require("fs"));
|
|
4240
|
+
function loadFileFromPaths(filename, searchPaths, extNames) {
|
|
4241
|
+
let result;
|
|
4242
|
+
if (import_path2.default.isAbsolute(filename)) {
|
|
4243
|
+
if (fs.existsSync(filename)) {
|
|
4244
|
+
result = filename;
|
|
4245
|
+
}
|
|
4246
|
+
} else {
|
|
4247
|
+
if (!searchPaths) {
|
|
4248
|
+
searchPaths = ["."];
|
|
4249
|
+
}
|
|
4250
|
+
let exts = extNames ? extNames.map((ext) => getMultiLevelExtname(filename, extNameLevel(ext))) : void 0;
|
|
4251
|
+
for (const searchPath of searchPaths) {
|
|
4252
|
+
const filePath = import_path2.default.resolve(searchPath, filename);
|
|
4253
|
+
if (exts) {
|
|
4254
|
+
for (let i = 0; i < exts.length; i++) {
|
|
4255
|
+
const extName = exts[i] !== extNames[i] ? extNames[i] : "";
|
|
4256
|
+
const filenameWithExt = filePath + extName;
|
|
4257
|
+
if (fs.existsSync(filenameWithExt)) {
|
|
4258
|
+
result = filenameWithExt;
|
|
4259
|
+
break;
|
|
4260
|
+
}
|
|
4261
|
+
}
|
|
4262
|
+
} else {
|
|
4263
|
+
if (fs.existsSync(filePath)) {
|
|
4264
|
+
result = filePath;
|
|
4265
|
+
break;
|
|
4266
|
+
}
|
|
4267
|
+
}
|
|
4268
|
+
}
|
|
4269
|
+
}
|
|
4270
|
+
if (result) {
|
|
4271
|
+
result = fs.readFileSync(result);
|
|
4272
|
+
} else {
|
|
4273
|
+
throw new NotFoundError(filename, "loadFileFromPaths");
|
|
4274
|
+
}
|
|
4275
|
+
return result;
|
|
4276
|
+
}
|
|
4277
|
+
function loadTextFromPaths(filename, searchPaths, extNames, encoding = "utf8") {
|
|
4278
|
+
const result = loadFileFromPaths(filename, searchPaths, extNames);
|
|
4279
|
+
return result.toString(encoding);
|
|
4280
|
+
}
|
|
4281
|
+
|
|
4282
|
+
// src/utils/config.ts
|
|
4283
|
+
var import_fs = require("fs");
|
|
4284
|
+
var import_path3 = __toESM(require("path"));
|
|
4285
|
+
var import_load_config_file = require("load-config-file");
|
|
4286
|
+
var import_yaml = require("yaml");
|
|
4287
|
+
var import_yaml_types = require("yaml-types");
|
|
4288
|
+
var import_with_db = require("mime-type/with-db");
|
|
4289
|
+
var import_yaml2 = require("yaml");
|
|
4290
|
+
var YamlTags = [import_yaml_types.regexp];
|
|
4291
|
+
function registerYamlTag(tags) {
|
|
4292
|
+
if (!Array.isArray(tags)) {
|
|
4293
|
+
tags = [tags];
|
|
4294
|
+
}
|
|
4295
|
+
for (const tag of tags) {
|
|
4296
|
+
const result = YamlTags.indexOf(tag) === -1;
|
|
4297
|
+
if (result) {
|
|
4298
|
+
YamlTags.push(tag);
|
|
4299
|
+
}
|
|
4300
|
+
}
|
|
4301
|
+
}
|
|
4302
|
+
function parseYaml(content) {
|
|
4303
|
+
return (0, import_yaml.parse)(content, { customTags: YamlTags });
|
|
4304
|
+
}
|
|
4305
|
+
function parseJson(content) {
|
|
4306
|
+
return JSON.parse(content);
|
|
4307
|
+
}
|
|
4308
|
+
import_load_config_file.Config.register([".yml", ".yaml"], parseYaml);
|
|
4309
|
+
import_load_config_file.Config.register([".json"], parseJson);
|
|
4310
|
+
function traverseFolderSync(directoryPath, fileHandler) {
|
|
4311
|
+
const files = (0, import_fs.readdirSync)(directoryPath, { withFileTypes: true, recursive: true });
|
|
4312
|
+
for (const entry of files) {
|
|
4313
|
+
const filePath = import_path3.default.join(directoryPath, entry.name);
|
|
4314
|
+
if (entry.isFile()) {
|
|
4315
|
+
fileHandler(filePath, entry);
|
|
4316
|
+
}
|
|
4317
|
+
}
|
|
4318
|
+
}
|
|
4319
|
+
function getConfigFileNames(directoryPath) {
|
|
4320
|
+
const configFiles = [];
|
|
4321
|
+
traverseFolderSync(directoryPath, (filePath) => {
|
|
4322
|
+
const extname = import_path3.default.extname(filePath);
|
|
4323
|
+
if (extname === ".yml" || extname === ".yaml" || extname === ".json") {
|
|
4324
|
+
configFiles.push(filePath);
|
|
4325
|
+
}
|
|
4326
|
+
});
|
|
4327
|
+
return configFiles;
|
|
4328
|
+
}
|
|
4329
|
+
function getConfigs(directoryPath) {
|
|
4330
|
+
const files = getConfigFileNames(directoryPath);
|
|
4331
|
+
return files.map((file) => import_load_config_file.Config.loadSync(file)).filter((config) => config !== void 0);
|
|
4332
|
+
}
|
|
4333
|
+
function saveConfigFile(filename, config, extLevel = 1) {
|
|
4334
|
+
if (filename[0] === ".") {
|
|
4335
|
+
extLevel++;
|
|
4336
|
+
}
|
|
4337
|
+
const extname = getMultiLevelExtname(filename, extLevel);
|
|
4338
|
+
if (!extname || extname.split(".").length <= 1) {
|
|
4339
|
+
filename += ".yaml";
|
|
4340
|
+
}
|
|
4341
|
+
const mime = import_with_db.mimeType.lookup(filename);
|
|
4342
|
+
if (mime === "application/json")
|
|
4343
|
+
config = JSON.stringify(config, null, 2);
|
|
4344
|
+
else if (mime === "text/yaml") {
|
|
4345
|
+
config = (0, import_yaml.stringify)(config);
|
|
4346
|
+
} else {
|
|
4347
|
+
throw new Error(`${filename} unsupported mime type: ${mime}`);
|
|
4348
|
+
}
|
|
4349
|
+
const dirname = import_path3.default.dirname(filename);
|
|
4350
|
+
if (!(0, import_fs.existsSync)(dirname)) {
|
|
4351
|
+
(0, import_fs.mkdirSync)(dirname, { recursive: true });
|
|
4352
|
+
}
|
|
4353
|
+
(0, import_fs.writeFileSync)(filename, config, { encoding: "utf8" });
|
|
4354
|
+
}
|
|
4355
|
+
|
|
4356
|
+
// src/utils/json-filter.ts
|
|
4357
|
+
function quoteValueIfString(shouldQuote, value) {
|
|
4358
|
+
if (shouldQuote) {
|
|
4359
|
+
return `'${value}'`;
|
|
4360
|
+
}
|
|
4361
|
+
return String(value);
|
|
4362
|
+
}
|
|
4363
|
+
function generateAndClause(filters, wrapKey) {
|
|
4364
|
+
return filters.map((block) => jsonFilterToWhere(block, wrapKey)).join(" AND ");
|
|
4365
|
+
}
|
|
4366
|
+
function generateOrClause(filters, wrapKey) {
|
|
4367
|
+
return filters.map((block) => jsonFilterToWhere(block, wrapKey)).join(" OR ");
|
|
4368
|
+
}
|
|
4369
|
+
function generateCondition(dataKey, condition) {
|
|
4370
|
+
const andQuery = [];
|
|
4371
|
+
Object.keys(condition).forEach((key) => {
|
|
4372
|
+
const value = condition[key];
|
|
4373
|
+
let hasQuotes = typeof condition[key] === "string";
|
|
4374
|
+
switch (key) {
|
|
4375
|
+
case "$lt":
|
|
4376
|
+
case "<":
|
|
4377
|
+
andQuery.push(`${dataKey} < ${quoteValueIfString(hasQuotes, value)}`);
|
|
4378
|
+
break;
|
|
4379
|
+
case "<=":
|
|
4380
|
+
case "$lte":
|
|
4381
|
+
andQuery.push(`${dataKey} <= ${quoteValueIfString(hasQuotes, value)}`);
|
|
4382
|
+
break;
|
|
4383
|
+
case "$gt":
|
|
4384
|
+
case ">":
|
|
4385
|
+
andQuery.push(`${dataKey} > ${quoteValueIfString(hasQuotes, value)}`);
|
|
4386
|
+
break;
|
|
4387
|
+
case "$gte":
|
|
4388
|
+
case ">=":
|
|
4389
|
+
andQuery.push(`${dataKey} >= ${quoteValueIfString(hasQuotes, value)}`);
|
|
4390
|
+
break;
|
|
4391
|
+
case "$ne":
|
|
4392
|
+
case "!=":
|
|
4393
|
+
andQuery.push(`${dataKey} != ${quoteValueIfString(hasQuotes, value)}`);
|
|
4394
|
+
break;
|
|
4395
|
+
case "=":
|
|
4396
|
+
case "$eq":
|
|
4397
|
+
andQuery.push(`${dataKey} = ${quoteValueIfString(hasQuotes, value)}`);
|
|
4398
|
+
break;
|
|
4399
|
+
case "$in":
|
|
4400
|
+
andQuery.push(`${dataKey} IN (${value.map((e) => quoteValueIfString(true, e)).join(", ")})`);
|
|
4401
|
+
break;
|
|
4402
|
+
case "$nin":
|
|
4403
|
+
andQuery.push(`${dataKey} NOT IN (${value.map((e) => quoteValueIfString(true, e)).join(", ")})`);
|
|
4404
|
+
break;
|
|
4405
|
+
case "$regex":
|
|
4406
|
+
andQuery.push(`${dataKey} REGEXP '${value.source}'`);
|
|
4407
|
+
break;
|
|
4408
|
+
case "$like":
|
|
4409
|
+
andQuery.push(`${dataKey} LIKE '${value}'`);
|
|
4410
|
+
break;
|
|
4411
|
+
case "$nlike":
|
|
4412
|
+
andQuery.push(`${dataKey} NOT LIKE '${value}'`);
|
|
4413
|
+
break;
|
|
4414
|
+
case "$glob":
|
|
4415
|
+
andQuery.push(`${dataKey} GLOB '${value}'`);
|
|
4416
|
+
break;
|
|
4417
|
+
case "$nglob":
|
|
4418
|
+
andQuery.push(`${dataKey} NOT GLOB '${value}'`);
|
|
4419
|
+
break;
|
|
4420
|
+
default:
|
|
4421
|
+
throw new Error(`Unsupported condition operator: ${key}`);
|
|
4422
|
+
}
|
|
4423
|
+
});
|
|
4424
|
+
return andQuery.join(" AND ");
|
|
4425
|
+
}
|
|
4426
|
+
function jsonFilterToWhere(filter, wrapKey) {
|
|
4427
|
+
const andQuery = [];
|
|
4428
|
+
if (typeof wrapKey !== "function") {
|
|
4429
|
+
wrapKey = (k) => k;
|
|
4430
|
+
}
|
|
4431
|
+
if (Array.isArray(filter)) {
|
|
4432
|
+
andQuery.push(generateAndClause(filter, wrapKey));
|
|
4433
|
+
} else
|
|
4434
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
4435
|
+
if (key === "$and") {
|
|
4436
|
+
andQuery.push(`(${generateAndClause(filter[key], wrapKey)})`);
|
|
4437
|
+
} else if (key === "$or") {
|
|
4438
|
+
andQuery.push(`(${generateOrClause(filter[key], wrapKey)})`);
|
|
4439
|
+
} else {
|
|
4440
|
+
const tValue = typeof value;
|
|
4441
|
+
if (value == null) {
|
|
4442
|
+
andQuery.push(`${wrapKey(key)} IS NULL`);
|
|
4443
|
+
} else if (tValue === "object" && !Array.isArray(value)) {
|
|
4444
|
+
andQuery.push(generateCondition(wrapKey(key), value));
|
|
4445
|
+
} else if (tValue === "string") {
|
|
4446
|
+
andQuery.push(`${wrapKey(key)}='${value}'`);
|
|
4447
|
+
} else if (tValue === "number" || tValue === "boolean") {
|
|
4448
|
+
andQuery.push(`${wrapKey(key)}=${value}`);
|
|
4449
|
+
} else {
|
|
4450
|
+
throw new Error(`Unsupported value type for key ${key}`);
|
|
4451
|
+
}
|
|
4452
|
+
}
|
|
4453
|
+
}
|
|
4454
|
+
;
|
|
4455
|
+
return andQuery.length > 1 ? andQuery.join(" AND ") : andQuery[0];
|
|
4456
|
+
}
|
|
4457
|
+
|
|
4458
|
+
// src/client-tools.ts
|
|
4459
|
+
var ClientTools = class _ClientTools extends ToolFunc {
|
|
4460
|
+
static get apiRoot() {
|
|
4461
|
+
return _ClientTools._apiRoot;
|
|
4462
|
+
}
|
|
4463
|
+
static setApiRoot(v) {
|
|
4464
|
+
_ClientTools._apiRoot = v;
|
|
4465
|
+
}
|
|
4466
|
+
// static items = ClientToolItems;
|
|
4467
|
+
static async loadFrom() {
|
|
4468
|
+
if (this._apiRoot) {
|
|
4469
|
+
const res = await fetch(this._apiRoot, { headers: {
|
|
4470
|
+
"Content-Type": "application/json"
|
|
4471
|
+
} });
|
|
4472
|
+
const items = await res.json();
|
|
4473
|
+
if (items)
|
|
4474
|
+
this.loadFromSync(items);
|
|
4475
|
+
} else {
|
|
4476
|
+
throwError2("missing apiRoot to load tools", "ClientTools");
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
static loadFromSync(items) {
|
|
4480
|
+
for (const name in items) {
|
|
4481
|
+
const item = this.get(name);
|
|
4482
|
+
const funcItem = items[name];
|
|
4483
|
+
if (!item) {
|
|
4484
|
+
this.register(funcItem);
|
|
4485
|
+
} else if (item instanceof _ClientTools) {
|
|
4486
|
+
item.assign(funcItem);
|
|
4487
|
+
} else {
|
|
4488
|
+
throwError2(`${name} already registered as ${item.constructor.name}`, "ClientTools");
|
|
4489
|
+
}
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
static async fetch(name, objParam) {
|
|
4493
|
+
const func = this.get(name);
|
|
4494
|
+
if (func && func.fetch) {
|
|
4495
|
+
return func.fetch(objParam);
|
|
4496
|
+
}
|
|
4497
|
+
}
|
|
4498
|
+
getUrlParams(objParam) {
|
|
4499
|
+
if (objParam !== void 0) {
|
|
4500
|
+
const objParamStr = JSON.stringify(objParam);
|
|
4501
|
+
if (objParamStr !== "{}" && objParamStr !== "[]" && objParamStr !== '""') {
|
|
4502
|
+
return "?p=" + encodeURIComponent(objParamStr);
|
|
4503
|
+
}
|
|
4504
|
+
}
|
|
4505
|
+
return "";
|
|
4506
|
+
}
|
|
4507
|
+
async fetch(objParam, act, subName) {
|
|
4508
|
+
const fetchOptions = { ...this.fetchOptions };
|
|
4509
|
+
if (!fetchOptions.headers || !fetchOptions.headers["Content-Type"]) {
|
|
4510
|
+
fetchOptions.headers = {
|
|
4511
|
+
"Content-Type": "application/json",
|
|
4512
|
+
...fetchOptions.headers
|
|
4513
|
+
};
|
|
4514
|
+
}
|
|
4515
|
+
if (objParam?.stream && !fetchOptions.headers.Connection) {
|
|
4516
|
+
fetchOptions.headers.Connection = "keep-alive";
|
|
4517
|
+
}
|
|
4518
|
+
if (!act) {
|
|
4519
|
+
act = this.action || "post";
|
|
4520
|
+
}
|
|
4521
|
+
if (act === "res") {
|
|
4522
|
+
act = "get";
|
|
4523
|
+
}
|
|
4524
|
+
if (subName) {
|
|
4525
|
+
if (typeof subName !== "string") {
|
|
4526
|
+
subName = JSON.stringify(subName);
|
|
4527
|
+
}
|
|
4528
|
+
subName = this.name + "/" + subName;
|
|
4529
|
+
} else {
|
|
4530
|
+
subName = this.name;
|
|
4531
|
+
}
|
|
4532
|
+
fetchOptions.method = act.toUpperCase();
|
|
4533
|
+
let urlPart;
|
|
4534
|
+
if (act === "get" || act === "delete") {
|
|
4535
|
+
urlPart = subName + this.getUrlParams(objParam);
|
|
4536
|
+
} else {
|
|
4537
|
+
fetchOptions.body = JSON.stringify(objParam);
|
|
4538
|
+
urlPart = subName;
|
|
4539
|
+
}
|
|
4540
|
+
const res = await fetch(`${this.apiRoot}/${urlPart}`, fetchOptions);
|
|
4541
|
+
if (!res.ok) {
|
|
4542
|
+
const err = await this.errorFrom(res);
|
|
4543
|
+
throw err;
|
|
4544
|
+
}
|
|
4545
|
+
return res;
|
|
4546
|
+
}
|
|
4547
|
+
async errorFrom(res) {
|
|
4548
|
+
let errCode = res.status;
|
|
4549
|
+
let errMsg = res.statusText;
|
|
4550
|
+
let name = this.name;
|
|
4551
|
+
let data;
|
|
4552
|
+
if (res.body) {
|
|
4553
|
+
const text = await res.text();
|
|
4554
|
+
try {
|
|
4555
|
+
const body = JSON.parse(text);
|
|
4556
|
+
if (body) {
|
|
4557
|
+
if (body.error) {
|
|
4558
|
+
errMsg = body.error;
|
|
4559
|
+
}
|
|
4560
|
+
if (body.name) {
|
|
4561
|
+
name = body.name;
|
|
4562
|
+
}
|
|
4563
|
+
if (body.data) {
|
|
4564
|
+
data = body.data;
|
|
4565
|
+
data.name = name;
|
|
4566
|
+
if (data.what) {
|
|
4567
|
+
data.msg = errMsg;
|
|
4568
|
+
errMsg = data.what;
|
|
4569
|
+
}
|
|
4570
|
+
}
|
|
4571
|
+
if (body.message) {
|
|
4572
|
+
errMsg = errMsg + ":" + body.message;
|
|
4573
|
+
}
|
|
4574
|
+
}
|
|
4575
|
+
} catch (e) {
|
|
4576
|
+
console.warn("\u{1F680} ~ parse error body to json:", e);
|
|
4577
|
+
}
|
|
4578
|
+
}
|
|
4579
|
+
return createError(errMsg, name, errCode);
|
|
4580
|
+
}
|
|
4581
|
+
async func(objParam) {
|
|
4582
|
+
const res = await this.fetch(objParam);
|
|
4583
|
+
if (objParam?.stream) {
|
|
4584
|
+
return res;
|
|
4585
|
+
}
|
|
4586
|
+
const result = await res.json();
|
|
4587
|
+
return result;
|
|
4588
|
+
}
|
|
4589
|
+
};
|
|
4590
|
+
var ClientToolFuncSchema = { ...RemoteToolFuncSchema };
|
|
4591
|
+
ClientTools.defineProperties(ClientTools, ClientToolFuncSchema);
|
|
4592
|
+
|
|
4593
|
+
// src/res-client-tools.ts
|
|
4594
|
+
var ResClientTools = class extends ClientTools {
|
|
4595
|
+
async fetch(options, action) {
|
|
4596
|
+
if (!options) {
|
|
4597
|
+
options = {};
|
|
4598
|
+
}
|
|
4599
|
+
if (action && this.action === "res") {
|
|
4600
|
+
if (action === "get" || action === "delete") {
|
|
4601
|
+
let id = options.id;
|
|
4602
|
+
if (!id) {
|
|
4603
|
+
throwError2("id is required");
|
|
4604
|
+
}
|
|
4605
|
+
if (typeof id === "string") {
|
|
4606
|
+
id = encodeURIComponent(id);
|
|
4607
|
+
}
|
|
4608
|
+
delete options.id;
|
|
4609
|
+
return super.fetch(options, action, id);
|
|
4610
|
+
} else if (action === "list") {
|
|
4611
|
+
action = "get";
|
|
4612
|
+
} else if (action.startsWith("$")) {
|
|
4613
|
+
options.act = action;
|
|
4614
|
+
action = "post";
|
|
4615
|
+
}
|
|
4616
|
+
}
|
|
4617
|
+
return await super.fetch(options, action);
|
|
4618
|
+
}
|
|
4619
|
+
async _func(action, options) {
|
|
4620
|
+
const res = await this.fetch(options, action);
|
|
4621
|
+
if (options?.stream) {
|
|
4622
|
+
return res;
|
|
4623
|
+
}
|
|
4624
|
+
const result = await res.json();
|
|
4625
|
+
return result;
|
|
4626
|
+
}
|
|
4627
|
+
async func(options) {
|
|
4628
|
+
const action = options.action;
|
|
4629
|
+
if (action) {
|
|
4630
|
+
delete options.action;
|
|
4631
|
+
}
|
|
4632
|
+
return this._func(action, options);
|
|
4633
|
+
}
|
|
4634
|
+
assignMethods(methods) {
|
|
4635
|
+
if (Array.isArray(methods)) {
|
|
4636
|
+
for (const action of methods) {
|
|
4637
|
+
const name = action.startsWith("$") ? action.slice(1) : action;
|
|
4638
|
+
if (!this[name]) {
|
|
4639
|
+
this[name] = ((act) => this._func.bind(this, act))(action);
|
|
4640
|
+
}
|
|
4641
|
+
}
|
|
4642
|
+
}
|
|
4643
|
+
}
|
|
4644
|
+
};
|
|
4645
|
+
var ResClientToolsSchema = {
|
|
4646
|
+
methods: {
|
|
4647
|
+
type: "array",
|
|
4648
|
+
assign(value, dest, src, name, options) {
|
|
4649
|
+
if (!options?.isExported) {
|
|
4650
|
+
dest.assignMethods(value);
|
|
4651
|
+
}
|
|
4652
|
+
}
|
|
4653
|
+
}
|
|
4654
|
+
};
|
|
4655
|
+
ResClientTools.defineProperties(ResClientTools, ResClientToolsSchema);
|
|
4656
|
+
|
|
4657
|
+
// src/funcs/event-client.ts
|
|
4658
|
+
var EventClient = class extends ResClientTools {
|
|
4659
|
+
constructor() {
|
|
4660
|
+
super(...arguments);
|
|
4661
|
+
// subscribed sse events
|
|
4662
|
+
this._sseEvents = {};
|
|
4663
|
+
this._forwardEvents = /* @__PURE__ */ new Set();
|
|
4664
|
+
this.name = EventName;
|
|
4665
|
+
this.description = "subscribe server sent event";
|
|
4666
|
+
// pass event-bus event to server
|
|
4667
|
+
this.ebListener = async function(...data) {
|
|
4668
|
+
const event2 = this.type;
|
|
4669
|
+
if (this.target.publish) {
|
|
4670
|
+
await this.target.publish({ data, event: event2 });
|
|
4671
|
+
}
|
|
4672
|
+
};
|
|
4673
|
+
}
|
|
4674
|
+
get evtSource() {
|
|
4675
|
+
let result = this._es;
|
|
4676
|
+
if (!result || result.readyState === EventSource.CLOSED) {
|
|
4677
|
+
result = this.initEventSource(this._esEvents);
|
|
4678
|
+
}
|
|
4679
|
+
return result;
|
|
4680
|
+
}
|
|
4681
|
+
get active() {
|
|
4682
|
+
return !!this._es && this._es.readyState !== EventSource.CLOSED;
|
|
4683
|
+
}
|
|
4684
|
+
set active(v) {
|
|
4685
|
+
if (v !== this.active) {
|
|
4686
|
+
if (v) {
|
|
4687
|
+
this.initEventSource(this._esEvents);
|
|
4688
|
+
} else if (this._es) {
|
|
4689
|
+
this._es.close();
|
|
4690
|
+
this._es = void 0;
|
|
4691
|
+
}
|
|
4692
|
+
}
|
|
4693
|
+
}
|
|
4694
|
+
initEventSource(events) {
|
|
4695
|
+
if (typeof events === "string") {
|
|
4696
|
+
events = [events];
|
|
4697
|
+
}
|
|
4698
|
+
if (this._es && this._es.readyState !== EventSource.CLOSED) {
|
|
4699
|
+
if (!this._esEvents || events && events.every((e) => this._esEvents.includes(e))) {
|
|
4700
|
+
return this._es;
|
|
4701
|
+
} else {
|
|
4702
|
+
this._es.close();
|
|
4703
|
+
}
|
|
4704
|
+
}
|
|
4705
|
+
const urlParams = events ? this.getUrlParams({ event: events }) : "";
|
|
4706
|
+
const url = `${this.apiRoot}/${this.name}${urlParams}`;
|
|
4707
|
+
const evtSource = this._es = new EventSource(url);
|
|
4708
|
+
Object.entries(this._sseEvents).forEach(([event2, listener]) => {
|
|
4709
|
+
evtSource.addEventListener(event2, listener);
|
|
4710
|
+
});
|
|
4711
|
+
this._esEvents = events;
|
|
4712
|
+
return evtSource;
|
|
4713
|
+
}
|
|
4714
|
+
// pass server sent event to eventBus
|
|
4715
|
+
esListener(event2) {
|
|
4716
|
+
const data = event2.data ? JSON.parse(event2.data) : void 0;
|
|
4717
|
+
const evtType = event2.type;
|
|
4718
|
+
if (!this._forwardEvents.has(evtType)) {
|
|
4719
|
+
const emitter = this.emitter;
|
|
4720
|
+
if (emitter && data && evtType) {
|
|
4721
|
+
if (Array.isArray(data)) {
|
|
4722
|
+
emitter.emit(evtType, ...data);
|
|
4723
|
+
} else {
|
|
4724
|
+
emitter.emit(evtType, data);
|
|
4725
|
+
}
|
|
4726
|
+
}
|
|
4727
|
+
}
|
|
4728
|
+
}
|
|
4729
|
+
/**
|
|
4730
|
+
* subscribe server sent event(SSE)
|
|
4731
|
+
* @param events
|
|
4732
|
+
*/
|
|
4733
|
+
async subscribe(events) {
|
|
4734
|
+
const result = await this.sub({ event: events });
|
|
4735
|
+
if (typeof events === "string") {
|
|
4736
|
+
events = [events];
|
|
4737
|
+
}
|
|
4738
|
+
const evtSource = this.evtSource;
|
|
4739
|
+
for (const event2 of events) {
|
|
4740
|
+
if (!this._sseEvents[event2]) {
|
|
4741
|
+
const listener = this._sseEvents[event2] = this.esListener.bind(this);
|
|
4742
|
+
evtSource.addEventListener(event2, listener);
|
|
4743
|
+
}
|
|
4744
|
+
}
|
|
4745
|
+
return result;
|
|
4746
|
+
}
|
|
4747
|
+
/**
|
|
4748
|
+
* unsubscribe server sent event(SSE)
|
|
4749
|
+
* @param events
|
|
4750
|
+
*/
|
|
4751
|
+
async unsubscribe(events) {
|
|
4752
|
+
const result = await this.unsub({ event: events });
|
|
4753
|
+
if (typeof events === "string") {
|
|
4754
|
+
events = [events];
|
|
4755
|
+
}
|
|
4756
|
+
for (const event2 of events) {
|
|
4757
|
+
const listener = this._sseEvents[event2];
|
|
4758
|
+
if (listener) {
|
|
4759
|
+
delete this._sseEvents[event2];
|
|
4760
|
+
this.evtSource.removeEventListener(event2, listener);
|
|
4761
|
+
}
|
|
4762
|
+
}
|
|
4763
|
+
return result;
|
|
4764
|
+
}
|
|
4765
|
+
/**
|
|
4766
|
+
* forward local event(s) to server
|
|
4767
|
+
*
|
|
4768
|
+
* subscribe these local events to forward/publish to server
|
|
4769
|
+
*
|
|
4770
|
+
* Note: pls backendEventable(ClientTools or EventClient) first
|
|
4771
|
+
* @param events
|
|
4772
|
+
*/
|
|
4773
|
+
forwardEvent(events) {
|
|
4774
|
+
if (typeof events === "string") {
|
|
4775
|
+
events = [events];
|
|
4776
|
+
}
|
|
4777
|
+
for (const event2 of events) {
|
|
4778
|
+
if (!this._forwardEvents.has(event2)) {
|
|
4779
|
+
this._forwardEvents.add(event2);
|
|
4780
|
+
if (this.on)
|
|
4781
|
+
this.on(event2, this.ebListener);
|
|
4782
|
+
}
|
|
4783
|
+
}
|
|
4784
|
+
}
|
|
4785
|
+
/**
|
|
4786
|
+
* unforward local event(s) to server
|
|
4787
|
+
*
|
|
4788
|
+
* unsubscribe these local events not to forward/publish to server
|
|
4789
|
+
*
|
|
4790
|
+
* Note: pls backendEventable(ClientTools or EventClient) first
|
|
4791
|
+
* @param events
|
|
4792
|
+
*/
|
|
4793
|
+
unforwardEvent(events) {
|
|
4794
|
+
if (typeof events === "string") {
|
|
4795
|
+
events = [events];
|
|
4796
|
+
}
|
|
4797
|
+
for (const event2 of events) {
|
|
4798
|
+
if (this._forwardEvents.has(event2)) {
|
|
4799
|
+
this._forwardEvents.delete(event2);
|
|
4800
|
+
if (this.off)
|
|
4801
|
+
this.off(event2, this.ebListener);
|
|
4802
|
+
}
|
|
4803
|
+
}
|
|
4804
|
+
}
|
|
4805
|
+
async init(events) {
|
|
4806
|
+
this.active = false;
|
|
4807
|
+
this.initEventSource(events);
|
|
4808
|
+
if (events) {
|
|
4809
|
+
return await this.subscribe(events);
|
|
4810
|
+
}
|
|
4811
|
+
}
|
|
4812
|
+
};
|
|
4813
|
+
var eventClient = new EventClient(EventName);
|
|
4814
|
+
|
|
4815
|
+
// src/res-server-tools.ts
|
|
4816
|
+
var import_util_ex4 = require("util-ex");
|
|
4817
|
+
var ResServerTools = class extends ServerTools {
|
|
4818
|
+
constructor(name, options = {}) {
|
|
4819
|
+
super(name, options);
|
|
4820
|
+
this.action = "res";
|
|
4821
|
+
this.params = {
|
|
4822
|
+
"id": { type: "string" },
|
|
4823
|
+
"val": { type: "any" }
|
|
4824
|
+
};
|
|
4825
|
+
const methods = this.methods = [];
|
|
4826
|
+
for (const action of ActionNames) {
|
|
4827
|
+
if (typeof this[action] === "function") {
|
|
4828
|
+
methods.push(action);
|
|
4829
|
+
}
|
|
4830
|
+
}
|
|
4831
|
+
(0, import_util_ex4.getAllNames)(Object.getPrototypeOf(this)).filter((name2) => name2.startsWith("$") && typeof this[name2] === "function").forEach((name2) => {
|
|
4832
|
+
methods.push(name2);
|
|
4833
|
+
const n = name2.slice(1);
|
|
4834
|
+
if (this[n] === void 0) {
|
|
4835
|
+
this[n] = this[name2];
|
|
4836
|
+
}
|
|
4837
|
+
});
|
|
4838
|
+
}
|
|
4839
|
+
cast(key, value) {
|
|
4840
|
+
let vType = this.params[key];
|
|
4841
|
+
if (vType) {
|
|
4842
|
+
if (typeof vType !== "string") {
|
|
4843
|
+
vType = vType.type;
|
|
4844
|
+
}
|
|
4845
|
+
if (vType === "number") {
|
|
4846
|
+
value = Number(value);
|
|
4847
|
+
}
|
|
4848
|
+
}
|
|
4849
|
+
return value;
|
|
4850
|
+
}
|
|
4851
|
+
getMethodFromParams(params) {
|
|
4852
|
+
let method = params?._req?.method?.toLowerCase();
|
|
4853
|
+
if (method === "get" && params.id === void 0) {
|
|
4854
|
+
method = "list";
|
|
4855
|
+
}
|
|
4856
|
+
if (method === "post" && params.act) {
|
|
4857
|
+
method = params.act;
|
|
4858
|
+
}
|
|
4859
|
+
return method;
|
|
4860
|
+
}
|
|
4861
|
+
func(params) {
|
|
4862
|
+
const method = this.getMethodFromParams(params);
|
|
4863
|
+
if (method && typeof this[method] === "function") {
|
|
4864
|
+
if (params.id !== void 0) {
|
|
4865
|
+
params.id = this.cast("id", params.id);
|
|
4866
|
+
}
|
|
4867
|
+
return this[method](params);
|
|
4868
|
+
} else {
|
|
4869
|
+
throw new NotFoundError(method, this.name);
|
|
4870
|
+
}
|
|
4871
|
+
}
|
|
4872
|
+
};
|
|
4873
|
+
var ResServerToolsSchema = {
|
|
4874
|
+
methods: {
|
|
4875
|
+
type: "array"
|
|
4876
|
+
}
|
|
4877
|
+
};
|
|
4878
|
+
ResServerTools.defineProperties(ResServerTools, ResServerToolsSchema);
|
|
4879
|
+
|
|
4880
|
+
// src/funcs/event-server.ts
|
|
4881
|
+
var eventBus = event.runSync();
|
|
4882
|
+
var EventServer = class _EventServer extends ResServerTools {
|
|
4883
|
+
constructor() {
|
|
4884
|
+
super(...arguments);
|
|
4885
|
+
this.name = EventName;
|
|
4886
|
+
this.description = "subscribe server sent event";
|
|
4887
|
+
this.result = "event";
|
|
4888
|
+
this.depends = { [EventBusName]: event };
|
|
4889
|
+
}
|
|
4890
|
+
static get sse() {
|
|
4891
|
+
if (!this._sse) {
|
|
4892
|
+
this._sse = new SSEChannel();
|
|
4893
|
+
}
|
|
4894
|
+
return this._sse;
|
|
4895
|
+
}
|
|
4896
|
+
get sse() {
|
|
4897
|
+
return this.constructor.sse;
|
|
4898
|
+
}
|
|
4899
|
+
static publish(data, event2) {
|
|
4900
|
+
return _EventServer.sse.publish(data, event2);
|
|
4901
|
+
}
|
|
4902
|
+
static {
|
|
4903
|
+
// the local event-bus listener to forward to SSE
|
|
4904
|
+
this.ebListener = function ebListener(...data) {
|
|
4905
|
+
_EventServer.sse.publish(data, this.type);
|
|
4906
|
+
};
|
|
4907
|
+
}
|
|
4908
|
+
static subscribe(req, res, events) {
|
|
4909
|
+
return _EventServer.sse.subscribe(req, res, events);
|
|
4910
|
+
}
|
|
4911
|
+
static alreadyForward(event2) {
|
|
4912
|
+
const listeners = eventBus.listeners(event2);
|
|
4913
|
+
for (const listener of listeners) {
|
|
4914
|
+
if (listener === _EventServer.ebListener) {
|
|
4915
|
+
return true;
|
|
4916
|
+
}
|
|
4917
|
+
}
|
|
4918
|
+
}
|
|
4919
|
+
publishSSE(data, event2) {
|
|
4920
|
+
return this.constructor.publish(data, event2);
|
|
4921
|
+
}
|
|
4922
|
+
subscribeSSE(req, res, events) {
|
|
4923
|
+
if (typeof events === "string") {
|
|
4924
|
+
events = [events];
|
|
4925
|
+
}
|
|
4926
|
+
return this.constructor.subscribe(req, res, events);
|
|
4927
|
+
}
|
|
4928
|
+
// forward the events on the event-bus to client
|
|
4929
|
+
forward(events) {
|
|
4930
|
+
if (!Array.isArray(events)) {
|
|
4931
|
+
events = [events];
|
|
4932
|
+
}
|
|
4933
|
+
for (const event2 of events) {
|
|
4934
|
+
if (!_EventServer.alreadyForward(event2)) {
|
|
4935
|
+
eventBus.on(event2, _EventServer.ebListener);
|
|
4936
|
+
}
|
|
4937
|
+
}
|
|
4938
|
+
}
|
|
4939
|
+
unforward(events) {
|
|
4940
|
+
if (typeof events === "string") {
|
|
4941
|
+
events = [events];
|
|
4942
|
+
}
|
|
4943
|
+
for (const event2 of events) {
|
|
4944
|
+
eventBus.off(event2, _EventServer.ebListener);
|
|
4945
|
+
}
|
|
4946
|
+
}
|
|
4947
|
+
list({ _req, _res, event: event2 }) {
|
|
4948
|
+
if (_req && _res) {
|
|
4949
|
+
this.subscribeSSE(_req, _res, event2);
|
|
4950
|
+
}
|
|
4951
|
+
}
|
|
4952
|
+
$sub({ event: event2 }) {
|
|
4953
|
+
if (event2) {
|
|
4954
|
+
this.forward(event2);
|
|
4955
|
+
return { event: event2 };
|
|
4956
|
+
} else {
|
|
4957
|
+
throwError2("event is required", "sub", 400 /* InvalidArgument */);
|
|
4958
|
+
}
|
|
4959
|
+
}
|
|
4960
|
+
$unsub({ event: event2 }) {
|
|
4961
|
+
if (event2) {
|
|
4962
|
+
this.unforward(event2);
|
|
4963
|
+
return { event: event2 };
|
|
4964
|
+
} else {
|
|
4965
|
+
throwError2("event is required", "unsub", 400 /* InvalidArgument */);
|
|
4966
|
+
}
|
|
4967
|
+
}
|
|
4968
|
+
$publish({ event: events, data }) {
|
|
4969
|
+
if (events && data) {
|
|
4970
|
+
if (typeof events === "string") {
|
|
4971
|
+
events = [events];
|
|
4972
|
+
}
|
|
4973
|
+
for (const event2 of events) {
|
|
4974
|
+
this.publishSSE(data, event2);
|
|
4975
|
+
}
|
|
4976
|
+
return { event: events };
|
|
4977
|
+
} else {
|
|
4978
|
+
throwError2("event or data is required", "pub", 400 /* InvalidArgument */);
|
|
4979
|
+
}
|
|
4980
|
+
}
|
|
4981
|
+
isStream(params) {
|
|
4982
|
+
const method = this.getMethodFromParams(params);
|
|
4983
|
+
return method === "list";
|
|
4984
|
+
}
|
|
4985
|
+
};
|
|
4986
|
+
var eventServer = new EventServer(EventName);
|
|
4987
|
+
|
|
4988
|
+
// src/funcs/index.ts
|
|
4989
|
+
function registerCoreTools() {
|
|
4990
|
+
event.register();
|
|
4991
|
+
lrucache.register();
|
|
4992
|
+
similarity.register();
|
|
4993
|
+
isSimilar.register();
|
|
4994
|
+
splitSegments.register();
|
|
4995
|
+
mergeSegments.register();
|
|
4996
|
+
segments.register();
|
|
4997
|
+
}
|
|
4998
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
4999
|
+
0 && (module.exports = {
|
|
5000
|
+
EventClient,
|
|
5001
|
+
EventServer,
|
|
5002
|
+
EventToolFunc,
|
|
5003
|
+
_lrucache,
|
|
5004
|
+
createLRUCache,
|
|
5005
|
+
event,
|
|
5006
|
+
eventClient,
|
|
5007
|
+
eventServer,
|
|
5008
|
+
getDefaultSimilarPassingScore,
|
|
5009
|
+
isSimilar,
|
|
5010
|
+
lrucache,
|
|
5011
|
+
mergeSegments,
|
|
5012
|
+
registerCoreTools,
|
|
5013
|
+
segments,
|
|
5014
|
+
similarity,
|
|
5015
|
+
splitSegments
|
|
5016
|
+
});
|