alchemy 0.1.11 → 0.1.12
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 +324 -0
- package/lib/apply.d.ts +2 -2
- package/lib/apply.d.ts.map +1 -1
- package/lib/apply.js +2 -2
- package/lib/apply.js.map +1 -1
- package/lib/aws/index.d.ts +1 -0
- package/lib/aws/index.d.ts.map +1 -1
- package/lib/aws/index.js +1 -0
- package/lib/aws/index.js.map +1 -1
- package/lib/aws/ses.d.ts +75 -0
- package/lib/aws/ses.d.ts.map +1 -0
- package/lib/aws/ses.js +197 -0
- package/lib/aws/ses.js.map +1 -0
- package/lib/cloudflare/api.d.ts +84 -0
- package/lib/cloudflare/api.d.ts.map +1 -0
- package/lib/cloudflare/api.js +178 -0
- package/lib/cloudflare/api.js.map +1 -0
- package/lib/cloudflare/asset-manifest.d.ts +15 -0
- package/lib/cloudflare/asset-manifest.d.ts.map +1 -0
- package/lib/cloudflare/asset-manifest.js +2 -0
- package/lib/cloudflare/asset-manifest.js.map +1 -0
- package/lib/cloudflare/auth.d.ts +52 -0
- package/lib/cloudflare/auth.d.ts.map +1 -0
- package/lib/cloudflare/auth.js +114 -0
- package/lib/cloudflare/auth.js.map +1 -0
- package/lib/cloudflare/bindings.d.ts +244 -0
- package/lib/cloudflare/bindings.d.ts.map +1 -0
- package/lib/cloudflare/bindings.js +7 -0
- package/lib/cloudflare/bindings.js.map +1 -0
- package/lib/cloudflare/generate-asset-manifest.d.ts +3 -0
- package/lib/cloudflare/generate-asset-manifest.d.ts.map +1 -0
- package/lib/cloudflare/generate-asset-manifest.js +69 -0
- package/lib/cloudflare/generate-asset-manifest.js.map +1 -0
- package/lib/cloudflare/index.d.ts +6 -0
- package/lib/cloudflare/index.d.ts.map +1 -0
- package/lib/cloudflare/index.js +6 -0
- package/lib/cloudflare/index.js.map +1 -0
- package/lib/cloudflare/kv-namespace.d.ts +61 -0
- package/lib/cloudflare/kv-namespace.d.ts.map +1 -0
- package/lib/cloudflare/kv-namespace.js +93 -0
- package/lib/cloudflare/kv-namespace.js.map +1 -0
- package/lib/cloudflare/state.d.ts +83 -0
- package/lib/cloudflare/state.d.ts.map +1 -0
- package/lib/cloudflare/state.js +109 -0
- package/lib/cloudflare/state.js.map +1 -0
- package/lib/cloudflare/static-site-router.d.ts +10 -0
- package/lib/cloudflare/static-site-router.d.ts.map +1 -0
- package/lib/cloudflare/static-site-router.js +88 -0
- package/lib/cloudflare/static-site-router.js.map +1 -0
- package/lib/cloudflare/static-site.d.ts +166 -0
- package/lib/cloudflare/static-site.d.ts.map +1 -0
- package/lib/cloudflare/static-site.js +152 -0
- package/lib/cloudflare/static-site.js.map +1 -0
- package/lib/cloudflare/types.d.ts +52 -0
- package/lib/cloudflare/types.d.ts.map +1 -0
- package/lib/cloudflare/types.js +21 -0
- package/lib/cloudflare/types.js.map +1 -0
- package/lib/cloudflare/upload-asset-manifest.d.ts +12 -0
- package/lib/cloudflare/upload-asset-manifest.d.ts.map +1 -0
- package/lib/cloudflare/upload-asset-manifest.js +55 -0
- package/lib/cloudflare/upload-asset-manifest.js.map +1 -0
- package/lib/cloudflare/worker.d.ts +87 -0
- package/lib/cloudflare/worker.d.ts.map +1 -0
- package/lib/cloudflare/worker.js +364 -0
- package/lib/cloudflare/worker.js.map +1 -0
- package/lib/esbuild.d.ts +82 -0
- package/lib/esbuild.d.ts.map +1 -1
- package/lib/esbuild.js +24 -17
- package/lib/esbuild.js.map +1 -1
- package/lib/fs.d.ts +1 -1
- package/lib/global.d.ts.map +1 -1
- package/lib/global.js +3 -1
- package/lib/global.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/main.js +1 -1
- package/lib/main.js.map +1 -1
- package/lib/output.d.ts +17 -13
- package/lib/output.d.ts.map +1 -1
- package/lib/output.js +2 -2
- package/lib/output.js.map +1 -1
- package/lib/resource.d.ts +3 -2
- package/lib/resource.d.ts.map +1 -1
- package/lib/resource.js +2 -2
- package/lib/resource.js.map +1 -1
- package/lib/utils/content-type.d.ts +8 -0
- package/lib/utils/content-type.d.ts.map +1 -0
- package/lib/utils/content-type.js +41 -0
- package/lib/utils/content-type.js.map +1 -0
- package/package.json +26 -8
- package/src/apply.ts +5 -5
- package/src/aws/index.ts +1 -0
- package/src/aws/ses.ts +346 -0
- package/src/cloudflare/api.ts +276 -0
- package/src/cloudflare/asset-manifest.ts +16 -0
- package/src/cloudflare/auth.ts +187 -0
- package/src/cloudflare/bindings.ts +286 -0
- package/src/cloudflare/generate-asset-manifest.ts +93 -0
- package/src/cloudflare/index.ts +5 -0
- package/src/cloudflare/kv-namespace.ts +195 -0
- package/src/cloudflare/state.ts +138 -0
- package/src/cloudflare/static-site-router.ts +108 -0
- package/src/cloudflare/static-site.ts +379 -0
- package/src/cloudflare/types.ts +71 -0
- package/src/cloudflare/upload-asset-manifest.ts +83 -0
- package/src/cloudflare/worker.ts +568 -0
- package/src/esbuild.ts +31 -22
- package/src/global.ts +1 -0
- package/src/index.ts +0 -1
- package/src/main.ts +1 -1
- package/src/output.ts +32 -32
- package/src/resource.ts +11 -5
- package/src/utils/content-type.ts +42 -0
- package/lib/$.d.ts +0 -7
- package/lib/$.d.ts.map +0 -1
- package/lib/$.js +0 -31
- package/lib/$.js.map +0 -1
- package/lib/agent/ai.d.ts +0 -40
- package/lib/agent/ai.d.ts.map +0 -1
- package/lib/agent/ai.js +0 -132
- package/lib/agent/ai.js.map +0 -1
- package/lib/agent/anthropic.d.ts +0 -9
- package/lib/agent/anthropic.d.ts.map +0 -1
- package/lib/agent/anthropic.js +0 -25
- package/lib/agent/anthropic.js.map +0 -1
- package/lib/agent/dependencies.d.ts +0 -4
- package/lib/agent/dependencies.d.ts.map +0 -1
- package/lib/agent/dependencies.js +0 -48
- package/lib/agent/dependencies.js.map +0 -1
- package/lib/agent/file-context.d.ts +0 -13
- package/lib/agent/file-context.d.ts.map +0 -1
- package/lib/agent/file-context.js +0 -6
- package/lib/agent/file-context.js.map +0 -1
- package/lib/agent/index.d.ts +0 -10
- package/lib/agent/index.d.ts.map +0 -1
- package/lib/agent/index.js +0 -10
- package/lib/agent/index.js.map +0 -1
- package/lib/agent/model.d.ts +0 -8
- package/lib/agent/model.d.ts.map +0 -1
- package/lib/agent/model.js +0 -29
- package/lib/agent/model.js.map +0 -1
- package/lib/agent/module.d.ts +0 -81
- package/lib/agent/module.d.ts.map +0 -1
- package/lib/agent/module.js +0 -309
- package/lib/agent/module.js.map +0 -1
- package/lib/agent/openai.d.ts +0 -6
- package/lib/agent/openai.d.ts.map +0 -1
- package/lib/agent/openai.js +0 -41
- package/lib/agent/openai.js.map +0 -1
- package/lib/agent/program.d.ts +0 -12
- package/lib/agent/program.d.ts.map +0 -1
- package/lib/agent/program.js +0 -21
- package/lib/agent/program.js.map +0 -1
- package/lib/agent/prompts.d.ts +0 -4
- package/lib/agent/prompts.d.ts.map +0 -1
- package/lib/agent/prompts.js +0 -11
- package/lib/agent/prompts.js.map +0 -1
- package/lib/agent/scrape.d.ts +0 -71
- package/lib/agent/scrape.d.ts.map +0 -1
- package/lib/agent/scrape.js +0 -45
- package/lib/agent/scrape.js.map +0 -1
- package/lib/aws/auto/docs.d.ts +0 -11
- package/lib/aws/auto/docs.d.ts.map +0 -1
- package/lib/aws/auto/docs.js +0 -56
- package/lib/aws/auto/docs.js.map +0 -1
- package/lib/aws/auto/index.d.ts +0 -5
- package/lib/aws/auto/index.d.ts.map +0 -1
- package/lib/aws/auto/index.js +0 -5
- package/lib/aws/auto/index.js.map +0 -1
- package/lib/aws/auto/resource.d.ts +0 -24
- package/lib/aws/auto/resource.d.ts.map +0 -1
- package/lib/aws/auto/resource.js +0 -194
- package/lib/aws/auto/resource.js.map +0 -1
- package/lib/aws/auto/service.d.ts +0 -10
- package/lib/aws/auto/service.d.ts.map +0 -1
- package/lib/aws/auto/service.js +0 -34
- package/lib/aws/auto/service.js.map +0 -1
- package/lib/aws/auto/spec.d.ts +0 -83
- package/lib/aws/auto/spec.d.ts.map +0 -1
- package/lib/aws/auto/spec.js +0 -58
- package/lib/aws/auto/spec.js.map +0 -1
- package/lib/markdown/design.d.ts +0 -17
- package/lib/markdown/design.d.ts.map +0 -1
- package/lib/markdown/design.js +0 -92
- package/lib/markdown/design.js.map +0 -1
- package/lib/markdown/extract.d.ts +0 -7
- package/lib/markdown/extract.d.ts.map +0 -1
- package/lib/markdown/extract.js +0 -10
- package/lib/markdown/extract.js.map +0 -1
- package/lib/markdown/index.d.ts +0 -4
- package/lib/markdown/index.d.ts.map +0 -1
- package/lib/markdown/index.js +0 -4
- package/lib/markdown/index.js.map +0 -1
- package/lib/markdown/requirements.d.ts +0 -79
- package/lib/markdown/requirements.d.ts.map +0 -1
- package/lib/markdown/requirements.js +0 -105
- package/lib/markdown/requirements.js.map +0 -1
- package/lib/typescript/check-omission.d.ts +0 -2
- package/lib/typescript/check-omission.d.ts.map +0 -1
- package/lib/typescript/check-omission.js +0 -43
- package/lib/typescript/check-omission.js.map +0 -1
- package/lib/typescript/debug-type-errors.d.ts +0 -3
- package/lib/typescript/debug-type-errors.d.ts.map +0 -1
- package/lib/typescript/debug-type-errors.js +0 -35
- package/lib/typescript/debug-type-errors.js.map +0 -1
- package/lib/typescript/extract.d.ts +0 -7
- package/lib/typescript/extract.d.ts.map +0 -1
- package/lib/typescript/extract.js +0 -10
- package/lib/typescript/extract.js.map +0 -1
- package/lib/typescript/index.d.ts +0 -7
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/index.js +0 -7
- package/lib/typescript/index.js.map +0 -1
- package/lib/typescript/install-dependencies.d.ts +0 -29
- package/lib/typescript/install-dependencies.d.ts.map +0 -1
- package/lib/typescript/install-dependencies.js +0 -52
- package/lib/typescript/install-dependencies.js.map +0 -1
- package/lib/typescript/package.d.ts +0 -157
- package/lib/typescript/package.d.ts.map +0 -1
- package/lib/typescript/package.js +0 -115
- package/lib/typescript/package.js.map +0 -1
- package/lib/typescript/repair-omissions.d.ts +0 -3
- package/lib/typescript/repair-omissions.d.ts.map +0 -1
- package/lib/typescript/repair-omissions.js +0 -23
- package/lib/typescript/repair-omissions.js.map +0 -1
- package/lib/typescript/repair.d.ts +0 -3
- package/lib/typescript/repair.d.ts.map +0 -1
- package/lib/typescript/repair.js +0 -34
- package/lib/typescript/repair.js.map +0 -1
- package/lib/typescript/tsconfig.d.ts +0 -469
- package/lib/typescript/tsconfig.d.ts.map +0 -1
- package/lib/typescript/tsconfig.js +0 -143
- package/lib/typescript/tsconfig.js.map +0 -1
- package/lib/typescript/typescript.d.ts +0 -107
- package/lib/typescript/typescript.d.ts.map +0 -1
- package/lib/typescript/typescript.js +0 -180
- package/lib/typescript/typescript.js.map +0 -1
- package/lib/typescript/validate.d.ts +0 -10
- package/lib/typescript/validate.d.ts.map +0 -1
- package/lib/typescript/validate.js +0 -49
- package/lib/typescript/validate.js.map +0 -1
- package/src/$.ts +0 -36
- package/src/agent/ai.ts +0 -215
- package/src/agent/anthropic.ts +0 -31
- package/src/agent/dependencies.ts +0 -62
- package/src/agent/file-context.ts +0 -8
- package/src/agent/index.ts +0 -9
- package/src/agent/model.ts +0 -39
- package/src/agent/module.ts +0 -418
- package/src/agent/openai.ts +0 -50
- package/src/agent/program.md +0 -38
- package/src/agent/program.ts +0 -37
- package/src/agent/prompts.ts +0 -20
- package/src/agent/scrape.ts +0 -47
- package/src/aws/auto/cfn.json +0 -225310
- package/src/aws/auto/docs.ts +0 -73
- package/src/aws/auto/index.ts +0 -4
- package/src/aws/auto/resource.ts +0 -269
- package/src/aws/auto/service.ts +0 -51
- package/src/aws/auto/spec.ts +0 -160
- package/src/markdown/design.ts +0 -125
- package/src/markdown/extract.ts +0 -9
- package/src/markdown/index.ts +0 -3
- package/src/markdown/requirements.ts +0 -135
- package/src/typescript/check-omission.ts +0 -47
- package/src/typescript/debug-type-errors.ts +0 -42
- package/src/typescript/extract.ts +0 -9
- package/src/typescript/index.ts +0 -6
- package/src/typescript/install-dependencies.ts +0 -61
- package/src/typescript/package.ts +0 -143
- package/src/typescript/repair-omissions.ts +0 -31
- package/src/typescript/repair.ts +0 -45
- package/src/typescript/tsconfig.ts +0 -170
- package/src/typescript/typescript.ts +0 -217
- package/src/typescript/validate.ts +0 -73
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import type { CoreMessage } from "ai";
|
|
2
|
-
import { mkdir, writeFile } from "fs/promises";
|
|
3
|
-
import { dirname } from "path";
|
|
4
|
-
import { z } from "zod";
|
|
5
|
-
import { ModelId, generateText, resolveModel } from "../agent";
|
|
6
|
-
import { dependenciesAsMessages } from "../agent/dependencies";
|
|
7
|
-
import { FileContext } from "../agent/file-context";
|
|
8
|
-
import { rm } from "../fs";
|
|
9
|
-
import { type Context, Resource } from "../resource";
|
|
10
|
-
import { checkForCodeOmission } from "./check-omission";
|
|
11
|
-
import { debugTypeErrors } from "./debug-type-errors";
|
|
12
|
-
import { extractTypeScriptCode } from "./extract";
|
|
13
|
-
import { repairTypeScriptCode } from "./repair";
|
|
14
|
-
import { repairCodeOmissions } from "./repair-omissions";
|
|
15
|
-
import { validateTypeScript } from "./validate";
|
|
16
|
-
|
|
17
|
-
export type TypeScriptFileInput = z.infer<typeof TypeScriptFileInput>;
|
|
18
|
-
|
|
19
|
-
const TypeScriptFileInput = z.object({
|
|
20
|
-
/**
|
|
21
|
-
* The ID of the model to use for generating TypeScript code
|
|
22
|
-
* @default "gpt-4o"
|
|
23
|
-
*/
|
|
24
|
-
modelId: ModelId.optional(),
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The name of the file to write the code to
|
|
28
|
-
*/
|
|
29
|
-
path: z.string(),
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The requirements for the TypeScript file
|
|
33
|
-
*/
|
|
34
|
-
requirements: z.string(),
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* List of other code files that it depends on
|
|
38
|
-
*/
|
|
39
|
-
dependencies: z.array(FileContext).optional(),
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Temperature setting for model generation (higher = more creative, lower = more focused)
|
|
43
|
-
* @default 0.7
|
|
44
|
-
*/
|
|
45
|
-
temperature: z.number().optional(),
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Whether to perform TypeScript type checking on the generated code
|
|
49
|
-
* @default false
|
|
50
|
-
*/
|
|
51
|
-
typeCheck: z.boolean().optional(),
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Path to the tsconfig.json file to use for validation
|
|
55
|
-
* If not provided, will look for tsconfig.json in the project root
|
|
56
|
-
* Only used if typeCheck is true
|
|
57
|
-
*/
|
|
58
|
-
tsconfigPath: z.string().optional(),
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Project root directory. Used to resolve tsconfig.json and module imports
|
|
62
|
-
* If not provided, will use the directory of the target file
|
|
63
|
-
* Only used if typeCheck is true
|
|
64
|
-
*/
|
|
65
|
-
projectRoot: z.string().optional(),
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
export type TypeScriptFileOutput = z.infer<typeof TypeScriptFileOutput>;
|
|
69
|
-
export const TypeScriptFileOutput = FileContext;
|
|
70
|
-
|
|
71
|
-
export class TypeScriptFile extends Resource(
|
|
72
|
-
"code::TypeScriptFile",
|
|
73
|
-
async (ctx: Context<TypeScriptFileOutput>, props: TypeScriptFileInput) => {
|
|
74
|
-
if (ctx.event === "delete") {
|
|
75
|
-
await rm(props.path);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
console.log("Implementing", props.path);
|
|
80
|
-
|
|
81
|
-
// Get the appropriate model based on the ID
|
|
82
|
-
const model = await resolveModel(props.modelId ?? "gpt-4o");
|
|
83
|
-
|
|
84
|
-
const messages: CoreMessage[] = [
|
|
85
|
-
{
|
|
86
|
-
role: "system" as const,
|
|
87
|
-
content:
|
|
88
|
-
"You are an expert TypeScript developer that generates clean, well-documented code following best practices. " +
|
|
89
|
-
"Your response must follow this exact format:\n" +
|
|
90
|
-
"1. A single sentence describing what the code will do\n" +
|
|
91
|
-
"2. A single TypeScript code block surrounded by ```ts and ``` tags containing the COMPLETE implementation\n" +
|
|
92
|
-
"IMPORTANT: You must ALWAYS provide the ENTIRE file contents, including ALL imports, types, and implementations.\n" +
|
|
93
|
-
"NEVER output partial code or omit any functionality from the original file when making changes.\n" +
|
|
94
|
-
"Do not include any other explanations or multiple code blocks.",
|
|
95
|
-
},
|
|
96
|
-
...dependenciesAsMessages(props.dependencies),
|
|
97
|
-
{
|
|
98
|
-
role: "user" as const,
|
|
99
|
-
content: `Please generate TypeScript code based on the following specifications:
|
|
100
|
-
|
|
101
|
-
Requirements:
|
|
102
|
-
${props.requirements}
|
|
103
|
-
|
|
104
|
-
The code should:
|
|
105
|
-
1. Be well-documented with JSDoc comments
|
|
106
|
-
2. Follow TypeScript best practices
|
|
107
|
-
3. Include proper type definitions
|
|
108
|
-
4. Be ready to use with the provided context files
|
|
109
|
-
5. Include ALL necessary imports and dependencies
|
|
110
|
-
6. Contain the COMPLETE implementation with no omissions`,
|
|
111
|
-
},
|
|
112
|
-
];
|
|
113
|
-
|
|
114
|
-
const maxAttempts = 3;
|
|
115
|
-
let code: string | undefined;
|
|
116
|
-
let typeErrors: string | undefined;
|
|
117
|
-
|
|
118
|
-
// Try generating and validating code up to maxAttempts times
|
|
119
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
120
|
-
try {
|
|
121
|
-
// After 2 failed attempts, consult o3-mini for analysis
|
|
122
|
-
if (attempt === maxAttempts) {
|
|
123
|
-
if (!ctx.quiet) {
|
|
124
|
-
console.log("[TypeScript] Consulting expert for code analysis...");
|
|
125
|
-
}
|
|
126
|
-
const diagnosis = await debugTypeErrors(messages, typeErrors!);
|
|
127
|
-
|
|
128
|
-
messages.push({
|
|
129
|
-
role: "user",
|
|
130
|
-
content: diagnosis,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Generate the TypeScript code
|
|
135
|
-
if (!ctx.quiet) {
|
|
136
|
-
console.log(`[TypeScript] Generating code attempt ${attempt}...`);
|
|
137
|
-
}
|
|
138
|
-
const { text } = await generateText({
|
|
139
|
-
model,
|
|
140
|
-
temperature: Math.max(
|
|
141
|
-
0.1,
|
|
142
|
-
(props.temperature ?? 0.7) * (1 - attempt * 0.2),
|
|
143
|
-
),
|
|
144
|
-
messages,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
code = extractTypeScriptCode(text);
|
|
148
|
-
if (!code) {
|
|
149
|
-
console.log("Format mismatch");
|
|
150
|
-
messages.push(
|
|
151
|
-
{
|
|
152
|
-
role: "assistant" as const,
|
|
153
|
-
content: text,
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
role: "user" as const,
|
|
157
|
-
content:
|
|
158
|
-
"Your response did not match the required format. Please provide your response with exactly one sentence summary" +
|
|
159
|
-
" followed by a single TypeScript code block surrounded by ```ts and ``` tags containing the COMPLETE implementation. No other text or explanations.",
|
|
160
|
-
},
|
|
161
|
-
);
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// First check for omissions
|
|
166
|
-
if (!ctx.quiet) {
|
|
167
|
-
console.log("[TypeScript] Checking for code omissions...");
|
|
168
|
-
}
|
|
169
|
-
const hasOmissions = await checkForCodeOmission(code);
|
|
170
|
-
if (hasOmissions) {
|
|
171
|
-
code = await repairCodeOmissions(model, messages);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Ensure the directory exists
|
|
175
|
-
await mkdir(dirname(props.path), { recursive: true });
|
|
176
|
-
|
|
177
|
-
// Write the TypeScript file
|
|
178
|
-
await writeFile(props.path, code);
|
|
179
|
-
|
|
180
|
-
if (props.typeCheck) {
|
|
181
|
-
typeErrors = await validateTypeScript(props.path, {
|
|
182
|
-
tsconfigPath: props.tsconfigPath,
|
|
183
|
-
projectRoot: props.projectRoot,
|
|
184
|
-
});
|
|
185
|
-
if (typeErrors) {
|
|
186
|
-
if (attempt === maxAttempts) {
|
|
187
|
-
throw new Error(
|
|
188
|
-
`Failed to generate type-safe code after maximum attempts. Errors:\n${typeErrors}`,
|
|
189
|
-
);
|
|
190
|
-
} else {
|
|
191
|
-
code = await repairTypeScriptCode(model, typeErrors, messages);
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// If we get here, the code is valid
|
|
198
|
-
return {
|
|
199
|
-
path: props.path,
|
|
200
|
-
content: code,
|
|
201
|
-
};
|
|
202
|
-
} catch (error) {
|
|
203
|
-
// On the last attempt, rethrow the error
|
|
204
|
-
if (attempt === maxAttempts) {
|
|
205
|
-
throw error;
|
|
206
|
-
}
|
|
207
|
-
// Otherwise continue to the next attempt
|
|
208
|
-
if (!ctx.quiet) {
|
|
209
|
-
console.log(`Attempt ${attempt} failed:`, error);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// This should never be reached due to the throws above
|
|
215
|
-
throw new Error("Unexpected code generation failure");
|
|
216
|
-
},
|
|
217
|
-
) {}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { dirname, resolve } from "path";
|
|
2
|
-
import * as ts from "typescript";
|
|
3
|
-
|
|
4
|
-
export interface TypeCheckOptions {
|
|
5
|
-
tsconfigPath?: string;
|
|
6
|
-
projectRoot?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Validates TypeScript code and returns formatted error messages if there are any type errors
|
|
11
|
-
* @returns Formatted error message if there are errors, undefined if valid
|
|
12
|
-
*/
|
|
13
|
-
export async function validateTypeScript(
|
|
14
|
-
filePath: string,
|
|
15
|
-
options: TypeCheckOptions,
|
|
16
|
-
): Promise<string | undefined> {
|
|
17
|
-
const projectRoot = options.projectRoot ?? dirname(filePath);
|
|
18
|
-
const tsconfigPath =
|
|
19
|
-
options.tsconfigPath ?? resolve(projectRoot, "tsconfig.json");
|
|
20
|
-
|
|
21
|
-
// Load tsconfig.json if it exists
|
|
22
|
-
let compilerOptions: ts.CompilerOptions = {
|
|
23
|
-
target: ts.ScriptTarget.ESNext,
|
|
24
|
-
module: ts.ModuleKind.ESNext,
|
|
25
|
-
strict: true,
|
|
26
|
-
esModuleInterop: true,
|
|
27
|
-
skipLibCheck: true,
|
|
28
|
-
noEmit: true,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
if (ts.sys.fileExists(tsconfigPath)) {
|
|
32
|
-
const { config, error } = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
|
|
33
|
-
if (error) {
|
|
34
|
-
throw new Error(`Error reading tsconfig.json: ${error.messageText}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const { options: parsedOptions, errors } = ts.parseJsonConfigFileContent(
|
|
38
|
-
config,
|
|
39
|
-
ts.sys,
|
|
40
|
-
projectRoot,
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
if (errors.length) {
|
|
44
|
-
throw new Error(
|
|
45
|
-
`Error parsing tsconfig.json: ${errors.map((e) => e.messageText).join("\n")}`,
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
compilerOptions = parsedOptions;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Create program
|
|
53
|
-
const program = ts.createProgram({
|
|
54
|
-
rootNames: [filePath],
|
|
55
|
-
options: compilerOptions,
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Get diagnostics
|
|
59
|
-
const diagnostics = [
|
|
60
|
-
...program.getSemanticDiagnostics(),
|
|
61
|
-
...program.getSyntacticDiagnostics(),
|
|
62
|
-
];
|
|
63
|
-
|
|
64
|
-
if (diagnostics.length > 0) {
|
|
65
|
-
return ts.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
66
|
-
getCanonicalFileName: (path) => path,
|
|
67
|
-
getCurrentDirectory: () => projectRoot,
|
|
68
|
-
getNewLine: () => ts.sys.newLine,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|