fix: ai error handling (#6588)

upstream: https://github.com/toeverything/blocksuite/pull/6775
This commit is contained in:
pengx17
2024-04-17 13:27:05 +00:00
parent 09a984d113
commit 03a7f9939e
2 changed files with 26 additions and 1 deletions
@@ -1,17 +1,33 @@
import {
createCopilotMessageMutation,
createCopilotSessionMutation,
fetcher,
fetcher as defaultFetcher,
getBaseUrl,
getCopilotHistoriesQuery,
getCopilotSessionsQuery,
type GraphQLQuery,
type QueryOptions,
type RequestOptions,
} from '@affine/graphql';
import { GeneralNetworkError, PaymentRequiredError } from '@blocksuite/blocks';
type OptionsField<T extends GraphQLQuery> =
RequestOptions<T>['variables'] extends { options: infer U } ? U : never;
const fetcher = async <Query extends GraphQLQuery>(
options: QueryOptions<Query>
) => {
try {
return await defaultFetcher<Query>(options);
} catch (_err) {
const error = Array.isArray(_err) ? _err.at(0) : _err;
if (error.extensions?.code === 402) {
throw new PaymentRequiredError();
}
throw new GeneralNetworkError();
}
};
export class CopilotClient {
readonly backendUrl = getBaseUrl();
@@ -1,5 +1,7 @@
import { openSettingModalAtom } from '@affine/core/atoms';
import { assertExists } from '@blocksuite/global/utils';
import { AIProvider } from '@blocksuite/presets';
import { getCurrentStore } from '@toeverything/infra';
import type { PromptKey } from './prompt';
import {
@@ -263,4 +265,11 @@ export function setupAIProvider() {
forceToImage: true,
});
});
AIProvider.slots.requestUpgradePlan.on(() => {
getCurrentStore().set(openSettingModalAtom, {
activeTab: 'billing',
open: true,
});
});
}