-
Notifications
You must be signed in to change notification settings - Fork 128
Feature/suggestion pills #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
9ddfda9
8b94070
170504f
6c4274f
9f5b62a
21eb29f
e121be3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Button } from '@/components/ui/button'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| interface SuggestionPillsProps { | ||
| suggestions: string[]; | ||
| onSelect: (suggestion: string) => void; | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| export function SuggestionPills({ | ||
| disabled, | ||
| suggestions, | ||
| onSelect, | ||
| }: SuggestionPillsProps) { | ||
| if (!suggestions.length) return null; | ||
|
|
||
| return ( | ||
| <div className="scrollbar-hide flex gap-2 overflow-x-auto pb-2 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"> | ||
| {suggestions.map((suggestion, index) => ( | ||
| <Button | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: using array index as Prompt To Fix With AIThis is a comment left during a code review.
Path: src/components/chat/SuggestionPills.tsx
Line: 20:20
Comment:
**style:** using array index as `key` can cause rendering issues if suggestions change. use the suggestion text itself as the key since suggestions are unique
How can I resolve this? If you propose a fix, please make it concise. |
||
| key={index} | ||
| variant="outline" | ||
| size="sm" | ||
| className={cn( | ||
| 'shrink-0 rounded-full border border-adam-neutral-700 bg-adam-neutral-800 text-xs text-white hover:text-white hover:opacity-80', | ||
| disabled ? 'opacity-50' : '', | ||
| )} | ||
| onClick={() => onSelect(suggestion)} | ||
| disabled={disabled} | ||
| > | ||
| {suggestion} | ||
| </Button> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "imports": { | ||
| "@shared/": "../../../shared/" | ||
| }, | ||
| "lint": { | ||
| "rules": { | ||
| "exclude": ["no-import-prefix", "no-unversioned-import"] | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import 'jsr:@supabase/functions-js/edge-runtime.d.ts'; | ||
| import { corsHeaders } from '../_shared/cors.ts'; | ||
|
|
||
| const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions'; | ||
| const OPENROUTER_API_KEY = Deno.env.get('OPENROUTER_API_KEY') ?? ''; | ||
|
|
||
| Deno.serve(async (req) => { | ||
| if (req.method === 'OPTIONS') { | ||
| return new Response('ok', { headers: corsHeaders }); | ||
| } | ||
|
|
||
| if (req.method !== 'POST') { | ||
| return new Response('Method not allowed', { status: 405 }); | ||
| } | ||
|
Comment on lines
+8
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: add function registration to Prompt To Fix With AIThis is a comment left during a code review.
Path: supabase/functions/suggestion-generator/index.ts
Line: 7:14
Comment:
**style:** add function registration to `config.toml` with `verify_jwt = true` per backend architecture guidelines
How can I resolve this? If you propose a fix, please make it concise. |
||
|
|
||
| try { | ||
| const { userPrompt, generatedCode, parameters } = await req.json(); | ||
|
|
||
| if (!userPrompt) { | ||
| return new Response(JSON.stringify({ suggestions: [] }), { | ||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
|
|
||
| // Build parameter summary for context | ||
| const paramSummary = parameters | ||
| ?.map( | ||
| (p: { name: string; value: string | number | boolean }) => | ||
| `${p.name}=${p.value}`, | ||
| ) | ||
| .join(', '); | ||
|
||
|
|
||
| const suggestionPrompt = `You are helping a user iterate on a 3D CAD model. | ||
| USER REQUEST: "${userPrompt}" | ||
| CURRENT PARAMETERS: ${paramSummary || 'none'} | ||
| GENERATED CODE: | ||
| \`\`\`openscad | ||
| ${generatedCode?.slice(0, 1500) || 'No code available'} | ||
| \`\`\` | ||
| Based on the ACTUAL model above, suggest exactly 2 specific improvements the user could make next. | ||
| Your suggestions should: | ||
| - Reference actual parameters or features in the code (e.g., if there's cup_height, suggest "Taller cup" not generic "Make bigger") | ||
| - Be actionable modifications (2-4 words) | ||
| - Be different from each other (one could adjust a dimension, another could add a feature) | ||
| DO NOT suggest: | ||
| - Generic things like "Add more detail" or "Improve design" | ||
| - Exporting, rendering, or color changes | ||
| - Things already in the model | ||
| Return exactly 2 suggestions: | ||
| <suggestion>First suggestion</suggestion> | ||
| <suggestion>Second suggestion</suggestion>`; | ||
|
|
||
| const response = await fetch(OPENROUTER_API_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${OPENROUTER_API_KEY}`, | ||
| 'HTTP-Referer': 'https://adam-cad.com', | ||
| 'X-Title': 'Adam CAD', | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: 'anthropic/claude-3.5-haiku', | ||
| max_tokens: 100, | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: suggestionPrompt, | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`OpenRouter API error: ${response.statusText}`); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| let suggestions: string[] = []; | ||
|
|
||
| if (data.choices && data.choices[0]?.message?.content) { | ||
| const responseText = data.choices[0].message.content; | ||
| const suggestionRegex = /<suggestion>(.*?)<\/suggestion>/gi; | ||
| const matches = responseText.matchAll(suggestionRegex); | ||
|
|
||
| suggestions = Array.from( | ||
| new Set( | ||
| Array.from(matches) | ||
| .map(([, text]) => { | ||
| if (!text) return null; | ||
| const cleaned = text | ||
| .trim() | ||
| .replace(/[""'']/g, '') | ||
| .replace(/^["']|["']$/g, '') | ||
| .trim(); | ||
| const words = cleaned.split(/\s+/); | ||
| if (words.length > 5) return null; | ||
| return words | ||
| .map( | ||
| (w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase(), | ||
| ) | ||
| .join(' '); | ||
| }) | ||
| .filter((s): s is string => s !== null && s.length > 0), | ||
| ), | ||
| ).slice(0, 2); | ||
| } | ||
|
|
||
| return new Response(JSON.stringify({ suggestions }), { | ||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||
| }); | ||
| } catch (error) { | ||
| console.error('Error generating suggestions:', error); | ||
| return new Response(JSON.stringify({ suggestions: [] }), { | ||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: no retry logic if suggestion generation fails - users won't see suggestions after a transient network error. consider persisting failure state to avoid repeated failed calls
Prompt To Fix With AI