@mapomodule/mcp 1.0.0-beta.2 → 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 +408 -4
- 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 +69 -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 +429 -4
- 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,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The guide to `@mapomodule/ai`: it asks the developer what the AI is for,
|
|
3
|
+
* says what the module can and cannot do about it, and proposes the
|
|
4
|
+
* configuration, the keys, the files and the checks.
|
|
5
|
+
*
|
|
6
|
+
* Without answers it returns the questions, in rounds; with them, the plan.
|
|
7
|
+
* Like `mapo_scaffold`, it only shows the files of the app unless asked to
|
|
8
|
+
* write them (`write: true`, through `../write.ts`), and it never edits
|
|
9
|
+
* `nuxt.config.ts` nor writes a key anywhere.
|
|
10
|
+
*/
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import {
|
|
13
|
+
AI_DEPLOY_CHOICES,
|
|
14
|
+
AI_KEY_CHOICES,
|
|
15
|
+
AI_MAGIC_FILL_CHOICES,
|
|
16
|
+
AI_NEED_IDS,
|
|
17
|
+
AI_PROVIDER_CHOICES,
|
|
18
|
+
aiSetupFiles,
|
|
19
|
+
hasAiAnswers,
|
|
20
|
+
renderAiSetupPlan,
|
|
21
|
+
renderAiSetupQuestions,
|
|
22
|
+
} from "../ai-setup/index.js";
|
|
23
|
+
import type { AiSetupAnswers } from "../ai-setup/index.js";
|
|
24
|
+
import { defineMapoTool } from "../tool-spec.js";
|
|
25
|
+
import { writeScaffold } from "../write.js";
|
|
26
|
+
import type { WriteRequest } from "../write.js";
|
|
27
|
+
import type { AnyMapoToolSpec, MapoToolContext } from "../tool-spec.js";
|
|
28
|
+
|
|
29
|
+
interface AiSetupArgs extends AiSetupAnswers {
|
|
30
|
+
write?: boolean;
|
|
31
|
+
overwrite?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The files, shown for review, or what became of them once written. */
|
|
35
|
+
function filesSection(
|
|
36
|
+
files: WriteRequest[],
|
|
37
|
+
args: AiSetupArgs,
|
|
38
|
+
context: MapoToolContext,
|
|
39
|
+
): string {
|
|
40
|
+
if (!files.length) return "None: the answers need no server code of the app.";
|
|
41
|
+
if (!args.write) {
|
|
42
|
+
const blocks = files.map(
|
|
43
|
+
(file) => `### \`${file.path}\`\n\n\`\`\`ts\n${file.content}\`\`\``,
|
|
44
|
+
);
|
|
45
|
+
return `${blocks.join("\n\n")}\n\nNothing was written. Call again with \`write: true\` to create these files.`;
|
|
46
|
+
}
|
|
47
|
+
const root = context.rootDir();
|
|
48
|
+
if (!root) {
|
|
49
|
+
return "Cannot write: the project root is unknown. Connect to the app's endpoint (`/mcp/mapo`), or run `mapo-mcp serve --root <path>`.";
|
|
50
|
+
}
|
|
51
|
+
const outcomes = writeScaffold(files, {
|
|
52
|
+
root,
|
|
53
|
+
overwrite: args.overwrite ?? false,
|
|
54
|
+
allowed: context.canWrite(),
|
|
55
|
+
});
|
|
56
|
+
const lines = outcomes.map((outcome) => {
|
|
57
|
+
const icon = outcome.status === "written" ? "✔" : "✖";
|
|
58
|
+
const detail = outcome.reason ? `: ${outcome.reason}` : "";
|
|
59
|
+
return `- ${icon} \`${outcome.path}\` (${outcome.status})${detail}`;
|
|
60
|
+
});
|
|
61
|
+
return `Root: \`${root}\`\n\n${lines.join("\n")}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const aiSetupTool = defineMapoTool({
|
|
65
|
+
name: "mapo_ai_setup",
|
|
66
|
+
title: "Set up the AI and Magic Fill",
|
|
67
|
+
description:
|
|
68
|
+
"Guide a developer through turning on @mapomodule/ai (experimental) and Magic Fill: without answers, " +
|
|
69
|
+
"the questions to ask them, in rounds, with every need the module can meet and everything it offers; " +
|
|
70
|
+
"with their answers, whether the module can do what they need, the `mapo.ai` configuration, the " +
|
|
71
|
+
"environment's variables (names only), where each call's key comes from and how to keep the tokens " +
|
|
72
|
+
"and their cost in check, the forms' options, the server plugins of the app and the checks. Shows " +
|
|
73
|
+
"the files unless `write: true`; never edits nuxt.config.ts, never writes a key.",
|
|
74
|
+
inputSchema: {
|
|
75
|
+
need: z
|
|
76
|
+
.string()
|
|
77
|
+
.max(4000)
|
|
78
|
+
.optional()
|
|
79
|
+
.describe("The developer's need, in their own words"),
|
|
80
|
+
wants: z
|
|
81
|
+
.array(z.enum(AI_NEED_IDS))
|
|
82
|
+
.optional()
|
|
83
|
+
.describe(
|
|
84
|
+
"The need mapped to the module's needs, such as `fill-record`, `extract-records`, `pdf-images`, `private-relations`, `team-keys`, `costs`: the questions list them all",
|
|
85
|
+
),
|
|
86
|
+
dataMayLeave: z
|
|
87
|
+
.boolean()
|
|
88
|
+
.optional()
|
|
89
|
+
.describe(
|
|
90
|
+
"Whether the records and their sources may go to a cloud provider",
|
|
91
|
+
),
|
|
92
|
+
providers: z
|
|
93
|
+
.array(z.enum(AI_PROVIDER_CHOICES))
|
|
94
|
+
.optional()
|
|
95
|
+
.describe(
|
|
96
|
+
"The providers to enable; `own` is a provider of the app's own",
|
|
97
|
+
),
|
|
98
|
+
keys: z
|
|
99
|
+
.array(z.enum(AI_KEY_CHOICES))
|
|
100
|
+
.optional()
|
|
101
|
+
.describe(
|
|
102
|
+
"Where the keys come from: the project's (environment), the admins' assignments, users' own tokens",
|
|
103
|
+
),
|
|
104
|
+
userGroups: z
|
|
105
|
+
.array(z.string())
|
|
106
|
+
.optional()
|
|
107
|
+
.describe("The groups that may use the AI (`mapo.ai.users.groups`)"),
|
|
108
|
+
userPermissions: z
|
|
109
|
+
.array(z.string())
|
|
110
|
+
.optional()
|
|
111
|
+
.describe("The permissions that let a user use the AI"),
|
|
112
|
+
allowedFlag: z
|
|
113
|
+
.string()
|
|
114
|
+
.max(64)
|
|
115
|
+
.optional()
|
|
116
|
+
.describe(
|
|
117
|
+
"A field of the user the backend returns that must be `true` for the AI, such as `is_internal`",
|
|
118
|
+
),
|
|
119
|
+
adminGroups: z
|
|
120
|
+
.array(z.string())
|
|
121
|
+
.optional()
|
|
122
|
+
.describe("The groups that administer the AI (`mapo.ai.admin.groups`)"),
|
|
123
|
+
costs: z
|
|
124
|
+
.boolean()
|
|
125
|
+
.optional()
|
|
126
|
+
.describe("Whether the usage shows costs, with the models' prices"),
|
|
127
|
+
perUserPerMinute: z
|
|
128
|
+
.number()
|
|
129
|
+
.int()
|
|
130
|
+
.min(1)
|
|
131
|
+
.max(1000)
|
|
132
|
+
.optional()
|
|
133
|
+
.describe("The calls to `run` one user may make in a minute"),
|
|
134
|
+
magicFill: z
|
|
135
|
+
.enum(AI_MAGIC_FILL_CHOICES)
|
|
136
|
+
.optional()
|
|
137
|
+
.describe("Where Magic Fill shows, or the app's own panel instead"),
|
|
138
|
+
instructions: z
|
|
139
|
+
.string()
|
|
140
|
+
.max(2000)
|
|
141
|
+
.optional()
|
|
142
|
+
.describe(
|
|
143
|
+
"Rules the AI follows in every prompt (`mapo.ai.instructions.all`)",
|
|
144
|
+
),
|
|
145
|
+
ownFormat: z
|
|
146
|
+
.string()
|
|
147
|
+
.max(16)
|
|
148
|
+
.optional()
|
|
149
|
+
.describe(
|
|
150
|
+
"The extension of a file format the app reads itself, such as `ics`",
|
|
151
|
+
),
|
|
152
|
+
deploy: z
|
|
153
|
+
.enum(AI_DEPLOY_CHOICES)
|
|
154
|
+
.optional()
|
|
155
|
+
.describe("Where the server runs in production"),
|
|
156
|
+
write: z
|
|
157
|
+
.boolean()
|
|
158
|
+
.optional()
|
|
159
|
+
.describe(
|
|
160
|
+
"Create the plugin files instead of only showing them (development only)",
|
|
161
|
+
),
|
|
162
|
+
overwrite: z
|
|
163
|
+
.boolean()
|
|
164
|
+
.optional()
|
|
165
|
+
.describe("Allow replacing files that already exist"),
|
|
166
|
+
},
|
|
167
|
+
annotations: {
|
|
168
|
+
// Not read-only: with `write` it creates the plugins. Never destructive:
|
|
169
|
+
// an existing file is refused unless `overwrite` is set.
|
|
170
|
+
readOnlyHint: false,
|
|
171
|
+
destructiveHint: false,
|
|
172
|
+
idempotentHint: true,
|
|
173
|
+
openWorldHint: false,
|
|
174
|
+
},
|
|
175
|
+
inputExamples: [
|
|
176
|
+
{ need: "Our editors paste press releases into the event form" },
|
|
177
|
+
{
|
|
178
|
+
need: "Editors fill articles from press releases; no data may leave the company",
|
|
179
|
+
wants: ["fill-record", "files", "local-only"],
|
|
180
|
+
dataMayLeave: false,
|
|
181
|
+
providers: ["ollama"],
|
|
182
|
+
keys: ["project"],
|
|
183
|
+
deploy: "single-node",
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
wants: ["fill-record", "team-keys", "costs"],
|
|
187
|
+
providers: ["openai"],
|
|
188
|
+
keys: ["project", "assigned"],
|
|
189
|
+
userGroups: ["editors"],
|
|
190
|
+
costs: true,
|
|
191
|
+
perUserPerMinute: 10,
|
|
192
|
+
deploy: "several-instances",
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
run: (args: AiSetupArgs, context: MapoToolContext) => {
|
|
196
|
+
const manifest = context.manifest();
|
|
197
|
+
if (!hasAiAnswers(args)) {
|
|
198
|
+
const questions = renderAiSetupQuestions(args, manifest);
|
|
199
|
+
// Nothing to write before the answers say what the app needs.
|
|
200
|
+
return args.write
|
|
201
|
+
? `Nothing was written: the answers decide which files the app needs.\n\n${questions}`
|
|
202
|
+
: questions;
|
|
203
|
+
}
|
|
204
|
+
const files = filesSection(aiSetupFiles(args), args, context);
|
|
205
|
+
return renderAiSetupPlan(args, manifest, files);
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
export const aiTools: AnyMapoToolSpec[] = [aiSetupTool];
|
|
@@ -82,6 +82,12 @@ interface ComponentApiArgs {
|
|
|
82
82
|
include?: Array<"props" | "events" | "slots" | "exposed">;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/** What an app needs before it has the components of an opt-in package. */
|
|
86
|
+
const OPT_IN_PACKAGES: Record<string, string> = {
|
|
87
|
+
"@mapomodule/ai":
|
|
88
|
+
"Experimental and opt-in: it exists only in an app that sets `mapo.ai` (the meta-module then installs `@mapomodule/ai`).",
|
|
89
|
+
};
|
|
90
|
+
|
|
85
91
|
function renderComponent(
|
|
86
92
|
component: ComponentApi,
|
|
87
93
|
include?: ComponentApiArgs["include"],
|
|
@@ -99,9 +105,11 @@ function renderComponent(
|
|
|
99
105
|
sections.push(renderMembers("Exposed", component.exposed));
|
|
100
106
|
}
|
|
101
107
|
|
|
108
|
+
const optIn = OPT_IN_PACKAGES[component.pkg];
|
|
102
109
|
return (
|
|
103
110
|
`# ${component.name}\n` +
|
|
104
111
|
`package: \`${component.pkg}\` · source: \`${component.file}\`\n` +
|
|
112
|
+
(optIn ? `\n${optIn}\n` : "") +
|
|
105
113
|
(component.description ? `\n${component.description}\n` : "") +
|
|
106
114
|
`\n${sections.join("\n")}` +
|
|
107
115
|
renderDocs(component.docs)
|