@nextblock-cms/cortex 0.18.1 → 0.18.3

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.
@@ -1,8 +1,8 @@
1
- import { tool as P } from "ai";
2
- import { requireActorRole as ae } from "./ai-global-agent-theming-tools.es.js";
3
- import { cortexSiteBriefInputSchema as de, safeParseCortexSiteBrief as le, CORTEX_AI_SITE_BRIEF_SETTING_KEY as Q, cortexBuildSessionSchema as ue, CORTEX_AI_BUILD_SESSION_SETTING_KEY as G, cortexSiteBriefSchema as Se, isCortexBuildSessionUsable as ve } from "./site-brief.es.js";
1
+ import { tool as L } from "ai";
2
+ import { requireActorRole as se } from "./ai-global-agent-theming-tools.es.js";
3
+ import { cortexSiteBriefInputSchema as le, safeParseCortexSiteBrief as de, CORTEX_AI_SITE_BRIEF_SETTING_KEY as V, cortexBuildSessionSchema as ue, CORTEX_AI_BUILD_SESSION_SETTING_KEY as G, cortexSiteBriefSchema as we, isCortexBuildSessionUsable as ke } from "./site-brief.es.js";
4
4
  import "./zod-config.es.js";
5
- import { z as d } from "zod";
5
+ import { z as l } from "zod";
6
6
  const te = /* @__PURE__ */ new Set([
7
7
  // pages: home, articles, contact, privacy policy, terms of service
8
8
  "0098e4f0-e5f3-4e28-acfc-bb9fdb3a4a6b",
@@ -30,7 +30,7 @@ const te = /* @__PURE__ */ new Set([
30
30
  "images/nextblock-logo-small.webp",
31
31
  "images/programmer-upscaled.webp",
32
32
  "images/update_nextblock.webp"
33
- ]), ke = "NextBlock™ CMS", Te = "nextblock cms", D = "home", Ce = [
33
+ ]), ve = "NextBlock™ CMS", $e = "nextblock cms", N = "home", Te = [
34
34
  "active_logo_id",
35
35
  "footer_copyright",
36
36
  "footer_show_attribution",
@@ -38,33 +38,33 @@ const te = /* @__PURE__ */ new Set([
38
38
  "site_keywords",
39
39
  "site_title"
40
40
  ];
41
- function L(t) {
41
+ function O(t) {
42
42
  if (!t?.supabase)
43
43
  throw new Error("No database connection is available for this tool.");
44
44
  return t.supabase;
45
45
  }
46
- function g(t) {
46
+ function h(t) {
47
47
  return t ? t instanceof Error ? t.message : typeof t == "object" && "message" in t ? String(t.message) : String(t) : "unknown error";
48
48
  }
49
49
  function ie(t) {
50
50
  return t === null || typeof t != "object" ? JSON.stringify(t) : Array.isArray(t) ? `[${t.map((i) => ie(i)).join(",")}]` : `{${Object.keys(t).sort().map((i) => `${JSON.stringify(i)}:${ie(t[i])}`).join(",")}}`;
51
51
  }
52
- function Ee(t) {
52
+ function Ce(t) {
53
53
  let i = 2166136261;
54
- const s = ie(t);
55
- for (let n = 0; n < s.length; n++)
56
- i ^= s.charCodeAt(n), i = Math.imul(i, 16777619);
54
+ const a = ie(t);
55
+ for (let s = 0; s < a.length; s++)
56
+ i ^= a.charCodeAt(s), i = Math.imul(i, 16777619);
57
57
  return (i >>> 0).toString(16).padStart(8, "0");
58
58
  }
59
59
  function ee(t) {
60
60
  return t.replace(/\s+/g, " ").trim().toUpperCase();
61
61
  }
62
- function se(t) {
62
+ function ae(t) {
63
63
  if (t.context?.skipConfirmation)
64
64
  return null;
65
65
  const i = `${ee(
66
66
  `CONFIRM ${t.action} ${t.subject}`
67
- )} #${Ee(t.payload)}`;
67
+ )} #${Ce(t.payload)}`;
68
68
  return ee(t.context?.latestUserMessage || "").includes(ee(i)) ? null : {
69
69
  confirmationPhrase: i,
70
70
  mutationExecuted: !1,
@@ -77,179 +77,180 @@ function xe() {
77
77
  return globalThis.crypto?.randomUUID?.() || `id-${Date.now()}-${Math.random().toString(36).slice(2)}`;
78
78
  }
79
79
  function ce(t, i = 200) {
80
- const s = [];
81
- for (let n = 0; n < t.length; n += i)
82
- s.push(t.slice(n, n + i));
83
- return s;
80
+ const a = [];
81
+ for (let s = 0; s < t.length; s += i)
82
+ a.push(t.slice(s, s + i));
83
+ return a;
84
84
  }
85
- function ne(t, i = []) {
86
- let s = t?.revalidatePath;
87
- if (!s)
85
+ function oe(t, i = []) {
86
+ let a = t?.revalidatePath;
87
+ if (!a)
88
88
  try {
89
- s = require("next/cache").revalidatePath;
89
+ a = require("next/cache").revalidatePath;
90
90
  } catch {
91
91
  return;
92
92
  }
93
93
  try {
94
- s("/", "layout");
95
- for (const n of i)
96
- s(n);
94
+ a("/", "layout");
95
+ for (const s of i)
96
+ a(s);
97
97
  } catch {
98
98
  }
99
99
  }
100
- async function Ie(t, i) {
101
- const { data: s, error: n } = await t.from("site_settings").select("key, value").in("key", [...i]);
102
- if (n)
103
- throw new Error(`Could not read the site settings: ${g(n)}`);
104
- return new Map((Array.isArray(s) ? s : []).map((o) => [o.key, o.value]));
100
+ async function Ee(t, i) {
101
+ const { data: a, error: s } = await t.from("site_settings").select("key, value").in("key", [...i]);
102
+ if (s)
103
+ throw new Error(`Could not read the site settings: ${h(s)}`);
104
+ return new Map((Array.isArray(a) ? a : []).map((n) => [n.key, n.value]));
105
105
  }
106
- async function v(t, i, s) {
107
- const { error: n } = await t.from("site_settings").upsert({ key: i, value: s });
108
- if (n)
109
- throw new Error(`Could not save the "${i}" setting: ${g(n)}`);
106
+ async function k(t, i, a) {
107
+ const { error: s } = await t.from("site_settings").upsert({ key: i, value: a });
108
+ if (s)
109
+ throw new Error(`Could not save the "${i}" setting: ${h(s)}`);
110
110
  }
111
- async function N(t, i, s, n) {
112
- let o = 0;
113
- for (const a of ce(n)) {
114
- const { error: r } = await t.from(i).delete().in(s, a);
111
+ async function I(t, i, a, s) {
112
+ let n = 0;
113
+ for (const o of ce(s)) {
114
+ const { error: r } = await t.from(i).delete().in(a, o);
115
115
  if (r)
116
- throw new Error(`Could not delete from ${i}: ${g(r)}`);
117
- o += a.length;
116
+ throw new Error(`Could not delete from ${i}: ${h(r)}`);
117
+ n += o.length;
118
118
  }
119
- return o;
119
+ return n;
120
120
  }
121
- function Ne(t) {
121
+ function Ie(t) {
122
122
  return !t || typeof t != "object" ? !1 : Object.values(t).some(
123
- (i) => typeof i == "string" && i.toLowerCase().includes(Te)
123
+ (i) => typeof i == "string" && i.toLowerCase().includes($e)
124
124
  );
125
125
  }
126
- async function oe(t) {
127
- const { data: i, error: s } = await t.from("site_settings").select("value").eq("key", Q).maybeSingle();
128
- return s ? null : le(i?.value);
126
+ async function ne(t) {
127
+ const { data: i, error: a } = await t.from("site_settings").select("value").eq("key", V).maybeSingle();
128
+ return a ? null : de(i?.value);
129
129
  }
130
- const ge = d.strictObject({
131
- brief: de.describe(
130
+ const ge = l.strictObject({
131
+ brief: le.describe(
132
132
  'The fields you learned. With mode "merge" (default) they are layered over the saved brief, so you can save after every answer.'
133
133
  ),
134
- mode: d.enum(["merge", "replace"]).default("merge")
134
+ mode: l.enum(["merge", "replace"]).default("merge")
135
135
  });
136
- async function V(t, i) {
137
- const s = ge.parse(t), n = L(i), o = s.mode === "merge" ? await oe(n) : null, a = new Set(Object.keys(t.brief ?? {})), r = Object.fromEntries(
138
- Object.entries(s.brief).filter(([p]) => a.has(p))
139
- ), u = Se.parse({
140
- ...o ?? {},
136
+ async function Q(t, i) {
137
+ const a = ge.parse(t), s = O(i), n = a.mode === "merge" ? await ne(s) : null, o = new Set(Object.keys(t.brief ?? {})), r = Object.fromEntries(
138
+ Object.entries(a.brief).filter(([f]) => o.has(f))
139
+ ), d = we.parse({
140
+ ...n ?? {},
141
141
  ...r,
142
142
  updated_at: (/* @__PURE__ */ new Date()).toISOString()
143
143
  });
144
- return await v(n, Q, u), {
145
- brief: u,
146
- created: !o,
144
+ return await k(s, V, d), {
145
+ brief: d,
146
+ created: !n,
147
147
  mutationExecuted: !0,
148
148
  success: !0
149
149
  };
150
150
  }
151
151
  async function pe(t) {
152
- const { data: i, error: s } = await t.from("site_settings").select("value").eq("key", G).maybeSingle();
153
- if (s)
152
+ const { data: i, error: a } = await t.from("site_settings").select("value").eq("key", G).maybeSingle();
153
+ if (a)
154
154
  return null;
155
- const n = ue.safeParse(i?.value);
156
- return n.success ? n.data : null;
155
+ const s = ue.safeParse(i?.value);
156
+ return s.success ? s.data : null;
157
157
  }
158
- async function Fe(t, i) {
159
- const s = await pe(t);
160
- return ve(s, { actorUserId: i }) ? s : null;
158
+ async function He(t, i) {
159
+ const a = await pe(t);
160
+ return ke(a, { actorUserId: i }) ? a : null;
161
161
  }
162
- async function De(t) {
162
+ async function Ne(t) {
163
163
  const { error: i } = await t.from("site_settings").delete().eq("key", G);
164
164
  if (i)
165
- throw new Error(`Could not close the build session: ${g(i)}`);
165
+ throw new Error(`Could not close the build session: ${h(i)}`);
166
166
  }
167
- const me = d.strictObject({
168
- includeMedia: d.boolean().default(!1).describe("Also list the most recent media library entries (id, file name, alt text)."),
169
- includeNavigation: d.boolean().default(!0),
170
- limit: d.number().int().min(10).max(300).default(120).describe("Maximum pages, posts, and products to list.")
167
+ const me = l.strictObject({
168
+ includeMedia: l.boolean().default(!1).describe("Also list the most recent media library entries (id, file name, alt text)."),
169
+ includeNavigation: l.boolean().default(!0),
170
+ limit: l.number().int().min(10).max(300).default(120).describe("Maximum pages, posts, and products to list.")
171
171
  });
172
- function $e(t, i) {
173
- const s = { footer: {}, header: {} }, n = /* @__PURE__ */ new Map();
174
- for (const a of t) {
175
- const r = String(a.menu_key).toUpperCase() === "FOOTER" ? "footer" : "header";
176
- n.set(Number(a.id), {
172
+ function Ae(t, i) {
173
+ const a = { footer: {}, header: {} }, s = /* @__PURE__ */ new Map();
174
+ for (const o of t) {
175
+ const r = String(o.menu_key).toUpperCase() === "FOOTER" ? "footer" : "header";
176
+ s.set(Number(o.id), {
177
177
  children: [],
178
- id: Number(a.id),
179
- label: String(a.label ?? ""),
180
- languageCode: i.get(Number(a.language_id)) ?? String(a.language_id),
178
+ id: Number(o.id),
179
+ label: String(o.label ?? ""),
180
+ languageCode: i.get(Number(o.language_id)) ?? String(o.language_id),
181
181
  menu: r,
182
- order: Number(a.order ?? 0),
183
- parentId: a.parent_id === null || a.parent_id === void 0 ? null : Number(a.parent_id),
184
- url: String(a.url ?? "")
182
+ order: Number(o.order ?? 0),
183
+ parentId: o.parent_id === null || o.parent_id === void 0 ? null : Number(o.parent_id),
184
+ url: String(o.url ?? "")
185
185
  });
186
186
  }
187
- const o = [...n.values()].sort((a, r) => a.order - r.order);
188
- for (const a of o) {
189
- const r = a.parentId === null ? null : n.get(a.parentId);
187
+ const n = [...s.values()].sort((o, r) => o.order - r.order);
188
+ for (const o of n) {
189
+ const r = o.parentId === null ? null : s.get(o.parentId);
190
190
  if (r) {
191
- r.children.push({ children: a.children, id: a.id, label: a.label, url: a.url });
191
+ r.children.push({ children: o.children, id: o.id, label: o.label, url: o.url });
192
192
  continue;
193
193
  }
194
- (s[a.menu][a.languageCode] ??= []).push({ children: a.children, id: a.id, label: a.label, url: a.url });
194
+ (a[o.menu][o.languageCode] ??= []).push({ children: o.children, id: o.id, label: o.label, url: o.url });
195
195
  }
196
- return s;
196
+ return a;
197
197
  }
198
- async function Ae(t, i) {
199
- const s = me.parse(t), n = L(i), [o, a, r, u, p, b, m] = await Promise.all([
200
- n.from("languages").select("id, code, name, is_default, is_active").order("id"),
201
- Ie(n, [...Ce, Q, G]),
202
- n.from("pages").select("id, slug, title, status, language_id, translation_group_id, author_id, updated_at").order("id").limit(s.limit),
203
- n.from("posts").select("id, slug, title, status, language_id, translation_group_id, author_id, published_at").order("id").limit(s.limit),
204
- n.from("site_themes").select("slug, name, is_default, is_active, color_scheme").order("sort_order"),
205
- n.from("custom_block_definitions").select("slug, name, fields").order("name"),
206
- n.from("content_drafts").select("parent_type, parent_id, updated_at")
198
+ async function De(t, i) {
199
+ const a = me.parse(t), s = O(i), [n, o, r, d, f, c, g] = await Promise.all([
200
+ s.from("languages").select("id, code, name, is_default, is_active").order("id"),
201
+ Ee(s, [...Te, V, G]),
202
+ // `count: 'exact'` so the totals are the site's, not the capped list's.
203
+ s.from("pages").select("id, slug, title, status, language_id, translation_group_id, author_id, updated_at", { count: "exact" }).order("id").limit(a.limit),
204
+ s.from("posts").select("id, slug, title, status, language_id, translation_group_id, author_id, published_at", { count: "exact" }).order("id").limit(a.limit),
205
+ s.from("site_themes").select("slug, name, is_default, is_active, color_scheme").order("sort_order"),
206
+ s.from("custom_block_definitions").select("slug, name, fields").order("name"),
207
+ s.from("content_drafts").select("parent_type, parent_id, updated_at")
207
208
  ]);
208
- for (const [e, l] of [
209
- ["languages", o],
209
+ for (const [e, u] of [
210
+ ["languages", n],
210
211
  ["pages", r],
211
- ["posts", u],
212
- ["themes", p],
213
- ["custom blocks", b],
214
- ["drafts", m]
212
+ ["posts", d],
213
+ ["themes", f],
214
+ ["custom blocks", c],
215
+ ["drafts", g]
215
216
  ])
216
- if (l?.error)
217
- throw new Error(`Could not read the ${e}: ${g(l.error)}`);
218
- const f = o.data ?? [], _ = new Map(f.map((e) => [Number(e.id), String(e.code)])), M = f.find((e) => e.is_default) ?? f[0] ?? null;
219
- let k = [];
217
+ if (u?.error)
218
+ throw new Error(`Could not read the ${e}: ${h(u.error)}`);
219
+ const p = n.data ?? [], _ = new Map(p.map((e) => [Number(e.id), String(e.code)])), M = p.find((e) => e.is_default) ?? p[0] ?? null;
220
+ let v = [];
220
221
  try {
221
- const { data: e } = await n.from("products").select("id, slug, title, status, language_id").order("created_at", { ascending: !1 }).limit(s.limit);
222
- k = Array.isArray(e) ? e : [];
222
+ const { data: e } = await s.from("products").select("id, slug, title, status, language_id").order("created_at", { ascending: !1 }).limit(a.limit);
223
+ v = Array.isArray(e) ? e : [];
223
224
  } catch {
224
- k = [];
225
+ v = [];
225
226
  }
226
- const T = /* @__PURE__ */ new Map();
227
+ const $ = /* @__PURE__ */ new Map();
227
228
  {
228
- const { data: e } = await n.from("blocks").select("page_id, post_id").limit(5e3);
229
- for (const l of Array.isArray(e) ? e : []) {
230
- const h = l.page_id ? `page:${l.page_id}` : l.post_id ? `post:${l.post_id}` : null;
231
- h && T.set(h, (T.get(h) ?? 0) + 1);
229
+ const { data: e } = await s.from("blocks").select("page_id, post_id").limit(5e3);
230
+ for (const u of Array.isArray(e) ? e : []) {
231
+ const b = u.page_id ? `page:${u.page_id}` : u.post_id ? `post:${u.post_id}` : null;
232
+ b && $.set(b, ($.get(b) ?? 0) + 1);
232
233
  }
233
234
  }
234
235
  let F = null;
235
- if (s.includeNavigation) {
236
- const { data: e, error: l } = await n.from("navigation_items").select("id, label, url, menu_key, language_id, parent_id, order, page_id").order("order");
237
- if (l)
238
- throw new Error(`Could not read the navigation: ${g(l)}`);
239
- F = $e(Array.isArray(e) ? e : [], _);
236
+ if (a.includeNavigation) {
237
+ const { data: e, error: u } = await s.from("navigation_items").select("id, label, url, menu_key, language_id, parent_id, order, page_id").order("order");
238
+ if (u)
239
+ throw new Error(`Could not read the navigation: ${h(u)}`);
240
+ F = Ae(Array.isArray(e) ? e : [], _);
240
241
  }
241
- const { data: Y } = await n.from("media").select("id, object_key, file_name, description, created_at").order("created_at", { ascending: !1 }).limit(500), y = Array.isArray(Y) ? Y : [], C = y.filter((e) => J.has(String(e.object_key))).length, { data: E } = await n.from("logos").select("id, name, media_id, created_at").order("created_at", { ascending: !1 }), j = Array.isArray(E) ? E : [], K = typeof a.get("active_logo_id") == "string" ? a.get("active_logo_id") : null, S = (K ? j.find((e) => e.id === K) : null) ?? j[0] ?? null, $ = S ? y.find((e) => e.id === S.media_id) ?? null : null, A = $ ? String($.object_key) : null, B = (r.data ?? []).map((e) => ({
242
- blockCount: T.get(`page:${e.id}`) ?? 0,
242
+ const { data: H } = await s.from("media").select("id, object_key, file_name, description, created_at").order("created_at", { ascending: !1 }).limit(500), y = Array.isArray(H) ? H : [], T = y.filter((e) => J.has(String(e.object_key))).length, { data: C } = await s.from("logos").select("id, name, media_id, created_at").order("created_at", { ascending: !1 }), j = Array.isArray(C) ? C : [], K = typeof o.get("active_logo_id") == "string" ? o.get("active_logo_id") : null, w = (K ? j.find((e) => e.id === K) : null) ?? j[0] ?? null, A = w ? y.find((e) => e.id === w.media_id) ?? null : null, D = A ? String(A.object_key) : null, B = (r.data ?? []).map((e) => ({
243
+ blockCount: $.get(`page:${e.id}`) ?? 0,
243
244
  id: Number(e.id),
244
- isHome: e.slug === D && (!M || Number(e.language_id) === Number(M.id)),
245
+ isHome: e.slug === N && (!M || Number(e.language_id) === Number(M.id)),
245
246
  isSeeded: te.has(String(e.translation_group_id)),
246
247
  languageCode: _.get(Number(e.language_id)) ?? String(e.language_id),
247
248
  slug: String(e.slug),
248
249
  status: String(e.status),
249
250
  title: String(e.title ?? ""),
250
251
  translationGroupId: e.translation_group_id ? String(e.translation_group_id) : null
251
- })), R = (u.data ?? []).map((e) => ({
252
- blockCount: T.get(`post:${e.id}`) ?? 0,
252
+ })), P = (d.data ?? []).map((e) => ({
253
+ blockCount: $.get(`post:${e.id}`) ?? 0,
253
254
  id: Number(e.id),
254
255
  isSeeded: te.has(String(e.translation_group_id)),
255
256
  languageCode: _.get(Number(e.language_id)) ?? String(e.language_id),
@@ -258,54 +259,54 @@ async function Ae(t, i) {
258
259
  status: String(e.status),
259
260
  title: String(e.title ?? ""),
260
261
  translationGroupId: e.translation_group_id ? String(e.translation_group_id) : null
261
- })), U = typeof a.get("site_title") == "string" ? a.get("site_title") : "", H = a.get("footer_copyright"), c = {
262
- copyright: Ne(H),
263
- logo: !!(A && J.has(A)),
264
- media: C,
262
+ })), U = typeof o.get("site_title") == "string" ? o.get("site_title") : "", Y = o.get("footer_copyright"), m = {
263
+ copyright: Ie(Y),
264
+ logo: !!(D && J.has(D)),
265
+ media: T,
265
266
  pages: B.filter((e) => e.isSeeded).length,
266
- posts: R.filter((e) => e.isSeeded).length,
267
- siteTitle: U.trim() === ke
268
- }, X = c.copyright || c.logo || c.media > 0 || c.pages > 0 || c.posts > 0 || c.siteTitle, q = le(a.get(Q)), z = ue.safeParse(a.get(G)), x = z.success ? z.data : null, O = B.find((e) => e.isHome) ?? null, w = [];
269
- return O ? O.blockCount === 0 && w.push("The home page exists but has no blocks. Fill it with rewrite_page_draft then publish_content_draft.") : w.push(
270
- `There is no "${D}" page in the default language, so "/" returns 404. Create one with create_cms_page (slug "${D}", status "published").`
271
- ), X && w.push(
272
- `NextBlock demo content is still present (${c.pages} seeded pages, ${c.posts} seeded posts, ${c.media} bundled images${c.logo ? ", the NextBlock logo" : ""}${c.siteTitle ? ", the default site title" : ""}${c.copyright ? ", the default copyright" : ""}). Remove it with reset_site_content, or pass reset to start_site_build.`
273
- ), q || w.push("No site brief is saved yet. Interview the client and record the answers with save_site_brief."), {
267
+ posts: P.filter((e) => e.isSeeded).length,
268
+ siteTitle: U.trim() === ve
269
+ }, W = m.copyright || m.logo || m.media > 0 || m.pages > 0 || m.posts > 0 || m.siteTitle, q = de(o.get(V)), X = ue.safeParse(o.get(G)), x = X.success ? X.data : null, R = B.find((e) => e.isHome) ?? null, S = [];
270
+ return R ? R.blockCount === 0 && S.push("The home page exists but has no blocks. Fill it with rewrite_page_draft then publish_content_draft.") : S.push(
271
+ `There is no "${N}" page in the default language, so "/" returns 404. Create one with create_cms_page (slug "${N}", status "published").`
272
+ ), W && S.push(
273
+ `NextBlock demo content is still present (${m.pages} seeded pages, ${m.posts} seeded posts, ${m.media} bundled images${m.logo ? ", the NextBlock logo" : ""}${m.siteTitle ? ", the default site title" : ""}${m.copyright ? ", the default copyright" : ""}). Remove it with reset_site_content, or pass reset to start_site_build.`
274
+ ), q || S.push("No site brief is saved yet. Interview the client and record the answers with save_site_brief."), {
274
275
  brief: q,
275
276
  buildSession: x ? { expiresAt: x.expiresAt, id: x.id, summary: x.summary } : null,
276
277
  counts: {
277
- customBlocks: (b.data ?? []).length,
278
- drafts: (m.data ?? []).length,
278
+ customBlocks: (c.data ?? []).length,
279
+ drafts: (g.data ?? []).length,
279
280
  media: y.length,
280
- pages: B.length,
281
- posts: R.length,
282
- products: k.length
281
+ pages: typeof r.count == "number" ? r.count : B.length,
282
+ posts: typeof d.count == "number" ? d.count : P.length,
283
+ products: v.length
283
284
  },
284
- customBlocks: (b.data ?? []).map((e) => ({
285
+ customBlocks: (c.data ?? []).map((e) => ({
285
286
  fieldCount: Array.isArray(e.fields) ? e.fields.length : 0,
286
- fields: Array.isArray(e.fields) ? e.fields.map((l) => ({ key: l?.key, required: l?.required === !0, type: l?.type })) : [],
287
+ fields: Array.isArray(e.fields) ? e.fields.map((u) => ({ key: u?.key, required: u?.required === !0, type: u?.type })) : [],
287
288
  name: String(e.name ?? ""),
288
289
  slug: String(e.slug)
289
290
  })),
290
- drafts: (m.data ?? []).map((e) => ({
291
+ drafts: (g.data ?? []).map((e) => ({
291
292
  parentId: Number(e.parent_id),
292
293
  parentType: String(e.parent_type)
293
294
  })),
294
- homePage: O,
295
+ homePage: R,
295
296
  identity: {
296
- activeLogo: S ? {
297
- id: String(S.id),
298
- isSeeded: c.logo,
299
- name: String(S.name ?? ""),
300
- objectKey: A
297
+ activeLogo: w ? {
298
+ id: String(w.id),
299
+ isSeeded: m.logo,
300
+ name: String(w.name ?? ""),
301
+ objectKey: D
301
302
  } : null,
302
- footerCopyright: H ?? null,
303
- footerShowAttribution: a.get("footer_show_attribution") !== !1,
304
- siteDescription: typeof a.get("site_description") == "string" ? a.get("site_description") : "",
305
- siteKeywords: typeof a.get("site_keywords") == "string" ? a.get("site_keywords") : "",
303
+ footerCopyright: Y ?? null,
304
+ footerShowAttribution: o.get("footer_show_attribution") !== !1,
305
+ siteDescription: typeof o.get("site_description") == "string" ? o.get("site_description") : "",
306
+ siteKeywords: typeof o.get("site_keywords") == "string" ? o.get("site_keywords") : "",
306
307
  siteTitle: U
307
308
  },
308
- languages: f.map((e) => ({
309
+ languages: p.map((e) => ({
309
310
  code: String(e.code),
310
311
  id: Number(e.id),
311
312
  isActive: e.is_active !== !1,
@@ -314,28 +315,28 @@ async function Ae(t, i) {
314
315
  })),
315
316
  media: {
316
317
  count: y.length,
317
- sample: s.includeMedia ? y.slice(0, 40).map((e) => ({
318
+ sample: a.includeMedia ? y.slice(0, 40).map((e) => ({
318
319
  altText: e.description ?? null,
319
320
  fileName: e.file_name ?? null,
320
321
  id: String(e.id),
321
322
  isSeeded: J.has(String(e.object_key))
322
323
  })) : void 0,
323
- seededCount: C
324
+ seededCount: T
324
325
  },
325
326
  navigation: F,
326
- nextSteps: w,
327
+ nextSteps: S,
327
328
  pages: B,
328
- posts: R,
329
- products: k.map((e) => ({
329
+ posts: P,
330
+ products: v.map((e) => ({
330
331
  id: String(e.id),
331
332
  languageCode: _.get(Number(e.language_id)) ?? String(e.language_id),
332
333
  slug: String(e.slug),
333
334
  status: String(e.status),
334
335
  title: String(e.title ?? "")
335
336
  })),
336
- seeded: { ...c, anyPresent: X },
337
+ seeded: { ...m, anyPresent: W },
337
338
  success: !0,
338
- themes: (p.data ?? []).map((e) => ({
339
+ themes: (f.data ?? []).map((e) => ({
339
340
  colorScheme: String(e.color_scheme ?? "light"),
340
341
  isActive: e.is_active !== !1,
341
342
  isDefault: e.is_default === !0,
@@ -344,57 +345,97 @@ async function Ae(t, i) {
344
345
  }))
345
346
  };
346
347
  }
347
- const fe = d.strictObject({
348
- active_logo_id: d.string().uuid().nullable().optional().describe("Pin a logos.id as the active logo, or null to fall back to the newest logo."),
349
- footer_copyright: d.record(d.string().trim().min(2).max(12), d.string().trim().min(1).max(500)).optional().describe('Copyright line per locale code; "{year}" is replaced with the current year. Example: { "en": {year} Acme Bakery", "fr": {year} Boulangerie Acme" }.'),
350
- footer_show_attribution: d.boolean().optional().describe('Whether the "Published with NextBlock" credit shows in the footer. Set false when the client wants no NextBlock mention.'),
351
- site_description: d.string().trim().max(500).optional().describe("Default meta description and header tagline."),
352
- site_keywords: d.string().trim().max(500).optional().describe("Comma-separated SEO keywords."),
353
- site_title: d.string().trim().min(1).max(160).optional().describe("The brand name shown in the header and <title>.")
348
+ const Be = 30;
349
+ function Ye(t) {
350
+ const { counts: i, seeded: a } = t, s = [];
351
+ s.push(
352
+ `Counts: ${i.pages} pages, ${i.posts} posts, ${i.products} products, ${i.media} media, ${i.customBlocks} custom blocks, ${i.drafts} drafts.`
353
+ );
354
+ const n = t.languages.filter((c) => c.isActive), o = (n.length > 0 ? n : t.languages).map((c) => `${c.code}${c.isDefault ? " (default)" : ""}`).join(", ");
355
+ if (s.push(`Languages: ${o || "none"}.`), a.anyPresent) {
356
+ const c = [
357
+ a.pages > 0 ? `${a.pages} demo pages` : null,
358
+ a.posts > 0 ? `${a.posts} demo posts` : null,
359
+ a.media > 0 ? `${a.media} bundled images` : null,
360
+ a.logo ? "the NextBlock logo" : null,
361
+ a.siteTitle ? "the default site title" : null,
362
+ a.copyright ? "the default copyright" : null
363
+ ].filter((g) => !!g);
364
+ s.push(`NextBlock demo content: present (${c.join(", ")}).`);
365
+ } else
366
+ s.push("NextBlock demo content: none detected.");
367
+ const r = t.homePage;
368
+ s.push(
369
+ r ? `Home page: "${r.title}" (/${r.slug}) ${r.status}, ${r.blockCount} blocks${r.isSeeded ? ", seeded demo content" : ""}.` : 'Home page: missing in the default language ("/" returns 404).'
370
+ );
371
+ const d = t.identity, f = d.activeLogo ? `logo "${d.activeLogo.name || d.activeLogo.id}"${d.activeLogo.isSeeded ? " (NextBlock demo logo)" : ""}` : "no logo";
372
+ if (s.push(`Site title: ${d.siteTitle ? `"${d.siteTitle}"` : "not set"}; ${f}.`), t.themes.length > 0 && s.push(
373
+ `Themes: ${t.themes.map(
374
+ (c) => `${c.name || c.slug} (${c.colorScheme}${c.isDefault ? ", default" : ""}${c.isActive ? "" : ", inactive"})`
375
+ ).join("; ")}.`
376
+ ), s.push(t.brief ? `Site brief: saved (status ${t.brief.status}).` : "Site brief: none saved."), s.push(
377
+ t.buildSession ? `Build session: active until ${t.buildSession.expiresAt}.` : "Build session: none."
378
+ ), t.pages.length > 0) {
379
+ const c = t.pages.slice(0, Be), g = Math.max(i.pages, t.pages.length) - c.length;
380
+ s.push(
381
+ `Pages: ${c.map((p) => `${p.title || p.slug} (/${p.slug}) [${p.languageCode}]`).join(", ")}${g > 0 ? `, +${g} more` : ""}.`
382
+ );
383
+ } else
384
+ s.push("Pages: none.");
385
+ return s.join(`
386
+ `);
387
+ }
388
+ const fe = l.strictObject({
389
+ active_logo_id: l.string().uuid().nullable().optional().describe("Pin a logos.id as the active logo, or null to fall back to the newest logo."),
390
+ footer_copyright: l.record(l.string().trim().min(2).max(12), l.string().trim().min(1).max(500)).optional().describe('Copyright line per locale code; "{year}" is replaced with the current year. Example: { "en": "© {year} Acme Bakery", "fr": "© {year} Boulangerie Acme" }.'),
391
+ footer_show_attribution: l.boolean().optional().describe('Whether the "Published with NextBlock" credit shows in the footer. Set false when the client wants no NextBlock mention.'),
392
+ site_description: l.string().trim().max(500).optional().describe("Default meta description and header tagline."),
393
+ site_keywords: l.string().trim().max(500).optional().describe("Comma-separated SEO keywords."),
394
+ site_title: l.string().trim().min(1).max(160).optional().describe("The brand name shown in the header and <title>.")
354
395
  }).refine((t) => Object.values(t).some((i) => i !== void 0), {
355
396
  message: "Provide at least one identity field to update."
356
397
  });
357
- async function Be(t, i) {
358
- const s = fe.parse(t);
359
- await ae(i, ["ADMIN", "WRITER"], "Updating the site identity");
360
- const n = L(i), o = Object.entries(s).filter(([, r]) => r !== void 0), a = se({
398
+ async function Pe(t, i) {
399
+ const a = fe.parse(t);
400
+ await se(i, ["ADMIN", "WRITER"], "Updating the site identity");
401
+ const s = O(i), n = Object.entries(a).filter(([, r]) => r !== void 0), o = ae({
361
402
  action: "UPDATE SITE IDENTITY",
362
403
  context: i,
363
- payload: { input: s, tool: "update_site_identity" },
404
+ payload: { input: a, tool: "update_site_identity" },
364
405
  preview: {
365
- changes: Object.fromEntries(o),
366
- summary: `Update ${o.map(([r]) => r).join(", ")} for the whole site.`
406
+ changes: Object.fromEntries(n),
407
+ summary: `Update ${n.map(([r]) => r).join(", ")} for the whole site.`
367
408
  },
368
- subject: o.map(([r]) => r).join(" ")
409
+ subject: n.map(([r]) => r).join(" ")
369
410
  });
370
- if (a)
371
- return a;
372
- for (const [r, u] of o)
373
- await v(n, r, u);
374
- return ne(i, ["/cms/settings/logos", "/cms/settings/copyright"]), {
411
+ if (o)
412
+ return o;
413
+ for (const [r, d] of n)
414
+ await k(s, r, d);
415
+ return oe(i, ["/cms/settings/logos", "/cms/settings/copyright"]), {
375
416
  mutationExecuted: !0,
376
417
  success: !0,
377
- updatedKeys: o.map(([r]) => r)
418
+ updatedKeys: n.map(([r]) => r)
378
419
  };
379
420
  }
380
- const Re = d.strictObject({
381
- customBlocks: d.boolean().default(!1).describe("Delete every custom block definition."),
382
- identity: d.boolean().default(!0).describe("Clear the NextBlock site title, description, keywords, copyright line, and the seeded logo."),
383
- navigation: d.boolean().default(!0).describe("Delete every header and footer navigation item."),
384
- pages: d.boolean().default(!0).describe("Delete pages (all languages) except keepPageSlugs."),
385
- posts: d.boolean().default(!0).describe("Delete every blog post."),
386
- products: d.boolean().default(!1).describe("Delete every product, with its variants, images, and drafts."),
387
- seededMedia: d.boolean().default(!0).describe("Delete the bundled NextBlock demo images from the media library.")
388
- }).partial(), Z = d.strictObject({
389
- clearKeptPageBlocks: d.boolean().default(!0).describe('Empty the blocks of the kept pages (usually "home") so they can be rebuilt. Set false to leave their content untouched.'),
390
- dryRun: d.boolean().default(!1).describe("Report what would be removed without changing anything. No confirmation needed."),
391
- keepLanguages: d.array(d.string().trim().min(2).max(10)).max(10).optional().describe("Locale codes the site should keep. Other languages are deactivated (never deleted) and their pages and posts removed. Omit to keep every language as it is."),
392
- keepPageSlugs: d.array(d.string().trim().min(1).max(300)).max(50).default([D]).describe('Pages to keep, by slug in any language; every translation of a kept page is kept. Defaults to ["home"] because "/" must always resolve.'),
393
- onlySeeded: d.boolean().default(!1).describe("Remove only rows that match the NextBlock demo seed (fixed translation groups, bundled images). Use when the site already has real content next to the demo content."),
421
+ const Re = l.strictObject({
422
+ customBlocks: l.boolean().default(!1).describe("Delete every custom block definition."),
423
+ identity: l.boolean().default(!0).describe("Clear the NextBlock site title, description, keywords, copyright line, and the seeded logo."),
424
+ navigation: l.boolean().default(!0).describe("Delete every header and footer navigation item."),
425
+ pages: l.boolean().default(!0).describe("Delete pages (all languages) except keepPageSlugs."),
426
+ posts: l.boolean().default(!0).describe("Delete every blog post."),
427
+ products: l.boolean().default(!1).describe("Delete every product, with its variants, images, and drafts."),
428
+ seededMedia: l.boolean().default(!0).describe("Delete the bundled NextBlock demo images from the media library.")
429
+ }).partial(), Z = l.strictObject({
430
+ clearKeptPageBlocks: l.boolean().default(!0).describe('Empty the blocks of the kept pages (usually "home") so they can be rebuilt. Set false to leave their content untouched.'),
431
+ dryRun: l.boolean().default(!1).describe("Report what would be removed without changing anything. No confirmation needed."),
432
+ keepLanguages: l.array(l.string().trim().min(2).max(10)).max(10).optional().describe("Locale codes the site should keep. Other languages are deactivated (never deleted) and their pages and posts removed. Omit to keep every language as it is."),
433
+ keepPageSlugs: l.array(l.string().trim().min(1).max(300)).max(50).default([N]).describe('Pages to keep, by slug in any language; every translation of a kept page is kept. Defaults to ["home"] because "/" must always resolve.'),
434
+ onlySeeded: l.boolean().default(!1).describe("Remove only rows that match the NextBlock demo seed (fixed translation groups, bundled images). Use when the site already has real content next to the demo content."),
394
435
  scope: Re.default({})
395
436
  });
396
437
  async function he(t, i) {
397
- const s = {
438
+ const a = {
398
439
  customBlocks: t.scope.customBlocks ?? !1,
399
440
  identity: t.scope.identity ?? !0,
400
441
  navigation: t.scope.navigation ?? !0,
@@ -402,84 +443,84 @@ async function he(t, i) {
402
443
  posts: t.scope.posts ?? !0,
403
444
  products: t.scope.products ?? !1,
404
445
  seededMedia: t.scope.seededMedia ?? !0
405
- }, { data: n, error: o } = await i.from("languages").select("id, code, name, is_default, is_active");
406
- if (o)
407
- throw new Error(`Could not read the languages: ${g(o)}`);
408
- const a = Array.isArray(n) ? n : [], r = new Map(a.map((e) => [Number(e.id), String(e.code).toLowerCase()])), u = t.keepLanguages ? new Set(t.keepLanguages.map((e) => e.toLowerCase())) : null;
409
- if (u) {
410
- const e = new Set(r.values()), l = [...u].filter((h) => !e.has(h));
411
- if (l.length > 0)
412
- throw new Error(`Unknown language code(s) in keepLanguages: ${l.join(", ")}. Existing: ${[...e].join(", ")}.`);
446
+ }, { data: s, error: n } = await i.from("languages").select("id, code, name, is_default, is_active");
447
+ if (n)
448
+ throw new Error(`Could not read the languages: ${h(n)}`);
449
+ const o = Array.isArray(s) ? s : [], r = new Map(o.map((e) => [Number(e.id), String(e.code).toLowerCase()])), d = t.keepLanguages ? new Set(t.keepLanguages.map((e) => e.toLowerCase())) : null;
450
+ if (d) {
451
+ const e = new Set(r.values()), u = [...d].filter((b) => !e.has(b));
452
+ if (u.length > 0)
453
+ throw new Error(`Unknown language code(s) in keepLanguages: ${u.join(", ")}. Existing: ${[...e].join(", ")}.`);
413
454
  }
414
- const p = new Set(
415
- a.filter((e) => !u || u.has(String(e.code).toLowerCase())).map((e) => Number(e.id))
416
- ), b = a.filter((e) => u && !u.has(String(e.code).toLowerCase()) && e.is_active !== !1).map((e) => ({ code: String(e.code), id: Number(e.id) })), m = a.find((e) => e.is_default === !0), f = u && m && !u.has(String(m.code).toLowerCase()) ? (() => {
417
- const e = a.find((l) => u.has(String(l.code).toLowerCase()));
455
+ const f = new Set(
456
+ o.filter((e) => !d || d.has(String(e.code).toLowerCase())).map((e) => Number(e.id))
457
+ ), c = o.filter((e) => d && !d.has(String(e.code).toLowerCase()) && e.is_active !== !1).map((e) => ({ code: String(e.code), id: Number(e.id) })), g = o.find((e) => e.is_default === !0), p = d && g && !d.has(String(g.code).toLowerCase()) ? (() => {
458
+ const e = o.find((u) => d.has(String(u.code).toLowerCase()));
418
459
  return e ? { code: String(e.code), id: Number(e.id) } : null;
419
- })() : null, _ = (e) => te.has(String(e.translation_group_id)), { data: M, error: k } = await i.from("pages").select("id, slug, language_id, translation_group_id, author_id");
420
- if (k)
421
- throw new Error(`Could not read the pages: ${g(k)}`);
422
- const T = Array.isArray(M) ? M : [], F = new Set(t.keepPageSlugs.map((e) => e.toLowerCase())), Y = new Set(
423
- T.filter((e) => F.has(String(e.slug).toLowerCase())).map((e) => String(e.translation_group_id))
460
+ })() : null, _ = (e) => te.has(String(e.translation_group_id)), { data: M, error: v } = await i.from("pages").select("id, slug, language_id, translation_group_id, author_id");
461
+ if (v)
462
+ throw new Error(`Could not read the pages: ${h(v)}`);
463
+ const $ = Array.isArray(M) ? M : [], F = new Set(t.keepPageSlugs.map((e) => e.toLowerCase())), H = new Set(
464
+ $.filter((e) => F.has(String(e.slug).toLowerCase())).map((e) => String(e.translation_group_id))
424
465
  ), y = (e) => ({
425
466
  id: Number(e.id),
426
467
  languageCode: r.get(Number(e.language_id)) ?? String(e.language_id),
427
468
  slug: String(e.slug)
428
- }), C = [], E = [];
429
- for (const e of T) {
430
- const l = Y.has(String(e.translation_group_id)), h = p.has(Number(e.language_id)), W = !t.onlySeeded || _(e);
431
- l && h ? E.push(y(e)) : s.pages && W || !h ? C.push(y(e)) : E.push(y(e));
469
+ }), T = [], C = [];
470
+ for (const e of $) {
471
+ const u = H.has(String(e.translation_group_id)), b = f.has(Number(e.language_id)), z = !t.onlySeeded || _(e);
472
+ u && b ? C.push(y(e)) : a.pages && z || !b ? T.push(y(e)) : C.push(y(e));
432
473
  }
433
474
  const { data: j, error: K } = await i.from("posts").select("id, slug, language_id, translation_group_id, author_id");
434
475
  if (K)
435
- throw new Error(`Could not read the posts: ${g(K)}`);
436
- const S = (Array.isArray(j) ? j : []).filter((e) => {
437
- const l = p.has(Number(e.language_id)), h = !t.onlySeeded || _(e);
438
- return s.posts && h || !l;
476
+ throw new Error(`Could not read the posts: ${h(K)}`);
477
+ const w = (Array.isArray(j) ? j : []).filter((e) => {
478
+ const u = f.has(Number(e.language_id)), b = !t.onlySeeded || _(e);
479
+ return a.posts && b || !u;
439
480
  }).map(y);
440
- let $ = [];
441
- if (s.products)
481
+ let A = [];
482
+ if (a.products)
442
483
  try {
443
484
  const { data: e } = await i.from("products").select("id");
444
- $ = (Array.isArray(e) ? e : []).map((l) => String(l.id));
485
+ A = (Array.isArray(e) ? e : []).map((u) => String(u.id));
445
486
  } catch {
446
- $ = [];
487
+ A = [];
447
488
  }
448
- const A = new Set(C.map((e) => e.id)), B = new Set(C.map((e) => `/${e.slug}`)), { data: R, error: U } = await i.from("navigation_items").select("id, url, page_id");
489
+ const D = new Set(T.map((e) => e.id)), B = new Set(T.map((e) => `/${e.slug}`)), { data: P, error: U } = await i.from("navigation_items").select("id, url, page_id");
449
490
  if (U)
450
- throw new Error(`Could not read the navigation: ${g(U)}`);
451
- const H = (Array.isArray(R) ? R : []).filter(
452
- (e) => s.navigation || e.page_id !== null && e.page_id !== void 0 && A.has(Number(e.page_id)) || B.has(String(e.url))
453
- ).map((e) => Number(e.id)), c = E.map((e) => e.id), X = new Set(S.map((e) => e.id)), { data: q } = await i.from("content_drafts").select("id, parent_type, parent_id"), z = (Array.isArray(q) ? q : []).filter((e) => {
454
- const l = Number(e.parent_id);
455
- return e.parent_type === "page" ? A.has(l) || t.clearKeptPageBlocks && c.includes(l) : e.parent_type === "post" && X.has(l);
491
+ throw new Error(`Could not read the navigation: ${h(U)}`);
492
+ const Y = (Array.isArray(P) ? P : []).filter(
493
+ (e) => a.navigation || e.page_id !== null && e.page_id !== void 0 && D.has(Number(e.page_id)) || B.has(String(e.url))
494
+ ).map((e) => Number(e.id)), m = C.map((e) => e.id), W = new Set(w.map((e) => e.id)), { data: q } = await i.from("content_drafts").select("id, parent_type, parent_id"), X = (Array.isArray(q) ? q : []).filter((e) => {
495
+ const u = Number(e.parent_id);
496
+ return e.parent_type === "page" ? D.has(u) || t.clearKeptPageBlocks && m.includes(u) : e.parent_type === "post" && W.has(u);
456
497
  }).map((e) => ({ parentId: Number(e.parent_id), parentType: String(e.parent_type) }));
457
498
  let x = [];
458
- if (s.customBlocks) {
499
+ if (a.customBlocks) {
459
500
  const { data: e } = await i.from("custom_block_definitions").select("slug");
460
- x = (Array.isArray(e) ? e : []).map((l) => String(l.slug));
501
+ x = (Array.isArray(e) ? e : []).map((u) => String(u.slug));
461
502
  }
462
- let O = [], w = [];
463
- if (s.seededMedia || s.identity) {
464
- const { data: e } = await i.from("media").select("id, object_key"), l = (Array.isArray(e) ? e : []).filter(
465
- (I) => J.has(String(I.object_key))
466
- ), h = new Set(l.map((I) => String(I.id))), { data: W } = await i.from("logos").select("id, media_id");
467
- w = (Array.isArray(W) ? W : []).filter((I) => h.has(String(I.media_id))).map((I) => String(I.id)), O = s.seededMedia ? [...h] : [], s.identity || (w = []);
503
+ let R = [], S = [];
504
+ if (a.seededMedia || a.identity) {
505
+ const { data: e } = await i.from("media").select("id, object_key"), u = (Array.isArray(e) ? e : []).filter(
506
+ (E) => J.has(String(E.object_key))
507
+ ), b = new Set(u.map((E) => String(E.id))), { data: z } = await i.from("logos").select("id, media_id");
508
+ S = (Array.isArray(z) ? z : []).filter((E) => b.has(String(E.media_id))).map((E) => String(E.id)), R = a.seededMedia ? [...b] : [], a.identity || (S = []);
468
509
  }
469
510
  return {
470
- blocksToClearPageIds: t.clearKeptPageBlocks ? c : [],
511
+ blocksToClearPageIds: t.clearKeptPageBlocks ? m : [],
471
512
  customBlockSlugs: x,
472
- draftsToDelete: z,
473
- identityKeys: s.identity ? ["site_title", "site_description", "site_keywords", "footer_copyright"] : [],
474
- languagesToDeactivate: b,
475
- logosToDelete: w,
476
- mediaToDelete: O,
477
- navigationToDelete: H,
478
- newDefaultLanguage: f,
479
- pagesToDelete: C,
480
- pagesToKeep: E,
481
- postsToDelete: S,
482
- productsToDelete: $
513
+ draftsToDelete: X,
514
+ identityKeys: a.identity ? ["site_title", "site_description", "site_keywords", "footer_copyright"] : [],
515
+ languagesToDeactivate: c,
516
+ logosToDelete: S,
517
+ mediaToDelete: R,
518
+ navigationToDelete: Y,
519
+ newDefaultLanguage: p,
520
+ pagesToDelete: T,
521
+ pagesToKeep: C,
522
+ postsToDelete: w,
523
+ productsToDelete: A
483
524
  };
484
525
  }
485
526
  function be(t) {
@@ -498,194 +539,194 @@ function be(t) {
498
539
  products: t.productsToDelete.length
499
540
  };
500
541
  }
501
- async function Oe(t, i, s) {
502
- for (const n of t.draftsToDelete) {
503
- const { error: o } = await i.from("content_drafts").delete().eq("parent_type", n.parentType).eq("parent_id", n.parentId);
504
- if (o)
505
- throw new Error(`Could not delete a draft: ${g(o)}`);
542
+ async function Le(t, i, a) {
543
+ for (const s of t.draftsToDelete) {
544
+ const { error: n } = await i.from("content_drafts").delete().eq("parent_type", s.parentType).eq("parent_id", s.parentId);
545
+ if (n)
546
+ throw new Error(`Could not delete a draft: ${h(n)}`);
506
547
  }
507
- if (await N(i, "navigation_items", "id", t.navigationToDelete), t.blocksToClearPageIds.length > 0)
508
- for (const n of ce(t.blocksToClearPageIds)) {
509
- const { error: o } = await i.from("blocks").delete().in("page_id", n);
510
- if (o)
511
- throw new Error(`Could not clear the kept pages: ${g(o)}`);
548
+ if (await I(i, "navigation_items", "id", t.navigationToDelete), t.blocksToClearPageIds.length > 0)
549
+ for (const s of ce(t.blocksToClearPageIds)) {
550
+ const { error: n } = await i.from("blocks").delete().in("page_id", s);
551
+ if (n)
552
+ throw new Error(`Could not clear the kept pages: ${h(n)}`);
512
553
  }
513
- if (await N(i, "pages", "id", t.pagesToDelete.map((n) => n.id)), await N(i, "posts", "id", t.postsToDelete.map((n) => n.id)), await N(i, "products", "id", t.productsToDelete), await N(i, "custom_block_definitions", "slug", t.customBlockSlugs), await N(i, "logos", "id", t.logosToDelete), await N(i, "media", "id", t.mediaToDelete), t.identityKeys.length > 0 && (await v(i, "site_title", ""), await v(i, "site_description", ""), await v(i, "site_keywords", ""), await v(
554
+ if (await I(i, "pages", "id", t.pagesToDelete.map((s) => s.id)), await I(i, "posts", "id", t.postsToDelete.map((s) => s.id)), await I(i, "products", "id", t.productsToDelete), await I(i, "custom_block_definitions", "slug", t.customBlockSlugs), await I(i, "logos", "id", t.logosToDelete), await I(i, "media", "id", t.mediaToDelete), t.identityKeys.length > 0 && (await k(i, "site_title", ""), await k(i, "site_description", ""), await k(i, "site_keywords", ""), await k(
514
555
  i,
515
556
  "footer_copyright",
516
- Object.fromEntries(s.map((n) => [n, "© {year}"]))
557
+ Object.fromEntries(a.map((s) => [s, "© {year}"]))
517
558
  )), t.logosToDelete.length > 0) {
518
- const { data: n } = await i.from("site_settings").select("value").eq("key", "active_logo_id").maybeSingle();
519
- typeof n?.value == "string" && t.logosToDelete.includes(n.value) && await v(i, "active_logo_id", null);
559
+ const { data: s } = await i.from("site_settings").select("value").eq("key", "active_logo_id").maybeSingle();
560
+ typeof s?.value == "string" && t.logosToDelete.includes(s.value) && await k(i, "active_logo_id", null);
520
561
  }
521
- for (const n of t.languagesToDeactivate) {
522
- const { error: o } = await i.from("languages").update({ is_active: !1, is_default: !1 }).eq("id", n.id);
523
- if (o)
524
- throw new Error(`Could not deactivate the "${n.code}" language: ${g(o)}`);
562
+ for (const s of t.languagesToDeactivate) {
563
+ const { error: n } = await i.from("languages").update({ is_active: !1, is_default: !1 }).eq("id", s.id);
564
+ if (n)
565
+ throw new Error(`Could not deactivate the "${s.code}" language: ${h(n)}`);
525
566
  }
526
567
  if (t.newDefaultLanguage) {
527
- const { error: n } = await i.from("languages").update({ is_default: !1 }).neq("id", t.newDefaultLanguage.id);
568
+ const { error: s } = await i.from("languages").update({ is_default: !1 }).neq("id", t.newDefaultLanguage.id);
569
+ if (s)
570
+ throw new Error(`Could not change the default language: ${h(s)}`);
571
+ const { error: n } = await i.from("languages").update({ is_active: !0, is_default: !0 }).eq("id", t.newDefaultLanguage.id);
528
572
  if (n)
529
- throw new Error(`Could not change the default language: ${g(n)}`);
530
- const { error: o } = await i.from("languages").update({ is_active: !0, is_default: !0 }).eq("id", t.newDefaultLanguage.id);
531
- if (o)
532
- throw new Error(`Could not change the default language: ${g(o)}`);
573
+ throw new Error(`Could not change the default language: ${h(n)}`);
533
574
  }
534
575
  }
535
576
  function re(t) {
536
- const i = [], s = t.pagesToKeep.some((n) => n.slug === D);
537
- return s && t.blocksToClearPageIds.length > 0 ? i.push('The home page was kept but is now empty: rebuild it with rewrite_page_draft (contentType "page", slug "home") and then publish_content_draft.') : s || i.push(`No "${D}" page remains, so "/" is a 404 until you create_cms_page with slug "${D}" and status "published".`), t.navigationToDelete.length > 0 && i.push('The header and footer menus are empty: rebuild them with update_navigation_bar (mode "replace") and update_footer for every active language.'), t.identityKeys.length > 0 && i.push("Site title, description, keywords, and copyright are blank: set them with update_site_identity."), i.push('The "Published with NextBlock" footer credit is unchanged; set footer_show_attribution to false with update_site_identity if the client wants no NextBlock mention.'), i;
577
+ const i = [], a = t.pagesToKeep.some((s) => s.slug === N);
578
+ return a && t.blocksToClearPageIds.length > 0 ? i.push('The home page was kept but is now empty: rebuild it with rewrite_page_draft (contentType "page", slug "home") and then publish_content_draft.') : a || i.push(`No "${N}" page remains, so "/" is a 404 until you create_cms_page with slug "${N}" and status "published".`), t.navigationToDelete.length > 0 && i.push('The header and footer menus are empty: rebuild them with update_navigation_bar (mode "replace") and update_footer for every active language.'), t.identityKeys.length > 0 && i.push("Site title, description, keywords, and copyright are blank: set them with update_site_identity."), i.push('The "Published with NextBlock" footer credit is unchanged; set footer_show_attribution to false with update_site_identity if the client wants no NextBlock mention.'), i;
538
579
  }
539
580
  async function ye(t, i) {
540
- const s = Z.parse(t);
541
- await ae(i, ["ADMIN"], "Resetting the site content");
542
- const n = L(i), o = await he(s, n), a = be(o), r = a.pages + a.posts + a.products + a.navigationItems + a.customBlocks + a.media + a.logos;
543
- if (s.dryRun)
581
+ const a = Z.parse(t);
582
+ await se(i, ["ADMIN"], "Resetting the site content");
583
+ const s = O(i), n = await he(a, s), o = be(n), r = o.pages + o.posts + o.products + o.navigationItems + o.customBlocks + o.media + o.logos;
584
+ if (a.dryRun)
544
585
  return {
545
586
  dryRun: !0,
546
587
  mutationExecuted: !1,
547
- nextSteps: re(o),
588
+ nextSteps: re(n),
548
589
  success: !0,
549
- summary: a,
590
+ summary: o,
550
591
  wouldDelete: {
551
- pages: o.pagesToDelete,
552
- posts: o.postsToDelete
592
+ pages: n.pagesToDelete,
593
+ posts: n.postsToDelete
553
594
  }
554
595
  };
555
- const u = se({
596
+ const d = ae({
556
597
  action: "RESET SITE CONTENT",
557
598
  context: i,
558
- payload: { input: s, summary: a, tool: "reset_site_content" },
599
+ payload: { input: a, summary: o, tool: "reset_site_content" },
559
600
  preview: {
560
601
  irreversible: !0,
561
- summary: `Permanently delete ${a.pages} page(s), ${a.posts} post(s), ${a.products} product(s), ${a.navigationItems} navigation item(s), ${a.customBlocks} custom block definition(s), ${a.media} demo image(s) and ${a.logos} logo(s)${a.identityCleared ? ", clear the site identity" : ""}${a.languagesDeactivated.length ? `, deactivate ${a.languagesDeactivated.join(", ")}` : ""}. Kept: ${a.pagesKept.join(", ") || "nothing"}. This cannot be undone — take a backup first at /cms/settings/backup-restore if the content matters.`,
562
- ...a
602
+ summary: `Permanently delete ${o.pages} page(s), ${o.posts} post(s), ${o.products} product(s), ${o.navigationItems} navigation item(s), ${o.customBlocks} custom block definition(s), ${o.media} demo image(s) and ${o.logos} logo(s)${o.identityCleared ? ", clear the site identity" : ""}${o.languagesDeactivated.length ? `, deactivate ${o.languagesDeactivated.join(", ")}` : ""}. Kept: ${o.pagesKept.join(", ") || "nothing"}. This cannot be undone — take a backup first at /cms/settings/backup-restore if the content matters.`,
603
+ ...o
563
604
  },
564
605
  subject: `${r} items`
565
606
  });
566
- if (u)
567
- return u;
568
- const p = s.keepLanguages?.map((m) => m.toLowerCase()) ?? [], b = p.length > 0 ? p : await (async () => {
569
- const { data: m } = await n.from("languages").select("code, is_active");
570
- return (Array.isArray(m) ? m : []).filter((f) => f.is_active !== !1).map((f) => String(f.code).toLowerCase());
607
+ if (d)
608
+ return d;
609
+ const f = a.keepLanguages?.map((g) => g.toLowerCase()) ?? [], c = f.length > 0 ? f : await (async () => {
610
+ const { data: g } = await s.from("languages").select("code, is_active");
611
+ return (Array.isArray(g) ? g : []).filter((p) => p.is_active !== !1).map((p) => String(p.code).toLowerCase());
571
612
  })();
572
- return await Oe(o, n, b), ne(i, ["/cms/pages", "/cms/posts", "/cms/navigation", "/cms/media", "/cms/custom-blocks", "/cms/products"]), {
573
- deleted: a,
613
+ return await Le(n, s, c), oe(i, ["/cms/pages", "/cms/posts", "/cms/navigation", "/cms/media", "/cms/custom-blocks", "/cms/products"]), {
614
+ deleted: o,
574
615
  mutationExecuted: !0,
575
- nextSteps: re(o),
616
+ nextSteps: re(n),
576
617
  success: !0
577
618
  };
578
619
  }
579
- const Pe = 60, _e = d.strictObject({
580
- brief: de.optional().describe("The brief as agreed with the client. Merged over the saved brief and marked confirmed."),
581
- durationMinutes: d.number().int().min(15).max(180).default(Pe).describe("How long the unattended build session stays open."),
620
+ const Oe = 60, _e = l.strictObject({
621
+ brief: le.optional().describe("The brief as agreed with the client. Merged over the saved brief and marked confirmed."),
622
+ durationMinutes: l.number().int().min(15).max(180).default(Oe).describe("How long the unattended build session stays open."),
582
623
  reset: Z.optional().describe("Remove the seeded NextBlock demo content as the first step of the build. Omit when the client keeps existing content."),
583
- summary: d.string().trim().min(20).max(4e3).describe("The plan the operator is approving, in plain language: what is removed, every page and its sections, navigation, footer, theme, languages.")
624
+ summary: l.string().trim().min(20).max(4e3).describe("The plan the operator is approving, in plain language: what is removed, every page and its sections, navigation, footer, theme, languages.")
584
625
  });
585
- async function Le(t, i) {
586
- const s = _e.parse(t);
587
- await ae(i, ["ADMIN"], "Starting a site build");
588
- const n = L(i), o = i?.actorUserId;
589
- let a = null, r = null;
590
- s.reset && (r = Z.parse({ ...s.reset, dryRun: !1 }), a = be(await he(r, n)));
591
- const u = se({
626
+ async function Me(t, i) {
627
+ const a = _e.parse(t);
628
+ await se(i, ["ADMIN"], "Starting a site build");
629
+ const s = O(i), n = i?.actorUserId;
630
+ let o = null, r = null;
631
+ a.reset && (r = Z.parse({ ...a.reset, dryRun: !1 }), o = be(await he(r, s)));
632
+ const d = ae({
592
633
  action: "START SITE BUILD",
593
634
  context: i,
594
- payload: { brief: s.brief ?? null, durationMinutes: s.durationMinutes, reset: r, summary: s.summary, tool: "start_site_build" },
635
+ payload: { brief: a.brief ?? null, durationMinutes: a.durationMinutes, reset: r, summary: a.summary, tool: "start_site_build" },
595
636
  preview: {
596
- durationMinutes: s.durationMinutes,
597
- plan: s.summary,
598
- reset: a,
599
- summary: `${s.summary}${a ? ` First, permanently delete ${a.pages} page(s), ${a.posts} post(s), ${a.navigationItems} navigation item(s), ${a.media} demo image(s)${a.identityCleared ? " and the NextBlock site identity" : ""} (kept: ${a.pagesKept.join(", ") || "nothing"}).` : ""} After you confirm, Cortex applies every step of this plan without asking again, for up to ${s.durationMinutes} minutes. You can stop it at any time.`
637
+ durationMinutes: a.durationMinutes,
638
+ plan: a.summary,
639
+ reset: o,
640
+ summary: `${a.summary}${o ? ` First, permanently delete ${o.pages} page(s), ${o.posts} post(s), ${o.navigationItems} navigation item(s), ${o.media} demo image(s)${o.identityCleared ? " and the NextBlock site identity" : ""} (kept: ${o.pagesKept.join(", ") || "nothing"}).` : ""} After you confirm, Cortex applies every step of this plan without asking again, for up to ${a.durationMinutes} minutes. You can stop it at any time.`
600
641
  },
601
642
  subject: "plan"
602
643
  });
603
- if (u)
604
- return u;
605
- let p = null;
606
- s.brief ? p = (await V(
607
- { brief: { ...s.brief, status: "confirmed" }, mode: "merge" },
644
+ if (d)
645
+ return d;
646
+ let f = null;
647
+ a.brief ? f = (await Q(
648
+ { brief: { ...a.brief, status: "confirmed" }, mode: "merge" },
608
649
  { ...i }
609
- )).brief : await oe(n) && (p = (await V({ brief: { status: "confirmed" }, mode: "merge" }, { ...i })).brief);
610
- let b = null;
611
- r && (b = await ye(r, { ...i, skipConfirmation: !0 }));
612
- const m = /* @__PURE__ */ new Date(), f = {
613
- actorUserId: o,
614
- createdAt: m.toISOString(),
615
- expiresAt: new Date(m.getTime() + s.durationMinutes * 6e4).toISOString(),
650
+ )).brief : await ne(s) && (f = (await Q({ brief: { status: "confirmed" }, mode: "merge" }, { ...i })).brief);
651
+ let c = null;
652
+ r && (c = await ye(r, { ...i, skipConfirmation: !0 }));
653
+ const g = /* @__PURE__ */ new Date(), p = {
654
+ actorUserId: n,
655
+ createdAt: g.toISOString(),
656
+ expiresAt: new Date(g.getTime() + a.durationMinutes * 6e4).toISOString(),
616
657
  id: xe(),
617
- summary: s.summary
658
+ summary: a.summary
618
659
  };
619
- return await v(n, G, f), {
620
- brief: p,
621
- buildSession: { expiresAt: f.expiresAt, id: f.id },
660
+ return await k(s, G, p), {
661
+ brief: f,
662
+ buildSession: { expiresAt: p.expiresAt, id: p.id },
622
663
  continuePrompt: "The build session is active and the plan is approved. Continue the site build now, step by step, without asking for confirmation: identity, languages, theme, pages (rewrite home, create the others), publish every draft, navigation, footer, translations, then call finish_site_build and summarize what was built with links.",
623
664
  mutationExecuted: !0,
624
- resetResult: b,
665
+ resetResult: c,
625
666
  success: !0
626
667
  };
627
668
  }
628
- const we = d.strictObject({
629
- outcome: d.enum(["built", "stopped"]).default("built").describe('"built" when the plan was completed; "stopped" when the operator interrupted it.'),
630
- summary: d.string().trim().max(4e3).optional().describe("What was built, for the record.")
669
+ const Se = l.strictObject({
670
+ outcome: l.enum(["built", "stopped"]).default("built").describe('"built" when the plan was completed; "stopped" when the operator interrupted it.'),
671
+ summary: l.string().trim().max(4e3).optional().describe("What was built, for the record.")
631
672
  });
632
- async function Me(t, i) {
633
- const s = we.parse(t), n = L(i), o = await pe(n);
634
- await De(n);
635
- let a = null;
636
- if (s.outcome === "built") {
637
- const r = await oe(n);
638
- r && (a = (await V(
639
- { brief: { status: "built", ...s.summary ? { notes: `${r.notes ? `${r.notes}
640
- ` : ""}Built: ${s.summary}` } : {} }, mode: "merge" },
673
+ async function je(t, i) {
674
+ const a = Se.parse(t), s = O(i), n = await pe(s);
675
+ await Ne(s);
676
+ let o = null;
677
+ if (a.outcome === "built") {
678
+ const r = await ne(s);
679
+ r && (o = (await Q(
680
+ { brief: { status: "built", ...a.summary ? { notes: `${r.notes ? `${r.notes}
681
+ ` : ""}Built: ${a.summary}` } : {} }, mode: "merge" },
641
682
  { ...i }
642
683
  )).brief);
643
684
  }
644
- return ne(i, ["/cms/dashboard"]), {
645
- brief: a,
685
+ return oe(i, ["/cms/dashboard"]), {
686
+ brief: o,
646
687
  buildSessionEnded: !0,
647
- hadSession: !!o,
688
+ hadSession: !!n,
648
689
  mutationExecuted: !0,
649
- outcome: s.outcome,
690
+ outcome: a.outcome,
650
691
  success: !0
651
692
  };
652
693
  }
653
- function Ye(t) {
694
+ function We(t) {
654
695
  return {
655
- get_site_overview: P({
696
+ get_site_overview: L({
656
697
  description: 'Read the WHOLE site in one call: languages, site identity (title, description, copyright, active logo), every page, post, and product with status and language, the header and footer menus per language, themes, custom block definitions, media counts, pending Live Drafts, the saved site brief, and whether NextBlock demo content is still present. Read-only. Call this FIRST for any request about the site as a whole ("build my site", "update the whole website", "what do I have?") instead of several narrower reads.',
657
- execute: (i) => Ae(i, t),
698
+ execute: (i) => De(i, t),
658
699
  inputSchema: me,
659
700
  strict: !0
660
701
  }),
661
- update_site_identity: P({
702
+ update_site_identity: L({
662
703
  description: 'Set the site-wide identity: site_title (brand name in the header and <title>), site_description, site_keywords, footer_copyright per locale ("{year}" is substituted), footer_show_attribution (the "Published with NextBlock" credit), and the pinned active_logo_id. Use it right after a reset and whenever the client renames or re-describes their business. Mutating: first returns a confirmation phrase; only executes after exact confirmation.',
663
- execute: (i) => Be(i, t),
704
+ execute: (i) => Pe(i, t),
664
705
  inputSchema: fe,
665
706
  strict: !0
666
707
  }),
667
- save_site_brief: P({
708
+ save_site_brief: L({
668
709
  description: 'Record what the client wants their website to be — business, audience, goals, site type (a one-page landing site is fine), pages and their sections, languages, brand colours and tone, contact details, whether existing content is kept. Saved site-wide and shown to you at the start of every later conversation, so save it as soon as you learn something (mode "merge" adds to what is saved). Runs immediately, no confirmation.',
669
- execute: (i) => V(i, t),
710
+ execute: (i) => Q(i, t),
670
711
  inputSchema: ge,
671
712
  strict: !0
672
713
  }),
673
- reset_site_content: P({
714
+ reset_site_content: L({
674
715
  description: 'Remove the NextBlock demo content that ships with a fresh install — or, by scope, everything: pages (all languages, except keepPageSlugs, default ["home"] which is kept but emptied), posts, navigation, the bundled demo images and logo, the default site title and copyright; optionally products and custom block definitions. keepLanguages deactivates every other language and removes its content, so a client who only wants English gets an English-only site. Pass dryRun true to see the counts first. ADMIN only, irreversible; take a backup at /cms/settings/backup-restore if in doubt. Mutating: first returns a confirmation phrase; only executes after exact confirmation. Prefer start_site_build with `reset` when you are about to rebuild the site, so one confirmation covers both.',
675
716
  execute: (i) => ye(i, t),
676
717
  inputSchema: Z,
677
718
  strict: !0
678
719
  }),
679
- start_site_build: P({
720
+ start_site_build: L({
680
721
  description: "Approve a whole-site plan with ONE confirmation. Pass the plain-language plan in `summary`, the agreed `brief`, and (usually) `reset` so the demo content is removed first. On confirmation it saves the brief, runs the reset, and opens a time-boxed build session: until it expires, every later tool call in this chat executes without asking for confirmation, so you can create pages, publish drafts, set navigation, footer, theme, and translations in one unattended run. Call it only after the client has seen the plan and agreed. After it succeeds, keep working through the plan immediately and finish with finish_site_build. ADMIN only.",
681
- execute: (i) => Le(i, t),
722
+ execute: (i) => Me(i, t),
682
723
  inputSchema: _e,
683
724
  strict: !0
684
725
  }),
685
- finish_site_build: P({
726
+ finish_site_build: L({
686
727
  description: 'Close the current build session (opened by start_site_build) and mark the brief as built. Call it as the LAST step of a site build, or with outcome "stopped" if the operator interrupted. Runs immediately, no confirmation.',
687
- execute: (i) => Me(i, t),
688
- inputSchema: we,
728
+ execute: (i) => je(i, t),
729
+ inputSchema: Se,
689
730
  strict: !0
690
731
  })
691
732
  };
@@ -693,19 +734,20 @@ function Ye(t) {
693
734
  export {
694
735
  J as NEXTBLOCK_SEED_MEDIA_OBJECT_KEYS,
695
736
  te as NEXTBLOCK_SEED_TRANSLATION_GROUP_IDS,
696
- Ye as createCortexSiteTools,
697
- Me as executeFinishSiteBuild,
698
- Ae as executeGetSiteOverview,
737
+ We as createCortexSiteTools,
738
+ je as executeFinishSiteBuild,
739
+ De as executeGetSiteOverview,
699
740
  ye as executeResetSiteContent,
700
- V as executeSaveSiteBrief,
701
- Le as executeStartSiteBuild,
702
- Be as executeUpdateSiteIdentity,
703
- we as finishSiteBuildInputSchema,
741
+ Q as executeSaveSiteBrief,
742
+ Me as executeStartSiteBuild,
743
+ Pe as executeUpdateSiteIdentity,
744
+ Se as finishSiteBuildInputSchema,
745
+ Ye as formatCortexSiteOverviewForPrompt,
704
746
  me as getSiteOverviewInputSchema,
705
747
  pe as readCortexBuildSession,
706
- oe as readCortexSiteBrief,
748
+ ne as readCortexSiteBrief,
707
749
  Z as resetSiteContentInputSchema,
708
- Fe as resolveCortexBuildSession,
750
+ He as resolveCortexBuildSession,
709
751
  ge as saveSiteBriefInputSchema,
710
752
  _e as startSiteBuildInputSchema,
711
753
  fe as updateSiteIdentityInputSchema