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.
Files changed (197) hide show
  1. package/README.md +3 -3
  2. package/lib/ai/approve.d.ts +99 -0
  3. package/lib/ai/approve.js +76 -0
  4. package/lib/ai/astro-file.d.ts +137 -0
  5. package/lib/ai/astro-file.js +156 -0
  6. package/lib/ai/client.d.ts +7 -8
  7. package/lib/ai/client.js +48 -19
  8. package/lib/ai/css-file.d.ts +131 -0
  9. package/lib/ai/css-file.js +141 -0
  10. package/lib/ai/data.d.ts +51 -6
  11. package/lib/ai/data.js +58 -12
  12. package/lib/ai/document.d.ts +108 -24
  13. package/lib/ai/document.js +166 -52
  14. package/lib/ai/html-file.d.ts +128 -0
  15. package/lib/ai/html-file.js +138 -0
  16. package/lib/ai/index.d.ts +9 -0
  17. package/lib/ai/index.js +9 -0
  18. package/lib/ai/json-file.d.ts +160 -0
  19. package/lib/ai/json-file.js +187 -0
  20. package/lib/ai/review.d.ts +122 -0
  21. package/lib/ai/review.js +101 -0
  22. package/lib/ai/typescript-file.d.ts +139 -0
  23. package/lib/ai/typescript-file.js +145 -0
  24. package/lib/ai/vue-file.d.ts +126 -0
  25. package/lib/ai/vue-file.js +136 -0
  26. package/lib/ai/yaml-file.d.ts +169 -0
  27. package/lib/ai/yaml-file.js +200 -0
  28. package/lib/alchemy.js +17 -2
  29. package/lib/apply.js +1 -1
  30. package/lib/cloudflare/dns.d.ts +88 -0
  31. package/lib/cloudflare/dns.js +192 -0
  32. package/lib/cloudflare/generate-asset-manifest.js +1 -1
  33. package/lib/cloudflare/index.d.ts +2 -1
  34. package/lib/cloudflare/index.js +2 -1
  35. package/lib/cloudflare/r2-rest-state-store.d.ts +125 -0
  36. package/lib/cloudflare/r2-rest-state-store.js +254 -0
  37. package/lib/cloudflare/response.d.ts +12 -0
  38. package/lib/cloudflare/response.js +0 -0
  39. package/lib/cloudflare/static-site-router.js +16 -7
  40. package/lib/cloudflare/static-site.js +3 -0
  41. package/lib/cloudflare/upload-asset-manifest.js +4 -2
  42. package/lib/cloudflare/zone.d.ts +18 -0
  43. package/lib/cloudflare/zone.js +17 -4
  44. package/lib/dns/godaddy.d.ts +7 -0
  45. package/lib/dns/godaddy.js +21 -0
  46. package/lib/dns/import-dns.d.ts +82 -0
  47. package/lib/dns/import-dns.js +114 -0
  48. package/lib/dns/index.d.ts +2 -0
  49. package/lib/dns/index.js +2 -0
  50. package/lib/dns/record.d.ts +88 -0
  51. package/lib/dns/record.js +13 -0
  52. package/lib/fs/copy-file.d.ts +54 -0
  53. package/lib/fs/copy-file.js +63 -0
  54. package/lib/fs/file-system-state-store.d.ts +17 -0
  55. package/lib/fs/file-system-state-store.js +87 -0
  56. package/lib/fs/file.d.ts +13 -0
  57. package/lib/fs/file.js +24 -5
  58. package/lib/fs/folder.d.ts +10 -0
  59. package/lib/fs/folder.js +2 -2
  60. package/lib/fs/index.d.ts +10 -4
  61. package/lib/fs/index.js +10 -4
  62. package/lib/fs/static-astro-file.d.ts +34 -0
  63. package/lib/fs/static-astro-file.js +39 -0
  64. package/lib/fs/static-css-file.d.ts +24 -0
  65. package/lib/fs/static-css-file.js +29 -0
  66. package/lib/fs/static-html-file.d.ts +31 -0
  67. package/lib/fs/static-html-file.js +36 -0
  68. package/lib/fs/{json-file.d.ts → static-json-file.d.ts} +3 -3
  69. package/lib/fs/static-json-file.js +15 -0
  70. package/lib/fs/{text-file.d.ts → static-text-file.d.ts} +2 -2
  71. package/lib/fs/static-text-file.js +8 -0
  72. package/lib/fs/{typescript-file.d.ts → static-typescript-file.d.ts} +3 -3
  73. package/lib/fs/{typescript-file.js → static-typescript-file.js} +3 -2
  74. package/lib/fs/static-vue-file.d.ts +28 -0
  75. package/lib/fs/static-vue-file.js +33 -0
  76. package/lib/fs/static-yaml-file.d.ts +22 -0
  77. package/lib/fs/{yaml-file.js → static-yaml-file.js} +3 -2
  78. package/lib/internal/getting-started.d.ts +21 -0
  79. package/lib/internal/getting-started.js +87 -0
  80. package/lib/internal/index.d.ts +3 -0
  81. package/lib/internal/index.js +3 -0
  82. package/lib/internal/providers.d.ts +27 -0
  83. package/lib/internal/providers.js +172 -0
  84. package/lib/internal/tutorial.d.ts +104 -0
  85. package/lib/internal/tutorial.js +251 -0
  86. package/lib/resource.js +8 -1
  87. package/lib/scope.d.ts +1 -1
  88. package/lib/scope.js +4 -2
  89. package/lib/state.d.ts +1 -16
  90. package/lib/state.js +0 -87
  91. package/lib/test/bun.d.ts +11 -0
  92. package/lib/test/bun.js +36 -8
  93. package/lib/web/astro.d.ts +147 -0
  94. package/lib/web/astro.js +414 -0
  95. package/lib/{shadcn/component.js → web/shadcn-component.js} +2 -9
  96. package/lib/web/shadcn.d.ts +97 -0
  97. package/lib/web/shadcn.js +99 -0
  98. package/lib/web/tailwind.d.ts +64 -0
  99. package/lib/web/tailwind.js +97 -0
  100. package/lib/{vite → web}/vite.d.ts +5 -0
  101. package/lib/{vite → web}/vite.js +44 -44
  102. package/lib/web/vitepress/config.d.ts +10 -0
  103. package/lib/web/vitepress/config.js +31 -0
  104. package/lib/web/vitepress/custom-theme.d.ts +173 -0
  105. package/lib/web/vitepress/custom-theme.js +336 -0
  106. package/lib/{vitepress → web/vitepress}/dependencies.d.ts +2 -2
  107. package/lib/{vitepress → web/vitepress}/dependencies.js +1 -1
  108. package/lib/web/vitepress/home-page.d.ts +246 -0
  109. package/lib/web/vitepress/home-page.js +147 -0
  110. package/lib/{vitepress → web/vitepress}/index.d.ts +2 -0
  111. package/lib/{vitepress → web/vitepress}/index.js +2 -0
  112. package/lib/{vitepress → web/vitepress}/vitepress.d.ts +15 -12
  113. package/lib/{vitepress → web/vitepress}/vitepress.js +26 -44
  114. package/package.json +6 -2
  115. package/src/ai/approve.ts +163 -0
  116. package/src/ai/astro-file.ts +269 -0
  117. package/src/ai/client.ts +60 -21
  118. package/src/ai/css-file.ts +247 -0
  119. package/src/ai/data.ts +81 -19
  120. package/src/ai/document.ts +240 -62
  121. package/src/ai/html-file.ts +244 -0
  122. package/src/ai/index.ts +9 -0
  123. package/src/ai/json-file.ts +325 -0
  124. package/src/ai/review.ts +213 -0
  125. package/src/ai/typescript-file.ts +259 -0
  126. package/src/ai/vue-file.ts +242 -0
  127. package/src/ai/yaml-file.ts +328 -0
  128. package/src/alchemy.ts +18 -7
  129. package/src/apply.ts +8 -9
  130. package/src/cloudflare/dns.ts +344 -0
  131. package/src/cloudflare/generate-asset-manifest.ts +4 -4
  132. package/src/cloudflare/index.ts +2 -1
  133. package/src/cloudflare/r2-rest-state-store.ts +353 -0
  134. package/src/cloudflare/response.ts +9 -0
  135. package/src/cloudflare/static-site-router.ts +18 -8
  136. package/src/cloudflare/static-site.ts +11 -7
  137. package/src/cloudflare/upload-asset-manifest.ts +6 -4
  138. package/src/cloudflare/worker.ts +44 -43
  139. package/src/cloudflare/zone.ts +39 -23
  140. package/src/dns/godaddy.ts +29 -0
  141. package/src/dns/import-dns.ts +213 -0
  142. package/src/dns/index.ts +2 -0
  143. package/src/dns/record.ts +121 -0
  144. package/src/fs/copy-file.ts +115 -0
  145. package/src/fs/file-system-state-store.ts +109 -0
  146. package/src/fs/file.ts +35 -9
  147. package/src/fs/folder.ts +14 -2
  148. package/src/fs/index.ts +10 -4
  149. package/src/fs/static-astro-file.ts +45 -0
  150. package/src/fs/static-css-file.ts +35 -0
  151. package/src/fs/static-html-file.ts +42 -0
  152. package/src/fs/static-json-file.ts +34 -0
  153. package/src/fs/{text-file.ts → static-text-file.ts} +9 -3
  154. package/src/fs/{typescript-file.ts → static-typescript-file.ts} +7 -6
  155. package/src/fs/static-vue-file.ts +39 -0
  156. package/src/fs/static-yaml-file.ts +33 -0
  157. package/src/internal/getting-started.ts +115 -0
  158. package/src/internal/index.ts +3 -0
  159. package/src/internal/providers.ts +241 -0
  160. package/src/internal/tutorial.ts +392 -0
  161. package/src/resource.ts +10 -1
  162. package/src/scope.ts +5 -6
  163. package/src/state.ts +1 -111
  164. package/src/test/bun.ts +60 -10
  165. package/src/web/astro.ts +644 -0
  166. package/src/{shadcn/component.ts → web/shadcn-component.ts} +5 -13
  167. package/src/web/shadcn.ts +219 -0
  168. package/src/web/tailwind.ts +167 -0
  169. package/src/{vite → web}/vite.ts +54 -56
  170. package/src/web/vitepress/config.ts +48 -0
  171. package/src/web/vitepress/custom-theme.ts +514 -0
  172. package/src/{vitepress → web/vitepress}/dependencies.ts +2 -2
  173. package/src/web/vitepress/home-page.ts +375 -0
  174. package/src/{vitepress → web/vitepress}/index.ts +2 -0
  175. package/src/{vitepress → web/vitepress}/vitepress.ts +62 -72
  176. package/lib/cloudflare/state.d.ts +0 -82
  177. package/lib/cloudflare/state.js +0 -108
  178. package/lib/fs/json-file.js +0 -7
  179. package/lib/fs/text-file.js +0 -7
  180. package/lib/fs/yaml-file.d.ts +0 -19
  181. package/lib/internal/docs.d.ts +0 -5
  182. package/lib/internal/docs.js +0 -287
  183. package/lib/shadcn/index.d.ts +0 -1
  184. package/lib/shadcn/index.js +0 -1
  185. package/lib/vite/index.d.ts +0 -1
  186. package/lib/vite/index.js +0 -1
  187. package/lib/vitepress/home-page.d.ts +0 -133
  188. package/lib/vitepress/home-page.js +0 -11
  189. package/src/cloudflare/state.ts +0 -138
  190. package/src/fs/json-file.ts +0 -23
  191. package/src/fs/yaml-file.ts +0 -26
  192. package/src/internal/docs.ts +0 -338
  193. package/src/shadcn/index.ts +0 -1
  194. package/src/vite/index.ts +0 -1
  195. package/src/vitepress/home-page.ts +0 -166
  196. /package/lib/{shadcn/component.d.ts → web/shadcn-component.d.ts} +0 -0
  197. /package/src/{vitepress → web/vitepress}/index.md +0 -0
@@ -0,0 +1,139 @@
1
+ import type { Context } from "../context";
2
+ import { Resource } from "../resource";
3
+ import type { Secret } from "../secret";
4
+ import { type ModelConfig } from "./client";
5
+ /**
6
+ * Properties for creating or updating a TypeScriptFile
7
+ */
8
+ export interface TypeScriptFileProps {
9
+ /**
10
+ * Path to the TypeScript file
11
+ */
12
+ path: string;
13
+ /**
14
+ * Base URL for the OpenAI API
15
+ * @default 'https://api.openai.com/v1'
16
+ */
17
+ baseURL?: string;
18
+ /**
19
+ * Prompt for generating content
20
+ * Use alchemy template literals to include file context:
21
+ * @example
22
+ * prompt: await alchemy`
23
+ * Generate a TypeScript utility function using:
24
+ * ${alchemy.file("src/types.ts")}
25
+ * `
26
+ */
27
+ prompt: string;
28
+ /**
29
+ * System prompt for the model
30
+ * This is used to provide instructions to the model about how to format the response
31
+ * The default system prompt instructs the model to return TypeScript code inside ```ts fences
32
+ * @default "You are a TypeScript code generator. Create TypeScript code based on the user's requirements. Your response MUST include only TypeScript code inside ```ts fences. Do not include any other text, explanations, or multiple code blocks."
33
+ */
34
+ system?: string;
35
+ /**
36
+ * OpenAI API key to use for generating content
37
+ * If not provided, will use OPENAI_API_KEY environment variable
38
+ */
39
+ apiKey?: Secret;
40
+ /**
41
+ * Model configuration
42
+ */
43
+ model?: ModelConfig;
44
+ /**
45
+ * Temperature for controlling randomness in generation.
46
+ * Higher values (e.g., 0.8) make output more random,
47
+ * lower values (e.g., 0.2) make it more deterministic.
48
+ * @default 0.7
49
+ */
50
+ temperature?: number;
51
+ /**
52
+ * Prettier configuration to use for formatting the TypeScript code
53
+ * If not provided, will use the default Prettier configuration
54
+ */
55
+ prettierConfig?: object;
56
+ }
57
+ /**
58
+ * A TypeScript file that can be created, updated, and deleted
59
+ */
60
+ export interface TypeScriptFile extends TypeScriptFileProps, Resource<"ai::TypeScriptFile"> {
61
+ /**
62
+ * Content of the TypeScript file
63
+ */
64
+ content: string;
65
+ /**
66
+ * Time at which the file was created
67
+ */
68
+ createdAt: number;
69
+ /**
70
+ * Time at which the file was last updated
71
+ */
72
+ updatedAt: number;
73
+ }
74
+ /**
75
+ * Resource for generating TypeScript files using AI models.
76
+ * Extracts TypeScript code from between ```ts fences, validates the response,
77
+ * and formats the code with Prettier.
78
+ *
79
+ * @example
80
+ * // Create a utility function
81
+ * const utils = await TypeScriptFile("string-utils", {
82
+ * path: "./src/utils/string-utils.ts",
83
+ * prompt: await alchemy`
84
+ * Generate TypeScript utility functions for string manipulation:
85
+ * - Capitalize first letter
86
+ * - Truncate with ellipsis
87
+ * - Convert to camelCase and kebab-case
88
+ * - Remove special characters
89
+ * `,
90
+ * model: {
91
+ * id: "gpt-4o",
92
+ * provider: "openai"
93
+ * }
94
+ * });
95
+ *
96
+ * @example
97
+ * // Generate a TypeScript class with custom formatting
98
+ * const userService = await TypeScriptFile("user-service", {
99
+ * path: "./src/services/UserService.ts",
100
+ * prompt: await alchemy`
101
+ * Create a UserService class that handles user authentication and profile management.
102
+ * The service should use the User type from:
103
+ * ${alchemy.file("src/types/User.ts")}
104
+ *
105
+ * Include methods for:
106
+ * - login(email, password)
107
+ * - register(user)
108
+ * - updateProfile(userId, profileData)
109
+ * - deleteAccount(userId)
110
+ * `,
111
+ * temperature: 0.2,
112
+ * prettierConfig: {
113
+ * semi: false,
114
+ * singleQuote: true,
115
+ * printWidth: 120
116
+ * }
117
+ * });
118
+ *
119
+ * @example
120
+ * // Generate a React hook with custom system prompt
121
+ * const useFormHook = await TypeScriptFile("use-form", {
122
+ * path: "./src/hooks/useForm.ts",
123
+ * prompt: await alchemy`
124
+ * Create a custom React hook called useForm that handles form state, validation, and submission.
125
+ * It should support:
126
+ * - Initial values
127
+ * - Validation rules
128
+ * - Field errors
129
+ * - Form submission with loading state
130
+ * - Reset functionality
131
+ * `,
132
+ * system: "You are an expert React developer specializing in TypeScript hooks. Create a single TypeScript file inside ```ts fences with no additional text. Follow React best practices and include proper typing.",
133
+ * model: {
134
+ * id: "claude-3-opus-20240229",
135
+ * provider: "anthropic"
136
+ * }
137
+ * });
138
+ */
139
+ export declare const TypeScriptFile: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<TypeScriptFile>, id: string, props: TypeScriptFileProps) => Promise<TypeScriptFile>);
@@ -0,0 +1,145 @@
1
+ import { generateText } from "ai";
2
+ import { StaticTypeScriptFile } from "../fs/static-typescript-file";
3
+ import { Resource } from "../resource";
4
+ import { createModel } from "./client";
5
+ /**
6
+ * Default system prompt for TypeScript file generation
7
+ */
8
+ const DEFAULT_TS_SYSTEM_PROMPT = "You are a TypeScript code generator. Create TypeScript code based on the user's requirements. Your response MUST include only TypeScript code inside ```ts fences. Do not include any other text, explanations, or multiple code blocks.";
9
+ /**
10
+ * Resource for generating TypeScript files using AI models.
11
+ * Extracts TypeScript code from between ```ts fences, validates the response,
12
+ * and formats the code with Prettier.
13
+ *
14
+ * @example
15
+ * // Create a utility function
16
+ * const utils = await TypeScriptFile("string-utils", {
17
+ * path: "./src/utils/string-utils.ts",
18
+ * prompt: await alchemy`
19
+ * Generate TypeScript utility functions for string manipulation:
20
+ * - Capitalize first letter
21
+ * - Truncate with ellipsis
22
+ * - Convert to camelCase and kebab-case
23
+ * - Remove special characters
24
+ * `,
25
+ * model: {
26
+ * id: "gpt-4o",
27
+ * provider: "openai"
28
+ * }
29
+ * });
30
+ *
31
+ * @example
32
+ * // Generate a TypeScript class with custom formatting
33
+ * const userService = await TypeScriptFile("user-service", {
34
+ * path: "./src/services/UserService.ts",
35
+ * prompt: await alchemy`
36
+ * Create a UserService class that handles user authentication and profile management.
37
+ * The service should use the User type from:
38
+ * ${alchemy.file("src/types/User.ts")}
39
+ *
40
+ * Include methods for:
41
+ * - login(email, password)
42
+ * - register(user)
43
+ * - updateProfile(userId, profileData)
44
+ * - deleteAccount(userId)
45
+ * `,
46
+ * temperature: 0.2,
47
+ * prettierConfig: {
48
+ * semi: false,
49
+ * singleQuote: true,
50
+ * printWidth: 120
51
+ * }
52
+ * });
53
+ *
54
+ * @example
55
+ * // Generate a React hook with custom system prompt
56
+ * const useFormHook = await TypeScriptFile("use-form", {
57
+ * path: "./src/hooks/useForm.ts",
58
+ * prompt: await alchemy`
59
+ * Create a custom React hook called useForm that handles form state, validation, and submission.
60
+ * It should support:
61
+ * - Initial values
62
+ * - Validation rules
63
+ * - Field errors
64
+ * - Form submission with loading state
65
+ * - Reset functionality
66
+ * `,
67
+ * system: "You are an expert React developer specializing in TypeScript hooks. Create a single TypeScript file inside ```ts fences with no additional text. Follow React best practices and include proper typing.",
68
+ * model: {
69
+ * id: "claude-3-opus-20240229",
70
+ * provider: "anthropic"
71
+ * }
72
+ * });
73
+ */
74
+ export const TypeScriptFile = Resource("ai::TypeScriptFile", async function (id, props) {
75
+ // Handle delete phase
76
+ if (this.phase === "delete") {
77
+ // StaticTypeScriptFile will handle the deletion
78
+ return this.destroy();
79
+ }
80
+ // Use provided system prompt or default
81
+ const system = props.system || DEFAULT_TS_SYSTEM_PROMPT;
82
+ // Generate initial content
83
+ const { text } = await generateText({
84
+ model: createModel(props),
85
+ prompt: props.prompt,
86
+ system,
87
+ providerOptions: props.model?.options,
88
+ ...(props.temperature === undefined
89
+ ? {}
90
+ : { temperature: props.temperature }),
91
+ });
92
+ // Extract and validate TypeScript code
93
+ let { code, error } = await extractTypeScriptCode(text);
94
+ // Re-prompt if there are validation errors
95
+ if (error) {
96
+ const errorSystem = `${system}\n\nERROR: ${error}\n\nPlease try again and ensure your response contains exactly one TypeScript code block inside \`\`\`ts fences.`;
97
+ const { text: retryText } = await generateText({
98
+ model: createModel(props),
99
+ prompt: props.prompt,
100
+ system: errorSystem,
101
+ providerOptions: props.model?.options,
102
+ ...(props.temperature === undefined
103
+ ? {}
104
+ : { temperature: props.temperature }),
105
+ });
106
+ const retryResult = await extractTypeScriptCode(retryText);
107
+ if (retryResult.error) {
108
+ throw new Error(`Failed to generate valid TypeScript code: ${retryResult.error}`);
109
+ }
110
+ code = retryResult.code;
111
+ }
112
+ // Use StaticTypeScriptFile to create/update the file
113
+ const file = await StaticTypeScriptFile("file", props.path, code);
114
+ // Return the resource
115
+ return this({
116
+ ...props,
117
+ content: file.content,
118
+ createdAt: Date.now(),
119
+ updatedAt: Date.now(),
120
+ });
121
+ });
122
+ /**
123
+ * Extracts TypeScript code from between ```ts fences
124
+ * Validates that exactly one TypeScript code block exists
125
+ *
126
+ * @param text The text to extract TypeScript code from
127
+ * @returns The extracted TypeScript code or error message
128
+ */
129
+ async function extractTypeScriptCode(text) {
130
+ const tsCodeRegex = /```ts\s*([\s\S]*?)```/g;
131
+ const matches = Array.from(text.matchAll(tsCodeRegex));
132
+ if (matches.length === 0) {
133
+ return {
134
+ code: "",
135
+ error: "No TypeScript code block found in the response. Please include your code within ```ts fences.",
136
+ };
137
+ }
138
+ if (matches.length > 1) {
139
+ return {
140
+ code: "",
141
+ error: "Multiple TypeScript code blocks found in the response. Please provide exactly one code block within ```ts fences.",
142
+ };
143
+ }
144
+ return { code: matches[0][1].trim() };
145
+ }
@@ -0,0 +1,126 @@
1
+ import type { Context } from "../context";
2
+ import { Resource } from "../resource";
3
+ import type { Secret } from "../secret";
4
+ import { type ModelConfig } from "./client";
5
+ /**
6
+ * Properties for creating or updating a VueFile
7
+ */
8
+ export interface VueFileProps {
9
+ /**
10
+ * Path to the Vue file
11
+ */
12
+ path: string;
13
+ /**
14
+ * Base URL for the OpenAI API
15
+ * @default 'https://api.openai.com/v1'
16
+ */
17
+ baseURL?: string;
18
+ /**
19
+ * Prompt for generating content
20
+ * Use alchemy template literals to include file context:
21
+ * @example
22
+ * prompt: await alchemy`
23
+ * Generate a Vue component using:
24
+ * ${alchemy.file("src/api.ts")}
25
+ * `
26
+ */
27
+ prompt: string;
28
+ /**
29
+ * System prompt for the model
30
+ * This is used to provide instructions to the model about how to format the response
31
+ * The default system prompt instructs the model to return a single Vue component inside ```vue fences
32
+ * @default "You are a Vue component generator. Create a single Vue component based on the user's requirements. Your response MUST include only a single Vue component inside ```vue fences. Do not include any other text, explanations, or multiple code blocks."
33
+ */
34
+ system?: string;
35
+ /**
36
+ * OpenAI API key to use for generating content
37
+ * If not provided, will use OPENAI_API_KEY environment variable
38
+ */
39
+ apiKey?: Secret;
40
+ /**
41
+ * Model configuration
42
+ */
43
+ model?: ModelConfig;
44
+ /**
45
+ * Temperature for controlling randomness in generation.
46
+ * Higher values (e.g., 0.8) make output more random,
47
+ * lower values (e.g., 0.2) make it more deterministic.
48
+ * @default 0.7
49
+ */
50
+ temperature?: number;
51
+ }
52
+ /**
53
+ * A Vue file that can be created, updated, and deleted
54
+ */
55
+ export interface VueFile extends VueFileProps, Resource<"ai::VueFile"> {
56
+ /**
57
+ * Content of the Vue file
58
+ */
59
+ content: string;
60
+ /**
61
+ * Time at which the file was created
62
+ */
63
+ createdAt: number;
64
+ /**
65
+ * Time at which the file was last updated
66
+ */
67
+ updatedAt: number;
68
+ }
69
+ /**
70
+ * Resource for generating Vue files using AI models.
71
+ * Extracts Vue code from between ```vue fences and validates the response.
72
+ *
73
+ * @example
74
+ * // Create a simple Vue component
75
+ * const button = await VueFile("button-component", {
76
+ * path: "./src/components/Button.vue",
77
+ * prompt: await alchemy`
78
+ * Generate a customizable button Vue component with:
79
+ * - Primary, secondary, and outline variants
80
+ * - Small, medium, and large sizes
81
+ * - Loading state with spinner
82
+ * - Disabled state
83
+ * `,
84
+ * model: {
85
+ * id: "gpt-4o",
86
+ * provider: "openai"
87
+ * }
88
+ * });
89
+ *
90
+ * @example
91
+ * // Generate a Vue component using existing files as reference
92
+ * const userCard = await VueFile("user-card", {
93
+ * path: "./src/components/UserCard.vue",
94
+ * prompt: await alchemy`
95
+ * Create a UserCard Vue component that displays user information.
96
+ * Follow the styling patterns from:
97
+ * ${alchemy.file("src/components/Card.vue")}
98
+ *
99
+ * Use the user type from:
100
+ * ${alchemy.file("src/types/User.ts")}
101
+ * `,
102
+ * temperature: 0.2
103
+ * });
104
+ *
105
+ * @example
106
+ * // Generate a complex form component with validation and custom system prompt
107
+ * const form = await VueFile("registration-form", {
108
+ * path: "./src/components/RegistrationForm.vue",
109
+ * prompt: await alchemy`
110
+ * Generate a registration form Vue component with:
111
+ * - Email, password, and confirm password fields
112
+ * - Form validation using Vuelidate or similar
113
+ * - Error messages for each field
114
+ * - Submit handler that emits form data
115
+ *
116
+ * Follow these style guidelines:
117
+ * ${alchemy.file("src/styles/guidelines.md")}
118
+ * `,
119
+ * system: "You are an expert Vue component creator specializing in form components with validation. Create a single Vue component inside ```vue fences with no additional text.",
120
+ * model: {
121
+ * id: "claude-3-opus-20240229",
122
+ * provider: "anthropic"
123
+ * }
124
+ * });
125
+ */
126
+ export declare const VueFile: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<VueFile>, id: string, props: VueFileProps) => Promise<VueFile>);
@@ -0,0 +1,136 @@
1
+ import { generateText } from "ai";
2
+ import { StaticVueFile } from "../fs/static-vue-file";
3
+ import { Resource } from "../resource";
4
+ import { createModel } from "./client";
5
+ /**
6
+ * Default system prompt for Vue file generation
7
+ */
8
+ const DEFAULT_VUE_SYSTEM_PROMPT = "You are a Vue component generator. Create a single Vue component based on the user's requirements. Your response MUST include only a single Vue component inside ```vue fences. Do not include any other text, explanations, or multiple code blocks.";
9
+ /**
10
+ * Resource for generating Vue files using AI models.
11
+ * Extracts Vue code from between ```vue fences and validates the response.
12
+ *
13
+ * @example
14
+ * // Create a simple Vue component
15
+ * const button = await VueFile("button-component", {
16
+ * path: "./src/components/Button.vue",
17
+ * prompt: await alchemy`
18
+ * Generate a customizable button Vue component with:
19
+ * - Primary, secondary, and outline variants
20
+ * - Small, medium, and large sizes
21
+ * - Loading state with spinner
22
+ * - Disabled state
23
+ * `,
24
+ * model: {
25
+ * id: "gpt-4o",
26
+ * provider: "openai"
27
+ * }
28
+ * });
29
+ *
30
+ * @example
31
+ * // Generate a Vue component using existing files as reference
32
+ * const userCard = await VueFile("user-card", {
33
+ * path: "./src/components/UserCard.vue",
34
+ * prompt: await alchemy`
35
+ * Create a UserCard Vue component that displays user information.
36
+ * Follow the styling patterns from:
37
+ * ${alchemy.file("src/components/Card.vue")}
38
+ *
39
+ * Use the user type from:
40
+ * ${alchemy.file("src/types/User.ts")}
41
+ * `,
42
+ * temperature: 0.2
43
+ * });
44
+ *
45
+ * @example
46
+ * // Generate a complex form component with validation and custom system prompt
47
+ * const form = await VueFile("registration-form", {
48
+ * path: "./src/components/RegistrationForm.vue",
49
+ * prompt: await alchemy`
50
+ * Generate a registration form Vue component with:
51
+ * - Email, password, and confirm password fields
52
+ * - Form validation using Vuelidate or similar
53
+ * - Error messages for each field
54
+ * - Submit handler that emits form data
55
+ *
56
+ * Follow these style guidelines:
57
+ * ${alchemy.file("src/styles/guidelines.md")}
58
+ * `,
59
+ * system: "You are an expert Vue component creator specializing in form components with validation. Create a single Vue component inside ```vue fences with no additional text.",
60
+ * model: {
61
+ * id: "claude-3-opus-20240229",
62
+ * provider: "anthropic"
63
+ * }
64
+ * });
65
+ */
66
+ export const VueFile = Resource("ai::VueFile", async function (id, props) {
67
+ // Handle deletion phase
68
+ if (this.phase === "delete") {
69
+ return this.destroy();
70
+ }
71
+ // Use provided system prompt or default
72
+ const system = props.system || DEFAULT_VUE_SYSTEM_PROMPT;
73
+ // Generate initial content
74
+ const { text } = await generateText({
75
+ model: createModel(props),
76
+ prompt: props.prompt,
77
+ system,
78
+ providerOptions: props.model?.options,
79
+ ...(props.temperature === undefined
80
+ ? {}
81
+ : { temperature: props.temperature }),
82
+ });
83
+ // Extract and validate Vue code
84
+ let { code, error } = await extractVueCode(text);
85
+ // Re-prompt if there are validation errors
86
+ if (error) {
87
+ const errorSystem = `${system}\n\nERROR: ${error}\n\nPlease try again and ensure your response contains exactly one Vue component inside \`\`\`vue fences.`;
88
+ const { text: retryText } = await generateText({
89
+ model: createModel(props),
90
+ prompt: props.prompt,
91
+ system: errorSystem,
92
+ providerOptions: props.model?.options,
93
+ ...(props.temperature === undefined
94
+ ? {}
95
+ : { temperature: props.temperature }),
96
+ });
97
+ const retryResult = await extractVueCode(retryText);
98
+ if (retryResult.error) {
99
+ throw new Error(`Failed to generate valid Vue code: ${retryResult.error}`);
100
+ }
101
+ code = retryResult.code;
102
+ }
103
+ // Use StaticVueFile to create/update the file
104
+ const file = await StaticVueFile("file", props.path, code);
105
+ // Return the resource
106
+ return this({
107
+ ...props,
108
+ content: file.content,
109
+ createdAt: Date.now(),
110
+ updatedAt: Date.now(),
111
+ });
112
+ });
113
+ /**
114
+ * Extracts Vue code from between ```vue fences
115
+ * Validates that exactly one Vue code block exists
116
+ *
117
+ * @param text The text to extract Vue code from
118
+ * @returns The extracted Vue code or error message
119
+ */
120
+ async function extractVueCode(text) {
121
+ const vueCodeRegex = /```vue\s*([\s\S]*?)```/g;
122
+ const matches = Array.from(text.matchAll(vueCodeRegex));
123
+ if (matches.length === 0) {
124
+ return {
125
+ code: "",
126
+ error: "No Vue code block found in the response. Please include your code within ```vue fences.",
127
+ };
128
+ }
129
+ if (matches.length > 1) {
130
+ return {
131
+ code: "",
132
+ error: "Multiple Vue code blocks found in the response. Please provide exactly one code block within ```vue fences.",
133
+ };
134
+ }
135
+ return { code: matches[0][1].trim() };
136
+ }