diff --git a/src/components/PermissionPrompt.tsx b/src/components/PermissionPrompt.tsx index 94e5798..a41b510 100644 --- a/src/components/PermissionPrompt.tsx +++ b/src/components/PermissionPrompt.tsx @@ -1,6 +1,7 @@ import { useState } from "react"; import { ShieldAlert, Check, X, Send, Play } from "lucide-react"; import { Button } from "@/components/ui/button"; +import { buildAskUserQuestionResult, getAskUserQuestionKey } from "@/lib/ask-user-question"; import type { PermissionRequest, RespondPermissionFn } from "@/types"; const TOOL_LABELS: Record = { @@ -24,7 +25,7 @@ interface QuestionOption { } interface Question { - id: string; + id?: string; question: string; header: string; isOther?: boolean; @@ -128,16 +129,16 @@ function AskUserQuestionPrompt({ request, onRespond }: PermissionPromptProps) { const questions = (request.toolInput.questions ?? []) as Question[]; const [selections, setSelections] = useState>>(() => { const init: Record> = {}; - for (const q of questions) { - init[q.id] = new Set(); + for (const [index, q] of questions.entries()) { + init[getAskUserQuestionKey(q, index)] = new Set(); } return init; }); const [freeText, setFreeText] = useState>({}); - const toggleOption = (questionId: string, label: string, multiSelect: boolean) => { + const toggleOption = (questionKey: string, label: string, multiSelect: boolean) => { setSelections((prev) => { - const current = prev[questionId] ?? new Set(); + const current = prev[questionKey] ?? new Set(); const next = new Set(current); if (multiSelect) { if (next.has(label)) next.delete(label); @@ -149,25 +150,13 @@ function AskUserQuestionPrompt({ request, onRespond }: PermissionPromptProps) { next.add(label); } } - return { ...prev, [questionId]: next }; + return { ...prev, [questionKey]: next }; }); - setFreeText((prev) => ({ ...prev, [questionId]: "" })); + setFreeText((prev) => ({ ...prev, [questionKey]: "" })); }; const handleSubmit = () => { - const answers: Record = {}; - const answersByQuestionId: Record = {}; - for (const q of questions) { - const custom = freeText[q.id]?.trim(); - if (custom) { - answers[q.question] = custom; - answersByQuestionId[q.id] = [custom]; - } else { - const selected = [...(selections[q.id] ?? [])]; - answers[q.question] = selected.join(", "); - answersByQuestionId[q.id] = selected; - } - } + const { answers, answersByQuestionId } = buildAskUserQuestionResult(questions, selections, freeText); onRespond("allow", { questions: request.toolInput.questions, answers, @@ -175,9 +164,10 @@ function AskUserQuestionPrompt({ request, onRespond }: PermissionPromptProps) { }); }; - const hasAllAnswers = questions.every((q) => { - const custom = freeText[q.id]?.trim(); - const selected = selections[q.id]; + const hasAllAnswers = questions.every((q, index) => { + const questionKey = getAskUserQuestionKey(q, index); + const custom = freeText[questionKey]?.trim(); + const selected = selections[questionKey]; return custom || (selected && selected.size > 0); }); @@ -186,19 +176,20 @@ function AskUserQuestionPrompt({ request, onRespond }: PermissionPromptProps) {
{questions.map((q, qi) => (
0 ? "border-t border-border/40" : ""}`} >

{q.question}

{(q.options ?? []).map((opt) => { - const isSelected = selections[q.id]?.has(opt.label); + const questionKey = getAskUserQuestionKey(q, qi); + const isSelected = selections[questionKey]?.has(opt.label); return (