Drafts in every chat, factory chat overlay, factory main switch
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58475ce65e
commit
171b87b766
48 files changed
+754
-10
No files matched your search
@@ -543,6 +543,57 @@ function OlderLoader({ sessionId, afterSummary, scrollRef }: { sessionId: string
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the unsent message of a chat on the server, so that it survives a reload and can be continued
|
||||
* on another device: it is saved shortly after typing, loaded when the chat opens and again when the
|
||||
* window comes back – as long as this device has no unsaved edits of its own.
|
||||
*/
|
||||
function useDraftSync(sessionId: string, text: string, setText: (s: string) => void, readOnly: boolean) {
|
||||
const synced = useRef<string | null>(null);
|
||||
const current = useRef(text);
|
||||
current.current = text;
|
||||
const pull = (initial: boolean) => {
|
||||
if (readOnly) return;
|
||||
get<{ text: string }>(`/sessions/${sessionId}/composer-draft`)
|
||||
.then((d) => {
|
||||
// Only take over when nothing was typed here since the last sync (on opening: when the field is empty).
|
||||
const untouched = initial ? current.current === "" : current.current === (synced.current ?? "");
|
||||
if (synced.current === null) synced.current = initial && current.current !== "" ? "" : d.text;
|
||||
if (untouched && d.text !== current.current) {
|
||||
synced.current = d.text;
|
||||
setText(d.text);
|
||||
} else if (initial && current.current === "") synced.current = d.text;
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
useEffect(() => {
|
||||
synced.current = null;
|
||||
pull(true);
|
||||
const again = () => document.visibilityState === "visible" && pull(false);
|
||||
document.addEventListener("visibilitychange", again);
|
||||
window.addEventListener("focus", again);
|
||||
const h = setInterval(again, 10000);
|
||||
return () => {
|
||||
document.removeEventListener("visibilitychange", again);
|
||||
window.removeEventListener("focus", again);
|
||||
clearInterval(h);
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [sessionId, readOnly]);
|
||||
useEffect(() => {
|
||||
if (readOnly || synced.current === null || text === synced.current) return;
|
||||
const h = setTimeout(() => {
|
||||
const value = text;
|
||||
put(`/sessions/${sessionId}/composer-draft`, { text: value })
|
||||
.then(() => {
|
||||
synced.current = value;
|
||||
})
|
||||
.catch(() => {});
|
||||
}, 700);
|
||||
return () => clearTimeout(h);
|
||||
}, [text, sessionId, readOnly]);
|
||||
}
|
||||
|
||||
export function ChatView({ sessionId, onMenu, onClosePane }: { sessionId: string; onMenu: () => void; onClosePane?: () => void }) {
|
||||
const t = useT();
|
||||
const session = useStore((s) => s.sessionById[sessionId]);
|
||||
@@ -572,6 +623,7 @@ export function ChatView({ sessionId, onMenu, onClosePane }: { sessionId: string
|
||||
const models = useModels();
|
||||
const [loadError, setLoadError] = useState<string>();
|
||||
const [text, setText] = useState(() => takePrefill(sessionId) ?? "");
|
||||
useDraftSync(sessionId, text, setText, readOnly);
|
||||
const [stats, setStats] = useState<SessionStats>();
|
||||
const [showStats, setShowStats] = useState(false);
|
||||
const [editingTitle, setEditingTitle] = useState(false);
|
||||
|
||||
@@ -7,6 +7,7 @@ import { Icon } from "./Icon";
|
||||
import { Spinner } from "./Activity";
|
||||
import { ModelSelect } from "./ModelSelect";
|
||||
import { RecreateDialog } from "./FactoryDraft";
|
||||
import { ChatOverlay, openFactoryChat } from "./FactoryChat";
|
||||
import { ReleaseDialog, ReleasesPanel, type ReleaseRow } from "./FactoryRelease";
|
||||
|
||||
interface PrInfo {
|
||||
@@ -76,6 +77,9 @@ interface Issue {
|
||||
pr_url: string;
|
||||
branch: string;
|
||||
rounds: number;
|
||||
/** Newest phase chat that still exists. */
|
||||
chat_session?: string;
|
||||
chat_phase?: Phase;
|
||||
created_at: number;
|
||||
updated_at: number;
|
||||
}
|
||||
@@ -114,8 +118,15 @@ interface RepoInfo {
|
||||
running: number;
|
||||
version?: string | null;
|
||||
}
|
||||
interface Power {
|
||||
on: boolean;
|
||||
/** Agents that are working right now. */
|
||||
active: number;
|
||||
state: "running" | "stopping" | "stopped";
|
||||
}
|
||||
interface Overview {
|
||||
area: boolean;
|
||||
power?: Power;
|
||||
repos: RepoInfo[];
|
||||
issues: Issue[];
|
||||
runs: Run[];
|
||||
@@ -147,7 +158,7 @@ function fmtDuration(ms: number) {
|
||||
const runMs = (r: Run, now: number) => (r.ended_at ?? now) - r.started_at;
|
||||
|
||||
/** Software factory: spec issues in Gitea are reviewed, built and accepted by agents with a model per phase. */
|
||||
export function Factory({ onMenu }: { onMenu: () => void }) {
|
||||
export function Factory({ onMenu, overlayId }: { onMenu: () => void; overlayId?: string }) {
|
||||
const t = useT();
|
||||
const [tab, setTab] = useState<"overview" | "settings">("overview");
|
||||
return (
|
||||
@@ -168,14 +179,33 @@ export function Factory({ onMenu }: { onMenu: () => void }) {
|
||||
</div>
|
||||
</div>
|
||||
<div className="page">
|
||||
<div className="page-inner" style={{ maxWidth: 1280 }}>
|
||||
<div className="page-inner" style={{ maxWidth: tab === "overview" ? "none" : 1280 }}>
|
||||
{tab === "overview" ? <FactoryOverview onSettings={() => setTab("settings")} /> : <FactorySettings />}
|
||||
</div>
|
||||
</div>
|
||||
{overlayId && <ChatOverlay id={overlayId} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** The factory's main switch, coloured by state: green = running, yellow = stopping, red = stopped. */
|
||||
function PowerSwitch({ power, onToggle }: { power: Power; onToggle: (on: boolean) => void }) {
|
||||
const t = useT();
|
||||
const label = power.state === "running" ? t("factory.power.running") : power.state === "stopping" ? t("factory.power.stopping", { n: power.active }) : t("factory.power.stopped");
|
||||
const hint = power.on ? t("factory.power.stopHint") : t("factory.power.startHint");
|
||||
return (
|
||||
<button type="button" role="switch" aria-checked={power.on} className={`factory-power ${power.state}`} onClick={() => onToggle(!power.on)} title={hint}>
|
||||
<span className="factory-power-track">
|
||||
<span className="factory-power-knob" />
|
||||
</span>
|
||||
<span className="factory-power-text">
|
||||
<b>{label}</b>
|
||||
<span className="hint">{hint}</span>
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
function FactoryOverview({ onSettings }: { onSettings: () => void }) {
|
||||
const t = useT();
|
||||
const [data, setData] = useState<Overview>();
|
||||
@@ -417,10 +447,21 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) {
|
||||
}
|
||||
};
|
||||
const phaseRunning = !!selected && ["review", "building", "testing"].includes(selected.state);
|
||||
/** Master switch: a soft stop – no new agent starts, running ones finish their phase. */
|
||||
const setPower = async (on: boolean) => {
|
||||
try {
|
||||
const power = await post<Power>("/factory/power", { on });
|
||||
setData((d) => (d ? { ...d, power } : d));
|
||||
void reload();
|
||||
} catch (e: any) {
|
||||
toast(e.message);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{!data.area && <div className="notice err">{t("factory.noArea")}</div>}
|
||||
{data.power && <PowerSwitch power={data.power} onToggle={(on) => void setPower(on)} />}
|
||||
<div className="kpis">
|
||||
<div className="kpi">
|
||||
<div className="k-label">{t("factory.kpi.running")}</div>
|
||||
@@ -539,8 +580,32 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) {
|
||||
const tokens = rs.reduce((a, r) => a + r.input_tokens + r.output_tokens, 0);
|
||||
return (
|
||||
<button key={`${i.repo}#${i.number}`} data-state={i.state} className={`factory-card ${sel === `${i.repo}#${i.number}` ? "active" : ""} ${active ? "running" : ""}`} onClick={() => setSel(sel === `${i.repo}#${i.number}` ? "" : `${i.repo}#${i.number}`)}>
|
||||
<span className="hint">
|
||||
{repoName(i.repo)} #{i.number}
|
||||
<span className="factory-card-head">
|
||||
<span className="hint">
|
||||
{repoName(i.repo)} #{i.number}
|
||||
</span>
|
||||
{i.chat_session && (
|
||||
<span
|
||||
role="link"
|
||||
tabIndex={0}
|
||||
className="factory-open"
|
||||
title={`${t("factory.openChat")}: ${t(`factory.phase.${i.chat_phase ?? "review"}` as any)}`}
|
||||
aria-label={t("factory.openChat")}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
openFactoryChat(i.chat_session!);
|
||||
}}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
openFactoryChat(i.chat_session!);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Icon name="eye" />
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
<b>{i.title || `#${i.number}`}</b>
|
||||
{latest && (
|
||||
@@ -677,7 +742,7 @@ function FactoryOverview({ onSettings }: { onSettings: () => void }) {
|
||||
</td>
|
||||
<td>{fmtCost(r.cost, "USD")}</td>
|
||||
<td>
|
||||
<button className="small" onClick={() => navigate(`/s/${r.session_id}`)}>
|
||||
<button className="small" onClick={() => openFactoryChat(r.session_id)}>
|
||||
<Icon name="eye" /> {t("factory.openChat")}
|
||||
</button>
|
||||
</td>
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { useEffect } from "react";
|
||||
import { navigate } from "../router";
|
||||
import { useT } from "../i18n";
|
||||
import { ChatView } from "./ChatView";
|
||||
import { Icon } from "./Icon";
|
||||
|
||||
/** Desktop only: chats of the factory open over the board and are closed with one click. */
|
||||
export const isDesktop = () => window.matchMedia("(min-width: 801px)").matches;
|
||||
|
||||
/** Open the chat of a phase: in the overlay on the desktop, as a page on small screens. */
|
||||
export function openFactoryChat(sessionId: string) {
|
||||
navigate(isDesktop() ? `/factory/s/${sessionId}` : `/s/${sessionId}`);
|
||||
}
|
||||
|
||||
export function ChatOverlay({ id }: { id: string }) {
|
||||
const t = useT();
|
||||
const close = () => navigate("/factory");
|
||||
useEffect(() => {
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
// Esc closes – unless it is meant for a field, a dialog or the image viewer on top.
|
||||
const el = document.activeElement;
|
||||
const typing = el instanceof HTMLTextAreaElement || el instanceof HTMLInputElement || el instanceof HTMLSelectElement;
|
||||
if (e.key === "Escape" && !typing && !document.querySelector(".modal-backdrop, .viewer-overlay")) close();
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, []);
|
||||
return (
|
||||
<div className="chat-overlay" onMouseDown={(e) => e.target === e.currentTarget && close()}>
|
||||
<div className="chat-overlay-box" role="dialog" aria-modal="true">
|
||||
<div className="chat-overlay-bar">
|
||||
<span className="hint">{t("factory.chatOverlay")}</span>
|
||||
<span className="spacer" />
|
||||
<button className="small" onClick={() => navigate(`/s/${id}`)} title={t("factory.openFullHint")}>
|
||||
<Icon name="external" /> {t("factory.openFull")}
|
||||
</button>
|
||||
<button className="small" onClick={close} aria-label={t("common.close")}>
|
||||
<Icon name="close" /> {t("common.close")}
|
||||
</button>
|
||||
</div>
|
||||
<div className="chat-overlay-body">
|
||||
<ChatView key={id} sessionId={id} onMenu={close} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user