@mapomodule/mcp 1.0.0-beta.3 → 1.0.0-experimental.1
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/README.md +26 -24
- package/dist/build.mjs +66 -4
- package/dist/cli.mjs +8 -1
- package/dist/knowledge/api.json +1 -1
- package/dist/knowledge/docs.json +1 -1
- package/dist/module.json +1 -1
- package/dist/runtime/core/ai-setup/catalog.d.ts +0 -0
- package/dist/runtime/core/ai-setup/catalog.js +411 -0
- package/dist/runtime/core/ai-setup/index.d.ts +0 -0
- package/dist/runtime/core/ai-setup/index.js +20 -0
- package/dist/runtime/core/ai-setup/plan.d.ts +0 -0
- package/dist/runtime/core/ai-setup/plan.js +640 -0
- package/dist/runtime/core/ai-setup/questions.d.ts +0 -0
- package/dist/runtime/core/ai-setup/questions.js +237 -0
- package/dist/runtime/core/indexer.js +10 -3
- package/dist/runtime/core/instructions.js +3 -2
- package/dist/runtime/core/prompts.js +25 -1
- package/dist/runtime/core/recipes.js +388 -3
- package/dist/runtime/core/search.js +2 -1
- package/dist/runtime/core/tools/ai.d.ts +0 -0
- package/dist/runtime/core/tools/ai.js +125 -0
- package/dist/runtime/core/tools/api.js +7 -1
- package/dist/runtime/mcp/handlers/mapo/prompts/ai-setup.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/ai-setup.js +3 -0
- package/package.json +1 -1
- package/src/__tests__/aiSetup.test.ts +330 -0
- package/src/__tests__/cli.test.ts +1 -0
- package/src/__tests__/composables.test.ts +95 -0
- package/src/__tests__/indexer.test.ts +19 -0
- package/src/__tests__/knowledge.test.ts +48 -0
- package/src/__tests__/overview.test.ts +11 -0
- package/src/__tests__/recipes.test.ts +67 -0
- package/src/__tests__/search.test.ts +16 -0
- package/src/__tests__/tools.test.ts +27 -1
- package/src/build/extract-components.ts +33 -5
- package/src/build/extract-composables.ts +65 -2
- package/src/cli.ts +8 -1
- package/src/runtime/core/ai-setup/catalog.ts +475 -0
- package/src/runtime/core/ai-setup/index.ts +29 -0
- package/src/runtime/core/ai-setup/plan.ts +706 -0
- package/src/runtime/core/ai-setup/questions.ts +290 -0
- package/src/runtime/core/indexer.ts +16 -4
- package/src/runtime/core/instructions.ts +3 -2
- package/src/runtime/core/prompts.ts +28 -0
- package/src/runtime/core/recipes.ts +408 -3
- package/src/runtime/core/search.ts +4 -1
- package/src/runtime/core/tools/ai.ts +209 -0
- package/src/runtime/core/tools/api.ts +8 -0
- package/src/runtime/mcp/handlers/mapo/prompts/ai-setup.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/ai-setup.ts +4 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AI_AREAS,
|
|
3
|
+
AI_NEEDS,
|
|
4
|
+
AI_TOPICS,
|
|
5
|
+
AI_VERDICTS,
|
|
6
|
+
aiTopic,
|
|
7
|
+
topicLink
|
|
8
|
+
} from "./catalog.js";
|
|
9
|
+
import { AI_QUESTIONS, AI_ROUNDS } from "./questions.js";
|
|
10
|
+
export const AI_PROVIDER_CHOICES = [
|
|
11
|
+
"openai",
|
|
12
|
+
"anthropic",
|
|
13
|
+
"ollama",
|
|
14
|
+
"openai-compatible",
|
|
15
|
+
"own",
|
|
16
|
+
"mock"
|
|
17
|
+
];
|
|
18
|
+
export const AI_KEY_CHOICES = ["project", "assigned", "personal"];
|
|
19
|
+
export const AI_MAGIC_FILL_CHOICES = [
|
|
20
|
+
"every-page",
|
|
21
|
+
"some-pages",
|
|
22
|
+
"own-panel"
|
|
23
|
+
];
|
|
24
|
+
export const AI_DEPLOY_CHOICES = [
|
|
25
|
+
"single-node",
|
|
26
|
+
"several-instances",
|
|
27
|
+
"serverless"
|
|
28
|
+
];
|
|
29
|
+
const ANSWER_KEYS = [
|
|
30
|
+
"wants",
|
|
31
|
+
"dataMayLeave",
|
|
32
|
+
"providers",
|
|
33
|
+
"keys",
|
|
34
|
+
"userGroups",
|
|
35
|
+
"userPermissions",
|
|
36
|
+
"allowedFlag",
|
|
37
|
+
"adminGroups",
|
|
38
|
+
"costs",
|
|
39
|
+
"perUserPerMinute",
|
|
40
|
+
"magicFill",
|
|
41
|
+
"instructions",
|
|
42
|
+
"ownFormat",
|
|
43
|
+
"deploy"
|
|
44
|
+
];
|
|
45
|
+
export function hasAiAnswers(answers) {
|
|
46
|
+
return ANSWER_KEYS.some((key) => {
|
|
47
|
+
const value = answers[key];
|
|
48
|
+
if (Array.isArray(value)) return value.length > 0;
|
|
49
|
+
if (typeof value === "string") return value.trim() !== "";
|
|
50
|
+
return value !== void 0;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const CLOUD = ["openai", "anthropic"];
|
|
54
|
+
const LOCAL = ["ollama", "openai-compatible"];
|
|
55
|
+
function keyVariable(provider) {
|
|
56
|
+
const id = provider.toUpperCase().replace(/[^A-Z0-9]+/g, "_");
|
|
57
|
+
return `NUXT_MAPO_AI_PROVIDERS_${id}_API_KEY`;
|
|
58
|
+
}
|
|
59
|
+
const OWN_PROVIDER = "my-ai";
|
|
60
|
+
const EXTENSION = /^[a-z0-9]{1,16}$/;
|
|
61
|
+
const MAPO_EXTENSIONS = [
|
|
62
|
+
"txt",
|
|
63
|
+
"md",
|
|
64
|
+
"markdown",
|
|
65
|
+
"json",
|
|
66
|
+
"html",
|
|
67
|
+
"htm",
|
|
68
|
+
"xhtml",
|
|
69
|
+
"csv",
|
|
70
|
+
"tsv",
|
|
71
|
+
"xlsx"
|
|
72
|
+
];
|
|
73
|
+
const MAPO_READER_IDS = ["text", "markdown", "json", "html", "csv", "xlsx"];
|
|
74
|
+
function formatOf(answers) {
|
|
75
|
+
const format = answers.ownFormat?.trim().replace(/^\./, "").toLowerCase();
|
|
76
|
+
return format || void 0;
|
|
77
|
+
}
|
|
78
|
+
function isDraftable(format) {
|
|
79
|
+
return EXTENSION.test(format) && !MAPO_EXTENSIONS.includes(format);
|
|
80
|
+
}
|
|
81
|
+
function readerIdOf(format) {
|
|
82
|
+
const isId = /^[a-z]/.test(format) && !MAPO_READER_IDS.includes(format);
|
|
83
|
+
return isId ? format : `format-${format}`;
|
|
84
|
+
}
|
|
85
|
+
function quoted(value) {
|
|
86
|
+
const line = value.replace(/\s+/g, " ").replace(/`/g, "'").trim();
|
|
87
|
+
return line.length > 40 ? `${line.slice(0, 40)}\u2026` : line;
|
|
88
|
+
}
|
|
89
|
+
const FIELD_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
90
|
+
function appNotes(manifest) {
|
|
91
|
+
if (!manifest) {
|
|
92
|
+
return [
|
|
93
|
+
"No app is connected. With the dev server running, `mapo_inspect_app` lists the Mapo modules installed, the routes and the order of the modules."
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
const names = manifest.modules.map((entry) => entry.name);
|
|
97
|
+
const notes = [
|
|
98
|
+
names.includes("@mapomodule/ai") ? "`@mapomodule/ai` is installed: `mapo.ai` is set." : "`@mapomodule/ai` is not installed yet: it ships with `mapomodule` on npm's `experimental` dist-tag only (`pnpm add mapomodule@experimental`), and there setting `mapo.ai` in `nuxt.config.ts` installs it, through the meta-module.",
|
|
99
|
+
names.includes("@mapomodule/uikit") ? "uikit is installed: Magic Fill shows on every `MapoDetail` once the module is on." : "uikit is not installed: Magic Fill needs it. `useMapoAiFill()` still fills a form in the app's own components."
|
|
100
|
+
];
|
|
101
|
+
const hasSettings = manifest.routes.some(
|
|
102
|
+
(route) => route.path === "/ai-settings"
|
|
103
|
+
);
|
|
104
|
+
if (hasSettings)
|
|
105
|
+
notes.push("The AI settings page answers at `/ai-settings`.");
|
|
106
|
+
const ui = manifest.moduleOrder.indexOf("@nuxt/ui");
|
|
107
|
+
const mapo = manifest.moduleOrder.indexOf("mapomodule");
|
|
108
|
+
const isOrdered = ui >= 0 && mapo >= 0 && ui < mapo;
|
|
109
|
+
if (!isOrdered) {
|
|
110
|
+
notes.push(
|
|
111
|
+
"Register `@nuxt/ui` before `mapomodule` in `modules`: `mapo_doctor` checks it."
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return notes;
|
|
115
|
+
}
|
|
116
|
+
function catalog() {
|
|
117
|
+
const areas = AI_AREAS.map((area) => {
|
|
118
|
+
const lines = AI_TOPICS.filter((topic) => topic.area === area).map(
|
|
119
|
+
(topic) => `- ${topic.title}: ${topicLink(topic)}`
|
|
120
|
+
);
|
|
121
|
+
return `### ${area}
|
|
122
|
+
|
|
123
|
+
${lines.join("\n")}`;
|
|
124
|
+
});
|
|
125
|
+
return `${areas.join("\n\n")}
|
|
126
|
+
|
|
127
|
+
Read any of them with \`mapo_get_doc\`.`;
|
|
128
|
+
}
|
|
129
|
+
function needsTable() {
|
|
130
|
+
const rows = AI_NEEDS.map(
|
|
131
|
+
(need) => `| \`${need.id}\` | ${need.need} | ${AI_VERDICTS[need.verdict]} |`
|
|
132
|
+
);
|
|
133
|
+
return [
|
|
134
|
+
"| `wants` | The need | The module |",
|
|
135
|
+
"| --- | --- | --- |",
|
|
136
|
+
...rows
|
|
137
|
+
].join("\n");
|
|
138
|
+
}
|
|
139
|
+
function question(one, index) {
|
|
140
|
+
const options = one.options.map((option) => {
|
|
141
|
+
const mark = option.recommended ? " (recommended)" : "";
|
|
142
|
+
const pass = option.pass ? `\`${option.pass}\`` : "nothing to pass";
|
|
143
|
+
return ` - **${option.label}**${mark}: ${option.detail} \u2192 ${pass}`;
|
|
144
|
+
});
|
|
145
|
+
const several = one.several ? "; several may apply" : "";
|
|
146
|
+
const fills = one.fills.map((name) => `\`${name}\``).join(", ");
|
|
147
|
+
return [
|
|
148
|
+
`${index}. **${one.question}** ${one.why} (fills ${fills}${several})`,
|
|
149
|
+
...options
|
|
150
|
+
].join("\n");
|
|
151
|
+
}
|
|
152
|
+
function interview() {
|
|
153
|
+
const rounds = [1, 2, 3].map((round) => {
|
|
154
|
+
const lines = AI_QUESTIONS.flatMap(
|
|
155
|
+
(one, index) => one.round === round ? [question(one, index + 1)] : []
|
|
156
|
+
);
|
|
157
|
+
return `### Round ${round}: ${AI_ROUNDS[round]}
|
|
158
|
+
|
|
159
|
+
${lines.join("\n")}`;
|
|
160
|
+
});
|
|
161
|
+
return rounds.join("\n\n");
|
|
162
|
+
}
|
|
163
|
+
export function renderAiSetupQuestions(answers, manifest) {
|
|
164
|
+
const need = answers.need?.trim();
|
|
165
|
+
const asked = need ? `## The need
|
|
166
|
+
|
|
167
|
+
> ${need.replace(/\n+/g, " ")}
|
|
168
|
+
|
|
169
|
+
Map it to the needs below (\`wants\`) and ask the questions: the plan then says what the module can do about it.
|
|
170
|
+
|
|
171
|
+
` : "";
|
|
172
|
+
return `# Set up the AI and Magic Fill
|
|
173
|
+
|
|
174
|
+
\`@mapomodule/ai\` is experimental and opt-in, published with \`mapomodule\` on npm's \`experimental\` dist-tag only (\`pnpm add mapomodule@experimental\`): there, setting \`mapo.ai\` in \`nuxt.config.ts\` installs it. It fills records from a source (a pasted text, a web page, a sheet) through a gateway on the app's server, and the operator checks every value: the AI never saves anything by itself.
|
|
175
|
+
|
|
176
|
+
## This app
|
|
177
|
+
|
|
178
|
+
${appNotes(manifest).map((note) => `- ${note}`).join("\n")}
|
|
179
|
+
|
|
180
|
+
` + asked + `## Questions for the developer
|
|
181
|
+
|
|
182
|
+
Ask one round at a time, at most three questions, the recommended answer first, and wait for the answers. Then call \`mapo_ai_setup\` again with them: it says whether the module can do what is needed, and gives the configuration, the keys, the files and the checks.
|
|
183
|
+
|
|
184
|
+
${interview()}
|
|
185
|
+
|
|
186
|
+
## What the module can do
|
|
187
|
+
|
|
188
|
+
${needsTable()}
|
|
189
|
+
|
|
190
|
+
## Everything it offers
|
|
191
|
+
|
|
192
|
+
${catalog()}
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
function fit(answers) {
|
|
196
|
+
const wants = [...new Set(answers.wants ?? [])];
|
|
197
|
+
if (!wants.length) {
|
|
198
|
+
return "No need was mapped to `wants` yet: ask the first round, then call again with them.";
|
|
199
|
+
}
|
|
200
|
+
const needs = AI_NEEDS.filter((need) => wants.includes(need.id));
|
|
201
|
+
const rows = needs.map((need) => {
|
|
202
|
+
const docs = need.topics.map((topic) => aiTopic(topic)).filter((topic) => topic !== void 0).map(topicLink).join("; ");
|
|
203
|
+
return `| ${need.need} | **${AI_VERDICTS[need.verdict]}** | ${need.how} Docs: ${docs} |`;
|
|
204
|
+
});
|
|
205
|
+
return ["| Need | The module | How |", "| --- | --- | --- |", ...rows].join(
|
|
206
|
+
"\n"
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
function warnings(answers, manifest) {
|
|
210
|
+
const providers = answers.providers ?? [];
|
|
211
|
+
const keys = answers.keys ?? [];
|
|
212
|
+
const found = [];
|
|
213
|
+
const cloud = providers.filter((one) => CLOUD.includes(one));
|
|
214
|
+
if (answers.dataMayLeave === false && cloud.length) {
|
|
215
|
+
found.push(
|
|
216
|
+
`The data must stay on the company's servers, but ${cloud.map((one) => `\`${one}\``).join(" and ")} sends it to a cloud: choose \`ollama\` or \`openai-compatible\` instead.`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
const onlyLocal = providers.length > 0 && providers.every((one) => LOCAL.includes(one) || one === "mock");
|
|
220
|
+
if (onlyLocal && keys.some((one) => one !== "project")) {
|
|
221
|
+
found.push(
|
|
222
|
+
"Models on the company's servers need no key: an assignment of the admins still narrows the models each user gets, but personal tokens have nothing to pay for."
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
const names = manifest?.modules.map((entry) => entry.name) ?? [];
|
|
226
|
+
const wantsMagicFill = answers.magicFill !== "own-panel";
|
|
227
|
+
if (manifest && wantsMagicFill && !names.includes("@mapomodule/uikit")) {
|
|
228
|
+
found.push(
|
|
229
|
+
"Magic Fill needs `@mapomodule/uikit`, which this app does not install: build a panel of the app's own with `useMapoAiFill()` instead."
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
const flag = answers.allowedFlag?.trim();
|
|
233
|
+
if (flag && !FIELD_NAME.test(flag)) {
|
|
234
|
+
found.push(
|
|
235
|
+
`\`${quoted(flag)}\` is not a field name (letters, digits and \`_\`): no hook was drafted.`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
const format = formatOf(answers);
|
|
239
|
+
if (format && !EXTENSION.test(format)) {
|
|
240
|
+
found.push(
|
|
241
|
+
`\`${quoted(answers.ownFormat ?? "")}\` is not a file extension (letters and digits): no reader was drafted.`
|
|
242
|
+
);
|
|
243
|
+
} else if (format && MAPO_EXTENSIONS.includes(format)) {
|
|
244
|
+
found.push(
|
|
245
|
+
`Mapo reads \`.${format}\` files already: a reader of the app would take the place of Mapo's, so none was drafted. The \`mapo-ai:read\` hook touches up what Mapo's reader reads.`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
return found;
|
|
249
|
+
}
|
|
250
|
+
function providerLines(providers) {
|
|
251
|
+
return providers.map((provider) => {
|
|
252
|
+
switch (provider) {
|
|
253
|
+
case "openai":
|
|
254
|
+
return " openai: {}, // its default models; `models` narrows them, the default first";
|
|
255
|
+
case "anthropic":
|
|
256
|
+
return " anthropic: {}, // its default models; `models` narrows them, the default first";
|
|
257
|
+
case "ollama":
|
|
258
|
+
return ' ollama: { models: ["gemma4:12b"] }, // the models you pulled (`ollama list`), the default first';
|
|
259
|
+
case "openai-compatible":
|
|
260
|
+
return ` "openai-compatible": { baseURL: "http://localhost:1234/v1", models: ["qwen3-32b"] }, // your server's address and model ids`;
|
|
261
|
+
case "own":
|
|
262
|
+
return ` "${OWN_PROVIDER}": {}, // registered with registerMapoAiProvider() in a Nitro plugin`;
|
|
263
|
+
case "mock":
|
|
264
|
+
return " mock: {}, // development and tests: no key, no network, no cost";
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
function list(values) {
|
|
269
|
+
return `[${values.map((value) => JSON.stringify(value)).join(", ")}]`;
|
|
270
|
+
}
|
|
271
|
+
function configBlock(answers) {
|
|
272
|
+
const providers = answers.providers ?? [];
|
|
273
|
+
const keys = answers.keys ?? [];
|
|
274
|
+
const lines = [];
|
|
275
|
+
if (providers.length) {
|
|
276
|
+
lines.push(" providers: {", ...providerLines(providers), " },");
|
|
277
|
+
} else {
|
|
278
|
+
lines.push(
|
|
279
|
+
" providers: {}, // none yet: `run` answers 503 until one is enabled"
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const users = [
|
|
283
|
+
...answers.userGroups?.length ? [`groups: ${list(answers.userGroups)}`] : [],
|
|
284
|
+
...answers.userPermissions?.length ? [`permissions: ${list(answers.userPermissions)}`] : []
|
|
285
|
+
];
|
|
286
|
+
if (users.length) lines.push(` users: { ${users.join(", ")} },`);
|
|
287
|
+
if (answers.adminGroups?.length) {
|
|
288
|
+
lines.push(` admin: { groups: ${list(answers.adminGroups)} },`);
|
|
289
|
+
}
|
|
290
|
+
const rules = answers.instructions?.trim();
|
|
291
|
+
if (rules) {
|
|
292
|
+
lines.push(` instructions: { all: ${JSON.stringify(rules)} },`);
|
|
293
|
+
}
|
|
294
|
+
if (answers.perUserPerMinute !== void 0) {
|
|
295
|
+
lines.push(
|
|
296
|
+
` limits: { perUserPerMinute: ${answers.perUserPerMinute} },`
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
const assigns = keys.includes("assigned") || keys.includes("personal");
|
|
300
|
+
if (assigns) {
|
|
301
|
+
lines.push(
|
|
302
|
+
` settings: { usersApi: "/api/users/", groupsApi: "/api/groups/" }, // adjust: your backend's users and groups, which the admins search`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
if (answers.magicFill === "own-panel") {
|
|
306
|
+
lines.push(" magicFill: false, // the app's own panel instead");
|
|
307
|
+
}
|
|
308
|
+
return [
|
|
309
|
+
"```ts",
|
|
310
|
+
"// nuxt.config.ts",
|
|
311
|
+
"export default defineNuxtConfig({",
|
|
312
|
+
' modules: ["@nuxt/ui", "mapomodule"], // @nuxt/ui first',
|
|
313
|
+
" mapo: {",
|
|
314
|
+
" ai: {",
|
|
315
|
+
...lines,
|
|
316
|
+
" },",
|
|
317
|
+
" },",
|
|
318
|
+
"});",
|
|
319
|
+
"```"
|
|
320
|
+
].join("\n");
|
|
321
|
+
}
|
|
322
|
+
function environmentBlock(answers) {
|
|
323
|
+
const providers = answers.providers ?? [];
|
|
324
|
+
const keys = answers.keys ?? [];
|
|
325
|
+
const lines = [
|
|
326
|
+
"# .env in development, the server's environment in production; never in nuxt.config.ts",
|
|
327
|
+
"NUXT_MAPO_AI_SECRET= # openssl rand -base64 32: the same on every instance"
|
|
328
|
+
];
|
|
329
|
+
const projectKeys = keys.length === 0 || keys.includes("project");
|
|
330
|
+
for (const provider of providers) {
|
|
331
|
+
if (CLOUD.includes(provider) && projectKeys) {
|
|
332
|
+
lines.push(`${keyVariable(provider)}= # the project's key`);
|
|
333
|
+
}
|
|
334
|
+
if (provider === "own") {
|
|
335
|
+
lines.push(
|
|
336
|
+
`${keyVariable(OWN_PROVIDER)}= # its key, if it declares one`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (providers.includes("ollama")) {
|
|
341
|
+
lines.push(
|
|
342
|
+
"# NUXT_MAPO_AI_PROVIDERS_OLLAMA_BASE_URL= # when Ollama is not at http://localhost:11434"
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
if (providers.includes("openai-compatible")) {
|
|
346
|
+
lines.push(
|
|
347
|
+
"# NUXT_MAPO_AI_PROVIDERS_OPENAI_COMPATIBLE_BASE_URL= # the server's address in production"
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
return ["```bash", ...lines, "```"].join("\n");
|
|
351
|
+
}
|
|
352
|
+
function keysSection(answers) {
|
|
353
|
+
const providers = answers.providers ?? [];
|
|
354
|
+
const keys = answers.keys ?? [];
|
|
355
|
+
const lines = [
|
|
356
|
+
"A call uses the key nearest its user: their own token where an assignment allows it, else the key the admins assigned to them, to their group (in the order the admins set) or to everyone, else the project's key. `status` tells each user which level their key comes from and its last four characters, never the key."
|
|
357
|
+
];
|
|
358
|
+
if (keys.length === 0 || keys.includes("project")) {
|
|
359
|
+
lines.push(
|
|
360
|
+
"- **The project's key** goes in the server's environment only. A key written in `nuxt.config.ts` is copied into the server bundle: the module warns about it."
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
if (keys.includes("assigned")) {
|
|
364
|
+
lines.push(
|
|
365
|
+
"- **The admins' keys**: on the AI settings page (`/ai-settings`, Keys) an admin assigns a key and the models to everyone, a group or a user, and orders the groups; a user in two groups gets the higher one's. Nothing to deploy."
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
if (keys.includes("personal")) {
|
|
369
|
+
lines.push(
|
|
370
|
+
"- **Personal tokens** are off until an assignment allows them; then each user saves theirs on the AI settings page, and pays for their own calls."
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
if (providers.some((one) => LOCAL.includes(one))) {
|
|
374
|
+
lines.push(
|
|
375
|
+
"- **Models on the company's servers** need no key; the token of a proxy in front of them is their project key (`NUXT_MAPO_AI_PROVIDERS_OLLAMA_API_KEY`, `NUXT_MAPO_AI_PROVIDERS_OPENAI_COMPATIBLE_API_KEY`)."
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
lines.push(
|
|
379
|
+
"- **The secret** seals every key, token and setting the admins save: without `NUXT_MAPO_AI_SECRET` in production the storage stays closed and `run` answers 503. Changing it makes what was saved unreadable."
|
|
380
|
+
);
|
|
381
|
+
const [intro, ...rest] = lines;
|
|
382
|
+
return `${intro}
|
|
383
|
+
|
|
384
|
+
${rest.join("\n")}`;
|
|
385
|
+
}
|
|
386
|
+
function tokensSection(answers) {
|
|
387
|
+
const providers = answers.providers ?? [];
|
|
388
|
+
const lines = [
|
|
389
|
+
"Every call is audited with its tokens. What a call spends grows with the form (the fields the AI may fill), the source and the instructions, and with the model.",
|
|
390
|
+
"- **The usage**: the AI settings page sums it by user, model and day; the admins read everyone's, each user their own."
|
|
391
|
+
];
|
|
392
|
+
if (answers.costs !== false) {
|
|
393
|
+
const priced = providers.filter((one) => CLOUD.includes(one));
|
|
394
|
+
const example = priced.length ? priced[0] : "openai";
|
|
395
|
+
lines.push(
|
|
396
|
+
`- **The costs**: \`mapo.ai.prices\` turns tokens into money, with your contract's prices per million tokens; Mapo keeps no price list:
|
|
397
|
+
|
|
398
|
+
\`\`\`ts
|
|
399
|
+
prices: {
|
|
400
|
+
currency: "USD",
|
|
401
|
+
providers: {
|
|
402
|
+
${example}: {
|
|
403
|
+
// "<model>": { input: \u2026, output: \u2026 }, your prices per million tokens,
|
|
404
|
+
// with cacheRead and cacheWrite where the prompt cache is on
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
\`\`\``
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
lines.push(
|
|
412
|
+
`- **The limits**: \`limits.perUserPerMinute\` (${answers.perUserPerMinute ?? 20} calls), \`maxSourceChars\` (100,000) and \`maxFileBytes\` (10 MB). Each server process counts its own calls. There is no spending cap per month yet.`
|
|
413
|
+
);
|
|
414
|
+
if (providers.some((one) => CLOUD.includes(one))) {
|
|
415
|
+
lines.push(
|
|
416
|
+
'- **Spend less**: an efficient model for everyday forms, listed first in `models`; `effort` stays `"low"`, since the reasoning is paid as written tokens; `promptCache: true` pays less for the part of the prompt that repeats; keep the forms\' fields that need no AI out with `ai: { exclude: true }`.'
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
if (providers.some((one) => LOCAL.includes(one))) {
|
|
420
|
+
lines.push(
|
|
421
|
+
"- **Models on the company's servers** cost nothing per token: the usage still counts their tokens, and the hardware decides how long a call takes."
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
const [intro, ...rest] = lines;
|
|
425
|
+
return `${intro}
|
|
426
|
+
|
|
427
|
+
${rest.join("\n")}`;
|
|
428
|
+
}
|
|
429
|
+
function formsSection(answers) {
|
|
430
|
+
const lines = [];
|
|
431
|
+
if (answers.magicFill === "own-panel") {
|
|
432
|
+
lines.push(
|
|
433
|
+
"- **A panel of the app's own**: `mapo.ai.magicFill: false`, then `useMapoAiFill(detail)` in a detail extension sends the form and the source to `run` and marks every value for the operator; `useMapoAiRead()` reads a file."
|
|
434
|
+
);
|
|
435
|
+
} else {
|
|
436
|
+
lines.push(
|
|
437
|
+
'- **Magic Fill** shows on every `MapoDetail`: "Fill with AI" in the sidebar card, and a banner above the form that previews every value; "Next to check" takes the operator to each one that waits.'
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
if (answers.magicFill === "some-pages") {
|
|
441
|
+
lines.push(
|
|
442
|
+
"- **Pages without it**: `definePageMeta({ magicFill: false })`; `{ magicFill: { evidence: false } }` asks the AI for no quotes, so every value waits."
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
lines.push(
|
|
446
|
+
"- **Drafts**: on a `MapoDetail` with `draft`, the session comes back after a reload, with Restore.",
|
|
447
|
+
'- **The fields** take an `ai` option:\n\n ```ts\n { key: "excerpt", type: "textarea", label: "Excerpt", ai: { hint: "One or two sentences, in the source\'s own words." } },\n { key: "internal_notes", type: "textarea", label: "Internal notes", ai: { exclude: true } },\n { key: "contact", type: "fks", label: "Contact", attrs: { endpoint: "/api/users/", itemText: "email" }, ai: { list: false } },\n ```\n\n `hint` guides the model; `exclude` keeps the field from the AI; `list: false` never lists a relation\'s records for it, which then searches the name it reads. Fields named as secrets, read-only fields and media stay out by themselves.'
|
|
448
|
+
);
|
|
449
|
+
return lines.join("\n");
|
|
450
|
+
}
|
|
451
|
+
export function aiSetupFiles(answers) {
|
|
452
|
+
const files = [];
|
|
453
|
+
const flag = answers.allowedFlag?.trim();
|
|
454
|
+
if (flag && FIELD_NAME.test(flag)) {
|
|
455
|
+
files.push({
|
|
456
|
+
path: "server/plugins/mapo-ai-access.ts",
|
|
457
|
+
content: `// The AI only for the users the backend marks with \`${flag}\`: the gateway
|
|
458
|
+
// asks the backend who is signed in, and this hook refuses the others, after
|
|
459
|
+
// mapo.ai.users. A hook can only refuse: it never lets a user in.
|
|
460
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
461
|
+
nitroApp.hooks.hook("mapo-ai:user", (context) => {
|
|
462
|
+
if (context.payload[${JSON.stringify(flag)}] !== true) context.allowed = false;
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
`
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
const format = formatOf(answers);
|
|
469
|
+
if (format && isDraftable(format)) {
|
|
470
|
+
files.push({
|
|
471
|
+
path: "server/plugins/mapo-ai-readers.ts",
|
|
472
|
+
content: `// A reader of .${format} files for the AI: read() turns a file into the text
|
|
473
|
+
// the AI gets. Decoding the bytes as text is a start: write what your format
|
|
474
|
+
// needs. Return [] for a file that holds no text.
|
|
475
|
+
export default defineNitroPlugin(() => {
|
|
476
|
+
registerMapoAiReader({
|
|
477
|
+
id: ${JSON.stringify(readerIdOf(format))},
|
|
478
|
+
label: ${JSON.stringify(`${format.toUpperCase()} file`)},
|
|
479
|
+
extensions: [${JSON.stringify(format)}],
|
|
480
|
+
read(file) {
|
|
481
|
+
const text = new TextDecoder().decode(file.bytes).trim();
|
|
482
|
+
return text ? [{ kind: "text", text, name: file.name }] : [];
|
|
483
|
+
},
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
`
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
const shared = answers.deploy === "several-instances" || answers.deploy === "serverless";
|
|
490
|
+
if (shared) {
|
|
491
|
+
files.push({
|
|
492
|
+
path: "server/plugins/mapo-ai-storage.ts",
|
|
493
|
+
content: `// The AI's sealed state in Redis, shared by every instance, which need the
|
|
494
|
+
// same NUXT_MAPO_AI_SECRET too. The address comes from the environment, so
|
|
495
|
+
// the driver is mounted when the server starts, in place of the module's
|
|
496
|
+
// file-system default, which stays in development without REDIS_URL. The
|
|
497
|
+
// redis driver of unstorage needs \`ioredis\` in the app.
|
|
498
|
+
import redisDriver from "unstorage/drivers/redis";
|
|
499
|
+
|
|
500
|
+
export default defineNitroPlugin(async () => {
|
|
501
|
+
const url = process.env.REDIS_URL;
|
|
502
|
+
if (!url) {
|
|
503
|
+
if (import.meta.dev) return;
|
|
504
|
+
throw new Error("REDIS_URL is not set: the AI's state needs the shared store");
|
|
505
|
+
}
|
|
506
|
+
const storage = useStorage();
|
|
507
|
+
await storage.unmount("mapo-ai");
|
|
508
|
+
storage.mount("mapo-ai", redisDriver({ url }));
|
|
509
|
+
});
|
|
510
|
+
`
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
return files;
|
|
514
|
+
}
|
|
515
|
+
function productionSection(answers) {
|
|
516
|
+
const lines = [
|
|
517
|
+
"- **The secret** in the server's environment, 32 characters or more, made by a generator: the storage never keeps anything in clear."
|
|
518
|
+
];
|
|
519
|
+
switch (answers.deploy) {
|
|
520
|
+
case "several-instances":
|
|
521
|
+
lines.push(
|
|
522
|
+
"- **Several instances** share one store, here Redis: the plugin above, `REDIS_URL` in the environment and `ioredis` installed in the app. In development without `REDIS_URL` the file system stays; in production the server does not start without it. They share the secret too, and each process counts the calls per minute for itself."
|
|
523
|
+
);
|
|
524
|
+
break;
|
|
525
|
+
case "serverless":
|
|
526
|
+
lines.push(
|
|
527
|
+
"- **Serverless**: a function's file system does not last, so the state goes to a driver. On functions that run Node (the `vercel`, `netlify` and `aws-lambda` presets) the Redis plugin above does it, with `REDIS_URL` and `ioredis` installed. An edge runtime (Cloudflare Workers and the like) has no TCP for Redis: mount a driver that works over HTTP instead, such as Upstash's or Cloudflare KV, the same way."
|
|
528
|
+
);
|
|
529
|
+
break;
|
|
530
|
+
default:
|
|
531
|
+
lines.push(
|
|
532
|
+
"- **One Node server**: the state goes to `.data/mapo-ai` where the server starts; in a container, put it on a persistent volume, or every deploy loses the keys and the usage."
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
lines.push(
|
|
536
|
+
"- **The audit** keeps each call's metadata for 90 days (`audit.retentionDays`), never the source, the instructions or the values."
|
|
537
|
+
);
|
|
538
|
+
return lines.join("\n");
|
|
539
|
+
}
|
|
540
|
+
function stillToAsk(answers) {
|
|
541
|
+
const missing = [
|
|
542
|
+
[!answers.wants?.length, "task", "no need mapped yet"],
|
|
543
|
+
[
|
|
544
|
+
answers.dataMayLeave === void 0,
|
|
545
|
+
"data",
|
|
546
|
+
"nothing about where the data may go"
|
|
547
|
+
],
|
|
548
|
+
[
|
|
549
|
+
!answers.providers?.length,
|
|
550
|
+
"providers",
|
|
551
|
+
"no provider, so `run` answers 503"
|
|
552
|
+
],
|
|
553
|
+
[!answers.keys?.length, "keys", "the project's key"],
|
|
554
|
+
[
|
|
555
|
+
answers.costs === void 0 && answers.perUserPerMinute === void 0,
|
|
556
|
+
"tokens",
|
|
557
|
+
"the usage with costs, 20 calls per minute"
|
|
558
|
+
],
|
|
559
|
+
[
|
|
560
|
+
answers.magicFill === void 0,
|
|
561
|
+
"forms",
|
|
562
|
+
"Magic Fill on every detail page"
|
|
563
|
+
],
|
|
564
|
+
[
|
|
565
|
+
answers.deploy === void 0,
|
|
566
|
+
"deploy",
|
|
567
|
+
"one Node server with a lasting disk"
|
|
568
|
+
]
|
|
569
|
+
];
|
|
570
|
+
return missing.flatMap(([isMissing, id, assumed]) => {
|
|
571
|
+
const one = AI_QUESTIONS.find((asked) => asked.id === id);
|
|
572
|
+
if (!isMissing || !one) return [];
|
|
573
|
+
return [`- Round ${one.round}: "${one.question}" (assumed: ${assumed})`];
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
export function renderAiSetupPlan(answers, manifest, files) {
|
|
577
|
+
const need = answers.need?.trim();
|
|
578
|
+
const found = warnings(answers, manifest);
|
|
579
|
+
const missing = stillToAsk(answers);
|
|
580
|
+
return "# The AI for this backoffice: the plan\n\n" + (need ? `> ${need.replace(/\n+/g, " ")}
|
|
581
|
+
|
|
582
|
+
` : "") + `## Can the module do it?
|
|
583
|
+
|
|
584
|
+
${fit(answers)}
|
|
585
|
+
|
|
586
|
+
` + (found.length ? `## Check these first
|
|
587
|
+
|
|
588
|
+
${found.map((one) => `- ${one}`).join("\n")}
|
|
589
|
+
|
|
590
|
+
` : "") + `## This app
|
|
591
|
+
|
|
592
|
+
${appNotes(manifest).map((note) => `- ${note}`).join("\n")}
|
|
593
|
+
|
|
594
|
+
## 1. The configuration
|
|
595
|
+
|
|
596
|
+
Merge it into the app's \`nuxt.config.ts\` (this tool never edits it):
|
|
597
|
+
|
|
598
|
+
${configBlock(answers)}
|
|
599
|
+
|
|
600
|
+
## 2. The environment
|
|
601
|
+
|
|
602
|
+
The names only: the values never go in a file of the repository.
|
|
603
|
+
|
|
604
|
+
${environmentBlock(answers)}
|
|
605
|
+
|
|
606
|
+
## 3. Keys and tokens
|
|
607
|
+
|
|
608
|
+
### Where each call's key comes from
|
|
609
|
+
|
|
610
|
+
${keysSection(answers)}
|
|
611
|
+
|
|
612
|
+
### The tokens a call spends
|
|
613
|
+
|
|
614
|
+
${tokensSection(answers)}
|
|
615
|
+
|
|
616
|
+
## 4. The forms
|
|
617
|
+
|
|
618
|
+
${formsSection(answers)}
|
|
619
|
+
|
|
620
|
+
## 5. Files of the app
|
|
621
|
+
|
|
622
|
+
${files}
|
|
623
|
+
|
|
624
|
+
## 6. Production
|
|
625
|
+
|
|
626
|
+
${productionSection(answers)}
|
|
627
|
+
|
|
628
|
+
## 7. Check it
|
|
629
|
+
|
|
630
|
+
1. Restart the dev server: a new module and new server plugins need it.
|
|
631
|
+
2. Signed in, \`GET /_mapo/ai/status\`: \`allowed\`, the providers and models each user gets, and where their key comes from.
|
|
632
|
+
3. Open \`/ai-settings\`: the preference, the connection check, and for the admins the keys, how the AI works and the usage.
|
|
633
|
+
4. On a detail page, "Fill with AI": paste a text, Extract, check the values in the banner, Save.
|
|
634
|
+
5. \`mapo_doctor\` for anything that looks misconfigured.
|
|
635
|
+
` + (missing.length ? `
|
|
636
|
+
## Still to ask
|
|
637
|
+
|
|
638
|
+
${missing.join("\n")}
|
|
639
|
+
` : "");
|
|
640
|
+
}
|
|
File without changes
|