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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: Replace request helper with ai request handler (#2433)
Giuseppe4040b8ccc2a9
diff --git a/apps/builder/app/builder/features/ai/commands-bar.tsx b/apps/builder/app/builder/features/ai/commands-bar.tsxindex c9c04310b9..b7cfc4d09c 100644--- a/apps/builder/app/builder/features/ai/commands-bar.tsx+++ b/apps/builder/app/builder/features/ai/commands-bar.tsx@@ -1,5 +1,5 @@import { z } from "zod";-import { copywriter, type operations, request } from "@webstudio-is/ai";+import { copywriter, type operations, handleAiRequest } 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";@@ -36,9 +36,7 @@ const handleSubmit = async (event: React.FormEvent<HTMLFormElement>,abortSignal: AbortSignal) => {- const requestParams = Object.fromEntries(- new FormData(event.currentTarget).entries()- );+ const requestParams = Object.fromEntries(new FormData(event.currentTarget));requestParams.components =typeof requestParams.components === "string"@@ -49,16 +47,14 @@ const handleSubmit = async (throw new Error("Invalid prompt data");}- const result = await request<operations.Response>(- [- restAi(),- {- method: "POST",- body: JSON.stringify(requestParams),- signal: abortSignal,- },- ],+ const result = await handleAiRequest<operations.Response>(+ fetch(restAi(), {+ method: "POST",+ body: JSON.stringify(requestParams),+ signal: abortSignal,+ }),{+ signal: abortSignal,onChunk: (operationId, { completion }) => {if (operationId === "copywriter") {try {@@ -87,10 +83,10 @@ const handleSubmit = async (applyOperations(result.data);}return;- } else {- if (abortSignal.aborted === false) {- throw new Error(result.data.message);- }+ }++ if (abortSignal.aborted === false) {+ throw new Error(result.data.message);}};diff --git a/apps/builder/app/routes/rest.ai.copy.ts b/apps/builder/app/routes/rest.ai.copy.tsindex 3e792ba2fc..9a5ea77aa5 100644--- a/apps/builder/app/routes/rest.ai.copy.ts+++ b/apps/builder/app/routes/rest.ai.copy.ts@@ -3,7 +3,7 @@ import {copywriter,createErrorResponse,createGptModel,- type GPTModelMessageFormat,+ type GptModelMessageFormat,} from "@webstudio-is/ai/index.server";import { isFeatureEnabled } from "@webstudio-is/feature-flags";import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";@@ -97,7 +97,7 @@ export const action = async ({ request }: ActionArgs) => {model: "gpt-3.5-turbo",});- const chain = copywriter.createChain<GPTModelMessageFormat>();+ const chain = copywriter.createChain<GptModelMessageFormat>();const response = await chain({model,diff --git a/apps/builder/app/routes/rest.ai.ts b/apps/builder/app/routes/rest.ai.tsindex e4d9accb5a..351a9e9974 100644--- a/apps/builder/app/routes/rest.ai.ts+++ b/apps/builder/app/routes/rest.ai.ts@@ -6,7 +6,7 @@ import {operations,templateGenerator,createGptModel,- type GPTModelMessageFormat,+ type GptModelMessageFormat,createErrorResponse,type ModelMessage,} from "@webstudio-is/ai/index.server";@@ -111,7 +111,7 @@ export const action = async ({ request }: ActionArgs) => {// Given a prompt, categorize the user request.// Based on that we can then decide whether we should execute standalone chains// or the operations chain which needs full context about the instances tree as JSX and CSS.- const commandDetectChain = commandDetect.createChain<GPTModelMessageFormat>();+ const commandDetectChain = commandDetect.createChain<GptModelMessageFormat>();const commandDetectResponse = await commandDetectChain({model,context: {@@ -156,7 +156,7 @@ export const action = async ({ request }: ActionArgs) => {}const { instances } = await loadBuildByProjectId(projectId);- const copywriterChain = copywriter.createChain<GPTModelMessageFormat>();+ const copywriterChain = copywriter.createChain<GptModelMessageFormat>();const copywriterResponse = await copywriterChain({model,context: {@@ -188,7 +188,7 @@ export const action = async ({ request }: ActionArgs) => {model: "gpt-3.5-turbo-16k",});- const chain = operations.createChain<GPTModelMessageFormat>();+ const chain = operations.createChain<GptModelMessageFormat>();const response = await chain({model,@@ -223,7 +223,7 @@ export const action = async ({ request }: ActionArgs) => {});const generationChain =- templateGenerator.createChain<GPTModelMessageFormat>();+ templateGenerator.createChain<GptModelMessageFormat>();const results = await Promise.all(generateTemplatePrompts.map((operation, index) =>diff --git a/packages/ai/src/chains/README.md b/packages/ai/src/chains/README.mdindex f0c2ec9205..6d64c752e8 100644--- a/packages/ai/src/chains/README.md+++ b/packages/ai/src/chains/README.md@@ -85,7 +85,7 @@ Server side:import {vibes,createGptModel- type GPTModelMessageFormat+ type GptModelMessageFormat} from "@webstudio-is/ai";export async function handler({ request }) {@@ -98,7 +98,7 @@ export async function handler({ request }) {model: "gpt-3.5-turbo",});- const chain = vibes.createChain<GPTModelMessageFormat>();+ const chain = vibes.createChain<GptModelMessageFormat>();return chain({model,diff --git a/packages/ai/src/chains/command-detect/README.md b/packages/ai/src/chains/command-detect/README.mdindex a20a45dbe1..883d8bd4f4 100644--- a/packages/ai/src/chains/command-detect/README.md+++ b/packages/ai/src/chains/command-detect/README.md@@ -10,7 +10,7 @@ Given a prompt and a list of possible commands and descriptions, it returns an aimport {commandDetect,createGptModel- type GPTModelMessageFormat+ type GptModelMessageFormat} from "@webstudio-is/ai";export async function handler({ request }) {@@ -23,7 +23,7 @@ export async function handler({ request }) {model: "gpt-3.5-turbo",});- const chain = commandDetect.createChain<GPTModelMessageFormat>();+ const chain = commandDetect.createChain<GptModelMessageFormat>();const result = await chain({model,diff --git a/packages/ai/src/chains/copywriter/README.md b/packages/ai/src/chains/copywriter/README.mdindex 8d5d34e4de..84a86a43ec 100644--- a/packages/ai/src/chains/copywriter/README.md+++ b/packages/ai/src/chains/copywriter/README.md@@ -12,7 +12,7 @@ Server side:import {copywriter,createGptModel- type GPTModelMessageFormat+ type GptModelMessageFormat} from "@webstudio-is/ai";export async function handler({ request }) {@@ -25,7 +25,7 @@ export async function handler({ request }) {model: "gpt-3.5-turbo",});- const chain = copywriter.createChain<GPTModelMessageFormat>();+ const chain = copywriter.createChain<GptModelMessageFormat>();// Respond with a stream.return chain({diff --git a/packages/ai/src/chains/operations/README.md b/packages/ai/src/chains/operations/README.mdindex b8a28f6764..b0011ded24 100644--- a/packages/ai/src/chains/operations/README.md+++ b/packages/ai/src/chains/operations/README.md@@ -53,7 +53,7 @@ import {operations,templateGenerator,createGptModel- type GPTModelMessageFormat+ type GptModelMessageFormat} from "@webstudio-is/ai";export async function handler({ request }) {@@ -66,7 +66,7 @@ export async function handler({ request }) {model: "gpt-3.5-turbo",});- const chain = operations.createChain<GPTModelMessageFormat>();+ const chain = operations.createChain<GptModelMessageFormat>();const response = await chain({model,@@ -90,7 +90,7 @@ export async function handler({ request }) {temperature: 0.2,model: "gpt-4",});- const chain = templateGenerator.createChain<GPTModelMessageFormat>();+ const chain = templateGenerator.createChain<GptModelMessageFormat>();const results = await Promise.all(promptOperations.map(operation => chain({model,diff --git a/packages/ai/src/chains/template-generator/README.md b/packages/ai/src/chains/template-generator/README.mdindex 6ba101872b..1652760802 100644--- a/packages/ai/src/chains/template-generator/README.md+++ b/packages/ai/src/chains/template-generator/README.md@@ -12,7 +12,7 @@ Server side:import {templateGenerator,createGptModel- type GPTModelMessageFormat+ type GptModelMessageFormat} from "@webstudio-is/ai";export async function handler({ request }) {@@ -25,7 +25,7 @@ export async function handler({ request }) {model: "gpt-3.5-turbo",});- const chain = templateGenerator.createChain<GPTModelMessageFormat>();+ const chain = templateGenerator.createChain<GptModelMessageFormat>();const response = await chain({model,diff --git a/packages/ai/src/index.server.ts b/packages/ai/src/index.server.tsindex 346f77e6c1..bbad01b1fb 100644--- a/packages/ai/src/index.server.ts+++ b/packages/ai/src/index.server.ts@@ -7,13 +7,13 @@ export * as templateGenerator from "./chains/template-generator/index.server";/** Models */export {create as createGptModel,- type Model as GPTModel,- type ModelConfig as GPTModelConfig,- type ModelMessageFormat as GPTModelMessageFormat,+ type Model as GptModel,+ type ModelConfig as GptModelConfig,+ type ModelMessageFormat as GptModelMessageFormat,} from "./models/gpt";/** Utils */-export * from "./utils/request";+export * from "./utils/handle-ai-request";export * from "./utils/create-error-response";export * from "./utils/streaming-text-response";diff --git a/packages/ai/src/index.ts b/packages/ai/src/index.tsindex 2a3b96c82b..0ee7c9370f 100644--- a/packages/ai/src/index.ts+++ b/packages/ai/src/index.ts@@ -5,7 +5,7 @@ export * as operations from "./chains/operations/index";export * as templateGenerator from "./chains/template-generator/index";/** Utils */-export * from "./utils/request";+export * from "./utils/handle-ai-request";export * from "./utils/create-error-response";export * from "./utils/streaming-text-response";diff --git a/packages/ai/src/models/index.ts b/packages/ai/src/models/index.tsindex 3f66e20ee5..f2a81094d8 100644--- a/packages/ai/src/models/index.ts+++ b/packages/ai/src/models/index.ts@@ -1,6 +1,6 @@export {create as createGptModel,- type Model as GPTModel,- type ModelConfig as GPTModelConfig,- type ModelMessageFormat as GPTModelMessageFormat,+ type Model as GptModel,+ type ModelConfig as GptModelConfig,+ type ModelMessageFormat as GptModelMessageFormat,} from "./gpt";diff --git a/packages/ai/src/utils/request.ts b/packages/ai/src/utils/handle-ai-request.tssimilarity index 90%rename from packages/ai/src/utils/request.tsrename to packages/ai/src/utils/handle-ai-request.tsindex 919bfc674c..0c7ec6764d 100644--- a/packages/ai/src/utils/request.ts+++ b/packages/ai/src/utils/handle-ai-request.ts@@ -4,6 +4,7 @@ import { createErrorResponse } from "./create-error-response";import { StreamingTextResponse } from "./streaming-text-response";type RequestOptions = {+ signal: AbortSignal;onChunk?: (operationId: string,data: {@@ -15,12 +16,11 @@ type RequestOptions = {) => void;};-export const request = <ResponseData>(- fetchArgs: Parameters<typeof fetch>,+export const handleAiRequest = <ResponseData>(+ request: Promise<Response>,options?: RequestOptions) => {- const signal = fetchArgs[1]?.signal;- return fetch(...fetchArgs)+ return request.then(async (response) => {if (response.status !== 200) {return {@@ -65,7 +65,7 @@ export const request = <ResponseData>(});}- if (signal?.aborted === true) {+ if (options?.signal?.aborted === true) {reader.cancel();break;}@@ -91,7 +91,8 @@ export const request = <ResponseData>(id: "",...createErrorResponse({status: 500,- error: "ai.unhandledError",+ error:+ options?.signal?.aborted === true ? "aborted" : "ai.unhandledError",debug: error.message + "\n" + error.stack,}),} as const;