@mainsoft/agent-cms 0.7.0 → 0.10.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 (111) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +291 -32
  3. package/TODO.md +72 -26
  4. package/UPGRADE-0.10.md +36 -0
  5. package/UPGRADE-0.8.md +117 -0
  6. package/dist/agent.cjs +39 -17
  7. package/dist/agent.d.cts +2 -2
  8. package/dist/agent.d.ts +2 -2
  9. package/dist/agent.js +9 -7
  10. package/dist/bin.js +4528 -355
  11. package/dist/{chunk-OQ55GEF3.js → chunk-3OPKTBEI.js} +1 -1
  12. package/dist/chunk-6PB4ZCHH.js +328 -0
  13. package/dist/chunk-7SCYDG2G.js +2943 -0
  14. package/dist/chunk-7WJIIJCI.js +544 -0
  15. package/dist/chunk-7Y6TU2UA.js +150 -0
  16. package/dist/{chunk-EXV6JNLY.js → chunk-AKCXMT3X.js} +15 -22
  17. package/dist/{chunk-QC2M4NZY.js → chunk-BFVBHIHV.js} +72 -1
  18. package/dist/chunk-DSJETZT4.js +493 -0
  19. package/dist/{chunk-IW3D4I7Q.js → chunk-IU2SMWAM.js} +246 -4
  20. package/dist/{chunk-VICZ76TR.js → chunk-KAO3Z3CF.js} +43 -2
  21. package/dist/{chunk-CRDW6Q72.js → chunk-MA6VKJ3F.js} +87 -25
  22. package/dist/{chunk-6K5ZTGHQ.js → chunk-PEQY4N73.js} +16 -0
  23. package/dist/{chunk-DOADAH4L.js → chunk-PGV2QPIL.js} +40 -0
  24. package/dist/{chunk-Y63XQS2J.js → chunk-TOZQYJFG.js} +1 -1
  25. package/dist/cli.cjs +4528 -355
  26. package/dist/cli.d.cts +19 -3
  27. package/dist/cli.d.ts +19 -3
  28. package/dist/cli.js +206 -16
  29. package/dist/client.cjs +72 -1
  30. package/dist/client.d.cts +308 -6
  31. package/dist/client.d.ts +308 -6
  32. package/dist/client.js +1 -1
  33. package/dist/cms-C_vkAxYV.d.cts +1537 -0
  34. package/dist/cms-pqJyowu1.d.ts +1537 -0
  35. package/dist/config.d.cts +7 -1
  36. package/dist/config.d.ts +7 -1
  37. package/dist/drizzle-repo-CviH3EX7.d.cts +146 -0
  38. package/dist/drizzle-repo-DWLD0rWe.d.ts +146 -0
  39. package/dist/errors.cjs +82 -1
  40. package/dist/errors.d.cts +43 -3
  41. package/dist/errors.d.ts +43 -3
  42. package/dist/errors.js +2 -2
  43. package/dist/frontmatter.d.cts +5 -4
  44. package/dist/frontmatter.d.ts +5 -4
  45. package/dist/http.cjs +640 -94
  46. package/dist/http.d.cts +51 -2
  47. package/dist/http.d.ts +51 -2
  48. package/dist/http.js +203 -13
  49. package/dist/index.cjs +3759 -575
  50. package/dist/index.d.cts +8 -8
  51. package/dist/index.d.ts +8 -8
  52. package/dist/index.js +226 -30
  53. package/dist/markdown.cjs +89 -25
  54. package/dist/markdown.d.cts +57 -3
  55. package/dist/markdown.d.ts +57 -3
  56. package/dist/markdown.js +5 -1
  57. package/dist/mcp.cjs +314 -37
  58. package/dist/mcp.d.cts +3 -2
  59. package/dist/mcp.d.ts +3 -2
  60. package/dist/mcp.js +12 -9
  61. package/dist/messages/en.cjs +40 -0
  62. package/dist/messages/en.js +1 -1
  63. package/dist/messages/ko.cjs +40 -0
  64. package/dist/messages/ko.js +40 -0
  65. package/dist/registry/cms-audit.json +1 -1
  66. package/dist/registry/cms-doc-editor.json +15 -4
  67. package/dist/registry/cms-doc-list.json +2 -1
  68. package/dist/registry/cms-files.json +29 -0
  69. package/dist/registry/cms-lib.json +3 -3
  70. package/dist/registry/cms-media-picker.json +29 -0
  71. package/dist/registry/cms-media.json +28 -0
  72. package/dist/registry/cms-preview-comments.json +24 -0
  73. package/dist/registry/cms-releases.json +28 -0
  74. package/dist/registry/cms-shell.json +2 -2
  75. package/dist/registry/cms-trash.json +24 -0
  76. package/dist/registry/cms-versions.json +2 -1
  77. package/dist/registry/registry.json +163 -3
  78. package/dist/storage-s3.cjs +168 -0
  79. package/dist/storage-s3.d.cts +53 -0
  80. package/dist/storage-s3.d.ts +53 -0
  81. package/dist/storage-s3.js +143 -0
  82. package/dist/storage.cjs +373 -0
  83. package/dist/storage.d.cts +183 -0
  84. package/dist/storage.d.ts +183 -0
  85. package/dist/storage.js +49 -0
  86. package/dist/tables.cjs +251 -3
  87. package/dist/tables.d.cts +4965 -681
  88. package/dist/tables.d.ts +4965 -681
  89. package/dist/tables.js +14 -2
  90. package/dist/tools.cjs +331 -20
  91. package/dist/tools.d.cts +54 -7
  92. package/dist/tools.d.ts +54 -7
  93. package/dist/tools.js +16 -7
  94. package/dist/types.cjs +99 -1
  95. package/dist/types.d.cts +205 -5
  96. package/dist/types.d.ts +205 -5
  97. package/dist/types.js +5 -3
  98. package/package.json +32 -22
  99. package/dist/ai.cjs +0 -555
  100. package/dist/ai.d.cts +0 -48
  101. package/dist/ai.d.ts +0 -48
  102. package/dist/ai.js +0 -73
  103. package/dist/chunk-52NUUZC7.js +0 -1446
  104. package/dist/chunk-IGL2V6US.js +0 -324
  105. package/dist/chunk-QQV6VLYJ.js +0 -204
  106. package/dist/cms-BPp01z-8.d.ts +0 -411
  107. package/dist/cms-D6D89-qZ.d.cts +0 -411
  108. package/dist/drizzle-repo-B1rt7r4j.d.ts +0 -90
  109. package/dist/drizzle-repo-Cn-kL2FK.d.cts +0 -90
  110. package/dist/repo-BWI2Pkrv.d.ts +0 -162
  111. package/dist/repo-CeDxUxO6.d.cts +0 -162
package/dist/cli.d.cts CHANGED
@@ -1,10 +1,13 @@
1
- import { D as DoctorReport } from './drizzle-repo-Cn-kL2FK.cjs';
1
+ import { M as MediaOptions } from './cms-C_vkAxYV.cjs';
2
+ import { D as DoctorReport } from './drizzle-repo-CviH3EX7.cjs';
2
3
  import { cmsTables } from './tables.cjs';
3
- import 'drizzle-orm/pg-core';
4
- import './repo-CeDxUxO6.cjs';
4
+ import './storage.cjs';
5
5
  import './types.cjs';
6
6
  import 'zod';
7
7
  import './errors.cjs';
8
+ import './markdown.cjs';
9
+ import './diff.cjs';
10
+ import 'drizzle-orm/pg-core';
8
11
 
9
12
  /**
10
13
  * The copy-in admin screens: reading the registry (built JSON in
@@ -46,6 +49,13 @@ interface Registry {
46
49
  * print: the full idempotent DDL; apply: run it
47
50
  * agent-cms add <item...> [--diff | --update] [--yes] [--overwrite] [--dir path]
48
51
  * copy admin screens into the app (see `agent-cms add --list`)
52
+ * agent-cms storage check [--origin https://your-site]
53
+ * try every file store for real (upload, signed addresses, large-file parts, CORS)
54
+ * and print what each store accepts. Needs `media` in agent-cms.config.ts.
55
+ * agent-cms storage cleanup remove uploads that were started and never finished
56
+ * agent-cms storage sync [--store name] [--folder path]
57
+ * make every file of a bucket known to the CMS (files that got there some other way),
58
+ * and list files the CMS knows that are gone from the bucket
49
59
  *
50
60
  * Against a running app, with an API key (same operations as the MCP tools):
51
61
  * agent-cms tools | collections
@@ -57,6 +67,8 @@ interface Registry {
57
67
  * so an edit someone made since is never overwritten. Frontmatter: see frontmatter.ts.
58
68
  * agent-cms patch <collection> <slug> <ops.json> [--expected-version N] [--dry-run]
59
69
  * agent-cms publish <collection> <slug>
70
+ * agent-cms upload <files...> [--store name] [--folder path] [--alt "text"] [--tag a,b]
71
+ * put files into the media library (large files go up in parts)
60
72
  * agent-cms call <tool> '<json args>'
61
73
  *
62
74
  * Env: AGENT_CMS_URL (https://app/api/cms) and AGENT_CMS_KEY (sgk_…), or
@@ -75,6 +87,10 @@ interface CliIo {
75
87
  registry?: Registry;
76
88
  /** Database for `doctor` / `migrate` (tests). Default: `db()` from agent-cms.config.ts. */
77
89
  db?: any;
90
+ /** Read a file as bytes (for `upload`; tests). Default: from disk, part by part. */
91
+ readBytes?: (path: string) => Uint8Array;
92
+ /** File stores for `storage` (tests). Default: `media` from agent-cms.config.ts. */
93
+ media?: MediaOptions;
78
94
  /** Run a command (for `add --yes`). */
79
95
  run?: (cmd: string, args: string[], cwd: string) => number;
80
96
  }
package/dist/cli.d.ts CHANGED
@@ -1,10 +1,13 @@
1
- import { D as DoctorReport } from './drizzle-repo-B1rt7r4j.js';
1
+ import { M as MediaOptions } from './cms-pqJyowu1.js';
2
+ import { D as DoctorReport } from './drizzle-repo-DWLD0rWe.js';
2
3
  import { cmsTables } from './tables.js';
3
- import 'drizzle-orm/pg-core';
4
- import './repo-BWI2Pkrv.js';
4
+ import './storage.js';
5
5
  import './types.js';
6
6
  import 'zod';
7
7
  import './errors.js';
8
+ import './markdown.js';
9
+ import './diff.js';
10
+ import 'drizzle-orm/pg-core';
8
11
 
9
12
  /**
10
13
  * The copy-in admin screens: reading the registry (built JSON in
@@ -46,6 +49,13 @@ interface Registry {
46
49
  * print: the full idempotent DDL; apply: run it
47
50
  * agent-cms add <item...> [--diff | --update] [--yes] [--overwrite] [--dir path]
48
51
  * copy admin screens into the app (see `agent-cms add --list`)
52
+ * agent-cms storage check [--origin https://your-site]
53
+ * try every file store for real (upload, signed addresses, large-file parts, CORS)
54
+ * and print what each store accepts. Needs `media` in agent-cms.config.ts.
55
+ * agent-cms storage cleanup remove uploads that were started and never finished
56
+ * agent-cms storage sync [--store name] [--folder path]
57
+ * make every file of a bucket known to the CMS (files that got there some other way),
58
+ * and list files the CMS knows that are gone from the bucket
49
59
  *
50
60
  * Against a running app, with an API key (same operations as the MCP tools):
51
61
  * agent-cms tools | collections
@@ -57,6 +67,8 @@ interface Registry {
57
67
  * so an edit someone made since is never overwritten. Frontmatter: see frontmatter.ts.
58
68
  * agent-cms patch <collection> <slug> <ops.json> [--expected-version N] [--dry-run]
59
69
  * agent-cms publish <collection> <slug>
70
+ * agent-cms upload <files...> [--store name] [--folder path] [--alt "text"] [--tag a,b]
71
+ * put files into the media library (large files go up in parts)
60
72
  * agent-cms call <tool> '<json args>'
61
73
  *
62
74
  * Env: AGENT_CMS_URL (https://app/api/cms) and AGENT_CMS_KEY (sgk_…), or
@@ -75,6 +87,10 @@ interface CliIo {
75
87
  registry?: Registry;
76
88
  /** Database for `doctor` / `migrate` (tests). Default: `db()` from agent-cms.config.ts. */
77
89
  db?: any;
90
+ /** Read a file as bytes (for `upload`; tests). Default: from disk, part by part. */
91
+ readBytes?: (path: string) => Uint8Array;
92
+ /** File stores for `storage` (tests). Default: `media` from agent-cms.config.ts. */
93
+ media?: MediaOptions;
78
94
  /** Run a command (for `add --yes`). */
79
95
  run?: (cmd: string, args: string[], cwd: string) => number;
80
96
  }
package/dist/cli.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-T66ZKBHF.js";
4
4
  import {
5
5
  DrizzleRepo
6
- } from "./chunk-IGL2V6US.js";
6
+ } from "./chunk-7WJIIJCI.js";
7
7
  import {
8
8
  docFromMarkdown
9
9
  } from "./chunk-DD56PTZ4.js";
@@ -13,30 +13,42 @@ import {
13
13
  schemaVersionSql,
14
14
  splitSql,
15
15
  upgradeSteps
16
- } from "./chunk-IW3D4I7Q.js";
16
+ } from "./chunk-IU2SMWAM.js";
17
17
  import {
18
18
  VERSION
19
- } from "./chunk-Y63XQS2J.js";
19
+ } from "./chunk-TOZQYJFG.js";
20
20
  import {
21
21
  CmsRemoteError,
22
22
  createCmsClient,
23
+ createRestClient,
23
24
  isCmsRemoteError
24
- } from "./chunk-QC2M4NZY.js";
25
+ } from "./chunk-BFVBHIHV.js";
26
+ import {
27
+ createCmsCore,
28
+ humanSize,
29
+ imageSize
30
+ } from "./chunk-7SCYDG2G.js";
31
+ import "./chunk-MA6VKJ3F.js";
32
+ import "./chunk-PEQY4N73.js";
25
33
  import {
26
34
  SCHEMA_VERSION
27
- } from "./chunk-OQ55GEF3.js";
35
+ } from "./chunk-3OPKTBEI.js";
28
36
  import {
29
37
  unifiedDiff
30
38
  } from "./chunk-AKN7SUXU.js";
31
- import "./chunk-VICZ76TR.js";
32
- import "./chunk-DOADAH4L.js";
39
+ import "./chunk-KAO3Z3CF.js";
40
+ import "./chunk-PGV2QPIL.js";
41
+ import {
42
+ DEFAULT_ACCEPT,
43
+ DEFAULT_MAX_FILE_SIZE
44
+ } from "./chunk-6PB4ZCHH.js";
33
45
  import {
34
46
  __dirname
35
47
  } from "./chunk-2NTK7H4W.js";
36
48
 
37
49
  // src/cli.ts
38
50
  import { spawnSync } from "child_process";
39
- import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
51
+ import { closeSync, existsSync as existsSync2, mkdirSync as mkdirSync2, openSync, readFileSync as readFileSync2, readSync, statSync, writeFileSync as writeFileSync2 } from "fs";
40
52
  import { basename as basename2, dirname as dirname2, join as join2 } from "path";
41
53
 
42
54
  // src/registry.ts
@@ -181,7 +193,7 @@ function addItems(o) {
181
193
  }
182
194
 
183
195
  // src/cli.ts
184
- var WITH_VALUE = /* @__PURE__ */ new Set(["--env", "--note", "--collections", "--expected-version", "--dir", "--format"]);
196
+ var WITH_VALUE = /* @__PURE__ */ new Set(["--env", "--note", "--collections", "--expected-version", "--dir", "--format", "--store", "--folder", "--alt", "--tag", "--origin"]);
185
197
  function flag(argv, name) {
186
198
  const i = argv.indexOf(`--${name}`);
187
199
  if (i >= 0) return argv[i + 1];
@@ -210,10 +222,10 @@ function parseEnvFile(text) {
210
222
  return out;
211
223
  }
212
224
  var USAGE = `agent-cms ${VERSION}
213
- in your app: init | doctor | migrate <check|print|apply> | add <item...> [--diff|--update]
214
- with a key: tools | collections | list | get | search | put | patch | publish | call
225
+ in your app: init | doctor | migrate <check|print|apply> | add <item...> [--diff|--update] | storage <check|cleanup|sync>
226
+ with a key: tools | collections | list | get | search | put | patch | publish | upload | call
215
227
  see https://github.com/mainsoft-2024/agent-cms#cli`;
216
- var LOCAL = /* @__PURE__ */ new Set(["init", "doctor", "migrate", "add"]);
228
+ var LOCAL = /* @__PURE__ */ new Set(["init", "doctor", "migrate", "add", "storage"]);
217
229
  async function runCli(argv, io = {}) {
218
230
  const out = io.out ?? ((l) => console.log(l));
219
231
  const err = io.err ?? ((l) => console.error(l));
@@ -305,6 +317,14 @@ ${USAGE}`, "usage");
305
317
  print(result);
306
318
  return 0;
307
319
  }
320
+ case "upload": {
321
+ need(1);
322
+ const rest = createRestClient({ url: env.AGENT_CMS_URL, fetch: io.fetch, headers: { authorization: `Bearer ${env.AGENT_CMS_KEY}`, "x-agent-cms-client": "cli" } });
323
+ const done = [];
324
+ for (const path of args) done.push(await uploadFile(rest, path, { store: flag(argv, "store"), folder: flag(argv, "folder"), alt: flag(argv, "alt"), tags: flag(argv, "tag")?.split(",").map((t) => t.trim()).filter(Boolean) }, io));
325
+ print(done);
326
+ return 0;
327
+ }
308
328
  case "call": {
309
329
  need(1);
310
330
  const parsed = args[1] ? JSON.parse(args[1]) : {};
@@ -322,6 +342,12 @@ async function runLocal(cmd, args, argv, io) {
322
342
  if (cmd === "add") return add(args, argv, io);
323
343
  const loaded = await loadConfig(io.cwd);
324
344
  const tables = cmsTables(loaded?.config.tables);
345
+ const media = async () => io.media ?? (typeof loaded?.config.media === "function" ? await loaded.config.media() : loaded?.config.media);
346
+ if (cmd === "storage" && (args[0] ?? "check") === "check") {
347
+ const m = await media();
348
+ if (!m) return noMedia(io);
349
+ return storageCheck(m, flag(argv, "origin"), io);
350
+ }
325
351
  if (cmd === "migrate" && args[0] === "print") {
326
352
  io.out(cmsDdl(tables));
327
353
  return 0;
@@ -340,7 +366,30 @@ async function runLocal(cmd, args, argv, io) {
340
366
  return 1;
341
367
  }
342
368
  const repo = new DrizzleRepo(db, { tables });
343
- if (cmd === "doctor") return doctor(await repo.inspect(), tables.options, io);
369
+ if (cmd === "storage") {
370
+ if (args[0] !== "cleanup" && args[0] !== "sync") {
371
+ io.err(`unknown: storage ${args[0]} (check | cleanup | sync)`);
372
+ return 1;
373
+ }
374
+ const m = await media();
375
+ if (!m) return noMedia(io);
376
+ if (args[0] === "sync") {
377
+ const r = await createCmsCore({ repo, collections: [], media: m }).assets.sync({ store: flag(argv, "store"), prefix: flag(argv, "folder") });
378
+ io.out(`[agent-cms] ${r.registered} file(s) registered, ${r.known} already known.`);
379
+ if (r.missing.length) io.out(`[agent-cms] ${r.missing.length} file(s) the CMS knows are gone from the bucket (nothing was deleted):`);
380
+ for (const f of r.missing.slice(0, 200)) io.out(` ${f.id} ${f.key}`);
381
+ return 0;
382
+ }
383
+ const { removed } = await createCmsCore({ repo, collections: [], media: m }).assets.cleanup();
384
+ io.out(`[agent-cms] removed ${removed} unfinished upload(s).`);
385
+ return 0;
386
+ }
387
+ if (cmd === "doctor") {
388
+ const report = await repo.inspect();
389
+ let stale = 0;
390
+ if (!report.missingTables.length && !report.missingColumns.length) stale = (await repo.listAssets({ status: "pending", createdBefore: new Date(Date.now() - 24 * 3600 * 1e3).toISOString(), limit: 500 })).length;
391
+ return doctor(report, tables.options, io, stale);
392
+ }
344
393
  if (cmd === "migrate") {
345
394
  const sub = args[0] ?? "check";
346
395
  if (sub === "apply") {
@@ -369,7 +418,7 @@ async function runLocal(cmd, args, argv, io) {
369
418
  function migrationSql(r, tables) {
370
419
  const current = r.schemaVersion ?? 0;
371
420
  if (!r.missingTables.length && !r.missingColumns.length && current >= SCHEMA_VERSION) return null;
372
- if (r.missingTables.some((t) => !t.endsWith("audit") && !t.endsWith("meta"))) return { sql: cmsDdl(tables), steps: ["create the agent-cms tables"] };
421
+ if (r.missingTables.some((t) => !/(audit|meta|assets|asset_refs|comments|releases|saved_filters|presence)$/.test(t))) return { sql: cmsDdl(tables), steps: ["create the agent-cms tables"] };
373
422
  const steps = upgradeSteps(tables).filter((s) => s.version > current || r.missingColumns.length > 0 || r.missingTables.length > 0);
374
423
  const meta = r.missingTables.some((t) => t.endsWith("meta")) ? `CREATE TABLE IF NOT EXISTS ${tables.options.schema ? `"${tables.options.schema}".` : ""}"${tables.options.prefix}meta" ("key" text PRIMARY KEY, "value" jsonb, "updated_at" timestamptz NOT NULL DEFAULT now());
375
424
  ` : "";
@@ -377,7 +426,7 @@ function migrationSql(r, tables) {
377
426
  ${s.sql}`).join("\n\n")}
378
427
  ${schemaVersionSql(tables)}`, steps: steps.map((s) => s.name) };
379
428
  }
380
- function doctor(r, o, io) {
429
+ function doctor(r, o, io, staleUploads = 0) {
381
430
  const lines = [];
382
431
  let bad = false;
383
432
  const ok = (m) => lines.push(`ok ${m}`);
@@ -394,6 +443,7 @@ function doctor(r, o, io) {
394
443
  else ok(`schema version ${r.schemaVersion}`);
395
444
  if (r.badPublished) fail(`${r.badPublished} document(s) point at a published version that is missing`);
396
445
  if (r.orphanVersions) warn(`${r.orphanVersions} version row(s) without a document`);
446
+ if (staleUploads) warn(`${staleUploads === 500 ? "500+" : staleUploads} upload(s) started more than a day ago and never finished (npx agent-cms storage cleanup)`);
397
447
  if (r.legacyColumns.length) {
398
448
  warn(`old columns nothing reads any more: ${r.legacyColumns.join(", ")}. To drop them:`);
399
449
  const t = (name) => o.schema ? `"${o.schema}"."${name}"` : `"${name}"`;
@@ -408,6 +458,146 @@ function doctor(r, o, io) {
408
458
  for (const l of lines) io.out(l);
409
459
  return bad ? 1 : 0;
410
460
  }
461
+ function noMedia(io) {
462
+ io.err("[agent-cms] no file stores: add `media` (the same object you pass to createCms) to agent-cms.config.ts.");
463
+ return 1;
464
+ }
465
+ async function uploadFile(rest, path, o, io) {
466
+ const doFetch = io.fetch ?? fetch;
467
+ const whole = io.readBytes?.(path);
468
+ const size = whole ? whole.length : statSync(path).size;
469
+ const fd = whole ? null : openSync(path, "r");
470
+ const slice = (start, end) => {
471
+ if (whole) return whole.subarray(start, end);
472
+ const buf = new Uint8Array(end - start);
473
+ readSync(fd, buf, 0, buf.length, start);
474
+ return buf;
475
+ };
476
+ const send = async (url, bytes, headers = {}) => {
477
+ let last = "";
478
+ for (let attempt = 0; attempt < 3; attempt++) {
479
+ const res = await doFetch(url, { method: "PUT", headers, body: bytes }).catch((e) => ({ ok: false, status: 0, text: async () => String(e) }));
480
+ if (res.ok) return;
481
+ last = `${res.status} ${(await res.text()).slice(0, 200)}`;
482
+ }
483
+ throw new CmsRemoteError(`the storage refused the upload of ${path}: ${last}`, "upload_failed");
484
+ };
485
+ try {
486
+ const { asset, upload } = await rest.requestUpload({ name: basename2(path), size, store: o.store, folder: o.folder, alt: o.alt, tags: o.tags });
487
+ try {
488
+ if (upload.kind === "single") await send(upload.url, slice(0, size), upload.headers);
489
+ else {
490
+ for (let first = 1; first <= upload.partCount; first += 4) {
491
+ const numbers = Array.from({ length: Math.min(4, upload.partCount - first + 1) }, (_, i) => first + i);
492
+ const urls = await rest.uploadPartUrls(asset.id, numbers);
493
+ await Promise.all(urls.map((u) => send(u.url, slice((u.partNumber - 1) * upload.partSize, Math.min(u.partNumber * upload.partSize, size)))));
494
+ }
495
+ }
496
+ const dims = asset.kind === "image" ? imageSize(slice(0, Math.min(size, 256 * 1024))) : null;
497
+ const done = await rest.completeUpload(asset.id, dims ?? {});
498
+ return { id: done.id, name: done.name, store: done.store, folder: done.folder, size: done.size, url: done.publicUrl ?? done.href, markdown: `${done.kind === "image" || done.kind === "video" || done.kind === "audio" ? "!" : ""}[${done.kind === "image" ? done.alt : done.name}](asset:${done.id}/${encodeURIComponent(done.name)})` };
499
+ } catch (e) {
500
+ await rest.abortUpload(asset.id).catch(() => {
501
+ });
502
+ throw e;
503
+ }
504
+ } finally {
505
+ if (fd !== null) closeSync(fd);
506
+ }
507
+ }
508
+ async function storageCheck(media, origin, io) {
509
+ const doFetch = io.fetch ?? fetch;
510
+ let bad = false;
511
+ const run = `_cms/check-${Date.now().toString(36)}`;
512
+ for (const [name, cfg] of Object.entries(media.stores)) {
513
+ const a = cfg.adapter;
514
+ io.out(`store "${name}" (${cfg.visibility})`);
515
+ io.out(` accepts: ${(cfg.accept ?? media.accept ?? DEFAULT_ACCEPT).join(", ")}`);
516
+ io.out(` largest file: ${humanSize(cfg.maxFileSize ?? media.maxFileSize ?? DEFAULT_MAX_FILE_SIZE)}`);
517
+ const step = async (label, fn, o = {}) => {
518
+ try {
519
+ const note = await fn();
520
+ io.out(`ok ${label}${note ? ` (${note})` : ""}`);
521
+ } catch (e) {
522
+ if (!o.warnOnly) bad = true;
523
+ io.out(`${o.warnOnly ? "warn " : "FAIL "} ${label}: ${e instanceof Error ? e.message : String(e)}`);
524
+ }
525
+ };
526
+ const expect = (ok, message) => {
527
+ if (!ok) throw new Error(message);
528
+ };
529
+ const text = new TextEncoder().encode("agent-cms storage check");
530
+ await step("write, read and size from the server", async () => {
531
+ await a.put(`${run}/a.txt`, text, { mime: "text/plain" });
532
+ expect((await a.head(`${run}/a.txt`))?.size === text.length, "the stored size differs");
533
+ expect((await a.readStart(`${run}/a.txt`, 5))?.length === 5, "could not read the first bytes");
534
+ });
535
+ let signedPut = "";
536
+ await step("upload with a signed address, as browsers do", async () => {
537
+ const signed = await a.presignPut(`${run}/b.txt`, { mime: "text/plain", expires: 300 });
538
+ signedPut = signed.url;
539
+ const res = await doFetch(signed.url, { method: "PUT", headers: signed.headers, body: text });
540
+ expect(res.ok, `PUT answered ${res.status}`);
541
+ expect((await a.head(`${run}/b.txt`))?.size === text.length, "the file did not arrive");
542
+ });
543
+ await step("large files in parts", async () => {
544
+ const id = await a.createMultipart(`${run}/c.bin`, { mime: "application/octet-stream" });
545
+ const sizes = [5 * 1024 * 1024, 1024];
546
+ for (const n of [1, 2]) {
547
+ const res = await doFetch(await a.presignPart(`${run}/c.bin`, id, n, 300), { method: "PUT", body: new Uint8Array(sizes[n - 1]) });
548
+ expect(res.ok, `part ${n} answered ${res.status}`);
549
+ }
550
+ const parts = await a.listParts(`${run}/c.bin`, id);
551
+ expect(parts.length === 2, `the storage lists ${parts.length} of 2 parts`);
552
+ await a.completeMultipart(`${run}/c.bin`, id, parts);
553
+ expect((await a.head(`${run}/c.bin`))?.size === sizes[0] + sizes[1], "the joined file has the wrong size");
554
+ });
555
+ await step("short-lived link to read a file", async () => {
556
+ const url = await a.presignGet(`${run}/a.txt`, { expires: 60 });
557
+ const res = await doFetch(url);
558
+ expect(res.ok, `GET answered ${res.status}`);
559
+ await res.arrayBuffer();
560
+ });
561
+ if (cfg.visibility === "private") {
562
+ await step("files cannot be read without a link", async () => {
563
+ const res = await doFetch((await a.presignGet(`${run}/a.txt`, { expires: 60 })).split("?")[0]);
564
+ await res.arrayBuffer().catch(() => {
565
+ });
566
+ expect(!res.ok, "the bucket answers anyone: a private store needs a bucket that is not public");
567
+ });
568
+ } else {
569
+ await step("public address", async () => {
570
+ const url = a.publicUrl(`${run}/a.txt`);
571
+ expect(url, "no publicUrl: files open through a short-lived link instead (set publicUrl on the adapter for direct, cacheable addresses)");
572
+ const res = await doFetch(url);
573
+ await res.arrayBuffer().catch(() => {
574
+ });
575
+ expect(res.ok, `${url} answered ${res.status}: the bucket (or its CDN) is not public`);
576
+ return url.split(run)[0];
577
+ }, { warnOnly: true });
578
+ }
579
+ await step("move (copy and delete)", async () => {
580
+ await a.copy(`${run}/a.txt`, `${run}/d.txt`);
581
+ await a.delete(`${run}/a.txt`);
582
+ expect(!await a.head(`${run}/a.txt`) && await a.head(`${run}/d.txt`), "copy or delete did not take effect");
583
+ });
584
+ if (origin && signedPut) {
585
+ await step(`browsers on ${origin} may upload (CORS)`, async () => {
586
+ const res = await doFetch(signedPut, { method: "OPTIONS", headers: { origin, "access-control-request-method": "PUT", "access-control-request-headers": "content-type" } });
587
+ const allow = res.headers.get("access-control-allow-origin");
588
+ await res.arrayBuffer().catch(() => {
589
+ });
590
+ expect(allow === "*" || allow === origin, `the bucket's CORS rule does not allow ${origin}`);
591
+ });
592
+ } else io.out("skip CORS (pass --origin https://your-site to test it)");
593
+ for (const k of ["a.txt", "b.txt", "c.bin", "d.txt"]) await a.delete(`${run}/${k}`).catch(() => {
594
+ });
595
+ }
596
+ io.out("");
597
+ io.out("CORS rule the bucket needs (replace the origin with your site):");
598
+ io.out(JSON.stringify([{ AllowedOrigins: [origin ?? "https://your-site.example"], AllowedMethods: ["GET", "HEAD", "PUT"], AllowedHeaders: ["*"], ExposeHeaders: ["ETag"], MaxAgeSeconds: 3600 }], null, 2));
599
+ return bad ? 1 : 0;
600
+ }
411
601
  function init(argv, io) {
412
602
  const cwd = io.cwd;
413
603
  const appDir = existsSync2(join2(cwd, "src", "app")) ? "src/" : "";
@@ -425,7 +615,7 @@ export default defineConfig({
425
615
  [
426
616
  `${appDir}lib/cms-schema.ts`,
427
617
  `// Re-export the agent-cms tables from your Drizzle schema so drizzle-kit sees them.
428
- export { cmsApiKeys, cmsAudit, cmsDocuments, cmsHits, cmsMeta, cmsVersions } from "@mainsoft/agent-cms/tables";
618
+ export { cmsApiKeys, cmsAssetRefs, cmsAssets, cmsAudit, cmsComments, cmsDocuments, cmsHits, cmsMeta, cmsPresence, cmsReleases, cmsSavedFilters, cmsVersions } from "@mainsoft/agent-cms/tables";
429
619
  `
430
620
  ],
431
621
  [
package/dist/client.cjs CHANGED
@@ -168,11 +168,82 @@ function createRestClient(opts) {
168
168
  restore: (c, s, n, o = {}) => req("POST", `${doc(c, s)}/versions/${n}/restore`, { body: { publish: o.publish }, ifMatch: o.expectedVersion }).then((r) => r.doc),
169
169
  changes: (c, s) => req("GET", `${doc(c, s)}/changes`).then((r) => r.changes),
170
170
  review: (c, s, hunks, action = "accepted") => req("POST", `${doc(c, s)}/review`, { body: { hunks, action } }),
171
- previewLink: (c, s, ttl = "24h") => req("POST", `${doc(c, s)}/preview`, { body: { ttl } }),
171
+ previewLink: (c, s, ttl = "24h", o = {}) => req("POST", `${doc(c, s)}/preview`, { body: { ttl, ...o } }),
172
+ /** The working copy plus the open release it is in, if any. */
173
+ getWithRelease: (c, s) => req("GET", doc(c, s), { query: { draft: true } }),
174
+ preview: (c, d, slug) => req("POST", `/collections/${enc(c)}/_preview`, { body: { doc: d, slug } }),
175
+ schedule: (c, s, b) => req("POST", `${doc(c, s)}/schedule`, { body: b }).then((r) => r.doc),
176
+ unschedule: (c, s) => req("DELETE", `${doc(c, s)}/schedule`).then((r) => r.doc),
177
+ requestReview: (c, s, note) => req("POST", `${doc(c, s)}/review/request`, { body: { note } }).then((r) => r.doc),
178
+ approveReview: (c, s, o = {}) => req("POST", `${doc(c, s)}/review/approve`, { body: o }).then((r) => r.doc),
179
+ rejectReview: (c, s, note) => req("POST", `${doc(c, s)}/review/reject`, { body: { note } }).then((r) => r.doc),
180
+ reviewRequests: (collection) => req("GET", "/reviews", { query: { collection } }).then((r) => r.requests),
181
+ comments: (c, s) => req("GET", `${doc(c, s)}/comments`).then((r) => r.comments),
182
+ addComment: (c, s, b) => req("POST", `${doc(c, s)}/comments`, { body: b }).then((r) => r.comment),
183
+ resolveComment: (c, s, id, resolved = true) => req("POST", `${doc(c, s)}/comments/${enc(id)}/resolve`, { body: { resolved } }).then((r) => r.comment),
184
+ previewComments: (token) => req("GET", "/preview/comments", { query: { token } }).then((r) => r.comments),
185
+ addPreviewComment: (token, b) => req("POST", "/preview/comments", { query: { token }, body: b }).then((r) => r.comment),
186
+ presence: (c, s, b = {}) => req("POST", `${doc(c, s)}/presence`, { body: b }),
187
+ leave: (c, s) => req("DELETE", `${doc(c, s)}/presence`),
188
+ presenceIn: (c) => req("GET", `/collections/${enc(c)}/_presence`).then((r) => r.presence),
189
+ labelVersion: (c, s, n, b) => req("PATCH", `${doc(c, s)}/versions/${n}`, { body: b }).then((r) => r.version),
190
+ filters: (c) => req("GET", `/collections/${enc(c)}/_filters`).then((r) => r.filters),
191
+ saveFilter: (c, b) => req("POST", `/collections/${enc(c)}/_filters`, { body: b }).then((r) => r.filter),
192
+ removeFilter: (c, id) => req("DELETE", `/collections/${enc(c)}/_filters/${enc(id)}`),
193
+ trash: (kind) => req("GET", "/trash", { query: { kind } }),
194
+ restoreTrash: (items) => req("POST", "/trash/_restore", { body: { items: items.map((i) => ({ kind: i.kind, id: i.id })) } }),
195
+ purgeTrash: (items) => req("POST", "/trash/_purge", { body: items === "all" ? { all: true } : { items: items.map((i) => ({ kind: i.kind, id: i.id })) } }),
196
+ releases: () => req("GET", "/releases").then((r) => r.releases),
197
+ release: (id) => req("GET", `/releases/${enc(id)}`).then((r) => r.release),
198
+ createRelease: (name) => req("POST", "/releases", { body: { name } }).then((r) => r.release),
199
+ renameRelease: (id, name) => req("PATCH", `/releases/${enc(id)}`, { body: { name } }).then((r) => r.release),
200
+ removeRelease: (id) => req("DELETE", `/releases/${enc(id)}`),
201
+ addToRelease: (id, items) => req("POST", `/releases/${enc(id)}/items`, { body: { items } }).then((r) => r.release),
202
+ removeFromRelease: (id, documentId) => req("DELETE", `/releases/${enc(id)}/items/${enc(documentId)}`).then((r) => r.release),
203
+ publishRelease: (id) => req("POST", `/releases/${enc(id)}/publish`, { body: {} }).then((r) => r.release),
204
+ scheduleRelease: (id, publishAt) => req("POST", `/releases/${enc(id)}/schedule`, { body: { publishAt } }).then((r) => r.release),
172
205
  search: (q, collections) => req("GET", "/search", { query: { q, collections: collections?.join(",") } }),
173
206
  keys: () => req("GET", "/keys").then((r) => r.keys),
174
207
  createKey: (k) => req("POST", "/keys", { body: k }),
175
208
  revokeKey: (id) => req("DELETE", `/keys/${enc(id)}`),
209
+ assets: (q = {}) => req("GET", "/assets", { query: { ...q } }),
210
+ assetFolders: () => req("GET", "/assets/_folders").then((r) => r.stores),
211
+ createAssetFolder: (path, store) => req("POST", "/assets/_folders", { body: { path, store } }),
212
+ deleteAssetFolder: (path, store) => req("DELETE", "/assets/_folders", { query: { path, store } }),
213
+ asset: (id) => req("GET", `/assets/${enc(id)}`).then((r) => r.asset),
214
+ updateAsset: (id, patch) => req("PATCH", `/assets/${enc(id)}`, { body: patch }).then((r) => r.asset),
215
+ moveAsset: (id, to) => req("POST", `/assets/${enc(id)}/move`, { body: to }).then((r) => r.asset),
216
+ /** Fails with `asset_in_use` (the documents are in `error.current`) unless `confirm` is set. */
217
+ removeAsset: (id, o = {}) => req("DELETE", `/assets/${enc(id)}`, { query: { confirm: o.confirm } }),
218
+ importAsset: (b) => req("POST", "/assets/_import", { body: b }).then((r) => r.asset),
219
+ requestUpload: (b) => req("POST", "/assets/uploads", { body: b }),
220
+ uploadStatus: (id) => req("GET", `/assets/uploads/${enc(id)}`),
221
+ uploadPartUrls: (id, parts) => req("POST", `/assets/uploads/${enc(id)}/parts`, { body: { parts } }).then((r) => r.parts),
222
+ completeUpload: (id, meta = {}) => req("POST", `/assets/uploads/${enc(id)}/complete`, { body: meta }).then((r) => r.asset),
223
+ abortUpload: (id) => req("DELETE", `/assets/uploads/${enc(id)}`),
224
+ /** Store a small picture of the file (WebP, JPEG or PNG, up to 512KB). */
225
+ async setAssetThumb(id, bytes) {
226
+ let res;
227
+ try {
228
+ res = await doFetch(`${base}/assets/${enc(id)}/thumb`, { method: "POST", credentials: "same-origin", headers: { accept: "application/json", "content-type": "application/octet-stream", ...opts.headers }, body: bytes });
229
+ } catch (e) {
230
+ throw new CmsRemoteError(e instanceof Error ? e.message : String(e), "network");
231
+ }
232
+ const data = await res.json().catch(() => null);
233
+ if (!res.ok || !data?.asset) throw new CmsRemoteError(data?.message ?? `HTTP ${res.status}`, data?.error ?? "http", { kind: data?.kind, status: res.status, params: data?.params });
234
+ return data.asset;
235
+ },
236
+ storeFiles: (store, q = {}) => req("GET", `/stores/${enc(store)}/files`, { query: { ...q } }),
237
+ registerFile: (store, key, more = {}) => req("POST", `/stores/${enc(store)}/files/_register`, { body: { key, ...more } }).then((r) => r.asset),
238
+ /** Move files and whole folders into the folder `to` ("" = the top). */
239
+ moveFiles: (store, b) => req("POST", `/stores/${enc(store)}/files/_move`, { body: b }),
240
+ /** Fails with `asset_in_use` unless `confirm` is set. `check: true` deletes nothing and only counts the files and their uses. */
241
+ removeFiles: (store, b) => req("POST", `/stores/${enc(store)}/files/_delete`, { body: b }),
242
+ shareAsset: (id, ttl = "24h") => req("POST", `/assets/${enc(id)}/share`, { body: { ttl } }),
243
+ /** Address of a file by its key (files the CMS has no row for). */
244
+ storeFileUrl: (store, key, o = {}) => `${base}/stores/${enc(store)}/files/_open?key=${enc(key)}${o.download ? "&download=1" : ""}`,
245
+ /** Address of a file through the CMS route (`thumb`: its small picture). */
246
+ fileUrl: (a, o = {}) => `${base}/files/${enc(a.id)}/${enc(a.name)}${o.thumb ? "?thumb=1" : o.download ? "?download=1" : ""}`,
176
247
  audit: (q = {}) => req("GET", "/audit", { query: q })
177
248
  };
178
249
  }