Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix: ai error handling (#6588)

pengx1703a7f9939ea0
diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts
index 9332bde26f..5b2691162c 100644
--- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts
+++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts
@@ -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();
diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts
index e81fb5c2c8..5537522d35 100644
--- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts
+++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts
@@ -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,
+ });
+ });
}