tune-sdk 0.1.0
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.md +155 -0
- package/dist/tune.js +2032 -0
- package/dist/tune.mjs +24 -0
- package/package.json +27 -0
package/dist/tune.js
ADDED
|
@@ -0,0 +1,2032 @@
|
|
|
1
|
+
var $roles, fs, assert, util;
|
|
2
|
+
|
|
3
|
+
function extend() {
|
|
4
|
+
var _i;
|
|
5
|
+
var objects = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
|
|
6
|
+
return (function(result) {
|
|
7
|
+
var object, key, value, _i0, _ref, _len, _ref0, _len0;
|
|
8
|
+
_ref = objects;
|
|
9
|
+
for (_i0 = 0, _len = _ref.length; _i0 < _len; ++_i0) {
|
|
10
|
+
object = _ref[_i0];
|
|
11
|
+
_ref0 = object;
|
|
12
|
+
for (key in _ref0) {
|
|
13
|
+
value = _ref0[key];
|
|
14
|
+
if ((typeof value !== 'undefined')) result[key] = JSON.parse(JSON.stringify(value));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
})({});
|
|
19
|
+
}
|
|
20
|
+
extend;
|
|
21
|
+
|
|
22
|
+
function TuneError(message, filename, row, col, stack, originalError) {
|
|
23
|
+
var lastItem;
|
|
24
|
+
var stack;
|
|
25
|
+
stack = stack || [];
|
|
26
|
+
var lastItem;
|
|
27
|
+
lastItem = stack["slice"](-1)[0];
|
|
28
|
+
if ((!filename || ((((typeof lastItem !== "undefined") && (lastItem !== null) && !Number.isNaN(lastItem) && (typeof lastItem.filename !== "undefined") && (lastItem.filename !== null) && !Number.isNaN(lastItem.filename)) ? lastItem.filename : undefined) !== filename))) stack.push({
|
|
29
|
+
filename: filename || "",
|
|
30
|
+
row: row,
|
|
31
|
+
col: col
|
|
32
|
+
});
|
|
33
|
+
this.name = "TuneError";
|
|
34
|
+
this.message = message;
|
|
35
|
+
this.row = row;
|
|
36
|
+
this.col = col;
|
|
37
|
+
this.stack = stack;
|
|
38
|
+
this.error = originalError;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
TuneError;
|
|
42
|
+
TuneError.prototype = Object.create(Error.prototype);
|
|
43
|
+
TuneError.prototype.constructor = TuneError;
|
|
44
|
+
TuneError.wrap = (function(err, filename, row, col) {
|
|
45
|
+
return ((err.name === "TuneError") ? new TuneError(err.message, filename, row, col, err.stack, err.error) : new TuneError("Internal Error", filename, row, col, undefined, err));
|
|
46
|
+
});
|
|
47
|
+
TuneError.ctx2stack = (function(ctx) {
|
|
48
|
+
return ctx.stack.map((function(item) {
|
|
49
|
+
return {
|
|
50
|
+
filename: item.filename,
|
|
51
|
+
row: item.row,
|
|
52
|
+
col: item.col
|
|
53
|
+
}
|
|
54
|
+
}));
|
|
55
|
+
});
|
|
56
|
+
TuneError.prototype.toString = (function() {
|
|
57
|
+
return (this.message + "\n" + (this.stack || [])
|
|
58
|
+
.map((function() {
|
|
59
|
+
return (" at " + arguments[0].filename + (((typeof arguments !== 'undefined') && (typeof arguments[0] !== 'undefined') && (typeof arguments[0].row !== 'undefined')) ? (":" + arguments[0].row) : "") + (((typeof arguments !== 'undefined') && (typeof arguments[0] !== 'undefined') && (typeof arguments[0].col !== 'undefined')) ? (":" + arguments[0].col) : ""));
|
|
60
|
+
}))
|
|
61
|
+
.join("\n") + (this.error ? (this.error.stack ? ("\n" + this.error.stack) : this.error.message) : ""));
|
|
62
|
+
});
|
|
63
|
+
var dJSON = (() => {
|
|
64
|
+
var de = Object.create;
|
|
65
|
+
var N = Object.defineProperty;
|
|
66
|
+
var ye = Object.getOwnPropertyDescriptor;
|
|
67
|
+
var xe = Object.getOwnPropertyNames;
|
|
68
|
+
var Ee = Object.getPrototypeOf,
|
|
69
|
+
ge = Object.prototype.hasOwnProperty;
|
|
70
|
+
var c = (e, r) => () => (r || e((r = {
|
|
71
|
+
exports: {}
|
|
72
|
+
}).exports, r), r.exports),
|
|
73
|
+
be = (e, r) => {
|
|
74
|
+
for (var u in r) N(e, u, {
|
|
75
|
+
get: r[u],
|
|
76
|
+
enumerable: !0
|
|
77
|
+
})
|
|
78
|
+
},
|
|
79
|
+
j = (e, r, u, t) => {
|
|
80
|
+
if (r && typeof r == "object" || typeof r == "function")
|
|
81
|
+
for (let i of xe(r)) !ge.call(e, i) && i !== u && N(e, i, {
|
|
82
|
+
get: () => r[i],
|
|
83
|
+
enumerable: !(t = ye(r, i)) || t.enumerable
|
|
84
|
+
});
|
|
85
|
+
return e
|
|
86
|
+
},
|
|
87
|
+
U = (e, r, u) => (j(e, r, "default"), u && j(u, r, "default")),
|
|
88
|
+
H = (e, r, u) => (u = e != null ? de(Ee(e)) : {}, j(r || !e || !e.__esModule ? N(u, "default", {
|
|
89
|
+
value: e,
|
|
90
|
+
enumerable: !0
|
|
91
|
+
}) : u, e)),
|
|
92
|
+
Fe = e => j(N({}, "__esModule", {
|
|
93
|
+
value: !0
|
|
94
|
+
}), e);
|
|
95
|
+
var P = c((We, D) => {
|
|
96
|
+
typeof D == "object" && typeof D.exports == "object" && (D.exports = Y);
|
|
97
|
+
Y.defunct = function(e) {
|
|
98
|
+
throw new Error("Unexpected character at index " + (this.index - 1) + ": " + e)
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
function Y(e) {
|
|
102
|
+
typeof e != "function" && (e = Y.defunct);
|
|
103
|
+
var r = [],
|
|
104
|
+
u = [],
|
|
105
|
+
t = 0;
|
|
106
|
+
this.state = 0, this.index = 0, this.input = "", this.addRule = function(n, v, x) {
|
|
107
|
+
var y = n.global;
|
|
108
|
+
if (!y) {
|
|
109
|
+
var E = "g";
|
|
110
|
+
n.multiline && (E += "m"), n.ignoreCase && (E += "i"), n = new RegExp(n.source, E)
|
|
111
|
+
}
|
|
112
|
+
return Object.prototype.toString.call(x) !== "[object Array]" && (x = [0]), u.push({
|
|
113
|
+
pattern: n,
|
|
114
|
+
global: y,
|
|
115
|
+
action: v,
|
|
116
|
+
start: x
|
|
117
|
+
}), this
|
|
118
|
+
}, this.setInput = function(n) {
|
|
119
|
+
return t = 0, this.state = 0, this.index = 0, r.length = 0, this.input = n, this
|
|
120
|
+
}, this.lex = function() {
|
|
121
|
+
if (r.length) return r.shift();
|
|
122
|
+
for (this.reject = !0; this.index <= this.input.length;) {
|
|
123
|
+
for (var n = i.call(this).splice(t), v = this.index; n.length && this.reject;) {
|
|
124
|
+
var x = n.shift(),
|
|
125
|
+
y = x.result,
|
|
126
|
+
E = x.length;
|
|
127
|
+
this.index += E, this.reject = !1, t++;
|
|
128
|
+
var f = x.action.apply(this, y);
|
|
129
|
+
if (this.reject) this.index = y.index;
|
|
130
|
+
else if (typeof f < "u") switch (Object.prototype.toString.call(f)) {
|
|
131
|
+
case "[object Array]":
|
|
132
|
+
r = f.slice(1), f = f[0];
|
|
133
|
+
default:
|
|
134
|
+
return E && (t = 0), f
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
var F = this.input;
|
|
138
|
+
if (v < F.length)
|
|
139
|
+
if (this.reject) {
|
|
140
|
+
t = 0;
|
|
141
|
+
var f = e.call(this, F.charAt(this.index++));
|
|
142
|
+
if (typeof f < "u") return Object.prototype.toString.call(f) === "[object Array]" ? (r = f.slice(1), f[0]) : f
|
|
143
|
+
} else this.index !== v && (t = 0), this.reject = !0;
|
|
144
|
+
else if (n.length) this.reject = !0;
|
|
145
|
+
else break
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
function i() {
|
|
150
|
+
for (var n = [], v = 0, x = this.state, y = this.index, E = this.input, f = 0, F = u.length; f < F; f++) {
|
|
151
|
+
var h = u[f],
|
|
152
|
+
T = h.start,
|
|
153
|
+
p = T.length;
|
|
154
|
+
if (!p || T.indexOf(x) >= 0 || x % 2 && p === 1 && !T[0]) {
|
|
155
|
+
var o = h.pattern;
|
|
156
|
+
o.lastIndex = y;
|
|
157
|
+
var s = o.exec(E);
|
|
158
|
+
if (s && s.index === y) {
|
|
159
|
+
var d = n.push({
|
|
160
|
+
result: s,
|
|
161
|
+
action: h.action,
|
|
162
|
+
length: s[0].length
|
|
163
|
+
});
|
|
164
|
+
for (h.global && (v = d); --d > v;) {
|
|
165
|
+
var a = d - 1;
|
|
166
|
+
if (n[d].length > n[a].length) {
|
|
167
|
+
var _ = n[d];
|
|
168
|
+
n[d] = n[a], n[a] = _
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return n
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
var k = c(() => {
|
|
179
|
+
String.fromCodePoint || function() {
|
|
180
|
+
var e = function() {
|
|
181
|
+
try {
|
|
182
|
+
var i = {},
|
|
183
|
+
n = Object.defineProperty,
|
|
184
|
+
v = n(i, i, i) && n
|
|
185
|
+
} catch {}
|
|
186
|
+
return v
|
|
187
|
+
}(),
|
|
188
|
+
r = String.fromCharCode,
|
|
189
|
+
u = Math.floor,
|
|
190
|
+
t = function(i) {
|
|
191
|
+
var n = 16384,
|
|
192
|
+
v = [],
|
|
193
|
+
x, y, E = -1,
|
|
194
|
+
f = arguments.length;
|
|
195
|
+
if (!f) return "";
|
|
196
|
+
for (var F = ""; ++E < f;) {
|
|
197
|
+
var h = Number(arguments[E]);
|
|
198
|
+
if (!isFinite(h) || h < 0 || h > 1114111 || u(h) != h) throw RangeError("Invalid code point: " + h);
|
|
199
|
+
h <= 65535 ? v.push(h) : (h -= 65536, x = (h >> 10) + 55296, y = h % 1024 + 56320, v.push(x, y)), (E + 1 == f || v.length > n) && (F += r.apply(null, v), v.length = 0)
|
|
200
|
+
}
|
|
201
|
+
return F
|
|
202
|
+
};
|
|
203
|
+
e ? e(String, "fromCodePoint", {
|
|
204
|
+
value: t,
|
|
205
|
+
configurable: !0,
|
|
206
|
+
writable: !0
|
|
207
|
+
}) : String.fromCodePoint = t
|
|
208
|
+
}()
|
|
209
|
+
});
|
|
210
|
+
var re = c((V, ee) => {
|
|
211
|
+
"use strict";
|
|
212
|
+
Object.defineProperty(V, "__esModule", {
|
|
213
|
+
value: !0
|
|
214
|
+
});
|
|
215
|
+
V.default = void 0;
|
|
216
|
+
k();
|
|
217
|
+
var Le = /\\(u\{([0-9A-Fa-f]+)\}|u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([1-7][0-7]{0,2}|[0-7]{2,3})|(['"tbrnfv0\\]))|\\U([0-9A-Fa-f]{8})/g,
|
|
218
|
+
Re = {
|
|
219
|
+
0: "\0",
|
|
220
|
+
b: "\b",
|
|
221
|
+
f: "\f",
|
|
222
|
+
n: `
|
|
223
|
+
`,
|
|
224
|
+
r: "\r",
|
|
225
|
+
t: " ",
|
|
226
|
+
v: "\v",
|
|
227
|
+
"'": "'",
|
|
228
|
+
'"': '"',
|
|
229
|
+
"\\": "\\"
|
|
230
|
+
},
|
|
231
|
+
K = function(r) {
|
|
232
|
+
return String.fromCodePoint(parseInt(r, 16))
|
|
233
|
+
},
|
|
234
|
+
we = function(r) {
|
|
235
|
+
return String.fromCodePoint(parseInt(r, 8))
|
|
236
|
+
},
|
|
237
|
+
_e = function(r) {
|
|
238
|
+
return r.replace(Le, function(u, t, i, n, v, x, y, E) {
|
|
239
|
+
return i !== void 0 ? K(i) : n !== void 0 ? K(n) : v !== void 0 ? K(v) : x !== void 0 ? we(x) : E !== void 0 ? K(E) : Re[y]
|
|
240
|
+
})
|
|
241
|
+
};
|
|
242
|
+
V.default = _e;
|
|
243
|
+
ee.exports = V.default
|
|
244
|
+
});
|
|
245
|
+
var ue = c(J => {
|
|
246
|
+
(function(e) {
|
|
247
|
+
var r = String.fromCharCode;
|
|
248
|
+
|
|
249
|
+
function u(p) {
|
|
250
|
+
for (var o = [], s = 0, d = p.length, a, _; s < d;) a = p.charCodeAt(s++), a >= 55296 && a <= 56319 && s < d ? (_ = p.charCodeAt(s++), (_ & 64512) == 56320 ? o.push(((a & 1023) << 10) + (_ & 1023) + 65536) : (o.push(a), s--)) : o.push(a);
|
|
251
|
+
return o
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function t(p) {
|
|
255
|
+
for (var o = p.length, s = -1, d, a = ""; ++s < o;) d = p[s], d > 65535 && (d -= 65536, a += r(d >>> 10 & 1023 | 55296), d = 56320 | d & 1023), a += r(d);
|
|
256
|
+
return a
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function i(p) {
|
|
260
|
+
if (p >= 55296 && p <= 57343) throw Error("Lone surrogate U+" + p.toString(16).toUpperCase() + " is not a scalar value")
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function n(p, o) {
|
|
264
|
+
return r(p >> o & 63 | 128)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function v(p) {
|
|
268
|
+
if (!(p & 4294967168)) return r(p);
|
|
269
|
+
var o = "";
|
|
270
|
+
return p & 4294965248 ? p & 4294901760 ? p & 4292870144 || (o = r(p >> 18 & 7 | 240), o += n(p, 12), o += n(p, 6)) : (i(p), o = r(p >> 12 & 15 | 224), o += n(p, 6)) : o = r(p >> 6 & 31 | 192), o += r(p & 63 | 128), o
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function x(p) {
|
|
274
|
+
for (var o = u(p), s = o.length, d = -1, a, _ = ""; ++d < s;) a = o[d], _ += v(a);
|
|
275
|
+
return _
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function y() {
|
|
279
|
+
if (h >= F) throw Error("Invalid byte index");
|
|
280
|
+
var p = f[h] & 255;
|
|
281
|
+
if (h++, (p & 192) == 128) return p & 63;
|
|
282
|
+
throw Error("Invalid continuation byte")
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function E() {
|
|
286
|
+
var p, o, s, d, a;
|
|
287
|
+
if (h > F) throw Error("Invalid byte index");
|
|
288
|
+
if (h == F) return !1;
|
|
289
|
+
if (p = f[h] & 255, h++, !(p & 128)) return p;
|
|
290
|
+
if ((p & 224) == 192) {
|
|
291
|
+
if (o = y(), a = (p & 31) << 6 | o, a >= 128) return a;
|
|
292
|
+
throw Error("Invalid continuation byte")
|
|
293
|
+
}
|
|
294
|
+
if ((p & 240) == 224) {
|
|
295
|
+
if (o = y(), s = y(), a = (p & 15) << 12 | o << 6 | s, a >= 2048) return i(a), a;
|
|
296
|
+
throw Error("Invalid continuation byte")
|
|
297
|
+
}
|
|
298
|
+
if ((p & 248) == 240 && (o = y(), s = y(), d = y(), a = (p & 7) << 18 | o << 12 | s << 6 | d, a >= 65536 && a <= 1114111)) return a;
|
|
299
|
+
throw Error("Invalid UTF-8 detected")
|
|
300
|
+
}
|
|
301
|
+
var f, F, h;
|
|
302
|
+
|
|
303
|
+
function T(p) {
|
|
304
|
+
f = u(p), F = f.length, h = 0;
|
|
305
|
+
for (var o = [], s;
|
|
306
|
+
(s = E()) !== !1;) o.push(s);
|
|
307
|
+
return t(o)
|
|
308
|
+
}
|
|
309
|
+
e.version = "3.0.0", e.encode = x, e.decode = T
|
|
310
|
+
})(typeof J > "u" ? J.utf8 = {} : J)
|
|
311
|
+
});
|
|
312
|
+
var pe = c((Pe, G) => {
|
|
313
|
+
"use strict";
|
|
314
|
+
var Ce = P(),
|
|
315
|
+
Se = re(),
|
|
316
|
+
He = ue(),
|
|
317
|
+
te = 6,
|
|
318
|
+
ce = 7,
|
|
319
|
+
le = 11,
|
|
320
|
+
Xe = 12,
|
|
321
|
+
Oe = 13,
|
|
322
|
+
me = 14,
|
|
323
|
+
Ae = -1,
|
|
324
|
+
Ie = -2,
|
|
325
|
+
Te = -3,
|
|
326
|
+
Ve = -4,
|
|
327
|
+
Be = -5,
|
|
328
|
+
je = [
|
|
329
|
+
[/\s*:\s*/, Ae],
|
|
330
|
+
[/\s*,\s*/, Ie],
|
|
331
|
+
[/\s*{\s*/, Te],
|
|
332
|
+
[/\s*}\s*/, Oe],
|
|
333
|
+
[/\s*\[\s*/, Ve],
|
|
334
|
+
[/\s*\]\s*/, Xe],
|
|
335
|
+
[/\s*\.\s*/, Be]
|
|
336
|
+
];
|
|
337
|
+
|
|
338
|
+
function ne(e) {
|
|
339
|
+
return e = e.replace(/\\\//, "/"), Se(e)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function Ne(e) {
|
|
343
|
+
let r = new Ce,
|
|
344
|
+
u = 0,
|
|
345
|
+
t = 0;
|
|
346
|
+
return r.addRule(/"((?:\\.|[^"])*?)($|")/, (i, n) => (u += i.length, {
|
|
347
|
+
type: le,
|
|
348
|
+
value: ne(n),
|
|
349
|
+
row: t,
|
|
350
|
+
col: u,
|
|
351
|
+
single: !1
|
|
352
|
+
})), r.addRule(/'((?:\\.|[^'])*?)($|'|(",?[ \t]*\n))/, (i, n) => (u += i.length, {
|
|
353
|
+
type: le,
|
|
354
|
+
value: ne(n),
|
|
355
|
+
row: t,
|
|
356
|
+
col: u,
|
|
357
|
+
single: !0
|
|
358
|
+
})), r.addRule(/[\-0-9]*\.[0-9]*([eE][\+\-]?)?[0-9]*(?:\s*)/, i => (u += i.length, {
|
|
359
|
+
type: te,
|
|
360
|
+
value: parseFloat(i),
|
|
361
|
+
row: t,
|
|
362
|
+
col: u
|
|
363
|
+
})), r.addRule(/\-?[0-9]+([eE][\+\-]?)[0-9]*(?:\s*)/, i => (u += i.length, {
|
|
364
|
+
type: te,
|
|
365
|
+
value: parseFloat(i),
|
|
366
|
+
row: t,
|
|
367
|
+
col: u
|
|
368
|
+
})), r.addRule(/\-?[0-9]+(?:\s*)/, i => (u += i.length, {
|
|
369
|
+
type: ce,
|
|
370
|
+
value: parseInt(i),
|
|
371
|
+
row: t,
|
|
372
|
+
col: u
|
|
373
|
+
})), je.forEach(i => {
|
|
374
|
+
r.addRule(i[0], n => (u += n.length, {
|
|
375
|
+
type: i[1],
|
|
376
|
+
value: n,
|
|
377
|
+
row: t,
|
|
378
|
+
col: u
|
|
379
|
+
}))
|
|
380
|
+
}), r.addRule(/\s/, i => {
|
|
381
|
+
i == `
|
|
382
|
+
` ? (u = 0, t++) : u += i.length
|
|
383
|
+
}), r.addRule(/\S[ \t]*/, i => (u += i.length, {
|
|
384
|
+
type: me,
|
|
385
|
+
value: i,
|
|
386
|
+
row: t,
|
|
387
|
+
col: u
|
|
388
|
+
})), r.setInput(e), r
|
|
389
|
+
}
|
|
390
|
+
G.exports.lexString = ie;
|
|
391
|
+
|
|
392
|
+
function ie(e, r) {
|
|
393
|
+
let u = Ne(e),
|
|
394
|
+
t = "";
|
|
395
|
+
for (; t = u.lex();) r(t)
|
|
396
|
+
}
|
|
397
|
+
G.exports.getAllTokens = Ue;
|
|
398
|
+
|
|
399
|
+
function Ue(e) {
|
|
400
|
+
let r = [];
|
|
401
|
+
return ie(e, function(t) {
|
|
402
|
+
r.push(t)
|
|
403
|
+
}), r
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
var he = c((ke, fe) => {
|
|
407
|
+
"use strict";
|
|
408
|
+
var De = pe(),
|
|
409
|
+
W = 0,
|
|
410
|
+
X = 1,
|
|
411
|
+
C = 2,
|
|
412
|
+
Z = 3,
|
|
413
|
+
M = 4,
|
|
414
|
+
q = 5,
|
|
415
|
+
Ke = 6,
|
|
416
|
+
oe = 7,
|
|
417
|
+
b = 8,
|
|
418
|
+
R = 9,
|
|
419
|
+
S = 10,
|
|
420
|
+
Je = 11,
|
|
421
|
+
A = 12,
|
|
422
|
+
I = 13,
|
|
423
|
+
Me = 14,
|
|
424
|
+
g = 15,
|
|
425
|
+
m = -1,
|
|
426
|
+
L = -2,
|
|
427
|
+
$ = -3,
|
|
428
|
+
O = -4;
|
|
429
|
+
|
|
430
|
+
function ae(e) {
|
|
431
|
+
e.peek == null && Object.defineProperty(e, "peek", {
|
|
432
|
+
enumerable: !1,
|
|
433
|
+
value: function() {
|
|
434
|
+
return this[this.length - 1]
|
|
435
|
+
}
|
|
436
|
+
}), e.last == null && Object.defineProperty(e, "last", {
|
|
437
|
+
enumerable: !1,
|
|
438
|
+
value: function(r) {
|
|
439
|
+
return this[this.length - (1 + r)]
|
|
440
|
+
}
|
|
441
|
+
})
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function l(e, r) {
|
|
445
|
+
return e && e.hasOwnProperty("type") && e.type == r
|
|
446
|
+
}
|
|
447
|
+
fe.exports.parse = qe;
|
|
448
|
+
|
|
449
|
+
function qe(e, r) {
|
|
450
|
+
let u = [],
|
|
451
|
+
t = [];
|
|
452
|
+
ae(u), ae(t);
|
|
453
|
+
let i = function(n) {
|
|
454
|
+
t.push(n)
|
|
455
|
+
};
|
|
456
|
+
De.lexString(e, i), t[0].type == O && t.last(0).type != A && t.push({
|
|
457
|
+
type: A,
|
|
458
|
+
value: "]",
|
|
459
|
+
row: -1,
|
|
460
|
+
col: -1
|
|
461
|
+
}), t[0].type == $ && t.last(0).type != I && t.push({
|
|
462
|
+
type: I,
|
|
463
|
+
value: "}",
|
|
464
|
+
row: -1,
|
|
465
|
+
col: -1
|
|
466
|
+
});
|
|
467
|
+
for (let n = 0; n < t.length; n++)
|
|
468
|
+
for ("" + t[n].type, u.push(t[n]); w(u););
|
|
469
|
+
return u.length == 1 && u[0].type == X && (u = [{
|
|
470
|
+
type: S,
|
|
471
|
+
value: u[0].value
|
|
472
|
+
}]), Q(u[0], r)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function w(e) {
|
|
476
|
+
let r = e.pop();
|
|
477
|
+
switch (r.type) {
|
|
478
|
+
case b:
|
|
479
|
+
if (r.value.trim() == "true") return e.push({
|
|
480
|
+
type: Z,
|
|
481
|
+
value: "true"
|
|
482
|
+
}), !0;
|
|
483
|
+
if (r.value.trim() == "false") return e.push({
|
|
484
|
+
type: Z,
|
|
485
|
+
value: "false"
|
|
486
|
+
}), !0;
|
|
487
|
+
if (r.value.trim() == "null") return e.push({
|
|
488
|
+
type: g,
|
|
489
|
+
value: null
|
|
490
|
+
}), !0;
|
|
491
|
+
break;
|
|
492
|
+
case Me:
|
|
493
|
+
return l(e.peek(), b) ? (e.peek().value += r.value, !0) : (e.push({
|
|
494
|
+
type: b,
|
|
495
|
+
value: r.value
|
|
496
|
+
}), !0);
|
|
497
|
+
case oe:
|
|
498
|
+
return l(r, oe) && l(e.peek(), b) ? (e.peek().value += r.value, !0) : (r.type = g, e.push(r), !0);
|
|
499
|
+
case Je:
|
|
500
|
+
return r.type = g, r.value = r.value, e.push(r), !0;
|
|
501
|
+
case Z:
|
|
502
|
+
return r.type = g, r.value == "true" ? r.value = !0 : r.value = !1, e.push(r), !0;
|
|
503
|
+
case Ke:
|
|
504
|
+
return r.type = g, e.push(r), !0;
|
|
505
|
+
case g:
|
|
506
|
+
if (l(e.peek(), L)) return r.type = q, e.pop(), e.push(r), !0;
|
|
507
|
+
if (l(e.peek(), m)) return r.type = M, e.pop(), e.push(r), !0;
|
|
508
|
+
if (l(e.peek(), b) && l(e.last(1), g)) {
|
|
509
|
+
let u = e.pop();
|
|
510
|
+
return e.peek().value += '"' + u.value + '"', e.peek().value += r.value, !0
|
|
511
|
+
}
|
|
512
|
+
if (l(e.peek(), b) && l(e.last(1), C)) {
|
|
513
|
+
let u = e.pop(),
|
|
514
|
+
t = e.peek().value.pop();
|
|
515
|
+
return t += '"' + u.value + '"', t += r.value, e.peek().value.push(t), !0
|
|
516
|
+
}
|
|
517
|
+
if (l(e.peek(), b) && l(e.last(1), X)) {
|
|
518
|
+
let u = e.pop(),
|
|
519
|
+
t = e.peek().value.pop(),
|
|
520
|
+
i = r.single ? "'" : '"';
|
|
521
|
+
return t.value += i + u.value + i, t.value += r.value, e.peek().value.push(t), !0
|
|
522
|
+
}
|
|
523
|
+
if (l(e.peek(), b)) {
|
|
524
|
+
let u = e.pop().value;
|
|
525
|
+
return r.value = u + r.value, e.push(r), !0
|
|
526
|
+
}
|
|
527
|
+
break;
|
|
528
|
+
case R:
|
|
529
|
+
if (l(r, R) && l(e.peek(), L)) return r.type = q, e.pop(), e.push(r), !0;
|
|
530
|
+
if (l(e.peek(), m)) return r.type = M, e.pop(), e.push(r), !0;
|
|
531
|
+
break;
|
|
532
|
+
case S:
|
|
533
|
+
if (l(e.peek(), L)) {
|
|
534
|
+
let u = {
|
|
535
|
+
type: q,
|
|
536
|
+
value: r
|
|
537
|
+
};
|
|
538
|
+
return e.pop(), e.push(u), !0
|
|
539
|
+
}
|
|
540
|
+
if (l(e.peek(), m)) {
|
|
541
|
+
let u = {
|
|
542
|
+
type: M,
|
|
543
|
+
value: r
|
|
544
|
+
};
|
|
545
|
+
return e.pop(), e.push(u), !0
|
|
546
|
+
}
|
|
547
|
+
if (l(e.peek(), b)) {
|
|
548
|
+
let u = e.pop();
|
|
549
|
+
return e.push({
|
|
550
|
+
type: W,
|
|
551
|
+
key: u.value.trim(),
|
|
552
|
+
value: r
|
|
553
|
+
}), !0
|
|
554
|
+
}
|
|
555
|
+
break;
|
|
556
|
+
case q:
|
|
557
|
+
return l(e.peek(), C) ? (e.peek().value.push(r.value), !0) : (e.push({
|
|
558
|
+
type: C,
|
|
559
|
+
value: [r.value]
|
|
560
|
+
}), !0);
|
|
561
|
+
case C:
|
|
562
|
+
if (l(e.peek(), g)) return r.value.unshift(e.peek().value), e.pop(), e.push(r), !0;
|
|
563
|
+
if (l(e.peek(), R)) return r.value.unshift(e.peek().value), e.pop(), e.push(r), !0;
|
|
564
|
+
if (l(e.peek(), S)) return r.value.unshift(e.peek()), e.pop(), e.push(r), !0;
|
|
565
|
+
if (l(e.peek(), b) && (e.last(1), L)) {
|
|
566
|
+
let u = e.pop();
|
|
567
|
+
for (e.push({
|
|
568
|
+
type: g,
|
|
569
|
+
value: u.value
|
|
570
|
+
}), "" + u.value; w(e););
|
|
571
|
+
return e.push(r), !0
|
|
572
|
+
}
|
|
573
|
+
if (l(e.peek(), C)) return e.peek().value.push(r.value[0]), !0;
|
|
574
|
+
break;
|
|
575
|
+
case M:
|
|
576
|
+
if (l(e.peek(), b) || l(e.peek(), g) || l(e.peek(), C)) {
|
|
577
|
+
let u = e.pop();
|
|
578
|
+
return e.push({
|
|
579
|
+
type: W,
|
|
580
|
+
key: u.value,
|
|
581
|
+
value: r.value
|
|
582
|
+
}), !0
|
|
583
|
+
}
|
|
584
|
+
throw new Error("Got a :value that can't be handled at line " + r.row + ":" + r.col);
|
|
585
|
+
case W:
|
|
586
|
+
return l(e.last(0), L) && l(e.last(1), X) ? (e.last(1).value.push(r), e.pop(), !0) : (e.push({
|
|
587
|
+
type: X,
|
|
588
|
+
value: [r]
|
|
589
|
+
}), !0);
|
|
590
|
+
case X:
|
|
591
|
+
if (l(e.peek(), X)) return r.value.forEach(function(u) {
|
|
592
|
+
e.peek().value.push(u)
|
|
593
|
+
}), !0;
|
|
594
|
+
break;
|
|
595
|
+
case A:
|
|
596
|
+
if (l(e.peek(), C) && l(e.last(1), O)) {
|
|
597
|
+
let u = e.pop();
|
|
598
|
+
return e.pop(), e.push({
|
|
599
|
+
type: R,
|
|
600
|
+
value: u.value
|
|
601
|
+
}), !0
|
|
602
|
+
}
|
|
603
|
+
if (l(e.peek(), R) && l(e.last(1), O)) {
|
|
604
|
+
let u = e.pop();
|
|
605
|
+
return e.pop(), e.push({
|
|
606
|
+
type: R,
|
|
607
|
+
value: [u.value]
|
|
608
|
+
}), !0
|
|
609
|
+
}
|
|
610
|
+
if (l(e.peek(), O)) return e.pop(), e.push({
|
|
611
|
+
type: R,
|
|
612
|
+
value: []
|
|
613
|
+
}), !0;
|
|
614
|
+
if (l(e.peek(), g) && l(e.last(1), O)) {
|
|
615
|
+
let u = e.pop().value;
|
|
616
|
+
return e.pop(), e.push({
|
|
617
|
+
type: R,
|
|
618
|
+
value: [u]
|
|
619
|
+
}), !0
|
|
620
|
+
}
|
|
621
|
+
if (l(e.peek(), S) && l(e.last(1), O)) {
|
|
622
|
+
let u = e.pop();
|
|
623
|
+
return e.pop(), e.push({
|
|
624
|
+
type: R,
|
|
625
|
+
value: [u]
|
|
626
|
+
}), !0
|
|
627
|
+
}
|
|
628
|
+
if (l(e.peek(), b) && l(e.last(1), L)) {
|
|
629
|
+
let u = e.pop();
|
|
630
|
+
for (e.push({
|
|
631
|
+
type: g,
|
|
632
|
+
value: u.value
|
|
633
|
+
}), "" + u.value; w(e););
|
|
634
|
+
return e.push({
|
|
635
|
+
type: A
|
|
636
|
+
}), !0
|
|
637
|
+
}
|
|
638
|
+
if (l(e.peek(), L) && (l(e.last(1), b) || l(e.last(1), S) || l(e.last(1), g))) {
|
|
639
|
+
for (e.pop(), e.push({
|
|
640
|
+
type: A,
|
|
641
|
+
value: "]"
|
|
642
|
+
}), "" + JSON.stringify(e); w(e););
|
|
643
|
+
return !0
|
|
644
|
+
}
|
|
645
|
+
if (l(e.peek(), b) && l(e.last(1), O)) {
|
|
646
|
+
let u = e.pop();
|
|
647
|
+
return e.pop(), e.push({
|
|
648
|
+
type: R,
|
|
649
|
+
value: [u.value]
|
|
650
|
+
}), !0
|
|
651
|
+
}
|
|
652
|
+
if (l(e.peek(), L) && l(e.last(1), C)) {
|
|
653
|
+
for (e.pop(), e.push({
|
|
654
|
+
type: A
|
|
655
|
+
}), "" + JSON.stringify(e); w(e););
|
|
656
|
+
return !0
|
|
657
|
+
}
|
|
658
|
+
break;
|
|
659
|
+
case I:
|
|
660
|
+
if (l(e.peek(), X) && l(e.last(1), $)) {
|
|
661
|
+
let u = e.pop();
|
|
662
|
+
return e.pop(), e.push({
|
|
663
|
+
type: S,
|
|
664
|
+
value: u.value
|
|
665
|
+
}), !0
|
|
666
|
+
}
|
|
667
|
+
if (l(e.peek(), $)) return e.pop(), e.push({
|
|
668
|
+
type: S,
|
|
669
|
+
value: null
|
|
670
|
+
}), !0;
|
|
671
|
+
if (l(e.peek(), b) && l(e.last(1), m)) {
|
|
672
|
+
let u = e.pop();
|
|
673
|
+
for (e.push({
|
|
674
|
+
type: g,
|
|
675
|
+
value: u.value
|
|
676
|
+
}), "" + u.value; w(e););
|
|
677
|
+
return e.push({
|
|
678
|
+
type: I
|
|
679
|
+
}), !0
|
|
680
|
+
}
|
|
681
|
+
if (l(e.peek(), m)) {
|
|
682
|
+
for (e.push({
|
|
683
|
+
type: g,
|
|
684
|
+
value: null
|
|
685
|
+
}); w(e););
|
|
686
|
+
return e.push({
|
|
687
|
+
type: I
|
|
688
|
+
}), !0
|
|
689
|
+
}
|
|
690
|
+
if (l(e.peek(), L)) return e.pop(), e.push({
|
|
691
|
+
type: I
|
|
692
|
+
}), !0;
|
|
693
|
+
throw new Error("Found } that I can't handle at line " + r.row + ":" + r.col);
|
|
694
|
+
case L:
|
|
695
|
+
if (l(e.peek(), L)) return !0;
|
|
696
|
+
if (l(e.peek(), b)) {
|
|
697
|
+
let u = e.pop();
|
|
698
|
+
for (e.push({
|
|
699
|
+
type: g,
|
|
700
|
+
value: u.value
|
|
701
|
+
}); w(e););
|
|
702
|
+
return e.push(r), !0
|
|
703
|
+
}
|
|
704
|
+
if (l(e.peek(), m)) {
|
|
705
|
+
for (e.push({
|
|
706
|
+
type: g,
|
|
707
|
+
value: null
|
|
708
|
+
}); w(e););
|
|
709
|
+
return e.push(r), !0
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return e.push(r), !1
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function Q(e, r) {
|
|
716
|
+
if (["boolean", "number", "string"].indexOf(typeof e) != -1) return e;
|
|
717
|
+
if (e === null) return null;
|
|
718
|
+
if (Array.isArray(e)) {
|
|
719
|
+
let t = [];
|
|
720
|
+
for (; e.length > 0;) t.unshift(Q(e.pop()));
|
|
721
|
+
return t
|
|
722
|
+
}
|
|
723
|
+
if (l(e, S)) {
|
|
724
|
+
let t = {};
|
|
725
|
+
return e.value === null ? {} : (e.value.forEach(function(i) {
|
|
726
|
+
let n = i.key,
|
|
727
|
+
v = Q(i.value);
|
|
728
|
+
r && n in t ? t[n] = {
|
|
729
|
+
value: t[n],
|
|
730
|
+
next: v
|
|
731
|
+
} : t[n] = v
|
|
732
|
+
}), t)
|
|
733
|
+
}
|
|
734
|
+
return l(e, R) ? Q(e.value) : e.value
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
var z = c((er, ve) => {
|
|
738
|
+
"use strict";
|
|
739
|
+
var Qe = he();
|
|
740
|
+
ve.exports.parse = Ye;
|
|
741
|
+
|
|
742
|
+
function Ye(e, r) {
|
|
743
|
+
let u = !0,
|
|
744
|
+
t = !1;
|
|
745
|
+
r && ("fallback" in r && r[u] === !1 && (u = !1), t = "duplicateKeys" in r && r.duplicateKeys === !0);
|
|
746
|
+
try {
|
|
747
|
+
return Qe.parse(e, t)
|
|
748
|
+
} catch (i) {
|
|
749
|
+
if (u === !1) throw i;
|
|
750
|
+
try {
|
|
751
|
+
let n = JSON.parse(e);
|
|
752
|
+
return console.warn("dirty-json got valid JSON that failed with the custom parser. We're returning the valid JSON, but please file a bug report here: https://github.com/RyanMarcus/dirty-json/issues -- the JSON that caused the failure was: " + e), n
|
|
753
|
+
} catch {
|
|
754
|
+
throw i
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
var B = {};
|
|
760
|
+
be(B, {
|
|
761
|
+
default: () => se.default
|
|
762
|
+
});
|
|
763
|
+
U(B, H(z()));
|
|
764
|
+
var se = H(z());
|
|
765
|
+
return Fe(B);
|
|
766
|
+
})();
|
|
767
|
+
/*! http://mths.be/fromcodepoint v0.2.1 by @mathias */
|
|
768
|
+
/*! https://mths.be/utf8js v3.0.0 by @mathias */
|
|
769
|
+
;
|
|
770
|
+
|
|
771
|
+
function Context() {
|
|
772
|
+
var _i;
|
|
773
|
+
var args = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
|
|
774
|
+
this.ms = args.filter((function(item) {
|
|
775
|
+
return (item.name !== "write");
|
|
776
|
+
}));
|
|
777
|
+
this.ws = args.filter((function(item) {
|
|
778
|
+
return (item.name === "write");
|
|
779
|
+
}));
|
|
780
|
+
this.stack = [];
|
|
781
|
+
return this;
|
|
782
|
+
}
|
|
783
|
+
Context;
|
|
784
|
+
Context.prototype.clone = (function() {
|
|
785
|
+
var res;
|
|
786
|
+
var res;
|
|
787
|
+
res = new Context();
|
|
788
|
+
res.ms = this.ms.concat([]);
|
|
789
|
+
res.ws = this.ws.concat([]);
|
|
790
|
+
res.stack = (this.stack || [])
|
|
791
|
+
.concat([]);
|
|
792
|
+
return res;
|
|
793
|
+
});
|
|
794
|
+
Context.prototype.use = (function(middleware) {
|
|
795
|
+
return ((middleware.name === "write") ? this.ws.push(middleware) : this.ms.push(middleware));
|
|
796
|
+
});
|
|
797
|
+
Context.prototype.resolve = (async function(name, args, ms) {
|
|
798
|
+
ms = ms || this.ms;
|
|
799
|
+
if (!ms.length) return;
|
|
800
|
+
return ms[0](name, this, args, this.resolve.bind(this, name, args, ms.slice(1)));
|
|
801
|
+
});
|
|
802
|
+
Context.prototype.read = (async function(name, args) {
|
|
803
|
+
var resolved, _ref;
|
|
804
|
+
var resolved;
|
|
805
|
+
resolved = await this.resolve(name);
|
|
806
|
+
if ((resolved && resolved.read)) {
|
|
807
|
+
return _ref = await resolved.read(args);
|
|
808
|
+
} else {
|
|
809
|
+
_ref = undefined;
|
|
810
|
+
}
|
|
811
|
+
return _ref;
|
|
812
|
+
});
|
|
813
|
+
Context.prototype.exec = (async function(name, args) {
|
|
814
|
+
var resolved, _ref;
|
|
815
|
+
var resolved;
|
|
816
|
+
resolved = await this.resolve(name);
|
|
817
|
+
if ((resolved && resolved.exec)) {
|
|
818
|
+
return _ref = await resolved.exec(args);
|
|
819
|
+
} else {
|
|
820
|
+
_ref = undefined;
|
|
821
|
+
}
|
|
822
|
+
return _ref;
|
|
823
|
+
});
|
|
824
|
+
Context.prototype.write = (async function(name, args, ws) {
|
|
825
|
+
ws = ws || this.ws;
|
|
826
|
+
if (!ws.length) return;
|
|
827
|
+
return ws[0](name, args, this, this.write.bind(this, name, args, ws.slice(1)));
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
function envmd(md) {
|
|
831
|
+
var lmd;
|
|
832
|
+
var lmd;
|
|
833
|
+
lmd = {};
|
|
834
|
+
Object.keys(md)
|
|
835
|
+
.forEach((function(name) {
|
|
836
|
+
var _ref;
|
|
837
|
+
if ((typeof md[name] === "string" || typeof md[name] === "number" || typeof md[name] === "boolean")) {
|
|
838
|
+
_ref = (lmd[name] = {
|
|
839
|
+
type: "text",
|
|
840
|
+
read: (function() {
|
|
841
|
+
return md[name];
|
|
842
|
+
})
|
|
843
|
+
});
|
|
844
|
+
} else if (typeof md[name] === "object") {
|
|
845
|
+
_ref = (lmd[name] = md[name]);
|
|
846
|
+
} else {
|
|
847
|
+
_ref = undefined;
|
|
848
|
+
throw new TuneError(("unsupported type of value '" + name + "': " + util.inspect(md)));
|
|
849
|
+
}
|
|
850
|
+
return _ref;
|
|
851
|
+
}));
|
|
852
|
+
return (async function(name, ctx, args, next) {
|
|
853
|
+
var llmd, key, val, firstKey, _ref, _len, _ref0;
|
|
854
|
+
var llmd;
|
|
855
|
+
llmd = {};
|
|
856
|
+
_ref = lmd;
|
|
857
|
+
for (key in _ref) {
|
|
858
|
+
val = _ref[key];
|
|
859
|
+
if ((args && (args === val.type))) {
|
|
860
|
+
llmd[key] = val;
|
|
861
|
+
} else if (!args) {
|
|
862
|
+
llmd[key] = val;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
var firstKey;
|
|
866
|
+
firstKey = Object.keys(llmd)[0];
|
|
867
|
+
if ((firstKey && (name === "*"))) {
|
|
868
|
+
_ref0 = llmd[firstKey];
|
|
869
|
+
} else if (llmd.hasOwnProperty(name)) {
|
|
870
|
+
_ref0 = llmd[name];
|
|
871
|
+
} else {
|
|
872
|
+
_ref0 = next();
|
|
873
|
+
}
|
|
874
|
+
return _ref0;
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
envmd;
|
|
878
|
+
|
|
879
|
+
function makeContext() {
|
|
880
|
+
var ctx, _i;
|
|
881
|
+
var args = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
|
|
882
|
+
if ((args[0] instanceof Context)) return args[0];
|
|
883
|
+
var ctx;
|
|
884
|
+
ctx = new Context();
|
|
885
|
+
if (!args[0]) return ctx;
|
|
886
|
+
args.forEach((function(md) {
|
|
887
|
+
var _ref;
|
|
888
|
+
if ((typeof md === "function")) {
|
|
889
|
+
_ref = ctx.use(md);
|
|
890
|
+
} else if (typeof md === "object") {
|
|
891
|
+
_ref = ctx.use(envmd(md));
|
|
892
|
+
} else {
|
|
893
|
+
_ref = undefined;
|
|
894
|
+
throw new TuneError("context middlewares might be either function or an object");
|
|
895
|
+
}
|
|
896
|
+
return _ref;
|
|
897
|
+
}));
|
|
898
|
+
return ctx;
|
|
899
|
+
}
|
|
900
|
+
makeContext;
|
|
901
|
+
|
|
902
|
+
function getPos(offset, str) {
|
|
903
|
+
return (function(it) {
|
|
904
|
+
it = it.split("\n");
|
|
905
|
+
it = Array(it.length - 1, it["slice"](-1)[0].length);
|
|
906
|
+
return it;
|
|
907
|
+
})(str.substr(0, offset));
|
|
908
|
+
}
|
|
909
|
+
getPos;
|
|
910
|
+
$roles = {
|
|
911
|
+
short2long: {
|
|
912
|
+
"s": "system",
|
|
913
|
+
"u": "user",
|
|
914
|
+
"a": "assistant",
|
|
915
|
+
"au": "audio",
|
|
916
|
+
"tc": "tool_call",
|
|
917
|
+
"v": "variable",
|
|
918
|
+
"err": "error",
|
|
919
|
+
"c": "comment",
|
|
920
|
+
"tr": "tool_result",
|
|
921
|
+
"tool": "tool_result"
|
|
922
|
+
},
|
|
923
|
+
long2short: {
|
|
924
|
+
"system": "s",
|
|
925
|
+
"user": "u",
|
|
926
|
+
"audio": "au",
|
|
927
|
+
"assistant": "a",
|
|
928
|
+
"tool_call": "tc",
|
|
929
|
+
"error": "err",
|
|
930
|
+
"comment": "c",
|
|
931
|
+
"tool_result": "tr",
|
|
932
|
+
"tool": "tr"
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
function text2roles(text, lineNum) {
|
|
937
|
+
return (function(it) {
|
|
938
|
+
it = it.split(/(^(?:s|system|u|user|a|assistant|tc|tool_call|tr|tool_result|c|comment|au|audio|err|error)\s*:)/gm);
|
|
939
|
+
it = it.reduce((function(memo, item, index, arr) {
|
|
940
|
+
memo.row = memo.row || 0;
|
|
941
|
+
(function(it) {
|
|
942
|
+
var res, _ref;
|
|
943
|
+
if (it) {
|
|
944
|
+
var res;
|
|
945
|
+
res = {
|
|
946
|
+
role: $roles.short2long[it[1]] || it[1],
|
|
947
|
+
content: arr[index + 1]
|
|
948
|
+
.replace(/\n$/, ""),
|
|
949
|
+
row: memo.row,
|
|
950
|
+
col: 0
|
|
951
|
+
};
|
|
952
|
+
memo.push(res);
|
|
953
|
+
_ref = (memo.row += res.content.split("\n").length);
|
|
954
|
+
} else {
|
|
955
|
+
_ref = undefined;
|
|
956
|
+
}
|
|
957
|
+
return _ref;
|
|
958
|
+
})(item.match(/^(s|system|u|user|a|assistant|tc|tool_call|tr|tool_result|c|comment|au|audio|err|error)\s*:/));
|
|
959
|
+
return memo;
|
|
960
|
+
}), []);
|
|
961
|
+
it = it.map((function() {
|
|
962
|
+
return (lineNum ? arguments[0] : pick(arguments[0], "role", "content"));
|
|
963
|
+
}));
|
|
964
|
+
return it;
|
|
965
|
+
})(text);
|
|
966
|
+
}
|
|
967
|
+
text2roles;
|
|
968
|
+
|
|
969
|
+
function roles2text(msgs) {
|
|
970
|
+
return msgs
|
|
971
|
+
.map((function(msg) {
|
|
972
|
+
return (function(it) {
|
|
973
|
+
return (it ? (it + ":" + msg.content) : undefined);
|
|
974
|
+
})($roles.long2short[msg.role]);
|
|
975
|
+
}))
|
|
976
|
+
.join("\n");
|
|
977
|
+
}
|
|
978
|
+
roles2text;
|
|
979
|
+
|
|
980
|
+
function text2html(text, mimetype) {
|
|
981
|
+
var _ref;
|
|
982
|
+
if ((mimetype === "text/chat")) {
|
|
983
|
+
_ref = text2roles(text)
|
|
984
|
+
.map((function(item, index, arr) {
|
|
985
|
+
var _ref;
|
|
986
|
+
switch (item.role) {
|
|
987
|
+
case "system":
|
|
988
|
+
_ref = "bullet-s";
|
|
989
|
+
break;
|
|
990
|
+
case "variable":
|
|
991
|
+
_ref = "bullet-s";
|
|
992
|
+
break;
|
|
993
|
+
case "assistant":
|
|
994
|
+
_ref = "bullet-a";
|
|
995
|
+
break;
|
|
996
|
+
case "tool_call":
|
|
997
|
+
_ref = "bullet-a";
|
|
998
|
+
break;
|
|
999
|
+
case "tool":
|
|
1000
|
+
_ref = "bullet-s";
|
|
1001
|
+
break;
|
|
1002
|
+
case "user":
|
|
1003
|
+
_ref = "bullet-u";
|
|
1004
|
+
break;
|
|
1005
|
+
case "error":
|
|
1006
|
+
_ref = "bullet-s";
|
|
1007
|
+
break;
|
|
1008
|
+
case "comment":
|
|
1009
|
+
_ref = "bullet-c";
|
|
1010
|
+
break;
|
|
1011
|
+
default:
|
|
1012
|
+
_ref = undefined;
|
|
1013
|
+
}
|
|
1014
|
+
return span({
|
|
1015
|
+
class: "mb-10px relative " + _ref
|
|
1016
|
+
}, span({
|
|
1017
|
+
title: item.role
|
|
1018
|
+
}, $roles.long2short[item.role], ":", vars2html(item.content), ((index !== (arr.length - 1)) ? "\n" : "")));
|
|
1019
|
+
}));
|
|
1020
|
+
} else if (0 === mimetype.indexOf("text/")) {
|
|
1021
|
+
_ref = vars2html(text);
|
|
1022
|
+
} else {
|
|
1023
|
+
_ref = undefined;
|
|
1024
|
+
}
|
|
1025
|
+
return _ref;
|
|
1026
|
+
}
|
|
1027
|
+
text2html;
|
|
1028
|
+
|
|
1029
|
+
function vars2html(str) {
|
|
1030
|
+
return str
|
|
1031
|
+
.split(/({\w+})/g)
|
|
1032
|
+
.map((function(item, index, arr) {
|
|
1033
|
+
return (function(it) {
|
|
1034
|
+
return (it ? twwc(b("{", it[1], "}"), "t-black dark:t-white") : document.createTextNode(String(item)));
|
|
1035
|
+
})(item.match(/^{(\w+)}$/));
|
|
1036
|
+
}));
|
|
1037
|
+
}
|
|
1038
|
+
vars2html;
|
|
1039
|
+
|
|
1040
|
+
function text2var(text) {
|
|
1041
|
+
return (function(it) {
|
|
1042
|
+
var _ref;
|
|
1043
|
+
if (it) {
|
|
1044
|
+
_ref = (function(res) {
|
|
1045
|
+
res[it[1]] = it[2];
|
|
1046
|
+
return res;
|
|
1047
|
+
})({});
|
|
1048
|
+
} else {
|
|
1049
|
+
_ref = undefined;
|
|
1050
|
+
throw new Error(("var syntax: " + text));
|
|
1051
|
+
}
|
|
1052
|
+
return _ref;
|
|
1053
|
+
})(text.match(/^\s*(\w+)\s*=?\s*([\s\S]*)$/m));
|
|
1054
|
+
}
|
|
1055
|
+
text2var;
|
|
1056
|
+
|
|
1057
|
+
function text2call(text) {
|
|
1058
|
+
var lines, m, txt, name, args, _ref;
|
|
1059
|
+
var lines;
|
|
1060
|
+
var m;
|
|
1061
|
+
var txt;
|
|
1062
|
+
lines = text.split(/\r?\n/g);
|
|
1063
|
+
m = lines[0].match(/^\s*([\@\w\-]+)\s*({.*})?\s*$/);
|
|
1064
|
+
txt = lines
|
|
1065
|
+
.slice(1)
|
|
1066
|
+
.join("\n")
|
|
1067
|
+
.trim();
|
|
1068
|
+
if (!m) throw new Error(("can not parse tool call: " + lines[0]));
|
|
1069
|
+
var name;
|
|
1070
|
+
var args;
|
|
1071
|
+
name = m[1];
|
|
1072
|
+
try {
|
|
1073
|
+
_ref = dJSON.parse(m[2] || "{}");
|
|
1074
|
+
} catch (e) {
|
|
1075
|
+
throw new Error(("Can not parse arguments :" + m[2] + " " + (((typeof e !== "undefined") && (e !== null) && !Number.isNaN(e) && (typeof e.message !== "undefined") && (e.message !== null) && !Number.isNaN(e.message)) ? e.message : (((typeof e !== "undefined") && (e !== null) && !Number.isNaN(e)) ? e : undefined))));
|
|
1076
|
+
}
|
|
1077
|
+
args = _ref;
|
|
1078
|
+
if (txt) args.text = txt;
|
|
1079
|
+
return {
|
|
1080
|
+
name: name,
|
|
1081
|
+
arguments: JSON.stringify(args)
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
text2call;
|
|
1085
|
+
async function text2payload(text, ctx) {
|
|
1086
|
+
var ast;
|
|
1087
|
+
ast = await text2ast(text, ctx);
|
|
1088
|
+
return await ast2payload(ast);
|
|
1089
|
+
}
|
|
1090
|
+
text2payload;
|
|
1091
|
+
async function text2ast(text, ctx) {
|
|
1092
|
+
var lastChar, lastRole;
|
|
1093
|
+
var lastChar;
|
|
1094
|
+
lastChar = '';
|
|
1095
|
+
var lastRole;
|
|
1096
|
+
lastRole = '';
|
|
1097
|
+
async function parse(text, recursive, ctx) {
|
|
1098
|
+
var nodeStack, re, nodes, index, m, match, role, roleName, name, prefix, args, proc, row, col, filename, resolved, pargs, pname, p, err, schema, lctx, _ref, _i, _i0, _ref0, _len;
|
|
1099
|
+
ctx = makeContext(ctx);
|
|
1100
|
+
nodeStack = nodeStack || [];
|
|
1101
|
+
var re;
|
|
1102
|
+
re = /(?<prefix1>@{1,2})\{(?<name1>[^\}]+)\}|(?<prefix>@{1,2})(?<name>[\S]+)|(?<role>^(?:s|system|u|user|a|assistant|tc|tool_call|tr|tool_result|c|comment|au|audio|err|error))\s*(?<roleName>\([^\)]+\))?\s*:/gm;
|
|
1103
|
+
var nodes;
|
|
1104
|
+
nodes = [];
|
|
1105
|
+
var index;
|
|
1106
|
+
index = 0;
|
|
1107
|
+
while (m = re.exec(text)) {
|
|
1108
|
+
if (((lastRole === "a" || lastRole === "tc" || lastRole === "err" || lastRole === "assistant" || lastRole === "tool_call" || lastRole === "error") && !m.groups.role)) continue;
|
|
1109
|
+
if (((m.index > 0) && (text.charAt(m.index - 1) === String.fromCharCode(92)))) continue;
|
|
1110
|
+
if ((m.index - index)) {
|
|
1111
|
+
nodes.push({
|
|
1112
|
+
type: "text",
|
|
1113
|
+
start: index,
|
|
1114
|
+
end: m.index,
|
|
1115
|
+
value: text.slice(index, m.index)
|
|
1116
|
+
});
|
|
1117
|
+
lastChar = text.charAt(m.index - 1);
|
|
1118
|
+
}
|
|
1119
|
+
var match;
|
|
1120
|
+
match = m[0];
|
|
1121
|
+
var role;
|
|
1122
|
+
var roleName;
|
|
1123
|
+
role = m.groups.role;
|
|
1124
|
+
roleName = m.groups.roleName;
|
|
1125
|
+
if (role) {
|
|
1126
|
+
nodes.push({
|
|
1127
|
+
type: ((lastChar === '' || lastChar === "\n") ? "role" : "text"),
|
|
1128
|
+
start: index,
|
|
1129
|
+
end: m.index,
|
|
1130
|
+
role: role,
|
|
1131
|
+
roleName: (roleName ? roleName.slice(1, -1) : undefined),
|
|
1132
|
+
value: match
|
|
1133
|
+
});
|
|
1134
|
+
index = m.index + match.length;
|
|
1135
|
+
lastChar = text.charAt(index - 1);
|
|
1136
|
+
lastRole = match.slice(0, -1);
|
|
1137
|
+
continue;
|
|
1138
|
+
}
|
|
1139
|
+
var name;
|
|
1140
|
+
var prefix;
|
|
1141
|
+
var args;
|
|
1142
|
+
var proc;
|
|
1143
|
+
name = m.groups.name || m.groups.name1;
|
|
1144
|
+
prefix = m.groups.prefix || m.groups.prefix1;
|
|
1145
|
+
args = undefined;
|
|
1146
|
+
proc = undefined;
|
|
1147
|
+
(function(it) {
|
|
1148
|
+
it = it.split("|");
|
|
1149
|
+
it = it.map((function(it) {
|
|
1150
|
+
return it.trim();
|
|
1151
|
+
}));
|
|
1152
|
+
it = it.map((function(item, index) {
|
|
1153
|
+
var idx;
|
|
1154
|
+
if ((index === 0)) return item;
|
|
1155
|
+
var idx;
|
|
1156
|
+
idx = item.indexOf(" ");
|
|
1157
|
+
if ((idx === -1)) return Array(item, "");
|
|
1158
|
+
return Array(item.slice(0, idx), item.slice(idx));
|
|
1159
|
+
}));
|
|
1160
|
+
name = it[0];
|
|
1161
|
+
it = (proc = ((it.length > 1) ? it.slice(1) : undefined));
|
|
1162
|
+
return it;
|
|
1163
|
+
})(name);
|
|
1164
|
+
_ref = getPos(m.index, text);
|
|
1165
|
+
row = _ref[0];
|
|
1166
|
+
col = _ref[1];
|
|
1167
|
+
var filename;
|
|
1168
|
+
filename = (function(it) {
|
|
1169
|
+
it = it["slice"](-1)[0];
|
|
1170
|
+
it = (it ? (it.fullname || it.name) : "");
|
|
1171
|
+
return it;
|
|
1172
|
+
})(ctx.stack || []);
|
|
1173
|
+
var resolved;
|
|
1174
|
+
resolved = await ctx.resolve(name);
|
|
1175
|
+
if (proc) {
|
|
1176
|
+
while (pargs = proc.shift()) {
|
|
1177
|
+
var pname;
|
|
1178
|
+
pname = pargs.shift();
|
|
1179
|
+
var p;
|
|
1180
|
+
p = await ctx.resolve(pname);
|
|
1181
|
+
if (!p) throw new TuneError(("'" + pname + "' processor not found"), filename, row, col);
|
|
1182
|
+
if ((typeof p.exec !== "function")) throw new TuneError(("'" + pname + "' does not have exec function"), filename, row, col);
|
|
1183
|
+
try {
|
|
1184
|
+
resolved = await p.exec(resolved, pargs[0], ctx);
|
|
1185
|
+
} catch (e) {
|
|
1186
|
+
var err;
|
|
1187
|
+
err = new TuneError(e.message, (p.fullname || p.name), undefined, undefined, [], e);
|
|
1188
|
+
err.stack.push({
|
|
1189
|
+
filename: filename,
|
|
1190
|
+
row: row,
|
|
1191
|
+
col: col
|
|
1192
|
+
});
|
|
1193
|
+
throw err;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
_ref0 = (Array.isArray(resolved) ? resolved : [resolved]);
|
|
1198
|
+
for (_i0 = 0, _len = _ref0.length; _i0 < _len; ++_i0) {
|
|
1199
|
+
resolved = _ref0[_i0];
|
|
1200
|
+
if (!resolved) throw new TuneError(("'" + name + "' not found"), filename, row, col);
|
|
1201
|
+
resolved.start = m.index;
|
|
1202
|
+
resolved.end = m.index + match.length;
|
|
1203
|
+
resolved.stack = TuneError.ctx2stack(ctx);
|
|
1204
|
+
resolved.prefix = prefix;
|
|
1205
|
+
resolved.name = resolved.name || name;
|
|
1206
|
+
resolved.stack.push({
|
|
1207
|
+
col: col,
|
|
1208
|
+
row: row,
|
|
1209
|
+
filename: filename
|
|
1210
|
+
});
|
|
1211
|
+
if ((resolved.type === "text" || resolved.type === "image" || resolved.type === "audio")) resolved.value = await resolved.read();
|
|
1212
|
+
if ((resolved.type === "tool")) {
|
|
1213
|
+
var schema;
|
|
1214
|
+
schema = resolved.schema;
|
|
1215
|
+
if (!schema) throw new TuneError(("schema has to be set" + " for '" + resolved.name + "'"), filename, row, col);
|
|
1216
|
+
schema.name = resolved.name;
|
|
1217
|
+
if (!schema.description) throw new TuneError(("no description set" + " for '" + resolved.name + "'"), filename, row, col);
|
|
1218
|
+
if (!schema.parameters) throw new TuneError(("no parameters set" + " for '" + resolved.name + "'"), filename, row, col);
|
|
1219
|
+
if (!schema.parameters.type) throw new TuneError(("no parameters.type set" + " for '" + resolved.name + "'"), filename, row, col);
|
|
1220
|
+
if (!schema.parameters.properties) throw new TuneError(("no parameters.properties set" + " for '" + resolved.name + "'"), filename, row, col);
|
|
1221
|
+
}
|
|
1222
|
+
index = m.index + match.length;
|
|
1223
|
+
if ((recursive && (resolved.prefix.length === 2) && (resolved.type === "text"))) {
|
|
1224
|
+
var lctx;
|
|
1225
|
+
lctx = ctx.clone();
|
|
1226
|
+
lctx.stack.push(resolved);
|
|
1227
|
+
try {
|
|
1228
|
+
resolved.nodes = await parse(await resolved.read(), true, lctx);
|
|
1229
|
+
} catch (e) {
|
|
1230
|
+
throw TuneError.wrap(e, filename, row, col);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
nodes.push(resolved);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
if ((text.length - index)) nodes.push({
|
|
1237
|
+
type: "text",
|
|
1238
|
+
start: index,
|
|
1239
|
+
end: text.length,
|
|
1240
|
+
value: text.slice(index, text.length)
|
|
1241
|
+
});
|
|
1242
|
+
return nodes;
|
|
1243
|
+
}
|
|
1244
|
+
parse;
|
|
1245
|
+
return await parse(text, true, ctx);
|
|
1246
|
+
}
|
|
1247
|
+
text2ast;
|
|
1248
|
+
|
|
1249
|
+
function ast2payload(ast) {
|
|
1250
|
+
var payload, toolId, tools, llms, messages, roles, lastRole, lastChar;
|
|
1251
|
+
var payload;
|
|
1252
|
+
var toolId;
|
|
1253
|
+
var tools;
|
|
1254
|
+
var llms;
|
|
1255
|
+
var messages;
|
|
1256
|
+
var roles;
|
|
1257
|
+
var lastRole;
|
|
1258
|
+
var lastChar;
|
|
1259
|
+
payload = {};
|
|
1260
|
+
toolId = 0;
|
|
1261
|
+
tools = [];
|
|
1262
|
+
llms = [];
|
|
1263
|
+
messages = [];
|
|
1264
|
+
roles = [];
|
|
1265
|
+
lastRole = undefined;
|
|
1266
|
+
lastChar = '';
|
|
1267
|
+
|
|
1268
|
+
function visit(ast) {
|
|
1269
|
+
var node, _i, _res, _ref, _len, _ref0;
|
|
1270
|
+
_res = [];
|
|
1271
|
+
_ref = ast;
|
|
1272
|
+
for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
|
|
1273
|
+
node = _ref[_i];
|
|
1274
|
+
if ((node.type === "role")) {
|
|
1275
|
+
if (lastRole) roles.push(lastRole);
|
|
1276
|
+
_ref0 = (lastRole = node);
|
|
1277
|
+
} else if (lastRole) {
|
|
1278
|
+
lastRole.nodes = lastRole.nodes || [];
|
|
1279
|
+
_ref0 = (node.nodes ? visit(node.nodes) : lastRole.nodes.push(node));
|
|
1280
|
+
} else if (!!node.nodes) {
|
|
1281
|
+
_ref0 = visit(node.nodes);
|
|
1282
|
+
} else {
|
|
1283
|
+
_ref0 = undefined;
|
|
1284
|
+
}
|
|
1285
|
+
if (typeof _ref0 !== 'undefined') _res.push(_ref0);
|
|
1286
|
+
}
|
|
1287
|
+
return _res;
|
|
1288
|
+
}
|
|
1289
|
+
visit;
|
|
1290
|
+
visit(ast);
|
|
1291
|
+
roles.push(lastRole);
|
|
1292
|
+
|
|
1293
|
+
function transformRoles(memo, item, index, arr) {
|
|
1294
|
+
var tcItem, lines, toolIndex, tc;
|
|
1295
|
+
if ((item.role === "user" || item.role === "system" || item.role === "tool_call")) item.content = (Array.isArray(item.content) ? item.content.map((function(item) {
|
|
1296
|
+
if (item.text) item.text = unescape(item.text);
|
|
1297
|
+
return item;
|
|
1298
|
+
})) : unescape(item.content));
|
|
1299
|
+
|
|
1300
|
+
function findToolCalls() {
|
|
1301
|
+
var item1, _i, _ref, _len;
|
|
1302
|
+
var tools;
|
|
1303
|
+
tools = [];
|
|
1304
|
+
_ref = memo
|
|
1305
|
+
.slice()
|
|
1306
|
+
.reverse();
|
|
1307
|
+
for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
|
|
1308
|
+
item1 = _ref[_i];
|
|
1309
|
+
if ((item1.role === "tool")) tools.push(item1);
|
|
1310
|
+
if ((!!item1.tool_calls && !!tools.length && (tools.length === item1.tool_calls.length))) return;
|
|
1311
|
+
if (item1.tool_calls) return item1;
|
|
1312
|
+
if ((item1.role === "user" || item1.role === "assistant")) return;
|
|
1313
|
+
}
|
|
1314
|
+
return null;
|
|
1315
|
+
}
|
|
1316
|
+
findToolCalls;
|
|
1317
|
+
if ((item.role === "tool_call")) {
|
|
1318
|
+
tcItem = findToolCalls();
|
|
1319
|
+
if (!tcItem) {
|
|
1320
|
+
tcItem = {
|
|
1321
|
+
role: "assistant",
|
|
1322
|
+
content: null,
|
|
1323
|
+
tool_calls: Array()
|
|
1324
|
+
};
|
|
1325
|
+
memo.push(tcItem);
|
|
1326
|
+
}
|
|
1327
|
+
tcItem.tool_calls.push({
|
|
1328
|
+
id: String(toolId++),
|
|
1329
|
+
type: "function",
|
|
1330
|
+
"function": text2call(item.content)
|
|
1331
|
+
});
|
|
1332
|
+
} else if (item.role === "audio") {
|
|
1333
|
+
var lines;
|
|
1334
|
+
lines = item.content.split("\n");
|
|
1335
|
+
memo.push({
|
|
1336
|
+
role: "assistant",
|
|
1337
|
+
content: null,
|
|
1338
|
+
audio: {
|
|
1339
|
+
id: item.id,
|
|
1340
|
+
data: item.data,
|
|
1341
|
+
expires_at: (function(it) {
|
|
1342
|
+
it = it.trim();
|
|
1343
|
+
it = parseInt(it);
|
|
1344
|
+
return it;
|
|
1345
|
+
})(lines[0]),
|
|
1346
|
+
transcript: lines.slice(1)
|
|
1347
|
+
.join("\n")
|
|
1348
|
+
}
|
|
1349
|
+
});
|
|
1350
|
+
} else if (item.role === "tool") {
|
|
1351
|
+
tcItem = findToolCalls();
|
|
1352
|
+
if (!tcItem) throw "No tool_calls found for item.role 'tool'";
|
|
1353
|
+
toolIndex = memo.slice(memo.indexOf(tcItem))
|
|
1354
|
+
.reverse()
|
|
1355
|
+
.reduce((function(memo, item) {
|
|
1356
|
+
return ((item.role === "tool") ? (memo + 1) : memo);
|
|
1357
|
+
}), 0);
|
|
1358
|
+
tc = tcItem.tool_calls[toolIndex];
|
|
1359
|
+
item.tool_call_id = tc.id;
|
|
1360
|
+
item.name = tc.function.name;
|
|
1361
|
+
memo.push(item);
|
|
1362
|
+
} else {
|
|
1363
|
+
memo.push(item);
|
|
1364
|
+
}
|
|
1365
|
+
return memo;
|
|
1366
|
+
}
|
|
1367
|
+
payload.messages = roles
|
|
1368
|
+
.map((function(item) {
|
|
1369
|
+
var res, _ref;
|
|
1370
|
+
var res;
|
|
1371
|
+
switch (item.role) {
|
|
1372
|
+
case "s":
|
|
1373
|
+
_ref = "system";
|
|
1374
|
+
break;
|
|
1375
|
+
case "u":
|
|
1376
|
+
_ref = "user";
|
|
1377
|
+
break;
|
|
1378
|
+
case "a":
|
|
1379
|
+
_ref = "assistant";
|
|
1380
|
+
break;
|
|
1381
|
+
case "au":
|
|
1382
|
+
_ref = "audio";
|
|
1383
|
+
break;
|
|
1384
|
+
case "tc":
|
|
1385
|
+
_ref = "tool_call";
|
|
1386
|
+
break;
|
|
1387
|
+
case "err":
|
|
1388
|
+
_ref = "error";
|
|
1389
|
+
break;
|
|
1390
|
+
case "c":
|
|
1391
|
+
_ref = "comment";
|
|
1392
|
+
break;
|
|
1393
|
+
case "tr":
|
|
1394
|
+
_ref = "tool";
|
|
1395
|
+
break;
|
|
1396
|
+
case "tool":
|
|
1397
|
+
_ref = "tool";
|
|
1398
|
+
break;
|
|
1399
|
+
case "tool_result":
|
|
1400
|
+
_ref = "tool";
|
|
1401
|
+
break;
|
|
1402
|
+
default:
|
|
1403
|
+
_ref = item.role;
|
|
1404
|
+
}
|
|
1405
|
+
res = {
|
|
1406
|
+
role: _ref,
|
|
1407
|
+
nodes: item.nodes
|
|
1408
|
+
};
|
|
1409
|
+
if (item.roleName) res.name = item.roleName;
|
|
1410
|
+
return res;
|
|
1411
|
+
}))
|
|
1412
|
+
.map((function(item) {
|
|
1413
|
+
item.nodes = item.nodes.reduce((function(memo, node) {
|
|
1414
|
+
if ((node.type === "llm")) {
|
|
1415
|
+
llms.push(node);
|
|
1416
|
+
} else if (node.type === "tool") {
|
|
1417
|
+
tools.push(node);
|
|
1418
|
+
} else {
|
|
1419
|
+
memo.push(node);
|
|
1420
|
+
}
|
|
1421
|
+
return memo;
|
|
1422
|
+
}), []);
|
|
1423
|
+
return item;
|
|
1424
|
+
}))
|
|
1425
|
+
.map((function(item) {
|
|
1426
|
+
item.content = item.nodes.reduce((function(memo, node) {
|
|
1427
|
+
var lastNode, _ref;
|
|
1428
|
+
if ((node.type === "image")) {
|
|
1429
|
+
if ((typeof memo === "string")) memo = Array({
|
|
1430
|
+
type: "text",
|
|
1431
|
+
text: memo
|
|
1432
|
+
});
|
|
1433
|
+
memo.push({
|
|
1434
|
+
type: "image_url",
|
|
1435
|
+
image_url: {
|
|
1436
|
+
url: "data:" + node.mimetype + ";base64," + node.value.toString("base64")
|
|
1437
|
+
}
|
|
1438
|
+
});
|
|
1439
|
+
} else if ((item.role === "user") && (node.type === "audio")) {
|
|
1440
|
+
if ((typeof memo === "string")) memo = Array({
|
|
1441
|
+
type: "text",
|
|
1442
|
+
text: memo
|
|
1443
|
+
});
|
|
1444
|
+
switch (node.mimetype) {
|
|
1445
|
+
case "audio/mpeg":
|
|
1446
|
+
_ref = "mp3";
|
|
1447
|
+
break;
|
|
1448
|
+
case "audio/wav":
|
|
1449
|
+
_ref = "wav";
|
|
1450
|
+
break;
|
|
1451
|
+
default:
|
|
1452
|
+
_ref = undefined;
|
|
1453
|
+
}
|
|
1454
|
+
memo.push({
|
|
1455
|
+
type: "input_audio",
|
|
1456
|
+
input_audio: {
|
|
1457
|
+
data: node.value.toString("base64"),
|
|
1458
|
+
format: _ref
|
|
1459
|
+
}
|
|
1460
|
+
});
|
|
1461
|
+
} else if ((item.role === "audio") && (node.type === "audio")) {
|
|
1462
|
+
item.id = node.name;
|
|
1463
|
+
item.data = node.value.toString("base64");
|
|
1464
|
+
} else if (node.type === "var" || node.type === "text") {
|
|
1465
|
+
if ((typeof memo === "string")) {
|
|
1466
|
+
memo += node.value;
|
|
1467
|
+
} else {
|
|
1468
|
+
var lastNode;
|
|
1469
|
+
lastNode = memo["slice"](-1)[0];
|
|
1470
|
+
(lastNode.type === "text") ? lastNode.text += node.value: memo.push({
|
|
1471
|
+
type: "text",
|
|
1472
|
+
text: node.value
|
|
1473
|
+
});
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
return memo;
|
|
1477
|
+
}), "");
|
|
1478
|
+
delete item.nodes;
|
|
1479
|
+
item.content = ((typeof item.content === "string") ? item.content.trim() : item.content.map((function(content) {
|
|
1480
|
+
if ((content.type === "text")) content.text = content.text.trim();
|
|
1481
|
+
return content;
|
|
1482
|
+
})));
|
|
1483
|
+
return item;
|
|
1484
|
+
}))
|
|
1485
|
+
.reduce(transformRoles, Array());
|
|
1486
|
+
if (tools.length) payload.tools = tools;
|
|
1487
|
+
if (llms.length) payload.llm = llms["slice"](-1)[0];
|
|
1488
|
+
return payload;
|
|
1489
|
+
}
|
|
1490
|
+
ast2payload;
|
|
1491
|
+
async function payload2http(payload, ctx) {
|
|
1492
|
+
var llm, stack, lastStack, body, _ref;
|
|
1493
|
+
ctx = makeContext(ctx);
|
|
1494
|
+
var llm;
|
|
1495
|
+
llm = payload.llm;
|
|
1496
|
+
delete payload.llm;
|
|
1497
|
+
if (!llm) llm = await ctx.resolve("*", "llm");
|
|
1498
|
+
if (!llm) {
|
|
1499
|
+
var stack;
|
|
1500
|
+
stack = TuneError.ctx2stack(ctx);
|
|
1501
|
+
var lastStack;
|
|
1502
|
+
lastStack = stack.pop();
|
|
1503
|
+
throw new TuneError("llm file not found", (((typeof lastStack !== "undefined") && (lastStack !== null) && !Number.isNaN(lastStack) && (typeof lastStack.filename !== "undefined") && (lastStack.filename !== null) && !Number.isNaN(lastStack.filename)) ? lastStack.filename : undefined), (((typeof lastStack !== "undefined") && (lastStack !== null) && !Number.isNaN(lastStack) && (typeof lastStack.row !== "undefined") && (lastStack.row !== null) && !Number.isNaN(lastStack.row)) ? lastStack.row : undefined), (((typeof lastStack !== "undefined") && (lastStack !== null) && !Number.isNaN(lastStack) && (typeof lastStack.col !== "undefined") && (lastStack.col !== null) && !Number.isNaN(lastStack.col)) ? lastStack.col : undefined), stack);
|
|
1504
|
+
}
|
|
1505
|
+
var body;
|
|
1506
|
+
body = Object.assign({}, payload);
|
|
1507
|
+
if (body.tools) body.tools = body.tools.map((function(tool) {
|
|
1508
|
+
return {
|
|
1509
|
+
type: "function",
|
|
1510
|
+
function: Object.assign({}, tool.schema)
|
|
1511
|
+
}
|
|
1512
|
+
}));
|
|
1513
|
+
try {
|
|
1514
|
+
_ref = await llm.exec(body, ctx);
|
|
1515
|
+
} catch (e) {
|
|
1516
|
+
throw new TuneError(e.message, (llm.filename || llm.fullname || llm.name), llm.row, llm.col, llm.stack, e);
|
|
1517
|
+
}
|
|
1518
|
+
return _ref;
|
|
1519
|
+
}
|
|
1520
|
+
payload2http;
|
|
1521
|
+
async function toolCall(payload, ctx) {
|
|
1522
|
+
var lastMsg, tools;
|
|
1523
|
+
var lastMsg;
|
|
1524
|
+
lastMsg = payload.messages["slice"](-1)[0];
|
|
1525
|
+
if (((((typeof lastMsg !== "undefined") && (lastMsg !== null) && !Number.isNaN(lastMsg) && (typeof lastMsg.tool_calls !== "undefined") && (lastMsg.tool_calls !== null) && !Number.isNaN(lastMsg.tool_calls) && (typeof lastMsg.tool_calls.length !== "undefined") && (lastMsg.tool_calls.length !== null) && !Number.isNaN(lastMsg.tool_calls.length)) ? lastMsg.tool_calls.length : (((typeof 0 !== "undefined") && (0 !== null) && !Number.isNaN(0)) ? 0 : undefined)) === 0)) return [];
|
|
1526
|
+
ctx = makeContext(ctx);
|
|
1527
|
+
var tools;
|
|
1528
|
+
tools = (payload.tools || [])
|
|
1529
|
+
.reduce((function(memo, tool) {
|
|
1530
|
+
memo[tool.name] = tool;
|
|
1531
|
+
return memo;
|
|
1532
|
+
}), {});
|
|
1533
|
+
return Promise.all(lastMsg.tool_calls.map((async function(item) {
|
|
1534
|
+
var res, tc, tool;
|
|
1535
|
+
var res;
|
|
1536
|
+
res;
|
|
1537
|
+
var tc;
|
|
1538
|
+
tc = item.function;
|
|
1539
|
+
var tool;
|
|
1540
|
+
tool = tools[tc.name];
|
|
1541
|
+
if (!tool) throw new TuneError(("tool '" + tc.name + "' not defined"), "", undefined, undefined, TuneError.ctx2stack(ctx));
|
|
1542
|
+
try {
|
|
1543
|
+
res = await tools[tc.name].exec(JSON.parse(tc.arguments), ctx);
|
|
1544
|
+
} catch (e) {
|
|
1545
|
+
throw new TuneError(e.message, (tool.filename || tool.fullname || tool.name), tool.row, tool.col, tool.stack, e);
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
function transformRes(res) {
|
|
1549
|
+
var _ref;
|
|
1550
|
+
if (((typeof res === "string" || typeof res === "number" || typeof res === "boolean" || typeof res === "undefined") || (res instanceof String) || (res === null))) {
|
|
1551
|
+
_ref = String(res);
|
|
1552
|
+
} else if (Array.isArray(res)) {
|
|
1553
|
+
_ref = res
|
|
1554
|
+
.map(transformRes)
|
|
1555
|
+
.join("\n");
|
|
1556
|
+
} else if (typeof util !== 'undefined') {
|
|
1557
|
+
_ref = util.inspect(res);
|
|
1558
|
+
} else if (((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res.toString !== "undefined") && (res.toString !== null) && !Number.isNaN(res.toString)) ? res.toString : undefined) {
|
|
1559
|
+
_ref = res.toString();
|
|
1560
|
+
} else {
|
|
1561
|
+
_ref = String(res);
|
|
1562
|
+
}
|
|
1563
|
+
return _ref;
|
|
1564
|
+
}
|
|
1565
|
+
transformRes;
|
|
1566
|
+
return {
|
|
1567
|
+
role: "tool",
|
|
1568
|
+
id: item.id,
|
|
1569
|
+
content: transformRes(res)
|
|
1570
|
+
}
|
|
1571
|
+
})));
|
|
1572
|
+
}
|
|
1573
|
+
toolCall;
|
|
1574
|
+
|
|
1575
|
+
function TunePromise(executor, iterator) {
|
|
1576
|
+
this.promise = new Promise(executor);
|
|
1577
|
+
if (iterator) {
|
|
1578
|
+
this.next = iterator;
|
|
1579
|
+
this[Symbol.asyncIterator] = this;
|
|
1580
|
+
}
|
|
1581
|
+
return this;
|
|
1582
|
+
}
|
|
1583
|
+
TunePromise;
|
|
1584
|
+
TunePromise.prototype.then = (function(onFulfilled, onRejected) {
|
|
1585
|
+
return this.promise.then(onFulfilled, onRejected);
|
|
1586
|
+
});
|
|
1587
|
+
TunePromise.prototype.catch = (function(onRejected) {
|
|
1588
|
+
return this.promise.catch(onRejected);
|
|
1589
|
+
});
|
|
1590
|
+
TunePromise.prototype.finally = (function(onFinally) {
|
|
1591
|
+
return this.promise.finally(onFinally);
|
|
1592
|
+
});
|
|
1593
|
+
|
|
1594
|
+
function text2run(text, ctx, opts) {
|
|
1595
|
+
var msgs, stopVal, stream, ires, ierr, ifinish, resolve, reject, p;
|
|
1596
|
+
if (!ctx) throw Error("context not set");
|
|
1597
|
+
ctx = makeContext(ctx);
|
|
1598
|
+
var msgs;
|
|
1599
|
+
var stopVal;
|
|
1600
|
+
var stream;
|
|
1601
|
+
var ires;
|
|
1602
|
+
var ierr;
|
|
1603
|
+
var ifinish;
|
|
1604
|
+
var resolve;
|
|
1605
|
+
var reject;
|
|
1606
|
+
msgs = [];
|
|
1607
|
+
stopVal = (((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.stop !== "undefined") && (opts.stop !== null) && !Number.isNaN(opts.stop)) ? opts.stop : (((typeof "step" !== "undefined") && ("step" !== null) && !Number.isNaN("step")) ? "step" : undefined));
|
|
1608
|
+
stream = (((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.stream !== "undefined") && (opts.stream !== null) && !Number.isNaN(opts.stream)) ? opts.stream : (((typeof false !== "undefined") && (false !== null) && !Number.isNaN(false)) ? false : undefined));
|
|
1609
|
+
ires = undefined;
|
|
1610
|
+
ierr = undefined;
|
|
1611
|
+
ifinish = false;
|
|
1612
|
+
resolve = undefined;
|
|
1613
|
+
reject = undefined;
|
|
1614
|
+
var p;
|
|
1615
|
+
p = new Promise((function(res, rej) {
|
|
1616
|
+
resolve = res;
|
|
1617
|
+
return (reject = rej);
|
|
1618
|
+
}));
|
|
1619
|
+
if (stream) p = new TunePromise((function(res, rej) {
|
|
1620
|
+
resolve = res;
|
|
1621
|
+
return (reject = rej);
|
|
1622
|
+
}), (async function() {
|
|
1623
|
+
var val;
|
|
1624
|
+
await _once((function() {
|
|
1625
|
+
return (!!ires || !!ierr || !!ifinish);
|
|
1626
|
+
}), (function() {
|
|
1627
|
+
return undefined;
|
|
1628
|
+
}));
|
|
1629
|
+
if (ierr) throw ierr;
|
|
1630
|
+
if (ires) {
|
|
1631
|
+
var val;
|
|
1632
|
+
val = ires;
|
|
1633
|
+
ires = undefined;
|
|
1634
|
+
return val;
|
|
1635
|
+
}
|
|
1636
|
+
return ifinish;
|
|
1637
|
+
}));
|
|
1638
|
+
|
|
1639
|
+
function stop() {
|
|
1640
|
+
var lastMsg, _ref;
|
|
1641
|
+
var lastMsg;
|
|
1642
|
+
lastMsg = msgs["slice"](-1)[0];
|
|
1643
|
+
if ((stopVal === "step")) {
|
|
1644
|
+
_ref = !!lastMsg;
|
|
1645
|
+
} else if (stopVal === "assistant") {
|
|
1646
|
+
_ref = (!!lastMsg && (lastMsg.role === "assistant") && !!lastMsg.content);
|
|
1647
|
+
} else if (typeof stopVal === "function") {
|
|
1648
|
+
_ref = stopVal(msgs);
|
|
1649
|
+
} else {
|
|
1650
|
+
_ref = undefined;
|
|
1651
|
+
}
|
|
1652
|
+
return _ref;
|
|
1653
|
+
}
|
|
1654
|
+
stop;
|
|
1655
|
+
async function doit() {
|
|
1656
|
+
var ast, payload, res, ctype, err, reader, data, reData, reComment;
|
|
1657
|
+
while (!stop(msgs)) {
|
|
1658
|
+
var ast;
|
|
1659
|
+
ast = await text2ast(text + "\n" + msg2text(msgs), ctx);
|
|
1660
|
+
var payload;
|
|
1661
|
+
payload = await ast2payload(ast, ctx);
|
|
1662
|
+
if (stream) payload.stream = stream;
|
|
1663
|
+
var res;
|
|
1664
|
+
res = await toolCall(payload, ctx);
|
|
1665
|
+
if (res.length) {
|
|
1666
|
+
msgs = msgs.concat(res);
|
|
1667
|
+
ires = {
|
|
1668
|
+
value: msgs
|
|
1669
|
+
};
|
|
1670
|
+
continue;
|
|
1671
|
+
}
|
|
1672
|
+
payload = await payload2http(payload, ctx);
|
|
1673
|
+
res = await fetch(payload.url, payload);
|
|
1674
|
+
var ctype;
|
|
1675
|
+
ctype = res.headers.get("content-type");
|
|
1676
|
+
if ((!stream || ctype.includes("application/json"))) {
|
|
1677
|
+
res = await res.json();
|
|
1678
|
+
if ((((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res.error !== "undefined") && (res.error !== null) && !Number.isNaN(res.error)) ? res.error : undefined)) {
|
|
1679
|
+
var err;
|
|
1680
|
+
err = new TuneError(tpl("{type: }{message}", res.error));
|
|
1681
|
+
err.stack = TuneError.ctx2stack(ctx);
|
|
1682
|
+
throw err;
|
|
1683
|
+
}
|
|
1684
|
+
msgs.push(res.choices[0].message);
|
|
1685
|
+
continue;
|
|
1686
|
+
}
|
|
1687
|
+
var reader;
|
|
1688
|
+
var data;
|
|
1689
|
+
var reData;
|
|
1690
|
+
var reComment;
|
|
1691
|
+
reader = res.body
|
|
1692
|
+
.pipeThrough(new TextDecoderStream("utf8"))
|
|
1693
|
+
.getReader();
|
|
1694
|
+
data = "";
|
|
1695
|
+
reData = new RegExp("^data: (.*)");
|
|
1696
|
+
reComment = new RegExp("^:.*");
|
|
1697
|
+
if (ctype.includes("text/event-stream")) {
|
|
1698
|
+
while (res = await reader.read()) {
|
|
1699
|
+
if (res.done) break;
|
|
1700
|
+
(function(it) {
|
|
1701
|
+
it = it.split(/\n/);
|
|
1702
|
+
it = it.map((function(item) {
|
|
1703
|
+
return item.trim();
|
|
1704
|
+
}));
|
|
1705
|
+
it = it.filter((function(item) {
|
|
1706
|
+
return !item.match(reComment);
|
|
1707
|
+
}));
|
|
1708
|
+
it = it.map((function(item, index) {
|
|
1709
|
+
var m;
|
|
1710
|
+
var m;
|
|
1711
|
+
m = item.match(reData);
|
|
1712
|
+
return ((m && ('' === it[(index + 1)])) ? m[1] : undefined);
|
|
1713
|
+
}));
|
|
1714
|
+
it = it.filter((function(item) {
|
|
1715
|
+
return item;
|
|
1716
|
+
}));
|
|
1717
|
+
it = it.map((function(item) {
|
|
1718
|
+
return ((item === '[DONE]') ? item : JSON.parse(item));
|
|
1719
|
+
}));
|
|
1720
|
+
it = it.reduce((function(msg, chunk) {
|
|
1721
|
+
var delta, tc;
|
|
1722
|
+
if ((chunk === "[DONE]")) return msg;
|
|
1723
|
+
var delta;
|
|
1724
|
+
delta = (((typeof chunk !== "undefined") && (chunk !== null) && !Number.isNaN(chunk) && (typeof chunk.choices !== "undefined") && (chunk.choices !== null) && !Number.isNaN(chunk.choices) && (typeof chunk.choices[0] !== "undefined") && (chunk.choices[0] !== null) && !Number.isNaN(chunk.choices[0]) && (typeof chunk.choices[0].delta !== "undefined") && (chunk.choices[0].delta !== null) && !Number.isNaN(chunk.choices[0].delta)) ? chunk.choices[0].delta : (((typeof {} !== "undefined") && ({} !== null) && !Number.isNaN({})) ? {} : undefined));
|
|
1725
|
+
if ((((typeof chunk !== "undefined") && (chunk !== null) && !Number.isNaN(chunk) && (typeof chunk.error !== "undefined") && (chunk.error !== null) && !Number.isNaN(chunk.error)) ? chunk.error : undefined)) {
|
|
1726
|
+
var err;
|
|
1727
|
+
err = new TuneError(JSON.stringify((((typeof chunk !== "undefined") && (chunk !== null) && !Number.isNaN(chunk) && (typeof chunk.error !== "undefined") && (chunk.error !== null) && !Number.isNaN(chunk.error)) ? chunk.error : undefined), null, " "));
|
|
1728
|
+
err.stack = TuneError.ctx2stack(ctx);
|
|
1729
|
+
throw err;
|
|
1730
|
+
}
|
|
1731
|
+
if (delta.content) {
|
|
1732
|
+
msg.content = msg.content || "";
|
|
1733
|
+
msg.content += delta.content;
|
|
1734
|
+
} else if (delta.tool_calls) {
|
|
1735
|
+
msg.tool_calls = msg.tool_calls || [];
|
|
1736
|
+
tc = delta.tool_calls[0];
|
|
1737
|
+
msg.tool_calls[tc.index] = msg.tool_calls[tc.index] || tc;
|
|
1738
|
+
msg.tool_calls[tc.index].function.arguments += tc.function.arguments;
|
|
1739
|
+
}
|
|
1740
|
+
return msg;
|
|
1741
|
+
}), {
|
|
1742
|
+
role: "assistant",
|
|
1743
|
+
content: null
|
|
1744
|
+
});
|
|
1745
|
+
it = (ires = {
|
|
1746
|
+
value: msgs.concat(Array(it))
|
|
1747
|
+
});
|
|
1748
|
+
return it;
|
|
1749
|
+
})(data += res.value);
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
if (ires) msgs = ires.value;
|
|
1753
|
+
}
|
|
1754
|
+
ires = {
|
|
1755
|
+
value: msgs,
|
|
1756
|
+
done: false
|
|
1757
|
+
};
|
|
1758
|
+
ifinish = {
|
|
1759
|
+
done: true
|
|
1760
|
+
};
|
|
1761
|
+
return resolve(msgs);
|
|
1762
|
+
}
|
|
1763
|
+
doit;
|
|
1764
|
+
doit()
|
|
1765
|
+
.catch((function(e) {
|
|
1766
|
+
var err;
|
|
1767
|
+
if ((e.name === "TuneError")) return reject(e);
|
|
1768
|
+
var err;
|
|
1769
|
+
err = new TuneError(e.message);
|
|
1770
|
+
err.stack = TuneError.ctx2stack(ctx);
|
|
1771
|
+
err.error = e;
|
|
1772
|
+
return reject(err);
|
|
1773
|
+
}));
|
|
1774
|
+
return p;
|
|
1775
|
+
}
|
|
1776
|
+
text2run;
|
|
1777
|
+
|
|
1778
|
+
function msg2text(msg, long) {
|
|
1779
|
+
var _ref, _ref0, _ref1;
|
|
1780
|
+
|
|
1781
|
+
function mkline(role, content) {
|
|
1782
|
+
return tpl("{role}: {content}", {
|
|
1783
|
+
role: (long ? role : $roles.long2short[role]),
|
|
1784
|
+
content: content
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
mkline;
|
|
1788
|
+
if (Array.isArray(msg)) {
|
|
1789
|
+
_ref1 = msg
|
|
1790
|
+
.map((function(item) {
|
|
1791
|
+
return msg2text(item, long);
|
|
1792
|
+
}))
|
|
1793
|
+
.join("\n");
|
|
1794
|
+
} else {
|
|
1795
|
+
switch (msg.role) {
|
|
1796
|
+
case "user":
|
|
1797
|
+
if (((typeof msg.content === "string") || (msg.content instanceof String))) {
|
|
1798
|
+
_ref0 = (msg.name ? tpl("{role}({name}): {content}", {
|
|
1799
|
+
name: msg.name,
|
|
1800
|
+
content: msg.content,
|
|
1801
|
+
role: (long ? "user" : "u")
|
|
1802
|
+
}) : mkline("user", msg.content));
|
|
1803
|
+
} else if (Array.isArray(msg.content)) {
|
|
1804
|
+
_ref0 = msg.content
|
|
1805
|
+
.map((function(item) {
|
|
1806
|
+
var _ref1;
|
|
1807
|
+
switch (item.type) {
|
|
1808
|
+
case "text":
|
|
1809
|
+
_ref1 = mkline("user", item.text);
|
|
1810
|
+
break;
|
|
1811
|
+
case "tool_result":
|
|
1812
|
+
_ref1 = mkline("tool_result", item.content);
|
|
1813
|
+
break;
|
|
1814
|
+
default:
|
|
1815
|
+
_ref1 = undefined;
|
|
1816
|
+
throw new Error(("unsupported user content type: " + item.type));
|
|
1817
|
+
}
|
|
1818
|
+
return _ref1;
|
|
1819
|
+
}))
|
|
1820
|
+
.join("\n");
|
|
1821
|
+
} else {
|
|
1822
|
+
_ref0 = undefined;
|
|
1823
|
+
}
|
|
1824
|
+
_ref = _ref0;
|
|
1825
|
+
break;
|
|
1826
|
+
case "assistant":
|
|
1827
|
+
_ref = (function(res) {
|
|
1828
|
+
if (((typeof msg.content === "string") || (msg.content instanceof String))) {
|
|
1829
|
+
res.push(mkline("assistant", msg.content));
|
|
1830
|
+
} else if (Array.isArray(msg.content)) {
|
|
1831
|
+
res.push(msg.content
|
|
1832
|
+
.map((function(item) {
|
|
1833
|
+
var _ref1;
|
|
1834
|
+
switch (item.type) {
|
|
1835
|
+
case "text":
|
|
1836
|
+
_ref1 = mkline("assistant", item.text);
|
|
1837
|
+
break;
|
|
1838
|
+
case "tool_use":
|
|
1839
|
+
_ref1 = tpl("{role}: {name} {args}", {
|
|
1840
|
+
role: (long ? "tool_call" : "tc"),
|
|
1841
|
+
name: item.name,
|
|
1842
|
+
args: (function(it) {
|
|
1843
|
+
it = ((it.text && (1 === Object.keys(it).length)) ? it.text : JSON.stringify(it));
|
|
1844
|
+
return it;
|
|
1845
|
+
})(item.input)
|
|
1846
|
+
});
|
|
1847
|
+
break;
|
|
1848
|
+
default:
|
|
1849
|
+
_ref1 = undefined;
|
|
1850
|
+
throw new Error(("unsupported user content type: " + item.type));
|
|
1851
|
+
}
|
|
1852
|
+
return _ref1;
|
|
1853
|
+
}))
|
|
1854
|
+
.join("\n"));
|
|
1855
|
+
}
|
|
1856
|
+
if (msg.audio) res.push(tpl("{role}: @{id} {expires_at}\n{transcript}", extend(msg.audio, {
|
|
1857
|
+
role: (long ? "audio" : "au")
|
|
1858
|
+
})));
|
|
1859
|
+
if (msg.tool_calls) res.push(msg.tool_calls
|
|
1860
|
+
.map((function(tc) {
|
|
1861
|
+
var args, text;
|
|
1862
|
+
var args;
|
|
1863
|
+
var text;
|
|
1864
|
+
args = dJSON.parse(tc.function.arguments || "{}");
|
|
1865
|
+
text = args.text;
|
|
1866
|
+
delete args.text;
|
|
1867
|
+
args = (Object.keys(args).length ? JSON.stringify(args) : undefined);
|
|
1868
|
+
return tpl("{role}: {name}{ args}{\ntext}", {
|
|
1869
|
+
name: tc.function.name,
|
|
1870
|
+
args: args,
|
|
1871
|
+
text: text,
|
|
1872
|
+
role: (long ? "tool_call" : "tc")
|
|
1873
|
+
});
|
|
1874
|
+
}))
|
|
1875
|
+
.join("\n"));
|
|
1876
|
+
return res.join("\n");
|
|
1877
|
+
})([]);
|
|
1878
|
+
break;
|
|
1879
|
+
case "tool":
|
|
1880
|
+
_ref = mkline("tool_result", msg.content);
|
|
1881
|
+
break;
|
|
1882
|
+
case "error":
|
|
1883
|
+
_ref = mkline("error ", msg.content);
|
|
1884
|
+
break;
|
|
1885
|
+
default:
|
|
1886
|
+
_ref = undefined;
|
|
1887
|
+
}
|
|
1888
|
+
_ref1 = _ref;
|
|
1889
|
+
}
|
|
1890
|
+
return _ref1;
|
|
1891
|
+
}
|
|
1892
|
+
msg2text;
|
|
1893
|
+
|
|
1894
|
+
function msg2role(msg) {
|
|
1895
|
+
return (((msg.role === "assistant") && msg.tool_calls) ? msg.tool_calls
|
|
1896
|
+
.map((function(tc) {
|
|
1897
|
+
return {
|
|
1898
|
+
role: "tool_call",
|
|
1899
|
+
content: " " + tc.function.name + " " + tc.function.arguments
|
|
1900
|
+
}
|
|
1901
|
+
})) : Array({
|
|
1902
|
+
role: msg.role,
|
|
1903
|
+
content: " " + msg.content
|
|
1904
|
+
}));
|
|
1905
|
+
}
|
|
1906
|
+
msg2role;
|
|
1907
|
+
|
|
1908
|
+
function escape(text) {
|
|
1909
|
+
return String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)))
|
|
1910
|
+
.replace(/{(\s*\w+\s*)}/g, "{!$1}")
|
|
1911
|
+
.replace(/^(s|u|a|c|tr|tc|err):/gm, "\\$1:");
|
|
1912
|
+
}
|
|
1913
|
+
escape;
|
|
1914
|
+
|
|
1915
|
+
function unescape(text) {
|
|
1916
|
+
return String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)))
|
|
1917
|
+
.replace(/\\(?<item>@{1,2}\{\s*[~\.\-\w/]+\s*)(?<proc>(?:\s*\|\s*\w+(?:\s+\w+)*)*\})/g, "$<item>$<proc>")
|
|
1918
|
+
.replace(/\\(?<item>@{1,2}[~\.\-\w/]+)/g, "$<item>")
|
|
1919
|
+
.replace(/^\\(s|system|u|user|a|assistant|c|comment|tr|tool_result|tc|tool_call|err|error):/gm, "$1:");
|
|
1920
|
+
}
|
|
1921
|
+
unescape;
|
|
1922
|
+
|
|
1923
|
+
function text2cut(text, cursor) {
|
|
1924
|
+
return (function(line) {
|
|
1925
|
+
return (function(it) {
|
|
1926
|
+
it = it.map((function(item) {
|
|
1927
|
+
item.start = line;
|
|
1928
|
+
line = line + item.content.split("\n").length;
|
|
1929
|
+
item.end = line;
|
|
1930
|
+
if (((item.role === "comment") && item.content.match(/^\s*\-{3,}\s*$/))) item.delim = true;
|
|
1931
|
+
return item;
|
|
1932
|
+
}));
|
|
1933
|
+
it = it.reduce((function(memo, item, index) {
|
|
1934
|
+
if (item.delim) {
|
|
1935
|
+
if (((cursor < item.end) && (cursor >= item.start))) {
|
|
1936
|
+
memo = {
|
|
1937
|
+
start: item.start,
|
|
1938
|
+
mid: item.start,
|
|
1939
|
+
end: item.start
|
|
1940
|
+
};
|
|
1941
|
+
} else if (item.end <= cursor) {
|
|
1942
|
+
memo.start = item.end;
|
|
1943
|
+
} else if ((cursor < item.start) && (item.end < memo.end)) {
|
|
1944
|
+
memo.end = item.start;
|
|
1945
|
+
}
|
|
1946
|
+
} else if ((item.start <= cursor) && (item.end >= cursor)) {
|
|
1947
|
+
memo.mid = item.end;
|
|
1948
|
+
}
|
|
1949
|
+
return memo;
|
|
1950
|
+
}), {
|
|
1951
|
+
start: it[0].start,
|
|
1952
|
+
mid: cursor,
|
|
1953
|
+
end: it["slice"](-1)[0].end
|
|
1954
|
+
});
|
|
1955
|
+
return it;
|
|
1956
|
+
})(text2roles(text));
|
|
1957
|
+
})(0);
|
|
1958
|
+
}
|
|
1959
|
+
text2cut;
|
|
1960
|
+
|
|
1961
|
+
function tpl(str) {
|
|
1962
|
+
var _i;
|
|
1963
|
+
var params = 2 <= arguments.length ? [].slice.call(arguments, 1, _i = arguments.length - 0) : (_i = 1, []);
|
|
1964
|
+
return (function(paramIndex, params) {
|
|
1965
|
+
var _ref;
|
|
1966
|
+
try {
|
|
1967
|
+
_ref = str.replace(/{(\W*)(\w*)(\W*)}/gm, (function(_, pre, name, post) {
|
|
1968
|
+
return (function(res) {
|
|
1969
|
+
paramIndex += 1;
|
|
1970
|
+
return (res ? ((pre || "") + res + (post || "")) : "");
|
|
1971
|
+
})(params[name || paramIndex]);
|
|
1972
|
+
}));
|
|
1973
|
+
} catch (e) {
|
|
1974
|
+
_ref = console.log.apply(console, [].concat([e, str]).concat(params));
|
|
1975
|
+
}
|
|
1976
|
+
return _ref;
|
|
1977
|
+
})(0, (((typeof params[0] === "object") && (params.length === 1)) ? params[0] : params));
|
|
1978
|
+
}
|
|
1979
|
+
tpl;
|
|
1980
|
+
|
|
1981
|
+
function _once(cond, body) {
|
|
1982
|
+
return new Promise((function(resolve, reject) {
|
|
1983
|
+
function handler() {
|
|
1984
|
+
var _ref;
|
|
1985
|
+
try {
|
|
1986
|
+
_ref = cond() ? resolve(body()) : setTimeout(handler, 10);
|
|
1987
|
+
} catch (err) {
|
|
1988
|
+
_ref = reject(err);
|
|
1989
|
+
}
|
|
1990
|
+
return _ref;
|
|
1991
|
+
}
|
|
1992
|
+
setTimeout(handler, 10);
|
|
1993
|
+
return "";
|
|
1994
|
+
}));
|
|
1995
|
+
}
|
|
1996
|
+
_once;
|
|
1997
|
+
|
|
1998
|
+
function pick(obj) {
|
|
1999
|
+
var _i;
|
|
2000
|
+
var props = 2 <= arguments.length ? [].slice.call(arguments, 1, _i = arguments.length - 0) : (_i = 1, []);
|
|
2001
|
+
return (function(it) {
|
|
2002
|
+
var prop, _i0, _ref, _len;
|
|
2003
|
+
_ref = props;
|
|
2004
|
+
for (_i0 = 0, _len = _ref.length; _i0 < _len; ++_i0) {
|
|
2005
|
+
prop = _ref[_i0];
|
|
2006
|
+
it[prop] = obj[prop];
|
|
2007
|
+
}
|
|
2008
|
+
return it;
|
|
2009
|
+
})({});
|
|
2010
|
+
}
|
|
2011
|
+
pick;
|
|
2012
|
+
fs = require("fs");
|
|
2013
|
+
assert = require("node:assert/strict");
|
|
2014
|
+
util = require("util");
|
|
2015
|
+
exports.makeContext = makeContext;
|
|
2016
|
+
exports.text2roles = text2roles;
|
|
2017
|
+
exports.roles2text = roles2text;
|
|
2018
|
+
exports.text2call = text2call;
|
|
2019
|
+
exports.text2ast = text2ast;
|
|
2020
|
+
exports.ast2payload = ast2payload;
|
|
2021
|
+
exports.toolCall = toolCall;
|
|
2022
|
+
exports.text2run = text2run;
|
|
2023
|
+
exports.msg2text = msg2text;
|
|
2024
|
+
exports.msg2role = msg2role;
|
|
2025
|
+
exports.text2cut = text2cut;
|
|
2026
|
+
exports.TuneError = TuneError;
|
|
2027
|
+
exports.text2cut = text2cut;
|
|
2028
|
+
exports.text2payload = text2payload;
|
|
2029
|
+
exports.payload2http = payload2http;
|
|
2030
|
+
exports.envmd = envmd;
|
|
2031
|
+
exports.unescape = unescape;
|
|
2032
|
+
exports.escape = escape;
|