Code blocks are drawn in the same pass as the Markdown, so a streaming reply no longer flickers. Two fixes from the review on 40eb3027:
- An unlabelled fence is detected by highlight.js again (`highlightAuto`, behind a 128-entry LRU). Only a fence that is still open at the end of a streaming message stays plain "text" (`isClosedFence`, which follows marked's fence rule).
- DOMPurify is back to the html profile only. The code-block icons are per-load random placeholders until after sanitize, and `codeBlockKey` is gone.
The Windows desktop reconnect is also fixed. `safeBackUrl` (gate, wrapper and web) takes loopback Magpie pages only, never `http://wails.localhost`. WebView2 can't route a 127.0.0.1 frame's request there, so it showed its gray error page. In the desktop window the wrapper, or an unwrapped daemon page, posts `{magpie:1, method:"error"}` instead, and Magpie's pluginpages.js answers with its Try again card. A stale stored wails back URL is dropped.
Reviewed at c520fa46 (merge base 1eb9473f = main) on mote (mote-run, HOME=$(mktemp -d), package copied out of the workspace):
- npm ci; `npm run check` clean; `npm run verify:build` OK for all 5 files.
- `npx vitest run` with CHROME_PATH set to Playwright's Chromium 1248: 77 files, 567 passed, 17 skipped, 0 failed. markdown-render-browser ran (it went red under the hand-breaks).
- `bun scripts/check.mjs agent-console` ok. Repo `bun test`: 6449 pass, 0 fail.
- Broken by hand, each red:
- autoDetect forced on while streaming: markdown-render-browser red.
- `svg: true` put back in the profile: markdown-render-browser red.
- Auto-detection removed: codeBlock and markdown-render-browser red.
- LRU eviction removed: codeBlock red.
- `isClosedFence` accepting any backtick run: codeBlock red.
- wails.localhost accepted again in gate.mjs, then in backToMagpie.ts: ac11 red (3 and 2 tests).
- The same in wrapper.mjs: wrapper red (4 tests).
- The stale stored back kept: wrapper red.
- Read magpie's pluginpages.js on main: `method:"error"` from the page's own frame calls `broke(key, why)`.
Not tried: a real WebView2 on Windows (the reconnect path), or a real streaming reply in the desktop app.
880 lines
42 KiB
TypeScript
880 lines
42 KiB
TypeScript
// Input area (§8.6 Composer): auto-grow, IME, queue/steer, waiting for permission, attachments,
|
||
// Agent·model popover, thinking level, permission mode, ↑ to recall.
|
||
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import { isImeComposing } from "../composer/ime";
|
||
import { useUi } from "../stores/ui";
|
||
import { useData } from "../stores/data";
|
||
import { useTimeline } from "../stores/timeline";
|
||
import { ws } from "../main";
|
||
import { getToken } from "../api/token";
|
||
import { Icon } from "./Icons";
|
||
import { attachmentUrl, MentionChip } from "./Timeline";
|
||
import { addQuote, MAX_QUOTES, quoteLabel, quotesToText, quotesFromText, type Quote } from "../composer/quotes";
|
||
import { MENTION_MIME, addMention, joinProjectPath, mentionsForSend, parseMentionDrag, relFromAbs, type MentionDraft } from "../composer/mentions";
|
||
import type { Mode, ModelInfo } from "../types/models";
|
||
import type { SlashCommand } from "../composer/slashMenu";
|
||
import { toast } from "./Toast";
|
||
import { useLiveSettings } from "./Settings";
|
||
import { composerModeOptions, effortLabel } from "../i18n/modes.js";
|
||
import { Logo } from "./Logo";
|
||
import { currentChecklist, todoProgress } from "../timeline/fold";
|
||
import { SelectMenu } from "./SelectMenu";
|
||
import { AutoApproveBanner } from "./AutoApproveBanner";
|
||
import { errMsg, t as tr, tf } from "../i18n/index.js";
|
||
import {
|
||
applySlashPick,
|
||
slashEnterSends,
|
||
clampSlashIndex,
|
||
filterSlashCommands,
|
||
selectableSlashRows,
|
||
slashMenuRows,
|
||
slashMenuState,
|
||
} from "../composer/slashMenu";
|
||
|
||
type Attachment = { path: string; name: string; mime: string; uploading?: boolean };
|
||
|
||
/** Maximum rows drawn in the model list at once (hundreds of buttons make the popover noticeably sluggish) */
|
||
const MODEL_LIST_CAP = 150;
|
||
|
||
/** memo: no props, so Chat re-rendering on every streamed token (items, virtual height) leaves the composer alone */
|
||
export const Composer = memo(function Composer() {
|
||
const ui = useUi();
|
||
const { threads, providers, projects } = useData();
|
||
// 1.3.7: subscribe field-by-field so assistant_delta tokens don't re-render the whole composer
|
||
const status = useTimeline((s) => s.status);
|
||
const pending = useTimeline((s) => s.pending);
|
||
const slashCommands = useTimeline((s) => s.slashCommands);
|
||
const queue = useTimeline((s) => s.queue);
|
||
const hasUserItem = useTimeline((s) => s.items.some((it) => it.kind === "user"));
|
||
const hasAgentItem = useTimeline((s) => s.items.some((it) =>
|
||
it.kind === "assistant" || it.kind === "reasoning" || it.kind === "toolgroup" || it.kind === "permission"));
|
||
const checklist = useTimeline((s) => currentChecklist(s.items));
|
||
const [text, setText] = useState("");
|
||
const [attachments, setAttachments] = useState<Attachment[]>([]);
|
||
const [mentions, setMentions] = useState<MentionDraft[]>([]);
|
||
const [quotes, setQuotes] = useState<Quote[]>([]);
|
||
const [dropHint, setDropHint] = useState(false);
|
||
const [agentPop, setAgentPop] = useState(false);
|
||
const [pickerProviderId, setPickerProviderId] = useState<string | null>(null);
|
||
const [modelQ, setModelQ] = useState("");
|
||
const [modelsRefreshing, setModelsRefreshing] = useState(false);
|
||
const modelsRefreshSeq = useRef(0);
|
||
const modelsForceSeq = useRef(0);
|
||
const pickerRef = useRef<HTMLDivElement>(null);
|
||
const taRef = useRef<HTMLTextAreaElement>(null);
|
||
const fileRef = useRef<HTMLInputElement>(null);
|
||
const [lastSent, setLastSent] = useState<string[]>([]);
|
||
const [elapsed, setElapsed] = useState(0);
|
||
const [slashSel, setSlashSel] = useState(0);
|
||
const slashSuppress = useRef(false);
|
||
const slashLoadThreads = useRef(new Set<string>());
|
||
const [slashLoading, setSlashLoading] = useState(false);
|
||
const prefs = useLiveSettings();
|
||
|
||
const thread = threads.find((t) => t.id === ui.currentThreadId);
|
||
const provider = providers.find((p) => p.id === thread?.provider);
|
||
const running = status === "running" || status === "initializing";
|
||
const waiting = pending.length > 0;
|
||
const steerOk = provider?.capabilities.steer ?? false;
|
||
// Known not to take images (an ACP Agent whose initialize doesn't advertise them): images still go, as file paths
|
||
const imagesByPath = !!provider && !provider.capabilities.images && provider.capabilities.imagesKnown !== false;
|
||
const mode = thread?.mode ?? "default";
|
||
const modes = composerModeOptions(thread?.provider, provider?.capabilities.modes ?? [], provider?.capabilities.modeLabels);
|
||
const modeMeta = modes.find((m) => m.id === mode);
|
||
// The Agent is locked once the conversation has run. 1.3: the user's message is saved before the Agent starts, so a
|
||
// first message whose Agent never started (no session id, no reply) leaves the picker open (the daemon agrees)
|
||
const started = hasUserItem && (running || !!thread?.providerSessionId || hasAgentItem);
|
||
|
||
const slashMenu = useMemo(() => slashMenuState(text, slashSuppress.current), [text]);
|
||
const slashFiltered = useMemo(
|
||
() => filterSlashCommands(slashCommands, slashMenu.query),
|
||
[slashCommands, slashMenu.query],
|
||
);
|
||
const slashRows = useMemo(() => slashMenuRows(slashFiltered), [slashFiltered]);
|
||
const slashPickable = useMemo(() => selectableSlashRows(slashRows), [slashRows]);
|
||
const slashOpen = slashMenu.open && !waiting;
|
||
|
||
useEffect(() => {
|
||
setSlashSel(0);
|
||
}, [slashMenu.query, slashOpen]);
|
||
|
||
// Only the first "/" typed fetches commands with load:true (opening a conversation doesn't start the Agent)
|
||
useEffect(() => {
|
||
const id = thread?.id;
|
||
if (!slashOpen || !id) return;
|
||
if (slashLoadThreads.current.has(id)) return;
|
||
slashLoadThreads.current.add(id);
|
||
// The first time we start the Agent and wait for its commands (seconds): meanwhile the menu shows "loading commands…" rather than "no commands"
|
||
setSlashLoading(true);
|
||
void ws.call("thread.commands", { id, load: true }, 30000).then((r) => {
|
||
const commands = (r as { commands?: SlashCommand[] }).commands ?? [];
|
||
// A background push (commands event) may already have arrived: don't overwrite it with an empty result
|
||
if (commands.length || !useTimeline.getState().slashCommands.length) {
|
||
useTimeline.setState({ slashCommands: commands });
|
||
}
|
||
}).catch(() => {}).finally(() => setSlashLoading(false));
|
||
}, [slashOpen, thread?.id]);
|
||
|
||
const visibleProviders = useMemo(
|
||
() => providers.filter((p) => p.enabled !== false || p.id === thread?.provider),
|
||
[providers, thread?.provider],
|
||
);
|
||
|
||
const pickerProvider = visibleProviders.find((p) => p.id === (pickerProviderId ?? thread?.provider));
|
||
|
||
const providerOk = (p: (typeof visibleProviders)[0]) =>
|
||
p.enabled !== false && p.available && p.status !== "planned";
|
||
|
||
/** Soft sync (silent) or forced ↻ refresh; Magpie config mtime invalidates the daemon cache automatically.
|
||
* Soft sync never disables ↻; a forced click is never coalesced onto an in-flight soft sync. */
|
||
const refreshModels = (opts?: { force?: boolean; provider?: string | null }) => {
|
||
const force = !!opts?.force;
|
||
const seq = ++modelsRefreshSeq.current;
|
||
let forceSeq = 0;
|
||
if (force) {
|
||
forceSeq = ++modelsForceSeq.current;
|
||
setModelsRefreshing(true);
|
||
}
|
||
const args: { refresh?: boolean; provider?: string } = {};
|
||
if (force) args.refresh = true;
|
||
if (opts?.provider) args.provider = opts.provider;
|
||
return (ws.call("providers.list", Object.keys(args).length ? args : undefined) as Promise<import("../types/models").ProviderInfo[]>)
|
||
.then((list) => {
|
||
if (seq !== modelsRefreshSeq.current) return; // a newer soft/force request won
|
||
useData.setState({ providers: list });
|
||
})
|
||
.catch(() => {})
|
||
.finally(() => {
|
||
// Soft sync must not clear (or leave stuck) the forced spinner
|
||
if (force && forceSeq === modelsForceSeq.current) setModelsRefreshing(false);
|
||
});
|
||
};
|
||
|
||
const openPicker = (open: boolean) => {
|
||
setAgentPop(open);
|
||
if (open) {
|
||
setPickerProviderId(null);
|
||
setModelQ("");
|
||
// Opening the picker: silent soft sync (does not disable ↻)
|
||
void refreshModels({
|
||
provider: started ? (thread?.provider ?? null) : null,
|
||
});
|
||
}
|
||
};
|
||
|
||
// Close on outside click / Esc (it used to close as soon as the pointer left, so a height change would "throw" the user out)
|
||
useEffect(() => {
|
||
if (!agentPop) return;
|
||
const onDown = (e: PointerEvent) => {
|
||
if (pickerRef.current && !pickerRef.current.contains(e.target as Node)) openPicker(false);
|
||
};
|
||
const onKey = (e: KeyboardEvent) => {
|
||
if (e.key === "Escape") { e.stopPropagation(); openPicker(false); }
|
||
};
|
||
document.addEventListener("pointerdown", onDown, true);
|
||
window.addEventListener("keydown", onKey, true);
|
||
return () => {
|
||
document.removeEventListener("pointerdown", onDown, true);
|
||
window.removeEventListener("keydown", onKey, true);
|
||
};
|
||
}, [agentPop]);
|
||
|
||
const pickModelForProvider = async (providerId: string, model: string | null) => {
|
||
if (!thread) return;
|
||
// Close the popover first; the switch happens in the background (no longer waits for detection/model fetching to respond)
|
||
openPicker(false);
|
||
try {
|
||
if (!started && providerId !== thread.provider) {
|
||
await ws.call("thread.setProvider", { id: thread.id, provider: providerId });
|
||
}
|
||
await ws.call("thread.setModel", { id: thread.id, model });
|
||
} catch (e) {
|
||
toast(tr("composer.failSwitchAgent") + errMsg(e), true);
|
||
}
|
||
};
|
||
|
||
const modelSearchHits = useMemo(() => {
|
||
const q = modelQ.trim().toLowerCase();
|
||
if (!q) return [] as { providerId: string; providerName: string; model: ModelInfo }[];
|
||
const hits: { providerId: string; providerName: string; model: ModelInfo }[] = [];
|
||
for (const p of visibleProviders) {
|
||
if (!providerOk(p)) continue;
|
||
for (const m of p.models ?? []) {
|
||
if (m.name.toLowerCase().includes(q) || m.id.toLowerCase().includes(q)) {
|
||
hits.push({ providerId: p.id, providerName: p.name, model: m });
|
||
}
|
||
}
|
||
}
|
||
return hits.slice(0, 100);
|
||
}, [modelQ, visibleProviders]);
|
||
|
||
const inModelList = Boolean(pickerProviderId && !modelQ.trim());
|
||
|
||
// Timer
|
||
useEffect(() => {
|
||
if (!running) return;
|
||
const t0 = Date.now();
|
||
const t = setInterval(() => setElapsed(Math.floor((Date.now() - t0) / 1000)), 1000);
|
||
return () => clearInterval(t);
|
||
}, [running]);
|
||
|
||
// Auto-grow 1–12 lines
|
||
useEffect(() => {
|
||
const ta = taRef.current;
|
||
if (!ta) return;
|
||
ta.style.height = "auto";
|
||
// 12 lines at the current chat font size (line-height follows --chat-fs)
|
||
const lh = parseFloat(getComputedStyle(ta).lineHeight) || 22;
|
||
ta.style.height = Math.min(ta.scrollHeight, Math.round(12 * lh)) + "px";
|
||
}, [text]);
|
||
|
||
const send = useCallback(async (behavior: "queue" | "steer" = "queue") => {
|
||
const id = ui.currentThreadId;
|
||
const typed = text.trim();
|
||
if (!id || (!typed && attachments.length === 0 && mentions.length === 0 && quotes.length === 0)) return;
|
||
// 1.3: quotes go in front of the message as "> " blocks
|
||
const msg = quotesToText(quotes, typed);
|
||
const steer = running && behavior === "steer" && steerOk;
|
||
// 1.3: a message that starts a turn (or steers one) shows in the timeline at once; one queued behind a running
|
||
// turn goes to the queue list instead (it joins the timeline when its turn starts)
|
||
const optimistic = !running || steer;
|
||
const sent = { attachments, mentions, quotes };
|
||
if (running && behavior === "queue") setLastSent((s) => [...s, msg]);
|
||
setText("");
|
||
setAttachments([]);
|
||
setMentions([]);
|
||
setQuotes([]);
|
||
const root = projects.find((p) => p.id === thread?.projectId)?.path;
|
||
try {
|
||
const r = await useTimeline.getState().send(id, {
|
||
text: msg,
|
||
attachments: sent.attachments.map((a) => ({ path: a.path, mime: a.mime, name: a.name })),
|
||
...(sent.mentions.length ? { mentions: mentionsForSend(sent.mentions) } : {}),
|
||
behavior: steer ? "steer" : "queue",
|
||
}, {
|
||
attachments: sent.attachments.map((a) => ({ id: a.path, name: a.name.trim() || a.path, mime: a.mime })),
|
||
...(root && sent.mentions.length ? { mentions: sent.mentions.map((m) => ({ path: joinProjectPath(root, m.rel), isDir: m.isDir })) } : {}),
|
||
}, { optimistic });
|
||
// Say what really happened to a message sent mid-turn (B7: agents without steer queue it)
|
||
if (r?.delivered === "queued" && running) toast(tr("composer.queuedToast"));
|
||
else if (r?.delivered === "steered") toast(tr("composer.steeredToast"));
|
||
if (!running) setLastSent((s) => [...s.slice(-9), msg]);
|
||
} catch (e) {
|
||
// An optimistic bubble stays in the timeline marked failed, with 重试; a queued one goes back into the input
|
||
if (!optimistic) {
|
||
setText(typed); // the quotes are still in their chips
|
||
setAttachments(sent.attachments);
|
||
setMentions(sent.mentions);
|
||
setQuotes(sent.quotes);
|
||
}
|
||
toast(tr("composer.failSend") + errMsg(e), true);
|
||
}
|
||
}, [text, attachments, mentions, quotes, ui.currentThreadId, running, steerOk, projects, thread?.projectId]);
|
||
|
||
const onKey = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||
if (isImeComposing(e.nativeEvent)) return;
|
||
|
||
if (slashOpen && slashPickable.length > 0) {
|
||
if (e.key === "ArrowDown") {
|
||
e.preventDefault();
|
||
setSlashSel((i) => clampSlashIndex(i + 1, slashPickable.length));
|
||
return;
|
||
}
|
||
if (e.key === "ArrowUp") {
|
||
e.preventDefault();
|
||
setSlashSel((i) => clampSlashIndex(i - 1, slashPickable.length));
|
||
return;
|
||
}
|
||
if (e.key === "Escape") {
|
||
e.preventDefault();
|
||
slashSuppress.current = true;
|
||
setSlashSel(0);
|
||
return;
|
||
}
|
||
const pick = slashPickable[slashSel] ?? slashPickable[0];
|
||
const enterSends = e.key === "Enter" && slashEnterSends(text, pick?.name);
|
||
if (((e.key === "Enter" && !e.shiftKey) || e.key === "Tab") && !enterSends) {
|
||
e.preventDefault();
|
||
if (pick) {
|
||
slashSuppress.current = false;
|
||
setText(applySlashPick(pick.name));
|
||
}
|
||
return;
|
||
}
|
||
}
|
||
|
||
const ctrl = e.ctrlKey || e.metaKey;
|
||
const sendCombo = prefs.sendKey === "ctrlEnter"
|
||
? e.key === "Enter" && ctrl
|
||
: e.key === "Enter" && !e.shiftKey && !ctrl;
|
||
if (sendCombo) {
|
||
e.preventDefault();
|
||
if (running && prefs.runningSend === "steer" && steerOk) void send("steer");
|
||
else void send("queue");
|
||
return;
|
||
}
|
||
// With Enter to send: Ctrl+Enter while running still steers (with the "steer" setting the branch above already steers)
|
||
if (prefs.sendKey === "enter" && e.key === "Enter" && ctrl && running) {
|
||
e.preventDefault();
|
||
if (steerOk) void send("steer");
|
||
else void send("queue");
|
||
return;
|
||
}
|
||
if (e.key === "ArrowUp" && text === "" && lastSent.length > 0) {
|
||
e.preventDefault();
|
||
setText(lastSent[lastSent.length - 1]);
|
||
}
|
||
};
|
||
|
||
// Paste: image → upload; text >32KB → .txt attachment (§8.6)
|
||
const onPaste = async (e: React.ClipboardEvent) => {
|
||
const items = e.clipboardData?.items;
|
||
if (!items) return;
|
||
for (const it of items) {
|
||
if (it.type.startsWith("image/")) {
|
||
e.preventDefault();
|
||
const f = it.getAsFile();
|
||
if (f) await upload(f);
|
||
return;
|
||
}
|
||
}
|
||
const t = e.clipboardData.getData("text/plain");
|
||
if (t && t.length > 32 * 1024) {
|
||
e.preventDefault();
|
||
const f = new File([t], "paste.txt", { type: "text/plain" });
|
||
await upload(f);
|
||
}
|
||
};
|
||
|
||
const upload = async (f: File) => {
|
||
const token = getToken();
|
||
const tmp: Attachment = { path: "", name: f.name, mime: f.type || "application/octet-stream", uploading: true };
|
||
setAttachments((a) => [...a, tmp]);
|
||
try {
|
||
const res = await fetch(`/api/upload?name=${encodeURIComponent(f.name)}`, {
|
||
method: "POST", headers: { "x-agent-console": token }, body: f,
|
||
});
|
||
const r = await res.json();
|
||
if (!r.ok) throw new Error(r.e?.message ?? tr("composer.uploadFailed"));
|
||
setAttachments((a) => a.map((x) => (x === tmp ? { ...x, path: r.id, name: r.name ?? f.name, uploading: false } : x)));
|
||
} catch (e) {
|
||
setAttachments((a) => a.filter((x) => x !== tmp));
|
||
toast(tr("composer.failUpload") + errMsg(e), true);
|
||
}
|
||
};
|
||
|
||
const project = projects.find((p) => p.id === thread?.projectId) ?? null;
|
||
const takeMention = (m: MentionDraft | null) => {
|
||
if (!m) return;
|
||
if (!thread || m.projectId !== thread.projectId) {
|
||
toast(tr("mention.otherProject"), true);
|
||
return;
|
||
}
|
||
setMentions((list) => addMention(list, m));
|
||
taRef.current?.focus();
|
||
};
|
||
// Files panel → context menu "Add to chat"
|
||
useEffect(() => {
|
||
const on = (e: Event) => takeMention((e as CustomEvent).detail as MentionDraft);
|
||
window.addEventListener("ac-insert-mention", on);
|
||
return () => window.removeEventListener("ac-insert-mention", on);
|
||
});
|
||
// 1.3: "编辑" on a user message: its text (and its path chips) back in the input, nothing sent
|
||
useEffect(() => {
|
||
const on = (e: Event) => {
|
||
const d = (e as CustomEvent).detail as { text?: string; mentions?: { path: string; isDir: boolean }[] } | undefined;
|
||
if (!d || typeof d.text !== "string") return;
|
||
// Never overwrite a draft (or chips) the user has not sent yet
|
||
if (text.trim() || quotes.length > 0 || mentions.length > 0 || attachments.length > 0) { toast(tr("timeline.editBusy"), true); return; }
|
||
// Quotes the message was sent with come back as quote chips, the rest as text
|
||
const back = quotesFromText(d.text);
|
||
setQuotes(back.quotes);
|
||
setText(back.rest);
|
||
const root = project?.path;
|
||
if (thread && root && d.mentions?.length) {
|
||
let list: MentionDraft[] = [];
|
||
for (const m of d.mentions) {
|
||
const rel = relFromAbs(root, m.path);
|
||
if (rel) list = addMention(list, { projectId: thread.projectId, rel, isDir: m.isDir });
|
||
}
|
||
setMentions(list);
|
||
}
|
||
requestAnimationFrame(() => {
|
||
const ta = taRef.current;
|
||
if (!ta) return;
|
||
ta.focus();
|
||
ta.setSelectionRange(ta.value.length, ta.value.length);
|
||
});
|
||
};
|
||
window.addEventListener("ac-composer-fill", on);
|
||
return () => window.removeEventListener("ac-composer-fill", on);
|
||
});
|
||
// 1.3: "添加到对话" from a text selection
|
||
useEffect(() => {
|
||
const on = (e: Event) => {
|
||
const d = (e as CustomEvent).detail as { text?: string; src?: string } | undefined;
|
||
if (!d?.text) return;
|
||
if (quotes.length >= MAX_QUOTES) { toast(tf("quote.full", { n: MAX_QUOTES }), true); return; }
|
||
setQuotes((list) => addQuote(list, d.text!, d.src));
|
||
taRef.current?.focus();
|
||
};
|
||
window.addEventListener("ac-add-quote", on);
|
||
return () => window.removeEventListener("ac-add-quote", on);
|
||
});
|
||
// A new conversation starts with no chips
|
||
useEffect(() => { setMentions([]); setQuotes([]); }, [ui.currentThreadId]);
|
||
|
||
const onDrop = async (e: React.DragEvent) => {
|
||
e.preventDefault();
|
||
setDropHint(false);
|
||
// A row from our Files panel: a path chip, not an upload. Files from the OS file manager keep the old behaviour.
|
||
if (e.dataTransfer.types.includes(MENTION_MIME)) {
|
||
takeMention(parseMentionDrag(e.dataTransfer.getData(MENTION_MIME)));
|
||
return;
|
||
}
|
||
for (const f of e.dataTransfer.files) {
|
||
if (f.type.startsWith("image/") || f.type.startsWith("text/")) await upload(f);
|
||
}
|
||
};
|
||
|
||
const setMode = (m: Mode) => {
|
||
if (!thread) return;
|
||
void ws.call("thread.setMode", { id: thread.id, mode: m }).catch((e) => toast(errMsg(e), true));
|
||
};
|
||
// 1.3 (#727 review 1): while the mode this Agent really uses is 全部自动同意, a persistent banner + warning-coloured chip
|
||
const autoOn = !!(modes.find((m) => m.id === mode) ?? modes[0])?.auto;
|
||
const canAsk = modes.some((m) => m.id === "default" && !m.auto);
|
||
const setEffort = (effort: string | null) => {
|
||
if (thread) void ws.call("thread.setEffort", { id: thread.id, effort });
|
||
};
|
||
const setCursorOpts = (patch: { cursorFast?: boolean; cursorAutoReview?: boolean }) => {
|
||
if (thread) void ws.call("thread.setCursorOpts", { id: thread.id, ...patch }).catch((e) => toast(errMsg(e), true));
|
||
};
|
||
|
||
// With the default model: some Providers don't mark isDefault → fall back to the first one so the level dropdown appears
|
||
const modelRow = thread?.model
|
||
? provider?.models?.find((m) => m.id === thread.model)
|
||
: provider?.models?.find((m) => m.isDefault) ?? provider?.models?.[0];
|
||
// Defensive: an old daemon may pass Codex's { reasoningEffort, description } objects straight through, and rendering an object would blank the whole page
|
||
const thinkingOpts = (modelRow?.thinkingOptions ?? [])
|
||
.map((o) => {
|
||
const raw = o as unknown as { id?: unknown; label?: unknown; reasoningEffort?: unknown };
|
||
const id = typeof raw.id === "string" ? raw.id : typeof raw.reasoningEffort === "string" ? raw.reasoningEffort : null;
|
||
const idObj = raw.id as { reasoningEffort?: unknown } | null;
|
||
const fixedId = id ?? (idObj && typeof idObj.reasoningEffort === "string" ? idObj.reasoningEffort : null);
|
||
return fixedId ? { id: fixedId, label: typeof raw.label === "string" ? raw.label : fixedId } : null;
|
||
})
|
||
.filter((o): o is { id: string; label: string } => o !== null);
|
||
const defaultThinking = typeof modelRow?.defaultThinking === "string" ? modelRow.defaultThinking : null;
|
||
const showThinking = thinkingOpts.length > 0;
|
||
|
||
const modelLabel = (() => {
|
||
if (!thread?.model) {
|
||
const d = provider?.models?.find((x) => x.isDefault);
|
||
return d ? d.name : tr("composer.defaultModel");
|
||
}
|
||
const m = provider?.models?.find((x) => x.id === thread.model);
|
||
let name = m?.name ?? thread.model;
|
||
if (thread.cursorFast) name += " · Fast";
|
||
return name;
|
||
})();
|
||
|
||
if (!ui.currentThreadId) return null;
|
||
return (
|
||
<div className={`composer${waiting ? " waiting" : ""}`} onDrop={onDrop}
|
||
onDragOver={(e) => {
|
||
e.preventDefault();
|
||
const ours = e.dataTransfer.types.includes(MENTION_MIME);
|
||
if (ours) e.dataTransfer.dropEffect = "copy";
|
||
if (ours !== dropHint) setDropHint(ours);
|
||
}}
|
||
onDragLeave={(e) => { if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setDropHint(false); }}>
|
||
{/* 1.3: the latest turn's step list as a small "N/M tasks" pill; click shows the list in the timeline */}
|
||
{checklist && (
|
||
<div className="cp-tasks">
|
||
<button type="button" className="tasks-pill" title={tr("timeline.tasksPillTitle")}
|
||
onClick={() => window.dispatchEvent(new CustomEvent("ac-show-item", { detail: { key: checklist.key } }))}>
|
||
<Icon name="listChecks" size={12} />
|
||
<span>{tf("timeline.tasksPill", todoProgress(checklist.items))}</span>
|
||
</button>
|
||
</div>
|
||
)}
|
||
{/* queue list */}
|
||
{queue.length > 0 && (
|
||
<div className="q-list">
|
||
{queue.map((q) => (
|
||
<div key={q.id} className="q-item">
|
||
<span className="q-text">{q.text}</span>
|
||
<button className="ibtn" title={tr("common.remove")} onClick={() => void ws.call("thread.queueRemove", { id: thread?.id, queueId: q.id }).catch(() => {})}>
|
||
<Icon name="x" size={12} />
|
||
</button>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
{autoOn && <AutoApproveBanner providerId={thread?.provider} onBack={canAsk ? () => setMode("default") : undefined} />}
|
||
<div className={`cp-card${waiting ? " waiting" : ""}${autoOn ? " auto-on" : ""}${dropHint ? " drop-on" : ""}`}>
|
||
{dropHint && <div className="cp-drop-hint">{tr("mention.drop")}</div>}
|
||
{imagesByPath && attachments.some((a) => a.mime.startsWith("image/")) && (
|
||
<div className="att-note">{tf("composer.imagesByPath", { agent: provider?.name ?? "" })}</div>
|
||
)}
|
||
{quotes.length > 0 && (
|
||
<div className="att-bar q-bar">
|
||
{quotes.map((q) => (
|
||
<span key={q.id} className="qchip" title={`${tr("quote.chipTitle")}\n\n${q.src ? `[${q.src}]\n` : ""}${q.text.slice(0, 600)}`}>
|
||
<Icon name="quote" size={12} />
|
||
<span className="qchip-t">{quoteLabel(q)}</span>
|
||
{q.text.includes("\n") && <span className="qchip-n">{tf("quote.lines", { n: q.text.split("\n").length })}</span>}
|
||
<button type="button" className="att-x" aria-label={tr("quote.remove")} title={tr("quote.remove")}
|
||
onClick={() => setQuotes((list) => list.filter((x) => x.id !== q.id))}>×</button>
|
||
</span>
|
||
))}
|
||
</div>
|
||
)}
|
||
{(attachments.length > 0 || mentions.length > 0) && (
|
||
<div className="att-bar">
|
||
{mentions.map((m) => (
|
||
<MentionChip key={m.rel} path={project ? joinProjectPath(project.path, m.rel) : m.rel} isDir={m.isDir}
|
||
onRemove={() => setMentions((list) => list.filter((x) => x !== m))} />
|
||
))}
|
||
{attachments.map((a, i) => (
|
||
<span key={i} className={`att${a.uploading ? " up" : ""}`}>
|
||
{a.mime.startsWith("image/") && a.path
|
||
? <img src={attachmentUrl(a.path)} alt={a.name} />
|
||
: <Icon name="attach" size={12} />}
|
||
<span className="att-name">{a.name}</span>
|
||
{a.uploading && <span className="att-prog">…</span>}
|
||
<button className="att-x" onClick={() => setAttachments((s) => s.filter((_, j) => j !== i))}>×</button>
|
||
</span>
|
||
))}
|
||
</div>
|
||
)}
|
||
<div className="cp-ta-wrap">
|
||
{slashOpen && (
|
||
<div className="pop agent-picker slash-pop" role="listbox">
|
||
<div className="ap-scroll">
|
||
{slashRows.length === 0 ? (
|
||
<div className="ap-empty">{tr(slashLoading ? "composer.slashLoading" : "composer.slashNoCommands")}</div>
|
||
) : (
|
||
slashRows.map((row) => {
|
||
if (row.type === "heading") {
|
||
return (
|
||
<div key={row.id} className="slash-h">
|
||
{row.labelKey === "slashGroupCommand"
|
||
? tr("composer.slashGroupCommand")
|
||
: tr("composer.slashGroupSkill")}
|
||
</div>
|
||
);
|
||
}
|
||
const idx = slashPickable.indexOf(row.command);
|
||
const sel = idx === slashSel;
|
||
return (
|
||
<button
|
||
key={row.id}
|
||
type="button"
|
||
role="option"
|
||
aria-selected={sel}
|
||
className={`mi slash-item${sel ? " sel" : ""}`}
|
||
onMouseDown={(ev) => ev.preventDefault()}
|
||
onClick={() => {
|
||
slashSuppress.current = false;
|
||
setText(applySlashPick(row.command.name));
|
||
}}
|
||
>
|
||
<span className="slash-name">/{row.command.name}</span>
|
||
<span className="slash-desc">{row.command.description}</span>
|
||
{row.command.argumentHint ? (
|
||
<span className="slash-hint">{row.command.argumentHint}</span>
|
||
) : null}
|
||
</button>
|
||
);
|
||
})
|
||
)}
|
||
</div>
|
||
</div>
|
||
)}
|
||
<textarea
|
||
ref={taRef}
|
||
className="cp-ta"
|
||
rows={1}
|
||
placeholder={waiting ? tr("composer.waitPerm") : running ? tr(steerOk ? "composer.queuePlaceholder" : "composer.queuePlaceholderNoSteer") : tr("composer.inputPlaceholder")}
|
||
value={text}
|
||
disabled={waiting}
|
||
onChange={(e) => {
|
||
slashSuppress.current = false;
|
||
setText(e.target.value);
|
||
}}
|
||
onKeyDown={onKey}
|
||
onPaste={onPaste}
|
||
/>
|
||
</div>
|
||
<div className="cp-bar">
|
||
{/* Agent·model pill */}
|
||
<div className="pill-wrap" ref={pickerRef}>
|
||
<button className="pill chip" onClick={() => openPicker(!agentPop)} title={tr("composer.agentModel")}>
|
||
<Logo size={16} className="chip-logo" />
|
||
<span className="p-a">{provider?.name ?? thread?.provider ?? tr("composer.agent")}</span>
|
||
<span className="p-t">{modelLabel}</span>
|
||
<ChipCaret />
|
||
</button>
|
||
{agentPop && (
|
||
<div className="pop agent-pop agent-picker">
|
||
<div className="ap-search">
|
||
<Icon name="search" size={14} />
|
||
<input
|
||
className="ap-search-in"
|
||
autoFocus
|
||
placeholder={tr("composer.searchAllModels")}
|
||
value={modelQ}
|
||
onChange={(e) => setModelQ(e.target.value)}
|
||
/>
|
||
<button
|
||
type="button"
|
||
className={`ap-refresh${modelsRefreshing ? " spinning" : ""}`}
|
||
title={modelsRefreshing ? tr("composer.refreshingModels") : tr("composer.refreshModels")}
|
||
aria-label={tr("composer.refreshModels")}
|
||
disabled={modelsRefreshing}
|
||
onMouseDown={(ev) => ev.preventDefault()}
|
||
onClick={() => {
|
||
// Inside an Agent's model list: refresh only that provider (avoids waiting on every CLI)
|
||
void refreshModels({
|
||
force: true,
|
||
provider: inModelList ? (pickerProviderId ?? thread?.provider ?? null) : null,
|
||
});
|
||
}}
|
||
>
|
||
<Icon name="refresh" size={14} />
|
||
</button>
|
||
</div>
|
||
|
||
{modelQ.trim() ? (
|
||
<div className="ap-scroll">
|
||
{modelSearchHits.length === 0 && (
|
||
<div className="ap-empty">{tr("composer.noModelMatch")}</div>
|
||
)}
|
||
{modelSearchHits.map(({ providerId, providerName, model }) => {
|
||
const locked = started && providerId !== thread?.provider;
|
||
return (
|
||
<button
|
||
key={`${providerId}:${model.id}`}
|
||
className="mi mi-row"
|
||
disabled={locked}
|
||
onClick={() => void pickModelForProvider(providerId, model.id)}
|
||
>
|
||
<span className="mi-sub">{providerName}</span>
|
||
<span className="mi-name">{model.name}</span>
|
||
{thread?.provider === providerId && thread?.model === model.id && (
|
||
<span className="mi-check">✓</span>
|
||
)}
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
) : inModelList && pickerProvider ? (
|
||
<div className="ap-scroll">
|
||
<button
|
||
type="button"
|
||
className="ap-back mi"
|
||
onClick={() => setPickerProviderId(null)}
|
||
>
|
||
<span className="ap-back-icon"><Icon name="caret" size={14} /></span>
|
||
{pickerProvider.name}
|
||
</button>
|
||
<button
|
||
className="mi model-pick-row"
|
||
title={pickerProvider.models?.find((m) => m.isDefault)?.name ?? tr("composer.defaultModel")}
|
||
onClick={() => void pickModelForProvider(pickerProvider.id, null)}
|
||
>
|
||
<span className="model-pick-name">{tr("composer.defaultModel")}</span>
|
||
{/* which model the default really is (behind magpie = the model magpie applies to this Agent) */}
|
||
{(() => {
|
||
const d = pickerProvider.models?.find((m) => m.isDefault);
|
||
return d ? <span className="mi-sub model-pick-default"> ({d.name})</span> : null;
|
||
})()}
|
||
{thread?.provider === pickerProvider.id && !thread?.model ? <span className="mi-check">✓</span> : ""}
|
||
</button>
|
||
{/* Devin/Cursor have hundreds of models: draw only the first MODEL_LIST_CAP, search for the rest */}
|
||
{/* the default model already appears in the "Default (…)" row above, so it isn't listed again (Copilot used to show "Default (Auto)" + "Auto") */}
|
||
{(pickerProvider.models ?? []).filter((m) => !m.isDefault).slice(0, MODEL_LIST_CAP).map((m) => (
|
||
<button
|
||
key={m.id}
|
||
className="mi model-pick-row"
|
||
title={m.name}
|
||
onClick={() => void pickModelForProvider(pickerProvider.id, m.id)}
|
||
>
|
||
<span className="model-pick-name">{m.name}</span>
|
||
{thread?.provider === pickerProvider.id && thread?.model === m.id
|
||
? <span className="mi-check">✓</span>
|
||
: ""}
|
||
</button>
|
||
))}
|
||
{(pickerProvider.models?.length ?? 0) > MODEL_LIST_CAP && (
|
||
<div className="ap-empty">
|
||
{tf("composer.moreModels", { n: String((pickerProvider.models?.length ?? 0) - MODEL_LIST_CAP) })}
|
||
</div>
|
||
)}
|
||
</div>
|
||
) : (
|
||
<div className="ap-scroll">
|
||
<div className="mlabel">
|
||
{started ? tr("composer.agentLocked") : tr("composer.providers")}
|
||
</div>
|
||
{started && <div className="ap-empty">{tr("composer.agentLockedHint")}</div>}
|
||
{visibleProviders.length === 0 && <div className="ap-empty">{tr("composer.providersLoading")}</div>}
|
||
{visibleProviders.map((p) => {
|
||
const ok = providerOk(p);
|
||
const canOpen = ok && (!started || p.id === thread?.provider);
|
||
const n = p.models?.length ?? 0;
|
||
const label = p.name?.trim() || p.id;
|
||
return (
|
||
<button
|
||
key={p.id}
|
||
className="mi mi-row provider-pick-row"
|
||
disabled={!canOpen}
|
||
onClick={() => {
|
||
if (!canOpen) return;
|
||
setPickerProviderId(p.id);
|
||
}}
|
||
>
|
||
<span className="provider-pick-name">{label}</span>
|
||
<span className="provider-pick-meta">
|
||
{p.id === thread?.provider && <span className="mi-tag">Current</span>}
|
||
{/* option labels in the picker are English throughout */}
|
||
<span className="mi-meta">{n === 1 ? "1 model" : `${n} models`}</span>
|
||
{canOpen && <span className="mi-caret"><Icon name="caret" size={14} /></span>}
|
||
{p.enabled === false && <span className="dlg-hint">Disabled</span>}
|
||
{p.enabled !== false && p.status === "planned" && <span className="dlg-hint">Not available</span>}
|
||
{p.enabled !== false && p.status !== "planned" && !p.available && (
|
||
<span className="dlg-hint">Not detected</span>
|
||
)}
|
||
</span>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
{/* thinking level: icon + short label; the menu is the page's own (SelectMenu), opening upwards */}
|
||
{showThinking && (
|
||
<SelectMenu
|
||
className="pill ipill"
|
||
title={tr("composer.effortTitle")}
|
||
ariaLabel={tr("composer.effortTitle")}
|
||
prefer="up"
|
||
upAnchor=".cp-card"
|
||
value={thread?.effort ?? ""}
|
||
onChange={(v) => setEffort(v || null)}
|
||
options={[
|
||
{
|
||
value: "",
|
||
label: defaultThinking
|
||
? tf("composer.defaultEffortNamed", { level: effortLabel(defaultThinking) })
|
||
: tr("composer.defaultEffort"),
|
||
},
|
||
// an Agent's own "default" level is the same thing as the first entry
|
||
...thinkingOpts.filter((e) => e.id !== "default").map((e) => ({ value: e.id, label: effortLabel(e.id, e.label) })),
|
||
]}
|
||
>
|
||
<Icon name="gauge" size={14} />
|
||
<span className="ip-l">{(() => {
|
||
const cur = thread?.effort ?? defaultThinking;
|
||
if (!cur) return tr("composer.defaultEffort");
|
||
return effortLabel(cur, thinkingOpts.find((o) => o.id === cur)?.label);
|
||
})()}</span>
|
||
<ChipCaret />
|
||
</SelectMenu>
|
||
)}
|
||
{/* permission mode: list only the modes this Agent really has; one mode only (Pi: always auto) = a plain label */}
|
||
{modes.length === 1 && (
|
||
<span className={`pill ipill mode-chip static${modes[0]!.auto ? " auto" : ""}`} title={modes[0]!.desc} data-mode={modes[0]!.id}>
|
||
<Icon name={modes[0]!.auto ? "shieldCheck" : "shield"} size={14} />
|
||
<span className="ip-l">{modes[0]!.label}</span>
|
||
</span>
|
||
)}
|
||
{modes.length > 1 && (() => {
|
||
// The mode stored on the conversation doesn't exist for this Agent → show the one it will actually use
|
||
const shown = modes.find((m) => m.id === mode) ?? modes[0]!;
|
||
return (
|
||
<SelectMenu
|
||
className={`pill ipill mode-chip${shown.danger ? " danger" : ""}${shown.auto ? " auto" : ""}`}
|
||
title={shown.desc}
|
||
ariaLabel={tr("composer.modeTitle")}
|
||
prefer="up"
|
||
upAnchor=".cp-card"
|
||
menuClassName="mode-menu"
|
||
value={shown.id}
|
||
onChange={(v) => setMode(v as Mode)}
|
||
options={modes.map((m) => ({ value: m.id, label: m.label, hint: m.desc, danger: m.danger, warn: m.auto, icon: m.auto ? "shieldCheck" : undefined }))}
|
||
>
|
||
<Icon name={shown.auto ? "shieldCheck" : "shield"} size={14} />
|
||
<span className="ip-l">{shown.label}</span>
|
||
<ChipCaret />
|
||
</SelectMenu>
|
||
);
|
||
})()}
|
||
{/* Cursor speed (like Paseo's Fast toggle) */}
|
||
{thread?.provider === "cursor" && (
|
||
<SelectMenu
|
||
className="pill ipill cursor-fast"
|
||
title="Fast"
|
||
ariaLabel="Fast"
|
||
prefer="up"
|
||
upAnchor=".cp-card"
|
||
value={thread.cursorFast ? "fast" : "off"}
|
||
onChange={(v) => setCursorOpts({ cursorFast: v === "fast" })}
|
||
options={[{ value: "off", label: "Fast: Off" }, { value: "fast", label: "Fast: On" }]}
|
||
>
|
||
<Icon name="zap" size={13} />
|
||
<span className="ip-l">{thread.cursorFast ? "Fast: On" : "Fast: Off"}</span>
|
||
<ChipCaret />
|
||
</SelectMenu>
|
||
)}
|
||
<span className="cp-div" aria-hidden="true" />
|
||
{/* attachments */}
|
||
<button className="ibtn cp-attach" title={tr("composer.attachTitle")} onClick={() => fileRef.current?.click()}>
|
||
<Icon name="attach" />
|
||
</button>
|
||
<input ref={fileRef} type="file" hidden multiple accept="image/*,.txt,.md,.log,.json,.csv"
|
||
onChange={(e) => { for (const f of e.target.files ?? []) void upload(f); e.target.value = ""; }} />
|
||
<div className="cp-right">
|
||
{running ? (
|
||
<>
|
||
<span className="cp-clock">{elapsed}s</span>
|
||
{(text.trim() || attachments.length > 0 || mentions.length > 0 || quotes.length > 0) && (
|
||
<button className="cp-send" title={tr(steerOk && prefs.runningSend === "steer" ? "settings.steer" : "settings.queue")}
|
||
disabled={waiting || attachments.some((a) => a.uploading)}
|
||
onClick={() => void send(prefs.runningSend === "steer" && steerOk ? "steer" : "queue")}>
|
||
<Icon name="arrowUp" />
|
||
</button>
|
||
)}
|
||
<button className="cp-stop" title={tr("common.stop")} onClick={() => void ws.call("thread.interrupt", { id: thread?.id })}>
|
||
<Icon name="stopFill" />
|
||
</button>
|
||
</>
|
||
) : (
|
||
<button className="cp-send" title={tr("common.send")} disabled={waiting || (!text.trim() && attachments.length === 0 && mentions.length === 0 && quotes.length === 0) || attachments.some((a) => a.uploading)}
|
||
onClick={() => void send("queue")}>
|
||
<Icon name="arrowUp" />
|
||
</button>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
{running && !steerOk && prefs.runningSend === "steer" && (
|
||
<div className="cp-hint">{tf("composer.steerUnsupported", { agent: provider?.name ?? thread?.provider ?? "" })}</div>
|
||
)}
|
||
</div>
|
||
);
|
||
});
|
||
|
||
/** The small ⌄ every composer chip ends with (1.3: one chip style for agent·model, effort, permission, Fast) */
|
||
function ChipCaret() {
|
||
return (
|
||
<svg className="chip-caret" width="10" height="10" viewBox="0 0 10 10" aria-hidden="true">
|
||
<path d="M2.5 4l2.5 2.5L7.5 4" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
|
||
</svg>
|
||
);
|
||
}
|