mirascope 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +397 -0
  2. package/dist/bun.cjs +447 -0
  3. package/dist/bun.cjs.map +1 -0
  4. package/dist/bun.d.cts +53 -0
  5. package/dist/bun.d.ts +53 -0
  6. package/dist/bun.js +94 -0
  7. package/dist/bun.js.map +1 -0
  8. package/dist/chunk-2R5IW35Y.js +116 -0
  9. package/dist/chunk-2R5IW35Y.js.map +1 -0
  10. package/dist/chunk-A6ZCB7BU.js +6826 -0
  11. package/dist/chunk-A6ZCB7BU.js.map +1 -0
  12. package/dist/chunk-NSBPE2FW.js +15 -0
  13. package/dist/chunk-NSBPE2FW.js.map +1 -0
  14. package/dist/chunk-RMNCGJYW.js +49 -0
  15. package/dist/chunk-RMNCGJYW.js.map +1 -0
  16. package/dist/chunk-U4MFJ4DP.js +358 -0
  17. package/dist/chunk-U4MFJ4DP.js.map +1 -0
  18. package/dist/index.cjs +7705 -0
  19. package/dist/index.cjs.map +1 -0
  20. package/dist/index.d.cts +4859 -0
  21. package/dist/index.d.ts +4859 -0
  22. package/dist/index.js +324 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/model-T6IQ7UUA.js +4 -0
  25. package/dist/model-T6IQ7UUA.js.map +1 -0
  26. package/dist/tool-schema-Dh-RLHhC.d.cts +45 -0
  27. package/dist/tool-schema-Dh-RLHhC.d.ts +45 -0
  28. package/dist/transform/index.cjs +525 -0
  29. package/dist/transform/index.cjs.map +1 -0
  30. package/dist/transform/index.d.cts +89 -0
  31. package/dist/transform/index.d.ts +89 -0
  32. package/dist/transform/index.js +6 -0
  33. package/dist/transform/index.js.map +1 -0
  34. package/dist/transform/plugins/esbuild.cjs +472 -0
  35. package/dist/transform/plugins/esbuild.cjs.map +1 -0
  36. package/dist/transform/plugins/esbuild.d.cts +46 -0
  37. package/dist/transform/plugins/esbuild.d.ts +46 -0
  38. package/dist/transform/plugins/esbuild.js +5 -0
  39. package/dist/transform/plugins/esbuild.js.map +1 -0
  40. package/dist/transform/plugins/vite.cjs +405 -0
  41. package/dist/transform/plugins/vite.cjs.map +1 -0
  42. package/dist/transform/plugins/vite.d.cts +50 -0
  43. package/dist/transform/plugins/vite.d.ts +50 -0
  44. package/dist/transform/plugins/vite.js +5 -0
  45. package/dist/transform/plugins/vite.js.map +1 -0
  46. package/package.json +127 -0
package/dist/bun.js ADDED
@@ -0,0 +1,94 @@
1
+ import { createToolSchemaTransformer } from './chunk-U4MFJ4DP.js';
2
+ import './chunk-NSBPE2FW.js';
3
+ import { plugin } from 'bun';
4
+ import { readFileSync } from 'fs';
5
+ import { dirname, resolve } from 'path';
6
+ import ts from 'typescript';
7
+
8
+ function needsTransform(contents) {
9
+ return /\bdefineTool\b|\bdefineContextTool\b|\bdefineFormat\b/.test(contents);
10
+ }
11
+ function transformSource(filePath, contents) {
12
+ const configPath = ts.findConfigFile(
13
+ dirname(filePath),
14
+ ts.sys.fileExists,
15
+ "tsconfig.json"
16
+ );
17
+ const compilerOptions = {
18
+ target: ts.ScriptTarget.ESNext,
19
+ module: ts.ModuleKind.ESNext,
20
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
21
+ esModuleInterop: true,
22
+ strict: true
23
+ };
24
+ if (configPath) {
25
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
26
+ if (!configFile.error) {
27
+ const parsed = ts.parseJsonConfigFileContent(
28
+ configFile.config,
29
+ ts.sys,
30
+ dirname(configPath)
31
+ );
32
+ Object.assign(compilerOptions, parsed.options);
33
+ }
34
+ }
35
+ const host = ts.createCompilerHost(compilerOptions);
36
+ const originalGetSourceFile = host.getSourceFile;
37
+ host.getSourceFile = (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
38
+ if (resolve(fileName) === resolve(filePath)) {
39
+ return ts.createSourceFile(fileName, contents, languageVersion, true);
40
+ }
41
+ return originalGetSourceFile(
42
+ fileName,
43
+ languageVersion,
44
+ onError,
45
+ shouldCreateNewSourceFile
46
+ );
47
+ };
48
+ const program = ts.createProgram([filePath], compilerOptions, host);
49
+ const sourceFile = program.getSourceFile(filePath);
50
+ if (!sourceFile) {
51
+ return void 0;
52
+ }
53
+ const transformer = createToolSchemaTransformer(program);
54
+ const result = ts.transform(sourceFile, [transformer]);
55
+ const transformedSourceFile = result.transformed[0];
56
+ if (!transformedSourceFile) {
57
+ result.dispose();
58
+ return void 0;
59
+ }
60
+ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
61
+ const output = printer.printFile(transformedSourceFile);
62
+ result.dispose();
63
+ return output;
64
+ }
65
+ function mirascope(options = {}) {
66
+ const { filter = /\.tsx?$/, exclude = [/node_modules/] } = options;
67
+ plugin({
68
+ name: "mirascope",
69
+ setup(build) {
70
+ build.onLoad({ filter }, (args) => {
71
+ for (const pattern of exclude) {
72
+ if (pattern.test(args.path)) {
73
+ return;
74
+ }
75
+ }
76
+ const contents = readFileSync(args.path, "utf-8");
77
+ const loader = args.path.endsWith(".tsx") ? "tsx" : "ts";
78
+ if (!needsTransform(contents)) {
79
+ return { contents, loader };
80
+ }
81
+ const transformed = transformSource(args.path, contents);
82
+ return {
83
+ contents: transformed ?? contents,
84
+ loader
85
+ };
86
+ });
87
+ }
88
+ });
89
+ }
90
+ var bun_default = mirascope;
91
+
92
+ export { bun_default as default, mirascope };
93
+ //# sourceMappingURL=bun.js.map
94
+ //# sourceMappingURL=bun.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/bun.ts"],"names":[],"mappings":";;;;;;;AAqDA,SAAS,eAAe,QAAA,EAA2B;AACjD,EAAA,OAAO,uDAAA,CAAwD,KAAK,QAAQ,CAAA;AAC9E;AAKA,SAAS,eAAA,CACP,UACA,QAAA,EACoB;AAEpB,EAAA,MAAM,aAAa,EAAA,CAAG,cAAA;AAAA,IACpB,QAAQ,QAAQ,CAAA;AAAA,IAChB,GAAG,GAAA,CAAI,UAAA;A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AA;AAChD,QAAA,MAAM,SAAS,IAAA,CAAK,IAAA,CAAK,QAAA,CAAS,MAAM,IAAI,KAAA,GAAQ,IAAA;AAGpD,QAAA,IAAI,CAAC,cAAA,CAAe,QAAQ,CAAA,EAAG;AAE7B,UAAA,OAAO,EAAE,UAAU,MAAA,EAAO;AAAA,QAC5B;AAEA,QAAA,MAAM,WAAA,GAAc,eAAA,CAAgB,IAAA,CAAK,IAAA,EAAM,QAAQ,CAAA;AAGvD,QAAA,OAAO;AAAA,UACL,UAAU,WAAA,IAAe,QAAA;AAAA,UACzB;AAAA,SACF;AAAA,MACF,CAAC,CAAA;AAAA,IACH;AAAA,GACD,CAAA;AACH;AAEA,IAAO,WAAA,GAAQ","file":"bun.js","sourcesContent":["/**\n * Bun preload plugin for Mirascope tool schema transformation.\n *\n * This plugin intercepts TypeScript file loading and applies the Mirascope\n * transformer to inject tool schemas at runtime.\n *\n * ## Usage\n *\n * Create a preload file (e.g., `preload.ts`):\n * ```typescript\n * import { mirascope } from 'mirascope/bun';\n * mirascope();\n * ```\n *\n * Add to your `bunfig.toml`:\n * ```toml\n * preload = [\"./preload.ts\"]\n * ```\n *\n * Then run your files normally:\n * ```bash\n * bun run my-file.ts\n * ```\n */\n\n/* eslint-disable @typescript-eslint/unbound-method */\nimport { plugin } from \"bun\";\nimport { readFileSync } from \"fs\";\nimport { resolve, dirname } from \"path\";\nimport ts from \"typescript\";\n\nimport { createToolSchemaTransformer } from \"./transform/transformer\";\n\n/**\n * Options for the Mirascope Bun plugin.\n */\nexport interface MirascopeBunPluginOptions {\n /**\n * File filter pattern. Defaults to /\\.tsx?$/\n */\n filter?: RegExp;\n\n /**\n * Patterns to exclude. Files matching these won't be transformed.\n * Defaults to [/node_modules/]\n */\n exclude?: RegExp[];\n}\n\n/**\n * Check if a file contains defineTool, defineContextTool, or defineFormat calls.\n * Quick regex check to avoid expensive TypeScript compilation for files that don't need it.\n */\nfunction needsTransform(contents: string): boolean {\n return /\\bdefineTool\\b|\\bdefineContextTool\\b|\\bdefineFormat\\b/.test(contents);\n}\n\n/**\n * Transform TypeScript source code to inject tool schemas.\n */\nfunction transformSource(\n filePath: string,\n contents: string,\n): string | undefined {\n // Find tsconfig.json\n const configPath = ts.findConfigFile(\n dirname(filePath),\n ts.sys.fileExists,\n \"tsconfig.json\",\n );\n\n const compilerOptions: ts.CompilerOptions = {\n target: ts.ScriptTarget.ESNext,\n module: ts.ModuleKind.ESNext,\n moduleResolution: ts.ModuleResolutionKind.Bundler,\n esModuleInterop: true,\n strict: true,\n };\n\n // Load tsconfig if found\n if (configPath) {\n const configFile = ts.readConfigFile(configPath, ts.sys.readFile);\n if (!configFile.error) {\n const parsed = ts.parseJsonConfigFileContent(\n configFile.config,\n ts.sys,\n dirname(configPath),\n );\n Object.assign(compilerOptions, parsed.options);\n }\n }\n\n // Create a program with just this file\n const host = ts.createCompilerHost(compilerOptions);\n const originalGetSourceFile = host.getSourceFile;\n\n // Override to provide our in-memory source\n host.getSourceFile = (\n fileName: string,\n languageVersion: ts.ScriptTarget,\n onError?: (message: string) => void,\n shouldCreateNewSourceFile?: boolean,\n ) => {\n if (resolve(fileName) === resolve(filePath)) {\n return ts.createSourceFile(fileName, contents, languageVersion, true);\n }\n return originalGetSourceFile(\n fileName,\n languageVersion,\n onError,\n shouldCreateNewSourceFile,\n );\n };\n\n const program = ts.createProgram([filePath], compilerOptions, host);\n const sourceFile = program.getSourceFile(filePath);\n\n if (!sourceFile) {\n return undefined;\n }\n\n // Apply the transformer\n const transformer = createToolSchemaTransformer(program);\n const result = ts.transform(sourceFile, [transformer]);\n const transformedSourceFile = result.transformed[0];\n\n if (!transformedSourceFile) {\n result.dispose();\n return undefined;\n }\n\n // Print the transformed source back to string\n const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });\n const output = printer.printFile(transformedSourceFile);\n\n result.dispose();\n return output;\n}\n\n/**\n * Register the Mirascope Bun plugin for tool schema transformation.\n *\n * @param options - Plugin options.\n *\n * @example\n * ```typescript\n * // preload.ts\n * import { mirascope } from 'mirascope/bun';\n * mirascope();\n * ```\n */\nexport function mirascope(options: MirascopeBunPluginOptions = {}): void {\n const { filter = /\\.tsx?$/, exclude = [/node_modules/] } = options;\n\n plugin({\n name: \"mirascope\",\n setup(build) {\n build.onLoad({ filter }, (args) => {\n // Check exclusions - let Bun handle these normally\n for (const pattern of exclude) {\n if (pattern.test(args.path)) {\n // Return undefined to let Bun handle it normally\n return;\n }\n }\n\n const contents = readFileSync(args.path, \"utf-8\");\n const loader = args.path.endsWith(\".tsx\") ? \"tsx\" : \"ts\";\n\n // Quick check: skip files without tool definitions\n if (!needsTransform(contents)) {\n // Return original contents to keep Bun happy\n return { contents, loader };\n }\n\n const transformed = transformSource(args.path, contents);\n\n // Return transformed or original contents\n return {\n contents: transformed ?? contents,\n loader,\n };\n });\n },\n });\n}\n\nexport default mirascope;\n"]}
@@ -0,0 +1,116 @@
1
+ import { createToolSchemaTransformer } from './chunk-U4MFJ4DP.js';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import ts from 'typescript';
5
+
6
+ function mirascope(options = {}) {
7
+ const {
8
+ tsconfig = "./tsconfig.json",
9
+ filter = /\.(ts|tsx)$/,
10
+ compilerOptions: extraOptions = {}
11
+ } = options;
12
+ let cachedProgram;
13
+ let cachedTransformer;
14
+ let cachedConfigPath;
15
+ let cachedCompilerOptions;
16
+ return {
17
+ name: "mirascope",
18
+ setup(build) {
19
+ const cwd = process.cwd();
20
+ const configPath = path.resolve(cwd, tsconfig);
21
+ let parsedConfig;
22
+ if (fs.existsSync(configPath)) {
23
+ const configFile = ts.readConfigFile(
24
+ configPath,
25
+ (path2) => ts.sys.readFile(path2)
26
+ );
27
+ if (configFile.error) {
28
+ throw new Error(
29
+ `Error reading tsconfig.json: ${ts.flattenDiagnosticMessageText(
30
+ configFile.error.messageText,
31
+ "\n"
32
+ )}`
33
+ );
34
+ }
35
+ parsedConfig = ts.parseJsonConfigFileContent(
36
+ configFile.config,
37
+ ts.sys,
38
+ path.dirname(configPath)
39
+ );
40
+ } else {
41
+ parsedConfig = ts.parseJsonConfigFileContent({}, ts.sys, cwd);
42
+ }
43
+ const compilerOptions = {
44
+ ...parsedConfig.options,
45
+ ...extraOptions,
46
+ // Required for transformation
47
+ noEmit: false,
48
+ sourceMap: false,
49
+ inlineSourceMap: false,
50
+ declaration: false,
51
+ declarationMap: false
52
+ };
53
+ build.onLoad(
54
+ { filter },
55
+ async (args) => {
56
+ const fileName = args.path;
57
+ const sourceText = await fs.promises.readFile(fileName, "utf-8");
58
+ if (!sourceText.includes("defineTool") && !sourceText.includes("defineContextTool")) {
59
+ return {
60
+ contents: sourceText,
61
+ loader: fileName.endsWith(".tsx") ? "tsx" : "ts"
62
+ };
63
+ }
64
+ if (!cachedProgram || cachedConfigPath !== configPath || cachedCompilerOptions !== compilerOptions) {
65
+ cachedProgram = ts.createProgram([fileName], compilerOptions);
66
+ cachedTransformer = createToolSchemaTransformer(cachedProgram);
67
+ cachedConfigPath = configPath;
68
+ cachedCompilerOptions = compilerOptions;
69
+ }
70
+ const sourceFile = cachedProgram.getSourceFile(fileName);
71
+ if (!sourceFile) {
72
+ const singleFileProgram = ts.createProgram(
73
+ [fileName],
74
+ compilerOptions
75
+ );
76
+ const transformer = createToolSchemaTransformer(singleFileProgram);
77
+ const sf = singleFileProgram.getSourceFile(fileName);
78
+ if (!sf) {
79
+ return {
80
+ errors: [
81
+ {
82
+ text: `Could not parse source file: ${fileName}`,
83
+ location: null
84
+ }
85
+ ]
86
+ };
87
+ }
88
+ const result2 = ts.transform(sf, [transformer]);
89
+ const transformedSourceFile2 = result2.transformed[0];
90
+ const printer2 = ts.createPrinter();
91
+ const outputText2 = transformedSourceFile2 ? printer2.printFile(transformedSourceFile2) : sourceText;
92
+ result2.dispose();
93
+ return {
94
+ contents: outputText2,
95
+ loader: fileName.endsWith(".tsx") ? "tsx" : "ts"
96
+ };
97
+ }
98
+ const result = ts.transform(sourceFile, [cachedTransformer]);
99
+ const transformedSourceFile = result.transformed[0];
100
+ const printer = ts.createPrinter();
101
+ const outputText = transformedSourceFile ? printer.printFile(transformedSourceFile) : sourceText;
102
+ result.dispose();
103
+ return {
104
+ contents: outputText,
105
+ loader: fileName.endsWith(".tsx") ? "tsx" : "ts"
106
+ };
107
+ }
108
+ );
109
+ }
110
+ };
111
+ }
112
+ var esbuild_default = mirascope;
113
+
114
+ export { esbuild_default, mirascope };
115
+ //# sourceMappingURL=chunk-2R5IW35Y.js.map
116
+ //# sourceMappingURL=chunk-2R5IW35Y.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/transform/plugins/esbuild.ts"],"names":["path","result","transformedSourceFile","printer","outputText"],"mappings":";;;;;AAmDO,SAAS,SAAA,CAAU,OAAA,GAAyC,EAAC,EAAW;AAC7E,EAAA,MAAM;AAAA,IACJ,QAAA,GAAW,iBAAA;AAAA,IACX,MAAA,GAAS,aAAA;AAAA,IACT,eAAA,EAAiB,eAAe;AAAC,GACnC,GAAI,OAAA;AAGJ,EAAA,IAAI,aAAA;AACJ,EAAA,IAAI,iBAAA;AACJ,EAAA,IAAI,gBAAA;AACJ,EAAA,IAAI,qBAAA;AAEJ,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,WAAA;AAAA,IAEN,MAAM,KAAA,EAAoB;AACxB,MAAA,MAAM,GAAA,GAAM,QAAQ,GAAA,EAAI;AACxB,MAAA,MAAM,UAAA,GAAa,IAAA,CAAK,OAAA,CAAQ,GAAA,EAAK,QAAQ,CAAA;AAG7C,MAAA,IAAI,YAAA;AACJ,MAAA,IAAI,EAAA,CAAG,UAAA,CAAW,UAAU,CAAA,EAAG;AAC7B,QAAA,MAAM,aAAa,EAAA,CAAG,cAAA;AAAA,UAAe,UAAA;AAAA,UAAY,CAACA,KAAAA,KAChD,EAAA,CAAG,GAAA,CAAI,SAASA,KAAI;AAAA,SACtB;AACA,QAAA,IAAI,WAAW,KAAA,EAAO;AACpB,UAAA,MAAM,IAAI,KAAA;AAAA,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AAU,UAAA,EAAY,CAAC,iBAAkB,CAAC,CAAA;AAC5D,UAAA,MAAM,qBAAA,GAAwB,MAAA,CAAO,WAAA,CAAY,CAAC,CAAA;AAElD,UAAA,MAAM,OAAA,GAAU,GAAG,aAAA,EAAc;AAIjC,UAAA,MAAM,UAAA,GAAa,qBAAA,GACf,OAAA,CAAQ,SAAA,CAAU,qBAAqB,CAAA,GACvC,UAAA;AAEJ,UAAA,MAAA,CAAO,OAAA,EAAQ;AAEf,UAAA,OAAO;AAAA,YACL,QAAA,EAAU,UAAA;AAAA,YACV,MAAA,EAAQ,QAAA,CAAS,QAAA,CAAS,MAAM,IAAI,KAAA,GAAQ;AAAA,WAC9C;AAAA,QACF;AAAA,OACF;AAAA,IACF;AAAA,GACF;AACF;AAEA,IAAO,eAAA,GAAQ","file":"chunk-2R5IW35Y.js","sourcesContent":["/**\n * esbuild plugin for Mirascope tool schema transformation.\n *\n * This plugin uses the TypeScript compiler instead of esbuild's built-in\n * TypeScript support to ensure access to type information for schema generation.\n */\n\nimport type { Plugin, OnLoadArgs, OnLoadResult, PluginBuild } from \"esbuild\";\n\nimport fs from \"fs\";\nimport path from \"path\";\nimport ts from \"typescript\";\n\nimport { createToolSchemaTransformer } from \"@/transform/transformer\";\n\nexport interface MirascopeEsbuildPluginOptions {\n /**\n * Path to tsconfig.json. Defaults to './tsconfig.json'\n */\n tsconfig?: string;\n\n /**\n * File filter pattern. Defaults to /\\.(ts|tsx)$/\n */\n filter?: RegExp;\n\n /**\n * Additional TypeScript compiler options to merge with tsconfig.\n */\n compilerOptions?: ts.CompilerOptions;\n}\n\n/**\n * esbuild plugin that applies the Mirascope tool schema transformer.\n *\n * This plugin uses the TypeScript compiler instead of esbuild's built-in\n * TypeScript support to ensure access to type information for schema generation.\n *\n * @example\n * ```typescript\n * import * as esbuild from 'esbuild';\n * import { mirascope } from 'mirascope/esbuild';\n *\n * await esbuild.build({\n * entryPoints: ['src/index.ts'],\n * bundle: true,\n * outfile: 'dist/bundle.js',\n * plugins: [mirascope()],\n * });\n * ```\n */\nexport function mirascope(options: MirascopeEsbuildPluginOptions = {}): Plugin {\n const {\n tsconfig = \"./tsconfig.json\",\n filter = /\\.(ts|tsx)$/,\n compilerOptions: extraOptions = {},\n } = options;\n\n // Cache for the TypeScript program and transformer\n let cachedProgram: ts.Program | undefined;\n let cachedTransformer: ts.TransformerFactory<ts.SourceFile> | undefined;\n let cachedConfigPath: string | undefined;\n let cachedCompilerOptions: ts.CompilerOptions | undefined;\n\n return {\n name: \"mirascope\",\n\n setup(build: PluginBuild) {\n const cwd = process.cwd();\n const configPath = path.resolve(cwd, tsconfig);\n\n // Read and parse tsconfig.json\n let parsedConfig: ts.ParsedCommandLine;\n if (fs.existsSync(configPath)) {\n const configFile = ts.readConfigFile(configPath, (path) =>\n ts.sys.readFile(path),\n );\n if (configFile.error) {\n throw new Error(\n `Error reading tsconfig.json: ${ts.flattenDiagnosticMessageText(\n configFile.error.messageText,\n \"\\n\",\n )}`,\n );\n }\n\n parsedConfig = ts.parseJsonConfigFileContent(\n configFile.config,\n ts.sys,\n path.dirname(configPath),\n );\n } else {\n // Use default config if tsconfig.json doesn't exist\n parsedConfig = ts.parseJsonConfigFileContent({}, ts.sys, cwd);\n }\n\n // Merge compiler options\n const compilerOptions: ts.CompilerOptions = {\n ...parsedConfig.options,\n ...extraOptions,\n // Required for transformation\n noEmit: false,\n sourceMap: false,\n inlineSourceMap: false,\n declaration: false,\n declarationMap: false,\n };\n\n // Handle .ts and .tsx files\n build.onLoad(\n { filter },\n async (args: OnLoadArgs): Promise<OnLoadResult> => {\n const fileName = args.path;\n\n // Read the source file\n const sourceText = await fs.promises.readFile(fileName, \"utf-8\");\n\n // Check if this file contains defineTool or defineContextTool\n // If not, we can skip the expensive transformation\n if (\n !sourceText.includes(\"defineTool\") &&\n !sourceText.includes(\"defineContextTool\")\n ) {\n return {\n contents: sourceText,\n loader: fileName.endsWith(\".tsx\") ? \"tsx\" : \"ts\",\n };\n }\n\n // Create or reuse program\n // Coverage: The caching condition is always true on first invocation since\n // cachedProgram starts as undefined. Additional checks for config/options\n // changes exist for watch mode but are hard to test in unit tests.\n /* v8 ignore next 5 */\n if (\n !cachedProgram ||\n cachedConfigPath !== configPath ||\n cachedCompilerOptions !== compilerOptions\n ) {\n // Create a new program with all files\n cachedProgram = ts.createProgram([fileName], compilerOptions);\n cachedTransformer = createToolSchemaTransformer(cachedProgram);\n cachedConfigPath = configPath;\n cachedCompilerOptions = compilerOptions;\n }\n\n const sourceFile = cachedProgram.getSourceFile(fileName);\n\n // Coverage ignored: This fallback handles edge cases where the cached program\n // doesn't have the source file (e.g., new files added during watch mode).\n // In normal builds, the program always contains the requested file.\n /* v8 ignore start */\n if (!sourceFile) {\n // File not in program, create a minimal program for just this file\n const singleFileProgram = ts.createProgram(\n [fileName],\n compilerOptions,\n );\n const transformer = createToolSchemaTransformer(singleFileProgram);\n const sf = singleFileProgram.getSourceFile(fileName);\n\n if (!sf) {\n return {\n errors: [\n {\n text: `Could not parse source file: ${fileName}`,\n location: null,\n },\n ],\n };\n }\n\n const result = ts.transform(sf, [transformer]);\n const transformedSourceFile = result.transformed[0];\n const printer = ts.createPrinter();\n const outputText = transformedSourceFile\n ? printer.printFile(transformedSourceFile)\n : sourceText;\n result.dispose();\n\n return {\n contents: outputText,\n loader: fileName.endsWith(\".tsx\") ? \"tsx\" : \"ts\",\n };\n }\n /* v8 ignore end */\n\n // Transform the source file\n const result = ts.transform(sourceFile, [cachedTransformer!]);\n const transformedSourceFile = result.transformed[0];\n\n const printer = ts.createPrinter();\n // Coverage ignored: ts.transform always returns at least one transformed file\n // when given a valid source file. The fallback is defensive.\n /* v8 ignore next 3 */\n const outputText = transformedSourceFile\n ? printer.printFile(transformedSourceFile)\n : sourceText;\n\n result.dispose();\n\n return {\n contents: outputText,\n loader: fileName.endsWith(\".tsx\") ? \"tsx\" : \"ts\",\n };\n },\n );\n },\n };\n}\n\nexport default mirascope;\n"]}