mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
fix: ai chat session handling (#6751)
fix AFF-999 upstream: https://github.com/toeverything/blocksuite/pull/6932
This commit is contained in:
+18
-5
@@ -3,6 +3,7 @@ import { authAtom, openSettingModalAtom } from '@affine/core/atoms';
|
|||||||
import { mixpanel } from '@affine/core/utils';
|
import { mixpanel } from '@affine/core/utils';
|
||||||
import { getBaseUrl } from '@affine/graphql';
|
import { getBaseUrl } from '@affine/graphql';
|
||||||
import { Trans } from '@affine/i18n';
|
import { Trans } from '@affine/i18n';
|
||||||
|
import { UnauthorizedError } from '@blocksuite/blocks';
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import { AIProvider } from '@blocksuite/presets';
|
import { AIProvider } from '@blocksuite/presets';
|
||||||
import { getCurrentStore } from '@toeverything/infra';
|
import { getCurrentStore } from '@toeverything/infra';
|
||||||
@@ -71,11 +72,17 @@ const provideAction = <T extends AIAction>(
|
|||||||
|
|
||||||
export function setupAIProvider() {
|
export function setupAIProvider() {
|
||||||
// a single workspace should have only a single chat session
|
// a single workspace should have only a single chat session
|
||||||
// workspace-id:doc-id -> chat session id
|
// user-id:workspace-id:doc-id -> chat session id
|
||||||
const chatSessions = new Map<string, Promise<string>>();
|
const chatSessions = new Map<string, Promise<string>>();
|
||||||
|
|
||||||
async function getChatSessionId(workspaceId: string, docId: string) {
|
async function getChatSessionId(workspaceId: string, docId: string) {
|
||||||
const storeKey = `${workspaceId}:${docId}`;
|
const userId = (await AIProvider.userInfo)?.id;
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
throw new UnauthorizedError();
|
||||||
|
}
|
||||||
|
|
||||||
|
const storeKey = `${userId}:${workspaceId}:${docId}`;
|
||||||
if (!chatSessions.has(storeKey)) {
|
if (!chatSessions.has(storeKey)) {
|
||||||
chatSessions.set(
|
chatSessions.set(
|
||||||
storeKey,
|
storeKey,
|
||||||
@@ -85,9 +92,15 @@ export function setupAIProvider() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const sessionId = await chatSessions.get(storeKey);
|
try {
|
||||||
assertExists(sessionId);
|
const sessionId = await chatSessions.get(storeKey);
|
||||||
return sessionId;
|
assertExists(sessionId);
|
||||||
|
return sessionId;
|
||||||
|
} catch (err) {
|
||||||
|
// do not cache the error
|
||||||
|
chatSessions.delete(storeKey);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//#region actions
|
//#region actions
|
||||||
|
|||||||
@@ -24,6 +24,16 @@ export const AccountLoggedIn = createEvent<AuthAccountInfo>('AccountLoggedIn');
|
|||||||
export const AccountLoggedOut =
|
export const AccountLoggedOut =
|
||||||
createEvent<AuthAccountInfo>('AccountLoggedOut');
|
createEvent<AuthAccountInfo>('AccountLoggedOut');
|
||||||
|
|
||||||
|
function toAIUserInfo(account: AuthAccountInfo | null) {
|
||||||
|
if (!account) return null;
|
||||||
|
return {
|
||||||
|
avatarUrl: account.avatar ?? '',
|
||||||
|
email: account.email ?? '',
|
||||||
|
id: account.id,
|
||||||
|
name: account.label,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
|
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
|
||||||
@OnEvent(ApplicationFocused, e => e.onApplicationFocused)
|
@OnEvent(ApplicationFocused, e => e.onApplicationFocused)
|
||||||
export class AuthService extends Service {
|
export class AuthService extends Service {
|
||||||
@@ -36,14 +46,7 @@ export class AuthService extends Service {
|
|||||||
super();
|
super();
|
||||||
|
|
||||||
AIProvider.provide('userInfo', () => {
|
AIProvider.provide('userInfo', () => {
|
||||||
const account = this.session.account$.value;
|
return toAIUserInfo(this.session.account$.value);
|
||||||
if (!account) return null;
|
|
||||||
return {
|
|
||||||
avatarUrl: account.avatar ?? '',
|
|
||||||
email: account.email ?? '',
|
|
||||||
id: account.id,
|
|
||||||
name: account.label,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.session.account$
|
this.session.account$
|
||||||
@@ -62,6 +65,7 @@ export class AuthService extends Service {
|
|||||||
this.eventBus.emit(AccountLoggedIn, account);
|
this.eventBus.emit(AccountLoggedIn, account);
|
||||||
}
|
}
|
||||||
this.eventBus.emit(AccountChanged, account);
|
this.eventBus.emit(AccountChanged, account);
|
||||||
|
AIProvider.slots.userInfo.emit(toAIUserInfo(account));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user