Skip to content

Commit 91ee5e0

Browse files
committed
fix: ai chat session handling (toeverything#6751)
fix AFF-999 upstream: toeverything/blocksuite#6932
1 parent 0c175ad commit 91ee5e0

File tree

2 files changed

+30
-13
lines changed
  • packages/frontend/core/src

2 files changed

+30
-13
lines changed

packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { authAtom, openSettingModalAtom } from '@affine/core/atoms';
33
import { mixpanel } from '@affine/core/utils';
44
import { getBaseUrl } from '@affine/graphql';
55
import { Trans } from '@affine/i18n';
6+
import { UnauthorizedError } from '@blocksuite/blocks';
67
import { assertExists } from '@blocksuite/global/utils';
78
import { AIProvider } from '@blocksuite/presets';
89
import { getCurrentStore } from '@toeverything/infra';
@@ -71,11 +72,17 @@ const provideAction = <T extends AIAction>(
7172

7273
export function setupAIProvider() {
7374
// a single workspace should have only a single chat session
74-
// workspace-id:doc-id -> chat session id
75+
// user-id:workspace-id:doc-id -> chat session id
7576
const chatSessions = new Map<string, Promise<string>>();
7677

7778
async function getChatSessionId(workspaceId: string, docId: string) {
78-
const storeKey = `${workspaceId}:${docId}`;
79+
const userId = (await AIProvider.userInfo)?.id;
80+
81+
if (!userId) {
82+
throw new UnauthorizedError();
83+
}
84+
85+
const storeKey = `${userId}:${workspaceId}:${docId}`;
7986
if (!chatSessions.has(storeKey)) {
8087
chatSessions.set(
8188
storeKey,
@@ -85,9 +92,15 @@ export function setupAIProvider() {
8592
})
8693
);
8794
}
88-
const sessionId = await chatSessions.get(storeKey);
89-
assertExists(sessionId);
90-
return sessionId;
95+
try {
96+
const sessionId = await chatSessions.get(storeKey);
97+
assertExists(sessionId);
98+
return sessionId;
99+
} catch (err) {
100+
// do not cache the error
101+
chatSessions.delete(storeKey);
102+
throw err;
103+
}
91104
}
92105

93106
//#region actions

packages/frontend/core/src/modules/cloud/services/auth.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ export const AccountLoggedIn = createEvent<AuthAccountInfo>('AccountLoggedIn');
2424
export const AccountLoggedOut =
2525
createEvent<AuthAccountInfo>('AccountLoggedOut');
2626

27+
function toAIUserInfo(account: AuthAccountInfo | null) {
28+
if (!account) return null;
29+
return {
30+
avatarUrl: account.avatar ?? '',
31+
email: account.email ?? '',
32+
id: account.id,
33+
name: account.label,
34+
};
35+
}
36+
2737
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
2838
@OnEvent(ApplicationFocused, e => e.onApplicationFocused)
2939
export class AuthService extends Service {
@@ -36,14 +46,7 @@ export class AuthService extends Service {
3646
super();
3747

3848
AIProvider.provide('userInfo', () => {
39-
const account = this.session.account$.value;
40-
if (!account) return null;
41-
return {
42-
avatarUrl: account.avatar ?? '',
43-
email: account.email ?? '',
44-
id: account.id,
45-
name: account.label,
46-
};
49+
return toAIUserInfo(this.session.account$.value);
4750
});
4851

4952
this.session.account$
@@ -62,6 +65,7 @@ export class AuthService extends Service {
6265
this.eventBus.emit(AccountLoggedIn, account);
6366
}
6467
this.eventBus.emit(AccountChanged, account);
68+
AIProvider.slots.userInfo.emit(toAIUserInfo(account));
6569
});
6670
}
6771

0 commit comments

Comments
 (0)