@guren/server 2.35.0 → 2.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{DocsViewerServiceProvider-BVPZjwQp.js → DocsViewerServiceProvider-DzrxQoYh.js} +15 -4
- package/dist/Event-COT3f5Xd.js +28 -0
- package/dist/{McpServiceProvider-CXOokGax.js → McpServiceProvider-BOeQyt7u.js} +2 -0
- package/dist/PrivateChannel-1krGz028.js +187 -0
- package/dist/{Resource-y-7zzZSB.js → Resource-PhnO_wNO.js} +113 -12
- package/dist/agent/derive.d.ts +23 -1
- package/dist/agent/dispatch.d.ts +12 -1
- package/dist/agent/pipeline.d.ts +6 -0
- package/dist/agent/public.d.ts +1 -1
- package/dist/agent/public.js +1 -1
- package/dist/{api-token-1-INqeVU.js → api-token-Cs1myxf6.js} +15 -1
- package/dist/auth/index.js +2 -2
- package/dist/{auth-CO9MlQNE.js → auth-DYWBlmpS.js} +1 -1
- package/dist/broadcasting/BroadcastManager.d.ts +19 -5
- package/dist/broadcasting/event-target.d.ts +9 -0
- package/dist/broadcasting/index.d.ts +3 -1
- package/dist/broadcasting/index.js +3 -2
- package/dist/broadcasting/sender-envelope.d.ts +26 -0
- package/dist/broadcasting/types.d.ts +27 -2
- package/dist/{broadcasting-D04a3R02.js → broadcasting-Bh6QoMpy.js} +50 -95
- package/dist/{contained-path-BkNSBljW.js → contained-path-DOVauNQC.js} +1 -22
- package/dist/{dev-banner-BUrHz-LF.js → dev-banner-DGxB3HGY.js} +3 -3
- package/dist/{dispatch-ENWpBxbE.js → dispatch-CDNqcB3Q.js} +51 -12
- package/dist/events/index.js +2 -1
- package/dist/{events-B6gfJkCH.js → events-DW197Nur.js} +2 -28
- package/dist/http/vite-dev-server.d.ts +18 -1
- package/dist/http/vite-manifest.d.ts +4 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +109 -22
- package/dist/internal/agent-principal.d.ts +8 -0
- package/dist/internal/testing.d.ts +1 -0
- package/dist/internal/testing.js +2 -2
- package/dist/mcp/index.js +1 -1
- package/dist/mvc/Router.d.ts +13 -0
- package/dist/mvc/inertia/InertiaEngine.d.ts +3 -0
- package/dist/mvc/inertia/props.d.ts +54 -3
- package/dist/runtime/index.js +5 -3
- package/dist/storage/index.js +1 -1
- package/dist/{storage-YPbsV9ju.js → storage-ziIKgHON.js} +2 -1
- package/dist/trim-slashes-BLY9hKAa.js +22 -0
- package/dist/vite/index.d.ts +2 -0
- package/dist/vite/index.js +283 -1
- package/dist/vite/mcp-app.d.ts +59 -0
- package/dist/vite/plugin.d.ts +12 -0
- package/dist/{vite-manifest-DO0pU1vm.js → vite-dev-server-Ag90otai.js} +25 -114
- package/dist/vite-manifest-BdXRybeq.js +129 -0
- package/package.json +1 -1
- package/dist/host-port-t6wQFaRu.js +0 -15
package/dist/vite/index.js
CHANGED
|
@@ -1,5 +1,163 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
1
3
|
import path from "node:path";
|
|
2
4
|
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
//#region src/vite/mcp-app.ts
|
|
6
|
+
/**
|
|
7
|
+
* `vite build --mode mcp-app` (RFC 0038 §4): one self-contained HTML document
|
|
8
|
+
* per page an `.agent({ ui })` route names, everything inlined, since a host's
|
|
9
|
+
* default CSP allows no origin but its own sandbox. The mode's own build only
|
|
10
|
+
* drives: each page is a nested build of the app's Vite config with one entry
|
|
11
|
+
* and no code splitting, which a single multi-input build cannot give.
|
|
12
|
+
*/
|
|
13
|
+
const MCP_APP_MODE = "mcp-app";
|
|
14
|
+
/** Names the page a nested build makes; unset in the driving build. */
|
|
15
|
+
const MCP_APP_PAGE_ENV = "GUREN_MCP_APP_PAGE";
|
|
16
|
+
/** `rolldown` or `rollup`: which bundler the driver's Vite runs, so a page build disables splitting in its spelling. */
|
|
17
|
+
const MCP_APP_BUNDLER_ENV = "GUREN_MCP_APP_BUNDLER";
|
|
18
|
+
const MCP_APP_OUT_DIR = ".guren/mcp-app";
|
|
19
|
+
const MCP_APP_MODULE = ".guren/mcp-app/index.gen.ts";
|
|
20
|
+
const MCP_APP_SETUP_FILE = "resources/js/mcp-app.tsx";
|
|
21
|
+
const MCP_APP_ENTRY_ID = "\0guren-mcp-app-entry";
|
|
22
|
+
const MCP_APP_DRIVER_ID = "\0guren-mcp-app-driver";
|
|
23
|
+
const AGENTS_MODULE = ".guren/agents.gen.ts";
|
|
24
|
+
const PAGE_EXTENSIONS = [
|
|
25
|
+
".tsx",
|
|
26
|
+
".jsx",
|
|
27
|
+
".ts",
|
|
28
|
+
".js"
|
|
29
|
+
];
|
|
30
|
+
/** The page ids codegen wrote as `agentUiPages`; none when the app declares no `ui` route. */
|
|
31
|
+
function readAgentUiPages(root) {
|
|
32
|
+
let source;
|
|
33
|
+
try {
|
|
34
|
+
source = readFileSync(path.resolve(root, AGENTS_MODULE), "utf8");
|
|
35
|
+
} catch {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
if (!source.includes("export const agentUiPages")) return [];
|
|
39
|
+
const literal = /^export const agentUiPages = (\[.*\]) as const$/mu.exec(source)?.[1];
|
|
40
|
+
const parsed = literal === void 0 ? void 0 : JSON.parse(literal);
|
|
41
|
+
if (!Array.isArray(parsed)) throw new Error(`mcp-app: cannot read agentUiPages in ${AGENTS_MODULE}; run \`guren codegen\`.`);
|
|
42
|
+
return parsed.filter((id) => typeof id === "string");
|
|
43
|
+
}
|
|
44
|
+
/** The page module an id names, by the rule `pages.gen.ts` is written with: `pages/<id>` under the resources directory. */
|
|
45
|
+
function resolvePageFile(context, id) {
|
|
46
|
+
const base = path.resolve(context.root, context.resourcesDir, "pages", id);
|
|
47
|
+
return PAGE_EXTENSIONS.map((extension) => `${base}${extension}`).find((file) => existsSync(file));
|
|
48
|
+
}
|
|
49
|
+
function renderSetupTemplate(stylesheet) {
|
|
50
|
+
return `${stylesheet ? `import '${stylesheet}'\n` : ""}import React from 'react'
|
|
51
|
+
import { createRoot } from 'react-dom/client'
|
|
52
|
+
import { ErrorBoundary } from '@guren/inertia-client'
|
|
53
|
+
import type { StartMcpAppOptions } from '@guren/inertia-client/mcp-app'
|
|
54
|
+
|
|
55
|
+
// What every MCP App view mounts with (RFC 0038 §4). Owned by the app: keep its
|
|
56
|
+
// providers and stylesheet in step with resources/js/app.tsx.
|
|
57
|
+
export const setup: StartMcpAppOptions['setup'] = ({ el, App, props }) => {
|
|
58
|
+
createRoot(el).render(
|
|
59
|
+
React.createElement(ErrorBoundary, null,
|
|
60
|
+
React.createElement(App, props as any),
|
|
61
|
+
),
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
}
|
|
66
|
+
/** Writes the setup file on the first build; an existing one is the app's and is never touched. */
|
|
67
|
+
function ensureSetupFile(context) {
|
|
68
|
+
const file = path.resolve(context.root, context.setupFile);
|
|
69
|
+
const css = path.resolve(context.root, context.resourcesDir, "../css/app.css");
|
|
70
|
+
const stylesheet = existsSync(css) ? toImportPath(path.dirname(file), css) : void 0;
|
|
71
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
72
|
+
try {
|
|
73
|
+
writeFileSync(file, renderSetupTemplate(stylesheet), { flag: "wx" });
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error.code !== "EEXIST") throw error;
|
|
76
|
+
}
|
|
77
|
+
return file;
|
|
78
|
+
}
|
|
79
|
+
function renderPageEntry(id, setupFile, pageFile) {
|
|
80
|
+
return `import { startMcpApp } from '@guren/inertia-client/mcp-app'
|
|
81
|
+
import * as app from ${JSON.stringify(setupFile)}
|
|
82
|
+
import Page from ${JSON.stringify(pageFile)}
|
|
83
|
+
|
|
84
|
+
const id = ${JSON.stringify(id)}
|
|
85
|
+
void startMcpApp({
|
|
86
|
+
...app,
|
|
87
|
+
resolve: async (name) => {
|
|
88
|
+
if (name === id) return { default: Page }
|
|
89
|
+
throw new Error(\`This view bundles the page "\${id}" only, not "\${name}".\`)
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
`;
|
|
93
|
+
}
|
|
94
|
+
function renderPageDocument(scripts, styles) {
|
|
95
|
+
return `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">${styles.map((css) => `<style>${css.replaceAll("</style", "<\\/style")}</style>`).join("")}</head><body><div id="app"></div>${scripts.map((js) => `<script type="module">${js.replaceAll("<\/script", "<\\/script")}<\/script>`).join("")}</body></html>`;
|
|
96
|
+
}
|
|
97
|
+
function contentHash(html) {
|
|
98
|
+
return createHash("sha256").update(html).digest("hex").slice(0, 12);
|
|
99
|
+
}
|
|
100
|
+
function pageDocumentFile(root, id) {
|
|
101
|
+
return path.resolve(root, MCP_APP_OUT_DIR, "pages", `${id}.html`);
|
|
102
|
+
}
|
|
103
|
+
/** `.guren/mcp-app/index.gen.ts`, the shape `mcpPlugin({ ui: { pages } })` imports. */
|
|
104
|
+
function renderPagesModule(pages) {
|
|
105
|
+
return `// Generated by \`vite build --mode mcp-app\` — DO NOT EDIT.
|
|
106
|
+
export const mcpAppPages: Record<string, { html: string; hash: string }> = {
|
|
107
|
+
${Object.entries(pages).map(([id, page]) => ` ${JSON.stringify(id)}: { html: ${JSON.stringify(page.html)}, hash: ${JSON.stringify(page.hash)} },`).join("\n")}
|
|
108
|
+
}
|
|
109
|
+
`;
|
|
110
|
+
}
|
|
111
|
+
/** One page's document from its bundle, and the files it could not inline. */
|
|
112
|
+
function documentFromBundle(items) {
|
|
113
|
+
const scripts = items.filter((item) => item.type === "chunk" && item.isEntry).map((item) => item.code ?? "");
|
|
114
|
+
const styles = items.filter((item) => item.type === "asset" && item.fileName.endsWith(".css")).map((item) => typeof item.source === "string" ? item.source : new TextDecoder().decode(item.source));
|
|
115
|
+
const leftovers = items.filter((item) => !(item.type === "chunk" && item.isEntry) && !(item.type === "asset" && item.fileName.endsWith(".css"))).map((item) => item.fileName);
|
|
116
|
+
return {
|
|
117
|
+
html: renderPageDocument(scripts, styles),
|
|
118
|
+
leftovers
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** The driving build: every page in turn, then the module the plugin imports. */
|
|
122
|
+
async function runMcpAppBuild(options) {
|
|
123
|
+
const { context } = options;
|
|
124
|
+
const ids = readAgentUiPages(context.root);
|
|
125
|
+
rmSync(path.resolve(context.root, MCP_APP_OUT_DIR, "pages"), {
|
|
126
|
+
recursive: true,
|
|
127
|
+
force: true
|
|
128
|
+
});
|
|
129
|
+
const pages = {};
|
|
130
|
+
if (ids.length > 0) {
|
|
131
|
+
assertExtAppsInstalled(context.root);
|
|
132
|
+
ensureSetupFile(context);
|
|
133
|
+
for (const id of ids) {
|
|
134
|
+
if (!resolvePageFile(context, id)) throw new Error(`The page "${id}" an .agent({ ui }) route names has no file under ${context.resourcesDir}/pages/.`);
|
|
135
|
+
await options.buildPage(id);
|
|
136
|
+
const html = readFileSync(pageDocumentFile(context.root, id), "utf8");
|
|
137
|
+
pages[id] = {
|
|
138
|
+
html,
|
|
139
|
+
hash: contentHash(html)
|
|
140
|
+
};
|
|
141
|
+
options.log?.(`mcp-app: ${id} (${(html.length / 1024).toFixed(1)} KB)`);
|
|
142
|
+
}
|
|
143
|
+
} else options.log?.("mcp-app: no .agent({ ui }) route in .guren/agents.gen.ts; run `guren codegen` after adding one.");
|
|
144
|
+
const module = path.resolve(context.root, MCP_APP_MODULE);
|
|
145
|
+
mkdirSync(path.dirname(module), { recursive: true });
|
|
146
|
+
writeFileSync(module, renderPagesModule(pages));
|
|
147
|
+
return pages;
|
|
148
|
+
}
|
|
149
|
+
function assertExtAppsInstalled(root) {
|
|
150
|
+
try {
|
|
151
|
+
createRequire(path.resolve(root, "package.json")).resolve("@modelcontextprotocol/ext-apps");
|
|
152
|
+
} catch {
|
|
153
|
+
throw new Error("MCP App views need @modelcontextprotocol/ext-apps: run `bun add @modelcontextprotocol/ext-apps`.");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function toImportPath(fromDir, file) {
|
|
157
|
+
const relative = path.relative(fromDir, file).split(path.sep).join("/");
|
|
158
|
+
return relative.startsWith(".") ? relative : `./${relative}`;
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
3
161
|
//#region src/vite/plugin.ts
|
|
4
162
|
/** Where the generated shell goes; `.guren/` is already gitignored and codegen-owned. */
|
|
5
163
|
const PROTOTYPE_SHELL_FILE = ".guren/prototype/index.html";
|
|
@@ -26,19 +184,60 @@ function gurenVitePlugin(options = {}) {
|
|
|
26
184
|
...options
|
|
27
185
|
};
|
|
28
186
|
let prototype;
|
|
187
|
+
let mcpApp;
|
|
29
188
|
return {
|
|
30
189
|
name: "guren:vite-config",
|
|
31
190
|
enforce: "pre",
|
|
32
191
|
config(config, env) {
|
|
192
|
+
mcpApp = void 0;
|
|
33
193
|
if (env?.mode === "prototype") {
|
|
34
194
|
prototype = ensurePrototype(config, resolved);
|
|
35
195
|
return;
|
|
36
196
|
}
|
|
37
197
|
prototype = void 0;
|
|
198
|
+
if (env?.mode === "mcp-app") {
|
|
199
|
+
mcpApp = ensureMcpApp(config, resolved, process.env[MCP_APP_PAGE_ENV]);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
38
202
|
ensureDefaults(config, resolved, env);
|
|
39
203
|
},
|
|
40
204
|
configResolved(config) {
|
|
41
205
|
if (prototype) prototype.outDir = path.resolve(config.root ?? prototype.root, config.build?.outDir ?? prototype.outDir);
|
|
206
|
+
if (mcpApp) {
|
|
207
|
+
mcpApp.configFile = config.configFile;
|
|
208
|
+
mcpApp.logLevel = config.logLevel;
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
resolveId(id) {
|
|
212
|
+
if (mcpApp && (id === "\0guren-mcp-app-entry" || id === "\0guren-mcp-app-driver")) return id;
|
|
213
|
+
return null;
|
|
214
|
+
},
|
|
215
|
+
load(id) {
|
|
216
|
+
if (!mcpApp) return null;
|
|
217
|
+
if (id === "\0guren-mcp-app-driver") return "export {}\n";
|
|
218
|
+
if (id === "\0guren-mcp-app-entry" && mcpApp.page) return renderPageEntry(mcpApp.page.id, mcpApp.page.setupFile, mcpApp.page.pageFile);
|
|
219
|
+
return null;
|
|
220
|
+
},
|
|
221
|
+
generateBundle: {
|
|
222
|
+
order: "post",
|
|
223
|
+
handler(_output, bundle) {
|
|
224
|
+
const page = mcpApp?.page;
|
|
225
|
+
if (!page) return;
|
|
226
|
+
const { html, leftovers } = documentFromBundle(Object.values(bundle));
|
|
227
|
+
if (leftovers.length > 0) this.warn(`mcp-app: ${page.id} produced files a view cannot load, left out of its document: ${leftovers.join(", ")}`);
|
|
228
|
+
const file = pageDocumentFile(mcpApp.context.root, page.id);
|
|
229
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
230
|
+
writeFileSync(file, html);
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
async closeBundle() {
|
|
234
|
+
const driver = mcpApp;
|
|
235
|
+
if (!driver || driver.page) return;
|
|
236
|
+
await runMcpAppBuild({
|
|
237
|
+
context: driver.context,
|
|
238
|
+
buildPage: (id) => buildMcpAppPage(driver, id),
|
|
239
|
+
log: (message) => console.log(message)
|
|
240
|
+
});
|
|
42
241
|
},
|
|
43
242
|
configureServer(server) {
|
|
44
243
|
if (!prototype) return;
|
|
@@ -65,6 +264,89 @@ function gurenVitePlugin(options = {}) {
|
|
|
65
264
|
};
|
|
66
265
|
}
|
|
67
266
|
/**
|
|
267
|
+
* Both builds own `build` outright: nothing is written by Vite itself (the
|
|
268
|
+
* page build's `generateBundle` writes its document), and a page build is one
|
|
269
|
+
* entry with every dynamic import, stylesheet and asset inlined.
|
|
270
|
+
*/
|
|
271
|
+
function ensureMcpApp(config, options, pageId) {
|
|
272
|
+
const root = resolveRoot(config.root);
|
|
273
|
+
const context = {
|
|
274
|
+
root,
|
|
275
|
+
resourcesDir: options.resourcesDir,
|
|
276
|
+
setupFile: options.mcpApp?.entry ?? "resources/js/mcp-app.tsx"
|
|
277
|
+
};
|
|
278
|
+
config.define ??= {};
|
|
279
|
+
config.define["import.meta.env.GUREN_PROTOTYPE"] ??= "false";
|
|
280
|
+
ensureAliases(config, options, root);
|
|
281
|
+
config.publicDir = false;
|
|
282
|
+
const shared = {
|
|
283
|
+
outDir: path.resolve(root, MCP_APP_OUT_DIR, "build"),
|
|
284
|
+
write: false,
|
|
285
|
+
emptyOutDir: false,
|
|
286
|
+
copyPublicDir: false,
|
|
287
|
+
manifest: false,
|
|
288
|
+
ssrManifest: false
|
|
289
|
+
};
|
|
290
|
+
if (pageId === void 0) {
|
|
291
|
+
config.build = {
|
|
292
|
+
...config.build,
|
|
293
|
+
...shared,
|
|
294
|
+
rollupOptions: { input: MCP_APP_DRIVER_ID }
|
|
295
|
+
};
|
|
296
|
+
return { context };
|
|
297
|
+
}
|
|
298
|
+
const pageFile = resolvePageFile(context, pageId);
|
|
299
|
+
if (!pageFile) throw new Error(`mcp-app: no page file for "${pageId}" under ${options.resourcesDir}/pages/.`);
|
|
300
|
+
config.build = {
|
|
301
|
+
...config.build,
|
|
302
|
+
...shared,
|
|
303
|
+
cssCodeSplit: false,
|
|
304
|
+
assetsInlineLimit: () => true,
|
|
305
|
+
modulePreload: false,
|
|
306
|
+
rollupOptions: {
|
|
307
|
+
input: MCP_APP_ENTRY_ID,
|
|
308
|
+
output: process.env["GUREN_MCP_APP_BUNDLER"] === "rolldown" ? {
|
|
309
|
+
format: "es",
|
|
310
|
+
codeSplitting: false
|
|
311
|
+
} : {
|
|
312
|
+
format: "es",
|
|
313
|
+
inlineDynamicImports: true
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
return {
|
|
318
|
+
context,
|
|
319
|
+
page: {
|
|
320
|
+
id: pageId,
|
|
321
|
+
setupFile: path.resolve(root, context.setupFile),
|
|
322
|
+
pageFile
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
/** A nested build of the app's own Vite config, so its plugins (React, Tailwind) apply to the view as to the app. */
|
|
327
|
+
async function buildMcpAppPage(driver, id) {
|
|
328
|
+
const vite = await import("vite");
|
|
329
|
+
const previous = process.env[MCP_APP_PAGE_ENV];
|
|
330
|
+
const previousBundler = process.env[MCP_APP_BUNDLER_ENV];
|
|
331
|
+
process.env[MCP_APP_PAGE_ENV] = id;
|
|
332
|
+
process.env[MCP_APP_BUNDLER_ENV] = vite.rolldownVersion ? "rolldown" : "rollup";
|
|
333
|
+
try {
|
|
334
|
+
await vite.build({
|
|
335
|
+
root: driver.context.root,
|
|
336
|
+
configFile: driver.configFile,
|
|
337
|
+
mode: MCP_APP_MODE,
|
|
338
|
+
logLevel: driver.logLevel ?? "warn"
|
|
339
|
+
});
|
|
340
|
+
} finally {
|
|
341
|
+
restoreEnv(MCP_APP_PAGE_ENV, previous);
|
|
342
|
+
restoreEnv(MCP_APP_BUNDLER_ENV, previousBundler);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
function restoreEnv(name, value) {
|
|
346
|
+
if (value === void 0) delete process.env[name];
|
|
347
|
+
else process.env[name] = value;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
68
350
|
* The prototype branch, taken before the ordinary client defaults so none of
|
|
69
351
|
* `ensureBuild`'s outDir/base/publicDir reasoning applies: there is no server
|
|
70
352
|
* to serve `public/`, no manifest to read, and the entry is an HTML shell.
|
|
@@ -289,4 +571,4 @@ function normalizeRollupOutput(output) {
|
|
|
289
571
|
return output ?? {};
|
|
290
572
|
}
|
|
291
573
|
//#endregion
|
|
292
|
-
export { gurenVitePlugin as default, gurenVitePlugin };
|
|
574
|
+
export { MCP_APP_MODE, MCP_APP_MODULE, MCP_APP_SETUP_FILE, gurenVitePlugin as default, gurenVitePlugin, renderPagesModule };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export declare const MCP_APP_MODE = "mcp-app";
|
|
2
|
+
/** Names the page a nested build makes; unset in the driving build. */
|
|
3
|
+
export declare const MCP_APP_PAGE_ENV = "GUREN_MCP_APP_PAGE";
|
|
4
|
+
/** `rolldown` or `rollup`: which bundler the driver's Vite runs, so a page build disables splitting in its spelling. */
|
|
5
|
+
export declare const MCP_APP_BUNDLER_ENV = "GUREN_MCP_APP_BUNDLER";
|
|
6
|
+
export declare const MCP_APP_OUT_DIR = ".guren/mcp-app";
|
|
7
|
+
export declare const MCP_APP_MODULE = ".guren/mcp-app/index.gen.ts";
|
|
8
|
+
export declare const MCP_APP_SETUP_FILE = "resources/js/mcp-app.tsx";
|
|
9
|
+
export declare const MCP_APP_ENTRY_ID = "\0guren-mcp-app-entry";
|
|
10
|
+
export declare const MCP_APP_DRIVER_ID = "\0guren-mcp-app-driver";
|
|
11
|
+
export interface GurenMcpAppOptions {
|
|
12
|
+
/** The app-owned setup every view mounts with, relative to the root. Defaults to `resources/js/mcp-app.tsx`. */
|
|
13
|
+
entry?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface McpAppBuildContext {
|
|
16
|
+
root: string;
|
|
17
|
+
resourcesDir: string;
|
|
18
|
+
setupFile: string;
|
|
19
|
+
}
|
|
20
|
+
/** The page ids codegen wrote as `agentUiPages`; none when the app declares no `ui` route. */
|
|
21
|
+
export declare function readAgentUiPages(root: string): string[];
|
|
22
|
+
/** The page module an id names, by the rule `pages.gen.ts` is written with: `pages/<id>` under the resources directory. */
|
|
23
|
+
export declare function resolvePageFile(context: McpAppBuildContext, id: string): string | undefined;
|
|
24
|
+
export declare function renderSetupTemplate(stylesheet: string | undefined): string;
|
|
25
|
+
/** Writes the setup file on the first build; an existing one is the app's and is never touched. */
|
|
26
|
+
export declare function ensureSetupFile(context: McpAppBuildContext): string;
|
|
27
|
+
export declare function renderPageEntry(id: string, setupFile: string, pageFile: string): string;
|
|
28
|
+
export declare function renderPageDocument(scripts: readonly string[], styles: readonly string[]): string;
|
|
29
|
+
export declare function contentHash(html: string): string;
|
|
30
|
+
export declare function pageDocumentFile(root: string, id: string): string;
|
|
31
|
+
/** `.guren/mcp-app/index.gen.ts`, the shape `mcpPlugin({ ui: { pages } })` imports. */
|
|
32
|
+
export declare function renderPagesModule(pages: Readonly<Record<string, {
|
|
33
|
+
html: string;
|
|
34
|
+
hash: string;
|
|
35
|
+
}>>): string;
|
|
36
|
+
/** A page build's output, as the nested build's `generateBundle` sees it. */
|
|
37
|
+
export interface BundleItem {
|
|
38
|
+
type: 'chunk' | 'asset';
|
|
39
|
+
fileName: string;
|
|
40
|
+
code?: string;
|
|
41
|
+
source?: string | Uint8Array;
|
|
42
|
+
isEntry?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** One page's document from its bundle, and the files it could not inline. */
|
|
45
|
+
export declare function documentFromBundle(items: readonly BundleItem[]): {
|
|
46
|
+
html: string;
|
|
47
|
+
leftovers: string[];
|
|
48
|
+
};
|
|
49
|
+
export interface RunMcpAppBuildOptions {
|
|
50
|
+
context: McpAppBuildContext;
|
|
51
|
+
/** Builds one page; the nested build writes its document to `pageDocumentFile()`. */
|
|
52
|
+
buildPage(id: string): Promise<void>;
|
|
53
|
+
log?(message: string): void;
|
|
54
|
+
}
|
|
55
|
+
/** The driving build: every page in turn, then the module the plugin imports. */
|
|
56
|
+
export declare function runMcpAppBuild(options: RunMcpAppBuildOptions): Promise<Record<string, {
|
|
57
|
+
html: string;
|
|
58
|
+
hash: string;
|
|
59
|
+
}>>;
|
package/dist/vite/plugin.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type BundleItem, type GurenMcpAppOptions } from './mcp-app.js';
|
|
1
2
|
export interface GurenVitePluginOptions {
|
|
2
3
|
/** Alias prefix for application imports such as `@/.guren/routes.gen` (defaults to `@`). */
|
|
3
4
|
appAlias?: string;
|
|
@@ -27,6 +28,8 @@ export interface GurenVitePluginOptions {
|
|
|
27
28
|
assetFileNames?: string;
|
|
28
29
|
/** Prototype mode (`vite --mode prototype`, RFC 0021): the static, server-less build. */
|
|
29
30
|
prototype?: GurenPrototypeOptions;
|
|
31
|
+
/** MCP App mode (`vite build --mode mcp-app`, RFC 0038): one inlined document per `.agent({ ui })` page. */
|
|
32
|
+
mcpApp?: GurenMcpAppOptions;
|
|
30
33
|
}
|
|
31
34
|
export interface GurenPrototypeOptions {
|
|
32
35
|
/** Vite `base` for a build hosted under a subpath (a GitHub project page, a preview URL). Defaults to `/`. */
|
|
@@ -50,6 +53,15 @@ export declare function gurenVitePlugin(options?: GurenVitePluginOptions): {
|
|
|
50
53
|
enforce: 'pre';
|
|
51
54
|
config(config: Record<string, any>, env: Record<string, any>): void;
|
|
52
55
|
configResolved(config: Record<string, any>): void;
|
|
56
|
+
resolveId(id: string): "\0guren-mcp-app-driver" | "\0guren-mcp-app-entry" | null;
|
|
57
|
+
load(id: string): string | null;
|
|
58
|
+
generateBundle: {
|
|
59
|
+
order: 'post';
|
|
60
|
+
handler(this: {
|
|
61
|
+
warn(message: string): void;
|
|
62
|
+
}, _output: unknown, bundle: Record<string, BundleItem>): void;
|
|
63
|
+
};
|
|
64
|
+
closeBundle(): Promise<void>;
|
|
53
65
|
configureServer(server: ViteDevServerLike): (() => void) | undefined;
|
|
54
66
|
writeBundle(): void;
|
|
55
67
|
};
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { a as trimTrailingSlashes } from "./contained-path-BkNSBljW.js";
|
|
2
|
-
import { resolve } from "node:path";
|
|
3
|
-
import { readFileSync } from "node:fs";
|
|
4
1
|
//#region src/http/server-lifecycle.ts
|
|
5
2
|
const MANAGED_VITE_ENV_FLAG = "GUREN_MANAGED_VITE_DEV_SERVER";
|
|
6
3
|
const DEFAULT_DEV_ENTRY_PATH = "/resources/js/dev-entry.ts";
|
|
@@ -243,9 +240,26 @@ function adoptViteDevServer(owner, viteServer, localUrl) {
|
|
|
243
240
|
//#endregion
|
|
244
241
|
//#region src/http/vite-dev-server.ts
|
|
245
242
|
/**
|
|
243
|
+
* The port `listen()` starts its managed Vite dev server on. `guren browser:serve`
|
|
244
|
+
* restates the name in packages/cli/src/browser-serve.ts; tests/browser-serve.test.ts pins the two.
|
|
245
|
+
*/
|
|
246
|
+
const VITE_PORT_ENV = "GUREN_VITE_PORT";
|
|
247
|
+
/**
|
|
248
|
+
* `GUREN_VITE_PORT` as a port, or `undefined` when it is unset or blank, so the
|
|
249
|
+
* project's Vite config decides. Throws on anything else: a typo silently
|
|
250
|
+
* ignored would put two apps back on the same port.
|
|
251
|
+
*/
|
|
252
|
+
function readViteDevServerPort(value = typeof process === "undefined" ? void 0 : process.env[VITE_PORT_ENV]) {
|
|
253
|
+
const trimmed = value?.trim();
|
|
254
|
+
if (!trimmed) return;
|
|
255
|
+
const port = /^\d{1,5}$/u.test(trimmed) ? Number(trimmed) : NaN;
|
|
256
|
+
if (!(port <= 65535)) throw new Error(`${VITE_PORT_ENV} must be a port number from 0 to 65535, got "${value}".`);
|
|
257
|
+
return port;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
246
260
|
* Build the inline config the managed dev server starts with. `server.host` is
|
|
247
261
|
* left undefined unless asked for, so Vite's config merge drops it and the
|
|
248
|
-
* project's own `vite.config.ts` decides.
|
|
262
|
+
* project's own `vite.config.ts` decides; `server.port` likewise.
|
|
249
263
|
*/
|
|
250
264
|
function resolveViteDevServerConfig(options = {}) {
|
|
251
265
|
const { root = process.cwd(), config = {}, host, port } = options;
|
|
@@ -260,6 +274,11 @@ function resolveViteDevServerConfig(options = {}) {
|
|
|
260
274
|
}
|
|
261
275
|
};
|
|
262
276
|
}
|
|
277
|
+
/** The port the HTTP server is bound to; Vite keeps the requested one in `config.server.port`. */
|
|
278
|
+
function boundPort(server) {
|
|
279
|
+
const address = server.httpServer?.address();
|
|
280
|
+
return typeof address === "object" && address ? address.port : void 0;
|
|
281
|
+
}
|
|
263
282
|
async function startViteDevServer(options = {}) {
|
|
264
283
|
const { host, port } = options;
|
|
265
284
|
const { createServer } = await import("vite");
|
|
@@ -267,7 +286,7 @@ async function startViteDevServer(options = {}) {
|
|
|
267
286
|
try {
|
|
268
287
|
await server.listen();
|
|
269
288
|
const resolved = server.resolvedUrls;
|
|
270
|
-
const localUrls = resolved?.local?.length ? resolved.local : [`http://${typeof host === "string" ? host : "localhost"}:${server.config.server.port ?? port ?? 5173}`];
|
|
289
|
+
const localUrls = resolved?.local?.length ? resolved.local : [`http://${typeof host === "string" ? host : "localhost"}:${boundPort(server) ?? server.config.server.port ?? port ?? 5173}`];
|
|
271
290
|
const networkUrls = resolved?.network ?? [];
|
|
272
291
|
return {
|
|
273
292
|
server,
|
|
@@ -280,112 +299,4 @@ async function startViteDevServer(options = {}) {
|
|
|
280
299
|
}
|
|
281
300
|
}
|
|
282
301
|
//#endregion
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* The one rule for how Vite build output is found and addressed, shared by the
|
|
286
|
-
* Inertia asset wiring (`inertia-assets.ts`) and `viteAsset()` (`vite-assets.ts`,
|
|
287
|
-
* RFC 0014) so the two cannot drift: what counts as a manifest, where one may
|
|
288
|
-
* live, which URL prefix serves the hashed files, and what "the dev server is
|
|
289
|
-
* on" means.
|
|
290
|
-
*/
|
|
291
|
-
/**
|
|
292
|
-
* Where `configureInertiaAssets` serves hashed build output from. The deploy
|
|
293
|
-
* builds restate it as `CLIENT_ASSETS_URL_PREFIX` in @guren/core's
|
|
294
|
-
* internal/deploy-build.ts; tests/http/vite-manifest.test.ts pins the two equal.
|
|
295
|
-
*/
|
|
296
|
-
const PUBLIC_ASSETS_URL_PREFIX = "/public/assets/";
|
|
297
|
-
/** The Vite default the framework assumes when no dev-server URL is set. */
|
|
298
|
-
const DEFAULT_DEV_SERVER_URL = "http://localhost:5173";
|
|
299
|
-
/**
|
|
300
|
-
* Production for *asset* purposes: `NODE_ENV` says so and no dev server is
|
|
301
|
-
* configured — a configured dev server means dev asset serving even when
|
|
302
|
-
* `NODE_ENV` is `production` (E2E runs do exactly this).
|
|
303
|
-
*/
|
|
304
|
-
function isViteProduction(devServerUrl = process.env.VITE_DEV_SERVER_URL) {
|
|
305
|
-
return process.env.NODE_ENV === "production" && typeof devServerUrl !== "string";
|
|
306
|
-
}
|
|
307
|
-
function normalizeDevServerUrl(value) {
|
|
308
|
-
if (!value) return value;
|
|
309
|
-
const trimmed = value.trim();
|
|
310
|
-
if (!trimmed) return trimmed;
|
|
311
|
-
const stripped = trimTrailingSlashes(trimmed);
|
|
312
|
-
return stripped.length > 0 ? stripped : "/";
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Candidate client-manifest locations, `.vite/manifest.json` first: Vite >= 5
|
|
316
|
-
* writes it there, so a flat `manifest.json` is likely a stale leftover. The
|
|
317
|
-
* deploy plugins order the same pair through `@guren/core`'s deploy-build
|
|
318
|
-
* helpers and must agree, or an app with both layouts resolves different asset
|
|
319
|
-
* versions after a serverless deploy; `tests/http/vite-manifest.test.ts` pins it.
|
|
320
|
-
*/
|
|
321
|
-
function clientManifestCandidates(baseDir) {
|
|
322
|
-
const root = baseDir ?? process.cwd();
|
|
323
|
-
return [resolve(root, "public/assets/.vite/manifest.json"), resolve(root, "public/assets/manifest.json")];
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* `__path__` label for an injected manifest, so "entry not in the manifest"
|
|
327
|
-
* errors name the real source rather than a path that was never read.
|
|
328
|
-
*/
|
|
329
|
-
const INJECTED_CLIENT_MANIFEST_SOURCE = "GUREN_VITE_MANIFEST (injected by the deploy build)";
|
|
330
|
-
const INJECTED_MANIFEST_HINT = "It must hold the Vite client manifest text (deploy plugins inject it at build time), not a path.";
|
|
331
|
-
/**
|
|
332
|
-
* Attach one of the manifest's bookkeeping fields. `enumerable: false` is the
|
|
333
|
-
* invariant: a stamped field must never leak into `Object.keys` or a
|
|
334
|
-
* re-serialization of the manifest.
|
|
335
|
-
*/
|
|
336
|
-
function defineHidden(manifest, key, value) {
|
|
337
|
-
Object.defineProperty(manifest, key, {
|
|
338
|
-
value,
|
|
339
|
-
enumerable: false
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* The client manifest a deploy build injected for runtimes that do not ship
|
|
344
|
-
* `public/assets/manifest.json`: `GUREN_VITE_MANIFEST` holds the manifest *JSON
|
|
345
|
-
* text*, not a path. Fails loudly on a value that is set but not a manifest,
|
|
346
|
-
* because falling through to the filesystem would end in "run `bunx vite build`"
|
|
347
|
-
* — a diagnosis pointing away from the variable that is actually broken.
|
|
348
|
-
*/
|
|
349
|
-
function injectedClientManifest() {
|
|
350
|
-
const raw = process.env.GUREN_VITE_MANIFEST;
|
|
351
|
-
if (typeof raw !== "string" || raw.length === 0) return;
|
|
352
|
-
let parsed;
|
|
353
|
-
try {
|
|
354
|
-
parsed = JSON.parse(raw);
|
|
355
|
-
} catch (error) {
|
|
356
|
-
throw new Error(`GUREN_VITE_MANIFEST is set but is not valid JSON. ${INJECTED_MANIFEST_HINT}`, { cause: error });
|
|
357
|
-
}
|
|
358
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error(`GUREN_VITE_MANIFEST is set but does not hold a manifest object. ${INJECTED_MANIFEST_HINT}`);
|
|
359
|
-
const manifest = parsed;
|
|
360
|
-
defineHidden(manifest, "__path__", INJECTED_CLIENT_MANIFEST_SOURCE);
|
|
361
|
-
return manifest;
|
|
362
|
-
}
|
|
363
|
-
function loadViteManifest(candidatePaths, label, options = {}) {
|
|
364
|
-
const warnOnMissing = options.warnOnMissing ?? true;
|
|
365
|
-
const command = label === "SSR" ? "bunx vite build --ssr" : "bunx vite build";
|
|
366
|
-
for (const manifestPath of candidatePaths) try {
|
|
367
|
-
const raw = readFileSync(manifestPath, "utf8");
|
|
368
|
-
const manifest = JSON.parse(raw);
|
|
369
|
-
defineHidden(manifest, "__path__", manifestPath);
|
|
370
|
-
if (options.includeRaw) defineHidden(manifest, "__raw__", raw);
|
|
371
|
-
return manifest;
|
|
372
|
-
} catch (error) {
|
|
373
|
-
if (error.code !== "ENOENT") {
|
|
374
|
-
console.warn(`Unable to load ${label} Vite manifest at ${manifestPath}.`, error);
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
if (warnOnMissing && candidatePaths.length) console.warn(`Unable to load ${label} Vite manifest. Checked paths:\n${candidatePaths.map((p) => ` - ${p}`).join("\n")}\nRun \`${command}\` before starting in production.`);
|
|
379
|
-
}
|
|
380
|
-
function getManifestFile(entry) {
|
|
381
|
-
if (!entry) return;
|
|
382
|
-
if (Array.isArray(entry)) return entry[0];
|
|
383
|
-
if (typeof entry === "string") return entry;
|
|
384
|
-
if ("file" in entry && typeof entry.file === "string") return entry.file;
|
|
385
|
-
}
|
|
386
|
-
function getManifestCss(entry) {
|
|
387
|
-
if (!entry || Array.isArray(entry) || typeof entry === "string") return;
|
|
388
|
-
return entry.css;
|
|
389
|
-
}
|
|
390
|
-
//#endregion
|
|
391
|
-
export { stopBunServerBounded as C, stopActiveViteDevServer as S, registerBunTeardown as _, getManifestCss as a, setActiveBunServer as b, isViteProduction as c, startViteDevServer as d, MANAGED_VITE_ENV_FLAG as f, hotReloadStopBound as g, getGlobalState as h, clientManifestCandidates as i, loadViteManifest as l, closeOwnedViteDevServer as m, INJECTED_CLIENT_MANIFEST_SOURCE as n, getManifestFile as o, adoptViteDevServer as p, PUBLIC_ASSETS_URL_PREFIX as r, injectedClientManifest as s, DEFAULT_DEV_SERVER_URL as t, normalizeDevServerUrl as u, releaseActiveBunServer as v, stopActiveBunServer as x, reusableActiveViteDevServer as y };
|
|
302
|
+
export { closeOwnedViteDevServer as a, registerBunTeardown as c, setActiveBunServer as d, stopActiveBunServer as f, adoptViteDevServer as i, releaseActiveBunServer as l, stopBunServerBounded as m, startViteDevServer as n, getGlobalState as o, stopActiveViteDevServer as p, MANAGED_VITE_ENV_FLAG as r, hotReloadStopBound as s, readViteDevServerPort as t, reusableActiveViteDevServer as u };
|