A local-first multi-agent app development platform, built on PenguinHarness
/**
* Draft view (/chat/new): the pre-persistence form of a new
* conversation, before any Session exists. The input card sits vertically centered;
* before sending, this is where Agent / Workspace / approval mode / Model are all
* chosen in one place — two small dropdown pills sit right below the card (pill
* buttons, styled after ChatGPT's project picker): Agent selection and Workspace
* directory selection (the menu browses server-side directories, and the current
* path can be edited directly); the model picker lives in the input card's bottom
* toolbar, left of the send button (with a vendor logo). The Session is only
* created when **the first message is sent**; once created, Agent / Workspace /
* Model are locked in via meta, and only approval mode remains editable (in the
* session-mode input area).
*
* Draft auto-cache (storage and validation in draft-cache.ts; keys are isolated by
* "user × Project", #68): the four selections are saved as soon as they change;
* body text is keystroke-frequent and deferred/coalesced (if there's an unsaved
* change before unmount, one final write is flushed) — closing and returning to
* the page resumes where you left off; on successful send the cache clears, except
* the model selection, which carries over as the next conversation's default
* (switch-becomes-default, mirroring the thinking level persisting on the Agent).
* The one thing that does not resume is a prompt a "Create with AI" surface composed
* (`aiPrefill`, draft-cache.ts): nobody typed it, so leaving this page without editing
* or sending it clears the slot exactly as a send would (see dropAiPrefill).
* Every "New chat" entry point first rewrites the slot to the model carry-over and staged
* skills, releasing whatever else an earlier visit left in the cache (prepareNewChatDraft,
* new-chat.ts), then names in route state only what it is about: an Agent group's "+" or
* an Agent card names its Agent, a Workspace group's "+" its path ("" = temporary
* workspace), and the plain "New chat" names nothing. A direct visit or refresh keeps the
* cache. Precedence per field: route state > draft cache > the Project's new-chat defaults
* ([default_chat]) > built-in fallback (for the Agent: default_agent, then the first —
* newChatAgentId); the model default already flows through models.defaultModel.
*
* Saving the Project's new-chat defaults resets the seeded selections so new chats pick
* the change up: the project-settings dialog strips the cached pins (next visits reseed
* from the fresh defaults) and dispatches a same-tab chat-defaults-changed event that a
* MOUNTED draft answers by resetting Agent / Workspace / approval mode / Model in
* component state (see onDefaultsChanged) — typed-but-unsent text and staged skills
* always survive.
*/
import { useCallback, useEffect, useRef, useState } from "react";
import type { RefObject } from "react";
import { useLocation, useNavigate } from "react-router";
import type {
AgentModelConfigDto,
AgentSummary,
ApprovalMode,
ChatDefaultsDto,
ModelRefDto,
ModelsResponse,
SessionCreateRequest,
SessionSandbox,
SkillMetadataItem,
TaskInputPart,
} from "@adelie/server/api";
import { AgentAvatar, Chevron, Dropdown, MenuItem, AppLogo } from "@adelie/ui";
import * as api from "../../api/endpoints";
import { S } from "../../lib/strings";
import { UNCONFINED, draftSandboxAfter } from "../../lib/permission-level";
import type { PermissionPick } from "../../lib/permission-level";
import { formatMonthDay } from "../../lib/format";
import { sendRefusalReason, sendRefused } from "./first-send-refusal";
import { thinkingLevelRefused } from "./thinking-level-refusal";
import { rememberSessionMachine } from "../../lib/session-machines";
import { cachedMachineAgents, rememberMachineAgents } from "../../lib/machine-cache";
import { useAuth } from "../../state/auth";
import { useLocale } from "../../state/locale";
import { agentDisplayName, useProject } from "../../state/project";
import { useSessions } from "../../state/sessions";
import { useVersionInfo } from "../../lib/use-version-info";
import { versionBadgeFor } from "../../lib/update-flow";
import { openUpdateModal, useUpdateFlow } from "../../lib/use-update-flow";
import { ChatInput } from "./chat-input";
import type { ComposerControl } from "./chat-input";
import { APPROVAL_MODES } from "./approval-mode";
import { adoptDockScope, currentDockScope } from "../dock/dock-state";
import { setDockCwd } from "../dock/dock-terminal";
import { ShortcutsFolder } from "./shortcuts-folder";
import { clearDraft, draftKey, loadDraft, saveDraft } from "./draft-cache";
import type { DraftCache } from "./draft-cache";
import {
DRAFT_FLUSH_EVENT,
getDraftSession,
removeDraftSession,
saveDraftSession,
} from "./draft-sessions";
import {
CHAT_DEFAULTS_CHANGED_EVENT,
chatDefaultsChangedDetail,
type ChatDefaultsChangedDetail,
} from "./chat-defaults-event";
import { newChatAgentId } from "./new-chat";
import { onPluginConfigSaved } from "../../lib/plugin-config-event";
import { effectiveThinkingLevel } from "./thinking-level";
import { WorkspaceSelect, pillClass } from "./workspace-select";
import { FilesPanelToggle } from "./dock-toggles";
import { sameModelRef } from "../models/model-grouping";
import { toneInk } from "../../lib/tone";
/** Coalescing window for writing body text to the cache: keystrokes are frequent, so a short batch accumulates before persisting (option changes are still written immediately). */
const DRAFT_SAVE_DEBOUNCE_MS = 300;
/**
* "Applied" markers for the route-state overrides (one slot per field, holding the last
* consumed location.key). React Router persists location.state AND location.key in
* history.state, which survives a full page reload, while a ref resets with the JS
* context — with a ref alone, a reload would re-apply the override and clobber whatever
* the user changed since (restored from the draft cache). sessionStorage is per-tab
* exactly like history.state, so the marker follows the history entry; on storage
* failure (private mode) both helpers degrade to "not consumed", and the in-component
* ref still provides the previous apply-once-per-mount behavior.
*/
type RouteStateField = "agentId" | "workspace";
function loadAppliedRouteKey(field: RouteStateField): string | null {
try {
return sessionStorage.getItem(`penguin.chatRouteApplied.${field}`);
} catch {
return null;
}
}
function saveAppliedRouteKey(field: RouteStateField, key: string): void {
try {
sessionStorage.setItem(`penguin.chatRouteApplied.${field}`, key);
} catch {
/* best-effort: the dedup marker falls back to the per-mount ref */
}
}
export function DraftView({
projectId,
models,
draftId,
composerRef: pageComposerRef,
onWorkspaceChange,
}: {
projectId: string;
/** Project model config (already fetched by ChatPage): candidate list and default model. */
models: ModelsResponse | null;
/** Parked draft conversation id (`/chat/draft-…` — see draft-sessions.ts); absent = the ordinary active draft (`/chat/new`). */
draftId?: string;
/**
* The page's handle on the composer, so what the dock's panels hand the conversation (the
* Files panel's references) reaches this draft's composer as it reaches a live Session's.
*/
composerRef?: RefObject<ComposerControl | null>;
/** The Workspace picked here ("" = a temporary one) and its machine, for the Files panel. */
onWorkspaceChange?: (path: string, machineId: string | null) => void;
}) {
const navigate = useNavigate();
const location = useLocation();
const { agents: localAgents, setCurrentAgentId } = useProject();
/**
* Agents on the machine the workspace is on. They are per-server: a Session created on
* another machine can only name an Agent that exists THERE, so offering this machine's
* list would offer choices that cannot be made. Empty while loading, which the validation
* below already treats as "not ready" rather than "none".
*/
const [remoteAgents, setRemoteAgents] = useState<AgentSummary[]>([]);
/** Why the remote list may be empty, for the composer's empty row: still asking, or beyond reach. */
const [remoteAgentsState, setRemoteAgentsState] = useState<"loading" | "unreachable">("loading");
const { add } = useSessions();
// The draft key includes a user dimension (#68 cross-account leakage). RequireAuth
// guarantees the user is logged in here; on the off chance there's no user (the
// type allows null), it's better to disable caching entirely than to read/write a
// key that isn't account-scoped.
const userId = useAuth().user?.userId ?? null;
// The cache is read only once, on mount: the component remounts keyed by Project (and
// by parked-draft id), so switching Projects or parked drafts automatically switches to
// the corresponding content; switching accounts always goes through logout (clearing
// the user unmounts the whole route tree), so logging back in is likewise a fresh
// mount. A parked draft reads its own entry instead of the active slot.
const [parkedMissing] = useState(
() =>
draftId !== undefined && (!userId || getDraftSession(userId, projectId, draftId) === null),
);
const [cached] = useState<DraftCache>(() => {
if (!userId) return {};
if (draftId !== undefined) return getDraftSession(userId, projectId, draftId)?.draft ?? {};
return loadDraft(draftKey(userId, projectId));
});
// A parked id that no longer exists (deleted in the sidebar, stale bookmark): fall
// back to the plain new-chat draft instead of editing into a void.
useEffect(() => {
if (parkedMissing) navigate("/chat/new", { replace: true });
}, [parkedMissing, navigate]);
// Null until something names an Agent: the cache here, otherwise the resolution effect below
// once the Agent list and the Project's defaults are in — never a stand-in it would swap out.
const [agentId, setAgentId] = useState<string | null>(cached.agentId ?? null);
const [workspace, setWorkspace] = useState(cached.workspace ?? "");
/**
* The machine that workspace is on (null = this one). Carried beside the path because a
* path alone does not identify a directory: `/srv/app` exists on many machines and means
* a different one on each.
*/
const [workspaceMachine, setWorkspaceMachine] = useState<string | null>(cached.machineId ?? null);
/** The Agents actually offerable for this draft: the target machine's, or this one's. */
const agents = workspaceMachine === null ? localAgents : remoteAgents;
// A terminal opened while drafting starts in the Workspace chosen here; "" is the
// temporary Workspace, whose directory the server only creates with the Session, so
// that case falls back to home (setDockCwd's null).
useEffect(() => {
setDockCwd(workspace || null, workspaceMachine);
onWorkspaceChange?.(workspace, workspaceMachine);
}, [workspace, workspaceMachine, onWorkspaceChange]);
const [approvalMode, setApprovalMode] = useState<ApprovalMode>(
cached.approvalMode ?? "allow-all",
);
// Only what the person picked: the rest follows the server's sandbox settings, which the
// chat-defaults response carries, so the button shows what the Session would start with.
const [sandboxPick, setSandboxPick] = useState<Partial<SessionSandbox>>(cached.sandbox ?? {});
const [modelRef, setModelRef] = useState<ModelRefDto | null>(cached.modelRef ?? null);
const textRef = useRef(cached.text ?? "");
/**
* The text came from a "Create with AI" surface rather than a keyboard (`aiPrefill`,
* draft-cache.ts): it seeded this draft and dies with it. The mark rides in the cache so a
* reload restores the same prefilled draft, and it is dropped by the first edit — from then on
* the text is the user's and is cached, parked and kept exactly like any other draft.
*/
const aiPrefillRef = useRef(cached.aiPrefill === true);
/**
* Selected skills (prefilled by "quick invoke" from the Skills page + checked in
* the input area): passed to ChatInput as the initial selection via initialSkills
* on mount, then written back through onSkillsChange and persisted immediately
* (discrete clicks) — survives a refresh; cleared along with the whole draft on
* successful send, kept on failure so it can be resent.
*/
const skillsRef = useRef<string[]>(cached.skills ?? []);
// —— Project new-chat defaults ([default_chat]) ——
// Fetched once per Project mount (fail-soft: an error reads as "no defaults", so the
// draft keeps working). They prefill the seams below with the precedence
// route location.state > mount-time draft cache > project default > built-in fallback;
// null = still loading (the thinking picker below stays disabled until resolved).
const [chatDefaults, setChatDefaults] = useState<ChatDefaultsDto | null>(null);
/**
* Set once the chat-defaults-changed event delivered a fresh block (see the reseed
* handler below): from then on the mount-time fetch must not apply — it was started
* earlier and would overwrite the fresher event payload when it resolves.
*/
const defaultsFromEventRef = useRef(false);
useEffect(() => {
let cancelled = false;
api
.getChatDefaults(projectId)
.then((res) => {
if (!cancelled && !defaultsFromEventRef.current) setChatDefaults(res);
})
.catch(() => {
if (!cancelled && !defaultsFromEventRef.current) setChatDefaults({});
});
return () => {
cancelled = true;
};
}, [projectId]);
/**
* Fields the user already touched this mount: the project defaults arrive async (after
* mount), and an explicit pick made in the meantime must never be clobbered by them.
* The mount-time cache (`cached`) covers everything picked in PREVIOUS visits; these
* refs cover the window between mount and the defaults resolving. The Agent needs no flag:
* the resolution below never replaces a valid selection.
*/
const touchedRef = useRef({ workspace: false, approval: false });
// Unified resolution of the Agent selection (a single effect, single writer):
// explicit route state > current valid value (from cache / panel selection) > the
// new-chat default Agent (newChatAgentId: the Project's `[default_chat].agent_id`, then
// default_agent, then the first one), which waits for the Project's defaults to resolve.
// Explicit intent (an Agent group's "+", an Agent card) is applied only once per
// location.key — clicking "+" again for the same Agent gets a new key and re-aligns, while
// the user's subsequent reselection in the panel won't keep getting overridden. Merging
// this into one effect is essential: splitting it into an "apply state" effect and a
// "fallback on invalid value" effect would let the former write B in one render while the
// latter, still judging by the stale closure's invalid value, writes the default Agent and
// clobbers B.
const routeState = location.state as {
agentId?: string;
workspace?: string;
machineId?: string;
} | null;
const stateAgentId = routeState?.agentId;
// Read here rather than beside their own effect below: the Agent resolution waits on them
// (see `routeMachineSettled`), and a dependency array is evaluated during the render that
// declares it.
const stateWorkspace = routeState?.workspace;
const stateMachineId = routeState?.machineId;
const appliedStateKey = useRef<string | null>(null);
useEffect(() => {
if (agents.length === 0) return; // list not ready yet, nothing to validate against — wait for the next pass
const valid = (id: string | null | undefined): id is string =>
!!id && agents.some((a) => a.agentId === id);
// The route may name an Agent that only exists on the machine it names alongside it (the
// Agents page offers a new chat on a machine's own Agent). That machine is applied by the
// Workspace effect below, one render later, and its Agents are fetched after that — so
// this pass would validate the id against THIS server's list, reject it, and spend the
// one-shot key on the fallback Agent, which is then what the Session is created with.
// Nothing is decided until the list being validated against is the one the route asked
// for. A machine named without a path is never applied at all (the machine travels with
// the path), so it cannot hold this up.
const routeMachineSettled =
stateMachineId === undefined ||
stateWorkspace === undefined ||
workspaceMachine === stateMachineId;
if (
stateAgentId &&
appliedStateKey.current !== location.key &&
loadAppliedRouteKey("agentId") !== location.key
) {
if (!routeMachineSettled) return;
appliedStateKey.current = location.key;
saveAppliedRouteKey("agentId", location.key);
if (valid(stateAgentId)) {
setAgentId(stateAgentId);
return;
}
}
if (valid(agentId)) return;
// Nothing names a valid Agent (no route override, no cached or picked one, or the one
// there was is gone): the new-chat default, once the Project's defaults are known.
if (chatDefaults === null) return;
setAgentId(newChatAgentId(agents, chatDefaults));
}, [
agents,
agentId,
location.key,
stateAgentId,
stateMachineId,
stateWorkspace,
workspaceMachine,
chatDefaults,
]);
// Explicit Workspace from route state (the workspace-mode group header "+"): applied once per
// location.key, same convention as the Agent above, overriding the cached selection ("" pre-fills
// the temporary workspace). Unlike the Agent there's no list to validate against, so this is a
// separate effect that never has to wait for a load.
const appliedWorkspaceKey = useRef<string | null>(null);
useEffect(() => {
if (
stateWorkspace === undefined ||
appliedWorkspaceKey.current === location.key ||
loadAppliedRouteKey("workspace") === location.key
) {
return;
}
appliedWorkspaceKey.current = location.key;
saveAppliedRouteKey("workspace", location.key);
setWorkspace(stateWorkspace);
// Set together with the path, and to null when the route names none: a machine left over
// from a cached draft would send this Session to a machine the chosen path is not on.
setWorkspaceMachine(stateMachineId ?? null);
}, [location.key, stateWorkspace, stateMachineId]);
// Project defaults for Workspace / approval mode: the same apply-once discipline as the
// route-state effects above, deferred until the defaults resolve. A field is only seeded
// when nothing with higher precedence claims it — no route override (workspace only), no
// mount-time cached value (a cached "" workspace counts: it is an explicit temporary workspace),
// and no user edit since mount. Model is deliberately not here (models.defaultModel
// already flows through its own fallback effect below — the single-sourced default).
const appliedProjectDefaults = useRef(false);
useEffect(() => {
if (chatDefaults === null || appliedProjectDefaults.current) return;
appliedProjectDefaults.current = true;
if (
chatDefaults.workspace !== undefined &&
stateWorkspace === undefined &&
cached.workspace === undefined &&
!touchedRef.current.workspace
) {
setWorkspace(chatDefaults.workspace);
}
// The Project's own default wins; else the Sandbox card's default preset, while it is on.
const seeded = chatDefaults.approvalMode ?? chatDefaults.sandbox?.defaultApprovalMode;
if (seeded !== undefined && cached.approvalMode === undefined && !touchedRef.current.approval) {
setApprovalMode(seeded);
}
}, [chatDefaults, stateWorkspace, cached.workspace, cached.approvalMode]);
// Model fallback: once config is ready, if nothing is selected or the selection is no longer valid, fall back to the project default → the first model (always as a paired reference).
useEffect(() => {
if (!models) return;
if (modelRef && models.models.some((m) => sameModelRef(m, modelRef))) return;
const first = models.models[0];
setModelRef(
models.defaultModel ?? (first ? { provider: first.provider, modelId: first.modelId } : null),
);
}, [models, modelRef]);
/**
* Live reseed: the project-settings dialog saved new defaults in THIS tab while the
* draft is mounted. The dialog already stripped the cached pins, but this component's
* state still holds the old selections and persistNow would silently write them right
* back over the stripped cache — so the seeded fields are reset here to exactly what a
* fresh /chat/new mount would now produce (with the cache stripped, the seeding
* precedence collapses to: fresh project default > built-in fallback); the persist
* effect then pins the NEW values. Typed text and staged skills are user content and
* stay untouched; route-state overrides and in-mount picks are superseded — the save is
* the later explicit intent, and the next fresh mount would drop them anyway. Values
* come from the event payload (server-confirmed by the dialog's PUTs), not a refetch.
* The mount-time seeding effects re-run when chatDefaults changes but cannot fight
* this: their apply-once refs are already consumed, and where they are not, they
* re-apply the same fresh values.
*/
const onDefaultsChanged = useCallback(
(detail: ChatDefaultsChangedDetail) => {
if (detail.defaults) {
const d = detail.defaults;
defaultsFromEventRef.current = true;
setChatDefaults(d);
touchedRef.current = { workspace: false, approval: false };
setWorkspace(d.workspace ?? "");
setApprovalMode(d.approvalMode ?? d.sandbox?.defaultApprovalMode ?? "allow-all");
// The Agent a fresh mount would now start on (the new block's default while it names
// an Agent, then default_agent, then the first).
setAgentId(newChatAgentId(agents, d));
}
// New default model: adopt it directly (the event carries the authoritative pair).
// Setting null and leaning on the fallback effect would race ChatPage's models
// refetch and re-pin the STALE default from the old models prop.
if (detail.defaultModel !== undefined) setModelRef(detail.defaultModel);
},
[agents],
);
/** Latest-closure mirror for the window listener (same convention as persistRef). */
const onDefaultsChangedRef = useRef(onDefaultsChanged);
onDefaultsChangedRef.current = onDefaultsChanged;
// The Sandbox card was saved (from the composer's More…, say): the permission menu reads the
// sandbox view again, and an approval mode nobody picked follows the new default preset.
useEffect(
() =>
onPluginConfigSaved("sandbox", () => {
void api.getChatDefaults(projectId).then(
(res) => {
setChatDefaults((prev) => ({
...(prev ?? {}),
...(res.sandbox !== undefined ? { sandbox: res.sandbox } : {}),
}));
const seeded = res.approvalMode ?? res.sandbox?.defaultApprovalMode;
if (seeded !== undefined && !touchedRef.current.approval) setApprovalMode(seeded);
},
() => undefined,
);
}),
[projectId],
);
useEffect(() => {
const onEvent = (e: Event) => {
const detail = chatDefaultsChangedDetail(e, projectId);
if (detail) onDefaultsChangedRef.current(detail);
};
window.addEventListener(CHAT_DEFAULTS_CHANGED_EVENT, onEvent);
return () => window.removeEventListener(CHAT_DEFAULTS_CHANGED_EVENT, onEvent);
}, [projectId]);
// —— Conversation-time thinking level (backed by the Agent settings) ——
// The picker DISPLAYS the effective level, resolved by the same chain core applies when
// the Session is created (core agent.ts `configuredThinkingLevel`): the Agent's explicit
// `model.thinking_level` > the Project's `default_chat.thinking_level` > the built-in
// "medium" (see effectiveThinkingLevel). `agentThinkingLevel` keeps the raw agent value
// ("" = no explicit override); the derived value below waits for BOTH fetches. Picking a
// level immediately persists it via the agent-config API (the PUT carries only that key —
// the server merges per-key into the YAML, so nothing else is clobbered): the session
// created on first send reads systemConfig fresh, so it runs with the picked level, which
// also becomes the Agent's new default — the project default is only a fallback and is
// never written from here. Refetched whenever the draft's Agent changes; while loading
// (or after a failed fetch) the picker stays disabled (null).
const [agentThinkingLevel, setAgentThinkingLevel] = useState<string | null>(null);
/**
* The draft's Agent once the list on offer carries it. Nothing is asked about an id the cache
* or the route named until then: a cached id may be one this Project does not have (deleted
* since, or written while another Project's Agents were on screen), and the request would only
* come back "This agent no longer exists."
*/
const selectedAgent = agents.find((a) => a.agentId === agentId) ?? null;
const resolvedAgentId = selectedAgent?.agentId ?? null;
useEffect(() => {
setAgentThinkingLevel(null);
if (!resolvedAgentId) return;
let cancelled = false;
api
.getAgentConfig(projectId, resolvedAgentId)
.then((res) => {
if (!cancelled) setAgentThinkingLevel(res.config.model?.thinkingLevel ?? "");
})
.catch(() => undefined);
return () => {
cancelled = true;
};
}, [projectId, resolvedAgentId]);
const thinkingLevel =
agentThinkingLevel === null || chatDefaults === null
? null
: effectiveThinkingLevel(agentThinkingLevel, chatDefaults.thinkingLevel);
/**
* A refused level write and the Agent it was aimed at. The display rolls back to the level it
* read before, so this sentence is the only thing saying the press did not take; it is handed to
* the composer only while that Agent is still the one on screen.
*/
const [thinkingRefusal, setThinkingRefusal] = useState<{ agentId: string; text: string } | null>(
null,
);
/** Live mirror for the rollback value (a stale closure would roll back to an outdated level). */
const thinkingRef = useRef<string | null>(null);
thinkingRef.current = agentThinkingLevel;
const onChangeThinkingLevel = useCallback(
(level: string) => {
// "" (no override) is not persistable through the config API — the picker disables that row.
if (!agentId || !level) return;
const rollback = thinkingRef.current;
setThinkingRefusal(null); // The new attempt answers for itself; the old sentence goes.
setAgentThinkingLevel(level); // Optimistic: the derived display follows immediately.
api
.putAgentConfig(projectId, agentId, {
config: { model: { thinkingLevel: level as AgentModelConfigDto["thinkingLevel"] } },
})
.catch((e: unknown) => {
setAgentThinkingLevel(rollback);
// This picker writes through to the Agent's settings, so the refusal left more than
// this draft alone — the sentence says so (forAgent).
setThinkingRefusal({ agentId, text: thinkingLevelRefused(e, true) });
});
},
[projectId, agentId],
);
// Skills installed on the currently selected Agent (candidates for the input
// area's skills dropdown): switching Agents first clears the list (which also
// clears the selection in the input area), then refetches; a fetch failure is
// silently treated as no skills. Clearing preserves the reference when already
// empty (doesn't swap in a new array): swapping the reference on the very first
// mount render would trigger ChatInput's pruning effect and wrongly clear the
// quick-invoke preselection.
const [agentSkills, setAgentSkills] = useState<SkillMetadataItem[]>([]);
useEffect(() => {
setAgentSkills((prev) => (prev.length > 0 ? [] : prev));
if (!resolvedAgentId) return;
let cancelled = false;
api
.getAgentSkills(projectId, resolvedAgentId)
.then((res) => {
if (!cancelled) setAgentSkills(res.skills);
})
.catch(() => undefined);
return () => {
cancelled = true;
};
}, [projectId, resolvedAgentId]);
// —— Auto-cache ——
// Options (Agent / Workspace / approval mode / Model) are discrete clicks: written
// immediately on change; body text is keystroke-frequent: debounced trailing write,
// with a final flush on unmount if there's an unsaved change.
const saveTimer = useRef<number | null>(null);
const cancelPendingSave = useCallback(() => {
if (saveTimer.current !== null) {
window.clearTimeout(saveTimer.current);
saveTimer.current = null;
}
}, []);
const persistNow = useCallback(() => {
cancelPendingSave();
if (!userId) return;
const data: DraftCache = { text: textRef.current, workspace, approvalMode };
if (Object.keys(sandboxPick).length > 0) data.sandbox = sandboxPick;
// Saved with the path: a draft restored without its machine would create the Session
// here, against a path that only exists somewhere else.
if (workspaceMachine !== null) data.machineId = workspaceMachine;
if (agentId) data.agentId = agentId;
if (modelRef) data.modelRef = modelRef;
if (skillsRef.current.length > 0) data.skills = skillsRef.current;
// Carried through every write, so a reload finds the prefill still marked as composed
// rather than resuming it as if it had been typed here.
if (aiPrefillRef.current) data.aiPrefill = true;
// A parked draft writes back into its own list entry; the active draft into its slot.
if (draftId !== undefined) saveDraftSession(userId, projectId, draftId, data);
else saveDraft(draftKey(userId, projectId), data);
}, [
cancelPendingSave,
userId,
projectId,
draftId,
agentId,
workspace,
workspaceMachine,
approvalMode,
sandboxPick,
modelRef,
]);
// The timer and unmount cleanup read persistNow via a ref to always get the **latest version**: a stale closure would write back outdated options.
const persistRef = useRef(persistNow);
useEffect(() => {
persistRef.current = persistNow;
// Write immediately on option change (also writes once on mount, idempotently).
persistNow();
}, [persistNow]);
const onTextChange = useCallback(
(text: string) => {
textRef.current = text;
// Editing an AI-composed prefill makes it the user's own text — an ordinary draft, kept
// when the page is left and parkable like any other (see aiPrefillRef).
aiPrefillRef.current = false;
cancelPendingSave();
saveTimer.current = window.setTimeout(() => {
saveTimer.current = null;
persistRef.current();
}, DRAFT_SAVE_DEBOUNCE_MS);
},
[cancelPendingSave],
);
/** Skill checklist change: writes back to the ref and persists immediately (discrete click, same convention as Agent/Model and other options). */
const onSkillsChange = useCallback((names: string[]) => {
skillsRef.current = names;
persistRef.current();
}, []);
/**
* Leaving an AI-composed prefill the user neither edited nor sent: the slot is left exactly as
* a successful send leaves it — model carry-over only — so the prompt cannot reappear in a
* later new conversation, and the next "Create with AI" jump has nothing to park. Storage is
* all this touches, deliberately: StrictMode runs the unmount cleanup once right after mount,
* and the persist effect above then rewrites the slot from component state, which must still
* describe the prefilled draft on screen.
*/
const dropAiPrefill = useCallback(() => {
if (!userId) return;
if (modelRef) saveDraft(draftKey(userId, projectId), { modelRef });
else clearDraft(draftKey(userId, projectId));
}, [userId, projectId, modelRef]);
/** Latest-closure mirror for the unmount cleanup (same convention as persistRef). */
const dropAiPrefillRef = useRef(dropAiPrefill);
dropAiPrefillRef.current = dropAiPrefill;
// Unmount: drop an untouched AI prefill, or else flush still-unsaved body text (so a route
// change/page switch doesn't lose the last few keystrokes). Exclusive by construction — a
// pending save exists only after an edit, and an edit clears the prefill mark.
useEffect(
() => () => {
if (aiPrefillRef.current) {
dropAiPrefillRef.current();
} else if (saveTimer.current !== null) {
window.clearTimeout(saveTimer.current);
persistRef.current();
}
},
[],
);
// parkActiveDraft ("New chat" clicked while text is typed here) reads the active cache
// synchronously right after firing this event: flush the debounce window so the park
// captures the latest keystrokes — and so this instance's unmount flush, which would
// otherwise fire AFTER the park, has nothing left to write back into the just-cleared
// active slot. A parked instance flushes into its own entry (harmless).
useEffect(() => {
const onFlush = (): void => {
if (saveTimer.current !== null) {
window.clearTimeout(saveTimer.current);
saveTimer.current = null;
persistRef.current();
}
};
window.addEventListener(DRAFT_FLUSH_EVENT, onFlush);
return () => window.removeEventListener(DRAFT_FLUSH_EVENT, onFlush);
}, []);
/**
* Discard the draft after a successful send: first cancels the pending save timer, otherwise
* it would write the just-cleared draft back. The **model selection carries over** as the
* next conversation's default (review: switching the model, like switching the thinking
* level, makes the switched-to value the new default — the level persists on the Agent
* config, the model here in the per-user draft cache); everything else clears.
*/
const discardDraft = useCallback(() => {
cancelPendingSave();
// Clear the preselected skills too: any subsequent write (e.g. the unmount flush) must not resurrect a selection that's already been sent.
skillsRef.current = [];
// A sent prefill has done its job; the unmount that follows has nothing left to drop.
aiPrefillRef.current = false;
// The unmount flush routes through persistNow, which would otherwise write the
// just-sent content back (into the parked entry, resurrecting a deleted row): with
// the text gone the flush becomes an idempotent empty-shell write.
textRef.current = "";
if (!userId) return;
if (draftId !== undefined) {
// A sent parked draft simply disappears from the list; the ACTIVE slot is not
// touched — it may hold a different conversation-in-the-making.
removeDraftSession(userId, projectId, draftId);
return;
}
if (modelRef) saveDraft(draftKey(userId, projectId), { modelRef });
else clearDraft(draftKey(userId, projectId));
}, [cancelPendingSave, userId, projectId, draftId, modelRef]);
const selectAgent = (a: AgentSummary) => {
setAgentId(a.agentId);
// Follow through to the global current Agent: keeps the sidebar memory and stats convention consistent.
setCurrentAgentId(a.agentId);
};
// The Agents of the machine the workspace is on. What that machine was last seen running
// is offered first, so the picker has something while the machine is asked — and stays on
// offer if it cannot be: it is the best account of that machine anyone has. The machine's
// own answer replaces it wholesale, including with nothing, so an Agent deleted over there
// stops being offered here. Nothing here connects: a machine with no held connection
// answers as unreachable, and the Machines page is where a person connects it.
useEffect(() => {
if (workspaceMachine === null) {
setRemoteAgents([]);
return;
}
let cancelled = false;
setRemoteAgents(cachedMachineAgents(projectId, workspaceMachine));
setRemoteAgentsState("loading");
void (async () => {
try {
const answered = (await api.listAgents(projectId, workspaceMachine)).agents;
rememberMachineAgents(projectId, workspaceMachine, answered);
if (!cancelled) setRemoteAgents(answered);
} catch {
if (!cancelled) setRemoteAgentsState("unreachable");
}
})();
return () => {
cancelled = true;
};
}, [workspaceMachine, projectId]);
/** User edits routed through these two so a late-arriving project default cannot clobber them. */
const changeWorkspace = useCallback((path: string, machineId?: string | null) => {
touchedRef.current.workspace = true;
setWorkspace(path);
// The machine travels with the path, always — including back to null when the pick moves
// home, or the next Session would be created on the machine the previous pick named.
setWorkspaceMachine(machineId ?? null);
}, []);
// A preset sets both halves of the draft's level at once, as it does on a Session; an
// approval-mode pick (switch off) leaves the sandbox half unpicked, so the settings decide it.
const changePermission = useCallback((pick: PermissionPick) => {
touchedRef.current.approval = true;
setApprovalMode(pick.approvalMode);
setSandboxPick((prev) => draftSandboxAfter(prev, pick));
}, []);
// Synchronous in-flight guard for the one send entry point (the composer): a second
// submission while one is running would create a second Session with its own first task and
// a racing navigation. A ref rather than state — the composer disables its own send button
// off its `busy` state, and nothing else on this page renders differently mid-send.
const sendingRef = useRef(false);
/**
* The reason this draft's last send did not go out. Sending is the page's only write, and a
* refusal changes nothing else on screen: the text stays in the card, the send button is the
* retry — so the sentence belongs right under the card instead of in a notification that lives
* six seconds (first-send-refusal.ts). Cleared when the next attempt starts.
*/
const [sendRefusal, setSendRefusal] = useState<string | null>(null);
// Whether this draft's page is still the one open: the first send's round trips can outlast
// it, and a new draft of the same Project would reuse its dock scope.
const openRef = useRef(true);
useEffect(() => {
openRef.current = true;
return () => {
openRef.current = false;
};
}, []);
// First message sent: only now is the Session created (Agent / Workspace / Model / approval
// mode are all locked in together), then the route jumps once sent; returns false on any
// failure, so the input area keeps the draft and can resend — with the reason left under the
// card by the catch below.
const onSend = useCallback(
async (input: TaskInputPart[], goal: { budget: number } | null = null): Promise<boolean> => {
if (!agentId || sendingRef.current) return false;
sendingRef.current = true;
// This attempt's own reason, not the one the attempt before it left (one attempt, one reason).
setSendRefusal(null);
// The draft's own docks, named now: the send takes round trips, and the user may be on
// another conversation by the time it has a Session to hand them to.
const dockScope = currentDockScope();
let createdId: string | null = null;
try {
const body: SessionCreateRequest = { approvalMode };
if (Object.keys(sandboxPick).length > 0) body.sandbox = sandboxPick;
// Model reference is submitted as a pair (provider + modelId; falls back to the Project default when not set).
if (modelRef) {
body.modelId = modelRef.modelId;
body.provider = modelRef.provider;
}
if (workspace.trim()) body.workspace = workspace.trim();
// Created ON the machine that owns the workspace: that server runs the agent in it.
const created = await api.createSession(projectId, agentId, body, workspaceMachine);
createdId = created.session.sessionId;
const res = await api.postTask(createdId, { input, ...(goal ? { goal } : {}) });
// postTask answers with the CURRENT id: a Session with no Trace whose process
// restarted in between self-heals into a new one. Everything recorded a moment ago
// under the id we created is then about a Session that no longer answers to it, and
// the routing map is the half that fails silently — a remote Session left mapped
// under the old id would be asked of THIS server, which does not have it. Re-recorded
// BEFORE the lookup below, which is itself one of those calls.
if (res.sessionId !== createdId) rememberSessionMachine(res.sessionId, workspaceMachine);
// Re-fetch the row before listing it: the server persisted the fallback title at
// Task start (inside the postTask call), and its session_title push may have gone
// out before this row existed in the list, where it patched nothing. The fresh row
// also carries the post-self-heal id, matching where we navigate.
const fresh = await api.getSession(res.sessionId).catch(() => null);
// Listed under the id we are about to navigate to. Falling back to the created row
// verbatim would list the OLD id — a stale row, and a route naming a Session the
// list does not contain.
add(fresh?.session ?? { ...created.session, sessionId: res.sessionId });
discardDraft();
// The draft now has an id of its own, so its docks move with it: anything left
// behind under the draft's scope would surface in the NEXT new conversation
// instead (dock-state.ts). Only someone still on this draft is taken to the new
// Session; whoever moved to another conversation stays there, and the Session waits
// in the sidebar.
if (adoptDockScope(dockScope, res.sessionId, openRef.current))
navigate(`/chat/${res.sessionId}`, { replace: true });
return true;
} catch (e) {
// The Session was created but the first message failed to send (postTask failed): delete
// this empty Session, otherwise every resend attempt would create another one, piling up
// empty sessions with no messages in the sidebar (best-effort cleanup).
if (createdId) void api.deleteSession(createdId).catch(() => undefined);
// Nothing was written and the card still holds the message: the reason stays under it,
// where the send button that retries is (see the `sendRefusal` state).
setSendRefusal(sendRefused(sendRefusalReason(e, modelRef?.modelId)));
return false;
} finally {
sendingRef.current = false;
}
},
[
projectId,
agentId,
approvalMode,
sandboxPick,
modelRef,
workspace,
add,
discardDraft,
navigate,
],
);
/**
* The composer handle this page drives. The dock's panels reach the same draft through it,
* and the shortcuts folder below the input card fills the composer with it.
*/
const ownComposerRef = useRef<ComposerControl | null>(null);
const composerRef = pageComposerRef ?? ownComposerRef;
/**
* A saved shortcut FILLS the composer — its prompt into the text body, no Skills pinned —
* and sends nothing; the user reads what landed, edits it if they want, and presses Send.
* Filling is instant and local: there is no busy state and no in-flight guard to keep here,
* and everything else — where the prompt goes when text is already typed, focus, the caret —
* is the composer's, reached through this handle. No Skills are pinned because the prompt is
* the user's own text, not a card authored against the Skill catalog this product ships (see
* user-shortcuts.ts).
*/
const fillShortcut = useCallback((prompt: string) => {
composerRef.current?.fillPrompt(prompt, []);
}, []);
/**
* Whether the shortcuts folder below the input card is open. Closed on arrival: the new-chat
* screen offers nothing to click but the input card, and a block of canned or saved prompts
* under it is one more thing to read past. Opening it is a one-way move — the row is a tab,
* not a disclosure (see shortcuts-folder.tsx), and with a single folder there is nothing to
* switch to.
*/
const [shortcutsOpen, setShortcutsOpen] = useState(false);
// Capability info for the currently selected model (vision/video/context window) switches instantly with the selection (matched by paired reference).
const modelInfo = models?.models.find((m) => sameModelRef(m, modelRef));
const contextWindow = modelInfo?.contextWindow;
const vision = modelInfo?.vision !== false;
// Only a model that says it takes video gets a clip inline; every other one is given it as a
// scratchpad file (see lib/video-input). The default is therefore the opposite of vision's.
const video = modelInfo?.video === true;
return (
<div className="anim-fade flex min-h-0 flex-1 flex-col overflow-y-auto px-3 py-6 md:px-4">
{/*
* Vertical layout: everything visible — brand, input card, ownership pills, the shortcuts
* folder — lives in ONE block between two empty flex-1 spacers, so the block is centred and
* the free space above and below it is exactly equal. The brand deliberately sits inside that
* block rather than in the upper spacer: keeping it in the spacer made the upper gap shorter
* than the lower one by the brand's own height, which pushed the card up the viewport and left
* the slash menu — it opens upward, `bottom-full` — too little room, so it clipped against
* the top of this scroll container. When the viewport is too short the spacers collapse to
* nothing, the container's own py-6 keeps the content off the edges, and the page falls back
* to natural scrolling.
*/}
<div className="flex-1" />
<div className="mx-auto w-full max-w-3xl">
{/* Large brand logo + brand name + subtitle (e2e tests identify the draft page by this
heading). The asset is square-cropped and the graphic already has a bit of built-in
padding, so a small margin is enough to sit visually close to the title. */}
<div className="mb-10 text-center">
<AppLogo src="/adelie-icon.svg" className="mx-auto mb-1 h-36 w-36 rounded-3xl" />
<h1 className="ui-display text-3xl font-semibold tracking-tight text-gray-900 dark:text-gray-100">
{S.appName}
</h1>
<p className="mt-2 text-base text-gray-400 dark:text-gray-500">{S.chat.draftSubtitle}</p>
<VersionLine />
</div>
<ChatInput
status="idle"
controlRef={composerRef}
onSend={onSend}
onStop={async () => undefined}
modelRef={modelRef}
models={models?.models ?? []}
onChangeModel={setModelRef}
thinkingLevel={thinkingLevel}
onChangeThinkingLevel={onChangeThinkingLevel}
thinkingRefusal={thinkingRefusal?.agentId === agentId ? thinkingRefusal.text : null}
{...(models?.defaultModel !== undefined ? { defaultModel: models.defaultModel } : {})}
{...(contextWindow !== undefined ? { contextWindow } : {})}
contextNow={0}
vision={vision}
video={video}
approvalMode={approvalMode}
// A draft becomes an ordinary conversation, never an organization's: every mode.
approvalModes={APPROVAL_MODES}
sandbox={{ ...(chatDefaults?.sandbox ?? UNCONFINED), ...sandboxPick }}
onChangePermission={changePermission}
modeSaving={false}
autoFocus
agents={agents}
{...(agentId ? { currentAgentId: agentId } : {})}
skills={agentSkills}
{...(cached.skills && cached.skills.length > 0 ? { initialSkills: cached.skills } : {})}
onSkillsChange={onSkillsChange}
initialText={cached.text ?? ""}
onTextChange={onTextChange}
/>
{/* The first message did not go out: nothing was written (an empty Session is deleted
again), the text and the staged files are still in the card above, and the send button
is the retry — so the reason stays right here, under what it is about. */}
{sendRefusal !== null && (
<p
role="alert"
data-testid="draft-send-refusal"
data-tooltip={sendRefusal}
data-tooltip-content="text"
className={`mt-2 line-clamp-2 break-words text-xs ${toneInk.danger}`}
>
{sendRefusal}
</p>
)}
{/* Ownership selection right below the card (small pill dropdowns, styled after ChatGPT's project picker button) */}
<div className="mt-2 flex flex-wrap items-center gap-2">
<AgentSelect
agents={agents}
selected={selectedAgent}
onSelect={selectAgent}
empty={
workspaceMachine !== null && remoteAgentsState === "unreachable"
? S.machines.agentsUnreachable
: S.common.loading
}
/>
<WorkspaceSelect
projectId={projectId}
workspace={workspace}
machineId={workspaceMachine}
onChange={changeWorkspace}
chooseMachine
{...(agentId ? { agentId } : {})}
/>
{/* The dock's Files panel on the folder picked beside it; a temporary Workspace has
none yet (see FilesPanelToggle). */}
<FilesPanelToggle available={workspace.trim() !== ""} />
</div>
{/* The user's own saved prompts, the only folder left under the input card (see
shortcuts-folder.tsx): a click fills the composer with the prompt and the user
sends it. Closed on arrival, so the new-chat screen is the brand, the input card
and the two ownership pills unless the reader asks for more — and its own header
folds it back up. */}
<div className="mt-6 space-y-1">
<ShortcutsFolder
open={shortcutsOpen}
onToggle={() => setShortcutsOpen((open) => !open)}
readComposerText={() => textRef.current}
onFill={fillShortcut}
/>
</div>
</div>
{/* Lower symmetric space — empty, so it matches the upper one exactly */}
<div className="flex-1" />
</div>
);
}
/**
* Superscript "new version" hint on the version line: plain small text raised via
* align-super, in the version line's own muted color and weight. Deliberately not a pill —
* user feedback was that the earlier accent-colored pill read as a button; the link case
* only adds a hover underline. The only remaining copy: the sidebar's version row dropped
* its badge when the three update rows collapsed into one whose label already names the
* new version.
*/
const versionBadgeClass =
"ml-1.5 inline-block align-super text-xs leading-4 text-gray-400 dark:text-gray-500";
/**
* Quiet version line under the brand subtitle: `vX.Y.Z · Last updated Jul 26`, plus
* `· Latest vA.B.C` when the update check knows a newer release (localized per
* dictionary). The product name is not repeated here — the brand wordmark
* sits directly above, and the sidebar's version footer is bare `vX.Y.Z` too. The date is
* the running version's release
* date, stamped into core's BUILD_DATE at build time — displayed as-is, no network;
* dev builds and releases that predate the stamping (v0.1.2 and earlier) carry null
* and show the version alone. When the update flow has something waiting — a release
* offered, a download in the background, a restart pending — a small superscript badge
* follows, a button into the update modal (the same modal the sidebar's update row opens).
* Fetching starts on mount — useVersionInfo caches at module level, so after the first
* resolution anywhere in the app this renders instantly and never refetches. Nothing
* renders until the version resolves (no placeholder flicker under the brand).
*/
function VersionLine() {
const { locale } = useLocale();
const { version, update } = useVersionInfo(true);
if (version === null) return null;
const date = version.buildDate;
// The newest published release, when the check knows one newer than this build: the
// version line is where a reader looks to answer "我落后了没有", and the superscript
// badge alone only says that an update exists — its version lives in a tooltip. Shown
// only when it is newer, so an up-to-date build has nothing to add: the version above
// already is the latest one. Naming none (the lookup failed, checks are off, or the
// backend has no release) leaves the line as it was.
const latest = update !== null && update.updateAvailable ? update.latestVersion : null;
return (
<p className="mt-1.5 text-xs text-gray-400 dark:text-gray-500">
{`v${version.version}${
date !== null ? ` · ${S.update.lastUpdated(formatMonthDay(date, locale))}` : ""
}`}
{latest !== null && ` · ${S.update.latestRelease(latest)}`}
<VersionBadge />
</p>
);
}
/**
* The superscript on the version line: a button into the update modal, worded by where the
* flow stands. Its title and accessible name carry the update row's own sentence, so the
* two surfaces say the same thing about the same release.
*/
function VersionBadge() {
const { mode, flow } = useUpdateFlow();
const badge = versionBadgeFor(flow);
if (mode === "none" || badge === null) return null;
const text =
badge === "available"
? S.update.newVersionBadge
: badge === "downloading"
? S.update.badgeDownloading
: S.update.badgeReady;
const note =
flow.kind === "available"
? S.update.newVersion(flow.version)
: flow.kind === "downloading"
? S.update.rowDownloading(flow.version, flow.percent)
: flow.kind === "ready"
? S.update.restartToUpdate(flow.version)
: text;
return (
<button
type="button"
onClick={openUpdateModal}
data-tooltip={note}
data-tooltip-extra
aria-label={note}
className={`${versionBadgeClass} hover:underline`}
>
{text}
</button>
);
}
/** Agent selection (pill dropdown): avatar + name, menu opens downward with an internal scroll cap. */
function AgentSelect({
agents,
selected,
onSelect,
empty,
}: {
agents: AgentSummary[];
selected: AgentSummary | null;
onSelect: (agent: AgentSummary) => void;
/** The empty row's text — why there is nothing to pick yet. */
empty: string;
}) {
const [open, setOpen] = useState(false);
return (
<Dropdown
open={open}
setOpen={setOpen}
menuClass="left-0 top-full mt-1 w-72 max-w-[calc(100vw-2rem)] origin-top-left"
button={
<button
type="button"
data-tooltip={S.chat.chooseAgent}
aria-label={S.chat.chooseAgent}
onClick={() => setOpen(!open)}
className={pillClass}
>
{selected ? (
<AgentAvatar
id={selected.agentId}
name={agentDisplayName(selected)}
size={16}
className="shrink-0 rounded"
/>
) : null}
<span className="min-w-0 truncate">
{selected ? agentDisplayName(selected) : S.common.loading}
</span>
<Chevron open={open} size={12} className="shrink-0 text-gray-400" />
</button>
}
>
<div className="max-h-56 overflow-y-auto">
{agents.length === 0 && <p className="px-3 py-1.5 text-xs text-gray-400">{empty}</p>}
{agents.map((a) => {
const active = a.agentId === selected?.agentId;
return (
<MenuItem
key={a.agentId}
density="sm"
aria-pressed={active}
checked={active}
onSelect={() => {
onSelect(a);
setOpen(false);
}}
glyph={
<AgentAvatar
id={a.agentId}
name={agentDisplayName(a)}
size={20}
className="shrink-0 rounded"
/>
}
label={agentDisplayName(a)}
description={
a.description ? <span className="block truncate">{a.description}</span> : undefined
}
/>
);
})}
</div>
</Dropdown>
);
}