diff --git a/ui/src/App.tsx b/ui/src/App.tsx index c39253d30..2b3fee036 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -20,7 +20,6 @@ const ResourcesPage = lazy(() => import('./pages/ResourcesPage')); const ResourceDetailPage = lazy(() => import('./pages/ResourceDetailPage')); const TargetsPage = lazy(() => import('./pages/TargetsPage')); const ExtrasPage = lazy(() => import('./pages/ExtrasPage')); -const SharedInstructionPage = lazy(() => import('./pages/SharedInstructionPage')); const PluginsPage = lazy(() => import('./pages/PluginsPage')); const MCPPage = lazy(() => import('./pages/MCPPage')); const SyncPage = lazy(() => import('./pages/SyncPage')); @@ -49,6 +48,12 @@ function LegacyResourceRedirect() { return ; } +/** A shared AGENTS.md had its own page; it is now selected on the Extras tab. */ +function LegacySharedInstructionRedirect() { + const { name = '' } = useParams(); + return ; +} + /** Filter Studio and the Collect page became tabs and a dialog on the target page. */ function LegacyTargetRedirect() { const { name } = useParams(); @@ -83,7 +88,7 @@ function AppRoutes() { } /> } /> } /> - } /> + } /> } /> } /> } /> diff --git a/ui/src/components/instructions/InstructionsEditorDialog.tsx b/ui/src/components/instructions/InstructionsEditorDialog.tsx index c358069e1..05f91e0b4 100644 --- a/ui/src/components/instructions/InstructionsEditorDialog.tsx +++ b/ui/src/components/instructions/InstructionsEditorDialog.tsx @@ -1,56 +1,110 @@ -import { useState } from 'react'; -import { X } from 'lucide-react'; +import { useEffect, useRef, useState } from 'react'; +import { TriangleAlert, X } from 'lucide-react'; +import AgentIcon from '../AgentIcon'; import Button from '../Button'; import CodeEditor from '../CodeEditor'; +import ConfirmDialog from '../ConfirmDialog'; import DialogShell from '../DialogShell'; import { useToast } from '../Toast'; import { useT } from '../../i18n'; import { shortenHome } from '../../lib/paths'; -import { isImportLine } from './instructionsView'; +import { formatSize, isImportLine, lineCount } from './instructionsView'; /** Edits one instruction file in place: a shared file or the project AGENTS.md. */ -export default function InstructionsEditorDialog({ title, path, content, note, onSave, onClose }: { +export default function InstructionsEditorDialog({ title, path, content, note, readers, warnings, onSave, onClose }: { title: string; path: string; content: string; - /** Shown under the editor, e.g. who else reads the file. */ + /** Shown in the side panel, e.g. who else reads the file. */ note?: string; + /** Targets that read the saved file directly. */ + readers?: string[]; + /** Problems the side panel should point out, e.g. targets that read only part of the file. */ + warnings?: string[]; onSave: (content: string) => Promise; onClose: () => void; }) { const t = useT(); const { toast } = useToast(); + const [base, setBase] = useState(content); const [draft, setDraft] = useState(content); const [saving, setSaving] = useState(false); + const [reverting, setReverting] = useState(false); + const dirty = draft !== base; + const save = async () => { + if (!dirty || saving) return; setSaving(true); try { await onSave(draft); + setBase(draft); toast(t('instructions.saved', { path: shortenHome(path) }), 'success'); - onClose(); } catch (err) { toast((err as Error).message, 'error'); + } finally { setSaving(false); } }; + const saveRef = useRef(save); + saveRef.current = save; + useEffect(() => { + const onKey = (e: KeyboardEvent) => { + if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 's') { + e.preventDefault(); + void saveRef.current(); + } + }; + window.addEventListener('keydown', onKey); + return () => window.removeEventListener('keydown', onKey); + }, []); + return ( - +
-

{title}

+

{title}

{shortenHome(path)}
- - {note &&

{note}

} +
+ + +
- - - + {t('instructions.editor.saveHint')} + +
+ setReverting(false)} + onConfirm={() => { setDraft(base); setReverting(false); }} + />
); } diff --git a/ui/src/components/instructions/SharedInstructions.tsx b/ui/src/components/instructions/SharedInstructions.tsx index 37091e1a3..9a660f9a3 100644 --- a/ui/src/components/instructions/SharedInstructions.tsx +++ b/ui/src/components/instructions/SharedInstructions.tsx @@ -1,134 +1,53 @@ -import { useMemo, useState } from 'react'; -import { Link } from 'react-router-dom'; +import { useState } from 'react'; +import type { ReactNode } from 'react'; +import { Link, useSearchParams } from 'react-router-dom'; import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { ChevronRight, Ellipsis, FilePlus, Link2, Plus, RotateCcw, Search, Upload, X } from 'lucide-react'; +import { ChevronDown, ChevronUp, Copy, Ellipsis, FilePlus, Plus, Trash2, TriangleAlert, X } from 'lucide-react'; import { api } from '../../api/client'; -import type { SharedInstructionsTarget } from '../../api/client'; +import type { SharedInstructionsFile, SharedInstructionsTarget } from '../../api/client'; import AgentIcon from '../AgentIcon'; import Button from '../Button'; import { Checkbox } from '../Checkbox'; import ConfirmDialog from '../ConfirmDialog'; import EmptyState from '../EmptyState'; -import { Select } from '../Select'; import { PageSkeleton } from '../Skeleton'; -import { SkillContextMenu, type ContextMenuItem } from '../TargetMenu'; +import { SkillContextMenu } from '../TargetMenu'; import { useToast } from '../Toast'; import { useT } from '../../i18n'; import { queryKeys } from '../../lib/queryKeys'; import { shortenHome } from '../../lib/paths'; import InstructionsEditorDialog from './InstructionsEditorDialog'; import NewSharedDialog from './NewSharedDialog'; -import { overLimit, refreshInstructions, statusTone, worstStatus } from './instructionsView'; +import { + connectExtras, connectPlan, connectedTo, formatSize, lineCount, needsSync, refreshInstructions, restorePlan, rowHint, statusTone, usesOf, +} from './instructionsView'; +import type { ConnectStep, RestoreStep, RowHint } from './instructionsView'; -const OWN = ''; -type Menu = { x: number; y: number; items: ContextMenuItem[] }; -type Pending = { title: string; message: string; confirm?: string; run: () => Promise }; -type Editing = { name: string; file: string; path: string; content: string; count: number }; +const PREVIEW_LINES = 8; +type Pending = { title: string; message: ReactNode; confirm: string; danger?: boolean; run: () => Promise }; -/** ④ Extras › Instructions (global): shared files on the left as a filter, targets on the right. */ +/** ④ Extras › AGENTS.md (global): shared files on the left, the selected file and its targets on the right. */ export default function SharedInstructions({ creating, setCreating }: { creating: boolean; setCreating: (open: boolean) => void }) { const t = useT(); - const { toast } = useToast(); const queryClient = useQueryClient(); + const [params, setParams] = useSearchParams(); const { data, error, isPending } = useQuery({ queryKey: queryKeys.instructions.shared, queryFn: () => api.listSharedInstructions() }); - const [filter, setFilter] = useState(null); // null = all, OWN = own file, else a file name - const [search, setSearch] = useState(''); - const [selected, setSelected] = useState>(new Set()); - const [menu, setMenu] = useState(null); - const [pending, setPending] = useState(null); - const [busy, setBusy] = useState(false); - const [editing, setEditing] = useState(null); - const files = useMemo(() => data?.files ?? [], [data]); - const targets = useMemo(() => data?.targets ?? [], [data]); - const uses = (tg: SharedInstructionsTarget) => tg.assigned.map((a) => a.name); - const countOf = (f: string) => targets.filter((tg) => (f === OWN ? tg.assigned.length === 0 : uses(tg).includes(f))).length; - const visible = targets.filter((tg) => - (filter === null || (filter === OWN ? tg.assigned.length === 0 : uses(tg).includes(filter))) && - (!search.trim() || tg.name.toLowerCase().includes(search.trim().toLowerCase()))); - const selectable = visible.filter((tg) => !tg.same_as); - const chosen = targets.filter((tg) => selected.has(tg.name) && !tg.same_as); - const allOn = selectable.length > 0 && selectable.every((tg) => selected.has(tg.name)); - - const assign = async (names: string[], extras: string[]) => { - setBusy(true); - try { - const res = await api.assignSharedInstructions(names, extras); - if (res.success) toast(t(extras.length ? 'instructions.shared.assigned' : 'instructions.shared.restored', { targets: names.join(', '), files: extras.join(', ') }), 'success'); - else res.errors.forEach((e) => toast(e, 'error')); - } catch (err) { - toast((err as Error).message, 'error'); - } finally { - refreshInstructions(queryClient); - setBusy(false); - } - }; - - // Going back to the own file puts the pre-attach backup in place, so ask first. - const restore = (names: string[]) => setPending({ - title: t('instructions.restore.title'), - message: t('instructions.restore.message', { targets: names.join(', ') }), - confirm: t('instructions.detail.restore'), - run: () => assign(names, []), - }); - - const resolve = (name: string, target: string, action: 'collect' | 'reapply') => setPending({ - title: t(`instructions.resolve.${action}.title`), - message: t(`instructions.resolve.${action}.message`, { name, target }), - run: async () => { - await api.resolveSharedInstructions(name, target, action); - toast(t(`instructions.resolve.${action}.done`, { name, target }), 'success'); - }, - }); - - const rowMenu = (tg: SharedInstructionsTarget): ContextMenuItem[] => [ - ...tg.assigned.filter((a) => a.status === 'modified').flatMap((a) => [ - { key: `collect-${a.name}`, label: t('instructions.resolve.collect.item', { name: a.name }), icon: , onSelect: () => resolve(a.name, tg.name, 'collect') }, - { key: `reapply-${a.name}`, label: t('instructions.resolve.reapply.item', { name: a.name }), icon: , onSelect: () => resolve(a.name, tg.name, 'reapply') }, - ]), - { key: 'restore', label: t('instructions.shared.restoreOwn'), icon: , onSelect: () => restore([tg.name]) }, - ]; - - const openEditor = async (f: { name: string; file: string; path: string }) => { - try { - const res = await api.getSharedInstructionsContent(f.name); - setEditing({ ...f, content: res.content, count: countOf(f.name) }); - } catch (err) { - toast((err as Error).message, 'error'); - } - }; - - const openMenu = (e: React.MouseEvent, items: ContextMenuItem[]) => { - const r = e.currentTarget.getBoundingClientRect(); - setMenu({ x: r.left, y: r.bottom + 4, items }); - }; + const pick = (name: string | null) => setParams((prev) => { + const next = new URLSearchParams(prev); + if (name) next.set('file', name); + else next.delete('file'); + return next; + }, { replace: true }); if (isPending) return ; if (error) return
{error.message}
; - - const ownLabel = t('instructions.shared.own'); - const options = files.map((f) => ({ value: f.name, label: f.name })); - const current = files.find((f) => f.name === filter); - // Same rule as the file's own page: import targets can take one more file, - // link targets only when they are not on another shared file yet. - const addable = current ? targets.filter((tg) => !tg.same_as && !uses(tg).includes(current.name) && (tg.import || tg.assigned.length === 0)) : []; - const filterRow = (key: string | null, label: string, count: number, mono: boolean) => ( -
- - {count} - {mono && key && ( - - - - )} -
- ); + const { files, targets } = data; + const current = files.find((f) => f.name === params.get('file')) ?? files[0]; return ( <> - {files.length === 0 ? ( + {!current ? ( setCreating(true)}>{t('instructions.shared.new')}} /> ) : ( -
- - -
-
- - - setSearch(e.target.value)} placeholder={t('instructions.shared.search')} aria-label={t('instructions.shared.search')} /> - - {t(visible.length === 1 ? 'instructions.shared.count.one' : 'instructions.shared.count.other', { count: visible.length })} - - global -
- {current && ( -
- - {current.name} - {shortenHome(current.path)} - - {addable.length > 0 && ( - (next.length ? void assign([tg.name], next) : restore([tg.name]))} options={options} disabled={busy || Boolean(tg.same_as)} /> - ) : ( - ({ value: tg.name, label: tg.name, icon: }))]} - onChange={(v) => { const tg = addable.find((x) => x.name === v); if (tg) void add(tg); }} disabled={busy} /> - )} -
-
-
- - {t('instructions.shared.colTarget')} - {t('instructions.detail.colWrites')} - mode - {t('instructions.project.colStatus')} - -
- {using.length === 0 &&
{t('instructions.detail.unused')}
} - {using.map((tg) => { - const a = tg.assigned.find((x) => x.name === name) as InstructionsAssignment; - const base = tg.path.split('/').pop() ?? ''; - const dir = shortenHome(tg.path).slice(0, -base.length); - const hint = tg.same_as ? t('instructions.shared.sameAs', { name: tg.same_as }) - : a.status === 'modified' ? t('instructions.detail.how.modified') - : t(`instructions.detail.how.${a.mode === 'import' ? 'import' : 'link'}`, { path: `…/${name}/${file.file}` }); - return ( -
-
- - {tg.name} - - {dir}{base} - {hint} - - {a.mode} - {a.status} - - {!tg.same_as && ( - - )} - -
- {a.status === 'modified' && !tg.same_as && ( -
-
- - - {t('instructions.detail.modified', { path: shortenHome(tg.path) })} - - - - - {t('instructions.detail.modifiedHint', { name })} - -
-
- )} -
- ); - })} -
- {using.flatMap((tg) => overLimit(tg, [file]).map((f) => ( -

{t('instructions.shared.tooLong', { name: f.name, chars: f.chars.toLocaleString(), max: tg.max_chars?.toLocaleString() ?? '' })}

- )))} -

{t('instructions.detail.restoreHint', { name })}

-
- - {editing && content.data && ( - { - await api.putSharedInstructionsContent(name, next); - refreshInstructions(queryClient); - }} - onClose={() => setEditing(false)} - /> - )} - setPending(null)} - onConfirm={() => void confirmRun()} - /> -
- ); -}