alchemy 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) 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/bucket.d.ts +118 -3
  22. package/lib/cloudflare/bucket.js +269 -89
  23. package/lib/cloudflare/custom-domain.d.ts +75 -0
  24. package/lib/cloudflare/custom-domain.js +154 -0
  25. package/lib/cloudflare/{dns.js → dns-records.js} +14 -1
  26. package/lib/cloudflare/index.d.ts +5 -1
  27. package/lib/cloudflare/index.js +5 -1
  28. package/lib/cloudflare/permission-groups.d.ts +78 -0
  29. package/lib/cloudflare/permission-groups.js +48 -0
  30. package/lib/cloudflare/r2-rest-state-store.d.ts +2 -1
  31. package/lib/cloudflare/r2-rest-state-store.js +3 -2
  32. package/lib/cloudflare/static-site.d.ts +17 -18
  33. package/lib/cloudflare/static-site.js +19 -10
  34. package/lib/{util/encrypt.d.ts → encrypt.d.ts} +1 -1
  35. package/lib/{util/encrypt.js → encrypt.js} +1 -1
  36. package/lib/fs/file-system-state-store.js +1 -1
  37. package/lib/fs/file.d.ts +18 -0
  38. package/lib/fs/file.js +6 -0
  39. package/lib/internal/{providers.d.ts → docs/providers.d.ts} +10 -4
  40. package/lib/internal/docs/providers.js +196 -0
  41. package/lib/secret.d.ts +3 -0
  42. package/lib/secret.js +13 -0
  43. package/lib/{util/serde.d.ts → serde.d.ts} +1 -1
  44. package/lib/{util/serde.js → serde.js} +12 -4
  45. package/lib/test/bun.js +3 -2
  46. package/lib/util/sha256.d.ts +1 -0
  47. package/lib/util/sha256.js +4 -0
  48. package/lib/web/vitepress/config.d.ts +2 -1
  49. package/lib/web/vitepress/config.js +1 -0
  50. package/lib/web/vitepress/index.d.ts +1 -0
  51. package/lib/web/vitepress/index.js +1 -0
  52. package/lib/web/vitepress/process-front-matter-files.d.ts +18 -0
  53. package/lib/web/vitepress/process-front-matter-files.js +68 -0
  54. package/lib/web/vitepress/vitepress.js +3 -2
  55. package/package.json +8 -4
  56. package/src/ai/document.ts +26 -2
  57. package/src/ai/index.ts +0 -2
  58. package/src/alchemy.ts +56 -2
  59. package/src/apply.ts +21 -20
  60. package/src/aws/account-id.ts +6 -2
  61. package/src/aws/credentials.ts +6 -0
  62. package/src/aws/oidc/oidc-provider.ts +17 -17
  63. package/src/aws/role.ts +68 -54
  64. package/src/cloudflare/account-api-token.ts +365 -0
  65. package/src/cloudflare/account-id.ts +0 -0
  66. package/src/cloudflare/api.ts +88 -17
  67. package/src/cloudflare/bucket.ts +493 -133
  68. package/src/cloudflare/custom-domain.ts +318 -0
  69. package/src/cloudflare/{dns.ts → dns-records.ts} +36 -22
  70. package/src/cloudflare/index.ts +5 -1
  71. package/src/cloudflare/permission-groups.ts +137 -0
  72. package/src/cloudflare/r2-rest-state-store.ts +14 -13
  73. package/src/cloudflare/static-site.ts +20 -32
  74. package/src/dns/import-dns.ts +4 -4
  75. package/src/{util/encrypt.ts → encrypt.ts} +7 -10
  76. package/src/fs/file-system-state-store.ts +5 -5
  77. package/src/fs/file.ts +29 -0
  78. package/src/internal/docs/providers.ts +281 -0
  79. package/src/secret.ts +16 -0
  80. package/src/{util/serde.ts → serde.ts} +16 -10
  81. package/src/test/bun.ts +8 -7
  82. package/src/util/sha256.ts +5 -0
  83. package/src/web/vitepress/config.ts +3 -1
  84. package/src/web/vitepress/index.ts +1 -0
  85. package/src/web/vitepress/process-front-matter-files.ts +98 -0
  86. package/src/web/vitepress/vitepress.ts +3 -2
  87. package/lib/ai/approve.d.ts +0 -99
  88. package/lib/ai/approve.js +0 -76
  89. package/lib/ai/review.d.ts +0 -122
  90. package/lib/ai/review.js +0 -101
  91. package/lib/internal/getting-started.d.ts +0 -21
  92. package/lib/internal/getting-started.js +0 -87
  93. package/lib/internal/index.d.ts +0 -3
  94. package/lib/internal/index.js +0 -3
  95. package/lib/internal/providers.js +0 -172
  96. package/lib/internal/tutorial.d.ts +0 -104
  97. package/lib/internal/tutorial.js +0 -251
  98. package/src/ai/approve.ts +0 -163
  99. package/src/ai/review.ts +0 -213
  100. package/src/internal/getting-started.ts +0 -115
  101. package/src/internal/index.ts +0 -3
  102. package/src/internal/providers.ts +0 -241
  103. package/src/internal/tutorial.ts +0 -392
  104. /package/lib/cloudflare/{dns.d.ts → dns-records.d.ts} +0 -0
@@ -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
- }
@@ -1,392 +0,0 @@
1
- import type { CoreMessage } from "ai";
2
- import { Approve, Document, Review } from "../ai";
3
- import type { ModelConfig } from "../ai/client";
4
- import { alchemy } from "../alchemy";
5
- import type { Context } from "../context";
6
- import type { Folder } from "../fs";
7
- import { Resource } from "../resource";
8
-
9
- /**
10
- * Properties for creating or updating a Tutorial
11
- */
12
- export interface TutorialProps {
13
- /**
14
- * The output directory for the tutorial document.
15
- */
16
- path: string | Folder;
17
-
18
- /**
19
- * The title of the tutorial.
20
- */
21
- title: string;
22
-
23
- /**
24
- * The prompt to use for generating the tutorial.
25
- * This should include specific details about the tool or framework.
26
- */
27
- prompt: string;
28
-
29
- /**
30
- * Optional difficulty level for the tutorial.
31
- * Defaults to "beginner".
32
- */
33
- difficulty?: "beginner" | "intermediate" | "advanced";
34
-
35
- /**
36
- * Optional estimated time to complete the tutorial in minutes.
37
- * Defaults to 30.
38
- */
39
- estimatedTime?: number;
40
-
41
- /**
42
- * Optional model configuration for generating the tutorial.
43
- * Defaults to Claude 3.5 Sonnet.
44
- */
45
- model?: ModelConfig;
46
-
47
- /**
48
- * Maximum number of review iterations.
49
- * Defaults to 3.
50
- */
51
- maxIterations?: number;
52
-
53
- /**
54
- * Initial message history for the conversation.
55
- * If not provided, a new conversation will be started.
56
- */
57
- messages?: CoreMessage[];
58
- }
59
-
60
- /**
61
- * Output returned after Tutorial creation/update
62
- */
63
- export interface Tutorial extends TutorialProps, Resource<"docs::Tutorial"> {
64
- /**
65
- * The Document resource
66
- */
67
- document: Document;
68
-
69
- /**
70
- * The content of the tutorial
71
- */
72
- content: string;
73
-
74
- /**
75
- * Message history from the conversation.
76
- */
77
- messages: CoreMessage[];
78
-
79
- /**
80
- * Time at which the tutorial was created
81
- */
82
- createdAt: number;
83
-
84
- /**
85
- * Time at which the tutorial was last updated
86
- */
87
- updatedAt: number;
88
- }
89
-
90
- /**
91
- * Creates a tutorial document with iterative review and improvement
92
- *
93
- * @example
94
- * // Create a basic tutorial
95
- * const tutorial = await Tutorial("getting-started", {
96
- * path: "docs/tutorials/getting-started",
97
- * title: "Getting Started with Alchemy",
98
- * prompt: "Create a tutorial for getting started with the Alchemy framework",
99
- * difficulty: "beginner",
100
- * estimatedTime: 30
101
- * });
102
- *
103
- * @example
104
- * // Create an advanced tutorial with custom model and continue from existing messages
105
- * const advancedTutorial = await Tutorial("advanced-features", {
106
- * path: "docs/tutorials/advanced-features",
107
- * title: "Advanced Alchemy Features",
108
- * prompt: "Create a tutorial covering advanced features of the Alchemy framework",
109
- * difficulty: "advanced",
110
- * estimatedTime: 60,
111
- * model: {
112
- * id: "claude-3-7-sonnet-latest",
113
- * provider: "anthropic"
114
- * },
115
- * maxIterations: 5,
116
- * messages: basicTutorial.messages
117
- * });
118
- */
119
- export const Tutorial = Resource(
120
- "docs::Tutorial",
121
- async function (
122
- this: Context<Tutorial>,
123
- id: string,
124
- props: TutorialProps
125
- ): Promise<Tutorial> {
126
- const {
127
- path: outFile,
128
- title,
129
- prompt,
130
- difficulty = "beginner",
131
- estimatedTime = 30,
132
- model = {
133
- id: "claude-3-5-sonnet-latest",
134
- provider: "anthropic",
135
- },
136
- maxIterations = 3,
137
- messages: initialMessages = [],
138
- } = props;
139
-
140
- console.log(`Tutorial: Starting creation of "${title}" (ID: ${id})`);
141
-
142
- // Handle deletion phase
143
- if (this.phase === "delete") {
144
- console.log(`Tutorial: Deleting tutorial "${title}" (ID: ${id})`);
145
- return this.destroy();
146
- }
147
-
148
- // System prompts
149
- const docSystemPrompt = `You are a technical writer creating a comprehensive tutorial.
150
-
151
- Your task is to create a detailed tutorial about ${title} with difficulty level: ${difficulty} and estimated time to complete: ${estimatedTime} minutes.
152
-
153
- The tutorial should take users from zero knowledge to a working understanding of the tool or framework.
154
-
155
- The tutorial should be structured as follows:
156
-
157
- # ${title}
158
-
159
- ## Overview
160
-
161
- (Provide a brief introduction to the tool/framework, its purpose, and what users will learn in this tutorial)
162
-
163
- ## Prerequisites
164
-
165
- (List any prerequisites, tools, or knowledge required before starting the tutorial)
166
-
167
- ## Setup
168
-
169
- ### Installation
170
-
171
- \`\`\`bash
172
- # Installation commands
173
- \`\`\`
174
-
175
- ### Configuration
176
-
177
- (Explain any necessary configuration steps)
178
-
179
- \`\`\`bash
180
- # Configuration commands or code
181
- \`\`\`
182
-
183
- ## Step 1: [First Step Title]
184
-
185
- (Explain the first step in detail)
186
-
187
- \`\`\`bash
188
- # Commands or code for the first step
189
- \`\`\`
190
-
191
- (Explain what the code does and why it's important)
192
-
193
- ## Step 2: [Second Step Title]
194
-
195
- (Explain the second step in detail)
196
-
197
- \`\`\`bash
198
- # Commands or code for the second step
199
- \`\`\`
200
-
201
- (Explain what the code does and why it's important)
202
-
203
- ## Step 3: [Third Step Title]
204
-
205
- (Explain the third step in detail)
206
-
207
- \`\`\`bash
208
- # Commands or code for the third step
209
- \`\`\`
210
-
211
- (Explain what the code does and why it's important)
212
-
213
- ## Step 4: [Fourth Step Title]
214
-
215
- (Explain the fourth step in detail)
216
-
217
- \`\`\`bash
218
- # Commands or code for the fourth step
219
- \`\`\`
220
-
221
- (Explain what the code does and why it's important)
222
-
223
- ## Step 5: [Fifth Step Title]
224
-
225
- (Explain the fifth step in detail)
226
-
227
- \`\`\`bash
228
- # Commands or code for the fifth step
229
- \`\`\`
230
-
231
- (Explain what the code does and why it's important)
232
-
233
- ## Testing Your Work
234
-
235
- (Explain how to verify that everything is working correctly)
236
-
237
- \`\`\`bash
238
- # Testing commands or code
239
- \`\`\`
240
-
241
- ## Troubleshooting
242
-
243
- (Common issues users might encounter and how to resolve them)
244
-
245
- ## Next Steps
246
-
247
- (Suggest additional resources, advanced topics, or projects to try next)
248
-
249
- ## Additional Resources
250
-
251
- (Links to documentation, community resources, or related tutorials)`;
252
-
253
- // Initial message if none provided
254
- const startingMessages =
255
- initialMessages.length > 0
256
- ? initialMessages
257
- : [
258
- {
259
- role: "user" as const,
260
- content: `Create a comprehensive tutorial about ${title}.\n\n${prompt}\n\nDifficulty level: ${difficulty}\nEstimated time to complete: ${estimatedTime} minutes`,
261
- },
262
- ];
263
-
264
- // Generate the initial tutorial
265
- console.log(`Tutorial: Generating initial tutorial for "${title}"`);
266
- let tutorial = await Document(`document`, {
267
- title: title,
268
- path: typeof outFile === "string" ? outFile : outFile.path,
269
- model,
270
- messages: startingMessages,
271
- system: docSystemPrompt,
272
- });
273
- console.log(
274
- `Tutorial: Initial tutorial generated for "${title}" (${tutorial.content.length} chars)`
275
- );
276
-
277
- // Review and improve the tutorial in a loop
278
- let iteration = 0;
279
- let approved = false;
280
- let finalMessages = tutorial.messages;
281
-
282
- while (!approved && iteration < maxIterations) {
283
- iteration++;
284
- console.log(
285
- `Tutorial: Starting review iteration ${iteration}/${maxIterations} for "${title}"`
286
- );
287
-
288
- // Review the tutorial using system prompt
289
- console.log(`Tutorial: Requesting review for iteration ${iteration}`);
290
- const review = await Review(`review-${iteration}`, {
291
- messages: finalMessages,
292
- system:
293
- await alchemy`Please review this tutorial for correctness, conciseness, logical flow, and accurateness.
294
-
295
- The tutorial should:
296
- 1. Be technically accurate and free of errors
297
- 2. Be concise without over-explaining
298
- 3. Have a logical flow from basic to advanced concepts
299
- 4. Be accurate in its descriptions and instructions
300
- 5. Be appropriate for the ${difficulty} difficulty level
301
- 6. Be completable within approximately ${estimatedTime} minutes
302
-
303
- Provide specific feedback on areas that need improvement.`,
304
- model,
305
- });
306
-
307
- // Extract the actual review content from the last assistant message
308
- const reviewContent =
309
- review.messages[review.messages.length - 1]?.content || "";
310
- console.log(
311
- `Tutorial: Review received for iteration ${iteration} (${reviewContent.length} chars)`
312
- );
313
-
314
- // Check if the tutorial is approved using system prompt
315
- console.log(`Tutorial: Evaluating approval for iteration ${iteration}`);
316
- const approvalResult = await Approve(`approval-${iteration}`, {
317
- messages: review.messages,
318
- system:
319
- await alchemy`Based on the review above, determine if the tutorial meets the quality standards.
320
- The tutorial should be approved if:
321
- 1. It is technically accurate and free of errors
322
- 2. It is concise without over-explaining
323
- 3. It has a logical flow from basic to advanced concepts
324
- 4. It is accurate in its descriptions and instructions
325
- 5. It is appropriate for the ${difficulty} difficulty level
326
- 6. It is completable within approximately ${estimatedTime} minutes
327
-
328
- Respond with APPROVED or DENIED, followed by your explanation.`,
329
- });
330
- console.log(
331
- `Tutorial: Approval result for iteration ${iteration}: ${approvalResult.approved ? "APPROVED" : "DENIED"}`
332
- );
333
-
334
- // Log approval status
335
- if (approvalResult.approved) {
336
- console.log(
337
- `Tutorial: Final tutorial approved: ${approvalResult.explanation}`
338
- );
339
- approved = true;
340
- finalMessages = approvalResult.messages;
341
- } else {
342
- console.log(
343
- `Tutorial: Tutorial needs improvement: ${approvalResult.explanation}`
344
- );
345
-
346
- // Add improvement request and generate an improved tutorial
347
- const improvementMessages = [
348
- ...approvalResult.messages,
349
- {
350
- role: "user" as const,
351
- content: `Please create an improved version of the tutorial that addresses the issues identified in the review: ${approvalResult.explanation}`,
352
- },
353
- ];
354
-
355
- console.log(
356
- `Tutorial: Regenerating tutorial based on feedback for iteration ${iteration}`
357
- );
358
-
359
- tutorial = await Document(`tutorial-${iteration}`, {
360
- title: title,
361
- path: typeof outFile === "string" ? outFile : outFile.path,
362
- model,
363
- messages: improvementMessages,
364
- system: docSystemPrompt,
365
- });
366
- console.log(
367
- `Tutorial: Improved tutorial generated for iteration ${iteration} (${tutorial.content.length} chars)`
368
- );
369
-
370
- finalMessages = tutorial.messages;
371
- }
372
- }
373
-
374
- if (!approved) {
375
- console.log(
376
- `Tutorial: Tutorial not approved after ${maxIterations} iterations. Using the best version available.`
377
- );
378
- }
379
-
380
- console.log(`Tutorial: Completed creation of "${title}" (ID: ${id})`);
381
-
382
- // Return the tutorial resource
383
- return this({
384
- ...props,
385
- document: tutorial,
386
- content: tutorial.content,
387
- messages: finalMessages,
388
- createdAt: Date.now(),
389
- updatedAt: Date.now(),
390
- });
391
- }
392
- );
File without changes