feat(webui): search loaded message text in Ctrl+K

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-25 21:03:35 +08:00
1 parent 8af6313c56
commit e821144c7e
22 files changed
+396 -23

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import { projectWebuiThreadMessages } from "./thread-display-projection";
import type { LoadedThreadMessages } from "./thread-message-cache";
import type { UIMessage } from "./types";
// Message objects and arrays are immutable; eviction also releases these weak entries.
const visibleMessages = new WeakMap<UIMessage[], UIMessage[]>();
const foldedContent = new WeakMap<UIMessage, string>();
function excerpt(text: string, folded: string, index: number): string {
if (text.length !== folded.length) {
let offset = 0;
let original = 0;
for (const char of text) {
if (offset >= index) break;
offset += char.toLowerCase().length;
original += char.length;
}
index = original;
}
const start = Math.max(0, index - 48);
const end = Math.min(text.length, Math.max(index + 112, start + 160));
return `${start ? "…" : ""}${text.slice(start, end).replace(/\s+/g, " ").trim()}${end < text.length ? "…" : ""}`;
}
/** Find a literal substring in received user/answer text, never load history. */
export function findLoadedMessagePreview(groups: LoadedThreadMessages, query: string): string | undefined {
for (const group of groups) {
let visible = visibleMessages.get(group);
if (!visible) {
visible = projectWebuiThreadMessages(group);
visibleMessages.set(group, visible);
}
for (let index = visible.length - 1; index >= 0; index--) {
const message = visible[index];
if ((message.role !== "user" && message.role !== "assistant")
|| (message.kind && message.kind !== "message") || message.activityKind) continue;
let folded = foldedContent.get(message);
if (folded === undefined) {
folded = message.content.toLowerCase();
foldedContent.set(message, folded);
}
const match = folded.indexOf(query);
if (match >= 0) return excerpt(message.content, folded, match);
}
}
return undefined;
}
+5
View File
@@ -18,6 +18,11 @@ export class MemoryLruCache<T> {
return entry.value;
}
/** Read without making a background observer retain an otherwise old entry. */
peek(key: string): T | undefined {
return this.entries.get(key)?.value;
}
set(key: string, value: T): void {
this.delete(key);
const bytes = this.estimate(value);
+57
View File
@@ -1,8 +1,15 @@
import type { ThreadTranscriptSnapshot } from "./thread-transcript";
import { MemoryLruCache } from "./memory-lru-cache";
import type { UIMessage } from "./types";
export type LoadedThreadMessages = readonly UIMessage[][];
/** Bounded replay cache; temporary chats remain pinned because they have no disk history. */
export class ThreadMessageCache extends MemoryLruCache<ThreadTranscriptSnapshot> {
private loaded = new Map<string, LoadedThreadMessages>();
private listeners = new Set<() => void>();
private revision = 0;
private updating = false;
constructor(
isPinned: (key: string) => boolean,
@@ -11,4 +18,54 @@ export class ThreadMessageCache extends MemoryLruCache<ThreadTranscriptSnapshot>
) {
super(maxBytes, maxEntries, (key) => !isPinned(key));
}
readonly subscribe = (listener: () => void): (() => void) => {
this.listeners.add(listener);
return () => { this.listeners.delete(listener); };
};
readonly getRevision = (): number => this.revision;
readLoadedMessages(key: string): LoadedThreadMessages {
const mounted = this.loaded.get(key);
if (mounted) return mounted;
const saved = this.peek(key);
return saved ? [saved.projection.messages] : [];
}
setLoadedMessages(key: string, messages: LoadedThreadMessages): void {
this.loaded.set(key, messages);
this.changed();
}
deleteLoadedMessages(key: string): void {
if (this.loaded.delete(key)) this.changed();
}
override set(key: string, value: ThreadTranscriptSnapshot): void {
this.updating = true;
try {
super.set(key, value);
} finally {
this.updating = false;
this.changed();
}
}
override delete(key: string): void {
const existed = this.peek(key) !== undefined;
super.delete(key);
if (existed) this.changed();
}
override clear(): void {
super.clear();
this.changed();
}
private changed(): void {
if (this.updating) return;
this.revision++;
for (const listener of this.listeners) listener();
}
}