-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix error when server not support ai (#6796)
- Loading branch information
Showing
13 changed files
with
226 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/frontend/core/src/modules/cloud/entities/user-copilot-quota.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
backoffRetry, | ||
catchErrorInto, | ||
effect, | ||
Entity, | ||
exhaustMapSwitchUntilChanged, | ||
fromPromise, | ||
LiveData, | ||
onComplete, | ||
onStart, | ||
} from '@toeverything/infra'; | ||
import { EMPTY, map, mergeMap } from 'rxjs'; | ||
|
||
import { isBackendError, isNetworkError } from '../error'; | ||
import type { AuthService } from '../services/auth'; | ||
import type { ServerConfigService } from '../services/server-config'; | ||
import type { UserCopilotQuotaStore } from '../stores/user-copilot-quota'; | ||
|
||
export class UserCopilotQuota extends Entity { | ||
copilotActionLimit$ = new LiveData<number | 'unlimited' | null>(null); | ||
copilotActionUsed$ = new LiveData<number | null>(null); | ||
|
||
isRevalidating$ = new LiveData(false); | ||
error$ = new LiveData<any | null>(null); | ||
|
||
constructor( | ||
private readonly authService: AuthService, | ||
private readonly store: UserCopilotQuotaStore, | ||
private readonly serverConfigService: ServerConfigService | ||
) { | ||
super(); | ||
} | ||
|
||
revalidate = effect( | ||
map(() => ({ | ||
accountId: this.authService.session.account$.value?.id, | ||
})), | ||
exhaustMapSwitchUntilChanged( | ||
(a, b) => a.accountId === b.accountId, | ||
({ accountId }) => | ||
fromPromise(async signal => { | ||
if (!accountId) { | ||
return; // no quota if no user | ||
} | ||
|
||
const serverConfig = | ||
await this.serverConfigService.serverConfig.features$.waitForNonNull( | ||
signal | ||
); | ||
|
||
let aiQuota = null; | ||
|
||
if (serverConfig.copilot) { | ||
aiQuota = await this.store.fetchUserCopilotQuota(signal); | ||
} | ||
|
||
return aiQuota; | ||
}).pipe( | ||
backoffRetry({ | ||
when: isNetworkError, | ||
count: Infinity, | ||
}), | ||
backoffRetry({ | ||
when: isBackendError, | ||
}), | ||
mergeMap(data => { | ||
if (data) { | ||
const { limit, used } = data; | ||
this.copilotActionUsed$.next(used); | ||
this.copilotActionLimit$.next( | ||
limit === null ? 'unlimited' : limit | ||
); // fix me: unlimited status | ||
} else { | ||
this.copilotActionUsed$.next(null); | ||
this.copilotActionLimit$.next(null); | ||
} | ||
return EMPTY; | ||
}), | ||
catchErrorInto(this.error$), | ||
onStart(() => this.isRevalidating$.next(true)), | ||
onComplete(() => this.isRevalidating$.next(false)) | ||
), | ||
() => { | ||
// Reset the state when the user is changed | ||
this.reset(); | ||
} | ||
) | ||
); | ||
|
||
reset() { | ||
this.copilotActionUsed$.next(null); | ||
this.copilotActionLimit$.next(null); | ||
this.error$.next(null); | ||
this.isRevalidating$.next(false); | ||
} | ||
|
||
override dispose(): void { | ||
this.revalidate.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/frontend/core/src/modules/cloud/services/user-copilot-quota.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { OnEvent, Service } from '@toeverything/infra'; | ||
|
||
import { UserCopilotQuota } from '../entities/user-copilot-quota'; | ||
import { AccountChanged } from './auth'; | ||
|
||
@OnEvent(AccountChanged, e => e.onAccountChanged) | ||
export class UserCopilotQuotaService extends Service { | ||
copilotQuota = this.framework.createEntity(UserCopilotQuota); | ||
|
||
private onAccountChanged() { | ||
this.copilotQuota.revalidate(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/frontend/core/src/modules/cloud/stores/user-copilot-quota.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { copilotQuotaQuery } from '@affine/graphql'; | ||
import { Store } from '@toeverything/infra'; | ||
|
||
import type { GraphQLService } from '../services/graphql'; | ||
|
||
export class UserCopilotQuotaStore extends Store { | ||
constructor(private readonly graphqlService: GraphQLService) { | ||
super(); | ||
} | ||
|
||
async fetchUserCopilotQuota(abortSignal?: AbortSignal) { | ||
const data = await this.graphqlService.gql({ | ||
query: copilotQuotaQuery, | ||
context: { | ||
signal: abortSignal, | ||
}, | ||
}); | ||
|
||
if (!data.currentUser) { | ||
throw new Error('No logged in'); | ||
} | ||
|
||
return data.currentUser.copilot.quota; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...graphql/src/graphql/get-copilot-quota.gql → ...end/graphql/src/graphql/copilot-quota.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
query getCopilotQuota { | ||
query copilotQuota { | ||
currentUser { | ||
copilot { | ||
quota { | ||
|
Oops, something went wrong.