'use client' import Tooltip from "@/shared/ui/Tooltip"; import { useCallback, useEffect, useState } from 'react' import Link from 'next/link' import dynamic from 'next/dynamic' import { Archive, ArchiveRestore, FileText, Folder, FolderInput, Globe2, Pencil, Plus, Settings2 } from 'lucide-react' import { useTranslation } from 'react-i18next' import { SettingSection, SettingsPageHeader, inputClass, subPanelClass } from '@/components/settings/shared' import { WorkspaceResourcePicker } from '@/components/workspaces/WorkspaceResourcePicker' import { WorkspaceInstructionsField } from '@/components/workspaces/WorkspaceInstructionsField' import { SystemWorkspaceSnapshot } from '@/components/workspaces/SystemWorkspaceSnapshot' import { getWorkspaceCatalog, saveWorkspace, migrateWorkspace, workspaceChatHref, inheritedWorkspaceResources, type WorkspaceCatalog, type ChatWorkspaceRegistration, } from '@/lib/workspaces-api' type RunAction = (action: () => Promise) => Promise const ServerFolderPicker = dynamic(() => import('@/components/workspaces/ServerFolderPicker').then(module => module.ServerFolderPicker), { ssr: false }) const actionClass = 'inline-flex items-center justify-center gap-1.5 rounded-lg px-2.5 py-2 text-xs text-[var(--muted-foreground)] transition-colors hover:bg-[var(--muted)] hover:text-[var(--foreground)] disabled:opacity-50' const primaryClass = 'inline-flex items-center justify-center gap-1.5 rounded-lg bg-[var(--foreground)] px-3 py-2 text-sm text-[var(--background)] disabled:opacity-50' function WorkspaceRow({ row, run, busy }: { row: ChatWorkspaceRegistration; run: RunAction; busy: boolean }) { const { t } = useTranslation() const [resourcesOpen, setResourcesOpen] = useState(false) const [instructionsOpen, setInstructionsOpen] = useState(false) const [instructions, setInstructions] = useState(row.instructions ?? '') const [resources, setResources] = useState(row.resources ?? inheritedWorkspaceResources()) const [editing, setEditing] = useState(false) const [name, setName] = useState(row.display_name) const [moving, setMoving] = useState(false) const [destination, setDestination] = useState('') const custom = row.kind === 'workspace' const Icon = row.kind === 'system' ? Settings2 : row.kind === 'general' ? Globe2 : Folder const label = row.kind === 'system' ? t('System workspace') : row.kind === 'general' ? t('Default workspace') : row.display_name return (

{label}

{row.archived && {t('Archived')}}
{row.kind !== 'system' && !row.archived && row.status === 'ready' && !busy && ( )} {row.kind !== 'system' && !row.archived && } {row.kind !== 'system' && !row.archived && } {custom && } {custom && }
{!custom &&

{row.kind === 'system' ? t('Sanitized configuration snapshots, Skill files and MCP inventory. API keys and tokens remain in private configuration.') : t('Conversations without a selected workspace share files in this folder.')}

}

{row.path}

{row.archived &&

{t('Archived. Existing conversations and files are kept.')}

} {instructionsOpen && (
{ event.preventDefault(); if (await run(() => saveWorkspace({ instructions }, row.workspace_id))) setInstructionsOpen(false) }}>
)} {resourcesOpen && (
{ event.preventDefault(); if (await run(() => saveWorkspace({ resources }, row.workspace_id))) setResourcesOpen(false) }}>

{t('Changes apply to subsequent turns. Existing conversations and files are kept.')}

)} {editing && (
{ event.preventDefault(); if (await run(() => saveWorkspace({ name: name.trim() }, row.workspace_id))) setEditing(false) }}>
)} {moving && (
{ event.preventDefault(); if (await run(() => migrateWorkspace(destination.trim(), row.workspace_id))) setMoving(false) }}>

{row.follows_root ? t('Follows root') : t('Custom location')}

{t('Copy and verify before switching. The workspace ID and conversation bindings stay unchanged; the old folder is kept as a backup.')}

)} {row.error &&

{row.error}

} {row.kind === 'system' && }
) } export default function WorkspaceSettingsSection() { const { t } = useTranslation() const [catalog, setCatalog] = useState(null) const [root, setRoot] = useState('') const [busy, setBusy] = useState(false) const [creating, setCreating] = useState(false) const [browsing, setBrowsing] = useState(false) const [resources, setResources] = useState(inheritedWorkspaceResources) const [instructions, setInstructions] = useState('') const [name, setName] = useState('') const [path, setPath] = useState('') const [error, setError] = useState('') const [notice, setNotice] = useState('') const refresh = useCallback(async () => { const next = await getWorkspaceCatalog() setCatalog(next) setRoot(next.root) setError('') }, []) useEffect(() => { void refresh().catch(err => setError(String(err.message))) }, [refresh]) const run: RunAction = async action => { setBusy(true) setError('') setNotice('') try { await action() await refresh() setNotice(t('Workspace updated.')) return true } catch (err) { setError(err instanceof Error ? err.message : String(err)) return false } finally { setBusy(false) } } return (
setCreating(!creating)} className={primaryClass}>{t('New workspace')} } /> {error &&
{error}{!catalog && }
} {(busy || notice) &&

{busy ? t('Working. Migration copies and verifies your files; please wait…') : notice}

} {!catalog ? (!error &&
) : (
{creating && (
{ event.preventDefault() if (await run(() => saveWorkspace({ name: name.trim(), resources, ...(instructions.trim() ? { instructions } : {}), ...(path.trim() ? { path: path.trim() } : {}) }))) { setName(''); setPath(''); setInstructions(''); setResources(inheritedWorkspaceResources()); setCreating(false) } }}>

{t('Place files in the folder for the agent to read. Generated files go into outputs/.')}

)} {catalog.workspaces.filter(row => row.kind === 'workspace').map(row => )} {!catalog.workspaces.some(row => row.kind === 'workspace') && !creating &&

{t('Create a workspace for a topic or project so related conversations share its files.')}

}
{catalog.workspaces.filter(row => row.kind !== 'workspace').map(row => )}
{ event.preventDefault(); await run(() => migrateWorkspace(root.trim())) }}> {root.trim() !== catalog.root &&

{t('Old folders are kept as a backup. Finish running conversations before migrating.')}

}
)} {browsing && setBrowsing(false)} onSelect={folder => { setPath(folder.path) if (!name.trim()) setName(folder.name.slice(0, 100)) }} />}
) }