alchemy 0.3.1 → 0.4.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.
Files changed (113) hide show
  1. package/README.md +8 -3
  2. package/lib/ai/document.d.ts +12 -1
  3. package/lib/ai/document.js +10 -1
  4. package/lib/ai/index.d.ts +0 -2
  5. package/lib/ai/index.js +0 -2
  6. package/lib/alchemy.d.ts +5 -0
  7. package/lib/alchemy.js +44 -1
  8. package/lib/apply.js +17 -10
  9. package/lib/aws/account-id.d.ts +4 -1
  10. package/lib/aws/account-id.js +1 -1
  11. package/lib/aws/credentials.d.ts +5 -0
  12. package/lib/aws/credentials.js +0 -0
  13. package/lib/aws/oidc/oidc-provider.js +2 -2
  14. package/lib/aws/role.js +35 -23
  15. package/lib/cloudflare/account-api-token.d.ts +168 -0
  16. package/lib/cloudflare/account-api-token.js +179 -0
  17. package/lib/cloudflare/account-id.d.ts +1 -0
  18. package/lib/cloudflare/account-id.js +0 -0
  19. package/lib/cloudflare/api.d.ts +38 -1
  20. package/lib/cloudflare/api.js +55 -4
  21. package/lib/cloudflare/assets.d.ts +74 -0
  22. package/lib/cloudflare/assets.js +73 -0
  23. package/lib/cloudflare/bindings.d.ts +6 -1
  24. package/lib/cloudflare/bindings.js +6 -0
  25. package/lib/cloudflare/bucket.d.ts +118 -3
  26. package/lib/cloudflare/bucket.js +269 -89
  27. package/lib/cloudflare/custom-domain.d.ts +75 -0
  28. package/lib/cloudflare/custom-domain.js +154 -0
  29. package/lib/cloudflare/{dns.js → dns-records.js} +14 -1
  30. package/lib/cloudflare/index.d.ts +6 -1
  31. package/lib/cloudflare/index.js +6 -1
  32. package/lib/cloudflare/permission-groups.d.ts +78 -0
  33. package/lib/cloudflare/permission-groups.js +48 -0
  34. package/lib/cloudflare/r2-rest-state-store.d.ts +2 -1
  35. package/lib/cloudflare/r2-rest-state-store.js +55 -24
  36. package/lib/cloudflare/static-site.d.ts +17 -18
  37. package/lib/cloudflare/static-site.js +19 -10
  38. package/lib/cloudflare/worker.d.ts +14 -0
  39. package/lib/cloudflare/worker.js +150 -3
  40. package/lib/{util/encrypt.d.ts → encrypt.d.ts} +1 -1
  41. package/lib/{util/encrypt.js → encrypt.js} +1 -1
  42. package/lib/fs/file-system-state-store.js +1 -1
  43. package/lib/fs/file.d.ts +18 -0
  44. package/lib/fs/file.js +6 -0
  45. package/lib/internal/{providers.d.ts → docs/providers.d.ts} +10 -4
  46. package/lib/internal/docs/providers.js +196 -0
  47. package/lib/secret.d.ts +3 -0
  48. package/lib/secret.js +13 -0
  49. package/lib/{util/serde.d.ts → serde.d.ts} +1 -1
  50. package/lib/{util/serde.js → serde.js} +12 -4
  51. package/lib/test/bun.js +3 -2
  52. package/lib/util/sha256.d.ts +1 -0
  53. package/lib/util/sha256.js +4 -0
  54. package/lib/web/vitepress/config.d.ts +2 -1
  55. package/lib/web/vitepress/config.js +1 -0
  56. package/lib/web/vitepress/index.d.ts +1 -0
  57. package/lib/web/vitepress/index.js +1 -0
  58. package/lib/web/vitepress/process-front-matter-files.d.ts +18 -0
  59. package/lib/web/vitepress/process-front-matter-files.js +68 -0
  60. package/lib/web/vitepress/vitepress.js +3 -2
  61. package/package.json +8 -4
  62. package/src/ai/document.ts +26 -2
  63. package/src/ai/index.ts +0 -2
  64. package/src/alchemy.ts +56 -2
  65. package/src/apply.ts +21 -20
  66. package/src/aws/account-id.ts +6 -2
  67. package/src/aws/credentials.ts +6 -0
  68. package/src/aws/oidc/oidc-provider.ts +17 -17
  69. package/src/aws/role.ts +68 -54
  70. package/src/cloudflare/account-api-token.ts +365 -0
  71. package/src/cloudflare/account-id.ts +0 -0
  72. package/src/cloudflare/api.ts +88 -17
  73. package/src/cloudflare/assets.ts +155 -0
  74. package/src/cloudflare/bindings.ts +11 -2
  75. package/src/cloudflare/bucket.ts +493 -133
  76. package/src/cloudflare/custom-domain.ts +318 -0
  77. package/src/cloudflare/{dns.ts → dns-records.ts} +36 -22
  78. package/src/cloudflare/index.ts +6 -1
  79. package/src/cloudflare/permission-groups.ts +137 -0
  80. package/src/cloudflare/r2-rest-state-store.ts +87 -42
  81. package/src/cloudflare/static-site.ts +20 -32
  82. package/src/cloudflare/worker.ts +253 -5
  83. package/src/dns/import-dns.ts +4 -4
  84. package/src/{util/encrypt.ts → encrypt.ts} +7 -10
  85. package/src/fs/file-system-state-store.ts +5 -5
  86. package/src/fs/file.ts +29 -0
  87. package/src/internal/docs/providers.ts +281 -0
  88. package/src/secret.ts +16 -0
  89. package/src/{util/serde.ts → serde.ts} +16 -10
  90. package/src/test/bun.ts +8 -7
  91. package/src/util/sha256.ts +5 -0
  92. package/src/web/vitepress/config.ts +3 -1
  93. package/src/web/vitepress/index.ts +1 -0
  94. package/src/web/vitepress/process-front-matter-files.ts +98 -0
  95. package/src/web/vitepress/vitepress.ts +3 -2
  96. package/lib/ai/approve.d.ts +0 -99
  97. package/lib/ai/approve.js +0 -76
  98. package/lib/ai/review.d.ts +0 -122
  99. package/lib/ai/review.js +0 -101
  100. package/lib/internal/getting-started.d.ts +0 -21
  101. package/lib/internal/getting-started.js +0 -87
  102. package/lib/internal/index.d.ts +0 -3
  103. package/lib/internal/index.js +0 -3
  104. package/lib/internal/providers.js +0 -172
  105. package/lib/internal/tutorial.d.ts +0 -104
  106. package/lib/internal/tutorial.js +0 -251
  107. package/src/ai/approve.ts +0 -163
  108. package/src/ai/review.ts +0 -213
  109. package/src/internal/getting-started.ts +0 -115
  110. package/src/internal/index.ts +0 -3
  111. package/src/internal/providers.ts +0 -241
  112. package/src/internal/tutorial.ts +0 -392
  113. /package/lib/cloudflare/{dns.d.ts → dns-records.d.ts} +0 -0
package/src/ai/review.ts DELETED
@@ -1,213 +0,0 @@
1
- import { generateText, type CoreMessage } from "ai";
2
- import type { Context } from "../context";
3
- import { Resource } from "../resource";
4
- import type { Secret } from "../secret";
5
- import { createModel, withRateLimitRetry, type ModelConfig } from "./client";
6
-
7
- /**
8
- * Properties for creating or updating a Review
9
- */
10
- export interface ReviewProps {
11
- /**
12
- * Content to be reviewed
13
- * Use alchemy template literals to include file context:
14
- * @example
15
- * content: await alchemy`
16
- * Review this code:
17
- * ${alchemy.file("src/api.ts")}
18
- * `
19
- *
20
- * Required unless messages are provided
21
- */
22
- content?: string;
23
-
24
- /**
25
- * Prompt for the review
26
- * This should include specific instructions for the review
27
- * @example
28
- * prompt: "Review this code for security vulnerabilities, performance issues, and best practices."
29
- *
30
- * Required unless messages are provided
31
- */
32
- prompt?: string;
33
-
34
- /**
35
- * Message history for the conversation
36
- * If provided, this will be used instead of the prompt and content
37
- */
38
- messages?: CoreMessage[];
39
-
40
- /**
41
- * System prompt for the model
42
- * This is used to provide instructions to the model about how to format the response
43
- * The default system prompt instructs the model to act as a reviewer
44
- */
45
- system?: string;
46
-
47
- /**
48
- * AI API key to use for generating content
49
- * If not provided, will use OPENAI_API_KEY environment variable
50
- */
51
- apiKey?: Secret;
52
-
53
- /**
54
- * Model configuration
55
- */
56
- model?: ModelConfig;
57
-
58
- /**
59
- * Temperature for controlling randomness in generation.
60
- * Higher values (e.g., 0.8) make output more random,
61
- * lower values (e.g., 0.2) make it more deterministic.
62
- * @default 0.7
63
- */
64
- temperature?: number;
65
-
66
- /**
67
- * Maximum number of tokens to generate.
68
- * Higher values allow for longer documents but may increase cost and generation time.
69
- * @default 10000
70
- */
71
- maxTokens?: number;
72
- }
73
-
74
- /**
75
- * Output returned after Review creation/update
76
- */
77
- export interface Review extends ReviewProps, Resource<"ai::Review"> {
78
- /**
79
- * Content of the review
80
- */
81
- content: string;
82
-
83
- /**
84
- * Updated message history with the review response appended
85
- */
86
- messages: CoreMessage[];
87
-
88
- /**
89
- * Time at which the review was created
90
- */
91
- createdAt: number;
92
-
93
- /**
94
- * Time at which the review was last updated
95
- */
96
- updatedAt: number;
97
- }
98
-
99
- /**
100
- * Default system prompt for review generation
101
- */
102
- const DEFAULT_REVIEW_SYSTEM_PROMPT = `You are an expert code reviewer and technical writer. Your task is to provide a thorough review of the provided content. Your review should include:
103
-
104
- 1. A high-level summary of the content
105
- 2. Detailed feedback on the content's quality, accuracy, and completeness
106
- 3. Specific suggestions for improvement
107
- 4. Alternative approaches or solutions where applicable
108
- 5. Best practices that should be followed
109
- 6. Potential issues or concerns that should be addressed
110
-
111
- Provide your review in a clear, well-structured format.`;
112
-
113
- /**
114
- * Creates or updates a Review resource
115
- *
116
- * @example
117
- * // Create a review of a code file
118
- * const codeReview = await Review("api-review", {
119
- * content: await alchemy`
120
- * Review this API implementation:
121
- * ${alchemy.file("src/api.ts")}
122
- * `,
123
- * prompt: "Review this API implementation for security vulnerabilities, performance issues, and best practices. Suggest improvements and alternative approaches."
124
- * });
125
- *
126
- * @example
127
- * // Create a review of a document
128
- * const docReview = await Review("tutorial-review", {
129
- * content: await alchemy`
130
- * Review this tutorial:
131
- * ${alchemy.file("docs/tutorials/getting-started.md")}
132
- * `,
133
- * prompt: "Review this tutorial for clarity, accuracy, and completeness. Suggest improvements to make it more user-friendly."
134
- * });
135
- *
136
- * @example
137
- * // Create a review with message history
138
- * const reviewWithHistory = await Review("code-review-iteration-2", {
139
- * content: "Updated code implementation",
140
- * prompt: "Review this updated implementation",
141
- * messages: [
142
- * { role: "user", content: "Can you review my code?" },
143
- * { role: "assistant", content: "I'll review your code. Please share it." },
144
- * { role: "user", content: "Here's my implementation: [code]" },
145
- * { role: "assistant", content: "Here's my review of your code: [previous review]" }
146
- * ]
147
- * });
148
- */
149
- export const Review = Resource(
150
- "ai::Review",
151
- async function (
152
- this: Context<Review>,
153
- id: string,
154
- props: ReviewProps
155
- ): Promise<Review> {
156
- if (this.phase === "delete") {
157
- return this.destroy();
158
- }
159
-
160
- // Use provided system prompt or default
161
- const system = props.system || DEFAULT_REVIEW_SYSTEM_PROMPT;
162
-
163
- // Create messages array if not provided
164
- const messages =
165
- props.messages ||
166
- (props.content && props.prompt
167
- ? [
168
- {
169
- role: "user",
170
- content: `${props.content}\n\n${props.prompt}`,
171
- },
172
- ]
173
- : []);
174
-
175
- if (messages.length === 0) {
176
- throw new Error(
177
- "Either messages or both content and prompt must be provided"
178
- );
179
- }
180
-
181
- // Generate review content with rate limit retry
182
- const { text } = await withRateLimitRetry(async () => {
183
- return generateText({
184
- model: createModel(props),
185
- messages,
186
- system,
187
- maxTokens: props.maxTokens || 8192,
188
- providerOptions: props.model?.options,
189
- ...(props.temperature === undefined
190
- ? {}
191
- : { temperature: props.temperature }),
192
- });
193
- });
194
-
195
- // Create updated message history with the AI's response
196
- const updatedMessages = [
197
- ...messages,
198
- {
199
- role: "assistant" as const,
200
- content: text,
201
- },
202
- ];
203
-
204
- // Return the review with the direct text from the LLM and updated messages
205
- return this({
206
- ...props,
207
- content: text,
208
- messages: updatedMessages,
209
- createdAt: Date.now(),
210
- updatedAt: Date.now(),
211
- });
212
- }
213
- );
@@ -1,115 +0,0 @@
1
- import { Document } from "../ai";
2
- import type { ModelConfig } from "../ai/client";
3
- import { alchemy } from "../alchemy";
4
- import type { Folder } from "../fs";
5
-
6
- export interface GettingStartedProps {
7
- /**
8
- * The output directory for the getting started document.
9
- */
10
- path: string | Folder;
11
-
12
- /**
13
- * The prompt to use for generating the getting started guide.
14
- * This should include specific details about the product or framework.
15
- */
16
- prompt: string;
17
-
18
- /**
19
- * Optional model configuration for generating the getting started guide.
20
- * Defaults to Claude 3.5 Sonnet.
21
- */
22
- model?: ModelConfig;
23
- }
24
-
25
- export type GettingStarted = Document;
26
-
27
- export async function GettingStarted({
28
- path: outFile,
29
- prompt,
30
- model = {
31
- id: "claude-3-5-sonnet-latest",
32
- provider: "anthropic",
33
- },
34
- }: GettingStartedProps): Promise<Document> {
35
- return Document(`docs/getting-started`, {
36
- title: "Getting Started Guide",
37
- path: typeof outFile === "string" ? outFile : outFile.path,
38
- model,
39
- prompt: await alchemy`
40
- You are a technical writer creating a getting started guide.
41
-
42
- ${prompt}
43
-
44
- If you need a template structure to follow, use this format:
45
-
46
- # Getting Started with [Product/Framework Name]
47
-
48
- (Brief introduction to the product/framework)
49
-
50
- ## Installation
51
-
52
- \`\`\`bash
53
- # Installation command
54
- \`\`\`
55
-
56
- ## Creating Your First Project
57
-
58
- (Step-by-step guide to creating a basic project)
59
-
60
- \`\`\`ts
61
- // Example of a simple project
62
- \`\`\`
63
-
64
- ## Core Concepts
65
-
66
- ### [Concept 1]
67
-
68
- (Explanation of the first core concept)
69
-
70
- \`\`\`ts
71
- // Example of using the concept
72
- \`\`\`
73
-
74
- ### [Concept 2]
75
-
76
- (Explanation of the second core concept)
77
-
78
- \`\`\`ts
79
- // Example of using the concept
80
- \`\`\`
81
-
82
- ## Working with [Feature]
83
-
84
- (How to use a specific feature)
85
-
86
- \`\`\`ts
87
- // Example of using the feature
88
- \`\`\`
89
-
90
- ## Testing
91
-
92
- (How to test your project)
93
-
94
- \`\`\`ts
95
- // Example of testing
96
- \`\`\`
97
-
98
- ## Deployment
99
-
100
- (How to deploy your project)
101
-
102
- \`\`\`ts
103
- // Example of deployment
104
- \`\`\`
105
-
106
- ## Next Steps
107
-
108
- (Where to go next to learn more)
109
-
110
- > [!TIP]
111
- > Make sure the examples follow a natural progression from minimal examples to more complex use cases.
112
- > Keep explanations concise and focused on high-value information.
113
- `,
114
- });
115
- }
@@ -1,3 +0,0 @@
1
- export * from "./getting-started";
2
- export * from "./providers";
3
- export * from "./tutorial";
@@ -1,241 +0,0 @@
1
- import { type } from "arktype";
2
- import fs from "fs/promises";
3
- import path from "path";
4
- import { Data, Document } from "../ai";
5
- import { alchemy } from "../alchemy";
6
- import { Folder } from "../fs";
7
-
8
- export interface DocsProps {
9
- /**
10
- * The output directory for the docs.
11
- */
12
- outDir: string | Folder;
13
-
14
- /**
15
- * The source directory for the docs.
16
- */
17
- srcDir: string;
18
-
19
- /**
20
- * Whether to filter the docs.
21
- * If true, include all docs.
22
- * If false, include none.
23
- * If a number, include that many providers.
24
- *
25
- * @default true (all docs)
26
- */
27
- filter?: boolean | number;
28
- }
29
-
30
- export type AlchemyProviderDocs = {
31
- dir: string;
32
- provider: string;
33
- documents: Document[];
34
- }[];
35
-
36
- export async function AlchemyProviderDocs({
37
- srcDir,
38
- outDir,
39
- filter,
40
- }: DocsProps): Promise<AlchemyProviderDocs> {
41
- outDir = typeof outDir === "string" ? outDir : outDir.path;
42
-
43
- const exclude = [
44
- "util",
45
- "test",
46
- "vitepress",
47
- "vite",
48
- "shadcn",
49
- "internal",
50
- "web",
51
- ];
52
-
53
- // Get all folders in the alchemy/src directory
54
- let providers = (
55
- await fs.readdir(srcDir, {
56
- withFileTypes: true,
57
- })
58
- )
59
- .filter((dirent) => dirent.isDirectory() && !exclude.includes(dirent.name))
60
- .map((dirent) => path.join(dirent.parentPath, dirent.name));
61
-
62
- // For each provider, list all files
63
- if (filter === false) {
64
- return [];
65
- } else if (typeof filter === "number") {
66
- providers = providers.slice(0, filter);
67
- }
68
-
69
- return await Promise.all(
70
- providers.map(async (provider) => {
71
- const providerName = path.basename(provider);
72
- const files = (
73
- await fs.readdir(path.resolve(provider), {
74
- withFileTypes: true,
75
- })
76
- )
77
- .filter((dirent) => dirent.isFile())
78
- .map((dirent) =>
79
- path.relative(process.cwd(), path.resolve(provider, dirent.name))
80
- )
81
- .filter((file) => file.endsWith(".ts") && !file.endsWith("index.ts"));
82
-
83
- const {
84
- object: { groups },
85
- } = await Data(`docs/${providerName}`, {
86
- model: {
87
- id: "o3-mini",
88
- provider: "openai",
89
- options: {
90
- reasoningEffort: "high",
91
- },
92
- },
93
- temperature: 0.1,
94
- schema: type({
95
- groups: type({
96
- title: type("string").describe(
97
- "The title of the group, should be the Resource Name exactly as it's defined in code (const ResourceName translates to 'Resource Name') without spaces, e.g. Bucket or Static Site."
98
- ),
99
- filename: type("string").describe(
100
- "The filename of the Resource's Document, e.g. bucket.md or static-site.md"
101
- ),
102
- category: type("'Resource'|'Client'|'Utility'|'Types'").describe(
103
- "The classification of the Resource's Document, one of: Resource, Client, Utility, or Types."
104
- ),
105
- }).array(),
106
- }),
107
- system: await alchemy`
108
- You are a technical writer tasked with identifying the distinct documents that need to be written for a document group (folder) in a documentation site.
109
- You will be provided with a list of documents and instructions on how to classify them.
110
- Each document has a title, file name, and category.
111
- `,
112
- prompt: await alchemy`
113
- Identify and classify the documents that need to be written for the '${provider}' Service's Alchemy Resources.
114
- For background knowledge on Alchemy, see ${alchemy.file("./README.md")}.
115
- For background knowledge on the structure of an Alchemy Resource, see ${alchemy.file("./.cursorrules")}.
116
-
117
- The ${provider} Service has the following resources:
118
- ${alchemy.files(files)}
119
-
120
- A file is considered a "Resource" if it contains a const <ResourceName> = Resource(...) call or if it is a function that calls a Resource function, e.g. const TypeScriptFile = () => File(...).
121
- A file is considered a "Client" if it exposes a wrapper around creating a SDK client or fetch.
122
- A file is considered a "Utility" if it contains utility functions that are not resources or clients.
123
- A file is considered a "Types" if it contains just type definitions and maybe helpers around working with those types.
124
-
125
- The title should be simply the name of the resource's const in code (with spaces added in between each word), e.g. "Bucket" or "Function", except with spaces, e.g. "Static Site" for "const StaticSite". Maintain all other casing.
126
-
127
- // "Resource Name"
128
- const ResourceName = Resource(...)
129
- `,
130
- });
131
-
132
- // console.log(groups);
133
-
134
- const providerDocsDir = (await Folder(path.join(outDir, providerName)))
135
- .path;
136
-
137
- const documents = await Promise.allSettled(
138
- groups
139
- .filter((g) => g.category === "Resource")
140
- .map(async (g) =>
141
- Document(`docs/${providerName}/${g.title}`, {
142
- title: g.title,
143
- path: path.join(
144
- providerDocsDir,
145
- `${g.filename.replace(".ts", "").replace(".md", "")}.md`
146
- ),
147
- model: {
148
- id: "claude-3-5-sonnet-latest",
149
- provider: "anthropic",
150
- // options: {
151
- // reasoningEffort: "high",
152
- // },
153
- },
154
- prompt: await alchemy`
155
- You are a technical writer writing API documentation for an Alchemy IaC Resource.
156
- See ${alchemy.file("./README.md")} to understand the overview of Alchemy.
157
- See ${alchemy.file("./.cursorrules")} to better understand the structure and convention of an Alchemy Resource.
158
-
159
- Relevant files for the ${providerName} Service:
160
- ${alchemy.files(files)}
161
-
162
- Write concise documentation for the "${g.title}" Resource.
163
-
164
- > [!CAUTION]
165
- > Avoid the temptation to over explain or over describe. Focus on concise, simple, high value snippets. One heading and 0-1 descriptions per snippet.
166
-
167
- > [!TIP]
168
- > Make sure the examples follow a natural progression from the minimal example to logical next steps of how the Resource might be used.
169
-
170
- Each document must follow the following format:
171
-
172
- # ${g.title}
173
-
174
- (simple description with an external link to the provider's website)
175
- e.g.
176
- The Efs component lets you add [Amazon Elastic File System (EFS)](https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html) to your app.
177
-
178
- # Minimal Example
179
-
180
- (brief 1-2 sentences of what it does)
181
-
182
- \`\`\`ts
183
- import { ${g.title.replaceAll(" ", "")} } from "alchemy/${providerName}";
184
-
185
- (example)
186
- \`\`\`
187
-
188
-
189
- # Create the ${g.title}
190
-
191
- \`\`\`ts
192
- import { ${g.title.replaceAll(" ", "")} } from "alchemy/${providerName}";
193
-
194
- (example)
195
- \`\`\`
196
-
197
- ${
198
- providerName === "cloudflare"
199
- ? await alchemy`# Bind to a Worker
200
- (if it is a Cloudflare Resource)
201
-
202
- \`\`\`ts
203
- import { Worker, ${g.title.replaceAll(" ", "")} } from "alchemy/${providerName}";
204
-
205
- const myResource = await ${g.title.replaceAll(" ", "")}("my-resource", {
206
- // ...
207
- });
208
-
209
- await Worker("my-worker", {
210
- name: "my-worker",
211
- script: "console.log('Hello, world!')",
212
- bindings: {
213
- myResource,
214
- },
215
- });
216
- \`\`\``
217
- : ""
218
- }
219
- `,
220
- })
221
- )
222
- );
223
-
224
- // Unwrap all documents, fail if any rejected
225
- const results = await Promise.all(
226
- documents.map((r) => {
227
- if (r.status === "rejected") {
228
- throw r.reason;
229
- }
230
- return r.value;
231
- })
232
- );
233
-
234
- return {
235
- dir: providerDocsDir,
236
- provider: providerName,
237
- documents: results,
238
- } as const;
239
- })
240
- );
241
- }