@assistant-ui/x-generative-compiler 0.0.8 → 0.0.9

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 CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Target } from "./constants.js";
2
-
3
2
  //#region src/compile.d.ts
4
3
  type ToolType = "frontend" | "backend" | "human" | "provider";
5
4
  interface CompileOptions {
@@ -1 +1 @@
1
- {"version":3,"file":"compile.d.ts","names":[],"sources":["../src/compile.ts"],"mappings":";;;KAoBY,QAAA;AAAA,UA2CK,cAAA;EA3CL;EA6CV,MAAA,EAAQ,MAAM;;EAEd,QAAA;EA/CkB;EAiDlB,UAAA;EAN6B;;;;;;;;EAe7B,gBAAA;AAAA;AAAA,UAGe,aAAA;EACf,IAAA;EACA,GAAG;AAAA;AAAA;AAAA,cAIQ,sBAAA,SAA+B,KAAK;cACnC,OAAA,UAAiB,QAAA;AAAA;;iBASf,kBAAA,CAAmB,IAAY;;;;;iBA+I/B,iBAAA,CACd,IAAA,UACA,OAAA,EAAS,cAAA,GACR,aAAa"}
1
+ {"version":3,"file":"compile.d.ts","names":[],"sources":["../src/compile.ts"],"mappings":";;KA0BY;UA2CK;;EAEf,QAAQ;;EAER;;EAEA;;;;;;;;;EASA;;UAGe;EACf;EACA;;;cAIW,+BAA+B;cAC9B,iBAAiB;;;iBASf,mBAAmB;;;;;iBA+InB,kBACd,cACA,SAAS,iBACR"}
package/dist/compile.js CHANGED
@@ -9,6 +9,11 @@ import _generate from "@babel/generator";
9
9
  import * as t from "@babel/types";
10
10
  import { satisfies } from "semver";
11
11
  //#region src/compile.ts
12
+ const parserPlugins = [
13
+ "typescript",
14
+ "jsx",
15
+ "explicitResourceManagement"
16
+ ];
12
17
  const traverse = typeof _traverse === "function" ? _traverse : _traverse.default;
13
18
  const generate = typeof _generate === "function" ? _generate : _generate.default;
14
19
  /** The required wrapper around a toolkit's tools (stripped at build time). */
@@ -157,11 +162,7 @@ function compileGenerative(code, options) {
157
162
  const { target, filename } = options;
158
163
  const ast = parse(code, {
159
164
  sourceType: "module",
160
- plugins: [
161
- "typescript",
162
- "jsx",
163
- "explicitResourceManagement"
164
- ]
165
+ plugins: parserPlugins
165
166
  });
166
167
  if (!ast.program.directives.some((d) => d.value.value === "use generative")) throw new GenerativeCompileError(`missing "${DIRECTIVE}" directive`, filename);
167
168
  ensureCompilerCompatibleWithCore(ast, filename);
@@ -197,7 +198,7 @@ function compileGenerative(code, options) {
197
198
  if (target === "server" && keptBackendExecute && (options.injectServerOnly ?? true)) ast.program.body.unshift(t.importDeclaration([], t.stringLiteral("server-only")));
198
199
  const result = generate(ast, {
199
200
  sourceMaps: options.sourceMaps ?? false,
200
- filename,
201
+ ...filename ? { filename } : {},
201
202
  jsescOption: { minimal: true }
202
203
  }, code);
203
204
  return {
@@ -319,6 +320,15 @@ function unwrapToCall(node, name) {
319
320
  return null;
320
321
  }
321
322
  /**
323
+ * Unwraps a statement to its variable declaration through an `export` wrapper,
324
+ * or returns `null`.
325
+ */
326
+ function asVariableDeclaration(statement) {
327
+ if (t.isVariableDeclaration(statement)) return statement;
328
+ if (t.isExportNamedDeclaration(statement) && t.isVariableDeclaration(statement.declaration)) return statement.declaration;
329
+ return null;
330
+ }
331
+ /**
322
332
  * Collects the names bound to `new JSONGenerativeUI(...)` (e.g.
323
333
  * `const generative = new JSONGenerativeUI({ library })`). A toolkit entry that
324
334
  * calls a method on one of these is a generative tool whose halves the library
@@ -328,7 +338,7 @@ function collectGenerativeInstances(ast) {
328
338
  const names = /* @__PURE__ */ new Set();
329
339
  const generativeFactories = collectGenerativeFactoryImports(ast);
330
340
  for (const statement of ast.program.body) {
331
- const variableDeclaration = t.isVariableDeclaration(statement) ? statement : t.isExportNamedDeclaration(statement) && t.isVariableDeclaration(statement.declaration) ? statement.declaration : null;
341
+ const variableDeclaration = asVariableDeclaration(statement);
332
342
  if (!variableDeclaration) continue;
333
343
  for (const declaration of variableDeclaration.declarations) {
334
344
  const { id, init } = declaration;
@@ -392,8 +402,9 @@ function collectToolkitSpreadNames(ast, filename, context) {
392
402
  const spreadNames = /* @__PURE__ */ new Map();
393
403
  const localToolkitCalls = /* @__PURE__ */ new Map();
394
404
  for (const statement of ast.program.body) {
395
- if (t.isVariableDeclaration(statement)) {
396
- for (const declaration of statement.declarations) {
405
+ const localDeclaration = asVariableDeclaration(statement);
406
+ if (localDeclaration) {
407
+ for (const declaration of localDeclaration.declarations) {
397
408
  const { id, init } = declaration;
398
409
  if (t.isIdentifier(id) && init) {
399
410
  const toolkitCall = unwrapToToolkitCall(init);
@@ -466,11 +477,7 @@ function getGenerativeImportToolkitNames(source, filename, context) {
466
477
  try {
467
478
  const names = getDefaultExportToolkitNames(parse(code, {
468
479
  sourceType: "module",
469
- plugins: [
470
- "typescript",
471
- "jsx",
472
- "explicitResourceManagement"
473
- ]
480
+ plugins: parserPlugins
474
481
  }), resolved, context);
475
482
  context.importedToolkitNamesByFile.set(resolved, names);
476
483
  return names;
@@ -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\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\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\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\n const ast = parse(code, {\n sourceType: \"module\",\n plugins: [\"typescript\", \"jsx\", \"explicitResourceManagement\"],\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 toolkitSpreadNames = collectToolkitSpreadNames(\n ast,\n filename,\n createToolkitNameContext(),\n );\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 flags,\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,\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 * 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 = t.isVariableDeclaration(statement)\n ? statement\n : t.isExportNamedDeclaration(statement) &&\n t.isVariableDeclaration(statement.declaration)\n ? statement.declaration\n : null;\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\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 * 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 */\nfunction collectToolkitSpreadNames(\n ast: t.File,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitSpreadNames {\n const spreadNames: ToolkitSpreadNames = new Map();\n const localToolkitCalls = new Map<string, t.CallExpression>();\n\n for (const statement of ast.program.body) {\n if (t.isVariableDeclaration(statement)) {\n for (const declaration of statement.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 if (!defaultSpecifier) continue;\n\n const names = getGenerativeImportToolkitNames(\n statement.source.value,\n filename,\n context,\n );\n if (names !== undefined) {\n spreadNames.set(defaultSpecifier.local.name, names);\n }\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;\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: [\"typescript\", \"jsx\", \"explicitResourceManagement\"],\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 warnDuplicateToolkitNames(\n object: t.ObjectExpression,\n toolkitSpreadNames: ToolkitSpreadNames,\n filename: string | undefined,\n): void {\n const names = collectToolkitObjectNames(object, toolkitSpreadNames);\n if (!names) return;\n\n const seen = new Set<string>();\n const warned = new Set<string>();\n for (const name of names) {\n if (seen.has(name)) {\n if (!warned.has(name)) {\n console.warn(\n new GenerativeCompileError(\n `Duplicate tool name \"${name}\" while composing toolkits. ` +\n \"JavaScript object spread keeps the last definition.\",\n filename,\n ).message,\n );\n warned.add(name);\n }\n continue;\n }\n seen.add(name);\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\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 flags: TargetFlags,\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 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 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);\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): 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.\n removeMember(object, \"unstable_backendDefault\");\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":";;;;;;;;;;;AAaA,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;;;;;;;AAO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EAAE,YAAY,eAAe,EAAE,KAAK,CAC3C;CAEF,IAAI,EAAE,eAAe,IAAI,GACvB,OAAO,KAAK,SAAS,OAAO,OAAO,MAAM,QAAQ,eAAe,EAAE,CAAC;CAErE,OAAO;AACT;;AAGA,SAAS,gBAAgB,MAAgD;CACvE,IAAI,QAAQ,MAAM,OAAO;CACzB,IAAI,EAAE,iBAAiB,IAAI,KAAK,EAAE,wBAAwB,IAAI,GAC5D,OAAO,gBAAgB,KAAK,UAAU;CAIxC,IAAI,EAAE,kBAAkB,IAAI,GAC1B,OAAO,KAAK,SAAS,OAClB,OAAO,MAAM,QAAS,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,CACjE;CAEF,IAAI,EAAE,mBAAmB,IAAI,GAC3B,OAAO,KAAK,WAAW,OACpB,MACC,EAAE,iBAAiB,CAAC,KACpB,CAAC,EAAE,YACH,EAAE,aAAa,EAAE,KAAK,KACtB,gBAAgB,EAAE,KAAK,CAC3B;CAEF,IAAI,EAAE,kBAAkB,IAAI,GAC1B,OAAO,KAAK,YAAY,OACrB,MAAM,EAAE,aAAa,CAAC,KAAK,gBAAgB,CAAC,CAC/C;CAEF,OACE,EAAE,0BAA0B,IAAI,KAChC,EAAE,qBAAqB,IAAI,KAC3B,EAAE,kBAAkB,IAAI,KACxB,EAAE,aAAa,IAAI,KAElB,EAAE,mBAAmB,IAAI,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 { 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\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\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\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 toolkitSpreadNames = collectToolkitSpreadNames(\n ast,\n filename,\n createToolkitNameContext(),\n );\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 flags,\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\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 * 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 */\nfunction collectToolkitSpreadNames(\n ast: t.File,\n filename: string | undefined,\n context: ToolkitNameContext,\n): ToolkitSpreadNames {\n const spreadNames: ToolkitSpreadNames = new Map();\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 if (!defaultSpecifier) continue;\n\n const names = getGenerativeImportToolkitNames(\n statement.source.value,\n filename,\n context,\n );\n if (names !== undefined) {\n spreadNames.set(defaultSpecifier.local.name, names);\n }\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;\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 warnDuplicateToolkitNames(\n object: t.ObjectExpression,\n toolkitSpreadNames: ToolkitSpreadNames,\n filename: string | undefined,\n): void {\n const names = collectToolkitObjectNames(object, toolkitSpreadNames);\n if (!names) return;\n\n const seen = new Set<string>();\n const warned = new Set<string>();\n for (const name of names) {\n if (seen.has(name)) {\n if (!warned.has(name)) {\n console.warn(\n new GenerativeCompileError(\n `Duplicate tool name \"${name}\" while composing toolkits. ` +\n \"JavaScript object spread keeps the last definition.\",\n filename,\n ).message,\n );\n warned.add(name);\n }\n continue;\n }\n seen.add(name);\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\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 flags: TargetFlags,\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 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 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);\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): 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.\n removeMember(object, \"unstable_backendDefault\");\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;;AAkClC,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,GAAG;EAC/C,IAAI,KAAK,WAAW,MAAM,CAAC,GAAG;GAC5B,MAAM,KAAK,KAAK,QAAQ,MAAM,CAAC;GAC/B,IAAI,OAAO,IAAI,OAAO;GACtB,IAAI,KAAK;EACX,OAAO,IAAI,KAAK,WAAW,MAAM,CAAC,GAAG;GACnC,MAAM,MAAM,KAAK,QAAQ,MAAM,IAAI,CAAC;GACpC,IAAI,QAAQ,IAAI,OAAO;GACvB,IAAI,MAAM;EACZ,OACE;CAEJ;CAEA,MAAM,QAA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E,aAAa,EAAE,KAAK,KACtB,gBAAgB,EAAE,KAAK,CAC3B;CAEF,IAAI,EAAE,kBAAkB,IAAI,GAC1B,OAAO,KAAK,YAAY,OACrB,MAAM,EAAE,aAAa,CAAC,KAAK,gBAAgB,CAAC,CAC/C;CAEF,OACE,EAAE,0BAA0B,IAAI,KAChC,EAAE,qBAAqB,IAAI,KAC3B,EAAE,kBAAkB,IAAI,KACxB,EAAE,aAAa,IAAI,KAElB,EAAE,mBAAmB,IAAI,KAAK,CAAC,KAAK,YACrC,EAAE,aAAa,IAAI,KACnB,EAAE,cAAc,IAAI,KACpB,EAAE,UAAU,IAAI;AAEpB"}
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","names":[],"sources":["../src/constants.ts"],"mappings":";;cACa,SAAA;;KAGD,MAAA"}
1
+ {"version":3,"file":"constants.d.ts","names":[],"sources":["../src/constants.ts"],"mappings":";;cACa;;KAGD"}
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.0.8";
2
+ var version = "0.0.9";
3
3
  //#endregion
4
4
  export { version };
5
5
 
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.8",
4
+ "version": "0.0.9",
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",
@@ -30,18 +30,18 @@
30
30
  "sideEffects": false,
31
31
  "dependencies": {
32
32
  "@babel/generator": "^8.0.0",
33
- "@babel/parser": "^8.0.0",
34
- "@babel/traverse": "^8.0.0",
35
- "@babel/types": "^8.0.0",
33
+ "@babel/parser": "^8.0.4",
34
+ "@babel/traverse": "^8.0.4",
35
+ "@babel/types": "^8.0.4",
36
36
  "semver": "^7.8.5"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/babel__generator": "^7.27.0",
40
40
  "@types/babel__traverse": "^7.28.0",
41
- "@types/node": "^26.0.0",
41
+ "@types/node": "^26.1.1",
42
42
  "@types/semver": "^7.7.1",
43
- "vitest": "^4.1.9",
44
- "@assistant-ui/x-buildutils": "0.0.17"
43
+ "vitest": "^4.1.10",
44
+ "@assistant-ui/x-buildutils": "0.0.18"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public",
@@ -587,6 +587,52 @@ export default defineToolkit({
587
587
  expect(client).toContain("render");
588
588
  });
589
589
 
590
+ it("allows spreading an exported compiler-visible local toolkit", () => {
591
+ const src = `"use generative";
592
+ import { defineToolkit } from "@assistant-ui/react";
593
+ import { db } from "@/db";
594
+ export const base = defineToolkit({
595
+ get_weather: {
596
+ execute: async () => db.getWeather(),
597
+ render: () => null,
598
+ },
599
+ });
600
+ export default defineToolkit({
601
+ ...base,
602
+ get_time: {
603
+ execute: async () => db.getTime(),
604
+ render: () => null,
605
+ },
606
+ });`;
607
+
608
+ const server = compileGenerative(src, { target: "server" }).code;
609
+ expect(server).toContain("...base");
610
+ expect(server).toContain("db.getWeather");
611
+ expect(server).toContain("db.getTime");
612
+ expect(server).not.toContain("render");
613
+
614
+ const client = compileGenerative(src, { target: "client" }).code;
615
+ expect(client).toContain("...base");
616
+ expect(client).not.toContain("db.getWeather");
617
+ expect(client).not.toContain("db.getTime");
618
+ expect(client).toContain("render");
619
+ });
620
+
621
+ it("allows spreading an exported MCP toolkit fragment", () => {
622
+ const src = `"use generative";
623
+ import { defineMcpToolkit, defineToolkit } from "@assistant-ui/react";
624
+ export const mcp = defineMcpToolkit({
625
+ docs: { type: "http", url: "http://localhost:3001/mcp" },
626
+ });
627
+ export default defineToolkit({
628
+ ...mcp,
629
+ });`;
630
+
631
+ const server = compileGenerative(src, { target: "server" }).code;
632
+ expect(server).toContain("...mcp");
633
+ expect(server).toContain("docs");
634
+ });
635
+
590
636
  it("allows directly spreading an MCP toolkit fragment", () => {
591
637
  const src = `"use generative";
592
638
  import { defineMcpToolkit, defineToolkit } from "@assistant-ui/react";
@@ -614,6 +660,19 @@ export default defineToolkit({
614
660
  );
615
661
  });
616
662
 
663
+ it("does not treat exported opaque toolkit declarations as compiler-visible spreads", () => {
664
+ const src = `"use generative";
665
+ import { defineToolkit } from "@assistant-ui/react";
666
+ export const base = makeToolkit();
667
+ export default defineToolkit({
668
+ ...base,
669
+ });`;
670
+
671
+ expect(() => compileGenerative(src, { target: "client" })).toThrow(
672
+ /compiler-visible toolkit spread/,
673
+ );
674
+ });
675
+
617
676
  it("does not treat nested toolkit declarations as compiler-visible spreads", () => {
618
677
  const src = `"use generative";
619
678
  import { defineToolkit } from "@assistant-ui/react";
package/src/compile.ts CHANGED
@@ -9,6 +9,12 @@ import { satisfies } from "semver";
9
9
  import { DIRECTIVE, type Target } from "./constants";
10
10
  import pkgJson from "../package.json" with { type: "json" };
11
11
 
12
+ const parserPlugins = [
13
+ "typescript",
14
+ "jsx",
15
+ "explicitResourceManagement",
16
+ ] as NonNullable<NonNullable<Parameters<typeof parse>[1]>["plugins"]>;
17
+
12
18
  // @babel/traverse and @babel/generator are CJS; their default export is the
13
19
  // function itself under some interop and `{ default }` under others.
14
20
  const traverse = (
@@ -246,7 +252,7 @@ export function compileGenerative(
246
252
 
247
253
  const ast = parse(code, {
248
254
  sourceType: "module",
249
- plugins: ["typescript", "jsx", "explicitResourceManagement"],
255
+ plugins: parserPlugins,
250
256
  });
251
257
 
252
258
  if (!ast.program.directives.some((d) => d.value.value === DIRECTIVE)) {
@@ -346,7 +352,7 @@ export function compileGenerative(
346
352
  ast,
347
353
  {
348
354
  sourceMaps: options.sourceMaps ?? false,
349
- filename,
355
+ ...(filename ? { filename } : {}),
350
356
  jsescOption: { minimal: true },
351
357
  },
352
358
  code,
@@ -594,6 +600,23 @@ function unwrapToCall(node: t.Node, name: string): t.CallExpression | null {
594
600
  return null;
595
601
  }
596
602
 
603
+ /**
604
+ * Unwraps a statement to its variable declaration through an `export` wrapper,
605
+ * or returns `null`.
606
+ */
607
+ function asVariableDeclaration(
608
+ statement: t.Statement,
609
+ ): t.VariableDeclaration | null {
610
+ if (t.isVariableDeclaration(statement)) return statement;
611
+ if (
612
+ t.isExportNamedDeclaration(statement) &&
613
+ t.isVariableDeclaration(statement.declaration)
614
+ ) {
615
+ return statement.declaration;
616
+ }
617
+ return null;
618
+ }
619
+
597
620
  /**
598
621
  * Collects the names bound to `new JSONGenerativeUI(...)` (e.g.
599
622
  * `const generative = new JSONGenerativeUI({ library })`). A toolkit entry that
@@ -604,12 +627,7 @@ function collectGenerativeInstances(ast: t.File): Set<string> {
604
627
  const names = new Set<string>();
605
628
  const generativeFactories = collectGenerativeFactoryImports(ast);
606
629
  for (const statement of ast.program.body) {
607
- const variableDeclaration = t.isVariableDeclaration(statement)
608
- ? statement
609
- : t.isExportNamedDeclaration(statement) &&
610
- t.isVariableDeclaration(statement.declaration)
611
- ? statement.declaration
612
- : null;
630
+ const variableDeclaration = asVariableDeclaration(statement);
613
631
  if (!variableDeclaration) continue;
614
632
  for (const declaration of variableDeclaration.declarations) {
615
633
  const { id, init } = declaration;
@@ -726,8 +744,9 @@ function collectToolkitSpreadNames(
726
744
  const localToolkitCalls = new Map<string, t.CallExpression>();
727
745
 
728
746
  for (const statement of ast.program.body) {
729
- if (t.isVariableDeclaration(statement)) {
730
- for (const declaration of statement.declarations) {
747
+ const localDeclaration = asVariableDeclaration(statement);
748
+ if (localDeclaration) {
749
+ for (const declaration of localDeclaration.declarations) {
731
750
  const { id, init } = declaration;
732
751
  if (t.isIdentifier(id) && init) {
733
752
  const toolkitCall = unwrapToToolkitCall(init);
@@ -833,7 +852,7 @@ function getGenerativeImportToolkitNames(
833
852
  try {
834
853
  const importedAst = parse(code, {
835
854
  sourceType: "module",
836
- plugins: ["typescript", "jsx", "explicitResourceManagement"],
855
+ plugins: parserPlugins,
837
856
  });
838
857
  const names = getDefaultExportToolkitNames(importedAst, resolved, context);
839
858
  context.importedToolkitNamesByFile.set(resolved, names);