@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/http.d.cts CHANGED
@@ -1,10 +1,11 @@
1
- import { C as CmsLike } from './cms-D6D89-qZ.cjs';
1
+ import { C as CmsLike } from './cms-C_vkAxYV.cjs';
2
2
  import { McpOptions } from './mcp.cjs';
3
3
  import { Actor } from './types.cjs';
4
+ import './storage.cjs';
4
5
  import 'zod';
6
+ import './markdown.cjs';
5
7
  import './diff.cjs';
6
8
  import './errors.cjs';
7
- import './repo-CeDxUxO6.cjs';
8
9
  import '@modelcontextprotocol/sdk/server/index.js';
9
10
 
10
11
  /**
@@ -46,6 +47,54 @@ import '@modelcontextprotocol/sdk/server/index.js';
46
47
  * GET /keys | POST /keys {name, scopes, collections?, expiresAt?} | DELETE /keys/:id
47
48
  * GET /audit?cursor=&action=&outcome=&actorKind= (also /_audit)
48
49
  * POST /mcp MCP (official SDK, streamable HTTP, stateless)
50
+ *
51
+ * Media library (only with `createCms({ media })`):
52
+ *
53
+ * GET /assets?store=&folder=&kind=image,video&q=&tag=&cursor=&limit=
54
+ * GET /assets/_folders stores with their folders and file counts
55
+ * POST /assets/_folders {store?, path} DELETE /assets/_folders?store=&path= (empty folders only)
56
+ * POST /assets/uploads {name, mime?, size, folder?, store?, alt?, tags?, field?}
57
+ * → {asset, upload: {kind: "single", url, headers} | {kind: "multipart", partSize, partCount}}
58
+ * GET /assets/uploads/:id which parts have arrived DELETE: give up
59
+ * POST /assets/uploads/:id/parts {parts: [1, 2]} signed addresses for parts
60
+ * POST /assets/uploads/:id/complete {width?, height?, duration?}
61
+ * POST /assets/_import {url, name?, folder?, store?, alt?, tags?}
62
+ * GET /assets/:id the file and the documents that use it
63
+ * PATCH /assets/:id {alt?, tags?}
64
+ * POST /assets/:id/move {folder?, name?}
65
+ * POST /assets/:id/thumb body: a WebP, JPEG or PNG up to 512KB
66
+ * DELETE /assets/:id 409 asset_in_use with the documents; ?confirm=1 deletes anyway
67
+ * GET /files/:id/:name redirect to the file (?thumb=1, ?download=1); private files need a session or key
68
+ *
69
+ * File manager (the bucket as it is, also files the CMS did not upload):
70
+ *
71
+ * GET /stores/:store/files?prefix=&q=&cursor=&limit= one folder: sub-folders and files, each with its asset when known
72
+ * GET /stores/:store/files/_open?key=&download=1 redirect to a file by key
73
+ * POST /stores/:store/files/_register {key, alt?, tags?}
74
+ * POST /stores/:store/files/_move {keys?, folders?, to} → {moved, failed: [{key, message}]}
75
+ * POST /stores/:store/files/_delete {keys?, folders?, confirm?} 409 asset_in_use with the documents unless confirm
76
+ * POST /assets/:id/share {ttl: "1h" | "24h" | "7d"} → {url, expiresAt}
77
+ *
78
+ * Editing and publishing flow (0.10.0):
79
+ *
80
+ * GET|POST /_cron run what is due; `Authorization: Bearer <cronSecret>` (404 without `cronSecret`)
81
+ * POST /collections/:c/_preview {doc, slug?} blocks, toc, diagnostics, diff; nothing saved
82
+ * GET /collections/:c/_presence who has which document open
83
+ * GET /collections/:c/_filters | POST {name, query, shared?} | DELETE /collections/:c/_filters/:id
84
+ * POST /collections/:c/:slug/schedule {publishAt?, unpublishAt?, expectedVersion?} DELETE: cancel
85
+ * POST /collections/:c/:slug/review/request {note?} | review/approve {publish?} | review/reject {note?}
86
+ * GET /collections/:c/:slug/comments | POST {body, anchor?, quote?, threadId?}
87
+ * POST /collections/:c/:slug/comments/:id/resolve {resolved?}
88
+ * POST /collections/:c/:slug/presence {cursor?, selection?} DELETE: leave
89
+ * PATCH /collections/:c/:slug/versions/:n {label?, pinned?}
90
+ * GET /reviews?collection= documents waiting for a review
91
+ * GET /trash?kind=document|asset | POST /trash/_restore {items} | POST /trash/_purge {items | all: true}
92
+ * GET /releases | POST {name}
93
+ * GET /releases/:id | PATCH {name} | DELETE
94
+ * POST /releases/:id/items {items: [{collection, slug, action?}]} | DELETE /releases/:id/items/:documentId
95
+ * POST /releases/:id/publish all or nothing: 428 release_blocked lists what blocked
96
+ * POST /releases/:id/schedule {publishAt | null}
97
+ * GET /preview/comments?token= | POST {name?, body, anchor?, quote?, threadId?} links made with comments: true
49
98
  */
50
99
 
51
100
  interface CmsHandlerOptions {
package/dist/http.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { C as CmsLike } from './cms-BPp01z-8.js';
1
+ import { C as CmsLike } from './cms-pqJyowu1.js';
2
2
  import { McpOptions } from './mcp.js';
3
3
  import { Actor } from './types.js';
4
+ import './storage.js';
4
5
  import 'zod';
6
+ import './markdown.js';
5
7
  import './diff.js';
6
8
  import './errors.js';
7
- import './repo-BWI2Pkrv.js';
8
9
  import '@modelcontextprotocol/sdk/server/index.js';
9
10
 
10
11
  /**
@@ -46,6 +47,54 @@ import '@modelcontextprotocol/sdk/server/index.js';
46
47
  * GET /keys | POST /keys {name, scopes, collections?, expiresAt?} | DELETE /keys/:id
47
48
  * GET /audit?cursor=&action=&outcome=&actorKind= (also /_audit)
48
49
  * POST /mcp MCP (official SDK, streamable HTTP, stateless)
50
+ *
51
+ * Media library (only with `createCms({ media })`):
52
+ *
53
+ * GET /assets?store=&folder=&kind=image,video&q=&tag=&cursor=&limit=
54
+ * GET /assets/_folders stores with their folders and file counts
55
+ * POST /assets/_folders {store?, path} DELETE /assets/_folders?store=&path= (empty folders only)
56
+ * POST /assets/uploads {name, mime?, size, folder?, store?, alt?, tags?, field?}
57
+ * → {asset, upload: {kind: "single", url, headers} | {kind: "multipart", partSize, partCount}}
58
+ * GET /assets/uploads/:id which parts have arrived DELETE: give up
59
+ * POST /assets/uploads/:id/parts {parts: [1, 2]} signed addresses for parts
60
+ * POST /assets/uploads/:id/complete {width?, height?, duration?}
61
+ * POST /assets/_import {url, name?, folder?, store?, alt?, tags?}
62
+ * GET /assets/:id the file and the documents that use it
63
+ * PATCH /assets/:id {alt?, tags?}
64
+ * POST /assets/:id/move {folder?, name?}
65
+ * POST /assets/:id/thumb body: a WebP, JPEG or PNG up to 512KB
66
+ * DELETE /assets/:id 409 asset_in_use with the documents; ?confirm=1 deletes anyway
67
+ * GET /files/:id/:name redirect to the file (?thumb=1, ?download=1); private files need a session or key
68
+ *
69
+ * File manager (the bucket as it is, also files the CMS did not upload):
70
+ *
71
+ * GET /stores/:store/files?prefix=&q=&cursor=&limit= one folder: sub-folders and files, each with its asset when known
72
+ * GET /stores/:store/files/_open?key=&download=1 redirect to a file by key
73
+ * POST /stores/:store/files/_register {key, alt?, tags?}
74
+ * POST /stores/:store/files/_move {keys?, folders?, to} → {moved, failed: [{key, message}]}
75
+ * POST /stores/:store/files/_delete {keys?, folders?, confirm?} 409 asset_in_use with the documents unless confirm
76
+ * POST /assets/:id/share {ttl: "1h" | "24h" | "7d"} → {url, expiresAt}
77
+ *
78
+ * Editing and publishing flow (0.10.0):
79
+ *
80
+ * GET|POST /_cron run what is due; `Authorization: Bearer <cronSecret>` (404 without `cronSecret`)
81
+ * POST /collections/:c/_preview {doc, slug?} blocks, toc, diagnostics, diff; nothing saved
82
+ * GET /collections/:c/_presence who has which document open
83
+ * GET /collections/:c/_filters | POST {name, query, shared?} | DELETE /collections/:c/_filters/:id
84
+ * POST /collections/:c/:slug/schedule {publishAt?, unpublishAt?, expectedVersion?} DELETE: cancel
85
+ * POST /collections/:c/:slug/review/request {note?} | review/approve {publish?} | review/reject {note?}
86
+ * GET /collections/:c/:slug/comments | POST {body, anchor?, quote?, threadId?}
87
+ * POST /collections/:c/:slug/comments/:id/resolve {resolved?}
88
+ * POST /collections/:c/:slug/presence {cursor?, selection?} DELETE: leave
89
+ * PATCH /collections/:c/:slug/versions/:n {label?, pinned?}
90
+ * GET /reviews?collection= documents waiting for a review
91
+ * GET /trash?kind=document|asset | POST /trash/_restore {items} | POST /trash/_purge {items | all: true}
92
+ * GET /releases | POST {name}
93
+ * GET /releases/:id | PATCH {name} | DELETE
94
+ * POST /releases/:id/items {items: [{collection, slug, action?}]} | DELETE /releases/:id/items/:documentId
95
+ * POST /releases/:id/publish all or nothing: 428 release_blocked lists what blocked
96
+ * POST /releases/:id/schedule {publishAt | null}
97
+ * GET /preview/comments?token= | POST {name?, body, anchor?, quote?, threadId?} links made with comments: true
49
98
  */
50
99
 
51
100
  interface CmsHandlerOptions {
package/dist/http.js CHANGED
@@ -1,31 +1,36 @@
1
1
  import {
2
2
  handleMcp,
3
3
  toJsonSchema
4
- } from "./chunk-EXV6JNLY.js";
5
- import "./chunk-Y63XQS2J.js";
6
- import "./chunk-QQV6VLYJ.js";
4
+ } from "./chunk-AKCXMT3X.js";
5
+ import "./chunk-TOZQYJFG.js";
6
+ import "./chunk-DSJETZT4.js";
7
+ import "./chunk-BFVBHIHV.js";
8
+ import "./chunk-7Y6TU2UA.js";
7
9
  import {
8
10
  BULK_ACTIONS,
9
11
  PREVIEW_TTLS,
10
12
  coreOf,
11
13
  keyFromHeader
12
- } from "./chunk-52NUUZC7.js";
14
+ } from "./chunk-7SCYDG2G.js";
15
+ import "./chunk-MA6VKJ3F.js";
13
16
  import {
17
+ ASSET_KINDS,
14
18
  DOC_STATUSES,
15
19
  KEY_SCOPES
16
- } from "./chunk-6K5ZTGHQ.js";
17
- import "./chunk-OQ55GEF3.js";
20
+ } from "./chunk-PEQY4N73.js";
21
+ import "./chunk-3OPKTBEI.js";
18
22
  import "./chunk-AKN7SUXU.js";
19
23
  import {
20
24
  CmsError,
21
25
  formatMessage,
22
26
  isCmsError
23
- } from "./chunk-VICZ76TR.js";
24
- import "./chunk-DOADAH4L.js";
25
- import "./chunk-CRDW6Q72.js";
27
+ } from "./chunk-KAO3Z3CF.js";
28
+ import "./chunk-PGV2QPIL.js";
29
+ import "./chunk-6PB4ZCHH.js";
26
30
  import "./chunk-2NTK7H4W.js";
27
31
 
28
32
  // src/http.ts
33
+ import { timingSafeEqual } from "crypto";
29
34
  import { z } from "zod";
30
35
  var DocInput = z.object({
31
36
  slug: z.string().optional(),
@@ -75,7 +80,9 @@ async function resolveActor(cms, req, resolveUser) {
75
80
  const origin = req.headers.get("origin");
76
81
  if (site === "cross-site" || origin && new URL(req.url).host !== safeHost(origin)) throw new CmsError("cross_site", {}, { messages: core.messages });
77
82
  }
78
- return user ?? { kind: "anonymous" };
83
+ if (!user) return { kind: "anonymous" };
84
+ const { agent: _agent, ...person } = user;
85
+ return person;
79
86
  }
80
87
  function safeHost(origin) {
81
88
  try {
@@ -123,6 +130,13 @@ function createCmsHandler(opts) {
123
130
  throw new CmsError("invalid_request", { issues: "path encoding" }, { status: 400 });
124
131
  }
125
132
  const m = req.method.toUpperCase();
133
+ if (seg[0] === "_cron" && seg.length === 1 && (m === "GET" || m === "POST")) {
134
+ if (!core.cronSecret) throw new CmsError("route_not_found");
135
+ const given = Buffer.from((req.headers.get("authorization") ?? "").replace(/^Bearer\s+/i, ""));
136
+ const want = Buffer.from(core.cronSecret);
137
+ if (given.length !== want.length || !timingSafeEqual(given, want)) throw new CmsError("unauthorized", {}, { messages: core.messages });
138
+ return json(await core.runScheduled());
139
+ }
126
140
  const client = req.headers.get("x-agent-cms-client");
127
141
  const who = await resolveActor(core, req, opts.resolveUser);
128
142
  const actor = { ...who, via: seg[0] === "mcp" ? client === "cli" ? "cli" : "mcp" : client === "cli" ? "cli" : "rest" };
@@ -138,6 +152,9 @@ function createCmsHandler(opts) {
138
152
  actor: actor.kind === "anonymous" ? { kind: "anonymous" } : { kind: actor.kind, label: actor.label, id: "id" in actor ? actor.id : null },
139
153
  isAdmin: core.isAdmin(actor),
140
154
  previewLinks: core.previewEnabled(),
155
+ trashDays: core.trash.days,
156
+ timeZone: core.timeZone,
157
+ media: core.assets.describe(actor),
141
158
  collections: core.collections().filter((c) => core.can(actor, c.slug, "read")).map((c) => ({ slug: c.slug, label: c.label, visibility: c.visibility, canWrite: core.can(actor, c.slug, "write"), canPublish: core.can(actor, c.slug, "publish") }))
142
159
  });
143
160
  }
@@ -155,6 +172,46 @@ function createCmsHandler(opts) {
155
172
  });
156
173
  }
157
174
  if (seg[0] === "preview" && seg.length === 1 && m === "GET") return json(await core.readPreview(q.get("token") ?? ""));
175
+ const CommentBody = z.object({ body: z.string().max(8e3), anchor: z.string().max(300).nullable().optional(), quote: z.string().max(2e3).nullable().optional(), threadId: z.string().max(60).nullable().optional() });
176
+ if (seg[0] === "preview" && seg[1] === "comments" && seg.length === 2) {
177
+ const token = q.get("token") ?? "";
178
+ if (m === "GET") return json({ comments: await core.previewComments(token) });
179
+ if (m === "POST") return json({ comment: await core.addPreviewComment(token, await body(req, CommentBody.extend({ name: z.string().max(120).nullable().optional() }))) }, 201);
180
+ }
181
+ if (seg[0] === "reviews" && seg.length === 1 && m === "GET") return json({ requests: await core.reviewRequests(actor, { collection: q.get("collection") ?? void 0 }) });
182
+ if (seg[0] === "trash") {
183
+ const Items = z.array(z.object({ kind: z.enum(["document", "asset"]), id: z.string().max(60) })).min(1).max(200);
184
+ const kind = q.get("kind");
185
+ if (seg.length === 1 && m === "GET") return json(await core.trash.list(actor, { kind: kind === "document" || kind === "asset" ? kind : void 0 }));
186
+ if (seg[1] === "_restore" && seg.length === 2 && m === "POST") return json(await core.trash.restore(actor, (await body(req, z.object({ items: Items }))).items));
187
+ if (seg[1] === "_purge" && seg.length === 2 && m === "POST") {
188
+ const b = await body(req, z.object({ items: Items.optional(), all: z.boolean().optional() }));
189
+ if (!b.all && !b.items) throw new CmsError("invalid_request", { issues: "items or all" }, { messages: core.messages });
190
+ return json(await core.trash.purge(actor, b.all ? "all" : b.items));
191
+ }
192
+ }
193
+ if (seg[0] === "releases") {
194
+ const rel = core.releases;
195
+ const Name = z.object({ name: z.string().max(200) });
196
+ if (seg.length === 1 && m === "GET") return json({ releases: await rel.list(actor) });
197
+ if (seg.length === 1 && m === "POST") return json({ release: await rel.create(actor, await body(req, Name)) }, 201);
198
+ const id = seg[1];
199
+ if (id && seg.length === 2) {
200
+ if (m === "GET") return json({ release: await rel.get(actor, id) });
201
+ if (m === "PATCH") return json({ release: await rel.rename(actor, id, await body(req, Name)) });
202
+ if (m === "DELETE") {
203
+ await rel.remove(actor, id);
204
+ return json({ ok: true });
205
+ }
206
+ }
207
+ if (id && seg[2] === "items" && seg.length === 3 && m === "POST") {
208
+ const b = await body(req, z.object({ items: z.array(z.object({ collection: z.string(), slug: z.string(), action: z.enum(["publish", "unpublish"]).optional() })).min(1).max(200) }));
209
+ return json({ release: await rel.add(actor, id, b.items) });
210
+ }
211
+ if (id && seg[2] === "items" && seg.length === 4 && m === "DELETE") return json({ release: await rel.removeItem(actor, id, seg[3]) });
212
+ if (id && seg[2] === "publish" && seg.length === 3 && m === "POST") return json({ release: await rel.publish(actor, id) });
213
+ if (id && seg[2] === "schedule" && seg.length === 3 && m === "POST") return json({ release: await rel.schedule(actor, id, await body(req, z.object({ publishAt: z.string().max(40).nullable() }))) });
214
+ }
158
215
  if ((seg[0] === "audit" || seg[0] === "_audit") && seg.length === 1 && m === "GET") {
159
216
  const outcome = q.get("outcome");
160
217
  return json(
@@ -178,6 +235,96 @@ function createCmsHandler(opts) {
178
235
  return json({ ok: true });
179
236
  }
180
237
  }
238
+ if (seg[0] === "files" && seg[1] && seg.length <= 3 && (m === "GET" || m === "HEAD")) {
239
+ const to = await core.assets.open(actor, seg[1], { thumb: q.get("thumb") === "1", download: q.get("download") === "1" });
240
+ return new Response(null, { status: 302, headers: { location: to.url, "cache-control": to.visibility === "public" && q.get("download") !== "1" ? "public, max-age=300" : "private, no-store" } });
241
+ }
242
+ if (seg[0] === "stores" && seg[1] && seg[2] === "files") {
243
+ const assets = core.assets;
244
+ const store = seg[1];
245
+ const pick = z.object({ keys: z.array(z.string().max(1024)).max(1e3).optional(), folders: z.array(z.string().max(1024)).max(1e3).optional() });
246
+ if (seg.length === 3 && m === "GET") return json(await assets.browse(actor, { store, prefix: q.get("prefix"), q: q.get("q"), cursor: q.get("cursor"), limit: Number(q.get("limit") ?? 200) || 200 }));
247
+ if (seg[3] === "_open" && seg.length === 4 && (m === "GET" || m === "HEAD")) {
248
+ const to = await assets.openKey(actor, { store, key: q.get("key") ?? "", download: q.get("download") === "1" });
249
+ return new Response(null, { status: 302, headers: { location: to.url, "cache-control": "private, no-store" } });
250
+ }
251
+ if (seg[3] === "_register" && seg.length === 4 && m === "POST") {
252
+ const b = await body(req, z.object({ key: z.string().min(1).max(1024), alt: z.string().max(500).optional(), tags: z.array(z.string().max(40)).max(30).optional() }));
253
+ return json({ asset: await assets.register(actor, { store, ...b }) }, 201);
254
+ }
255
+ if (seg[3] === "_move" && seg.length === 4 && m === "POST") return json(await assets.moveMany(actor, { store, ...await body(req, pick.extend({ to: z.string().max(1024).nullable() })) }));
256
+ if (seg[3] === "_delete" && seg.length === 4 && m === "POST") return json(await assets.removeMany(actor, { store, ...await body(req, pick.extend({ confirm: z.boolean().optional(), check: z.boolean().optional() })) }));
257
+ }
258
+ if (seg[0] === "assets") {
259
+ const assets = core.assets;
260
+ const tags = z.array(z.string().max(40)).max(30).optional();
261
+ if (seg.length === 1 && m === "GET") {
262
+ const kinds = (q.get("kind") ?? "").split(",").filter((k) => ASSET_KINDS.includes(k) && k !== "folder");
263
+ return json(await assets.list(actor, { store: q.get("store"), folder: q.get("folder"), kinds, q: q.get("q") ?? void 0, tag: q.get("tag") ?? void 0, cursor: q.get("cursor"), limit: Number(q.get("limit") ?? 48) || 48 }));
264
+ }
265
+ if (seg[1] === "_folders" && seg.length === 2) {
266
+ if (m === "GET") return json({ stores: await assets.folders(actor) });
267
+ if (m === "POST") return json(await assets.createFolder(actor, await body(req, z.object({ store: z.string().nullable().optional(), path: z.string().min(1).max(400) }))), 201);
268
+ if (m === "DELETE") {
269
+ await assets.deleteFolder(actor, { store: q.get("store"), path: q.get("path") ?? "" });
270
+ return json({ ok: true });
271
+ }
272
+ }
273
+ if (seg[1] === "_import" && seg.length === 2 && m === "POST") {
274
+ const b = await body(req, z.object({ url: z.string().max(2e3), name: z.string().max(300).nullable().optional(), folder: z.string().max(400).nullable().optional(), store: z.string().nullable().optional(), alt: z.string().max(500).optional(), tags }));
275
+ return json({ asset: await assets.import(actor, b) }, 201);
276
+ }
277
+ if (seg[1] === "uploads") {
278
+ if (seg.length === 2 && m === "POST") {
279
+ const b = await body(
280
+ req,
281
+ z.object({
282
+ name: z.string().min(1).max(300),
283
+ mime: z.string().max(200).nullable().optional(),
284
+ size: z.number().int().positive(),
285
+ folder: z.string().max(400).nullable().optional(),
286
+ store: z.string().nullable().optional(),
287
+ alt: z.string().max(500).optional(),
288
+ tags,
289
+ field: z.object({ collection: z.string(), path: z.string() }).nullable().optional()
290
+ })
291
+ );
292
+ return json(await assets.requestUpload(actor, b), 201);
293
+ }
294
+ const id2 = seg[2];
295
+ if (id2 && seg.length === 3 && m === "GET") return json(await assets.uploadStatus(actor, id2));
296
+ if (id2 && seg.length === 3 && m === "DELETE") {
297
+ await assets.abortUpload(actor, id2);
298
+ return json({ ok: true });
299
+ }
300
+ if (id2 && seg[3] === "parts" && seg.length === 4 && m === "POST") {
301
+ const b = await body(req, z.object({ parts: z.array(z.number().int().positive()).min(1).max(200) }));
302
+ return json({ parts: await assets.partUrls(actor, id2, b.parts) });
303
+ }
304
+ if (id2 && seg[3] === "complete" && seg.length === 4 && m === "POST") {
305
+ const n = z.number().nonnegative().nullable().optional();
306
+ return json({ asset: await assets.completeUpload(actor, id2, await body(req, z.object({ width: n, height: n, duration: n }), true)) });
307
+ }
308
+ }
309
+ const id = seg[1];
310
+ if (id && !id.startsWith("_") && id !== "uploads") {
311
+ if (seg.length === 2 && m === "GET") return json({ asset: await assets.get(actor, id) });
312
+ if (seg.length === 2 && m === "PATCH") return json({ asset: await assets.update(actor, id, await body(req, z.object({ alt: z.string().max(500).optional(), tags }))) });
313
+ if (seg.length === 2 && m === "DELETE") {
314
+ await assets.remove(actor, id, { confirm: q.get("confirm") === "1" });
315
+ return json({ ok: true });
316
+ }
317
+ if (seg[2] === "move" && seg.length === 3 && m === "POST") {
318
+ return json({ asset: await assets.move(actor, id, await body(req, z.object({ folder: z.string().max(400).nullable().optional(), name: z.string().max(300).nullable().optional() }))) });
319
+ }
320
+ if (seg[2] === "share" && seg.length === 3 && m === "POST") return json(await assets.share(actor, id, await body(req, z.object({ ttl: z.enum(["1h", "24h", "7d"]).optional() }), true)));
321
+ if (seg[2] === "thumb" && seg.length === 3 && m === "POST") {
322
+ const bytes = new Uint8Array(await req.arrayBuffer());
323
+ if (bytes.length > MAX_BODY_BYTES) throw new CmsError("body_too_large", {}, { status: 413 });
324
+ return json({ asset: await assets.setThumb(actor, id, bytes) });
325
+ }
326
+ }
327
+ }
181
328
  if (seg[0] === "collections" && seg[1]) {
182
329
  const col = seg[1];
183
330
  const cfg = core.collection(col);
@@ -192,6 +339,9 @@ function createCmsHandler(opts) {
192
339
  hasUrl: !!cfg.url,
193
340
  fields: cfg.fields ? toJsonSchema(cfg.fields) : null,
194
341
  admin: cfg.admin?.fields ?? {},
342
+ requireAlt: !!cfg.media?.requireAlt,
343
+ review: cfg.review ?? null,
344
+ needsReview: core.needsReview(actor, col),
195
345
  forbiddenChars: cfg.forbiddenChars ?? [],
196
346
  canWrite: core.can(actor, col, "write"),
197
347
  canPublish: core.can(actor, col, "publish")
@@ -201,6 +351,19 @@ function createCmsHandler(opts) {
201
351
  const b = await body(req, z.object({ doc: DocInput, slug: z.string().optional() }));
202
352
  return json({ diagnostics: await core.diagnose(actor, col, b.doc, { slug: b.slug }) });
203
353
  }
354
+ if (seg.length === 3 && seg[2] === "_preview" && m === "POST") {
355
+ const b = await body(req, z.object({ doc: DocInput, slug: z.string().optional() }));
356
+ return json(await core.preview(actor, col, b.doc, { slug: b.slug }));
357
+ }
358
+ if (seg.length === 3 && seg[2] === "_presence" && m === "GET") return json({ presence: await core.presence.in(actor, col) });
359
+ if (seg[2] === "_filters") {
360
+ if (seg.length === 3 && m === "GET") return json({ filters: await core.filters.list(actor, col) });
361
+ if (seg.length === 3 && m === "POST") return json({ filter: await core.filters.save(actor, col, await body(req, z.object({ name: z.string().max(200), query: z.record(z.unknown()), shared: z.boolean().optional() }))) }, 201);
362
+ if (seg.length === 4 && m === "DELETE") {
363
+ await core.filters.remove(actor, seg[3]);
364
+ return json({ ok: true });
365
+ }
366
+ }
204
367
  if (seg.length === 3 && seg[2] === "_bulk" && m === "POST") {
205
368
  const b = await body(req, z.object({ action: z.enum(BULK_ACTIONS), items: z.array(z.object({ slug: z.string(), expectedVersion: z.number().int().positive().optional() })).min(1).max(200) }));
206
369
  return json({ results: await core.bulk(actor, col, b.action, b.items) });
@@ -213,7 +376,7 @@ function createCmsHandler(opts) {
213
376
  const lo = {
214
377
  drafts: q.get("drafts") === "1" || q.get("fields") === "summary",
215
378
  status: DOC_STATUSES.includes(status ?? "") ? status : void 0,
216
- state: ["draft", "published", "modified", "archived"].find((x) => x === state),
379
+ state: ["draft", "published", "modified", "archived", "scheduled", "review"].find((x) => x === state),
217
380
  by: ["user", "key", "system"].find((x) => x === by),
218
381
  category: q.get("category") ?? void 0,
219
382
  q: q.get("q") ?? void 0
@@ -237,7 +400,8 @@ function createCmsHandler(opts) {
237
400
  const doc = await core.get(actor, col, slug, { draft: q.get("draft") === "1" });
238
401
  const tag = etag(doc);
239
402
  if (req.headers.get("if-none-match") === tag) return new Response(null, { status: 304, headers: { etag: tag } });
240
- return json({ doc: wire(doc) }, 200, { etag: tag });
403
+ const release = q.get("draft") === "1" && core.can(actor, col, "write", doc) ? await core.releases.of(doc.id) : void 0;
404
+ return json({ doc: wire(doc), release }, 200, { etag: tag });
241
405
  }
242
406
  if (m === "PATCH") {
243
407
  const b = await body(req, WriteBody);
@@ -278,9 +442,25 @@ function createCmsHandler(opts) {
278
442
  return json(await core.reviewChanges(actor, col, slug, b));
279
443
  }
280
444
  if (action === "preview" && m === "POST") {
281
- const b = await body(req, z.object({ ttl: z.enum(Object.keys(PREVIEW_TTLS)).optional() }), true);
445
+ const b = await body(req, z.object({ ttl: z.enum(Object.keys(PREVIEW_TTLS)).optional(), comments: z.boolean().optional() }), true);
282
446
  return json(await core.createPreviewLink(actor, col, slug, b), 201);
283
447
  }
448
+ if (action === "schedule" && m === "POST") {
449
+ const b = await body(req, z.object({ publishAt: z.string().max(40).nullable().optional(), unpublishAt: z.string().max(40).nullable().optional(), expectedVersion: z.number().int().positive().optional() }));
450
+ const doc = await core.schedule(actor, col, slug, { ...b, expectedVersion: expected(b.expectedVersion) });
451
+ return json({ doc: wire(doc) }, 200, { etag: etag(doc) });
452
+ }
453
+ if (action === "schedule" && m === "DELETE") return json({ doc: wire(await core.unschedule(actor, col, slug)) });
454
+ if (action === "comments" && m === "GET") return json({ comments: await core.comments(actor, col, slug) });
455
+ if (action === "comments" && m === "POST") return json({ comment: await core.addComment(actor, col, slug, await body(req, CommentBody)) }, 201);
456
+ if (action === "presence" && m === "POST") {
457
+ const at = z.object({ line: z.number().int(), ch: z.number().int() }).nullable().optional();
458
+ return json(await core.presence.touch(actor, col, slug, await body(req, z.object({ cursor: at, selection: at }), true)));
459
+ }
460
+ if (action === "presence" && m === "DELETE") {
461
+ await core.presence.leave(actor, col, slug);
462
+ return json({ ok: true });
463
+ }
284
464
  if (action === "diff" && m === "GET") {
285
465
  const a = Number(q.get("a"));
286
466
  const b = Number(q.get("b"));
@@ -294,11 +474,21 @@ function createCmsHandler(opts) {
294
474
  return json({ doc: wire(doc) }, 200, { etag: etag(doc) });
295
475
  }
296
476
  }
477
+ if (slug && seg[3] === "review" && seg.length === 5 && m === "POST") {
478
+ const b = await body(req, z.object({ note: z.string().max(2e3).nullable().optional(), publish: z.boolean().optional() }), true);
479
+ const doc = seg[4] === "request" ? await core.requestReview(actor, col, slug, b) : seg[4] === "approve" ? await core.approveReview(actor, col, slug, b) : seg[4] === "reject" ? await core.rejectReview(actor, col, slug, b) : null;
480
+ if (doc) return json({ doc: wire(doc) }, 200, { etag: etag(doc) });
481
+ }
482
+ if (slug && seg[3] === "comments" && seg[5] === "resolve" && seg.length === 6 && m === "POST") {
483
+ const b = await body(req, z.object({ resolved: z.boolean().optional() }), true);
484
+ return json({ comment: await core.resolveComment(actor, col, slug, seg[4], b) });
485
+ }
297
486
  if (slug && seg[3] === "versions") {
298
487
  if (seg.length === 4 && m === "GET") return json({ versions: q.get("fields") === "summary" ? await core.versionList(actor, col, slug) : await core.versions(actor, col, slug) });
299
488
  const n = Number(seg[4]);
300
489
  if (!Number.isInteger(n) || n < 1) throw new CmsError("invalid_version", {}, { messages: core.messages });
301
490
  if (seg.length === 5 && m === "GET") return json({ version: await core.version(actor, col, slug, n) });
491
+ if (seg.length === 5 && m === "PATCH") return json({ version: await core.labelVersion(actor, col, slug, n, await body(req, z.object({ label: z.string().max(200).nullable().optional(), pinned: z.boolean().optional() }))) });
302
492
  if (seg.length === 6 && seg[5] === "restore" && m === "POST") {
303
493
  const b = await body(req, Expected.extend({ publish: z.boolean().optional() }), true);
304
494
  const doc = await core.restore(actor, col, slug, n, { ...b, expectedVersion: expected(b.expectedVersion) });