diff --git a/packages/backend-core/src/errors/errors.ts b/packages/backend-core/src/errors/errors.ts index ef6a3038ba..ceff6945af 100644 --- a/packages/backend-core/src/errors/errors.ts +++ b/packages/backend-core/src/errors/errors.ts @@ -1,5 +1,7 @@ // BASE +import { ErrorCode } from "@budibase/types" + export abstract class BudibaseError extends Error { code: string @@ -13,13 +15,6 @@ export abstract class BudibaseError extends Error { // ERROR HANDLING -export enum ErrorCode { - USAGE_LIMIT_EXCEEDED = "usage_limit_exceeded", - FEATURE_DISABLED = "feature_disabled", - INVALID_API_KEY = "invalid_api_key", - HTTP = "http", -} - /** * For the given error, build the public representation that is safe * to be exposed over an api. diff --git a/packages/backend-core/src/middleware/authenticated.ts b/packages/backend-core/src/middleware/authenticated.ts index 54d808de86..86c0c687dc 100644 --- a/packages/backend-core/src/middleware/authenticated.ts +++ b/packages/backend-core/src/middleware/authenticated.ts @@ -16,11 +16,12 @@ import env from "../environment" import { Ctx, EndpointMatcher, + ErrorCode, LoginMethod, SessionCookie, User, } from "@budibase/types" -import { ErrorCode, InvalidAPIKeyError } from "../errors" +import { InvalidAPIKeyError } from "../errors" import tracer from "dd-trace" import type { Middleware, Next } from "koa" diff --git a/packages/builder/src/components/common/CodeEditor/AIGen.svelte b/packages/builder/src/components/common/CodeEditor/AIGen.svelte index c7e3a0f1ce..d32bf130da 100644 --- a/packages/builder/src/components/common/CodeEditor/AIGen.svelte +++ b/packages/builder/src/components/common/CodeEditor/AIGen.svelte @@ -3,7 +3,7 @@ import { createEventDispatcher } from "svelte" import { API } from "@/api" - import type { EnrichedBinding } from "@budibase/types" + import { ErrorCode, type EnrichedBinding } from "@budibase/types" import analytics, { Events } from "@/analytics" import AiInput from "../ai/AIInput.svelte" @@ -43,17 +43,26 @@ const resp = await API.generateJs({ prompt, bindings }) const code = resp.code if (code === "") { - throw new Error("We didn't understand your prompt. Please rephrase it.") + throw new Error( + "We didn't understand your prompt. This can happen if the prompt isn't specific, or if it's a request for something other than code. Try expressing your request in a different way." + ) } suggestedCode = code dispatch("update", { code }) } catch (e) { console.error(e) - notifications.error( - e instanceof Error - ? `Unable to generate code: ${e.message}` - : "Unable to generate code. Please try again later." - ) + if (!(e instanceof Error)) { + notifications.error("Unable to generate code. Please try again later.") + return + } + + if ("code" in e && e.code === ErrorCode.USAGE_LIMIT_EXCEEDED) { + notifications.error( + "Monthly usage limit reached. We're exploring options to expand this soon. Questions? Contact support@budibase.com" + ) + } else { + notifications.error(`Unable to generate code: ${e.message}`) + } } } diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index ff19cf1050..40b892e337 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -26,7 +26,6 @@ import { db as dbCore, docIds, env as envCore, - ErrorCode, events, objectStore, roles, @@ -70,6 +69,7 @@ import { AddAppSampleDataResponse, UnpublishAppResponse, SetRevertableAppVersionResponse, + ErrorCode, } from "@budibase/types" import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts" import sdk from "../../sdk" diff --git a/packages/types/src/api/web/errors.ts b/packages/types/src/api/web/errors.ts index 996c0ba34c..9e448701ad 100644 --- a/packages/types/src/api/web/errors.ts +++ b/packages/types/src/api/web/errors.ts @@ -4,3 +4,10 @@ export interface APIError { error?: any validationErrors?: any } + +export enum ErrorCode { + USAGE_LIMIT_EXCEEDED = "usage_limit_exceeded", + FEATURE_DISABLED = "feature_disabled", + INVALID_API_KEY = "invalid_api_key", + HTTP = "http", +} diff --git a/packages/worker/src/api/controllers/global/configs.ts b/packages/worker/src/api/controllers/global/configs.ts index 91caef16f5..46146355fe 100644 --- a/packages/worker/src/api/controllers/global/configs.ts +++ b/packages/worker/src/api/controllers/global/configs.ts @@ -32,14 +32,12 @@ import { OIDCConfigs, OIDCLogosConfig, PASSWORD_REPLACEMENT, - QuotaUsageType, SaveConfigRequest, SaveConfigResponse, SettingsBrandingConfig, SettingsInnerConfig, SSOConfig, SSOConfigType, - StaticQuotaName, UploadConfigFileResponse, UserCtx, } from "@budibase/types" @@ -53,7 +51,6 @@ const getEventFns = async (config: Config, existing?: Config) => { fns.push(events.email.SMTPCreated) } else if (isAIConfig(config)) { fns.push(() => events.ai.AIConfigCreated) - fns.push(() => pro.quotas.addCustomAIConfig()) } else if (isGoogleConfig(config)) { fns.push(() => events.auth.SSOCreated(ConfigType.GOOGLE)) if (config.config.activated) { @@ -92,12 +89,6 @@ const getEventFns = async (config: Config, existing?: Config) => { fns.push(events.email.SMTPUpdated) } else if (isAIConfig(config)) { fns.push(() => events.ai.AIConfigUpdated) - if ( - Object.keys(existing.config).length > Object.keys(config.config).length - ) { - fns.push(() => pro.quotas.removeCustomAIConfig()) - } - fns.push(() => pro.quotas.addCustomAIConfig()) } else if (isGoogleConfig(config)) { fns.push(() => events.auth.SSOUpdated(ConfigType.GOOGLE)) if (!existing.config.activated && config.config.activated) { @@ -576,13 +567,6 @@ export async function destroy(ctx: UserCtx) { try { await db.remove(id, rev) await cache.destroy(cache.CacheKey.CHECKLIST) - if (id === configs.generateConfigID(ConfigType.AI)) { - await pro.quotas.set( - StaticQuotaName.AI_CUSTOM_CONFIGS, - QuotaUsageType.STATIC, - 0 - ) - } ctx.body = { message: "Config deleted successfully" } } catch (err: any) { ctx.throw(err.status, err) diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index e36c45a3ba..270b728ad1 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -16,6 +16,7 @@ import { DeleteInviteUsersRequest, DeleteInviteUsersResponse, DeleteUserResponse, + ErrorCode, FetchUsersResponse, FindUserResponse, GetUserInvitesResponse, @@ -42,7 +43,6 @@ import { import { users, cache, - ErrorCode, events, platform, tenancy,