@mindstudio-ai/agent 0.1.128 → 0.1.130
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/cli.js +758 -320
- package/dist/index.js +612 -217
- package/dist/index.js.map +1 -1
- package/dist/postinstall.js +758 -320
- package/package.json +1 -5
package/dist/index.js
CHANGED
|
@@ -58,16 +58,22 @@ async function requestWithRetry(config, method, url, body, attempt) {
|
|
|
58
58
|
const body2 = JSON.parse(text);
|
|
59
59
|
details = body2;
|
|
60
60
|
const errMsg = (typeof body2.errorMessage === "string" ? body2.errorMessage : void 0) ?? (typeof body2.errorString === "string" ? body2.errorString : void 0) ?? (typeof body2.error === "string" ? body2.error : void 0) ?? (typeof body2.message === "string" ? body2.message : void 0) ?? (typeof body2.details === "string" ? body2.details : void 0);
|
|
61
|
-
if (errMsg)
|
|
62
|
-
|
|
61
|
+
if (errMsg) {
|
|
62
|
+
message = errMsg;
|
|
63
|
+
} else if (body2.error || body2.message || body2.details) {
|
|
63
64
|
message = JSON.stringify(body2.error ?? body2.message ?? body2.details);
|
|
64
65
|
}
|
|
65
|
-
if (body2.code)
|
|
66
|
-
|
|
66
|
+
if (body2.code) {
|
|
67
|
+
code = String(body2.code);
|
|
68
|
+
} else if (typeof body2.errorString === "string") {
|
|
69
|
+
code = body2.errorString;
|
|
70
|
+
}
|
|
67
71
|
} catch {
|
|
68
72
|
if (text) {
|
|
69
73
|
const stripped = text.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
70
|
-
if (stripped)
|
|
74
|
+
if (stripped) {
|
|
75
|
+
message = stripped.slice(0, 200);
|
|
76
|
+
}
|
|
71
77
|
}
|
|
72
78
|
}
|
|
73
79
|
} catch {
|
|
@@ -424,7 +430,9 @@ var RateLimiter = class {
|
|
|
424
430
|
release() {
|
|
425
431
|
this.inflight--;
|
|
426
432
|
const next = this.queue.shift();
|
|
427
|
-
if (next)
|
|
433
|
+
if (next) {
|
|
434
|
+
next();
|
|
435
|
+
}
|
|
428
436
|
}
|
|
429
437
|
/** Update limits from response headers. */
|
|
430
438
|
updateFromHeaders(headers) {
|
|
@@ -541,7 +549,9 @@ var Roles = new Proxy(
|
|
|
541
549
|
{},
|
|
542
550
|
{
|
|
543
551
|
get(_, prop) {
|
|
544
|
-
if (typeof prop === "string")
|
|
552
|
+
if (typeof prop === "string") {
|
|
553
|
+
return prop;
|
|
554
|
+
}
|
|
545
555
|
return void 0;
|
|
546
556
|
}
|
|
547
557
|
}
|
|
@@ -549,9 +559,15 @@ var Roles = new Proxy(
|
|
|
549
559
|
|
|
550
560
|
// src/db/sql.ts
|
|
551
561
|
function serializeParam(val) {
|
|
552
|
-
if (val === null || val === void 0)
|
|
553
|
-
|
|
554
|
-
|
|
562
|
+
if (val === null || val === void 0) {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
if (typeof val === "boolean") {
|
|
566
|
+
return val ? 1 : 0;
|
|
567
|
+
}
|
|
568
|
+
if (typeof val === "number" || typeof val === "string") {
|
|
569
|
+
return val;
|
|
570
|
+
}
|
|
555
571
|
return JSON.stringify(val);
|
|
556
572
|
}
|
|
557
573
|
function serializeColumnParam(val, columnName, columns) {
|
|
@@ -562,16 +578,26 @@ function serializeColumnParam(val, columnName, columns) {
|
|
|
562
578
|
return serializeParam(val);
|
|
563
579
|
}
|
|
564
580
|
function escapeValue(val) {
|
|
565
|
-
if (val === null || val === void 0)
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
if (typeof val === "
|
|
581
|
+
if (val === null || val === void 0) {
|
|
582
|
+
return "NULL";
|
|
583
|
+
}
|
|
584
|
+
if (typeof val === "boolean") {
|
|
585
|
+
return val ? "1" : "0";
|
|
586
|
+
}
|
|
587
|
+
if (typeof val === "number") {
|
|
588
|
+
return String(val);
|
|
589
|
+
}
|
|
590
|
+
if (typeof val === "string") {
|
|
591
|
+
return `'${val.replace(/'/g, "''")}'`;
|
|
592
|
+
}
|
|
569
593
|
const json = JSON.stringify(val);
|
|
570
594
|
return `'${json.replace(/'/g, "''")}'`;
|
|
571
595
|
}
|
|
572
596
|
var USER_PREFIX = "@@user@@";
|
|
573
597
|
function deserializeRow(row, columns) {
|
|
574
|
-
if (row == null)
|
|
598
|
+
if (row == null) {
|
|
599
|
+
return row;
|
|
600
|
+
}
|
|
575
601
|
const result = {};
|
|
576
602
|
for (const [key, value] of Object.entries(row)) {
|
|
577
603
|
const col = columns.find((c) => c.name === key);
|
|
@@ -605,11 +631,19 @@ function buildSelect(table, options = {}) {
|
|
|
605
631
|
const params = [];
|
|
606
632
|
if (options.where) {
|
|
607
633
|
sql += ` WHERE ${options.where}`;
|
|
608
|
-
if (options.whereParams)
|
|
634
|
+
if (options.whereParams) {
|
|
635
|
+
params.push(...options.whereParams);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
if (options.orderBy) {
|
|
639
|
+
sql += ` ORDER BY ${options.orderBy}${options.desc ? " DESC" : " ASC"}`;
|
|
640
|
+
}
|
|
641
|
+
if (options.limit != null) {
|
|
642
|
+
sql += ` LIMIT ${options.limit}`;
|
|
643
|
+
}
|
|
644
|
+
if (options.offset != null) {
|
|
645
|
+
sql += ` OFFSET ${options.offset}`;
|
|
609
646
|
}
|
|
610
|
-
if (options.orderBy) sql += ` ORDER BY ${options.orderBy}${options.desc ? " DESC" : " ASC"}`;
|
|
611
|
-
if (options.limit != null) sql += ` LIMIT ${options.limit}`;
|
|
612
|
-
if (options.offset != null) sql += ` OFFSET ${options.offset}`;
|
|
613
647
|
return { sql, params: params.length > 0 ? params : void 0 };
|
|
614
648
|
}
|
|
615
649
|
var SQL_IDENTIFIER_RE = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
@@ -639,27 +673,47 @@ function aggregateExpr(fn, column) {
|
|
|
639
673
|
}
|
|
640
674
|
}
|
|
641
675
|
function buildScalarAggregate(table, fn, column, where, whereParams) {
|
|
642
|
-
if (column != null)
|
|
676
|
+
if (column != null) {
|
|
677
|
+
assertSqlIdentifier(column, "aggregate column");
|
|
678
|
+
}
|
|
643
679
|
let sql = `SELECT ${aggregateExpr(fn, column ?? void 0)} AS value FROM ${table}`;
|
|
644
|
-
if (where)
|
|
680
|
+
if (where) {
|
|
681
|
+
sql += ` WHERE ${where}`;
|
|
682
|
+
}
|
|
645
683
|
return { sql, params: whereParams?.length ? whereParams : void 0 };
|
|
646
684
|
}
|
|
647
685
|
function buildGroupedAggregate(table, options) {
|
|
648
|
-
for (const col of options.by)
|
|
686
|
+
for (const col of options.by) {
|
|
687
|
+
assertSqlIdentifier(col, "group-by column");
|
|
688
|
+
}
|
|
649
689
|
for (const term of options.select) {
|
|
650
690
|
assertSqlIdentifier(term.alias, "aggregate alias");
|
|
651
|
-
if (term.column != null)
|
|
691
|
+
if (term.column != null) {
|
|
692
|
+
assertSqlIdentifier(term.column, "aggregate column");
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (options.orderBy != null) {
|
|
696
|
+
assertSqlIdentifier(options.orderBy, "aggregate order key");
|
|
652
697
|
}
|
|
653
|
-
if (options.orderBy != null) assertSqlIdentifier(options.orderBy, "aggregate order key");
|
|
654
698
|
const selectList = [
|
|
655
699
|
...options.by,
|
|
656
|
-
...options.select.map(
|
|
700
|
+
...options.select.map(
|
|
701
|
+
(t) => `${aggregateExpr(t.fn, t.column)} AS ${t.alias}`
|
|
702
|
+
)
|
|
657
703
|
].join(", ");
|
|
658
704
|
let sql = `SELECT ${selectList} FROM ${table}`;
|
|
659
|
-
if (options.where)
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
if (options.
|
|
705
|
+
if (options.where) {
|
|
706
|
+
sql += ` WHERE ${options.where}`;
|
|
707
|
+
}
|
|
708
|
+
if (options.by.length > 0) {
|
|
709
|
+
sql += ` GROUP BY ${options.by.join(", ")}`;
|
|
710
|
+
}
|
|
711
|
+
if (options.orderBy) {
|
|
712
|
+
sql += ` ORDER BY ${options.orderBy}${options.desc ? " DESC" : " ASC"}`;
|
|
713
|
+
}
|
|
714
|
+
if (options.limit != null) {
|
|
715
|
+
sql += ` LIMIT ${options.limit}`;
|
|
716
|
+
}
|
|
663
717
|
return {
|
|
664
718
|
sql,
|
|
665
719
|
params: options.whereParams?.length ? options.whereParams : void 0
|
|
@@ -668,7 +722,10 @@ function buildGroupedAggregate(table, options) {
|
|
|
668
722
|
function buildExists(table, where, whereParams, negate) {
|
|
669
723
|
const inner = where ? `SELECT 1 FROM ${table} WHERE ${where}` : `SELECT 1 FROM ${table}`;
|
|
670
724
|
const fn = negate ? "NOT EXISTS" : "EXISTS";
|
|
671
|
-
return {
|
|
725
|
+
return {
|
|
726
|
+
sql: `SELECT ${fn}(${inner}) as result`,
|
|
727
|
+
params: whereParams?.length ? whereParams : void 0
|
|
728
|
+
};
|
|
672
729
|
}
|
|
673
730
|
function buildInsert(table, data, columns) {
|
|
674
731
|
const filtered = stripSystemColumns(data);
|
|
@@ -698,9 +755,7 @@ function buildUpsert(table, data, conflictColumns, columns) {
|
|
|
698
755
|
const filtered = stripSystemColumns(data);
|
|
699
756
|
const keys = Object.keys(filtered);
|
|
700
757
|
const placeholders = keys.map(() => "?").join(", ");
|
|
701
|
-
const params = keys.map(
|
|
702
|
-
(k) => serializeColumnParam(filtered[k], k, columns)
|
|
703
|
-
);
|
|
758
|
+
const params = keys.map((k) => serializeColumnParam(filtered[k], k, columns));
|
|
704
759
|
const updateKeys = keys.filter((k) => !conflictColumns.includes(k));
|
|
705
760
|
const conflict = conflictColumns.join(", ");
|
|
706
761
|
const sql = updateKeys.length > 0 ? `INSERT INTO ${table} (${keys.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${conflict}) DO UPDATE SET ${updateKeys.map((k) => `${k} = excluded.${k}`).join(", ")} RETURNING *` : `INSERT INTO ${table} (${keys.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${conflict}) DO NOTHING RETURNING *`;
|
|
@@ -708,7 +763,9 @@ function buildUpsert(table, data, conflictColumns, columns) {
|
|
|
708
763
|
}
|
|
709
764
|
function buildDelete(table, where, whereParams) {
|
|
710
765
|
let sql = `DELETE FROM ${table}`;
|
|
711
|
-
if (where)
|
|
766
|
+
if (where) {
|
|
767
|
+
sql += ` WHERE ${where}`;
|
|
768
|
+
}
|
|
712
769
|
return { sql, params: whereParams?.length ? whereParams : void 0 };
|
|
713
770
|
}
|
|
714
771
|
var SYSTEM_COLUMNS = /* @__PURE__ */ new Set([
|
|
@@ -735,37 +792,65 @@ function compilePredicate(fn, bindings) {
|
|
|
735
792
|
try {
|
|
736
793
|
const source = fn.toString();
|
|
737
794
|
const names = extractParamNames(source);
|
|
738
|
-
if (!names)
|
|
795
|
+
if (!names) {
|
|
796
|
+
return { type: "js", fn, reason: "could not extract parameter name" };
|
|
797
|
+
}
|
|
739
798
|
const body = extractBody(source);
|
|
740
|
-
if (!body)
|
|
799
|
+
if (!body) {
|
|
800
|
+
return { type: "js", fn, reason: "could not extract function body" };
|
|
801
|
+
}
|
|
741
802
|
const tokens = tokenize(body);
|
|
742
|
-
if (tokens.length === 0)
|
|
803
|
+
if (tokens.length === 0) {
|
|
804
|
+
return { type: "js", fn, reason: "empty token stream" };
|
|
805
|
+
}
|
|
743
806
|
const parser = new Parser(tokens, names.row, names.bindings, bindings, fn);
|
|
744
807
|
const ast = parser.parseExpression();
|
|
745
|
-
if (!ast)
|
|
746
|
-
|
|
808
|
+
if (!ast) {
|
|
809
|
+
return { type: "js", fn, reason: "could not parse expression" };
|
|
810
|
+
}
|
|
811
|
+
if (parser.pos < tokens.length) {
|
|
812
|
+
return { type: "js", fn, reason: "unexpected tokens after expression" };
|
|
813
|
+
}
|
|
747
814
|
const where = compileNode(ast);
|
|
748
|
-
if (!where)
|
|
815
|
+
if (!where) {
|
|
816
|
+
return { type: "js", fn, reason: "could not compile to SQL" };
|
|
817
|
+
}
|
|
749
818
|
return { type: "sql", where };
|
|
750
819
|
} catch (err) {
|
|
751
|
-
return {
|
|
820
|
+
return {
|
|
821
|
+
type: "js",
|
|
822
|
+
fn,
|
|
823
|
+
reason: `compilation error: ${err?.message || "unknown"}`
|
|
824
|
+
};
|
|
752
825
|
}
|
|
753
826
|
}
|
|
754
827
|
function extractParamNames(source) {
|
|
755
828
|
const arrowIdx = source.indexOf("=>");
|
|
756
|
-
if (arrowIdx === -1)
|
|
829
|
+
if (arrowIdx === -1) {
|
|
830
|
+
return null;
|
|
831
|
+
}
|
|
757
832
|
let paramList = source.slice(0, arrowIdx).trim();
|
|
758
833
|
if (paramList.startsWith("(") && paramList.endsWith(")")) {
|
|
759
834
|
paramList = paramList.slice(1, -1).trim();
|
|
760
835
|
}
|
|
761
|
-
if (paramList.length === 0)
|
|
836
|
+
if (paramList.length === 0) {
|
|
837
|
+
return null;
|
|
838
|
+
}
|
|
762
839
|
const parts = splitParams(paramList);
|
|
763
|
-
if (parts.length === 0 || parts.length > 2)
|
|
840
|
+
if (parts.length === 0 || parts.length > 2) {
|
|
841
|
+
return null;
|
|
842
|
+
}
|
|
764
843
|
const row = stripTypeAnnotation(parts[0]);
|
|
765
|
-
if (!row)
|
|
766
|
-
|
|
844
|
+
if (!row) {
|
|
845
|
+
return null;
|
|
846
|
+
}
|
|
847
|
+
if (parts.length === 1) {
|
|
848
|
+
return { row };
|
|
849
|
+
}
|
|
767
850
|
const bindings = stripTypeAnnotation(parts[1]);
|
|
768
|
-
if (!bindings)
|
|
851
|
+
if (!bindings) {
|
|
852
|
+
return null;
|
|
853
|
+
}
|
|
769
854
|
return { row, bindings };
|
|
770
855
|
}
|
|
771
856
|
function splitParams(input) {
|
|
@@ -773,8 +858,11 @@ function splitParams(input) {
|
|
|
773
858
|
let depth = 0;
|
|
774
859
|
let current = "";
|
|
775
860
|
for (const ch of input) {
|
|
776
|
-
if (ch === "<" || ch === "(")
|
|
777
|
-
|
|
861
|
+
if (ch === "<" || ch === "(") {
|
|
862
|
+
depth++;
|
|
863
|
+
} else if (ch === ">" || ch === ")") {
|
|
864
|
+
depth--;
|
|
865
|
+
}
|
|
778
866
|
if (ch === "," && depth === 0) {
|
|
779
867
|
parts.push(current.trim());
|
|
780
868
|
current = "";
|
|
@@ -782,7 +870,9 @@ function splitParams(input) {
|
|
|
782
870
|
current += ch;
|
|
783
871
|
}
|
|
784
872
|
}
|
|
785
|
-
if (current.trim())
|
|
873
|
+
if (current.trim()) {
|
|
874
|
+
parts.push(current.trim());
|
|
875
|
+
}
|
|
786
876
|
return parts;
|
|
787
877
|
}
|
|
788
878
|
function stripTypeAnnotation(part) {
|
|
@@ -792,11 +882,15 @@ function stripTypeAnnotation(part) {
|
|
|
792
882
|
}
|
|
793
883
|
function extractBody(source) {
|
|
794
884
|
const arrowIdx = source.indexOf("=>");
|
|
795
|
-
if (arrowIdx === -1)
|
|
885
|
+
if (arrowIdx === -1) {
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
796
888
|
let body = source.slice(arrowIdx + 2).trim();
|
|
797
889
|
if (body.startsWith("{")) {
|
|
798
890
|
const match = body.match(/^\{\s*return\s+([\s\S]+?)\s*;?\s*\}$/);
|
|
799
|
-
if (!match)
|
|
891
|
+
if (!match) {
|
|
892
|
+
return null;
|
|
893
|
+
}
|
|
800
894
|
body = match[1];
|
|
801
895
|
}
|
|
802
896
|
return body.trim() || null;
|
|
@@ -817,18 +911,24 @@ function tokenize(expr) {
|
|
|
817
911
|
while (i < expr.length && expr[i] !== quote) {
|
|
818
912
|
if (expr[i] === "\\") {
|
|
819
913
|
i++;
|
|
820
|
-
if (i < expr.length)
|
|
914
|
+
if (i < expr.length) {
|
|
915
|
+
str += expr[i];
|
|
916
|
+
}
|
|
821
917
|
} else {
|
|
822
918
|
str += expr[i];
|
|
823
919
|
}
|
|
824
920
|
i++;
|
|
825
921
|
}
|
|
826
|
-
if (i >= expr.length)
|
|
922
|
+
if (i >= expr.length) {
|
|
923
|
+
return [];
|
|
924
|
+
}
|
|
827
925
|
i++;
|
|
828
926
|
tokens.push({ type: "string", value: str });
|
|
829
927
|
continue;
|
|
830
928
|
}
|
|
831
|
-
if (ch === "`")
|
|
929
|
+
if (ch === "`") {
|
|
930
|
+
return [];
|
|
931
|
+
}
|
|
832
932
|
if (/[0-9]/.test(ch) || ch === "-" && i + 1 < expr.length && /[0-9]/.test(expr[i + 1])) {
|
|
833
933
|
let num = ch;
|
|
834
934
|
i++;
|
|
@@ -918,9 +1018,15 @@ var Parser = class {
|
|
|
918
1018
|
/** Check if the current token matches an expected type and value. */
|
|
919
1019
|
match(type, value) {
|
|
920
1020
|
const t = this.peek();
|
|
921
|
-
if (!t)
|
|
922
|
-
|
|
923
|
-
|
|
1021
|
+
if (!t) {
|
|
1022
|
+
return false;
|
|
1023
|
+
}
|
|
1024
|
+
if (t.type !== type) {
|
|
1025
|
+
return false;
|
|
1026
|
+
}
|
|
1027
|
+
if (value !== void 0 && t.value !== value) {
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
924
1030
|
return true;
|
|
925
1031
|
}
|
|
926
1032
|
/** Consume a token if it matches, otherwise return false. */
|
|
@@ -939,11 +1045,15 @@ var Parser = class {
|
|
|
939
1045
|
/** or_expr → and_expr ( '||' and_expr )* */
|
|
940
1046
|
parseOr() {
|
|
941
1047
|
let left = this.parseAnd();
|
|
942
|
-
if (!left)
|
|
1048
|
+
if (!left) {
|
|
1049
|
+
return null;
|
|
1050
|
+
}
|
|
943
1051
|
while (this.match("operator", "||")) {
|
|
944
1052
|
this.advance();
|
|
945
1053
|
const right = this.parseAnd();
|
|
946
|
-
if (!right)
|
|
1054
|
+
if (!right) {
|
|
1055
|
+
return null;
|
|
1056
|
+
}
|
|
947
1057
|
left = { kind: "logical", operator: "OR", left, right };
|
|
948
1058
|
}
|
|
949
1059
|
return left;
|
|
@@ -951,11 +1061,15 @@ var Parser = class {
|
|
|
951
1061
|
/** and_expr → not_expr ( '&&' not_expr )* */
|
|
952
1062
|
parseAnd() {
|
|
953
1063
|
let left = this.parseNot();
|
|
954
|
-
if (!left)
|
|
1064
|
+
if (!left) {
|
|
1065
|
+
return null;
|
|
1066
|
+
}
|
|
955
1067
|
while (this.match("operator", "&&")) {
|
|
956
1068
|
this.advance();
|
|
957
1069
|
const right = this.parseNot();
|
|
958
|
-
if (!right)
|
|
1070
|
+
if (!right) {
|
|
1071
|
+
return null;
|
|
1072
|
+
}
|
|
959
1073
|
left = { kind: "logical", operator: "AND", left, right };
|
|
960
1074
|
}
|
|
961
1075
|
return left;
|
|
@@ -967,12 +1081,18 @@ var Parser = class {
|
|
|
967
1081
|
if (this.match("lparen")) {
|
|
968
1082
|
this.advance();
|
|
969
1083
|
const inner2 = this.parseExpression();
|
|
970
|
-
if (!inner2)
|
|
971
|
-
|
|
1084
|
+
if (!inner2) {
|
|
1085
|
+
return null;
|
|
1086
|
+
}
|
|
1087
|
+
if (!this.eat("rparen")) {
|
|
1088
|
+
return null;
|
|
1089
|
+
}
|
|
972
1090
|
return { kind: "not", operand: inner2 };
|
|
973
1091
|
}
|
|
974
1092
|
const inner = this.parsePrimary();
|
|
975
|
-
if (!inner)
|
|
1093
|
+
if (!inner) {
|
|
1094
|
+
return null;
|
|
1095
|
+
}
|
|
976
1096
|
if (inner.kind === "booleanField") {
|
|
977
1097
|
return { ...inner, negated: !inner.negated };
|
|
978
1098
|
}
|
|
@@ -990,8 +1110,12 @@ var Parser = class {
|
|
|
990
1110
|
if (this.match("lparen")) {
|
|
991
1111
|
this.advance();
|
|
992
1112
|
const inner = this.parseExpression();
|
|
993
|
-
if (!inner)
|
|
994
|
-
|
|
1113
|
+
if (!inner) {
|
|
1114
|
+
return null;
|
|
1115
|
+
}
|
|
1116
|
+
if (!this.eat("rparen")) {
|
|
1117
|
+
return null;
|
|
1118
|
+
}
|
|
995
1119
|
return inner;
|
|
996
1120
|
}
|
|
997
1121
|
if (this.match("lbracket")) {
|
|
@@ -1020,7 +1144,9 @@ var Parser = class {
|
|
|
1020
1144
|
parseFieldExpression() {
|
|
1021
1145
|
this.advance();
|
|
1022
1146
|
const field = this.parseFieldPath();
|
|
1023
|
-
if (!field)
|
|
1147
|
+
if (!field) {
|
|
1148
|
+
return null;
|
|
1149
|
+
}
|
|
1024
1150
|
const next = this.peek();
|
|
1025
1151
|
if (next?.type === "dot" && this.lookAheadForIncludes()) {
|
|
1026
1152
|
return this.parseFieldIncludes(field);
|
|
@@ -1035,11 +1161,17 @@ var Parser = class {
|
|
|
1035
1161
|
* `o.status` → `"status"`, `o.address.city` → `"json_extract(address, '$.city')"`.
|
|
1036
1162
|
*/
|
|
1037
1163
|
parseFieldPath() {
|
|
1038
|
-
if (!this.eat("dot"))
|
|
1039
|
-
|
|
1164
|
+
if (!this.eat("dot")) {
|
|
1165
|
+
return null;
|
|
1166
|
+
}
|
|
1167
|
+
if (!this.match("identifier")) {
|
|
1168
|
+
return null;
|
|
1169
|
+
}
|
|
1040
1170
|
const parts = [this.advance().value];
|
|
1041
1171
|
while (this.match("dot") && this.tokens[this.pos + 1]?.type === "identifier") {
|
|
1042
|
-
if (this.tokens[this.pos + 2]?.type === "lparen")
|
|
1172
|
+
if (this.tokens[this.pos + 2]?.type === "lparen") {
|
|
1173
|
+
break;
|
|
1174
|
+
}
|
|
1043
1175
|
this.advance();
|
|
1044
1176
|
parts.push(this.advance().value);
|
|
1045
1177
|
}
|
|
@@ -1058,7 +1190,9 @@ var Parser = class {
|
|
|
1058
1190
|
const opToken = this.advance();
|
|
1059
1191
|
const jsOp = opToken.value;
|
|
1060
1192
|
const value = this.parseValue();
|
|
1061
|
-
if (value === PARSE_FAILED)
|
|
1193
|
+
if (value === PARSE_FAILED) {
|
|
1194
|
+
return null;
|
|
1195
|
+
}
|
|
1062
1196
|
if (value === null || value === void 0) {
|
|
1063
1197
|
if (jsOp === "===" || jsOp === "==") {
|
|
1064
1198
|
return { kind: "nullCheck", field, isNull: true };
|
|
@@ -1069,7 +1203,9 @@ var Parser = class {
|
|
|
1069
1203
|
return null;
|
|
1070
1204
|
}
|
|
1071
1205
|
const sqlOp = JS_TO_SQL_OP[jsOp];
|
|
1072
|
-
if (!sqlOp)
|
|
1206
|
+
if (!sqlOp) {
|
|
1207
|
+
return null;
|
|
1208
|
+
}
|
|
1073
1209
|
return { kind: "comparison", field, operator: sqlOp, value };
|
|
1074
1210
|
}
|
|
1075
1211
|
/**
|
|
@@ -1079,10 +1215,16 @@ var Parser = class {
|
|
|
1079
1215
|
parseFieldIncludes(field) {
|
|
1080
1216
|
this.advance();
|
|
1081
1217
|
this.advance();
|
|
1082
|
-
if (!this.eat("lparen"))
|
|
1218
|
+
if (!this.eat("lparen")) {
|
|
1219
|
+
return null;
|
|
1220
|
+
}
|
|
1083
1221
|
const value = this.parseValue();
|
|
1084
|
-
if (value === PARSE_FAILED || typeof value !== "string")
|
|
1085
|
-
|
|
1222
|
+
if (value === PARSE_FAILED || typeof value !== "string") {
|
|
1223
|
+
return null;
|
|
1224
|
+
}
|
|
1225
|
+
if (!this.eat("rparen")) {
|
|
1226
|
+
return null;
|
|
1227
|
+
}
|
|
1086
1228
|
const escaped = value.replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
1087
1229
|
return { kind: "like", field, pattern: `%${escaped}%` };
|
|
1088
1230
|
}
|
|
@@ -1095,22 +1237,38 @@ var Parser = class {
|
|
|
1095
1237
|
const values = [];
|
|
1096
1238
|
while (!this.match("rbracket")) {
|
|
1097
1239
|
if (values.length > 0) {
|
|
1098
|
-
if (!this.eat("comma"))
|
|
1240
|
+
if (!this.eat("comma")) {
|
|
1241
|
+
return null;
|
|
1242
|
+
}
|
|
1099
1243
|
}
|
|
1100
1244
|
const val = this.parseValue();
|
|
1101
|
-
if (val === PARSE_FAILED)
|
|
1245
|
+
if (val === PARSE_FAILED) {
|
|
1246
|
+
return null;
|
|
1247
|
+
}
|
|
1102
1248
|
values.push(val);
|
|
1103
1249
|
}
|
|
1104
1250
|
this.advance();
|
|
1105
|
-
if (!this.eat("dot"))
|
|
1106
|
-
|
|
1251
|
+
if (!this.eat("dot")) {
|
|
1252
|
+
return null;
|
|
1253
|
+
}
|
|
1254
|
+
if (!this.match("identifier", "includes")) {
|
|
1255
|
+
return null;
|
|
1256
|
+
}
|
|
1107
1257
|
this.advance();
|
|
1108
|
-
if (!this.eat("lparen"))
|
|
1109
|
-
|
|
1258
|
+
if (!this.eat("lparen")) {
|
|
1259
|
+
return null;
|
|
1260
|
+
}
|
|
1261
|
+
if (!this.match("identifier", this.paramName)) {
|
|
1262
|
+
return null;
|
|
1263
|
+
}
|
|
1110
1264
|
this.advance();
|
|
1111
1265
|
const field = this.parseFieldPath();
|
|
1112
|
-
if (!field)
|
|
1113
|
-
|
|
1266
|
+
if (!field) {
|
|
1267
|
+
return null;
|
|
1268
|
+
}
|
|
1269
|
+
if (!this.eat("rparen")) {
|
|
1270
|
+
return null;
|
|
1271
|
+
}
|
|
1114
1272
|
return { kind: "in", field, values };
|
|
1115
1273
|
}
|
|
1116
1274
|
/**
|
|
@@ -1121,7 +1279,9 @@ var Parser = class {
|
|
|
1121
1279
|
*/
|
|
1122
1280
|
parseNonParamExpression() {
|
|
1123
1281
|
const ident = this.peek().value;
|
|
1124
|
-
if (ident === "true" || ident === "false")
|
|
1282
|
+
if (ident === "true" || ident === "false") {
|
|
1283
|
+
return null;
|
|
1284
|
+
}
|
|
1125
1285
|
return null;
|
|
1126
1286
|
}
|
|
1127
1287
|
/**
|
|
@@ -1133,17 +1293,33 @@ var Parser = class {
|
|
|
1133
1293
|
*/
|
|
1134
1294
|
parseBindingsArrayIncludes() {
|
|
1135
1295
|
const bound = this.tryResolveBindingsValue();
|
|
1136
|
-
if (bound === PARSE_FAILED)
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
if (!
|
|
1296
|
+
if (bound === PARSE_FAILED) {
|
|
1297
|
+
return null;
|
|
1298
|
+
}
|
|
1299
|
+
if (!Array.isArray(bound)) {
|
|
1300
|
+
return null;
|
|
1301
|
+
}
|
|
1302
|
+
if (!this.eat("dot")) {
|
|
1303
|
+
return null;
|
|
1304
|
+
}
|
|
1305
|
+
if (!this.match("identifier", "includes")) {
|
|
1306
|
+
return null;
|
|
1307
|
+
}
|
|
1140
1308
|
this.advance();
|
|
1141
|
-
if (!this.eat("lparen"))
|
|
1142
|
-
|
|
1309
|
+
if (!this.eat("lparen")) {
|
|
1310
|
+
return null;
|
|
1311
|
+
}
|
|
1312
|
+
if (!this.match("identifier", this.paramName)) {
|
|
1313
|
+
return null;
|
|
1314
|
+
}
|
|
1143
1315
|
this.advance();
|
|
1144
1316
|
const field = this.parseFieldPath();
|
|
1145
|
-
if (!field)
|
|
1146
|
-
|
|
1317
|
+
if (!field) {
|
|
1318
|
+
return null;
|
|
1319
|
+
}
|
|
1320
|
+
if (!this.eat("rparen")) {
|
|
1321
|
+
return null;
|
|
1322
|
+
}
|
|
1147
1323
|
return { kind: "in", field, values: bound };
|
|
1148
1324
|
}
|
|
1149
1325
|
/**
|
|
@@ -1157,14 +1333,22 @@ var Parser = class {
|
|
|
1157
1333
|
* Restores `pos` on failure so callers can fall through cleanly.
|
|
1158
1334
|
*/
|
|
1159
1335
|
tryResolveBindingsValue() {
|
|
1160
|
-
if (!this.bindingsName)
|
|
1161
|
-
|
|
1162
|
-
|
|
1336
|
+
if (!this.bindingsName) {
|
|
1337
|
+
return PARSE_FAILED;
|
|
1338
|
+
}
|
|
1339
|
+
if (!this.match("identifier", this.bindingsName)) {
|
|
1340
|
+
return PARSE_FAILED;
|
|
1341
|
+
}
|
|
1342
|
+
if (this.bindingsValue == null) {
|
|
1343
|
+
return PARSE_FAILED;
|
|
1344
|
+
}
|
|
1163
1345
|
const startPos = this.pos;
|
|
1164
1346
|
this.advance();
|
|
1165
1347
|
const path = [];
|
|
1166
1348
|
while (this.match("dot") && this.tokens[this.pos + 1]?.type === "identifier") {
|
|
1167
|
-
if (this.tokens[this.pos + 2]?.type === "lparen")
|
|
1349
|
+
if (this.tokens[this.pos + 2]?.type === "lparen") {
|
|
1350
|
+
break;
|
|
1351
|
+
}
|
|
1168
1352
|
this.advance();
|
|
1169
1353
|
path.push(this.advance().value);
|
|
1170
1354
|
}
|
|
@@ -1194,7 +1378,9 @@ var Parser = class {
|
|
|
1194
1378
|
*/
|
|
1195
1379
|
parseValue() {
|
|
1196
1380
|
const t = this.peek();
|
|
1197
|
-
if (!t)
|
|
1381
|
+
if (!t) {
|
|
1382
|
+
return PARSE_FAILED;
|
|
1383
|
+
}
|
|
1198
1384
|
if (t.type === "string") {
|
|
1199
1385
|
this.advance();
|
|
1200
1386
|
return t.value;
|
|
@@ -1222,7 +1408,9 @@ var Parser = class {
|
|
|
1222
1408
|
}
|
|
1223
1409
|
if (this.bindingsName && t.value === this.bindingsName) {
|
|
1224
1410
|
const bound = this.tryResolveBindingsValue();
|
|
1225
|
-
if (bound !== PARSE_FAILED)
|
|
1411
|
+
if (bound !== PARSE_FAILED) {
|
|
1412
|
+
return bound;
|
|
1413
|
+
}
|
|
1226
1414
|
}
|
|
1227
1415
|
return this.resolveClosureVariable();
|
|
1228
1416
|
}
|
|
@@ -1284,7 +1472,9 @@ function compileNode(node) {
|
|
|
1284
1472
|
case "nullCheck":
|
|
1285
1473
|
return `${node.field} ${node.isNull ? "IS NULL" : "IS NOT NULL"}`;
|
|
1286
1474
|
case "in": {
|
|
1287
|
-
if (node.values.length === 0)
|
|
1475
|
+
if (node.values.length === 0) {
|
|
1476
|
+
return "0";
|
|
1477
|
+
}
|
|
1288
1478
|
const vals = node.values.map(escapeValue).join(", ");
|
|
1289
1479
|
return `${node.field} IN (${vals})`;
|
|
1290
1480
|
}
|
|
@@ -1295,12 +1485,16 @@ function compileNode(node) {
|
|
|
1295
1485
|
case "logical": {
|
|
1296
1486
|
const left = compileNode(node.left);
|
|
1297
1487
|
const right = compileNode(node.right);
|
|
1298
|
-
if (!left || !right)
|
|
1488
|
+
if (!left || !right) {
|
|
1489
|
+
return null;
|
|
1490
|
+
}
|
|
1299
1491
|
return `(${left} ${node.operator} ${right})`;
|
|
1300
1492
|
}
|
|
1301
1493
|
case "not": {
|
|
1302
1494
|
const inner = compileNode(node.operand);
|
|
1303
|
-
if (!inner)
|
|
1495
|
+
if (!inner) {
|
|
1496
|
+
return null;
|
|
1497
|
+
}
|
|
1304
1498
|
return `NOT (${inner})`;
|
|
1305
1499
|
}
|
|
1306
1500
|
default:
|
|
@@ -1431,11 +1625,21 @@ function aggregateValues(fn, values) {
|
|
|
1431
1625
|
}
|
|
1432
1626
|
}
|
|
1433
1627
|
function compareForOrder(a, b) {
|
|
1434
|
-
if (a == null && b == null)
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
if (a
|
|
1438
|
-
|
|
1628
|
+
if (a == null && b == null) {
|
|
1629
|
+
return 0;
|
|
1630
|
+
}
|
|
1631
|
+
if (a == null) {
|
|
1632
|
+
return -1;
|
|
1633
|
+
}
|
|
1634
|
+
if (b == null) {
|
|
1635
|
+
return 1;
|
|
1636
|
+
}
|
|
1637
|
+
if (a < b) {
|
|
1638
|
+
return -1;
|
|
1639
|
+
}
|
|
1640
|
+
if (a > b) {
|
|
1641
|
+
return 1;
|
|
1642
|
+
}
|
|
1439
1643
|
return 0;
|
|
1440
1644
|
}
|
|
1441
1645
|
function computeAggregateInJs(rows, spec) {
|
|
@@ -1443,8 +1647,11 @@ function computeAggregateInJs(rows, spec) {
|
|
|
1443
1647
|
for (const row of rows) {
|
|
1444
1648
|
const key = JSON.stringify(spec.by.map((col) => row[col]));
|
|
1445
1649
|
const group = groups.get(key);
|
|
1446
|
-
if (group)
|
|
1447
|
-
|
|
1650
|
+
if (group) {
|
|
1651
|
+
group.push(row);
|
|
1652
|
+
} else {
|
|
1653
|
+
groups.set(key, [row]);
|
|
1654
|
+
}
|
|
1448
1655
|
}
|
|
1449
1656
|
if (spec.by.length === 0 && groups.size === 0) {
|
|
1450
1657
|
groups.set("[]", []);
|
|
@@ -1465,7 +1672,9 @@ function computeAggregateInJs(rows, spec) {
|
|
|
1465
1672
|
if (spec.orderBy) {
|
|
1466
1673
|
const key = spec.orderBy;
|
|
1467
1674
|
out.sort((a, b) => compareForOrder(a[key], b[key]));
|
|
1468
|
-
if (spec.desc)
|
|
1675
|
+
if (spec.desc) {
|
|
1676
|
+
out.reverse();
|
|
1677
|
+
}
|
|
1469
1678
|
}
|
|
1470
1679
|
if (spec.limit != null) {
|
|
1471
1680
|
out = out.slice(0, spec.limit);
|
|
@@ -1589,7 +1798,11 @@ var Query = class _Query {
|
|
|
1589
1798
|
*/
|
|
1590
1799
|
sum(accessor) {
|
|
1591
1800
|
return this._clone({
|
|
1592
|
-
aggregate: {
|
|
1801
|
+
aggregate: {
|
|
1802
|
+
kind: "scalar",
|
|
1803
|
+
fn: "sum",
|
|
1804
|
+
accessor
|
|
1805
|
+
},
|
|
1593
1806
|
postProcess: (rows) => rows.reduce((total, row) => {
|
|
1594
1807
|
const value = accessor(row);
|
|
1595
1808
|
return value == null ? total : total + value;
|
|
@@ -1603,7 +1816,11 @@ var Query = class _Query {
|
|
|
1603
1816
|
*/
|
|
1604
1817
|
avg(accessor) {
|
|
1605
1818
|
return this._clone({
|
|
1606
|
-
aggregate: {
|
|
1819
|
+
aggregate: {
|
|
1820
|
+
kind: "scalar",
|
|
1821
|
+
fn: "avg",
|
|
1822
|
+
accessor
|
|
1823
|
+
},
|
|
1607
1824
|
postProcess: (rows) => {
|
|
1608
1825
|
const values = rows.map((row) => accessor(row)).filter((v) => v != null);
|
|
1609
1826
|
return values.length === 0 ? null : values.reduce((s, v) => s + v, 0) / values.length;
|
|
@@ -1626,10 +1843,7 @@ var Query = class _Query {
|
|
|
1626
1843
|
);
|
|
1627
1844
|
return this._clone({
|
|
1628
1845
|
aggregate: { kind: "grouped", spec: normalized },
|
|
1629
|
-
postProcess: (rows) => computeAggregateInJs(
|
|
1630
|
-
rows,
|
|
1631
|
-
normalized
|
|
1632
|
-
)
|
|
1846
|
+
postProcess: (rows) => computeAggregateInJs(rows, normalized)
|
|
1633
1847
|
});
|
|
1634
1848
|
}
|
|
1635
1849
|
some() {
|
|
@@ -1651,7 +1865,9 @@ var Query = class _Query {
|
|
|
1651
1865
|
const row = results[0]?.rows[0];
|
|
1652
1866
|
return row?.result === 1;
|
|
1653
1867
|
}
|
|
1654
|
-
if (this._predicates.length === 0)
|
|
1868
|
+
if (this._predicates.length === 0) {
|
|
1869
|
+
return true;
|
|
1870
|
+
}
|
|
1655
1871
|
const allRows = await this._fetchAllRows();
|
|
1656
1872
|
return allRows.every(
|
|
1657
1873
|
(row) => this._predicates.every((p) => p.fn(row, p.bindings))
|
|
@@ -1670,8 +1886,11 @@ var Query = class _Query {
|
|
|
1670
1886
|
for (const row of rows) {
|
|
1671
1887
|
const key = accessor(row);
|
|
1672
1888
|
const group = map.get(key);
|
|
1673
|
-
if (group)
|
|
1674
|
-
|
|
1889
|
+
if (group) {
|
|
1890
|
+
group.push(row);
|
|
1891
|
+
} else {
|
|
1892
|
+
map.set(key, [row]);
|
|
1893
|
+
}
|
|
1675
1894
|
}
|
|
1676
1895
|
return map;
|
|
1677
1896
|
}
|
|
@@ -1697,14 +1916,26 @@ var Query = class _Query {
|
|
|
1697
1916
|
limit: this._limit,
|
|
1698
1917
|
offset: this._offset
|
|
1699
1918
|
});
|
|
1700
|
-
return {
|
|
1919
|
+
return {
|
|
1920
|
+
type: "query",
|
|
1921
|
+
query,
|
|
1922
|
+
fallbackQuery: null,
|
|
1923
|
+
config: this._config,
|
|
1924
|
+
postProcess: this._postProcess
|
|
1925
|
+
};
|
|
1701
1926
|
}
|
|
1702
1927
|
const compiled = this._compilePredicates();
|
|
1703
1928
|
const sortField = this._sortAccessor ? extractFieldName(this._sortAccessor) : void 0;
|
|
1704
1929
|
if (compiled.allSql) {
|
|
1705
1930
|
const agg = this._compileAggregateSql(compiled.sqlWhere);
|
|
1706
1931
|
if (agg) {
|
|
1707
|
-
return {
|
|
1932
|
+
return {
|
|
1933
|
+
type: "query",
|
|
1934
|
+
query: agg.query,
|
|
1935
|
+
fallbackQuery: null,
|
|
1936
|
+
config: this._config,
|
|
1937
|
+
processRaw: agg.processRaw
|
|
1938
|
+
};
|
|
1708
1939
|
}
|
|
1709
1940
|
const query = buildSelect(this._config.tableName, {
|
|
1710
1941
|
where: compiled.sqlWhere || void 0,
|
|
@@ -1713,7 +1944,13 @@ var Query = class _Query {
|
|
|
1713
1944
|
limit: this._limit,
|
|
1714
1945
|
offset: this._offset
|
|
1715
1946
|
});
|
|
1716
|
-
return {
|
|
1947
|
+
return {
|
|
1948
|
+
type: "query",
|
|
1949
|
+
query,
|
|
1950
|
+
fallbackQuery: null,
|
|
1951
|
+
config: this._config,
|
|
1952
|
+
postProcess: this._postProcess
|
|
1953
|
+
};
|
|
1717
1954
|
}
|
|
1718
1955
|
const fallbackQuery = buildSelect(this._config.tableName);
|
|
1719
1956
|
return {
|
|
@@ -1749,15 +1986,21 @@ var Query = class _Query {
|
|
|
1749
1986
|
if (compiled.query) {
|
|
1750
1987
|
return compiled.postProcess ? compiled.postProcess(rows) : rows;
|
|
1751
1988
|
}
|
|
1752
|
-
let filtered = compiled.predicates ? rows.filter(
|
|
1989
|
+
let filtered = compiled.predicates ? rows.filter(
|
|
1990
|
+
(row) => compiled.predicates.every((p) => p.fn(row, p.bindings))
|
|
1991
|
+
) : rows;
|
|
1753
1992
|
if (compiled.sortAccessor) {
|
|
1754
1993
|
const accessor = compiled.sortAccessor;
|
|
1755
1994
|
const reversed = compiled.reversed ?? false;
|
|
1756
1995
|
filtered.sort((a, b) => {
|
|
1757
1996
|
const aVal = accessor(a);
|
|
1758
1997
|
const bVal = accessor(b);
|
|
1759
|
-
if (aVal < bVal)
|
|
1760
|
-
|
|
1998
|
+
if (aVal < bVal) {
|
|
1999
|
+
return reversed ? 1 : -1;
|
|
2000
|
+
}
|
|
2001
|
+
if (aVal > bVal) {
|
|
2002
|
+
return reversed ? -1 : 1;
|
|
2003
|
+
}
|
|
1761
2004
|
return 0;
|
|
1762
2005
|
});
|
|
1763
2006
|
}
|
|
@@ -1894,8 +2137,12 @@ var Query = class _Query {
|
|
|
1894
2137
|
rows.sort((a, b) => {
|
|
1895
2138
|
const aVal = accessor(a);
|
|
1896
2139
|
const bVal = accessor(b);
|
|
1897
|
-
if (aVal < bVal)
|
|
1898
|
-
|
|
2140
|
+
if (aVal < bVal) {
|
|
2141
|
+
return this._reversed ? 1 : -1;
|
|
2142
|
+
}
|
|
2143
|
+
if (aVal > bVal) {
|
|
2144
|
+
return this._reversed ? -1 : 1;
|
|
2145
|
+
}
|
|
1899
2146
|
return 0;
|
|
1900
2147
|
});
|
|
1901
2148
|
}
|
|
@@ -1910,7 +2157,9 @@ var Query = class _Query {
|
|
|
1910
2157
|
if (this._predicates.length === 0) {
|
|
1911
2158
|
return { allSql: true, sqlWhere: "", compiled: [] };
|
|
1912
2159
|
}
|
|
1913
|
-
const compiled = this._predicates.map(
|
|
2160
|
+
const compiled = this._predicates.map(
|
|
2161
|
+
(p) => compilePredicate(p.fn, p.bindings)
|
|
2162
|
+
);
|
|
1914
2163
|
const allSql = compiled.every((c) => c.type === "sql");
|
|
1915
2164
|
let sqlWhere = "";
|
|
1916
2165
|
if (allSql) {
|
|
@@ -2053,7 +2302,9 @@ var Table = class {
|
|
|
2053
2302
|
return this.filter(predicate, bindings).first();
|
|
2054
2303
|
}
|
|
2055
2304
|
count(predicate, bindings) {
|
|
2056
|
-
if (predicate)
|
|
2305
|
+
if (predicate) {
|
|
2306
|
+
return this.filter(predicate, bindings).count();
|
|
2307
|
+
}
|
|
2057
2308
|
return this.toArray().count();
|
|
2058
2309
|
}
|
|
2059
2310
|
some(predicate, bindings) {
|
|
@@ -2064,7 +2315,12 @@ var Table = class {
|
|
|
2064
2315
|
}
|
|
2065
2316
|
/** True if the table has zero rows. */
|
|
2066
2317
|
async isEmpty() {
|
|
2067
|
-
const query = buildExists(
|
|
2318
|
+
const query = buildExists(
|
|
2319
|
+
this._config.tableName,
|
|
2320
|
+
void 0,
|
|
2321
|
+
void 0,
|
|
2322
|
+
true
|
|
2323
|
+
);
|
|
2068
2324
|
const results = await this._config.executeBatch([query]);
|
|
2069
2325
|
const row = results[0]?.rows[0];
|
|
2070
2326
|
return row?.result === 1;
|
|
@@ -2113,7 +2369,10 @@ var Table = class {
|
|
|
2113
2369
|
return new Query(this._config);
|
|
2114
2370
|
}
|
|
2115
2371
|
filter(predicate, bindings) {
|
|
2116
|
-
return new Query(this._config).filter(
|
|
2372
|
+
return new Query(this._config).filter(
|
|
2373
|
+
predicate,
|
|
2374
|
+
bindings
|
|
2375
|
+
);
|
|
2117
2376
|
}
|
|
2118
2377
|
/** Sort rows by a field. Returns a chainable Query. */
|
|
2119
2378
|
sortBy(accessor) {
|
|
@@ -2181,25 +2440,29 @@ var Table = class {
|
|
|
2181
2440
|
results[0].rows[0],
|
|
2182
2441
|
this._config.columns
|
|
2183
2442
|
);
|
|
2184
|
-
this._syncRolesIfNeeded(
|
|
2185
|
-
[data],
|
|
2186
|
-
result,
|
|
2187
|
-
false
|
|
2188
|
-
);
|
|
2443
|
+
this._syncRolesIfNeeded([data], result, false);
|
|
2189
2444
|
return result;
|
|
2190
2445
|
});
|
|
2191
2446
|
}
|
|
2192
2447
|
remove(id) {
|
|
2193
2448
|
const query = buildDelete(this._config.tableName, `id = ?`, [id]);
|
|
2194
|
-
return new Mutation(
|
|
2195
|
-
|
|
2196
|
-
|
|
2449
|
+
return new Mutation(
|
|
2450
|
+
this._config,
|
|
2451
|
+
[query],
|
|
2452
|
+
(results) => ({
|
|
2453
|
+
deleted: results[0].changes > 0
|
|
2454
|
+
})
|
|
2455
|
+
);
|
|
2197
2456
|
}
|
|
2198
2457
|
removeAll(predicate, bindings) {
|
|
2199
2458
|
const compiled = compilePredicate(predicate, bindings);
|
|
2200
2459
|
if (compiled.type === "sql") {
|
|
2201
2460
|
const query = buildDelete(this._config.tableName, compiled.where);
|
|
2202
|
-
return new Mutation(
|
|
2461
|
+
return new Mutation(
|
|
2462
|
+
this._config,
|
|
2463
|
+
[query],
|
|
2464
|
+
(results) => results[0].changes
|
|
2465
|
+
);
|
|
2203
2466
|
}
|
|
2204
2467
|
return Mutation.fromExecutor(this._config, async () => {
|
|
2205
2468
|
console.warn(
|
|
@@ -2214,8 +2477,12 @@ var Table = class {
|
|
|
2214
2477
|
)
|
|
2215
2478
|
);
|
|
2216
2479
|
const matching = allRows.filter((row) => predicate(row, bindings));
|
|
2217
|
-
if (matching.length === 0)
|
|
2218
|
-
|
|
2480
|
+
if (matching.length === 0) {
|
|
2481
|
+
return 0;
|
|
2482
|
+
}
|
|
2483
|
+
const deleteQueries = matching.filter((row) => row.id).map(
|
|
2484
|
+
(row) => buildDelete(this._config.tableName, `id = ?`, [row.id])
|
|
2485
|
+
);
|
|
2219
2486
|
if (deleteQueries.length > 0) {
|
|
2220
2487
|
await this._config.executeBatch(deleteQueries);
|
|
2221
2488
|
}
|
|
@@ -2224,7 +2491,11 @@ var Table = class {
|
|
|
2224
2491
|
}
|
|
2225
2492
|
clear() {
|
|
2226
2493
|
const query = buildDelete(this._config.tableName);
|
|
2227
|
-
return new Mutation(
|
|
2494
|
+
return new Mutation(
|
|
2495
|
+
this._config,
|
|
2496
|
+
[query],
|
|
2497
|
+
(results) => results[0].changes
|
|
2498
|
+
);
|
|
2228
2499
|
}
|
|
2229
2500
|
/**
|
|
2230
2501
|
* Insert a row, or update it if a row with the same unique key already
|
|
@@ -2279,7 +2550,9 @@ var Table = class {
|
|
|
2279
2550
|
/** @internal Throw if data includes a platform-managed read-only column. */
|
|
2280
2551
|
_checkManagedColumns(data) {
|
|
2281
2552
|
const mc = this._config.managedColumns;
|
|
2282
|
-
if (!mc)
|
|
2553
|
+
if (!mc) {
|
|
2554
|
+
return;
|
|
2555
|
+
}
|
|
2283
2556
|
const keys = Object.keys(data);
|
|
2284
2557
|
for (const key of keys) {
|
|
2285
2558
|
if (mc.email && key === mc.email || mc.phone && key === mc.phone || mc.apiKey && key === mc.apiKey) {
|
|
@@ -2300,8 +2573,12 @@ var Table = class {
|
|
|
2300
2573
|
_syncRolesIfNeeded(inputItems, result, isArray) {
|
|
2301
2574
|
const rolesCol = this._config.managedColumns?.roles;
|
|
2302
2575
|
const syncRoles = this._config.syncRoles;
|
|
2303
|
-
if (!rolesCol || !syncRoles)
|
|
2304
|
-
|
|
2576
|
+
if (!rolesCol || !syncRoles) {
|
|
2577
|
+
return;
|
|
2578
|
+
}
|
|
2579
|
+
if (!inputItems.some((item) => rolesCol in item)) {
|
|
2580
|
+
return;
|
|
2581
|
+
}
|
|
2305
2582
|
if (isArray) {
|
|
2306
2583
|
for (const row of result) {
|
|
2307
2584
|
if (row?.id) {
|
|
@@ -2451,7 +2728,9 @@ function createDb(databases, executeBatch, authConfig, syncRoles) {
|
|
|
2451
2728
|
for (let i = 0; i < compiled.length; i++) {
|
|
2452
2729
|
const c = compiled[i];
|
|
2453
2730
|
const dbId = c.config.databaseId;
|
|
2454
|
-
if (!groups.has(dbId))
|
|
2731
|
+
if (!groups.has(dbId)) {
|
|
2732
|
+
groups.set(dbId, []);
|
|
2733
|
+
}
|
|
2455
2734
|
if (c.type === "query") {
|
|
2456
2735
|
const sqlQuery = c.query ?? c.fallbackQuery;
|
|
2457
2736
|
groups.get(dbId).push({ opIndex: i, sqlQueries: [sqlQuery] });
|
|
@@ -2524,9 +2803,7 @@ function resolveDatabase(databases, hint) {
|
|
|
2524
2803
|
);
|
|
2525
2804
|
}
|
|
2526
2805
|
if (hint) {
|
|
2527
|
-
const targetDb = databases.find(
|
|
2528
|
-
(db2) => db2.id === hint || db2.name === hint
|
|
2529
|
-
);
|
|
2806
|
+
const targetDb = databases.find((db2) => db2.id === hint || db2.name === hint);
|
|
2530
2807
|
if (!targetDb) {
|
|
2531
2808
|
const available = databases.map((db2) => db2.name || db2.id).join(", ");
|
|
2532
2809
|
throw new MindStudioError(
|
|
@@ -2665,7 +2942,10 @@ var Store = class {
|
|
|
2665
2942
|
);
|
|
2666
2943
|
form.append(
|
|
2667
2944
|
"file",
|
|
2668
|
-
new Blob(
|
|
2945
|
+
new Blob(
|
|
2946
|
+
[view],
|
|
2947
|
+
options.contentType ? { type: options.contentType } : void 0
|
|
2948
|
+
)
|
|
2669
2949
|
);
|
|
2670
2950
|
const upload = await fetch(res.uploadUrl, {
|
|
2671
2951
|
method: "POST",
|
|
@@ -4450,9 +4730,13 @@ var stepMetadata = {
|
|
|
4450
4730
|
|
|
4451
4731
|
// src/task/schema.ts
|
|
4452
4732
|
function describe(value) {
|
|
4453
|
-
if (value === void 0)
|
|
4733
|
+
if (value === void 0) {
|
|
4734
|
+
return "undefined";
|
|
4735
|
+
}
|
|
4454
4736
|
const json = JSON.stringify(value);
|
|
4455
|
-
if (json === void 0)
|
|
4737
|
+
if (json === void 0) {
|
|
4738
|
+
return String(value);
|
|
4739
|
+
}
|
|
4456
4740
|
return json.length > 60 ? `${json.slice(0, 57)}...` : json;
|
|
4457
4741
|
}
|
|
4458
4742
|
function matchesTypeName(value, name) {
|
|
@@ -4565,7 +4849,9 @@ function assertSupportedSchema(schema) {
|
|
|
4565
4849
|
}
|
|
4566
4850
|
function assertSupportedNode(schema, path) {
|
|
4567
4851
|
for (const key of Object.keys(schema)) {
|
|
4568
|
-
if (SUPPORTED_KEYWORDS.has(key))
|
|
4852
|
+
if (SUPPORTED_KEYWORDS.has(key)) {
|
|
4853
|
+
continue;
|
|
4854
|
+
}
|
|
4569
4855
|
const hint = key === "nullable" ? ' Use a type array instead: type: ["string", "null"].' : key === "oneOf" || key === "anyOf" || key === "allOf" ? " Model alternatives with an enum, a type array, or a discriminating property instead." : "";
|
|
4570
4856
|
throw new MindStudioError(
|
|
4571
4857
|
`[task] Unsupported JSON Schema keyword "${key}" at ${path} in outputSchema. Supported: type, properties, required, additionalProperties, items, enum, const.${hint}`,
|
|
@@ -4583,17 +4869,18 @@ function assertSupportedNode(schema, path) {
|
|
|
4583
4869
|
}
|
|
4584
4870
|
}
|
|
4585
4871
|
function buildExampleFromSchema(schema) {
|
|
4586
|
-
if (schema.const !== void 0)
|
|
4587
|
-
|
|
4872
|
+
if (schema.const !== void 0) {
|
|
4873
|
+
return schema.const;
|
|
4874
|
+
}
|
|
4875
|
+
if (schema.enum !== void 0 && schema.enum.length > 0) {
|
|
4588
4876
|
return schema.enum[0];
|
|
4877
|
+
}
|
|
4589
4878
|
const names = Array.isArray(schema.type) ? schema.type : schema.type ? [schema.type] : [];
|
|
4590
4879
|
const name = names.find((n) => n !== "null") ?? names[0];
|
|
4591
4880
|
switch (name) {
|
|
4592
4881
|
case "object": {
|
|
4593
4882
|
const example = {};
|
|
4594
|
-
for (const [key, propSchema] of Object.entries(
|
|
4595
|
-
schema.properties ?? {}
|
|
4596
|
-
)) {
|
|
4883
|
+
for (const [key, propSchema] of Object.entries(schema.properties ?? {})) {
|
|
4597
4884
|
example[key] = buildExampleFromSchema(propSchema);
|
|
4598
4885
|
}
|
|
4599
4886
|
return example;
|
|
@@ -4670,7 +4957,9 @@ function isDevMode() {
|
|
|
4670
4957
|
return !!(process.env.CALLBACK_TOKEN || getRequestContext()?.callbackToken);
|
|
4671
4958
|
}
|
|
4672
4959
|
function logTaskResult(result) {
|
|
4673
|
-
if (!isDevMode())
|
|
4960
|
+
if (!isDevMode()) {
|
|
4961
|
+
return;
|
|
4962
|
+
}
|
|
4674
4963
|
const toolSummary = result.toolCalls.map((tc) => `${tc.name} (${tc.durationMs}ms) ${tc.success ? "\u2713" : "\u2717"}`).join(", ");
|
|
4675
4964
|
console.log(
|
|
4676
4965
|
`[task] ${result.turns} turn${result.turns === 1 ? "" : "s"}` + (toolSummary ? `: ${toolSummary}` : "") + ` | ${result.parsedSuccessfully ? "output OK" : "\u26A0 output not valid JSON"} | cost: ${result.usage.totalBillingCost}`
|
|
@@ -4692,8 +4981,9 @@ async function runTaskPoll(httpConfig, body) {
|
|
|
4692
4981
|
const res = await fetch(pollUrl, {
|
|
4693
4982
|
headers: { "User-Agent": "@mindstudio-ai/agent" }
|
|
4694
4983
|
});
|
|
4695
|
-
if (res.status === 502 || res.status === 503 || res.status === 504)
|
|
4984
|
+
if (res.status === 502 || res.status === 503 || res.status === 504) {
|
|
4696
4985
|
continue;
|
|
4986
|
+
}
|
|
4697
4987
|
if (res.status === 404) {
|
|
4698
4988
|
throw new MindStudioError(
|
|
4699
4989
|
"Task poll token not found or expired.",
|
|
@@ -4766,11 +5056,17 @@ async function runTaskStream(httpConfig, body, onEvent) {
|
|
|
4766
5056
|
const errBody = JSON.parse(text);
|
|
4767
5057
|
details = errBody;
|
|
4768
5058
|
const errMsg = (typeof errBody.error === "string" ? errBody.error : void 0) ?? (typeof errBody.message === "string" ? errBody.message : void 0);
|
|
4769
|
-
if (errMsg)
|
|
4770
|
-
|
|
5059
|
+
if (errMsg) {
|
|
5060
|
+
message = errMsg;
|
|
5061
|
+
}
|
|
5062
|
+
if (errBody.code) {
|
|
5063
|
+
code = errBody.code;
|
|
5064
|
+
}
|
|
4771
5065
|
} catch {
|
|
4772
5066
|
const stripped = text.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
4773
|
-
if (stripped)
|
|
5067
|
+
if (stripped) {
|
|
5068
|
+
message = stripped.slice(0, 200);
|
|
5069
|
+
}
|
|
4774
5070
|
}
|
|
4775
5071
|
} catch {
|
|
4776
5072
|
}
|
|
@@ -4782,12 +5078,16 @@ async function runTaskStream(httpConfig, body, onEvent) {
|
|
|
4782
5078
|
let result = null;
|
|
4783
5079
|
while (true) {
|
|
4784
5080
|
const { done, value } = await reader.read();
|
|
4785
|
-
if (done)
|
|
5081
|
+
if (done) {
|
|
5082
|
+
break;
|
|
5083
|
+
}
|
|
4786
5084
|
buffer += decoder.decode(value, { stream: true });
|
|
4787
5085
|
const lines = buffer.split("\n");
|
|
4788
5086
|
buffer = lines.pop() ?? "";
|
|
4789
5087
|
for (const line of lines) {
|
|
4790
|
-
if (!line.startsWith("data: "))
|
|
5088
|
+
if (!line.startsWith("data: ")) {
|
|
5089
|
+
continue;
|
|
5090
|
+
}
|
|
4791
5091
|
try {
|
|
4792
5092
|
const event = JSON.parse(line.slice(6));
|
|
4793
5093
|
onEvent(event);
|
|
@@ -4814,7 +5114,9 @@ async function runTaskStream(httpConfig, body, onEvent) {
|
|
|
4814
5114
|
};
|
|
4815
5115
|
}
|
|
4816
5116
|
} catch (err) {
|
|
4817
|
-
if (err instanceof MindStudioError)
|
|
5117
|
+
if (err instanceof MindStudioError) {
|
|
5118
|
+
throw err;
|
|
5119
|
+
}
|
|
4818
5120
|
}
|
|
4819
5121
|
}
|
|
4820
5122
|
}
|
|
@@ -4845,7 +5147,9 @@ async function runTaskStream(httpConfig, body, onEvent) {
|
|
|
4845
5147
|
};
|
|
4846
5148
|
}
|
|
4847
5149
|
} catch (err) {
|
|
4848
|
-
if (err instanceof MindStudioError)
|
|
5150
|
+
if (err instanceof MindStudioError) {
|
|
5151
|
+
throw err;
|
|
5152
|
+
}
|
|
4849
5153
|
}
|
|
4850
5154
|
}
|
|
4851
5155
|
if (!result) {
|
|
@@ -4931,8 +5235,12 @@ async function attemptTurn(httpConfig, body) {
|
|
|
4931
5235
|
try {
|
|
4932
5236
|
const errBody = await res.json();
|
|
4933
5237
|
const errMsg = (typeof errBody.errorMessage === "string" ? errBody.errorMessage : void 0) ?? (typeof errBody.errorString === "string" ? errBody.errorString : void 0) ?? (typeof errBody.error === "string" ? errBody.error : void 0);
|
|
4934
|
-
if (errMsg)
|
|
4935
|
-
|
|
5238
|
+
if (errMsg) {
|
|
5239
|
+
message = errMsg;
|
|
5240
|
+
}
|
|
5241
|
+
if (typeof errBody.errorString === "string") {
|
|
5242
|
+
code = errBody.errorString;
|
|
5243
|
+
}
|
|
4936
5244
|
} catch {
|
|
4937
5245
|
}
|
|
4938
5246
|
const retryable = res.status >= 500 || res.status === 429;
|
|
@@ -5060,7 +5368,9 @@ async function runTaskLocal(deps, options) {
|
|
|
5060
5368
|
const { httpConfig } = deps;
|
|
5061
5369
|
const onEvent = options.onEvent;
|
|
5062
5370
|
const outputSchema = options.outputSchema;
|
|
5063
|
-
if (outputSchema)
|
|
5371
|
+
if (outputSchema) {
|
|
5372
|
+
assertSupportedSchema(outputSchema);
|
|
5373
|
+
}
|
|
5064
5374
|
let system;
|
|
5065
5375
|
if (outputSchema) {
|
|
5066
5376
|
system = `${options.prompt}
|
|
@@ -5088,12 +5398,16 @@ ${structuredOutputExample}
|
|
|
5088
5398
|
for (const t of wireTools) {
|
|
5089
5399
|
if ("appMethod" in t) {
|
|
5090
5400
|
toolKinds.set(t.appMethod, "method");
|
|
5091
|
-
if (t.defaults)
|
|
5401
|
+
if (t.defaults) {
|
|
5402
|
+
toolDefaults.set(t.appMethod, t.defaults);
|
|
5403
|
+
}
|
|
5092
5404
|
} else if ("name" in t) {
|
|
5093
5405
|
toolKinds.set(t.name, "function");
|
|
5094
5406
|
} else {
|
|
5095
5407
|
toolKinds.set(t.stepType, "step");
|
|
5096
|
-
if (t.defaults)
|
|
5408
|
+
if (t.defaults) {
|
|
5409
|
+
toolDefaults.set(t.stepType, t.defaults);
|
|
5410
|
+
}
|
|
5097
5411
|
}
|
|
5098
5412
|
}
|
|
5099
5413
|
const maxTurns = Math.min(
|
|
@@ -5226,7 +5540,11 @@ ${structuredOutputExample}
|
|
|
5226
5540
|
const output3 = await functionTools.get(toolCall.name)(
|
|
5227
5541
|
mergedInput
|
|
5228
5542
|
);
|
|
5229
|
-
result = {
|
|
5543
|
+
result = {
|
|
5544
|
+
output: output3 ?? null,
|
|
5545
|
+
billingCost: 0,
|
|
5546
|
+
isError: false
|
|
5547
|
+
};
|
|
5230
5548
|
} catch (err) {
|
|
5231
5549
|
result = {
|
|
5232
5550
|
output: {
|
|
@@ -5274,7 +5592,9 @@ ${structuredOutputExample}
|
|
|
5274
5592
|
let output2;
|
|
5275
5593
|
let parseOk = true;
|
|
5276
5594
|
try {
|
|
5277
|
-
output2 = JSON.parse(
|
|
5595
|
+
output2 = JSON.parse(
|
|
5596
|
+
outputSchema ? stripCodeFences(turn.text) : turn.text
|
|
5597
|
+
);
|
|
5278
5598
|
} catch {
|
|
5279
5599
|
parseOk = false;
|
|
5280
5600
|
}
|
|
@@ -5869,7 +6189,9 @@ var MindStudioAgent = class {
|
|
|
5869
6189
|
*/
|
|
5870
6190
|
get _token() {
|
|
5871
6191
|
const rctx = getRequestContext();
|
|
5872
|
-
if (rctx?.callbackToken)
|
|
6192
|
+
if (rctx?.callbackToken) {
|
|
6193
|
+
return rctx.callbackToken;
|
|
6194
|
+
}
|
|
5873
6195
|
if (this._authType === "internal" && process.env.CALLBACK_TOKEN) {
|
|
5874
6196
|
return process.env.CALLBACK_TOKEN;
|
|
5875
6197
|
}
|
|
@@ -5974,8 +6296,9 @@ var MindStudioAgent = class {
|
|
|
5974
6296
|
const res = await fetch(pollUrl, {
|
|
5975
6297
|
headers: { "User-Agent": "@mindstudio-ai/agent" }
|
|
5976
6298
|
});
|
|
5977
|
-
if (res.status === 502 || res.status === 503 || res.status === 504)
|
|
6299
|
+
if (res.status === 502 || res.status === 503 || res.status === 504) {
|
|
5978
6300
|
continue;
|
|
6301
|
+
}
|
|
5979
6302
|
if (res.status === 404) {
|
|
5980
6303
|
throw new MindStudioError(
|
|
5981
6304
|
`[${stepType}] Execution token expired.`,
|
|
@@ -5993,7 +6316,9 @@ var MindStudioAgent = class {
|
|
|
5993
6316
|
);
|
|
5994
6317
|
}
|
|
5995
6318
|
const poll = await res.json();
|
|
5996
|
-
if (poll.status === "pending")
|
|
6319
|
+
if (poll.status === "pending") {
|
|
6320
|
+
continue;
|
|
6321
|
+
}
|
|
5997
6322
|
if (poll.status === "error") {
|
|
5998
6323
|
throw new MindStudioError(
|
|
5999
6324
|
`[${stepType}] ${poll.error ?? "Step execution failed."}`,
|
|
@@ -6074,16 +6399,21 @@ var MindStudioAgent = class {
|
|
|
6074
6399
|
const body2 = JSON.parse(text);
|
|
6075
6400
|
details = body2;
|
|
6076
6401
|
const errMsg = (typeof body2.error === "string" ? body2.error : void 0) ?? (typeof body2.message === "string" ? body2.message : void 0) ?? (typeof body2.details === "string" ? body2.details : void 0);
|
|
6077
|
-
if (errMsg)
|
|
6078
|
-
|
|
6402
|
+
if (errMsg) {
|
|
6403
|
+
message = errMsg;
|
|
6404
|
+
} else if (body2.error || body2.message || body2.details) {
|
|
6079
6405
|
message = JSON.stringify(
|
|
6080
6406
|
body2.error ?? body2.message ?? body2.details
|
|
6081
6407
|
);
|
|
6082
6408
|
}
|
|
6083
|
-
if (body2.code)
|
|
6409
|
+
if (body2.code) {
|
|
6410
|
+
code = body2.code;
|
|
6411
|
+
}
|
|
6084
6412
|
} catch {
|
|
6085
6413
|
const stripped = text.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
6086
|
-
if (stripped)
|
|
6414
|
+
if (stripped) {
|
|
6415
|
+
message = stripped.slice(0, 200);
|
|
6416
|
+
}
|
|
6087
6417
|
}
|
|
6088
6418
|
} catch {
|
|
6089
6419
|
}
|
|
@@ -6102,12 +6432,16 @@ var MindStudioAgent = class {
|
|
|
6102
6432
|
let doneEvent = null;
|
|
6103
6433
|
while (true) {
|
|
6104
6434
|
const { done, value } = await reader.read();
|
|
6105
|
-
if (done)
|
|
6435
|
+
if (done) {
|
|
6436
|
+
break;
|
|
6437
|
+
}
|
|
6106
6438
|
buffer += decoder.decode(value, { stream: true });
|
|
6107
6439
|
const lines = buffer.split("\n");
|
|
6108
6440
|
buffer = lines.pop() ?? "";
|
|
6109
6441
|
for (const line of lines) {
|
|
6110
|
-
if (!line.startsWith("data: "))
|
|
6442
|
+
if (!line.startsWith("data: ")) {
|
|
6443
|
+
continue;
|
|
6444
|
+
}
|
|
6111
6445
|
try {
|
|
6112
6446
|
const event = JSON.parse(line.slice(6));
|
|
6113
6447
|
if (event.type === "log") {
|
|
@@ -6131,7 +6465,9 @@ var MindStudioAgent = class {
|
|
|
6131
6465
|
);
|
|
6132
6466
|
}
|
|
6133
6467
|
} catch (err) {
|
|
6134
|
-
if (err instanceof MindStudioError)
|
|
6468
|
+
if (err instanceof MindStudioError) {
|
|
6469
|
+
throw err;
|
|
6470
|
+
}
|
|
6135
6471
|
}
|
|
6136
6472
|
}
|
|
6137
6473
|
}
|
|
@@ -6159,7 +6495,9 @@ var MindStudioAgent = class {
|
|
|
6159
6495
|
});
|
|
6160
6496
|
}
|
|
6161
6497
|
} catch (err) {
|
|
6162
|
-
if (err instanceof MindStudioError)
|
|
6498
|
+
if (err instanceof MindStudioError) {
|
|
6499
|
+
throw err;
|
|
6500
|
+
}
|
|
6163
6501
|
}
|
|
6164
6502
|
}
|
|
6165
6503
|
if (!doneEvent) {
|
|
@@ -6243,8 +6581,9 @@ var MindStudioAgent = class {
|
|
|
6243
6581
|
const res = await fetch(pollUrl, {
|
|
6244
6582
|
headers: { "User-Agent": "@mindstudio-ai/agent" }
|
|
6245
6583
|
});
|
|
6246
|
-
if (res.status === 502 || res.status === 503 || res.status === 504)
|
|
6584
|
+
if (res.status === 502 || res.status === 503 || res.status === 504) {
|
|
6247
6585
|
continue;
|
|
6586
|
+
}
|
|
6248
6587
|
if (res.status === 404) {
|
|
6249
6588
|
throw new MindStudioError(
|
|
6250
6589
|
"Batch poll token not found or expired.",
|
|
@@ -6538,8 +6877,9 @@ var MindStudioAgent = class {
|
|
|
6538
6877
|
const res = await fetch(pollUrl, {
|
|
6539
6878
|
headers: { "User-Agent": "@mindstudio-ai/agent" }
|
|
6540
6879
|
});
|
|
6541
|
-
if (res.status === 502 || res.status === 503 || res.status === 504)
|
|
6880
|
+
if (res.status === 502 || res.status === 503 || res.status === 504) {
|
|
6542
6881
|
continue;
|
|
6882
|
+
}
|
|
6543
6883
|
if (res.status === 404) {
|
|
6544
6884
|
throw new MindStudioError(
|
|
6545
6885
|
"Poll token not found or expired.",
|
|
@@ -6557,7 +6897,9 @@ var MindStudioAgent = class {
|
|
|
6557
6897
|
);
|
|
6558
6898
|
}
|
|
6559
6899
|
const poll = await res.json();
|
|
6560
|
-
if (poll.status === "pending")
|
|
6900
|
+
if (poll.status === "pending") {
|
|
6901
|
+
continue;
|
|
6902
|
+
}
|
|
6561
6903
|
if (poll.status === "error") {
|
|
6562
6904
|
throw new MindStudioError(
|
|
6563
6905
|
poll.error ?? "Agent run failed.",
|
|
@@ -6700,7 +7042,9 @@ var MindStudioAgent = class {
|
|
|
6700
7042
|
*/
|
|
6701
7043
|
stream = async (data) => {
|
|
6702
7044
|
const streamId = this._currentStreamId;
|
|
6703
|
-
if (!streamId)
|
|
7045
|
+
if (!streamId) {
|
|
7046
|
+
return;
|
|
7047
|
+
}
|
|
6704
7048
|
const url = `${this._currentHttpConfig.baseUrl}/_internal/v2/stream-chunk`;
|
|
6705
7049
|
const body = typeof data === "string" ? { streamId, type: "token", text: data } : { streamId, type: "data", data };
|
|
6706
7050
|
const res = await fetch(url, {
|
|
@@ -6784,7 +7128,9 @@ var MindStudioAgent = class {
|
|
|
6784
7128
|
if (!this._db) {
|
|
6785
7129
|
this._trySandboxHydration();
|
|
6786
7130
|
}
|
|
6787
|
-
if (this._db)
|
|
7131
|
+
if (this._db) {
|
|
7132
|
+
return this._db;
|
|
7133
|
+
}
|
|
6788
7134
|
return this._createLazyDb();
|
|
6789
7135
|
}
|
|
6790
7136
|
/**
|
|
@@ -6809,8 +7155,12 @@ var MindStudioAgent = class {
|
|
|
6809
7155
|
* ```
|
|
6810
7156
|
*/
|
|
6811
7157
|
async ensureContext() {
|
|
6812
|
-
if (this._getContext())
|
|
6813
|
-
|
|
7158
|
+
if (this._getContext()) {
|
|
7159
|
+
return;
|
|
7160
|
+
}
|
|
7161
|
+
if (this._context) {
|
|
7162
|
+
return;
|
|
7163
|
+
}
|
|
6814
7164
|
if (!this._contextPromise) {
|
|
6815
7165
|
this._contextPromise = this._hydrateContext();
|
|
6816
7166
|
}
|
|
@@ -6856,7 +7206,9 @@ var MindStudioAgent = class {
|
|
|
6856
7206
|
* - `ai.databases`: [{ id, name, tables[] }]
|
|
6857
7207
|
*/
|
|
6858
7208
|
_trySandboxHydration() {
|
|
6859
|
-
if (getRequestContext())
|
|
7209
|
+
if (getRequestContext()) {
|
|
7210
|
+
return;
|
|
7211
|
+
}
|
|
6860
7212
|
const ai = globalThis.ai;
|
|
6861
7213
|
if (ai?.auth && ai?.databases) {
|
|
6862
7214
|
this._applyContext({
|
|
@@ -7016,7 +7368,9 @@ var MindStudioAgent = class {
|
|
|
7016
7368
|
try {
|
|
7017
7369
|
const errBody = await res.json();
|
|
7018
7370
|
details = errBody;
|
|
7019
|
-
if (typeof errBody.errorString === "string")
|
|
7371
|
+
if (typeof errBody.errorString === "string") {
|
|
7372
|
+
code = errBody.errorString;
|
|
7373
|
+
}
|
|
7020
7374
|
message = typeof errBody.errorMessage === "string" && errBody.errorMessage || typeof errBody.errorString === "string" && errBody.errorString || message;
|
|
7021
7375
|
} catch {
|
|
7022
7376
|
}
|
|
@@ -7218,15 +7572,20 @@ var MindStudioAgent = class {
|
|
|
7218
7572
|
try {
|
|
7219
7573
|
const body = JSON.parse(text);
|
|
7220
7574
|
const errMsg = (typeof body.error === "string" ? body.error : void 0) ?? (typeof body.message === "string" ? body.message : void 0) ?? (typeof body.details === "string" ? body.details : void 0);
|
|
7221
|
-
if (errMsg)
|
|
7222
|
-
|
|
7575
|
+
if (errMsg) {
|
|
7576
|
+
message = errMsg;
|
|
7577
|
+
} else if (body.error || body.message || body.details) {
|
|
7223
7578
|
message = JSON.stringify(
|
|
7224
7579
|
body.error ?? body.message ?? body.details
|
|
7225
7580
|
);
|
|
7226
7581
|
}
|
|
7227
|
-
if (body.code)
|
|
7582
|
+
if (body.code) {
|
|
7583
|
+
code = body.code;
|
|
7584
|
+
}
|
|
7228
7585
|
} catch {
|
|
7229
|
-
if (text && text.length < 500)
|
|
7586
|
+
if (text && text.length < 500) {
|
|
7587
|
+
message = text;
|
|
7588
|
+
}
|
|
7230
7589
|
}
|
|
7231
7590
|
} catch {
|
|
7232
7591
|
}
|
|
@@ -7489,7 +7848,9 @@ var MindStudioAgent = class {
|
|
|
7489
7848
|
try {
|
|
7490
7849
|
const body = await res.json();
|
|
7491
7850
|
details = body;
|
|
7492
|
-
if (typeof body.errorString === "string")
|
|
7851
|
+
if (typeof body.errorString === "string") {
|
|
7852
|
+
code = body.errorString;
|
|
7853
|
+
}
|
|
7493
7854
|
message = typeof body.errorMessage === "string" && body.errorMessage || typeof body.errorString === "string" && body.errorString || message;
|
|
7494
7855
|
} catch {
|
|
7495
7856
|
}
|
|
@@ -7533,7 +7894,9 @@ var MindStudioAgent = class {
|
|
|
7533
7894
|
try {
|
|
7534
7895
|
const body = await res.json();
|
|
7535
7896
|
details = body;
|
|
7536
|
-
if (typeof body.errorString === "string")
|
|
7897
|
+
if (typeof body.errorString === "string") {
|
|
7898
|
+
code = body.errorString;
|
|
7899
|
+
}
|
|
7537
7900
|
message = typeof body.errorMessage === "string" && body.errorMessage || typeof body.errorString === "string" && body.errorString || message;
|
|
7538
7901
|
} catch {
|
|
7539
7902
|
}
|
|
@@ -7604,7 +7967,9 @@ var MindStudioAgent = class {
|
|
|
7604
7967
|
const filename = options.filename ?? `upload.${options.extension}`;
|
|
7605
7968
|
const { data } = await request(this._currentHttpConfig, "POST", "/account/upload", { filename });
|
|
7606
7969
|
const form = new FormData();
|
|
7607
|
-
for (const [k, v] of Object.entries(data.fields))
|
|
7970
|
+
for (const [k, v] of Object.entries(data.fields)) {
|
|
7971
|
+
form.append(k, v);
|
|
7972
|
+
}
|
|
7608
7973
|
const buf = content.buffer.slice(
|
|
7609
7974
|
content.byteOffset,
|
|
7610
7975
|
content.byteOffset + content.byteLength
|
|
@@ -7647,14 +8012,21 @@ function resolveStepType2(name) {
|
|
|
7647
8012
|
}
|
|
7648
8013
|
function resolveToken(provided, config) {
|
|
7649
8014
|
const rctx = getRequestContext();
|
|
7650
|
-
if (rctx?.callbackToken)
|
|
8015
|
+
if (rctx?.callbackToken) {
|
|
7651
8016
|
return { token: rctx.callbackToken, authType: "internal" };
|
|
7652
|
-
|
|
8017
|
+
}
|
|
8018
|
+
if (process.env.CALLBACK_TOKEN) {
|
|
7653
8019
|
return { token: process.env.CALLBACK_TOKEN, authType: "internal" };
|
|
7654
|
-
|
|
7655
|
-
if (
|
|
8020
|
+
}
|
|
8021
|
+
if (provided) {
|
|
8022
|
+
return { token: provided, authType: "apiKey" };
|
|
8023
|
+
}
|
|
8024
|
+
if (process.env.MINDSTUDIO_API_KEY) {
|
|
7656
8025
|
return { token: process.env.MINDSTUDIO_API_KEY, authType: "apiKey" };
|
|
7657
|
-
|
|
8026
|
+
}
|
|
8027
|
+
if (config?.apiKey) {
|
|
8028
|
+
return { token: config.apiKey, authType: "apiKey" };
|
|
8029
|
+
}
|
|
7658
8030
|
throw new MindStudioError(
|
|
7659
8031
|
"No API key provided. Run `mindstudio login`, pass `apiKey` to the constructor, or set the MINDSTUDIO_API_KEY environment variable.",
|
|
7660
8032
|
"missing_api_key",
|
|
@@ -7664,16 +8036,22 @@ function resolveToken(provided, config) {
|
|
|
7664
8036
|
|
|
7665
8037
|
// src/jewel/index.ts
|
|
7666
8038
|
function errorInfo(e) {
|
|
7667
|
-
if (e instanceof Error)
|
|
8039
|
+
if (e instanceof Error) {
|
|
8040
|
+
return { message: e.message, stack: e.stack };
|
|
8041
|
+
}
|
|
7668
8042
|
return { message: String(e) };
|
|
7669
8043
|
}
|
|
7670
8044
|
function deepEqual(a, b) {
|
|
7671
|
-
if (a === b)
|
|
8045
|
+
if (a === b) {
|
|
8046
|
+
return true;
|
|
8047
|
+
}
|
|
7672
8048
|
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
|
|
7673
8049
|
return false;
|
|
7674
8050
|
}
|
|
7675
8051
|
const aIsArray = Array.isArray(a);
|
|
7676
|
-
if (aIsArray !== Array.isArray(b))
|
|
8052
|
+
if (aIsArray !== Array.isArray(b)) {
|
|
8053
|
+
return false;
|
|
8054
|
+
}
|
|
7677
8055
|
if (aIsArray) {
|
|
7678
8056
|
const aa = a;
|
|
7679
8057
|
const ba = b;
|
|
@@ -7683,7 +8061,9 @@ function deepEqual(a, b) {
|
|
|
7683
8061
|
const bo = b;
|
|
7684
8062
|
const aKeys = Object.keys(ao).filter((k) => ao[k] !== void 0);
|
|
7685
8063
|
const bKeys = Object.keys(bo).filter((k) => bo[k] !== void 0);
|
|
7686
|
-
if (aKeys.length !== bKeys.length)
|
|
8064
|
+
if (aKeys.length !== bKeys.length) {
|
|
8065
|
+
return false;
|
|
8066
|
+
}
|
|
7687
8067
|
return aKeys.every((k) => deepEqual(ao[k], bo[k]));
|
|
7688
8068
|
}
|
|
7689
8069
|
var VERDICTS = /* @__PURE__ */ new Set(["agree", "disagree", "skip"]);
|
|
@@ -7702,7 +8082,10 @@ function defineJewel(method, config) {
|
|
|
7702
8082
|
try {
|
|
7703
8083
|
return normalizeVerdict(await config.grade({ proposed, actual }));
|
|
7704
8084
|
} catch (e) {
|
|
7705
|
-
return {
|
|
8085
|
+
return {
|
|
8086
|
+
verdict: "skip",
|
|
8087
|
+
notes: `grade threw: ${errorInfo(e).message}`
|
|
8088
|
+
};
|
|
7706
8089
|
}
|
|
7707
8090
|
}
|
|
7708
8091
|
return deepEqual(proposed, actual) ? { verdict: "agree" } : { verdict: "disagree" };
|
|
@@ -7718,7 +8101,9 @@ function defineJewel(method, config) {
|
|
|
7718
8101
|
...rest
|
|
7719
8102
|
});
|
|
7720
8103
|
const traceIds = (t) => {
|
|
7721
|
-
if (t === void 0)
|
|
8104
|
+
if (t === void 0) {
|
|
8105
|
+
return void 0;
|
|
8106
|
+
}
|
|
7722
8107
|
const ids = (Array.isArray(t) ? t : [t]).filter(
|
|
7723
8108
|
(id) => typeof id === "string" && id.length > 0
|
|
7724
8109
|
);
|
|
@@ -7745,7 +8130,11 @@ function defineJewel(method, config) {
|
|
|
7745
8130
|
try {
|
|
7746
8131
|
subject = config.subject(actual);
|
|
7747
8132
|
} catch (e) {
|
|
7748
|
-
return done({
|
|
8133
|
+
return done({
|
|
8134
|
+
mode,
|
|
8135
|
+
actual,
|
|
8136
|
+
error: { phase: "subject", ...errorInfo(e) }
|
|
8137
|
+
});
|
|
7749
8138
|
}
|
|
7750
8139
|
} else {
|
|
7751
8140
|
subject = params.subject;
|
|
@@ -7813,7 +8202,9 @@ function deletes(externalIds) {
|
|
|
7813
8202
|
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
7814
8203
|
var DEFAULT_CONCURRENCY = 4;
|
|
7815
8204
|
function errorInfo2(e) {
|
|
7816
|
-
if (e instanceof Error)
|
|
8205
|
+
if (e instanceof Error) {
|
|
8206
|
+
return { message: e.message, stack: e.stack };
|
|
8207
|
+
}
|
|
7817
8208
|
return { message: String(e) };
|
|
7818
8209
|
}
|
|
7819
8210
|
function objectFor(input) {
|
|
@@ -7872,7 +8263,9 @@ async function withTimeout(work, timeoutMs, key) {
|
|
|
7872
8263
|
})
|
|
7873
8264
|
]);
|
|
7874
8265
|
} finally {
|
|
7875
|
-
if (timer)
|
|
8266
|
+
if (timer) {
|
|
8267
|
+
clearTimeout(timer);
|
|
8268
|
+
}
|
|
7876
8269
|
}
|
|
7877
8270
|
}
|
|
7878
8271
|
function defineMapper(source, config) {
|
|
@@ -7886,7 +8279,9 @@ function defineMapper(source, config) {
|
|
|
7886
8279
|
const worker = async () => {
|
|
7887
8280
|
while (true) {
|
|
7888
8281
|
const index = next++;
|
|
7889
|
-
if (index >= objects.length)
|
|
8282
|
+
if (index >= objects.length) {
|
|
8283
|
+
return;
|
|
8284
|
+
}
|
|
7890
8285
|
const input = objects[index];
|
|
7891
8286
|
const t = Date.now();
|
|
7892
8287
|
let outcome;
|