dsh-better-chat/src/client/chat-view-seat.ts

125 lines
4.4 KiB
TypeScript

/**
* Chat-view tab ownership for the reader-in-chat layout.
*
* The reader used to live in its own "阅读" View tab. It now occupies the
* built-in "对话" View id instead, so the transcript area draws the reader
* while the composer, header, approvals, and scroll port stay official.
*
* `conversation.view` is a list Slot: a registration with the same `id` at a
* lower priority wins the cell for rendering. The tab row, however, is derived
* from the raw entry ledger (`slots.entries`), and it skips entries whose
* `id` is undefined. Shadowing alone would therefore show two "对话" tabs.
*
* The official ChatView row must stay on the ledger. Its disposer only runs
* `releaseEntry` (store-handle count and declared child slots such as
* `conversation.chat.flow`) while the row is still in `entries`; removing the
* row would leak those if ui-chat tears down while we shadow it, and the next
* ui-chat registration would throw "slot is already declared". So instead of
* touching the lifecycle, this seat clears only the row's View `id` — the one
* field the tab projection reads — and puts it back on teardown. The row,
* its children, store, and disposer remain entirely official.
*
* A subscription re-hides a ChatView row that ui-chat re-registers while the
* plugin stays mounted. Every visibility change bumps the ledger version with
* a throwaway probe registration (never selected) so View subscribers such as
* the header recompute their tab list.
*/
import type { StoredEntry } from '@deepseek-ai/dsh-client-ui-slots';
export const CHAT_VIEW_ID = 'chat';
/** Lower than the official ChatView registration (priority 0) so it shadows. */
export const CHAT_VIEW_PRIORITY = -10;
const PROBE_ID = 'dsh-better-chat:chat-view-ledger-probe';
/** The registry operations this seat needs; documented public surface. */
export interface ViewLedger {
entries(name: string): readonly StoredEntry[];
register(options: {
name: string;
id: string;
priority?: number;
order?: number;
label?: () => string;
}, component: unknown): () => void;
subscribe(name: string, listener: () => void): () => void;
}
interface HiddenRow {
/** The original View id, restored on teardown. */
readonly id: string;
}
/** One active retirement per View ledger (`ctx.slots` is a single shared object). */
const ACTIVE = new WeakMap<ViewLedger, () => void>();
/**
* Hide the official ChatView row from the View tab projection by clearing its
* View `id`; the row itself stays live and keeps its official disposer.
* @param ledger - Slot registry surfaced through `ctx.slots`.
* @returns idempotent restore for plugin teardown.
*/
export function retireHostedChatView(ledger: ViewLedger): () => void {
if (ACTIVE.has(ledger)) return () => {};
const hidden = new Map<StoredEntry, HiddenRow>();
let live = true;
let notifying = false;
// Splicing ids does not bump the slot version, so View subscribers would
// keep a stale tab list. A throwaway registration both notifies and is gone
// before paint: its id is never the selected View.
const notify = (): void => {
if (notifying) return;
notifying = true;
try {
const dispose = ledger.register({
name: 'conversation.view',
id: PROBE_ID,
priority: CHAT_VIEW_PRIORITY,
order: 0,
label: () => '',
}, () => null);
dispose();
} finally {
notifying = false;
}
};
const hide = (): void => {
let changed = false;
for (const entry of ledger.entries('conversation.view')) {
if (entry.options.id !== CHAT_VIEW_ID) continue;
if ((entry.options.priority ?? 0) === CHAT_VIEW_PRIORITY) continue;
if (hidden.has(entry)) continue;
hidden.set(entry, { id: CHAT_VIEW_ID });
entry.options.id = undefined;
changed = true;
}
if (changed) notify();
};
// Registration-time callback; a later ui-chat registration also lands here.
const unsubscribe = ledger.subscribe('conversation.view', () => {
if (live) hide();
});
hide();
const restore = (): void => {
if (!live) return;
live = false;
unsubscribe();
ACTIVE.delete(ledger);
const present = new Set(ledger.entries('conversation.view'));
let changed = false;
for (const [entry, row] of hidden) {
if (!present.has(entry)) continue;
entry.options.id = row.id;
changed = true;
}
hidden.clear();
if (changed) notify();
};
ACTIVE.set(ledger, restore);
return restore;
}