fix: ai error handling (#6588)
upstream: https://github.com/toeverything/blocksuite/pull/6775
This commit is contained in:
@@ -1,17 +1,33 @@
|
|||||||
import {
|
import {
|
||||||
createCopilotMessageMutation,
|
createCopilotMessageMutation,
|
||||||
createCopilotSessionMutation,
|
createCopilotSessionMutation,
|
||||||
fetcher,
|
fetcher as defaultFetcher,
|
||||||
getBaseUrl,
|
getBaseUrl,
|
||||||
getCopilotHistoriesQuery,
|
getCopilotHistoriesQuery,
|
||||||
getCopilotSessionsQuery,
|
getCopilotSessionsQuery,
|
||||||
type GraphQLQuery,
|
type GraphQLQuery,
|
||||||
|
type QueryOptions,
|
||||||
type RequestOptions,
|
type RequestOptions,
|
||||||
} from '@affine/graphql';
|
} from '@affine/graphql';
|
||||||
|
import { GeneralNetworkError, PaymentRequiredError } from '@blocksuite/blocks';
|
||||||
|
|
||||||
type OptionsField<T extends GraphQLQuery> =
|
type OptionsField<T extends GraphQLQuery> =
|
||||||
RequestOptions<T>['variables'] extends { options: infer U } ? U : never;
|
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 {
|
export class CopilotClient {
|
||||||
readonly backendUrl = getBaseUrl();
|
readonly backendUrl = getBaseUrl();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
import { openSettingModalAtom } from '@affine/core/atoms';
|
||||||
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 type { PromptKey } from './prompt';
|
import type { PromptKey } from './prompt';
|
||||||
import {
|
import {
|
||||||
@@ -263,4 +265,11 @@ export function setupAIProvider() {
|
|||||||
forceToImage: true,
|
forceToImage: true,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AIProvider.slots.requestUpgradePlan.on(() => {
|
||||||
|
getCurrentStore().set(openSettingModalAtom, {
|
||||||
|
activeTab: 'billing',
|
||||||
|
open: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user