Skip to content

Commit 32d39ed

Browse files
committed
1/1
Next.js 15.3.3 (outdated) Turbopack Runtime Error TypeError: Cann
1 parent e8c9edd commit 32d39ed

2 files changed

Lines changed: 101 additions & 100 deletions

File tree

src/ai/flows/assess-health-safety.ts

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,67 @@ export async function assessHealthSafety(input: AssessHealthSafetyInput): Promis
5050
};
5151
}
5252

53+
const prompt = ai.definePrompt({
54+
name: 'assessHealthSafetyPrompt',
55+
input: {schema: AssessHealthSafetyInputSchema},
56+
output: {schema: AssessHealthSafetyOutputSchema},
57+
prompt: `You are a world-class nutritionist and food safety expert. Your task is to analyze the following list of food ingredients and provide a concise, objective, and non-overlapping health and safety assessment. Your analysis must be grounded in established nutritional science and presented with the authority and trustworthiness of a professional expert. Prioritize factual accuracy and clarity for the user.
58+
59+
Ingredients list:
60+
"{{ingredients}}"
61+
62+
Based on the ingredients, perform the following actions and return the result in the specified JSON format:
63+
64+
1. **Health Rating (1-5):** Provide an overall health score from 1 (very unhealthy) to 5 (very healthy). Consider factors like processing level, presence of whole foods, additives, sugar content, etc.
65+
2. **Pros:** List 2-4 distinct positive aspects. Focus on healthy, natural, or beneficial ingredients. Do not simply state the absence of a negative as a positive (e.g., instead of "Low in sugar", focus on "Made with whole grains"). Ensure these are genuine benefits.
66+
3. **Cons:** List 2-4 distinct negative aspects that are separate from the 'Pros'. Focus on artificial additives, high sugar/sodium indicators, unhealthy fats, or heavily processed components. Ensure these are genuine drawbacks.
67+
4. **Warnings:** This section is for CRITICAL alerts only. List only ingredients that are banned in major regions (e.g., certain food dyes in the EU), are part of a major scientific controversy regarding health dangers, or pose a significant, non-obvious risk (e.g., high saturated fat for heart conditions). Do not use this section for common allergens or generally unhealthy items; those belong in 'Cons' or dietary information. If there are no such critical warnings, return an empty array.
68+
5. **Dietary Information:** Based on the ingredients, analyze for common dietary concerns. If the label explicitly states a status (e.g., "Certified Gluten-Free"), trust it. Otherwise, infer based on the ingredients.
69+
- **allergens:** Identify and list common allergens such as Gluten, Dairy, Soy, Peanuts, Tree Nuts, Fish, Shellfish. Do not list an allergen if a "-free" version is specified (e.g. "soy lecithin" is an allergen, but if the label also says "soy-free", do not list it).
70+
- **suitability:** Create a list of statements explaining why the product is NOT suitable for certain groups. For example: "Not suitable for vegans" if it contains honey or gelatin. "Not suitable for lactose intolerant individuals" if it contains milk. "Not suitable for people on low-sodium diets" if sodium is high. If there are no specific suitability issues, return an empty array.
71+
- **isVegetarian:** Determine if the product is vegetarian (contains no meat, poultry, or fish).
72+
- **isVegan:** Determine if the product is vegan (contains no animal products, including dairy, eggs, or honey).
73+
- **isGlutenFree:** Determine if the product is gluten-free (contains no wheat, barley, rye, or their derivatives).
74+
- **summary:** Provide a brief, human-readable summary of the key dietary points. For example: "Contains Dairy and Soy. Suitable for vegetarians but not vegans." If no specific concerns are found, state that.
75+
76+
Your analysis must be objective and based on general nutritional science. Be concise, clear, and ensure the 'Pros' and 'Cons' provide a balanced view without contradicting each other.`,
77+
config: {
78+
safetySettings: [
79+
{
80+
category: 'HARM_CATEGORY_HARASSMENT',
81+
threshold: 'BLOCK_NONE',
82+
},
83+
{
84+
category: 'HARM_CATEGORY_HATE_SPEECH',
85+
threshold: 'BLOCK_NONE',
86+
},
87+
{
88+
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
89+
threshold: 'BLOCK_NONE',
90+
},
91+
{
92+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
93+
threshold: 'BLOCK_NONE',
94+
},
95+
],
96+
},
97+
});
98+
99+
const assessHealthSafetyFlow = ai.defineFlow(
100+
{
101+
name: 'assessHealthSafetyFlow',
102+
inputSchema: AssessHealthSafetyInputSchema,
103+
outputSchema: AssessHealthSafetyOutputSchema,
104+
},
105+
async (input) => {
106+
const {output} = await prompt(input);
107+
if (!output) {
108+
throw new Error('The AI model failed to provide an assessment.');
109+
}
110+
return output;
111+
}
112+
);
113+
53114
try {
54115
return await assessHealthSafetyFlow(input);
55116
} catch (error: any) {
@@ -78,64 +139,3 @@ export async function assessHealthSafety(input: AssessHealthSafetyInput): Promis
78139
};
79140
}
80141
}
81-
82-
const prompt = ai.definePrompt({
83-
name: 'assessHealthSafetyPrompt',
84-
input: {schema: AssessHealthSafetyInputSchema},
85-
output: {schema: AssessHealthSafetyOutputSchema},
86-
prompt: `You are a world-class nutritionist and food safety expert. Your task is to analyze the following list of food ingredients and provide a concise, objective, and non-overlapping health and safety assessment. Your analysis must be grounded in established nutritional science and presented with the authority and trustworthiness of a professional expert. Prioritize factual accuracy and clarity for the user.
87-
88-
Ingredients list:
89-
"{{ingredients}}"
90-
91-
Based on the ingredients, perform the following actions and return the result in the specified JSON format:
92-
93-
1. **Health Rating (1-5):** Provide an overall health score from 1 (very unhealthy) to 5 (very healthy). Consider factors like processing level, presence of whole foods, additives, sugar content, etc.
94-
2. **Pros:** List 2-4 distinct positive aspects. Focus on healthy, natural, or beneficial ingredients. Do not simply state the absence of a negative as a positive (e.g., instead of "Low in sugar", focus on "Made with whole grains"). Ensure these are genuine benefits.
95-
3. **Cons:** List 2-4 distinct negative aspects that are separate from the 'Pros'. Focus on artificial additives, high sugar/sodium indicators, unhealthy fats, or heavily processed components. Ensure these are genuine drawbacks.
96-
4. **Warnings:** This section is for CRITICAL alerts only. List only ingredients that are banned in major regions (e.g., certain food dyes in the EU), are part of a major scientific controversy regarding health dangers, or pose a significant, non-obvious risk (e.g., high saturated fat for heart conditions). Do not use this section for common allergens or generally unhealthy items; those belong in 'Cons' or dietary information. If there are no such critical warnings, return an empty array.
97-
5. **Dietary Information:** Based on the ingredients, analyze for common dietary concerns. If the label explicitly states a status (e.g., "Certified Gluten-Free"), trust it. Otherwise, infer based on the ingredients.
98-
- **allergens:** Identify and list common allergens such as Gluten, Dairy, Soy, Peanuts, Tree Nuts, Fish, Shellfish. Do not list an allergen if a "-free" version is specified (e.g. "soy lecithin" is an allergen, but if the label also says "soy-free", do not list it).
99-
- **suitability:** Create a list of statements explaining why the product is NOT suitable for certain groups. For example: "Not suitable for vegans" if it contains honey or gelatin. "Not suitable for lactose intolerant individuals" if it contains milk. "Not suitable for people on low-sodium diets" if sodium is high. If there are no specific suitability issues, return an empty array.
100-
- **isVegetarian:** Determine if the product is vegetarian (contains no meat, poultry, or fish).
101-
- **isVegan:** Determine if the product is vegan (contains no animal products, including dairy, eggs, or honey).
102-
- **isGlutenFree:** Determine if the product is gluten-free (contains no wheat, barley, rye, or their derivatives).
103-
- **summary:** Provide a brief, human-readable summary of the key dietary points. For example: "Contains Dairy and Soy. Suitable for vegetarians but not vegans." If no specific concerns are found, state that.
104-
105-
Your analysis must be objective and based on general nutritional science. Be concise, clear, and ensure the 'Pros' and 'Cons' provide a balanced view without contradicting each other.`,
106-
config: {
107-
safetySettings: [
108-
{
109-
category: 'HARM_CATEGORY_HARASSMENT',
110-
threshold: 'BLOCK_NONE',
111-
},
112-
{
113-
category: 'HARM_CATEGORY_HATE_SPEECH',
114-
threshold: 'BLOCK_NONE',
115-
},
116-
{
117-
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
118-
threshold: 'BLOCK_NONE',
119-
},
120-
{
121-
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
122-
threshold: 'BLOCK_NONE',
123-
},
124-
],
125-
},
126-
});
127-
128-
const assessHealthSafetyFlow = ai.defineFlow(
129-
{
130-
name: 'assessHealthSafetyFlow',
131-
inputSchema: AssessHealthSafetyInputSchema,
132-
outputSchema: AssessHealthSafetyOutputSchema,
133-
},
134-
async (input) => {
135-
const {output} = await prompt(input);
136-
if (!output) {
137-
throw new Error('The AI model failed to provide an assessment.');
138-
}
139-
return output;
140-
}
141-
);

src/ai/flows/extract-ingredients.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,12 @@ export async function extractIngredients(input: ExtractIngredientsInput): Promis
1818
status: 'unreadable',
1919
};
2020
}
21-
try {
22-
return await extractIngredientsFlow(input);
23-
} catch (error) {
24-
console.error("Error in extractIngredientsFlow:", error);
25-
// Return a structured error on any unexpected exception
26-
return {
27-
ingredients: [],
28-
nutrition: { rawText: 'The AI model failed to process the image due to an unexpected error.', nutrients: [] },
29-
status: 'unreadable',
30-
};
31-
}
32-
}
33-
34-
const prompt = ai.definePrompt({
35-
name: 'extractIngredientsPrompt',
36-
input: {schema: ExtractIngredientsInputSchema},
37-
output: {schema: ExtractIngredientsOutputSchema},
38-
prompt: `You are an expert Optical Character Recognition (OCR) system specializing in food labels. Your task is to extract the ingredients list and nutritional information from the provided image.
21+
22+
const prompt = ai.definePrompt({
23+
name: 'extractIngredientsPrompt',
24+
input: {schema: ExtractIngredientsInputSchema},
25+
output: {schema: ExtractIngredientsOutputSchema},
26+
prompt: `You are an expert Optical Character Recognition (OCR) system specializing in food labels. Your task is to extract the ingredients list and nutritional information from the provided image.
3927
4028
Analyze the image carefully and return the data in the specified JSON format.
4129
@@ -52,28 +40,41 @@ Analyze the image carefully and return the data in the specified JSON format.
5240
If a section is not found, return empty values for it, but adhere to the schema.
5341
5442
Image to analyze: {{media url=image}}`,
55-
});
43+
});
5644

57-
const extractIngredientsFlow = ai.defineFlow(
58-
{
59-
name: 'extractIngredientsFlow',
60-
inputSchema: ExtractIngredientsInputSchema,
61-
outputSchema: ExtractIngredientsOutputSchema,
62-
},
63-
async (input) => {
64-
const {output} = await prompt(input);
65-
if (!output) {
66-
return {
67-
ingredients: [],
68-
nutrition: undefined,
69-
status: 'unreadable',
70-
};
71-
}
72-
// A simple check to refine status if model returns success but no data
73-
if (output.status === 'success' && output.ingredients.length === 0 && !output.nutrition?.rawText && !output.nutrition?.nutrients) {
74-
output.status = 'no_data';
45+
const extractIngredientsFlow = ai.defineFlow(
46+
{
47+
name: 'extractIngredientsFlow',
48+
inputSchema: ExtractIngredientsInputSchema,
49+
outputSchema: ExtractIngredientsOutputSchema,
50+
},
51+
async (input) => {
52+
const {output} = await prompt(input);
53+
if (!output) {
54+
return {
55+
ingredients: [],
56+
nutrition: undefined,
57+
status: 'unreadable',
58+
};
59+
}
60+
// A simple check to refine status if model returns success but no data
61+
if (output.status === 'success' && output.ingredients.length === 0 && !output.nutrition?.rawText && !output.nutrition?.nutrients) {
62+
output.status = 'no_data';
63+
}
64+
65+
return output;
7566
}
67+
);
7668

77-
return output;
69+
try {
70+
return await extractIngredientsFlow(input);
71+
} catch (error) {
72+
console.error("Error in extractIngredientsFlow:", error);
73+
// Return a structured error on any unexpected exception
74+
return {
75+
ingredients: [],
76+
nutrition: { rawText: 'The AI model failed to process the image due to an unexpected error.', nutrients: [] },
77+
status: 'unreadable',
78+
};
7879
}
79-
);
80+
}

0 commit comments

Comments
 (0)