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.

feat: AI Copywriter (#2314)

Giuseppeccf0280a20f3
diff --git a/apps/builder/app/builder/features/ai/copywriter.tsx b/apps/builder/app/builder/features/ai/copywriter.tsx
new file mode 100644
index 0000000000..36bf3dd303
--- /dev/null
+++ b/apps/builder/app/builder/features/ai/copywriter.tsx
@@ -0,0 +1,182 @@
+import { z } from "zod";
+import store from "immerhin";
+import untruncateJson from "untruncate-json";
+import { copywriter, requestStream } from "@webstudio-is/ai";
+import { restAiCopy } from "~/shared/router-utils";
+import {
+ Box,
+ Button,
+ InputField,
+ Label,
+ Text,
+} from "@webstudio-is/design-system";
+import {
+ instancesStore,
+ projectStore,
+ selectedInstanceStore,
+} from "~/shared/nano-states";
+import { useStore } from "@nanostores/react";
+import { useRef, useState } from "react";
+import { computed } from "nanostores";
+
+const patchTextInstance = (textInstance: copywriter.TextInstance) => {
+ store.createTransaction([instancesStore], (instances) => {
+ const currentInstance = instances.get(textInstance.instanceId);
+
+ if (currentInstance === undefined) {
+ return;
+ }
+
+ // Instances can have a number of text child nodes without interleaving components.
+ // When this is the case we treat the child nodes as a single text node,
+ // otherwise the AI would generate children.length chunks of separate text.
+ // We can identify this case of "joint" text instances when the index is -1.
+ const replaceAll = textInstance.index === -1;
+ if (replaceAll) {
+ if (currentInstance.children.every((child) => child.type === "text")) {
+ currentInstance.children = [{ type: "text", value: textInstance.text }];
+ }
+ return;
+ }
+
+ if (currentInstance.children[textInstance.index].type === "text") {
+ currentInstance.children[textInstance.index].value = textInstance.text;
+ }
+ });
+};
+
+const onChunk = (completion: string) => {
+ try {
+ const jsonResponse = z
+ .array(copywriter.TextInstanceSchema)
+ .parse(JSON.parse(untruncateJson(completion)));
+
+ const currenTextInstance = jsonResponse.pop();
+
+ if (currenTextInstance === undefined) {
+ return;
+ }
+
+ patchTextInstance(currenTextInstance);
+ } catch {
+ /**/
+ }
+};
+
+const $textInstances = computed(
+ [instancesStore, selectedInstanceStore],
+ (instances, selectedInstance) => {
+ if (selectedInstance) {
+ return copywriter.collectTextInstances({
+ instances: instancesStore.get(),
+ rootInstanceId: selectedInstance.id,
+ });
+ }
+ return [];
+ }
+);
+
+export const Copywriter = () => {
+ const [isLoading, setIsLoading] = useState(false);
+ const abort = useRef<AbortController | null>(null);
+
+ const project = useStore(projectStore);
+
+ const textInstances = useStore($textInstances);
+ const textInstancesCount = textInstances.length;
+
+ return (
+ <Box>
+ <Text>
+ Generate copy for all the text nodes contained in the selected instance.
+ Found{" "}
+ <Text css={{ fontWeight: "bold", display: "inline" }}>
+ {textInstancesCount}
+ </Text>{" "}
+ text instances.
+ </Text>
+ {textInstancesCount > 0 ? (
+ <>
+ <form
+ onSubmit={(event) => {
+ event.preventDefault();
+
+ if (isLoading) {
+ return;
+ }
+
+ const formData = new FormData(event.currentTarget);
+ const prompt = formData.get("prompt");
+ const projectId = formData.get("projectId");
+ const textInstances = formData.get("textInstances");
+
+ if (
+ typeof prompt !== "string" ||
+ typeof projectId !== "string" ||
+ typeof textInstances !== "string"
+ ) {
+ return;
+ }
+
+ abort.current = new AbortController();
+ setIsLoading(true);
+ requestStream(
+ [
+ restAiCopy(),
+ {
+ method: "POST",
+ body: JSON.stringify({
+ prompt,
+ projectId,
+ textInstances: JSON.parse(textInstances),
+ }),
+ signal: abort.current.signal,
+ },
+ ],
+ {
+ onChunk,
+ }
+ ).then((result) => {
+ abort.current = null;
+ if (typeof result !== "string") {
+ alert("Error " + result.type);
+ }
+ setIsLoading(false);
+ });
+ }}
+ >
+ <Label>
+ Instructions
+ <InputField
+ type="text"
+ placeholder=""
+ name="prompt"
+ maxLength={1200}
+ />
+ </Label>
+
+ <input type="hidden" name="projectId" value={project?.id} />
+ <input
+ type="hidden"
+ name="textInstances"
+ value={JSON.stringify(textInstances)}
+ />
+
+ <Button type="submit" disabled={isLoading}>
+ Generate Copy
+ </Button>
+ {isLoading ? (
+ <Button
+ onClick={() => {
+ abort.current?.abort();
+ }}
+ >
+ Stop
+ </Button>
+ ) : null}
+ </form>
+ </>
+ ) : null}
+ </Box>
+ );
+};
diff --git a/apps/builder/app/builder/features/ai/index.ts b/apps/builder/app/builder/features/ai/index.ts
new file mode 100644
index 0000000000..2c8c10ffa2
--- /dev/null
+++ b/apps/builder/app/builder/features/ai/index.ts
@@ -0,0 +1 @@
+export * from "./copywriter";
diff --git a/apps/builder/app/builder/features/inspector/inspector.tsx b/apps/builder/app/builder/features/inspector/inspector.tsx
index 5e120fdd65..1a22b6f575 100644
--- a/apps/builder/app/builder/features/inspector/inspector.tsx
+++ b/apps/builder/app/builder/features/inspector/inspector.tsx
@@ -28,6 +28,7 @@ import { NavigatorTree } from "~/builder/shared/navigator-tree";
import type { Settings } from "~/builder/shared/client-settings";
import { MetaIcon } from "~/builder/shared/meta-icon";
import { getInstanceLabel } from "~/shared/instance-utils";
+import { Copywriter } from "../ai";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
const InstanceInfo = ({ instance }: { instance: Instance }) => {
@@ -145,11 +146,9 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {
</ScrollArea>
</PanelTabsContent>
{isFeatureEnabled("ai") ? (
- <PanelTabsContent
- value="ai"
- css={contentStyle}
- tabIndex={-1}
- ></PanelTabsContent>
+ <PanelTabsContent value="ai" css={contentStyle} tabIndex={-1}>
+ {isFeatureEnabled("aiCopy") ? <Copywriter /> : null}
+ </PanelTabsContent>
) : null}
</Flex>
</PanelTabs>
diff --git a/apps/builder/app/routes/rest.ai.copy.ts b/apps/builder/app/routes/rest.ai.copy.ts
index 39a55232e3..f090e1035f 100644
--- a/apps/builder/app/routes/rest.ai.copy.ts
+++ b/apps/builder/app/routes/rest.ai.copy.ts
@@ -10,12 +10,6 @@ import { z } from "zod";
import env from "~/env/env.server";
import { createContext } from "~/shared/context.server";
-import { ReadableStream as NodeReadableStream } from "node:stream/web";
-// @todo Remix polyfills ReadableStream in the wrong way. Upgrading to v2 should fix this as they dropped polyfills. https://github.com/vercel/ai/issues/199
-// eslint-disable-next-line @typescript-eslint/ban-ts-comment
-// @ts-ignore
-global.ReadableStream = NodeReadableStream;
-
const RequestSchema = copywriter.ContextSchema.extend({
projectId: z.string(),
});
@@ -45,7 +39,7 @@ export const action = async function action({ request }: ActionArgs) {
) {
return {
success: false,
- type: "invalid_org",
+ type: "invalidOrg",
status: 401,
message: "",
};
@@ -65,6 +59,8 @@ export const action = async function action({ request }: ActionArgs) {
};
}
+ // @todo add rate limiting
+
const { projectId, prompt, textInstances } = parsed.data;
// Permissions check
@@ -84,8 +80,6 @@ export const action = async function action({ request }: ActionArgs) {
}
// End of Permissions check
- // @todo add rate limiting
-
const model = createGptModel({
apiKey: env.OPENAI_KEY,
organization: env.OPENAI_ORG,
diff --git a/apps/builder/package.json b/apps/builder/package.json
index 9c00060cd8..8db2cba820 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -98,6 +98,7 @@
"slugify": "^1.6.6",
"strip-indent": "^4.0.0",
"title-case": "^3.0.3",
+ "untruncate-json": "^0.0.1",
"use-debounce": "^9.0.4",
"uuid": "^9.0.0",
"warn-once": "^0.1.1",
diff --git a/packages/ai/package.json b/packages/ai/package.json
index 9d68827edc..bec0bc60a2 100644
--- a/packages/ai/package.json
+++ b/packages/ai/package.json
@@ -36,7 +36,7 @@
"license": "AGPL-3.0-or-later",
"sideEffects": false,
"dependencies": {
- "@webstudio-is/sdk": "workspace:^",
+ "@webstudio-is/sdk": "workspace:*",
"ai": "^2.2.12",
"escape-string-regexp": "^5.0.0",
"openai": "^4.8.0",
diff --git a/packages/ai/src/chains/copywriter/README.md b/packages/ai/src/chains/copywriter/README.md
index aaf4b2720f..19d948d24a 100644
--- a/packages/ai/src/chains/copywriter/README.md
+++ b/packages/ai/src/chains/copywriter/README.md
@@ -41,26 +41,15 @@ export async function handler({ request }) {
Client side:
```tsx
-import { useCompletion } from "ai/react";
-import { copywriter, type ErrorResponse } from "@webstudio-is/ai";
+import { copywriter, requestStream, type ErrorResponse } from "@webstudio-is/ai";
function UiComponent() {
const [error, setError] = useState(null);
- const { completion, complete, stop, isLoading } = useCompletion({
- api: "/rest/ai/copy",
- onResponse: (response) => {
- if (response.headers.get("Content-Type")?.includes("application/json")) {
- const json: ErrorResponse = await response.json();
- if (json.success === false) {
- setError(json.message);
- }
- }
- },
- });
+ const [copy, setCopy] = useState([]);
useEffect(() => {
- console.log(completion);
- }, [completion]);
+ console.log(copy);
+ }, [copy]);
return (
<form
@@ -84,11 +73,29 @@ function UiComponent() {
return;
}
- complete(prompt, {
- body: {
- projectId,
- textInstances: JSON.parse(textInstances),
- },
+ requestStream(
+ [
+ '/rest/ai/copy',
+ {
+ method: "POST",
+ body: JSON.stringify({
+ prompt,
+ projectId,
+ textInstances: JSON.parse(textInstances),
+ }),
+ signal: abort.current.signal,
+ },
+ ],
+ {
+ onChunk,
+ }
+ ).then((result) => {
+ abort.current = null;
+ if (typeof result !== "string") {
+ alert("Error " + result.type);
+ }
+ setCopy(parseResult(result));
+ setIsLoading(false);
});
}}
>
diff --git a/packages/ai/src/chains/copywriter/__generated__/copy.system.prompt.ts b/packages/ai/src/chains/copywriter/__generated__/copy.system.prompt.ts
index 7dc53a28b4..6b93393274 100644
--- a/packages/ai/src/chains/copywriter/__generated__/copy.system.prompt.ts
+++ b/packages/ai/src/chains/copywriter/__generated__/copy.system.prompt.ts
@@ -2,7 +2,7 @@ export const prompt = `You are a copywriter AI and your task is to produce an ar
The user will provide a description and a list of copy snippets that you should generate text for.
-The copy must pop, be unique and in line with the user request, so do not generate generic lorem ipsum text nor use generic names.
+The copy must pop, be unique and in line with the user request, so do not generate generic lorem ipsum text nor use generic names. Unless specified usually the new text should have similar length of the existing one.
You will respond with the exact original input array of copy to complete replacing only the \`text\` property with the generated copy.
diff --git a/packages/ai/src/chains/copywriter/copy.system.prompt.md b/packages/ai/src/chains/copywriter/copy.system.prompt.md
index 3ed414cac8..86cf7b2e9a 100644
--- a/packages/ai/src/chains/copywriter/copy.system.prompt.md
+++ b/packages/ai/src/chains/copywriter/copy.system.prompt.md
@@ -2,7 +2,7 @@ You are a copywriter AI and your task is to produce an array of copy completions
The user will provide a description and a list of copy snippets that you should generate text for.
-The copy must pop, be unique and in line with the user request, so do not generate generic lorem ipsum text nor use generic names.
+The copy must pop, be unique and in line with the user request, so do not generate generic lorem ipsum text nor use generic names. Unless specified usually the new text should have similar length of the existing one.
You will respond with the exact original input array of copy to complete replacing only the `text` property with the generated copy.
diff --git a/packages/ai/src/chains/copywriter/index.ts b/packages/ai/src/chains/copywriter/index.ts
index f48c78f6a7..a609574e61 100644
--- a/packages/ai/src/chains/copywriter/index.ts
+++ b/packages/ai/src/chains/copywriter/index.ts
@@ -19,7 +19,11 @@ import type { Instance, Instances } from "@webstudio-is/sdk";
export const TextInstanceSchema = z.object({
instanceId: z.string(),
index: z.number(),
- type: z.union([z.literal("Heading"), z.literal("Paragraph")]),
+ type: z.union([
+ z.literal("Heading"),
+ z.literal("Paragraph"),
+ z.literal("Text"),
+ ]),
text: z.string(),
});
@@ -46,7 +50,7 @@ export const createChain = <ModelMessageFormat>(): ChainStream<
if (textInstances.length === 0) {
return {
success: false,
- type: "generic_error",
+ type: "genericError",
status: 404,
message: "No text nodes found for the instance",
};
@@ -57,7 +61,7 @@ export const createChain = <ModelMessageFormat>(): ChainStream<
} catch (error) {
return {
success: false,
- type: "parsing_error",
+ type: "parseError",
status: 500,
message: "Invalid text nodes list",
};
@@ -100,29 +104,45 @@ export const collectTextInstances = ({
return textInstances;
}
- rootInstance.children.forEach((child, index) => {
- if (child.type === "text") {
- if (textComponents.has(rootInstance.component)) {
- const nodeType =
- rootInstance.component === "Heading" ? "Heading" : "Paragraph";
-
- textInstances.push({
- instanceId: rootInstanceId,
- index,
- type: nodeType,
- text: child.value,
- });
+ const nodeType =
+ rootInstance.component === "Heading" ||
+ rootInstance.component === "Paragraph"
+ ? rootInstance.component
+ : "Text";
+
+ // Instances can have a number of text child nodes without interleaving components.
+ // When this is the case we treat the child nodes as a single text node,
+ // otherwise the AI would generate children.length chunks of separate text.
+ // To signal that a textInstance is "joint" we set the index to -1.
+ if (rootInstance.children.every((child) => child.type === "text")) {
+ textInstances.push({
+ instanceId: rootInstanceId,
+ index: -1,
+ type: nodeType,
+ text: rootInstance.children.map((child) => child.value).join(" "),
+ });
+ } else {
+ rootInstance.children.forEach((child, index) => {
+ if (child.type === "text") {
+ if (textComponents.has(rootInstance.component)) {
+ textInstances.push({
+ instanceId: rootInstanceId,
+ index,
+ type: nodeType,
+ text: child.value,
+ });
+ }
+ } else if (child.type === "id") {
+ textInstances.push(
+ ...collectTextInstances({
+ instances,
+ rootInstanceId: child.value,
+ textComponents,
+ })
+ );
}
- } else if (child.type === "id") {
- textInstances.push(
- ...collectTextInstances({
- instances,
- rootInstanceId: child.value,
- textComponents,
- })
- );
- }
- });
+ });
+ }
return textInstances;
};
diff --git a/packages/ai/src/index.ts b/packages/ai/src/index.ts
index 92339ae40a..172b13b661 100644
--- a/packages/ai/src/index.ts
+++ b/packages/ai/src/index.ts
@@ -14,3 +14,4 @@ export * from "./utils/request";
/** Types */
export * from "./types";
+export * from "./utils/request";
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ee3be503c6..c6438c26cc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -297,6 +297,9 @@ importers:
title-case:
specifier: ^3.0.3
version: 3.0.3
+ untruncate-json:
+ specifier: ^0.0.1
+ version: 0.0.1
use-debounce:
specifier: ^9.0.4
version: 9.0.4([email protected])
@@ -639,7 +642,7 @@ importers:
packages/ai:
dependencies:
'@webstudio-is/sdk':
- specifier: workspace:^
+ specifier: workspace:*
version: link:../sdk
ai:
specifier: ^2.2.12
@@ -16766,6 +16769,10 @@ packages:
resolution: {integrity: sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==}
engines: {node: '>=8'}
+ resolution: {integrity: sha512-4W9enDK4X1y1s2S/Rz7ysw6kDuMS3VmRjMFg7GZrNO+98OSe+x5Lh7PKYoVjy3lW/1wmhs6HW0lusnQRHgMarA==}
+ dev: false
+
resolution: {integrity: sha512-dCwEFf0/oT85M1fHBg4F0jtLwJrutGoHSQXCh7u4o2t1drG+c0a9Flnqww6XUKSfQMPpJBRjU8d4RXB09qtvaA==}
hasBin: true