@harbinger-ai/harbinger 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +406 -0
- package/agents/README.md +76 -0
- package/agents/_template/CONFIG.yaml +7 -0
- package/agents/_template/HEARTBEAT.md +59 -0
- package/agents/_template/IDENTITY.md +4 -0
- package/agents/_template/SKILLS.md +1 -0
- package/agents/_template/SOUL.md +25 -0
- package/agents/_template/TOOLS.md +3 -0
- package/agents/binary-reverser/CONFIG.yaml +21 -0
- package/agents/binary-reverser/HEARTBEAT.md +65 -0
- package/agents/binary-reverser/IDENTITY.md +1 -0
- package/agents/binary-reverser/SKILLS.md +1 -0
- package/agents/binary-reverser/SOUL.md +23 -0
- package/agents/binary-reverser/TOOLS.md +99 -0
- package/agents/browser-agent/CONFIG.yaml +20 -0
- package/agents/browser-agent/HEARTBEAT.md +79 -0
- package/agents/browser-agent/IDENTITY.md +5 -0
- package/agents/browser-agent/SKILLS.md +86 -0
- package/agents/browser-agent/SOUL.md +23 -0
- package/agents/browser-agent/TOOLS.md +186 -0
- package/agents/cloud-infiltrator/CONFIG.yaml +22 -0
- package/agents/cloud-infiltrator/HEARTBEAT.md +78 -0
- package/agents/cloud-infiltrator/IDENTITY.md +1 -0
- package/agents/cloud-infiltrator/SKILLS.md +1 -0
- package/agents/cloud-infiltrator/SOUL.md +23 -0
- package/agents/cloud-infiltrator/TOOLS.md +68 -0
- package/agents/coding-assistant/CONFIG.yaml +22 -0
- package/agents/coding-assistant/HEARTBEAT.md +57 -0
- package/agents/coding-assistant/IDENTITY.md +5 -0
- package/agents/coding-assistant/SKILLS.md +69 -0
- package/agents/coding-assistant/SOUL.md +60 -0
- package/agents/coding-assistant/TOOLS.md +168 -0
- package/agents/learning-agent/CONFIG.yaml +21 -0
- package/agents/learning-agent/HEARTBEAT.md +63 -0
- package/agents/learning-agent/IDENTITY.md +5 -0
- package/agents/learning-agent/SKILLS.md +86 -0
- package/agents/learning-agent/SOUL.md +77 -0
- package/agents/learning-agent/TOOLS.md +145 -0
- package/agents/maintainer/CONFIG.yaml +31 -0
- package/agents/maintainer/HEARTBEAT.md +28 -0
- package/agents/maintainer/IDENTITY.md +33 -0
- package/agents/maintainer/SKILLS.md +24 -0
- package/agents/maintainer/SOUL.md +61 -0
- package/agents/maintainer/TOOLS.md +29 -0
- package/agents/maintainer/lib/engine.js +279 -0
- package/agents/maintainer/lib/safe-fixer.js +183 -0
- package/agents/morning-brief/CONFIG.yaml +22 -0
- package/agents/morning-brief/HEARTBEAT.md +60 -0
- package/agents/morning-brief/IDENTITY.md +5 -0
- package/agents/morning-brief/SKILLS.md +56 -0
- package/agents/morning-brief/SOUL.md +64 -0
- package/agents/morning-brief/TOOLS.md +112 -0
- package/agents/osint-detective/CONFIG.yaml +24 -0
- package/agents/osint-detective/HEARTBEAT.md +66 -0
- package/agents/osint-detective/IDENTITY.md +1 -0
- package/agents/osint-detective/SKILLS.md +1 -0
- package/agents/osint-detective/SOUL.md +23 -0
- package/agents/osint-detective/TOOLS.md +81 -0
- package/agents/recon-scout/CONFIG.yaml +22 -0
- package/agents/recon-scout/HEARTBEAT.md +79 -0
- package/agents/recon-scout/IDENTITY.md +1 -0
- package/agents/recon-scout/SKILLS.md +1 -0
- package/agents/recon-scout/SOUL.md +23 -0
- package/agents/recon-scout/TOOLS.md +93 -0
- package/agents/report-writer/CONFIG.yaml +21 -0
- package/agents/report-writer/HEARTBEAT.md +63 -0
- package/agents/report-writer/IDENTITY.md +1 -0
- package/agents/report-writer/SKILLS.md +1 -0
- package/agents/report-writer/SOUL.md +23 -0
- package/agents/report-writer/TOOLS.md +69 -0
- package/agents/shared/README.md +13 -0
- package/agents/web-hacker/CONFIG.yaml +24 -0
- package/agents/web-hacker/HEARTBEAT.md +78 -0
- package/agents/web-hacker/IDENTITY.md +1 -0
- package/agents/web-hacker/SKILLS.md +1 -0
- package/agents/web-hacker/SOUL.md +23 -0
- package/agents/web-hacker/TOOLS.md +86 -0
- package/api/CLAUDE.md +19 -0
- package/api/index.js +274 -0
- package/bin/cli.js +620 -0
- package/bin/local.sh +31 -0
- package/bin/postinstall.js +63 -0
- package/config/index.js +24 -0
- package/config/instrumentation.js +93 -0
- package/drizzle/0000_initial.sql +52 -0
- package/drizzle/0001_bounty_and_registry.sql +82 -0
- package/drizzle/0002_sync_columns.sql +7 -0
- package/drizzle/0003_graceful_bloodscream.sql +86 -0
- package/drizzle/meta/0000_snapshot.json +321 -0
- package/drizzle/meta/0003_snapshot.json +878 -0
- package/drizzle/meta/_journal.json +34 -0
- package/drizzle/relations.ts +3 -0
- package/drizzle/schema.ts +145 -0
- package/lib/actions.js +47 -0
- package/lib/agents.js +166 -0
- package/lib/ai/agent.js +96 -0
- package/lib/ai/autonomous-engine.js +261 -0
- package/lib/ai/index.js +359 -0
- package/lib/ai/model-router.js +254 -0
- package/lib/ai/model.js +73 -0
- package/lib/ai/tools.js +84 -0
- package/lib/auth/actions.js +28 -0
- package/lib/auth/config.js +27 -0
- package/lib/auth/edge-config.js +27 -0
- package/lib/auth/index.js +27 -0
- package/lib/auth/middleware.js +53 -0
- package/lib/bounty/actions.js +119 -0
- package/lib/bounty/findings.js +64 -0
- package/lib/bounty/programs.js +34 -0
- package/lib/bounty/sync-targets.js +267 -0
- package/lib/bounty/targets.js +33 -0
- package/lib/channels/base.js +56 -0
- package/lib/channels/index.js +15 -0
- package/lib/channels/telegram.js +148 -0
- package/lib/chat/actions.js +288 -0
- package/lib/chat/api.js +135 -0
- package/lib/chat/components/app-sidebar.js +237 -0
- package/lib/chat/components/app-sidebar.jsx +289 -0
- package/lib/chat/components/chat-header.js +27 -0
- package/lib/chat/components/chat-header.jsx +37 -0
- package/lib/chat/components/chat-input.js +230 -0
- package/lib/chat/components/chat-input.jsx +228 -0
- package/lib/chat/components/chat-nav-context.js +11 -0
- package/lib/chat/components/chat-nav-context.jsx +11 -0
- package/lib/chat/components/chat-page.js +81 -0
- package/lib/chat/components/chat-page.jsx +100 -0
- package/lib/chat/components/chat.js +150 -0
- package/lib/chat/components/chat.jsx +182 -0
- package/lib/chat/components/chats-page.js +302 -0
- package/lib/chat/components/chats-page.jsx +330 -0
- package/lib/chat/components/crons-page.js +172 -0
- package/lib/chat/components/crons-page.jsx +244 -0
- package/lib/chat/components/enhanced-tool-call.js +103 -0
- package/lib/chat/components/enhanced-tool-call.jsx +139 -0
- package/lib/chat/components/findings-page.js +175 -0
- package/lib/chat/components/findings-page.jsx +214 -0
- package/lib/chat/components/greeting.js +22 -0
- package/lib/chat/components/greeting.jsx +26 -0
- package/lib/chat/components/icons.js +777 -0
- package/lib/chat/components/icons.jsx +741 -0
- package/lib/chat/components/index.js +26 -0
- package/lib/chat/components/mcp-page.js +260 -0
- package/lib/chat/components/mcp-page.jsx +355 -0
- package/lib/chat/components/message.js +289 -0
- package/lib/chat/components/message.jsx +315 -0
- package/lib/chat/components/messages.js +66 -0
- package/lib/chat/components/messages.jsx +77 -0
- package/lib/chat/components/notifications-page.js +56 -0
- package/lib/chat/components/notifications-page.jsx +87 -0
- package/lib/chat/components/page-layout.js +21 -0
- package/lib/chat/components/page-layout.jsx +28 -0
- package/lib/chat/components/registry-page.js +222 -0
- package/lib/chat/components/registry-page.jsx +255 -0
- package/lib/chat/components/settings-layout.js +40 -0
- package/lib/chat/components/settings-layout.jsx +54 -0
- package/lib/chat/components/settings-secrets-page.js +216 -0
- package/lib/chat/components/settings-secrets-page.jsx +264 -0
- package/lib/chat/components/sidebar-history-item.js +132 -0
- package/lib/chat/components/sidebar-history-item.jsx +113 -0
- package/lib/chat/components/sidebar-history.js +115 -0
- package/lib/chat/components/sidebar-history.jsx +157 -0
- package/lib/chat/components/sidebar-user-nav.js +63 -0
- package/lib/chat/components/sidebar-user-nav.jsx +73 -0
- package/lib/chat/components/status-bar.js +39 -0
- package/lib/chat/components/status-bar.jsx +51 -0
- package/lib/chat/components/swarm-page.js +157 -0
- package/lib/chat/components/swarm-page.jsx +210 -0
- package/lib/chat/components/targets-page.js +376 -0
- package/lib/chat/components/targets-page.jsx +389 -0
- package/lib/chat/components/tool-call.js +86 -0
- package/lib/chat/components/tool-call.jsx +104 -0
- package/lib/chat/components/tool-panel.js +107 -0
- package/lib/chat/components/tool-panel.jsx +145 -0
- package/lib/chat/components/triggers-page.js +153 -0
- package/lib/chat/components/triggers-page.jsx +221 -0
- package/lib/chat/components/ui/confirm-dialog.js +53 -0
- package/lib/chat/components/ui/confirm-dialog.jsx +57 -0
- package/lib/chat/components/ui/dropdown-menu.js +98 -0
- package/lib/chat/components/ui/dropdown-menu.jsx +116 -0
- package/lib/chat/components/ui/rename-dialog.js +74 -0
- package/lib/chat/components/ui/rename-dialog.jsx +72 -0
- package/lib/chat/components/ui/scroll-area.js +13 -0
- package/lib/chat/components/ui/scroll-area.jsx +17 -0
- package/lib/chat/components/ui/separator.js +21 -0
- package/lib/chat/components/ui/separator.jsx +18 -0
- package/lib/chat/components/ui/sheet.js +75 -0
- package/lib/chat/components/ui/sheet.jsx +95 -0
- package/lib/chat/components/ui/sidebar.js +227 -0
- package/lib/chat/components/ui/sidebar.jsx +245 -0
- package/lib/chat/components/ui/tooltip.js +56 -0
- package/lib/chat/components/ui/tooltip.jsx +66 -0
- package/lib/chat/components/upgrade-dialog.js +151 -0
- package/lib/chat/components/upgrade-dialog.jsx +170 -0
- package/lib/chat/utils.js +11 -0
- package/lib/cron.js +246 -0
- package/lib/db/api-keys.js +163 -0
- package/lib/db/chats.js +145 -0
- package/lib/db/index.js +52 -0
- package/lib/db/notifications.js +99 -0
- package/lib/db/schema.js +145 -0
- package/lib/db/update-check.js +96 -0
- package/lib/db/users.js +89 -0
- package/lib/mcp/actions.js +104 -0
- package/lib/mcp/client.js +79 -0
- package/lib/mcp/handler.js +57 -0
- package/lib/mcp/server.js +165 -0
- package/lib/paths.js +46 -0
- package/lib/registry/actions.js +164 -0
- package/lib/registry/catalog.js +137 -0
- package/lib/registry/tools.js +71 -0
- package/lib/tools/create-job.js +99 -0
- package/lib/tools/github.js +217 -0
- package/lib/tools/openai.js +35 -0
- package/lib/tools/telegram.js +292 -0
- package/lib/triggers.js +118 -0
- package/lib/utils/render-md.js +102 -0
- package/package.json +103 -0
- package/setup/lib/auth.mjs +81 -0
- package/setup/lib/env.mjs +21 -0
- package/setup/lib/fs-utils.mjs +20 -0
- package/setup/lib/github.mjs +149 -0
- package/setup/lib/prerequisites.mjs +155 -0
- package/setup/lib/prompts.mjs +267 -0
- package/setup/lib/providers.mjs +48 -0
- package/setup/lib/sync.mjs +125 -0
- package/setup/lib/targets.mjs +45 -0
- package/setup/lib/telegram-verify.mjs +63 -0
- package/setup/lib/telegram.mjs +76 -0
- package/setup/setup-telegram.mjs +264 -0
- package/setup/setup.mjs +842 -0
- package/templates/.dockerignore +5 -0
- package/templates/.env.example +63 -0
- package/templates/.github/workflows/auto-merge.yml +117 -0
- package/templates/.github/workflows/build-image.yml +36 -0
- package/templates/.github/workflows/notify-job-failed.yml +64 -0
- package/templates/.github/workflows/notify-pr-complete.yml +119 -0
- package/templates/.github/workflows/rebuild-event-handler.yml +121 -0
- package/templates/.github/workflows/run-job.yml +89 -0
- package/templates/.github/workflows/upgrade-event-handler.yml +62 -0
- package/templates/.gitignore.template +45 -0
- package/templates/.pi/extensions/env-sanitizer/index.ts +48 -0
- package/templates/.pi/extensions/env-sanitizer/package.json +5 -0
- package/templates/CLAUDE.md +29 -0
- package/templates/CLAUDE.md.template +307 -0
- package/templates/app/api/[...thepopebot]/route.js +1 -0
- package/templates/app/api/auth/[...nextauth]/route.js +1 -0
- package/templates/app/chat/[chatId]/page.js +8 -0
- package/templates/app/chats/page.js +7 -0
- package/templates/app/components/ascii-logo.jsx +10 -0
- package/templates/app/components/login-form.jsx +92 -0
- package/templates/app/components/setup-form.jsx +82 -0
- package/templates/app/components/theme-provider.jsx +11 -0
- package/templates/app/components/theme-toggle.jsx +38 -0
- package/templates/app/components/ui/button.jsx +21 -0
- package/templates/app/components/ui/card.jsx +23 -0
- package/templates/app/components/ui/input.jsx +10 -0
- package/templates/app/components/ui/label.jsx +10 -0
- package/templates/app/crons/page.js +5 -0
- package/templates/app/findings/page.js +7 -0
- package/templates/app/globals.css +90 -0
- package/templates/app/layout.js +19 -0
- package/templates/app/login/page.js +15 -0
- package/templates/app/notifications/page.js +7 -0
- package/templates/app/page.js +7 -0
- package/templates/app/settings/crons/page.js +5 -0
- package/templates/app/settings/layout.js +7 -0
- package/templates/app/settings/mcp/page.js +5 -0
- package/templates/app/settings/page.js +5 -0
- package/templates/app/settings/secrets/page.js +5 -0
- package/templates/app/settings/triggers/page.js +5 -0
- package/templates/app/stream/chat/route.js +1 -0
- package/templates/app/swarm/page.js +7 -0
- package/templates/app/targets/page.js +7 -0
- package/templates/app/toolbox/page.js +7 -0
- package/templates/app/triggers/page.js +5 -0
- package/templates/config/AGENT.md +34 -0
- package/templates/config/CRONS.json +56 -0
- package/templates/config/EVENT_HANDLER.md +224 -0
- package/templates/config/HEARTBEAT.md +3 -0
- package/templates/config/JOB_SUMMARY.md +130 -0
- package/templates/config/MCP_SERVERS.json +1 -0
- package/templates/config/SKILL_BUILDING_GUIDE.md +90 -0
- package/templates/config/SOUL.md +17 -0
- package/templates/config/TRIGGERS.json +58 -0
- package/templates/docker/event-handler/Dockerfile +20 -0
- package/templates/docker/event-handler/ecosystem.config.cjs +8 -0
- package/templates/docker/job-claude-code/Dockerfile +34 -0
- package/templates/docker/job-claude-code/entrypoint.sh +139 -0
- package/templates/docker/job-pi-coding-agent/Dockerfile +44 -0
- package/templates/docker/job-pi-coding-agent/entrypoint.sh +163 -0
- package/templates/docker-compose.yml +63 -0
- package/templates/instrumentation.js +6 -0
- package/templates/middleware.js +1 -0
- package/templates/next.config.mjs +3 -0
- package/templates/postcss.config.mjs +5 -0
- package/templates/skills/LICENSE +21 -0
- package/templates/skills/README.md +119 -0
- package/templates/skills/brave-search/SKILL.md +79 -0
- package/templates/skills/brave-search/content.js +86 -0
- package/templates/skills/brave-search/package-lock.json +621 -0
- package/templates/skills/brave-search/package.json +14 -0
- package/templates/skills/brave-search/search.js +199 -0
- package/templates/skills/browser-tools/SKILL.md +196 -0
- package/templates/skills/browser-tools/browser-content.js +103 -0
- package/templates/skills/browser-tools/browser-cookies.js +35 -0
- package/templates/skills/browser-tools/browser-eval.js +53 -0
- package/templates/skills/browser-tools/browser-hn-scraper.js +108 -0
- package/templates/skills/browser-tools/browser-nav.js +44 -0
- package/templates/skills/browser-tools/browser-pick.js +162 -0
- package/templates/skills/browser-tools/browser-screenshot.js +34 -0
- package/templates/skills/browser-tools/browser-start.js +87 -0
- package/templates/skills/browser-tools/package-lock.json +2556 -0
- package/templates/skills/browser-tools/package.json +19 -0
- package/templates/skills/llm-secrets/SKILL.md +34 -0
- package/templates/skills/llm-secrets/llm-secrets.js +33 -0
- package/templates/skills/modify-self/SKILL.md +12 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRef, useEffect, useCallback, useState } from "react";
|
|
4
|
+
import { SendIcon, StopIcon, PaperclipIcon, XIcon, FileTextIcon } from "./icons.js";
|
|
5
|
+
import { cn } from "../utils.js";
|
|
6
|
+
const ACCEPTED_TYPES = [
|
|
7
|
+
"image/jpeg",
|
|
8
|
+
"image/png",
|
|
9
|
+
"image/gif",
|
|
10
|
+
"image/webp",
|
|
11
|
+
"application/pdf",
|
|
12
|
+
"text/plain",
|
|
13
|
+
"text/markdown",
|
|
14
|
+
"text/csv",
|
|
15
|
+
"text/html",
|
|
16
|
+
"text/css",
|
|
17
|
+
"text/javascript",
|
|
18
|
+
"text/x-python",
|
|
19
|
+
"text/x-typescript",
|
|
20
|
+
"application/json"
|
|
21
|
+
];
|
|
22
|
+
const MAX_FILES = 5;
|
|
23
|
+
function isAcceptedType(file) {
|
|
24
|
+
if (ACCEPTED_TYPES.includes(file.type)) return true;
|
|
25
|
+
const ext = file.name?.split(".").pop()?.toLowerCase();
|
|
26
|
+
const textExts = ["txt", "md", "csv", "json", "js", "ts", "jsx", "tsx", "py", "html", "css", "yml", "yaml", "xml", "sh", "bash", "rb", "go", "rs", "java", "c", "cpp", "h", "hpp"];
|
|
27
|
+
return textExts.includes(ext);
|
|
28
|
+
}
|
|
29
|
+
function getEffectiveType(file) {
|
|
30
|
+
if (ACCEPTED_TYPES.includes(file.type) && file.type !== "") return file.type;
|
|
31
|
+
const ext = file.name?.split(".").pop()?.toLowerCase();
|
|
32
|
+
const extMap = {
|
|
33
|
+
txt: "text/plain",
|
|
34
|
+
md: "text/markdown",
|
|
35
|
+
csv: "text/csv",
|
|
36
|
+
json: "application/json",
|
|
37
|
+
js: "text/javascript",
|
|
38
|
+
ts: "text/x-typescript",
|
|
39
|
+
jsx: "text/javascript",
|
|
40
|
+
tsx: "text/x-typescript",
|
|
41
|
+
py: "text/x-python",
|
|
42
|
+
html: "text/html",
|
|
43
|
+
css: "text/css",
|
|
44
|
+
yml: "text/plain",
|
|
45
|
+
yaml: "text/plain",
|
|
46
|
+
xml: "text/plain",
|
|
47
|
+
sh: "text/plain",
|
|
48
|
+
bash: "text/plain",
|
|
49
|
+
rb: "text/plain",
|
|
50
|
+
go: "text/plain",
|
|
51
|
+
rs: "text/plain",
|
|
52
|
+
java: "text/plain",
|
|
53
|
+
c: "text/plain",
|
|
54
|
+
cpp: "text/plain",
|
|
55
|
+
h: "text/plain",
|
|
56
|
+
hpp: "text/plain"
|
|
57
|
+
};
|
|
58
|
+
return extMap[ext] || file.type || "text/plain";
|
|
59
|
+
}
|
|
60
|
+
function ChatInput({ input, setInput, onSubmit, status, stop, files, setFiles }) {
|
|
61
|
+
const textareaRef = useRef(null);
|
|
62
|
+
const fileInputRef = useRef(null);
|
|
63
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
64
|
+
const isStreaming = status === "streaming" || status === "submitted";
|
|
65
|
+
const adjustHeight = useCallback(() => {
|
|
66
|
+
const textarea = textareaRef.current;
|
|
67
|
+
if (!textarea) return;
|
|
68
|
+
textarea.style.height = "auto";
|
|
69
|
+
textarea.style.height = `${Math.min(textarea.scrollHeight, 200)}px`;
|
|
70
|
+
}, []);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
adjustHeight();
|
|
73
|
+
}, [input, adjustHeight]);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
textareaRef.current?.focus();
|
|
76
|
+
}, []);
|
|
77
|
+
const handleFiles = useCallback((fileList) => {
|
|
78
|
+
const newFiles = Array.from(fileList).filter(isAcceptedType);
|
|
79
|
+
if (newFiles.length === 0) return;
|
|
80
|
+
newFiles.forEach((file) => {
|
|
81
|
+
const reader = new FileReader();
|
|
82
|
+
reader.onload = () => {
|
|
83
|
+
setFiles((current) => {
|
|
84
|
+
if (current.length >= MAX_FILES) return current;
|
|
85
|
+
return [...current, { file, previewUrl: reader.result }];
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
reader.readAsDataURL(file);
|
|
89
|
+
});
|
|
90
|
+
}, [setFiles]);
|
|
91
|
+
const removeFile = (index) => {
|
|
92
|
+
setFiles((prev) => prev.filter((_, i) => i !== index));
|
|
93
|
+
};
|
|
94
|
+
const handleSubmit = (e) => {
|
|
95
|
+
if (e) e.preventDefault();
|
|
96
|
+
if (!input.trim() && files.length === 0 || isStreaming) return;
|
|
97
|
+
onSubmit();
|
|
98
|
+
};
|
|
99
|
+
const handleKeyDown = (e) => {
|
|
100
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
handleSubmit();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const handleDragOver = (e) => {
|
|
106
|
+
e.preventDefault();
|
|
107
|
+
setIsDragging(true);
|
|
108
|
+
};
|
|
109
|
+
const handleDragLeave = (e) => {
|
|
110
|
+
e.preventDefault();
|
|
111
|
+
setIsDragging(false);
|
|
112
|
+
};
|
|
113
|
+
const handleDrop = (e) => {
|
|
114
|
+
e.preventDefault();
|
|
115
|
+
setIsDragging(false);
|
|
116
|
+
if (e.dataTransfer?.files?.length) {
|
|
117
|
+
handleFiles(e.dataTransfer.files);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const canSend = input.trim() || files.length > 0;
|
|
121
|
+
return /* @__PURE__ */ jsx("div", { className: "mx-auto w-full max-w-4xl px-4 pb-4 md:px-6", children: /* @__PURE__ */ jsx("form", { onSubmit: handleSubmit, className: "relative", children: /* @__PURE__ */ jsxs(
|
|
122
|
+
"div",
|
|
123
|
+
{
|
|
124
|
+
className: cn(
|
|
125
|
+
"flex flex-col rounded-lg border bg-[--card] p-2 transition-colors backdrop-blur-sm",
|
|
126
|
+
isDragging ? "border-[--primary] bg-[--primary]/5" : "border-white/[0.06]"
|
|
127
|
+
),
|
|
128
|
+
onDragOver: handleDragOver,
|
|
129
|
+
onDragLeave: handleDragLeave,
|
|
130
|
+
onDrop: handleDrop,
|
|
131
|
+
children: [
|
|
132
|
+
files.length > 0 && /* @__PURE__ */ jsx("div", { className: "mb-2 flex gap-2 overflow-x-auto px-1 py-1", children: files.map((f, i) => {
|
|
133
|
+
const isImage = f.file.type.startsWith("image/");
|
|
134
|
+
return /* @__PURE__ */ jsxs("div", { className: "group relative flex-shrink-0", children: [
|
|
135
|
+
isImage ? /* @__PURE__ */ jsx(
|
|
136
|
+
"img",
|
|
137
|
+
{
|
|
138
|
+
src: f.previewUrl,
|
|
139
|
+
alt: f.file.name,
|
|
140
|
+
className: "h-16 w-16 rounded-lg object-cover"
|
|
141
|
+
}
|
|
142
|
+
) : /* @__PURE__ */ jsxs("div", { className: "flex h-16 items-center gap-1.5 rounded-lg bg-white/[0.04] border border-white/[0.06] px-3", children: [
|
|
143
|
+
/* @__PURE__ */ jsx(FileTextIcon, { size: 14 }),
|
|
144
|
+
/* @__PURE__ */ jsx("span", { className: "max-w-[100px] truncate text-xs font-mono", children: f.file.name })
|
|
145
|
+
] }),
|
|
146
|
+
/* @__PURE__ */ jsx(
|
|
147
|
+
"button",
|
|
148
|
+
{
|
|
149
|
+
type: "button",
|
|
150
|
+
onClick: () => removeFile(i),
|
|
151
|
+
className: "absolute -right-1.5 -top-1.5 hidden rounded-full bg-foreground p-0.5 text-background group-hover:flex items-center justify-center",
|
|
152
|
+
"aria-label": `Remove ${f.file.name}`,
|
|
153
|
+
children: /* @__PURE__ */ jsx(XIcon, { size: 10 })
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
] }, i);
|
|
157
|
+
}) }),
|
|
158
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-end gap-2", children: [
|
|
159
|
+
/* @__PURE__ */ jsx(
|
|
160
|
+
"button",
|
|
161
|
+
{
|
|
162
|
+
type: "button",
|
|
163
|
+
onClick: () => fileInputRef.current?.click(),
|
|
164
|
+
className: "inline-flex items-center justify-center rounded-md p-2 text-muted-foreground hover:text-[--cyan] transition-colors",
|
|
165
|
+
"aria-label": "Attach files",
|
|
166
|
+
disabled: isStreaming,
|
|
167
|
+
children: /* @__PURE__ */ jsx(PaperclipIcon, { size: 16 })
|
|
168
|
+
}
|
|
169
|
+
),
|
|
170
|
+
/* @__PURE__ */ jsx(
|
|
171
|
+
"input",
|
|
172
|
+
{
|
|
173
|
+
ref: fileInputRef,
|
|
174
|
+
type: "file",
|
|
175
|
+
multiple: true,
|
|
176
|
+
accept: "image/*,application/pdf,text/*,application/json,.md,.csv,.json,.js,.ts,.jsx,.tsx,.py,.html,.css,.yml,.yaml,.xml,.sh,.rb,.go,.rs,.java,.c,.cpp,.h",
|
|
177
|
+
className: "hidden",
|
|
178
|
+
onChange: (e) => {
|
|
179
|
+
if (e.target.files?.length) handleFiles(e.target.files);
|
|
180
|
+
e.target.value = "";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
),
|
|
184
|
+
/* @__PURE__ */ jsx(
|
|
185
|
+
"textarea",
|
|
186
|
+
{
|
|
187
|
+
ref: textareaRef,
|
|
188
|
+
value: input,
|
|
189
|
+
onChange: (e) => setInput(e.target.value),
|
|
190
|
+
onKeyDown: handleKeyDown,
|
|
191
|
+
placeholder: "Send a message...",
|
|
192
|
+
rows: 1,
|
|
193
|
+
className: cn(
|
|
194
|
+
"flex-1 resize-none bg-transparent px-2 py-1.5 text-sm text-foreground font-mono",
|
|
195
|
+
"placeholder:text-muted-foreground/50 focus:outline-none",
|
|
196
|
+
"max-h-[200px]"
|
|
197
|
+
),
|
|
198
|
+
disabled: isStreaming
|
|
199
|
+
}
|
|
200
|
+
),
|
|
201
|
+
isStreaming ? /* @__PURE__ */ jsx(
|
|
202
|
+
"button",
|
|
203
|
+
{
|
|
204
|
+
type: "button",
|
|
205
|
+
onClick: stop,
|
|
206
|
+
className: "inline-flex items-center justify-center rounded-md bg-foreground p-2 text-background hover:opacity-80 transition-opacity",
|
|
207
|
+
"aria-label": "Stop generating",
|
|
208
|
+
children: /* @__PURE__ */ jsx(StopIcon, { size: 16 })
|
|
209
|
+
}
|
|
210
|
+
) : /* @__PURE__ */ jsx(
|
|
211
|
+
"button",
|
|
212
|
+
{
|
|
213
|
+
type: "submit",
|
|
214
|
+
disabled: !canSend,
|
|
215
|
+
className: cn(
|
|
216
|
+
"inline-flex items-center justify-center rounded-md p-2 transition-all",
|
|
217
|
+
canSend ? "bg-[--primary] text-primary-foreground hover:opacity-80" : "bg-white/[0.04] text-muted-foreground/40 cursor-not-allowed"
|
|
218
|
+
),
|
|
219
|
+
"aria-label": "Send message",
|
|
220
|
+
children: /* @__PURE__ */ jsx(SendIcon, { size: 16 })
|
|
221
|
+
}
|
|
222
|
+
)
|
|
223
|
+
] })
|
|
224
|
+
]
|
|
225
|
+
}
|
|
226
|
+
) }) });
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
ChatInput
|
|
230
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useRef, useEffect, useCallback, useState } from 'react';
|
|
4
|
+
import { SendIcon, StopIcon, PaperclipIcon, XIcon, FileTextIcon } from './icons.js';
|
|
5
|
+
import { cn } from '../utils.js';
|
|
6
|
+
|
|
7
|
+
const ACCEPTED_TYPES = [
|
|
8
|
+
'image/jpeg', 'image/png', 'image/gif', 'image/webp',
|
|
9
|
+
'application/pdf',
|
|
10
|
+
'text/plain', 'text/markdown', 'text/csv', 'text/html', 'text/css',
|
|
11
|
+
'text/javascript', 'text/x-python', 'text/x-typescript',
|
|
12
|
+
'application/json',
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const MAX_FILES = 5;
|
|
16
|
+
|
|
17
|
+
function isAcceptedType(file) {
|
|
18
|
+
if (ACCEPTED_TYPES.includes(file.type)) return true;
|
|
19
|
+
const ext = file.name?.split('.').pop()?.toLowerCase();
|
|
20
|
+
const textExts = ['txt', 'md', 'csv', 'json', 'js', 'ts', 'jsx', 'tsx', 'py', 'html', 'css', 'yml', 'yaml', 'xml', 'sh', 'bash', 'rb', 'go', 'rs', 'java', 'c', 'cpp', 'h', 'hpp'];
|
|
21
|
+
return textExts.includes(ext);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function getEffectiveType(file) {
|
|
25
|
+
if (ACCEPTED_TYPES.includes(file.type) && file.type !== '') return file.type;
|
|
26
|
+
const ext = file.name?.split('.').pop()?.toLowerCase();
|
|
27
|
+
const extMap = {
|
|
28
|
+
txt: 'text/plain', md: 'text/markdown', csv: 'text/csv',
|
|
29
|
+
json: 'application/json', js: 'text/javascript', ts: 'text/x-typescript',
|
|
30
|
+
jsx: 'text/javascript', tsx: 'text/x-typescript', py: 'text/x-python',
|
|
31
|
+
html: 'text/html', css: 'text/css', yml: 'text/plain', yaml: 'text/plain',
|
|
32
|
+
xml: 'text/plain', sh: 'text/plain', bash: 'text/plain', rb: 'text/plain',
|
|
33
|
+
go: 'text/plain', rs: 'text/plain', java: 'text/plain', c: 'text/plain',
|
|
34
|
+
cpp: 'text/plain', h: 'text/plain', hpp: 'text/plain',
|
|
35
|
+
};
|
|
36
|
+
return extMap[ext] || file.type || 'text/plain';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function ChatInput({ input, setInput, onSubmit, status, stop, files, setFiles }) {
|
|
40
|
+
const textareaRef = useRef(null);
|
|
41
|
+
const fileInputRef = useRef(null);
|
|
42
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
43
|
+
const isStreaming = status === 'streaming' || status === 'submitted';
|
|
44
|
+
|
|
45
|
+
const adjustHeight = useCallback(() => {
|
|
46
|
+
const textarea = textareaRef.current;
|
|
47
|
+
if (!textarea) return;
|
|
48
|
+
textarea.style.height = 'auto';
|
|
49
|
+
textarea.style.height = `${Math.min(textarea.scrollHeight, 200)}px`;
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
adjustHeight();
|
|
54
|
+
}, [input, adjustHeight]);
|
|
55
|
+
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
textareaRef.current?.focus();
|
|
58
|
+
}, []);
|
|
59
|
+
|
|
60
|
+
const handleFiles = useCallback((fileList) => {
|
|
61
|
+
const newFiles = Array.from(fileList).filter(isAcceptedType);
|
|
62
|
+
if (newFiles.length === 0) return;
|
|
63
|
+
|
|
64
|
+
newFiles.forEach((file) => {
|
|
65
|
+
const reader = new FileReader();
|
|
66
|
+
reader.onload = () => {
|
|
67
|
+
setFiles((current) => {
|
|
68
|
+
if (current.length >= MAX_FILES) return current;
|
|
69
|
+
return [...current, { file, previewUrl: reader.result }];
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
reader.readAsDataURL(file);
|
|
73
|
+
});
|
|
74
|
+
}, [setFiles]);
|
|
75
|
+
|
|
76
|
+
const removeFile = (index) => {
|
|
77
|
+
setFiles((prev) => prev.filter((_, i) => i !== index));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const handleSubmit = (e) => {
|
|
81
|
+
if (e) e.preventDefault();
|
|
82
|
+
if ((!input.trim() && files.length === 0) || isStreaming) return;
|
|
83
|
+
onSubmit();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const handleKeyDown = (e) => {
|
|
87
|
+
if (e.key === 'Enter' && !e.shiftKey) {
|
|
88
|
+
e.preventDefault();
|
|
89
|
+
handleSubmit();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handleDragOver = (e) => {
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
setIsDragging(true);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const handleDragLeave = (e) => {
|
|
99
|
+
e.preventDefault();
|
|
100
|
+
setIsDragging(false);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const handleDrop = (e) => {
|
|
104
|
+
e.preventDefault();
|
|
105
|
+
setIsDragging(false);
|
|
106
|
+
if (e.dataTransfer?.files?.length) {
|
|
107
|
+
handleFiles(e.dataTransfer.files);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const canSend = input.trim() || files.length > 0;
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<div className="mx-auto w-full max-w-4xl px-4 pb-4 md:px-6">
|
|
115
|
+
<form onSubmit={handleSubmit} className="relative">
|
|
116
|
+
<div
|
|
117
|
+
className={cn(
|
|
118
|
+
'flex flex-col rounded-lg border bg-[--card] p-2 transition-colors backdrop-blur-sm',
|
|
119
|
+
isDragging ? 'border-[--primary] bg-[--primary]/5' : 'border-white/[0.06]'
|
|
120
|
+
)}
|
|
121
|
+
onDragOver={handleDragOver}
|
|
122
|
+
onDragLeave={handleDragLeave}
|
|
123
|
+
onDrop={handleDrop}
|
|
124
|
+
>
|
|
125
|
+
{/* File preview strip */}
|
|
126
|
+
{files.length > 0 && (
|
|
127
|
+
<div className="mb-2 flex gap-2 overflow-x-auto px-1 py-1">
|
|
128
|
+
{files.map((f, i) => {
|
|
129
|
+
const isImage = f.file.type.startsWith('image/');
|
|
130
|
+
return (
|
|
131
|
+
<div key={i} className="group relative flex-shrink-0">
|
|
132
|
+
{isImage ? (
|
|
133
|
+
<img
|
|
134
|
+
src={f.previewUrl}
|
|
135
|
+
alt={f.file.name}
|
|
136
|
+
className="h-16 w-16 rounded-lg object-cover"
|
|
137
|
+
/>
|
|
138
|
+
) : (
|
|
139
|
+
<div className="flex h-16 items-center gap-1.5 rounded-lg bg-white/[0.04] border border-white/[0.06] px-3">
|
|
140
|
+
<FileTextIcon size={14} />
|
|
141
|
+
<span className="max-w-[100px] truncate text-xs font-mono">
|
|
142
|
+
{f.file.name}
|
|
143
|
+
</span>
|
|
144
|
+
</div>
|
|
145
|
+
)}
|
|
146
|
+
<button
|
|
147
|
+
type="button"
|
|
148
|
+
onClick={() => removeFile(i)}
|
|
149
|
+
className="absolute -right-1.5 -top-1.5 hidden rounded-full bg-foreground p-0.5 text-background group-hover:flex items-center justify-center"
|
|
150
|
+
aria-label={`Remove ${f.file.name}`}
|
|
151
|
+
>
|
|
152
|
+
<XIcon size={10} />
|
|
153
|
+
</button>
|
|
154
|
+
</div>
|
|
155
|
+
);
|
|
156
|
+
})}
|
|
157
|
+
</div>
|
|
158
|
+
)}
|
|
159
|
+
|
|
160
|
+
<div className="flex items-end gap-2">
|
|
161
|
+
{/* Paperclip button */}
|
|
162
|
+
<button
|
|
163
|
+
type="button"
|
|
164
|
+
onClick={() => fileInputRef.current?.click()}
|
|
165
|
+
className="inline-flex items-center justify-center rounded-md p-2 text-muted-foreground hover:text-[--cyan] transition-colors"
|
|
166
|
+
aria-label="Attach files"
|
|
167
|
+
disabled={isStreaming}
|
|
168
|
+
>
|
|
169
|
+
<PaperclipIcon size={16} />
|
|
170
|
+
</button>
|
|
171
|
+
|
|
172
|
+
<input
|
|
173
|
+
ref={fileInputRef}
|
|
174
|
+
type="file"
|
|
175
|
+
multiple
|
|
176
|
+
accept="image/*,application/pdf,text/*,application/json,.md,.csv,.json,.js,.ts,.jsx,.tsx,.py,.html,.css,.yml,.yaml,.xml,.sh,.rb,.go,.rs,.java,.c,.cpp,.h"
|
|
177
|
+
className="hidden"
|
|
178
|
+
onChange={(e) => {
|
|
179
|
+
if (e.target.files?.length) handleFiles(e.target.files);
|
|
180
|
+
e.target.value = '';
|
|
181
|
+
}}
|
|
182
|
+
/>
|
|
183
|
+
|
|
184
|
+
<textarea
|
|
185
|
+
ref={textareaRef}
|
|
186
|
+
value={input}
|
|
187
|
+
onChange={(e) => setInput(e.target.value)}
|
|
188
|
+
onKeyDown={handleKeyDown}
|
|
189
|
+
placeholder="Send a message..."
|
|
190
|
+
rows={1}
|
|
191
|
+
className={cn(
|
|
192
|
+
'flex-1 resize-none bg-transparent px-2 py-1.5 text-sm text-foreground font-mono',
|
|
193
|
+
'placeholder:text-muted-foreground/50 focus:outline-none',
|
|
194
|
+
'max-h-[200px]'
|
|
195
|
+
)}
|
|
196
|
+
disabled={isStreaming}
|
|
197
|
+
/>
|
|
198
|
+
|
|
199
|
+
{isStreaming ? (
|
|
200
|
+
<button
|
|
201
|
+
type="button"
|
|
202
|
+
onClick={stop}
|
|
203
|
+
className="inline-flex items-center justify-center rounded-md bg-foreground p-2 text-background hover:opacity-80 transition-opacity"
|
|
204
|
+
aria-label="Stop generating"
|
|
205
|
+
>
|
|
206
|
+
<StopIcon size={16} />
|
|
207
|
+
</button>
|
|
208
|
+
) : (
|
|
209
|
+
<button
|
|
210
|
+
type="submit"
|
|
211
|
+
disabled={!canSend}
|
|
212
|
+
className={cn(
|
|
213
|
+
'inline-flex items-center justify-center rounded-md p-2 transition-all',
|
|
214
|
+
canSend
|
|
215
|
+
? 'bg-[--primary] text-primary-foreground hover:opacity-80'
|
|
216
|
+
: 'bg-white/[0.04] text-muted-foreground/40 cursor-not-allowed'
|
|
217
|
+
)}
|
|
218
|
+
aria-label="Send message"
|
|
219
|
+
>
|
|
220
|
+
<SendIcon size={16} />
|
|
221
|
+
</button>
|
|
222
|
+
)}
|
|
223
|
+
</div>
|
|
224
|
+
</div>
|
|
225
|
+
</form>
|
|
226
|
+
</div>
|
|
227
|
+
);
|
|
228
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
const ChatNavContext = createContext(null);
|
|
4
|
+
const ChatNavProvider = ChatNavContext.Provider;
|
|
5
|
+
function useChatNav() {
|
|
6
|
+
return useContext(ChatNavContext);
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
ChatNavProvider,
|
|
10
|
+
useChatNav
|
|
11
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState, useEffect, useCallback } from "react";
|
|
4
|
+
import { AppSidebar } from "./app-sidebar.js";
|
|
5
|
+
import { Chat } from "./chat.js";
|
|
6
|
+
import { SidebarProvider, SidebarInset } from "./ui/sidebar.js";
|
|
7
|
+
import { ChatNavProvider } from "./chat-nav-context.js";
|
|
8
|
+
import { getChatMessages } from "../actions.js";
|
|
9
|
+
import { v4 as uuidv4 } from "uuid";
|
|
10
|
+
function ChatPage({ session, needsSetup, chatId }) {
|
|
11
|
+
const [activeChatId, setActiveChatId] = useState(chatId || null);
|
|
12
|
+
const [resolvedChatId, setResolvedChatId] = useState(() => chatId ? null : uuidv4());
|
|
13
|
+
const [initialMessages, setInitialMessages] = useState([]);
|
|
14
|
+
const navigateToChat = useCallback((id) => {
|
|
15
|
+
if (id) {
|
|
16
|
+
window.history.pushState({}, "", `/chat/${id}`);
|
|
17
|
+
setResolvedChatId(null);
|
|
18
|
+
setInitialMessages([]);
|
|
19
|
+
setActiveChatId(id);
|
|
20
|
+
} else {
|
|
21
|
+
window.history.pushState({}, "", "/");
|
|
22
|
+
setInitialMessages([]);
|
|
23
|
+
setActiveChatId(null);
|
|
24
|
+
setResolvedChatId(uuidv4());
|
|
25
|
+
}
|
|
26
|
+
}, []);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const onPopState = () => {
|
|
29
|
+
const match = window.location.pathname.match(/^\/chat\/(.+)/);
|
|
30
|
+
if (match) {
|
|
31
|
+
setResolvedChatId(null);
|
|
32
|
+
setInitialMessages([]);
|
|
33
|
+
setActiveChatId(match[1]);
|
|
34
|
+
} else {
|
|
35
|
+
setInitialMessages([]);
|
|
36
|
+
setActiveChatId(null);
|
|
37
|
+
setResolvedChatId(uuidv4());
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
window.addEventListener("popstate", onPopState);
|
|
41
|
+
return () => window.removeEventListener("popstate", onPopState);
|
|
42
|
+
}, []);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (activeChatId) {
|
|
45
|
+
getChatMessages(activeChatId).then((dbMessages) => {
|
|
46
|
+
if (dbMessages.length === 0) {
|
|
47
|
+
setInitialMessages([]);
|
|
48
|
+
setResolvedChatId(uuidv4());
|
|
49
|
+
window.history.replaceState({}, "", "/");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const uiMessages = dbMessages.map((msg) => ({
|
|
53
|
+
id: msg.id,
|
|
54
|
+
role: msg.role,
|
|
55
|
+
content: msg.content,
|
|
56
|
+
parts: [{ type: "text", text: msg.content }],
|
|
57
|
+
createdAt: new Date(msg.createdAt)
|
|
58
|
+
}));
|
|
59
|
+
setInitialMessages(uiMessages);
|
|
60
|
+
setResolvedChatId(activeChatId);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}, [activeChatId]);
|
|
64
|
+
if (needsSetup || !session) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return /* @__PURE__ */ jsx(ChatNavProvider, { value: { activeChatId: resolvedChatId, navigateToChat }, children: /* @__PURE__ */ jsxs(SidebarProvider, { children: [
|
|
68
|
+
/* @__PURE__ */ jsx(AppSidebar, { user: session.user }),
|
|
69
|
+
/* @__PURE__ */ jsx(SidebarInset, { children: resolvedChatId && /* @__PURE__ */ jsx(
|
|
70
|
+
Chat,
|
|
71
|
+
{
|
|
72
|
+
chatId: resolvedChatId,
|
|
73
|
+
initialMessages
|
|
74
|
+
},
|
|
75
|
+
resolvedChatId
|
|
76
|
+
) })
|
|
77
|
+
] }) });
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
ChatPage
|
|
81
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
4
|
+
import { AppSidebar } from './app-sidebar.js';
|
|
5
|
+
import { Chat } from './chat.js';
|
|
6
|
+
import { SidebarProvider, SidebarInset } from './ui/sidebar.js';
|
|
7
|
+
import { ChatNavProvider } from './chat-nav-context.js';
|
|
8
|
+
import { getChatMessages } from '../actions.js';
|
|
9
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Main chat page component.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} props
|
|
15
|
+
* @param {object|null} props.session - Auth session (null = not logged in)
|
|
16
|
+
* @param {boolean} props.needsSetup - Whether setup is needed
|
|
17
|
+
* @param {string} [props.chatId] - Chat ID from URL (only used for initial mount)
|
|
18
|
+
*/
|
|
19
|
+
export function ChatPage({ session, needsSetup, chatId }) {
|
|
20
|
+
const [activeChatId, setActiveChatId] = useState(chatId || null);
|
|
21
|
+
const [resolvedChatId, setResolvedChatId] = useState(() => chatId ? null : uuidv4());
|
|
22
|
+
const [initialMessages, setInitialMessages] = useState([]);
|
|
23
|
+
|
|
24
|
+
const navigateToChat = useCallback((id) => {
|
|
25
|
+
if (id) {
|
|
26
|
+
window.history.pushState({}, '', `/chat/${id}`);
|
|
27
|
+
setResolvedChatId(null);
|
|
28
|
+
setInitialMessages([]);
|
|
29
|
+
setActiveChatId(id);
|
|
30
|
+
} else {
|
|
31
|
+
window.history.pushState({}, '', '/');
|
|
32
|
+
setInitialMessages([]);
|
|
33
|
+
setActiveChatId(null);
|
|
34
|
+
setResolvedChatId(uuidv4());
|
|
35
|
+
}
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
// Browser back/forward
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const onPopState = () => {
|
|
41
|
+
const match = window.location.pathname.match(/^\/chat\/(.+)/);
|
|
42
|
+
if (match) {
|
|
43
|
+
setResolvedChatId(null);
|
|
44
|
+
setInitialMessages([]);
|
|
45
|
+
setActiveChatId(match[1]);
|
|
46
|
+
} else {
|
|
47
|
+
setInitialMessages([]);
|
|
48
|
+
setActiveChatId(null);
|
|
49
|
+
setResolvedChatId(uuidv4());
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
window.addEventListener('popstate', onPopState);
|
|
53
|
+
return () => window.removeEventListener('popstate', onPopState);
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
// Load messages when activeChatId changes
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (activeChatId) {
|
|
59
|
+
getChatMessages(activeChatId).then((dbMessages) => {
|
|
60
|
+
if (dbMessages.length === 0) {
|
|
61
|
+
// Stale chat (e.g. after login with old UUID) — start fresh
|
|
62
|
+
setInitialMessages([]);
|
|
63
|
+
setResolvedChatId(uuidv4());
|
|
64
|
+
window.history.replaceState({}, '', '/');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const uiMessages = dbMessages.map((msg) => ({
|
|
68
|
+
id: msg.id,
|
|
69
|
+
role: msg.role,
|
|
70
|
+
content: msg.content,
|
|
71
|
+
parts: [{ type: 'text', text: msg.content }],
|
|
72
|
+
createdAt: new Date(msg.createdAt),
|
|
73
|
+
}));
|
|
74
|
+
setInitialMessages(uiMessages);
|
|
75
|
+
setResolvedChatId(activeChatId);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}, [activeChatId]);
|
|
79
|
+
|
|
80
|
+
if (needsSetup || !session) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<ChatNavProvider value={{ activeChatId: resolvedChatId, navigateToChat }}>
|
|
86
|
+
<SidebarProvider>
|
|
87
|
+
<AppSidebar user={session.user} />
|
|
88
|
+
<SidebarInset>
|
|
89
|
+
{resolvedChatId && (
|
|
90
|
+
<Chat
|
|
91
|
+
key={resolvedChatId}
|
|
92
|
+
chatId={resolvedChatId}
|
|
93
|
+
initialMessages={initialMessages}
|
|
94
|
+
/>
|
|
95
|
+
)}
|
|
96
|
+
</SidebarInset>
|
|
97
|
+
</SidebarProvider>
|
|
98
|
+
</ChatNavProvider>
|
|
99
|
+
);
|
|
100
|
+
}
|