fix(core): can not clear chat-panel history (#10634)

Close [BS-2754](https://linear.app/affine-design/issue/BS-2754).

### What Changed?
Use the latest session id and display the corresponding historical messages.
This commit is contained in:
akumatus
2025-03-06 03:19:02 +00:00
parent 6b08e3f5d4
commit e990a5523e
3 changed files with 98 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ import type {
ChatHistoryOrder,
CopilotContextDoc,
CopilotContextFile,
CopilotSessionType,
getCopilotHistoriesQuery,
RequestOptions,
} from '@affine/graphql';
@@ -305,7 +306,7 @@ declare global {
workspaceId: string,
docId?: string,
options?: { action?: boolean }
) => Promise<{ id: string; promptName: string }[] | undefined>;
) => Promise<CopilotSessionType[] | undefined>;
updateSession: (sessionId: string, promptName: string) => Promise<string>;
}

View File

@@ -29,7 +29,6 @@ import type {
DocSearchMenuConfig,
} from './chat-config';
import type {
ChatAction,
ChatContextValue,
ChatItem,
DocChip,
@@ -150,9 +149,10 @@ export class ChatPanel extends SignalWatcher(
const items: ChatItem[] = actions ? [...actions] : [];
if (histories?.at(-1)) {
const history = histories.at(-1);
if (!history) return;
const history = histories?.find(
history => history.sessionId === this._chatSessionId
);
if (history) {
items.push(...history.messages);
AIProvider.LAST_ROOT_SESSION_ID = history.sessionId;
}
@@ -286,13 +286,12 @@ export class ChatPanel extends SignalWatcher(
cancelText: 'Cancel',
})
) {
const actionIds = this.chatContextValue.items
.filter(item => 'sessionId' in item)
.map(item => item.sessionId);
await AIProvider.histories?.cleanup(this.doc.workspace.id, this.doc.id, [
this._chatSessionId ?? '',
...(
this.chatContextValue.items.filter(
item => 'sessionId' in item
) as ChatAction[]
).map(item => item.sessionId),
...(this._chatSessionId ? [this._chatSessionId] : []),
...(actionIds || []),
]);
notification.toast('History cleared');
await this._updateHistory();
@@ -308,12 +307,16 @@ export class ChatPanel extends SignalWatcher(
if (!userId) return;
this.isLoading = true;
const sessions = await AIProvider.session?.getSessions(
this.doc.workspace.id,
this.doc.id
);
if (sessions?.length) {
this._chatSessionId = sessions?.[0].id;
const sessions = (
(await AIProvider.session?.getSessions(
this.doc.workspace.id,
this.doc.id,
{ action: false }
)) || []
).filter(session => !session.parentSessionId);
if (sessions && sessions.length) {
this._chatSessionId = sessions.at(-1)?.id;
await this._updateHistory();
}
this.isLoading = false;