Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: AI Command Bar error processing (#2472)

Ivan Starkovcfba6556d03b
diff --git a/apps/builder/app/builder/features/ai/ai-command-bar.tsx b/apps/builder/app/builder/features/ai/ai-command-bar.tsx
index e853260614..9258f58356 100644
--- a/apps/builder/app/builder/features/ai/ai-command-bar.tsx
+++ b/apps/builder/app/builder/features/ai/ai-command-bar.tsx
@@ -1,3 +1,5 @@
+/* eslint-disable import/no-internal-modules */
+import formatDistance from "date-fns/formatDistance";
import { useStore } from "@nanostores/react";
import {
AutogrowTextArea,
@@ -40,6 +42,7 @@ import { AiCommandBarButton } from "./ai-button";
import { fetchTranscription } from "./ai-fetch-transcription";
import { fetchResult } from "./ai-fetch-result";
import { useEffectEvent } from "./hooks/effect-event";
+import { AiApiException, RateLimitException } from "./api-exceptions";
type PartialButtonProps<T = ComponentPropsWithoutRef<typeof Button>> = {
[key in keyof T]?: T[key];
@@ -68,22 +71,62 @@ export const AiCommandBar = () => {
cancel,
state: mediaRecorderState,
} = useMediaRecorder({
- onComplete: async (file) => {
- setIsAudioTranscribing(true);
- guardIdRef.current++;
- const guardId = guardIdRef.current;
- const text = await fetchTranscription(file);
- if (guardId !== guardIdRef.current) {
+ onError: (error) => {
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
+ toast("Please enable your microphone.");
+ return;
+ }
+ if (error instanceof Error) {
+ toast.error(error.message);
return;
}
+ toast.error(`Unknown Error: ${error}`);
+ },
+ onComplete: async (file) => {
+ try {
+ setIsAudioTranscribing(true);
+ guardIdRef.current++;
+ const guardId = guardIdRef.current;
+ const text = await fetchTranscription(file);
+ if (guardId !== guardIdRef.current) {
+ return;
+ }
- const currentValue = getValue();
- const newValue = [currentValue, text].filter(Boolean).join(" ");
+ const currentValue = getValue();
+ const newValue = [currentValue, text].filter(Boolean).join(" ");
+
+ setValue(newValue);
+ handleAiRequest(newValue);
+ } catch (error) {
+ if (error instanceof RateLimitException) {
+ toast(
+ `Temporary AI rate limit reached. Please wait ${formatDistance(
+ Date.now(),
+ new Date(error.meta.reset),
+ {
+ includeSeconds: true,
+ }
+ )} and try again.`
+ );
+ return;
+ }
+
+ // Above are known errors; we're not interested in logging them.
+ // eslint-disable-next-line no-console
+ console.error(error);
- setValue(newValue);
- setIsAudioTranscribing(false);
+ if (error instanceof AiApiException) {
+ toast.error(`API Internal Error: ${error.message}`);
+ return;
+ }
- handleAiRequest(newValue);
+ if (error instanceof Error) {
+ // Unknown error, show toast
+ toast.error(`Unknown Error: ${error.message}`);
+ }
+ } finally {
+ setIsAudioTranscribing(false);
+ }
},
onReportSoundAmplitude: (amplitude) => {
recordButtonRef.current?.style.setProperty(
@@ -109,37 +152,71 @@ export const AiCommandBar = () => {
});
const handleAiRequest = async (prompt: string) => {
- abortController.current = new AbortController();
+ if (abortController.current) {
+ if (abortController.current.signal.aborted === false) {
+ // eslint-disable-next-line no-console
+ console.warn(`For some reason previous operation is not aborted.`);
+ }
+
+ abortController.current.abort();
+ }
+
+ const localAbortController = new AbortController();
+ abortController.current = localAbortController;
+
setIsAiRequesting(true);
- guardIdRef.current++;
- const guardId = guardIdRef.current;
// Skip Abort Logic for now
try {
- const errors = await fetchResult(prompt, abortController.current.signal);
+ await fetchResult(prompt, abortController.current.signal);
- if (guardId !== guardIdRef.current) {
+ if (localAbortController !== abortController.current) {
+ // skip
return;
}
- if (errors.length > 0) {
- toast(errors.join("\n"));
- }
-
setPrompts((previousPrompts) => [...previousPrompts, prompt]);
setValue("");
} catch (error) {
+ if (
+ (error instanceof Error && error.message.startsWith("AbortError:")) ||
+ (error instanceof DOMException && error.name === "AbortError")
+ ) {
+ // Aborted by user request
+ return;
+ }
+
+ if (error instanceof RateLimitException) {
+ toast(
+ `Temporary AI rate limit reached. Please wait ${formatDistance(
+ Date.now(),
+ new Date(error.meta.reset),
+ {
+ includeSeconds: true,
+ }
+ )} and try again.`
+ );
+ return;
+ }
+
+ // Above is known errors, we are not interesting in
// eslint-disable-next-line no-console
console.error(error);
+
+ if (error instanceof AiApiException) {
+ toast.error(`API Internal Error: ${error.message}`);
+ return;
+ }
+
if (error instanceof Error) {
- toast(error.message);
- } else {
- toast("Something went wrong");
+ // Unknown error, show toast
+ toast.error(`Unknown Error: ${error.message}`);
}
+ } finally {
+ abortController.current = undefined;
+ setIsAiRequesting(false);
}
- abortController.current = undefined;
- setIsAiRequesting(false);
};
const handleAiButtonClick = () => {
@@ -205,8 +282,6 @@ export const AiCommandBar = () => {
recordButtonProps = {
onClick: (event: MouseEvent<HTMLButtonElement>) => {
// Cancel AI request
- guardIdRef.current++;
- setIsAiRequesting(false);
abortController.current?.abort();
},
};
diff --git a/apps/builder/app/builder/features/ai/ai-fetch-result.ts b/apps/builder/app/builder/features/ai/ai-fetch-result.ts
index a4ace21f86..afe6ab4e1d 100644
--- a/apps/builder/app/builder/features/ai/ai-fetch-result.ts
+++ b/apps/builder/app/builder/features/ai/ai-fetch-result.ts
@@ -33,38 +33,53 @@ import { restAi } from "~/shared/router-utils";
import untruncateJson from "untruncate-json";
import { traverseTemplate } from "@webstudio-is/jsx-utils";
import { RequestParamsSchema } from "~/routes/rest.ai._index";
+import {
+ AiApiException,
+ RateLimitException,
+ textToRateLimitMeta,
+} from "./api-exceptions";
const unknownArray = z.array(z.unknown());
+const onResponseReceived = async (response: Response) => {
+ if (response.ok === false) {
+ const text = await response.text();
+
+ if (response.status === 429) {
+ const meta = textToRateLimitMeta(text);
+ throw new RateLimitException(text, meta);
+ }
+
+ throw new Error(
+ `Fetch error status="${response.status}" text="${text.slice(0, 1000)}"`
+ );
+ }
+};
+
export const fetchResult = async (
prompt: string,
abortSignal: AbortSignal
-): Promise<string[]> => {
+): Promise<void> => {
const commandsResponse = await handleAiRequest<commandDetect.Response>(
fetch(restAi("detect"), {
method: "POST",
body: JSON.stringify({ prompt }),
signal: abortSignal,
}),
- { signal: abortSignal }
- );
-
- if (
- commandsResponse.success === false ||
- commandsResponse.type === "stream"
- ) {
- // Commands detection is not using streaming
- // therefore this should never be the case.
- // The check is here just for type safety.
- if (commandsResponse.type === "stream") {
- return ["Something went wrong."];
+ {
+ onResponseReceived,
}
+ );
- if (abortSignal.aborted === false) {
- return [commandsResponse.data.message];
- }
+ if (commandsResponse.type === "stream") {
+ throw new Error(
+ "Commands detection is not using streaming. Something went wrong."
+ );
+ }
- return ["Something went wrong."];
+ if (commandsResponse.success === false) {
+ // Server error response
+ throw new Error(commandsResponse.data.message);
}
const project = projectStore.get();
@@ -83,13 +98,15 @@ export const fetchResult = async (
// @todo Future commands might not require all the requestParams above.
// When that will be the case, we should revisit the validatin below.
if (requestParams.instanceId === undefined) {
- return ["Please select an instance on the canvas."];
+ throw new Error("Please select an instance on the canvas.");
}
+
+ // @todo can be covered by ts
if (
RequestParamsSchema.omit({ command: true }).safeParse(requestParams)
.success === false
) {
- return ["Invalid prompt data"];
+ throw new Error("Invalid prompt data");
}
const appliedOperations = new Set<string>();
@@ -115,7 +132,7 @@ export const fetchResult = async (
signal: abortSignal,
}),
{
- signal: abortSignal,
+ onResponseReceived,
onChunk: (operationId, { completion }) => {
if (operationId === "copywriter") {
try {
@@ -156,15 +173,12 @@ export const fetchResult = async (
)
);
- const errors = [];
-
for (const promise of promises) {
if (promise.status === "fulfilled") {
const result = promise.value;
if (result.success === false) {
- errors.push(result.data.message);
- continue;
+ throw new AiApiException(result.data.message);
}
if (result.type !== "json") {
@@ -176,16 +190,14 @@ export const fetchResult = async (
applyOperations(result.data);
continue;
}
-
- // Handle other commands responses below.
- // ...
- //
} else if (promise.status === "rejected") {
- errors.push(promise.reason);
+ if (promise.reason instanceof Error) {
+ throw promise.reason;
+ }
+
+ throw new Error(promise.reason.message);
}
}
-
- return errors;
};
const $availableComponentsNames = computed(
@@ -305,26 +317,3 @@ const $jsx = computed(
];
}
);
-
-/*
-export const fetchResult = async (text: string) => {
- const formData = new FormData();
- formData.append("file", file);
-
- const response = await fetch(`${restAi()}/blabla`, {
- method: "POST",
- body: formData,
- });
-
- if (response.ok === false) {
- // @todo: show error
- return;
- }
-
- // @todo add response parsing
- const { text } = await response.json();
-
- // return text;
- await new Promise((resolve) => setTimeout(resolve, 10000));
-};
-*/
diff --git a/apps/builder/app/builder/features/ai/ai-fetch-transcription.ts b/apps/builder/app/builder/features/ai/ai-fetch-transcription.ts
index b514adb612..d0004e3081 100644
--- a/apps/builder/app/builder/features/ai/ai-fetch-transcription.ts
+++ b/apps/builder/app/builder/features/ai/ai-fetch-transcription.ts
@@ -1,7 +1,14 @@
import { restAi } from "~/shared/router-utils";
+import type { action } from "~/routes/rest.ai.audio.transcriptions";
+import {
+ AiApiException,
+ RateLimitException,
+ textToRateLimitMeta,
+} from "./api-exceptions";
export const fetchTranscription = async (file: File) => {
const formData = new FormData();
+
formData.append("file", file);
const response = await fetch(restAi("audio/transcriptions"), {
@@ -10,12 +17,24 @@ export const fetchTranscription = async (file: File) => {
});
if (response.ok === false) {
- // @todo: show error
- return;
+ const text = await response.text();
+
+ if (response.status === 429) {
+ const meta = textToRateLimitMeta(text);
+ throw new RateLimitException(text, meta);
+ }
+
+ throw new Error(
+ `Fetch error status="${response.status}" text="${text.slice(0, 1000)}"`
+ );
}
// @todo add response parsing
- const { text } = await response.json();
+ const result: Awaited<ReturnType<typeof action>> = await response.json();
+
+ if (result.success) {
+ return result.data.text;
+ }
- return text;
+ throw new AiApiException(result.error.message);
};
diff --git a/apps/builder/app/builder/features/ai/api-exceptions.ts b/apps/builder/app/builder/features/ai/api-exceptions.ts
new file mode 100644
index 0000000000..5bd764cf75
--- /dev/null
+++ b/apps/builder/app/builder/features/ai/api-exceptions.ts
@@ -0,0 +1,50 @@
+import { z } from "zod";
+
+/**
+ * To facilitate debugging, categorize errors into few types, one is and API-specific errors.
+ */
+export class AiApiException extends Error {
+ constructor(message: string) {
+ super(message);
+ }
+}
+
+const zRateLimit = z.object({
+ error: z.object({
+ message: z.string(),
+ code: z.number(),
+ meta: z.object({
+ limit: z.number(),
+ reset: z.number(),
+ remaining: z.number(),
+ ratelimitName: z.string(),
+ }),
+ }),
+});
+
+type RateLimitMeta = z.infer<typeof zRateLimit>["error"]["meta"];
+
+export const textToRateLimitMeta = (text: string): RateLimitMeta => {
+ try {
+ const { error } = zRateLimit.parse(JSON.parse(text));
+
+ return error.meta;
+ } catch {
+ // If a 429 status code is received and it's not from our API, default to a 1-minute wait time from the current moment.
+ return {
+ limit: 0,
+ remaining: 0,
+ reset: Date.now(),
+ ratelimitName: "unparsed",
+ };
+ }
+};
+
+export class RateLimitException extends Error {
+ meta: RateLimitMeta;
+
+ constructor(message: string, meta: RateLimitMeta) {
+ super(message);
+ this.meta = meta;
+ }
+}
diff --git a/apps/builder/app/builder/features/ai/commands-bar.tsx b/apps/builder/app/builder/features/ai/commands-bar.tsx
deleted file mode 100644
index 271f794e86..0000000000
--- a/apps/builder/app/builder/features/ai/commands-bar.tsx
+++ /dev/null
@@ -1,330 +0,0 @@
-import { z } from "zod";
-import {
- copywriter,
- type operations,
- handleAiRequest,
- commandDetect,
-} from "@webstudio-is/ai";
-import { createCssEngine } from "@webstudio-is/css-engine";
-import { Button, InputField, Label, Text } from "@webstudio-is/design-system";
-import { SpinnerIcon } from "@webstudio-is/icons";
-import {
- generateJsxElement,
- generateJsxChildren,
- getIndexesWithinAncestors,
- getStyleRules,
- idAttribute,
- componentAttribute,
-} from "@webstudio-is/react-sdk";
-import { createScope, findTreeInstanceIds } from "@webstudio-is/sdk";
-import { computed } from "nanostores";
-import { useRef, useState } from "react";
-import { getMapValuesByKeysSet } from "~/shared/array-utils";
-import {
- breakpointsStore,
- dataSourcesStore,
- instancesStore,
- projectStore,
- propsStore,
- registeredComponentMetasStore,
- selectedInstanceSelectorStore,
- styleSourceSelectionsStore,
- stylesStore,
-} from "~/shared/nano-states";
-import { applyOperations, patchTextInstance } from "./apply-operations";
-import { useStore } from "@nanostores/react";
-import { restAi } from "~/shared/router-utils";
-import { RequestParamsSchema } from "~/routes/rest.ai._index";
-import untruncateJson from "untruncate-json";
-import { traverseTemplate } from "@webstudio-is/jsx-utils";
-
-const handleSubmit = async (
- event: React.FormEvent<HTMLFormElement>,
- abortSignal: AbortSignal
-): Promise<string[]> => {
- const requestParams = Object.fromEntries(new FormData(event.currentTarget));
-
- requestParams.components =
- typeof requestParams.components === "string"
- ? JSON.parse(requestParams.components)
- : undefined;
-
- if (
- RequestParamsSchema.omit({ command: true }).safeParse(requestParams)
- .success === false
- ) {
- return ["Invalid prompt data"];
- }
-
- const commandsResponse = await handleAiRequest<commandDetect.Response>(
- fetch(restAi("detect"), {
- method: "POST",
- body: JSON.stringify({ prompt: requestParams.prompt }),
- signal: abortSignal,
- }),
- { signal: abortSignal }
- );
-
- if (
- commandsResponse.success === false ||
- commandsResponse.type === "stream"
- ) {
- // Commands detection is not using streaming
- // therefore this should never be the case.
- // The check is here just for type safety.
- if (commandsResponse.type === "stream") {
- return ["Something went wrong."];
- }
-
- if (abortSignal.aborted === false) {
- return [commandsResponse.data.message];
- }
-
- return [];
- }
-
- const promises = await Promise.allSettled(
- commandsResponse.data.map((command) =>
- handleAiRequest<operations.Response>(
- fetch(restAi(), {
- method: "POST",
- body: JSON.stringify({ ...requestParams, command }),
- signal: abortSignal,
- }),
- {
- signal: abortSignal,
- onChunk: (operationId, { completion }) => {
- if (operationId === "copywriter") {
- try {
- const jsonResponse = z
- .array(copywriter.TextInstanceSchema)
- .parse(JSON.parse(untruncateJson(completion)));
-
- const currenTextInstance = jsonResponse.pop();
-
- if (currenTextInstance === undefined) {
- return;
- }
-
- patchTextInstance(currenTextInstance);
- } catch {
- /**/
- }
- }
- },
- }
- )
- )
- );
-
- const errors = [];
-
- for (const promise of promises) {
- if (promise.status === "fulfilled") {
- const result = promise.value;
-
- if (result.success === false) {
- errors.push(result.data.message);
- continue;
- }
-
- if (result.type !== "json") {
- continue;
- }
-
- if (result.id === "operations") {
- restoreComponentsNamespace(result.data);
- applyOperations(result.data);
- continue;
- }
-
- // Handle other commands responses below.
- // ...
- //
- } else if (promise.status === "rejected") {
- errors.push(promise.reason);
- }
- }
-
- return errors;
-};
-
-export const CommandsBar = () => {
- const abort = useRef<AbortController>();
- const [isLoading, setIsLoading] = useState(false);
- const [error, setError] = useState<string>();
-
- const project = useStore(projectStore);
- const selectedInstanceSelector = useStore(selectedInstanceSelectorStore);
- const availableComponentsNames = useStore($availableComponentsNames);
- const jsx = useStore($jsx);
-
- return (
- <form
- method="POST"
- action={restAi()}
- onSubmit={async (event) => {
- event.preventDefault();
-
- if (isLoading) {
- return;
- }
- abort.current = new AbortController();
-
- setError(undefined);
- setIsLoading(true);
-
- try {
- const errors = await handleSubmit(event, abort.current.signal);
- if (errors.length > 0) {
- setError(errors.join("\n"));
- }
- } catch (error) {
- setError("Something went wrong.");
- }
-
- abort.current = undefined;
- setIsLoading(false);
- }}
- >
- <Label>
- Prompt
- <InputField name="prompt" />
- </Label>
- <input type="hidden" name="jsx" value={jsx ? JSON.stringify(jsx) : ""} />
- <input
- type="hidden"
- name="components"
- value={JSON.stringify(availableComponentsNames)}
- />
- <input type="hidden" name="projectId" value={project?.id ?? ""} />
- <input
- type="hidden"
- name="instanceId"
- value={selectedInstanceSelector ? selectedInstanceSelector[0] : ""}
- />
- <input type="hidden" name="command" value="" />
- <Button
- onClick={(event) => {
- if (isLoading) {
- event.preventDefault();
- abort.current?.abort();
- }
- }}
- >
- {isLoading ? "stop" : "go"}
- </Button>{" "}
- {isLoading ? <SpinnerIcon /> : null}
- {error ? <Text>{error}</Text> : null}
- </form>
- );
-};
-
-const $availableComponentsNames = computed(
- [registeredComponentMetasStore],
- (metas) => {
- const exclude = ["Body", "Slot"];
-
- return [...metas.keys()]
- .filter((name) => !exclude.includes(name))
- .map(parseComponentName);
- }
-);
-
-// The LLM gets a list of available component names
-// therefore we need to replace the component namespace with a LLM-friendly one
-// preserving context eg. Radix.Dialog instead of just Dialog
-const parseComponentName = (name: string) =>
- name.replace("@webstudio-is/sdk-components-react-radix:", "Radix.");
-// When AI generation is done we need to restore components namespaces.
-const restoreComponentsNamespace = (operations: operations.WsOperations) => {
- for (const operation of operations) {
- if (operation.operation === "insertTemplate") {
- traverseTemplate(operation.template, (node) => {
- if (node.type === "instance" && node.component.startsWith("Radix.")) {
- node.component =
- "@webstudio-is/sdk-components-react-radix:" +
- node.component.slice("Radix.".length);
- }
- });
- }
- }
-};
-
-const $jsx = computed(
- [
- selectedInstanceSelectorStore,
- instancesStore,
- propsStore,
- dataSourcesStore,
- registeredComponentMetasStore,
- breakpointsStore,
- stylesStore,
- styleSourceSelectionsStore,
- ],
- (
- selectedInstanceSelector,
- instances,
- props,
- dataSources,
- metas,
- breakpoints,
- styles,
- styleSourceSelections
- ) => {
- if (selectedInstanceSelector === undefined) {
- return;
- }
-
- const [rootInstanceId] = selectedInstanceSelector;
- const instance = instances.get(rootInstanceId);
- if (instance === undefined) {
- return;
- }
- const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
- rootInstanceId,
- ]);
- const scope = createScope();
-
- const jsx = generateJsxElement({
- scope,
- instance,
- props,
- dataSources,
- indexesWithinAncestors,
- children: generateJsxChildren({
- scope,
- children: instance.children,
- instances,
- props,
- dataSources,
- indexesWithinAncestors,
- }),
- });
-
- const treeInstanceIds = findTreeInstanceIds(instances, rootInstanceId);
-
- const treeStyleSourceSelections = new Map(
- getMapValuesByKeysSet(styleSourceSelections, treeInstanceIds).map(
- (styleSourceSelection) => [
- styleSourceSelection.instanceId,
- styleSourceSelection,
- ]
- )
- );
-
- const engine = createCssEngine({ name: "ssr" });
-
- const styleRules = getStyleRules(styles, treeStyleSourceSelections);
- for (const { breakpointId, instanceId, state, style } of styleRules) {
- engine.addStyleRule(`[${idAttribute}="${instanceId}"]${state ?? ""}`, {
- breakpoint: breakpointId,
- style,
- });
- }
-
- return `<style>{\`${engine.cssText.replace(/\n/gm, " ")}\`}</style>${jsx
- .replace(new RegExp(`${componentAttribute}="[^"]+"`, "g"), "")
- .replace(/\n(data-)/g, " $1")}`;
- }
-);
diff --git a/apps/builder/app/builder/features/ai/hooks/media-recorder.stories.tsx b/apps/builder/app/builder/features/ai/hooks/media-recorder.stories.tsx
index 3280ed6f90..5957e44900 100644
--- a/apps/builder/app/builder/features/ai/hooks/media-recorder.stories.tsx
+++ b/apps/builder/app/builder/features/ai/hooks/media-recorder.stories.tsx
@@ -34,6 +34,10 @@ export const MediaRecorder = (options: { audioBitsPerSecond: number }) => {
const { start, stop, cancel, state } = useMediaRecorder(
{
+ onError: (error) => {
+ // eslint-disable-next-line no-console
+ console.error(error);
+ },
onComplete: (file) => {
setFile(file);
},
diff --git a/apps/builder/app/builder/features/ai/hooks/media-recorder.ts b/apps/builder/app/builder/features/ai/hooks/media-recorder.ts
index 690127cfe3..2403541d56 100644
--- a/apps/builder/app/builder/features/ai/hooks/media-recorder.ts
+++ b/apps/builder/app/builder/features/ai/hooks/media-recorder.ts
@@ -9,6 +9,7 @@ const DEFAULT_OPTIONS: MediaRecorderOptions = {
export const useMediaRecorder = (
props: {
onComplete: (file: File) => void;
+ onError: (error: unknown) => void;
onReportSoundAmplitude?: (amplitude: number) => void;
},
options = DEFAULT_OPTIONS
@@ -18,7 +19,6 @@ export const useMediaRecorder = (
const cancelRef = useRef(false);
const isActiveRef = useRef<boolean>(false);
const idRef = useRef(0);
- const [error, setError] = useState<Error>();
const [state, setState] = useState<"inactive" | "recording">("inactive");
const onComplete = useEffectEvent(props.onComplete);
@@ -42,12 +42,7 @@ export const useMediaRecorder = (
} catch (error) {
// Not allowed, do not start new recording
isActiveRef.current = false;
-
- if (error instanceof Error) {
- setError(error);
- return;
- }
- setError(new Error("Unknown error"));
+ props.onError(error);
return;
}
@@ -65,7 +60,6 @@ export const useMediaRecorder = (
return;
}
- setError(undefined);
setState("recording");
const subtype = MediaRecorder.isTypeSupported("audio/webm; codecs=opus")
@@ -162,5 +156,5 @@ export const useMediaRecorder = (
stop();
});
- return { start, stop, cancel, error, state };
+ return { start, stop, cancel, state };
};
diff --git a/apps/builder/app/builder/features/ai/index.ts b/apps/builder/app/builder/features/ai/index.ts
deleted file mode 100644
index 47ae2f742b..0000000000
--- a/apps/builder/app/builder/features/ai/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export { CommandsBar } from "./commands-bar";
diff --git a/apps/builder/app/routes/rest.ai._index.ts b/apps/builder/app/routes/rest.ai._index.ts
index 69789d69fe..c0c27e19e7 100644
--- a/apps/builder/app/routes/rest.ai._index.ts
+++ b/apps/builder/app/routes/rest.ai._index.ts
@@ -49,6 +49,7 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidApiKey",
status: 401,
+ message: "Invalid OpenAI API key",
debug: "Invalid OpenAI API key",
}),
llmMessages: [],
@@ -64,6 +65,7 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidOrg",
status: 401,
+ message: "Invalid OpenAI API organization",
debug: "Invalid OpenAI API organization",
}),
llmMessages: [],
@@ -76,7 +78,8 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidApiKey",
status: 401,
- debug: "Invalid Pexels api key",
+ message: "Invalid Pexels API key",
+ debug: "Invalid Pexels API key",
}),
llmMessages: [],
};
@@ -91,6 +94,7 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidRequest",
status: 401,
+ message: "Invalid request data",
debug: "Invalid request data",
}),
llmMessages: [],
diff --git a/apps/builder/app/routes/rest.ai.audio.transcriptions.ts b/apps/builder/app/routes/rest.ai.audio.transcriptions.ts
index 6fc5b00314..b2c47b340f 100644
--- a/apps/builder/app/routes/rest.ai.audio.transcriptions.ts
+++ b/apps/builder/app/routes/rest.ai.audio.transcriptions.ts
@@ -1,4 +1,9 @@
import type { ActionArgs } from "@remix-run/node";
+import { z } from "zod";
+
+const zTranscription = z.object({
+ text: z.string().transform((value) => value.trim()),
+});
// @todo: move to AI package
export const action = async ({ request }: ActionArgs) => {
@@ -29,14 +34,16 @@ export const action = async ({ request }: ActionArgs) => {
status: response.status,
message,
},
- };
+ } as const;
}
// @todo untyped
- const data = await response.json();
+ const data = zTranscription.safeParse(await response.json());
+
+ if (data.success === false) {
+ // eslint-disable-next-line no-console
+ console.error("ERROR openai transcriptions", data.error);
+ }
- return {
- success: true,
- text: data.text.trim(),
- };
+ return data;
};
diff --git a/apps/builder/app/routes/rest.ai.detect.ts b/apps/builder/app/routes/rest.ai.detect.ts
index ae101ba40e..f37fe2f1d5 100644
--- a/apps/builder/app/routes/rest.ai.detect.ts
+++ b/apps/builder/app/routes/rest.ai.detect.ts
@@ -25,6 +25,7 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidApiKey",
status: 401,
+ message: "Invalid OpenAI API key",
debug: "Invalid OpenAI API key",
}),
llmMessages: [],
@@ -40,13 +41,15 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidOrg",
status: 401,
+ message: "Invalid OpenAI API organization",
debug: "Invalid OpenAI API organization",
}),
llmMessages: [],
};
}
- const parsed = RequestParamsSchema.safeParse(await request.json());
+ const requestJson = await request.json();
+ const parsed = RequestParamsSchema.safeParse(requestJson);
if (parsed.success === false) {
return {
@@ -54,6 +57,9 @@ export const action = async ({ request }: ActionArgs) => {
...createErrorResponse({
error: "ai.invalidRequest",
status: 401,
+ message: `RequestParamsSchema.safeParse failed on ${JSON.stringify(
+ requestJson
+ )}`,
debug: "Invalid request data",
}),
llmMessages: [],
diff --git a/apps/builder/middleware.js b/apps/builder/middleware.js
index f481d224ee..ae79babd95 100644
--- a/apps/builder/middleware.js
+++ b/apps/builder/middleware.js
@@ -100,8 +100,10 @@ const getRateLimitMemoized = memoize(
// No ephemeralCache for the AI endpoint
const ai = new Ratelimit({
redis: kv,
- limiter: Ratelimit.slidingWindow(100, "5 m"),
+ limiter: Ratelimit.slidingWindow(20, "1 m"),
prefix: "ai",
+ // Usually during AI request we need few requests available, small timeout here could break things
+ timeout: 60000,
});
// For performance testing purposes
@@ -215,24 +217,22 @@ const checkRateLimit = async (ctx, ratelimitName, key) => {
if (success === false) {
// eslint-disable-next-line no-console
console.warn(
- `ratelimit triggered: [${ratelimitName}] limit=${limit}, reset=${reset}, remaining=${remaining}, key=${key}`
+ `ratelimit triggered: [${ratelimitName}] limit=${limit}, reset=${reset}, remaining=${remaining} key=${key}`
);
throw new HTTPException(429, {
res: ctx.json(
{
- errors: [
- {
- message: `ratelimit triggered: [${ratelimitName}] limit=${limit}, reset=${reset}, remaining=${remaining}, key=${key}`,
- code: 429,
- meta: {
- limit,
- reset,
- remaining,
- ratelimitName,
- },
+ error: {
+ message: `ratelimit triggered: [${ratelimitName}] limit=${limit}, reset=${reset}, remaining=${remaining}, key=${key}`,
+ code: 429,
+ meta: {
+ limit,
+ reset,
+ remaining,
+ ratelimitName,
},
- ],
+ },
},
429
),
@@ -428,31 +428,40 @@ if (process.env.NODE_ENV !== "production") {
});
passthrough = async (ctx) => {
- const devUrl = new URL(ctx.req.url);
- devUrl.port = "3000";
-
- const raw = ctx.req.raw;
-
- const req = new Request(devUrl, {
- headers: raw.headers,
- method: raw.method,
- body: raw.body,
- redirect: "manual",
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- duplex: "half",
- });
-
- const res = await fetch(req);
-
- const responseHeaders = new Headers(res.headers);
- responseHeaders.delete("content-encoding");
-
- return new Response(res.body, {
- status: res.status,
- statusText: res.statusText,
- headers: responseHeaders,
- });
+ try {
+ const devUrl = new URL(ctx.req.url);
+ devUrl.port = "3000";
+
+ const raw = ctx.req.raw;
+
+ const req = new Request(devUrl, {
+ headers: raw.headers,
+ method: raw.method,
+ body: raw.body,
+ redirect: "manual",
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ duplex: "half",
+ });
+
+ const res = await fetch(req);
+
+ const responseHeaders = new Headers(res.headers);
+ responseHeaders.delete("content-encoding");
+
+ return new Response(res.body, {
+ status: res.status,
+ statusText: res.statusText,
+ headers: responseHeaders,
+ });
+ } catch (error) {
+ throw new HTTPException(500, {
+ message:
+ error instanceof Error
+ ? `Middlewared dev fetch error ${error.message}`
+ : "Middlewared dev fetch error",
+ });
+ }
};
}
}
diff --git a/packages/ai/src/chains/command-detect/chain.ts b/packages/ai/src/chains/command-detect/chain.ts
index a71e918796..09fadc0ce2 100644
--- a/packages/ai/src/chains/command-detect/chain.ts
+++ b/packages/ai/src/chains/command-detect/chain.ts
@@ -76,7 +76,11 @@ export const createChain = <ModelMessageFormat>(): Chain<
id: name,
...createErrorResponse({
status: 500,
- error: "ai.parseError",
+ error: `ai.${name}.parseError`,
+ message:
+ error instanceof Error
+ ? error.message
+ : "Failed to parse the completion",
debug: (
"Failed to parse the completion " +
(error instanceof Error ? error.message : "")
diff --git a/packages/ai/src/chains/copywriter/chain.ts b/packages/ai/src/chains/copywriter/chain.ts
index 9a144676f4..b8514ec3cd 100644
--- a/packages/ai/src/chains/copywriter/chain.ts
+++ b/packages/ai/src/chains/copywriter/chain.ts
@@ -70,7 +70,7 @@ export const createChain = <ModelMessageFormat>(): Chain<
id: name,
...createErrorResponse({
status: 404,
- error: "ai.parseError",
+ error: `ai.${name}.parseError`,
message,
debug: message,
}),
diff --git a/packages/ai/src/chains/operations/chain.ts b/packages/ai/src/chains/operations/chain.ts
index c5181ca275..7699b55d70 100644
--- a/packages/ai/src/chains/operations/chain.ts
+++ b/packages/ai/src/chains/operations/chain.ts
@@ -98,6 +98,7 @@ export const createChain = <ModelMessageFormat>(): Chain<
...createErrorResponse({
status: 500,
error: "ai.parseError",
+ message: `Failed to parse completion ${parsedCompletion.error.message}`,
debug: `Failed to parse completion ${parsedCompletion.error.message}`,
}),
tokens: completion.tokens,
@@ -123,6 +124,11 @@ export const createChain = <ModelMessageFormat>(): Chain<
...createErrorResponse({
status: 500,
error: "ai.parseError",
+ message:
+ error instanceof Error
+ ? error.message
+ : "Failed to parse the completion",
+
debug: (
"Failed to convert operations. " +
(error instanceof Error ? error.message : "")
diff --git a/packages/ai/src/chains/operations/edit-styles.ts b/packages/ai/src/chains/operations/edit-styles.ts
index e3e84842b2..ff3e0b0c01 100644
--- a/packages/ai/src/chains/operations/edit-styles.ts
+++ b/packages/ai/src/chains/operations/edit-styles.ts
@@ -30,7 +30,7 @@ export const aiOperationToWs = async (
operation: aiOperation
): Promise<wsOperation> => {
if (operation.className === "") {
- throw new Error("Invalid response");
+ throw new Error(`Operation ${operation.operation} className is empty`);
}
const styles = await parseTailwindToWebstudio(operation.className);
return {
diff --git a/packages/ai/src/chains/template-generator/chain.ts b/packages/ai/src/chains/template-generator/chain.ts
index 0db54f82d9..82e239c09e 100644
--- a/packages/ai/src/chains/template-generator/chain.ts
+++ b/packages/ai/src/chains/template-generator/chain.ts
@@ -72,14 +72,15 @@ export const createChain = <ModelMessageFormat>(): Chain<
try {
template = await jsxToTemplate(getCode(completionText, "jsx"));
} catch (error) {
+ const debug = `Failed to parse the completion error="${
+ error instanceof Error ? error.message : ""
+ }" completionText="${completionText}"`.trim();
return {
id: name,
...createErrorResponse({
status: 500,
- debug: (
- "Failed to parse the completion " +
- (error instanceof Error ? error.message : "")
- ).trim(),
+ message: debug,
+ debug,
}),
llmMessages,
};
@@ -88,14 +89,16 @@ export const createChain = <ModelMessageFormat>(): Chain<
try {
postProcessTemplate(template, components);
} catch (error) {
+ const debug = (
+ "Invalid completion " + (error instanceof Error ? error.message : "")
+ ).trim();
+
return {
id: name,
...createErrorResponse({
status: 500,
- debug: (
- "Invalid completion " +
- (error instanceof Error ? error.message : "")
- ).trim(),
+ message: debug,
+ debug,
}),
llmMessages,
};
diff --git a/packages/ai/src/models/gpt.ts b/packages/ai/src/models/gpt.ts
index 8f90b2c627..687e62f1b8 100644
--- a/packages/ai/src/models/gpt.ts
+++ b/packages/ai/src/models/gpt.ts
@@ -150,7 +150,7 @@ const errorToResponse = (id: string, error: unknown) => {
...createErrorResponse({
status,
error: getErrorType(error, status),
- message: "Something went wrong",
+ message: debug,
debug,
}),
} as const;
diff --git a/packages/ai/src/utils/create-error-response.ts b/packages/ai/src/utils/create-error-response.ts
index 17dc928174..34ab9960ef 100644
--- a/packages/ai/src/utils/create-error-response.ts
+++ b/packages/ai/src/utils/create-error-response.ts
@@ -9,7 +9,7 @@ export const createErrorResponse = ({
}: {
status?: number;
error?: string;
- message?: string;
+ message: string;
debug?: string;
tokens?: Tokens;
}): ErrorResponse => ({
diff --git a/packages/ai/src/utils/handle-ai-request.ts b/packages/ai/src/utils/handle-ai-request.ts
index 607d0c30a7..da9a9a895e 100644
--- a/packages/ai/src/utils/handle-ai-request.ts
+++ b/packages/ai/src/utils/handle-ai-request.ts
@@ -1,10 +1,8 @@
import { createChunkDecoder } from "ai";
import type { ModelResponse } from "../types";
-import { createErrorResponse } from "./create-error-response";
import { RemixStreamingTextResponse } from "./remix-streaming-text-response";
type RequestOptions = {
- signal: AbortSignal;
onChunk?: (
operationId: string,
data: {
@@ -14,87 +12,60 @@ type RequestOptions = {
done: boolean;
}
) => void;
+ onResponseReceived: (response: Response) => void;
};
-export const handleAiRequest = <ResponseData>(
+export const handleAiRequest = async <ResponseData>(
request: Promise<Response>,
options: RequestOptions
) => {
- return request
- .then(async (response) => {
- if (response.status !== 200) {
- return {
- id: "",
- ...createErrorResponse({
- status: response.status,
- error: "ai.unhandledError",
- debug: response.statusText,
- }),
- } as const;
- }
+ const response = await request;
- const isStream =
- (response.headers.get("content-type") || "").startsWith("text/plain") &&
- response.body instanceof ReadableStream;
+ await options.onResponseReceived?.(response);
- if (isStream) {
- // @todo Add delimiter to text streaming response to extract the operation id.
- const operationId = "copywriter";
+ const isStream =
+ (response.headers.get("content-type") || "").startsWith("text/plain") &&
+ response.body instanceof ReadableStream;
- let completion = "";
- const reader = response.body.getReader();
- const decoder = createChunkDecoder();
+ if (isStream) {
+ // @todo Add delimiter to text streaming response to extract the operation id.
+ const operationId = "copywriter";
- // eslint-disable-next-line no-constant-condition
- while (true) {
- const { done, value } = await reader.read();
+ let completion = "";
+ const reader = response.body.getReader();
+ const decoder = createChunkDecoder();
- if (done) {
- break;
- }
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const { done, value } = await reader.read();
- const decodedChunk = decoder(value);
- completion += decodedChunk;
+ if (done) {
+ break;
+ }
- if (typeof options?.onChunk === "function") {
- options.onChunk(operationId, {
- completion,
- chunk: value,
- decodedChunk,
- done,
- });
- }
+ const decodedChunk = decoder(value);
+ completion += decodedChunk;
- if (options?.signal?.aborted === true) {
- reader.cancel();
- break;
- }
- }
+ options.onChunk?.(operationId, {
+ completion,
+ chunk: value,
+ decodedChunk,
+ done,
+ });
+ }
- return {
- id: operationId,
- type: "stream",
- success: true,
- data: new RemixStreamingTextResponse(
- new Blob([completion], { type: "text/plain" }).stream()
- ),
- tokens: { prompt: -1, completion: -1 },
- } as const;
- }
+ return {
+ id: operationId,
+ type: "stream",
+ success: true,
+ data: new RemixStreamingTextResponse(
+ new Blob([completion], { type: "text/plain" }).stream()
+ ),
+ tokens: { prompt: -1, completion: -1 },
+ } as const;
+ }
- // @todo Convert the response types to Zod
- // so that responses can be parsed and validated on the client.
- return (await response.json()) as ModelResponse<ResponseData>;
- })
- .catch((error) => {
- return {
- id: "",
- ...createErrorResponse({
- status: 500,
- error:
- options?.signal?.aborted === true ? "aborted" : "ai.unhandledError",
- debug: error.message + "\n" + error.stack,
- }),
- } as const;
- });
+ // @todo Convert the response types to Zod
+ // so that responses can be parsed and validated on the client.
+ return (await response.json()) as ModelResponse<ResponseData>;
};