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.
- package/README.md +397 -0
- package/dist/bun.cjs +447 -0
- package/dist/bun.cjs.map +1 -0
- package/dist/bun.d.cts +53 -0
- package/dist/bun.d.ts +53 -0
- package/dist/bun.js +94 -0
- package/dist/bun.js.map +1 -0
- package/dist/chunk-2R5IW35Y.js +116 -0
- package/dist/chunk-2R5IW35Y.js.map +1 -0
- package/dist/chunk-A6ZCB7BU.js +6826 -0
- package/dist/chunk-A6ZCB7BU.js.map +1 -0
- package/dist/chunk-NSBPE2FW.js +15 -0
- package/dist/chunk-NSBPE2FW.js.map +1 -0
- package/dist/chunk-RMNCGJYW.js +49 -0
- package/dist/chunk-RMNCGJYW.js.map +1 -0
- package/dist/chunk-U4MFJ4DP.js +358 -0
- package/dist/chunk-U4MFJ4DP.js.map +1 -0
- package/dist/index.cjs +7705 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +4859 -0
- package/dist/index.d.ts +4859 -0
- package/dist/index.js +324 -0
- package/dist/index.js.map +1 -0
- package/dist/model-T6IQ7UUA.js +4 -0
- package/dist/model-T6IQ7UUA.js.map +1 -0
- package/dist/tool-schema-Dh-RLHhC.d.cts +45 -0
- package/dist/tool-schema-Dh-RLHhC.d.ts +45 -0
- package/dist/transform/index.cjs +525 -0
- package/dist/transform/index.cjs.map +1 -0
- package/dist/transform/index.d.cts +89 -0
- package/dist/transform/index.d.ts +89 -0
- package/dist/transform/index.js +6 -0
- package/dist/transform/index.js.map +1 -0
- package/dist/transform/plugins/esbuild.cjs +472 -0
- package/dist/transform/plugins/esbuild.cjs.map +1 -0
- package/dist/transform/plugins/esbuild.d.cts +46 -0
- package/dist/transform/plugins/esbuild.d.ts +46 -0
- package/dist/transform/plugins/esbuild.js +5 -0
- package/dist/transform/plugins/esbuild.js.map +1 -0
- package/dist/transform/plugins/vite.cjs +405 -0
- package/dist/transform/plugins/vite.cjs.map +1 -0
- package/dist/transform/plugins/vite.d.cts +50 -0
- package/dist/transform/plugins/vite.d.ts +50 -0
- package/dist/transform/plugins/vite.js +5 -0
- package/dist/transform/plugins/vite.js.map +1 -0
- package/package.json +127 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { J as JsonSchemaProperty, a as ToolParameterSchema } from '../tool-schema-Dh-RLHhC.cjs';
|
|
3
|
+
export { MirascopeVitePluginOptions, default as vitePlugin } from './plugins/vite.cjs';
|
|
4
|
+
export { MirascopeEsbuildPluginOptions, default as esbuildPlugin } from './plugins/esbuild.cjs';
|
|
5
|
+
import '@rollup/plugin-typescript';
|
|
6
|
+
import 'vite';
|
|
7
|
+
import 'esbuild';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* TypeScript transformer for injecting schemas into tool and format definitions.
|
|
11
|
+
*
|
|
12
|
+
* This transformer finds calls to `defineTool<T>()`, `defineContextTool<T, DepsT>()`,
|
|
13
|
+
* and `defineFormat<T>()` and injects the `__schema` property with the JSON schema
|
|
14
|
+
* generated from type T.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create a TypeScript transformer that injects __schema into tool definitions.
|
|
19
|
+
*
|
|
20
|
+
* @param program - The TypeScript program.
|
|
21
|
+
* @returns A transformer factory.
|
|
22
|
+
*/
|
|
23
|
+
declare function createToolSchemaTransformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
|
|
24
|
+
/**
|
|
25
|
+
* Default export for ts-patch and other transformer loaders.
|
|
26
|
+
*/
|
|
27
|
+
declare function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Convert TypeScript types to JSON Schema.
|
|
31
|
+
*
|
|
32
|
+
* Uses the TypeScript Compiler API to introspect types and generate
|
|
33
|
+
* JSON Schema representations suitable for LLM tool definitions.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Context for type-to-schema conversion.
|
|
38
|
+
*/
|
|
39
|
+
interface ConversionContext {
|
|
40
|
+
/** The TypeScript type checker. */
|
|
41
|
+
checker: ts.TypeChecker;
|
|
42
|
+
/** Definitions for referenced types ($defs). */
|
|
43
|
+
definitions: Map<string, JsonSchemaProperty>;
|
|
44
|
+
/** Set of type names currently being processed (for cycle detection). */
|
|
45
|
+
processing: Set<string>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convert a TypeScript type to a JSON Schema property.
|
|
49
|
+
*
|
|
50
|
+
* @param type - The TypeScript type to convert.
|
|
51
|
+
* @param ctx - The conversion context.
|
|
52
|
+
* @returns The JSON Schema property.
|
|
53
|
+
*/
|
|
54
|
+
declare function typeToJsonSchema(type: ts.Type, ctx: ConversionContext): JsonSchemaProperty;
|
|
55
|
+
/**
|
|
56
|
+
* Convert a TypeScript type to a ToolParameterSchema.
|
|
57
|
+
*
|
|
58
|
+
* This is the main entry point for converting tool argument types
|
|
59
|
+
* to the schema format expected by LLM providers.
|
|
60
|
+
*
|
|
61
|
+
* @param type - The TypeScript type to convert (should be an object type).
|
|
62
|
+
* @param checker - The TypeScript type checker.
|
|
63
|
+
* @returns The tool parameter schema.
|
|
64
|
+
*/
|
|
65
|
+
declare function typeToToolParameterSchema(type: ts.Type, checker: ts.TypeChecker): ToolParameterSchema;
|
|
66
|
+
/**
|
|
67
|
+
* Create a ConversionContext for testing or manual use.
|
|
68
|
+
*/
|
|
69
|
+
declare function createConversionContext(checker: ts.TypeChecker): ConversionContext;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Shared types for build system plugins.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Common transformer configuration that can be passed to build plugins.
|
|
77
|
+
*/
|
|
78
|
+
interface TransformerConfig {
|
|
79
|
+
/**
|
|
80
|
+
* Additional transformers to run before the Mirascope transformer.
|
|
81
|
+
*/
|
|
82
|
+
before?: ts.TransformerFactory<ts.SourceFile>[];
|
|
83
|
+
/**
|
|
84
|
+
* Additional transformers to run after the Mirascope transformer.
|
|
85
|
+
*/
|
|
86
|
+
after?: ts.TransformerFactory<ts.SourceFile>[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { type TransformerConfig, createConversionContext, createToolSchemaTransformer, transformer, typeToJsonSchema, typeToToolParameterSchema };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { J as JsonSchemaProperty, a as ToolParameterSchema } from '../tool-schema-Dh-RLHhC.js';
|
|
3
|
+
export { MirascopeVitePluginOptions, default as vitePlugin } from './plugins/vite.js';
|
|
4
|
+
export { MirascopeEsbuildPluginOptions, default as esbuildPlugin } from './plugins/esbuild.js';
|
|
5
|
+
import '@rollup/plugin-typescript';
|
|
6
|
+
import 'vite';
|
|
7
|
+
import 'esbuild';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* TypeScript transformer for injecting schemas into tool and format definitions.
|
|
11
|
+
*
|
|
12
|
+
* This transformer finds calls to `defineTool<T>()`, `defineContextTool<T, DepsT>()`,
|
|
13
|
+
* and `defineFormat<T>()` and injects the `__schema` property with the JSON schema
|
|
14
|
+
* generated from type T.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create a TypeScript transformer that injects __schema into tool definitions.
|
|
19
|
+
*
|
|
20
|
+
* @param program - The TypeScript program.
|
|
21
|
+
* @returns A transformer factory.
|
|
22
|
+
*/
|
|
23
|
+
declare function createToolSchemaTransformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
|
|
24
|
+
/**
|
|
25
|
+
* Default export for ts-patch and other transformer loaders.
|
|
26
|
+
*/
|
|
27
|
+
declare function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Convert TypeScript types to JSON Schema.
|
|
31
|
+
*
|
|
32
|
+
* Uses the TypeScript Compiler API to introspect types and generate
|
|
33
|
+
* JSON Schema representations suitable for LLM tool definitions.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Context for type-to-schema conversion.
|
|
38
|
+
*/
|
|
39
|
+
interface ConversionContext {
|
|
40
|
+
/** The TypeScript type checker. */
|
|
41
|
+
checker: ts.TypeChecker;
|
|
42
|
+
/** Definitions for referenced types ($defs). */
|
|
43
|
+
definitions: Map<string, JsonSchemaProperty>;
|
|
44
|
+
/** Set of type names currently being processed (for cycle detection). */
|
|
45
|
+
processing: Set<string>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convert a TypeScript type to a JSON Schema property.
|
|
49
|
+
*
|
|
50
|
+
* @param type - The TypeScript type to convert.
|
|
51
|
+
* @param ctx - The conversion context.
|
|
52
|
+
* @returns The JSON Schema property.
|
|
53
|
+
*/
|
|
54
|
+
declare function typeToJsonSchema(type: ts.Type, ctx: ConversionContext): JsonSchemaProperty;
|
|
55
|
+
/**
|
|
56
|
+
* Convert a TypeScript type to a ToolParameterSchema.
|
|
57
|
+
*
|
|
58
|
+
* This is the main entry point for converting tool argument types
|
|
59
|
+
* to the schema format expected by LLM providers.
|
|
60
|
+
*
|
|
61
|
+
* @param type - The TypeScript type to convert (should be an object type).
|
|
62
|
+
* @param checker - The TypeScript type checker.
|
|
63
|
+
* @returns The tool parameter schema.
|
|
64
|
+
*/
|
|
65
|
+
declare function typeToToolParameterSchema(type: ts.Type, checker: ts.TypeChecker): ToolParameterSchema;
|
|
66
|
+
/**
|
|
67
|
+
* Create a ConversionContext for testing or manual use.
|
|
68
|
+
*/
|
|
69
|
+
declare function createConversionContext(checker: ts.TypeChecker): ConversionContext;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Shared types for build system plugins.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Common transformer configuration that can be passed to build plugins.
|
|
77
|
+
*/
|
|
78
|
+
interface TransformerConfig {
|
|
79
|
+
/**
|
|
80
|
+
* Additional transformers to run before the Mirascope transformer.
|
|
81
|
+
*/
|
|
82
|
+
before?: ts.TransformerFactory<ts.SourceFile>[];
|
|
83
|
+
/**
|
|
84
|
+
* Additional transformers to run after the Mirascope transformer.
|
|
85
|
+
*/
|
|
86
|
+
after?: ts.TransformerFactory<ts.SourceFile>[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { type TransformerConfig, createConversionContext, createToolSchemaTransformer, transformer, typeToJsonSchema, typeToToolParameterSchema };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { mirascope as esbuildPlugin } from '../chunk-2R5IW35Y.js';
|
|
2
|
+
export { mirascope as vitePlugin } from '../chunk-RMNCGJYW.js';
|
|
3
|
+
export { createConversionContext, createToolSchemaTransformer, transformer, typeToJsonSchema, typeToToolParameterSchema } from '../chunk-U4MFJ4DP.js';
|
|
4
|
+
import '../chunk-NSBPE2FW.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var fs = require('fs');
|
|
6
|
+
var path = require('path');
|
|
7
|
+
var ts3 = require('typescript');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
12
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
13
|
+
var ts3__default = /*#__PURE__*/_interopDefault(ts3);
|
|
14
|
+
|
|
15
|
+
// src/transform/plugins/esbuild.ts
|
|
16
|
+
function isArrayLikeType(type, checker) {
|
|
17
|
+
if (type.getFlags() & ts3__default.default.TypeFlags.Object) {
|
|
18
|
+
const objectType = type;
|
|
19
|
+
const objectFlags = objectType.objectFlags;
|
|
20
|
+
if (objectFlags & ts3__default.default.ObjectFlags.Reference) {
|
|
21
|
+
const typeRef = type;
|
|
22
|
+
const symbol = typeRef.symbol;
|
|
23
|
+
if (symbol && symbol.getName() === "Array") {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
const target = typeRef.target;
|
|
27
|
+
if (target && target.symbol && target.symbol.getName() === "Array") {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const typeString = checker.typeToString(type);
|
|
33
|
+
if (typeString.endsWith("[]") || typeString.startsWith("Array<")) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
function typeToJsonSchema(type, ctx) {
|
|
39
|
+
const checker = ctx.checker;
|
|
40
|
+
if (type.isUnion()) {
|
|
41
|
+
return handleUnionType(type, ctx);
|
|
42
|
+
}
|
|
43
|
+
if (type.isIntersection()) {
|
|
44
|
+
return handleIntersectionType(type, ctx);
|
|
45
|
+
}
|
|
46
|
+
if (type.isLiteral()) {
|
|
47
|
+
return handleLiteralType(type);
|
|
48
|
+
}
|
|
49
|
+
const flags = type.getFlags();
|
|
50
|
+
if (flags & ts3__default.default.TypeFlags.BooleanLiteral) {
|
|
51
|
+
const intrinsicName = type.intrinsicName;
|
|
52
|
+
const isTrue = intrinsicName === "true";
|
|
53
|
+
return { type: "boolean", enum: [isTrue] };
|
|
54
|
+
}
|
|
55
|
+
if (flags & ts3__default.default.TypeFlags.String) {
|
|
56
|
+
return { type: "string" };
|
|
57
|
+
}
|
|
58
|
+
if (flags & ts3__default.default.TypeFlags.Number) {
|
|
59
|
+
return { type: "number" };
|
|
60
|
+
}
|
|
61
|
+
if (flags & ts3__default.default.TypeFlags.Null) {
|
|
62
|
+
return { type: "null" };
|
|
63
|
+
}
|
|
64
|
+
if (flags & ts3__default.default.TypeFlags.Undefined) {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
if (checker.isArrayType(type) || isArrayLikeType(type, checker)) {
|
|
68
|
+
const typeRef = type;
|
|
69
|
+
const typeArgs = checker.getTypeArguments(typeRef);
|
|
70
|
+
if (typeArgs && typeArgs.length > 0) {
|
|
71
|
+
return {
|
|
72
|
+
type: "array",
|
|
73
|
+
items: typeToJsonSchema(typeArgs[0], ctx)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return { type: "array" };
|
|
77
|
+
}
|
|
78
|
+
if (flags & ts3__default.default.TypeFlags.Object) {
|
|
79
|
+
return handleObjectType(type, ctx);
|
|
80
|
+
}
|
|
81
|
+
return {};
|
|
82
|
+
}
|
|
83
|
+
function handleUnionType(type, ctx) {
|
|
84
|
+
const types = type.types;
|
|
85
|
+
const nonUndefinedTypes = types.filter(
|
|
86
|
+
(t) => !(t.getFlags() & ts3__default.default.TypeFlags.Undefined)
|
|
87
|
+
);
|
|
88
|
+
if (nonUndefinedTypes.length === 2 && nonUndefinedTypes.every((t) => t.getFlags() & ts3__default.default.TypeFlags.BooleanLiteral)) {
|
|
89
|
+
return { type: "boolean" };
|
|
90
|
+
}
|
|
91
|
+
if (nonUndefinedTypes.every(
|
|
92
|
+
(t) => t.isStringLiteral()
|
|
93
|
+
)) {
|
|
94
|
+
return {
|
|
95
|
+
type: "string",
|
|
96
|
+
enum: nonUndefinedTypes.map((t) => t.value)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (nonUndefinedTypes.every(
|
|
100
|
+
(t) => t.isNumberLiteral()
|
|
101
|
+
)) {
|
|
102
|
+
return {
|
|
103
|
+
type: "number",
|
|
104
|
+
enum: nonUndefinedTypes.map((t) => t.value)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (nonUndefinedTypes.length === 1) {
|
|
108
|
+
return typeToJsonSchema(nonUndefinedTypes[0], ctx);
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
oneOf: nonUndefinedTypes.map((t) => typeToJsonSchema(t, ctx))
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function handleIntersectionType(type, ctx) {
|
|
115
|
+
const allOf = type.types.map((t) => typeToJsonSchema(t, ctx));
|
|
116
|
+
if (allOf.every((s) => s.type === "object")) {
|
|
117
|
+
const merged = {
|
|
118
|
+
type: "object",
|
|
119
|
+
properties: {},
|
|
120
|
+
required: []
|
|
121
|
+
};
|
|
122
|
+
for (const schema of allOf) {
|
|
123
|
+
if (schema.properties) {
|
|
124
|
+
merged.properties = { ...merged.properties, ...schema.properties };
|
|
125
|
+
}
|
|
126
|
+
if (schema.required) {
|
|
127
|
+
merged.required = [
|
|
128
|
+
...merged.required,
|
|
129
|
+
...schema.required
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return merged;
|
|
134
|
+
}
|
|
135
|
+
return { allOf };
|
|
136
|
+
}
|
|
137
|
+
function handleLiteralType(type) {
|
|
138
|
+
if (type.isStringLiteral()) {
|
|
139
|
+
return { type: "string", enum: [type.value] };
|
|
140
|
+
}
|
|
141
|
+
if (type.isNumberLiteral()) {
|
|
142
|
+
return { type: "number", enum: [type.value] };
|
|
143
|
+
}
|
|
144
|
+
return {};
|
|
145
|
+
}
|
|
146
|
+
function handleObjectType(type, ctx) {
|
|
147
|
+
const checker = ctx.checker;
|
|
148
|
+
const properties = {};
|
|
149
|
+
const required = [];
|
|
150
|
+
const props = checker.getPropertiesOfType(type);
|
|
151
|
+
for (const prop of props) {
|
|
152
|
+
const propName = prop.getName();
|
|
153
|
+
const propType = checker.getTypeOfSymbol(prop);
|
|
154
|
+
const isOptional = (prop.getFlags() & ts3__default.default.SymbolFlags.Optional) !== 0;
|
|
155
|
+
const propSchema = typeToJsonSchema(propType, ctx);
|
|
156
|
+
const jsDocComment = prop.getDocumentationComment(checker);
|
|
157
|
+
if (jsDocComment.length > 0) {
|
|
158
|
+
const description = jsDocComment.map((c) => c.text).join("");
|
|
159
|
+
propSchema.description = description;
|
|
160
|
+
}
|
|
161
|
+
properties[propName] = propSchema;
|
|
162
|
+
if (!isOptional) {
|
|
163
|
+
required.push(propName);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
type: "object",
|
|
168
|
+
properties,
|
|
169
|
+
required: required.length > 0 ? required : void 0
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function typeToToolParameterSchema(type, checker) {
|
|
173
|
+
const ctx = {
|
|
174
|
+
checker,
|
|
175
|
+
definitions: /* @__PURE__ */ new Map()};
|
|
176
|
+
const schema = typeToJsonSchema(type, ctx);
|
|
177
|
+
if (schema.type !== "object") {
|
|
178
|
+
throw new Error(
|
|
179
|
+
"Tool parameter type must be an object type, got: " + JSON.stringify(schema)
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
const result = {
|
|
183
|
+
type: "object",
|
|
184
|
+
properties: schema.properties ?? {},
|
|
185
|
+
required: schema.required ?? [],
|
|
186
|
+
additionalProperties: false
|
|
187
|
+
};
|
|
188
|
+
if (ctx.definitions.size > 0) {
|
|
189
|
+
result.$defs = Object.fromEntries(ctx.definitions);
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/transform/transformer.ts
|
|
195
|
+
var TOOL_FUNCTION_NAMES = /* @__PURE__ */ new Set(["defineTool", "defineContextTool"]);
|
|
196
|
+
var FORMAT_FUNCTION_NAMES = /* @__PURE__ */ new Set(["defineFormat"]);
|
|
197
|
+
function createToolSchemaTransformer(program) {
|
|
198
|
+
const checker = program.getTypeChecker();
|
|
199
|
+
return (context) => {
|
|
200
|
+
return (sourceFile) => {
|
|
201
|
+
const visitor = (node) => {
|
|
202
|
+
if (ts3__default.default.isCallExpression(node)) {
|
|
203
|
+
const toolTransformed = tryTransformToolCall(node, checker, context);
|
|
204
|
+
if (toolTransformed) {
|
|
205
|
+
return toolTransformed;
|
|
206
|
+
}
|
|
207
|
+
const formatTransformed = tryTransformFormatCall(
|
|
208
|
+
node,
|
|
209
|
+
checker,
|
|
210
|
+
context
|
|
211
|
+
);
|
|
212
|
+
if (formatTransformed) {
|
|
213
|
+
return formatTransformed;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return ts3__default.default.visitEachChild(node, visitor, context);
|
|
217
|
+
};
|
|
218
|
+
return ts3__default.default.visitNode(sourceFile, visitor);
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function tryTransformToolCall(node, checker, context) {
|
|
223
|
+
const functionName = getFunctionName(node);
|
|
224
|
+
if (!functionName || !TOOL_FUNCTION_NAMES.has(functionName)) {
|
|
225
|
+
return void 0;
|
|
226
|
+
}
|
|
227
|
+
const typeArgs = node.typeArguments;
|
|
228
|
+
if (!typeArgs || typeArgs.length === 0) {
|
|
229
|
+
return void 0;
|
|
230
|
+
}
|
|
231
|
+
const argsTypeNode = typeArgs[0];
|
|
232
|
+
const argsType = checker.getTypeFromTypeNode(argsTypeNode);
|
|
233
|
+
let schema;
|
|
234
|
+
try {
|
|
235
|
+
schema = typeToToolParameterSchema(argsType, checker);
|
|
236
|
+
} catch {
|
|
237
|
+
return void 0;
|
|
238
|
+
}
|
|
239
|
+
const args = node.arguments;
|
|
240
|
+
const firstArg = args[0];
|
|
241
|
+
if (!firstArg || !ts3__default.default.isObjectLiteralExpression(firstArg)) {
|
|
242
|
+
return void 0;
|
|
243
|
+
}
|
|
244
|
+
const optionsObject = firstArg;
|
|
245
|
+
const hasSchema = optionsObject.properties.some(
|
|
246
|
+
(prop) => ts3__default.default.isPropertyAssignment(prop) && ts3__default.default.isIdentifier(prop.name) && prop.name.text === "__schema"
|
|
247
|
+
);
|
|
248
|
+
if (hasSchema) {
|
|
249
|
+
return void 0;
|
|
250
|
+
}
|
|
251
|
+
const schemaProperty = createSchemaProperty(schema, context.factory);
|
|
252
|
+
const newProperties = [...optionsObject.properties, schemaProperty];
|
|
253
|
+
const newOptionsObject = context.factory.updateObjectLiteralExpression(
|
|
254
|
+
optionsObject,
|
|
255
|
+
newProperties
|
|
256
|
+
);
|
|
257
|
+
const newArgs = [newOptionsObject, ...args.slice(1)];
|
|
258
|
+
return context.factory.updateCallExpression(
|
|
259
|
+
node,
|
|
260
|
+
node.expression,
|
|
261
|
+
node.typeArguments,
|
|
262
|
+
newArgs
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
function tryTransformFormatCall(node, checker, context) {
|
|
266
|
+
const functionName = getFunctionName(node);
|
|
267
|
+
if (!functionName || !FORMAT_FUNCTION_NAMES.has(functionName)) {
|
|
268
|
+
return void 0;
|
|
269
|
+
}
|
|
270
|
+
const typeArgs = node.typeArguments;
|
|
271
|
+
if (!typeArgs || typeArgs.length === 0) {
|
|
272
|
+
return void 0;
|
|
273
|
+
}
|
|
274
|
+
const outputTypeNode = typeArgs[0];
|
|
275
|
+
const outputType = checker.getTypeFromTypeNode(outputTypeNode);
|
|
276
|
+
const args = node.arguments;
|
|
277
|
+
const firstArg = args[0];
|
|
278
|
+
if (!firstArg) {
|
|
279
|
+
return void 0;
|
|
280
|
+
}
|
|
281
|
+
if (ts3__default.default.isObjectLiteralExpression(firstArg)) {
|
|
282
|
+
const formatSpecObject = firstArg;
|
|
283
|
+
const hasSchema = formatSpecObject.properties.some(
|
|
284
|
+
(prop) => ts3__default.default.isPropertyAssignment(prop) && ts3__default.default.isIdentifier(prop.name) && prop.name.text === "__schema"
|
|
285
|
+
);
|
|
286
|
+
if (hasSchema) {
|
|
287
|
+
return void 0;
|
|
288
|
+
}
|
|
289
|
+
let schema;
|
|
290
|
+
try {
|
|
291
|
+
schema = typeToToolParameterSchema(outputType, checker);
|
|
292
|
+
} catch {
|
|
293
|
+
return void 0;
|
|
294
|
+
}
|
|
295
|
+
const schemaProperty = createSchemaProperty(schema, context.factory);
|
|
296
|
+
const newProperties = [...formatSpecObject.properties, schemaProperty];
|
|
297
|
+
const newFormatSpecObject = context.factory.updateObjectLiteralExpression(
|
|
298
|
+
formatSpecObject,
|
|
299
|
+
newProperties
|
|
300
|
+
);
|
|
301
|
+
const newArgs = [newFormatSpecObject, ...args.slice(1)];
|
|
302
|
+
return context.factory.updateCallExpression(
|
|
303
|
+
node,
|
|
304
|
+
node.expression,
|
|
305
|
+
node.typeArguments,
|
|
306
|
+
newArgs
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
return void 0;
|
|
310
|
+
}
|
|
311
|
+
function getFunctionName(node) {
|
|
312
|
+
const expression = node.expression;
|
|
313
|
+
if (ts3__default.default.isIdentifier(expression)) {
|
|
314
|
+
return expression.text;
|
|
315
|
+
}
|
|
316
|
+
if (ts3__default.default.isPropertyAccessExpression(expression)) {
|
|
317
|
+
return expression.name.text;
|
|
318
|
+
}
|
|
319
|
+
return void 0;
|
|
320
|
+
}
|
|
321
|
+
function createSchemaProperty(schema, factory) {
|
|
322
|
+
return factory.createPropertyAssignment(
|
|
323
|
+
factory.createIdentifier("__schema"),
|
|
324
|
+
jsonToAst(schema, factory)
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
function jsonToAst(value, factory) {
|
|
328
|
+
if (value === null) {
|
|
329
|
+
return factory.createNull();
|
|
330
|
+
}
|
|
331
|
+
if (value === void 0) {
|
|
332
|
+
return factory.createIdentifier("undefined");
|
|
333
|
+
}
|
|
334
|
+
if (typeof value === "string") {
|
|
335
|
+
return factory.createStringLiteral(value);
|
|
336
|
+
}
|
|
337
|
+
if (typeof value === "number") {
|
|
338
|
+
return factory.createNumericLiteral(value);
|
|
339
|
+
}
|
|
340
|
+
if (typeof value === "boolean") {
|
|
341
|
+
return value ? factory.createTrue() : factory.createFalse();
|
|
342
|
+
}
|
|
343
|
+
if (Array.isArray(value)) {
|
|
344
|
+
return factory.createArrayLiteralExpression(
|
|
345
|
+
value.map((item) => jsonToAst(item, factory))
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
if (typeof value === "object") {
|
|
349
|
+
const properties = Object.entries(value).map(
|
|
350
|
+
([key, val]) => factory.createPropertyAssignment(
|
|
351
|
+
factory.createIdentifier(key),
|
|
352
|
+
jsonToAst(val, factory)
|
|
353
|
+
)
|
|
354
|
+
);
|
|
355
|
+
return factory.createObjectLiteralExpression(properties, true);
|
|
356
|
+
}
|
|
357
|
+
return factory.createIdentifier("undefined");
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// src/transform/plugins/esbuild.ts
|
|
361
|
+
function mirascope(options = {}) {
|
|
362
|
+
const {
|
|
363
|
+
tsconfig = "./tsconfig.json",
|
|
364
|
+
filter = /\.(ts|tsx)$/,
|
|
365
|
+
compilerOptions: extraOptions = {}
|
|
366
|
+
} = options;
|
|
367
|
+
let cachedProgram;
|
|
368
|
+
let cachedTransformer;
|
|
369
|
+
let cachedConfigPath;
|
|
370
|
+
let cachedCompilerOptions;
|
|
371
|
+
return {
|
|
372
|
+
name: "mirascope",
|
|
373
|
+
setup(build) {
|
|
374
|
+
const cwd = process.cwd();
|
|
375
|
+
const configPath = path__default.default.resolve(cwd, tsconfig);
|
|
376
|
+
let parsedConfig;
|
|
377
|
+
if (fs__default.default.existsSync(configPath)) {
|
|
378
|
+
const configFile = ts3__default.default.readConfigFile(
|
|
379
|
+
configPath,
|
|
380
|
+
(path2) => ts3__default.default.sys.readFile(path2)
|
|
381
|
+
);
|
|
382
|
+
if (configFile.error) {
|
|
383
|
+
throw new Error(
|
|
384
|
+
`Error reading tsconfig.json: ${ts3__default.default.flattenDiagnosticMessageText(
|
|
385
|
+
configFile.error.messageText,
|
|
386
|
+
"\n"
|
|
387
|
+
)}`
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
parsedConfig = ts3__default.default.parseJsonConfigFileContent(
|
|
391
|
+
configFile.config,
|
|
392
|
+
ts3__default.default.sys,
|
|
393
|
+
path__default.default.dirname(configPath)
|
|
394
|
+
);
|
|
395
|
+
} else {
|
|
396
|
+
parsedConfig = ts3__default.default.parseJsonConfigFileContent({}, ts3__default.default.sys, cwd);
|
|
397
|
+
}
|
|
398
|
+
const compilerOptions = {
|
|
399
|
+
...parsedConfig.options,
|
|
400
|
+
...extraOptions,
|
|
401
|
+
// Required for transformation
|
|
402
|
+
noEmit: false,
|
|
403
|
+
sourceMap: false,
|
|
404
|
+
inlineSourceMap: false,
|
|
405
|
+
declaration: false,
|
|
406
|
+
declarationMap: false
|
|
407
|
+
};
|
|
408
|
+
build.onLoad(
|
|
409
|
+
{ filter },
|
|
410
|
+
async (args) => {
|
|
411
|
+
const fileName = args.path;
|
|
412
|
+
const sourceText = await fs__default.default.promises.readFile(fileName, "utf-8");
|
|
413
|
+
if (!sourceText.includes("defineTool") && !sourceText.includes("defineContextTool")) {
|
|
414
|
+
return {
|
|
415
|
+
contents: sourceText,
|
|
416
|
+
loader: fileName.endsWith(".tsx") ? "tsx" : "ts"
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
if (!cachedProgram || cachedConfigPath !== configPath || cachedCompilerOptions !== compilerOptions) {
|
|
420
|
+
cachedProgram = ts3__default.default.createProgram([fileName], compilerOptions);
|
|
421
|
+
cachedTransformer = createToolSchemaTransformer(cachedProgram);
|
|
422
|
+
cachedConfigPath = configPath;
|
|
423
|
+
cachedCompilerOptions = compilerOptions;
|
|
424
|
+
}
|
|
425
|
+
const sourceFile = cachedProgram.getSourceFile(fileName);
|
|
426
|
+
if (!sourceFile) {
|
|
427
|
+
const singleFileProgram = ts3__default.default.createProgram(
|
|
428
|
+
[fileName],
|
|
429
|
+
compilerOptions
|
|
430
|
+
);
|
|
431
|
+
const transformer = createToolSchemaTransformer(singleFileProgram);
|
|
432
|
+
const sf = singleFileProgram.getSourceFile(fileName);
|
|
433
|
+
if (!sf) {
|
|
434
|
+
return {
|
|
435
|
+
errors: [
|
|
436
|
+
{
|
|
437
|
+
text: `Could not parse source file: ${fileName}`,
|
|
438
|
+
location: null
|
|
439
|
+
}
|
|
440
|
+
]
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
const result2 = ts3__default.default.transform(sf, [transformer]);
|
|
444
|
+
const transformedSourceFile2 = result2.transformed[0];
|
|
445
|
+
const printer2 = ts3__default.default.createPrinter();
|
|
446
|
+
const outputText2 = transformedSourceFile2 ? printer2.printFile(transformedSourceFile2) : sourceText;
|
|
447
|
+
result2.dispose();
|
|
448
|
+
return {
|
|
449
|
+
contents: outputText2,
|
|
450
|
+
loader: fileName.endsWith(".tsx") ? "tsx" : "ts"
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
const result = ts3__default.default.transform(sourceFile, [cachedTransformer]);
|
|
454
|
+
const transformedSourceFile = result.transformed[0];
|
|
455
|
+
const printer = ts3__default.default.createPrinter();
|
|
456
|
+
const outputText = transformedSourceFile ? printer.printFile(transformedSourceFile) : sourceText;
|
|
457
|
+
result.dispose();
|
|
458
|
+
return {
|
|
459
|
+
contents: outputText,
|
|
460
|
+
loader: fileName.endsWith(".tsx") ? "tsx" : "ts"
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
var esbuild_default = mirascope;
|
|
468
|
+
|
|
469
|
+
exports.default = esbuild_default;
|
|
470
|
+
exports.mirascope = mirascope;
|
|
471
|
+
//# sourceMappingURL=esbuild.cjs.map
|
|
472
|
+
//# sourceMappingURL=esbuild.cjs.map
|