@mapomodule/mcp 0.0.0-stage → 1.0.0-beta.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 +509 -2
- package/bin/mapo-mcp.mjs +3 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.d.ts +18 -0
- package/dist/build.mjs +351 -0
- package/dist/cli.d.mts +7 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.mjs +274 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.mjs +1 -0
- package/dist/knowledge/api.json +1 -0
- package/dist/knowledge/docs.json +1 -0
- package/dist/module.d.mts +16 -0
- package/dist/module.d.ts +16 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +124 -0
- package/dist/runtime/core/doctor.d.ts +0 -0
- package/dist/runtime/core/doctor.js +128 -0
- package/dist/runtime/core/endpoint.d.ts +0 -0
- package/dist/runtime/core/endpoint.js +8 -0
- package/dist/runtime/core/indexer.d.ts +0 -0
- package/dist/runtime/core/indexer.js +194 -0
- package/dist/runtime/core/instructions.d.ts +0 -0
- package/dist/runtime/core/instructions.js +10 -0
- package/dist/runtime/core/knowledge.d.ts +0 -0
- package/dist/runtime/core/knowledge.js +76 -0
- package/dist/runtime/core/openapi.d.ts +0 -0
- package/dist/runtime/core/openapi.js +229 -0
- package/dist/runtime/core/overview.d.ts +0 -0
- package/dist/runtime/core/overview.js +15 -0
- package/dist/runtime/core/prompts.d.ts +0 -0
- package/dist/runtime/core/prompts.js +108 -0
- package/dist/runtime/core/recipes.d.ts +0 -0
- package/dist/runtime/core/recipes.js +496 -0
- package/dist/runtime/core/resources.d.ts +0 -0
- package/dist/runtime/core/resources.js +92 -0
- package/dist/runtime/core/scaffold/fields.d.ts +0 -0
- package/dist/runtime/core/scaffold/fields.js +64 -0
- package/dist/runtime/core/scaffold/index.d.ts +0 -0
- package/dist/runtime/core/scaffold/index.js +74 -0
- package/dist/runtime/core/scaffold/templates.d.ts +0 -0
- package/dist/runtime/core/scaffold/templates.js +349 -0
- package/dist/runtime/core/schema-source.d.ts +0 -0
- package/dist/runtime/core/schema-source.js +70 -0
- package/dist/runtime/core/search.d.ts +0 -0
- package/dist/runtime/core/search.js +173 -0
- package/dist/runtime/core/tokenize.d.ts +0 -0
- package/dist/runtime/core/tokenize.js +96 -0
- package/dist/runtime/core/tool-spec.d.ts +0 -0
- package/dist/runtime/core/tool-spec.js +3 -0
- package/dist/runtime/core/tools/api.d.ts +0 -0
- package/dist/runtime/core/tools/api.js +192 -0
- package/dist/runtime/core/tools/app.d.ts +0 -0
- package/dist/runtime/core/tools/app.js +141 -0
- package/dist/runtime/core/tools/backend.d.ts +0 -0
- package/dist/runtime/core/tools/backend.js +143 -0
- package/dist/runtime/core/tools/docs.d.ts +0 -0
- package/dist/runtime/core/tools/docs.js +154 -0
- package/dist/runtime/core/tools/scaffold.d.ts +0 -0
- package/dist/runtime/core/tools/scaffold.js +127 -0
- package/dist/runtime/core/types.d.ts +0 -0
- package/dist/runtime/core/types.js +0 -0
- package/dist/runtime/core/write.d.ts +0 -0
- package/dist/runtime/core/write.js +53 -0
- package/dist/runtime/mcp/handlers/mapo/index.js +7 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/crud-page.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/custom-field.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/debug-form.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/migrate-v1.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/theme.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/resources/app-manifest.js +19 -0
- package/dist/runtime/mcp/handlers/mapo/resources/docs.js +34 -0
- package/dist/runtime/mcp/handlers/mapo/resources/fields.js +34 -0
- package/dist/runtime/mcp/handlers/mapo/resources/knowledge-index.js +22 -0
- package/dist/runtime/mcp/handlers/mapo/resources/overview.js +20 -0
- package/dist/runtime/mcp/handlers/mapo/tools/backend-schema.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/component-api.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/composable-api.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/doctor.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/get-doc.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/inspect-app.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/list-field-types.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/list-recipes.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/scaffold.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/search-docs.js +3 -0
- package/dist/runtime/nitro/context.d.ts +0 -0
- package/dist/runtime/nitro/context.js +21 -0
- package/dist/runtime/nitro/to-mcp-prompt.d.ts +0 -0
- package/dist/runtime/nitro/to-mcp-prompt.js +23 -0
- package/dist/runtime/nitro/to-mcp-tool.d.ts +0 -0
- package/dist/runtime/nitro/to-mcp-tool.js +28 -0
- package/dist/runtime/types/virtual-modules.d.ts +6 -0
- package/dist/types.d.mts +9 -0
- package/package.json +73 -4
- package/src/__tests__/cli.test.ts +137 -0
- package/src/__tests__/doctor.test.ts +145 -0
- package/src/__tests__/endpoint.test.ts +31 -0
- package/src/__tests__/fixtures/drf-schema.json +142 -0
- package/src/__tests__/fixtures.ts +198 -0
- package/src/__tests__/indexer.test.ts +114 -0
- package/src/__tests__/knowledge.test.ts +225 -0
- package/src/__tests__/openapi.test.ts +256 -0
- package/src/__tests__/overview.test.ts +105 -0
- package/src/__tests__/recipes.test.ts +55 -0
- package/src/__tests__/resources.test.ts +69 -0
- package/src/__tests__/scaffold.test.ts +153 -0
- package/src/__tests__/search.test.ts +105 -0
- package/src/__tests__/tools.test.ts +226 -0
- package/src/__tests__/write.test.ts +134 -0
- package/src/build/extract-components.ts +135 -0
- package/src/build/extract-composables.ts +125 -0
- package/src/build/extract-fields.ts +250 -0
- package/src/build/index.ts +103 -0
- package/src/cli.ts +375 -0
- package/src/index.ts +65 -0
- package/src/module.ts +213 -0
- package/src/runtime/core/doctor.ts +204 -0
- package/src/runtime/core/endpoint.ts +27 -0
- package/src/runtime/core/indexer.ts +285 -0
- package/src/runtime/core/instructions.ts +18 -0
- package/src/runtime/core/knowledge.ts +121 -0
- package/src/runtime/core/openapi.ts +417 -0
- package/src/runtime/core/overview.ts +44 -0
- package/src/runtime/core/prompts.ts +150 -0
- package/src/runtime/core/recipes.ts +574 -0
- package/src/runtime/core/resources.ts +150 -0
- package/src/runtime/core/scaffold/fields.ts +127 -0
- package/src/runtime/core/scaffold/index.ts +120 -0
- package/src/runtime/core/scaffold/templates.ts +394 -0
- package/src/runtime/core/schema-source.ts +123 -0
- package/src/runtime/core/search.ts +282 -0
- package/src/runtime/core/tokenize.ts +146 -0
- package/src/runtime/core/tool-spec.ts +74 -0
- package/src/runtime/core/tools/api.ts +300 -0
- package/src/runtime/core/tools/app.ts +201 -0
- package/src/runtime/core/tools/backend.ts +197 -0
- package/src/runtime/core/tools/docs.ts +218 -0
- package/src/runtime/core/tools/scaffold.ts +176 -0
- package/src/runtime/core/types.ts +133 -0
- package/src/runtime/core/write.ts +119 -0
- package/src/runtime/mcp/handlers/mapo/index.ts +16 -0
- package/src/runtime/mcp/handlers/mapo/prompts/crud-page.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/custom-field.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/debug-form.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/migrate-v1.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/theme.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/resources/app-manifest.ts +28 -0
- package/src/runtime/mcp/handlers/mapo/resources/docs.ts +41 -0
- package/src/runtime/mcp/handlers/mapo/resources/fields.ts +37 -0
- package/src/runtime/mcp/handlers/mapo/resources/knowledge-index.ts +24 -0
- package/src/runtime/mcp/handlers/mapo/resources/overview.ts +22 -0
- package/src/runtime/mcp/handlers/mapo/tools/backend-schema.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/component-api.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/composable-api.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/doctor.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/get-doc.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/inspect-app.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/list-field-types.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/list-recipes.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/scaffold.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/search-docs.ts +4 -0
- package/src/runtime/nitro/context.ts +41 -0
- package/src/runtime/nitro/to-mcp-prompt.ts +30 -0
- package/src/runtime/nitro/to-mcp-tool.ts +35 -0
- package/src/runtime/types/virtual-modules.d.ts +6 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { parseFields } from "./fields.js";
|
|
2
|
+
import * as templates from "./templates.js";
|
|
3
|
+
export const SCAFFOLD_KINDS = [
|
|
4
|
+
"list-page",
|
|
5
|
+
"detail-page",
|
|
6
|
+
"standalone-form",
|
|
7
|
+
"custom-field",
|
|
8
|
+
"login-page",
|
|
9
|
+
"theme-override",
|
|
10
|
+
"backend-proxy",
|
|
11
|
+
"menu-page",
|
|
12
|
+
"media-page"
|
|
13
|
+
];
|
|
14
|
+
export const SCAFFOLD_DESCRIPTIONS = {
|
|
15
|
+
"list-page": "MapoList page with typed columns",
|
|
16
|
+
"detail-page": "MapoDetail page with field descriptors",
|
|
17
|
+
"standalone-form": "MapoForm driven by useCrud and your own submit handler",
|
|
18
|
+
"custom-field": "field component plus the plugin registering its type",
|
|
19
|
+
"login-page": "MapoLogin page wired to the configured auth endpoints",
|
|
20
|
+
"theme-override": "CSS token file plus the nuxt.config fragment that loads it",
|
|
21
|
+
"backend-proxy": "Nitro middleware proxying /api/* to a backend",
|
|
22
|
+
"menu-page": "MapoMenuManager page for a navigation tree",
|
|
23
|
+
"media-page": "MapoMediaManager page"
|
|
24
|
+
};
|
|
25
|
+
const RESOURCE_KINDS = /* @__PURE__ */ new Set([
|
|
26
|
+
"list-page",
|
|
27
|
+
"detail-page",
|
|
28
|
+
"standalone-form"
|
|
29
|
+
]);
|
|
30
|
+
function toModelName(name) {
|
|
31
|
+
const pascal = name.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
32
|
+
if (/ies$/.test(pascal)) return `${pascal.slice(0, -3)}y`;
|
|
33
|
+
if (/(ses|xes|zes|ches|shes)$/.test(pascal)) return pascal.slice(0, -2);
|
|
34
|
+
if (/[^s]s$/.test(pascal)) return pascal.slice(0, -1);
|
|
35
|
+
return pascal;
|
|
36
|
+
}
|
|
37
|
+
export function scaffold(options) {
|
|
38
|
+
const name = (options.name ?? "items").trim().replace(/^\/+|\/+$/g, "");
|
|
39
|
+
const route = (options.route ?? `/${name}`).replace(/\/+$/, "");
|
|
40
|
+
const endpoint = (options.endpoint ?? `/api/${name}`).replace(/\/+$/, "");
|
|
41
|
+
const fields = RESOURCE_KINDS.has(options.kind) ? parseFields(
|
|
42
|
+
options.fields?.length ? options.fields : ["title:text"],
|
|
43
|
+
options.api
|
|
44
|
+
) : [];
|
|
45
|
+
const input = {
|
|
46
|
+
name,
|
|
47
|
+
model: toModelName(name),
|
|
48
|
+
endpoint,
|
|
49
|
+
route,
|
|
50
|
+
fields,
|
|
51
|
+
...options.api ? { api: options.api } : {}
|
|
52
|
+
};
|
|
53
|
+
switch (options.kind) {
|
|
54
|
+
case "list-page":
|
|
55
|
+
return templates.listPage(input);
|
|
56
|
+
case "detail-page":
|
|
57
|
+
return templates.detailPage(input);
|
|
58
|
+
case "standalone-form":
|
|
59
|
+
return templates.standaloneForm(input);
|
|
60
|
+
case "custom-field":
|
|
61
|
+
return templates.customField(input);
|
|
62
|
+
case "login-page":
|
|
63
|
+
return templates.loginPage();
|
|
64
|
+
case "theme-override":
|
|
65
|
+
return templates.themeOverride();
|
|
66
|
+
case "backend-proxy":
|
|
67
|
+
return templates.backendProxy(input);
|
|
68
|
+
case "menu-page":
|
|
69
|
+
return templates.menuPage(input);
|
|
70
|
+
case "media-page":
|
|
71
|
+
return templates.mediaPage(input);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export { UnknownFieldTypeError } from "./fields.js";
|
|
File without changes
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { renderDescriptors, renderModel } from "./fields.js";
|
|
2
|
+
const PAGE_META = (label, icon) => `definePageMeta({
|
|
3
|
+
layout: "mapo-default",
|
|
4
|
+
middleware: ["auth"],
|
|
5
|
+
label: "${label}",
|
|
6
|
+
icon: "${icon}",
|
|
7
|
+
});`;
|
|
8
|
+
export function listPage(input) {
|
|
9
|
+
const columns = input.fields.map(
|
|
10
|
+
(field) => ` { key: "${field.key}", label: "${field.label}", sortable: true },`
|
|
11
|
+
).join("\n");
|
|
12
|
+
return {
|
|
13
|
+
files: [
|
|
14
|
+
{
|
|
15
|
+
path: `app/pages${input.route}/index.vue`,
|
|
16
|
+
content: `<script setup lang="ts">
|
|
17
|
+
import type { ListColumn } from "mapomodule/types";
|
|
18
|
+
|
|
19
|
+
${PAGE_META(input.model + "s", "i-lucide-list")}
|
|
20
|
+
|
|
21
|
+
interface ${input.model} {
|
|
22
|
+
id: number;
|
|
23
|
+
${renderModel(input.fields)}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const columns: ListColumn<${input.model}>[] = [
|
|
27
|
+
{ key: "id", label: "ID", class: "w-14 font-mono text-xs text-muted" },
|
|
28
|
+
${columns}
|
|
29
|
+
];
|
|
30
|
+
<\/script>
|
|
31
|
+
|
|
32
|
+
<template>
|
|
33
|
+
<MapoList
|
|
34
|
+
endpoint="${input.endpoint}"
|
|
35
|
+
detail-base="${input.route}"
|
|
36
|
+
:columns="columns"
|
|
37
|
+
model-name="${input.model}"
|
|
38
|
+
/>
|
|
39
|
+
</template>
|
|
40
|
+
`
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
notes: [
|
|
44
|
+
`The list calls \`${input.endpoint}\` with \`?page=&search=&ordering=\` \u2014 the backend must paginate.`,
|
|
45
|
+
`Row clicks navigate to \`${input.route}/<id>\`; scaffold the detail page next.`
|
|
46
|
+
],
|
|
47
|
+
docs: ["howto/crud-list.md", "uikit/list.md"]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function detailPage(input) {
|
|
51
|
+
return {
|
|
52
|
+
files: [
|
|
53
|
+
{
|
|
54
|
+
path: `app/pages${input.route}/[id].vue`,
|
|
55
|
+
content: `<script setup lang="ts">
|
|
56
|
+
import type { FieldDescriptor } from "mapomodule/types";
|
|
57
|
+
|
|
58
|
+
${PAGE_META(input.model, "i-lucide-file-pen")}
|
|
59
|
+
|
|
60
|
+
interface ${input.model} {
|
|
61
|
+
id: number;
|
|
62
|
+
${renderModel(input.fields)}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const route = useRoute();
|
|
66
|
+
const id = computed(() => route.params.id as string);
|
|
67
|
+
|
|
68
|
+
const fields: FieldDescriptor<${input.model}>[] = [
|
|
69
|
+
${renderDescriptors(input.fields, input.api)}
|
|
70
|
+
];
|
|
71
|
+
<\/script>
|
|
72
|
+
|
|
73
|
+
<template>
|
|
74
|
+
<MapoDetail
|
|
75
|
+
:endpoint="'${input.endpoint}'"
|
|
76
|
+
:id="id"
|
|
77
|
+
:fields="fields"
|
|
78
|
+
model-name="${input.model}"
|
|
79
|
+
@saved="navigateTo('${input.route}')"
|
|
80
|
+
/>
|
|
81
|
+
</template>
|
|
82
|
+
`
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
notes: [
|
|
86
|
+
"`id` is `'new'` when creating: MapoDetail switches to POST on its own.",
|
|
87
|
+
"Move fields to `sidebarFields` to render them in the sticky right column."
|
|
88
|
+
],
|
|
89
|
+
docs: ["howto/crud-detail.md", "uikit/detail.md"]
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export function standaloneForm(input) {
|
|
93
|
+
return {
|
|
94
|
+
files: [
|
|
95
|
+
{
|
|
96
|
+
path: `app/pages${input.route}/form.vue`,
|
|
97
|
+
content: `<script setup lang="ts">
|
|
98
|
+
import type { FieldDescriptor } from "mapomodule/types";
|
|
99
|
+
|
|
100
|
+
${PAGE_META(input.model + " form", "i-lucide-square-pen")}
|
|
101
|
+
|
|
102
|
+
interface ${input.model} {
|
|
103
|
+
${renderModel(input.fields)}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const repository = useCrud<${input.model}>("${input.endpoint}");
|
|
107
|
+
const model = ref<${input.model}>({} as ${input.model});
|
|
108
|
+
const errors = ref<Record<string, string[]>>({});
|
|
109
|
+
const saving = ref(false);
|
|
110
|
+
const snack = useSnackStore();
|
|
111
|
+
|
|
112
|
+
const fields: FieldDescriptor<${input.model}>[] = [
|
|
113
|
+
${renderDescriptors(input.fields, input.api)}
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
async function save() {
|
|
117
|
+
saving.value = true;
|
|
118
|
+
errors.value = {};
|
|
119
|
+
try {
|
|
120
|
+
await repository.create(model.value);
|
|
121
|
+
snack.success("Saved");
|
|
122
|
+
} catch (error) {
|
|
123
|
+
// Mapo puts the backend payload on the error, so field errors map 1:1.
|
|
124
|
+
errors.value = (error as { data?: Record<string, string[]> }).data ?? {};
|
|
125
|
+
snack.error("Could not save");
|
|
126
|
+
} finally {
|
|
127
|
+
saving.value = false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
<\/script>
|
|
131
|
+
|
|
132
|
+
<template>
|
|
133
|
+
<div class="p-6 space-y-4">
|
|
134
|
+
<MapoForm v-model="model" :fields="fields" :errors="errors" />
|
|
135
|
+
<UButton :loading="saving" @click="save">Save</UButton>
|
|
136
|
+
</div>
|
|
137
|
+
</template>
|
|
138
|
+
`
|
|
139
|
+
}
|
|
140
|
+
],
|
|
141
|
+
notes: [
|
|
142
|
+
"`useCrud` is auto-imported; no import statement is needed.",
|
|
143
|
+
"Use this when you own the submit flow \u2014 otherwise MapoDetail does it for you."
|
|
144
|
+
],
|
|
145
|
+
docs: ["howto/form-standalone.md", "uikit/form/index.md"]
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
export function customField(input) {
|
|
149
|
+
const component = `${input.model}Field`;
|
|
150
|
+
const type = input.name;
|
|
151
|
+
return {
|
|
152
|
+
files: [
|
|
153
|
+
{
|
|
154
|
+
path: `app/components/${component}.vue`,
|
|
155
|
+
content: `<script setup lang="ts">
|
|
156
|
+
import type { CustomDescriptor } from "mapomodule/types";
|
|
157
|
+
|
|
158
|
+
// The contract a field component must implement \u2014 nothing more.
|
|
159
|
+
defineProps<{
|
|
160
|
+
modelValue: string | null;
|
|
161
|
+
descriptor: CustomDescriptor;
|
|
162
|
+
errors?: string[];
|
|
163
|
+
readonly?: boolean;
|
|
164
|
+
disabled?: boolean;
|
|
165
|
+
}>();
|
|
166
|
+
|
|
167
|
+
const emit = defineEmits<{ "update:modelValue": [value: string | null] }>();
|
|
168
|
+
<\/script>
|
|
169
|
+
|
|
170
|
+
<template>
|
|
171
|
+
<UFormField :label="descriptor.label" :error="errors?.[0]">
|
|
172
|
+
<UInput
|
|
173
|
+
:model-value="modelValue ?? ''"
|
|
174
|
+
:disabled="disabled || readonly"
|
|
175
|
+
@update:model-value="emit('update:modelValue', String($event) || null)"
|
|
176
|
+
/>
|
|
177
|
+
</UFormField>
|
|
178
|
+
</template>
|
|
179
|
+
`
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
path: `app/plugins/${type}-field.ts`,
|
|
183
|
+
content: `export default defineNuxtPlugin(() => {
|
|
184
|
+
// Registers the type globally: any descriptor can now use \`type: "${type}"\`.
|
|
185
|
+
defineFormField("${type}", () => import("~/components/${component}.vue"), {
|
|
186
|
+
attrs: {},
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
`
|
|
190
|
+
}
|
|
191
|
+
],
|
|
192
|
+
notes: [
|
|
193
|
+
`Use it with \`{ key: "\u2026", type: "${type}", label: "\u2026" }\`.`,
|
|
194
|
+
"Registering an existing type warns unless you pass `{ override: true }`.",
|
|
195
|
+
"Types added this way are invisible to `mapo_inspect_app`, which reads a build-time snapshot."
|
|
196
|
+
],
|
|
197
|
+
docs: ["uikit/form/registry.md", "uikit/form/custom-fields.md"]
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
export function loginPage() {
|
|
201
|
+
return {
|
|
202
|
+
files: [
|
|
203
|
+
{
|
|
204
|
+
path: "app/pages/login.vue",
|
|
205
|
+
content: `<script setup lang="ts">
|
|
206
|
+
// The login page must opt out of the admin layout.
|
|
207
|
+
definePageMeta({ layout: false });
|
|
208
|
+
<\/script>
|
|
209
|
+
|
|
210
|
+
<template>
|
|
211
|
+
<MapoLogin />
|
|
212
|
+
</template>
|
|
213
|
+
`
|
|
214
|
+
}
|
|
215
|
+
],
|
|
216
|
+
notes: [
|
|
217
|
+
"The path must match `mapo.loginUrl` (default `/login`), or the auth middleware redirects to a 404.",
|
|
218
|
+
"MapoLogin posts to `mapo.authLoginUrl` and then reads `mapo.userInfoApi`."
|
|
219
|
+
],
|
|
220
|
+
docs: ["howto/auth-permissions.md", "uikit/login.md"]
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
export function themeOverride() {
|
|
224
|
+
return {
|
|
225
|
+
files: [
|
|
226
|
+
{
|
|
227
|
+
path: "app/assets/css/mapo.css",
|
|
228
|
+
content: `@import "tailwindcss";
|
|
229
|
+
@import "@nuxt/ui";
|
|
230
|
+
|
|
231
|
+
/* Mapo design tokens. Override only what you need: everything else keeps
|
|
232
|
+
the defaults shipped by @mapomodule/uikit. */
|
|
233
|
+
:root {
|
|
234
|
+
--ui-primary: var(--color-green-600);
|
|
235
|
+
--mapo-sidebar-width: 16rem;
|
|
236
|
+
--mapo-topbar-height: 64px;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.dark {
|
|
240
|
+
--ui-primary: var(--color-green-400);
|
|
241
|
+
}
|
|
242
|
+
`
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
path: "nuxt.config.snippet.ts",
|
|
246
|
+
content: `// Add to nuxt.config.ts \u2014 the CSS must go through the uikit module so
|
|
247
|
+
// Tailwind v4 and @nuxt/ui process it in the same pipeline.
|
|
248
|
+
export default defineNuxtConfig({
|
|
249
|
+
mapo: {
|
|
250
|
+
uikit: {
|
|
251
|
+
css: "~/assets/css/mapo.css",
|
|
252
|
+
ui: {
|
|
253
|
+
button: { defaultVariants: { size: "md" } },
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
`
|
|
259
|
+
}
|
|
260
|
+
],
|
|
261
|
+
notes: [
|
|
262
|
+
"`nuxt.config.snippet.ts` is a fragment to merge by hand, not a file Nuxt loads.",
|
|
263
|
+
"To replace a whole component instead of restyling it, use the MapoOverride system."
|
|
264
|
+
],
|
|
265
|
+
docs: ["howto/theming.md", "uikit/theming.md", "uikit/mapoverride.md"]
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
export function backendProxy(input) {
|
|
269
|
+
return {
|
|
270
|
+
files: [
|
|
271
|
+
{
|
|
272
|
+
path: "server/middleware/backend-proxy.ts",
|
|
273
|
+
content: `/**
|
|
274
|
+
* Proxies /api/* to the backend, keeping cookies on the app's own origin so
|
|
275
|
+
* the session survives without CORS.
|
|
276
|
+
*/
|
|
277
|
+
export default defineEventHandler(async (event) => {
|
|
278
|
+
const url = getRequestURL(event);
|
|
279
|
+
if (!url.pathname.startsWith("/api/")) return;
|
|
280
|
+
|
|
281
|
+
const target = process.env.BACKEND_URL ?? "http://localhost:8000";
|
|
282
|
+
|
|
283
|
+
return proxyRequest(event, \`\${target}\${url.pathname}\${url.search}\`, {
|
|
284
|
+
// Forward the session cookie both ways.
|
|
285
|
+
cookieDomainRewrite: url.host,
|
|
286
|
+
headers: { host: new URL(target).host },
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
`
|
|
290
|
+
}
|
|
291
|
+
],
|
|
292
|
+
notes: [
|
|
293
|
+
`Point \`mapo.authLoginUrl\`, \`mapo.userInfoApi\` and endpoints such as \`${input.endpoint}\` at the proxied paths.`,
|
|
294
|
+
"For a Django/DRF backend, CSRF needs the token header \u2014 see the recipe."
|
|
295
|
+
],
|
|
296
|
+
docs: ["howto/backend-integration.md", "modules/camomilla.md"]
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
export function menuPage(input) {
|
|
300
|
+
return {
|
|
301
|
+
files: [
|
|
302
|
+
{
|
|
303
|
+
path: `app/pages${input.route}/[id].vue`,
|
|
304
|
+
content: `<script setup lang="ts">
|
|
305
|
+
${PAGE_META("Menu", "i-lucide-menu")}
|
|
306
|
+
|
|
307
|
+
const route = useRoute();
|
|
308
|
+
const id = computed(() => route.params.id as string);
|
|
309
|
+
<\/script>
|
|
310
|
+
|
|
311
|
+
<template>
|
|
312
|
+
<MapoMenuManager
|
|
313
|
+
:endpoint="'${input.endpoint}'"
|
|
314
|
+
:id="id"
|
|
315
|
+
page-types-endpoint="${input.endpoint}/page_types"
|
|
316
|
+
/>
|
|
317
|
+
</template>
|
|
318
|
+
`
|
|
319
|
+
}
|
|
320
|
+
],
|
|
321
|
+
notes: [
|
|
322
|
+
"The backend must expose the menu tree and the available page types.",
|
|
323
|
+
"Nodes are reordered by drag & drop; the tree is saved as a whole."
|
|
324
|
+
],
|
|
325
|
+
docs: ["howto/menu-manager.md", "uikit/menu-manager.md"]
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
export function mediaPage(input) {
|
|
329
|
+
return {
|
|
330
|
+
files: [
|
|
331
|
+
{
|
|
332
|
+
path: `app/pages${input.route}/index.vue`,
|
|
333
|
+
content: `<script setup lang="ts">
|
|
334
|
+
${PAGE_META("Media", "i-lucide-image")}
|
|
335
|
+
<\/script>
|
|
336
|
+
|
|
337
|
+
<template>
|
|
338
|
+
<MapoMediaManager />
|
|
339
|
+
</template>
|
|
340
|
+
`
|
|
341
|
+
}
|
|
342
|
+
],
|
|
343
|
+
notes: [
|
|
344
|
+
"Configure `mapo.uikit.media.endpoints` unless an integration already serves them.",
|
|
345
|
+
"In a form, pick media with the `media`, `media-m2m` or `enhanced-media` field types."
|
|
346
|
+
],
|
|
347
|
+
docs: ["howto/media-manager.md", "uikit/media.md"]
|
|
348
|
+
};
|
|
349
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, resolve } from "node:path";
|
|
3
|
+
const CACHE_TTL_MS = 6e4;
|
|
4
|
+
const cache = /* @__PURE__ */ new Map();
|
|
5
|
+
export class SchemaUnavailableError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "SchemaUnavailableError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function parseDocument(raw, origin) {
|
|
12
|
+
const trimmed = raw.trimStart();
|
|
13
|
+
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) {
|
|
14
|
+
const looksLikeYaml = /^openapi:\s/m.test(trimmed);
|
|
15
|
+
throw new SchemaUnavailableError(
|
|
16
|
+
looksLikeYaml ? `${origin} returned YAML. Request JSON instead \u2014 drf-spectacular accepts \`?format=json\`.` : `${origin} did not return JSON (the response starts with "${trimmed.slice(0, 30)}\u2026"). An HTML page usually means the schema endpoint requires authentication.`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(raw);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new SchemaUnavailableError(
|
|
23
|
+
`${origin} returned malformed JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function fetchDocument(url, token) {
|
|
28
|
+
let response;
|
|
29
|
+
try {
|
|
30
|
+
response = await fetch(url, {
|
|
31
|
+
headers: {
|
|
32
|
+
accept: "application/json",
|
|
33
|
+
...token ? { authorization: `Bearer ${token}` } : {}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw new SchemaUnavailableError(
|
|
38
|
+
`Could not reach ${url} (${error instanceof Error ? error.message : String(error)}). Is the backend running?`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
const hint = response.status === 401 || response.status === 403 ? " The schema endpoint needs credentials: set the token env var declared in `mapo.mcp.backend.tokenEnv`." : "";
|
|
43
|
+
throw new SchemaUnavailableError(
|
|
44
|
+
`${url} answered ${response.status}.${hint}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return parseDocument(await response.text(), url);
|
|
48
|
+
}
|
|
49
|
+
function readDocument(path, rootDir) {
|
|
50
|
+
const absolute = isAbsolute(path) ? path : resolve(rootDir ?? process.cwd(), path);
|
|
51
|
+
if (!existsSync(absolute)) {
|
|
52
|
+
throw new SchemaUnavailableError(`No schema file at ${absolute}.`);
|
|
53
|
+
}
|
|
54
|
+
return parseDocument(readFileSync(absolute, "utf-8"), absolute);
|
|
55
|
+
}
|
|
56
|
+
export async function loadOpenApiDocument(source, { token = null, rootDir = null } = {}) {
|
|
57
|
+
const cached = cache.get(source);
|
|
58
|
+
if (cached && Date.now() - cached.at < CACHE_TTL_MS) return cached.document;
|
|
59
|
+
const document = /^https?:\/\//i.test(source) ? await fetchDocument(source, token) : readDocument(source, rootDir);
|
|
60
|
+
if (!document.components?.schemas) {
|
|
61
|
+
throw new SchemaUnavailableError(
|
|
62
|
+
`${source} is not an OpenAPI document with \`components.schemas\` (found keys: ${Object.keys(document).join(", ") || "none"}).`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
cache.set(source, { at: Date.now(), document });
|
|
66
|
+
return document;
|
|
67
|
+
}
|
|
68
|
+
export function clearSchemaCache() {
|
|
69
|
+
cache.clear();
|
|
70
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { tokenize } from "./tokenize.js";
|
|
2
|
+
const K1 = 1.5;
|
|
3
|
+
const B = 0.75;
|
|
4
|
+
const SNIPPET_CHARS = 360;
|
|
5
|
+
const PAGE_CONTEXT_WEIGHT = 0.2;
|
|
6
|
+
const TITLE_BOOST_STEP = 0.15;
|
|
7
|
+
const TITLE_BOOST_CAP = 1.5;
|
|
8
|
+
const MAX_HITS_PER_PAGE = 2;
|
|
9
|
+
const REFERENCE_PENALTY = 0.75;
|
|
10
|
+
const REFERENCE_INTENT = /\b(prop|props|slot|slots|emit|emits|event|events|api|signature|reference|type)\b/i;
|
|
11
|
+
const GENERIC_IDENTITY_TOKENS = /* @__PURE__ */ new Set([
|
|
12
|
+
"api",
|
|
13
|
+
"index",
|
|
14
|
+
"doc",
|
|
15
|
+
"overview",
|
|
16
|
+
"reference",
|
|
17
|
+
"uikit",
|
|
18
|
+
"modul",
|
|
19
|
+
"module",
|
|
20
|
+
"howto",
|
|
21
|
+
"guid",
|
|
22
|
+
"guide",
|
|
23
|
+
"migrat",
|
|
24
|
+
"migration",
|
|
25
|
+
"md"
|
|
26
|
+
]);
|
|
27
|
+
function matchesFilters(chunk, options) {
|
|
28
|
+
if (options.section) {
|
|
29
|
+
const wanted = options.section.toLowerCase();
|
|
30
|
+
if (chunk.section !== wanted && chunk.group !== wanted) return false;
|
|
31
|
+
}
|
|
32
|
+
if (options.pkg) {
|
|
33
|
+
const wanted = options.pkg.toLowerCase().replace(/^@mapomodule\//, "");
|
|
34
|
+
if (!chunk.pkg || !chunk.pkg.toLowerCase().includes(wanted)) return false;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
export function extractSnippet(text, query) {
|
|
39
|
+
const words = query.toLowerCase().split(/[^a-z0-9]+/).filter((word) => word.length > 2);
|
|
40
|
+
const haystack = text.toLowerCase();
|
|
41
|
+
let best = -1;
|
|
42
|
+
for (const word of words) {
|
|
43
|
+
const at = haystack.indexOf(word);
|
|
44
|
+
if (at !== -1 && (best === -1 || at < best)) best = at;
|
|
45
|
+
}
|
|
46
|
+
if (best === -1) {
|
|
47
|
+
return text.length > SNIPPET_CHARS ? `${text.slice(0, SNIPPET_CHARS).trimEnd()}\u2026` : text;
|
|
48
|
+
}
|
|
49
|
+
const start = Math.max(0, best - Math.floor(SNIPPET_CHARS / 3));
|
|
50
|
+
const end = Math.min(text.length, start + SNIPPET_CHARS);
|
|
51
|
+
const slice = text.slice(start, end).trim();
|
|
52
|
+
return `${start > 0 ? "\u2026" : ""}${slice}${end < text.length ? "\u2026" : ""}`;
|
|
53
|
+
}
|
|
54
|
+
export function searchDocs(knowledge, query, options = {}) {
|
|
55
|
+
const { chunks, index } = knowledge;
|
|
56
|
+
const limit = options.limit ?? 5;
|
|
57
|
+
const queryTerms = tokenize(query);
|
|
58
|
+
if (!queryTerms.length || !index.total) return [];
|
|
59
|
+
const scores = /* @__PURE__ */ new Map();
|
|
60
|
+
const seenTerms = /* @__PURE__ */ new Set();
|
|
61
|
+
for (const term of queryTerms) {
|
|
62
|
+
if (seenTerms.has(term)) continue;
|
|
63
|
+
seenTerms.add(term);
|
|
64
|
+
const postings = index.terms[term];
|
|
65
|
+
if (!postings) continue;
|
|
66
|
+
const idf = Math.max(
|
|
67
|
+
0.05,
|
|
68
|
+
Math.log(
|
|
69
|
+
1 + (index.total - postings.length + 0.5) / (postings.length + 0.5)
|
|
70
|
+
)
|
|
71
|
+
);
|
|
72
|
+
for (const [chunkIndex, frequency] of postings) {
|
|
73
|
+
const chunk = chunks[chunkIndex];
|
|
74
|
+
if (!chunk || !matchesFilters(chunk, options)) continue;
|
|
75
|
+
const length = index.lengths[chunkIndex] ?? 0;
|
|
76
|
+
const norm = 1 - B + B * length / (index.avgLength || 1);
|
|
77
|
+
const score = idf * (frequency * (K1 + 1) / (frequency + K1 * norm));
|
|
78
|
+
scores.set(chunkIndex, (scores.get(chunkIndex) ?? 0) + score);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const pageTotals = /* @__PURE__ */ new Map();
|
|
82
|
+
for (const [chunkIndex, score] of scores) {
|
|
83
|
+
const { path } = chunks[chunkIndex];
|
|
84
|
+
pageTotals.set(path, (pageTotals.get(path) ?? 0) + score);
|
|
85
|
+
}
|
|
86
|
+
const phrase = query.trim().toLowerCase();
|
|
87
|
+
const distinctTerms = [...seenTerms];
|
|
88
|
+
const wantsReference = REFERENCE_INTENT.test(query);
|
|
89
|
+
const ranked = [...scores.entries()].map(([chunkIndex, score]) => {
|
|
90
|
+
const chunk = chunks[chunkIndex];
|
|
91
|
+
let final = score + PAGE_CONTEXT_WEIGHT * ((pageTotals.get(chunk.path) ?? score) - score);
|
|
92
|
+
const identity = new Set(
|
|
93
|
+
tokenize(`${chunk.title} ${chunk.path.replace(/[/.\-_]/g, " ")}`)
|
|
94
|
+
);
|
|
95
|
+
const matched = distinctTerms.filter(
|
|
96
|
+
(term) => identity.has(term) && !GENERIC_IDENTITY_TOKENS.has(term)
|
|
97
|
+
).length;
|
|
98
|
+
if (matched)
|
|
99
|
+
final *= Math.min(TITLE_BOOST_CAP, 1 + TITLE_BOOST_STEP * matched);
|
|
100
|
+
if (chunk.kind === "reference" && !wantsReference)
|
|
101
|
+
final *= REFERENCE_PENALTY;
|
|
102
|
+
if (phrase.length > 6 && chunk.text.toLowerCase().includes(phrase))
|
|
103
|
+
final *= 1.5;
|
|
104
|
+
return [chunkIndex, final];
|
|
105
|
+
});
|
|
106
|
+
const perPage = /* @__PURE__ */ new Map();
|
|
107
|
+
return ranked.sort((a, b) => b[1] - a[1]).filter(([chunkIndex]) => {
|
|
108
|
+
const { path } = chunks[chunkIndex];
|
|
109
|
+
const used = perPage.get(path) ?? 0;
|
|
110
|
+
if (used >= MAX_HITS_PER_PAGE) return false;
|
|
111
|
+
perPage.set(path, used + 1);
|
|
112
|
+
return true;
|
|
113
|
+
}).slice(0, limit).map(([chunkIndex, score]) => {
|
|
114
|
+
const chunk = chunks[chunkIndex];
|
|
115
|
+
return {
|
|
116
|
+
id: chunk.id,
|
|
117
|
+
path: chunk.path,
|
|
118
|
+
section: chunk.section,
|
|
119
|
+
title: chunk.title,
|
|
120
|
+
heading: chunk.heading,
|
|
121
|
+
anchor: chunk.anchor,
|
|
122
|
+
score: Number(score.toFixed(3)),
|
|
123
|
+
snippet: extractSnippet(chunk.text, query),
|
|
124
|
+
tags: chunk.tags.slice(0, 8)
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
export function getDoc(knowledge, path, options = {}) {
|
|
129
|
+
const wantedPath = path.replace(/^\/+/, "").replace(/^docs\//, "");
|
|
130
|
+
const pageChunks = knowledge.chunks.filter(
|
|
131
|
+
(chunk) => chunk.path === wantedPath
|
|
132
|
+
);
|
|
133
|
+
if (!pageChunks.length) return null;
|
|
134
|
+
const maxChars = options.maxChars ?? 8e3;
|
|
135
|
+
const headings = [...new Set(pageChunks.map((chunk) => chunk.heading))];
|
|
136
|
+
let selected = pageChunks;
|
|
137
|
+
if (options.heading) {
|
|
138
|
+
const wanted = options.heading.toLowerCase();
|
|
139
|
+
selected = pageChunks.filter(
|
|
140
|
+
(chunk) => chunk.heading.toLowerCase() === wanted || chunk.anchor === wanted.replace(/^#/, "")
|
|
141
|
+
);
|
|
142
|
+
if (!selected.length) selected = pageChunks;
|
|
143
|
+
}
|
|
144
|
+
const body = selected.map(
|
|
145
|
+
(chunk) => chunk.level > 1 ? `${"#".repeat(chunk.level)} ${chunk.heading}
|
|
146
|
+
|
|
147
|
+
${chunk.text}` : chunk.text
|
|
148
|
+
).join("\n\n");
|
|
149
|
+
return {
|
|
150
|
+
path: wantedPath,
|
|
151
|
+
title: pageChunks[0].title,
|
|
152
|
+
headings,
|
|
153
|
+
content: body.length > maxChars ? `${body.slice(0, maxChars)}
|
|
154
|
+
|
|
155
|
+
\u2026[truncated]` : body,
|
|
156
|
+
truncated: body.length > maxChars
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export function listDocs(knowledge) {
|
|
160
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
161
|
+
for (const chunk of knowledge.chunks) {
|
|
162
|
+
const entry = byPath.get(chunk.path) ?? {
|
|
163
|
+
path: chunk.path,
|
|
164
|
+
title: chunk.title,
|
|
165
|
+
section: chunk.section,
|
|
166
|
+
headings: []
|
|
167
|
+
};
|
|
168
|
+
if (chunk.level > 1 && !entry.headings.includes(chunk.heading))
|
|
169
|
+
entry.headings.push(chunk.heading);
|
|
170
|
+
byPath.set(chunk.path, entry);
|
|
171
|
+
}
|
|
172
|
+
return [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path));
|
|
173
|
+
}
|
|
File without changes
|