@nextblock-cms/cortex 0.21.3 → 0.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ai-global-agent-db-tools.cjs +1 -1
- package/lib/ai-global-agent-db-tools.es.js +148 -122
- package/lib/ai-global-agent-tools.cjs +1 -1
- package/lib/ai-global-agent-tools.es.js +801 -800
- package/lib/fetch-host-guard.cjs +1 -1
- package/lib/fetch-host-guard.d.ts +13 -0
- package/lib/fetch-host-guard.es.js +15 -4
- package/lib/mcp-tokens.cjs +1 -1
- package/lib/mcp-tokens.d.ts +20 -9
- package/lib/mcp-tokens.es.js +1 -1
- package/package.json +4 -4
|
@@ -6,16 +6,21 @@ import { tool as o } from "ai";
|
|
|
6
6
|
var s = 100, c = 20, l = 100, u = 20, d = "[REDACTED]", f = "cortex_ai_openrouter_api_key", p = /* @__PURE__ */ new Set([
|
|
7
7
|
f,
|
|
8
8
|
"bot_protection_secret",
|
|
9
|
+
"cortex_ai_mcp_settings",
|
|
9
10
|
"cortex_ai_pexels_api_key",
|
|
10
11
|
"cortex_ai_unsplash_access_key",
|
|
11
12
|
"email_secret",
|
|
12
13
|
"payment_secret"
|
|
13
|
-
]),
|
|
14
|
+
]), ee = /(?:_secret(?:_key)?|_api_key|_access_key|_private_key|_token|_password|_credentials?)$/i;
|
|
15
|
+
function m(e) {
|
|
16
|
+
return p.has(e) || ee.test(e);
|
|
17
|
+
}
|
|
18
|
+
var h = /* @__PURE__ */ new Set([
|
|
14
19
|
"cortex_ai_build_session",
|
|
15
20
|
"is_admin_created",
|
|
16
21
|
"migration_baseline_generation",
|
|
17
22
|
"security_settings"
|
|
18
|
-
]),
|
|
23
|
+
]), g = {
|
|
19
24
|
active_logo_id: e.string().uuid().nullable(),
|
|
20
25
|
cortex_ai_site_brief: t,
|
|
21
26
|
footer_copyright: e.record(e.string().trim().min(2).max(12), e.string().max(500)),
|
|
@@ -26,20 +31,20 @@ var s = 100, c = 20, l = 100, u = 20, d = "[REDACTED]", f = "cortex_ai_openroute
|
|
|
26
31
|
site_keywords: e.string().max(1e3),
|
|
27
32
|
site_title: e.string().max(200)
|
|
28
33
|
};
|
|
29
|
-
function
|
|
34
|
+
function te(e) {
|
|
30
35
|
return e.map((e) => {
|
|
31
36
|
let t = e.path.map(String).join(".");
|
|
32
37
|
return t ? `${t}: ${e.message}` : e.message;
|
|
33
38
|
}).join("; ");
|
|
34
39
|
}
|
|
35
|
-
function
|
|
36
|
-
if (
|
|
37
|
-
let n =
|
|
40
|
+
function _(e, t) {
|
|
41
|
+
if (h.has(e)) throw Error(`The "${e}" site setting cannot be written through Cortex AI database tools.`);
|
|
42
|
+
let n = g[e];
|
|
38
43
|
if (!n) return;
|
|
39
44
|
let r = n.safeParse(t);
|
|
40
|
-
if (!r.success) throw Error(`Invalid value for site setting "${e}": ${
|
|
45
|
+
if (!r.success) throw Error(`Invalid value for site setting "${e}": ${te(r.error.issues)}`);
|
|
41
46
|
}
|
|
42
|
-
async function
|
|
47
|
+
async function v(e) {
|
|
43
48
|
let t = typeof e.blockType == "string" ? e.blockType : "";
|
|
44
49
|
if (!t) throw Error(`${e.label}: block_type is required.`);
|
|
45
50
|
if (!e.content || typeof e.content != "object" || Array.isArray(e.content)) throw Error(`${e.label}: content must be a JSON object.`);
|
|
@@ -54,19 +59,19 @@ async function _(e) {
|
|
|
54
59
|
let s = a(o, r);
|
|
55
60
|
if (!s.isValid) throw Error(`${e.label}: content is invalid for custom block "${t}": ${s.errors.join("; ")}`);
|
|
56
61
|
}
|
|
57
|
-
async function
|
|
62
|
+
async function y(e, t, n) {
|
|
58
63
|
if (t === "site_settings") {
|
|
59
|
-
for (let t of e.rows ?? []) typeof t.key == "string" &&
|
|
64
|
+
for (let t of e.rows ?? []) typeof t.key == "string" && _(t.key, t.value);
|
|
60
65
|
if (e.operation === "update" && e.values) {
|
|
61
66
|
let t = typeof e.values.key == "string" ? e.values.key : null, n = (e.filters ?? []).find((e) => e.column === "key" && e.operator === "eq" && typeof e.value == "string"), r = t ?? (n ? String(n.value) : null);
|
|
62
|
-
r && ("value" in e.values ?
|
|
67
|
+
r && ("value" in e.values ? _(r, e.values.value) : h.has(r) && _(r, void 0));
|
|
63
68
|
}
|
|
64
69
|
return;
|
|
65
70
|
}
|
|
66
71
|
if (t !== "blocks") return;
|
|
67
|
-
let i = null, a = () => i ??= r(
|
|
72
|
+
let i = null, a = () => i ??= r(k(n));
|
|
68
73
|
if (e.operation === "insert" || e.operation === "upsert") {
|
|
69
|
-
for (let [t, r] of (e.rows ?? []).entries()) await
|
|
74
|
+
for (let [t, r] of (e.rows ?? []).entries()) await v({
|
|
70
75
|
blockType: r.block_type,
|
|
71
76
|
content: r.content ?? {},
|
|
72
77
|
context: n,
|
|
@@ -82,13 +87,13 @@ async function v(e, t, n) {
|
|
|
82
87
|
let o = /* @__PURE__ */ new Set();
|
|
83
88
|
if (i) o.add(String(e.values.block_type));
|
|
84
89
|
else {
|
|
85
|
-
let r =
|
|
90
|
+
let r = k(n).from("blocks").select("id, block_type").limit(101);
|
|
86
91
|
r = W(t, r, e.filters ?? []);
|
|
87
92
|
let { data: i, error: a } = await r;
|
|
88
|
-
if (a) throw Error(`Failed to read the blocks to update: ${
|
|
93
|
+
if (a) throw Error(`Failed to read the blocks to update: ${A(a)}`);
|
|
89
94
|
for (let e of Array.isArray(i) ? i : []) o.add(String(e.block_type ?? ""));
|
|
90
95
|
}
|
|
91
|
-
for (let t of o) await
|
|
96
|
+
for (let t of o) await v({
|
|
92
97
|
blockType: t,
|
|
93
98
|
content: e.values.content,
|
|
94
99
|
context: n,
|
|
@@ -97,7 +102,7 @@ async function v(e, t, n) {
|
|
|
97
102
|
});
|
|
98
103
|
}
|
|
99
104
|
}
|
|
100
|
-
var
|
|
105
|
+
var b = {
|
|
101
106
|
blocks: {
|
|
102
107
|
columns: [
|
|
103
108
|
"id",
|
|
@@ -551,7 +556,7 @@ var y = {
|
|
|
551
556
|
},
|
|
552
557
|
site_settings: {
|
|
553
558
|
columns: ["key", "value"],
|
|
554
|
-
description: "Global site settings.
|
|
559
|
+
description: "Global site settings. Credential rows are redacted on read and refused on write (see safetyNotes).",
|
|
555
560
|
primaryKey: ["key"]
|
|
556
561
|
},
|
|
557
562
|
tax_rates: {
|
|
@@ -603,7 +608,10 @@ var y = {
|
|
|
603
608
|
description: "Variant to attribute-term join rows.",
|
|
604
609
|
primaryKey: ["variant_id", "attribute_term_id"]
|
|
605
610
|
}
|
|
606
|
-
},
|
|
611
|
+
}, x = { site_settings: {
|
|
612
|
+
column: "key",
|
|
613
|
+
isProtected: m
|
|
614
|
+
} }, S = e.enum([
|
|
607
615
|
"eq",
|
|
608
616
|
"neq",
|
|
609
617
|
"gt",
|
|
@@ -613,25 +621,25 @@ var y = {
|
|
|
613
621
|
"ilike",
|
|
614
622
|
"in",
|
|
615
623
|
"is"
|
|
616
|
-
]),
|
|
624
|
+
]), C = e.strictObject({
|
|
617
625
|
column: e.string().trim().min(1).max(120),
|
|
618
|
-
operator:
|
|
626
|
+
operator: S.default("eq"),
|
|
619
627
|
value: e.unknown()
|
|
620
|
-
}),
|
|
628
|
+
}), w = e.strictObject({
|
|
621
629
|
ascending: e.boolean().default(!0),
|
|
622
630
|
column: e.string().trim().min(1).max(120)
|
|
623
|
-
}),
|
|
631
|
+
}), T = e.strictObject({
|
|
624
632
|
includeReadOnly: e.boolean().default(!0),
|
|
625
633
|
table: e.string().trim().min(1).max(120).optional()
|
|
626
|
-
}),
|
|
634
|
+
}), E = e.strictObject({
|
|
627
635
|
columns: e.array(e.string().trim().min(1).max(120)).min(1).max(40).optional(),
|
|
628
|
-
filters: e.array(
|
|
636
|
+
filters: e.array(C).max(12).default([]),
|
|
629
637
|
limit: e.number().int().min(1).max(s).default(c),
|
|
630
638
|
offset: e.number().int().min(0).max(1e4).default(0),
|
|
631
|
-
orderBy:
|
|
639
|
+
orderBy: w.optional(),
|
|
632
640
|
table: e.string().trim().min(1).max(120)
|
|
633
|
-
}),
|
|
634
|
-
filters: e.array(
|
|
641
|
+
}), D = e.strictObject({
|
|
642
|
+
filters: e.array(C).max(12).optional(),
|
|
635
643
|
operation: e.enum([
|
|
636
644
|
"delete",
|
|
637
645
|
"insert",
|
|
@@ -642,102 +650,120 @@ var y = {
|
|
|
642
650
|
summary: e.string().trim().min(1).max(500).optional(),
|
|
643
651
|
table: e.string().trim().min(1).max(120),
|
|
644
652
|
values: e.record(e.string(), e.unknown()).optional()
|
|
645
|
-
}),
|
|
646
|
-
actions: e.array(
|
|
653
|
+
}), O = e.strictObject({
|
|
654
|
+
actions: e.array(D).min(1).max(8),
|
|
647
655
|
summary: e.string().trim().min(1).max(500).optional()
|
|
648
656
|
});
|
|
649
|
-
function
|
|
657
|
+
function k(e) {
|
|
650
658
|
if (!e?.supabase) throw Error("Cortex AI database tools require a Supabase client.");
|
|
651
659
|
return e.supabase;
|
|
652
660
|
}
|
|
653
|
-
function
|
|
661
|
+
function A(e) {
|
|
654
662
|
return e ? typeof e == "object" && "message" in e ? String(e.message || "Unknown database error.") : String(e) : "Unknown database error.";
|
|
655
663
|
}
|
|
656
|
-
function
|
|
657
|
-
return typeof e != "object" || !e ? JSON.stringify(e) : Array.isArray(e) ? `[${e.map((e) =>
|
|
664
|
+
function j(e) {
|
|
665
|
+
return typeof e != "object" || !e ? JSON.stringify(e) : Array.isArray(e) ? `[${e.map((e) => j(e)).join(",")}]` : `{${Object.keys(e).sort().map((t) => `${JSON.stringify(t)}:${j(e[t])}`).join(",")}}`;
|
|
658
666
|
}
|
|
659
|
-
function
|
|
660
|
-
let t = 2166136261, n =
|
|
667
|
+
function M(e) {
|
|
668
|
+
let t = 2166136261, n = j(e);
|
|
661
669
|
for (let e = 0; e < n.length; e++) t ^= n.charCodeAt(e), t = Math.imul(t, 16777619);
|
|
662
670
|
return (t >>> 0).toString(16).padStart(8, "0");
|
|
663
671
|
}
|
|
664
|
-
function
|
|
672
|
+
function N(e) {
|
|
665
673
|
return e.replace(/\s+/g, " ").trim().toUpperCase();
|
|
666
674
|
}
|
|
667
|
-
function
|
|
668
|
-
return `${
|
|
675
|
+
function P(e, t, n) {
|
|
676
|
+
return `${N(`CONFIRM ${e} ${t}`)} #${M(n)}`;
|
|
669
677
|
}
|
|
670
|
-
function
|
|
678
|
+
function F(e) {
|
|
671
679
|
return {
|
|
672
|
-
confirmationPhrase:
|
|
680
|
+
confirmationPhrase: P(e.action, e.subject, e.payload),
|
|
673
681
|
mutationExecuted: !1,
|
|
674
682
|
preview: e.preview,
|
|
675
683
|
requiresConfirmation: !0,
|
|
676
684
|
success: !0
|
|
677
685
|
};
|
|
678
686
|
}
|
|
679
|
-
function
|
|
687
|
+
function I(e) {
|
|
680
688
|
if (e.context?.skipConfirmation) return null;
|
|
681
|
-
let t =
|
|
689
|
+
let t = F(e), n = N(e.context?.latestUserMessage || ""), r = N(t.confirmationPhrase);
|
|
682
690
|
return n && !n.includes(r) ? {
|
|
683
691
|
...t,
|
|
684
692
|
message: "The database target changed or the confirmation did not match. Please review and confirm again."
|
|
685
693
|
} : n.includes(r) ? null : t;
|
|
686
694
|
}
|
|
687
|
-
function
|
|
688
|
-
return Object.prototype.hasOwnProperty.call(
|
|
695
|
+
function L(e) {
|
|
696
|
+
return Object.prototype.hasOwnProperty.call(b, e);
|
|
689
697
|
}
|
|
690
|
-
function
|
|
698
|
+
function R(e) {
|
|
691
699
|
let t = e.trim();
|
|
692
700
|
if (t.includes(".") || t.toLowerCase().startsWith("auth")) throw Error("Cortex AI cannot access auth schema tables.");
|
|
693
|
-
if (!
|
|
701
|
+
if (!L(t)) throw Error(`Table "${t}" is not available to Cortex AI database tools.`);
|
|
694
702
|
return t;
|
|
695
703
|
}
|
|
696
|
-
function
|
|
697
|
-
return
|
|
704
|
+
function z(e) {
|
|
705
|
+
return b[e];
|
|
698
706
|
}
|
|
699
|
-
function
|
|
707
|
+
function B(e) {
|
|
700
708
|
return /(?:password|token|secret|api[_-]?key|private[_-]?key|credential)/i.test(e);
|
|
701
709
|
}
|
|
702
|
-
function
|
|
703
|
-
if (!
|
|
704
|
-
if (
|
|
710
|
+
function V(e, t) {
|
|
711
|
+
if (!z(e).columns.includes(t)) throw Error(`Column "${t}" is not available on table "${e}".`);
|
|
712
|
+
if (B(t)) throw Error(`Column "${t}" is protected and cannot be used through Cortex AI.`);
|
|
705
713
|
}
|
|
706
|
-
function
|
|
714
|
+
function H(e, t = "payload") {
|
|
707
715
|
if (Array.isArray(e)) {
|
|
708
|
-
e.forEach((e, n) =>
|
|
716
|
+
e.forEach((e, n) => H(e, `${t}[${n}]`));
|
|
709
717
|
return;
|
|
710
718
|
}
|
|
711
719
|
if (e && typeof e == "object") for (let [n, r] of Object.entries(e)) {
|
|
712
|
-
if (
|
|
713
|
-
|
|
720
|
+
if (B(n)) throw Error(`Protected field "${t}.${n}" cannot be written through Cortex AI.`);
|
|
721
|
+
H(r, `${t}.${n}`);
|
|
714
722
|
}
|
|
715
723
|
}
|
|
716
|
-
function
|
|
717
|
-
if (Array.isArray(e)) return e.map((e) =>
|
|
724
|
+
function U(e) {
|
|
725
|
+
if (Array.isArray(e)) return e.map((e) => U(e));
|
|
718
726
|
if (!e || typeof e != "object") return e;
|
|
719
727
|
let t = {};
|
|
720
|
-
for (let [n, r] of Object.entries(e)) t[n] =
|
|
728
|
+
for (let [n, r] of Object.entries(e)) t[n] = B(n) ? d : U(r);
|
|
721
729
|
return t;
|
|
722
730
|
}
|
|
723
|
-
function
|
|
724
|
-
return e.map((
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
731
|
+
function ne(e, t) {
|
|
732
|
+
return Object.fromEntries(t.filter((t) => t in e).map((t) => [t, e[t]]));
|
|
733
|
+
}
|
|
734
|
+
function re(e, t, n) {
|
|
735
|
+
let r = x[e];
|
|
736
|
+
return t.map((e) => {
|
|
737
|
+
if (!e || typeof e != "object" || Array.isArray(e)) return U(e);
|
|
738
|
+
let t = e;
|
|
739
|
+
if (!r) return U(t);
|
|
740
|
+
let i = t[r.column];
|
|
741
|
+
return ne(typeof i == "string" && r.isProtected(i) ? Object.fromEntries(Object.keys(t).map((e) => [e, e === r.column ? i : d])) : U(t), n);
|
|
733
742
|
});
|
|
734
743
|
}
|
|
735
|
-
function
|
|
744
|
+
function ie(e) {
|
|
745
|
+
let t = [], n = (e) => {
|
|
746
|
+
typeof e == "string" && t.push(e);
|
|
747
|
+
};
|
|
748
|
+
for (let t of e.rows ?? []) n(t.key);
|
|
749
|
+
n(e.values?.key);
|
|
750
|
+
for (let t of e.filters ?? []) t.column === "key" && (Array.isArray(t.value) ? t.value.forEach(n) : n(t.value));
|
|
751
|
+
return t;
|
|
752
|
+
}
|
|
753
|
+
function ae(e, t) {
|
|
736
754
|
if (e !== "site_settings") return;
|
|
737
755
|
let n = JSON.stringify(t).toLowerCase();
|
|
738
756
|
for (let e of p) if (n.includes(e)) throw Error(`The "${e}" site setting is protected.`);
|
|
757
|
+
for (let e of ie(t)) if (m(e)) throw Error(`The "${e}" site setting is protected.`);
|
|
739
758
|
}
|
|
740
|
-
function
|
|
759
|
+
function oe(e, t) {
|
|
760
|
+
let n = x[e];
|
|
761
|
+
if (n) for (let e of t) {
|
|
762
|
+
let t = e[n.column];
|
|
763
|
+
if (typeof t == "string" && n.isProtected(t)) throw Error(`The "${t}" site setting is protected.`);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function se(e, t) {
|
|
741
767
|
switch (t.operator) {
|
|
742
768
|
case "eq": return e.eq(t.column, t.value);
|
|
743
769
|
case "neq": return e.neq(t.column, t.value);
|
|
@@ -756,42 +782,42 @@ function U(e, t) {
|
|
|
756
782
|
}
|
|
757
783
|
function W(e, t, n) {
|
|
758
784
|
let r = t;
|
|
759
|
-
for (let t of n)
|
|
785
|
+
for (let t of n) V(e, t.column), r = se(r, t);
|
|
760
786
|
return e === "site_settings" && (r = r.neq("key", f)), r;
|
|
761
787
|
}
|
|
762
|
-
function
|
|
763
|
-
let n =
|
|
764
|
-
return r.forEach((t) =>
|
|
788
|
+
function ce(e, t) {
|
|
789
|
+
let n = z(e), r = t?.length ? t : n.columns.filter((e) => !B(e));
|
|
790
|
+
return r.forEach((t) => V(e, t)), r;
|
|
765
791
|
}
|
|
766
792
|
function G(e, t) {
|
|
767
793
|
return t.map((t) => `${t}=${String(e[t] ?? "")}`).join("|");
|
|
768
794
|
}
|
|
769
795
|
async function K(e, t, n) {
|
|
770
|
-
let r =
|
|
796
|
+
let r = z(t), i = e.from(t).select(r.primaryKey.join(",")).limit(101);
|
|
771
797
|
i = W(t, i, n);
|
|
772
798
|
let { data: a, error: o } = await i;
|
|
773
|
-
if (o) throw Error(`Failed to preview database mutation: ${
|
|
799
|
+
if (o) throw Error(`Failed to preview database mutation: ${A(o)}`);
|
|
774
800
|
let s = Array.isArray(a) ? a : [];
|
|
775
801
|
if (s.length > l) throw Error(`Refusing to mutate more than ${l} rows.`);
|
|
776
|
-
return s;
|
|
802
|
+
return oe(t, s), s;
|
|
777
803
|
}
|
|
778
804
|
function q(e) {
|
|
779
|
-
let t =
|
|
805
|
+
let t = R(e.table), n = z(t), r = e.operation;
|
|
780
806
|
if (n.readOnly) throw Error(`Table "${t}" is read-only for Cortex AI.`);
|
|
781
|
-
if (
|
|
807
|
+
if (ae(t, e), (r === "update" || r === "delete") && (!e.filters || e.filters.length === 0)) throw Error(`${r} operations require at least one filter.`);
|
|
782
808
|
if ((r === "insert" || r === "upsert") && (!e.rows || e.rows.length === 0)) throw Error(`${r} operations require rows.`);
|
|
783
809
|
if (r === "update" && (!e.values || Object.keys(e.values).length === 0)) throw Error("update operations require values.");
|
|
784
810
|
if (e.rows && e.rows.length > u) throw Error(`Refusing to mutate more than ${u} input rows.`);
|
|
785
811
|
let i = e.rows ?? (e.values ? [e.values] : []);
|
|
786
812
|
for (let e of i) {
|
|
787
|
-
|
|
788
|
-
for (let n of Object.keys(e))
|
|
813
|
+
H(e);
|
|
814
|
+
for (let n of Object.keys(e)) V(t, n);
|
|
789
815
|
}
|
|
790
|
-
for (let n of e.filters ?? [])
|
|
816
|
+
for (let n of e.filters ?? []) V(t, n.column);
|
|
791
817
|
return t;
|
|
792
818
|
}
|
|
793
819
|
function J(e) {
|
|
794
|
-
let { input: t, table: n, targetRows: r } = e, i =
|
|
820
|
+
let { input: t, table: n, targetRows: r } = e, i = z(n), a = r.map((e) => G(e, i.primaryKey)).sort(), o = t.rows?.length ?? 0, s = t.operation === "insert" || t.operation === "upsert" ? o : r.length, c = t.summary || `${t.operation} ${s} ${s === 1 ? "row" : "rows"} in ${n}.`;
|
|
795
821
|
return {
|
|
796
822
|
affectedCount: s,
|
|
797
823
|
operation: t.operation,
|
|
@@ -808,41 +834,41 @@ async function Y(e) {
|
|
|
808
834
|
auditLogged: !1
|
|
809
835
|
};
|
|
810
836
|
try {
|
|
811
|
-
let { error: n } = await
|
|
837
|
+
let { error: n } = await k(e.context).from("cortex_ai_db_mutation_audit").insert({
|
|
812
838
|
action_name: e.toolName,
|
|
813
839
|
actor_user_id: t,
|
|
814
840
|
error_message: e.errorMessage ?? null,
|
|
815
841
|
operation_summary: e.summary,
|
|
816
|
-
payload:
|
|
817
|
-
payload_hash:
|
|
818
|
-
preview:
|
|
842
|
+
payload: U(e.input),
|
|
843
|
+
payload_hash: M(e.input),
|
|
844
|
+
preview: U(e.preview),
|
|
819
845
|
status: e.status,
|
|
820
846
|
target_tables: e.targetTables,
|
|
821
847
|
tool_name: e.toolName
|
|
822
848
|
});
|
|
823
849
|
return n ? {
|
|
824
|
-
auditError:
|
|
850
|
+
auditError: A(n),
|
|
825
851
|
auditLogged: !1
|
|
826
852
|
} : { auditLogged: !0 };
|
|
827
853
|
} catch (e) {
|
|
828
854
|
return {
|
|
829
|
-
auditError:
|
|
855
|
+
auditError: A(e),
|
|
830
856
|
auditLogged: !1
|
|
831
857
|
};
|
|
832
858
|
}
|
|
833
859
|
}
|
|
834
|
-
async function
|
|
835
|
-
let r =
|
|
860
|
+
async function le(e, t, n) {
|
|
861
|
+
let r = z(t).primaryKey.join(","), i;
|
|
836
862
|
e.operation === "insert" ? i = n.from(t).insert(e.rows).select(r) : e.operation === "upsert" ? i = n.from(t).upsert(e.rows).select(r) : e.operation === "update" ? (i = n.from(t).update(e.values), i = W(t, i, e.filters ?? []), i = i.select(r)) : (i = n.from(t).delete(), i = W(t, i, e.filters ?? []), i = i.select(r));
|
|
837
863
|
let { data: a, error: o } = await i;
|
|
838
|
-
if (o) throw Error(
|
|
864
|
+
if (o) throw Error(A(o));
|
|
839
865
|
return Array.isArray(a) ? a : [];
|
|
840
866
|
}
|
|
841
867
|
async function X(e) {
|
|
842
|
-
let t =
|
|
843
|
-
let t =
|
|
868
|
+
let t = T.parse(e), n = (t.table ? [R(t.table)] : Object.keys(b)).filter((e) => t.includeReadOnly || !z(e).readOnly).map((e) => {
|
|
869
|
+
let t = z(e);
|
|
844
870
|
return {
|
|
845
|
-
columns: t.columns.filter((e) => !
|
|
871
|
+
columns: t.columns.filter((e) => !B(e)),
|
|
846
872
|
description: t.description,
|
|
847
873
|
primaryKey: t.primaryKey,
|
|
848
874
|
readOnly: !!t.readOnly,
|
|
@@ -854,8 +880,8 @@ async function X(e) {
|
|
|
854
880
|
"Use read_database_records for reads and execute_database_mutation/action_plan for writes.",
|
|
855
881
|
"No auth schema access, no arbitrary SQL, no password/secret/API-key fields.",
|
|
856
882
|
"profiles, user_addresses, and cortex_ai_db_mutation_audit are read-only.",
|
|
857
|
-
`Protected site_settings keys (never readable or writable): ${[...p].sort().join(", ")}.`,
|
|
858
|
-
`Write-protected site_settings keys (readable, never writable here): ${[...
|
|
883
|
+
`Protected site_settings keys (never readable or writable): ${[...p].sort().join(", ")}, plus any key ending in _secret, _api_key, _access_key, _private_key, _token, _password or _credentials.`,
|
|
884
|
+
`Write-protected site_settings keys (readable, never writable here): ${[...h].sort().join(", ")}.`,
|
|
859
885
|
"blocks.content is validated against its block_type (built-in schema or custom block definition) before any insert, upsert, or update; prefer the typed content tools, which also normalize and record revisions."
|
|
860
886
|
],
|
|
861
887
|
success: !0,
|
|
@@ -863,23 +889,23 @@ async function X(e) {
|
|
|
863
889
|
};
|
|
864
890
|
}
|
|
865
891
|
async function Z(e, t) {
|
|
866
|
-
let n =
|
|
867
|
-
|
|
868
|
-
let { data:
|
|
869
|
-
if (
|
|
870
|
-
let
|
|
892
|
+
let n = E.parse(e), r = R(n.table), i = ce(r, n.columns), a = x[r], o = a && !i.includes(a.column) ? [...i, a.column] : i, s = k(t).from(r).select(o.join(",")).range(n.offset, n.offset + n.limit - 1);
|
|
893
|
+
s = W(r, s, n.filters), n.orderBy && (V(r, n.orderBy.column), s = s.order(n.orderBy.column, { ascending: n.orderBy.ascending }));
|
|
894
|
+
let { data: c, error: l } = await s;
|
|
895
|
+
if (l) throw Error(`Failed to read database records: ${A(l)}`);
|
|
896
|
+
let u = Array.isArray(c) ? c : [];
|
|
871
897
|
return {
|
|
872
|
-
columns: i
|
|
898
|
+
columns: i,
|
|
873
899
|
limit: n.limit,
|
|
874
900
|
offset: n.offset,
|
|
875
|
-
rows:
|
|
901
|
+
rows: re(r, u, i),
|
|
876
902
|
success: !0,
|
|
877
903
|
table: r
|
|
878
904
|
};
|
|
879
905
|
}
|
|
880
906
|
async function Q(e, t) {
|
|
881
|
-
let n =
|
|
882
|
-
await
|
|
907
|
+
let n = D.parse(e), r = q(n), i = k(t);
|
|
908
|
+
await y(n, r, t);
|
|
883
909
|
let a = J({
|
|
884
910
|
input: n,
|
|
885
911
|
table: r,
|
|
@@ -887,7 +913,7 @@ async function Q(e, t) {
|
|
|
887
913
|
}), o = {
|
|
888
914
|
input: n,
|
|
889
915
|
targetIds: a.targetIds
|
|
890
|
-
}, s =
|
|
916
|
+
}, s = I({
|
|
891
917
|
action: `DATABASE ${n.operation}`,
|
|
892
918
|
context: t,
|
|
893
919
|
payload: o,
|
|
@@ -896,7 +922,7 @@ async function Q(e, t) {
|
|
|
896
922
|
});
|
|
897
923
|
if (s) return s;
|
|
898
924
|
try {
|
|
899
|
-
let e = await
|
|
925
|
+
let e = await le(n, r, i), s = t?.skipAudit ? { auditLogged: !1 } : await Y({
|
|
900
926
|
context: t,
|
|
901
927
|
input: o,
|
|
902
928
|
preview: a,
|
|
@@ -911,13 +937,13 @@ async function Q(e, t) {
|
|
|
911
937
|
...s.auditError ? { auditError: s.auditError } : {},
|
|
912
938
|
mutationExecuted: !0,
|
|
913
939
|
operation: n.operation,
|
|
914
|
-
sampleTargetIds: e.map((e) => G(e,
|
|
940
|
+
sampleTargetIds: e.map((e) => G(e, z(r).primaryKey)).slice(0, 10),
|
|
915
941
|
success: !0,
|
|
916
942
|
summary: a.summary,
|
|
917
943
|
table: r
|
|
918
944
|
};
|
|
919
945
|
} catch (e) {
|
|
920
|
-
let i =
|
|
946
|
+
let i = A(e), s = t?.skipAudit ? { auditLogged: !1 } : await Y({
|
|
921
947
|
context: t,
|
|
922
948
|
errorMessage: i,
|
|
923
949
|
input: o,
|
|
@@ -939,11 +965,11 @@ async function Q(e, t) {
|
|
|
939
965
|
}
|
|
940
966
|
}
|
|
941
967
|
async function $(e, t) {
|
|
942
|
-
let n =
|
|
968
|
+
let n = O.parse(e), r = [];
|
|
943
969
|
for (let e of n.actions) {
|
|
944
970
|
let n = q(e);
|
|
945
|
-
await
|
|
946
|
-
let i = e.operation === "update" || e.operation === "delete" ? await K(
|
|
971
|
+
await y(e, n, t);
|
|
972
|
+
let i = e.operation === "update" || e.operation === "delete" ? await K(k(t), n, e.filters ?? []) : [];
|
|
947
973
|
r.push({
|
|
948
974
|
input: e,
|
|
949
975
|
preview: J({
|
|
@@ -963,7 +989,7 @@ async function $(e, t) {
|
|
|
963
989
|
input: e,
|
|
964
990
|
table: n,
|
|
965
991
|
targetIds: t.targetIds
|
|
966
|
-
})), s =
|
|
992
|
+
})), s = I({
|
|
967
993
|
action: "DATABASE ACTION PLAN",
|
|
968
994
|
context: t,
|
|
969
995
|
payload: o,
|
|
@@ -1020,33 +1046,33 @@ async function $(e, t) {
|
|
|
1020
1046
|
summary: n.summary ?? null
|
|
1021
1047
|
};
|
|
1022
1048
|
}
|
|
1023
|
-
function
|
|
1049
|
+
function ue(e) {
|
|
1024
1050
|
return {
|
|
1025
1051
|
describe_database_schema: o({
|
|
1026
1052
|
description: "Describe the public database tables Cortex AI can read or mutate, including read-only tables, primary keys, and writable columns. Use before broad database tasks when unsure about schema.",
|
|
1027
1053
|
execute: (e) => X(e),
|
|
1028
|
-
inputSchema:
|
|
1054
|
+
inputSchema: T,
|
|
1029
1055
|
strict: !0
|
|
1030
1056
|
}),
|
|
1031
1057
|
execute_database_action_plan: o({
|
|
1032
1058
|
description: "Execute up to 8 typed database mutations as one confirmed plan. Mutating: first returns one combined confirmation preview and Confirm button; after confirmation, runs each action in order and stops on the first failure.",
|
|
1033
1059
|
execute: (t) => $(t, e),
|
|
1034
|
-
inputSchema:
|
|
1060
|
+
inputSchema: O,
|
|
1035
1061
|
strict: !0
|
|
1036
1062
|
}),
|
|
1037
1063
|
execute_database_mutation: o({
|
|
1038
1064
|
description: "Run one typed insert, update, upsert, or delete against an allowed public database table. Mutating: always previews affected rows and requires exact user confirmation before execution. Does not support arbitrary SQL.",
|
|
1039
1065
|
execute: (t) => Q(t, e),
|
|
1040
|
-
inputSchema:
|
|
1066
|
+
inputSchema: D,
|
|
1041
1067
|
strict: !0
|
|
1042
1068
|
}),
|
|
1043
1069
|
read_database_records: o({
|
|
1044
1070
|
description: "Read records from an allowed public database table with validated columns, filters, ordering, limit, and offset. Read-only and redacts protected secret-like values.",
|
|
1045
1071
|
execute: (t) => Z(t, e),
|
|
1046
|
-
inputSchema:
|
|
1072
|
+
inputSchema: E,
|
|
1047
1073
|
strict: !0
|
|
1048
1074
|
})
|
|
1049
1075
|
};
|
|
1050
1076
|
}
|
|
1051
1077
|
//#endregion
|
|
1052
|
-
export {
|
|
1078
|
+
export { ue as createCortexDatabaseAgentTools, T as describeDatabaseSchemaInputSchema, $ as executeDatabaseActionPlan, O as executeDatabaseActionPlanInputSchema, Q as executeDatabaseMutation, D as executeDatabaseMutationInputSchema, X as executeDescribeDatabaseSchema, Z as executeReadDatabaseRecords, E as readDatabaseRecordsInputSchema };
|