working with voice tts
This commit is contained in:
41
.gitignore
vendored
Normal file
41
.gitignore
vendored
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
|
||||||
|
# dependencies
|
||||||
|
/node_modules
|
||||||
|
/.pnp
|
||||||
|
.pnp.*
|
||||||
|
.yarn/*
|
||||||
|
!.yarn/patches
|
||||||
|
!.yarn/plugins
|
||||||
|
!.yarn/releases
|
||||||
|
!.yarn/versions
|
||||||
|
|
||||||
|
# testing
|
||||||
|
/coverage
|
||||||
|
|
||||||
|
# next.js
|
||||||
|
/.next/
|
||||||
|
/out/
|
||||||
|
|
||||||
|
# production
|
||||||
|
/build
|
||||||
|
|
||||||
|
# misc
|
||||||
|
.DS_Store
|
||||||
|
*.pem
|
||||||
|
|
||||||
|
# debug
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# env files (can opt-in for committing if needed)
|
||||||
|
.env*
|
||||||
|
|
||||||
|
# vercel
|
||||||
|
.vercel
|
||||||
|
|
||||||
|
# typescript
|
||||||
|
*.tsbuildinfo
|
||||||
|
next-env.d.ts
|
||||||
36
README.md
Normal file
36
README.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
First, run the development server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
# or
|
||||||
|
yarn dev
|
||||||
|
# or
|
||||||
|
pnpm dev
|
||||||
|
# or
|
||||||
|
bun dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
||||||
|
|
||||||
|
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
||||||
|
|
||||||
|
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
||||||
|
|
||||||
|
## Learn More
|
||||||
|
|
||||||
|
To learn more about Next.js, take a look at the following resources:
|
||||||
|
|
||||||
|
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
||||||
|
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
||||||
|
|
||||||
|
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
|
||||||
|
|
||||||
|
## Deploy on Vercel
|
||||||
|
|
||||||
|
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
||||||
|
|
||||||
|
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
||||||
18
eslint.config.mjs
Normal file
18
eslint.config.mjs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { defineConfig, globalIgnores } from "eslint/config";
|
||||||
|
import nextVitals from "eslint-config-next/core-web-vitals";
|
||||||
|
import nextTs from "eslint-config-next/typescript";
|
||||||
|
|
||||||
|
const eslintConfig = defineConfig([
|
||||||
|
...nextVitals,
|
||||||
|
...nextTs,
|
||||||
|
// Override default ignores of eslint-config-next.
|
||||||
|
globalIgnores([
|
||||||
|
// Default ignores of eslint-config-next:
|
||||||
|
".next/**",
|
||||||
|
"out/**",
|
||||||
|
"build/**",
|
||||||
|
"next-env.d.ts",
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
export default eslintConfig;
|
||||||
21
next.config.ts
Normal file
21
next.config.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// next.config.ts
|
||||||
|
import type { NextConfig } from "next";
|
||||||
|
|
||||||
|
const nextConfig: NextConfig = {
|
||||||
|
turbopack: {
|
||||||
|
// Turbopack handles WASM and workers natively — no extra rules needed
|
||||||
|
},
|
||||||
|
async headers() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
source: "/(.*)",
|
||||||
|
headers: [
|
||||||
|
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
|
||||||
|
{ key: "Cross-Origin-Embedder-Policy", value: "require-corp" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default nextConfig;
|
||||||
6966
package-lock.json
generated
Normal file
6966
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
28
package.json
Normal file
28
package.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"name": "nail",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "next dev --webpack",
|
||||||
|
"build": "next build",
|
||||||
|
"start": "next start",
|
||||||
|
"lint": "eslint"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@huggingface/transformers": "^3.8.1",
|
||||||
|
"next": "16.2.1",
|
||||||
|
"openai": "^6.33.0",
|
||||||
|
"react": "19.2.4",
|
||||||
|
"react-dom": "19.2.4"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@tailwindcss/postcss": "^4",
|
||||||
|
"@types/node": "^20.19.37",
|
||||||
|
"@types/react": "^19",
|
||||||
|
"@types/react-dom": "^19",
|
||||||
|
"eslint": "^9",
|
||||||
|
"eslint-config-next": "16.2.1",
|
||||||
|
"tailwindcss": "^4",
|
||||||
|
"typescript": "^5"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
postcss.config.mjs
Normal file
7
postcss.config.mjs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const config = {
|
||||||
|
plugins: {
|
||||||
|
"@tailwindcss/postcss": {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
||||||
1
public/file.svg
Normal file
1
public/file.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.5 13.5V5.41a1 1 0 0 0-.3-.7L9.8.29A1 1 0 0 0 9.08 0H1.5v13.5A2.5 2.5 0 0 0 4 16h8a2.5 2.5 0 0 0 2.5-2.5m-1.5 0v-7H8v-5H3v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1M9.5 5V2.12L12.38 5zM5.13 5h-.62v1.25h2.12V5zm-.62 3h7.12v1.25H4.5zm.62 3h-.62v1.25h7.12V11z" clip-rule="evenodd" fill="#666" fill-rule="evenodd"/></svg>
|
||||||
|
After Width: | Height: | Size: 391 B |
1
public/globe.svg
Normal file
1
public/globe.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>
|
||||||
|
After Width: | Height: | Size: 1.0 KiB |
1
public/next.svg
Normal file
1
public/next.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>
|
||||||
|
After Width: | Height: | Size: 1.3 KiB |
1
public/vercel.svg
Normal file
1
public/vercel.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1155 1000"><path d="m577.3 0 577.4 1000H0z" fill="#fff"/></svg>
|
||||||
|
After Width: | Height: | Size: 128 B |
1
public/window.svg
Normal file
1
public/window.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.5h13v10a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1zM0 1h16v11.5a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 0 12.5zm3.75 4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5M7 4.75a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0m1.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5" fill="#666"/></svg>
|
||||||
|
After Width: | Height: | Size: 385 B |
31
src/app/api/chat/route.ts
Normal file
31
src/app/api/chat/route.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// src/app/api/chat/route.ts
|
||||||
|
import { NextRequest } from "next/server";
|
||||||
|
|
||||||
|
export async function POST(req: NextRequest) {
|
||||||
|
const { messages } = await req.json();
|
||||||
|
|
||||||
|
const res = await fetch(
|
||||||
|
`${process.env.OPENCLAW_BASE_URL}/v1/chat/completions`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: `openclaw:${process.env.OPENCLAW_AGENT_ID}`,
|
||||||
|
messages,
|
||||||
|
stream: true,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!res.ok || !res.body) {
|
||||||
|
return new Response(`OpenClaw error: ${res.status}`, { status: 502 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(res.body, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/event-stream",
|
||||||
|
"Cache-Control": "no-cache",
|
||||||
|
Connection: "keep-alive",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
29
src/app/api/transcribe/route.ts
Normal file
29
src/app/api/transcribe/route.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { pipeline } from "@huggingface/transformers";
|
||||||
|
|
||||||
|
let transcriber: Awaited<ReturnType<typeof pipeline>> | null = null;
|
||||||
|
|
||||||
|
async function getTranscriber() {
|
||||||
|
if (!transcriber) {
|
||||||
|
transcriber = await pipeline(
|
||||||
|
"automatic-speech-recognition",
|
||||||
|
"Xenova/whisper-tiny.en",
|
||||||
|
{ dtype: "q8" }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return transcriber;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(req: NextRequest) {
|
||||||
|
const { audio } = await req.json();
|
||||||
|
try {
|
||||||
|
const t = await getTranscriber();
|
||||||
|
const result = await (t as any)(Float32Array.from(audio), {
|
||||||
|
chunk_length_s: 30,
|
||||||
|
});
|
||||||
|
return NextResponse.json({ text: result.text?.trim() ?? "" });
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Whisper error:", err);
|
||||||
|
return NextResponse.json({ error: "Transcription failed" }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/app/favicon.ico
Normal file
BIN
src/app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
26
src/app/globals.css
Normal file
26
src/app/globals.css
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
@import "tailwindcss";
|
||||||
|
|
||||||
|
:root {
|
||||||
|
--background: #ffffff;
|
||||||
|
--foreground: #171717;
|
||||||
|
}
|
||||||
|
|
||||||
|
@theme inline {
|
||||||
|
--color-background: var(--background);
|
||||||
|
--color-foreground: var(--foreground);
|
||||||
|
--font-sans: var(--font-geist-sans);
|
||||||
|
--font-mono: var(--font-geist-mono);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root {
|
||||||
|
--background: #0a0a0a;
|
||||||
|
--foreground: #ededed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
background: var(--background);
|
||||||
|
color: var(--foreground);
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
}
|
||||||
33
src/app/layout.tsx
Normal file
33
src/app/layout.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import type { Metadata } from "next";
|
||||||
|
import { Geist, Geist_Mono } from "next/font/google";
|
||||||
|
import "./globals.css";
|
||||||
|
|
||||||
|
const geistSans = Geist({
|
||||||
|
variable: "--font-geist-sans",
|
||||||
|
subsets: ["latin"],
|
||||||
|
});
|
||||||
|
|
||||||
|
const geistMono = Geist_Mono({
|
||||||
|
variable: "--font-geist-mono",
|
||||||
|
subsets: ["latin"],
|
||||||
|
});
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: "Create Next App",
|
||||||
|
description: "Generated by create next app",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function RootLayout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<html
|
||||||
|
lang="en"
|
||||||
|
className={`${geistSans.variable} ${geistMono.variable} h-full antialiased`}
|
||||||
|
>
|
||||||
|
<body className="min-h-full flex flex-col">{children}</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
}
|
||||||
156
src/app/page.tsx
Normal file
156
src/app/page.tsx
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
// src/app/page.tsx
|
||||||
|
"use client";
|
||||||
|
import { useState, useRef, useEffect } from "react";
|
||||||
|
import { useChat } from "@/hooks/useChat";
|
||||||
|
import { useWhisper } from "@/hooks/useWhisper";
|
||||||
|
import { useVoiceRecorder } from "@/hooks/useVoiceRecorder";
|
||||||
|
import { stopSpeaking } from "@/lib/tts";
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
const [textInput, setTextInput] = useState("");
|
||||||
|
const { messages, isLoading, sendMessage } = useChat();
|
||||||
|
const { status: whisperStatus, modelMessage, transcribe } = useWhisper();
|
||||||
|
const { isRecording, startRecording, stopRecording } = useVoiceRecorder();
|
||||||
|
const bottomRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||||
|
}, [messages]);
|
||||||
|
|
||||||
|
const handleTextSubmit = (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!textInput.trim()) return;
|
||||||
|
sendMessage(textInput, "text");
|
||||||
|
setTextInput("");
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePTTDown = async () => {
|
||||||
|
if (whisperStatus !== "ready") return;
|
||||||
|
stopSpeaking();
|
||||||
|
await startRecording();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePTTUp = async () => {
|
||||||
|
if (!isRecording) return;
|
||||||
|
const audioData = await stopRecording();
|
||||||
|
const text = await transcribe(audioData);
|
||||||
|
if (text) sendMessage(text, "voice");
|
||||||
|
};
|
||||||
|
|
||||||
|
const pttDisabled =
|
||||||
|
whisperStatus !== "ready" || isLoading;
|
||||||
|
|
||||||
|
const pttLabel = () => {
|
||||||
|
if (whisperStatus === "loading") return "⏳";
|
||||||
|
if (whisperStatus === "transcribing") return "💬";
|
||||||
|
if (isRecording) return "🔴";
|
||||||
|
return "🎙";
|
||||||
|
};
|
||||||
|
|
||||||
|
const statusLine = () => {
|
||||||
|
if (whisperStatus === "loading") return modelMessage;
|
||||||
|
if (whisperStatus === "transcribing") return "Transcribing on-device…";
|
||||||
|
if (isRecording) return "Recording… release to send";
|
||||||
|
if (whisperStatus === "ready") return "Hold to talk — Whisper ready ✓";
|
||||||
|
return "Initialising Whisper…";
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="flex flex-col h-screen bg-gray-950 text-white">
|
||||||
|
{/* Header */}
|
||||||
|
<header className="flex items-center gap-3 px-6 py-4 border-b border-gray-800 bg-gray-900">
|
||||||
|
<span className="text-2xl">🦞</span>
|
||||||
|
<div>
|
||||||
|
<h1 className="text-xl font-bold tracking-tight">OpenClaw Voice</h1>
|
||||||
|
<p className="text-xs text-gray-500">On-device Whisper · No API keys</p>
|
||||||
|
</div>
|
||||||
|
<span
|
||||||
|
className={`ml-auto w-2 h-2 rounded-full ${
|
||||||
|
whisperStatus === "ready" ? "bg-green-400" : "bg-yellow-400 animate-pulse"
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{/* Messages */}
|
||||||
|
<div className="flex-1 overflow-y-auto px-4 py-6 space-y-4">
|
||||||
|
{messages.length === 0 && (
|
||||||
|
<div className="text-center mt-20 space-y-2">
|
||||||
|
<p className="text-gray-500 text-sm">
|
||||||
|
{whisperStatus === "ready"
|
||||||
|
? "Whisper loaded. Hold the button to talk or type below."
|
||||||
|
: modelMessage || "Loading Whisper model…"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{messages.map((msg) => (
|
||||||
|
<div
|
||||||
|
key={msg.id}
|
||||||
|
className={`flex ${msg.role === "user" ? "justify-end" : "justify-start"}`}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={`max-w-[75%] rounded-2xl px-4 py-3 text-sm leading-relaxed ${
|
||||||
|
msg.role === "user"
|
||||||
|
? "bg-indigo-600 text-white rounded-br-sm"
|
||||||
|
: "bg-gray-800 text-gray-100 rounded-bl-sm"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{msg.source === "voice" && (
|
||||||
|
<span className="text-xs opacity-40 block mb-1">
|
||||||
|
{msg.role === "user" ? "🎙 transcribed" : "🔊 spoken"}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{msg.content || <span className="opacity-40 animate-pulse">▍</span>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<div ref={bottomRef} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Controls */}
|
||||||
|
<div className="border-t border-gray-800 bg-gray-900 px-4 py-4 space-y-3">
|
||||||
|
{/* PTT Button */}
|
||||||
|
<div className="flex justify-center">
|
||||||
|
<button
|
||||||
|
onMouseDown={handlePTTDown}
|
||||||
|
onMouseUp={handlePTTUp}
|
||||||
|
onTouchStart={(e) => { e.preventDefault(); handlePTTDown(); }}
|
||||||
|
onTouchEnd={handlePTTUp}
|
||||||
|
disabled={pttDisabled}
|
||||||
|
className={`w-20 h-20 rounded-full text-3xl font-bold transition-all shadow-lg select-none
|
||||||
|
${isRecording
|
||||||
|
? "bg-red-500 scale-110 shadow-red-500/40 animate-pulse"
|
||||||
|
: whisperStatus === "transcribing"
|
||||||
|
? "bg-yellow-500 cursor-wait"
|
||||||
|
: pttDisabled
|
||||||
|
? "bg-gray-700 cursor-not-allowed opacity-50"
|
||||||
|
: "bg-indigo-600 hover:bg-indigo-500 active:scale-95 cursor-pointer"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{pttLabel()}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className="text-center text-xs text-gray-500">{statusLine()}</p>
|
||||||
|
|
||||||
|
{/* Text Input */}
|
||||||
|
<form onSubmit={handleTextSubmit} className="flex gap-2">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={textInput}
|
||||||
|
onChange={(e) => setTextInput(e.target.value)}
|
||||||
|
placeholder="Or type a message…"
|
||||||
|
disabled={isLoading || isRecording}
|
||||||
|
className="flex-1 bg-gray-800 rounded-xl px-4 py-2 text-sm outline-none focus:ring-2 focus:ring-indigo-500 disabled:opacity-50"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={isLoading || !textInput.trim()}
|
||||||
|
className="bg-indigo-600 hover:bg-indigo-500 disabled:opacity-40 px-4 py-2 rounded-xl text-sm font-medium transition"
|
||||||
|
>
|
||||||
|
Send
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
74
src/hooks/useChat.ts
Normal file
74
src/hooks/useChat.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
// src/hooks/useChat.ts
|
||||||
|
"use client";
|
||||||
|
import { useState, useCallback, useRef } from "react";
|
||||||
|
import { Message } from "@/lib/types";
|
||||||
|
import { speak } from "@/lib/tts";
|
||||||
|
|
||||||
|
export function useChat() {
|
||||||
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const historyRef = useRef<{ role: string; content: string }[]>([]);
|
||||||
|
|
||||||
|
const sendMessage = useCallback(
|
||||||
|
async (content: string, source: "voice" | "text" = "text") => {
|
||||||
|
if (!content.trim()) return;
|
||||||
|
|
||||||
|
const userMsg: Message = {
|
||||||
|
id: crypto.randomUUID(),
|
||||||
|
role: "user",
|
||||||
|
content,
|
||||||
|
source,
|
||||||
|
timestamp: new Date(),
|
||||||
|
};
|
||||||
|
setMessages((prev) => [...prev, userMsg]);
|
||||||
|
historyRef.current.push({ role: "user", content });
|
||||||
|
|
||||||
|
const assistantId = crypto.randomUUID();
|
||||||
|
setMessages((prev) => [
|
||||||
|
...prev,
|
||||||
|
{ id: assistantId, role: "assistant", content: "", source, timestamp: new Date() },
|
||||||
|
]);
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/chat", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ messages: historyRef.current }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const reader = res.body!.getReader();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
let fullText = "";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
|
||||||
|
for (const line of decoder.decode(value).split("\n")) {
|
||||||
|
if (!line.startsWith("data: ")) continue;
|
||||||
|
const json = line.slice(6).trim();
|
||||||
|
if (json === "[DONE]") continue;
|
||||||
|
try {
|
||||||
|
const delta = JSON.parse(json).choices?.[0]?.delta?.content ?? "";
|
||||||
|
fullText += delta;
|
||||||
|
setMessages((prev) =>
|
||||||
|
prev.map((m) =>
|
||||||
|
m.id === assistantId ? { ...m, content: fullText } : m
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
historyRef.current.push({ role: "assistant", content: fullText });
|
||||||
|
if (source === "voice") speak(fullText);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
return { messages, isLoading, sendMessage };
|
||||||
|
}
|
||||||
46
src/hooks/useVoiceRecorder.ts
Normal file
46
src/hooks/useVoiceRecorder.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// src/hooks/useVoiceRecorder.ts
|
||||||
|
"use client";
|
||||||
|
import { useRef, useState, useCallback } from "react";
|
||||||
|
|
||||||
|
export function useVoiceRecorder() {
|
||||||
|
const [isRecording, setIsRecording] = useState(false);
|
||||||
|
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
||||||
|
const chunksRef = useRef<Blob[]>([]);
|
||||||
|
|
||||||
|
const startRecording = useCallback(async () => {
|
||||||
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
|
const recorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
||||||
|
chunksRef.current = [];
|
||||||
|
recorder.ondataavailable = (e) => {
|
||||||
|
if (e.data.size > 0) chunksRef.current.push(e.data);
|
||||||
|
};
|
||||||
|
mediaRecorderRef.current = recorder;
|
||||||
|
recorder.start(250);
|
||||||
|
setIsRecording(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Returns a Float32Array that Transformers.js natively accepts
|
||||||
|
const stopRecording = useCallback((): Promise<Float32Array> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const recorder = mediaRecorderRef.current;
|
||||||
|
if (!recorder) return reject("No recorder active");
|
||||||
|
|
||||||
|
recorder.onstop = async () => {
|
||||||
|
recorder.stream.getTracks().forEach((t) => t.stop());
|
||||||
|
setIsRecording(false);
|
||||||
|
|
||||||
|
const blob = new Blob(chunksRef.current, { type: "audio/webm" });
|
||||||
|
const arrayBuffer = await blob.arrayBuffer();
|
||||||
|
const audioCtx = new AudioContext({ sampleRate: 16000 });
|
||||||
|
const decoded = await audioCtx.decodeAudioData(arrayBuffer);
|
||||||
|
|
||||||
|
// Whisper expects mono 16kHz Float32Array
|
||||||
|
resolve(decoded.getChannelData(0));
|
||||||
|
};
|
||||||
|
|
||||||
|
recorder.stop();
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { isRecording, startRecording, stopRecording };
|
||||||
|
}
|
||||||
29
src/hooks/useWhisper.ts
Normal file
29
src/hooks/useWhisper.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// src/hooks/useWhisper.ts
|
||||||
|
"use client";
|
||||||
|
import { useState, useCallback } from "react";
|
||||||
|
import { WhisperStatus } from "@/lib/types";
|
||||||
|
|
||||||
|
export function useWhisper() {
|
||||||
|
const [status, setStatus] = useState<WhisperStatus>("ready");
|
||||||
|
const [modelMessage, setModelMessage] = useState("");
|
||||||
|
|
||||||
|
const transcribe = useCallback(async (audio: Float32Array): Promise<string> => {
|
||||||
|
setStatus("transcribing");
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/transcribe", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ audio: Array.from(audio) }),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
return data.text ?? "";
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Transcription error:", err);
|
||||||
|
return "";
|
||||||
|
} finally {
|
||||||
|
setStatus("ready");
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { status, modelMessage, transcribe };
|
||||||
|
}
|
||||||
33
src/lib/tts.ts
Normal file
33
src/lib/tts.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// src/lib/tts.ts
|
||||||
|
export function speak(text: string, onEnd?: () => void): void {
|
||||||
|
if (typeof window === "undefined" || !window.speechSynthesis) return;
|
||||||
|
window.speechSynthesis.cancel();
|
||||||
|
|
||||||
|
const utterance = new SpeechSynthesisUtterance(text);
|
||||||
|
utterance.rate = 1.05;
|
||||||
|
utterance.pitch = 1.0;
|
||||||
|
|
||||||
|
// Wait for voices to load (Safari needs this)
|
||||||
|
const trySpeak = () => {
|
||||||
|
const voices = window.speechSynthesis.getVoices();
|
||||||
|
const preferred = voices.find(
|
||||||
|
(v) =>
|
||||||
|
v.name.includes("Samantha") ||
|
||||||
|
v.name.includes("Google UK English Female") ||
|
||||||
|
v.name.includes("Google US English")
|
||||||
|
);
|
||||||
|
if (preferred) utterance.voice = preferred;
|
||||||
|
if (onEnd) utterance.onend = onEnd;
|
||||||
|
window.speechSynthesis.speak(utterance);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (window.speechSynthesis.getVoices().length > 0) {
|
||||||
|
trySpeak();
|
||||||
|
} else {
|
||||||
|
window.speechSynthesis.onvoiceschanged = trySpeak;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stopSpeaking(): void {
|
||||||
|
window.speechSynthesis?.cancel();
|
||||||
|
}
|
||||||
15
src/lib/types.ts
Normal file
15
src/lib/types.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// src/lib/types.ts
|
||||||
|
export interface Message {
|
||||||
|
id: string;
|
||||||
|
role: "user" | "assistant";
|
||||||
|
content: string;
|
||||||
|
source?: "voice" | "text";
|
||||||
|
timestamp: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type WhisperStatus =
|
||||||
|
| "idle"
|
||||||
|
| "loading"
|
||||||
|
| "ready"
|
||||||
|
| "recording"
|
||||||
|
| "transcribing";
|
||||||
34
tsconfig.json
Normal file
34
tsconfig.json
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2017",
|
||||||
|
"lib": ["dom", "dom.iterable", "esnext"],
|
||||||
|
"allowJs": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"strict": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"incremental": true,
|
||||||
|
"plugins": [
|
||||||
|
{
|
||||||
|
"name": "next"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["./src/*"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"next-env.d.ts",
|
||||||
|
"**/*.ts",
|
||||||
|
"**/*.tsx",
|
||||||
|
".next/types/**/*.ts",
|
||||||
|
".next/dev/types/**/*.ts",
|
||||||
|
"**/*.mts"
|
||||||
|
],
|
||||||
|
"exclude": ["node_modules"]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user