180 lines
6.6 KiB
TypeScript
180 lines
6.6 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 normally skips entries whose
|
|
* `id` is undefined. Shadowing alone would therefore show two "对话" tabs.
|
|
*
|
|
* The preferred way to hide the official row is to clear only its View `id`:
|
|
* the row, its children, store, and disposer stay entirely official, so the
|
|
* official disposer still releases `conversation.chat.flow` and the Chat store
|
|
* if ui-chat tears down while we shadow it. Removing the row would leak those
|
|
* and make the next ui-chat registration throw "slot is already declared".
|
|
*
|
|
* Some Harness builds ignore an undefined View id in the tab projection (or
|
|
* hand back entry objects whose `id` cannot be cleared). For those the seat
|
|
* retries after the id write and, only if the official row is still visible as
|
|
* `id === 'chat'`, removes that row from the live ledger array as a last
|
|
* resort, remembering its index so restore can put it back in place. The
|
|
* fallback is deliberately narrow: it runs only when the primary hide failed.
|
|
*
|
|
* 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;
|
|
/** Original ledger index once the fallback had to remove the row. */
|
|
index?: number;
|
|
}
|
|
|
|
/** 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 });
|
|
try {
|
|
entry.options.id = undefined;
|
|
} catch {
|
|
// Frozen entry options: fall through to the ledger-row fallback.
|
|
}
|
|
changed = true;
|
|
}
|
|
|
|
// Runtime fallback for builds where clearing `options.id` does not remove
|
|
// the official tab. This skips the official disposer's `includes()` check,
|
|
// so it must remain the rare path, never the normal one.
|
|
const rows = ledger.entries('conversation.view') as StoredEntry[];
|
|
for (let index = rows.length - 1; index >= 0; index -= 1) {
|
|
const entry = rows[index];
|
|
if (entry.options.id !== CHAT_VIEW_ID) continue;
|
|
if ((entry.options.priority ?? 0) === CHAT_VIEW_PRIORITY) continue;
|
|
const row = hidden.get(entry);
|
|
if (!row || row.index !== undefined) continue;
|
|
try {
|
|
rows.splice(index, 1);
|
|
// A copy-returning `entries()` silently keeps the row; record the
|
|
// original index only after confirming the live ledger really changed.
|
|
if (!ledger.entries('conversation.view').includes(entry)) {
|
|
row.index = index;
|
|
changed = true;
|
|
}
|
|
} catch {
|
|
// Immutable ledger array: clearing the View id is all this host allows.
|
|
}
|
|
}
|
|
|
|
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);
|
|
let changed = false;
|
|
const present = new Set(ledger.entries('conversation.view'));
|
|
for (const [entry, row] of hidden) {
|
|
if (row.index !== undefined || !present.has(entry)) continue;
|
|
try {
|
|
entry.options.id = row.id;
|
|
changed = true;
|
|
} catch {
|
|
// Frozen options are already the host's own limitation.
|
|
}
|
|
}
|
|
|
|
// Put rows removed by the fallback back at their original ledger index.
|
|
const removed = [...hidden]
|
|
.filter(([, row]) => row.index !== undefined)
|
|
.sort((a, b) => a[1].index! - b[1].index!);
|
|
for (const [entry, row] of removed) {
|
|
const rows = ledger.entries('conversation.view') as StoredEntry[];
|
|
if (rows.includes(entry)) continue;
|
|
try {
|
|
rows.splice(Math.min(row.index!, rows.length), 0, entry);
|
|
if (ledger.entries('conversation.view').includes(entry)) changed = true;
|
|
} catch {
|
|
// Immutable ledger array: nothing to put back.
|
|
}
|
|
}
|
|
|
|
hidden.clear();
|
|
if (changed) notify();
|
|
};
|
|
ACTIVE.set(ledger, restore);
|
|
return restore;
|
|
}
|