fix: stabilize ai design history switching
This commit is contained in:
@@ -257,6 +257,7 @@ const LOCAL_CAPABILITIES: DesignCapabilities = {
|
||||
image: true,
|
||||
video: true,
|
||||
};
|
||||
const LOCAL_CONVERSATION_MESSAGE_PAGE_SIZE = 10;
|
||||
|
||||
type PersistedWorkspace = DesignWorkspaceSummary & {
|
||||
clientWorkspaceId: string;
|
||||
@@ -542,9 +543,13 @@ export class LocalImageWorkspace implements DesignWorkspaceModule {
|
||||
async getConversation(
|
||||
workspaceId: string,
|
||||
conversationId: string,
|
||||
before?: string,
|
||||
): Promise<DesignConversation> {
|
||||
const state = await this.load();
|
||||
return clone(this.requireConversation(state, workspaceId, conversationId));
|
||||
return this.conversationView(
|
||||
this.requireConversation(state, workspaceId, conversationId),
|
||||
before,
|
||||
);
|
||||
}
|
||||
|
||||
renameWorkspace(input: DesignRenameWorkspaceInput): Promise<DesignWorkspace> {
|
||||
@@ -1081,14 +1086,34 @@ export class LocalImageWorkspace implements DesignWorkspaceModule {
|
||||
});
|
||||
}
|
||||
|
||||
private conversationView(conversation: PersistedConversation): DesignConversation {
|
||||
private conversationView(
|
||||
conversation: PersistedConversation,
|
||||
before?: string,
|
||||
): DesignConversation {
|
||||
const {
|
||||
clientConversationId: _clientId,
|
||||
requestHashes: _requestHashes,
|
||||
...view
|
||||
} = conversation;
|
||||
const endIndex = before === undefined
|
||||
? conversation.messages.length
|
||||
: conversation.messages.findIndex((message) => message.id === before);
|
||||
if (endIndex < 0) {
|
||||
throw new LocalImageWorkspaceError(
|
||||
422,
|
||||
'conversation_history_cursor_invalid',
|
||||
'历史会话分页位置已失效,请重新打开会话',
|
||||
);
|
||||
}
|
||||
const startIndex = Math.max(0, endIndex - LOCAL_CONVERSATION_MESSAGE_PAGE_SIZE);
|
||||
const messages = conversation.messages.slice(startIndex, endIndex);
|
||||
return clone({
|
||||
...view,
|
||||
messages,
|
||||
messagePage: {
|
||||
hasOlder: startIndex > 0,
|
||||
nextBefore: startIndex > 0 ? messages[0]?.id ?? null : null,
|
||||
},
|
||||
latestMessagePreview: latestMessagePreview(conversation.messages),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user