aicms 0.0.0-stage → 0.5.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.
- package/ARCHITECTURE.md +467 -0
- package/LICENSE +21 -0
- package/README.md +200 -2
- package/README.uk.md +201 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +722 -0
- package/dist/config-edit.d.ts +18 -0
- package/dist/config-edit.js +201 -0
- package/dist/config.d.ts +55 -0
- package/dist/config.js +32 -0
- package/dist/edge.d.ts +22 -0
- package/dist/edge.js +56 -0
- package/dist/gates/a2a.d.ts +45 -0
- package/dist/gates/a2a.js +126 -0
- package/dist/gates/discovery.d.ts +36 -0
- package/dist/gates/discovery.js +54 -0
- package/dist/gates/events.d.ts +14 -0
- package/dist/gates/events.js +86 -0
- package/dist/gates/graphql.d.ts +41 -0
- package/dist/gates/graphql.js +226 -0
- package/dist/gates/mcp.d.ts +8 -0
- package/dist/gates/mcp.js +60 -0
- package/dist/gates/openapi.d.ts +56 -0
- package/dist/gates/openapi.js +61 -0
- package/dist/gates/rest.d.ts +21 -0
- package/dist/gates/rest.js +263 -0
- package/dist/gates/studio.d.ts +21 -0
- package/dist/gates/studio.js +107 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +19 -0
- package/dist/kernel/action.d.ts +49 -0
- package/dist/kernel/action.js +22 -0
- package/dist/kernel/briefs.d.ts +161 -0
- package/dist/kernel/briefs.js +197 -0
- package/dist/kernel/changesets.d.ts +367 -0
- package/dist/kernel/changesets.js +765 -0
- package/dist/kernel/cms.d.ts +127 -0
- package/dist/kernel/cms.js +419 -0
- package/dist/kernel/core-actions.d.ts +5 -0
- package/dist/kernel/core-actions.js +487 -0
- package/dist/kernel/errors.d.ts +14 -0
- package/dist/kernel/errors.js +25 -0
- package/dist/kernel/install.d.ts +32 -0
- package/dist/kernel/install.js +34 -0
- package/dist/kernel/model-actions.d.ts +5 -0
- package/dist/kernel/model-actions.js +126 -0
- package/dist/kernel/modules.d.ts +221 -0
- package/dist/kernel/modules.js +207 -0
- package/dist/kernel/policy.d.ts +42 -0
- package/dist/kernel/policy.js +47 -0
- package/dist/load.d.ts +10 -0
- package/dist/load.js +63 -0
- package/dist/modules/catalog.d.ts +15 -0
- package/dist/modules/catalog.js +21 -0
- package/dist/modules/commerce.d.ts +67 -0
- package/dist/modules/commerce.js +260 -0
- package/dist/modules/git-sync.d.ts +20 -0
- package/dist/modules/git-sync.js +187 -0
- package/dist/modules/i18n.d.ts +6 -0
- package/dist/modules/i18n.js +168 -0
- package/dist/modules/index.d.ts +9 -0
- package/dist/modules/index.js +10 -0
- package/dist/modules/lint.d.ts +22 -0
- package/dist/modules/lint.js +126 -0
- package/dist/modules/media.d.ts +52 -0
- package/dist/modules/media.js +319 -0
- package/dist/modules/payments.d.ts +19 -0
- package/dist/modules/payments.js +110 -0
- package/dist/modules/search.d.ts +27 -0
- package/dist/modules/search.js +81 -0
- package/dist/modules/seo.d.ts +34 -0
- package/dist/modules/seo.js +189 -0
- package/dist/modules/text.d.ts +10 -0
- package/dist/modules/text.js +42 -0
- package/dist/node.d.ts +7 -0
- package/dist/node.js +11 -0
- package/dist/registry.d.ts +57 -0
- package/dist/registry.js +110 -0
- package/dist/schema/fields.d.ts +100 -0
- package/dist/schema/fields.js +134 -0
- package/dist/schema/markdown.d.ts +7 -0
- package/dist/schema/markdown.js +168 -0
- package/dist/schema/model.d.ts +44 -0
- package/dist/schema/model.js +104 -0
- package/dist/schema/slug.d.ts +2 -0
- package/dist/schema/slug.js +40 -0
- package/dist/store/d1.d.ts +30 -0
- package/dist/store/d1.js +27 -0
- package/dist/store/postgres.d.ts +164 -0
- package/dist/store/postgres.js +552 -0
- package/dist/store/sql.d.ts +135 -0
- package/dist/store/sql.js +530 -0
- package/dist/store/sqlite.d.ts +14 -0
- package/dist/store/sqlite.js +60 -0
- package/dist/store/types.d.ts +226 -0
- package/dist/store/types.js +21 -0
- package/dist/studio/assets/geologica-cyrillic-ext-wght-normal-C8B9Chlh.woff2 +0 -0
- package/dist/studio/assets/geologica-cyrillic-wght-normal-DbeVG3Ps.woff2 +0 -0
- package/dist/studio/assets/geologica-greek-wght-normal-DPM5d9ub.woff2 +0 -0
- package/dist/studio/assets/geologica-latin-ext-wght-normal-DZyYlgcm.woff2 +0 -0
- package/dist/studio/assets/geologica-latin-wght-normal-BBL8hQxZ.woff2 +0 -0
- package/dist/studio/assets/geologica-vietnamese-wght-normal-DcI17N1q.woff2 +0 -0
- package/dist/studio/assets/index-3dJ0YHxb.js +22 -0
- package/dist/studio/assets/index-DuyGLFw4.css +1 -0
- package/dist/studio/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/studio/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/studio/assets/unbounded-cyrillic-wght-normal-C627f1f8.woff2 +0 -0
- package/dist/studio/assets/unbounded-latin-ext-wght-normal-D6H-S6MW.woff2 +0 -0
- package/dist/studio/assets/unbounded-latin-wght-normal-D5afBTBx.woff2 +0 -0
- package/dist/studio/assets/unbounded-vietnamese-wght-normal-rat7GD3o.woff2 +0 -0
- package/dist/studio/index.html +21 -0
- package/dist/typegen.d.ts +7 -0
- package/dist/typegen.js +55 -0
- package/package.json +88 -4
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { typeLabel } from "../schema/fields.js";
|
|
3
|
+
import { displayTitle, filterableFields, projectData, uniqueFields } from "../schema/model.js";
|
|
4
|
+
import { checkDrafts, diffChangeset, discardDrafts, entryHistory, entryVersion, mergeChangeset, proposeModule, rejectChangeset, restoreEntry, revertChangeset, submitChangeset, summarize, unscheduleChangeset, } from "./changesets.js";
|
|
5
|
+
import { AGENT_BRIEF_STATUSES, BRIEF_STATUSES, briefForSubmit, briefWriter, getBrief, linkSubmission, listBriefs, updateBrief, writeBrief, } from "./briefs.js";
|
|
6
|
+
import { AicmsError } from "./errors.js";
|
|
7
|
+
import { catalog } from "../modules/catalog.js";
|
|
8
|
+
import { installFromCatalog } from "./install.js";
|
|
9
|
+
import { loadRegistry } from "../registry.js";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { moduleManifest } from "./modules.js";
|
|
12
|
+
import { allows, describeReview, modelPermissions } from "./policy.js";
|
|
13
|
+
const signedIn = (a) => a.kind !== "public";
|
|
14
|
+
const reviewer = (a) => allows(a.scopes, "changesets:review");
|
|
15
|
+
const manager = (a) => allows(a.scopes, "agents:manage");
|
|
16
|
+
const auditor = (a) => allows(a.scopes, "events:read");
|
|
17
|
+
const installer = (a) => allows(a.scopes, "modules:install");
|
|
18
|
+
/** A field as clients need it to render a form or write valid input. */
|
|
19
|
+
export function describeField(name, f, writable) {
|
|
20
|
+
const o = f.options;
|
|
21
|
+
return {
|
|
22
|
+
name,
|
|
23
|
+
kind: f.kind,
|
|
24
|
+
type: typeLabel(f),
|
|
25
|
+
...(o.required ? { required: true } : {}),
|
|
26
|
+
...(o.unique ? { unique: true } : {}),
|
|
27
|
+
...(o.label ? { label: o.label } : {}),
|
|
28
|
+
...(o.description ? { description: o.description } : {}),
|
|
29
|
+
...(f.kind === "slug" && o.from ? { filledFrom: o.from } : {}),
|
|
30
|
+
...(o.min !== undefined ? { min: o.min } : {}),
|
|
31
|
+
...(o.max !== undefined ? { max: o.max } : {}),
|
|
32
|
+
...(o.multiline ? { multiline: true } : {}),
|
|
33
|
+
...(f.kind === "enum" ? { values: o.values } : {}),
|
|
34
|
+
...(f.kind === "ref" ? { model: o.model, ...(o.many ? { many: true } : {}) } : {}),
|
|
35
|
+
...(f.kind === "blocks" && o.types ? { blocks: o.types } : {}),
|
|
36
|
+
...(f.kind === "object" ? { fields: Object.entries(o.fields).map(([k, sub]) => describeField(k, sub)) } : {}),
|
|
37
|
+
...(writable !== undefined ? { writable } : {}),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function abilities(actor, m) {
|
|
41
|
+
const p = modelPermissions(actor, m);
|
|
42
|
+
const out = [];
|
|
43
|
+
if (p.read)
|
|
44
|
+
out.push("read");
|
|
45
|
+
if (p.create)
|
|
46
|
+
out.push("create");
|
|
47
|
+
if (p.updateFields.length === Object.keys(m.fields).length)
|
|
48
|
+
out.push("update");
|
|
49
|
+
else if (p.updateFields.length)
|
|
50
|
+
out.push(`update(${p.updateFields.join(", ")})`);
|
|
51
|
+
if (p.delete)
|
|
52
|
+
out.push("delete");
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
async function resolveExamples(k, m) {
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const ex of (m.agent?.examples ?? []).slice(0, 3)) {
|
|
58
|
+
const sep = ex.indexOf(":");
|
|
59
|
+
const entry = sep > 0 ? await k.store.findOne(m.name, ex.slice(0, sep), ex.slice(sep + 1)) : await k.store.getEntry(ex);
|
|
60
|
+
if (entry && entry.model === m.name)
|
|
61
|
+
out.push({ id: entry.id, ...projectData(m, entry.data, "markdown") });
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
export function coreActions() {
|
|
66
|
+
return [
|
|
67
|
+
{
|
|
68
|
+
name: "cms.describe",
|
|
69
|
+
description: "Start here. Lists the models you can work with, their fields and writing guides, your permissions and how publishing works. Pass { model } for full field details and examples.",
|
|
70
|
+
readOnly: true,
|
|
71
|
+
visible: () => true,
|
|
72
|
+
schema: () => z.strictObject({ model: z.string().optional().describe("Model name for full details") }),
|
|
73
|
+
async run(input, { actor, kernel: k }) {
|
|
74
|
+
const readable = [...k.models.values()].filter((m) => modelPermissions(actor, m).read);
|
|
75
|
+
if (input.model) {
|
|
76
|
+
const m = readable.find((x) => x.name === input.model);
|
|
77
|
+
if (!m)
|
|
78
|
+
throw new AicmsError("not_found", `No readable model "${input.model}". Available: ${readable.map((x) => x.name).join(", ") || "none"}`);
|
|
79
|
+
const p = modelPermissions(actor, m);
|
|
80
|
+
return {
|
|
81
|
+
name: m.name,
|
|
82
|
+
label: m.label,
|
|
83
|
+
description: m.description,
|
|
84
|
+
guide: m.agent?.guide,
|
|
85
|
+
youCan: abilities(actor, m),
|
|
86
|
+
humanPublishes: !!m.agent?.can && !m.agent.can.includes("publish"),
|
|
87
|
+
fields: Object.entries(m.fields).map(([key, f]) => describeField(key, f, p.create || p.updateFields.includes(key))),
|
|
88
|
+
filterBy: filterableFields(m),
|
|
89
|
+
findBy: ["id", ...uniqueFields(m)],
|
|
90
|
+
examples: await resolveExamples(k, m),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const open = signedIn(actor) ? await k.store.findOpenChangeset(actor.id) : null;
|
|
94
|
+
const briefs = actor.kind === "agent" ? (await listBriefs(k, actor)).total : 0;
|
|
95
|
+
return {
|
|
96
|
+
site: k.config.name,
|
|
97
|
+
you: { name: actor.id, scopes: actor.scopes, review: actor.kind === "agent" ? describeReview(actor.review) : undefined },
|
|
98
|
+
locales: k.config.locales,
|
|
99
|
+
models: await Promise.all(readable.map(async (m) => ({
|
|
100
|
+
name: m.name,
|
|
101
|
+
...(m.label ? { label: m.label } : {}),
|
|
102
|
+
entries: (await k.store.list({ model: m.name, limit: 0, offset: 0 })).total,
|
|
103
|
+
fields: Object.entries(m.fields).map(([key, f]) => key + (f.options.required ? "*" : "")).join(", "),
|
|
104
|
+
youCan: abilities(actor, m),
|
|
105
|
+
...(m.agent?.guide ? { guide: m.agent.guide } : {}),
|
|
106
|
+
}))),
|
|
107
|
+
openChangeset: open ? { id: open.id, items: open.items.length } : null,
|
|
108
|
+
...(briefs ? { briefs: `${briefs} brief${briefs === 1 ? "" : "s"} for you: see cms_briefs` } : {}),
|
|
109
|
+
workflow: signedIn(actor)
|
|
110
|
+
? "Read with <model>_list / <model>_get. Write with <model>_draft (block fields accept Markdown). cms_check runs the checks; cms_submit sends the changes for review. Nothing is public until it is merged; cms_changes shows review decisions and comments. cms_briefs lists work people gave you."
|
|
111
|
+
: "Read-only access. Ask the site owner for an agent token (aicms agent create <name>) to propose changes.",
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "cms.search",
|
|
117
|
+
description: "Full-text search across the models you can read. Returns ids and short snippets; fetch full entries with <model>_get.",
|
|
118
|
+
readOnly: true,
|
|
119
|
+
visible: () => true,
|
|
120
|
+
schema: () => z.strictObject({
|
|
121
|
+
query: z.string().min(2),
|
|
122
|
+
model: z.string().optional(),
|
|
123
|
+
limit: z.number().int().min(1).max(50).optional().describe("Default 10"),
|
|
124
|
+
}),
|
|
125
|
+
async run(input, { actor, kernel: k }) {
|
|
126
|
+
const models = [...k.models.values()].filter((m) => modelPermissions(actor, m).read && (!input.model || m.name === input.model));
|
|
127
|
+
// A module that provides "search" (e.g. the official search module) answers with ranked results.
|
|
128
|
+
const provider = k.service("search");
|
|
129
|
+
if (provider) {
|
|
130
|
+
const hits = await provider.query({ query: input.query, models: models.map((m) => m.name), limit: input.limit ?? 10 });
|
|
131
|
+
return { query: input.query, ranked: true, hits };
|
|
132
|
+
}
|
|
133
|
+
const rows = await k.store.scan(models.map((m) => m.name), 5000);
|
|
134
|
+
const q = input.query.toLocaleLowerCase();
|
|
135
|
+
const hits = [];
|
|
136
|
+
for (const row of rows) {
|
|
137
|
+
const m = k.models.get(row.model);
|
|
138
|
+
for (const [key, f] of Object.entries(m.fields)) {
|
|
139
|
+
const raw = row.data[key];
|
|
140
|
+
if (raw == null)
|
|
141
|
+
continue;
|
|
142
|
+
const text = f.kind === "blocks" ? String(projectData(m, { [key]: raw }, "markdown")[key]) : Array.isArray(raw) ? raw.join(", ") : typeof raw === "string" ? raw : "";
|
|
143
|
+
const at = text.toLocaleLowerCase().indexOf(q);
|
|
144
|
+
if (at < 0)
|
|
145
|
+
continue;
|
|
146
|
+
const start = Math.max(0, at - 50);
|
|
147
|
+
hits.push({
|
|
148
|
+
model: row.model,
|
|
149
|
+
id: row.id,
|
|
150
|
+
title: displayTitle(m, row.data),
|
|
151
|
+
field: key,
|
|
152
|
+
snippet: (start ? "…" : "") + text.slice(start, at + q.length + 70).replace(/\s+/g, " ") + (at + q.length + 70 < text.length ? "…" : ""),
|
|
153
|
+
});
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
if (hits.length >= (input.limit ?? 10))
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
return { query: input.query, hits };
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name: "cms.check",
|
|
164
|
+
description: "Run every check on your drafted changes without submitting: schema, conflicts and the site's content checks. Returns errors to fix, warnings a reviewer will see, and what cms_submit would do.",
|
|
165
|
+
readOnly: true,
|
|
166
|
+
visible: signedIn,
|
|
167
|
+
schema: () => z.strictObject({}),
|
|
168
|
+
run: (_input, { actor, kernel }) => checkDrafts(kernel, actor),
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: "cms.submit",
|
|
172
|
+
description: "Run checks on your drafted changes and send them for review. If checks fail you get field-level issues to fix. Depending on your review mode the changes are merged right away or wait for a person. Pass { brief } when the changes answer a brief, and { publishAt } to ask for a publish time.",
|
|
173
|
+
visible: signedIn,
|
|
174
|
+
schema: () => z.strictObject({
|
|
175
|
+
note: z.string().max(2000).optional().describe("What you changed and why, for the reviewer"),
|
|
176
|
+
brief: z.string().optional().describe("The brief these changes answer (from cms_briefs)"),
|
|
177
|
+
publishAt: z.string().optional().describe("ISO 8601 time to publish at, e.g. 2026-12-01T09:00:00Z. A reviewer approves it as a schedule."),
|
|
178
|
+
}),
|
|
179
|
+
async run(input, { actor, kernel }) {
|
|
180
|
+
const brief = input.brief ? await briefForSubmit(kernel, actor, input.brief) : null;
|
|
181
|
+
const publishAt = input.publishAt ?? (brief?.publishAt && Date.parse(brief.publishAt) > Date.now() ? brief.publishAt : undefined);
|
|
182
|
+
const result = await submitChangeset(kernel, actor, { note: input.note, brief: brief?.id, publishAt });
|
|
183
|
+
if (brief)
|
|
184
|
+
await linkSubmission(kernel, actor, brief.id, result.changeset, result.status);
|
|
185
|
+
return result;
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: "cms.discard",
|
|
190
|
+
description: "Drop drafted changes you haven't submitted: one entry with { id }, or all of them without it.",
|
|
191
|
+
visible: signedIn,
|
|
192
|
+
schema: () => z.strictObject({ id: z.string().min(1).optional() }),
|
|
193
|
+
run: (input, { actor, kernel }) => discardDrafts(kernel, actor, input.id),
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
name: "cms.changes",
|
|
197
|
+
description: "Your recent changesets: status, failed checks and reviewer comments. Use it to learn whether your changes were merged or sent back.",
|
|
198
|
+
readOnly: true,
|
|
199
|
+
visible: signedIn,
|
|
200
|
+
schema: () => z.strictObject({ limit: z.number().int().min(1).max(50).optional() }),
|
|
201
|
+
async run(input, { actor, kernel }) {
|
|
202
|
+
const rows = await kernel.store.listChangesets({ actor: actor.id, limit: input.limit ?? 10 });
|
|
203
|
+
return { changesets: rows.map(summarize) };
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
name: "cms.review",
|
|
208
|
+
description: "For reviewers: changesets waiting for a decision, with before/after for every changed field. Pass { id } for one changeset.",
|
|
209
|
+
readOnly: true,
|
|
210
|
+
visible: reviewer,
|
|
211
|
+
schema: () => z.strictObject({
|
|
212
|
+
id: z.string().optional(),
|
|
213
|
+
status: z.enum(["submitted", "scheduled", "open", "merged", "rejected"]).optional().describe("Default: submitted"),
|
|
214
|
+
limit: z.number().int().min(1).max(50).optional(),
|
|
215
|
+
}),
|
|
216
|
+
async run(input, { kernel }) {
|
|
217
|
+
if (input.id) {
|
|
218
|
+
const cs = await kernel.store.getChangeset(input.id);
|
|
219
|
+
if (!cs)
|
|
220
|
+
throw new AicmsError("not_found", `No changeset ${input.id}`);
|
|
221
|
+
return diffChangeset(kernel, cs);
|
|
222
|
+
}
|
|
223
|
+
const rows = await kernel.store.listChangesets({ status: [input.status ?? "submitted"], limit: input.limit ?? 20 });
|
|
224
|
+
return { changesets: await Promise.all(rows.map((cs) => diffChangeset(kernel, cs))) };
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
name: "cms.merge",
|
|
229
|
+
description: "For reviewers: publish a changeset, now or at a time. Checks run again first; if something changed meanwhile, the changeset goes back to its author. Without { at }, a changeset whose author asked for a publish time is scheduled for it.",
|
|
230
|
+
visible: reviewer,
|
|
231
|
+
schema: () => z.strictObject({
|
|
232
|
+
id: z.string().min(1),
|
|
233
|
+
at: z.string().optional().describe('ISO 8601 time to publish at, or "now"'),
|
|
234
|
+
}),
|
|
235
|
+
async run(input, { actor, kernel }) {
|
|
236
|
+
const cs = await kernel.store.getChangeset(input.id);
|
|
237
|
+
if (cs?.items.some((i) => i.op === "install") && !installer(actor)) {
|
|
238
|
+
throw new AicmsError("forbidden", "Installing a module needs the modules:install scope (the site owner has it)");
|
|
239
|
+
}
|
|
240
|
+
return mergeChangeset(kernel, input.id, actor.id, { at: input.at });
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
name: "cms.unschedule",
|
|
245
|
+
description: "For reviewers: take a scheduled changeset off the schedule. It waits for review again.",
|
|
246
|
+
visible: reviewer,
|
|
247
|
+
schema: () => z.strictObject({ id: z.string().min(1) }),
|
|
248
|
+
run: (input, { actor, kernel }) => unscheduleChangeset(kernel, input.id, actor.id),
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
name: "cms.briefs",
|
|
252
|
+
description: "Work people gave you: briefs with a title, details, what to produce and when. Without filters: the ones not done yet. Pass { id } for one brief with its notes.",
|
|
253
|
+
readOnly: true,
|
|
254
|
+
visible: signedIn,
|
|
255
|
+
schema: () => z.strictObject({
|
|
256
|
+
id: z.string().optional(),
|
|
257
|
+
status: z.array(z.enum(BRIEF_STATUSES)).optional().describe("Default: everything not done or canceled"),
|
|
258
|
+
agent: z.string().optional(),
|
|
259
|
+
limit: z.number().int().min(1).max(200).optional(),
|
|
260
|
+
}),
|
|
261
|
+
async run(input, { actor, kernel }) {
|
|
262
|
+
if (input.id)
|
|
263
|
+
return { brief: await getBrief(kernel, actor, input.id) };
|
|
264
|
+
return listBriefs(kernel, actor, input);
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
name: "cms.brief",
|
|
269
|
+
description: "For people and planning agents: write a brief for an agent, or change one ({ id }). Give it a title, details in Markdown, the agent, what to produce (model or entry), a due date and a publish time.",
|
|
270
|
+
visible: briefWriter,
|
|
271
|
+
schema: () => z.strictObject({
|
|
272
|
+
id: z.string().optional().describe("Omit to create a new brief"),
|
|
273
|
+
title: z.string().max(200).optional(),
|
|
274
|
+
details: z.string().max(20000).nullable().optional(),
|
|
275
|
+
agent: z.string().nullable().optional().describe("Omit to let any agent pick it up"),
|
|
276
|
+
model: z.string().nullable().optional(),
|
|
277
|
+
entry: z.string().nullable().optional(),
|
|
278
|
+
due: z.string().nullable().optional(),
|
|
279
|
+
publishAt: z.string().nullable().optional(),
|
|
280
|
+
status: z.enum(["open", "canceled", "done"]).optional(),
|
|
281
|
+
note: z.string().max(4000).optional().describe("Added to the brief's notes, e.g. an answer to a blocked agent"),
|
|
282
|
+
}),
|
|
283
|
+
run: (input, { actor, kernel }) => writeBrief(kernel, actor, input.id, input),
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
name: "cms.brief_update",
|
|
287
|
+
description: 'Report on a brief: take it ("in_progress"), ask a person something ("blocked" with a note), give it back ("open"), or finish work that needed no content change ("done" with a note). Content changes are answered with cms_submit({ brief }).',
|
|
288
|
+
visible: signedIn,
|
|
289
|
+
schema: () => z.strictObject({
|
|
290
|
+
id: z.string().min(1),
|
|
291
|
+
status: z.enum(AGENT_BRIEF_STATUSES).optional(),
|
|
292
|
+
note: z.string().max(4000).optional(),
|
|
293
|
+
}),
|
|
294
|
+
run: (input, { actor, kernel }) => updateBrief(kernel, actor, input.id, input),
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
name: "cms.reject",
|
|
298
|
+
description: "For reviewers: send a changeset back to its author with a comment. With final: true it is closed instead.",
|
|
299
|
+
visible: reviewer,
|
|
300
|
+
schema: () => z.strictObject({
|
|
301
|
+
id: z.string().min(1),
|
|
302
|
+
comment: z.string().min(1).describe("What to change; the author sees this"),
|
|
303
|
+
final: z.boolean().optional(),
|
|
304
|
+
}),
|
|
305
|
+
run: (input, { actor, kernel }) => rejectChangeset(kernel, input.id, actor.id, input.comment, input.final),
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
name: "cms.history",
|
|
309
|
+
description: "Versions of one entry, newest first: who changed it, when, in which changeset and which fields. Pass { version } to read that version's data.",
|
|
310
|
+
readOnly: true,
|
|
311
|
+
visible: signedIn,
|
|
312
|
+
schema: () => z.strictObject({
|
|
313
|
+
id: z.string().min(1),
|
|
314
|
+
version: z.number().int().min(1).optional(),
|
|
315
|
+
format: z.enum(["markdown", "blocks"]).optional(),
|
|
316
|
+
}),
|
|
317
|
+
async run(input, { actor, gate, kernel }) {
|
|
318
|
+
if (input.version === undefined)
|
|
319
|
+
return entryHistory(kernel, actor, input.id);
|
|
320
|
+
const v = await entryVersion(kernel, actor, input.id, input.version);
|
|
321
|
+
const m = kernel.models.get(v.model);
|
|
322
|
+
const format = input.format ?? (gate === "mcp" ? "markdown" : "blocks");
|
|
323
|
+
return { ...v, data: v.data ? projectData(m, v.data, format) : null };
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
name: "cms.restore",
|
|
328
|
+
description: "Draft a change that brings an entry back to an earlier version (a deleted entry comes back with the same id). Publish it with cms_submit like any other change.",
|
|
329
|
+
visible: signedIn,
|
|
330
|
+
schema: () => z.strictObject({ id: z.string().min(1), version: z.number().int().min(1) }),
|
|
331
|
+
run: (input, { actor, kernel }) => restoreEntry(kernel, actor, input.id, input.version),
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
name: "cms.revert",
|
|
335
|
+
description: "Draft the undo of a merged changeset: entries it created are deleted, edits and deletions are rolled back. Publish it with cms_submit.",
|
|
336
|
+
visible: signedIn,
|
|
337
|
+
schema: () => z.strictObject({ changeset: z.string().min(1) }),
|
|
338
|
+
run: (input, { actor, kernel }) => revertChangeset(kernel, actor, input.changeset),
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
name: "cms.agents",
|
|
342
|
+
description: "For admins: configured agents with their scopes, review mode, budget, active tokens, recent activity and pending changesets.",
|
|
343
|
+
readOnly: true,
|
|
344
|
+
visible: manager,
|
|
345
|
+
schema: () => z.strictObject({}),
|
|
346
|
+
async run(_input, { kernel: k }) {
|
|
347
|
+
const tokens = await k.store.listTokens();
|
|
348
|
+
const stats = new Map((await k.store.actorStats(new Date(Date.now() - 3_600_000).toISOString())).map((s) => [s.actor, s]));
|
|
349
|
+
const pending = await k.store.listChangesets({ status: ["open", "submitted"], limit: 1000 });
|
|
350
|
+
return {
|
|
351
|
+
agents: Object.entries(k.config.agents).map(([name, a]) => {
|
|
352
|
+
const mine = pending.filter((cs) => cs.actor === name);
|
|
353
|
+
return {
|
|
354
|
+
name,
|
|
355
|
+
...(a.description ? { description: a.description } : {}),
|
|
356
|
+
scopes: a.scopes,
|
|
357
|
+
review: a.review ?? "human",
|
|
358
|
+
...(a.budget ? { budget: a.budget } : {}),
|
|
359
|
+
tokens: tokens.filter((t) => t.agent === name && t.kind === "agent" && !t.revokedAt).length,
|
|
360
|
+
lastSeen: stats.get(name)?.lastAt ?? null,
|
|
361
|
+
actionsLastHour: stats.get(name)?.recent ?? 0,
|
|
362
|
+
waiting: mine.filter((cs) => cs.status === "submitted").length,
|
|
363
|
+
drafting: mine.find((cs) => cs.status === "open")?.items.length ?? 0,
|
|
364
|
+
};
|
|
365
|
+
}),
|
|
366
|
+
sessions: tokens.filter((t) => t.kind === "session" && !t.revokedAt && (!t.expiresAt || t.expiresAt > new Date().toISOString())).length,
|
|
367
|
+
};
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
name: "cms.token",
|
|
372
|
+
description: "For admins: issue a new token for an agent from the config. The token is shown once.",
|
|
373
|
+
visible: manager,
|
|
374
|
+
schema: () => z.strictObject({ agent: z.string().min(1) }),
|
|
375
|
+
async run(input, { kernel: k }) {
|
|
376
|
+
if (input.agent === "owner")
|
|
377
|
+
throw new AicmsError("invalid", "Owner tokens are issued from the command line: aicms agent create owner");
|
|
378
|
+
const token = await k.createToken(input.agent);
|
|
379
|
+
const a = k.actor(input.agent);
|
|
380
|
+
return { agent: input.agent, token, scopes: a.scopes, review: a.review, next: "Store it now; it can't be shown again." };
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
name: "cms.revoke",
|
|
385
|
+
description: "For admins: revoke every token of an agent and reject its unmerged changesets.",
|
|
386
|
+
destructive: true,
|
|
387
|
+
visible: manager,
|
|
388
|
+
schema: () => z.strictObject({ agent: z.string().min(1) }),
|
|
389
|
+
async run(input, { kernel: k }) {
|
|
390
|
+
if (!(input.agent in k.config.agents))
|
|
391
|
+
throw new AicmsError("not_found", `No agent "${input.agent}"`);
|
|
392
|
+
return { agent: input.agent, ...(await k.revokeAgent(input.agent)) };
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
name: "cms.log",
|
|
397
|
+
description: "For admins: the action log, newest first. Filter by actor; page back with { before: <id of the oldest event you have> }.",
|
|
398
|
+
readOnly: true,
|
|
399
|
+
visible: auditor,
|
|
400
|
+
schema: () => z.strictObject({
|
|
401
|
+
actor: z.string().optional(),
|
|
402
|
+
limit: z.number().int().min(1).max(200).optional().describe("Default 50"),
|
|
403
|
+
before: z.number().int().min(1).optional(),
|
|
404
|
+
}),
|
|
405
|
+
async run(input, { kernel }) {
|
|
406
|
+
return { events: await kernel.store.listEvents({ actor: input.actor, limit: input.limit ?? 50, before: input.before }) };
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
{
|
|
410
|
+
name: "cms.presence",
|
|
411
|
+
description: "Who is looking at or editing an entry right now, plus unpublished drafts of it (yours or others', e.g. waiting for review). Pass state to announce yourself: viewing, editing or left.",
|
|
412
|
+
visible: signedIn,
|
|
413
|
+
schema: () => z.strictObject({ id: z.string().min(1), state: z.enum(["viewing", "editing", "left"]).optional() }),
|
|
414
|
+
async run(input, { actor, kernel: k }) {
|
|
415
|
+
const entry = await k.store.getEntry(input.id);
|
|
416
|
+
const model = entry ? k.models.get(entry.model) : undefined;
|
|
417
|
+
if (!entry || !model || !modelPermissions(actor, model).read)
|
|
418
|
+
throw new AicmsError("not_found", `No entry ${input.id}`);
|
|
419
|
+
if (input.state === "left")
|
|
420
|
+
k.presence.leave(actor, input.id);
|
|
421
|
+
else if (input.state)
|
|
422
|
+
k.presence.touch(actor, input.id, model.name, input.state);
|
|
423
|
+
const drafts = [];
|
|
424
|
+
for (const cs of await k.store.listChangesets({ status: ["open", "submitted", "scheduled"], limit: 1000 })) {
|
|
425
|
+
const item = cs.items.find((i) => i.id === input.id && i.op !== "install");
|
|
426
|
+
const status = cs.status === "submitted" ? "waiting for review" : cs.status === "scheduled" ? "scheduled" : "drafting";
|
|
427
|
+
if (item)
|
|
428
|
+
drafts.push({ actor: cs.actor, changeset: cs.id, status, ...(cs.status === "scheduled" ? { publishAt: cs.publishAt } : {}), op: item.op, fields: Object.keys(item.data ?? {}), mine: cs.actor === actor.id });
|
|
429
|
+
}
|
|
430
|
+
return { id: input.id, model: model.name, version: entry.version, here: k.presence.list(input.id).filter((p) => p.actor !== actor.id), drafts };
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
name: "cms.login_code",
|
|
435
|
+
description: "For the owner: a one-time Studio sign-in code (15 minutes), for servers without a terminal such as the edge. Open /studio/login?code=<code>.",
|
|
436
|
+
visible: (a) => allows(a.scopes, "studio:login"),
|
|
437
|
+
schema: () => z.strictObject({}),
|
|
438
|
+
async run(_input, { kernel }) {
|
|
439
|
+
const code = await kernel.createLoginCode();
|
|
440
|
+
return { code, path: `/studio/login?code=${code}`, expiresInMinutes: 15 };
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
name: "cms.modules",
|
|
445
|
+
description: "Installed modules and what each may do (models, Actions, scopes, routes, hooks), plus official modules that can be installed. Propose one with cms_propose_module.",
|
|
446
|
+
readOnly: true,
|
|
447
|
+
visible: signedIn,
|
|
448
|
+
schema: () => z.strictObject({}),
|
|
449
|
+
async run(_input, { actor, kernel: k }) {
|
|
450
|
+
const installed = new Set(k.modules.map((m) => m.name));
|
|
451
|
+
// Community modules from the registry are added in a terminal (aicms add), never installed by the server.
|
|
452
|
+
let community = [];
|
|
453
|
+
let registryError;
|
|
454
|
+
if (k.config.registry) {
|
|
455
|
+
try {
|
|
456
|
+
const reg = await loadRegistry(k.config.registry, { cacheFile: join(k.moduleDir("registry"), "cache.json") });
|
|
457
|
+
community = reg.modules.filter((m) => !m.official && !installed.has(m.name)).map((m) => ({ ...m, install: `aicms add ${m.name}` }));
|
|
458
|
+
}
|
|
459
|
+
catch (e) {
|
|
460
|
+
registryError = e instanceof Error ? e.message : String(e);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
installed: k.modules.map(moduleManifest),
|
|
465
|
+
available: catalog.filter((c) => !installed.has(c.name)).map((c) => ({ ...moduleManifest(c.create()), title: c.title, summary: c.summary })),
|
|
466
|
+
community,
|
|
467
|
+
...(registryError ? { registryError } : {}),
|
|
468
|
+
youCan: installer(actor) ? "install" : "propose",
|
|
469
|
+
};
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
name: "cms.propose_module",
|
|
474
|
+
description: "Ask the site owner to install an official module (see cms_modules). The proposal waits in their Inbox with the module's permissions; you can't install code yourself.",
|
|
475
|
+
visible: signedIn,
|
|
476
|
+
schema: () => z.strictObject({ module: z.string().min(1), reason: z.string().min(10).max(2000).describe("Why the site needs it, for the person who decides") }),
|
|
477
|
+
run: (input, { actor, kernel }) => proposeModule(kernel, actor, input.module, input.reason),
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
name: "cms.install_module",
|
|
481
|
+
description: "For admins: install an official module now. It is loaded into the running server and written to aicms.config.",
|
|
482
|
+
visible: installer,
|
|
483
|
+
schema: () => z.strictObject({ module: z.string().min(1) }),
|
|
484
|
+
run: (input, { kernel }) => installFromCatalog(kernel, input.module),
|
|
485
|
+
},
|
|
486
|
+
];
|
|
487
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ErrorCode = "invalid" | "unauthorized" | "forbidden" | "not_found" | "conflict" | "budget";
|
|
2
|
+
/** An error an agent or person can act on: a code, a plain message, and details. */
|
|
3
|
+
export declare class AicmsError extends Error {
|
|
4
|
+
readonly code: ErrorCode;
|
|
5
|
+
readonly details?: unknown;
|
|
6
|
+
constructor(code: ErrorCode, message: string, details?: unknown);
|
|
7
|
+
get status(): number;
|
|
8
|
+
toJSON(): {
|
|
9
|
+
details?: {} | null | undefined;
|
|
10
|
+
code: ErrorCode;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export declare const isAicmsError: (e: unknown) => e is AicmsError;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const STATUS = {
|
|
2
|
+
invalid: 422,
|
|
3
|
+
unauthorized: 401,
|
|
4
|
+
forbidden: 403,
|
|
5
|
+
not_found: 404,
|
|
6
|
+
conflict: 409,
|
|
7
|
+
budget: 429,
|
|
8
|
+
};
|
|
9
|
+
/** An error an agent or person can act on: a code, a plain message, and details. */
|
|
10
|
+
export class AicmsError extends Error {
|
|
11
|
+
code;
|
|
12
|
+
details;
|
|
13
|
+
constructor(code, message, details) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.details = details;
|
|
17
|
+
}
|
|
18
|
+
get status() {
|
|
19
|
+
return STATUS[this.code];
|
|
20
|
+
}
|
|
21
|
+
toJSON() {
|
|
22
|
+
return { code: this.code, message: this.message, ...(this.details !== undefined ? { details: this.details } : {}) };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export const isAicmsError = (e) => e instanceof AicmsError;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Kernel } from "./cms.ts";
|
|
2
|
+
/** A catalog module, by name, if it isn't installed yet. */
|
|
3
|
+
export declare function catalogEntry(k: Kernel, name: string): import("../modules/catalog.ts").CatalogEntry;
|
|
4
|
+
/** Install an official module into the running kernel and, when the kernel came from a config file, write it there too. */
|
|
5
|
+
export declare function installFromCatalog(k: Kernel, name: string): Promise<{
|
|
6
|
+
note?: string | undefined;
|
|
7
|
+
module: string;
|
|
8
|
+
manifest: {
|
|
9
|
+
provides: string[];
|
|
10
|
+
requires: string[];
|
|
11
|
+
uses: string[];
|
|
12
|
+
models: {
|
|
13
|
+
name: string;
|
|
14
|
+
fields: string[];
|
|
15
|
+
}[];
|
|
16
|
+
actions: {
|
|
17
|
+
destructive?: boolean | undefined;
|
|
18
|
+
readOnly?: boolean | undefined;
|
|
19
|
+
name: string;
|
|
20
|
+
description: string;
|
|
21
|
+
scope: string;
|
|
22
|
+
}[];
|
|
23
|
+
scopes: string[];
|
|
24
|
+
routes: string[];
|
|
25
|
+
hooks: string[];
|
|
26
|
+
checks: string[];
|
|
27
|
+
version?: string | undefined;
|
|
28
|
+
description?: string | undefined;
|
|
29
|
+
name: string;
|
|
30
|
+
};
|
|
31
|
+
persisted: boolean;
|
|
32
|
+
}>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { catalog, findCatalog } from "../modules/catalog.js";
|
|
2
|
+
import { AicmsError } from "./errors.js";
|
|
3
|
+
import { moduleManifest } from "./modules.js";
|
|
4
|
+
/** A catalog module, by name, if it isn't installed yet. */
|
|
5
|
+
export function catalogEntry(k, name) {
|
|
6
|
+
const entry = findCatalog(name);
|
|
7
|
+
if (!entry)
|
|
8
|
+
throw new AicmsError("not_found", `No module "${name}" in the catalog. Available: ${catalog.map((c) => c.name).join(", ")}`);
|
|
9
|
+
if (k.modules.some((m) => m.name === entry.name))
|
|
10
|
+
throw new AicmsError("invalid", `Module "${entry.name}" is already installed`);
|
|
11
|
+
return entry;
|
|
12
|
+
}
|
|
13
|
+
/** Install an official module into the running kernel and, when the kernel came from a config file, write it there too. */
|
|
14
|
+
export async function installFromCatalog(k, name) {
|
|
15
|
+
const entry = catalogEntry(k, name);
|
|
16
|
+
const def = entry.create();
|
|
17
|
+
await k.installModule(def);
|
|
18
|
+
let persisted = false;
|
|
19
|
+
let note;
|
|
20
|
+
if (k.configFile) {
|
|
21
|
+
try {
|
|
22
|
+
const { addModuleToFile } = await import("../config-edit.js");
|
|
23
|
+
addModuleToFile(k.configFile, { name: entry.importName, from: "aicms/modules", isDefault: false, call: `${entry.importName}()` });
|
|
24
|
+
persisted = true;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
note = `Installed for this run, but aicms.config wasn't updated: ${e instanceof Error ? e.message : e}. Add ${entry.example} to modules by hand.`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
note = `Installed for this run. Add ${entry.example} to modules in aicms.config to keep it.`;
|
|
32
|
+
}
|
|
33
|
+
return { module: entry.name, manifest: moduleManifest(def), persisted, ...(note ? { note } : {}) };
|
|
34
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Model } from "../schema/model.ts";
|
|
2
|
+
import type { RegisteredAction } from "./action.ts";
|
|
3
|
+
export declare function arrayFields(model: Model): string[];
|
|
4
|
+
/** The four Actions every model gets: list, get, draft, delete. */
|
|
5
|
+
export declare function modelActions(model: Model): RegisteredAction[];
|