fix(core): fix ai with multiple server (#11570)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { toggleGeneralAIOnboarding } from '@affine/core/components/affine/ai-onboarding/apis';
|
import { toggleGeneralAIOnboarding } from '@affine/core/components/affine/ai-onboarding/apis';
|
||||||
|
import type { AuthAccountInfo, AuthService } from '@affine/core/modules/cloud';
|
||||||
import type { GlobalDialogService } from '@affine/core/modules/dialogs';
|
import type { GlobalDialogService } from '@affine/core/modules/dialogs';
|
||||||
import {
|
import {
|
||||||
type ChatHistoryOrder,
|
type ChatHistoryOrder,
|
||||||
@@ -14,6 +15,16 @@ import type { PromptKey } from './prompt';
|
|||||||
import { textToText, toImage } from './request';
|
import { textToText, toImage } from './request';
|
||||||
import { setupTracker } from './tracker';
|
import { setupTracker } from './tracker';
|
||||||
|
|
||||||
|
function toAIUserInfo(account: AuthAccountInfo | null) {
|
||||||
|
if (!account) return null;
|
||||||
|
return {
|
||||||
|
avatarUrl: account.avatar ?? '',
|
||||||
|
email: account.email ?? '',
|
||||||
|
id: account.id,
|
||||||
|
name: account.label,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const filterStyleToPromptName = new Map<string, PromptKey>(
|
const filterStyleToPromptName = new Map<string, PromptKey>(
|
||||||
Object.entries({
|
Object.entries({
|
||||||
'Clay style': 'workflow:image-clay',
|
'Clay style': 'workflow:image-clay',
|
||||||
@@ -33,7 +44,8 @@ const processTypeToPromptName = new Map<string, PromptKey>(
|
|||||||
|
|
||||||
export function setupAIProvider(
|
export function setupAIProvider(
|
||||||
client: CopilotClient,
|
client: CopilotClient,
|
||||||
globalDialogService: GlobalDialogService
|
globalDialogService: GlobalDialogService,
|
||||||
|
authService: AuthService
|
||||||
) {
|
) {
|
||||||
async function createSession({
|
async function createSession({
|
||||||
workspaceId,
|
workspaceId,
|
||||||
@@ -58,6 +70,16 @@ export function setupAIProvider(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AIProvider.provide('userInfo', () => {
|
||||||
|
return toAIUserInfo(authService.session.account$.value);
|
||||||
|
});
|
||||||
|
|
||||||
|
const accountSubscription = authService.session.account$.subscribe(
|
||||||
|
account => {
|
||||||
|
AIProvider.slots.userInfo.next(toAIUserInfo(account));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
//#region actions
|
//#region actions
|
||||||
AIProvider.provide('chat', async options => {
|
AIProvider.provide('chat', async options => {
|
||||||
const { input, contexts, attachments, networkSearch, retry } = options;
|
const { input, contexts, attachments, networkSearch, retry } = options;
|
||||||
@@ -774,5 +796,6 @@ Could you make a new website based on these notes and send back just the html fi
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
disposeRequestLoginHandler.unsubscribe();
|
disposeRequestLoginHandler.unsubscribe();
|
||||||
|
accountSubscription.unsubscribe();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { useRegisterFindInPageCommands } from '@affine/core/components/hooks/aff
|
|||||||
import { useRegisterWorkspaceCommands } from '@affine/core/components/hooks/use-register-workspace-commands';
|
import { useRegisterWorkspaceCommands } from '@affine/core/components/hooks/use-register-workspace-commands';
|
||||||
import { OverCapacityNotification } from '@affine/core/components/over-capacity';
|
import { OverCapacityNotification } from '@affine/core/components/over-capacity';
|
||||||
import {
|
import {
|
||||||
|
AuthService,
|
||||||
EventSourceService,
|
EventSourceService,
|
||||||
FetchService,
|
FetchService,
|
||||||
GraphQLService,
|
GraphQLService,
|
||||||
@@ -148,6 +149,7 @@ export const WorkspaceSideEffects = () => {
|
|||||||
const graphqlService = useService(GraphQLService);
|
const graphqlService = useService(GraphQLService);
|
||||||
const eventSourceService = useService(EventSourceService);
|
const eventSourceService = useService(EventSourceService);
|
||||||
const fetchService = useService(FetchService);
|
const fetchService = useService(FetchService);
|
||||||
|
const authService = useService(AuthService);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const dispose = setupAIProvider(
|
const dispose = setupAIProvider(
|
||||||
@@ -156,7 +158,8 @@ export const WorkspaceSideEffects = () => {
|
|||||||
fetchService.fetch,
|
fetchService.fetch,
|
||||||
eventSourceService.eventSource
|
eventSourceService.eventSource
|
||||||
),
|
),
|
||||||
globalDialogService
|
globalDialogService,
|
||||||
|
authService
|
||||||
);
|
);
|
||||||
return () => {
|
return () => {
|
||||||
dispose();
|
dispose();
|
||||||
@@ -167,6 +170,7 @@ export const WorkspaceSideEffects = () => {
|
|||||||
workspaceDialogService,
|
workspaceDialogService,
|
||||||
graphqlService,
|
graphqlService,
|
||||||
globalDialogService,
|
globalDialogService,
|
||||||
|
authService,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
useRegisterWorkspaceCommands();
|
useRegisterWorkspaceCommands();
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { AIProvider } from '@affine/core/blocksuite/ai';
|
|
||||||
import { UserFriendlyError } from '@affine/error';
|
import { UserFriendlyError } from '@affine/error';
|
||||||
import type { OAuthProviderType } from '@affine/graphql';
|
import type { OAuthProviderType } from '@affine/graphql';
|
||||||
import { track } from '@affine/track';
|
import { track } from '@affine/track';
|
||||||
@@ -8,7 +7,7 @@ import { distinctUntilChanged, map, skip } from 'rxjs';
|
|||||||
|
|
||||||
import { ApplicationFocused } from '../../lifecycle';
|
import { ApplicationFocused } from '../../lifecycle';
|
||||||
import type { UrlService } from '../../url';
|
import type { UrlService } from '../../url';
|
||||||
import { type AuthAccountInfo, AuthSession } from '../entities/session';
|
import { AuthSession } from '../entities/session';
|
||||||
import { AccountChanged } from '../events/account-changed';
|
import { AccountChanged } from '../events/account-changed';
|
||||||
import { AccountLoggedIn } from '../events/account-logged-in';
|
import { AccountLoggedIn } from '../events/account-logged-in';
|
||||||
import { AccountLoggedOut } from '../events/account-logged-out';
|
import { AccountLoggedOut } from '../events/account-logged-out';
|
||||||
@@ -16,16 +15,6 @@ import { ServerStarted } from '../events/server-started';
|
|||||||
import type { AuthStore } from '../stores/auth';
|
import type { AuthStore } from '../stores/auth';
|
||||||
import type { FetchService } from './fetch';
|
import type { FetchService } from './fetch';
|
||||||
|
|
||||||
function toAIUserInfo(account: AuthAccountInfo | null) {
|
|
||||||
if (!account) return null;
|
|
||||||
return {
|
|
||||||
avatarUrl: account.avatar ?? '',
|
|
||||||
email: account.email ?? '',
|
|
||||||
id: account.id,
|
|
||||||
name: account.label,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnEvent(ApplicationFocused, e => e.onApplicationFocused)
|
@OnEvent(ApplicationFocused, e => e.onApplicationFocused)
|
||||||
@OnEvent(ServerStarted, e => e.onServerStarted)
|
@OnEvent(ServerStarted, e => e.onServerStarted)
|
||||||
export class AuthService extends Service {
|
export class AuthService extends Service {
|
||||||
@@ -38,11 +27,6 @@ export class AuthService extends Service {
|
|||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
// TODO(@forehalo): make AIProvider a standalone service passed to AI elements by props
|
|
||||||
AIProvider.provide('userInfo', () => {
|
|
||||||
return toAIUserInfo(this.session.account$.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.session.account$
|
this.session.account$
|
||||||
.pipe(
|
.pipe(
|
||||||
map(a => ({
|
map(a => ({
|
||||||
@@ -53,8 +37,6 @@ export class AuthService extends Service {
|
|||||||
skip(1) // skip the initial value
|
skip(1) // skip the initial value
|
||||||
)
|
)
|
||||||
.subscribe(({ account }) => {
|
.subscribe(({ account }) => {
|
||||||
AIProvider.slots.userInfo.next(toAIUserInfo(account));
|
|
||||||
|
|
||||||
if (account === null) {
|
if (account === null) {
|
||||||
this.eventBus.emit(AccountLoggedOut, account);
|
this.eventBus.emit(AccountLoggedOut, account);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user