2026-10-18 14:57:23 +08:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
import { useClient } from "@/providers/ClientProvider";
|
|
|
|
|
import { fetchPromptIndex, fetchWebuiThread } from "@/lib/api";
|
|
|
|
|
import { projectThreadEvents } from "@/lib/thread-event-projection";
|
2026-11-14 22:52:44 +08:00
|
|
|
import { historyContentSnapshot, projectWebuiThreadMessages, sameHistoryContent } from "@/lib/thread-display-projection";
|
2026-11-11 17:32:47 +08:00
|
|
|
import { joinThreadFragments } from "@/lib/thread-page-fragments";
|
2026-10-18 14:57:23 +08:00
|
|
|
import { isModelCommandText } from "@/lib/format";
|
|
|
|
|
import { isSystemCommandTurnId } from "@/lib/nanobot-client";
|
2026-11-14 22:52:44 +08:00
|
|
|
import type { PromptIndex, ThreadProjectionEvent, UIMessage, WebuiThreadPersistedPayload } from "@/lib/types";
|
2026-10-18 14:57:23 +08:00
|
|
|
import { type PromptAnchor, userPromptAnchors } from "@/components/thread/promptNavigation";
|
|
|
|
|
|
|
|
|
|
interface HistoryPage {
|
|
|
|
|
messages: UIMessage[];
|
|
|
|
|
body: WebuiThreadPersistedPayload;
|
2026-11-13 19:16:03 +08:00
|
|
|
details: Map<string, UIMessage[]>;
|
2026-11-14 22:52:44 +08:00
|
|
|
/** One terminal record per raw turn, retained when its page leaves the bounded cache. */
|
|
|
|
|
completion?: Extract<ThreadProjectionEvent, { event: "turn_end" }>;
|
|
|
|
|
/** A crossed marker can precede all visible rows until an older page arrives. */
|
|
|
|
|
forkBoundaryBefore?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function projectionBody(page: HistoryPage): WebuiThreadPersistedPayload {
|
|
|
|
|
return page.forkBoundaryBefore ? { ...page.body, fork_boundary_event_index: 0 } : page.body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function retainFragmentCompletion(pages: HistoryPage[], page: HistoryPage): HistoryPage[] {
|
|
|
|
|
const turn = page.body.page?.record_range?.turn;
|
|
|
|
|
if (turn === undefined || !page.completion
|
|
|
|
|
|| !pages.some((saved) => saved.body.page?.record_range?.turn === turn && saved.completion !== page.completion)) return pages;
|
|
|
|
|
return pages.map((saved) => saved.body.page?.record_range?.turn === turn && saved.completion !== page.completion
|
|
|
|
|
? { ...saved, completion: page.completion } : saved);
|
2026-11-13 19:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function expandDetails(messages: UIMessage[], details: ReadonlyMap<string, UIMessage[]>): UIMessage[] {
|
|
|
|
|
return messages.flatMap((message) => {
|
|
|
|
|
const rows = message.traceDetail && details.get(message.traceDetail.ref);
|
|
|
|
|
return rows ? rows.map((row) => ({ ...row, activitySegmentId: message.activitySegmentId })) : [message];
|
|
|
|
|
});
|
2026-10-18 14:57:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-21 13:10:05 +08:00
|
|
|
const HISTORY_PAGE_LIMIT = 3;
|
|
|
|
|
|
2026-10-18 14:57:23 +08:00
|
|
|
export interface PromptHistoryNavigation {
|
|
|
|
|
prompts: PromptAnchor[];
|
|
|
|
|
window: {
|
|
|
|
|
messages: UIMessage[];
|
|
|
|
|
userMessageOffset: number;
|
|
|
|
|
hasMoreBefore: boolean;
|
|
|
|
|
hasMoreAfter: boolean;
|
|
|
|
|
forkBoundaryMessageCount: number | null;
|
|
|
|
|
} | null;
|
|
|
|
|
target: { id: string; sequence: number } | null;
|
2026-10-21 13:10:05 +08:00
|
|
|
/** Pending or failed page read; index errors have no page operation. */
|
|
|
|
|
operation: "jump" | "older" | "newer" | null;
|
2026-10-18 14:57:23 +08:00
|
|
|
loading: boolean;
|
|
|
|
|
error: string | null;
|
|
|
|
|
jump: (id: string) => void;
|
|
|
|
|
older: () => void;
|
|
|
|
|
newer: () => void;
|
2026-10-18 17:02:40 +08:00
|
|
|
cancel: () => void;
|
2026-10-18 14:57:23 +08:00
|
|
|
latest: () => void;
|
|
|
|
|
retry: () => void;
|
2026-11-13 19:16:03 +08:00
|
|
|
expandTraceDetail: (ref: string, rows: UIMessage[]) => void;
|
2026-10-18 14:57:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-14 22:52:44 +08:00
|
|
|
// Pages and detail maps are replaced, never mutated. Cache by the immutable raw window,
|
|
|
|
|
// so admission and the rendered window share one projection without retaining evicted pages.
|
|
|
|
|
const projectedWindows = new WeakMap<HistoryPage[], NonNullable<PromptHistoryNavigation["window"]>>();
|
|
|
|
|
|
|
|
|
|
function projectHistoryWindow(pages: HistoryPage[]): NonNullable<PromptHistoryNavigation["window"]> {
|
|
|
|
|
const cached = projectedWindows.get(pages);
|
|
|
|
|
if (cached) return cached;
|
|
|
|
|
const messages: UIMessage[] = [];
|
|
|
|
|
let boundary: number | null = null;
|
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
|
const page = pages[index];
|
|
|
|
|
let body = projectionBody(page);
|
|
|
|
|
let rows = page.messages;
|
|
|
|
|
const range = body.page?.record_range;
|
|
|
|
|
if (range) {
|
|
|
|
|
let joined = joinThreadFragments([], body);
|
|
|
|
|
const details = new Map(page.details);
|
|
|
|
|
let completion = page.completion;
|
|
|
|
|
while (pages[index + 1]?.body.page?.record_range?.turn === range.turn) {
|
|
|
|
|
const next = pages[++index];
|
|
|
|
|
joined = joinThreadFragments(joined.pages, projectionBody(next));
|
|
|
|
|
completion = next.completion ?? completion;
|
|
|
|
|
for (const [ref, detail] of next.details) details.set(ref, detail);
|
|
|
|
|
}
|
|
|
|
|
body = joined.body;
|
|
|
|
|
// Completion is data, not a content-ready signal. Replay it through the same
|
|
|
|
|
// reducer even when intervening telemetry or the terminal page was evicted.
|
|
|
|
|
const events = completion && !body.events.some((event) => event.event === "turn_end")
|
|
|
|
|
? [...body.events, completion] : body.events;
|
|
|
|
|
rows = expandDetails(projectWebuiThreadMessages(projectThreadEvents(events)), details);
|
|
|
|
|
}
|
|
|
|
|
const eventIndex = body.fork_boundary_event_index;
|
|
|
|
|
if (eventIndex !== undefined) boundary = messages.length + projectWebuiThreadMessages(projectThreadEvents(body.events.slice(0, eventIndex))).length;
|
|
|
|
|
messages.push(...rows);
|
|
|
|
|
}
|
|
|
|
|
const window = { messages, userMessageOffset: pages[0]?.body.page?.user_message_offset ?? 0,
|
|
|
|
|
hasMoreBefore: pages[0]?.body.page?.has_more_before === true,
|
|
|
|
|
hasMoreAfter: pages.at(-1)?.body.page?.has_more_after === true,
|
|
|
|
|
forkBoundaryMessageCount: boundary };
|
|
|
|
|
projectedWindows.set(pages, window);
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 14:57:23 +08:00
|
|
|
/** Browsing pages never replace the canonical latest page or live turn state. */
|
|
|
|
|
export function usePromptHistory(key: string | null, version: number, messages: UIMessage[]): PromptHistoryNavigation {
|
|
|
|
|
const { client, getToken, webuiCapabilities } = useClient();
|
|
|
|
|
const enabled = !!key && webuiCapabilities.includes("webui.prompt-navigation.v1");
|
|
|
|
|
const [index, setIndex] = useState<{ key: string; prompts: PromptAnchor[] } | null>(null);
|
2026-10-18 15:12:02 +08:00
|
|
|
const indexSnapshot = useRef<PromptIndex>();
|
2026-10-18 14:57:23 +08:00
|
|
|
const [pages, setPages] = useState<HistoryPage[]>([]);
|
2026-11-11 17:32:47 +08:00
|
|
|
const pagesRef = useRef(pages);
|
2026-11-14 22:52:44 +08:00
|
|
|
const publishPages = useCallback((next: HistoryPage[]) => {
|
|
|
|
|
// Reads and detail expansion share the latest window even within one React batch.
|
|
|
|
|
pagesRef.current = next;
|
|
|
|
|
setPages(next);
|
|
|
|
|
}, []);
|
2026-10-18 14:57:23 +08:00
|
|
|
const [target, setTarget] = useState<PromptHistoryNavigation["target"]>(null);
|
2026-10-21 13:10:05 +08:00
|
|
|
const [operation, setOperation] = useState<PromptHistoryNavigation["operation"]>(null);
|
2026-10-18 14:57:23 +08:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const [indexError, setIndexError] = useState<string | null>(null);
|
|
|
|
|
const [refresh, setRefresh] = useState(0);
|
|
|
|
|
const request = useRef<AbortController | null>(null);
|
|
|
|
|
const sequence = useRef(0);
|
2026-11-14 22:52:44 +08:00
|
|
|
const retry = useRef<(() => void) | null>(null);
|
2026-10-18 14:57:23 +08:00
|
|
|
const scope = useRef({ key, client });
|
|
|
|
|
const currentScope = scope.current.key === key && scope.current.client === client;
|
|
|
|
|
|
2026-10-18 17:02:40 +08:00
|
|
|
const cancel = useCallback(() => {
|
2026-10-18 14:57:23 +08:00
|
|
|
request.current?.abort();
|
|
|
|
|
request.current = null;
|
2026-11-14 22:52:44 +08:00
|
|
|
retry.current = null;
|
2026-10-18 14:57:23 +08:00
|
|
|
setTarget(null);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
setError(null);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-10-18 17:02:40 +08:00
|
|
|
const latest = useCallback(() => {
|
|
|
|
|
cancel();
|
2026-11-14 22:52:44 +08:00
|
|
|
publishPages([]);
|
|
|
|
|
}, [cancel, publishPages]);
|
2026-10-18 17:02:40 +08:00
|
|
|
|
2026-10-18 14:57:23 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
scope.current = { key, client };
|
|
|
|
|
latest();
|
|
|
|
|
setIndex(null);
|
2026-10-18 15:12:02 +08:00
|
|
|
indexSnapshot.current = undefined;
|
2026-10-18 14:57:23 +08:00
|
|
|
setIndexError(null);
|
|
|
|
|
return () => request.current?.abort();
|
|
|
|
|
}, [key, client, enabled, latest]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!enabled || !key) return;
|
|
|
|
|
const controller = new AbortController();
|
2026-10-18 15:12:02 +08:00
|
|
|
void fetchPromptIndex(getToken(), key, controller.signal, indexSnapshot.current).then((snapshot) => {
|
2026-10-18 14:57:23 +08:00
|
|
|
if (controller.signal.aborted) return;
|
2026-10-18 15:12:02 +08:00
|
|
|
if (snapshot === indexSnapshot.current) { setIndexError(null); return; }
|
|
|
|
|
indexSnapshot.current = snapshot;
|
|
|
|
|
setIndex({ key, prompts: snapshot.prompts.filter((item) => !isSystemCommandTurnId(item.turnId) && !isModelCommandText(item.preview)).map((item, position) => ({ ...item, index: position,
|
2026-10-18 14:57:23 +08:00
|
|
|
label: item.preview.replace(/\s+/g, " ").slice(0, 80) || `Prompt ${position + 1}`,
|
|
|
|
|
})) });
|
|
|
|
|
setIndexError(null);
|
|
|
|
|
}).catch((cause: unknown) => {
|
|
|
|
|
if (controller.signal.aborted) return;
|
|
|
|
|
setIndexError(cause instanceof Error ? cause.message : String(cause));
|
|
|
|
|
});
|
|
|
|
|
return () => controller.abort();
|
|
|
|
|
}, [client, enabled, getToken, key, version, refresh]);
|
|
|
|
|
|
2026-10-19 11:31:19 +08:00
|
|
|
const indexed = useMemo(() => {
|
|
|
|
|
const ids = new Set<string>();
|
2026-11-11 10:46:44 +08:00
|
|
|
const inputIds = new Set<string>();
|
2026-10-19 11:31:19 +08:00
|
|
|
const turns = new Map<string, PromptAnchor | null>();
|
|
|
|
|
for (const prompt of index?.prompts ?? []) {
|
|
|
|
|
ids.add(prompt.id);
|
2026-11-11 10:46:44 +08:00
|
|
|
if (prompt.inputId) inputIds.add(prompt.inputId);
|
2026-10-19 11:31:19 +08:00
|
|
|
if (prompt.turnId) turns.set(prompt.turnId, turns.has(prompt.turnId) ? null : prompt);
|
|
|
|
|
}
|
2026-11-11 10:46:44 +08:00
|
|
|
return { ids, inputIds, turns };
|
2026-10-19 11:31:19 +08:00
|
|
|
}, [index]);
|
2026-11-11 10:46:44 +08:00
|
|
|
const localCache = useRef<{ index: typeof index; users: UIMessage[]; responses: string; prompts: PromptAnchor[] }>();
|
2026-10-18 14:57:23 +08:00
|
|
|
const prompts = useMemo(() => {
|
|
|
|
|
if (!enabled) return userPromptAnchors(messages);
|
|
|
|
|
if (!currentScope || index?.key !== key) return [];
|
2026-10-19 11:31:19 +08:00
|
|
|
// Live rows can still have optimistic render ids after admission. Match the
|
|
|
|
|
// host's unambiguous original transport id and content, not text alone.
|
|
|
|
|
const users = messages.filter((message) => {
|
2026-11-11 10:46:44 +08:00
|
|
|
if (message.role !== "user" || indexed.ids.has(message.id)
|
|
|
|
|
|| (message.inputId && indexed.inputIds.has(message.inputId))) return false;
|
2026-10-19 11:31:19 +08:00
|
|
|
const admitted = message.turnId ? indexed.turns.get(message.turnId) : null;
|
|
|
|
|
return !admitted || admitted.preview !== message.content.trim().slice(0, 320);
|
|
|
|
|
});
|
2026-10-18 15:12:02 +08:00
|
|
|
if (!users.length) return index.prompts;
|
2026-11-11 10:46:44 +08:00
|
|
|
// Refresh consumed-input previews at response boundaries, not on every token.
|
|
|
|
|
const responses = JSON.stringify(messages.filter((message) => message.responseInputIds !== undefined)
|
|
|
|
|
.map((message) => [message.id, message.responseInputIds, message.isStreaming ? null : message.content.slice(0, 240)]));
|
2026-10-18 15:12:02 +08:00
|
|
|
const cached = localCache.current;
|
2026-11-11 10:46:44 +08:00
|
|
|
if (cached?.index === index && cached.responses === responses && users.length === cached.users.length
|
2026-10-18 15:12:02 +08:00
|
|
|
&& users.every((user, position) => user === cached.users[position])) return cached.prompts;
|
2026-10-19 11:31:19 +08:00
|
|
|
const localIds = new Set(users.map((message) => message.id));
|
|
|
|
|
const local = userPromptAnchors(messages).filter((prompt) => localIds.has(prompt.id));
|
|
|
|
|
// Directory order is logical admission order. Unknown legacy times are zero,
|
|
|
|
|
// not a reason to move an old prompt ahead of its preceding inputs.
|
|
|
|
|
const merged = [...index.prompts, ...local].map((prompt, position) => ({ ...prompt, index: position }));
|
2026-11-11 10:46:44 +08:00
|
|
|
localCache.current = { index, users, responses, prompts: merged };
|
2026-10-18 15:12:02 +08:00
|
|
|
return merged;
|
2026-10-19 11:31:19 +08:00
|
|
|
}, [currentScope, enabled, index, indexed, key, messages]);
|
2026-10-18 14:57:23 +08:00
|
|
|
|
2026-11-13 19:16:03 +08:00
|
|
|
const expandTraceDetail = useCallback((ref: string, rows: UIMessage[]) => {
|
2026-11-14 22:52:44 +08:00
|
|
|
publishPages(pagesRef.current.map((page) => {
|
2026-11-13 19:16:03 +08:00
|
|
|
if (!page.messages.some((message) => message.traceDetail?.ref === ref)) return page;
|
|
|
|
|
const details = new Map(page.details).set(ref, rows);
|
|
|
|
|
return { ...page, messages: expandDetails(page.messages, details), details };
|
|
|
|
|
}));
|
2026-11-14 22:52:44 +08:00
|
|
|
}, [publishPages]);
|
2026-10-18 14:57:23 +08:00
|
|
|
|
2026-11-14 22:52:44 +08:00
|
|
|
const read = useCallback(async (cursor: string, direction: "before" | "after", id?: string, resume?: HistoryPage[]) => {
|
2026-10-21 13:10:05 +08:00
|
|
|
// Boundary inputs can arrive before loading renders; only a new jump supersedes a read.
|
|
|
|
|
if (!key || (!id && request.current)) return;
|
2026-10-18 14:57:23 +08:00
|
|
|
request.current?.abort();
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
request.current = controller;
|
2026-10-21 13:10:05 +08:00
|
|
|
setOperation(id ? "jump" : direction === "before" ? "older" : "newer");
|
2026-10-18 14:57:23 +08:00
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
2026-11-14 22:52:44 +08:00
|
|
|
retry.current = null;
|
|
|
|
|
let staged = resume ?? pagesRef.current;
|
|
|
|
|
let retryRead: (() => void) | null = null;
|
2026-10-18 14:57:23 +08:00
|
|
|
try {
|
2026-11-14 22:52:44 +08:00
|
|
|
while (true) {
|
|
|
|
|
const requestedCursor = cursor;
|
|
|
|
|
const retryPages = staged;
|
|
|
|
|
retryRead = () => { void read(requestedCursor, direction, id, retryPages); };
|
|
|
|
|
const body = await fetchWebuiThread(getToken(), key, {
|
|
|
|
|
[direction]: cursor, limit: 24, signal: controller.signal,
|
|
|
|
|
});
|
|
|
|
|
if (controller.signal.aborted) return;
|
|
|
|
|
if (!body) throw new Error("History is no longer available");
|
|
|
|
|
const generation = body.page?.history_generation;
|
|
|
|
|
const previousGeneration = staged[0]?.body.page?.history_generation;
|
|
|
|
|
if (!id && generation && previousGeneration && generation !== previousGeneration) {
|
|
|
|
|
throw new Error("History changed; select the message again");
|
|
|
|
|
}
|
|
|
|
|
const turn = body.page?.record_range?.turn;
|
|
|
|
|
const page: HistoryPage = { body, messages: projectWebuiThreadMessages(projectThreadEvents(body.events)), details: new Map(),
|
|
|
|
|
completion: body.events.find((event) => event.event === "turn_end")
|
|
|
|
|
?? (!id && turn !== undefined ? staged.find((saved) => saved.body.page?.record_range?.turn === turn)?.completion : undefined) };
|
|
|
|
|
if (id && !page.messages.some((message) => message.id === id)) {
|
|
|
|
|
setRefresh((value) => value + 1);
|
|
|
|
|
throw new Error("History changed; select the message again");
|
|
|
|
|
}
|
|
|
|
|
// Trace details can finish while this read is pending; preserve those visible updates.
|
|
|
|
|
const updated = staged.map((saved) => pagesRef.current.find((current) => current.body === saved.body) ?? saved);
|
|
|
|
|
if (updated.some((saved, index) => saved !== staged[index])) staged = updated;
|
|
|
|
|
if (!id && page.completion) {
|
|
|
|
|
staged = retainFragmentCompletion(staged, page);
|
|
|
|
|
const retained = retainFragmentCompletion(pagesRef.current, page);
|
|
|
|
|
if (retained.some((saved, index) => saved !== pagesRef.current[index])) {
|
|
|
|
|
publishPages(retained);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Only fragments need the staged raw context. Self-contained pages extend
|
|
|
|
|
// the readable window, not the bookkeeping pages traversed on the way here.
|
|
|
|
|
const source = body.page?.record_range ? staged : pagesRef.current;
|
|
|
|
|
const expanded = id ? [page] : direction === "before" ? [page, ...source] : [...source, page];
|
|
|
|
|
// Compare before eviction: a full window can gain content without growing.
|
|
|
|
|
const previous = projectHistoryWindow(source);
|
|
|
|
|
const prepared = projectHistoryWindow(expanded);
|
|
|
|
|
const ready = id || !sameHistoryContent(
|
|
|
|
|
historyContentSnapshot(previous.messages, previous.forkBoundaryMessageCount),
|
|
|
|
|
historyContentSnapshot(prepared.messages, prepared.forkBoundaryMessageCount),
|
|
|
|
|
);
|
|
|
|
|
const nextCursor = direction === "before" ? body.page?.before_cursor : body.page?.after_cursor;
|
|
|
|
|
const more = direction === "before" ? body.page?.has_more_before : body.page?.has_more_after;
|
|
|
|
|
if (!id && more && (!nextCursor || nextCursor === cursor)) throw new Error("History cursor did not advance");
|
|
|
|
|
if (!ready && direction === "before" && body.fork_boundary_event_index !== undefined) {
|
|
|
|
|
// Admit the crossed boundary without ending a pull that still has no
|
|
|
|
|
// visible content before it. It must survive discarded staging pages.
|
|
|
|
|
publishPages(pagesRef.current.map((saved, index) => index === 0 ? { ...saved, forkBoundaryBefore: true } : saved));
|
|
|
|
|
}
|
|
|
|
|
// At most three bounded server pages; intermediate pages do not complete the operation.
|
|
|
|
|
staged = expanded.length <= HISTORY_PAGE_LIMIT ? expanded
|
|
|
|
|
: direction === "before" ? expanded.slice(0, HISTORY_PAGE_LIMIT) : expanded.slice(-HISTORY_PAGE_LIMIT);
|
|
|
|
|
if (ready || !more) {
|
|
|
|
|
// Reaching the end through bookkeeping must not evict the readable window.
|
|
|
|
|
if (!ready) staged = pagesRef.current.map((saved, index, current) => {
|
|
|
|
|
if (index !== (direction === "before" ? 0 : current.length - 1)) return saved;
|
|
|
|
|
const boundary = direction === "before"
|
|
|
|
|
? { before_cursor: nextCursor ?? null, has_more_before: false }
|
|
|
|
|
: { after_cursor: nextCursor ?? null, has_more_after: false };
|
|
|
|
|
return { ...saved, body: { ...saved.body, page: { ...saved.body.page, ...boundary } } };
|
|
|
|
|
});
|
|
|
|
|
publishPages(staged);
|
|
|
|
|
if (id) setTarget({ id, sequence: ++sequence.current });
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
cursor = nextCursor!;
|
2026-11-11 17:32:47 +08:00
|
|
|
}
|
2026-10-18 14:57:23 +08:00
|
|
|
} catch (cause) {
|
2026-11-14 22:52:44 +08:00
|
|
|
if (!controller.signal.aborted) {
|
|
|
|
|
retry.current = retryRead;
|
|
|
|
|
setError(cause instanceof Error ? cause.message : String(cause));
|
|
|
|
|
}
|
2026-10-18 14:57:23 +08:00
|
|
|
} finally {
|
|
|
|
|
if (request.current === controller) {
|
|
|
|
|
request.current = null;
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-14 22:52:44 +08:00
|
|
|
}, [getToken, key, publishPages]);
|
2026-10-18 14:57:23 +08:00
|
|
|
|
|
|
|
|
const first = pages[0]?.body.page;
|
|
|
|
|
const last = pages[pages.length - 1]?.body.page;
|
2026-11-14 22:52:44 +08:00
|
|
|
const window = useMemo(() => currentScope && pages.length ? projectHistoryWindow(pages) : null,
|
|
|
|
|
[currentScope, pages]);
|
|
|
|
|
const readBoundary = (direction: "before" | "after") => {
|
|
|
|
|
if (error && operation === (direction === "before" ? "older" : "newer")) {
|
|
|
|
|
retry.current?.();
|
|
|
|
|
return;
|
2026-10-18 14:57:23 +08:00
|
|
|
}
|
2026-11-14 22:52:44 +08:00
|
|
|
const cursor = direction === "before" ? first?.before_cursor : last?.after_cursor;
|
|
|
|
|
if (cursor) void read(cursor, direction);
|
|
|
|
|
};
|
2026-11-11 17:32:47 +08:00
|
|
|
|
2026-10-18 14:57:23 +08:00
|
|
|
return { prompts, window, target: currentScope ? target : null, loading: currentScope && loading,
|
2026-10-21 13:10:05 +08:00
|
|
|
operation: currentScope && (loading || error) ? operation : null,
|
2026-11-13 19:16:03 +08:00
|
|
|
error: currentScope ? error ?? indexError : null, cancel, latest, expandTraceDetail,
|
2026-11-14 22:52:44 +08:00
|
|
|
retry: () => error ? retry.current?.() : setRefresh((value) => value + 1),
|
2026-10-18 14:57:23 +08:00
|
|
|
jump: (id) => {
|
|
|
|
|
const prompt = prompts.find((item) => item.id === id);
|
|
|
|
|
if (enabled && prompt?.cursor) void read(prompt.cursor, "after", id);
|
|
|
|
|
},
|
2026-11-14 22:52:44 +08:00
|
|
|
older: () => readBoundary("before"),
|
|
|
|
|
newer: () => readBoundary("after"),
|
2026-10-18 14:57:23 +08:00
|
|
|
};
|
|
|
|
|
}
|