2026-03-14 15:52:27 +08:00
|
|
|
|
import { useState, useRef, useEffect } from "react";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
2026-03-18 10:57:48 +08:00
|
|
|
|
import { User, Loader2, Sparkles, ArrowUp, ChevronDown, Paperclip, Check, X, Square, Plus, Database, Wand2, Search, Zap, LayoutGrid, CheckCircle2, Table, XCircle, Settings } from "lucide-react";
|
2026-03-14 15:52:27 +08:00
|
|
|
|
import { api } from "@/lib/api";
|
2026-03-15 11:07:18 +08:00
|
|
|
|
import { type ChartSpec } from "@/store/visualizationStore";
|
2026-03-14 20:58:38 +08:00
|
|
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
|
|
|
|
|
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
|
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-03-14 22:15:38 +08:00
|
|
|
|
import ReactMarkdown from 'react-markdown';
|
|
|
|
|
|
import remarkGfm from 'remark-gfm';
|
|
|
|
|
|
import rehypeRaw from 'rehype-raw';
|
2026-03-14 22:25:01 +08:00
|
|
|
|
import { useLocation } from "react-router-dom";
|
2026-03-15 11:13:40 +08:00
|
|
|
|
import { InlineVisualizationCard } from "./InlineVisualizationCard";
|
2026-03-16 16:12:35 +08:00
|
|
|
|
import { useProjectStore } from "@/store/projectStore";
|
2026-03-18 17:28:48 +08:00
|
|
|
|
import { SlashCommandMenu } from "./SlashCommandMenu";
|
2026-03-14 15:52:27 +08:00
|
|
|
|
|
|
|
|
|
|
interface Message {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
role: 'user' | 'assistant';
|
|
|
|
|
|
content: string;
|
2026-03-14 23:15:41 +08:00
|
|
|
|
awaitingFirstToken?: boolean;
|
2026-03-15 11:07:18 +08:00
|
|
|
|
viz?: MessageViz;
|
2026-03-17 21:32:01 +08:00
|
|
|
|
progressLogs?: string[];
|
|
|
|
|
|
routeInfo?: string;
|
2026-03-15 11:07:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface MessageViz {
|
|
|
|
|
|
sql: string;
|
|
|
|
|
|
rows: unknown[];
|
|
|
|
|
|
chartSpec: ChartSpec | null;
|
|
|
|
|
|
canVisualize: boolean;
|
|
|
|
|
|
reasoning?: string;
|
|
|
|
|
|
error?: string | null;
|
2026-03-14 15:52:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 22:42:18 +08:00
|
|
|
|
const REPORT_HTML_BLOCK_REGEX = /<!--\s*REPORT_HTML_START\s*-->([\s\S]*?)<!--\s*REPORT_HTML_END\s*-->/i;
|
|
|
|
|
|
|
|
|
|
|
|
const splitReportHtml = (content: string): { markdown: string; reportHtml: string | null } => {
|
|
|
|
|
|
if (!content) {
|
|
|
|
|
|
return { markdown: "", reportHtml: null };
|
|
|
|
|
|
}
|
|
|
|
|
|
const match = content.match(REPORT_HTML_BLOCK_REGEX);
|
|
|
|
|
|
if (!match) {
|
|
|
|
|
|
return { markdown: content, reportHtml: null };
|
|
|
|
|
|
}
|
|
|
|
|
|
const reportHtml = (match[1] || "").trim();
|
|
|
|
|
|
const markdown = content.replace(REPORT_HTML_BLOCK_REGEX, "").trim();
|
|
|
|
|
|
return { markdown, reportHtml: reportHtml || null };
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-14 20:58:38 +08:00
|
|
|
|
interface ModelConfig {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
name?: string;
|
|
|
|
|
|
model: string;
|
|
|
|
|
|
provider: string;
|
|
|
|
|
|
is_active: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 18:25:38 +08:00
|
|
|
|
interface DataFileContext {
|
|
|
|
|
|
filename: string;
|
|
|
|
|
|
url: string;
|
|
|
|
|
|
columns?: string[];
|
|
|
|
|
|
summary?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 22:16:04 +08:00
|
|
|
|
interface Skill {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
description?: string;
|
|
|
|
|
|
type: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 22:25:01 +08:00
|
|
|
|
interface SessionData {
|
|
|
|
|
|
key: string;
|
2026-03-15 18:25:38 +08:00
|
|
|
|
metadata?: {
|
|
|
|
|
|
active_data_file?: DataFileContext | null;
|
2026-03-16 23:16:33 +08:00
|
|
|
|
selected_data_source?: string | null;
|
2026-03-15 18:25:38 +08:00
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
};
|
2026-03-14 22:25:01 +08:00
|
|
|
|
messages: Array<{
|
|
|
|
|
|
role: string;
|
|
|
|
|
|
content: string;
|
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
}>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 15:52:27 +08:00
|
|
|
|
export function ChatInterface() {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
const [messagesBySession, setMessagesBySession] = useState<Record<string, Message[]>>({});
|
2026-03-14 15:52:27 +08:00
|
|
|
|
const [input, setInput] = useState("");
|
2026-03-16 22:18:23 +08:00
|
|
|
|
const [selectedDataSource, setSelectedDataSource] = useState<string>("");
|
2026-03-15 22:16:04 +08:00
|
|
|
|
const [availableSkills, setAvailableSkills] = useState<Skill[]>([]);
|
|
|
|
|
|
const [selectedSkillIds, setSelectedSkillIds] = useState<string[]>([]);
|
|
|
|
|
|
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
2026-03-14 15:52:27 +08:00
|
|
|
|
const scrollRef = useRef<HTMLDivElement>(null);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
const location = useLocation();
|
2026-03-16 16:12:35 +08:00
|
|
|
|
const { currentProject } = useProjectStore();
|
2026-03-14 15:52:27 +08:00
|
|
|
|
|
2026-03-18 17:28:48 +08:00
|
|
|
|
// Slash Command State
|
|
|
|
|
|
const [slashQuery, setSlashQuery] = useState<string | null>(null);
|
|
|
|
|
|
const [slashIndex, setSlashIndex] = useState(0);
|
|
|
|
|
|
|
|
|
|
|
|
const filteredSlashSkills = slashQuery !== null
|
|
|
|
|
|
? availableSkills.filter(s => s.name.toLowerCase().includes(slashQuery.toLowerCase()))
|
|
|
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
|
|
const handleSelectSlashSkill = (skill: Skill) => {
|
|
|
|
|
|
if (!selectedSkillIds.includes(skill.id)) {
|
|
|
|
|
|
setSelectedSkillIds(prev => [...prev, skill.id]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the slash command from input
|
|
|
|
|
|
// Match the last occurrence of /query
|
|
|
|
|
|
const match = input.match(/(?:^|\s)\/([a-zA-Z0-9_\-]*)$/);
|
|
|
|
|
|
if (match && match.index !== undefined) {
|
|
|
|
|
|
// match[0] includes the leading space if present
|
|
|
|
|
|
const prefix = input.slice(0, match.index);
|
|
|
|
|
|
const suffix = input.slice(match.index + match[0].length);
|
|
|
|
|
|
setInput((prefix + suffix).trim());
|
|
|
|
|
|
}
|
|
|
|
|
|
setSlashQuery(null);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-19 14:57:42 +08:00
|
|
|
|
const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
|
|
// Avoid triggering Enter when using IME (Input Method Editor) for CJK characters
|
|
|
|
|
|
if (e.nativeEvent.isComposing) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 17:28:48 +08:00
|
|
|
|
if (slashQuery !== null && filteredSlashSkills.length > 0) {
|
|
|
|
|
|
if (e.key === 'ArrowUp') {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
setSlashIndex(prev => Math.max(0, prev - 1));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (e.key === 'ArrowDown') {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
setSlashIndex(prev => Math.min(filteredSlashSkills.length - 1, prev + 1));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
handleSelectSlashSkill(filteredSlashSkills[slashIndex]);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
setSlashQuery(null);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (e.key === 'Enter' && !isLoading) {
|
|
|
|
|
|
handleSend();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
|
const val = e.target.value;
|
|
|
|
|
|
setInput(val);
|
|
|
|
|
|
|
|
|
|
|
|
// Simple slash detection: if the last word starts with /
|
|
|
|
|
|
const match = val.match(/(?:^|\s)\/([a-zA-Z0-9_\-]*)$/);
|
|
|
|
|
|
if (match) {
|
|
|
|
|
|
setSlashQuery(match[1]);
|
|
|
|
|
|
setSlashIndex(0);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setSlashQuery(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-17 20:12:48 +08:00
|
|
|
|
const setMessagesForSession = (sessionKey: string, updater: React.SetStateAction<Message[]>) => {
|
|
|
|
|
|
setMessagesBySession(prev => {
|
|
|
|
|
|
const current = prev[sessionKey] || [];
|
|
|
|
|
|
const next = typeof updater === 'function' ? (updater as (msgs: Message[]) => Message[])(current) : updater;
|
|
|
|
|
|
return { ...prev, [sessionKey]: next };
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const setIsLoadingForSession = (sessionKey: string, loading: boolean) => {
|
|
|
|
|
|
setLoadingBySession(prev => ({ ...prev, [sessionKey]: loading }));
|
|
|
|
|
|
};
|
|
|
|
|
|
const queryParams = new URLSearchParams(location.search);
|
|
|
|
|
|
const activeSessionKey = queryParams.get("session") || "api:default";
|
|
|
|
|
|
|
|
|
|
|
|
const messages = messagesBySession[activeSessionKey] || [];
|
|
|
|
|
|
const [loadingBySession, setLoadingBySession] = useState<Record<string, boolean>>({});
|
|
|
|
|
|
const isLoading = loadingBySession[activeSessionKey] || false;
|
|
|
|
|
|
|
|
|
|
|
|
const generatingSessionsRef = useRef<Record<string, boolean>>({});
|
|
|
|
|
|
const abortControllersRef = useRef<Record<string, AbortController>>({});
|
|
|
|
|
|
|
2026-03-14 20:58:38 +08:00
|
|
|
|
// Model selection state
|
|
|
|
|
|
const [models, setModels] = useState<ModelConfig[]>([]);
|
|
|
|
|
|
const [selectedModelId, setSelectedModelId] = useState<string>("");
|
|
|
|
|
|
const [modelOpen, setModelOpen] = useState(false);
|
2026-03-15 19:36:02 +08:00
|
|
|
|
|
|
|
|
|
|
// Data Source selection state
|
2026-03-16 16:12:35 +08:00
|
|
|
|
const [availableDataSources, setAvailableDataSources] = useState<{id: string, name: string}[]>([]);
|
2026-03-14 20:58:38 +08:00
|
|
|
|
|
2026-03-15 00:10:01 +08:00
|
|
|
|
// File upload state
|
2026-03-15 18:25:38 +08:00
|
|
|
|
const [attachedFile, setAttachedFile] = useState<DataFileContext | null>(null);
|
|
|
|
|
|
const [activeDataFile, setActiveDataFile] = useState<DataFileContext | null>(null);
|
2026-03-15 00:10:01 +08:00
|
|
|
|
const [isUploading, setIsUploading] = useState(false);
|
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
2026-03-14 20:58:38 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
fetchModels();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-03-16 16:12:35 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (currentProject) {
|
|
|
|
|
|
fetchDataSources();
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [currentProject]);
|
|
|
|
|
|
|
2026-03-15 19:36:02 +08:00
|
|
|
|
const fetchDataSources = async () => {
|
2026-03-16 16:12:35 +08:00
|
|
|
|
if (!currentProject) return;
|
2026-03-15 19:36:02 +08:00
|
|
|
|
try {
|
2026-03-16 16:12:35 +08:00
|
|
|
|
const data = await api.get<Array<{id: number, name: string}>>(`/api/v1/datasources?project_id=${currentProject.id}`);
|
|
|
|
|
|
const projectSources = data.map(d => ({ id: `ds:${d.id}`, name: d.name }));
|
|
|
|
|
|
setAvailableDataSources(projectSources);
|
2026-03-16 22:18:23 +08:00
|
|
|
|
if (selectedDataSource && !projectSources.find(ds => ds.id === selectedDataSource)) {
|
|
|
|
|
|
setSelectedDataSource("");
|
2026-03-16 23:16:33 +08:00
|
|
|
|
void syncSessionContext({ selected_data_source: null });
|
2026-03-16 16:12:35 +08:00
|
|
|
|
}
|
2026-03-15 19:36:02 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error("Failed to fetch data sources", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-16 23:16:33 +08:00
|
|
|
|
const syncSessionContext = async (payload: {
|
|
|
|
|
|
active_data_file?: DataFileContext | null;
|
|
|
|
|
|
selected_data_source?: string | null;
|
|
|
|
|
|
}) => {
|
2026-03-15 18:25:38 +08:00
|
|
|
|
try {
|
2026-03-16 23:16:33 +08:00
|
|
|
|
await api.put(`/nanobot/sessions/${encodeURIComponent(activeSessionKey)}/context-file`, payload);
|
2026-03-15 18:25:38 +08:00
|
|
|
|
} catch (e) {
|
2026-03-16 23:16:33 +08:00
|
|
|
|
console.error("Failed to sync session context", e);
|
2026-03-15 18:25:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-16 23:16:33 +08:00
|
|
|
|
const handleSelectDataSource = async (sourceId: string) => {
|
|
|
|
|
|
setSelectedDataSource(sourceId);
|
|
|
|
|
|
await syncSessionContext({ selected_data_source: sourceId });
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleClearDataSource = async () => {
|
|
|
|
|
|
setSelectedDataSource("");
|
|
|
|
|
|
await syncSessionContext({ selected_data_source: null });
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-14 22:25:01 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const fetchSessionData = async () => {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
if (generatingSessionsRef.current[activeSessionKey]) {
|
|
|
|
|
|
return; // Do not fetch if we are currently generating for this session
|
|
|
|
|
|
}
|
|
|
|
|
|
setIsLoadingForSession(activeSessionKey, true);
|
2026-03-16 22:18:23 +08:00
|
|
|
|
setSelectedSkillIds([]);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const data = await api.get<SessionData>(`/nanobot/sessions/${activeSessionKey}`);
|
|
|
|
|
|
if (data.messages && data.messages.length > 0) {
|
|
|
|
|
|
const formattedMessages = data.messages.map((m, idx) => ({
|
|
|
|
|
|
id: `${Date.now()}-${idx}`,
|
|
|
|
|
|
role: m.role as 'user' | 'assistant',
|
2026-03-15 17:57:09 +08:00
|
|
|
|
content: m.content,
|
|
|
|
|
|
viz: m.viz ? buildMessageViz(m.viz) : undefined,
|
2026-03-14 22:25:01 +08:00
|
|
|
|
}));
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(activeSessionKey, formattedMessages);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
} else {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(activeSessionKey, []);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
}
|
2026-03-15 18:25:38 +08:00
|
|
|
|
const restoredFile = data.metadata?.active_data_file || null;
|
2026-03-16 23:16:33 +08:00
|
|
|
|
const restoredSource = data.metadata?.selected_data_source || "";
|
2026-03-15 18:25:38 +08:00
|
|
|
|
setActiveDataFile(restoredFile);
|
2026-03-16 23:16:33 +08:00
|
|
|
|
setSelectedDataSource(restoredSource);
|
2026-03-15 18:25:38 +08:00
|
|
|
|
setAttachedFile(null);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error("Failed to fetch session messages", e);
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(activeSessionKey, []);
|
2026-03-15 18:25:38 +08:00
|
|
|
|
setActiveDataFile(null);
|
2026-03-16 23:16:33 +08:00
|
|
|
|
setSelectedDataSource("");
|
2026-03-15 18:25:38 +08:00
|
|
|
|
setAttachedFile(null);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
} finally {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setIsLoadingForSession(activeSessionKey, false);
|
2026-03-14 22:25:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
fetchSessionData();
|
|
|
|
|
|
}, [activeSessionKey]);
|
|
|
|
|
|
|
2026-03-14 20:58:38 +08:00
|
|
|
|
const fetchModels = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = await api.get<ModelConfig[]>("/api/v1/llm");
|
|
|
|
|
|
setModels(data);
|
|
|
|
|
|
// Set default model if available
|
|
|
|
|
|
const active = data.find(m => m.is_active);
|
|
|
|
|
|
if (active) {
|
|
|
|
|
|
setSelectedModelId(active.id);
|
|
|
|
|
|
} else if (data.length > 0) {
|
|
|
|
|
|
setSelectedModelId(data[0].id);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error("Failed to fetch models", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const currentModel = models.find(m => m.id === selectedModelId);
|
|
|
|
|
|
|
2026-03-15 10:49:37 +08:00
|
|
|
|
const chartIntentPattern = /(图表|可视化|画图|作图|柱状图|折线图|饼图|趋势|分布|chart|plot|visuali[sz]e)/i;
|
2026-03-14 15:52:27 +08:00
|
|
|
|
|
2026-03-15 11:07:18 +08:00
|
|
|
|
const buildMessageViz = (payload: {
|
|
|
|
|
|
sql?: string;
|
|
|
|
|
|
result?: unknown;
|
|
|
|
|
|
error?: string | null;
|
|
|
|
|
|
chart?: { chart_spec?: ChartSpec | null; reasoning?: string; can_visualize?: boolean; chart_type?: string } | null;
|
|
|
|
|
|
}): MessageViz => {
|
|
|
|
|
|
const rows = Array.isArray(payload.result) ? payload.result : [];
|
|
|
|
|
|
const chart = payload.chart ?? undefined;
|
2026-03-19 12:27:31 +08:00
|
|
|
|
const canVisualize = chart?.can_visualize ?? Boolean(chart?.chart_spec);
|
|
|
|
|
|
const chartSpec = chart?.chart_spec ?? null;
|
2026-03-15 11:07:18 +08:00
|
|
|
|
return {
|
|
|
|
|
|
sql: typeof payload.sql === "string" ? payload.sql : "",
|
|
|
|
|
|
rows,
|
|
|
|
|
|
chartSpec,
|
|
|
|
|
|
canVisualize,
|
|
|
|
|
|
reasoning: chart?.reasoning,
|
|
|
|
|
|
error: payload.error ?? null,
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-15 00:10:01 +08:00
|
|
|
|
const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
|
const file = e.target.files?.[0];
|
|
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
|
|
|
|
setIsUploading(true);
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
formData.append("file", file);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await fetch("/api/v1/upload/file", {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
body: formData,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
...(localStorage.getItem("token") ? { Authorization: `Bearer ${localStorage.getItem("token")}` } : {}),
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error("Upload failed");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
2026-03-15 11:07:18 +08:00
|
|
|
|
const uploadedFile = {
|
2026-03-15 00:10:01 +08:00
|
|
|
|
filename: file.name,
|
|
|
|
|
|
url: data.url,
|
|
|
|
|
|
columns: data.columns,
|
|
|
|
|
|
summary: data.summary,
|
2026-03-15 11:07:18 +08:00
|
|
|
|
};
|
|
|
|
|
|
setAttachedFile(uploadedFile);
|
|
|
|
|
|
setActiveDataFile(uploadedFile);
|
2026-03-16 22:18:23 +08:00
|
|
|
|
setSelectedDataSource("");
|
2026-03-16 23:16:33 +08:00
|
|
|
|
await syncSessionContext({ active_data_file: uploadedFile, selected_data_source: null });
|
2026-03-15 00:10:01 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("File upload error:", error);
|
|
|
|
|
|
// Could show a toast notification here
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsUploading(false);
|
|
|
|
|
|
if (fileInputRef.current) {
|
|
|
|
|
|
fileInputRef.current.value = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-15 22:16:04 +08:00
|
|
|
|
const handleRemoveFile = async () => {
|
|
|
|
|
|
setAttachedFile(null);
|
|
|
|
|
|
setActiveDataFile(null);
|
2026-03-16 23:16:33 +08:00
|
|
|
|
await syncSessionContext({ active_data_file: null });
|
2026-03-15 22:16:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-16 22:18:23 +08:00
|
|
|
|
const selectedDataSourceName = availableDataSources.find(ds => ds.id === selectedDataSource)?.name || "";
|
|
|
|
|
|
const selectedSkills = availableSkills.filter(skill => selectedSkillIds.includes(skill.id));
|
|
|
|
|
|
|
|
|
|
|
|
const renderActiveSelections = () => {
|
|
|
|
|
|
if (!selectedDataSource && selectedSkills.length === 0) return null;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="px-2 pt-2">
|
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
{selectedDataSource ? (
|
|
|
|
|
|
<div className="px-3 py-1.5 rounded-full text-xs border flex items-center gap-1.5 bg-blue-50 text-blue-700 border-blue-200">
|
|
|
|
|
|
<Database className="h-3.5 w-3.5" />
|
|
|
|
|
|
{`数据源:${selectedDataSourceName}`}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
{selectedSkills.map((skill) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
key={skill.id}
|
|
|
|
|
|
className="px-3 py-1.5 rounded-full text-xs border flex items-center gap-1.5 bg-orange-50 text-orange-700 border-orange-200"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Wand2 className="h-3.5 w-3.5" />
|
|
|
|
|
|
{`Skill:${skill.name}`}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-15 22:16:04 +08:00
|
|
|
|
const renderFileCard = () => {
|
|
|
|
|
|
const file = attachedFile || activeDataFile;
|
|
|
|
|
|
if (!file) return null;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="px-2 pt-2">
|
|
|
|
|
|
<div className="p-2.5 bg-white border border-zinc-100 rounded-2xl flex items-center gap-3 relative group/file shadow-sm max-w-[280px]">
|
|
|
|
|
|
<div className="h-10 w-10 bg-emerald-600 rounded-xl flex items-center justify-center shrink-0">
|
|
|
|
|
|
<Table className="h-6 w-6 text-white" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex-1 min-w-0 pr-6">
|
|
|
|
|
|
<div className="text-sm font-bold text-zinc-900 truncate">{file.filename}</div>
|
|
|
|
|
|
<div className="text-xs text-zinc-500">电子表格</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleRemoveFile}
|
|
|
|
|
|
className="absolute top-1.5 right-1.5 h-5 w-5 rounded-full flex items-center justify-center transition-colors group/close"
|
|
|
|
|
|
>
|
|
|
|
|
|
<XCircle className="h-5 w-5 fill-zinc-900 text-white" />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const fetchSkills = async () => {
|
|
|
|
|
|
try {
|
2026-03-16 16:12:35 +08:00
|
|
|
|
let url = "/api/v1/skills";
|
|
|
|
|
|
if (currentProject) {
|
|
|
|
|
|
url += `?project_id=${currentProject.id}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
const skills = await api.get<Skill[]>(url);
|
2026-03-15 22:16:04 +08:00
|
|
|
|
setAvailableSkills(skills);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error("Failed to fetch skills:", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
fetchSkills();
|
2026-03-16 16:12:35 +08:00
|
|
|
|
}, [currentProject]);
|
2026-03-15 22:16:04 +08:00
|
|
|
|
|
2026-03-14 15:52:27 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (scrollRef.current) {
|
|
|
|
|
|
scrollRef.current.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
2026-03-15 18:41:58 +08:00
|
|
|
|
const handleForceStop = () => {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
const controller = abortControllersRef.current[activeSessionKey];
|
2026-03-15 18:41:58 +08:00
|
|
|
|
if (!controller) return;
|
|
|
|
|
|
controller.abort();
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setIsLoadingForSession(activeSessionKey, false);
|
|
|
|
|
|
generatingSessionsRef.current[activeSessionKey] = false;
|
|
|
|
|
|
setMessagesForSession(activeSessionKey, (prev) =>
|
2026-03-15 18:41:58 +08:00
|
|
|
|
prev.map((msg) =>
|
|
|
|
|
|
msg.awaitingFirstToken
|
|
|
|
|
|
? { ...msg, awaitingFirstToken: false, content: msg.content || "已中断输出" }
|
|
|
|
|
|
: msg
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-14 15:52:27 +08:00
|
|
|
|
const handleSend = async () => {
|
2026-03-17 11:38:02 +08:00
|
|
|
|
if (!input.trim() || isLoading) return;
|
2026-03-14 15:52:27 +08:00
|
|
|
|
|
2026-03-17 20:12:48 +08:00
|
|
|
|
const targetSessionKey = activeSessionKey;
|
2026-03-14 15:52:27 +08:00
|
|
|
|
const newMessage: Message = { id: Date.now().toString(), role: 'user', content: input };
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(targetSessionKey, prev => [...prev, newMessage]);
|
2026-03-14 15:52:27 +08:00
|
|
|
|
setInput("");
|
2026-03-15 00:10:01 +08:00
|
|
|
|
|
|
|
|
|
|
let messagePayload = newMessage.content;
|
2026-03-15 10:49:37 +08:00
|
|
|
|
const currentAttachedFile = attachedFile;
|
|
|
|
|
|
if (currentAttachedFile) {
|
|
|
|
|
|
messagePayload = `[用户上传了文件: ${currentAttachedFile.filename}]\n[文件内容摘要: ${currentAttachedFile.summary || "无"}]\n[数据列: ${currentAttachedFile.columns?.join(", ") || "无"}]\n[文件下载链接: ${currentAttachedFile.url}]\n\n${newMessage.content}`;
|
2026-03-15 00:10:01 +08:00
|
|
|
|
setAttachedFile(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 18:41:58 +08:00
|
|
|
|
const controller = new AbortController();
|
2026-03-17 20:12:48 +08:00
|
|
|
|
abortControllersRef.current[targetSessionKey] = controller;
|
|
|
|
|
|
generatingSessionsRef.current[targetSessionKey] = true;
|
|
|
|
|
|
setIsLoadingForSession(targetSessionKey, true);
|
2026-03-14 15:52:27 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-15 21:57:38 +08:00
|
|
|
|
const assistantId = (Date.now() + 1).toString();
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(targetSessionKey, prev => [...prev, {
|
2026-03-15 21:57:38 +08:00
|
|
|
|
id: assistantId,
|
|
|
|
|
|
role: "assistant",
|
|
|
|
|
|
content: "",
|
2026-03-17 21:32:01 +08:00
|
|
|
|
awaitingFirstToken: true,
|
|
|
|
|
|
progressLogs: ["请求已提交,准备路由..."],
|
2026-03-15 21:57:38 +08:00
|
|
|
|
}]);
|
|
|
|
|
|
|
2026-03-17 21:32:01 +08:00
|
|
|
|
const pushProgressLog = (text: string) => {
|
|
|
|
|
|
if (!text.trim()) return;
|
|
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
|
|
|
|
|
prev.map((msg) => {
|
|
|
|
|
|
if (msg.id !== assistantId) return msg;
|
|
|
|
|
|
const current = msg.progressLogs || [];
|
|
|
|
|
|
if (current[current.length - 1] === text) return msg;
|
|
|
|
|
|
const next = [...current, text].slice(-8);
|
|
|
|
|
|
return { ...msg, progressLogs: next };
|
|
|
|
|
|
})
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-15 21:57:38 +08:00
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
|
|
const effectiveModelId = selectedModelId || currentModel?.id || "";
|
2026-03-16 16:12:35 +08:00
|
|
|
|
|
2026-03-17 11:38:02 +08:00
|
|
|
|
let source = selectedDataSource || "postgres";
|
2026-03-16 16:12:35 +08:00
|
|
|
|
|
2026-03-16 22:18:23 +08:00
|
|
|
|
const useUploadSource = Boolean(currentAttachedFile?.url?.startsWith("local://"));
|
2026-03-16 16:12:35 +08:00
|
|
|
|
if (useUploadSource) {
|
|
|
|
|
|
source = "upload";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 21:57:38 +08:00
|
|
|
|
const fileUrl = useUploadSource ? (currentAttachedFile?.url || activeDataFile?.url) : undefined;
|
|
|
|
|
|
const preferSqlChart = chartIntentPattern.test(messagePayload);
|
|
|
|
|
|
const response = await fetch("/nanobot/chat/stream", {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
|
|
|
|
},
|
|
|
|
|
|
body: JSON.stringify({
|
2026-03-15 00:10:01 +08:00
|
|
|
|
message: messagePayload,
|
2026-03-17 20:12:48 +08:00
|
|
|
|
session_id: targetSessionKey,
|
2026-03-14 23:15:41 +08:00
|
|
|
|
model_id: effectiveModelId,
|
2026-03-15 22:16:04 +08:00
|
|
|
|
skill_ids: selectedSkillIds,
|
2026-03-15 21:57:38 +08:00
|
|
|
|
source,
|
|
|
|
|
|
prefer_sql_chart: preferSqlChart,
|
|
|
|
|
|
file_url: fileUrl,
|
2026-03-17 11:38:02 +08:00
|
|
|
|
route_mode: "auto",
|
2026-03-15 21:57:38 +08:00
|
|
|
|
}),
|
|
|
|
|
|
signal: controller.signal,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.ok || !response.body) {
|
|
|
|
|
|
const err = await response.json().catch(() => ({}));
|
|
|
|
|
|
throw new Error(err.detail || "流式响应失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
|
let buffer = "";
|
|
|
|
|
|
let streamedText = "";
|
|
|
|
|
|
let streamedViz: MessageViz | null = null;
|
2026-03-17 20:40:56 +08:00
|
|
|
|
let hasFinalPayload = false;
|
|
|
|
|
|
let hasDonePayload = false;
|
|
|
|
|
|
let rafPending = false;
|
|
|
|
|
|
let renderedText = "";
|
|
|
|
|
|
|
|
|
|
|
|
const flushAssistant = (force = false) => {
|
2026-03-19 12:27:31 +08:00
|
|
|
|
if (streamedText === renderedText && !force) return;
|
2026-03-17 20:40:56 +08:00
|
|
|
|
if (force) {
|
|
|
|
|
|
renderedText = streamedText;
|
|
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
|
|
|
|
|
prev.map((msg) =>
|
2026-03-19 12:27:31 +08:00
|
|
|
|
msg.id === assistantId ? { ...msg, content: streamedText, awaitingFirstToken: false, viz: streamedViz ?? msg.viz } : msg
|
2026-03-17 20:40:56 +08:00
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (rafPending) return;
|
|
|
|
|
|
rafPending = true;
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
|
rafPending = false;
|
|
|
|
|
|
if (streamedText === renderedText) return;
|
|
|
|
|
|
renderedText = streamedText;
|
|
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
|
|
|
|
|
prev.map((msg) =>
|
2026-03-19 12:27:31 +08:00
|
|
|
|
msg.id === assistantId ? { ...msg, content: streamedText, awaitingFirstToken: false, viz: streamedViz ?? msg.viz } : msg
|
2026-03-17 20:40:56 +08:00
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
2026-03-15 21:57:38 +08:00
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
|
|
if (done) break;
|
|
|
|
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
|
|
const events = buffer.split("\n\n");
|
|
|
|
|
|
buffer = events.pop() || "";
|
|
|
|
|
|
|
|
|
|
|
|
for (const eventBlock of events) {
|
|
|
|
|
|
const line = eventBlock
|
|
|
|
|
|
.split("\n")
|
|
|
|
|
|
.find((item) => item.startsWith("data:"));
|
|
|
|
|
|
if (!line) continue;
|
|
|
|
|
|
const payloadText = line.slice(5).trim();
|
|
|
|
|
|
if (!payloadText) continue;
|
|
|
|
|
|
const payload = JSON.parse(payloadText) as {
|
|
|
|
|
|
type: string;
|
|
|
|
|
|
content?: string;
|
|
|
|
|
|
sql?: string;
|
|
|
|
|
|
result?: unknown;
|
|
|
|
|
|
error?: string;
|
2026-03-17 21:32:01 +08:00
|
|
|
|
selected?: string;
|
|
|
|
|
|
reason?: string;
|
2026-03-15 21:57:38 +08:00
|
|
|
|
chart?: { chart_spec?: ChartSpec | null; reasoning?: string; can_visualize?: boolean; chart_type?: string } | null;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (payload.type === "delta" && payload.content) {
|
|
|
|
|
|
streamedText = `${streamedText}${payload.content}`;
|
2026-03-17 20:40:56 +08:00
|
|
|
|
flushAssistant(false);
|
2026-03-15 21:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 21:32:01 +08:00
|
|
|
|
if (payload.type === "routing") {
|
|
|
|
|
|
const selected = payload.selected === "sql" ? "SQL 分析" : "通用对话";
|
|
|
|
|
|
const reason = payload.reason ? `(${payload.reason})` : "";
|
|
|
|
|
|
pushProgressLog(`路由:${selected}${reason}`);
|
|
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
|
|
|
|
|
prev.map((msg) =>
|
|
|
|
|
|
msg.id === assistantId ? { ...msg, routeInfo: `${selected}${reason}` } : msg
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (payload.type === "progress" && payload.content) {
|
|
|
|
|
|
pushProgressLog(payload.content);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 21:57:38 +08:00
|
|
|
|
if (payload.type === "final" && payload.content) {
|
2026-03-17 20:40:56 +08:00
|
|
|
|
hasFinalPayload = true;
|
2026-03-15 21:57:38 +08:00
|
|
|
|
streamedText = payload.content;
|
2026-03-17 20:40:56 +08:00
|
|
|
|
flushAssistant(true);
|
2026-03-17 21:32:01 +08:00
|
|
|
|
pushProgressLog("回答生成完成");
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
2026-03-15 21:57:38 +08:00
|
|
|
|
prev.map((msg) =>
|
2026-03-17 21:32:01 +08:00
|
|
|
|
msg.id === assistantId ? { ...msg, content: payload.content || "", awaitingFirstToken: false, viz: streamedViz ?? msg.viz } : msg
|
2026-03-15 21:57:38 +08:00
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 20:40:56 +08:00
|
|
|
|
if (payload.type === "done") {
|
|
|
|
|
|
hasDonePayload = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-15 21:57:38 +08:00
|
|
|
|
if (payload.type === "error") {
|
|
|
|
|
|
throw new Error(payload.content || "流式响应错误");
|
|
|
|
|
|
}
|
2026-03-14 15:52:27 +08:00
|
|
|
|
|
2026-03-15 21:57:38 +08:00
|
|
|
|
if (payload.type === "viz") {
|
2026-03-19 15:33:12 +08:00
|
|
|
|
if (payload.chart?.chart_spec) {
|
|
|
|
|
|
pushProgressLog("图表生成完成");
|
|
|
|
|
|
} else if (payload.sql) {
|
|
|
|
|
|
pushProgressLog("数据查询完成");
|
|
|
|
|
|
}
|
2026-03-15 21:57:38 +08:00
|
|
|
|
streamedViz = buildMessageViz(payload);
|
2026-03-19 12:27:31 +08:00
|
|
|
|
flushAssistant(true); // 立即把 viz 状态刷入 messages
|
2026-03-15 21:57:38 +08:00
|
|
|
|
}
|
2026-03-14 15:52:27 +08:00
|
|
|
|
}
|
2026-03-15 21:57:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 20:40:56 +08:00
|
|
|
|
flushAssistant(true);
|
|
|
|
|
|
if (!streamedText && (hasFinalPayload || hasDonePayload)) {
|
|
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
|
|
|
|
|
prev.map((msg) =>
|
|
|
|
|
|
msg.id === assistantId ? { ...msg, content: "暂无回复", awaitingFirstToken: false, viz: streamedViz ?? msg.viz } : msg
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
2026-03-15 21:57:38 +08:00
|
|
|
|
}
|
2026-03-14 15:52:27 +08:00
|
|
|
|
} catch (error: any) {
|
2026-03-15 18:41:58 +08:00
|
|
|
|
if (error?.name === "AbortError" || String(error?.message || "").toLowerCase().includes("aborted")) {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(targetSessionKey, (prev) =>
|
2026-03-15 18:41:58 +08:00
|
|
|
|
prev.map((msg) =>
|
|
|
|
|
|
msg.awaitingFirstToken
|
|
|
|
|
|
? { ...msg, awaitingFirstToken: false, content: msg.content || "已中断输出" }
|
|
|
|
|
|
: msg
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-17 20:12:48 +08:00
|
|
|
|
setMessagesForSession(targetSessionKey, prev => [...prev, {
|
2026-03-14 15:52:27 +08:00
|
|
|
|
id: (Date.now() + 1).toString(),
|
|
|
|
|
|
role: 'assistant',
|
|
|
|
|
|
content: `Sorry, something went wrong: ${error.message}`
|
|
|
|
|
|
}]);
|
|
|
|
|
|
} finally {
|
2026-03-17 20:12:48 +08:00
|
|
|
|
if (abortControllersRef.current[targetSessionKey] === controller) {
|
|
|
|
|
|
delete abortControllersRef.current[targetSessionKey];
|
2026-03-15 18:41:58 +08:00
|
|
|
|
}
|
2026-03-17 20:12:48 +08:00
|
|
|
|
generatingSessionsRef.current[targetSessionKey] = false;
|
|
|
|
|
|
setIsLoadingForSession(targetSessionKey, false);
|
2026-03-14 23:15:41 +08:00
|
|
|
|
window.dispatchEvent(new Event("nanobot:sessions-changed"));
|
2026-03-14 15:52:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<div className="flex flex-col h-full bg-white relative">
|
|
|
|
|
|
{/* Header with Model Selection */}
|
|
|
|
|
|
<div className="px-4 py-3 flex items-center justify-between border-b border-zinc-100 bg-white/50 backdrop-blur-md sticky top-0 z-20">
|
2026-03-14 20:58:38 +08:00
|
|
|
|
<Popover open={modelOpen} onOpenChange={setModelOpen}>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<PopoverTrigger className="flex items-center gap-2 px-3 py-1.5 rounded-lg hover:bg-zinc-100 transition-colors group">
|
|
|
|
|
|
<span className="font-semibold text-zinc-900">
|
|
|
|
|
|
{selectedModelId ? models.find(m => m.id === selectedModelId)?.name || 'DataClaw' : 'DataClaw'}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<ChevronDown className="h-4 w-4 text-zinc-400 group-hover:text-zinc-600 transition-colors" />
|
2026-03-14 20:58:38 +08:00
|
|
|
|
</PopoverTrigger>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<PopoverContent className="w-[280px] p-0" align="start">
|
2026-03-14 20:58:38 +08:00
|
|
|
|
<Command>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<CommandInput placeholder="搜索模型..." />
|
|
|
|
|
|
<CommandList className="max-h-[300px]">
|
2026-03-14 20:58:38 +08:00
|
|
|
|
<CommandEmpty>未找到模型</CommandEmpty>
|
|
|
|
|
|
<CommandGroup heading="可用模型">
|
|
|
|
|
|
{models.map((model) => (
|
|
|
|
|
|
<CommandItem
|
|
|
|
|
|
key={model.id}
|
|
|
|
|
|
onSelect={() => {
|
|
|
|
|
|
setSelectedModelId(model.id);
|
|
|
|
|
|
setModelOpen(false);
|
|
|
|
|
|
}}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
className="flex items-center gap-2 py-2.5 cursor-pointer"
|
2026-03-14 20:58:38 +08:00
|
|
|
|
>
|
|
|
|
|
|
<div className="flex flex-col">
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<span className="font-medium text-zinc-900">{model.name || model.model}</span>
|
2026-03-14 20:58:38 +08:00
|
|
|
|
<span className="text-xs text-zinc-400">{model.provider}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<Check
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"ml-auto h-4 w-4",
|
|
|
|
|
|
selectedModelId === model.id ? "opacity-100" : "opacity-0"
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</CommandItem>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</CommandGroup>
|
|
|
|
|
|
</CommandList>
|
|
|
|
|
|
</Command>
|
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
|
</Popover>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-03-15 19:07:44 +08:00
|
|
|
|
<ScrollArea className="flex-1 min-h-0">
|
2026-03-15 00:10:01 +08:00
|
|
|
|
{/* Hidden file input available in all states */}
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="file"
|
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
|
className="hidden"
|
|
|
|
|
|
accept=".csv,.xls,.xlsx"
|
|
|
|
|
|
onChange={handleFileUpload}
|
|
|
|
|
|
/>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
<div className="min-h-full">
|
2026-03-15 17:57:09 +08:00
|
|
|
|
{messages.length === 0 ? (
|
2026-03-14 15:52:27 +08:00
|
|
|
|
<div className="h-full flex flex-col items-center justify-center pt-[20vh] px-4 pb-32">
|
|
|
|
|
|
{/* Logo Area */}
|
|
|
|
|
|
<div className="mb-16 flex items-center justify-center gap-4 select-none">
|
|
|
|
|
|
<div className="text-[64px] leading-none animate-bounce-slow pb-3">
|
|
|
|
|
|
🦞
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<h1 className="text-[56px] font-bold bg-clip-text text-transparent bg-gradient-to-r from-red-500 via-orange-500 to-amber-500 tracking-tight">
|
|
|
|
|
|
DataClaw
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Input Area */}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<div className="w-full max-w-4xl px-4">
|
|
|
|
|
|
<div className="relative group">
|
|
|
|
|
|
<div className="flex flex-col bg-white rounded-[26px] border border-zinc-200 shadow-[0_2px_12px_rgba(0,0,0,0.04)] transition-all duration-200">
|
|
|
|
|
|
{renderFileCard()}
|
2026-03-16 22:18:23 +08:00
|
|
|
|
{renderActiveSelections()}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<div className="flex items-center pl-2 pr-2 py-2">
|
|
|
|
|
|
<div className="flex items-center">
|
|
|
|
|
|
<Popover open={isMenuOpen} onOpenChange={setIsMenuOpen}>
|
|
|
|
|
|
<PopoverTrigger className="flex items-center justify-center h-9 w-9 rounded-full hover:bg-zinc-100 transition-colors text-zinc-500">
|
|
|
|
|
|
<Plus className="h-5 w-5" />
|
|
|
|
|
|
</PopoverTrigger>
|
|
|
|
|
|
<PopoverContent side="top" align="start" className="w-[480px] p-0 mb-2 overflow-hidden rounded-2xl border-zinc-200 shadow-xl">
|
|
|
|
|
|
<div className="flex divide-x divide-zinc-100">
|
|
|
|
|
|
{/* Left Column: Data Source */}
|
|
|
|
|
|
<div className="flex-1 p-3 bg-zinc-50/50">
|
|
|
|
|
|
<div className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider mb-2 px-2 flex items-center gap-1.5">
|
|
|
|
|
|
<Database className="h-3 w-3" />
|
|
|
|
|
|
数据源
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="space-y-0.5">
|
2026-03-16 16:12:35 +08:00
|
|
|
|
{availableDataSources.map((ds) => (
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<button
|
|
|
|
|
|
key={ds.id}
|
|
|
|
|
|
onClick={() => {
|
2026-03-16 23:16:33 +08:00
|
|
|
|
void handleSelectDataSource(ds.id);
|
2026-03-15 22:16:04 +08:00
|
|
|
|
}}
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"w-full flex items-center justify-between px-3 py-2.5 rounded-xl text-sm transition-all duration-200",
|
|
|
|
|
|
selectedDataSource === ds.id
|
|
|
|
|
|
? "bg-white text-zinc-900 shadow-sm ring-1 ring-zinc-200"
|
|
|
|
|
|
: "text-zinc-600 hover:bg-white hover:shadow-sm"
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div className="flex items-center gap-2.5">
|
2026-03-16 16:12:35 +08:00
|
|
|
|
<Database className={cn("h-4 w-4", selectedDataSource === ds.id ? "text-blue-500" : "text-zinc-400")} />
|
|
|
|
|
|
<span className="font-medium">{ds.name}</span>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
{selectedDataSource === ds.id && <CheckCircle2 className="h-4 w-4 text-blue-500" />}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
2026-03-16 22:18:23 +08:00
|
|
|
|
{selectedDataSource && (
|
|
|
|
|
|
<div className="mt-2 pt-2 border-t border-zinc-100">
|
|
|
|
|
|
<button
|
2026-03-16 23:16:33 +08:00
|
|
|
|
onClick={() => {
|
|
|
|
|
|
void handleClearDataSource();
|
|
|
|
|
|
}}
|
2026-03-16 22:18:23 +08:00
|
|
|
|
className="w-full py-1.5 text-[11px] text-zinc-400 hover:text-zinc-600 transition-colors flex items-center justify-center gap-1"
|
|
|
|
|
|
>
|
|
|
|
|
|
清除已选
|
|
|
|
|
|
</button>
|
2026-03-16 16:12:35 +08:00
|
|
|
|
</div>
|
2026-03-16 22:18:23 +08:00
|
|
|
|
)}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Right Column: Skills */}
|
|
|
|
|
|
<div className="flex-1 p-3 bg-white">
|
|
|
|
|
|
<div className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider mb-2 px-2 flex items-center gap-1.5">
|
|
|
|
|
|
<Wand2 className="h-3 w-3" />
|
|
|
|
|
|
Skills
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="space-y-0.5 max-h-[300px] overflow-y-auto pr-1">
|
|
|
|
|
|
{availableSkills.length > 0 ? (
|
|
|
|
|
|
availableSkills.map((skill) => {
|
|
|
|
|
|
const isSelected = selectedSkillIds.includes(skill.id);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={skill.id}
|
|
|
|
|
|
onClick={() => {
|
2026-03-16 22:18:23 +08:00
|
|
|
|
setSelectedSkillIds((prev) =>
|
|
|
|
|
|
isSelected
|
|
|
|
|
|
? prev.filter((id) => id !== skill.id)
|
2026-03-15 22:16:04 +08:00
|
|
|
|
: [...prev, skill.id]
|
|
|
|
|
|
);
|
|
|
|
|
|
}}
|
|
|
|
|
|
className={cn(
|
2026-03-16 22:18:23 +08:00
|
|
|
|
"w-full flex items-center justify-between px-3 py-2.5 rounded-xl text-sm transition-all duration-200",
|
2026-03-15 22:16:04 +08:00
|
|
|
|
isSelected
|
2026-03-16 22:18:23 +08:00
|
|
|
|
? "bg-white text-zinc-900 shadow-sm ring-1 ring-zinc-200"
|
|
|
|
|
|
: "text-zinc-600 hover:bg-white hover:shadow-sm"
|
2026-03-15 22:16:04 +08:00
|
|
|
|
)}
|
|
|
|
|
|
>
|
2026-03-16 22:18:23 +08:00
|
|
|
|
<div className="flex items-center text-left">
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<span className="font-medium">{skill.name}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{isSelected && <CheckCircle2 className="h-4 w-4 text-blue-500" />}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
})
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="px-3 py-8 text-center">
|
|
|
|
|
|
<Zap className="h-8 w-8 text-zinc-100 mx-auto mb-2" />
|
|
|
|
|
|
<p className="text-xs text-zinc-400">暂无可用技能</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{selectedSkillIds.length > 0 && (
|
|
|
|
|
|
<div className="mt-2 pt-2 border-t border-zinc-100">
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => setSelectedSkillIds([])}
|
|
|
|
|
|
className="w-full py-1.5 text-[11px] text-zinc-400 hover:text-zinc-600 transition-colors flex items-center justify-center gap-1"
|
|
|
|
|
|
>
|
|
|
|
|
|
清除已选 ({selectedSkillIds.length})
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
|
</Popover>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
value={input}
|
2026-03-18 17:28:48 +08:00
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
|
onKeyDown={handleInputKeyDown}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
placeholder="有问题,尽管问"
|
|
|
|
|
|
className="flex-1 bg-transparent border-none focus:ring-0 text-lg px-3 py-2 text-zinc-900 placeholder:text-zinc-300 outline-none"
|
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
|
/>
|
2026-03-18 17:28:48 +08:00
|
|
|
|
<SlashCommandMenu
|
|
|
|
|
|
isOpen={slashQuery !== null}
|
|
|
|
|
|
skills={filteredSlashSkills}
|
|
|
|
|
|
selectedIndex={slashIndex}
|
|
|
|
|
|
onSelect={handleSelectSlashSkill}
|
|
|
|
|
|
onClose={() => setSlashQuery(null)}
|
|
|
|
|
|
/>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleSend}
|
2026-03-17 11:38:02 +08:00
|
|
|
|
disabled={isLoading || !input.trim()}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
className={cn(
|
|
|
|
|
|
"flex items-center justify-center h-10 w-10 rounded-full transition-all duration-200",
|
|
|
|
|
|
(input.trim() || attachedFile || activeDataFile) && !isLoading
|
|
|
|
|
|
? "bg-zinc-900 text-white hover:bg-zinc-800 shadow-sm"
|
|
|
|
|
|
: "bg-zinc-100 text-zinc-300"
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
|
<Loader2 className="h-5 w-5 animate-spin" />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<ArrowUp className="h-6 w-6" />
|
|
|
|
|
|
)}
|
|
|
|
|
|
</button>
|
2026-03-15 00:10:01 +08:00
|
|
|
|
</div>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<div className="mt-4 flex flex-wrap justify-center gap-2">
|
|
|
|
|
|
{/* Common Questions or suggestions could go here */}
|
|
|
|
|
|
</div>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
2026-03-15 19:07:44 +08:00
|
|
|
|
<div className="max-w-3xl mx-auto px-4 py-8 space-y-8">
|
2026-03-18 22:42:18 +08:00
|
|
|
|
{messages.map((msg) => {
|
|
|
|
|
|
const { markdown, reportHtml } = splitReportHtml(msg.content);
|
|
|
|
|
|
return (
|
2026-03-14 15:52:27 +08:00
|
|
|
|
<div
|
|
|
|
|
|
key={msg.id}
|
|
|
|
|
|
className={`flex gap-4 ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
{msg.role !== "user" && (
|
2026-03-17 16:58:21 +08:00
|
|
|
|
<div className="w-8 h-8 flex items-center justify-center shrink-0 mt-1">
|
|
|
|
|
|
<span className="text-2xl">🦞</span>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<div
|
|
|
|
|
|
className={`rounded-2xl px-5 py-3.5 text-[15px] leading-relaxed max-w-[85%] shadow-sm ${
|
|
|
|
|
|
msg.role === "user"
|
|
|
|
|
|
? "bg-zinc-100 text-zinc-800"
|
2026-03-14 22:15:38 +08:00
|
|
|
|
: "bg-white border border-zinc-100 text-zinc-700 overflow-hidden"
|
2026-03-14 15:52:27 +08:00
|
|
|
|
}`}
|
|
|
|
|
|
>
|
2026-03-14 22:15:38 +08:00
|
|
|
|
{msg.role === "assistant" ? (
|
2026-03-17 21:32:01 +08:00
|
|
|
|
<>
|
|
|
|
|
|
{msg.progressLogs && msg.progressLogs.length > 0 ? (
|
|
|
|
|
|
<div className="mb-2 rounded-xl border border-zinc-100 bg-zinc-50/70 px-3 py-2">
|
|
|
|
|
|
<div className="flex items-center gap-2 text-zinc-500 text-xs">
|
|
|
|
|
|
{msg.awaitingFirstToken ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <CheckCircle2 className="h-3.5 w-3.5 text-emerald-500" />}
|
|
|
|
|
|
<span>{msg.awaitingFirstToken ? "正在处理中" : "处理完成"}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="mt-1.5 space-y-1">
|
2026-03-18 10:57:48 +08:00
|
|
|
|
{msg.progressLogs.map((log, idx, arr) => {
|
|
|
|
|
|
const isLast = idx === arr.length - 1;
|
|
|
|
|
|
const isLoading = isLast && msg.awaitingFirstToken;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div key={`${msg.id}-log-${idx}`} className="flex items-start gap-2 text-[12px] text-zinc-500 leading-5">
|
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
|
<Settings className="mt-0.5 h-3.5 w-3.5 text-amber-500 animate-spin" />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<CheckCircle2 className="mt-0.5 h-3.5 w-3.5 text-emerald-500" />
|
|
|
|
|
|
)}
|
|
|
|
|
|
<span>{log}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
})}
|
2026-03-17 21:32:01 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
{msg.awaitingFirstToken && !msg.content ? (
|
|
|
|
|
|
<div className="flex items-center gap-2 text-zinc-500 text-sm py-1">
|
|
|
|
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
|
|
|
|
<span>模型思考中,请稍候...</span>
|
2026-03-15 11:07:18 +08:00
|
|
|
|
</div>
|
2026-03-17 21:32:01 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<>
|
2026-03-18 22:42:18 +08:00
|
|
|
|
{markdown ? (
|
|
|
|
|
|
<div className="prose prose-sm prose-zinc max-w-none prose-p:leading-normal prose-p:my-2 prose-headings:my-3 prose-ul:my-2 prose-li:my-0.5 prose-pre:bg-zinc-50 prose-pre:text-zinc-800 prose-pre:border prose-pre:border-zinc-200">
|
|
|
|
|
|
<ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw]}>
|
|
|
|
|
|
{markdown}
|
|
|
|
|
|
</ReactMarkdown>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
{reportHtml ? (
|
|
|
|
|
|
<div className="mt-3 rounded-xl border border-zinc-200 overflow-hidden bg-white">
|
|
|
|
|
|
<iframe
|
|
|
|
|
|
title={`report-${msg.id}`}
|
|
|
|
|
|
srcDoc={reportHtml}
|
|
|
|
|
|
sandbox="allow-same-origin"
|
|
|
|
|
|
className="w-full h-[620px] bg-white"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
2026-03-17 21:32:01 +08:00
|
|
|
|
{msg.viz ? (
|
|
|
|
|
|
<div className="mt-3 pt-3 border-t border-zinc-100">
|
|
|
|
|
|
<InlineVisualizationCard viz={msg.viz} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</>
|
2026-03-14 22:15:38 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
msg.content
|
|
|
|
|
|
)}
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
{msg.role === "user" && (
|
|
|
|
|
|
<div className="w-8 h-8 rounded-full bg-zinc-200 flex items-center justify-center text-zinc-500 shrink-0 mt-1">
|
|
|
|
|
|
<User className="h-4 w-4" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2026-03-18 22:42:18 +08:00
|
|
|
|
)})}
|
2026-03-14 15:52:27 +08:00
|
|
|
|
<div ref={scrollRef} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Floating Input for Chat State */}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
{messages.length > 0 && (
|
2026-03-15 19:07:44 +08:00
|
|
|
|
<div className="px-4 pb-6 pt-3 border-t border-zinc-100 bg-white">
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<div className="relative group max-w-4xl mx-auto">
|
|
|
|
|
|
<div className="flex flex-col bg-white rounded-[26px] border border-zinc-200 shadow-[0_2px_12px_rgba(0,0,0,0.04)] transition-all duration-200">
|
|
|
|
|
|
{renderFileCard()}
|
2026-03-16 22:18:23 +08:00
|
|
|
|
{renderActiveSelections()}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<div className="flex items-center pl-2 pr-2 py-2">
|
|
|
|
|
|
<div className="flex items-center">
|
|
|
|
|
|
<Popover open={isMenuOpen} onOpenChange={setIsMenuOpen}>
|
|
|
|
|
|
<PopoverTrigger className="flex items-center justify-center h-9 w-9 rounded-full hover:bg-zinc-100 transition-colors text-zinc-500">
|
|
|
|
|
|
<Plus className="h-5 w-5" />
|
|
|
|
|
|
</PopoverTrigger>
|
|
|
|
|
|
<PopoverContent side="top" align="start" className="w-[480px] p-0 mb-2 overflow-hidden rounded-2xl border-zinc-200 shadow-xl">
|
|
|
|
|
|
<div className="flex divide-x divide-zinc-100">
|
|
|
|
|
|
{/* Left Column: Data Source */}
|
|
|
|
|
|
<div className="flex-1 p-3 bg-zinc-50/50">
|
|
|
|
|
|
<div className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider mb-2 px-2 flex items-center gap-1.5">
|
|
|
|
|
|
<Database className="h-3 w-3" />
|
|
|
|
|
|
数据源
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="space-y-0.5">
|
2026-03-16 16:12:35 +08:00
|
|
|
|
{availableDataSources.map((ds) => (
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<button
|
|
|
|
|
|
key={ds.id}
|
|
|
|
|
|
onClick={() => {
|
2026-03-16 23:16:33 +08:00
|
|
|
|
void handleSelectDataSource(ds.id);
|
2026-03-15 22:16:04 +08:00
|
|
|
|
}}
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"w-full flex items-center justify-between px-3 py-2.5 rounded-xl text-sm transition-all duration-200",
|
|
|
|
|
|
selectedDataSource === ds.id
|
|
|
|
|
|
? "bg-white text-zinc-900 shadow-sm ring-1 ring-zinc-200"
|
|
|
|
|
|
: "text-zinc-600 hover:bg-white hover:shadow-sm"
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<div className="flex items-center gap-2.5">
|
2026-03-16 16:12:35 +08:00
|
|
|
|
<Database className={cn("h-4 w-4", selectedDataSource === ds.id ? "text-blue-500" : "text-zinc-400")} />
|
|
|
|
|
|
<span className="font-medium">{ds.name}</span>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
{selectedDataSource === ds.id && <CheckCircle2 className="h-4 w-4 text-blue-500" />}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
2026-03-16 22:18:23 +08:00
|
|
|
|
{selectedDataSource && (
|
|
|
|
|
|
<div className="mt-2 pt-2 border-t border-zinc-100">
|
|
|
|
|
|
<button
|
2026-03-16 23:16:33 +08:00
|
|
|
|
onClick={() => {
|
|
|
|
|
|
void handleClearDataSource();
|
|
|
|
|
|
}}
|
2026-03-16 22:18:23 +08:00
|
|
|
|
className="w-full py-1.5 text-[11px] text-zinc-400 hover:text-zinc-600 transition-colors flex items-center justify-center gap-1"
|
|
|
|
|
|
>
|
|
|
|
|
|
清除已选
|
|
|
|
|
|
</button>
|
2026-03-16 16:12:35 +08:00
|
|
|
|
</div>
|
2026-03-16 22:18:23 +08:00
|
|
|
|
)}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Right Column: Skills */}
|
|
|
|
|
|
<div className="flex-1 p-3 bg-white">
|
|
|
|
|
|
<div className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider mb-2 px-2 flex items-center gap-1.5">
|
|
|
|
|
|
<Wand2 className="h-3 w-3" />
|
|
|
|
|
|
Skills
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="space-y-0.5 max-h-[300px] overflow-y-auto pr-1">
|
|
|
|
|
|
{availableSkills.length > 0 ? (
|
|
|
|
|
|
availableSkills.map((skill) => {
|
|
|
|
|
|
const isSelected = selectedSkillIds.includes(skill.id);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={skill.id}
|
|
|
|
|
|
onClick={() => {
|
2026-03-16 22:18:23 +08:00
|
|
|
|
setSelectedSkillIds((prev) =>
|
|
|
|
|
|
isSelected
|
|
|
|
|
|
? prev.filter((id) => id !== skill.id)
|
2026-03-15 22:16:04 +08:00
|
|
|
|
: [...prev, skill.id]
|
|
|
|
|
|
);
|
|
|
|
|
|
}}
|
|
|
|
|
|
className={cn(
|
2026-03-16 22:18:23 +08:00
|
|
|
|
"w-full flex items-center justify-between px-3 py-2.5 rounded-xl text-sm transition-all duration-200",
|
2026-03-15 22:16:04 +08:00
|
|
|
|
isSelected
|
2026-03-16 22:18:23 +08:00
|
|
|
|
? "bg-white text-zinc-900 shadow-sm ring-1 ring-zinc-200"
|
|
|
|
|
|
: "text-zinc-600 hover:bg-white hover:shadow-sm"
|
2026-03-15 22:16:04 +08:00
|
|
|
|
)}
|
|
|
|
|
|
>
|
2026-03-16 22:18:23 +08:00
|
|
|
|
<div className="flex items-center text-left">
|
2026-03-15 22:16:04 +08:00
|
|
|
|
<span className="font-medium">{skill.name}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{isSelected && <CheckCircle2 className="h-4 w-4 text-blue-500" />}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
})
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="px-3 py-8 text-center">
|
|
|
|
|
|
<Zap className="h-8 w-8 text-zinc-100 mx-auto mb-2" />
|
|
|
|
|
|
<p className="text-xs text-zinc-400">暂无可用技能</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{selectedSkillIds.length > 0 && (
|
|
|
|
|
|
<div className="mt-2 pt-2 border-t border-zinc-100">
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={() => setSelectedSkillIds([])}
|
|
|
|
|
|
className="w-full py-1.5 text-[11px] text-zinc-400 hover:text-zinc-600 transition-colors flex items-center justify-center gap-1"
|
|
|
|
|
|
>
|
|
|
|
|
|
清除已选 ({selectedSkillIds.length})
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
|
</Popover>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
value={input}
|
2026-03-18 17:28:48 +08:00
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
|
onKeyDown={handleInputKeyDown}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
placeholder="有问题,尽管问"
|
|
|
|
|
|
className="flex-1 bg-transparent border-none focus:ring-0 text-lg px-3 py-2 text-zinc-900 placeholder:text-zinc-300 outline-none"
|
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
|
/>
|
2026-03-18 17:28:48 +08:00
|
|
|
|
<SlashCommandMenu
|
|
|
|
|
|
isOpen={slashQuery !== null}
|
|
|
|
|
|
skills={filteredSlashSkills}
|
|
|
|
|
|
selectedIndex={slashIndex}
|
|
|
|
|
|
onSelect={handleSelectSlashSkill}
|
|
|
|
|
|
onClose={() => setSlashQuery(null)}
|
|
|
|
|
|
/>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
|
|
<button
|
2026-03-15 18:41:58 +08:00
|
|
|
|
onClick={isLoading ? handleForceStop : handleSend}
|
2026-03-17 11:38:02 +08:00
|
|
|
|
disabled={isLoading ? false : !input.trim()}
|
2026-03-15 22:16:04 +08:00
|
|
|
|
className={cn(
|
|
|
|
|
|
"flex items-center justify-center h-10 w-10 rounded-full transition-all duration-200",
|
|
|
|
|
|
(input.trim() || isLoading)
|
|
|
|
|
|
? (isLoading ? "bg-red-600 text-white hover:bg-red-700" : "bg-zinc-900 text-white hover:bg-zinc-800")
|
|
|
|
|
|
: "bg-zinc-100 text-zinc-300"
|
|
|
|
|
|
)}
|
2026-03-15 00:10:01 +08:00
|
|
|
|
>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
{isLoading ? (
|
|
|
|
|
|
<Square className="h-4 w-4" />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<ArrowUp className="h-6 w-6" />
|
|
|
|
|
|
)}
|
|
|
|
|
|
</button>
|
2026-03-15 00:10:01 +08:00
|
|
|
|
</div>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
2026-03-15 22:16:04 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="mt-2 flex justify-center">
|
|
|
|
|
|
<p className="text-[11px] text-zinc-400">
|
|
|
|
|
|
DataClaw 可能会出错。请核查重要信息。
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
2026-03-14 15:52:27 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|