@mapomodule/mcp 1.0.0-beta.3 → 1.0.0-experimental.2
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 +428 -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 +642 -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 +407 -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 +83 -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 +492 -0
- package/src/runtime/core/ai-setup/index.ts +29 -0
- package/src/runtime/core/ai-setup/plan.ts +710 -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 +428 -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,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mapo_ai_setup`: the interview while nothing is answered, then the plan.
|
|
3
|
+
* The plan proposes code a developer may paste into a project, so these
|
|
4
|
+
* tests read it as they would: what it says the module can do, the
|
|
5
|
+
* configuration it builds, the variables it names without a value, and the
|
|
6
|
+
* files it writes only when asked.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, mkdtempSync, readFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { describe, expect, it } from "vitest";
|
|
12
|
+
import {
|
|
13
|
+
AI_AREAS,
|
|
14
|
+
AI_NEEDS,
|
|
15
|
+
AI_QUESTIONS,
|
|
16
|
+
AI_TOPICS,
|
|
17
|
+
} from "../runtime/core/ai-setup/index.js";
|
|
18
|
+
import { aiSetupTool, aiTools } from "../runtime/core/tools/ai.js";
|
|
19
|
+
import { makeContext, makeManifest } from "./fixtures.js";
|
|
20
|
+
|
|
21
|
+
const context = makeContext();
|
|
22
|
+
const run = (args: Parameters<typeof aiSetupTool.run>[0]) =>
|
|
23
|
+
aiSetupTool.run(args, context) as string;
|
|
24
|
+
|
|
25
|
+
describe("mapo_ai_setup: the tool", () => {
|
|
26
|
+
it("is namespaced, may write and never destroys", () => {
|
|
27
|
+
expect(aiTools.map((tool) => tool.name)).toEqual(["mapo_ai_setup"]);
|
|
28
|
+
expect(aiSetupTool.annotations).toMatchObject({
|
|
29
|
+
readOnlyHint: false,
|
|
30
|
+
destructiveHint: false,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("declares input examples that its schema takes", async () => {
|
|
35
|
+
const { z } = await import("zod");
|
|
36
|
+
const schema = z.object(aiSetupTool.inputSchema);
|
|
37
|
+
for (const example of aiSetupTool.inputExamples ?? []) {
|
|
38
|
+
expect(schema.safeParse(example).success, JSON.stringify(example)).toBe(
|
|
39
|
+
true,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("mapo_ai_setup: the interview", () => {
|
|
46
|
+
it("asks every question, in three rounds, while nothing is answered", () => {
|
|
47
|
+
const output = run({});
|
|
48
|
+
|
|
49
|
+
expect(output).toContain("# Set up the AI and Magic Fill");
|
|
50
|
+
for (const round of [1, 2, 3])
|
|
51
|
+
expect(output).toContain(`### Round ${round}`);
|
|
52
|
+
for (const question of AI_QUESTIONS) {
|
|
53
|
+
expect(output).toContain(question.question);
|
|
54
|
+
}
|
|
55
|
+
// The recommended answer is marked, and the argument it fills named.
|
|
56
|
+
expect(output).toContain("(recommended)");
|
|
57
|
+
expect(output).toContain('`wants: ["fill-record"]`');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("lists every need the module answers, and everything it offers", () => {
|
|
61
|
+
const output = run({});
|
|
62
|
+
|
|
63
|
+
for (const need of AI_NEEDS) expect(output).toContain(`\`${need.id}\``);
|
|
64
|
+
for (const area of AI_AREAS) expect(output).toContain(`### ${area}`);
|
|
65
|
+
for (const topic of AI_TOPICS) expect(output).toContain(topic.title);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("echoes the developer's need, and still asks", () => {
|
|
69
|
+
const output = run({ need: "Fill events from the press releases" });
|
|
70
|
+
|
|
71
|
+
expect(output).toContain("> Fill events from the press releases");
|
|
72
|
+
expect(output).toContain("## Questions for the developer");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("mapo_ai_setup: the plan", () => {
|
|
77
|
+
it("says what the module can do, with app code, not yet, or not at all", () => {
|
|
78
|
+
const output = run({
|
|
79
|
+
wants: [
|
|
80
|
+
"fill-record",
|
|
81
|
+
"extract-records",
|
|
82
|
+
"pdf-images",
|
|
83
|
+
"save-without-review",
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
expect(output).toContain("## Can the module do it?");
|
|
88
|
+
expect(output).toMatch(/Fill the record .*\*\*Yes, out of the box\*\*/);
|
|
89
|
+
expect(output).toMatch(
|
|
90
|
+
/several records .*\*\*Yes, with code of the app\*\*/,
|
|
91
|
+
);
|
|
92
|
+
expect(output).toMatch(/PDFs, scans or images \| \*\*Not yet\*\*/);
|
|
93
|
+
expect(output).toMatch(/without an operator \| \*\*No\*\*/);
|
|
94
|
+
// Each answer points at the docs that explain it.
|
|
95
|
+
expect(output).toContain("`howto/magic-fill.md`");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("builds the `mapo.ai` block from the answers", () => {
|
|
99
|
+
const output = run({
|
|
100
|
+
providers: ["openai", "ollama"],
|
|
101
|
+
keys: ["project", "assigned"],
|
|
102
|
+
userGroups: ["editors"],
|
|
103
|
+
adminGroups: ["ai-admins"],
|
|
104
|
+
instructions: 'The records are the city\'s "culture" events.',
|
|
105
|
+
perUserPerMinute: 10,
|
|
106
|
+
magicFill: "own-panel",
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(output).toContain('modules: ["@nuxt/ui", "mapomodule"]');
|
|
110
|
+
expect(output).toContain("openai: {}");
|
|
111
|
+
expect(output).toContain('ollama: { models: ["gemma4:12b"] }');
|
|
112
|
+
expect(output).toContain('users: { groups: ["editors"] }');
|
|
113
|
+
expect(output).toContain('admin: { groups: ["ai-admins"] }');
|
|
114
|
+
expect(output).toContain(
|
|
115
|
+
'instructions: { all: "The records are the city\'s \\"culture\\" events." }',
|
|
116
|
+
);
|
|
117
|
+
expect(output).toContain("limits: { perUserPerMinute: 10 }");
|
|
118
|
+
expect(output).toContain('settings: { usersApi: "/api/users/"');
|
|
119
|
+
expect(output).toContain("magicFill: false");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("names the environment's variables, never with a value", () => {
|
|
123
|
+
const output = run({ providers: ["openai", "anthropic", "own"] });
|
|
124
|
+
|
|
125
|
+
expect(output).toContain("NUXT_MAPO_AI_SECRET=");
|
|
126
|
+
expect(output).toContain("NUXT_MAPO_AI_PROVIDERS_OPENAI_API_KEY=");
|
|
127
|
+
expect(output).toContain("NUXT_MAPO_AI_PROVIDERS_ANTHROPIC_API_KEY=");
|
|
128
|
+
expect(output).toContain("NUXT_MAPO_AI_PROVIDERS_MY_AI_API_KEY=");
|
|
129
|
+
// A variable is followed by spaces and a comment, never by a value.
|
|
130
|
+
expect(output).not.toMatch(/^#? ?NUXT_[A-Z_]+=[^\s]/m);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("leaves the project's key out when every key comes from the admins", () => {
|
|
134
|
+
const output = run({ providers: ["openai"], keys: ["assigned"] });
|
|
135
|
+
|
|
136
|
+
expect(output).not.toContain("NUXT_MAPO_AI_PROVIDERS_OPENAI_API_KEY=");
|
|
137
|
+
expect(output).toContain("**The admins' keys**");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("explains the tokens: usage, costs, limits and how to spend less", () => {
|
|
141
|
+
const output = run({ providers: ["anthropic"], costs: true });
|
|
142
|
+
|
|
143
|
+
expect(output).toContain("### The tokens a call spends");
|
|
144
|
+
expect(output).toContain("`mapo.ai.prices`");
|
|
145
|
+
// A price the developer must write: no number that would read as free.
|
|
146
|
+
expect(output).toContain('// "<model>": { input: …, output: … }');
|
|
147
|
+
expect(output).not.toContain("input: 0");
|
|
148
|
+
expect(output).toContain("perUserPerMinute");
|
|
149
|
+
expect(output).toContain("promptCache: true");
|
|
150
|
+
expect(output).toContain("no spending cap per month");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("warns when the data must stay home but a cloud provider is chosen", () => {
|
|
154
|
+
const output = run({ dataMayLeave: false, providers: ["openai"] });
|
|
155
|
+
|
|
156
|
+
expect(output).toContain("## Check these first");
|
|
157
|
+
expect(output).toContain("choose `ollama` or `openai-compatible` instead");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("takes no limit of 0, which would take no call at all", async () => {
|
|
161
|
+
const { z } = await import("zod");
|
|
162
|
+
const schema = z.object(aiSetupTool.inputSchema);
|
|
163
|
+
|
|
164
|
+
expect(schema.safeParse({ perUserPerMinute: 0 }).success).toBe(false);
|
|
165
|
+
expect(schema.safeParse({ perUserPerMinute: 1 }).success).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("asks the questions when the answers are blank, and writes nothing", () => {
|
|
169
|
+
const output = run({ instructions: " ", allowedFlag: "", write: true });
|
|
170
|
+
|
|
171
|
+
expect(output).toContain("Nothing was written: the answers decide");
|
|
172
|
+
expect(output).toContain("## Questions for the developer");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("lists what is still to ask, with what the plan assumed", () => {
|
|
176
|
+
const output = run({ wants: ["fill-record"] });
|
|
177
|
+
|
|
178
|
+
expect(output).toContain("## Still to ask");
|
|
179
|
+
expect(output).toContain('"Which models?" (assumed: no provider');
|
|
180
|
+
expect(output).not.toContain('"What should the AI do for the operators?"');
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("mapo_ai_setup: the files of the app", () => {
|
|
185
|
+
const answers = {
|
|
186
|
+
providers: ["openai" as const],
|
|
187
|
+
allowedFlag: "is_internal",
|
|
188
|
+
ownFormat: ".ics",
|
|
189
|
+
deploy: "several-instances" as const,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
it("shows the plugins the answers call for, and writes nothing", () => {
|
|
193
|
+
const output = run(answers);
|
|
194
|
+
|
|
195
|
+
expect(output).toContain("### `server/plugins/mapo-ai-access.ts`");
|
|
196
|
+
expect(output).toContain('context.payload["is_internal"] !== true');
|
|
197
|
+
expect(output).toContain("### `server/plugins/mapo-ai-readers.ts`");
|
|
198
|
+
expect(output).toContain('extensions: ["ics"]');
|
|
199
|
+
expect(output).toContain("### `server/plugins/mapo-ai-storage.ts`");
|
|
200
|
+
expect(output).toContain("Nothing was written");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("drafts no hook for a field name that would break out of its comment", () => {
|
|
204
|
+
const output = run({
|
|
205
|
+
providers: ["openai"],
|
|
206
|
+
allowedFlag: "is_internal\nprocess.exit(1)",
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
expect(output).not.toContain("mapo-ai-access.ts");
|
|
210
|
+
expect(output).not.toContain("process.exit(1)\n");
|
|
211
|
+
expect(output).toContain("is not a field name");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("gives a reader an id that starts with a letter and is none of Mapo's", () => {
|
|
215
|
+
const digits = run({ providers: ["openai"], ownFormat: "7z" });
|
|
216
|
+
const named = run({ providers: ["openai"], ownFormat: "text" });
|
|
217
|
+
|
|
218
|
+
expect(digits).toContain('id: "format-7z"');
|
|
219
|
+
expect(digits).toContain('extensions: ["7z"]');
|
|
220
|
+
expect(named).toContain('id: "format-text"');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("drafts no reader for a format Mapo reads, which it would replace", () => {
|
|
224
|
+
const output = run({ providers: ["openai"], ownFormat: "XLSX" });
|
|
225
|
+
|
|
226
|
+
expect(output).not.toContain("mapo-ai-readers.ts");
|
|
227
|
+
expect(output).toContain("Mapo reads `.xlsx` files already");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("keeps the file system in development, and needs REDIS_URL in production", () => {
|
|
231
|
+
const output = run({ providers: ["openai"], deploy: "several-instances" });
|
|
232
|
+
|
|
233
|
+
expect(output).toContain("if (import.meta.dev) return;");
|
|
234
|
+
expect(output).toContain('throw new Error("REDIS_URL is not set');
|
|
235
|
+
expect(output).toContain("`ioredis` installed in the app");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("sends an edge runtime to a driver over HTTP", () => {
|
|
239
|
+
const output = run({ providers: ["openai"], deploy: "serverless" });
|
|
240
|
+
|
|
241
|
+
expect(output).toContain("An edge runtime");
|
|
242
|
+
expect(output).toContain("over HTTP");
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("drafts no reader for an extension it cannot use", () => {
|
|
246
|
+
const output = run({ providers: ["openai"], ownFormat: "../x" });
|
|
247
|
+
|
|
248
|
+
expect(output).not.toContain("mapo-ai-readers.ts");
|
|
249
|
+
expect(output).toContain("is not a file extension");
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("writes them only when asked, inside the project, never over a file", async () => {
|
|
253
|
+
const root = mkdtempSync(join(tmpdir(), "mapo-ai-setup-"));
|
|
254
|
+
const writing = makeContext(undefined, makeManifest({ rootDir: root }));
|
|
255
|
+
|
|
256
|
+
const first = (await aiSetupTool.run(
|
|
257
|
+
{ ...answers, write: true },
|
|
258
|
+
writing,
|
|
259
|
+
)) as string;
|
|
260
|
+
const second = (await aiSetupTool.run(
|
|
261
|
+
{ ...answers, write: true },
|
|
262
|
+
writing,
|
|
263
|
+
)) as string;
|
|
264
|
+
|
|
265
|
+
const access = join(root, "server/plugins/mapo-ai-access.ts");
|
|
266
|
+
expect(existsSync(access)).toBe(true);
|
|
267
|
+
expect(readFileSync(access, "utf8")).toContain('hook("mapo-ai:user"');
|
|
268
|
+
expect(first).toContain("(written)");
|
|
269
|
+
expect(second).toContain("(exists)");
|
|
270
|
+
// The configuration is never written: only shown.
|
|
271
|
+
expect(existsSync(join(root, "nuxt.config.ts"))).toBe(false);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("writes nothing outside development", async () => {
|
|
275
|
+
const root = mkdtempSync(join(tmpdir(), "mapo-ai-setup-"));
|
|
276
|
+
const production = makeContext(
|
|
277
|
+
undefined,
|
|
278
|
+
makeManifest({ rootDir: root, dev: false }),
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
const output = (await aiSetupTool.run(
|
|
282
|
+
{ ...answers, write: true },
|
|
283
|
+
production,
|
|
284
|
+
)) as string;
|
|
285
|
+
|
|
286
|
+
expect(output).toContain("(not-allowed)");
|
|
287
|
+
expect(existsSync(join(root, "server/plugins/mapo-ai-access.ts"))).toBe(
|
|
288
|
+
false,
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
describe("mapo_ai_setup: the app it runs in", () => {
|
|
294
|
+
it("says what the app lacks: the module, uikit, the order of the modules", () => {
|
|
295
|
+
const manifest = makeManifest({
|
|
296
|
+
modules: [{ name: "@mapomodule/core" }],
|
|
297
|
+
moduleOrder: ["mapomodule", "@nuxt/ui"],
|
|
298
|
+
});
|
|
299
|
+
const output = aiSetupTool.run(
|
|
300
|
+
{ providers: ["openai"] },
|
|
301
|
+
makeContext(undefined, manifest),
|
|
302
|
+
) as string;
|
|
303
|
+
|
|
304
|
+
expect(output).toContain("`@mapomodule/ai` is not installed yet");
|
|
305
|
+
expect(output).toContain("uikit is not installed: Magic Fill needs it");
|
|
306
|
+
expect(output).toContain("Magic Fill needs `@mapomodule/uikit`");
|
|
307
|
+
expect(output).toContain("Register `@nuxt/ui` before `mapomodule`");
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("says what is there already", () => {
|
|
311
|
+
const manifest = makeManifest({
|
|
312
|
+
modules: [{ name: "@mapomodule/ai" }, { name: "@mapomodule/uikit" }],
|
|
313
|
+
routes: [{ path: "/ai-settings" }],
|
|
314
|
+
});
|
|
315
|
+
const output = aiSetupTool.run(
|
|
316
|
+
{},
|
|
317
|
+
makeContext(undefined, manifest),
|
|
318
|
+
) as string;
|
|
319
|
+
|
|
320
|
+
expect(output).toContain("`@mapomodule/ai` is installed");
|
|
321
|
+
expect(output).toContain("The AI settings page answers at `/ai-settings`");
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("points at mapo_inspect_app when no app is connected", () => {
|
|
325
|
+
const output = aiSetupTool.run({}, makeContext(undefined, null)) as string;
|
|
326
|
+
|
|
327
|
+
expect(output).toContain("No app is connected");
|
|
328
|
+
expect(output).toContain("mapo_inspect_app");
|
|
329
|
+
});
|
|
330
|
+
});
|
|
@@ -45,6 +45,7 @@ describeBuilt("mapo-mcp stdio server", () => {
|
|
|
45
45
|
it("advertises every tool that works without the dev server", async () => {
|
|
46
46
|
const { tools } = await client.listTools();
|
|
47
47
|
expect(tools.map((tool) => tool.name).sort()).toEqual([
|
|
48
|
+
"mapo_ai_setup",
|
|
48
49
|
"mapo_backend_schema",
|
|
49
50
|
"mapo_component_api",
|
|
50
51
|
"mapo_composable_api",
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
5
|
+
import { extractComposables } from "../build/extract-composables";
|
|
6
|
+
|
|
7
|
+
let repoRoot: string;
|
|
8
|
+
|
|
9
|
+
/** A repository with one Mapo module that auto-imports for the app and for its server. */
|
|
10
|
+
function writeModule(): void {
|
|
11
|
+
const pkg = join(repoRoot, "packages", "@mapomodule", "demo", "src");
|
|
12
|
+
mkdirSync(join(pkg, "runtime"), { recursive: true });
|
|
13
|
+
writeFileSync(
|
|
14
|
+
join(pkg, "module.ts"),
|
|
15
|
+
`
|
|
16
|
+
addImports([
|
|
17
|
+
{ name: "useDemo", from: resolver.resolve("./runtime/useDemo") },
|
|
18
|
+
]);
|
|
19
|
+
addServerImports([
|
|
20
|
+
{ name: "registerDemoServer", from: resolver.resolve("./runtime/server") },
|
|
21
|
+
]);
|
|
22
|
+
`,
|
|
23
|
+
);
|
|
24
|
+
writeFileSync(
|
|
25
|
+
join(pkg, "runtime", "useDemo.ts"),
|
|
26
|
+
"/** A composable of the app. */\nexport function useDemo(): number {\n return 1;\n}\n",
|
|
27
|
+
);
|
|
28
|
+
writeFileSync(
|
|
29
|
+
join(pkg, "runtime", "server.ts"),
|
|
30
|
+
"/** For the server's code only. */\nexport function registerDemoServer(): void {}\n",
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
repoRoot = mkdtempSync(join(tmpdir(), "mapo-composables-"));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
rmSync(repoRoot, { recursive: true, force: true });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("extractComposables", () => {
|
|
43
|
+
it("lists what addImports gives the app, with its description", () => {
|
|
44
|
+
writeModule();
|
|
45
|
+
|
|
46
|
+
const composables = extractComposables(repoRoot, () => []);
|
|
47
|
+
|
|
48
|
+
expect(composables).toMatchObject([
|
|
49
|
+
{
|
|
50
|
+
name: "useDemo",
|
|
51
|
+
pkg: "@mapomodule/demo",
|
|
52
|
+
description: "A composable of the app.",
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("reads past a parenthesis in a comment or a string of the call", () => {
|
|
58
|
+
const pkg = join(repoRoot, "packages", "@mapomodule", "demo", "src");
|
|
59
|
+
mkdirSync(join(pkg, "runtime"), { recursive: true });
|
|
60
|
+
writeFileSync(
|
|
61
|
+
join(pkg, "module.ts"),
|
|
62
|
+
`
|
|
63
|
+
addImports([
|
|
64
|
+
// the first one :)
|
|
65
|
+
{ name: "useFirst", from: resolver.resolve("./runtime/first") },
|
|
66
|
+
/* ) */ { name: "useSecond", from: resolver.resolve("./runtime/second") },
|
|
67
|
+
{ name: "useThird", from: resolver.resolve(")" && "./runtime/third") },
|
|
68
|
+
]);
|
|
69
|
+
`,
|
|
70
|
+
);
|
|
71
|
+
for (const name of ["first", "second", "third"]) {
|
|
72
|
+
const exported = `use${name[0]?.toUpperCase()}${name.slice(1)}`;
|
|
73
|
+
writeFileSync(
|
|
74
|
+
join(pkg, "runtime", `${name}.ts`),
|
|
75
|
+
`export function ${exported}(): void {}\n`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const names = extractComposables(repoRoot, () => []).map(
|
|
80
|
+
(composable) => composable.name,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(names).toEqual(["useFirst", "useSecond", "useThird"]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("leaves out what addServerImports gives the server's code", () => {
|
|
87
|
+
writeModule();
|
|
88
|
+
|
|
89
|
+
const names = extractComposables(repoRoot, () => []).map(
|
|
90
|
+
(composable) => composable.name,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(names).not.toContain("registerDemoServer");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -29,6 +29,25 @@ describe("tokenize", () => {
|
|
|
29
29
|
});
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
describe("buildIndex", () => {
|
|
33
|
+
it("indexes a word named like an object property, such as constructor", () => {
|
|
34
|
+
const chunks = chunkMarkdown(
|
|
35
|
+
"modules/utils.md",
|
|
36
|
+
"# Utils\n\n## Flags\n\nA model named constructor gets flags of its own.\n",
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const index = buildIndex(chunks);
|
|
40
|
+
|
|
41
|
+
expect(Object.hasOwn(index.terms, "constructor")).toBe(true);
|
|
42
|
+
expect(index.terms.constructor).toEqual(
|
|
43
|
+
expect.arrayContaining([expect.any(Array)]),
|
|
44
|
+
);
|
|
45
|
+
expect(JSON.parse(JSON.stringify(index)).terms.constructor).toEqual(
|
|
46
|
+
index.terms.constructor,
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
32
51
|
describe("splitSections", () => {
|
|
33
52
|
it("extracts the title and one section per H2", () => {
|
|
34
53
|
const { title, sections } = splitSections(CRUD_LIST_DOC);
|
|
@@ -15,6 +15,11 @@ import {
|
|
|
15
15
|
resolveKnowledgeDir,
|
|
16
16
|
} from "../runtime/core/knowledge.js";
|
|
17
17
|
import { overviewSummary } from "../runtime/core/overview.js";
|
|
18
|
+
import {
|
|
19
|
+
AI_NEEDS,
|
|
20
|
+
AI_TOPICS,
|
|
21
|
+
aiTopic,
|
|
22
|
+
} from "../runtime/core/ai-setup/index.js";
|
|
18
23
|
import { RECIPES } from "../runtime/core/recipes.js";
|
|
19
24
|
import { getDoc, searchDocs } from "../runtime/core/search.js";
|
|
20
25
|
|
|
@@ -95,6 +100,35 @@ describeBuilt("generated knowledge base", () => {
|
|
|
95
100
|
}
|
|
96
101
|
});
|
|
97
102
|
|
|
103
|
+
it("keeps every topic of the AI setup pointing at a real page and section", () => {
|
|
104
|
+
const pages = new Map(
|
|
105
|
+
knowledge.chunks.map((chunk) => [chunk.path, new Set<string>()] as const),
|
|
106
|
+
);
|
|
107
|
+
for (const chunk of knowledge.chunks)
|
|
108
|
+
pages.get(chunk.path)!.add(chunk.heading);
|
|
109
|
+
|
|
110
|
+
for (const topic of AI_TOPICS) {
|
|
111
|
+
expect(
|
|
112
|
+
pages.has(topic.doc),
|
|
113
|
+
`topic "${topic.id}" → missing doc ${topic.doc}`,
|
|
114
|
+
).toBe(true);
|
|
115
|
+
if (topic.heading) {
|
|
116
|
+
expect(
|
|
117
|
+
[...pages.get(topic.doc)!],
|
|
118
|
+
`topic "${topic.id}" → missing heading in ${topic.doc}`,
|
|
119
|
+
).toContain(topic.heading);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
for (const need of AI_NEEDS) {
|
|
123
|
+
for (const id of need.topics) {
|
|
124
|
+
expect(
|
|
125
|
+
aiTopic(id),
|
|
126
|
+
`need "${need.id}" → unknown topic ${id}`,
|
|
127
|
+
).toBeDefined();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
98
132
|
it("reads a real page back", () => {
|
|
99
133
|
const doc = getDoc(knowledge, "howto/crud-list.md")!;
|
|
100
134
|
expect(doc.title).toBeTruthy();
|
|
@@ -113,6 +147,20 @@ describeBuilt("generated API surface", () => {
|
|
|
113
147
|
expect(api.components.length).toBeGreaterThan(30);
|
|
114
148
|
});
|
|
115
149
|
|
|
150
|
+
it("extracts the components @mapomodule/ai registers for apps, not the parts of its settings page", () => {
|
|
151
|
+
const ai = api.components
|
|
152
|
+
.filter((component) => component.pkg === "@mapomodule/ai")
|
|
153
|
+
.map((component) => component.name);
|
|
154
|
+
|
|
155
|
+
expect(ai).toEqual([
|
|
156
|
+
"MapoAiMagicFillAction",
|
|
157
|
+
"MapoAiMagicFillBanner",
|
|
158
|
+
"MapoAiMagicFillPanel",
|
|
159
|
+
"MapoAiMagicFillPreview",
|
|
160
|
+
"MapoAiSettings",
|
|
161
|
+
]);
|
|
162
|
+
});
|
|
163
|
+
|
|
116
164
|
it("reports real prop types and JSDoc, not documentation prose", () => {
|
|
117
165
|
const list = api.components.find(
|
|
118
166
|
(component) => component.name === "MapoList",
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { MAPO_INSTRUCTIONS } from "../runtime/core/instructions.js";
|
|
3
|
+
import { PROMPTS } from "../runtime/core/prompts.js";
|
|
3
4
|
import { overviewSummary, renderOverview } from "../runtime/core/overview.js";
|
|
4
5
|
import { OVERVIEW_DOC, OVERVIEW_URI } from "../runtime/core/resources.js";
|
|
6
|
+
import { aiTools } from "../runtime/core/tools/ai.js";
|
|
5
7
|
import { apiTools } from "../runtime/core/tools/api.js";
|
|
6
8
|
import { appTools } from "../runtime/core/tools/app.js";
|
|
7
9
|
import { backendTools } from "../runtime/core/tools/backend.js";
|
|
@@ -90,6 +92,7 @@ describe("server instructions", () => {
|
|
|
90
92
|
...appTools,
|
|
91
93
|
...scaffoldTools,
|
|
92
94
|
...backendTools,
|
|
95
|
+
...aiTools,
|
|
93
96
|
].map((tool) => tool.name);
|
|
94
97
|
|
|
95
98
|
it("name only tools that exist", () => {
|
|
@@ -98,6 +101,14 @@ describe("server instructions", () => {
|
|
|
98
101
|
for (const name of named) expect(toolNames, name).toContain(name);
|
|
99
102
|
});
|
|
100
103
|
|
|
104
|
+
it("let every prompt name only tools that exist", () => {
|
|
105
|
+
for (const prompt of PROMPTS) {
|
|
106
|
+
const named = prompt.render({}).match(/\bmapo_[a-z_]+/g) ?? [];
|
|
107
|
+
expect(named.length, prompt.name).toBeGreaterThan(0);
|
|
108
|
+
for (const name of named) expect(toolNames, prompt.name).toContain(name);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
101
112
|
it("point at the overview", () => {
|
|
102
113
|
expect(MAPO_INSTRUCTIONS).toContain(OVERVIEW_URI);
|
|
103
114
|
expect(MAPO_INSTRUCTIONS).toContain(OVERVIEW_DOC);
|
|
@@ -25,8 +25,22 @@ describe("matchRecipes", () => {
|
|
|
25
25
|
["show a field only when another field is checked", "conditional-fields"],
|
|
26
26
|
["autosave a draft while the user edits", "form-draft"],
|
|
27
27
|
["generate the fields from a json schema", "schema-to-form"],
|
|
28
|
+
["describe my form as a json schema", "form-to-schema"],
|
|
29
|
+
["read the DRF OPTIONS metadata into form fields", "schema-sources"],
|
|
30
|
+
["add a button to every detail page", "detail-extensions"],
|
|
31
|
+
[
|
|
32
|
+
"read the current record from a component inside the detail",
|
|
33
|
+
"detail-context",
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
"add a typed property of my own to every field descriptor",
|
|
37
|
+
"descriptor-extensions",
|
|
38
|
+
],
|
|
28
39
|
["put a hint below one form field with a slot", "form-slots"],
|
|
29
40
|
["edit a repeater item fullscreen", "focus-mode"],
|
|
41
|
+
["mark a field as suggested by AI", "field-annotations"],
|
|
42
|
+
["show which fields were filled by the assistant", "field-annotations"],
|
|
43
|
+
["read the annotation of a field in my custom field", "field-annotations"],
|
|
30
44
|
["let editors choose a content block from a block picker", "block-editor"],
|
|
31
45
|
["add my page to the sidebar with an icon", "sidebar-navigation"],
|
|
32
46
|
["how to reorder sidebar/menu entries", "sidebar-navigation"],
|
|
@@ -34,6 +48,64 @@ describe("matchRecipes", () => {
|
|
|
34
48
|
["TS2307 cannot find module mapomodule/types", "imports"],
|
|
35
49
|
["connect claude code or copilot through mcp", "ai-assistant"],
|
|
36
50
|
["set up the camomilla session cookie", "camomilla"],
|
|
51
|
+
["guide me through setting up the AI in my backoffice", "ai-setup"],
|
|
52
|
+
["what can the AI module do for my project", "ai-setup"],
|
|
53
|
+
["ask the AI to fill a form from a pasted text", "ai-fill"],
|
|
54
|
+
["fill the detail form with values the AI verified", "ai-fill"],
|
|
55
|
+
["use magic fill on a detail page", "ai-magic-fill"],
|
|
56
|
+
["disable magic fill on one page", "ai-magic-fill"],
|
|
57
|
+
["show the operator what Magic Fill sends to the AI", "ai-magic-fill"],
|
|
58
|
+
["list the proposals without proof the AI gave", "ai-magic-fill"],
|
|
59
|
+
["apply selected values of Magic Fill before and after", "ai-magic-fill"],
|
|
60
|
+
["choose the items of a list Magic Fill proposes", "ai-magic-fill"],
|
|
61
|
+
["bring back Magic Fill's marks after a reload", "ai-magic-fill"],
|
|
62
|
+
["go to the next field Magic Fill left to check", "ai-magic-fill"],
|
|
63
|
+
["what does a Magic Fill error mean and what to do", "ai-magic-fill"],
|
|
64
|
+
["give the AI a hint about one field of the form", "ai-field-options"],
|
|
65
|
+
[
|
|
66
|
+
"which function writes the ai hints of my fields for the run route",
|
|
67
|
+
"ai-field-options",
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
"keep the names of a relation's records away from the AI provider",
|
|
71
|
+
"ai-field-options",
|
|
72
|
+
],
|
|
73
|
+
["keep my detail extension's state in the draft", "detail-context"],
|
|
74
|
+
["leave a password field out of the saved draft", "form-draft"],
|
|
75
|
+
["apply the values the AI proposed to the form", "ai-fill"],
|
|
76
|
+
["call the AI gateway's run route to extract records", "ai-run"],
|
|
77
|
+
["read the outcome of each field from the run route", "ai-run"],
|
|
78
|
+
["register my own LLM provider for the AI gateway", "ai-providers"],
|
|
79
|
+
["use a local model with ollama for the AI", "ai-local-models"],
|
|
80
|
+
["connect the AI gateway to LM Studio or vLLM", "ai-local-models"],
|
|
81
|
+
["use claude models from anthropic for the AI", "ai-cloud-models"],
|
|
82
|
+
["set the openai api key for the AI gateway", "ai-cloud-models"],
|
|
83
|
+
["turn on the prompt cache of the AI provider", "ai-cloud-models"],
|
|
84
|
+
["raise the reasoning effort of the AI provider", "ai-cloud-models"],
|
|
85
|
+
["set the api key of the ollama server", "ai-local-models"],
|
|
86
|
+
["let the operator upload an excel file for the AI", "ai-read-files"],
|
|
87
|
+
["give the AI a csv spreadsheet instead of pasted text", "ai-read-files"],
|
|
88
|
+
["replace the reader of html files in the AI gateway", "ai-readers"],
|
|
89
|
+
["turn off xlsx files for the AI", "ai-readers"],
|
|
90
|
+
["let the operator give the AI the link of a web page", "ai-read-links"],
|
|
91
|
+
[
|
|
92
|
+
"read a url server side without ssrf to private networks",
|
|
93
|
+
"ai-read-links",
|
|
94
|
+
],
|
|
95
|
+
["assign a different openai key to the editors group", "ai-keys"],
|
|
96
|
+
["let an operator use their own personal token for the AI", "ai-keys"],
|
|
97
|
+
["add a settings page for the AI to my backoffice", "ai-settings"],
|
|
98
|
+
["let users pick their AI model and check the connection", "ai-settings"],
|
|
99
|
+
[
|
|
100
|
+
"keep a vault field away from the AI with secretWords",
|
|
101
|
+
"ai-admin-settings",
|
|
102
|
+
],
|
|
103
|
+
[
|
|
104
|
+
"let the admins add an OpenAI model without a deploy",
|
|
105
|
+
"ai-admin-settings",
|
|
106
|
+
],
|
|
107
|
+
["how much does the AI cost per user this month", "ai-usage"],
|
|
108
|
+
["show the tokens used by each model", "ai-usage"],
|
|
37
109
|
])("maps %j to the %j recipe", (query, expectedId) => {
|
|
38
110
|
expect(matchRecipes(query)[0]?.id).toBe(expectedId);
|
|
39
111
|
});
|
|
@@ -47,6 +119,17 @@ describe("matchRecipes", () => {
|
|
|
47
119
|
expect(ids.length).toBeGreaterThan(1);
|
|
48
120
|
});
|
|
49
121
|
|
|
122
|
+
it("keeps a question about linking records away from reading a web page", () => {
|
|
123
|
+
for (const query of [
|
|
124
|
+
"link an author to the article",
|
|
125
|
+
"link a record to another record",
|
|
126
|
+
"a field for a link",
|
|
127
|
+
]) {
|
|
128
|
+
const ids = matchRecipes(query).map((recipe) => recipe.id);
|
|
129
|
+
expect(ids, query).not.toContain("ai-read-links");
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
50
133
|
it("stays silent when the query is unrelated", () => {
|
|
51
134
|
expect(matchRecipes("what is the weather today")).toEqual([]);
|
|
52
135
|
});
|