fix: handle optional configFiles in template details and improve mapping safety

This commit is contained in:
Mauricio Siu
2025-09-21 01:07:25 -06:00
parent b6b6b9f2ce
commit f13028ee70
4 changed files with 60 additions and 48 deletions
@@ -88,7 +88,7 @@ export const StepThree = ({ templateInfo }: StepProps) => {
<div> <div>
<h3 className="text-sm font-semibold">Configuration Files</h3> <h3 className="text-sm font-semibold">Configuration Files</h3>
<ul className="list-disc pl-5"> <ul className="list-disc pl-5">
{templateInfo?.details?.configFiles.map((file, index) => ( {templateInfo?.details?.configFiles?.map((file, index) => (
<li key={index}> <li key={index}>
<strong className="text-sm font-semibold"> <strong className="text-sm font-semibold">
{file.filePath} {file.filePath}
@@ -1,5 +1,5 @@
import { Bot, Eye, EyeOff, PlusCircle, Trash2 } from "lucide-react"; import { Bot, PlusCircle, Trash2 } from "lucide-react";
import { useEffect, useState } from "react"; import { useEffect } from "react";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
import { toast } from "sonner"; import { toast } from "sonner";
import { AlertBlock } from "@/components/shared/alert-block"; import { AlertBlock } from "@/components/shared/alert-block";
@@ -27,7 +27,6 @@ export interface StepProps {
export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => { export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => {
const suggestions = templateInfo.suggestions || []; const suggestions = templateInfo.suggestions || [];
const selectedVariant = templateInfo.details; const selectedVariant = templateInfo.details;
const [showValues, setShowValues] = useState<Record<string, boolean>>({});
const { mutateAsync, isLoading, error, isError } = const { mutateAsync, isLoading, error, isError } =
api.ai.suggest.useMutation(); api.ai.suggest.useMutation();
@@ -44,7 +43,7 @@ export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => {
.then((data) => { .then((data) => {
setTemplateInfo({ setTemplateInfo({
...templateInfo, ...templateInfo,
suggestions: data, suggestions: data || [],
}); });
}) })
.catch((error) => { .catch((error) => {
@@ -54,10 +53,6 @@ export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => {
}); });
}, [templateInfo.userInput]); }, [templateInfo.userInput]);
const toggleShowValue = (name: string) => {
setShowValues((prev) => ({ ...prev, [name]: !prev[name] }));
};
const handleEnvVariableChange = ( const handleEnvVariableChange = (
index: number, index: number,
field: "name" | "value", field: "name" | "value",
@@ -310,9 +305,7 @@ export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => {
/> />
<div className="flex-1 relative"> <div className="flex-1 relative">
<Input <Input
type={ type={"password"}
showValues[env.name] ? "text" : "password"
}
value={env.value} value={env.value}
onChange={(e) => onChange={(e) =>
handleEnvVariableChange( handleEnvVariableChange(
@@ -323,19 +316,6 @@ export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => {
} }
placeholder="Variable Value" placeholder="Variable Value"
/> />
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-2 top-1/2 transform -translate-y-1/2"
onClick={() => toggleShowValue(env.name)}
>
{showValues[env.name] ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div> </div>
<Button <Button
type="button" type="button"
@@ -437,13 +417,14 @@ export const StepTwo = ({ templateInfo, setTemplateInfo }: StepProps) => {
<AccordionContent> <AccordionContent>
<ScrollArea className="w-full rounded-md border"> <ScrollArea className="w-full rounded-md border">
<div className="p-4 space-y-4"> <div className="p-4 space-y-4">
{selectedVariant?.configFiles?.length > 0 ? ( {selectedVariant?.configFiles?.length &&
selectedVariant?.configFiles?.length > 0 ? (
<> <>
<div className="text-sm text-muted-foreground mb-4"> <div className="text-sm text-muted-foreground mb-4">
This template requires the following This template requires the following
configuration files to be mounted: configuration files to be mounted:
</div> </div>
{selectedVariant.configFiles.map( {selectedVariant?.configFiles?.map(
(config, index) => ( (config, index) => (
<div <div
key={index} key={index}
@@ -47,7 +47,7 @@ interface Details {
envVariables: EnvVariable[]; envVariables: EnvVariable[];
shortDescription: string; shortDescription: string;
domains: Domain[]; domains: Domain[];
configFiles: Mount[]; configFiles?: Mount[];
} }
interface Mount { interface Mount {
+51 -20
View File
@@ -92,31 +92,48 @@ export const suggestVariants = async ({
const { object } = await generateObject({ const { object } = await generateObject({
model, model,
output: "array", output: "object",
schema: z.object({ schema: z.object({
id: z.string(), suggestions: z.array(
name: z.string(), z.object({
shortDescription: z.string(), id: z.string(),
description: z.string(), name: z.string(),
shortDescription: z.string(),
description: z.string(),
}),
),
}), }),
prompt: ` prompt: `
Act as advanced DevOps engineer and generate a list of open source projects what can cover users needs(up to 3 items), the suggestion Act as advanced DevOps engineer and generate a list of open source projects what can cover users needs(up to 3 items).
should include id, name, shortDescription, and description. Use slug of title for id.
Return your response as a JSON object with the following structure:
{
"suggestions": [
{
"id": "project-slug",
"name": "Project Name",
"shortDescription": "Brief one-line description",
"description": "Detailed description"
}
]
}
Important rules for the response: Important rules for the response:
1. The description field should ONLY contain a plain text description of the project, its features, and use cases 1. Use slug format for the id field (lowercase, hyphenated)
2. Do NOT include any code snippets, configuration examples, or installation instructions in the description 2. The description field should ONLY contain a plain text description of the project, its features, and use cases
3. The shortDescription should be a single-line summary focusing on the main technologies 3. Do NOT include any code snippets, configuration examples, or installation instructions in the description
4. The shortDescription should be a single-line summary focusing on the main technologies
5. All projects should be installable in docker and have docker compose support
User wants to create a new project with the following details, it should be installable in docker and can be docker compose generated for it: User wants to create a new project with the following details:
${input} ${input}
`, `,
}); });
if (object?.length) { if (object?.suggestions?.length) {
const result = []; const result = [];
for (const suggestion of object) { for (const suggestion of object.suggestions) {
try { try {
const { object: docker } = await generateObject({ const { object: docker } = await generateObject({
model, model,
@@ -136,16 +153,29 @@ export const suggestVariants = async ({
serviceName: z.string(), serviceName: z.string(),
}), }),
), ),
configFiles: z.array( configFiles: z
z.object({ .array(
content: z.string(), z.object({
filePath: z.string(), content: z.string(),
}), filePath: z.string(),
), }),
)
.optional(),
}), }),
prompt: ` prompt: `
Act as advanced DevOps engineer and generate docker compose with environment variables and domain configurations needed to install the following project. Act as advanced DevOps engineer and generate docker compose with environment variables and domain configurations needed to install the following project.
Return the docker compose as a YAML string and environment variables configuration. Follow these rules:
Return your response as a JSON object with this structure:
{
"dockerCompose": "yaml string here",
"envVariables": [{"name": "VAR_NAME", "value": "example_value"}],
"domains": [{"host": "domain.com", "port": 3000, "serviceName": "service"}],
"configFiles": [{"content": "file content", "filePath": "path/to/file"}]
}
Note: configFiles is optional - only include it if configuration files are absolutely required.
Follow these rules:
Docker Compose Rules: Docker Compose Rules:
1. Use placeholder like \${VARIABLE_NAME-default} for generated variables in the docker-compose.yml 1. Use placeholder like \${VARIABLE_NAME-default} for generated variables in the docker-compose.yml
@@ -198,6 +228,7 @@ export const suggestVariants = async ({
console.error("Error in docker compose generation:", error); console.error("Error in docker compose generation:", error);
} }
} }
return result; return result;
} }