diff --git a/.claude/settings.json b/.claude/settings.json index 13f3491..11cf6d9 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -16,7 +16,9 @@ "Bash(cat:*)", "Bash(jq:*)", "Bash(python3 -c:*)", - "Bash(git commit:*)" + "Bash(git commit:*)", + "Bash(grep:*)", + "Bash(xargs cat:*)" ] }, "hooks": { diff --git a/apps/web/app/api/transcribe/route.ts b/apps/web/app/api/transcribe/route.ts new file mode 100644 index 0000000..04bc262 --- /dev/null +++ b/apps/web/app/api/transcribe/route.ts @@ -0,0 +1,56 @@ +import { experimental_transcribe as transcribe } from "ai"; +import { elevenlabs } from "@ai-sdk/elevenlabs"; +import { getServerSession } from "@/lib/session/get-server-session"; + +interface TranscribeRequestBody { + audio: string; // base64-encoded audio data + mimeType?: string; // e.g., "audio/webm" - accepted but not currently used +} + +export async function POST(req: Request) { + const session = await getServerSession(); + if (!session?.user) { + return Response.json({ error: "Not authenticated" }, { status: 401 }); + } + + let body: TranscribeRequestBody; + try { + body = (await req.json()) as TranscribeRequestBody; + } catch { + return Response.json({ error: "Invalid JSON body" }, { status: 400 }); + } + + const { audio } = body; + + if (!audio) { + return Response.json( + { error: "Missing required field: audio" }, + { status: 400 }, + ); + } + + // Limit audio size to ~7.5MB of raw audio (10MB base64) + const maxBase64Length = 10 * 1024 * 1024; + if (audio.length > maxBase64Length) { + return Response.json( + { error: "Audio file too large. Maximum size is approximately 7.5MB." }, + { status: 413 }, + ); + } + + try { + const result = await transcribe({ + model: elevenlabs.transcription("scribe_v1"), + audio: audio, // base64 string is accepted directly + }); + + return Response.json({ text: result.text }); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + console.error("Transcription failed:", message); + return Response.json( + { error: "Transcription failed", details: message }, + { status: 500 }, + ); + } +} diff --git a/apps/web/app/tasks/[id]/task-detail-content.tsx b/apps/web/app/tasks/[id]/task-detail-content.tsx index 5780a3d..9915d0e 100644 --- a/apps/web/app/tasks/[id]/task-detail-content.tsx +++ b/apps/web/app/tasks/[id]/task-detail-content.tsx @@ -20,6 +20,7 @@ import { GitCompare, Paperclip, Loader2, + Mic, } from "lucide-react"; import { Button } from "@/components/ui/button"; @@ -35,6 +36,7 @@ import { CreateRepoDialog } from "@/components/create-repo-dialog"; import { ImageAttachmentsPreview } from "@/components/image-attachments-preview"; import { useScrollToBottom } from "@/hooks/use-scroll-to-bottom"; import { useImageAttachments } from "@/hooks/use-image-attachments"; +import { useAudioRecording } from "@/hooks/use-audio-recording"; import { ACCEPT_IMAGE_TYPES, isValidImageType } from "@/lib/image-utils"; import type { WebAgentUIToolPart, WebAgentUIMessagePart } from "@/app/types"; import type { TaskToolUIPart } from "@open-harness/agent"; @@ -372,7 +374,52 @@ export function TaskDetailContent() { const [showDiffPanel, setShowDiffPanel] = useState(false); const [cursorPosition, setCursorPosition] = useState(0); const [isDragging, setIsDragging] = useState(false); - const inputRef = useRef(null); + const inputRef = useRef(null); + const { + state: recordingState, + error: recordingError, + clearError: clearRecordingError, + toggleRecording, + } = useAudioRecording(); + + const handleMicClick = async () => { + clearRecordingError(); + const transcribedText = await toggleRecording(); + if (transcribedText) { + setInput((prev) => + prev ? `${prev} ${transcribedText}` : transcribedText, + ); + inputRef.current?.focus(); + } + }; + + // Auto-resize textarea up to 3 lines + useEffect(() => { + const textarea = inputRef.current; + if (!textarea) return; + + const computedStyle = getComputedStyle(textarea); + const lineHeight = parseFloat(computedStyle.lineHeight) || 24; + const maxLines = 3; + const maxHeight = lineHeight * maxLines; + + // Store current height to avoid flicker + const currentHeight = textarea.offsetHeight; + + // Temporarily set height to 0 to measure scrollHeight accurately + textarea.style.height = "0"; + const scrollHeight = textarea.scrollHeight; + + // Set new height, capped at max + const newHeight = Math.min(scrollHeight, maxHeight); + + // Only update if height actually changed to minimize reflows + if (Math.abs(newHeight - currentHeight) > 1) { + textarea.style.height = `${newHeight}px`; + } else { + textarea.style.height = `${currentHeight}px`; + } + }, [input]); const { images, @@ -1155,54 +1202,7 @@ export function TaskDetailContent() { }} className="hidden" /> -
{ - e.preventDefault(); - const hasContent = input.trim() || images.length > 0; - if (!hasContent || !isSandboxValid(sandboxInfo)) return; - - const messageText = input; - const files = getFileParts(); - setInput(""); - clearImages(); - - sendMessage({ text: messageText, files }); - }} - onDragOver={(e) => { - e.preventDefault(); - setIsDragging(true); - }} - onDragLeave={(e) => { - e.preventDefault(); - // Only set isDragging to false if we're leaving the form entirely - // (not just moving to a child element) - if (!e.currentTarget.contains(e.relatedTarget as Node)) { - setIsDragging(false); - } - }} - onDrop={(e) => { - e.preventDefault(); - setIsDragging(false); - const files = e.dataTransfer.files; - if (files.length > 0) { - addImages(files); - } - }} - className={`relative overflow-hidden rounded-2xl bg-muted transition-colors ${isDragging ? "ring-2 ring-blue-500/50" : ""}`} - > - {/* Sandbox overlay when inactive */} - - - {/* Image attachments preview */} - - +
{showSuggestions && ( )} + { + e.preventDefault(); + const hasContent = input.trim() || images.length > 0; + if (!hasContent || !isSandboxValid(sandboxInfo)) return; -
- - { - setInput(e.currentTarget.value); - setCursorPosition(e.currentTarget.selectionStart ?? 0); - }} - onKeyDown={(e) => { - // Let suggestions handle keyboard events first - if (handleSuggestionsKeyDown(e)) { - return; - } - // Handle form submission - if (e.key === "Enter" && !e.shiftKey) { - e.preventDefault(); - e.currentTarget.form?.requestSubmit(); - } - }} - onKeyUp={(e) => { - setCursorPosition(e.currentTarget.selectionStart ?? 0); - }} - onClick={(e) => { - setCursorPosition(e.currentTarget.selectionStart ?? 0); - }} - onPaste={(e) => { - const items = e.clipboardData?.items; - if (!items) return; - for (const item of items) { - if (isValidImageType(item.type)) { - const file = item.getAsFile(); - if (file) { - e.preventDefault(); - addImage(file).catch(() => { - // Silently ignore paste errors - rare edge case - }); + const messageText = input; + const files = getFileParts(); + setInput(""); + clearImages(); + + sendMessage({ text: messageText, files }); + }} + onDragOver={(e) => { + e.preventDefault(); + setIsDragging(true); + }} + onDragLeave={(e) => { + e.preventDefault(); + // Only set isDragging to false if we're leaving the form entirely + // (not just moving to a child element) + if (!e.currentTarget.contains(e.relatedTarget as Node)) { + setIsDragging(false); + } + }} + onDrop={(e) => { + e.preventDefault(); + setIsDragging(false); + const files = e.dataTransfer.files; + if (files.length > 0) { + addImages(files); + } + }} + className={`overflow-hidden rounded-2xl bg-muted transition-colors ${isDragging ? "ring-2 ring-blue-500/50" : ""}`} + > + {/* Sandbox overlay when inactive */} + + + {/* Image attachments preview */} + + + {/* Textarea area */} +
+