Web interface (React/TS, shadcn, pure OpenAI-API client) under web/ (#23)
This commit is contained in:
+184
@@ -0,0 +1,184 @@
|
||||
import { useEffect, useMemo, useRef, useState } from "react"
|
||||
import {
|
||||
Activity,
|
||||
ArrowUp,
|
||||
BrainCircuit,
|
||||
CircleStop,
|
||||
Cpu,
|
||||
Feather,
|
||||
KeyRound,
|
||||
Link2,
|
||||
LoaderCircle,
|
||||
MessageSquareText,
|
||||
RefreshCw,
|
||||
SlidersHorizontal,
|
||||
Trash2,
|
||||
} from "lucide-react"
|
||||
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { listModels, streamChat, type ChatMessage } from "@/lib/api"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const stored = (key: string, fallback: string) => localStorage.getItem(key) || fallback
|
||||
const message = (role: ChatMessage["role"], content: string): ChatMessage => ({ id: crypto.randomUUID(), role, content })
|
||||
|
||||
export default function App() {
|
||||
const [baseUrl, setBaseUrl] = useState(() => stored("colibri.baseUrl", "http://127.0.0.1:8000/v1"))
|
||||
const [apiKey, setApiKey] = useState(() => stored("colibri.apiKey", ""))
|
||||
const [models, setModels] = useState<string[]>([])
|
||||
const [model, setModel] = useState(() => stored("colibri.model", "glm-5.2-colibri"))
|
||||
const [temperature, setTemperature] = useState(0.7)
|
||||
const [maxTokens, setMaxTokens] = useState(512)
|
||||
const [thinking, setThinking] = useState(false)
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([])
|
||||
const [draft, setDraft] = useState("")
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [connecting, setConnecting] = useState(false)
|
||||
const [connected, setConnected] = useState(false)
|
||||
const [error, setError] = useState("")
|
||||
const abortRef = useRef<AbortController | null>(null)
|
||||
const bottomRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
localStorage.setItem("colibri.baseUrl", baseUrl)
|
||||
localStorage.setItem("colibri.apiKey", apiKey)
|
||||
localStorage.setItem("colibri.model", model)
|
||||
}, [apiKey, baseUrl, model])
|
||||
|
||||
useEffect(() => bottomRef.current?.scrollIntoView({ behavior: "smooth" }), [messages])
|
||||
|
||||
const connect = async () => {
|
||||
setConnecting(true)
|
||||
setError("")
|
||||
try {
|
||||
const found = await listModels(baseUrl, apiKey)
|
||||
setModels(found)
|
||||
if (found.length && !found.includes(model)) setModel(found[0])
|
||||
setConnected(true)
|
||||
} catch (cause) {
|
||||
setConnected(false)
|
||||
setError(cause instanceof Error ? cause.message : "Could not reach the server.")
|
||||
} finally {
|
||||
setConnecting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const canSend = useMemo(() => draft.trim() && model && !loading, [draft, loading, model])
|
||||
|
||||
const send = async () => {
|
||||
const content = draft.trim()
|
||||
if (!content || loading) return
|
||||
const user = message("user", content)
|
||||
const assistant = message("assistant", "")
|
||||
const history = [...messages, user]
|
||||
setDraft("")
|
||||
setError("")
|
||||
setMessages([...history, assistant])
|
||||
setLoading(true)
|
||||
const controller = new AbortController()
|
||||
abortRef.current = controller
|
||||
try {
|
||||
await streamChat({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
model,
|
||||
messages: history,
|
||||
temperature,
|
||||
maxTokens,
|
||||
enableThinking: thinking,
|
||||
signal: controller.signal,
|
||||
onDelta: (delta) =>
|
||||
setMessages((current) => current.map((item) =>
|
||||
item.id === assistant.id ? { ...item, content: item.content + delta } : item,
|
||||
)),
|
||||
})
|
||||
setConnected(true)
|
||||
} catch (cause) {
|
||||
if (controller.signal.aborted) {
|
||||
setMessages((current) => current.filter((item) => item.id !== assistant.id || item.content))
|
||||
} else {
|
||||
setError(cause instanceof Error ? cause.message : "Generation failed.")
|
||||
setMessages((current) => current.filter((item) => item.id !== assistant.id || item.content))
|
||||
}
|
||||
} finally {
|
||||
abortRef.current = null
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="app-shell">
|
||||
<aside className="sidebar">
|
||||
<div className="brand-row">
|
||||
<div className="brand-mark"><Feather className="size-5" /></div>
|
||||
<div><h1>colibrì</h1><p>local giant, tiny footprint</p></div>
|
||||
</div>
|
||||
|
||||
<section className="side-section">
|
||||
<div className="section-title"><Link2 className="size-3.5" /> Connection</div>
|
||||
<label>API endpoint<Input value={baseUrl} onChange={(event) => setBaseUrl(event.target.value)} /></label>
|
||||
<label>API key<div className="relative"><KeyRound className="field-icon" /><Input className="pl-9" type="password" value={apiKey} placeholder="optional" onChange={(event) => setApiKey(event.target.value)} /></div></label>
|
||||
<Button variant="secondary" onClick={connect} disabled={connecting}>
|
||||
{connecting ? <LoaderCircle className="size-4 animate-spin" /> : <RefreshCw className="size-4" />}
|
||||
Probe server
|
||||
</Button>
|
||||
<div className={cn("connection-state", connected && "connected")}><span />{connected ? "Engine reachable" : "Not connected"}</div>
|
||||
</section>
|
||||
|
||||
<section className="side-section">
|
||||
<div className="section-title"><SlidersHorizontal className="size-3.5" /> Inference</div>
|
||||
<label>Model<select value={model} onChange={(event) => setModel(event.target.value)}>{models.length ? models.map((id) => <option key={id}>{id}</option>) : <option>{model}</option>}</select></label>
|
||||
<label><span className="label-line"><span>Temperature</span><code>{temperature.toFixed(1)}</code></span><input className="range" type="range" min="0" max="2" step="0.1" value={temperature} onChange={(event) => setTemperature(Number(event.target.value))} /></label>
|
||||
<label>Max output tokens<Input type="number" min={1} max={4096} value={maxTokens} onChange={(event) => setMaxTokens(Number(event.target.value))} /></label>
|
||||
<button className={cn("toggle-row", thinking && "active")} onClick={() => setThinking((value) => !value)}>
|
||||
<span><BrainCircuit className="size-4" /> Reasoning</span><i><b /></i>
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<div className="sidebar-foot"><Cpu className="size-3.5" /><span>OpenAI-compatible transport</span></div>
|
||||
</aside>
|
||||
|
||||
<main className="chat-panel">
|
||||
<header className="topbar">
|
||||
<div><span className="eyebrow">ACTIVE MODEL</span><strong>{model}</strong></div>
|
||||
<div className="top-actions"><Badge><Activity className="size-3" /> stream</Badge><Button variant="ghost" size="sm" onClick={() => setMessages([])} disabled={!messages.length || loading}><Trash2 className="size-3.5" /> Clear</Button></div>
|
||||
</header>
|
||||
|
||||
<div className="conversation">
|
||||
{!messages.length ? (
|
||||
<div className="empty-state">
|
||||
<div className="orb"><Feather /></div>
|
||||
<span className="eyebrow">COLIBRÌ ENGINE</span>
|
||||
<h2>Ask the giant.<br /><em>Keep the machine yours.</em></h2>
|
||||
<p>Connect to a local colibrì server and stream responses directly from your hardware. Nothing leaves the endpoint you choose.</p>
|
||||
<div className="suggestions">
|
||||
{["Explain how expert routing works", "Write a small C benchmark", "Compare RAM and VRAM caching"].map((item) => <button key={item} onClick={() => setDraft(item)}>{item}<ArrowUp className="size-3.5 rotate-45" /></button>)}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="message-list">
|
||||
{messages.map((item) => (
|
||||
<article key={item.id} className={cn("message", item.role)}>
|
||||
<div className="avatar">{item.role === "user" ? "Y" : <Feather className="size-4" />}</div>
|
||||
<div><div className="message-meta">{item.role === "user" ? "You" : "colibrì"}</div><div className="message-body">{item.content || <span className="typing"><i /><i /><i /></span>}</div></div>
|
||||
</article>
|
||||
))}
|
||||
<div ref={bottomRef} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="composer-wrap">
|
||||
{error && <div className="error-banner">{error}</div>}
|
||||
<div className="composer">
|
||||
<Textarea value={draft} onChange={(event) => setDraft(event.target.value)} placeholder="Message colibrì…" onKeyDown={(event) => { if (event.key === "Enter" && !event.shiftKey) { event.preventDefault(); void send() } }} />
|
||||
<div className="composer-foot"><span><MessageSquareText className="size-3.5" /> Enter to send · Shift+Enter for newline</span>{loading ? <Button variant="destructive" size="icon" aria-label="Stop generation" onClick={() => abortRef.current?.abort()}><CircleStop className="size-4" /></Button> : <Button size="icon" aria-label="Send message" disabled={!canSend} onClick={() => void send()}><ArrowUp className="size-4" /></Button>}</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Badge({ className, ...props }: React.ComponentProps<"span">) {
|
||||
return <span data-slot="badge" className={cn("inline-flex items-center rounded-full border border-border px-2 py-0.5 text-[11px] font-medium text-muted-foreground", className)} {...props} />
|
||||
}
|
||||
|
||||
export { Badge }
|
||||
@@ -0,0 +1,26 @@
|
||||
import * as React from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
const buttonVariants = cva(
|
||||
"inline-flex items-center justify-center gap-2 rounded-lg text-sm font-medium transition-colors outline-none disabled:pointer-events-none disabled:opacity-45 focus-visible:ring-2 focus-visible:ring-ring",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-primary text-primary-foreground shadow-[0_0_24px_-8px_var(--primary)] hover:bg-primary/90",
|
||||
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
||||
ghost: "text-muted-foreground hover:bg-secondary hover:text-foreground",
|
||||
destructive: "bg-destructive/15 text-destructive hover:bg-destructive/25",
|
||||
},
|
||||
size: { default: "h-10 px-4", sm: "h-8 px-3 text-xs", icon: "size-10" },
|
||||
},
|
||||
defaultVariants: { variant: "default", size: "default" },
|
||||
},
|
||||
)
|
||||
|
||||
function Button({ className, variant, size, ...props }: React.ComponentProps<"button"> & VariantProps<typeof buttonVariants>) {
|
||||
return <button data-slot="button" className={cn(buttonVariants({ variant, size, className }))} {...props} />
|
||||
}
|
||||
|
||||
export { Button, buttonVariants }
|
||||
@@ -0,0 +1,16 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
return (
|
||||
<input
|
||||
type={type}
|
||||
data-slot="input"
|
||||
className={cn("h-10 w-full rounded-lg border border-border bg-input/50 px-3 text-sm outline-none transition focus:border-primary/60 focus:ring-2 focus:ring-primary/15", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Input }
|
||||
@@ -0,0 +1,15 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
|
||||
return (
|
||||
<textarea
|
||||
data-slot="textarea"
|
||||
className={cn("min-h-20 w-full resize-none rounded-xl border border-border bg-transparent px-4 py-3 text-sm leading-6 outline-none placeholder:text-muted-foreground focus:border-primary/60", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Textarea }
|
||||
@@ -0,0 +1,80 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
@theme inline {
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--color-card: var(--card);
|
||||
--color-card-foreground: var(--foreground);
|
||||
--color-primary: var(--primary);
|
||||
--color-primary-foreground: var(--primary-foreground);
|
||||
--color-secondary: var(--secondary);
|
||||
--color-secondary-foreground: var(--foreground);
|
||||
--color-muted-foreground: var(--muted-foreground);
|
||||
--color-border: var(--border);
|
||||
--color-input: var(--input);
|
||||
--color-ring: var(--primary);
|
||||
--color-destructive: var(--destructive);
|
||||
}
|
||||
|
||||
:root {
|
||||
color-scheme: dark;
|
||||
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
||||
--background: #080b0d;
|
||||
--foreground: #e9eff0;
|
||||
--card: #0d1215;
|
||||
--primary: #4ed6a5;
|
||||
--primary-foreground: #052118;
|
||||
--secondary: #151c20;
|
||||
--muted-foreground: #829095;
|
||||
--border: #202a2f;
|
||||
--input: #10171a;
|
||||
--destructive: #ff766f;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
html, body, #root { min-height: 100%; margin: 0; }
|
||||
body { background: var(--background); color: var(--foreground); }
|
||||
button, input, textarea, select { font: inherit; }
|
||||
button { cursor: pointer; }
|
||||
|
||||
.app-shell { min-height: 100vh; display: grid; grid-template-columns: 292px minmax(0, 1fr); background: radial-gradient(circle at 72% -20%, rgba(78,214,165,.08), transparent 34%), var(--background); }
|
||||
.sidebar { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 30px; padding: 24px 20px; border-right: 1px solid var(--border); background: rgba(8,11,13,.86); backdrop-filter: blur(18px); }
|
||||
.brand-row { display: flex; align-items: center; gap: 12px; }
|
||||
.brand-mark { display: grid; place-items: center; width: 38px; height: 38px; border: 1px solid rgba(78,214,165,.3); border-radius: 12px; color: var(--primary); background: rgba(78,214,165,.08); transform: rotate(-4deg); }
|
||||
.brand-row h1 { margin: 0; font-family: Georgia, serif; font-size: 22px; font-style: italic; letter-spacing: -.02em; }
|
||||
.brand-row p { margin: 2px 0 0; color: var(--muted-foreground); font-size: 10px; letter-spacing: .08em; text-transform: uppercase; }
|
||||
.side-section { display: grid; gap: 13px; }
|
||||
.section-title { display: flex; align-items: center; gap: 7px; color: #66747a; font-size: 10px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; }
|
||||
.side-section label { display: grid; gap: 7px; color: #a9b4b8; font-size: 11px; font-weight: 600; }
|
||||
.side-section select { width: 100%; height: 40px; padding: 0 32px 0 11px; border: 1px solid var(--border); border-radius: 8px; color: var(--foreground); background: var(--input); outline: none; }
|
||||
.field-icon { position: absolute; top: 12px; left: 12px; width: 15px; color: #647177; }
|
||||
.label-line { display: flex; justify-content: space-between; align-items: center; }
|
||||
.label-line code { color: var(--primary); font-size: 11px; }
|
||||
.range { width: 100%; accent-color: var(--primary); }
|
||||
.connection-state { display: flex; align-items: center; gap: 7px; color: #69767b; font-size: 10px; }
|
||||
.connection-state span { width: 6px; height: 6px; border-radius: 99px; background: #4b5559; }
|
||||
.connection-state.connected { color: #8abfa9; }.connection-state.connected span { background: var(--primary); box-shadow: 0 0 10px var(--primary); }
|
||||
.toggle-row { display: flex; align-items: center; justify-content: space-between; height: 42px; padding: 0 11px; border: 1px solid var(--border); border-radius: 9px; color: #a9b4b8; background: var(--input); }
|
||||
.toggle-row > span { display: flex; align-items: center; gap: 8px; font-size: 11px; font-weight: 600; }.toggle-row.active { border-color: rgba(78,214,165,.35); color: var(--foreground); }
|
||||
.toggle-row i { width: 30px; height: 17px; padding: 2px; border-radius: 20px; background: #293136; transition: .2s; }.toggle-row i b { display: block; width: 13px; height: 13px; border-radius: 50%; background: #78858a; transition: .2s; }.toggle-row.active i { background: rgba(78,214,165,.28); }.toggle-row.active i b { transform: translateX(13px); background: var(--primary); }
|
||||
.sidebar-foot { margin-top: auto; display: flex; align-items: center; gap: 7px; color: #59666b; font-size: 10px; }
|
||||
|
||||
.chat-panel { min-width: 0; height: 100vh; display: grid; grid-template-rows: 72px minmax(0, 1fr) auto; }
|
||||
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 0 32px; border-bottom: 1px solid var(--border); }
|
||||
.topbar > div:first-child { display: grid; gap: 3px; }.topbar strong { font: 500 14px ui-monospace, SFMono-Regular, monospace; }.eyebrow { color: #5e6d72; font-size: 9px; font-weight: 800; letter-spacing: .16em; }
|
||||
.top-actions { display: flex; align-items: center; gap: 8px; }.top-actions [data-slot=badge] { gap: 5px; }
|
||||
.conversation { overflow-y: auto; }
|
||||
.empty-state { width: min(680px, calc(100% - 40px)); min-height: 100%; margin: 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 56px 0; text-align: center; }
|
||||
.orb { position: relative; display: grid; place-items: center; width: 66px; height: 66px; margin-bottom: 24px; border: 1px solid rgba(78,214,165,.25); border-radius: 50%; color: var(--primary); background: radial-gradient(circle, rgba(78,214,165,.17), rgba(78,214,165,.02) 68%); box-shadow: 0 0 55px rgba(78,214,165,.1); }.orb svg { width: 25px; }.orb::after { content: ""; position: absolute; inset: -7px; border: 1px dashed rgba(78,214,165,.14); border-radius: inherit; }
|
||||
.empty-state h2 { margin: 14px 0 14px; font-family: Georgia, serif; font-size: clamp(34px, 5vw, 55px); font-weight: 400; line-height: 1.02; letter-spacing: -.045em; }.empty-state h2 em { color: var(--primary); font-weight: 400; }.empty-state > p { max-width: 510px; margin: 0; color: var(--muted-foreground); font-size: 13px; line-height: 1.7; }
|
||||
.suggestions { width: 100%; display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; margin-top: 38px; }.suggestions button { display: flex; justify-content: space-between; align-items: center; min-height: 62px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 11px; color: #a5b0b4; background: rgba(13,18,21,.75); text-align: left; font-size: 11px; line-height: 1.4; transition: .2s; }.suggestions button:hover { border-color: rgba(78,214,165,.35); color: var(--foreground); transform: translateY(-2px); }
|
||||
.message-list { width: min(820px, calc(100% - 48px)); margin: 0 auto; padding: 44px 0 20px; }
|
||||
.message { display: grid; grid-template-columns: 34px minmax(0,1fr); gap: 14px; margin-bottom: 34px; }.avatar { display: grid; place-items: center; width: 31px; height: 31px; border: 1px solid var(--border); border-radius: 9px; color: #aeb8bc; background: var(--card); font-size: 11px; font-weight: 700; }.message.assistant .avatar { color: var(--primary); border-color: rgba(78,214,165,.25); background: rgba(78,214,165,.06); }.message-meta { height: 27px; color: #7f8c91; font-size: 10px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; }.message-body { color: #d8e0e2; font-size: 14px; line-height: 1.75; white-space: pre-wrap; overflow-wrap: anywhere; }.message.user .message-body { color: #aeb9bd; }
|
||||
.typing { display: inline-flex; gap: 4px; padding-top: 6px; }.typing i { width: 5px; height: 5px; border-radius: 50%; background: var(--primary); animation: pulse 1.1s infinite; }.typing i:nth-child(2) { animation-delay: .15s; }.typing i:nth-child(3) { animation-delay: .3s; }
|
||||
.composer-wrap { padding: 10px 32px 24px; background: linear-gradient(transparent, var(--background) 24%); }.composer { width: min(820px, 100%); margin: 0 auto; overflow: hidden; border: 1px solid #2a353a; border-radius: 15px; background: rgba(13,18,21,.94); box-shadow: 0 18px 55px rgba(0,0,0,.24); }.composer textarea { min-height: 74px; border: 0; }.composer-foot { display: flex; align-items: center; justify-content: space-between; padding: 0 9px 9px 15px; }.composer-foot > span { display: flex; align-items: center; gap: 6px; color: #56646a; font-size: 9px; }.error-banner { width: min(820px, 100%); margin: 0 auto 8px; padding: 9px 12px; border: 1px solid rgba(255,118,111,.25); border-radius: 9px; color: #ff9d98; background: rgba(255,118,111,.07); font-size: 11px; }
|
||||
@keyframes pulse { 0%, 70%, 100% { opacity: .25; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-3px); } }
|
||||
|
||||
@media (max-width: 820px) {
|
||||
.app-shell { grid-template-columns: 1fr; }.sidebar { position: static; width: 100%; height: auto; display: grid; grid-template-columns: 1fr 1fr; border-right: 0; border-bottom: 1px solid var(--border); }.brand-row, .sidebar-foot { grid-column: 1/-1; }.chat-panel { height: 100svh; }.suggestions { grid-template-columns: 1fr; }.topbar, .composer-wrap { padding-left: 16px; padding-right: 16px; }
|
||||
}
|
||||
@media (max-width: 560px) { .sidebar { grid-template-columns: 1fr; }.brand-row, .sidebar-foot { grid-column: auto; }.empty-state h2 { font-size: 36px; }.message-list { width: calc(100% - 28px); }.composer-foot > span { display: none; } }
|
||||
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { extractSSE } from "./api"
|
||||
|
||||
describe("extractSSE", () => {
|
||||
it("keeps an incomplete frame for the next network chunk", () => {
|
||||
const parsed = extractSSE('data: {"choices":[]}\n\ndata: {"cho')
|
||||
expect(parsed.data).toEqual(['{"choices":[]}'])
|
||||
expect(parsed.rest).toBe('data: {"cho')
|
||||
})
|
||||
|
||||
it("supports CRLF and multiple data frames", () => {
|
||||
const parsed = extractSSE("data: one\r\n\r\ndata: two\r\n\r\n")
|
||||
expect(parsed.data).toEqual(["one", "two"])
|
||||
expect(parsed.rest).toBe("")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,104 @@
|
||||
export type ChatRole = "system" | "user" | "assistant"
|
||||
|
||||
export interface ChatMessage {
|
||||
id: string
|
||||
role: ChatRole
|
||||
content: string
|
||||
}
|
||||
|
||||
interface OpenAIError {
|
||||
error?: { message?: string }
|
||||
}
|
||||
|
||||
function endpoint(baseUrl: string, path: string) {
|
||||
return `${baseUrl.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`
|
||||
}
|
||||
|
||||
function headers(apiKey: string) {
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
async function responseError(response: Response) {
|
||||
const fallback = `${response.status} ${response.statusText}`
|
||||
try {
|
||||
const body = (await response.json()) as OpenAIError
|
||||
return body.error?.message || fallback
|
||||
} catch {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
export async function listModels(baseUrl: string, apiKey: string, signal?: AbortSignal) {
|
||||
const response = await fetch(endpoint(baseUrl, "models"), { headers: headers(apiKey), signal })
|
||||
if (!response.ok) throw new Error(await responseError(response))
|
||||
const body = (await response.json()) as { data?: Array<{ id: string }> }
|
||||
return (body.data || []).map((model) => model.id)
|
||||
}
|
||||
|
||||
export function extractSSE(buffer: string) {
|
||||
const frames = buffer.split(/\r?\n\r?\n/)
|
||||
const rest = frames.pop() || ""
|
||||
const data = frames.flatMap((frame) =>
|
||||
frame
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).trimStart()),
|
||||
)
|
||||
return { data, rest }
|
||||
}
|
||||
|
||||
interface StreamChatOptions {
|
||||
baseUrl: string
|
||||
apiKey: string
|
||||
model: string
|
||||
messages: ChatMessage[]
|
||||
temperature: number
|
||||
maxTokens: number
|
||||
enableThinking: boolean
|
||||
signal: AbortSignal
|
||||
onDelta: (text: string) => void
|
||||
}
|
||||
|
||||
export async function streamChat(options: StreamChatOptions) {
|
||||
const response = await fetch(endpoint(options.baseUrl, "chat/completions"), {
|
||||
method: "POST",
|
||||
headers: headers(options.apiKey),
|
||||
signal: options.signal,
|
||||
body: JSON.stringify({
|
||||
model: options.model,
|
||||
messages: options.messages.map(({ role, content }) => ({ role, content })),
|
||||
temperature: options.temperature,
|
||||
max_completion_tokens: options.maxTokens,
|
||||
enable_thinking: options.enableThinking,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}),
|
||||
})
|
||||
if (!response.ok) throw new Error(await responseError(response))
|
||||
if (!response.body) throw new Error("The server returned an empty stream.")
|
||||
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ""
|
||||
|
||||
const consume = (data: string) => {
|
||||
if (data === "[DONE]") return
|
||||
const event = JSON.parse(data) as {
|
||||
choices?: Array<{ delta?: { content?: string } }>
|
||||
}
|
||||
const text = event.choices?.[0]?.delta?.content
|
||||
if (text) options.onDelta(text)
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const { value, done } = await reader.read()
|
||||
buffer += decoder.decode(value, { stream: !done })
|
||||
const parsed = extractSSE(buffer)
|
||||
buffer = parsed.rest
|
||||
parsed.data.forEach(consume)
|
||||
if (done) break
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { StrictMode } from "react"
|
||||
import { createRoot } from "react-dom/client"
|
||||
|
||||
import App from "./App"
|
||||
import "./index.css"
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
||||
Reference in New Issue
Block a user