Spaces:
Running
Running
File size: 13,893 Bytes
3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 837e3ac 3f76ff4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | "use client";
import {
useEffect,
useRef,
useState,
type Dispatch,
type SetStateAction,
} from "react";
import type {
ChatMessage,
ConnectionStatus,
WorkPackage,
} from "@/lib/work-package-types";
import type { LlmConfig } from "@/lib/llm-config";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { ScrollArea } from "@/components/ui/scroll-area";
import {
applyComposerSuggestion,
getComposerSuggestions,
type ComposerSuggestion,
} from "@/lib/composer-autocomplete";
import {
AlertCircle,
CheckCircle2,
LoaderCircle,
Send,
Settings2,
Sparkles,
X,
} from "lucide-react";
function statusTone(status: ConnectionStatus) {
if (status === "connected") return "text-emerald-700";
if (status === "checking") return "text-amber-700";
if (status === "error") return "text-rose-700";
return "text-muted-foreground";
}
function StatusIcon(props: { status: ConnectionStatus }) {
const { status } = props;
if (status === "connected") return <CheckCircle2 className="h-3.5 w-3.5" />;
if (status === "checking") {
return <LoaderCircle className="h-3.5 w-3.5 animate-spin" />;
}
if (status === "error") return <AlertCircle className="h-3.5 w-3.5" />;
return <Settings2 className="h-3.5 w-3.5" />;
}
export function ChatPanel(props: {
messages: ChatMessage[];
draft: string;
setDraft: (v: string) => void;
busy: boolean;
productTitle: string;
currentActivity: string;
workPackages: WorkPackage[];
selectedWorkPackageId?: string;
onSelectWorkPackage: (id: string) => void;
llmConfig: LlmConfig;
setLlmConfig: Dispatch<SetStateAction<LlmConfig>>;
isMockMode: boolean;
connectionStatus: ConnectionStatus;
connectionMessage: string;
canTestConnection: boolean;
onTestConnection: () => void;
onSend: (text: string) => void | Promise<void>;
initialSettingsOpen?: boolean;
}) {
const {
messages,
draft,
setDraft,
busy,
productTitle,
currentActivity,
workPackages,
selectedWorkPackageId,
onSelectWorkPackage,
llmConfig,
setLlmConfig,
isMockMode,
connectionStatus,
connectionMessage,
canTestConnection,
onTestConnection,
onSend,
initialSettingsOpen,
} = props;
const bottomRef = useRef<HTMLDivElement | null>(null);
const [settingsOpen, setSettingsOpen] = useState(initialSettingsOpen ?? false);
const [activeSuggestionIndex, setActiveSuggestionIndex] = useState(0);
const showSettings = settingsOpen;
const selectedWorkPackage = workPackages.find(
(workPackage) => workPackage.id === selectedWorkPackageId,
);
const suggestions = getComposerSuggestions(draft, workPackages);
useEffect(() => {
bottomRef.current?.scrollIntoView({ block: "end" });
}, [messages.length]);
function applySuggestion(suggestion: ComposerSuggestion) {
setDraft(
applyComposerSuggestion({
draft,
suggestion,
selectedWorkPackage,
}),
);
setActiveSuggestionIndex(0);
if (suggestion.kind === "package") {
onSelectWorkPackage(suggestion.id);
}
}
return (
<div className="flex h-full min-h-0 flex-col bg-muted/18">
<div className="px-4 py-3 md:px-5">
<div className="text-sm font-semibold">{productTitle}</div>
<div className="text-xs text-muted-foreground">
Use <span className="font-mono">@SRS ask</span>,{" "}
<span className="font-mono">@Design FMEA execute</span>,{" "}
<span className="font-mono">/plan</span>, or paste a product idea to
auto-run the whole board.
</div>
</div>
<div className="px-4 pb-2 md:px-5">
<div className="rounded-2xl bg-background/82 px-3 py-2 shadow-sm ring-1 ring-black/5">
<div className="flex items-center gap-2 text-[11px] font-medium">
<Sparkles className="h-3.5 w-3.5 text-primary" />
<span>{busy ? currentActivity : "Ready. You can describe a product or target a package directly."}</span>
</div>
<div className="mt-1 text-[11px] leading-5 text-muted-foreground">
Tip: type <span className="font-mono">@</span> for packages or{" "}
<span className="font-mono">/</span> for ask, plan, change, and
execute shortcuts.
</div>
</div>
</div>
<ScrollArea className="min-h-0 flex-1">
<div className="space-y-3 px-4 py-1 md:px-5">
{messages.map((message) => (
<div
key={message.id}
className={[
"max-w-[52rem] whitespace-pre-wrap rounded-xl px-3 py-2 text-sm leading-6 shadow-sm ring-1 ring-black/5",
message.role === "user"
? "ml-auto bg-secondary/90"
: "mr-auto bg-background",
].join(" ")}
>
<div className="mb-1 text-[11px] font-medium text-muted-foreground">
{message.role === "user" ? "You" : productTitle}
</div>
<div>{message.content}</div>
</div>
))}
<div ref={bottomRef} />
</div>
</ScrollArea>
<div className="relative p-4 pt-3 md:p-5 md:pt-3">
{suggestions.length ? (
<div className="absolute inset-x-4 bottom-[calc(100%+0.75rem)] z-20 rounded-2xl bg-background/96 p-2 shadow-lg ring-1 ring-black/10 md:inset-x-5">
<div className="space-y-1">
{suggestions.map((suggestion, index) => {
const active = index === activeSuggestionIndex;
return (
<button
key={
suggestion.kind === "package"
? suggestion.id
: suggestion.mode
}
type="button"
className={[
"flex w-full items-start justify-between gap-3 rounded-xl px-3 py-2 text-left transition-colors",
active ? "bg-muted" : "hover:bg-muted/70",
].join(" ")}
onClick={() => applySuggestion(suggestion)}
>
<div className="min-w-0">
<div className="text-sm font-medium">
{suggestion.kind === "package"
? `@${suggestion.shortName}`
: suggestion.label}
</div>
<div className="text-xs text-muted-foreground">
{suggestion.kind === "package"
? suggestion.title
: suggestion.description}
</div>
</div>
<div className="pt-0.5 text-[11px] text-muted-foreground">
{suggestion.kind === "package" ? "Package" : "Action"}
</div>
</button>
);
})}
</div>
</div>
) : null}
{showSettings ? (
<div className="absolute inset-x-4 bottom-[calc(100%+0.75rem)] z-20 rounded-2xl bg-background px-3 py-3 shadow-lg ring-1 ring-black/10 md:inset-x-5">
<div className="flex items-start justify-between gap-3">
<div>
<div className="text-xs font-semibold">Model Settings</div>
<div className="text-[11px] text-muted-foreground">
Stored locally in this browser. Live mode turns on after the connection test succeeds.
</div>
</div>
<Button
variant="ghost"
size="icon"
className="h-7 w-7 rounded-xl"
onClick={() => setSettingsOpen(false)}
>
<X className="h-4 w-4" />
</Button>
</div>
<div
className={[
"mt-3 flex items-center gap-2 rounded-xl bg-muted/28 px-2.5 py-2 text-[11px]",
statusTone(connectionStatus),
].join(" ")}
>
<StatusIcon status={connectionStatus} />
<span>{connectionMessage}</span>
</div>
<div className="mt-3 space-y-2.5">
<div>
<div className="mb-1 text-[11px] font-medium text-muted-foreground">
API Key
</div>
<Input
type="password"
value={llmConfig.apiKey}
placeholder="Paste your API key"
className="h-9 rounded-xl bg-background/92"
onChange={(e) =>
setLlmConfig((prev) => ({
...prev,
apiKey: e.target.value,
}))
}
/>
</div>
<div className="grid gap-2 md:grid-cols-2">
<div>
<div className="mb-1 text-[11px] font-medium text-muted-foreground">
Base URL
</div>
<Input
value={llmConfig.baseUrl}
className="h-9 rounded-xl bg-background/92"
onChange={(e) =>
setLlmConfig((prev) => ({
...prev,
baseUrl: e.target.value,
}))
}
/>
</div>
<div>
<div className="mb-1 text-[11px] font-medium text-muted-foreground">
Model
</div>
<Input
value={llmConfig.model}
className="h-9 rounded-xl bg-background/92"
onChange={(e) =>
setLlmConfig((prev) => ({
...prev,
model: e.target.value,
}))
}
/>
</div>
</div>
<div className="flex justify-end">
<Button
type="button"
variant="outline"
className="h-8 rounded-xl px-3 text-[11px]"
disabled={!canTestConnection || connectionStatus === "checking"}
onClick={onTestConnection}
>
<StatusIcon status={connectionStatus} />
<span className="ml-1">
{connectionStatus === "checking"
? "Testing connection..."
: "Test connection"}
</span>
</Button>
</div>
</div>
</div>
) : null}
<form
className="flex items-end gap-2.5"
onSubmit={(event) => {
event.preventDefault();
onSend(draft);
}}
>
<Textarea
value={draft}
onChange={(event) => {
setDraft(event.target.value);
setActiveSuggestionIndex(0);
}}
placeholder="Type a product idea, @Package..., or /ask /plan /change /execute ..."
className="min-h-[148px] resize-none rounded-[24px] bg-background/96 px-4 py-3.5 text-sm leading-6 shadow-sm ring-1 ring-black/5"
onKeyDown={(event) => {
if (suggestions.length && event.key === "ArrowDown") {
event.preventDefault();
setActiveSuggestionIndex((current) =>
Math.min(current + 1, suggestions.length - 1),
);
return;
}
if (suggestions.length && event.key === "ArrowUp") {
event.preventDefault();
setActiveSuggestionIndex((current) => Math.max(current - 1, 0));
return;
}
if (suggestions.length && event.key === "Escape") {
event.preventDefault();
setDraft(draft.replace(/(?:^|\s)([@/][A-Za-z\s-]*)$/, ""));
return;
}
if (
suggestions.length &&
event.key === "Enter" &&
!event.metaKey &&
!event.ctrlKey
) {
event.preventDefault();
applySuggestion(suggestions[activeSuggestionIndex] ?? suggestions[0]);
return;
}
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
onSend(draft);
}
}}
/>
<Button
type="submit"
disabled={busy || !draft.trim()}
size="icon"
className="h-12 w-12 rounded-2xl"
>
<Send className="h-4 w-4" />
</Button>
</form>
<div className="mt-2 flex items-center justify-between gap-3">
<div className="text-xs text-muted-foreground">
Press <span className="font-mono">Ctrl</span>+
<span className="font-mono">Enter</span> to send. Use{" "}
<span className="font-mono">@</span> or{" "}
<span className="font-mono">/</span> for suggestions.
</div>
<Button
type="button"
variant="ghost"
className="h-8 rounded-xl px-2.5 text-[11px]"
onClick={() => setSettingsOpen((value) => !value)}
>
<StatusIcon status={connectionStatus} />
<span className="ml-1">
{isMockMode ? "Model Settings" : "Live Settings"}
</span>
</Button>
</div>
</div>
</div>
);
}
|