tune-sdk 0.2.2 → 0.2.4

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/dist/fsctx.js DELETED
@@ -1,1139 +0,0 @@
1
- var path, tune, fs, envmd, TuneError, text2run, file2run;
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
- let msgpack = (function() {
22
- "use strict";
23
-
24
- // Serializes a value to a MessagePack byte array.
25
- //
26
- // data: The value to serialize. This can be a scalar, array or object.
27
- // options: An object that defines additional options.
28
- // - multiple: (boolean) Indicates whether multiple values in data are concatenated to multiple MessagePack arrays. Default: false.
29
- // - invalidTypeReplacement:
30
- // (any) The value that is used to replace values of unsupported types.
31
- // (function) A function that returns such a value, given the original value as parameter.
32
- function serialize(data, options) {
33
- if (options && options.multiple && !Array.isArray(data)) {
34
- throw new Error(
35
- "Invalid argument type: Expected an Array to serialize multiple values.",
36
- );
37
- }
38
- const pow32 = 0x100000000; // 2^32
39
- let floatBuffer, floatView;
40
- let array = new Uint8Array(128);
41
- let length = 0;
42
- if (options && options.multiple) {
43
- for (let i = 0; i < data.length; i++) {
44
- append(data[i]);
45
- }
46
- } else {
47
- append(data);
48
- }
49
- return array.subarray(0, length);
50
-
51
- function append(data, isReplacement) {
52
- switch (typeof data) {
53
- case "undefined":
54
- appendNull(data);
55
- break;
56
- case "boolean":
57
- appendBoolean(data);
58
- break;
59
- case "number":
60
- appendNumber(data);
61
- break;
62
- case "string":
63
- appendString(data);
64
- break;
65
- case "object":
66
- if (data === null) appendNull(data);
67
- else if (data instanceof Date) appendDate(data);
68
- else if (Array.isArray(data)) appendArray(data);
69
- else if (
70
- data instanceof Uint8Array ||
71
- data instanceof Uint8ClampedArray
72
- )
73
- appendBinArray(data);
74
- else if (
75
- data instanceof Int8Array ||
76
- data instanceof Int16Array ||
77
- data instanceof Uint16Array ||
78
- data instanceof Int32Array ||
79
- data instanceof Uint32Array ||
80
- data instanceof Float32Array ||
81
- data instanceof Float64Array
82
- )
83
- appendArray(data);
84
- else appendObject(data);
85
- break;
86
- default:
87
- if (!isReplacement && options && options.invalidTypeReplacement) {
88
- if (typeof options.invalidTypeReplacement === "function")
89
- append(options.invalidTypeReplacement(data), true);
90
- else append(options.invalidTypeReplacement, true);
91
- } else {
92
- throw new Error(
93
- "Invalid argument type: The type '" +
94
- typeof data +
95
- "' cannot be serialized.",
96
- );
97
- }
98
- }
99
- }
100
-
101
- function appendNull(data) {
102
- appendByte(0xc0);
103
- }
104
-
105
- function appendBoolean(data) {
106
- appendByte(data ? 0xc3 : 0xc2);
107
- }
108
-
109
- function appendNumber(data) {
110
- if (isFinite(data) && Number.isSafeInteger(data)) {
111
- // Integer
112
- if (data >= 0 && data <= 0x7f) {
113
- appendByte(data);
114
- } else if (data < 0 && data >= -0x20) {
115
- appendByte(data);
116
- } else if (data > 0 && data <= 0xff) {
117
- // uint8
118
- appendBytes([0xcc, data]);
119
- } else if (data >= -0x80 && data <= 0x7f) {
120
- // int8
121
- appendBytes([0xd0, data]);
122
- } else if (data > 0 && data <= 0xffff) {
123
- // uint16
124
- appendBytes([0xcd, data >>> 8, data]);
125
- } else if (data >= -0x8000 && data <= 0x7fff) {
126
- // int16
127
- appendBytes([0xd1, data >>> 8, data]);
128
- } else if (data > 0 && data <= 0xffffffff) {
129
- // uint32
130
- appendBytes([0xce, data >>> 24, data >>> 16, data >>> 8, data]);
131
- } else if (data >= -0x80000000 && data <= 0x7fffffff) {
132
- // int32
133
- appendBytes([0xd2, data >>> 24, data >>> 16, data >>> 8, data]);
134
- } else if (data > 0 && data <= 0xffffffffffffffff) {
135
- // uint64
136
- appendByte(0xcf);
137
- appendInt64(data);
138
- } else if (data >= -0x8000000000000000 && data <= 0x7fffffffffffffff) {
139
- // int64
140
- appendByte(0xd3);
141
- appendInt64(data);
142
- } else if (data < 0) {
143
- // below int64
144
- appendBytes([0xd3, 0x80, 0, 0, 0, 0, 0, 0, 0]);
145
- } else {
146
- // above uint64
147
- appendBytes([0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
148
- }
149
- } else {
150
- // Float
151
- if (!floatView) {
152
- floatBuffer = new ArrayBuffer(8);
153
- floatView = new DataView(floatBuffer);
154
- }
155
- floatView.setFloat64(0, data);
156
- appendByte(0xcb);
157
- appendBytes(new Uint8Array(floatBuffer));
158
- }
159
- }
160
-
161
- function appendString(data) {
162
- let bytes = encodeUtf8(data);
163
- let length = bytes.length;
164
-
165
- if (length <= 0x1f) appendByte(0xa0 + length);
166
- else if (length <= 0xff) appendBytes([0xd9, length]);
167
- else if (length <= 0xffff) appendBytes([0xda, length >>> 8, length]);
168
- else
169
- appendBytes([0xdb, length >>> 24, length >>> 16, length >>> 8, length]);
170
-
171
- appendBytes(bytes);
172
- }
173
-
174
- function appendArray(data) {
175
- let length = data.length;
176
-
177
- if (length <= 0xf) appendByte(0x90 + length);
178
- else if (length <= 0xffff) appendBytes([0xdc, length >>> 8, length]);
179
- else
180
- appendBytes([0xdd, length >>> 24, length >>> 16, length >>> 8, length]);
181
-
182
- for (let index = 0; index < length; index++) {
183
- append(data[index]);
184
- }
185
- }
186
-
187
- function appendBinArray(data) {
188
- let length = data.length;
189
-
190
- if (length <= 0xff) appendBytes([0xc4, length]);
191
- else if (length <= 0xffff) appendBytes([0xc5, length >>> 8, length]);
192
- else
193
- appendBytes([0xc6, length >>> 24, length >>> 16, length >>> 8, length]);
194
-
195
- appendBytes(data);
196
- }
197
-
198
- function appendObject(data) {
199
- let length = 0;
200
- for (let key in data) {
201
- if (data[key] !== undefined) {
202
- length++;
203
- }
204
- }
205
-
206
- if (length <= 0xf) appendByte(0x80 + length);
207
- else if (length <= 0xffff) appendBytes([0xde, length >>> 8, length]);
208
- else
209
- appendBytes([0xdf, length >>> 24, length >>> 16, length >>> 8, length]);
210
-
211
- for (let key in data) {
212
- let value = data[key];
213
- if (value !== undefined) {
214
- append(key);
215
- append(value);
216
- }
217
- }
218
- }
219
-
220
- function appendDate(data) {
221
- let sec = data.getTime() / 1000;
222
- if (data.getMilliseconds() === 0 && sec >= 0 && sec < 0x100000000) {
223
- // 32 bit seconds
224
- appendBytes([0xd6, 0xff, sec >>> 24, sec >>> 16, sec >>> 8, sec]);
225
- } else if (sec >= 0 && sec < 0x400000000) {
226
- // 30 bit nanoseconds, 34 bit seconds
227
- let ns = data.getMilliseconds() * 1000000;
228
- appendBytes([
229
- 0xd7,
230
- 0xff,
231
- ns >>> 22,
232
- ns >>> 14,
233
- ns >>> 6,
234
- ((ns << 2) >>> 0) | (sec / pow32),
235
- sec >>> 24,
236
- sec >>> 16,
237
- sec >>> 8,
238
- sec,
239
- ]);
240
- } else {
241
- // 32 bit nanoseconds, 64 bit seconds, negative values allowed
242
- let ns = data.getMilliseconds() * 1000000;
243
- appendBytes([0xc7, 12, 0xff, ns >>> 24, ns >>> 16, ns >>> 8, ns]);
244
- appendInt64(sec);
245
- }
246
- }
247
-
248
- function appendByte(byte) {
249
- if (array.length < length + 1) {
250
- let newLength = array.length * 2;
251
- while (newLength < length + 1) newLength *= 2;
252
- let newArray = new Uint8Array(newLength);
253
- newArray.set(array);
254
- array = newArray;
255
- }
256
- array[length] = byte;
257
- length++;
258
- }
259
-
260
- function appendBytes(bytes) {
261
- if (array.length < length + bytes.length) {
262
- let newLength = array.length * 2;
263
- while (newLength < length + bytes.length) newLength *= 2;
264
- let newArray = new Uint8Array(newLength);
265
- newArray.set(array);
266
- array = newArray;
267
- }
268
- array.set(bytes, length);
269
- length += bytes.length;
270
- }
271
-
272
- function appendInt64(value) {
273
- // Split 64 bit number into two 32 bit numbers because JavaScript only regards 32 bits for
274
- // bitwise operations.
275
- let hi, lo;
276
- if (value >= 0) {
277
- // Same as uint64
278
- hi = value / pow32;
279
- lo = value % pow32;
280
- } else {
281
- // Split absolute value to high and low, then NOT and ADD(1) to restore negativity
282
- value++;
283
- hi = Math.abs(value) / pow32;
284
- lo = Math.abs(value) % pow32;
285
- hi = ~hi;
286
- lo = ~lo;
287
- }
288
- appendBytes([
289
- hi >>> 24,
290
- hi >>> 16,
291
- hi >>> 8,
292
- hi,
293
- lo >>> 24,
294
- lo >>> 16,
295
- lo >>> 8,
296
- lo,
297
- ]);
298
- }
299
- }
300
-
301
- // Deserializes a MessagePack byte array to a value.
302
- //
303
- // array: The MessagePack byte array to deserialize. This must be an Array or Uint8Array containing bytes, not a string.
304
- // options: An object that defines additional options.
305
- // - multiple: (boolean) Indicates whether multiple concatenated MessagePack arrays are returned as an array. Default: false.
306
- function deserialize(array, options) {
307
- const pow32 = 0x100000000; // 2^32
308
- let pos = 0;
309
- if (array instanceof ArrayBuffer) {
310
- array = new Uint8Array(array);
311
- }
312
- if (typeof array !== "object" || typeof array.length === "undefined") {
313
- throw new Error(
314
- "Invalid argument type: Expected a byte array (Array or Uint8Array) to deserialize.",
315
- );
316
- }
317
- if (!array.length) {
318
- throw new Error(
319
- "Invalid argument: The byte array to deserialize is empty.",
320
- );
321
- }
322
- if (!(array instanceof Uint8Array)) {
323
- array = new Uint8Array(array);
324
- }
325
- let data;
326
- if (options && options.multiple) {
327
- // Read as many messages as are available
328
- data = [];
329
- while (pos < array.length) {
330
- data.push(read());
331
- }
332
- } else {
333
- // Read only one message and ignore additional data
334
- data = read();
335
- }
336
- return data;
337
-
338
- function read() {
339
- const byte = array[pos++];
340
- if (byte >= 0x00 && byte <= 0x7f) return byte; // positive fixint
341
- if (byte >= 0x80 && byte <= 0x8f) return readMap(byte - 0x80); // fixmap
342
- if (byte >= 0x90 && byte <= 0x9f) return readArray(byte - 0x90); // fixarray
343
- if (byte >= 0xa0 && byte <= 0xbf) return readStr(byte - 0xa0); // fixstr
344
- if (byte === 0xc0) return null; // nil
345
- if (byte === 0xc1) throw new Error("Invalid byte code 0xc1 found."); // never used
346
- if (byte === 0xc2) return false; // false
347
- if (byte === 0xc3) return true; // true
348
- if (byte === 0xc4) return readBin(-1, 1); // bin 8
349
- if (byte === 0xc5) return readBin(-1, 2); // bin 16
350
- if (byte === 0xc6) return readBin(-1, 4); // bin 32
351
- if (byte === 0xc7) return readExt(-1, 1); // ext 8
352
- if (byte === 0xc8) return readExt(-1, 2); // ext 16
353
- if (byte === 0xc9) return readExt(-1, 4); // ext 32
354
- if (byte === 0xca) return readFloat(4); // float 32
355
- if (byte === 0xcb) return readFloat(8); // float 64
356
- if (byte === 0xcc) return readUInt(1); // uint 8
357
- if (byte === 0xcd) return readUInt(2); // uint 16
358
- if (byte === 0xce) return readUInt(4); // uint 32
359
- if (byte === 0xcf) return readUInt(8); // uint 64
360
- if (byte === 0xd0) return readInt(1); // int 8
361
- if (byte === 0xd1) return readInt(2); // int 16
362
- if (byte === 0xd2) return readInt(4); // int 32
363
- if (byte === 0xd3) return readInt(8); // int 64
364
- if (byte === 0xd4) return readExt(1); // fixext 1
365
- if (byte === 0xd5) return readExt(2); // fixext 2
366
- if (byte === 0xd6) return readExt(4); // fixext 4
367
- if (byte === 0xd7) return readExt(8); // fixext 8
368
- if (byte === 0xd8) return readExt(16); // fixext 16
369
- if (byte === 0xd9) return readStr(-1, 1); // str 8
370
- if (byte === 0xda) return readStr(-1, 2); // str 16
371
- if (byte === 0xdb) return readStr(-1, 4); // str 32
372
- if (byte === 0xdc) return readArray(-1, 2); // array 16
373
- if (byte === 0xdd) return readArray(-1, 4); // array 32
374
- if (byte === 0xde) return readMap(-1, 2); // map 16
375
- if (byte === 0xdf) return readMap(-1, 4); // map 32
376
- if (byte >= 0xe0 && byte <= 0xff) return byte - 256; // negative fixint
377
- console.debug("msgpack array:", array);
378
- throw new Error(
379
- "Invalid byte value '" +
380
- byte +
381
- "' at index " +
382
- (pos - 1) +
383
- " in the MessagePack binary data (length " +
384
- array.length +
385
- "): Expecting a range of 0 to 255. This is not a byte array.",
386
- );
387
- }
388
-
389
- function readInt(size) {
390
- let value = 0;
391
- let first = true;
392
- while (size-- > 0) {
393
- if (first) {
394
- let byte = array[pos++];
395
- value += byte & 0x7f;
396
- if (byte & 0x80) {
397
- value -= 0x80; // Treat most-significant bit as -2^i instead of 2^i
398
- }
399
- first = false;
400
- } else {
401
- value *= 256;
402
- value += array[pos++];
403
- }
404
- }
405
- return value;
406
- }
407
-
408
- function readUInt(size) {
409
- let value = 0;
410
- while (size-- > 0) {
411
- value *= 256;
412
- value += array[pos++];
413
- }
414
- return value;
415
- }
416
-
417
- function readFloat(size) {
418
- let view = new DataView(array.buffer, pos + array.byteOffset, size);
419
- pos += size;
420
- if (size === 4) return view.getFloat32(0, false);
421
- if (size === 8) return view.getFloat64(0, false);
422
- }
423
-
424
- function readBin(size, lengthSize) {
425
- if (size < 0) size = readUInt(lengthSize);
426
- let data = array.subarray(pos, pos + size);
427
- pos += size;
428
- return data;
429
- }
430
-
431
- function readMap(size, lengthSize) {
432
- if (size < 0) size = readUInt(lengthSize);
433
- let data = {};
434
- while (size-- > 0) {
435
- let key = read();
436
- data[key] = read();
437
- }
438
- return data;
439
- }
440
-
441
- function readArray(size, lengthSize) {
442
- if (size < 0) size = readUInt(lengthSize);
443
- let data = [];
444
- while (size-- > 0) {
445
- data.push(read());
446
- }
447
- return data;
448
- }
449
-
450
- function readStr(size, lengthSize) {
451
- if (size < 0) size = readUInt(lengthSize);
452
- let start = pos;
453
- pos += size;
454
- return decodeUtf8(array, start, size);
455
- }
456
-
457
- function readExt(size, lengthSize) {
458
- if (size < 0) size = readUInt(lengthSize);
459
- let type = readUInt(1);
460
- let data = readBin(size);
461
- switch (type) {
462
- case 255:
463
- return readExtDate(data);
464
- }
465
- return {
466
- type: type,
467
- data: data
468
- };
469
- }
470
-
471
- function readExtDate(data) {
472
- if (data.length === 4) {
473
- let sec =
474
- ((data[0] << 24) >>> 0) +
475
- ((data[1] << 16) >>> 0) +
476
- ((data[2] << 8) >>> 0) +
477
- data[3];
478
- return new Date(sec * 1000);
479
- }
480
- if (data.length === 8) {
481
- let ns =
482
- ((data[0] << 22) >>> 0) +
483
- ((data[1] << 14) >>> 0) +
484
- ((data[2] << 6) >>> 0) +
485
- (data[3] >>> 2);
486
- let sec =
487
- (data[3] & 0x3) * pow32 +
488
- ((data[4] << 24) >>> 0) +
489
- ((data[5] << 16) >>> 0) +
490
- ((data[6] << 8) >>> 0) +
491
- data[7];
492
- return new Date(sec * 1000 + ns / 1000000);
493
- }
494
- if (data.length === 12) {
495
- let ns =
496
- ((data[0] << 24) >>> 0) +
497
- ((data[1] << 16) >>> 0) +
498
- ((data[2] << 8) >>> 0) +
499
- data[3];
500
- pos -= 8;
501
- let sec = readInt(8);
502
- return new Date(sec * 1000 + ns / 1000000);
503
- }
504
- throw new Error("Invalid data length for a date value.");
505
- }
506
- }
507
-
508
- // Encodes a string to UTF-8 bytes.
509
- function encodeUtf8(str) {
510
- // Prevent excessive array allocation and slicing for all 7-bit characters
511
- let ascii = true,
512
- length = str.length;
513
- for (let x = 0; x < length; x++) {
514
- if (str.charCodeAt(x) > 127) {
515
- ascii = false;
516
- break;
517
- }
518
- }
519
-
520
- // Based on: https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330
521
- let i = 0,
522
- bytes = new Uint8Array(str.length * (ascii ? 1 : 4));
523
- for (let ci = 0; ci !== length; ci++) {
524
- let c = str.charCodeAt(ci);
525
- if (c < 128) {
526
- bytes[i++] = c;
527
- continue;
528
- }
529
- if (c < 2048) {
530
- bytes[i++] = (c >> 6) | 192;
531
- } else {
532
- if (c > 0xd7ff && c < 0xdc00) {
533
- if (++ci >= length)
534
- throw new Error("UTF-8 encode: incomplete surrogate pair");
535
- let c2 = str.charCodeAt(ci);
536
- if (c2 < 0xdc00 || c2 > 0xdfff)
537
- throw new Error(
538
- "UTF-8 encode: second surrogate character 0x" +
539
- c2.toString(16) +
540
- " at index " +
541
- ci +
542
- " out of range",
543
- );
544
- c = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
545
- bytes[i++] = (c >> 18) | 240;
546
- bytes[i++] = ((c >> 12) & 63) | 128;
547
- } else bytes[i++] = (c >> 12) | 224;
548
- bytes[i++] = ((c >> 6) & 63) | 128;
549
- }
550
- bytes[i++] = (c & 63) | 128;
551
- }
552
- return ascii ? bytes : bytes.subarray(0, i);
553
- }
554
-
555
- // Decodes a string from UTF-8 bytes.
556
- function decodeUtf8(bytes, start, length) {
557
- // Based on: https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330
558
- let i = start,
559
- str = "";
560
- length += start;
561
- while (i < length) {
562
- let c = bytes[i++];
563
- if (c > 127) {
564
- if (c > 191 && c < 224) {
565
- if (i >= length)
566
- throw new Error("UTF-8 decode: incomplete 2-byte sequence");
567
- c = ((c & 31) << 6) | (bytes[i++] & 63);
568
- } else if (c > 223 && c < 240) {
569
- if (i + 1 >= length)
570
- throw new Error("UTF-8 decode: incomplete 3-byte sequence");
571
- c = ((c & 15) << 12) | ((bytes[i++] & 63) << 6) | (bytes[i++] & 63);
572
- } else if (c > 239 && c < 248) {
573
- if (i + 2 >= length)
574
- throw new Error("UTF-8 decode: incomplete 4-byte sequence");
575
- c =
576
- ((c & 7) << 18) |
577
- ((bytes[i++] & 63) << 12) |
578
- ((bytes[i++] & 63) << 6) |
579
- (bytes[i++] & 63);
580
- } else
581
- throw new Error(
582
- "UTF-8 decode: unknown multibyte start 0x" +
583
- c.toString(16) +
584
- " at index " +
585
- (i - 1),
586
- );
587
- }
588
- if (c <= 0xffff) str += String.fromCharCode(c);
589
- else if (c <= 0x10ffff) {
590
- c -= 0x10000;
591
- str += String.fromCharCode((c >> 10) | 0xd800);
592
- str += String.fromCharCode((c & 0x3ff) | 0xdc00);
593
- } else
594
- throw new Error(
595
- "UTF-8 decode: code point 0x" +
596
- c.toString(16) +
597
- " exceeds UTF-16 reach",
598
- );
599
- }
600
- return str;
601
- }
602
-
603
- // The exported functions
604
- return {
605
- serialize: serialize,
606
- deserialize: deserialize,
607
-
608
- // Compatibility with other libraries
609
- encode: serialize,
610
- decode: deserialize,
611
- };
612
- })();
613
-
614
- if (typeof window !== "undefined") {
615
- window.msgpack = msgpack;
616
- }
617
- path = require("path");
618
- tune = require("./tune");
619
- fs = require("fs");
620
- envmd = tune.envmd;
621
- TuneError = tune.TuneError;
622
- text2run = tune.text2run;
623
- file2run = tune.file2run;
624
-
625
- function env2vars(text) {
626
- return text
627
- .split(/^(\w+\s*=)/gm)
628
- .reduce((function(memo, item, index, arr) {
629
- (function(it) {
630
- return (it ? memo.push({
631
- name: it[1],
632
- content: arr[index + 1]
633
- .replace(/\n$/, "")
634
- }) : undefined);
635
- })(item.match(/^(\w+)\s*=/));
636
- return memo;
637
- }), [])
638
- .reduce((function(memo, item) {
639
- memo[item.name] = item
640
- .content.replace(new RegExp("^\\s*'(.*)'\\s*$"), "$1")
641
- .replace(new RegExp("^\\s*\"(.*)\"\\s*$"), "$1");
642
- return memo;
643
- }), {});
644
- }
645
- env2vars;
646
-
647
- function pparse(filename) {
648
- var parsed, parsed1;
649
- var parsed;
650
- var parsed1;
651
- parsed = path.parse(filename);
652
- parsed1 = path.parse(parsed.name);
653
- if (parsed1.ext) {
654
- parsed.ext2 = parsed1.ext;
655
- parsed.name = parsed1.name;
656
- }
657
- return parsed;
658
- }
659
- pparse;
660
- async function runFile(filename, ctx) {
661
- var parsed, res, module, spawnSync, result, sres, _i, _ref, _ref0, _ref1, _ref2, _ref3;
662
- var args = 3 <= arguments.length ? [].slice.call(arguments, 2, _i = arguments.length - 0) : (_i = 2, []);
663
- try {
664
- var parsed;
665
- parsed = pparse(filename);
666
- if ((parsed.ext === ".chat")) {
667
- var res;
668
- res = await ctx.file2run({
669
- filename: filename,
670
- stop: "assistant"
671
- }, args[0]);
672
- _ref = res.replace(/@/g, "\\@");
673
- } else if (parsed.ext === ".mjs") {
674
- var module;
675
- module = await import(filename + "?t=" + Date.now());
676
- if ((typeof module.default !== "function")) throw Error("JS file does not export default function");
677
- _ref = module.default.call.apply(module.default, [].concat([ctx]).concat(args).concat([ctx]));
678
- } else if (parsed.ext === ".js" || parsed.ext === ".cjs") {
679
- var module;
680
- module = require(filename);
681
- if ((typeof module !== "function")) throw Error("JS file does not export default function");
682
- _ref = module.call.apply(module, [].concat([ctx]).concat(args).concat([ctx]));
683
- } else if (parsed.ext === ".py" || parsed.ext === ".php") {
684
- var spawnSync;
685
- spawnSync = require("child_process").spawnSync;
686
- var res;
687
- switch (parsed.ext) {
688
- case ".py":
689
- _ref0 = "python";
690
- break;
691
- case ".php":
692
- _ref0 = "php";
693
- break;
694
- default:
695
- _ref0 = undefined;
696
- }
697
- switch (parsed.ext) {
698
- case ".py":
699
- _ref1 = "run.py";
700
- break;
701
- case ".php":
702
- _ref1 = "run.php";
703
- break;
704
- default:
705
- _ref1 = undefined;
706
- }
707
- res = spawnSync(_ref0, Array(path.resolve(__dirname, _ref1)), {
708
- input: JSON.stringify({
709
- filename: filename,
710
- arguments: args[0],
711
- ctx: ""
712
- }),
713
- env: extend(process.env, ctx.env)
714
- });
715
- if (res.error) throw res.error;
716
- var result;
717
- result = Array();
718
- if (res.stderr.length) result.push(res.stderr.toString("utf8"));
719
- if (res.stdout.length) {
720
- var sres;
721
- try {
722
- _ref2 = msgpack.deserialize(Buffer.from(res.stdout.toString("utf8"), "hex"));
723
- } catch (e) {
724
- console.log("cant decode messageback", e);
725
- _ref2 = res.stdout.toString("utf8");
726
- }
727
- sres = _ref2;
728
- Array.isArray(sres) ? result = result.concat(sres) : result.push(sres);
729
- }
730
- if ((result.length === 1)) result = result[0];
731
- _ref = result;
732
- } else {
733
- _ref = undefined;
734
- throw Error(tpl("{ext} extension is not supported, for {filename} ", {
735
- ext: parsed.ext,
736
- filename: filename
737
- }));
738
- }
739
- _ref3 = _ref;
740
- } catch (e) {
741
- throw e;
742
- }
743
- return _ref3;
744
- }
745
- runFile;
746
-
747
- function fsMod(opts, fs) {
748
- async function toolsMd(name, args) {
749
- var self, result, prefix, parsed, dir, re, item, parsed1, fileType, fullname, schemaFile, schema, res, _i, _ref, _len, _ref0, _ref1;
750
- fs = fs || require("fs");
751
- if (!(((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.path !== "undefined") && (opts.path !== null) && !Number.isNaN(opts.path)) ? opts.path : undefined)) throw Error("path is not set");
752
- var self;
753
- var result;
754
- self = this;
755
- result = ((args.output === "all") ? [] : undefined);
756
- if (!name) return;
757
- name = path.normalize(name);
758
- if (opts.mount) {
759
- var prefix;
760
- prefix = path.normalize(opts.mount + "/");
761
- if ((0 === name.indexOf(prefix))) {
762
- name = name.substr(prefix.length);
763
- } else {
764
- return;
765
- }
766
- }
767
- var parsed;
768
- var dir;
769
- var re;
770
- parsed = ((args.match === "exact") ? pparse(path.resolve(opts.path, name)) : undefined);
771
- dir = (((typeof parsed !== "undefined") && (parsed !== null) && !Number.isNaN(parsed) && (typeof parsed.dir !== "undefined") && (parsed.dir !== null) && !Number.isNaN(parsed.dir)) ? parsed.dir : (((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.path !== "undefined") && (opts.path !== null) && !Number.isNaN(opts.path)) ? opts.path : undefined));
772
- re = ((args.match === "regex") ? new RegExp(name) : undefined);
773
- if (!fs.existsSync(dir)) return;
774
- _ref = fs.readdirSync(dir);
775
- for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
776
- item = _ref[_i];
777
- var parsed1;
778
- parsed1 = pparse(path.join(dir, item));
779
- if (((parsed1.ext2 !== ".tool" && parsed1.ext2 !== ".llm" && parsed1.ext2 !== ".proc" && parsed1.ext2 !== ".ctx") || (parsed1.ext !== ".js" && parsed1.ext !== ".mjs" && parsed1.ext !== ".cjs" && parsed1.ext !== ".py" && parsed1.ext !== ".php" && parsed1.ext !== ".chat"))) continue;
780
- if (((args.match === "exact") && (parsed.base !== item) && (parsed.base !== (parsed1.name + parsed1.ext2)) && (parsed.base !== parsed1.name))) continue;
781
- if ((re && !re.test(item))) continue;
782
- var fileType;
783
- switch (parsed1.ext2) {
784
- case ".tool":
785
- _ref0 = "tool";
786
- break;
787
- case ".llm":
788
- _ref0 = "llm";
789
- break;
790
- case ".proc":
791
- _ref0 = "processor";
792
- break;
793
- case ".ctx":
794
- _ref0 = "context";
795
- break;
796
- default:
797
- _ref0 = undefined;
798
- }
799
- fileType = _ref0;
800
- var fullname;
801
- fullname = path.resolve(dir, item);
802
- if (((args.type !== "any") && (args.type !== fileType))) continue;
803
- var res;
804
- switch (fileType) {
805
- case "tool":
806
- var schemaFile;
807
- schemaFile = path.format({
808
- root: parsed1.root,
809
- dir: parsed1.dir,
810
- name: parsed1.name,
811
- ext: ".schema.json"
812
- });
813
- var schema;
814
- schema;
815
- if (fs.existsSync(schemaFile)) {
816
- try {
817
- schema = JSON.parse(fs.readFileSync(schemaFile, "utf8"));
818
- } catch (e) {
819
- throw new Error(tpl("Can not parse schema {schemaFile}\n{message}", {
820
- schemaFile: schemaFile,
821
- message: e.message
822
- }));
823
- }
824
- } else if (opts && opts.makeSchema && (opts.output !== "all")) {
825
- schema = await opts.makeSchema({
826
- text: fs.readFileSync(fullname, "utf8")
827
- }, self);
828
- fs.writeFileSync(schemaFile, schema);
829
- schema = JSON.parse(schema);
830
- } else {
831
- throw new Error(("schema file not found " + schemaFile));
832
- }
833
- _ref1 = {
834
- type: "tool",
835
- schema: schema,
836
- name: parsed1.name,
837
- exec: (async function(params, ctx) {
838
- return runFile(fullname, ctx, params);
839
- }),
840
- read: (async function() {
841
- return fs.readFileSync(fullname, "utf8");
842
- }),
843
- dirname: parsed1.dir,
844
- fullname: fullname
845
- }
846
- break;
847
- case "llm":
848
- _ref1 = {
849
- type: "llm",
850
- dirname: parsed1.dir,
851
- fullname: fullname,
852
- name: parsed1.name,
853
- exec: (async function(payload, ctx) {
854
- return runFile(fullname, ctx, payload);
855
- }),
856
- read: (async function() {
857
- return fs.readFileSync(fullname, "utf8");
858
- })
859
- }
860
- break;
861
- case "context":
862
- if ((args.output !== "all")) self.use((async function(name, args) {
863
- return runFile(fullname, this, name, args);
864
- }));
865
- _ref1 = {
866
- type: "text",
867
- fullname: fullname,
868
- name: parsed1.name,
869
- dirname: parsed1.dir,
870
- read: (async function() {
871
- return "";
872
- })
873
- }
874
- break;
875
- case "processor":
876
- _ref1 = {
877
- type: "processor",
878
- name: parsed1.name,
879
- exec: (function(node, args, ctx) {
880
- return runFile(fullname, ctx, node, args);
881
- }),
882
- read: (async function() {
883
- return fs.readFileSync(fullname, "utf8");
884
- }),
885
- dirname: parsed1.dir,
886
- fullname: fullname
887
- }
888
- break;
889
- default:
890
- _ref1 = undefined;
891
- }
892
- res = _ref1;
893
- if ((args.output === "all")) {
894
- result.push(res);
895
- } else {
896
- result = res;
897
- break;
898
- }
899
- }
900
- return result;
901
- }
902
- return toolsMd;
903
- }
904
- fsMod;
905
-
906
- function fsText(opts, fs) {
907
- async function fsmd(name, args) {
908
- var self, result, prefix, parsed, dir, re, item, parsed1, fileType, fullname, res, _i, _ref, _len, _ref0, _ref1;
909
- fs = fs || require("fs");
910
- if (!(((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.path !== "undefined") && (opts.path !== null) && !Number.isNaN(opts.path)) ? opts.path : undefined)) throw Error("path is not set");
911
- var self;
912
- var result;
913
- self = this;
914
- result = ((args.output === "all") ? [] : undefined);
915
- if (!name) return;
916
- name = path.normalize(name);
917
- if (opts.mount) {
918
- var prefix;
919
- prefix = path.normalize(opts.mount + "/");
920
- if ((0 === name.indexOf(prefix))) {
921
- name = name.substr(prefix.length);
922
- } else {
923
- return;
924
- }
925
- }
926
- var parsed;
927
- var dir;
928
- var re;
929
- parsed = ((args.match === "exact") ? pparse(path.resolve(opts.path, name)) : undefined);
930
- dir = (((typeof parsed !== "undefined") && (parsed !== null) && !Number.isNaN(parsed) && (typeof parsed.dir !== "undefined") && (parsed.dir !== null) && !Number.isNaN(parsed.dir)) ? parsed.dir : (((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.path !== "undefined") && (opts.path !== null) && !Number.isNaN(opts.path)) ? opts.path : undefined));
931
- re = ((args.match === "regex") ? new RegExp(name) : undefined);
932
- if (!fs.existsSync(dir)) return;
933
- _ref = fs.readdirSync(dir);
934
- for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
935
- item = _ref[_i];
936
- var parsed1;
937
- parsed1 = pparse(path.join(dir, item));
938
- if (((args.match === "exact") && (parsed.base !== item) && (parsed.base !== (parsed1.name + parsed1.ext2)) && (parsed.base !== parsed1.name))) continue;
939
- if ((re && !re.test(item))) continue;
940
- var fileType;
941
- fileType = ((parsed1.ext === ".jpg" || parsed1.ext === ".jpeg" || parsed1.ext === ".png" || parsed1.ext === ".webp") ? "image" : "text");
942
- var fullname;
943
- fullname = path.resolve(dir, item);
944
- if (((args.type !== "any") && (args.type !== fileType))) continue;
945
- var res;
946
- switch (fileType) {
947
- case "image":
948
- if ((parsed1.ext === ".jpg" || parsed1.ext === ".jpeg")) {
949
- _ref1 = "image/jpeg";
950
- } else if (parsed1.ext === ".png") {
951
- _ref1 = "image/png";
952
- } else if (parsed1.ext === ".webp") {
953
- _ref1 = "image/webp";
954
- } else {
955
- _ref1 = undefined;
956
- }
957
- _ref0 = {
958
- type: "image",
959
- dirname: parsed1.dir,
960
- fullname: fullname,
961
- mimetype: _ref1,
962
- read: (async function() {
963
- return fs.readFileSync(fullname);
964
- })
965
- }
966
- break;
967
- case "text":
968
- _ref0 = {
969
- type: "text",
970
- dirname: parsed1.dir,
971
- fullname: fullname,
972
- name: parsed1.name,
973
- read: (async function(binary) {
974
- var stat, buf, i, c;
975
- var stat;
976
- stat = fs.lstatSync(fullname);
977
- if (stat.isDirectory()) return fs.readdirSync(fullname);
978
- if (binary) return fs.readFileSync(fullname);
979
- var buf;
980
- buf = fs.readFileSync(fullname, "utf8");
981
- var i;
982
- i = 0;
983
- while (i < Math.min(1024, buf.length)) {
984
- var c;
985
- c = buf.charCodeAt(i);
986
- if (((c === 65533) || (c <= 8))) throw Error(tpl("{} is a binary file, can not include it", fullname));
987
- i++;
988
- }
989
- return buf;
990
- })
991
- }
992
- break;
993
- default:
994
- _ref0 = undefined;
995
- }
996
- res = _ref0;
997
- if ((args.output === "all")) {
998
- result.push(res);
999
- } else {
1000
- result = res;
1001
- break;
1002
- }
1003
- }
1004
- return result;
1005
- }
1006
- return fsmd;
1007
- }
1008
- fsText;
1009
-
1010
- function fsMix(paths, opts, fs) {
1011
- var envCache;
1012
- fs = fs || require("fs");
1013
- if (!Array.isArray(paths)) paths = Array(paths);
1014
- envCache = {};
1015
- return (async function(name, args) {
1016
- var lpaths, handles, result, p, envFile, lopts, handle, res, _i, _ref, _len;
1017
- var lpaths;
1018
- lpaths = this.stack
1019
- .filter((function(item) {
1020
- return !!item.dirname;
1021
- }))
1022
- .map((function(item) {
1023
- return item.dirname;
1024
- }))
1025
- .reverse()
1026
- .concat(paths);
1027
- var handles;
1028
- handles = [];
1029
- var result;
1030
- result = ((args.output === "all") ? [] : undefined);
1031
- _ref = lpaths;
1032
- for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
1033
- p = _ref[_i];
1034
- var envFile;
1035
- envFile = path.resolve(p, ".env");
1036
- if (!envCache[envFile]) envCache[envFile] = ((!envFile || !fs.existsSync(envFile)) ? {} : env2vars(fs.readFileSync(envFile, "utf8")));
1037
- if (envCache[envFile][name]) return {
1038
- type: "text",
1039
- read: (async function() {
1040
- return envCache[envFile][name];
1041
- })
1042
- };
1043
- var lopts;
1044
- lopts = Object.assign({}, opts);
1045
- lopts.path = p;
1046
- handles.push(fsMod(lopts, fs));
1047
- handles.push(fsText(lopts, fs));
1048
- }
1049
- while (handles.length) {
1050
- var handle;
1051
- handle = handles.shift();
1052
- var res;
1053
- res = await handle.call(this, name, args);
1054
- if (!res) continue;
1055
- if ((args.output === "all")) {
1056
- Array.isArray(res) ? result = result.concat(res) : result.push();
1057
- } else {
1058
- result = res;
1059
- break;
1060
- }
1061
- }
1062
- return result;
1063
- });
1064
- }
1065
- fsMix;
1066
- async function curFile(name, params) {
1067
- var filename, value, _ref;
1068
- if ((!this.stack || !this.stack.length)) return;
1069
- filename = this.stack[0].filename;
1070
- switch (name) {
1071
- case "__filename":
1072
- _ref = filename;
1073
- break;
1074
- case "__dirname":
1075
- _ref = path.dirname(filename);
1076
- break;
1077
- case "__basename":
1078
- _ref = path.basename(filename);
1079
- break;
1080
- case "__name":
1081
- _ref = path.parse(filename).name;
1082
- break;
1083
- case "__ext":
1084
- _ref = path.parse(filename).ext;
1085
- break;
1086
- default:
1087
- _ref = undefined;
1088
- }
1089
- value = _ref;
1090
- return (value ? {
1091
- type: "text",
1092
- read: (async function() {
1093
- return value;
1094
- })
1095
- } : undefined);
1096
- }
1097
- curFile;
1098
-
1099
- function defaultWrite(opts) {
1100
- async function write(filename, data) {
1101
- var directory;
1102
- var directory;
1103
- directory = path.dirname(filename);
1104
- fs.mkdirSync(directory, {
1105
- recursive: true
1106
- });
1107
- fs.writeFileSync(filename, data);
1108
- return true;
1109
- }
1110
- return write;
1111
- }
1112
- defaultWrite;
1113
-
1114
- function tpl(str) {
1115
- var _i;
1116
- var params = 2 <= arguments.length ? [].slice.call(arguments, 1, _i = arguments.length - 0) : (_i = 1, []);
1117
- return (function(paramIndex, params) {
1118
- var _ref;
1119
- try {
1120
- _ref = str.replace(/{(\W*)(\w*)(\W*)}/gm, (function(_, pre, name, post) {
1121
- return (function(res) {
1122
- paramIndex += 1;
1123
- return ((typeof res !== 'undefined') ? ((pre || "") + res + (post || "")) : "");
1124
- })(params[name || paramIndex]);
1125
- }));
1126
- } catch (e) {
1127
- _ref = console.log.apply(console, [].concat([e, str]).concat(params));
1128
- }
1129
- return _ref;
1130
- })(0, (((typeof params[0] === "object") && (params.length === 1)) ? params[0] : params));
1131
- }
1132
- tpl;
1133
- exports.runFile = runFile;
1134
- exports.fsMix = fsMix;
1135
- exports.fsMod = fsMod;
1136
- exports.fsText = fsText;
1137
- exports.defaultWrite = defaultWrite;
1138
- exports.curFile = curFile;
1139
- exports.pparse = pparse;