nanochat/services/frontend/hooks/useSSE.ts
Manmohan Sharma 634be4080b
feat(frontend): Next.js 14 frontend service for samosaChaat (#2)
Build services/frontend/ replacing the legacy nanochat/ui.html single-file UI.
Landing, login, and chat pages ported with full design system: Devanagari +
Great Vibes hero, samosa/chai/toran SVG animations, gold/cream palette.

- App Router pages: / (hero + floating illustrations), /login (split-screen
  OAuth with mandala motif), /chat (260px collapsible sidebar, suggestion
  chips, markdown + code-copy, auto-expanding input, slash commands)
- SSE streaming via useSSE hook and /api/chat/stream BFF route (proxies to
  CHAT_API_URL when set, falls back to mock echo for local dev)
- NextAuth.js v5 with Google + GitHub providers; middleware gates /chat/*
- Zustand store with localStorage persistence for conversations/settings
- Tailwind theme carries all ui.html tokens + keyframes (pendulum, float,
  wobble, steamFloat, steamType); SVG assets componentized under components/svg
- Multi-stage node:20-alpine Dockerfile with Next standalone output

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-16 11:26:57 -07:00

94 lines
2.5 KiB
TypeScript

'use client';
import { useCallback, useRef, useState } from 'react';
export interface SSEOptions {
onToken?: (token: string, gpu?: number) => void;
onDone?: () => void;
onError?: (err: Error) => void;
}
export interface StreamRequest {
messages: Array<{ role: string; content: string }>;
model?: string;
temperature?: number;
topK?: number;
}
export function useSSE(endpoint: string, options: SSEOptions = {}) {
const [isStreaming, setIsStreaming] = useState(false);
const abortRef = useRef<AbortController | null>(null);
const stop = useCallback(() => {
abortRef.current?.abort();
abortRef.current = null;
setIsStreaming(false);
}, []);
const start = useCallback(
async (body: StreamRequest) => {
stop();
const ac = new AbortController();
abortRef.current = ac;
setIsStreaming(true);
try {
const res = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
signal: ac.signal,
});
if (!res.ok || !res.body) {
throw new Error(`HTTP ${res.status}`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let nl: number;
while ((nl = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, nl).trim();
buffer = buffer.slice(nl + 1);
if (!line.startsWith('data:')) continue;
const payload = line.slice(5).trim();
if (!payload) continue;
try {
const data = JSON.parse(payload);
if (data.done) {
options.onDone?.();
setIsStreaming(false);
return;
}
if (typeof data.token === 'string') {
options.onToken?.(data.token, data.gpu);
}
} catch {
/* swallow malformed chunks */
}
}
}
options.onDone?.();
} catch (err) {
if ((err as Error).name !== 'AbortError') {
options.onError?.(err as Error);
}
} finally {
setIsStreaming(false);
abortRef.current = null;
}
},
[endpoint, options, stop],
);
return { start, stop, isStreaming };
}