alchemy 0.2.5 → 0.3.1
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 +3 -3
- package/lib/ai/approve.d.ts +99 -0
- package/lib/ai/approve.js +76 -0
- package/lib/ai/astro-file.d.ts +137 -0
- package/lib/ai/astro-file.js +156 -0
- package/lib/ai/client.d.ts +7 -8
- package/lib/ai/client.js +48 -19
- package/lib/ai/css-file.d.ts +131 -0
- package/lib/ai/css-file.js +141 -0
- package/lib/ai/data.d.ts +51 -6
- package/lib/ai/data.js +58 -12
- package/lib/ai/document.d.ts +108 -24
- package/lib/ai/document.js +166 -52
- package/lib/ai/html-file.d.ts +128 -0
- package/lib/ai/html-file.js +138 -0
- package/lib/ai/index.d.ts +9 -0
- package/lib/ai/index.js +9 -0
- package/lib/ai/json-file.d.ts +160 -0
- package/lib/ai/json-file.js +187 -0
- package/lib/ai/review.d.ts +122 -0
- package/lib/ai/review.js +101 -0
- package/lib/ai/typescript-file.d.ts +139 -0
- package/lib/ai/typescript-file.js +145 -0
- package/lib/ai/vue-file.d.ts +126 -0
- package/lib/ai/vue-file.js +136 -0
- package/lib/ai/yaml-file.d.ts +169 -0
- package/lib/ai/yaml-file.js +200 -0
- package/lib/alchemy.js +17 -2
- package/lib/apply.js +1 -1
- package/lib/cloudflare/dns.d.ts +88 -0
- package/lib/cloudflare/dns.js +192 -0
- package/lib/cloudflare/generate-asset-manifest.js +1 -1
- package/lib/cloudflare/index.d.ts +2 -1
- package/lib/cloudflare/index.js +2 -1
- package/lib/cloudflare/r2-rest-state-store.d.ts +125 -0
- package/lib/cloudflare/r2-rest-state-store.js +254 -0
- package/lib/cloudflare/response.d.ts +12 -0
- package/lib/cloudflare/response.js +0 -0
- package/lib/cloudflare/static-site-router.js +16 -7
- package/lib/cloudflare/static-site.js +3 -0
- package/lib/cloudflare/upload-asset-manifest.js +4 -2
- package/lib/cloudflare/zone.d.ts +18 -0
- package/lib/cloudflare/zone.js +17 -4
- package/lib/dns/godaddy.d.ts +7 -0
- package/lib/dns/godaddy.js +21 -0
- package/lib/dns/import-dns.d.ts +82 -0
- package/lib/dns/import-dns.js +114 -0
- package/lib/dns/index.d.ts +2 -0
- package/lib/dns/index.js +2 -0
- package/lib/dns/record.d.ts +88 -0
- package/lib/dns/record.js +13 -0
- package/lib/fs/copy-file.d.ts +54 -0
- package/lib/fs/copy-file.js +63 -0
- package/lib/fs/file-system-state-store.d.ts +17 -0
- package/lib/fs/file-system-state-store.js +87 -0
- package/lib/fs/file.d.ts +13 -0
- package/lib/fs/file.js +24 -5
- package/lib/fs/folder.d.ts +10 -0
- package/lib/fs/folder.js +2 -2
- package/lib/fs/index.d.ts +10 -4
- package/lib/fs/index.js +10 -4
- package/lib/fs/static-astro-file.d.ts +34 -0
- package/lib/fs/static-astro-file.js +39 -0
- package/lib/fs/static-css-file.d.ts +24 -0
- package/lib/fs/static-css-file.js +29 -0
- package/lib/fs/static-html-file.d.ts +31 -0
- package/lib/fs/static-html-file.js +36 -0
- package/lib/fs/{json-file.d.ts → static-json-file.d.ts} +3 -3
- package/lib/fs/static-json-file.js +15 -0
- package/lib/fs/{text-file.d.ts → static-text-file.d.ts} +2 -2
- package/lib/fs/static-text-file.js +8 -0
- package/lib/fs/{typescript-file.d.ts → static-typescript-file.d.ts} +3 -3
- package/lib/fs/{typescript-file.js → static-typescript-file.js} +3 -2
- package/lib/fs/static-vue-file.d.ts +28 -0
- package/lib/fs/static-vue-file.js +33 -0
- package/lib/fs/static-yaml-file.d.ts +22 -0
- package/lib/fs/{yaml-file.js → static-yaml-file.js} +3 -2
- package/lib/internal/getting-started.d.ts +21 -0
- package/lib/internal/getting-started.js +87 -0
- package/lib/internal/index.d.ts +3 -0
- package/lib/internal/index.js +3 -0
- package/lib/internal/providers.d.ts +27 -0
- package/lib/internal/providers.js +172 -0
- package/lib/internal/tutorial.d.ts +104 -0
- package/lib/internal/tutorial.js +251 -0
- package/lib/resource.js +8 -1
- package/lib/scope.d.ts +1 -1
- package/lib/scope.js +4 -2
- package/lib/state.d.ts +1 -16
- package/lib/state.js +0 -87
- package/lib/test/bun.d.ts +11 -0
- package/lib/test/bun.js +36 -8
- package/lib/web/astro.d.ts +147 -0
- package/lib/web/astro.js +414 -0
- package/lib/{shadcn/component.js → web/shadcn-component.js} +2 -9
- package/lib/web/shadcn.d.ts +97 -0
- package/lib/web/shadcn.js +99 -0
- package/lib/web/tailwind.d.ts +64 -0
- package/lib/web/tailwind.js +97 -0
- package/lib/{vite → web}/vite.d.ts +5 -0
- package/lib/{vite → web}/vite.js +44 -44
- package/lib/web/vitepress/config.d.ts +10 -0
- package/lib/web/vitepress/config.js +31 -0
- package/lib/web/vitepress/custom-theme.d.ts +173 -0
- package/lib/web/vitepress/custom-theme.js +336 -0
- package/lib/{vitepress → web/vitepress}/dependencies.d.ts +2 -2
- package/lib/{vitepress → web/vitepress}/dependencies.js +1 -1
- package/lib/web/vitepress/home-page.d.ts +246 -0
- package/lib/web/vitepress/home-page.js +147 -0
- package/lib/{vitepress → web/vitepress}/index.d.ts +2 -0
- package/lib/{vitepress → web/vitepress}/index.js +2 -0
- package/lib/{vitepress → web/vitepress}/vitepress.d.ts +15 -12
- package/lib/{vitepress → web/vitepress}/vitepress.js +26 -44
- package/package.json +6 -2
- package/src/ai/approve.ts +163 -0
- package/src/ai/astro-file.ts +269 -0
- package/src/ai/client.ts +60 -21
- package/src/ai/css-file.ts +247 -0
- package/src/ai/data.ts +81 -19
- package/src/ai/document.ts +240 -62
- package/src/ai/html-file.ts +244 -0
- package/src/ai/index.ts +9 -0
- package/src/ai/json-file.ts +325 -0
- package/src/ai/review.ts +213 -0
- package/src/ai/typescript-file.ts +259 -0
- package/src/ai/vue-file.ts +242 -0
- package/src/ai/yaml-file.ts +328 -0
- package/src/alchemy.ts +18 -7
- package/src/apply.ts +8 -9
- package/src/cloudflare/dns.ts +344 -0
- package/src/cloudflare/generate-asset-manifest.ts +4 -4
- package/src/cloudflare/index.ts +2 -1
- package/src/cloudflare/r2-rest-state-store.ts +353 -0
- package/src/cloudflare/response.ts +9 -0
- package/src/cloudflare/static-site-router.ts +18 -8
- package/src/cloudflare/static-site.ts +11 -7
- package/src/cloudflare/upload-asset-manifest.ts +6 -4
- package/src/cloudflare/worker.ts +44 -43
- package/src/cloudflare/zone.ts +39 -23
- package/src/dns/godaddy.ts +29 -0
- package/src/dns/import-dns.ts +213 -0
- package/src/dns/index.ts +2 -0
- package/src/dns/record.ts +121 -0
- package/src/fs/copy-file.ts +115 -0
- package/src/fs/file-system-state-store.ts +109 -0
- package/src/fs/file.ts +35 -9
- package/src/fs/folder.ts +14 -2
- package/src/fs/index.ts +10 -4
- package/src/fs/static-astro-file.ts +45 -0
- package/src/fs/static-css-file.ts +35 -0
- package/src/fs/static-html-file.ts +42 -0
- package/src/fs/static-json-file.ts +34 -0
- package/src/fs/{text-file.ts → static-text-file.ts} +9 -3
- package/src/fs/{typescript-file.ts → static-typescript-file.ts} +7 -6
- package/src/fs/static-vue-file.ts +39 -0
- package/src/fs/static-yaml-file.ts +33 -0
- package/src/internal/getting-started.ts +115 -0
- package/src/internal/index.ts +3 -0
- package/src/internal/providers.ts +241 -0
- package/src/internal/tutorial.ts +392 -0
- package/src/resource.ts +10 -1
- package/src/scope.ts +5 -6
- package/src/state.ts +1 -111
- package/src/test/bun.ts +60 -10
- package/src/web/astro.ts +644 -0
- package/src/{shadcn/component.ts → web/shadcn-component.ts} +5 -13
- package/src/web/shadcn.ts +219 -0
- package/src/web/tailwind.ts +167 -0
- package/src/{vite → web}/vite.ts +54 -56
- package/src/web/vitepress/config.ts +48 -0
- package/src/web/vitepress/custom-theme.ts +514 -0
- package/src/{vitepress → web/vitepress}/dependencies.ts +2 -2
- package/src/web/vitepress/home-page.ts +375 -0
- package/src/{vitepress → web/vitepress}/index.ts +2 -0
- package/src/{vitepress → web/vitepress}/vitepress.ts +62 -72
- package/lib/cloudflare/state.d.ts +0 -82
- package/lib/cloudflare/state.js +0 -108
- package/lib/fs/json-file.js +0 -7
- package/lib/fs/text-file.js +0 -7
- package/lib/fs/yaml-file.d.ts +0 -19
- package/lib/internal/docs.d.ts +0 -5
- package/lib/internal/docs.js +0 -287
- package/lib/shadcn/index.d.ts +0 -1
- package/lib/shadcn/index.js +0 -1
- package/lib/vite/index.d.ts +0 -1
- package/lib/vite/index.js +0 -1
- package/lib/vitepress/home-page.d.ts +0 -133
- package/lib/vitepress/home-page.js +0 -11
- package/src/cloudflare/state.ts +0 -138
- package/src/fs/json-file.ts +0 -23
- package/src/fs/yaml-file.ts +0 -26
- package/src/internal/docs.ts +0 -338
- package/src/shadcn/index.ts +0 -1
- package/src/vite/index.ts +0 -1
- package/src/vitepress/home-page.ts +0 -166
- /package/lib/{shadcn/component.d.ts → web/shadcn-component.d.ts} +0 -0
- /package/src/{vitepress → web/vitepress}/index.md +0 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { generateObject, generateText } from "ai";
|
|
2
|
+
import type { JsonSchema, Type, type } from "arktype";
|
|
3
|
+
import type { Context } from "../context";
|
|
4
|
+
import { StaticYamlFile } from "../fs/static-yaml-file";
|
|
5
|
+
import { Resource } from "../resource";
|
|
6
|
+
import type { Secret } from "../secret";
|
|
7
|
+
import { ark } from "./ark";
|
|
8
|
+
import { type ModelConfig, createModel } from "./client";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Properties for creating or updating a YAMLFile
|
|
12
|
+
*/
|
|
13
|
+
export interface YAMLFileProps<
|
|
14
|
+
T extends Type<any, any> | undefined = undefined,
|
|
15
|
+
> {
|
|
16
|
+
/**
|
|
17
|
+
* Path to the YAML file
|
|
18
|
+
*/
|
|
19
|
+
path: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Optional ArkType schema to validate and structure the generated YAML
|
|
23
|
+
* When provided, the resource will use generateObject with schema validation
|
|
24
|
+
* When not provided, it will extract YAML from between ```yaml fences
|
|
25
|
+
*/
|
|
26
|
+
schema?: T;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Base URL for the OpenAI API
|
|
30
|
+
* @default 'https://api.openai.com/v1'
|
|
31
|
+
*/
|
|
32
|
+
baseURL?: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Prompt for generating content
|
|
36
|
+
* Use alchemy template literals to include file context:
|
|
37
|
+
* @example
|
|
38
|
+
* prompt: await alchemy`
|
|
39
|
+
* Generate a YAML configuration for:
|
|
40
|
+
* ${alchemy.file("src/serverless.js")}
|
|
41
|
+
* `
|
|
42
|
+
*/
|
|
43
|
+
prompt: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* System prompt for the model
|
|
47
|
+
* This is used to provide instructions to the model about how to format the response
|
|
48
|
+
* @default Depends on whether schema is provided
|
|
49
|
+
*/
|
|
50
|
+
system?: string;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* OpenAI API key to use for generating content
|
|
54
|
+
* If not provided, will use OPENAI_API_KEY environment variable
|
|
55
|
+
*/
|
|
56
|
+
apiKey?: Secret;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Model configuration
|
|
60
|
+
*/
|
|
61
|
+
model?: ModelConfig;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Temperature for controlling randomness in generation.
|
|
65
|
+
* Higher values (e.g., 0.8) make output more random,
|
|
66
|
+
* lower values (e.g., 0.2) make it more deterministic.
|
|
67
|
+
* @default 0.7
|
|
68
|
+
*/
|
|
69
|
+
temperature?: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A YAML file that can be created, updated, and deleted
|
|
74
|
+
*/
|
|
75
|
+
export interface YAMLFile<T = any>
|
|
76
|
+
extends Omit<YAMLFileProps, "schema">,
|
|
77
|
+
Resource<"ai::YAMLFile"> {
|
|
78
|
+
/**
|
|
79
|
+
* Content of the YAML file as a string
|
|
80
|
+
*/
|
|
81
|
+
content: string;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Parsed YAML object
|
|
85
|
+
*/
|
|
86
|
+
yaml: T;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Schema used to validate the YAML (if provided)
|
|
90
|
+
*/
|
|
91
|
+
schema?: JsonSchema;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Time at which the file was created
|
|
95
|
+
*/
|
|
96
|
+
createdAt: number;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Time at which the file was last updated
|
|
100
|
+
*/
|
|
101
|
+
updatedAt: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Default system prompt for YAML file generation without schema
|
|
106
|
+
*/
|
|
107
|
+
const DEFAULT_YAML_SYSTEM_PROMPT =
|
|
108
|
+
"You are a YAML generator. Create valid YAML based on the user's requirements. Your response MUST include only YAML inside ```yaml fences. Do not include any other text, explanations, or multiple code blocks. Use standard YAML syntax with proper indentation. Use quotes around strings that contain special characters when necessary.";
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Resource for generating YAML files using AI models.
|
|
112
|
+
* Can operate in two modes:
|
|
113
|
+
* 1. With schema: Uses generateObject with type validation, then converts to YAML
|
|
114
|
+
* 2. Without schema: Extracts YAML from between ```yaml fences
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Generate a serverless configuration file
|
|
118
|
+
* const serverlessConfig = await YAMLFile("serverless-config", {
|
|
119
|
+
* path: "./serverless.yml",
|
|
120
|
+
* prompt: await alchemy`
|
|
121
|
+
* Generate a serverless.yml configuration for an AWS Lambda API with:
|
|
122
|
+
* - A service name "user-api"
|
|
123
|
+
* - Node.js 16.x runtime
|
|
124
|
+
* - Three functions: createUser, getUser, and listUsers
|
|
125
|
+
* - API Gateway endpoints for each function
|
|
126
|
+
* - DynamoDB table for users
|
|
127
|
+
* - IAM permissions for DynamoDB access
|
|
128
|
+
* `,
|
|
129
|
+
* model: {
|
|
130
|
+
* id: "gpt-4o",
|
|
131
|
+
* provider: "openai"
|
|
132
|
+
* }
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* // Generate YAML with schema validation
|
|
137
|
+
* import { type } from "arktype";
|
|
138
|
+
*
|
|
139
|
+
* const k8sConfigSchema = type({
|
|
140
|
+
* apiVersion: "string",
|
|
141
|
+
* kind: "string",
|
|
142
|
+
* metadata: {
|
|
143
|
+
* name: "string",
|
|
144
|
+
* namespace: "string?",
|
|
145
|
+
* labels: "Record<string, string>?"
|
|
146
|
+
* },
|
|
147
|
+
* spec: {
|
|
148
|
+
* replicas: "number",
|
|
149
|
+
* selector: {
|
|
150
|
+
* matchLabels: "Record<string, string>"
|
|
151
|
+
* },
|
|
152
|
+
* template: {
|
|
153
|
+
* metadata: {
|
|
154
|
+
* labels: "Record<string, string>"
|
|
155
|
+
* },
|
|
156
|
+
* spec: {
|
|
157
|
+
* containers: [{
|
|
158
|
+
* name: "string",
|
|
159
|
+
* image: "string",
|
|
160
|
+
* ports: [{
|
|
161
|
+
* containerPort: "number"
|
|
162
|
+
* }]
|
|
163
|
+
* }]
|
|
164
|
+
* }
|
|
165
|
+
* }
|
|
166
|
+
* }
|
|
167
|
+
* });
|
|
168
|
+
*
|
|
169
|
+
* const deployment = await YAMLFile("k8s-deployment", {
|
|
170
|
+
* path: "./kubernetes/deployment.yaml",
|
|
171
|
+
* schema: k8sConfigSchema,
|
|
172
|
+
* prompt: "Generate a Kubernetes deployment for a web application named 'frontend' with 3 replicas using the nginx:latest image and exposing port 80",
|
|
173
|
+
* temperature: 0.2
|
|
174
|
+
* });
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* // Generate GitHub Actions workflow with custom system prompt
|
|
178
|
+
* const workflow = await YAMLFile("github-workflow", {
|
|
179
|
+
* path: "./.github/workflows/ci.yml",
|
|
180
|
+
* prompt: await alchemy`
|
|
181
|
+
* Create a GitHub Actions workflow for a Node.js project that:
|
|
182
|
+
* - Runs on push to main and pull requests
|
|
183
|
+
* - Sets up Node.js 18
|
|
184
|
+
* - Installs dependencies with npm
|
|
185
|
+
* - Runs linting and tests
|
|
186
|
+
* - Builds the project
|
|
187
|
+
* - Deploys to GitHub Pages on success (main branch only)
|
|
188
|
+
* `,
|
|
189
|
+
* system: "You are a DevOps expert specializing in GitHub Actions workflows. Create a single YAML file inside ```yaml fences with no additional text. Follow GitHub Actions best practices and use proper YAML syntax.",
|
|
190
|
+
* model: {
|
|
191
|
+
* id: "claude-3-opus-20240229",
|
|
192
|
+
* provider: "anthropic"
|
|
193
|
+
* }
|
|
194
|
+
* });
|
|
195
|
+
*/
|
|
196
|
+
export const YAMLFile = Resource("ai::YAMLFile", async function <
|
|
197
|
+
const T extends Type<any, any> | undefined = undefined,
|
|
198
|
+
>(this: Context<YAMLFile<T extends Type<any, any> ? type.infer<T> : any>>, id: string, props: YAMLFileProps<T>): Promise<
|
|
199
|
+
YAMLFile<T extends Type<any, any> ? type.infer<T> : any>
|
|
200
|
+
> {
|
|
201
|
+
// Handle deletion phase
|
|
202
|
+
if (this.phase === "delete") {
|
|
203
|
+
return this.destroy();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let yamlContent: string;
|
|
207
|
+
let yamlObject: any;
|
|
208
|
+
|
|
209
|
+
// Check if schema is provided
|
|
210
|
+
if (props.schema) {
|
|
211
|
+
// Use schema-based generation
|
|
212
|
+
const { object } = await generateObject({
|
|
213
|
+
model: createModel(props),
|
|
214
|
+
schema: ark.schema<type.infer<typeof props.schema>>(props.schema),
|
|
215
|
+
providerOptions: props.model?.options,
|
|
216
|
+
system:
|
|
217
|
+
props.system ||
|
|
218
|
+
"Generate a valid object based on the provided requirements.",
|
|
219
|
+
prompt: props.prompt,
|
|
220
|
+
...(props.temperature === undefined
|
|
221
|
+
? {}
|
|
222
|
+
: { temperature: props.temperature }),
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
yamlObject = object;
|
|
226
|
+
|
|
227
|
+
// Let StaticYamlFile handle the YAML conversion
|
|
228
|
+
const file = await StaticYamlFile("file", props.path, yamlObject);
|
|
229
|
+
yamlContent = file.content;
|
|
230
|
+
} else {
|
|
231
|
+
// Use fence-based extraction
|
|
232
|
+
// Use provided system prompt or default
|
|
233
|
+
const system = props.system || DEFAULT_YAML_SYSTEM_PROMPT;
|
|
234
|
+
|
|
235
|
+
// Generate initial content
|
|
236
|
+
const { text } = await generateText({
|
|
237
|
+
model: createModel(props),
|
|
238
|
+
prompt: props.prompt,
|
|
239
|
+
system,
|
|
240
|
+
providerOptions: props.model?.options,
|
|
241
|
+
...(props.temperature === undefined
|
|
242
|
+
? {}
|
|
243
|
+
: { temperature: props.temperature }),
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// Extract and validate YAML content
|
|
247
|
+
let { content, error } = await extractYAMLContent(text);
|
|
248
|
+
|
|
249
|
+
// Re-prompt if there are validation errors
|
|
250
|
+
if (error) {
|
|
251
|
+
const errorSystem = `${system}\n\nERROR: ${error}\n\nPlease try again and ensure your response contains exactly one YAML block inside \`\`\`yaml fences.`;
|
|
252
|
+
|
|
253
|
+
const { text: retryText } = await generateText({
|
|
254
|
+
model: createModel(props),
|
|
255
|
+
prompt: props.prompt,
|
|
256
|
+
system: errorSystem,
|
|
257
|
+
providerOptions: props.model?.options,
|
|
258
|
+
...(props.temperature === undefined
|
|
259
|
+
? {}
|
|
260
|
+
: { temperature: props.temperature }),
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const retryResult = await extractYAMLContent(retryText);
|
|
264
|
+
|
|
265
|
+
if (retryResult.error) {
|
|
266
|
+
throw new Error(`Failed to generate valid YAML: ${retryResult.error}`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
content = retryResult.content;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
yamlContent = content;
|
|
273
|
+
|
|
274
|
+
// Create the file with the string content
|
|
275
|
+
const file = await StaticYamlFile("file", props.path, yamlContent);
|
|
276
|
+
|
|
277
|
+
// We need to parse the YAML to get the object representation
|
|
278
|
+
const yaml = await import("yaml");
|
|
279
|
+
yamlObject = yaml.parse(yamlContent);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Return the resource
|
|
283
|
+
return this({
|
|
284
|
+
...props,
|
|
285
|
+
schema: props.schema,
|
|
286
|
+
content: yamlContent,
|
|
287
|
+
yaml: yamlObject,
|
|
288
|
+
createdAt: Date.now(),
|
|
289
|
+
updatedAt: Date.now(),
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Extracts YAML content from between ```yaml fences
|
|
295
|
+
* Validates that exactly one YAML code block exists
|
|
296
|
+
*
|
|
297
|
+
* @param text The text to extract YAML from
|
|
298
|
+
* @returns The extracted YAML or error message
|
|
299
|
+
*/
|
|
300
|
+
async function extractYAMLContent(
|
|
301
|
+
text: string
|
|
302
|
+
): Promise<{ content: string; error?: string }> {
|
|
303
|
+
// Check for yaml or yml fence blocks
|
|
304
|
+
const yamlCodeRegex = /```(yaml|yml)\s*([\s\S]*?)```/g;
|
|
305
|
+
const matches = Array.from(text.matchAll(yamlCodeRegex));
|
|
306
|
+
|
|
307
|
+
if (matches.length === 0) {
|
|
308
|
+
return {
|
|
309
|
+
content: "",
|
|
310
|
+
error:
|
|
311
|
+
"No YAML code block found in the response. Please include your YAML within ```yaml fences.",
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (matches.length > 1) {
|
|
316
|
+
return {
|
|
317
|
+
content: "",
|
|
318
|
+
error:
|
|
319
|
+
"Multiple YAML code blocks found in the response. Please provide exactly one YAML block within ```yaml fences.",
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const content = matches[0][2].trim();
|
|
324
|
+
|
|
325
|
+
// We don't validate YAML parsing here because js-yaml might not be available
|
|
326
|
+
// Validation will happen at usage time if needed
|
|
327
|
+
return { content };
|
|
328
|
+
}
|
package/src/alchemy.ts
CHANGED
|
@@ -111,6 +111,16 @@ function _alchemy(
|
|
|
111
111
|
values.map(async function resolve(value): Promise<string> {
|
|
112
112
|
if (typeof value === "string") {
|
|
113
113
|
return indent + value;
|
|
114
|
+
} else if (value === null) {
|
|
115
|
+
return "null";
|
|
116
|
+
} else if (value === undefined) {
|
|
117
|
+
return "undefined";
|
|
118
|
+
} else if (
|
|
119
|
+
typeof value === "number" ||
|
|
120
|
+
typeof value === "boolean" ||
|
|
121
|
+
typeof value === "bigint"
|
|
122
|
+
) {
|
|
123
|
+
return value.toString();
|
|
114
124
|
} else if (value instanceof Promise) {
|
|
115
125
|
return resolve(await value);
|
|
116
126
|
} else if (isFileRef(value)) {
|
|
@@ -128,14 +138,15 @@ function _alchemy(
|
|
|
128
138
|
} else if (Array.isArray(value)) {
|
|
129
139
|
return (
|
|
130
140
|
await Promise.all(
|
|
131
|
-
value.map(async (value, i) => `${i}. ${await resolve(value)}`)
|
|
141
|
+
value.map(async (value, i) => `${i}. ${await resolve(value)}`)
|
|
132
142
|
)
|
|
133
143
|
).join("\n");
|
|
134
144
|
} else {
|
|
135
145
|
// TODO: support other types
|
|
136
|
-
|
|
146
|
+
console.log(value);
|
|
147
|
+
throw new Error(`Unsupported value type: ${value}`);
|
|
137
148
|
}
|
|
138
|
-
})
|
|
149
|
+
})
|
|
139
150
|
);
|
|
140
151
|
|
|
141
152
|
// Construct the string template by joining template parts with interpolated values
|
|
@@ -144,12 +155,12 @@ function _alchemy(
|
|
|
144
155
|
part
|
|
145
156
|
.split("\n")
|
|
146
157
|
.map((line) =>
|
|
147
|
-
line.startsWith(indent) ? line.slice(indent.length) : line
|
|
158
|
+
line.startsWith(indent) ? line.slice(indent.length) : line
|
|
148
159
|
)
|
|
149
|
-
.join("\n")
|
|
160
|
+
.join("\n")
|
|
150
161
|
)
|
|
151
162
|
.flatMap((part, i) =>
|
|
152
|
-
i < stringValues.length ? [part, stringValues[i] ?? ""] : [part]
|
|
163
|
+
i < stringValues.length ? [part, stringValues[i] ?? ""] : [part]
|
|
153
164
|
)
|
|
154
165
|
.join("")
|
|
155
166
|
.split("\n");
|
|
@@ -240,7 +251,7 @@ export interface AlchemyOptions {
|
|
|
240
251
|
*/
|
|
241
252
|
function scope(
|
|
242
253
|
id: string | undefined,
|
|
243
|
-
options?: AlchemyOptions
|
|
254
|
+
options?: AlchemyOptions
|
|
244
255
|
// TODO: maybe we want to allow using _ = await alchemy.scope(import.meta)
|
|
245
256
|
// | [meta: ImportMeta]
|
|
246
257
|
): Scope {
|
package/src/apply.ts
CHANGED
|
@@ -19,7 +19,7 @@ export interface ApplyOptions {
|
|
|
19
19
|
export async function apply<Out extends Resource>(
|
|
20
20
|
resource: PendingResource<Out>,
|
|
21
21
|
props: ResourceProps | undefined,
|
|
22
|
-
options?: ApplyOptions
|
|
22
|
+
options?: ApplyOptions
|
|
23
23
|
): Promise<Awaited<Out>> {
|
|
24
24
|
const scope = resource.Scope;
|
|
25
25
|
try {
|
|
@@ -64,18 +64,18 @@ export async function apply<Out extends Resource>(
|
|
|
64
64
|
"@secret": value.unencrypted,
|
|
65
65
|
}
|
|
66
66
|
: value,
|
|
67
|
-
2
|
|
67
|
+
2
|
|
68
68
|
);
|
|
69
69
|
const newProps = JSON.stringify(
|
|
70
70
|
await serialize(scope, props, {
|
|
71
71
|
encrypt: false,
|
|
72
72
|
}),
|
|
73
73
|
null,
|
|
74
|
-
2
|
|
74
|
+
2
|
|
75
75
|
);
|
|
76
76
|
if (oldProps === newProps && alwaysUpdate !== true) {
|
|
77
77
|
if (!quiet) {
|
|
78
|
-
console.log(`Skip: "${resource.FQN}" (no changes)`);
|
|
78
|
+
// console.log(`Skip: "${resource.FQN}" (no changes)`);
|
|
79
79
|
}
|
|
80
80
|
return state.output as Awaited<Out>;
|
|
81
81
|
}
|
|
@@ -88,7 +88,7 @@ export async function apply<Out extends Resource>(
|
|
|
88
88
|
|
|
89
89
|
if (!quiet) {
|
|
90
90
|
console.log(
|
|
91
|
-
`${phase === "create" ? "Create" : "Update"}: "${resource.FQN}"
|
|
91
|
+
`${phase === "create" ? "Create" : "Update"}: "${resource.FQN}"`
|
|
92
92
|
);
|
|
93
93
|
}
|
|
94
94
|
|
|
@@ -108,7 +108,7 @@ export async function apply<Out extends Resource>(
|
|
|
108
108
|
replace: () => {
|
|
109
109
|
if (isReplaced) {
|
|
110
110
|
console.warn(
|
|
111
|
-
`Resource ${resource.Kind} ${resource.FQN} is already marked as REPLACE
|
|
111
|
+
`Resource ${resource.Kind} ${resource.FQN} is already marked as REPLACE`
|
|
112
112
|
);
|
|
113
113
|
return;
|
|
114
114
|
}
|
|
@@ -117,12 +117,11 @@ export async function apply<Out extends Resource>(
|
|
|
117
117
|
});
|
|
118
118
|
|
|
119
119
|
const output = await alchemy.run(resource.ID, async () =>
|
|
120
|
-
provider.handler.bind(ctx)(resource.ID, props)
|
|
120
|
+
provider.handler.bind(ctx)(resource.ID, props)
|
|
121
121
|
);
|
|
122
|
-
|
|
123
122
|
if (!quiet) {
|
|
124
123
|
console.log(
|
|
125
|
-
`${phase === "create" ? "Created" : "Updated"}: "${resource.FQN}"
|
|
124
|
+
`${phase === "create" ? "Created" : "Updated"}: "${resource.FQN}"`
|
|
126
125
|
);
|
|
127
126
|
}
|
|
128
127
|
|