magic-editor-x 1.6.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/CHANGELOG.md +268 -0
  2. package/LICENSE +1 -7
  3. package/README.md +324 -981
  4. package/dist/admin/ConfigurationPage-B1uCIE3J.js +767 -0
  5. package/dist/admin/ConfigurationPage-DvAifELO.mjs +767 -0
  6. package/dist/admin/CustomBlocksPage-6-OQwpXQ.mjs +1741 -0
  7. package/dist/admin/CustomBlocksPage-6eWCOreU.js +1743 -0
  8. package/dist/admin/EditorField-DocyD2Q3.js +5506 -0
  9. package/dist/admin/EditorField-ThSoOCPm.mjs +5471 -0
  10. package/dist/admin/api-B-RsxJCs.js +34 -0
  11. package/dist/admin/api-C4XsDZxd.mjs +35 -0
  12. package/dist/admin/de-B8XFFhHh.js +95 -0
  13. package/dist/admin/de-BMxADn2Z.mjs +231 -0
  14. package/dist/admin/de-DBR893G4.js +13 -0
  15. package/dist/admin/de-DcDPRzV4.js +155 -0
  16. package/dist/admin/de-Dx7vUW0Z.js +231 -0
  17. package/dist/admin/de-RAxzvrwm.mjs +95 -0
  18. package/dist/admin/de-fBFh09Wl.mjs +13 -0
  19. package/dist/admin/de-n5V6c5RT.mjs +155 -0
  20. package/dist/admin/en-BQ5Pxe2o.mjs +231 -0
  21. package/dist/admin/en-BqNm3nMQ.mjs +155 -0
  22. package/dist/admin/en-BzfyifEU.mjs +95 -0
  23. package/dist/admin/en-D-DGftl0.js +155 -0
  24. package/dist/admin/en-D9Nfccux.mjs +13 -0
  25. package/dist/admin/en-DcWUTE2J.js +95 -0
  26. package/dist/admin/en-gueRT44P.js +13 -0
  27. package/dist/admin/en-sNn4z9lA.js +231 -0
  28. package/dist/admin/es-B0Zy8DVg.mjs +13 -0
  29. package/dist/admin/es-B6NYq0VH.mjs +231 -0
  30. package/dist/admin/es-BOtVc9qv.js +95 -0
  31. package/dist/admin/es-C2OJVYkV.mjs +95 -0
  32. package/dist/admin/es-DRGs7meF.js +13 -0
  33. package/dist/admin/es-DVsXnS0N.js +231 -0
  34. package/dist/admin/es-Di0pyQCh.js +155 -0
  35. package/dist/admin/es-SzeTV4q8.mjs +155 -0
  36. package/dist/admin/fr-B8rdLAIL.js +13 -0
  37. package/dist/admin/fr-Bpo5Uq1f.mjs +95 -0
  38. package/dist/admin/fr-D6F--jIB.js +155 -0
  39. package/dist/admin/fr-DfxKaOc_.js +95 -0
  40. package/dist/admin/fr-DhgPYHIH.mjs +155 -0
  41. package/dist/admin/fr-DmG3_Cn-.mjs +231 -0
  42. package/dist/admin/fr-Fwxcj4SM.js +231 -0
  43. package/dist/admin/fr-oiUb2bb_.mjs +13 -0
  44. package/dist/admin/index-CwfaB5-e.js +155 -0
  45. package/dist/admin/index-D1Q5Rlt6.mjs +156 -0
  46. package/dist/admin/index.js +1 -1
  47. package/dist/admin/index.mjs +2 -2
  48. package/dist/admin/pt-B3Jce6uT.mjs +13 -0
  49. package/dist/admin/pt-BPo0ex21.js +155 -0
  50. package/dist/admin/pt-BbdwHlWF.mjs +231 -0
  51. package/dist/admin/pt-BiWuSBcH.js +13 -0
  52. package/dist/admin/pt-BvSl1btT.mjs +155 -0
  53. package/dist/admin/pt-C6nr71Yw.js +231 -0
  54. package/dist/admin/pt-DqCXuI4S.mjs +95 -0
  55. package/dist/admin/pt-LCvjwk0y.js +95 -0
  56. package/dist/admin/sanitize-9UVcXFfc.js +552 -0
  57. package/dist/admin/sanitize-D4kfZ_ly.mjs +551 -0
  58. package/dist/admin/useT-B5M49wGH.js +130 -0
  59. package/dist/admin/useT-CjX6fL2m.mjs +131 -0
  60. package/dist/server/index.js +4150 -64739
  61. package/dist/server/index.mjs +4150 -64748
  62. package/package.json +90 -66
  63. package/strapi-admin.js +3 -0
  64. package/strapi-server.js +3 -0
  65. package/COPYRIGHT_NOTICE.txt +0 -20
  66. package/dist/admin/App-BwMEZnS-.mjs +0 -2144
  67. package/dist/admin/App-HNr2tizR.js +0 -2147
  68. package/dist/admin/CustomBlocksPage-BjcA7MGz.js +0 -1519
  69. package/dist/admin/CustomBlocksPage-COPOZ2-_.mjs +0 -1517
  70. package/dist/admin/LicensePage-D0DMTF41.mjs +0 -392
  71. package/dist/admin/LicensePage-D0_9yPkw.js +0 -394
  72. package/dist/admin/LiveCollaborationPanel-CZAbgAVr.js +0 -223
  73. package/dist/admin/LiveCollaborationPanel-D8z72afw.mjs +0 -221
  74. package/dist/admin/Settings-DRenvRcG.js +0 -441
  75. package/dist/admin/Settings-DjyNURlS.mjs +0 -439
  76. package/dist/admin/de-C_0Mj-Zo.mjs +0 -296
  77. package/dist/admin/de-DVNVpAt9.js +0 -296
  78. package/dist/admin/en-B7AAf1ie.mjs +0 -296
  79. package/dist/admin/en-C2hv5GsA.js +0 -296
  80. package/dist/admin/es-BzJqcIST.js +0 -274
  81. package/dist/admin/es-zdP8sd-f.mjs +0 -274
  82. package/dist/admin/fr-CtAgOgH1.mjs +0 -274
  83. package/dist/admin/fr-D3GwqAAJ.js +0 -274
  84. package/dist/admin/getTranslation-2OiEgVGt.mjs +0 -49459
  85. package/dist/admin/getTranslation-DQ_WJ-7M.js +0 -49475
  86. package/dist/admin/index-Bn2tfJrC.js +0 -2186
  87. package/dist/admin/index-DAtClhqy.js +0 -6956
  88. package/dist/admin/index-FVEbTfqO.mjs +0 -2187
  89. package/dist/admin/index-gjWl-6E2.mjs +0 -6934
  90. package/dist/admin/magic-editor-x.css +0 -1103
  91. package/dist/admin/pt-BmUw6YMP.mjs +0 -274
  92. package/dist/admin/pt-CFo0nTbj.js +0 -274
  93. package/dist/admin/tools-CzHTZaY_.mjs +0 -2268
  94. package/dist/admin/tools-DALFhvPO.js +0 -2299
  95. package/pics/collab-magiceditorX.png +0 -0
  96. package/pics/editorX.png +0 -0
  97. package/pics/liveCollabwidget1.png +0 -0
@@ -0,0 +1,767 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("react/jsx-runtime");
4
+ const designSystem = require("@strapi/design-system");
5
+ const admin = require("@strapi/strapi/admin");
6
+ const icons = require("@strapi/icons");
7
+ const index = require("./index-CwfaB5-e.js");
8
+ const useT = require("./useT-B5M49wGH.js");
9
+ const react = require("react");
10
+ require("styled-components");
11
+ const api = require("./api-B-RsxJCs.js");
12
+ const reactIntl = require("react-intl");
13
+ const DraftSync = ({ values }) => {
14
+ const setValues = admin.useForm("MagicEditorXDraftSync", (state) => state.setValues);
15
+ react.useEffect(() => {
16
+ setValues(values);
17
+ }, [values, setValues]);
18
+ return null;
19
+ };
20
+ const PropertyList = ({ items }) => /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { tag: "dl", margin: 0, children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 3, children: items.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(
21
+ designSystem.Flex,
22
+ {
23
+ gap: { initial: 1, medium: 4 },
24
+ direction: { initial: "column", medium: "row" },
25
+ alignItems: { initial: "flex-start", medium: "baseline" },
26
+ minWidth: 0,
27
+ children: [
28
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { tag: "dt", minWidth: 0, flex: { initial: "0 0 auto", medium: "0 0 26rem" }, children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "sigma", textColor: "neutral600", children: item.label }) }),
29
+ /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Box, { tag: "dd", margin: 0, minWidth: 0, flex: "1 1 auto", children: [
30
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "omega", textColor: "neutral800", children: item.value }),
31
+ item.hint ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { paddingTop: 1, children: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "pi", textColor: "neutral600", children: item.hint }) }) : null
32
+ ] })
33
+ ]
34
+ },
35
+ item.key
36
+ )) }) });
37
+ const PROVIDERS = Object.freeze(["anthropic", "openai", "openai-compatible"]);
38
+ const MODEL_PATTERN = /^[A-Za-z0-9._:/@-]+$/;
39
+ const LIMITS = Object.freeze({
40
+ maxInputChars: Object.freeze({ min: 200, max: 5e4 }),
41
+ maxOutputTokens: Object.freeze({ min: 64, max: 16e3 })
42
+ });
43
+ const EDITABLE = ["enabled", "provider", "baseUrl", "model", "maxInputChars", "maxOutputTokens"];
44
+ const toDraft = (ai) => ({
45
+ enabled: Boolean(ai.enabled),
46
+ provider: PROVIDERS.includes(ai.provider) ? ai.provider : "anthropic",
47
+ baseUrl: ai.baseUrl || "",
48
+ model: ai.model || "",
49
+ maxInputChars: ai.maxInputChars ?? 8e3,
50
+ maxOutputTokens: ai.maxOutputTokens ?? 4096,
51
+ apiKeyMode: "keep",
52
+ apiKey: ""
53
+ });
54
+ const applyProvider = (draft, provider, defaultModels = {}) => {
55
+ const previousDefault = defaultModels[draft.provider] || "";
56
+ const model = !draft.model || draft.model === previousDefault ? defaultModels[provider] || "" : draft.model;
57
+ return { ...draft, provider, model };
58
+ };
59
+ const endpointChanged = (draft, saved) => draft.provider !== saved.provider || draft.baseUrl !== saved.baseUrl;
60
+ const validateDraft = (draft, { saved, ai, environment }) => {
61
+ const errors = {};
62
+ if (!PROVIDERS.includes(draft.provider)) errors.provider = "required";
63
+ const model = String(draft.model || "").trim();
64
+ if (!model) errors.model = "required";
65
+ else if (model.length > 120 || !MODEL_PATTERN.test(model)) errors.model = "modelPattern";
66
+ const baseUrl = String(draft.baseUrl || "").trim();
67
+ if (!baseUrl && draft.provider === "openai-compatible") errors.baseUrl = "required";
68
+ if (baseUrl) {
69
+ const url = (() => {
70
+ try {
71
+ return new URL(baseUrl);
72
+ } catch {
73
+ return null;
74
+ }
75
+ })();
76
+ if (!url || !["http:", "https:"].includes(url.protocol) || url.username || url.password || baseUrl.length > 500) {
77
+ errors.baseUrl = "url";
78
+ } else if (url.protocol !== "https:" && !(environment && environment.allowPrivateBaseUrl)) {
79
+ errors.baseUrl = "https";
80
+ }
81
+ }
82
+ for (const key of ["maxInputChars", "maxOutputTokens"]) {
83
+ const value = draft[key];
84
+ const { min, max } = LIMITS[key];
85
+ if (value === void 0 || value === null || value === "") errors[key] = "required";
86
+ else if (!Number.isInteger(value) || value < min || value > max) errors[key] = "range";
87
+ }
88
+ if (draft.apiKeyMode === "replace") {
89
+ const key = String(draft.apiKey || "").trim();
90
+ if (!key) errors.apiKey = "required";
91
+ else if (key.length > 500) errors.apiKey = "tooLong";
92
+ else if (ai && !ai.canStoreApiKey) errors.apiKey = "cannotStore";
93
+ } else if (draft.apiKeyMode === "keep" && saved && endpointChanged(draft, saved) && ai && ai.hasApiKey && !ai.apiKeyFromEnvironment) {
94
+ errors.apiKey = "reenter";
95
+ }
96
+ return errors;
97
+ };
98
+ const toPayload = (draft) => {
99
+ const ai = {
100
+ enabled: Boolean(draft.enabled),
101
+ provider: draft.provider,
102
+ baseUrl: String(draft.baseUrl || "").trim(),
103
+ model: String(draft.model || "").trim(),
104
+ maxInputChars: draft.maxInputChars,
105
+ maxOutputTokens: draft.maxOutputTokens
106
+ };
107
+ if (draft.apiKeyMode === "remove") ai.apiKey = "";
108
+ if (draft.apiKeyMode === "replace") ai.apiKey = String(draft.apiKey || "").trim();
109
+ return { ai };
110
+ };
111
+ const adjustedFields = (draft, stored) => EDITABLE.filter((key) => {
112
+ const sent = key === "baseUrl" || key === "model" ? String(draft[key] || "").trim() : draft[key];
113
+ return sent !== stored[key];
114
+ });
115
+ const mapServerErrors = (details) => {
116
+ const out = {};
117
+ const fieldErrors = details && typeof details === "object" ? details.fieldErrors || {} : {};
118
+ for (const [key, messages] of Object.entries(fieldErrors)) {
119
+ const message = Array.isArray(messages) ? messages[0] : String(messages || "");
120
+ const field = key.startsWith("ai.") ? key.slice(3) : null;
121
+ if (field && EDITABLE.concat(["apiKey"]).includes(field)) out[field] = message;
122
+ else out._form = message;
123
+ }
124
+ if (!Object.keys(out).length && details && Array.isArray(details.formErrors) && details.formErrors[0]) {
125
+ out._form = details.formErrors[0];
126
+ }
127
+ return out;
128
+ };
129
+ const sameDraft = (a, b) => EDITABLE.every((key) => a[key] === b[key]) && a.apiKeyMode === b.apiKeyMode && (a.apiKeyMode !== "replace" || a.apiKey === b.apiKey);
130
+ const useConfigurationForm = () => {
131
+ const { get, put, post } = admin.useFetchClient();
132
+ const [status, setStatus] = react.useState("loading");
133
+ const [dto, setDto] = react.useState(null);
134
+ const [saved, setSaved] = react.useState(null);
135
+ const [draft, setDraft] = react.useState(null);
136
+ const [serverErrors, setServerErrors] = react.useState({});
137
+ const [showErrors, setShowErrors] = react.useState(false);
138
+ const [saving, setSaving] = react.useState(false);
139
+ const [testing, setTesting] = react.useState(false);
140
+ const mounted = react.useRef(true);
141
+ react.useEffect(() => {
142
+ mounted.current = true;
143
+ return () => {
144
+ mounted.current = false;
145
+ };
146
+ }, []);
147
+ const fetchSettings = react.useCallback(
148
+ () => get(api.API.settings).then(
149
+ ({ data }) => {
150
+ if (!mounted.current) return;
151
+ const next = toDraft(data.data.ai);
152
+ setDto(data.data);
153
+ setSaved(next);
154
+ setDraft(next);
155
+ setServerErrors({});
156
+ setShowErrors(false);
157
+ setStatus("ready");
158
+ },
159
+ () => {
160
+ if (mounted.current) setStatus("error");
161
+ }
162
+ ),
163
+ [get]
164
+ );
165
+ react.useEffect(() => {
166
+ fetchSettings();
167
+ }, [fetchSettings]);
168
+ const load = react.useCallback(() => {
169
+ setStatus("loading");
170
+ fetchSettings();
171
+ }, [fetchSettings]);
172
+ const errors = react.useMemo(() => {
173
+ if (!draft || !dto) return {};
174
+ return validateDraft(draft, { saved, ai: dto.ai, environment: dto.environment });
175
+ }, [draft, dto, saved]);
176
+ const setField = react.useCallback((key, value) => {
177
+ setDraft((current) => ({ ...current, [key]: value }));
178
+ setServerErrors((current) => {
179
+ if (!current[key] && !current._form) return current;
180
+ const next = { ...current };
181
+ delete next[key];
182
+ delete next._form;
183
+ return next;
184
+ });
185
+ }, []);
186
+ const replaceDraft = react.useCallback((next) => setDraft(next), []);
187
+ const dirty = Boolean(draft && saved && !sameDraft(draft, saved));
188
+ const hasErrors = Object.keys(errors).length > 0;
189
+ const save = react.useCallback(async () => {
190
+ setShowErrors(true);
191
+ if (hasErrors) return { ok: false, invalid: true };
192
+ setSaving(true);
193
+ try {
194
+ const { data } = await put(api.API.settings, toPayload(draft));
195
+ const stored = toDraft(data.data.ai);
196
+ const adjusted = adjustedFields(draft, stored);
197
+ setDto(data.data);
198
+ setSaved(stored);
199
+ setDraft(stored);
200
+ setServerErrors({});
201
+ setShowErrors(false);
202
+ return { ok: true, adjusted };
203
+ } catch (error) {
204
+ const info = api.readApiError(error);
205
+ setServerErrors(mapServerErrors(info.details));
206
+ return { ok: false, message: info.status === 400 ? null : info.message };
207
+ } finally {
208
+ if (mounted.current) setSaving(false);
209
+ }
210
+ }, [draft, hasErrors, put]);
211
+ const clearServerErrors = react.useCallback(() => setServerErrors({}), []);
212
+ const discard = react.useCallback(() => {
213
+ setDraft(saved);
214
+ setServerErrors({});
215
+ setShowErrors(false);
216
+ }, [saved]);
217
+ const test = react.useCallback(async () => {
218
+ setShowErrors(true);
219
+ const blocking = Object.keys(errors).filter((key) => key !== "apiKey" || draft.apiKeyMode === "replace");
220
+ if (blocking.length) return { ok: false, invalid: true };
221
+ setTesting(true);
222
+ try {
223
+ const payload = toPayload(draft);
224
+ if (draft.apiKeyMode === "remove") delete payload.ai.apiKey;
225
+ await post(api.API.settingsAiTest, payload);
226
+ return { ok: true };
227
+ } catch (error) {
228
+ const info = api.readApiError(error);
229
+ return { ok: false, code: info.code, status: info.status };
230
+ } finally {
231
+ if (mounted.current) setTesting(false);
232
+ }
233
+ }, [draft, errors, post]);
234
+ const visibleErrors = react.useMemo(() => {
235
+ const merged = showErrors ? { ...errors } : {};
236
+ for (const [key, value] of Object.entries(serverErrors)) merged[key] = { server: value };
237
+ return merged;
238
+ }, [errors, serverErrors, showErrors]);
239
+ return {
240
+ status,
241
+ dto,
242
+ saved,
243
+ draft,
244
+ errors: visibleErrors,
245
+ dirty,
246
+ hasErrors,
247
+ saving,
248
+ testing,
249
+ reload: load,
250
+ setField,
251
+ replaceDraft,
252
+ save,
253
+ discard,
254
+ test,
255
+ clearServerErrors
256
+ };
257
+ };
258
+ const errorText = (t, error, field) => {
259
+ if (!error) return void 0;
260
+ if (typeof error === "object") return error.server;
261
+ switch (error) {
262
+ case "required":
263
+ return t("settings.validation.required", "This field is required.");
264
+ case "modelPattern":
265
+ return t(
266
+ "settings.validation.model",
267
+ "Use letters, digits and the characters . _ : / @ - (up to 120 characters)."
268
+ );
269
+ case "url":
270
+ return t("settings.validation.url", "Enter a valid http or https URL without user name or password.");
271
+ case "https":
272
+ return t(
273
+ "settings.validation.https",
274
+ "Use an https URL. Private or http hosts need ai.allowPrivateBaseUrl in config/plugins.js."
275
+ );
276
+ case "range":
277
+ return t("settings.validation.range", "Enter a whole number from {min} to {max}.", LIMITS[field] || {});
278
+ case "reenter":
279
+ return t(
280
+ "settings.validation.reenterKey",
281
+ "Enter the API key again when you change the provider or base URL."
282
+ );
283
+ case "cannotStore":
284
+ return t("settings.validation.cannotStore", "Set MAGIC_EDITOR_X_ENCRYPTION_KEY to store an API key.");
285
+ case "tooLong":
286
+ return t("settings.validation.keyTooLong", "The API key is too long.");
287
+ default:
288
+ return t("settings.validation.invalid", "Check this value.");
289
+ }
290
+ };
291
+ const providerName = (t, draft) => {
292
+ if (draft.provider === "anthropic") return t("settings.ai.provider.anthropic.name", "Anthropic");
293
+ if (draft.provider === "openai") return t("settings.ai.provider.openai.name", "OpenAI");
294
+ const host = (() => {
295
+ try {
296
+ return draft.baseUrl ? new URL(draft.baseUrl).host : "";
297
+ } catch {
298
+ return "";
299
+ }
300
+ })();
301
+ return host || t("settings.ai.provider.compatible.name", "the server at the base URL");
302
+ };
303
+ const KeyStatus = ({ ai, draft, t }) => {
304
+ let tone = { backgroundColor: "neutral150", textColor: "neutral700" };
305
+ let label = t("settings.ai.key.status.none", "No key stored");
306
+ let text = "";
307
+ if (ai.apiKeyFromEnvironment) {
308
+ tone = { backgroundColor: "success100", textColor: "success700" };
309
+ label = t("settings.ai.key.status.environment", "From the environment");
310
+ text = t(
311
+ "settings.ai.key.environmentHint",
312
+ "The ai.apiKey setting in config/plugins.js is used and overrides a stored key."
313
+ );
314
+ } else if (draft.apiKeyMode === "remove") {
315
+ tone = { backgroundColor: "warning200", textColor: "neutral800" };
316
+ label = t("settings.ai.key.status.removing", "Removed when you save");
317
+ } else if (ai.apiKeyUnreadable) {
318
+ tone = { backgroundColor: "warning200", textColor: "neutral800" };
319
+ label = t("settings.ai.key.status.unreadable", "Stored key unreadable");
320
+ text = t(
321
+ "settings.ai.key.unreadableHint",
322
+ "A key is stored but cannot be decrypted, usually because MAGIC_EDITOR_X_ENCRYPTION_KEY changed. Enter the key again."
323
+ );
324
+ } else if (ai.hasApiKey) {
325
+ tone = { backgroundColor: "success100", textColor: "success700" };
326
+ label = t("settings.ai.key.status.stored", "Stored, encrypted");
327
+ }
328
+ return /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "flex-start", gap: 1, children: [
329
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Badge, { ...tone, children: label }),
330
+ text ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "pi", textColor: "neutral600", children: text }) : null
331
+ ] });
332
+ };
333
+ const AiSection = ({ form, readOnly, onTest }) => {
334
+ const t = useT.useT();
335
+ const { draft, dto, errors, setField, replaceDraft, testing } = form;
336
+ const ai = dto.ai;
337
+ const defaults = dto.environment.defaults || { models: {}, baseUrls: {} };
338
+ const keyDisabled = readOnly || ai.apiKeyFromEnvironment || !ai.canStoreApiKey;
339
+ return /* @__PURE__ */ jsxRuntime.jsxs(
340
+ useT.SectionCard,
341
+ {
342
+ title: t("settings.ai.title", "AI assistant"),
343
+ subtitle: t(
344
+ "settings.ai.subtitle",
345
+ "Editors can improve, shorten, translate or continue selected text with your own AI provider. It is off until you turn it on."
346
+ ),
347
+ action: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Button, { variant: "secondary", onClick: onTest, loading: testing, disabled: testing, children: t("settings.ai.test", "Test connection") }),
348
+ children: [
349
+ /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { gap: 4, alignItems: "flex-start", justifyContent: "space-between", minWidth: 0, children: [
350
+ /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "flex-start", gap: 1, minWidth: 0, flex: "1 1 auto", children: [
351
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "omega", fontWeight: "bold", textColor: "neutral800", id: "mex-ai-enabled-label", children: t("settings.ai.enabled.label", "Turn on the AI assistant") }),
352
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "pi", textColor: "neutral600", id: "mex-ai-enabled-hint", children: t(
353
+ "settings.ai.enabled.privacy",
354
+ "When an editor runs an AI action, the selected text is sent to {provider}. Nothing is sent while the assistant is off.",
355
+ { provider: providerName(t, draft) }
356
+ ) })
357
+ ] }),
358
+ /* @__PURE__ */ jsxRuntime.jsx(
359
+ designSystem.Switch,
360
+ {
361
+ checked: draft.enabled,
362
+ onCheckedChange: (checked) => setField("enabled", checked === true),
363
+ disabled: readOnly,
364
+ "aria-labelledby": "mex-ai-enabled-label",
365
+ "aria-describedby": "mex-ai-enabled-hint",
366
+ onLabel: t("settings.switch.on", "On"),
367
+ offLabel: t("settings.switch.off", "Off"),
368
+ visibleLabels: true
369
+ }
370
+ )
371
+ ] }),
372
+ /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Grid.Root, { gap: { initial: 4, medium: 5 }, children: [
373
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Grid.Item, { col: 6, xs: 12, s: 12, m: 12, direction: "column", alignItems: "stretch", minWidth: 0, children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Field.Root, { name: "provider", error: errorText(t, errors.provider, "provider"), required: true, children: [
374
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: t("settings.ai.provider.label", "Provider") }),
375
+ /* @__PURE__ */ jsxRuntime.jsxs(
376
+ designSystem.SingleSelect,
377
+ {
378
+ value: draft.provider,
379
+ disabled: readOnly,
380
+ onChange: (value) => replaceDraft(applyProvider(draft, String(value), defaults.models)),
381
+ children: [
382
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.SingleSelectOption, { value: "anthropic", children: t("settings.ai.provider.anthropic", "Anthropic (Messages API)") }),
383
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.SingleSelectOption, { value: "openai", children: t("settings.ai.provider.openai", "OpenAI (Chat Completions)") }),
384
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.SingleSelectOption, { value: "openai-compatible", children: t("settings.ai.provider.compatible", "OpenAI-compatible server") })
385
+ ]
386
+ }
387
+ ),
388
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
389
+ ] }) }),
390
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Grid.Item, { col: 6, xs: 12, s: 12, m: 12, direction: "column", alignItems: "stretch", minWidth: 0, children: /* @__PURE__ */ jsxRuntime.jsxs(
391
+ designSystem.Field.Root,
392
+ {
393
+ name: "model",
394
+ error: errorText(t, errors.model, "model"),
395
+ hint: defaults.models[draft.provider] ? t("settings.ai.model.hint", "Default: {model}", { model: defaults.models[draft.provider] }) : t("settings.ai.model.hintCompatible", "Use the model name your server expects."),
396
+ required: true,
397
+ children: [
398
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: t("settings.ai.model.label", "Model") }),
399
+ /* @__PURE__ */ jsxRuntime.jsx(
400
+ designSystem.TextInput,
401
+ {
402
+ value: draft.model,
403
+ disabled: readOnly,
404
+ onChange: (event) => setField("model", event.target.value)
405
+ }
406
+ ),
407
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, {}),
408
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
409
+ ]
410
+ }
411
+ ) }),
412
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Grid.Item, { col: 12, direction: "column", alignItems: "stretch", minWidth: 0, children: /* @__PURE__ */ jsxRuntime.jsxs(
413
+ designSystem.Field.Root,
414
+ {
415
+ name: "baseUrl",
416
+ error: errorText(t, errors.baseUrl, "baseUrl"),
417
+ hint: defaults.baseUrls[draft.provider] ? t("settings.ai.baseUrl.hint", "Leave empty to use {url}.", {
418
+ url: defaults.baseUrls[draft.provider]
419
+ }) : t(
420
+ "settings.ai.baseUrl.hintCompatible",
421
+ "Required. Requests go to the base URL followed by /v1/chat/completions."
422
+ ),
423
+ required: draft.provider === "openai-compatible",
424
+ children: [
425
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: t("settings.ai.baseUrl.label", "Base URL") }),
426
+ /* @__PURE__ */ jsxRuntime.jsx(
427
+ designSystem.TextInput,
428
+ {
429
+ type: "url",
430
+ value: draft.baseUrl,
431
+ disabled: readOnly,
432
+ placeholder: defaults.baseUrls[draft.provider] || "https://",
433
+ onChange: (event) => setField("baseUrl", event.target.value)
434
+ }
435
+ ),
436
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, {}),
437
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
438
+ ]
439
+ }
440
+ ) }),
441
+ /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Grid.Item, { col: 12, direction: "column", alignItems: "stretch", minWidth: 0, children: [
442
+ /* @__PURE__ */ jsxRuntime.jsxs(
443
+ designSystem.Field.Root,
444
+ {
445
+ name: "apiKey",
446
+ error: errorText(t, errors.apiKey, "apiKey"),
447
+ hint: !ai.canStoreApiKey && !ai.apiKeyFromEnvironment ? t(
448
+ "settings.ai.key.cannotStoreHint",
449
+ "Set MAGIC_EDITOR_X_ENCRYPTION_KEY (at least 32 characters) on the server to store a key here, or pass ai.apiKey in config/plugins.js."
450
+ ) : t(
451
+ "settings.ai.key.hint",
452
+ "Leave empty to keep the stored key. The key is never shown again after saving."
453
+ ),
454
+ children: [
455
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: t("settings.ai.key.label", "API key") }),
456
+ /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Flex, { direction: "column", alignItems: "stretch", gap: 2, children: [
457
+ /* @__PURE__ */ jsxRuntime.jsx(KeyStatus, { ai, draft, t }),
458
+ /* @__PURE__ */ jsxRuntime.jsx(
459
+ designSystem.TextInput,
460
+ {
461
+ type: "password",
462
+ autoComplete: "new-password",
463
+ value: draft.apiKeyMode === "replace" ? draft.apiKey : "",
464
+ disabled: keyDisabled || draft.apiKeyMode === "remove",
465
+ placeholder: t("settings.ai.key.placeholder", "Enter a new key"),
466
+ onChange: (event) => {
467
+ const value = event.target.value;
468
+ replaceDraft({ ...draft, apiKey: value, apiKeyMode: value ? "replace" : "keep" });
469
+ }
470
+ }
471
+ )
472
+ ] }),
473
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, {}),
474
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
475
+ ]
476
+ }
477
+ ),
478
+ !readOnly && ai.hasApiKey && !ai.apiKeyFromEnvironment ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { paddingTop: 2, children: draft.apiKeyMode === "remove" ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.TextButton, { onClick: () => replaceDraft({ ...draft, apiKeyMode: "keep", apiKey: "" }), children: t("settings.ai.key.keep", "Keep the stored key") }) : /* @__PURE__ */ jsxRuntime.jsx(designSystem.TextButton, { onClick: () => replaceDraft({ ...draft, apiKeyMode: "remove", apiKey: "" }), children: t("settings.ai.key.remove", "Remove the stored key") }) }) : null
479
+ ] }),
480
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Grid.Item, { col: 6, xs: 12, s: 12, m: 6, direction: "column", alignItems: "stretch", minWidth: 0, children: /* @__PURE__ */ jsxRuntime.jsxs(
481
+ designSystem.Field.Root,
482
+ {
483
+ name: "maxInputChars",
484
+ error: errorText(t, errors.maxInputChars, "maxInputChars"),
485
+ hint: t(
486
+ "settings.ai.maxInput.hint",
487
+ "Longer selections are refused. Allowed: {min} to {max} characters.",
488
+ LIMITS.maxInputChars
489
+ ),
490
+ required: true,
491
+ children: [
492
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: t("settings.ai.maxInput.label", "Maximum text length") }),
493
+ /* @__PURE__ */ jsxRuntime.jsx(
494
+ designSystem.NumberInput,
495
+ {
496
+ value: draft.maxInputChars ?? null,
497
+ disabled: readOnly,
498
+ onValueChange: (value) => setField("maxInputChars", value ?? null)
499
+ }
500
+ ),
501
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, {}),
502
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
503
+ ]
504
+ }
505
+ ) }),
506
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Grid.Item, { col: 6, xs: 12, s: 12, m: 6, direction: "column", alignItems: "stretch", minWidth: 0, children: /* @__PURE__ */ jsxRuntime.jsxs(
507
+ designSystem.Field.Root,
508
+ {
509
+ name: "maxOutputTokens",
510
+ error: errorText(t, errors.maxOutputTokens, "maxOutputTokens"),
511
+ hint: t(
512
+ "settings.ai.maxOutput.hint",
513
+ "Upper limit for one answer. Allowed: {min} to {max} tokens.",
514
+ LIMITS.maxOutputTokens
515
+ ),
516
+ required: true,
517
+ children: [
518
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: t("settings.ai.maxOutput.label", "Maximum answer length (tokens)") }),
519
+ /* @__PURE__ */ jsxRuntime.jsx(
520
+ designSystem.NumberInput,
521
+ {
522
+ value: draft.maxOutputTokens ?? null,
523
+ disabled: readOnly,
524
+ onValueChange: (value) => setField("maxOutputTokens", value ?? null)
525
+ }
526
+ ),
527
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, {}),
528
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
529
+ ]
530
+ }
531
+ ) })
532
+ ] }),
533
+ /* @__PURE__ */ jsxRuntime.jsx(designSystem.Typography, { variant: "pi", textColor: "neutral600", children: t(
534
+ "settings.ai.rbacHint",
535
+ 'Only roles with the "Use the AI assistant" permission see the assistant. Requests per editor are limited to {count} per minute.',
536
+ { count: dto.environment.requestsPerMinute }
537
+ ) })
538
+ ]
539
+ }
540
+ );
541
+ };
542
+ const megabytes = (t, formatNumber, bytes) => t("settings.environment.megabytes", "{size} MB", {
543
+ size: formatNumber(Number(bytes || 0) / (1024 * 1024), { maximumFractionDigits: 1 })
544
+ });
545
+ const EnvironmentSection = ({ environment }) => {
546
+ const t = useT.useT();
547
+ const { formatNumber } = reactIntl.useIntl();
548
+ const on = (value) => value ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.Badge, { backgroundColor: "success100", textColor: "success700", children: t("settings.environment.on", "On") }) : /* @__PURE__ */ jsxRuntime.jsx(designSystem.Badge, { backgroundColor: "neutral150", textColor: "neutral700", children: t("settings.environment.off", "Off") });
549
+ const rawHtml = {
550
+ sanitize: t(
551
+ "settings.environment.rawHtml.sanitize",
552
+ "Cleaned on save (scripts and event handlers removed)"
553
+ ),
554
+ allow: t("settings.environment.rawHtml.allow", "Stored as typed (the frontend must sanitise it)"),
555
+ disable: t("settings.environment.rawHtml.disable", "Emptied on save")
556
+ }[environment.rawHtml] || environment.rawHtml;
557
+ const collaboration = environment.collaboration || {};
558
+ return /* @__PURE__ */ jsxRuntime.jsx(
559
+ useT.SectionCard,
560
+ {
561
+ title: t("settings.environment.title", "Server configuration"),
562
+ subtitle: t(
563
+ "settings.environment.subtitle",
564
+ "These options are set in config/plugins.js and apply after a restart. They cannot be changed here."
565
+ ),
566
+ children: /* @__PURE__ */ jsxRuntime.jsx(
567
+ PropertyList,
568
+ {
569
+ items: [
570
+ {
571
+ key: "collaboration",
572
+ label: t("settings.environment.collaboration", "Live collaboration"),
573
+ value: on(collaboration.enabled),
574
+ hint: collaboration.enabled ? t(
575
+ "settings.environment.collaborationHint",
576
+ "Socket path {path}. Other servers are checked every {interval} ms.",
577
+ {
578
+ path: collaboration.path || "",
579
+ interval: collaboration.pollIntervalMs ?? 0
580
+ }
581
+ ) : void 0
582
+ },
583
+ {
584
+ key: "snapshots",
585
+ label: t("settings.environment.snapshots", "Automatic snapshots"),
586
+ value: collaboration.snapshotIntervalMinutes ? t("settings.environment.snapshotsEvery", "Every {minutes} minutes during live editing", {
587
+ minutes: collaboration.snapshotIntervalMinutes
588
+ }) : t("settings.environment.snapshotsOff", "Off")
589
+ },
590
+ { key: "rawHtml", label: t("settings.environment.rawHtml", "Raw HTML block"), value: rawHtml },
591
+ {
592
+ key: "images",
593
+ label: t("settings.environment.maxImage", "Largest image upload"),
594
+ value: megabytes(t, formatNumber, environment.uploads && environment.uploads.maxImageBytes)
595
+ },
596
+ {
597
+ key: "files",
598
+ label: t("settings.environment.maxFile", "Largest file upload"),
599
+ value: megabytes(t, formatNumber, environment.uploads && environment.uploads.maxFileBytes)
600
+ },
601
+ {
602
+ key: "privateAi",
603
+ label: t("settings.environment.privateAi", "Private AI hosts allowed"),
604
+ value: on(environment.allowPrivateBaseUrl),
605
+ hint: t(
606
+ "settings.environment.privateAiHint",
607
+ "Needed for local models on localhost or a private network (ai.allowPrivateBaseUrl)."
608
+ )
609
+ }
610
+ ]
611
+ }
612
+ )
613
+ }
614
+ );
615
+ };
616
+ const RBAC = [...index.PERMISSIONS.settingsRead, ...index.PERMISSIONS.settingsUpdate];
617
+ const testErrorText = (t, code) => {
618
+ switch (code) {
619
+ case "NOT_CONFIGURED":
620
+ return t("settings.test.notConfigured", "Connection failed: the provider is not configured.");
621
+ case "NO_API_KEY":
622
+ return t("settings.test.noKey", "Connection failed: no API key is available.");
623
+ case "AUTH_FAILED":
624
+ return t("settings.test.auth", "Connection failed: the provider rejected the API key.");
625
+ case "PROVIDER_RATE_LIMIT":
626
+ return t("settings.test.rateLimit", "Connection failed: the provider is busy or rate limited.");
627
+ case "TIMEOUT":
628
+ return t("settings.test.timeout", "Connection failed: the provider did not answer in time.");
629
+ case "REFUSED":
630
+ return t("settings.test.refused", "The provider answered but declined the test request.");
631
+ default:
632
+ return t("settings.test.failed", "Connection failed. Check the provider, base URL, model and key.");
633
+ }
634
+ };
635
+ const ConfigurationPage = () => {
636
+ const t = useT.useT();
637
+ const { toggleNotification } = admin.useNotification();
638
+ const { allowedActions, isLoading: rbacLoading } = admin.useRBAC(RBAC);
639
+ const form = useConfigurationForm();
640
+ const title = t("settings.configuration.title", "Configuration");
641
+ if (rbacLoading || form.status === "loading") {
642
+ return /* @__PURE__ */ jsxRuntime.jsxs(admin.Page.Main, { children: [
643
+ /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Title, { children: title }),
644
+ /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Loading, {})
645
+ ] });
646
+ }
647
+ if (!allowedActions.canRead) {
648
+ return /* @__PURE__ */ jsxRuntime.jsxs(admin.Page.Main, { children: [
649
+ /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Title, { children: title }),
650
+ /* @__PURE__ */ jsxRuntime.jsx(admin.Page.NoPermissions, {})
651
+ ] });
652
+ }
653
+ if (form.status === "error") {
654
+ return /* @__PURE__ */ jsxRuntime.jsxs(admin.Page.Main, { children: [
655
+ /* @__PURE__ */ jsxRuntime.jsx(admin.Page.Title, { children: title }),
656
+ /* @__PURE__ */ jsxRuntime.jsx(
657
+ admin.Page.Error,
658
+ {
659
+ content: t(
660
+ "settings.configuration.loadError",
661
+ "The settings could not be loaded. Nothing can be saved until they load."
662
+ ),
663
+ action: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Button, { variant: "secondary", onClick: form.reload, children: t("settings.retry", "Try again") })
664
+ }
665
+ )
666
+ ] });
667
+ }
668
+ const readOnly = !allowedActions.canUpdate;
669
+ const onSave = async () => {
670
+ const result = await form.save();
671
+ if (result.ok) {
672
+ toggleNotification({
673
+ type: "success",
674
+ message: result.adjusted.length ? t("settings.save.adjusted", "Settings saved. The server adjusted: {fields}.", {
675
+ fields: result.adjusted.join(", ")
676
+ }) : t("settings.save.success", "Settings saved.")
677
+ });
678
+ } else if (result.invalid) {
679
+ toggleNotification({
680
+ type: "warning",
681
+ message: t("settings.save.invalid", "Some values need attention before saving.")
682
+ });
683
+ } else {
684
+ toggleNotification({
685
+ type: "danger",
686
+ message: t(
687
+ "settings.save.error",
688
+ "The settings could not be saved. Your changes are still in the form."
689
+ )
690
+ });
691
+ }
692
+ };
693
+ const onTest = async () => {
694
+ const result = await form.test();
695
+ if (result.ok) {
696
+ toggleNotification({
697
+ type: "success",
698
+ message: t("settings.test.success", "Connection works. The provider answered.")
699
+ });
700
+ } else if (result.invalid) {
701
+ toggleNotification({
702
+ type: "warning",
703
+ message: t("settings.test.invalid", "Fix the highlighted fields before testing.")
704
+ });
705
+ } else {
706
+ toggleNotification({ type: "danger", message: testErrorText(t, result.code) });
707
+ }
708
+ };
709
+ return /* @__PURE__ */ jsxRuntime.jsxs(
710
+ useT.PageShell,
711
+ {
712
+ title,
713
+ subtitle: readOnly ? t(
714
+ "settings.configuration.readOnly",
715
+ 'You can view these settings. Changing them needs the "Update settings" permission.'
716
+ ) : t(
717
+ "settings.configuration.subtitle",
718
+ "Choose an AI provider for the editor assistant and review the server options."
719
+ ),
720
+ primaryAction: readOnly ? null : /* @__PURE__ */ jsxRuntime.jsx(
721
+ designSystem.Button,
722
+ {
723
+ startIcon: /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}),
724
+ onClick: onSave,
725
+ loading: form.saving,
726
+ disabled: form.saving || !form.dirty,
727
+ children: t("settings.save", "Save")
728
+ }
729
+ ),
730
+ secondaryAction: readOnly ? null : /* @__PURE__ */ jsxRuntime.jsx(
731
+ useT.ConfirmAction,
732
+ {
733
+ disabled: !form.dirty || form.saving,
734
+ trigger: /* @__PURE__ */ jsxRuntime.jsx(designSystem.Button, { variant: "tertiary", disabled: !form.dirty || form.saving, children: t("settings.discard", "Discard changes") }),
735
+ title: t("settings.discard.title", "Discard your changes?"),
736
+ body: t(
737
+ "settings.discard.body",
738
+ "The form goes back to the stored settings. Your unsaved changes are lost."
739
+ ),
740
+ confirmLabel: t("settings.discard.confirm", "Discard changes"),
741
+ variant: "danger-light",
742
+ onConfirm: () => form.discard()
743
+ }
744
+ ),
745
+ children: [
746
+ form.errors._form ? /* @__PURE__ */ jsxRuntime.jsx(
747
+ designSystem.Alert,
748
+ {
749
+ variant: "danger",
750
+ closeLabel: t("settings.close", "Close"),
751
+ onClose: form.clearServerErrors,
752
+ title: t("settings.save.rejected", "The server rejected the settings"),
753
+ children: typeof form.errors._form === "object" ? form.errors._form.server : form.errors._form
754
+ }
755
+ ) : null,
756
+ /* @__PURE__ */ jsxRuntime.jsx(AiSection, { form, readOnly, onTest }),
757
+ /* @__PURE__ */ jsxRuntime.jsx(EnvironmentSection, { environment: form.dto.environment }),
758
+ /* @__PURE__ */ jsxRuntime.jsxs(admin.Form, { method: "PUT", initialValues: form.saved, children: [
759
+ /* @__PURE__ */ jsxRuntime.jsx(DraftSync, { values: form.draft }),
760
+ /* @__PURE__ */ jsxRuntime.jsx(admin.Blocker, {})
761
+ ] })
762
+ ]
763
+ }
764
+ );
765
+ };
766
+ exports.ConfigurationPage = ConfigurationPage;
767
+ exports.default = ConfigurationPage;