Skip to content

Commit 8ed1a08

Browse files
committed
feat: improve template selection reliability and prompt clarity
- Added retry mechanism with configurable retry count to handle transient failures - Enhanced prompt formatting with clearer template list presentation and validation rules - Improved error handling to retry on failures before falling back to null selection
1 parent 0ee9218 commit 8ed1a08

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

worker/agents/planning/templateSelector.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,25 @@ interface SelectTemplateArgs {
2020
/**
2121
* Uses AI to select the most suitable template for a given query.
2222
*/
23-
export async function selectTemplate({ env, query, availableTemplates, inferenceContext, images }: SelectTemplateArgs): Promise<TemplateSelection> {
23+
export async function selectTemplate({ env, query, availableTemplates, inferenceContext, images }: SelectTemplateArgs, retryCount: number = 3): Promise<TemplateSelection> {
2424
if (availableTemplates.length === 0) {
2525
logger.info("No templates available for selection.");
2626
return { selectedTemplateName: null, reasoning: "No templates were available to choose from.", useCase: null, complexity: null, styleSelection: null, projectName: '' };
2727
}
2828

2929
try {
30-
logger.info("Asking AI to select a template", {
30+
logger.info(`Asking AI to select a template for the ${retryCount} time`, {
3131
query,
3232
queryLength: query.length,
3333
imagesCount: images?.length || 0,
3434
availableTemplates: availableTemplates.map(t => t.name),
3535
templateCount: availableTemplates.length
3636
});
3737

38+
const validTemplateNames = availableTemplates.map(t => t.name);
39+
3840
const templateDescriptions = availableTemplates.map((t, index) =>
39-
`- Template #${index + 1} \n Name - ${t.name} \n Language: ${t.language}, Frameworks: ${t.frameworks?.join(', ') || 'None'}\n ${t.description.selection}`
41+
`### Template #${index + 1} \n Name - ${t.name} \n Language: ${t.language}, Frameworks: ${t.frameworks?.join(', ') || 'None'}\n Description: \`\`\`${t.description.selection}\`\`\``
4042
).join('\n\n');
4143

4244
const systemPrompt = `You are an Expert Software Architect at Cloudflare specializing in template selection for rapid development. Your task is to select the most suitable starting template based on user requirements.
@@ -81,13 +83,16 @@ Reasoning: "Social template provides user interactions, content sharing, and com
8183
## RULES:
8284
- ALWAYS select a template (never return null)
8385
- Ignore misleading template names - analyze actual features
86+
- **ONLY** Choose from the list of available templates
8487
- Focus on functionality over naming conventions
8588
- Provide clear, specific reasoning for selection`
8689

8790
const userPrompt = `**User Request:** "${query}"
8891
89-
**Available Templates:**
90-
${templateDescriptions}
92+
## **Available Templates:**
93+
**ONLY** These template names are available for selection: ${validTemplateNames.join(', ')}
94+
95+
Template detail: ${templateDescriptions}
9196
9297
**Task:** Select the most suitable template and provide:
9398
1. Template name (exact match from list)
@@ -122,7 +127,6 @@ ENTROPY SEED: ${generateSecureToken(64)} - for unique results`;
122127
maxTokens: 2000,
123128
});
124129

125-
126130
logger.info(`AI template selection result: ${selection.selectedTemplateName || 'None'}, Reasoning: ${selection.reasoning}`);
127131
return selection;
128132

@@ -131,6 +135,10 @@ ENTROPY SEED: ${generateSecureToken(64)} - for unique results`;
131135
if (error instanceof RateLimitExceededError || error instanceof SecurityError) {
132136
throw error;
133137
}
138+
139+
if (retryCount > 0) {
140+
return selectTemplate({ env, query, availableTemplates, inferenceContext, images }, retryCount - 1);
141+
}
134142
// Fallback to no template selection in case of error
135143
return { selectedTemplateName: null, reasoning: "An error occurred during the template selection process.", useCase: null, complexity: null, styleSelection: null, projectName: '' };
136144
}

0 commit comments

Comments
 (0)