From 6f074df40eda7005083fc6de4df459dacc8f745f Mon Sep 17 00:00:00 2001 From: qixinbo Date: Sun, 22 Mar 2026 16:48:41 +0800 Subject: [PATCH] UI: project as seperated workspace --- backend/app/core/session_alias_store.py | 49 +++++--- backend/main.py | 22 +++- frontend/src/App.tsx | 8 +- frontend/src/components/ProjectSwitcher.tsx | 2 +- frontend/src/components/Sidebar.tsx | 12 +- frontend/src/i18n/locales/en.json | 1 + frontend/src/i18n/locales/zh.json | 1 + frontend/src/pages/Dashboard.tsx | 126 +++++++++++++++++++- frontend/src/store/dashboardStore.ts | 16 +++ 9 files changed, 206 insertions(+), 31 deletions(-) diff --git a/backend/app/core/session_alias_store.py b/backend/app/core/session_alias_store.py index 153d1c1..ea6b24f 100644 --- a/backend/app/core/session_alias_store.py +++ b/backend/app/core/session_alias_store.py @@ -42,6 +42,8 @@ class SessionAliasStore: conn.execute("ALTER TABLE session_cache ADD COLUMN pinned INTEGER NOT NULL DEFAULT 0") if "archived" not in cols: conn.execute("ALTER TABLE session_cache ADD COLUMN archived INTEGER NOT NULL DEFAULT 0") + if "project_id" not in cols: + conn.execute("ALTER TABLE session_cache ADD COLUMN project_id INTEGER") def sync_sessions(self, sessions: list[dict[str, Any]]) -> None: now = datetime.now(timezone.utc).isoformat() @@ -75,20 +77,31 @@ class SessionAliasStore: else: conn.execute("DELETE FROM session_cache") - def list_cached_sessions(self) -> list[dict[str, Any]]: + def list_cached_sessions(self, project_id: int | None = None) -> list[dict[str, Any]]: with self._connect() as conn: - rows = conn.execute( - """ - SELECT session_key, created_at, updated_at, alias, pinned, archived - FROM session_cache - ORDER BY pinned DESC, archived ASC, updated_at DESC - """ - ).fetchall() + if project_id is not None: + rows = conn.execute( + """ + SELECT session_key, created_at, updated_at, alias, pinned, archived, project_id + FROM session_cache + WHERE project_id = ? OR project_id IS NULL + ORDER BY pinned DESC, archived ASC, updated_at DESC + """, + (project_id,) + ).fetchall() + else: + rows = conn.execute( + """ + SELECT session_key, created_at, updated_at, alias, pinned, archived, project_id + FROM session_cache + ORDER BY pinned DESC, archived ASC, updated_at DESC + """ + ).fetchall() return [self._row_to_session_item(row) for row in rows] - def sync_and_list(self, sessions: list[dict[str, Any]]) -> list[dict[str, Any]]: + def sync_and_list(self, sessions: list[dict[str, Any]], project_id: int | None = None) -> list[dict[str, Any]]: self.sync_sessions(sessions) - return self.list_cached_sessions() + return self.list_cached_sessions(project_id) def set_alias(self, session_key: str, alias: str) -> None: now = datetime.now(timezone.utc).isoformat() @@ -111,32 +124,36 @@ class SessionAliasStore: alias: str | None = None, pinned: bool | None = None, archived: bool | None = None, + project_id: int | None = None, ) -> dict[str, Any]: now = datetime.now(timezone.utc).isoformat() with self._connect() as conn: row = conn.execute( - "SELECT alias, pinned, archived FROM session_cache WHERE session_key = ?", + "SELECT alias, pinned, archived, project_id FROM session_cache WHERE session_key = ?", (session_key,), ).fetchone() current_alias = (str(row["alias"]) if row and row["alias"] else "") current_pinned = bool(row["pinned"]) if row else False current_archived = bool(row["archived"]) if row else False + current_project_id = row["project_id"] if row and "project_id" in row.keys() else None next_alias = current_alias if alias is None else alias.strip() next_pinned = current_pinned if pinned is None else bool(pinned) next_archived = current_archived if archived is None else bool(archived) + next_project_id = current_project_id if project_id is None else project_id conn.execute( """ - INSERT INTO session_cache (session_key, created_at, updated_at, alias, pinned, archived, last_seen_at) - VALUES (?, '', '', ?, ?, ?, ?) + INSERT INTO session_cache (session_key, created_at, updated_at, alias, pinned, archived, project_id, last_seen_at) + VALUES (?, '', '', ?, ?, ?, ?, ?) ON CONFLICT(session_key) DO UPDATE SET alias = excluded.alias, pinned = excluded.pinned, archived = excluded.archived, + project_id = excluded.project_id, last_seen_at = excluded.last_seen_at """, - (session_key, next_alias, int(next_pinned), int(next_archived), now), + (session_key, next_alias, int(next_pinned), int(next_archived), next_project_id, now), ) - return {"alias": next_alias or None, "pinned": next_pinned, "archived": next_archived} + return {"alias": next_alias or None, "pinned": next_pinned, "archived": next_archived, "project_id": next_project_id} def get_alias(self, session_key: str) -> str | None: with self._connect() as conn: @@ -159,6 +176,7 @@ class SessionAliasStore: title = alias or fallback pinned = bool(row["pinned"]) if "pinned" in row.keys() else False archived = bool(row["archived"]) if "archived" in row.keys() else False + project_id = row["project_id"] if "project_id" in row.keys() else None return { "key": row["session_key"], "created_at": row["created_at"], @@ -167,6 +185,7 @@ class SessionAliasStore: "alias": alias or None, "pinned": pinned, "archived": archived, + "project_id": project_id, } diff --git a/backend/main.py b/backend/main.py index 00fcec7..7b1dddd 100644 --- a/backend/main.py +++ b/backend/main.py @@ -116,6 +116,7 @@ class SessionAliasUpdateRequest(BaseModel): title: Optional[str] = None pinned: Optional[bool] = None archived: Optional[bool] = None + project_id: Optional[int] = None class BatchDeleteRequest(BaseModel): @@ -299,11 +300,11 @@ async def nanobot_chat_stream(request: ChatRequest): ) @app.get("/nanobot/sessions") -def get_sessions(): +def get_sessions(project_id: Optional[int] = None): if not nanobot_service.agent: - return session_alias_store.list_cached_sessions() + return session_alias_store.list_cached_sessions(project_id=project_id) sessions = nanobot_service.agent.sessions.list_sessions() - return session_alias_store.sync_and_list(sessions) + return session_alias_store.sync_and_list(sessions, project_id=project_id) @app.get("/nanobot/sessions/{session_id}") def get_session(session_id: str): @@ -320,12 +321,23 @@ def get_session(session_id: str): "messages": session.messages } +class EnsureSessionRequest(BaseModel): + project_id: Optional[int] = None + @app.post("/nanobot/sessions/{session_id}/ensure") -def ensure_session(session_id: str): +def ensure_session(session_id: str, request: EnsureSessionRequest = EnsureSessionRequest()): if not nanobot_service.agent: raise HTTPException(status_code=400, detail="Nanobot not running") session = nanobot_service.agent.sessions.get_or_create(session_id) nanobot_service.agent.sessions.save(session) + + # Save project_id to the alias store immediately upon creation + if request.project_id is not None: + session_alias_store.update_alias_meta( + session_key=session_id, + project_id=request.project_id + ) + alias = session_alias_store.get_alias(session_id) return { "key": session.key, @@ -333,6 +345,7 @@ def ensure_session(session_id: str): "updated_at": session.updated_at, "metadata": session.metadata, "alias": alias, + "project_id": request.project_id } @app.delete("/nanobot/sessions/{session_id}") @@ -382,6 +395,7 @@ def update_session(session_id: str, payload: SessionAliasUpdateRequest): alias=payload.title, pinned=payload.pinned, archived=payload.archived, + project_id=payload.project_id, ) return {"status": "success", **updated} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 99f90d1..e4832f9 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -32,9 +32,11 @@ function MainLayout({ children }: { children: React.ReactNode }) { return (
-
-
- +
+
+
+ +
{children} diff --git a/frontend/src/components/ProjectSwitcher.tsx b/frontend/src/components/ProjectSwitcher.tsx index 532bba2..87a62c4 100644 --- a/frontend/src/components/ProjectSwitcher.tsx +++ b/frontend/src/components/ProjectSwitcher.tsx @@ -40,7 +40,7 @@ export function ProjectSwitcher() { }; return ( -
+
diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx index 91dbb20..8946f7c 100644 --- a/frontend/src/components/Sidebar.tsx +++ b/frontend/src/components/Sidebar.tsx @@ -359,7 +359,7 @@ function DashboardSection({ className="text-red-600 focus:text-red-600 focus:bg-red-50" > - {t('deleteSession')} + {t('delete')} @@ -409,7 +409,10 @@ function SidebarBody() { const fetchSessions = async () => { try { - const data = await api.get("/nanobot/sessions"); + const url = currentProject + ? `/nanobot/sessions?project_id=${currentProject.id}` + : "/nanobot/sessions"; + const data = await api.get(url); setSessions(data); } catch (e) { console.error("Failed to fetch sessions", e); @@ -418,7 +421,7 @@ function SidebarBody() { useEffect(() => { fetchSessions(); - }, [location.pathname, location.search]); + }, [location.pathname, location.search, currentProject?.id]); useEffect(() => { const onFocus = () => fetchSessions(); @@ -453,7 +456,8 @@ function SidebarBody() { const handleNewThread = async () => { const newSessionId = `api:${Date.now()}`; try { - await api.post(`/nanobot/sessions/${encodeURIComponent(newSessionId)}/ensure`, {}); + const payload = currentProject ? { project_id: currentProject.id } : {}; + await api.post(`/nanobot/sessions/${encodeURIComponent(newSessionId)}/ensure`, payload); await fetchSessions(); window.dispatchEvent(new Event("nanobot:sessions-changed")); } catch (e) { diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json index 6b24fdd..a22313f 100644 --- a/frontend/src/i18n/locales/en.json +++ b/frontend/src/i18n/locales/en.json @@ -8,6 +8,7 @@ "unpin": "Unpin", "archive": "Archive", "unarchive": "Unarchive", + "delete": "Delete", "deleteSession": "Delete Session", "confirmDeleteSession": "Are you sure you want to delete this session?", "confirmBatchDeleteSessions": "Are you sure you want to delete the selected {{count}} threads?", diff --git a/frontend/src/i18n/locales/zh.json b/frontend/src/i18n/locales/zh.json index 3cba2fc..cbe97ff 100644 --- a/frontend/src/i18n/locales/zh.json +++ b/frontend/src/i18n/locales/zh.json @@ -8,6 +8,7 @@ "unpin": "取消置顶", "archive": "归档", "unarchive": "取消归档", + "delete": "删除", "deleteSession": "删除会话", "confirmDeleteSession": "确定要删除这个会话吗?", "confirmBatchDeleteSessions": "确定要删除选中的 {{count}} 个会话吗?", diff --git a/frontend/src/pages/Dashboard.tsx b/frontend/src/pages/Dashboard.tsx index 267533a..29f3450 100644 --- a/frontend/src/pages/Dashboard.tsx +++ b/frontend/src/pages/Dashboard.tsx @@ -1,4 +1,4 @@ -import { useMemo, useEffect } from 'react'; +import { useState, useMemo, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { Responsive, WidthProvider } from 'react-grid-layout/legacy'; import { useDashboardStore } from '../store/dashboardStore'; @@ -7,7 +7,9 @@ import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/com import { Button } from "@/components/ui/button"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; -import { X } from "lucide-react"; +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; +import { Input } from "@/components/ui/input"; +import { X, Type, AlignLeft, AlignCenter, AlignRight, Bold, Italic, Underline } from "lucide-react"; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, LineChart, Line } from 'recharts'; import { VegaChart } from "@/components/VegaChart"; import 'react-grid-layout/css/styles.css'; @@ -45,9 +47,13 @@ function inferChartKeys(data: Record[]) { export function Dashboard() { const { t } = useTranslation(); - const { dashboards, activeDashboardId, removeChart, updateLayout, loadDashboards } = useDashboardStore(); + const { dashboards, activeDashboardId, removeChart, updateLayout, loadDashboards, renameDashboard, updateDashboardTitleStyle } = useDashboardStore(); const { currentProject } = useProjectStore(); + const [isEditingTitle, setIsEditingTitle] = useState(false); + const [editTitle, setEditTitle] = useState(""); + const titleInputRef = useRef(null); + useEffect(() => { if (currentProject) { loadDashboards(currentProject.id); @@ -85,6 +91,19 @@ export function Dashboard() { } }; + const handleTitleSubmit = () => { + if (activeDashboard && currentProject && editTitle.trim()) { + renameDashboard(activeDashboard.id, editTitle.trim(), currentProject.id); + } + setIsEditingTitle(false); + }; + + const handleStyleChange = (key: string, value: string) => { + if (activeDashboard && currentProject) { + updateDashboardTitleStyle(activeDashboard.id, { [key]: value }, currentProject.id); + } + }; + if (!currentProject) { return (
@@ -113,7 +132,106 @@ export function Dashboard() { return (
-

{activeDashboard.name || t('dashboardMenu')}

+
+ {isEditingTitle ? ( + setEditTitle(e.target.value)} + onBlur={handleTitleSubmit} + onKeyDown={(e) => { + if (e.key === 'Enter') handleTitleSubmit(); + if (e.key === 'Escape') setIsEditingTitle(false); + }} + className="text-2xl font-bold h-auto py-1 px-2 -ml-2 bg-transparent border-transparent hover:border-zinc-200 focus:border-indigo-500 focus:ring-indigo-500 max-w-md" + /> + ) : ( +
+

{ + setEditTitle(activeDashboard.name || t('dashboardMenu')); + setIsEditingTitle(true); + setTimeout(() => titleInputRef.current?.focus(), 0); + }} + > + {activeDashboard.name || t('dashboardMenu')} +

+ + +
+ +
+
+ +
+
+ +
+ + + + +
+
+ +
+ +
+ + + +
+
+ +
+ +
+ {['inherit', '#ef4444', '#f59e0b', '#10b981', '#3b82f6', '#8b5cf6'].map(color => ( +
+
+
+
+
+
+ )} +
string; deleteDashboard: (id: string, projectId: number) => void; renameDashboard: (id: string, newName: string, projectId: number) => void; + updateDashboardTitleStyle: (id: string, style: DashboardConfig['titleStyle'], projectId: number) => void; setActiveDashboard: (id: string | null) => void; addChart: (chart: Omit, dashboardId: string, projectId: number) => void; removeChart: (chartId: string, dashboardId: string, projectId: number) => void; @@ -133,6 +142,13 @@ export const useDashboardStore = create((set) => ({ saveDashboardsToStorage(nextDashboards, projectId); return { dashboards: nextDashboards }; }), + updateDashboardTitleStyle: (id, style, projectId) => set((state) => { + const nextDashboards = state.dashboards.map((d) => + d.id === id ? { ...d, titleStyle: { ...d.titleStyle, ...style } } : d + ); + saveDashboardsToStorage(nextDashboards, projectId); + return { dashboards: nextDashboards }; + }), setActiveDashboard: (id) => set({ activeDashboardId: id }), addChart: (chart, dashboardId, projectId) => set((state) => { const nextDashboards = state.dashboards.map((d) => {