@assistant-ui/x-generative-compiler 0.0.15 → 0.0.17
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/compile.d.ts.map +1 -1
- package/dist/compile.js +65 -17
- package/dist/compile.js.map +1 -1
- package/dist/package.js +1 -1
- package/package.json +3 -3
- package/src/compile.test.ts +184 -1
- package/src/compile.ts +110 -18
package/dist/compile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","names":[],"sources":["../src/compile.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"compile.d.ts","names":[],"sources":["../src/compile.ts"],"mappings":";;KA2BY;UA2CK;;EAEf,QAAQ;;EAER;;EAEA;;;;;;;;;EASA;;;;;;;;;EASA;;UAGe;EACf;EACA;;;cAIW,+BAA+B;EAC9B,YAAA,iBAAiB;;;iBASf,mBAAmB;cAgCtB,qBAAsB,kBAAkB;;;;;iBAkHrC,kBACd,cACA,SAAS,iBACR"}
|
package/dist/compile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DIRECTIVE } from "./constants.js";
|
|
2
2
|
import { version } from "./package.js";
|
|
3
3
|
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
4
5
|
import { createRequire } from "node:module";
|
|
5
6
|
import * as nodePath from "node:path";
|
|
6
7
|
import { parse } from "@babel/parser";
|
|
@@ -160,6 +161,7 @@ function isLineTerminator(char) {
|
|
|
160
161
|
* regions that target needs and pruning the imports the dropped regions used.
|
|
161
162
|
*/
|
|
162
163
|
function compileGenerative(code, options) {
|
|
164
|
+
currentCompilePass++;
|
|
163
165
|
const { target, filename } = options;
|
|
164
166
|
const backendless = options.backendless ?? false;
|
|
165
167
|
const ast = parse(code, {
|
|
@@ -585,39 +587,78 @@ function matchAliasPattern(pattern, source) {
|
|
|
585
587
|
if (source.length >= prefix.length + suffix.length && source.startsWith(prefix) && source.endsWith(suffix)) return source.slice(prefix.length, source.length - suffix.length);
|
|
586
588
|
return null;
|
|
587
589
|
}
|
|
588
|
-
/**
|
|
589
|
-
* Memoizes resolved aliases per start directory. The compiler runs once per
|
|
590
|
-
* file across a build, so without this every aliased spread re-walks and
|
|
591
|
-
* re-parses the same `tsconfig.json`. Process-lifetime, like
|
|
592
|
-
* `checkedCorePackageJsonPaths`.
|
|
593
|
-
*/
|
|
594
590
|
const tsconfigAliasesByDir = /* @__PURE__ */ new Map();
|
|
591
|
+
let currentCompilePass = 0;
|
|
592
|
+
function tsconfigFileVersion(content) {
|
|
593
|
+
return createHash("sha1").update(content).digest("hex");
|
|
594
|
+
}
|
|
595
|
+
function readTsconfigFile(state, path) {
|
|
596
|
+
const cached = state.fileContents.get(path);
|
|
597
|
+
if (cached !== void 0) return cached;
|
|
598
|
+
let content;
|
|
599
|
+
try {
|
|
600
|
+
content = readFileSync(path, "utf8");
|
|
601
|
+
} catch {
|
|
602
|
+
content = null;
|
|
603
|
+
}
|
|
604
|
+
state.fileContents.set(path, content);
|
|
605
|
+
state.fileVersions.set(path, content === null ? null : tsconfigFileVersion(content));
|
|
606
|
+
return content;
|
|
607
|
+
}
|
|
608
|
+
function currentFileVersion(path) {
|
|
609
|
+
try {
|
|
610
|
+
return tsconfigFileVersion(readFileSync(path, "utf8"));
|
|
611
|
+
} catch {
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
function isTsconfigCacheCurrent(entry) {
|
|
616
|
+
for (const [path, version] of entry.fileVersions) if (currentFileVersion(path) !== version) return false;
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
595
619
|
/** Walks up from a directory to the nearest `tsconfig.json` that declares `paths`. */
|
|
596
620
|
function loadTsconfigAliases(fromDir) {
|
|
597
621
|
const cached = tsconfigAliasesByDir.get(fromDir);
|
|
598
|
-
if (cached
|
|
622
|
+
if (cached) {
|
|
623
|
+
if (cached.validatedInPass === currentCompilePass || cached.reuseAcrossPasses && isTsconfigCacheCurrent(cached)) {
|
|
624
|
+
cached.validatedInPass = currentCompilePass;
|
|
625
|
+
return cached.aliases;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
599
628
|
let aliases = null;
|
|
629
|
+
const state = {
|
|
630
|
+
fileVersions: /* @__PURE__ */ new Map(),
|
|
631
|
+
fileContents: /* @__PURE__ */ new Map(),
|
|
632
|
+
cacheable: true
|
|
633
|
+
};
|
|
600
634
|
let dir = fromDir;
|
|
601
635
|
for (;;) {
|
|
602
636
|
const tsconfigPath = nodePath.join(dir, "tsconfig.json");
|
|
603
|
-
if (
|
|
604
|
-
aliases = readTsconfigAliases(tsconfigPath, /* @__PURE__ */ new Set());
|
|
637
|
+
if (readTsconfigFile(state, tsconfigPath) !== null) {
|
|
638
|
+
aliases = readTsconfigAliases(tsconfigPath, /* @__PURE__ */ new Set(), state);
|
|
605
639
|
if (aliases) break;
|
|
606
640
|
}
|
|
607
641
|
const parent = nodePath.dirname(dir);
|
|
608
642
|
if (parent === dir) break;
|
|
609
643
|
dir = parent;
|
|
610
644
|
}
|
|
611
|
-
tsconfigAliasesByDir.set(fromDir,
|
|
645
|
+
tsconfigAliasesByDir.set(fromDir, {
|
|
646
|
+
aliases,
|
|
647
|
+
fileVersions: state.fileVersions,
|
|
648
|
+
validatedInPass: currentCompilePass,
|
|
649
|
+
reuseAcrossPasses: state.cacheable
|
|
650
|
+
});
|
|
612
651
|
return aliases;
|
|
613
652
|
}
|
|
614
653
|
/** Reads `baseUrl`/`paths` from a tsconfig, following a single `extends` chain. */
|
|
615
|
-
function readTsconfigAliases(tsconfigPath, seen) {
|
|
654
|
+
function readTsconfigAliases(tsconfigPath, seen, state) {
|
|
616
655
|
if (seen.has(tsconfigPath)) return null;
|
|
617
656
|
seen.add(tsconfigPath);
|
|
657
|
+
const raw = readTsconfigFile(state, tsconfigPath);
|
|
658
|
+
if (raw === null) return null;
|
|
618
659
|
let config;
|
|
619
660
|
try {
|
|
620
|
-
config = parseJsonc(
|
|
661
|
+
config = parseJsonc(raw);
|
|
621
662
|
} catch {
|
|
622
663
|
return null;
|
|
623
664
|
}
|
|
@@ -632,23 +673,30 @@ function readTsconfigAliases(tsconfigPath, seen) {
|
|
|
632
673
|
for (let i = extendsList.length - 1; i >= 0; i--) {
|
|
633
674
|
const entry = extendsList[i];
|
|
634
675
|
if (typeof entry !== "string") continue;
|
|
635
|
-
const extended = resolveExtendedTsconfig(entry, configDir);
|
|
676
|
+
const extended = resolveExtendedTsconfig(entry, configDir, state);
|
|
636
677
|
if (extended) {
|
|
637
|
-
const aliases = readTsconfigAliases(extended, seen);
|
|
678
|
+
const aliases = readTsconfigAliases(extended, seen, state);
|
|
638
679
|
if (aliases) return aliases;
|
|
639
680
|
}
|
|
640
681
|
}
|
|
641
682
|
return null;
|
|
642
683
|
}
|
|
643
684
|
/** Resolves a tsconfig `extends` value (relative path or installed package). */
|
|
644
|
-
function resolveExtendedTsconfig(extendsValue, configDir) {
|
|
685
|
+
function resolveExtendedTsconfig(extendsValue, configDir, state) {
|
|
645
686
|
if (extendsValue.startsWith(".")) {
|
|
646
687
|
const base = nodePath.resolve(configDir, extendsValue);
|
|
647
|
-
|
|
688
|
+
const candidates = nodePath.extname(base) === ".json" ? [base] : [`${base}.json`, base];
|
|
689
|
+
for (const candidate of candidates) if (readTsconfigFile(state, candidate) !== null) return candidate;
|
|
690
|
+
return null;
|
|
648
691
|
}
|
|
692
|
+
const request = extendsValue.endsWith(".json") ? extendsValue : `${extendsValue}.json`;
|
|
693
|
+
const requireFromConfig = createRequire(nodePath.join(configDir, "package.json"));
|
|
649
694
|
try {
|
|
650
|
-
|
|
695
|
+
const resolved = requireFromConfig.resolve(request);
|
|
696
|
+
readTsconfigFile(state, resolved);
|
|
697
|
+
return resolved;
|
|
651
698
|
} catch {
|
|
699
|
+
state.cacheable = false;
|
|
652
700
|
return null;
|
|
653
701
|
}
|
|
654
702
|
}
|
package/dist/compile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.js","names":["pkgJson.version"],"sources":["../src/compile.ts"],"sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport * as nodePath from \"node:path\";\nimport { parse } from \"@babel/parser\";\nimport _traverse, { type NodePath } from \"@babel/traverse\";\nimport _generate from \"@babel/generator\";\nimport * as t from \"@babel/types\";\nimport { satisfies } from \"semver\";\nimport { DIRECTIVE, type Target } from \"./constants\";\nimport pkgJson from \"../package.json\" with { type: \"json\" };\n\nconst parserPlugins = [\n \"typescript\",\n \"jsx\",\n \"explicitResourceManagement\",\n] as NonNullable<NonNullable<Parameters<typeof parse>[1]>[\"plugins\"]>;\n\n// @babel/traverse and @babel/generator are CJS; their default export is the\n// function itself under some interop and `{ default }` under others.\nconst traverse = (\n typeof _traverse === \"function\" ? _traverse : (_traverse as any).default\n) as typeof _traverse;\nconst generate = (\n typeof _generate === \"function\" ? _generate : (_generate as any).default\n) as typeof _generate;\n\nexport type ToolType = \"frontend\" | \"backend\" | \"human\" | \"provider\";\n\n/** The required wrapper around a toolkit's tools (stripped at build time). */\nconst TOOLKIT_WRAPPER = \"defineToolkit\";\n/** The helper that produces MCP-only toolkit fragments. */\nconst MCP_TOOLKIT_WRAPPER = \"defineMcpToolkit\";\n/** The required wrapper around a generative-UI library (stripped at build time). */\nconst COMPONENTS_WRAPPER = \"defineGenerativeComponents\";\n/** The core package whose metadata declares supported compiler versions. */\nconst CORE_PACKAGE = \"@assistant-ui/core\";\n/** This package, checked against core's compatibility range. */\nconst COMPILER_PACKAGE = \"@assistant-ui/x-generative-compiler\";\n/** Packages that re-export core's generative markers. */\nconst DISTRIBUTION_PACKAGES = [\n CORE_PACKAGE,\n \"@assistant-ui/react\",\n \"@assistant-ui/react-native\",\n \"@assistant-ui/react-ink\",\n] as const;\n/** Package that exports the generative UI runtime split by export condition. */\nconst GENERATIVE_UI_PACKAGE = \"@assistant-ui/react-generative-ui\";\n/**\n * The class whose instances expose split-by-condition tools (`present()`,\n * `promptUser()`). A toolkit entry that calls a method on one of these passes\n * through untouched — the library, not this compiler, routes its halves.\n */\nconst GENERATIVE_FACTORY = \"JSONGenerativeUI\";\n/**\n * The factory producing an interactable's complete tool entry. Unlike\n * `JSONGenerativeUI`, its package has no per-target builds, so this compiler\n * splits the inline config: the client keeps `render`, the server drops it.\n * The factory's own `execute` is internal and client-safe (frontend tool).\n */\nconst INTERACTABLE_TOOL_FACTORY = \"unstable_interactableTool\";\n\n/** Mutable per-build outcomes the toolkit pass reports back for directive/guard injection. */\ninterface TargetFlags {\n /** A `render` survived on a client build (→ emit `\"use client\"`). */\n keptRender: boolean;\n /** A backend `execute` survived on a server build (→ emit `import \"server-only\"`). */\n keptBackendExecute: boolean;\n}\n\nexport interface CompileOptions {\n /** Which build target to emit. */\n target: Target;\n /** Source filename, used for error messages and source maps. */\n filename?: string;\n /** Emit a source map alongside the code. */\n sourceMaps?: boolean;\n /**\n * Whether the `server` target prepends `import \"server-only\"` when it keeps a\n * backend `execute`. Defaults to `true` (the Next.js/RSC convention, where the\n * guard fires via the `react-server` condition). Set `false` for bundlers\n * without a `react-server` layer (e.g. a Vite/TanStack Start `ssr` build),\n * where the split is already structural and importing `server-only` would\n * throw — see the Vite integration.\n */\n injectServerOnly?: boolean;\n /**\n * No backend of the app imports this module's server build (e.g. cloud-hosted\n * runs), so the client is the only place the model can learn the schemas\n * from. The `client` target then keeps frontend/human tool schemas\n * uploadable — no `unstable_backendDefault` marker is stamped — and local\n * `new JSONGenerativeUI({ ... })` instances are constructed with\n * `backendless: true` so their `present`/`prompt_user` schemas upload too.\n */\n backendless?: boolean;\n}\n\nexport interface CompileResult {\n code: string;\n map?: object | null;\n}\n\n/** Thrown when a `\"use generative\"` file violates an authoring constraint. */\nexport class GenerativeCompileError extends Error {\n constructor(message: string, filename?: string) {\n super(\n `[assistant-ui/use-generative]${filename ? ` ${filename}:` : \"\"} ${message}`,\n );\n this.name = \"GenerativeCompileError\";\n }\n}\n\n/** Whether a source string opts into generative compilation via the directive. */\nexport function isGenerativeModule(code: string): boolean {\n // The directive must be a leading statement, preceded only by an optional BOM,\n // whitespace, and line/block comments. Scanned linearly — a regex over the\n // comment prefix is prone to catastrophic backtracking on crafted input.\n let i = code.charCodeAt(0) === 0xfeff ? 1 : 0;\n for (;;) {\n while (i < code.length && /\\s/.test(code[i]!)) i++;\n if (code.startsWith(\"//\", i)) {\n const nl = code.indexOf(\"\\n\", i);\n if (nl === -1) return false;\n i = nl + 1;\n } else if (code.startsWith(\"/*\", i)) {\n const end = code.indexOf(\"*/\", i + 2);\n if (end === -1) return false;\n i = end + 2;\n } else {\n break;\n }\n }\n\n const quote = code[i];\n if (quote !== '\"' && quote !== \"'\") return false;\n\n const directiveStart = i + 1;\n if (!code.startsWith(DIRECTIVE, directiveStart)) return false;\n\n const directiveEnd = directiveStart + DIRECTIVE.length;\n if (code[directiveEnd] !== quote) return false;\n\n return hasDirectiveTerminator(code, directiveEnd + 1);\n}\n\nexport const isGenerativeSource = (filename: string, source: string): boolean =>\n /\\.[cm]?[jt]sx?$/.test(filename) && isGenerativeModule(source);\n\nfunction hasDirectiveTerminator(code: string, start: number): boolean {\n let i = start;\n let sawLineTerminator = false;\n for (;;) {\n if (i >= code.length) return true;\n\n const char = code.charCodeAt(i);\n if (isSemicolon(char)) return true;\n if (isLineTerminator(char)) {\n sawLineTerminator = true;\n i++;\n continue;\n }\n\n if (code.startsWith(\"//\", i)) {\n const lineEnd = nextLineTerminatorIndex(code, i + 2);\n if (lineEnd === -1) return true;\n sawLineTerminator = true;\n i = lineEnd + 1;\n continue;\n }\n\n if (code.startsWith(\"/*\", i)) {\n const end = code.indexOf(\"*/\", i + 2);\n if (end === -1) return false;\n sawLineTerminator ||= containsLineTerminator(code, i + 2, end);\n i = end + 2;\n continue;\n }\n\n if (/\\s/.test(code[i]!)) {\n i++;\n continue;\n }\n\n return sawLineTerminator && !startsExpressionContinuation(code, i);\n }\n}\n\nfunction nextLineTerminatorIndex(code: string, start: number): number {\n for (let i = start; i < code.length; i++) {\n if (isLineTerminator(code.charCodeAt(i))) return i;\n }\n return -1;\n}\n\nfunction containsLineTerminator(\n code: string,\n start: number,\n end: number,\n): boolean {\n for (let i = start; i < end; i++) {\n if (isLineTerminator(code.charCodeAt(i))) return true;\n }\n return false;\n}\n\nfunction startsExpressionContinuation(code: string, start: number): boolean {\n switch (code[start]) {\n case \".\":\n case \"+\":\n case \"-\":\n case \"*\":\n case \"/\":\n case \"%\":\n case \"<\":\n case \">\":\n case \"=\":\n case \"!\":\n case \"&\":\n case \"|\":\n case \"^\":\n case \"?\":\n case \":\":\n case \",\":\n case \"[\":\n case \"(\":\n case \"`\":\n return true;\n }\n\n return (\n startsKeywordContinuation(code, start, \"as\") ||\n startsKeywordContinuation(code, start, \"in\") ||\n startsKeywordContinuation(code, start, \"instanceof\") ||\n startsKeywordContinuation(code, start, \"satisfies\")\n );\n}\n\nfunction startsKeywordContinuation(\n code: string,\n start: number,\n keyword: string,\n): boolean {\n if (!code.startsWith(keyword, start)) return false;\n const next = code[start + keyword.length];\n return next === undefined || !/[\\p{ID_Continue}$]/u.test(next);\n}\n\nfunction isSemicolon(char: number): boolean {\n return char === 59;\n}\n\nfunction isLineTerminator(char: number): boolean {\n return char === 10 || char === 13 || char === 0x2028 || char === 0x2029;\n}\n\n/**\n * Rewrites a `\"use generative\"` module for a single build target, keeping only the\n * regions that target needs and pruning the imports the dropped regions used.\n */\nexport function compileGenerative(\n code: string,\n options: CompileOptions,\n): CompileResult {\n const { target, filename } = options;\n const backendless = options.backendless ?? false;\n\n const ast = parse(code, {\n sourceType: \"module\",\n plugins: parserPlugins,\n });\n\n if (!ast.program.directives.some((d) => d.value.value === DIRECTIVE)) {\n throw new GenerativeCompileError(\n `missing \"${DIRECTIVE}\" directive`,\n filename,\n );\n }\n\n ensureCompilerCompatibleWithCore(ast, filename);\n\n // A module may hold several `defineToolkit(...)` / `defineGenerativeComponents(...)`\n // calls anywhere (e.g. a library built inside `new JSONGenerativeUI(...)` plus\n // the toolkit that exposes it). Tools may reference a `JSONGenerativeUI`\n // instance's `present()`/`promptUser()`, which the library — not this compiler —\n // splits across builds via export conditions; collect those instance names so\n // such entries pass through.\n ensureDefaultExport(ast, filename);\n const generativeInstances = collectGenerativeInstances(ast);\n const interactableToolImports = collectInteractableToolImports(ast);\n const { spreadNames: toolkitSpreadNames, namespaceImports } =\n collectToolkitSpreadNames(ast, filename, createToolkitNameContext());\n\n const flags: TargetFlags = { keptRender: false, keptBackendExecute: false };\n\n traverse(ast, {\n CallExpression(path: NodePath<t.CallExpression>) {\n const callee = path.node.callee;\n const object = t.isObjectExpression(path.node.arguments[0])\n ? path.node.arguments[0]\n : null;\n\n if (t.isIdentifier(callee, { name: COMPONENTS_WRAPPER })) {\n if (!object) {\n throw new GenerativeCompileError(\n `${COMPONENTS_WRAPPER}() takes an inline object literal of components`,\n filename,\n );\n }\n compileComponents(object, target, flags, filename);\n // Unwrap the authoring helper to the bare library object so its import\n // can be pruned.\n path.replaceWith(object);\n path.skip();\n return;\n }\n\n if (t.isIdentifier(callee, { name: TOOLKIT_WRAPPER })) {\n if (!object) {\n throw new GenerativeCompileError(\n `${TOOLKIT_WRAPPER}() takes an inline object literal of tools`,\n filename,\n );\n }\n compileToolkit(\n object,\n target,\n generativeInstances,\n interactableToolImports,\n toolkitSpreadNames,\n namespaceImports,\n flags,\n backendless,\n filename,\n );\n path.replaceWith(object);\n path.skip();\n }\n },\n });\n\n const { keptRender, keptBackendExecute } = flags;\n\n pruneUnused(ast);\n\n // Replace the module directives with the target-appropriate one.\n ast.program.directives = ast.program.directives.filter(\n (d) => d.value.value !== DIRECTIVE && d.value.value !== \"use client\",\n );\n if (target === \"client\" && keptRender) {\n ast.program.directives.unshift(\n t.directive(t.directiveLiteral(\"use client\")),\n );\n }\n if (\n target === \"server\" &&\n keptBackendExecute &&\n (options.injectServerOnly ?? true)\n ) {\n ast.program.body.unshift(\n t.importDeclaration([], t.stringLiteral(\"server-only\")),\n );\n }\n\n const result = generate(\n ast,\n {\n sourceMaps: options.sourceMaps ?? false,\n ...(filename ? { filename } : {}),\n jsescOption: { minimal: true },\n },\n code,\n );\n\n return { code: result.code, map: result.map };\n}\n\ninterface PackageJson {\n name?: string;\n version?: string;\n optionalDevDependencies?: Record<string, string>;\n}\n\nconst checkedCorePackageJsonPaths = new Set<string>();\n\n// This compiler's own version, inlined from package.json at build time. Read via\n// an import (not by walking the filesystem at runtime) so the literal survives\n// being bundled into a host package like `@assistant-ui/metro`, where no\n// standalone `@assistant-ui/x-generative-compiler` sits on disk to walk up to.\nconst COMPILER_VERSION = pkgJson.version;\n\nfunction ensureCompilerCompatibleWithCore(\n ast: t.File,\n filename: string | undefined,\n): void {\n const corePackageJsonPath = resolveCorePackageJson(ast, filename);\n if (\n !corePackageJsonPath ||\n checkedCorePackageJsonPaths.has(corePackageJsonPath)\n ) {\n return;\n }\n\n const corePackageJson = readPackageJson(corePackageJsonPath);\n const range = corePackageJson?.optionalDevDependencies?.[COMPILER_PACKAGE];\n if (!range) {\n checkedCorePackageJsonPaths.add(corePackageJsonPath);\n return;\n }\n\n let compatible = false;\n try {\n compatible = satisfies(COMPILER_VERSION, range, {\n includePrerelease: true,\n });\n } catch {\n throw new GenerativeCompileError(\n `${CORE_PACKAGE}@${corePackageJson.version ?? \"unknown\"} declares an ` +\n `invalid optionalDevDependencies range for ${COMPILER_PACKAGE}: ` +\n JSON.stringify(range),\n filename,\n );\n }\n\n if (!compatible) {\n throw new GenerativeCompileError(\n `${CORE_PACKAGE}@${corePackageJson.version ?? \"unknown\"} requires ` +\n `${COMPILER_PACKAGE} ${range}, but the current compiler is ` +\n `${COMPILER_VERSION}. Update @assistant-ui/next, @assistant-ui/vite, ` +\n \"or @assistant-ui/metro so their compiler satisfies the core package's \" +\n \"optionalDevDependencies range.\",\n filename,\n );\n }\n\n checkedCorePackageJsonPaths.add(corePackageJsonPath);\n}\n\nfunction resolveCorePackageJson(\n ast: t.File,\n filename: string | undefined,\n): string | null {\n for (const packageName of collectImportedDistributionPackages(ast)) {\n const packageJsonPath = resolvePackageJson(packageName, filename);\n if (!packageJsonPath) continue;\n if (packageName === CORE_PACKAGE) return packageJsonPath;\n\n const corePackageJsonPath = resolvePackageJson(\n CORE_PACKAGE,\n packageJsonPath,\n );\n if (corePackageJsonPath) return corePackageJsonPath;\n }\n\n return null;\n}\n\nfunction collectImportedDistributionPackages(ast: t.File): Set<string> {\n const packages = new Set<string>();\n\n for (const statement of ast.program.body) {\n const source =\n (t.isImportDeclaration(statement) ||\n t.isExportNamedDeclaration(statement) ||\n t.isExportAllDeclaration(statement)) &&\n statement.source\n ? statement.source.value\n : null;\n if (!source) continue;\n\n const packageName = packageNameFromSpecifier(source);\n if (packageName) packages.add(packageName);\n }\n\n return packages;\n}\n\nfunction packageNameFromSpecifier(specifier: string): string | null {\n for (const packageName of DISTRIBUTION_PACKAGES) {\n if (specifier === packageName || specifier.startsWith(`${packageName}/`)) {\n return packageName;\n }\n }\n\n return null;\n}\n\nfunction resolvePackageJson(\n packageName: string,\n filename: string | undefined,\n): string | null {\n const requirePath = normalizeRequirePath(filename);\n const require = createRequire(requirePath);\n\n try {\n return findPackageJson(require.resolve(packageName), packageName);\n } catch {\n return findPackageJsonFromNodeModules(packageName, requirePath);\n }\n}\n\nfunction normalizeRequirePath(filename: string | undefined): string {\n return cleanAbsoluteFilename(filename) ?? import.meta.url;\n}\n\n/** Strips a `?query`/`#hash` suffix and returns the path only if it is absolute. */\nfunction cleanAbsoluteFilename(filename: string | undefined): string | null {\n if (!filename) return null;\n const clean = filename.split(/[?#]/, 1)[0]!;\n return nodePath.isAbsolute(clean) ? clean : null;\n}\n\nfunction findPackageJson(\n fromPathOrUrl: string,\n packageName: string,\n): string | null {\n let current = nodePath.dirname(\n fromPathOrUrl.startsWith(\"file:\")\n ? new URL(fromPathOrUrl).pathname\n : nodePath.resolve(fromPathOrUrl),\n );\n\n for (;;) {\n const packageJsonPath = nodePath.join(current, \"package.json\");\n const packageJson = readPackageJson(packageJsonPath);\n if (packageJson?.name === packageName) return packageJsonPath;\n\n const parent = nodePath.dirname(current);\n if (parent === current) return null;\n current = parent;\n }\n}\n\nfunction findPackageJsonFromNodeModules(\n packageName: string,\n fromPathOrUrl: string,\n): string | null {\n const parts = packageName.split(\"/\");\n let current = nodePath.dirname(\n fromPathOrUrl.startsWith(\"file:\")\n ? new URL(fromPathOrUrl).pathname\n : nodePath.resolve(fromPathOrUrl),\n );\n\n for (;;) {\n const packageJsonPath = nodePath.join(\n current,\n \"node_modules\",\n ...parts,\n \"package.json\",\n );\n const packageJson = readPackageJson(packageJsonPath);\n if (packageJson?.name === packageName) return packageJsonPath;\n\n const parent = nodePath.dirname(current);\n if (parent === current) return null;\n current = parent;\n }\n}\n\nfunction readPackageJson(packageJsonPath: string): PackageJson | null {\n if (!existsSync(packageJsonPath)) return null;\n try {\n return JSON.parse(readFileSync(packageJsonPath, \"utf8\")) as PackageJson;\n } catch (error) {\n throw new GenerativeCompileError(\n `could not parse package metadata at ${packageJsonPath}: ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n}\n\n/**\n * Errors unless the module's default export is the toolkit — a `defineToolkit(...)`\n * call (through `satisfies`/`as`/parens). This is the security boundary: the\n * default export is what the runtime registers, so it must be wrapped (and thus\n * split). A bare `export default { ... }` would ship a backend `execute` to the\n * client even if some *other* `defineToolkit(...)` exists elsewhere in the file.\n */\nfunction ensureDefaultExport(ast: t.File, filename: string | undefined): void {\n const def = ast.program.body.find(\n (stmt): stmt is t.ExportDefaultDeclaration =>\n t.isExportDefaultDeclaration(stmt),\n );\n if (!def) {\n throw new GenerativeCompileError(\"missing a default export\", filename);\n }\n if (!unwrapToToolkitCall(def.declaration)) {\n throw new GenerativeCompileError(\n `the default export must be ${TOOLKIT_WRAPPER}({ ... }) or ` +\n `${MCP_TOOLKIT_WRAPPER}({ ... }) (imported from \"@assistant-ui/react\"); ` +\n \"wrapping is required so a backend `execute` can't be authored in a way \" +\n \"that reaches the client\",\n filename,\n );\n }\n}\n\n/**\n * Unwraps a node through `satisfies`/`as`/parens to a call of the named function,\n * or returns `null`.\n */\nfunction unwrapToCall(node: t.Node, name: string): t.CallExpression | null {\n if (t.isTSSatisfiesExpression(node) || t.isTSAsExpression(node)) {\n return unwrapToCall(node.expression, name);\n }\n if (t.isParenthesizedExpression(node)) {\n return unwrapToCall(node.expression, name);\n }\n if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name })) {\n return node;\n }\n return null;\n}\n\n/**\n * Unwraps a statement to its variable declaration through an `export` wrapper,\n * or returns `null`.\n */\nfunction asVariableDeclaration(\n statement: t.Statement,\n): t.VariableDeclaration | null {\n if (t.isVariableDeclaration(statement)) return statement;\n if (\n t.isExportNamedDeclaration(statement) &&\n t.isVariableDeclaration(statement.declaration)\n ) {\n return statement.declaration;\n }\n return null;\n}\n\n/**\n * Collects the names bound to `new JSONGenerativeUI(...)` (e.g.\n * `const generative = new JSONGenerativeUI({ library })`). A toolkit entry that\n * calls a method on one of these is a generative tool whose halves the library\n * routes by export condition, so it passes through the toolkit pass untouched.\n */\nfunction collectGenerativeInstances(ast: t.File): Set<string> {\n const names = new Set<string>();\n const generativeFactories = collectGenerativeFactoryImports(ast);\n for (const statement of ast.program.body) {\n const variableDeclaration = asVariableDeclaration(statement);\n if (!variableDeclaration) continue;\n for (const declaration of variableDeclaration.declarations) {\n const { id, init } = declaration;\n if (\n t.isIdentifier(id) &&\n t.isNewExpression(init) &&\n t.isIdentifier(init.callee) &&\n generativeFactories.has(init.callee.name)\n ) {\n names.add(id.name);\n }\n }\n }\n return names;\n}\n\n/**\n * Wraps a pass-through generative entry (`generative.present()`) so the tool it\n * returns loses its `unstable_backendDefault` marker. The library stamps the\n * marker at runtime, out of this compiler's reach, so a backendless client\n * build strips it post-hoc instead of threading an option into the library.\n */\nfunction stripBackendDefaultExpression(expr: t.Expression): t.Expression {\n return t.callExpression(\n t.arrowFunctionExpression(\n [\n t.objectPattern([\n t.objectProperty(\n t.identifier(\"unstable_backendDefault\"),\n t.identifier(\"_backendDefault\"),\n ),\n t.restElement(t.identifier(\"tool\")),\n ]),\n ],\n t.identifier(\"tool\"),\n ),\n [expr],\n );\n}\n\nfunction collectGenerativeFactoryImports(ast: t.File): Set<string> {\n const names = new Set<string>();\n for (const statement of ast.program.body) {\n if (\n !t.isImportDeclaration(statement) ||\n statement.source.value !== GENERATIVE_UI_PACKAGE\n ) {\n continue;\n }\n\n for (const specifier of statement.specifiers) {\n if (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported, { name: GENERATIVE_FACTORY })\n ) {\n names.add(specifier.local.name);\n }\n }\n }\n return names;\n}\n\ntype ToolkitStaticNames = readonly string[] | null;\ntype ToolkitSpreadNames = Map<string, ToolkitStaticNames>;\n\ninterface ToolkitNameContext {\n importedToolkitNamesByFile: Map<string, ToolkitStaticNames | undefined>;\n resolvingImportedToolkitNames: Set<string>;\n}\n\n/**\n * The toolkit spreads a single module's pass can recognize: safe-to-spread\n * bindings plus the namespace imports that point at a generative module. The\n * namespace set is kept separate from {@link ToolkitSpreadNames} so a\n * whole-namespace `...kit` is not mistaken for a safe identifier spread — only\n * the default export crosses the build-split boundary, so a namespace spread is\n * rejected with a specific callout rather than allowed.\n */\ninterface ToolkitSpreadInfo {\n spreadNames: ToolkitSpreadNames;\n namespaceImports: Set<string>;\n}\n\n/**\n * Collects the local names `unstable_interactableTool` is imported under from a\n * distribution package, so toolkit entries calling it can be recognized\n * (and a same-named local function can't smuggle an arbitrary call through).\n */\nfunction collectInteractableToolImports(ast: t.File): Set<string> {\n const names = new Set<string>();\n for (const statement of ast.program.body) {\n if (!t.isImportDeclaration(statement)) continue;\n if (!packageNameFromSpecifier(statement.source.value)) continue;\n for (const specifier of statement.specifiers) {\n if (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported, { name: INTERACTABLE_TOOL_FACTORY })\n ) {\n names.add(specifier.local.name);\n }\n }\n }\n return names;\n}\n\nfunction createToolkitNameContext(): ToolkitNameContext {\n return {\n importedToolkitNamesByFile: new Map(),\n resolvingImportedToolkitNames: new Set(),\n };\n}\n\n/**\n * The inline config of an `unstable_interactableTool({ ... })` toolkit entry, or `null`\n * when the entry is some other expression.\n */\nfunction interactableToolConfig(\n value: t.Node,\n imports: Set<string>,\n): t.ObjectExpression | null {\n return t.isCallExpression(value) &&\n t.isIdentifier(value.callee) &&\n imports.has(value.callee.name) &&\n t.isObjectExpression(value.arguments[0])\n ? value.arguments[0]\n : null;\n}\n\n/**\n * Toolkit identifiers that are safe to spread into a `defineToolkit({ ... })`,\n * paired with the static tool names they contain. A `null` name list means the\n * spread is safe, but its names are not statically known for duplicate checks.\n *\n * Two kinds qualify:\n *\n * - A local variable whose initializer is visible to this compiler pass: a\n * `defineToolkit(...)` binding is compiled in-place before a later spread\n * reads it, and `defineMcpToolkit(...)` entries can't contain executable code.\n * - The default import of another `\"use generative\"` module: that module is\n * split per-target by its own compiler pass, so spreading its default-exported\n * toolkit can't leak a backend `execute` to the client. Only the default\n * export crosses the generative-module boundary, so named imports don't\n * qualify — they would be `undefined` once that module is build-split.\n *\n * A namespace import (`import * as kit from \"...\"`) of a generative module is\n * tracked separately in `namespaceImports` so a `...kit` / `...kit.default`\n * spread can be rejected with a specific callout: only the default export\n * crosses the boundary, so a namespace spread is never safe.\n */\nfunction collectToolkitSpreadNames(\n ast: t.File,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitSpreadInfo {\n const spreadNames: ToolkitSpreadNames = new Map();\n const namespaceImports = new Set<string>();\n const localToolkitCalls = new Map<string, t.CallExpression>();\n\n for (const statement of ast.program.body) {\n const localDeclaration = asVariableDeclaration(statement);\n if (localDeclaration) {\n for (const declaration of localDeclaration.declarations) {\n const { id, init } = declaration;\n if (t.isIdentifier(id) && init) {\n const toolkitCall = unwrapToToolkitCall(init);\n if (toolkitCall) localToolkitCalls.set(id.name, toolkitCall);\n }\n }\n continue;\n }\n\n if (t.isImportDeclaration(statement)) {\n const defaultSpecifier = statement.specifiers.find(\n (specifier): specifier is t.ImportDefaultSpecifier =>\n t.isImportDefaultSpecifier(specifier),\n );\n const namespaceSpecifier = statement.specifiers.find(\n (specifier): specifier is t.ImportNamespaceSpecifier =>\n t.isImportNamespaceSpecifier(specifier),\n );\n if (!defaultSpecifier && !namespaceSpecifier) continue;\n\n const names = getGenerativeImportToolkitNames(\n statement.source.value,\n filename,\n context,\n );\n if (names === undefined) continue;\n\n if (defaultSpecifier) spreadNames.set(defaultSpecifier.local.name, names);\n if (namespaceSpecifier)\n namespaceImports.add(namespaceSpecifier.local.name);\n }\n }\n\n const resolveLocal = (name: string): ToolkitStaticNames | undefined => {\n if (spreadNames.has(name)) return spreadNames.get(name);\n\n const call = localToolkitCalls.get(name);\n if (!call) return undefined;\n\n const object = t.isObjectExpression(call.arguments[0])\n ? call.arguments[0]\n : null;\n const names = object\n ? collectToolkitObjectNames(object, spreadNames)\n : null;\n const publicNames = uniqueToolkitNames(names);\n\n spreadNames.set(name, publicNames);\n return publicNames;\n };\n\n for (const name of localToolkitCalls.keys()) {\n resolveLocal(name);\n }\n\n return { spreadNames, namespaceImports };\n}\n\nconst MODULE_EXTENSIONS = [\n \".ts\",\n \".tsx\",\n \".mts\",\n \".cts\",\n \".js\",\n \".jsx\",\n \".mjs\",\n \".cjs\",\n] as const;\n\n/** Extensions a specifier may carry that actually map to a TS source file. */\nconst REWRITABLE_JS_EXTENSIONS = new Set([\".js\", \".jsx\", \".mjs\", \".cjs\"]);\n\n/**\n * Reads the static tool names from the default export of an imported\n * `\"use generative\"` module. Relative specifiers and `tsconfig` path aliases\n * (e.g. `@/tools`) are resolved; anything else (a bare package, an unresolvable\n * alias) is treated as non-generative, and thus an unsafe spread.\n */\nfunction getGenerativeImportToolkitNames(\n source: string,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitStaticNames | undefined {\n const cleanFilename = cleanAbsoluteFilename(filename);\n if (!cleanFilename) return undefined;\n\n const resolved = resolveImportedModuleFile(source, cleanFilename);\n if (!resolved) return undefined;\n\n if (context.importedToolkitNamesByFile.has(resolved)) {\n return context.importedToolkitNamesByFile.get(resolved);\n }\n if (context.resolvingImportedToolkitNames.has(resolved)) return null;\n\n let code: string;\n try {\n code = readFileSync(resolved, \"utf8\");\n } catch {\n context.importedToolkitNamesByFile.set(resolved, undefined);\n return undefined;\n }\n\n if (!isGenerativeModule(code)) {\n context.importedToolkitNamesByFile.set(resolved, undefined);\n return undefined;\n }\n\n context.resolvingImportedToolkitNames.add(resolved);\n try {\n const importedAst = parse(code, {\n sourceType: \"module\",\n plugins: parserPlugins,\n });\n const names = getDefaultExportToolkitNames(importedAst, resolved, context);\n context.importedToolkitNamesByFile.set(resolved, names);\n return names;\n } catch (error) {\n if (error instanceof GenerativeCompileError) throw error;\n context.importedToolkitNamesByFile.set(resolved, null);\n return null;\n } finally {\n context.resolvingImportedToolkitNames.delete(resolved);\n }\n}\n\nfunction getDefaultExportToolkitNames(\n ast: t.File,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitStaticNames {\n const def = ast.program.body.find(\n (stmt): stmt is t.ExportDefaultDeclaration =>\n t.isExportDefaultDeclaration(stmt),\n );\n if (!def) return null;\n\n const toolkitCall = unwrapToToolkitCall(def.declaration);\n if (!toolkitCall || !t.isObjectExpression(toolkitCall.arguments[0])) {\n return null;\n }\n\n const { spreadNames } = collectToolkitSpreadNames(ast, filename, context);\n return uniqueToolkitNames(\n collectToolkitObjectNames(toolkitCall.arguments[0], spreadNames),\n );\n}\n\n/** Resolves an import specifier (relative or `tsconfig`-aliased) to a file on disk. */\nfunction resolveImportedModuleFile(\n source: string,\n fromFilename: string,\n): string | null {\n if (source.startsWith(\".\")) {\n const base = nodePath.resolve(nodePath.dirname(fromFilename), source);\n return resolveModuleFileAtPath(base);\n }\n return resolveAliasImport(source, fromFilename);\n}\n\n/**\n * Resolves a candidate module path, trying TS/JS extensions then index files. A\n * specifier may carry a `.js`-family extension that maps to a `.ts`/`.tsx`\n * source (TypeScript's `bundler`/`nodenext` resolution), so the extension is\n * dropped before probing.\n */\nfunction resolveModuleFileAtPath(base: string): string | null {\n const ext = nodePath.extname(base);\n if (ext && existsSync(base)) return base;\n\n const stem = REWRITABLE_JS_EXTENSIONS.has(ext)\n ? base.slice(0, -ext.length)\n : base;\n\n for (const candidateExt of MODULE_EXTENSIONS) {\n const candidate = `${stem}${candidateExt}`;\n if (existsSync(candidate)) return candidate;\n }\n for (const candidateExt of MODULE_EXTENSIONS) {\n const candidate = nodePath.join(base, `index${candidateExt}`);\n if (existsSync(candidate)) return candidate;\n }\n return null;\n}\n\ninterface TsconfigAliases {\n /** Absolute directory that `paths` targets resolve against. */\n baseDir: string;\n paths: Record<string, string[]>;\n}\n\n/** Resolves a `tsconfig` path alias (e.g. `@/tools/x`) to a file on disk. */\nfunction resolveAliasImport(\n source: string,\n fromFilename: string,\n): string | null {\n const tsconfig = loadTsconfigAliases(nodePath.dirname(fromFilename));\n if (!tsconfig) return null;\n\n // TypeScript matches the most specific key first: an exact (wildcard-free) key\n // beats a wildcard one, and a longer static prefix beats a shorter one.\n const patterns = Object.entries(tsconfig.paths).sort(\n ([a], [b]) => aliasSpecificity(b) - aliasSpecificity(a),\n );\n\n for (const [pattern, targets] of patterns) {\n const matched = matchAliasPattern(pattern, source);\n if (matched === null) continue;\n\n for (const target of targets) {\n const specifier = substituteAliasWildcard(target, matched);\n const file = resolveModuleFileAtPath(\n nodePath.resolve(tsconfig.baseDir, specifier),\n );\n if (file) return file;\n }\n }\n return null;\n}\n\n/** Ranks a `paths` key: an exact key outranks any wildcard; longer prefixes win. */\nfunction aliasSpecificity(pattern: string): number {\n const star = pattern.indexOf(\"*\");\n return star === -1 ? pattern.length + 1 : star;\n}\n\n/** Substitutes the single `*` in a `paths` target with the matched text. */\nfunction substituteAliasWildcard(target: string, matched: string): string {\n const star = target.indexOf(\"*\");\n if (star === -1) return target;\n return target.slice(0, star) + matched + target.slice(star + 1);\n}\n\n/**\n * Matches an import specifier against a `tsconfig` `paths` key. Returns the text\n * captured by the key's `*` (or `\"\"` for an exact, wildcard-free key), or `null`\n * when the specifier doesn't match.\n */\nfunction matchAliasPattern(pattern: string, source: string): string | null {\n const star = pattern.indexOf(\"*\");\n if (star === -1) return pattern === source ? \"\" : null;\n\n const prefix = pattern.slice(0, star);\n const suffix = pattern.slice(star + 1);\n if (\n source.length >= prefix.length + suffix.length &&\n source.startsWith(prefix) &&\n source.endsWith(suffix)\n ) {\n return source.slice(prefix.length, source.length - suffix.length);\n }\n return null;\n}\n\n/**\n * Memoizes resolved aliases per start directory. The compiler runs once per\n * file across a build, so without this every aliased spread re-walks and\n * re-parses the same `tsconfig.json`. Process-lifetime, like\n * `checkedCorePackageJsonPaths`.\n */\nconst tsconfigAliasesByDir = new Map<string, TsconfigAliases | null>();\n\n/** Walks up from a directory to the nearest `tsconfig.json` that declares `paths`. */\nfunction loadTsconfigAliases(fromDir: string): TsconfigAliases | null {\n const cached = tsconfigAliasesByDir.get(fromDir);\n if (cached !== undefined) return cached;\n\n let aliases: TsconfigAliases | null = null;\n let dir = fromDir;\n for (;;) {\n const tsconfigPath = nodePath.join(dir, \"tsconfig.json\");\n if (existsSync(tsconfigPath)) {\n aliases = readTsconfigAliases(tsconfigPath, new Set());\n if (aliases) break;\n }\n const parent = nodePath.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n\n tsconfigAliasesByDir.set(fromDir, aliases);\n return aliases;\n}\n\n/** Reads `baseUrl`/`paths` from a tsconfig, following a single `extends` chain. */\nfunction readTsconfigAliases(\n tsconfigPath: string,\n seen: Set<string>,\n): TsconfigAliases | null {\n if (seen.has(tsconfigPath)) return null;\n seen.add(tsconfigPath);\n\n let config: {\n extends?: string | string[];\n compilerOptions?: { baseUrl?: string; paths?: Record<string, string[]> };\n } | null;\n try {\n config = parseJsonc(readFileSync(tsconfigPath, \"utf8\"));\n } catch {\n return null;\n }\n if (!config) return null;\n\n const configDir = nodePath.dirname(tsconfigPath);\n const { baseUrl, paths } = config.compilerOptions ?? {};\n\n if (paths) {\n return {\n baseDir: baseUrl ? nodePath.resolve(configDir, baseUrl) : configDir,\n paths,\n };\n }\n\n // `extends` may be a string or (TS 5.0+) a string array; later entries take\n // precedence, so try them last-first. Parsed from untrusted JSONC, so the\n // array case is a real runtime shape, not just a type.\n const extendsList = Array.isArray(config.extends)\n ? config.extends\n : config.extends\n ? [config.extends]\n : [];\n for (let i = extendsList.length - 1; i >= 0; i--) {\n const entry = extendsList[i];\n if (typeof entry !== \"string\") continue;\n const extended = resolveExtendedTsconfig(entry, configDir);\n if (extended) {\n const aliases = readTsconfigAliases(extended, seen);\n if (aliases) return aliases;\n }\n }\n return null;\n}\n\n/** Resolves a tsconfig `extends` value (relative path or installed package). */\nfunction resolveExtendedTsconfig(\n extendsValue: string,\n configDir: string,\n): string | null {\n if (extendsValue.startsWith(\".\")) {\n const base = nodePath.resolve(configDir, extendsValue);\n const candidates =\n nodePath.extname(base) === \".json\" ? [base] : [`${base}.json`, base];\n return candidates.find((candidate) => existsSync(candidate)) ?? null;\n }\n try {\n return createRequire(nodePath.join(configDir, \"package.json\")).resolve(\n extendsValue.endsWith(\".json\") ? extendsValue : `${extendsValue}.json`,\n );\n } catch {\n return null;\n }\n}\n\n/** Parses JSON with `//` and block comments and trailing commas stripped (tsconfig is JSONC). */\nfunction parseJsonc(text: string): any {\n const withoutComments = text.replace(\n /\"(?:[^\"\\\\]|\\\\.)*\"|\\/\\/[^\\n\\r]*|\\/\\*[\\s\\S]*?\\*\\//g,\n (match) => (match.startsWith('\"') ? match : \"\"),\n );\n const withoutTrailingCommas = withoutComments.replace(/,(\\s*[}\\]])/g, \"$1\");\n return JSON.parse(withoutTrailingCommas);\n}\n\nfunction unwrapToToolkitCall(node: t.Node): t.CallExpression | null {\n return (\n unwrapToCall(node, TOOLKIT_WRAPPER) ??\n unwrapToCall(node, MCP_TOOLKIT_WRAPPER)\n );\n}\n\nfunction collectToolkitObjectNames(\n object: t.ObjectExpression,\n toolkitSpreadNames: ToolkitSpreadNames,\n): ToolkitStaticNames {\n const names: string[] = [];\n\n for (const entry of object.properties) {\n const entryNames = toolkitEntryNames(entry, toolkitSpreadNames);\n if (!entryNames) return null;\n names.push(...entryNames);\n }\n\n return names;\n}\n\nfunction uniqueToolkitNames(names: ToolkitStaticNames): ToolkitStaticNames {\n return names ? [...new Set(names)] : names;\n}\n\nfunction toolkitEntryNames(\n entry: t.ObjectExpression[\"properties\"][number],\n toolkitSpreadNames: ToolkitSpreadNames,\n): ToolkitStaticNames {\n if (t.isSpreadElement(entry)) {\n if (t.isIdentifier(entry.argument)) {\n return toolkitSpreadNames.get(entry.argument.name) ?? null;\n }\n\n const directMcpToolkit = unwrapToCall(entry.argument, MCP_TOOLKIT_WRAPPER);\n if (\n directMcpToolkit &&\n t.isObjectExpression(directMcpToolkit.arguments[0])\n ) {\n return collectToolkitObjectNames(\n directMcpToolkit.arguments[0],\n toolkitSpreadNames,\n );\n }\n\n return null;\n }\n\n if (t.isObjectProperty(entry) || t.isObjectMethod(entry)) {\n const name = memberName(entry.key, entry.computed);\n return name ? [name] : [];\n }\n\n return [];\n}\n\nfunction toolkitEntrySourceLabel(entry: Entry): string {\n if (t.isSpreadElement(entry)) {\n if (t.isIdentifier(entry.argument)) return `...${entry.argument.name}`;\n if (unwrapToCall(entry.argument, MCP_TOOLKIT_WRAPPER)) {\n return `...${MCP_TOOLKIT_WRAPPER}(...)`;\n }\n return \"...\";\n }\n\n if (t.isObjectProperty(entry) || t.isObjectMethod(entry)) {\n const line = entry.loc?.start.line;\n return line === undefined\n ? \"inline property\"\n : `inline property (line ${line})`;\n }\n\n return \"inline property\";\n}\n\nfunction warnDuplicateToolkitNames(\n object: t.ObjectExpression,\n toolkitSpreadNames: ToolkitSpreadNames,\n filename: string | undefined,\n): void {\n const sourcesByToolName = new Map<string, string[]>();\n for (const entry of object.properties) {\n const entryNames = toolkitEntryNames(entry, toolkitSpreadNames);\n if (!entryNames) continue;\n const source = toolkitEntrySourceLabel(entry);\n for (const name of entryNames) {\n const sources = sourcesByToolName.get(name);\n if (sources) sources.push(source);\n else sourcesByToolName.set(name, [source]);\n }\n }\n\n for (const [name, sources] of sourcesByToolName) {\n const winner = sources.at(-1);\n const overridden = sources.slice(0, -1);\n if (winner === undefined || overridden.length === 0) continue;\n console.warn(\n new GenerativeCompileError(\n `Duplicate tool name \"${name}\": ${winner} overrides ` +\n `${overridden.join(\", \")}. ` +\n \"JavaScript object spread keeps the last definition.\",\n filename,\n ).message,\n );\n }\n}\n\nfunction isSafeToolkitSpread(\n entry: t.SpreadElement,\n toolkitSpreadNames: ToolkitSpreadNames,\n): boolean {\n if (t.isIdentifier(entry.argument)) {\n return toolkitSpreadNames.has(entry.argument.name);\n }\n\n const directMcpToolkit = unwrapToCall(entry.argument, MCP_TOOLKIT_WRAPPER);\n return (\n !!directMcpToolkit && t.isObjectExpression(directMcpToolkit.arguments[0])\n );\n}\n\nfunction genericUnsafeToolkitEntryMessage(): string {\n return (\n \"each tool must be an inline object literal (`name: { ... }`) or a \" +\n \"compiler-visible toolkit spread / generative tool (e.g. \" +\n \"`...defineMcpToolkit(...)`, `...baseToolkit`, \" +\n \"`generative.present()`, or `unstable_interactableTool(...)`) so its \" +\n \"`execute` can be routed\"\n );\n}\n\nfunction describeUnsafeToolkitEntry(entry: Entry): string {\n if (!t.isObjectProperty(entry)) return genericUnsafeToolkitEntryMessage();\n\n const toolName = memberName(entry.key, entry.computed);\n const raw = entryRawValue(entry);\n if (!toolName || !raw) return genericUnsafeToolkitEntryMessage();\n\n return (\n `tool \"${toolName}\" cannot be \\`${generate(raw).code}\\`; use an inline ` +\n `object literal (\\`${toolName}: { ... }\\`) or a compiler-visible toolkit ` +\n \"spread / generative tool so its `execute` can be routed\"\n );\n}\n\nfunction namespaceToolkitSpreadMessage(): string {\n return (\n 'a namespace import (`import * as kit from \"...\"`) can\\'t be spread into ' +\n `${TOOLKIT_WRAPPER}({ ... }); only a generative module's default export ` +\n \"crosses the build-split boundary, so import it as a default \" +\n '(`import kit from \"...\"`) and spread `...kit` instead'\n );\n}\n\n/** The `kit` in `...kit` or `...kit.default`, or null for a computed/opaque base. */\nfunction namespaceSpreadBaseName(argument: t.Expression): string | null {\n if (t.isIdentifier(argument)) return argument.name;\n if (\n t.isMemberExpression(argument) &&\n !argument.computed &&\n t.isIdentifier(argument.object)\n ) {\n return argument.object.name;\n }\n return null;\n}\n\n/** The `JSONGenerativeUI` methods that produce a split-by-condition tool. */\nconst GENERATIVE_TOOL_METHODS = new Set([\"present\", \"promptUser\"]);\n\n/**\n * Whether a toolkit entry's value is a call to a tool-producing method on a\n * collected `JSONGenerativeUI` instance (`generative.present()`), which passes\n * through. The method name is checked too, so a typo like `generative.presnt()`\n * is a compile error here rather than a pass-through that fails at runtime.\n */\nfunction isGenerativeToolEntry(value: t.Node, instances: Set<string>): boolean {\n return (\n t.isCallExpression(value) &&\n t.isMemberExpression(value.callee) &&\n !value.callee.computed &&\n t.isIdentifier(value.callee.object) &&\n instances.has(value.callee.object.name) &&\n t.isIdentifier(value.callee.property) &&\n GENERATIVE_TOOL_METHODS.has(value.callee.property.name)\n );\n}\n\n/**\n * Splits a `defineGenerativeComponents({ ... })` library for a build target:\n * a component's `render` (and the client imports it alone uses) is dropped on\n * the server; `properties`/`description` stay on both, since they drive the tool\n * schema either way. Mutates the object in place.\n */\nfunction compileComponents(\n object: t.ObjectExpression,\n target: Target,\n flags: TargetFlags,\n filename: string | undefined,\n): void {\n for (const entry of object.properties) {\n const value = entryValue(entry);\n if (!value) {\n throw new GenerativeCompileError(\n `each component in ${COMPONENTS_WRAPPER}() must be an inline object ` +\n \"literal (`name: { ... }`) so its `render` can be routed\",\n filename,\n );\n }\n if (!findMember(value, \"render\")) continue;\n // The client keeps `render` (and so needs the module marked `\"use client\"`);\n // the server drops it, since only the schema reaches the model there.\n if (target === \"client\") flags.keptRender = true;\n else removeMember(value, \"render\");\n }\n}\n\n/**\n * Splits a `defineToolkit({ ... })` for a build target. Each inline tool is\n * routed by inferred type (see the per-entry logic); a generative entry like\n * `generative.present()` passes through, the library having already split it.\n * Mutates the object in place and records outcomes in {@link TargetFlags}.\n */\nfunction compileToolkit(\n object: t.ObjectExpression,\n target: Target,\n instances: Set<string>,\n interactableToolImports: Set<string>,\n toolkitSpreadNames: ToolkitSpreadNames,\n namespaceImports: Set<string>,\n flags: TargetFlags,\n backendless: boolean,\n filename: string | undefined,\n): void {\n // Split builds compile both targets; emit target-independent warnings from\n // the client pass so each duplicate is logged once.\n if (target === \"client\") {\n warnDuplicateToolkitNames(object, toolkitSpreadNames, filename);\n }\n\n const nextProperties: t.ObjectExpression[\"properties\"] = [];\n\n for (const entry of object.properties) {\n const value = entryValue(entry);\n if (!value) {\n if (\n t.isSpreadElement(entry) &&\n isSafeToolkitSpread(entry, toolkitSpreadNames)\n ) {\n nextProperties.push(entry);\n continue;\n }\n // A generative tool (`generative.present()`) is split by the library's\n // export conditions, so it is safe to keep verbatim. Anything else — a\n // method or opaque call like `makeTool()` — can't be analyzed, so its\n // `execute` could reach the client unstripped.\n const raw = entryRawValue(entry);\n if (raw && isGenerativeToolEntry(raw, instances)) {\n if (backendless && target === \"client\" && t.isObjectProperty(entry)) {\n entry.value = stripBackendDefaultExpression(raw);\n }\n nextProperties.push(entry);\n continue;\n }\n const config =\n raw && interactableToolConfig(raw, interactableToolImports);\n if (config) {\n if (target === \"client\") flags.keptRender = true;\n else removeMember(config, \"render\");\n nextProperties.push(entry);\n continue;\n }\n if (t.isSpreadElement(entry)) {\n const namespaceBase = namespaceSpreadBaseName(entry.argument);\n if (namespaceBase && namespaceImports.has(namespaceBase)) {\n throw new GenerativeCompileError(\n namespaceToolkitSpreadMessage(),\n filename,\n );\n }\n }\n throw new GenerativeCompileError(\n describeUnsafeToolkitEntry(entry),\n filename,\n );\n }\n\n // Nature is inferred from `execute` (see inferToolType), not an authored\n // `type`. The resolved type is written back below so the runtime keeps it.\n const toolName = t.isObjectProperty(entry)\n ? memberName(entry.key, entry.computed)\n : undefined;\n const execute = findMember(value, \"execute\");\n const isStub = execute ? executeIsStubTool(execute) : false;\n const isExternal = execute ? executeIsExternalTool(execute) : false;\n const type = inferToolType(value, toolName, filename);\n const hasRender = !!findMember(value, \"render\");\n const hasRenderText = !!findMember(value, \"renderText\");\n\n if (type === \"frontend\" && !hasRender && !hasRenderText) {\n throw new GenerativeCompileError(\n `${typedToolSubject(\"frontend\", toolName)} must declare a ` +\n \"`render` or `renderText` \" +\n \"(it has no server execute to show otherwise)\",\n filename,\n );\n }\n\n if (type === \"human\" && !hasRender) {\n throw new GenerativeCompileError(\n `${typedToolSubject(\"human\", toolName)} must declare a ` +\n \"`render` so it can collect input\",\n filename,\n );\n }\n\n if (type === \"provider\" && execute) {\n applyProviderToolConfig(value, execute, toolName, filename);\n }\n\n if (isExternal) {\n if (!hasRender && !hasRenderText) {\n throw new GenerativeCompileError(\n `${typedToolSubject(\"external\", toolName)} must declare a ` +\n \"`render` or `renderText` \" +\n \"(assistant-ui only renders calls for tools defined elsewhere)\",\n filename,\n );\n }\n if (target === \"server\") continue;\n stripExternalToolMetadata(value);\n }\n\n if (target === \"client\") {\n // A non-stub frontend execute stays (its `\"use client\"` marker is no longer needed\n // once the module is client); backend, sentinel, and stub executes are dropped.\n if (execute && type === \"frontend\" && !isStub) stripUseClient(execute);\n else if (execute) removeMember(value, \"execute\");\n if (hasRender || hasRenderText) flags.keptRender = true;\n } else {\n // server: render is never needed; only a non-external backend execute survives.\n if (hasRender) removeMember(value, \"render\");\n if (hasRenderText) removeMember(value, \"renderText\");\n if (execute) {\n if (type === \"backend\" && !isExternal) flags.keptBackendExecute = true;\n else removeMember(value, \"execute\");\n }\n }\n\n setToolType(value, type);\n setBackendDefault(value, target, type, backendless);\n nextProperties.push(entry);\n }\n\n object.properties = nextProperties;\n}\n\nfunction applyProviderToolConfig(\n object: t.ObjectExpression,\n execute: t.ObjectProperty | t.ObjectMethod,\n toolName: string | undefined,\n filename: string | undefined,\n): void {\n if (\n !t.isObjectProperty(execute) ||\n !t.isCallExpression(execute.value) ||\n execute.value.arguments.length !== 1 ||\n !t.isObjectExpression(execute.value.arguments[0])\n ) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` must receive an inline object literal\",\n filename,\n );\n }\n\n const existingNames = new Set(\n object.properties.flatMap((prop) => {\n if (!t.isObjectProperty(prop) && !t.isObjectMethod(prop)) return [];\n const name = memberName(prop.key, prop.computed);\n return name ? [name] : [];\n }),\n );\n const configNames = new Set<string>();\n\n for (const prop of execute.value.arguments[0].properties) {\n if (!t.isObjectProperty(prop)) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` config can only contain object properties\",\n filename,\n );\n }\n const name = memberName(prop.key, prop.computed);\n if (!name) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` config can only contain static property names\",\n filename,\n );\n }\n if (\n t.isFunctionExpression(prop.value) ||\n t.isArrowFunctionExpression(prop.value)\n ) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` config cannot contain function-valued properties\",\n filename,\n );\n }\n if (existingNames.has(name) || configNames.has(name)) {\n const toolLabel = toolName ? ` for \"${toolName}\"` : \"\";\n throw new GenerativeCompileError(\n `\\`providerTool(...)\\` config${toolLabel} duplicates \"${name}\"`,\n filename,\n );\n }\n configNames.add(name);\n object.properties.push(prop);\n }\n}\n\ntype Entry = t.ObjectExpression[\"properties\"][number];\n\n/** The raw AST value of an entry (any expression), or null for spreads/methods. */\nfunction entryRawValue(entry: Entry): t.Expression | null {\n if (t.isObjectProperty(entry) && t.isExpression(entry.value)) {\n return entry.value;\n }\n return null;\n}\n\nfunction entryValue(entry: Entry): t.ObjectExpression | null {\n if (t.isObjectProperty(entry) && t.isObjectExpression(entry.value)) {\n return entry.value;\n }\n return null;\n}\n\n/** A member of an entry object: `render`/`execute`/`type`, as property or method. */\nfunction findMember(\n object: t.ObjectExpression,\n name: string,\n): t.ObjectProperty | t.ObjectMethod | undefined {\n return object.properties.find(\n (p): p is t.ObjectProperty | t.ObjectMethod =>\n (t.isObjectProperty(p) || t.isObjectMethod(p)) &&\n memberName(p.key, p.computed) === name,\n );\n}\n\nfunction removeMember(object: t.ObjectExpression, name: string): void {\n object.properties = object.properties.filter(\n (p) =>\n !(\n (t.isObjectProperty(p) || t.isObjectMethod(p)) &&\n memberName(p.key, p.computed) === name\n ),\n );\n}\n\n/** The `BlockStatement` body of an `execute` member, if it has one. */\nfunction executeBody(\n member: t.ObjectProperty | t.ObjectMethod,\n): t.BlockStatement | undefined {\n if (t.isObjectMethod(member)) return member.body;\n const value = member.value;\n if (\n (t.isArrowFunctionExpression(value) || t.isFunctionExpression(value)) &&\n t.isBlockStatement(value.body)\n ) {\n return value.body;\n }\n return undefined;\n}\n\n/** Whether an `execute` opts into the client via a leading `\"use client\"`. */\nfunction executeIsClient(member: t.ObjectProperty | t.ObjectMethod): boolean {\n return !!executeBody(member)?.directives.some(\n (d) => d.value.value === \"use client\",\n );\n}\n\nfunction executeIsSentinel(\n member: t.ObjectProperty | t.ObjectMethod,\n name: string,\n): boolean {\n return (\n t.isObjectProperty(member) &&\n t.isCallExpression(member.value) &&\n t.isIdentifier(member.value.callee, { name })\n );\n}\n\n/** Whether an `execute` is the human-in-the-loop sentinel. */\nfunction executeIsHumanTool(\n member: t.ObjectProperty | t.ObjectMethod,\n): boolean {\n return (\n executeIsSentinel(member, \"humanTool\") ||\n executeIsSentinel(member, \"hitlTool\") ||\n executeIsSentinel(member, \"hitl\")\n );\n}\n\n/** Whether an `execute` is the provider-tool sentinel. */\nfunction executeIsProviderTool(\n member: t.ObjectProperty | t.ObjectMethod,\n): boolean {\n return executeIsSentinel(member, \"providerTool\");\n}\n\n/** Whether an `execute` is the local override sentinel. */\nfunction executeIsStubTool(member: t.ObjectProperty | t.ObjectMethod): boolean {\n return executeIsSentinel(member, \"stubTool\");\n}\n\n/** Whether an `execute` is the externally-defined backend tool sentinel. */\nfunction executeIsExternalTool(\n member: t.ObjectProperty | t.ObjectMethod,\n): boolean {\n return executeIsSentinel(member, \"externalTool\");\n}\n\n/** Drops the `\"use client\"` directive from an `execute` body (kept frontend). */\nfunction stripUseClient(member: t.ObjectProperty | t.ObjectMethod): void {\n const body = executeBody(member);\n if (body) {\n body.directives = body.directives.filter(\n (d) => d.value.value !== \"use client\",\n );\n }\n}\n\n/**\n * The tool's nature, inferred from its (mandatory) `execute` rather than an\n * authored `type`: `humanTool()` → `human`; `providerTool(...)` → `provider`;\n * `stubTool()` → `frontend`; `externalTool()` → `backend`; `\"use client\"` →\n * `frontend`; otherwise `backend`.\n * The loader writes the result back as a `type` field (see {@link setToolType})\n * so the runtime keeps it.\n */\nfunction inferToolType(\n object: t.ObjectExpression,\n toolName: string | undefined,\n filename: string | undefined,\n): ToolType {\n const execute = findMember(object, \"execute\");\n if (!execute) {\n throw new GenerativeCompileError(\n `${toolSubject(toolName)} must declare an \\`execute\\`; use ` +\n \"`humanTool()` for a human-in-the-loop tool\",\n filename,\n );\n }\n if (executeIsHumanTool(execute)) return \"human\";\n if (executeIsProviderTool(execute)) return \"provider\";\n if (executeIsStubTool(execute)) return \"frontend\";\n if (executeIsExternalTool(execute)) return \"backend\";\n return executeIsClient(execute) ? \"frontend\" : \"backend\";\n}\n\nfunction toolSubject(toolName: string | undefined): string {\n return toolName ? `tool \"${toolName}\"` : \"every tool\";\n}\n\nfunction typedToolSubject(type: string, toolName: string | undefined): string {\n if (toolName) return `${type} tool \"${toolName}\"`;\n const article = /^[aeiou]/i.test(type) ? \"an\" : \"a\";\n return `${article} ${type} tool`;\n}\n\nfunction stripExternalToolMetadata(object: t.ObjectExpression): void {\n // Mirror BackendTool's forbidden metadata fields: execute is stripped by the\n // main routing loop, while streamCall is also removed because there is no\n // assistant-ui executor to stream from.\n removeMember(object, \"description\");\n removeMember(object, \"parameters\");\n removeMember(object, \"disabled\");\n removeMember(object, \"toModelOutput\");\n removeMember(object, \"experimental_onSchemaValidationError\");\n removeMember(object, \"providerOptions\");\n removeMember(object, \"streamCall\");\n}\n\n/** Writes the resolved `type` back onto the tool object (replacing any author's). */\nfunction setToolType(object: t.ObjectExpression, type: ToolType): void {\n removeMember(object, \"type\");\n // Append (not prepend) so the inferred type wins over any earlier spread.\n object.properties.push(\n t.objectProperty(t.identifier(\"type\"), t.stringLiteral(type)),\n );\n}\n\nfunction setBackendDefault(\n object: t.ObjectExpression,\n target: Target,\n type: ToolType,\n backendless: boolean,\n): void {\n // Always strip any hand-authored marker first; only re-add it for client\n // frontend/human tools whose schema is already known by the backend. A\n // backendless build has no such backend, so nothing is marked.\n removeMember(object, \"unstable_backendDefault\");\n if (backendless) return;\n if (target !== \"client\" || (type !== \"frontend\" && type !== \"human\")) return;\n\n object.properties.push(\n t.objectProperty(\n t.identifier(\"unstable_backendDefault\"),\n t.objectExpression([\n t.objectProperty(t.identifier(\"parameters\"), t.booleanLiteral(true)),\n ]),\n ),\n );\n}\n\nfunction memberName(\n key: t.Node,\n computed: boolean | undefined,\n): string | undefined {\n if (computed) return undefined;\n if (t.isIdentifier(key)) return key.name;\n if (t.isStringLiteral(key)) return key.value;\n return undefined;\n}\n\n/**\n * Removes declarations and import specifiers left unreferenced after region\n * removal, to a fixpoint (a dropped helper frees what it used). Keeps exports,\n * side-effect imports, and possibly-side-effectful initializers.\n */\nfunction pruneUnused(ast: t.File): void {\n const hadSpecifiers = new WeakSet<t.ImportDeclaration>();\n for (const stmt of ast.program.body) {\n if (t.isImportDeclaration(stmt) && stmt.specifiers.length > 0) {\n hadSpecifiers.add(stmt);\n }\n }\n\n let removedSomething = true;\n while (removedSomething) {\n removedSomething = false;\n traverse(ast, {\n Program(path: NodePath<t.Program>) {\n path.scope.crawl();\n const isUnused = (name: string): boolean => {\n const binding = path.scope.getBinding(name);\n return !!binding && !binding.referenced;\n };\n\n path.node.body = path.node.body.filter((stmt) => {\n if (\n (t.isFunctionDeclaration(stmt) || t.isClassDeclaration(stmt)) &&\n stmt.id &&\n isUnused(stmt.id.name)\n ) {\n removedSomething = true;\n return false;\n }\n if (t.isVariableDeclaration(stmt)) {\n stmt.declarations = stmt.declarations.filter((decl) => {\n // Handles destructuring too: drop the declarator only when *every*\n // bound name is unused (else a `const { a } = server` survives and\n // keeps a server import in the client graph). Restricted to plain\n // patterns so a default/computed-key side effect isn't dropped.\n const names = Object.keys(t.getBindingIdentifiers(decl.id));\n if (\n names.length > 0 &&\n isPlainPattern(decl.id) &&\n isRemovableInit(decl.init) &&\n names.every(isUnused)\n ) {\n removedSomething = true;\n return false;\n }\n return true;\n });\n if (stmt.declarations.length === 0) return false;\n }\n return true;\n });\n path.stop();\n },\n });\n }\n\n traverse(ast, {\n Program(path: NodePath<t.Program>) {\n path.scope.crawl();\n for (const stmt of path.node.body) {\n if (!t.isImportDeclaration(stmt)) continue;\n stmt.specifiers = stmt.specifiers.filter((spec) => {\n const binding = path.scope.getBinding(spec.local.name);\n return binding ? binding.referenced : true;\n });\n }\n path.node.body = path.node.body.filter(\n (stmt) =>\n !(\n t.isImportDeclaration(stmt) &&\n stmt.specifiers.length === 0 &&\n hadSpecifiers.has(stmt)\n ),\n );\n path.stop();\n },\n });\n}\n\n/**\n * Whether a binding pattern is side-effect-free to drop: a plain identifier, or\n * an object/array pattern of plain bindings — no default values\n * (`{ a = expr }`) or computed keys (`{ [expr]: a }`), which would evaluate (and\n * thus could have side effects) at destructuring time.\n */\nfunction isPlainPattern(node: t.Node): boolean {\n if (t.isIdentifier(node)) return true;\n if (t.isObjectPattern(node)) {\n return node.properties.every((p) =>\n t.isRestElement(p)\n ? isPlainPattern(p.argument)\n : !p.computed && isPlainPattern(p.value),\n );\n }\n if (t.isArrayPattern(node)) {\n return node.elements.every((el) => el == null || isPlainPattern(el));\n }\n return false; // AssignmentPattern (default), member expr, etc.\n}\n\n/** Whether a variable initializer is safe to drop (no observable side effects). */\nfunction isRemovableInit(node: t.Expression | null | undefined): boolean {\n if (node == null) return true;\n if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node)) {\n return isRemovableInit(node.expression);\n }\n // Containers are removable only if every element is — a nested call (e.g.\n // `[track()]`) has an observable side effect that dropping would lose.\n if (t.isArrayExpression(node)) {\n return node.elements.every(\n (el) => el == null || (t.isExpression(el) && isRemovableInit(el)),\n );\n }\n if (t.isObjectExpression(node)) {\n return node.properties.every(\n (p) =>\n t.isObjectProperty(p) &&\n !p.computed &&\n t.isExpression(p.value) &&\n isRemovableInit(p.value),\n );\n }\n if (t.isTemplateLiteral(node)) {\n return node.expressions.every(\n (e) => t.isExpression(e) && isRemovableInit(e),\n );\n }\n return (\n t.isArrowFunctionExpression(node) ||\n t.isFunctionExpression(node) ||\n t.isClassExpression(node) ||\n t.isIdentifier(node) ||\n // non-computed only — `obj[fn()]` could hide a side-effectful key\n (t.isMemberExpression(node) && !node.computed) ||\n t.isJSXElement(node) ||\n t.isJSXFragment(node) ||\n t.isLiteral(node)\n );\n}\n"],"mappings":";;;;;;;;;;;AAWA,MAAM,gBAAgB;CACpB;CACA;CACA;AACF;AAIA,MAAM,WACJ,OAAO,cAAc,aAAa,YAAa,UAAkB;AAEnE,MAAM,WACJ,OAAO,cAAc,aAAa,YAAa,UAAkB;;AAMnE,MAAM,kBAAkB;;AAExB,MAAM,sBAAsB;;AAE5B,MAAM,qBAAqB;;AAE3B,MAAM,eAAe;;AAErB,MAAM,mBAAmB;;AAEzB,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;AACF;;AAEA,MAAM,wBAAwB;;;;;;AAM9B,MAAM,qBAAqB;;;;;;;AAO3B,MAAM,4BAA4B;;AA2ClC,IAAa,yBAAb,cAA4C,MAAM;CAChD,YAAY,SAAiB,UAAmB;EAC9C,MACE,gCAAgC,WAAW,IAAI,SAAS,KAAK,GAAG,GAAG,SACrE;EACA,KAAK,OAAO;CACd;AACF;;AAGA,SAAgB,mBAAmB,MAAuB;CAIxD,IAAI,IAAI,KAAK,WAAW,CAAC,MAAM,QAAS,IAAI;CAC5C,SAAS;EACP,OAAO,IAAI,KAAK,UAAU,KAAK,KAAK,KAAK,EAAG,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AI,KAAK,CAAC,KAAK,YACrC,EAAE,aAAa,IAAI,KACnB,EAAE,cAAc,IAAI,KACpB,EAAE,UAAU,IAAI;AAEpB"}
|
|
1
|
+
{"version":3,"file":"compile.js","names":["pkgJson.version"],"sources":["../src/compile.ts"],"sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { createHash } from \"node:crypto\";\nimport { createRequire } from \"node:module\";\nimport * as nodePath from \"node:path\";\nimport { parse } from \"@babel/parser\";\nimport _traverse, { type NodePath } from \"@babel/traverse\";\nimport _generate from \"@babel/generator\";\nimport * as t from \"@babel/types\";\nimport { satisfies } from \"semver\";\nimport { DIRECTIVE, type Target } from \"./constants\";\nimport pkgJson from \"../package.json\" with { type: \"json\" };\n\nconst parserPlugins = [\n \"typescript\",\n \"jsx\",\n \"explicitResourceManagement\",\n] as NonNullable<NonNullable<Parameters<typeof parse>[1]>[\"plugins\"]>;\n\n// @babel/traverse and @babel/generator are CJS; their default export is the\n// function itself under some interop and `{ default }` under others.\nconst traverse = (\n typeof _traverse === \"function\" ? _traverse : (_traverse as any).default\n) as typeof _traverse;\nconst generate = (\n typeof _generate === \"function\" ? _generate : (_generate as any).default\n) as typeof _generate;\n\nexport type ToolType = \"frontend\" | \"backend\" | \"human\" | \"provider\";\n\n/** The required wrapper around a toolkit's tools (stripped at build time). */\nconst TOOLKIT_WRAPPER = \"defineToolkit\";\n/** The helper that produces MCP-only toolkit fragments. */\nconst MCP_TOOLKIT_WRAPPER = \"defineMcpToolkit\";\n/** The required wrapper around a generative-UI library (stripped at build time). */\nconst COMPONENTS_WRAPPER = \"defineGenerativeComponents\";\n/** The core package whose metadata declares supported compiler versions. */\nconst CORE_PACKAGE = \"@assistant-ui/core\";\n/** This package, checked against core's compatibility range. */\nconst COMPILER_PACKAGE = \"@assistant-ui/x-generative-compiler\";\n/** Packages that re-export core's generative markers. */\nconst DISTRIBUTION_PACKAGES = [\n CORE_PACKAGE,\n \"@assistant-ui/react\",\n \"@assistant-ui/react-native\",\n \"@assistant-ui/react-ink\",\n] as const;\n/** Package that exports the generative UI runtime split by export condition. */\nconst GENERATIVE_UI_PACKAGE = \"@assistant-ui/react-generative-ui\";\n/**\n * The class whose instances expose split-by-condition tools (`present()`,\n * `promptUser()`). A toolkit entry that calls a method on one of these passes\n * through untouched — the library, not this compiler, routes its halves.\n */\nconst GENERATIVE_FACTORY = \"JSONGenerativeUI\";\n/**\n * The factory producing an interactable's complete tool entry. Unlike\n * `JSONGenerativeUI`, its package has no per-target builds, so this compiler\n * splits the inline config: the client keeps `render`, the server drops it.\n * The factory's own `execute` is internal and client-safe (frontend tool).\n */\nconst INTERACTABLE_TOOL_FACTORY = \"unstable_interactableTool\";\n\n/** Mutable per-build outcomes the toolkit pass reports back for directive/guard injection. */\ninterface TargetFlags {\n /** A `render` survived on a client build (→ emit `\"use client\"`). */\n keptRender: boolean;\n /** A backend `execute` survived on a server build (→ emit `import \"server-only\"`). */\n keptBackendExecute: boolean;\n}\n\nexport interface CompileOptions {\n /** Which build target to emit. */\n target: Target;\n /** Source filename, used for error messages and source maps. */\n filename?: string;\n /** Emit a source map alongside the code. */\n sourceMaps?: boolean;\n /**\n * Whether the `server` target prepends `import \"server-only\"` when it keeps a\n * backend `execute`. Defaults to `true` (the Next.js/RSC convention, where the\n * guard fires via the `react-server` condition). Set `false` for bundlers\n * without a `react-server` layer (e.g. a Vite/TanStack Start `ssr` build),\n * where the split is already structural and importing `server-only` would\n * throw — see the Vite integration.\n */\n injectServerOnly?: boolean;\n /**\n * No backend of the app imports this module's server build (e.g. cloud-hosted\n * runs), so the client is the only place the model can learn the schemas\n * from. The `client` target then keeps frontend/human tool schemas\n * uploadable — no `unstable_backendDefault` marker is stamped — and local\n * `new JSONGenerativeUI({ ... })` instances are constructed with\n * `backendless: true` so their `present`/`prompt_user` schemas upload too.\n */\n backendless?: boolean;\n}\n\nexport interface CompileResult {\n code: string;\n map?: object | null;\n}\n\n/** Thrown when a `\"use generative\"` file violates an authoring constraint. */\nexport class GenerativeCompileError extends Error {\n constructor(message: string, filename?: string) {\n super(\n `[assistant-ui/use-generative]${filename ? ` ${filename}:` : \"\"} ${message}`,\n );\n this.name = \"GenerativeCompileError\";\n }\n}\n\n/** Whether a source string opts into generative compilation via the directive. */\nexport function isGenerativeModule(code: string): boolean {\n // The directive must be a leading statement, preceded only by an optional BOM,\n // whitespace, and line/block comments. Scanned linearly — a regex over the\n // comment prefix is prone to catastrophic backtracking on crafted input.\n let i = code.charCodeAt(0) === 0xfeff ? 1 : 0;\n for (;;) {\n while (i < code.length && /\\s/.test(code[i]!)) i++;\n if (code.startsWith(\"//\", i)) {\n const nl = code.indexOf(\"\\n\", i);\n if (nl === -1) return false;\n i = nl + 1;\n } else if (code.startsWith(\"/*\", i)) {\n const end = code.indexOf(\"*/\", i + 2);\n if (end === -1) return false;\n i = end + 2;\n } else {\n break;\n }\n }\n\n const quote = code[i];\n if (quote !== '\"' && quote !== \"'\") return false;\n\n const directiveStart = i + 1;\n if (!code.startsWith(DIRECTIVE, directiveStart)) return false;\n\n const directiveEnd = directiveStart + DIRECTIVE.length;\n if (code[directiveEnd] !== quote) return false;\n\n return hasDirectiveTerminator(code, directiveEnd + 1);\n}\n\nexport const isGenerativeSource = (filename: string, source: string): boolean =>\n /\\.[cm]?[jt]sx?$/.test(filename) && isGenerativeModule(source);\n\nfunction hasDirectiveTerminator(code: string, start: number): boolean {\n let i = start;\n let sawLineTerminator = false;\n for (;;) {\n if (i >= code.length) return true;\n\n const char = code.charCodeAt(i);\n if (isSemicolon(char)) return true;\n if (isLineTerminator(char)) {\n sawLineTerminator = true;\n i++;\n continue;\n }\n\n if (code.startsWith(\"//\", i)) {\n const lineEnd = nextLineTerminatorIndex(code, i + 2);\n if (lineEnd === -1) return true;\n sawLineTerminator = true;\n i = lineEnd + 1;\n continue;\n }\n\n if (code.startsWith(\"/*\", i)) {\n const end = code.indexOf(\"*/\", i + 2);\n if (end === -1) return false;\n sawLineTerminator ||= containsLineTerminator(code, i + 2, end);\n i = end + 2;\n continue;\n }\n\n if (/\\s/.test(code[i]!)) {\n i++;\n continue;\n }\n\n return sawLineTerminator && !startsExpressionContinuation(code, i);\n }\n}\n\nfunction nextLineTerminatorIndex(code: string, start: number): number {\n for (let i = start; i < code.length; i++) {\n if (isLineTerminator(code.charCodeAt(i))) return i;\n }\n return -1;\n}\n\nfunction containsLineTerminator(\n code: string,\n start: number,\n end: number,\n): boolean {\n for (let i = start; i < end; i++) {\n if (isLineTerminator(code.charCodeAt(i))) return true;\n }\n return false;\n}\n\nfunction startsExpressionContinuation(code: string, start: number): boolean {\n switch (code[start]) {\n case \".\":\n case \"+\":\n case \"-\":\n case \"*\":\n case \"/\":\n case \"%\":\n case \"<\":\n case \">\":\n case \"=\":\n case \"!\":\n case \"&\":\n case \"|\":\n case \"^\":\n case \"?\":\n case \":\":\n case \",\":\n case \"[\":\n case \"(\":\n case \"`\":\n return true;\n }\n\n return (\n startsKeywordContinuation(code, start, \"as\") ||\n startsKeywordContinuation(code, start, \"in\") ||\n startsKeywordContinuation(code, start, \"instanceof\") ||\n startsKeywordContinuation(code, start, \"satisfies\")\n );\n}\n\nfunction startsKeywordContinuation(\n code: string,\n start: number,\n keyword: string,\n): boolean {\n if (!code.startsWith(keyword, start)) return false;\n const next = code[start + keyword.length];\n return next === undefined || !/[\\p{ID_Continue}$]/u.test(next);\n}\n\nfunction isSemicolon(char: number): boolean {\n return char === 59;\n}\n\nfunction isLineTerminator(char: number): boolean {\n return char === 10 || char === 13 || char === 0x2028 || char === 0x2029;\n}\n\n/**\n * Rewrites a `\"use generative\"` module for a single build target, keeping only the\n * regions that target needs and pruning the imports the dropped regions used.\n */\nexport function compileGenerative(\n code: string,\n options: CompileOptions,\n): CompileResult {\n currentCompilePass++;\n const { target, filename } = options;\n const backendless = options.backendless ?? false;\n\n const ast = parse(code, {\n sourceType: \"module\",\n plugins: parserPlugins,\n });\n\n if (!ast.program.directives.some((d) => d.value.value === DIRECTIVE)) {\n throw new GenerativeCompileError(\n `missing \"${DIRECTIVE}\" directive`,\n filename,\n );\n }\n\n ensureCompilerCompatibleWithCore(ast, filename);\n\n // A module may hold several `defineToolkit(...)` / `defineGenerativeComponents(...)`\n // calls anywhere (e.g. a library built inside `new JSONGenerativeUI(...)` plus\n // the toolkit that exposes it). Tools may reference a `JSONGenerativeUI`\n // instance's `present()`/`promptUser()`, which the library — not this compiler —\n // splits across builds via export conditions; collect those instance names so\n // such entries pass through.\n ensureDefaultExport(ast, filename);\n const generativeInstances = collectGenerativeInstances(ast);\n const interactableToolImports = collectInteractableToolImports(ast);\n const { spreadNames: toolkitSpreadNames, namespaceImports } =\n collectToolkitSpreadNames(ast, filename, createToolkitNameContext());\n\n const flags: TargetFlags = { keptRender: false, keptBackendExecute: false };\n\n traverse(ast, {\n CallExpression(path: NodePath<t.CallExpression>) {\n const callee = path.node.callee;\n const object = t.isObjectExpression(path.node.arguments[0])\n ? path.node.arguments[0]\n : null;\n\n if (t.isIdentifier(callee, { name: COMPONENTS_WRAPPER })) {\n if (!object) {\n throw new GenerativeCompileError(\n `${COMPONENTS_WRAPPER}() takes an inline object literal of components`,\n filename,\n );\n }\n compileComponents(object, target, flags, filename);\n // Unwrap the authoring helper to the bare library object so its import\n // can be pruned.\n path.replaceWith(object);\n path.skip();\n return;\n }\n\n if (t.isIdentifier(callee, { name: TOOLKIT_WRAPPER })) {\n if (!object) {\n throw new GenerativeCompileError(\n `${TOOLKIT_WRAPPER}() takes an inline object literal of tools`,\n filename,\n );\n }\n compileToolkit(\n object,\n target,\n generativeInstances,\n interactableToolImports,\n toolkitSpreadNames,\n namespaceImports,\n flags,\n backendless,\n filename,\n );\n path.replaceWith(object);\n path.skip();\n }\n },\n });\n\n const { keptRender, keptBackendExecute } = flags;\n\n pruneUnused(ast);\n\n // Replace the module directives with the target-appropriate one.\n ast.program.directives = ast.program.directives.filter(\n (d) => d.value.value !== DIRECTIVE && d.value.value !== \"use client\",\n );\n if (target === \"client\" && keptRender) {\n ast.program.directives.unshift(\n t.directive(t.directiveLiteral(\"use client\")),\n );\n }\n if (\n target === \"server\" &&\n keptBackendExecute &&\n (options.injectServerOnly ?? true)\n ) {\n ast.program.body.unshift(\n t.importDeclaration([], t.stringLiteral(\"server-only\")),\n );\n }\n\n const result = generate(\n ast,\n {\n sourceMaps: options.sourceMaps ?? false,\n ...(filename ? { filename } : {}),\n jsescOption: { minimal: true },\n },\n code,\n );\n\n return { code: result.code, map: result.map };\n}\n\ninterface PackageJson {\n name?: string;\n version?: string;\n optionalDevDependencies?: Record<string, string>;\n}\n\nconst checkedCorePackageJsonPaths = new Set<string>();\n\n// This compiler's own version, inlined from package.json at build time. Read via\n// an import (not by walking the filesystem at runtime) so the literal survives\n// being bundled into a host package like `@assistant-ui/metro`, where no\n// standalone `@assistant-ui/x-generative-compiler` sits on disk to walk up to.\nconst COMPILER_VERSION = pkgJson.version;\n\nfunction ensureCompilerCompatibleWithCore(\n ast: t.File,\n filename: string | undefined,\n): void {\n const corePackageJsonPath = resolveCorePackageJson(ast, filename);\n if (\n !corePackageJsonPath ||\n checkedCorePackageJsonPaths.has(corePackageJsonPath)\n ) {\n return;\n }\n\n const corePackageJson = readPackageJson(corePackageJsonPath);\n const range = corePackageJson?.optionalDevDependencies?.[COMPILER_PACKAGE];\n if (!range) {\n checkedCorePackageJsonPaths.add(corePackageJsonPath);\n return;\n }\n\n let compatible = false;\n try {\n compatible = satisfies(COMPILER_VERSION, range, {\n includePrerelease: true,\n });\n } catch {\n throw new GenerativeCompileError(\n `${CORE_PACKAGE}@${corePackageJson.version ?? \"unknown\"} declares an ` +\n `invalid optionalDevDependencies range for ${COMPILER_PACKAGE}: ` +\n JSON.stringify(range),\n filename,\n );\n }\n\n if (!compatible) {\n throw new GenerativeCompileError(\n `${CORE_PACKAGE}@${corePackageJson.version ?? \"unknown\"} requires ` +\n `${COMPILER_PACKAGE} ${range}, but the current compiler is ` +\n `${COMPILER_VERSION}. Update @assistant-ui/next, @assistant-ui/vite, ` +\n \"or @assistant-ui/metro so their compiler satisfies the core package's \" +\n \"optionalDevDependencies range.\",\n filename,\n );\n }\n\n checkedCorePackageJsonPaths.add(corePackageJsonPath);\n}\n\nfunction resolveCorePackageJson(\n ast: t.File,\n filename: string | undefined,\n): string | null {\n for (const packageName of collectImportedDistributionPackages(ast)) {\n const packageJsonPath = resolvePackageJson(packageName, filename);\n if (!packageJsonPath) continue;\n if (packageName === CORE_PACKAGE) return packageJsonPath;\n\n const corePackageJsonPath = resolvePackageJson(\n CORE_PACKAGE,\n packageJsonPath,\n );\n if (corePackageJsonPath) return corePackageJsonPath;\n }\n\n return null;\n}\n\nfunction collectImportedDistributionPackages(ast: t.File): Set<string> {\n const packages = new Set<string>();\n\n for (const statement of ast.program.body) {\n const source =\n (t.isImportDeclaration(statement) ||\n t.isExportNamedDeclaration(statement) ||\n t.isExportAllDeclaration(statement)) &&\n statement.source\n ? statement.source.value\n : null;\n if (!source) continue;\n\n const packageName = packageNameFromSpecifier(source);\n if (packageName) packages.add(packageName);\n }\n\n return packages;\n}\n\nfunction packageNameFromSpecifier(specifier: string): string | null {\n for (const packageName of DISTRIBUTION_PACKAGES) {\n if (specifier === packageName || specifier.startsWith(`${packageName}/`)) {\n return packageName;\n }\n }\n\n return null;\n}\n\nfunction resolvePackageJson(\n packageName: string,\n filename: string | undefined,\n): string | null {\n const requirePath = normalizeRequirePath(filename);\n const require = createRequire(requirePath);\n\n try {\n return findPackageJson(require.resolve(packageName), packageName);\n } catch {\n return findPackageJsonFromNodeModules(packageName, requirePath);\n }\n}\n\nfunction normalizeRequirePath(filename: string | undefined): string {\n return cleanAbsoluteFilename(filename) ?? import.meta.url;\n}\n\n/** Strips a `?query`/`#hash` suffix and returns the path only if it is absolute. */\nfunction cleanAbsoluteFilename(filename: string | undefined): string | null {\n if (!filename) return null;\n const clean = filename.split(/[?#]/, 1)[0]!;\n return nodePath.isAbsolute(clean) ? clean : null;\n}\n\nfunction findPackageJson(\n fromPathOrUrl: string,\n packageName: string,\n): string | null {\n let current = nodePath.dirname(\n fromPathOrUrl.startsWith(\"file:\")\n ? new URL(fromPathOrUrl).pathname\n : nodePath.resolve(fromPathOrUrl),\n );\n\n for (;;) {\n const packageJsonPath = nodePath.join(current, \"package.json\");\n const packageJson = readPackageJson(packageJsonPath);\n if (packageJson?.name === packageName) return packageJsonPath;\n\n const parent = nodePath.dirname(current);\n if (parent === current) return null;\n current = parent;\n }\n}\n\nfunction findPackageJsonFromNodeModules(\n packageName: string,\n fromPathOrUrl: string,\n): string | null {\n const parts = packageName.split(\"/\");\n let current = nodePath.dirname(\n fromPathOrUrl.startsWith(\"file:\")\n ? new URL(fromPathOrUrl).pathname\n : nodePath.resolve(fromPathOrUrl),\n );\n\n for (;;) {\n const packageJsonPath = nodePath.join(\n current,\n \"node_modules\",\n ...parts,\n \"package.json\",\n );\n const packageJson = readPackageJson(packageJsonPath);\n if (packageJson?.name === packageName) return packageJsonPath;\n\n const parent = nodePath.dirname(current);\n if (parent === current) return null;\n current = parent;\n }\n}\n\nfunction readPackageJson(packageJsonPath: string): PackageJson | null {\n if (!existsSync(packageJsonPath)) return null;\n try {\n return JSON.parse(readFileSync(packageJsonPath, \"utf8\")) as PackageJson;\n } catch (error) {\n throw new GenerativeCompileError(\n `could not parse package metadata at ${packageJsonPath}: ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n}\n\n/**\n * Errors unless the module's default export is the toolkit — a `defineToolkit(...)`\n * call (through `satisfies`/`as`/parens). This is the security boundary: the\n * default export is what the runtime registers, so it must be wrapped (and thus\n * split). A bare `export default { ... }` would ship a backend `execute` to the\n * client even if some *other* `defineToolkit(...)` exists elsewhere in the file.\n */\nfunction ensureDefaultExport(ast: t.File, filename: string | undefined): void {\n const def = ast.program.body.find(\n (stmt): stmt is t.ExportDefaultDeclaration =>\n t.isExportDefaultDeclaration(stmt),\n );\n if (!def) {\n throw new GenerativeCompileError(\"missing a default export\", filename);\n }\n if (!unwrapToToolkitCall(def.declaration)) {\n throw new GenerativeCompileError(\n `the default export must be ${TOOLKIT_WRAPPER}({ ... }) or ` +\n `${MCP_TOOLKIT_WRAPPER}({ ... }) (imported from \"@assistant-ui/react\"); ` +\n \"wrapping is required so a backend `execute` can't be authored in a way \" +\n \"that reaches the client\",\n filename,\n );\n }\n}\n\n/**\n * Unwraps a node through `satisfies`/`as`/parens to a call of the named function,\n * or returns `null`.\n */\nfunction unwrapToCall(node: t.Node, name: string): t.CallExpression | null {\n if (t.isTSSatisfiesExpression(node) || t.isTSAsExpression(node)) {\n return unwrapToCall(node.expression, name);\n }\n if (t.isParenthesizedExpression(node)) {\n return unwrapToCall(node.expression, name);\n }\n if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name })) {\n return node;\n }\n return null;\n}\n\n/**\n * Unwraps a statement to its variable declaration through an `export` wrapper,\n * or returns `null`.\n */\nfunction asVariableDeclaration(\n statement: t.Statement,\n): t.VariableDeclaration | null {\n if (t.isVariableDeclaration(statement)) return statement;\n if (\n t.isExportNamedDeclaration(statement) &&\n t.isVariableDeclaration(statement.declaration)\n ) {\n return statement.declaration;\n }\n return null;\n}\n\n/**\n * Collects the names bound to `new JSONGenerativeUI(...)` (e.g.\n * `const generative = new JSONGenerativeUI({ library })`). A toolkit entry that\n * calls a method on one of these is a generative tool whose halves the library\n * routes by export condition, so it passes through the toolkit pass untouched.\n */\nfunction collectGenerativeInstances(ast: t.File): Set<string> {\n const names = new Set<string>();\n const generativeFactories = collectGenerativeFactoryImports(ast);\n for (const statement of ast.program.body) {\n const variableDeclaration = asVariableDeclaration(statement);\n if (!variableDeclaration) continue;\n for (const declaration of variableDeclaration.declarations) {\n const { id, init } = declaration;\n if (\n t.isIdentifier(id) &&\n t.isNewExpression(init) &&\n t.isIdentifier(init.callee) &&\n generativeFactories.has(init.callee.name)\n ) {\n names.add(id.name);\n }\n }\n }\n return names;\n}\n\n/**\n * Wraps a pass-through generative entry (`generative.present()`) so the tool it\n * returns loses its `unstable_backendDefault` marker. The library stamps the\n * marker at runtime, out of this compiler's reach, so a backendless client\n * build strips it post-hoc instead of threading an option into the library.\n */\nfunction stripBackendDefaultExpression(expr: t.Expression): t.Expression {\n return t.callExpression(\n t.arrowFunctionExpression(\n [\n t.objectPattern([\n t.objectProperty(\n t.identifier(\"unstable_backendDefault\"),\n t.identifier(\"_backendDefault\"),\n ),\n t.restElement(t.identifier(\"tool\")),\n ]),\n ],\n t.identifier(\"tool\"),\n ),\n [expr],\n );\n}\n\nfunction collectGenerativeFactoryImports(ast: t.File): Set<string> {\n const names = new Set<string>();\n for (const statement of ast.program.body) {\n if (\n !t.isImportDeclaration(statement) ||\n statement.source.value !== GENERATIVE_UI_PACKAGE\n ) {\n continue;\n }\n\n for (const specifier of statement.specifiers) {\n if (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported, { name: GENERATIVE_FACTORY })\n ) {\n names.add(specifier.local.name);\n }\n }\n }\n return names;\n}\n\ntype ToolkitStaticNames = readonly string[] | null;\ntype ToolkitSpreadNames = Map<string, ToolkitStaticNames>;\n\ninterface ToolkitNameContext {\n importedToolkitNamesByFile: Map<string, ToolkitStaticNames | undefined>;\n resolvingImportedToolkitNames: Set<string>;\n}\n\n/**\n * The toolkit spreads a single module's pass can recognize: safe-to-spread\n * bindings plus the namespace imports that point at a generative module. The\n * namespace set is kept separate from {@link ToolkitSpreadNames} so a\n * whole-namespace `...kit` is not mistaken for a safe identifier spread — only\n * the default export crosses the build-split boundary, so a namespace spread is\n * rejected with a specific callout rather than allowed.\n */\ninterface ToolkitSpreadInfo {\n spreadNames: ToolkitSpreadNames;\n namespaceImports: Set<string>;\n}\n\n/**\n * Collects the local names `unstable_interactableTool` is imported under from a\n * distribution package, so toolkit entries calling it can be recognized\n * (and a same-named local function can't smuggle an arbitrary call through).\n */\nfunction collectInteractableToolImports(ast: t.File): Set<string> {\n const names = new Set<string>();\n for (const statement of ast.program.body) {\n if (!t.isImportDeclaration(statement)) continue;\n if (!packageNameFromSpecifier(statement.source.value)) continue;\n for (const specifier of statement.specifiers) {\n if (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported, { name: INTERACTABLE_TOOL_FACTORY })\n ) {\n names.add(specifier.local.name);\n }\n }\n }\n return names;\n}\n\nfunction createToolkitNameContext(): ToolkitNameContext {\n return {\n importedToolkitNamesByFile: new Map(),\n resolvingImportedToolkitNames: new Set(),\n };\n}\n\n/**\n * The inline config of an `unstable_interactableTool({ ... })` toolkit entry, or `null`\n * when the entry is some other expression.\n */\nfunction interactableToolConfig(\n value: t.Node,\n imports: Set<string>,\n): t.ObjectExpression | null {\n return t.isCallExpression(value) &&\n t.isIdentifier(value.callee) &&\n imports.has(value.callee.name) &&\n t.isObjectExpression(value.arguments[0])\n ? value.arguments[0]\n : null;\n}\n\n/**\n * Toolkit identifiers that are safe to spread into a `defineToolkit({ ... })`,\n * paired with the static tool names they contain. A `null` name list means the\n * spread is safe, but its names are not statically known for duplicate checks.\n *\n * Two kinds qualify:\n *\n * - A local variable whose initializer is visible to this compiler pass: a\n * `defineToolkit(...)` binding is compiled in-place before a later spread\n * reads it, and `defineMcpToolkit(...)` entries can't contain executable code.\n * - The default import of another `\"use generative\"` module: that module is\n * split per-target by its own compiler pass, so spreading its default-exported\n * toolkit can't leak a backend `execute` to the client. Only the default\n * export crosses the generative-module boundary, so named imports don't\n * qualify — they would be `undefined` once that module is build-split.\n *\n * A namespace import (`import * as kit from \"...\"`) of a generative module is\n * tracked separately in `namespaceImports` so a `...kit` / `...kit.default`\n * spread can be rejected with a specific callout: only the default export\n * crosses the boundary, so a namespace spread is never safe.\n */\nfunction collectToolkitSpreadNames(\n ast: t.File,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitSpreadInfo {\n const spreadNames: ToolkitSpreadNames = new Map();\n const namespaceImports = new Set<string>();\n const localToolkitCalls = new Map<string, t.CallExpression>();\n\n for (const statement of ast.program.body) {\n const localDeclaration = asVariableDeclaration(statement);\n if (localDeclaration) {\n for (const declaration of localDeclaration.declarations) {\n const { id, init } = declaration;\n if (t.isIdentifier(id) && init) {\n const toolkitCall = unwrapToToolkitCall(init);\n if (toolkitCall) localToolkitCalls.set(id.name, toolkitCall);\n }\n }\n continue;\n }\n\n if (t.isImportDeclaration(statement)) {\n const defaultSpecifier = statement.specifiers.find(\n (specifier): specifier is t.ImportDefaultSpecifier =>\n t.isImportDefaultSpecifier(specifier),\n );\n const namespaceSpecifier = statement.specifiers.find(\n (specifier): specifier is t.ImportNamespaceSpecifier =>\n t.isImportNamespaceSpecifier(specifier),\n );\n if (!defaultSpecifier && !namespaceSpecifier) continue;\n\n const names = getGenerativeImportToolkitNames(\n statement.source.value,\n filename,\n context,\n );\n if (names === undefined) continue;\n\n if (defaultSpecifier) spreadNames.set(defaultSpecifier.local.name, names);\n if (namespaceSpecifier)\n namespaceImports.add(namespaceSpecifier.local.name);\n }\n }\n\n const resolveLocal = (name: string): ToolkitStaticNames | undefined => {\n if (spreadNames.has(name)) return spreadNames.get(name);\n\n const call = localToolkitCalls.get(name);\n if (!call) return undefined;\n\n const object = t.isObjectExpression(call.arguments[0])\n ? call.arguments[0]\n : null;\n const names = object\n ? collectToolkitObjectNames(object, spreadNames)\n : null;\n const publicNames = uniqueToolkitNames(names);\n\n spreadNames.set(name, publicNames);\n return publicNames;\n };\n\n for (const name of localToolkitCalls.keys()) {\n resolveLocal(name);\n }\n\n return { spreadNames, namespaceImports };\n}\n\nconst MODULE_EXTENSIONS = [\n \".ts\",\n \".tsx\",\n \".mts\",\n \".cts\",\n \".js\",\n \".jsx\",\n \".mjs\",\n \".cjs\",\n] as const;\n\n/** Extensions a specifier may carry that actually map to a TS source file. */\nconst REWRITABLE_JS_EXTENSIONS = new Set([\".js\", \".jsx\", \".mjs\", \".cjs\"]);\n\n/**\n * Reads the static tool names from the default export of an imported\n * `\"use generative\"` module. Relative specifiers and `tsconfig` path aliases\n * (e.g. `@/tools`) are resolved; anything else (a bare package, an unresolvable\n * alias) is treated as non-generative, and thus an unsafe spread.\n */\nfunction getGenerativeImportToolkitNames(\n source: string,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitStaticNames | undefined {\n const cleanFilename = cleanAbsoluteFilename(filename);\n if (!cleanFilename) return undefined;\n\n const resolved = resolveImportedModuleFile(source, cleanFilename);\n if (!resolved) return undefined;\n\n if (context.importedToolkitNamesByFile.has(resolved)) {\n return context.importedToolkitNamesByFile.get(resolved);\n }\n if (context.resolvingImportedToolkitNames.has(resolved)) return null;\n\n let code: string;\n try {\n code = readFileSync(resolved, \"utf8\");\n } catch {\n context.importedToolkitNamesByFile.set(resolved, undefined);\n return undefined;\n }\n\n if (!isGenerativeModule(code)) {\n context.importedToolkitNamesByFile.set(resolved, undefined);\n return undefined;\n }\n\n context.resolvingImportedToolkitNames.add(resolved);\n try {\n const importedAst = parse(code, {\n sourceType: \"module\",\n plugins: parserPlugins,\n });\n const names = getDefaultExportToolkitNames(importedAst, resolved, context);\n context.importedToolkitNamesByFile.set(resolved, names);\n return names;\n } catch (error) {\n if (error instanceof GenerativeCompileError) throw error;\n context.importedToolkitNamesByFile.set(resolved, null);\n return null;\n } finally {\n context.resolvingImportedToolkitNames.delete(resolved);\n }\n}\n\nfunction getDefaultExportToolkitNames(\n ast: t.File,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitStaticNames {\n const def = ast.program.body.find(\n (stmt): stmt is t.ExportDefaultDeclaration =>\n t.isExportDefaultDeclaration(stmt),\n );\n if (!def) return null;\n\n const toolkitCall = unwrapToToolkitCall(def.declaration);\n if (!toolkitCall || !t.isObjectExpression(toolkitCall.arguments[0])) {\n return null;\n }\n\n const { spreadNames } = collectToolkitSpreadNames(ast, filename, context);\n return uniqueToolkitNames(\n collectToolkitObjectNames(toolkitCall.arguments[0], spreadNames),\n );\n}\n\n/** Resolves an import specifier (relative or `tsconfig`-aliased) to a file on disk. */\nfunction resolveImportedModuleFile(\n source: string,\n fromFilename: string,\n): string | null {\n if (source.startsWith(\".\")) {\n const base = nodePath.resolve(nodePath.dirname(fromFilename), source);\n return resolveModuleFileAtPath(base);\n }\n return resolveAliasImport(source, fromFilename);\n}\n\n/**\n * Resolves a candidate module path, trying TS/JS extensions then index files. A\n * specifier may carry a `.js`-family extension that maps to a `.ts`/`.tsx`\n * source (TypeScript's `bundler`/`nodenext` resolution), so the extension is\n * dropped before probing.\n */\nfunction resolveModuleFileAtPath(base: string): string | null {\n const ext = nodePath.extname(base);\n if (ext && existsSync(base)) return base;\n\n const stem = REWRITABLE_JS_EXTENSIONS.has(ext)\n ? base.slice(0, -ext.length)\n : base;\n\n for (const candidateExt of MODULE_EXTENSIONS) {\n const candidate = `${stem}${candidateExt}`;\n if (existsSync(candidate)) return candidate;\n }\n for (const candidateExt of MODULE_EXTENSIONS) {\n const candidate = nodePath.join(base, `index${candidateExt}`);\n if (existsSync(candidate)) return candidate;\n }\n return null;\n}\n\ninterface TsconfigAliases {\n /** Absolute directory that `paths` targets resolve against. */\n baseDir: string;\n paths: Record<string, string[]>;\n}\n\n/** Resolves a `tsconfig` path alias (e.g. `@/tools/x`) to a file on disk. */\nfunction resolveAliasImport(\n source: string,\n fromFilename: string,\n): string | null {\n const tsconfig = loadTsconfigAliases(nodePath.dirname(fromFilename));\n if (!tsconfig) return null;\n\n // TypeScript matches the most specific key first: an exact (wildcard-free) key\n // beats a wildcard one, and a longer static prefix beats a shorter one.\n const patterns = Object.entries(tsconfig.paths).sort(\n ([a], [b]) => aliasSpecificity(b) - aliasSpecificity(a),\n );\n\n for (const [pattern, targets] of patterns) {\n const matched = matchAliasPattern(pattern, source);\n if (matched === null) continue;\n\n for (const target of targets) {\n const specifier = substituteAliasWildcard(target, matched);\n const file = resolveModuleFileAtPath(\n nodePath.resolve(tsconfig.baseDir, specifier),\n );\n if (file) return file;\n }\n }\n return null;\n}\n\n/** Ranks a `paths` key: an exact key outranks any wildcard; longer prefixes win. */\nfunction aliasSpecificity(pattern: string): number {\n const star = pattern.indexOf(\"*\");\n return star === -1 ? pattern.length + 1 : star;\n}\n\n/** Substitutes the single `*` in a `paths` target with the matched text. */\nfunction substituteAliasWildcard(target: string, matched: string): string {\n const star = target.indexOf(\"*\");\n if (star === -1) return target;\n return target.slice(0, star) + matched + target.slice(star + 1);\n}\n\n/**\n * Matches an import specifier against a `tsconfig` `paths` key. Returns the text\n * captured by the key's `*` (or `\"\"` for an exact, wildcard-free key), or `null`\n * when the specifier doesn't match.\n */\nfunction matchAliasPattern(pattern: string, source: string): string | null {\n const star = pattern.indexOf(\"*\");\n if (star === -1) return pattern === source ? \"\" : null;\n\n const prefix = pattern.slice(0, star);\n const suffix = pattern.slice(star + 1);\n if (\n source.length >= prefix.length + suffix.length &&\n source.startsWith(prefix) &&\n source.endsWith(suffix)\n ) {\n return source.slice(prefix.length, source.length - suffix.length);\n }\n return null;\n}\n\ninterface TsconfigAliasesCacheEntry {\n aliases: TsconfigAliases | null;\n fileVersions: Map<string, string | null>;\n validatedInPass: number;\n reuseAcrossPasses: boolean;\n}\n\nconst tsconfigAliasesByDir = new Map<string, TsconfigAliasesCacheEntry>();\nlet currentCompilePass = 0;\n\ninterface TsconfigResolutionState {\n fileVersions: Map<string, string | null>;\n fileContents: Map<string, string | null>;\n cacheable: boolean;\n}\n\n// Fingerprints content rather than mtime and size: filesystems with coarse\n// timestamp granularity report an unchanged mtime for a same-length rewrite,\n// which would serve the stale aliases this cache exists to invalidate.\nfunction tsconfigFileVersion(content: string): string {\n return createHash(\"sha1\").update(content).digest(\"hex\");\n}\n\nfunction readTsconfigFile(\n state: TsconfigResolutionState,\n path: string,\n): string | null {\n const cached = state.fileContents.get(path);\n if (cached !== undefined) return cached;\n\n let content: string | null;\n try {\n content = readFileSync(path, \"utf8\");\n } catch {\n content = null;\n }\n state.fileContents.set(path, content);\n state.fileVersions.set(\n path,\n content === null ? null : tsconfigFileVersion(content),\n );\n return content;\n}\n\nfunction currentFileVersion(path: string): string | null {\n try {\n return tsconfigFileVersion(readFileSync(path, \"utf8\"));\n } catch {\n return null;\n }\n}\n\nfunction isTsconfigCacheCurrent(entry: TsconfigAliasesCacheEntry): boolean {\n for (const [path, version] of entry.fileVersions) {\n if (currentFileVersion(path) !== version) return false;\n }\n return true;\n}\n\n/** Walks up from a directory to the nearest `tsconfig.json` that declares `paths`. */\nfunction loadTsconfigAliases(fromDir: string): TsconfigAliases | null {\n const cached = tsconfigAliasesByDir.get(fromDir);\n if (cached) {\n if (\n cached.validatedInPass === currentCompilePass ||\n (cached.reuseAcrossPasses && isTsconfigCacheCurrent(cached))\n ) {\n cached.validatedInPass = currentCompilePass;\n return cached.aliases;\n }\n }\n\n let aliases: TsconfigAliases | null = null;\n const state: TsconfigResolutionState = {\n fileVersions: new Map(),\n fileContents: new Map(),\n cacheable: true,\n };\n let dir = fromDir;\n for (;;) {\n const tsconfigPath = nodePath.join(dir, \"tsconfig.json\");\n if (readTsconfigFile(state, tsconfigPath) !== null) {\n aliases = readTsconfigAliases(tsconfigPath, new Set(), state);\n if (aliases) break;\n }\n const parent = nodePath.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n\n // Cache misses too; tracked absent paths invalidate when a config appears.\n // Unresolved package configs are reused only within the current compile.\n tsconfigAliasesByDir.set(fromDir, {\n aliases,\n fileVersions: state.fileVersions,\n validatedInPass: currentCompilePass,\n reuseAcrossPasses: state.cacheable,\n });\n return aliases;\n}\n\n/** Reads `baseUrl`/`paths` from a tsconfig, following a single `extends` chain. */\nfunction readTsconfigAliases(\n tsconfigPath: string,\n seen: Set<string>,\n state: TsconfigResolutionState,\n): TsconfigAliases | null {\n if (seen.has(tsconfigPath)) return null;\n seen.add(tsconfigPath);\n\n const raw = readTsconfigFile(state, tsconfigPath);\n if (raw === null) return null;\n\n let config: {\n extends?: string | string[];\n compilerOptions?: { baseUrl?: string; paths?: Record<string, string[]> };\n } | null;\n try {\n config = parseJsonc(raw);\n } catch {\n return null;\n }\n if (!config) return null;\n\n const configDir = nodePath.dirname(tsconfigPath);\n const { baseUrl, paths } = config.compilerOptions ?? {};\n\n if (paths) {\n return {\n baseDir: baseUrl ? nodePath.resolve(configDir, baseUrl) : configDir,\n paths,\n };\n }\n\n // `extends` may be a string or (TS 5.0+) a string array; later entries take\n // precedence, so try them last-first. Parsed from untrusted JSONC, so the\n // array case is a real runtime shape, not just a type.\n const extendsList = Array.isArray(config.extends)\n ? config.extends\n : config.extends\n ? [config.extends]\n : [];\n for (let i = extendsList.length - 1; i >= 0; i--) {\n const entry = extendsList[i];\n if (typeof entry !== \"string\") continue;\n const extended = resolveExtendedTsconfig(entry, configDir, state);\n if (extended) {\n const aliases = readTsconfigAliases(extended, seen, state);\n if (aliases) return aliases;\n }\n }\n return null;\n}\n\n/** Resolves a tsconfig `extends` value (relative path or installed package). */\nfunction resolveExtendedTsconfig(\n extendsValue: string,\n configDir: string,\n state: TsconfigResolutionState,\n): string | null {\n if (extendsValue.startsWith(\".\")) {\n const base = nodePath.resolve(configDir, extendsValue);\n const candidates =\n nodePath.extname(base) === \".json\" ? [base] : [`${base}.json`, base];\n for (const candidate of candidates) {\n if (readTsconfigFile(state, candidate) !== null) {\n return candidate;\n }\n }\n return null;\n }\n\n const request = extendsValue.endsWith(\".json\")\n ? extendsValue\n : `${extendsValue}.json`;\n const requireFromConfig = createRequire(\n nodePath.join(configDir, \"package.json\"),\n );\n\n try {\n const resolved = requireFromConfig.resolve(request);\n readTsconfigFile(state, resolved);\n return resolved;\n } catch {\n state.cacheable = false;\n return null;\n }\n}\n\n/** Parses JSON with `//` and block comments and trailing commas stripped (tsconfig is JSONC). */\nfunction parseJsonc(text: string): any {\n const withoutComments = text.replace(\n /\"(?:[^\"\\\\]|\\\\.)*\"|\\/\\/[^\\n\\r]*|\\/\\*[\\s\\S]*?\\*\\//g,\n (match) => (match.startsWith('\"') ? match : \"\"),\n );\n const withoutTrailingCommas = withoutComments.replace(/,(\\s*[}\\]])/g, \"$1\");\n return JSON.parse(withoutTrailingCommas);\n}\n\nfunction unwrapToToolkitCall(node: t.Node): t.CallExpression | null {\n return (\n unwrapToCall(node, TOOLKIT_WRAPPER) ??\n unwrapToCall(node, MCP_TOOLKIT_WRAPPER)\n );\n}\n\nfunction collectToolkitObjectNames(\n object: t.ObjectExpression,\n toolkitSpreadNames: ToolkitSpreadNames,\n): ToolkitStaticNames {\n const names: string[] = [];\n\n for (const entry of object.properties) {\n const entryNames = toolkitEntryNames(entry, toolkitSpreadNames);\n if (!entryNames) return null;\n names.push(...entryNames);\n }\n\n return names;\n}\n\nfunction uniqueToolkitNames(names: ToolkitStaticNames): ToolkitStaticNames {\n return names ? [...new Set(names)] : names;\n}\n\nfunction toolkitEntryNames(\n entry: t.ObjectExpression[\"properties\"][number],\n toolkitSpreadNames: ToolkitSpreadNames,\n): ToolkitStaticNames {\n if (t.isSpreadElement(entry)) {\n if (t.isIdentifier(entry.argument)) {\n return toolkitSpreadNames.get(entry.argument.name) ?? null;\n }\n\n const directMcpToolkit = unwrapToCall(entry.argument, MCP_TOOLKIT_WRAPPER);\n if (\n directMcpToolkit &&\n t.isObjectExpression(directMcpToolkit.arguments[0])\n ) {\n return collectToolkitObjectNames(\n directMcpToolkit.arguments[0],\n toolkitSpreadNames,\n );\n }\n\n return null;\n }\n\n if (t.isObjectProperty(entry) || t.isObjectMethod(entry)) {\n const name = memberName(entry.key, entry.computed);\n return name ? [name] : [];\n }\n\n return [];\n}\n\nfunction toolkitEntrySourceLabel(entry: Entry): string {\n if (t.isSpreadElement(entry)) {\n if (t.isIdentifier(entry.argument)) return `...${entry.argument.name}`;\n if (unwrapToCall(entry.argument, MCP_TOOLKIT_WRAPPER)) {\n return `...${MCP_TOOLKIT_WRAPPER}(...)`;\n }\n return \"...\";\n }\n\n if (t.isObjectProperty(entry) || t.isObjectMethod(entry)) {\n const line = entry.loc?.start.line;\n return line === undefined\n ? \"inline property\"\n : `inline property (line ${line})`;\n }\n\n return \"inline property\";\n}\n\nfunction warnDuplicateToolkitNames(\n object: t.ObjectExpression,\n toolkitSpreadNames: ToolkitSpreadNames,\n filename: string | undefined,\n): void {\n const sourcesByToolName = new Map<string, string[]>();\n for (const entry of object.properties) {\n const entryNames = toolkitEntryNames(entry, toolkitSpreadNames);\n if (!entryNames) continue;\n const source = toolkitEntrySourceLabel(entry);\n for (const name of entryNames) {\n const sources = sourcesByToolName.get(name);\n if (sources) sources.push(source);\n else sourcesByToolName.set(name, [source]);\n }\n }\n\n for (const [name, sources] of sourcesByToolName) {\n const winner = sources.at(-1);\n const overridden = sources.slice(0, -1);\n if (winner === undefined || overridden.length === 0) continue;\n console.warn(\n new GenerativeCompileError(\n `Duplicate tool name \"${name}\": ${winner} overrides ` +\n `${overridden.join(\", \")}. ` +\n \"JavaScript object spread keeps the last definition.\",\n filename,\n ).message,\n );\n }\n}\n\nfunction isSafeToolkitSpread(\n entry: t.SpreadElement,\n toolkitSpreadNames: ToolkitSpreadNames,\n): boolean {\n if (t.isIdentifier(entry.argument)) {\n return toolkitSpreadNames.has(entry.argument.name);\n }\n\n const directMcpToolkit = unwrapToCall(entry.argument, MCP_TOOLKIT_WRAPPER);\n return (\n !!directMcpToolkit && t.isObjectExpression(directMcpToolkit.arguments[0])\n );\n}\n\nfunction genericUnsafeToolkitEntryMessage(): string {\n return (\n \"each tool must be an inline object literal (`name: { ... }`) or a \" +\n \"compiler-visible toolkit spread / generative tool (e.g. \" +\n \"`...defineMcpToolkit(...)`, `...baseToolkit`, \" +\n \"`generative.present()`, or `unstable_interactableTool(...)`) so its \" +\n \"`execute` can be routed\"\n );\n}\n\nfunction describeUnsafeToolkitEntry(entry: Entry): string {\n if (!t.isObjectProperty(entry)) return genericUnsafeToolkitEntryMessage();\n\n const toolName = memberName(entry.key, entry.computed);\n const raw = entryRawValue(entry);\n if (!toolName || !raw) return genericUnsafeToolkitEntryMessage();\n\n return (\n `tool \"${toolName}\" cannot be \\`${generate(raw).code}\\`; use an inline ` +\n `object literal (\\`${toolName}: { ... }\\`) or a compiler-visible toolkit ` +\n \"spread / generative tool so its `execute` can be routed\"\n );\n}\n\nfunction namespaceToolkitSpreadMessage(): string {\n return (\n 'a namespace import (`import * as kit from \"...\"`) can\\'t be spread into ' +\n `${TOOLKIT_WRAPPER}({ ... }); only a generative module's default export ` +\n \"crosses the build-split boundary, so import it as a default \" +\n '(`import kit from \"...\"`) and spread `...kit` instead'\n );\n}\n\n/** The `kit` in `...kit` or `...kit.default`, or null for a computed/opaque base. */\nfunction namespaceSpreadBaseName(argument: t.Expression): string | null {\n if (t.isIdentifier(argument)) return argument.name;\n if (\n t.isMemberExpression(argument) &&\n !argument.computed &&\n t.isIdentifier(argument.object)\n ) {\n return argument.object.name;\n }\n return null;\n}\n\n/** The `JSONGenerativeUI` methods that produce a split-by-condition tool. */\nconst GENERATIVE_TOOL_METHODS = new Set([\"present\", \"promptUser\"]);\n\n/**\n * Whether a toolkit entry's value is a call to a tool-producing method on a\n * collected `JSONGenerativeUI` instance (`generative.present()`), which passes\n * through. The method name is checked too, so a typo like `generative.presnt()`\n * is a compile error here rather than a pass-through that fails at runtime.\n */\nfunction isGenerativeToolEntry(value: t.Node, instances: Set<string>): boolean {\n return (\n t.isCallExpression(value) &&\n t.isMemberExpression(value.callee) &&\n !value.callee.computed &&\n t.isIdentifier(value.callee.object) &&\n instances.has(value.callee.object.name) &&\n t.isIdentifier(value.callee.property) &&\n GENERATIVE_TOOL_METHODS.has(value.callee.property.name)\n );\n}\n\n/**\n * Splits a `defineGenerativeComponents({ ... })` library for a build target:\n * a component's `render` (and the client imports it alone uses) is dropped on\n * the server; `properties`/`description` stay on both, since they drive the tool\n * schema either way. Mutates the object in place.\n */\nfunction compileComponents(\n object: t.ObjectExpression,\n target: Target,\n flags: TargetFlags,\n filename: string | undefined,\n): void {\n for (const entry of object.properties) {\n const value = entryValue(entry);\n if (!value) {\n throw new GenerativeCompileError(\n `each component in ${COMPONENTS_WRAPPER}() must be an inline object ` +\n \"literal (`name: { ... }`) so its `render` can be routed\",\n filename,\n );\n }\n if (!findMember(value, \"render\")) continue;\n // The client keeps `render` (and so needs the module marked `\"use client\"`);\n // the server drops it, since only the schema reaches the model there.\n if (target === \"client\") flags.keptRender = true;\n else removeMember(value, \"render\");\n }\n}\n\n/**\n * Splits a `defineToolkit({ ... })` for a build target. Each inline tool is\n * routed by inferred type (see the per-entry logic); a generative entry like\n * `generative.present()` passes through, the library having already split it.\n * Mutates the object in place and records outcomes in {@link TargetFlags}.\n */\nfunction compileToolkit(\n object: t.ObjectExpression,\n target: Target,\n instances: Set<string>,\n interactableToolImports: Set<string>,\n toolkitSpreadNames: ToolkitSpreadNames,\n namespaceImports: Set<string>,\n flags: TargetFlags,\n backendless: boolean,\n filename: string | undefined,\n): void {\n // Split builds compile both targets; emit target-independent warnings from\n // the client pass so each duplicate is logged once.\n if (target === \"client\") {\n warnDuplicateToolkitNames(object, toolkitSpreadNames, filename);\n }\n\n const nextProperties: t.ObjectExpression[\"properties\"] = [];\n\n for (const entry of object.properties) {\n const value = entryValue(entry);\n if (!value) {\n if (\n t.isSpreadElement(entry) &&\n isSafeToolkitSpread(entry, toolkitSpreadNames)\n ) {\n nextProperties.push(entry);\n continue;\n }\n // A generative tool (`generative.present()`) is split by the library's\n // export conditions, so it is safe to keep verbatim. Anything else — a\n // method or opaque call like `makeTool()` — can't be analyzed, so its\n // `execute` could reach the client unstripped.\n const raw = entryRawValue(entry);\n if (raw && isGenerativeToolEntry(raw, instances)) {\n if (backendless && target === \"client\" && t.isObjectProperty(entry)) {\n entry.value = stripBackendDefaultExpression(raw);\n }\n nextProperties.push(entry);\n continue;\n }\n const config =\n raw && interactableToolConfig(raw, interactableToolImports);\n if (config) {\n if (target === \"client\") flags.keptRender = true;\n else removeMember(config, \"render\");\n nextProperties.push(entry);\n continue;\n }\n if (t.isSpreadElement(entry)) {\n const namespaceBase = namespaceSpreadBaseName(entry.argument);\n if (namespaceBase && namespaceImports.has(namespaceBase)) {\n throw new GenerativeCompileError(\n namespaceToolkitSpreadMessage(),\n filename,\n );\n }\n }\n throw new GenerativeCompileError(\n describeUnsafeToolkitEntry(entry),\n filename,\n );\n }\n\n // Nature is inferred from `execute` (see inferToolType), not an authored\n // `type`. The resolved type is written back below so the runtime keeps it.\n const toolName = t.isObjectProperty(entry)\n ? memberName(entry.key, entry.computed)\n : undefined;\n const execute = findMember(value, \"execute\");\n const isStub = execute ? executeIsStubTool(execute) : false;\n const isExternal = execute ? executeIsExternalTool(execute) : false;\n const type = inferToolType(value, toolName, filename);\n const hasRender = !!findMember(value, \"render\");\n const hasRenderText = !!findMember(value, \"renderText\");\n\n if (type === \"frontend\" && !hasRender && !hasRenderText) {\n throw new GenerativeCompileError(\n `${typedToolSubject(\"frontend\", toolName)} must declare a ` +\n \"`render` or `renderText` \" +\n \"(it has no server execute to show otherwise)\",\n filename,\n );\n }\n\n if (type === \"human\" && !hasRender) {\n throw new GenerativeCompileError(\n `${typedToolSubject(\"human\", toolName)} must declare a ` +\n \"`render` so it can collect input\",\n filename,\n );\n }\n\n if (type === \"provider\" && execute) {\n applyProviderToolConfig(value, execute, toolName, filename);\n }\n\n if (isExternal) {\n if (!hasRender && !hasRenderText) {\n throw new GenerativeCompileError(\n `${typedToolSubject(\"external\", toolName)} must declare a ` +\n \"`render` or `renderText` \" +\n \"(assistant-ui only renders calls for tools defined elsewhere)\",\n filename,\n );\n }\n if (target === \"server\") continue;\n stripExternalToolMetadata(value);\n }\n\n if (target === \"client\") {\n // A non-stub frontend execute stays (its `\"use client\"` marker is no longer needed\n // once the module is client); backend, sentinel, and stub executes are dropped.\n if (execute && type === \"frontend\" && !isStub) stripUseClient(execute);\n else if (execute) removeMember(value, \"execute\");\n if (hasRender || hasRenderText) flags.keptRender = true;\n } else {\n // server: render is never needed; only a non-external backend execute survives.\n if (hasRender) removeMember(value, \"render\");\n if (hasRenderText) removeMember(value, \"renderText\");\n if (execute) {\n if (type === \"backend\" && !isExternal) flags.keptBackendExecute = true;\n else removeMember(value, \"execute\");\n }\n }\n\n setToolType(value, type);\n setBackendDefault(value, target, type, backendless);\n nextProperties.push(entry);\n }\n\n object.properties = nextProperties;\n}\n\nfunction applyProviderToolConfig(\n object: t.ObjectExpression,\n execute: t.ObjectProperty | t.ObjectMethod,\n toolName: string | undefined,\n filename: string | undefined,\n): void {\n if (\n !t.isObjectProperty(execute) ||\n !t.isCallExpression(execute.value) ||\n execute.value.arguments.length !== 1 ||\n !t.isObjectExpression(execute.value.arguments[0])\n ) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` must receive an inline object literal\",\n filename,\n );\n }\n\n const existingNames = new Set(\n object.properties.flatMap((prop) => {\n if (!t.isObjectProperty(prop) && !t.isObjectMethod(prop)) return [];\n const name = memberName(prop.key, prop.computed);\n return name ? [name] : [];\n }),\n );\n const configNames = new Set<string>();\n\n for (const prop of execute.value.arguments[0].properties) {\n if (!t.isObjectProperty(prop)) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` config can only contain object properties\",\n filename,\n );\n }\n const name = memberName(prop.key, prop.computed);\n if (!name) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` config can only contain static property names\",\n filename,\n );\n }\n if (\n t.isFunctionExpression(prop.value) ||\n t.isArrowFunctionExpression(prop.value)\n ) {\n throw new GenerativeCompileError(\n \"`providerTool(...)` config cannot contain function-valued properties\",\n filename,\n );\n }\n if (existingNames.has(name) || configNames.has(name)) {\n const toolLabel = toolName ? ` for \"${toolName}\"` : \"\";\n throw new GenerativeCompileError(\n `\\`providerTool(...)\\` config${toolLabel} duplicates \"${name}\"`,\n filename,\n );\n }\n configNames.add(name);\n object.properties.push(prop);\n }\n}\n\ntype Entry = t.ObjectExpression[\"properties\"][number];\n\n/** The raw AST value of an entry (any expression), or null for spreads/methods. */\nfunction entryRawValue(entry: Entry): t.Expression | null {\n if (t.isObjectProperty(entry) && t.isExpression(entry.value)) {\n return entry.value;\n }\n return null;\n}\n\nfunction entryValue(entry: Entry): t.ObjectExpression | null {\n if (t.isObjectProperty(entry) && t.isObjectExpression(entry.value)) {\n return entry.value;\n }\n return null;\n}\n\n/** A member of an entry object: `render`/`execute`/`type`, as property or method. */\nfunction findMember(\n object: t.ObjectExpression,\n name: string,\n): t.ObjectProperty | t.ObjectMethod | undefined {\n return object.properties.find(\n (p): p is t.ObjectProperty | t.ObjectMethod =>\n (t.isObjectProperty(p) || t.isObjectMethod(p)) &&\n memberName(p.key, p.computed) === name,\n );\n}\n\nfunction removeMember(object: t.ObjectExpression, name: string): void {\n object.properties = object.properties.filter(\n (p) =>\n !(\n (t.isObjectProperty(p) || t.isObjectMethod(p)) &&\n memberName(p.key, p.computed) === name\n ),\n );\n}\n\n/** The `BlockStatement` body of an `execute` member, if it has one. */\nfunction executeBody(\n member: t.ObjectProperty | t.ObjectMethod,\n): t.BlockStatement | undefined {\n if (t.isObjectMethod(member)) return member.body;\n const value = member.value;\n if (\n (t.isArrowFunctionExpression(value) || t.isFunctionExpression(value)) &&\n t.isBlockStatement(value.body)\n ) {\n return value.body;\n }\n return undefined;\n}\n\n/** Whether an `execute` opts into the client via a leading `\"use client\"`. */\nfunction executeIsClient(member: t.ObjectProperty | t.ObjectMethod): boolean {\n return !!executeBody(member)?.directives.some(\n (d) => d.value.value === \"use client\",\n );\n}\n\nfunction executeIsSentinel(\n member: t.ObjectProperty | t.ObjectMethod,\n name: string,\n): boolean {\n return (\n t.isObjectProperty(member) &&\n t.isCallExpression(member.value) &&\n t.isIdentifier(member.value.callee, { name })\n );\n}\n\n/** Whether an `execute` is the human-in-the-loop sentinel. */\nfunction executeIsHumanTool(\n member: t.ObjectProperty | t.ObjectMethod,\n): boolean {\n return (\n executeIsSentinel(member, \"humanTool\") ||\n executeIsSentinel(member, \"hitlTool\") ||\n executeIsSentinel(member, \"hitl\")\n );\n}\n\n/** Whether an `execute` is the provider-tool sentinel. */\nfunction executeIsProviderTool(\n member: t.ObjectProperty | t.ObjectMethod,\n): boolean {\n return executeIsSentinel(member, \"providerTool\");\n}\n\n/** Whether an `execute` is the local override sentinel. */\nfunction executeIsStubTool(member: t.ObjectProperty | t.ObjectMethod): boolean {\n return executeIsSentinel(member, \"stubTool\");\n}\n\n/** Whether an `execute` is the externally-defined backend tool sentinel. */\nfunction executeIsExternalTool(\n member: t.ObjectProperty | t.ObjectMethod,\n): boolean {\n return executeIsSentinel(member, \"externalTool\");\n}\n\n/** Drops the `\"use client\"` directive from an `execute` body (kept frontend). */\nfunction stripUseClient(member: t.ObjectProperty | t.ObjectMethod): void {\n const body = executeBody(member);\n if (body) {\n body.directives = body.directives.filter(\n (d) => d.value.value !== \"use client\",\n );\n }\n}\n\n/**\n * The tool's nature, inferred from its (mandatory) `execute` rather than an\n * authored `type`: `humanTool()` → `human`; `providerTool(...)` → `provider`;\n * `stubTool()` → `frontend`; `externalTool()` → `backend`; `\"use client\"` →\n * `frontend`; otherwise `backend`.\n * The loader writes the result back as a `type` field (see {@link setToolType})\n * so the runtime keeps it.\n */\nfunction inferToolType(\n object: t.ObjectExpression,\n toolName: string | undefined,\n filename: string | undefined,\n): ToolType {\n const execute = findMember(object, \"execute\");\n if (!execute) {\n throw new GenerativeCompileError(\n `${toolSubject(toolName)} must declare an \\`execute\\`; use ` +\n \"`humanTool()` for a human-in-the-loop tool\",\n filename,\n );\n }\n if (executeIsHumanTool(execute)) return \"human\";\n if (executeIsProviderTool(execute)) return \"provider\";\n if (executeIsStubTool(execute)) return \"frontend\";\n if (executeIsExternalTool(execute)) return \"backend\";\n return executeIsClient(execute) ? \"frontend\" : \"backend\";\n}\n\nfunction toolSubject(toolName: string | undefined): string {\n return toolName ? `tool \"${toolName}\"` : \"every tool\";\n}\n\nfunction typedToolSubject(type: string, toolName: string | undefined): string {\n if (toolName) return `${type} tool \"${toolName}\"`;\n const article = /^[aeiou]/i.test(type) ? \"an\" : \"a\";\n return `${article} ${type} tool`;\n}\n\nfunction stripExternalToolMetadata(object: t.ObjectExpression): void {\n // Mirror BackendTool's forbidden metadata fields: execute is stripped by the\n // main routing loop, while streamCall is also removed because there is no\n // assistant-ui executor to stream from.\n removeMember(object, \"description\");\n removeMember(object, \"parameters\");\n removeMember(object, \"disabled\");\n removeMember(object, \"toModelOutput\");\n removeMember(object, \"experimental_onSchemaValidationError\");\n removeMember(object, \"providerOptions\");\n removeMember(object, \"streamCall\");\n}\n\n/** Writes the resolved `type` back onto the tool object (replacing any author's). */\nfunction setToolType(object: t.ObjectExpression, type: ToolType): void {\n removeMember(object, \"type\");\n // Append (not prepend) so the inferred type wins over any earlier spread.\n object.properties.push(\n t.objectProperty(t.identifier(\"type\"), t.stringLiteral(type)),\n );\n}\n\nfunction setBackendDefault(\n object: t.ObjectExpression,\n target: Target,\n type: ToolType,\n backendless: boolean,\n): void {\n // Always strip any hand-authored marker first; only re-add it for client\n // frontend/human tools whose schema is already known by the backend. A\n // backendless build has no such backend, so nothing is marked.\n removeMember(object, \"unstable_backendDefault\");\n if (backendless) return;\n if (target !== \"client\" || (type !== \"frontend\" && type !== \"human\")) return;\n\n object.properties.push(\n t.objectProperty(\n t.identifier(\"unstable_backendDefault\"),\n t.objectExpression([\n t.objectProperty(t.identifier(\"parameters\"), t.booleanLiteral(true)),\n ]),\n ),\n );\n}\n\nfunction memberName(\n key: t.Node,\n computed: boolean | undefined,\n): string | undefined {\n if (computed) return undefined;\n if (t.isIdentifier(key)) return key.name;\n if (t.isStringLiteral(key)) return key.value;\n return undefined;\n}\n\n/**\n * Removes declarations and import specifiers left unreferenced after region\n * removal, to a fixpoint (a dropped helper frees what it used). Keeps exports,\n * side-effect imports, and possibly-side-effectful initializers.\n */\nfunction pruneUnused(ast: t.File): void {\n const hadSpecifiers = new WeakSet<t.ImportDeclaration>();\n for (const stmt of ast.program.body) {\n if (t.isImportDeclaration(stmt) && stmt.specifiers.length > 0) {\n hadSpecifiers.add(stmt);\n }\n }\n\n let removedSomething = true;\n while (removedSomething) {\n removedSomething = false;\n traverse(ast, {\n Program(path: NodePath<t.Program>) {\n path.scope.crawl();\n const isUnused = (name: string): boolean => {\n const binding = path.scope.getBinding(name);\n return !!binding && !binding.referenced;\n };\n\n path.node.body = path.node.body.filter((stmt) => {\n if (\n (t.isFunctionDeclaration(stmt) || t.isClassDeclaration(stmt)) &&\n stmt.id &&\n isUnused(stmt.id.name)\n ) {\n removedSomething = true;\n return false;\n }\n if (t.isVariableDeclaration(stmt)) {\n stmt.declarations = stmt.declarations.filter((decl) => {\n // Handles destructuring too: drop the declarator only when *every*\n // bound name is unused (else a `const { a } = server` survives and\n // keeps a server import in the client graph). Restricted to plain\n // patterns so a default/computed-key side effect isn't dropped.\n const names = Object.keys(t.getBindingIdentifiers(decl.id));\n if (\n names.length > 0 &&\n isPlainPattern(decl.id) &&\n isRemovableInit(decl.init) &&\n names.every(isUnused)\n ) {\n removedSomething = true;\n return false;\n }\n return true;\n });\n if (stmt.declarations.length === 0) return false;\n }\n return true;\n });\n path.stop();\n },\n });\n }\n\n traverse(ast, {\n Program(path: NodePath<t.Program>) {\n path.scope.crawl();\n for (const stmt of path.node.body) {\n if (!t.isImportDeclaration(stmt)) continue;\n stmt.specifiers = stmt.specifiers.filter((spec) => {\n const binding = path.scope.getBinding(spec.local.name);\n return binding ? binding.referenced : true;\n });\n }\n path.node.body = path.node.body.filter(\n (stmt) =>\n !(\n t.isImportDeclaration(stmt) &&\n stmt.specifiers.length === 0 &&\n hadSpecifiers.has(stmt)\n ),\n );\n path.stop();\n },\n });\n}\n\n/**\n * Whether a binding pattern is side-effect-free to drop: a plain identifier, or\n * an object/array pattern of plain bindings — no default values\n * (`{ a = expr }`) or computed keys (`{ [expr]: a }`), which would evaluate (and\n * thus could have side effects) at destructuring time.\n */\nfunction isPlainPattern(node: t.Node): boolean {\n if (t.isIdentifier(node)) return true;\n if (t.isObjectPattern(node)) {\n return node.properties.every((p) =>\n t.isRestElement(p)\n ? isPlainPattern(p.argument)\n : !p.computed && isPlainPattern(p.value),\n );\n }\n if (t.isArrayPattern(node)) {\n return node.elements.every((el) => el == null || isPlainPattern(el));\n }\n return false; // AssignmentPattern (default), member expr, etc.\n}\n\n/** Whether a variable initializer is safe to drop (no observable side effects). */\nfunction isRemovableInit(node: t.Expression | null | undefined): boolean {\n if (node == null) return true;\n if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node)) {\n return isRemovableInit(node.expression);\n }\n // Containers are removable only if every element is — a nested call (e.g.\n // `[track()]`) has an observable side effect that dropping would lose.\n if (t.isArrayExpression(node)) {\n return node.elements.every(\n (el) => el == null || (t.isExpression(el) && isRemovableInit(el)),\n );\n }\n if (t.isObjectExpression(node)) {\n return node.properties.every(\n (p) =>\n t.isObjectProperty(p) &&\n !p.computed &&\n t.isExpression(p.value) &&\n isRemovableInit(p.value),\n );\n }\n if (t.isTemplateLiteral(node)) {\n return node.expressions.every(\n (e) => t.isExpression(e) && isRemovableInit(e),\n );\n }\n return (\n t.isArrowFunctionExpression(node) ||\n t.isFunctionExpression(node) ||\n t.isClassExpression(node) ||\n t.isIdentifier(node) ||\n // non-computed only — `obj[fn()]` could hide a side-effectful key\n (t.isMemberExpression(node) && !node.computed) ||\n t.isJSXElement(node) ||\n t.isJSXFragment(node) ||\n t.isLiteral(node)\n );\n}\n"],"mappings":";;;;;;;;;;;;AAYA,MAAM,gBAAgB;CACpB;CACA;CACA;AACF;AAIA,MAAM,WACJ,OAAO,cAAc,aAAa,YAAa,UAAkB;AAEnE,MAAM,WACJ,OAAO,cAAc,aAAa,YAAa,UAAkB;;AAMnE,MAAM,kBAAkB;;AAExB,MAAM,sBAAsB;;AAE5B,MAAM,qBAAqB;;AAE3B,MAAM,eAAe;;AAErB,MAAM,mBAAmB;;AAEzB,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;AACF;;AAEA,MAAM,wBAAwB;;;;;;AAM9B,MAAM,qBAAqB;;;;;;;AAO3B,MAAM,4BAA4B;;AA2ClC,IAAa,yBAAb,cAA4C,MAAM;CAChD,YAAY,SAAiB,UAAmB;EAC9C,MACE,gCAAgC,WAAW,IAAI,SAAS,KAAK,GAAG,GAAG,SACrE;EACA,KAAK,OAAO;CACd;AACF;;AAGA,SAAgB,mBAAmB,MAAuB;CAIxD,IAAI,IAAI,KAAK,WAAW,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AE,mBAAmB,IAAI,KAAK,CAAC,KAAK,YACrC,EAAE,aAAa,IAAI,KACnB,EAAE,cAAc,IAAI,KACpB,EAAE,UAAU,IAAI;AAEpB"}
|
package/dist/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@assistant-ui/x-generative-compiler",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.17",
|
|
5
5
|
"description": "Internal: the framework-agnostic \"use generative\" compiler that splits a colocated tool (schema + server-only execute + client-only render) into per-target builds. Shared by @assistant-ui/next and @assistant-ui/vite.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"assistant-ui",
|
|
@@ -36,10 +36,10 @@
|
|
|
36
36
|
"semver": "^7.8.5"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@assistant-ui/x-buildutils": "0.0.
|
|
39
|
+
"@assistant-ui/x-buildutils": "0.0.26",
|
|
40
40
|
"@types/babel__generator": "^7.27.0",
|
|
41
41
|
"@types/babel__traverse": "^7.28.0",
|
|
42
|
-
"@types/node": "^26.
|
|
42
|
+
"@types/node": "^26.4.1",
|
|
43
43
|
"@types/semver": "^7.8.0",
|
|
44
44
|
"vitest": "^4.1.11"
|
|
45
45
|
},
|
package/src/compile.test.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
mkdirSync,
|
|
3
|
+
mkdtempSync,
|
|
4
|
+
rmSync,
|
|
5
|
+
utimesSync,
|
|
6
|
+
writeFileSync,
|
|
7
|
+
} from "node:fs";
|
|
2
8
|
import { tmpdir } from "node:os";
|
|
3
9
|
import * as nodePath from "node:path";
|
|
4
10
|
import { describe, it, expect, vi } from "vitest";
|
|
@@ -131,6 +137,38 @@ export default defineToolkit({
|
|
|
131
137
|
});
|
|
132
138
|
`;
|
|
133
139
|
|
|
140
|
+
const nonGenerativeChild =
|
|
141
|
+
"export default { get_weather: { execute: async () => 1 } };\n";
|
|
142
|
+
|
|
143
|
+
const aliasedToolkitSource = `"use generative";
|
|
144
|
+
import { defineToolkit } from "@assistant-ui/react";
|
|
145
|
+
import weatherTools from "@/tools/weather";
|
|
146
|
+
export default defineToolkit({
|
|
147
|
+
...weatherTools,
|
|
148
|
+
});`;
|
|
149
|
+
|
|
150
|
+
function writeAliasTarget(
|
|
151
|
+
root: string,
|
|
152
|
+
directory: string,
|
|
153
|
+
childSource: string,
|
|
154
|
+
): void {
|
|
155
|
+
const target = nodePath.join(root, directory);
|
|
156
|
+
mkdirSync(target, { recursive: true });
|
|
157
|
+
writeFileSync(nodePath.join(target, "weather.tsx"), childSource);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function writeAliasConfig(path: string, target: string, baseUrl = "."): void {
|
|
161
|
+
writeFileSync(
|
|
162
|
+
path,
|
|
163
|
+
JSON.stringify({
|
|
164
|
+
compilerOptions: {
|
|
165
|
+
baseUrl,
|
|
166
|
+
paths: { "@/tools/*": [target] },
|
|
167
|
+
},
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
134
172
|
const source = `"use generative";
|
|
135
173
|
import { z } from "zod";
|
|
136
174
|
import { db } from "@/db";
|
|
@@ -1300,6 +1338,151 @@ export default defineToolkit({
|
|
|
1300
1338
|
expect(client).toContain('from "@/tools/weather"');
|
|
1301
1339
|
});
|
|
1302
1340
|
|
|
1341
|
+
it("refreshes cached path aliases after tsconfig changes", () => {
|
|
1342
|
+
const appRoot = mkdtempSync(
|
|
1343
|
+
nodePath.join(tmpdir(), "aui-generative-alias-refresh-"),
|
|
1344
|
+
);
|
|
1345
|
+
writeAliasTarget(appRoot, "generated", generativeChild);
|
|
1346
|
+
writeAliasTarget(appRoot, "fallbacks", nonGenerativeChild);
|
|
1347
|
+
const tsconfigPath = nodePath.join(appRoot, "tsconfig.json");
|
|
1348
|
+
writeAliasConfig(tsconfigPath, "./generated/*");
|
|
1349
|
+
const filename = nodePath.join(appRoot, "src", "toolkit.tsx");
|
|
1350
|
+
|
|
1351
|
+
expect(() =>
|
|
1352
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1353
|
+
).not.toThrow();
|
|
1354
|
+
|
|
1355
|
+
writeAliasConfig(tsconfigPath, "./fallbacks/*");
|
|
1356
|
+
expect(() =>
|
|
1357
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1358
|
+
).toThrow(/compiler-visible toolkit spread/);
|
|
1359
|
+
});
|
|
1360
|
+
|
|
1361
|
+
it("refreshes cached aliases when a missing extended config is created", () => {
|
|
1362
|
+
const appRoot = mkdtempSync(
|
|
1363
|
+
nodePath.join(tmpdir(), "aui-generative-extends-refresh-"),
|
|
1364
|
+
);
|
|
1365
|
+
const packageRoot = nodePath.join(appRoot, "package");
|
|
1366
|
+
writeAliasTarget(appRoot, "generated", generativeChild);
|
|
1367
|
+
writeAliasTarget(packageRoot, "fallbacks", nonGenerativeChild);
|
|
1368
|
+
mkdirSync(nodePath.join(packageRoot, "src"), { recursive: true });
|
|
1369
|
+
writeAliasConfig(nodePath.join(appRoot, "tsconfig.json"), "./generated/*");
|
|
1370
|
+
writeFileSync(
|
|
1371
|
+
nodePath.join(packageRoot, "tsconfig.json"),
|
|
1372
|
+
JSON.stringify({ extends: "./tsconfig.paths.json" }),
|
|
1373
|
+
);
|
|
1374
|
+
const filename = nodePath.join(packageRoot, "src", "toolkit.tsx");
|
|
1375
|
+
|
|
1376
|
+
expect(() =>
|
|
1377
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1378
|
+
).not.toThrow();
|
|
1379
|
+
|
|
1380
|
+
writeAliasConfig(
|
|
1381
|
+
nodePath.join(packageRoot, "tsconfig.paths.json"),
|
|
1382
|
+
"./fallbacks/*",
|
|
1383
|
+
);
|
|
1384
|
+
|
|
1385
|
+
expect(() =>
|
|
1386
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1387
|
+
).toThrow(/compiler-visible toolkit spread/);
|
|
1388
|
+
});
|
|
1389
|
+
|
|
1390
|
+
it("refreshes cached alias misses when a tsconfig is created", () => {
|
|
1391
|
+
const appRoot = mkdtempSync(
|
|
1392
|
+
nodePath.join(tmpdir(), "aui-generative-config-create-"),
|
|
1393
|
+
);
|
|
1394
|
+
writeAliasTarget(appRoot, "generated", generativeChild);
|
|
1395
|
+
mkdirSync(nodePath.join(appRoot, "src"), { recursive: true });
|
|
1396
|
+
const filename = nodePath.join(appRoot, "src", "toolkit.tsx");
|
|
1397
|
+
|
|
1398
|
+
expect(() =>
|
|
1399
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1400
|
+
).toThrow(/compiler-visible toolkit spread/);
|
|
1401
|
+
|
|
1402
|
+
writeAliasConfig(nodePath.join(appRoot, "tsconfig.json"), "./generated/*");
|
|
1403
|
+
|
|
1404
|
+
expect(() =>
|
|
1405
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1406
|
+
).not.toThrow();
|
|
1407
|
+
});
|
|
1408
|
+
|
|
1409
|
+
it("retries unresolved package configs on the next compile", () => {
|
|
1410
|
+
const appRoot = mkdtempSync(
|
|
1411
|
+
nodePath.join(tmpdir(), "aui-generative-package-config-"),
|
|
1412
|
+
);
|
|
1413
|
+
writeAliasTarget(appRoot, "generated", generativeChild);
|
|
1414
|
+
mkdirSync(nodePath.join(appRoot, "src"), { recursive: true });
|
|
1415
|
+
writeFileSync(
|
|
1416
|
+
nodePath.join(appRoot, "tsconfig.json"),
|
|
1417
|
+
JSON.stringify({ extends: "config/base" }),
|
|
1418
|
+
);
|
|
1419
|
+
const filename = nodePath.join(appRoot, "src", "toolkit.tsx");
|
|
1420
|
+
|
|
1421
|
+
expect(() =>
|
|
1422
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1423
|
+
).toThrow(/compiler-visible toolkit spread/);
|
|
1424
|
+
|
|
1425
|
+
const configPackage = nodePath.join(appRoot, "node_modules", "config");
|
|
1426
|
+
mkdirSync(configPackage, { recursive: true });
|
|
1427
|
+
writeAliasConfig(
|
|
1428
|
+
nodePath.join(configPackage, "base.json"),
|
|
1429
|
+
"./generated/*",
|
|
1430
|
+
nodePath.relative(configPackage, appRoot),
|
|
1431
|
+
);
|
|
1432
|
+
|
|
1433
|
+
expect(() =>
|
|
1434
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1435
|
+
).not.toThrow();
|
|
1436
|
+
});
|
|
1437
|
+
|
|
1438
|
+
it("refreshes cached aliases when a tsconfig is deleted", () => {
|
|
1439
|
+
const appRoot = mkdtempSync(
|
|
1440
|
+
nodePath.join(tmpdir(), "aui-generative-config-delete-"),
|
|
1441
|
+
);
|
|
1442
|
+
writeAliasTarget(appRoot, "generated", generativeChild);
|
|
1443
|
+
mkdirSync(nodePath.join(appRoot, "src"), { recursive: true });
|
|
1444
|
+
const tsconfigPath = nodePath.join(appRoot, "tsconfig.json");
|
|
1445
|
+
writeAliasConfig(tsconfigPath, "./generated/*");
|
|
1446
|
+
const filename = nodePath.join(appRoot, "src", "toolkit.tsx");
|
|
1447
|
+
|
|
1448
|
+
expect(() =>
|
|
1449
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1450
|
+
).not.toThrow();
|
|
1451
|
+
|
|
1452
|
+
rmSync(tsconfigPath);
|
|
1453
|
+
|
|
1454
|
+
expect(() =>
|
|
1455
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1456
|
+
).toThrow(/compiler-visible toolkit spread/);
|
|
1457
|
+
});
|
|
1458
|
+
|
|
1459
|
+
it("refreshes cached aliases when an edit preserves size and mtime", () => {
|
|
1460
|
+
const appRoot = mkdtempSync(
|
|
1461
|
+
nodePath.join(tmpdir(), "aui-generative-same-mtime-"),
|
|
1462
|
+
);
|
|
1463
|
+
writeAliasTarget(appRoot, "generated", generativeChild);
|
|
1464
|
+
writeAliasTarget(appRoot, "fallbacks", nonGenerativeChild);
|
|
1465
|
+
const tsconfigPath = nodePath.join(appRoot, "tsconfig.json");
|
|
1466
|
+
const filename = nodePath.join(appRoot, "src", "toolkit.tsx");
|
|
1467
|
+
|
|
1468
|
+
// Both writes are pinned to one timestamp, standing in for a filesystem
|
|
1469
|
+
// whose mtime granularity cannot separate them.
|
|
1470
|
+
const tick = 1700000000;
|
|
1471
|
+
writeAliasConfig(tsconfigPath, "./generated/*");
|
|
1472
|
+
utimesSync(tsconfigPath, tick, tick);
|
|
1473
|
+
|
|
1474
|
+
expect(() =>
|
|
1475
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1476
|
+
).not.toThrow();
|
|
1477
|
+
|
|
1478
|
+
writeAliasConfig(tsconfigPath, "./fallbacks/*");
|
|
1479
|
+
utimesSync(tsconfigPath, tick, tick);
|
|
1480
|
+
|
|
1481
|
+
expect(() =>
|
|
1482
|
+
compileGenerative(aliasedToolkitSource, { target: "client", filename }),
|
|
1483
|
+
).toThrow(/compiler-visible toolkit spread/);
|
|
1484
|
+
});
|
|
1485
|
+
|
|
1303
1486
|
it("prefers the most specific tsconfig path alias", () => {
|
|
1304
1487
|
const appRoot = mkdtempSync(
|
|
1305
1488
|
nodePath.join(tmpdir(), "aui-generative-alias-spec-"),
|
package/src/compile.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
import { createRequire } from "node:module";
|
|
3
4
|
import * as nodePath from "node:path";
|
|
4
5
|
import { parse } from "@babel/parser";
|
|
@@ -260,6 +261,7 @@ export function compileGenerative(
|
|
|
260
261
|
code: string,
|
|
261
262
|
options: CompileOptions,
|
|
262
263
|
): CompileResult {
|
|
264
|
+
currentCompilePass++;
|
|
263
265
|
const { target, filename } = options;
|
|
264
266
|
const backendless = options.backendless ?? false;
|
|
265
267
|
|
|
@@ -1055,25 +1057,89 @@ function matchAliasPattern(pattern: string, source: string): string | null {
|
|
|
1055
1057
|
return null;
|
|
1056
1058
|
}
|
|
1057
1059
|
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1060
|
+
interface TsconfigAliasesCacheEntry {
|
|
1061
|
+
aliases: TsconfigAliases | null;
|
|
1062
|
+
fileVersions: Map<string, string | null>;
|
|
1063
|
+
validatedInPass: number;
|
|
1064
|
+
reuseAcrossPasses: boolean;
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
const tsconfigAliasesByDir = new Map<string, TsconfigAliasesCacheEntry>();
|
|
1068
|
+
let currentCompilePass = 0;
|
|
1069
|
+
|
|
1070
|
+
interface TsconfigResolutionState {
|
|
1071
|
+
fileVersions: Map<string, string | null>;
|
|
1072
|
+
fileContents: Map<string, string | null>;
|
|
1073
|
+
cacheable: boolean;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// Fingerprints content rather than mtime and size: filesystems with coarse
|
|
1077
|
+
// timestamp granularity report an unchanged mtime for a same-length rewrite,
|
|
1078
|
+
// which would serve the stale aliases this cache exists to invalidate.
|
|
1079
|
+
function tsconfigFileVersion(content: string): string {
|
|
1080
|
+
return createHash("sha1").update(content).digest("hex");
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
function readTsconfigFile(
|
|
1084
|
+
state: TsconfigResolutionState,
|
|
1085
|
+
path: string,
|
|
1086
|
+
): string | null {
|
|
1087
|
+
const cached = state.fileContents.get(path);
|
|
1088
|
+
if (cached !== undefined) return cached;
|
|
1089
|
+
|
|
1090
|
+
let content: string | null;
|
|
1091
|
+
try {
|
|
1092
|
+
content = readFileSync(path, "utf8");
|
|
1093
|
+
} catch {
|
|
1094
|
+
content = null;
|
|
1095
|
+
}
|
|
1096
|
+
state.fileContents.set(path, content);
|
|
1097
|
+
state.fileVersions.set(
|
|
1098
|
+
path,
|
|
1099
|
+
content === null ? null : tsconfigFileVersion(content),
|
|
1100
|
+
);
|
|
1101
|
+
return content;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
function currentFileVersion(path: string): string | null {
|
|
1105
|
+
try {
|
|
1106
|
+
return tsconfigFileVersion(readFileSync(path, "utf8"));
|
|
1107
|
+
} catch {
|
|
1108
|
+
return null;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
function isTsconfigCacheCurrent(entry: TsconfigAliasesCacheEntry): boolean {
|
|
1113
|
+
for (const [path, version] of entry.fileVersions) {
|
|
1114
|
+
if (currentFileVersion(path) !== version) return false;
|
|
1115
|
+
}
|
|
1116
|
+
return true;
|
|
1117
|
+
}
|
|
1065
1118
|
|
|
1066
1119
|
/** Walks up from a directory to the nearest `tsconfig.json` that declares `paths`. */
|
|
1067
1120
|
function loadTsconfigAliases(fromDir: string): TsconfigAliases | null {
|
|
1068
1121
|
const cached = tsconfigAliasesByDir.get(fromDir);
|
|
1069
|
-
if (cached
|
|
1122
|
+
if (cached) {
|
|
1123
|
+
if (
|
|
1124
|
+
cached.validatedInPass === currentCompilePass ||
|
|
1125
|
+
(cached.reuseAcrossPasses && isTsconfigCacheCurrent(cached))
|
|
1126
|
+
) {
|
|
1127
|
+
cached.validatedInPass = currentCompilePass;
|
|
1128
|
+
return cached.aliases;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1070
1131
|
|
|
1071
1132
|
let aliases: TsconfigAliases | null = null;
|
|
1133
|
+
const state: TsconfigResolutionState = {
|
|
1134
|
+
fileVersions: new Map(),
|
|
1135
|
+
fileContents: new Map(),
|
|
1136
|
+
cacheable: true,
|
|
1137
|
+
};
|
|
1072
1138
|
let dir = fromDir;
|
|
1073
1139
|
for (;;) {
|
|
1074
1140
|
const tsconfigPath = nodePath.join(dir, "tsconfig.json");
|
|
1075
|
-
if (
|
|
1076
|
-
aliases = readTsconfigAliases(tsconfigPath, new Set());
|
|
1141
|
+
if (readTsconfigFile(state, tsconfigPath) !== null) {
|
|
1142
|
+
aliases = readTsconfigAliases(tsconfigPath, new Set(), state);
|
|
1077
1143
|
if (aliases) break;
|
|
1078
1144
|
}
|
|
1079
1145
|
const parent = nodePath.dirname(dir);
|
|
@@ -1081,7 +1147,14 @@ function loadTsconfigAliases(fromDir: string): TsconfigAliases | null {
|
|
|
1081
1147
|
dir = parent;
|
|
1082
1148
|
}
|
|
1083
1149
|
|
|
1084
|
-
|
|
1150
|
+
// Cache misses too; tracked absent paths invalidate when a config appears.
|
|
1151
|
+
// Unresolved package configs are reused only within the current compile.
|
|
1152
|
+
tsconfigAliasesByDir.set(fromDir, {
|
|
1153
|
+
aliases,
|
|
1154
|
+
fileVersions: state.fileVersions,
|
|
1155
|
+
validatedInPass: currentCompilePass,
|
|
1156
|
+
reuseAcrossPasses: state.cacheable,
|
|
1157
|
+
});
|
|
1085
1158
|
return aliases;
|
|
1086
1159
|
}
|
|
1087
1160
|
|
|
@@ -1089,16 +1162,20 @@ function loadTsconfigAliases(fromDir: string): TsconfigAliases | null {
|
|
|
1089
1162
|
function readTsconfigAliases(
|
|
1090
1163
|
tsconfigPath: string,
|
|
1091
1164
|
seen: Set<string>,
|
|
1165
|
+
state: TsconfigResolutionState,
|
|
1092
1166
|
): TsconfigAliases | null {
|
|
1093
1167
|
if (seen.has(tsconfigPath)) return null;
|
|
1094
1168
|
seen.add(tsconfigPath);
|
|
1095
1169
|
|
|
1170
|
+
const raw = readTsconfigFile(state, tsconfigPath);
|
|
1171
|
+
if (raw === null) return null;
|
|
1172
|
+
|
|
1096
1173
|
let config: {
|
|
1097
1174
|
extends?: string | string[];
|
|
1098
1175
|
compilerOptions?: { baseUrl?: string; paths?: Record<string, string[]> };
|
|
1099
1176
|
} | null;
|
|
1100
1177
|
try {
|
|
1101
|
-
config = parseJsonc(
|
|
1178
|
+
config = parseJsonc(raw);
|
|
1102
1179
|
} catch {
|
|
1103
1180
|
return null;
|
|
1104
1181
|
}
|
|
@@ -1125,9 +1202,9 @@ function readTsconfigAliases(
|
|
|
1125
1202
|
for (let i = extendsList.length - 1; i >= 0; i--) {
|
|
1126
1203
|
const entry = extendsList[i];
|
|
1127
1204
|
if (typeof entry !== "string") continue;
|
|
1128
|
-
const extended = resolveExtendedTsconfig(entry, configDir);
|
|
1205
|
+
const extended = resolveExtendedTsconfig(entry, configDir, state);
|
|
1129
1206
|
if (extended) {
|
|
1130
|
-
const aliases = readTsconfigAliases(extended, seen);
|
|
1207
|
+
const aliases = readTsconfigAliases(extended, seen, state);
|
|
1131
1208
|
if (aliases) return aliases;
|
|
1132
1209
|
}
|
|
1133
1210
|
}
|
|
@@ -1138,18 +1215,33 @@ function readTsconfigAliases(
|
|
|
1138
1215
|
function resolveExtendedTsconfig(
|
|
1139
1216
|
extendsValue: string,
|
|
1140
1217
|
configDir: string,
|
|
1218
|
+
state: TsconfigResolutionState,
|
|
1141
1219
|
): string | null {
|
|
1142
1220
|
if (extendsValue.startsWith(".")) {
|
|
1143
1221
|
const base = nodePath.resolve(configDir, extendsValue);
|
|
1144
1222
|
const candidates =
|
|
1145
1223
|
nodePath.extname(base) === ".json" ? [base] : [`${base}.json`, base];
|
|
1146
|
-
|
|
1224
|
+
for (const candidate of candidates) {
|
|
1225
|
+
if (readTsconfigFile(state, candidate) !== null) {
|
|
1226
|
+
return candidate;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
return null;
|
|
1147
1230
|
}
|
|
1231
|
+
|
|
1232
|
+
const request = extendsValue.endsWith(".json")
|
|
1233
|
+
? extendsValue
|
|
1234
|
+
: `${extendsValue}.json`;
|
|
1235
|
+
const requireFromConfig = createRequire(
|
|
1236
|
+
nodePath.join(configDir, "package.json"),
|
|
1237
|
+
);
|
|
1238
|
+
|
|
1148
1239
|
try {
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1240
|
+
const resolved = requireFromConfig.resolve(request);
|
|
1241
|
+
readTsconfigFile(state, resolved);
|
|
1242
|
+
return resolved;
|
|
1152
1243
|
} catch {
|
|
1244
|
+
state.cacheable = false;
|
|
1153
1245
|
return null;
|
|
1154
1246
|
}
|
|
1155
1247
|
}
|