web: compose with Enter, share chat titles, reserve the star
Enter inserts a newline and Ctrl/Cmd+Enter sends; slash commands still run on Enter. Chat renames are stored in the server registry so every device sees the same title, with a one-time upload of old local titles. The star no longer covers the row status, and shell frames patch the background bar in place so the composer stays mounted while streaming.
This commit is contained in:
+153
-32
@@ -427,6 +427,32 @@ async function loadChatSessions(force = false) {
|
||||
}
|
||||
writeChatStars(new Set());
|
||||
}
|
||||
// One-time migration from device-local chat titles. The server now owns the
|
||||
// shared title; upload any saved local title, then drop it so other devices
|
||||
// cannot be outvoted by a stale override.
|
||||
const localTitles = { ...(state.chatOverrides?.titles ?? {}) };
|
||||
let titlesChanged = false;
|
||||
for (const [id, title] of Object.entries(localTitles)) {
|
||||
const session = listed.find((entry) => entry.id === id);
|
||||
if (!session) continue;
|
||||
if (String(session.title ?? "").trim() === title) {
|
||||
delete localTitles[id];
|
||||
titlesChanged = true;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const updated = await repo.renameChat(id, title);
|
||||
if (updated) Object.assign(session, updated);
|
||||
delete localTitles[id];
|
||||
titlesChanged = true;
|
||||
} catch {
|
||||
// Keep the local title and retry on a later list load.
|
||||
}
|
||||
}
|
||||
if (titlesChanged) {
|
||||
state.chatOverrides = { ...state.chatOverrides, titles: localTitles };
|
||||
saveChatOverrides();
|
||||
}
|
||||
// Keep this page's own verification chats, which the server intentionally
|
||||
// never returns, at the head of the in-memory list (memory-only; see
|
||||
// chatEphemeralIds). Everything else is the server's list, in its order.
|
||||
@@ -629,24 +655,78 @@ function settleChatThreads() {
|
||||
* to, so this is a no-op in practice until a chat is opened.
|
||||
*/
|
||||
let chatRenderScheduled = false;
|
||||
let chatRenderMode = null;
|
||||
|
||||
/**
|
||||
* Coalesce a burst of pushed frames into one paint. A turn can emit many text
|
||||
* and tool frames in the same animation frame; rebuilding the transcript for
|
||||
* each one made the text flicker and restarted in-flight animations. State is
|
||||
* folded immediately; only the DOM paint is deferred.
|
||||
* and tool frames in the same animation frame; rebuilding the whole page for
|
||||
* each one made the text flicker and replaced the composer, which on a phone
|
||||
* drops the virtual keyboard. Transcript-only paints replace just the message
|
||||
* scroller and leave the composer DOM untouched.
|
||||
*/
|
||||
function scheduleChatRender() {
|
||||
function scheduleChatRender(mode = "full") {
|
||||
if (mode === "full" || chatRenderMode == null) chatRenderMode = mode === "full" ? "full" : chatRenderMode ?? "transcript";
|
||||
if (chatRenderScheduled) return;
|
||||
chatRenderScheduled = true;
|
||||
const paint = () => {
|
||||
chatRenderScheduled = false;
|
||||
const chosen = chatRenderMode;
|
||||
chatRenderMode = null;
|
||||
if (chosen === "transcript" && refreshChatTranscript(state.chatActiveId)) return;
|
||||
renderContent();
|
||||
};
|
||||
if (typeof requestAnimationFrame === "function") requestAnimationFrame(paint);
|
||||
else setTimeout(paint, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repaint only the transcript scroller for a live frame. The composer, its
|
||||
* focused textarea, the chat bar and the background-task bar are left mounted,
|
||||
* so a phone keeps its keyboard open while an answer streams. Returns false
|
||||
* when the page is not in a shape this can update safely; the caller then does
|
||||
* a full render.
|
||||
*/
|
||||
function refreshChatTranscript(sessionId) {
|
||||
if (!sessionId || state.chatActiveId !== sessionId || state.view !== "chat") return false;
|
||||
const oldWrap = document.querySelector(".chat-transcript-wrap");
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
if (!oldWrap || !thread) return false;
|
||||
const rows = transcriptRows(thread.messages ?? []);
|
||||
const notices = thread.notices ?? [];
|
||||
const approval = approvalCard(thread.permission, thread.permissionAnswer);
|
||||
const form =
|
||||
thread.form && thread.form.resolved !== true && thread.form.cancelled !== true
|
||||
? thread.form
|
||||
: null;
|
||||
if (rows.length === 0 && notices.length === 0 && !thread.running && !approval && !form) {
|
||||
return false;
|
||||
}
|
||||
const running = thread.running === true && approval?.actionable !== true;
|
||||
captureChatPosition();
|
||||
// The background-tasks bar sits outside the transcript but is driven by the
|
||||
// same shell frames. Patch it in place instead of full-rendering the pane (and
|
||||
// closing the phone keyboard) when a background command starts or exits.
|
||||
const oldTasks = document.getElementById("chatBackgroundTasks");
|
||||
const nextTasks = renderBackgroundTasks(thread);
|
||||
if (nextTasks) {
|
||||
if (oldTasks) oldTasks.replaceWith(nextTasks);
|
||||
else oldWrap.before(nextTasks);
|
||||
} else if (oldTasks) {
|
||||
oldTasks.remove();
|
||||
}
|
||||
const next = renderChatTranscript(sessionId, rows, {
|
||||
notices,
|
||||
running,
|
||||
now: Date.now(),
|
||||
approval,
|
||||
form,
|
||||
});
|
||||
oldWrap.replaceWith(next);
|
||||
afterChatRender();
|
||||
refreshChatClock();
|
||||
return true;
|
||||
}
|
||||
|
||||
function onChatEvent(frame) {
|
||||
const sessionId = String(frame?.session_id ?? "").trim();
|
||||
if (!sessionId || !state.chatThreads.has(sessionId)) return;
|
||||
@@ -660,7 +740,7 @@ function onChatEvent(frame) {
|
||||
// missed) appears without a manual refresh.
|
||||
const knownTurn = messageId !== "" && (thread.messages ?? []).some((m) => m?.id === messageId);
|
||||
state.chatThreads.set(sessionId, applyChatEvent(thread, frame, Date.now()));
|
||||
scheduleChatRender();
|
||||
scheduleChatRender("transcript");
|
||||
if (!knownTurn) scheduleHistorySync(sessionId, messageId);
|
||||
}
|
||||
|
||||
@@ -892,7 +972,10 @@ async function loadChatHistory(id) {
|
||||
} catch (error) {
|
||||
setThread(id, { loading: false, loaded: true, error: describeError(error) });
|
||||
}
|
||||
renderContent();
|
||||
// A streaming turn triggers this resync after its first unseen message. Keep
|
||||
// the composer mounted when the transcript already exists; replacing the whole
|
||||
// page here closed the phone keyboard mid-turn.
|
||||
if (!refreshChatTranscript(id)) renderContent();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -916,7 +999,7 @@ async function loadOlderChatHistory(id) {
|
||||
} catch (error) {
|
||||
setThread(id, { loading: false, error: describeError(error) });
|
||||
}
|
||||
renderContent();
|
||||
if (!refreshChatTranscript(id)) renderContent();
|
||||
}
|
||||
|
||||
/** Open the Close chat? confirm for [session] (the mutation only on confirm). */
|
||||
@@ -1057,31 +1140,39 @@ function dismissChatRename() {
|
||||
renderContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a local rename, persist it and reflect it at once: the row shows the
|
||||
* local title on the next render (and every later refresh), and an open
|
||||
* conversation's own title follows. Nothing is sent to the server — the
|
||||
* brokered protocol has no chat rename.
|
||||
*/
|
||||
function applyLocalChatRename(sessionId, title) {
|
||||
const next = renameChatOverride(state.chatOverrides, sessionId, title);
|
||||
if (next === state.chatOverrides) return false;
|
||||
state.chatOverrides = next;
|
||||
/** Drop this device's old local title now that the server carries it. */
|
||||
function clearLocalChatTitle(sessionId) {
|
||||
const id = String(sessionId ?? "");
|
||||
const titles = { ...(state.chatOverrides?.titles ?? {}) };
|
||||
if (!(id in titles)) return;
|
||||
delete titles[id];
|
||||
state.chatOverrides = { ...state.chatOverrides, titles };
|
||||
saveChatOverrides();
|
||||
const local = next.titles[String(sessionId)];
|
||||
if (local != null) setThread(sessionId, { title: local });
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The dialog's Save: trim, record the override, close the dialog and re-render.
|
||||
* A blank title is ignored (Save is already disabled while trimmed-blank).
|
||||
* The dialog's Save: trim, write the shared title through `chat_rename`, close
|
||||
* the dialog and re-render. The server's registry title is authoritative; a
|
||||
* failure leaves the dialog open and surfaces the server's reason. A blank title
|
||||
* is ignored (Save is already disabled while trimmed-blank).
|
||||
*/
|
||||
function acceptChatRename(session, title) {
|
||||
async function acceptChatRename(session, title) {
|
||||
if (state.chatBusy) return;
|
||||
const trimmed = String(title ?? "").trim();
|
||||
if (trimmed === "") return;
|
||||
applyLocalChatRename(session.id, trimmed);
|
||||
state.chatRename = null;
|
||||
state.chatBusy = true;
|
||||
try {
|
||||
const updated = await repo.renameChat(session.id, trimmed);
|
||||
if (updated) {
|
||||
applyChatSessionUpdate(session.id, updated, updated.closed === true);
|
||||
clearLocalChatTitle(session.id);
|
||||
setThread(session.id, { title: updated.title ?? trimmed });
|
||||
}
|
||||
state.chatRename = null;
|
||||
} catch (error) {
|
||||
state.actionError = describeError(error);
|
||||
}
|
||||
state.chatBusy = false;
|
||||
renderContent();
|
||||
}
|
||||
|
||||
@@ -1183,6 +1274,32 @@ function releaseAttachments(attachments) {
|
||||
* the rest of the frame fold; this only keeps the composer honest while C5 is
|
||||
* still to land.
|
||||
*/
|
||||
/**
|
||||
* Update the composer's primary control and queued notice in place when only
|
||||
* the running/queued flags changed. The textarea and its listeners stay mounted,
|
||||
* so a phone keeps the keyboard open across the start and end of a turn.
|
||||
*/
|
||||
function refreshChatComposerState(sessionId) {
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
if (!thread || state.chatActiveId !== sessionId) return;
|
||||
const primary = document.querySelector("#chatComposer .chat-composer-primary");
|
||||
if (primary) {
|
||||
const session = state.chatSessions.find((row) => row.id === sessionId) ?? null;
|
||||
fillComposerPrimary(primary, thread, {
|
||||
onSend: () => submitChatComposer(sessionId, session, { selected: 0 }),
|
||||
onStop: () => stopChatTurn(sessionId),
|
||||
});
|
||||
}
|
||||
const composer = document.getElementById("chatComposer");
|
||||
if (!composer) return;
|
||||
const existing = document.getElementById("chatQueued");
|
||||
if (thread.queued && !existing) {
|
||||
composer.prepend(h("div", { class: "chat-queued", id: "chatQueued", text: CHAT_QUEUED_NOTICE }));
|
||||
} else if (!thread.queued && existing) {
|
||||
existing.remove();
|
||||
}
|
||||
}
|
||||
|
||||
function onChatFrame(message) {
|
||||
const frame = message?.chat_event;
|
||||
if (!frame || !frame.session_id) return;
|
||||
@@ -1194,7 +1311,10 @@ function onChatFrame(message) {
|
||||
const running = runningAfterFrame(thread.running, frame);
|
||||
if (queued === thread.queued && running === thread.running) return;
|
||||
setThread(sessionId, { queued, running });
|
||||
if (sessionId === state.chatActiveId) scheduleChatRender();
|
||||
if (sessionId === state.chatActiveId) {
|
||||
refreshChatComposerState(sessionId);
|
||||
scheduleChatRender("transcript");
|
||||
}
|
||||
}
|
||||
|
||||
/** Add one local command line to a conversation's transcript. */
|
||||
@@ -1242,11 +1362,9 @@ function onComposerInput(event, sessionId, session, controls) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The composer's key handling. While the offer list is showing, Up/Down move the
|
||||
* highlight and Enter runs the highlighted command (mirrors ChatComposer's
|
||||
* preview handler). Enter otherwise submits — which is inert while a turn is
|
||||
* running unless the palette or a slash draft is present (see composerSubmit);
|
||||
* Shift+Enter is left to insert a newline.
|
||||
* The composer's key handling. Enter inserts a newline (so a phone keyboard can
|
||||
* compose multi-line text); Ctrl/Cmd+Enter sends. A slash draft still runs on
|
||||
* Enter, matching the command palette. Up/Down move the highlighted command.
|
||||
*/
|
||||
function onComposerKeydown(event, sessionId, session, controls) {
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
@@ -1259,7 +1377,9 @@ function onComposerKeydown(event, sessionId, session, controls) {
|
||||
refreshComposerControls(sessionId, session, controls);
|
||||
return;
|
||||
}
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
if (event.key !== "Enter") return;
|
||||
const slash = String(thread.draft ?? "").trimStart().startsWith("/");
|
||||
if (slash || event.metaKey || event.ctrlKey) {
|
||||
event.preventDefault();
|
||||
submitChatComposer(sessionId, session, controls);
|
||||
}
|
||||
@@ -3190,6 +3310,7 @@ function renderChatComposer(thread, session, sessionId) {
|
||||
rows: "1",
|
||||
placeholder: CHAT_COMPOSER_PLACEHOLDER,
|
||||
"aria-label": CHAT_COMPOSER_PLACEHOLDER,
|
||||
title: "Enter adds a new line · Ctrl/Cmd+Enter sends",
|
||||
spellcheck: "false",
|
||||
});
|
||||
input.value = thread.draft ?? "";
|
||||
|
||||
Reference in New Issue
Block a user