feat(chat): implement pull-to-refresh functionality and pagination for chat history

This commit is contained in:
2026-07-15 18:19:35 +08:00
parent 05ca15be48
commit c37a2f9040
21 changed files with 892 additions and 66 deletions
+45
View File
@@ -1,11 +1,14 @@
import {
applyOlderHistoryLoadedOutput,
applyHistoryLoadedOutput,
applyNetworkHistoryLoadedOutput,
canLoadMoreHistory,
} from "../helper/history";
import {
countLockedHistoryMessages,
shouldPromptUnlockHistory,
} from "../helper/unlock";
import type { ChatState } from "../chat-state";
import { baseChatMachineSetup } from "./setup";
const applyLocalHistoryLoadedAction = baseChatMachineSetup.assign(
@@ -49,6 +52,30 @@ const markHistoryLoadFailedAction = baseChatMachineSetup.assign({
historyLoaded: true,
});
const markLoadMoreHistoryStartedAction = baseChatMachineSetup.assign({
isLoadingMoreHistory: true,
});
const requestOlderHistoryPageAction = baseChatMachineSetup.sendTo(
"loadMoreHistory",
({ context }) => ({
type: "LoadMoreHistoryPageRequested",
offset: context.nextHistoryOffset,
limit: context.historyLimit,
}),
);
const applyOlderHistoryLoadedAction = baseChatMachineSetup.assign(
({ context, event }) => {
if (event.type !== "ChatOlderHistoryLoaded") return {};
return applyOlderHistoryLoadedOutput(context, event.output);
},
);
const markOlderHistoryLoadFailedAction = baseChatMachineSetup.assign({
isLoadingMoreHistory: false,
});
export const historyMachineSetup = baseChatMachineSetup.extend({
actions: {
applyLocalHistoryLoaded: applyLocalHistoryLoadedAction,
@@ -58,9 +85,27 @@ export const historyMachineSetup = baseChatMachineSetup.extend({
showUnlockHistoryPromptFromNetwork:
showUnlockHistoryPromptFromNetworkAction,
markHistoryLoadFailed: markHistoryLoadFailedAction,
markLoadMoreHistoryStarted: markLoadMoreHistoryStartedAction,
requestOlderHistoryPage: requestOlderHistoryPageAction,
applyOlderHistoryLoaded: applyOlderHistoryLoadedAction,
markOlderHistoryLoadFailed: markOlderHistoryLoadFailedAction,
},
});
export const historyPaginationTransitions = {
ChatLoadMoreHistoryRequested: {
guard: ({ context }: { context: ChatState }) =>
canLoadMoreHistory(context),
actions: ["markLoadMoreHistoryStarted", "requestOlderHistoryPage"],
},
ChatOlderHistoryLoaded: {
actions: "applyOlderHistoryLoaded",
},
ChatOlderHistoryLoadFailed: {
actions: "markOlderHistoryLoadFailed",
},
} as const;
export const guestInitializingState = historyMachineSetup.createStateConfig({
on: {
ChatLocalHistoryLoaded: {