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: Merge copywriter with commands (#2419)

Giuseppe43b2e7e23ca9
diff --git a/apps/builder/app/builder/features/ai/apply-operations.ts b/apps/builder/app/builder/features/ai/apply-operations.ts
index 87003c2774..02f3e19fc9 100644
--- a/apps/builder/app/builder/features/ai/apply-operations.ts
+++ b/apps/builder/app/builder/features/ai/apply-operations.ts
@@ -1,6 +1,6 @@
import { serverSyncStore } from "~/shared/sync";
import { generateDataFromEmbedTemplate } from "@webstudio-is/react-sdk";
-import { operations } from "@webstudio-is/ai";
+import { copywriter, type operations } from "@webstudio-is/ai";
import { isBaseBreakpoint } from "~/shared/breakpoints";
import {
deleteInstance as _deleteInstance,
@@ -18,19 +18,11 @@ import {
import type { DroppableTarget } from "~/shared/tree-utils";
import { getStyleDeclKey, type StyleSource } from "@webstudio-is/sdk";
import { nanoid } from "nanoid";
-import { traverseTemplate } from "@webstudio-is/jsx-utils";
-export const applyOperations = (operations: operations.Response) => {
+export const applyOperations = (operations: operations.WsOperations) => {
for (const operation of operations) {
switch (operation.operation) {
case "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);
- }
- });
insertTemplateByOp(operation);
break;
case "deleteInstance":
@@ -49,7 +41,7 @@ export const applyOperations = (operations: operations.Response) => {
};
const insertTemplateByOp = (
- operation: operations.generateInsertTemplate.wsOperation
+ operation: operations.generateInsertTemplateWsOperation
) => {
const breakpoints = breakpointsStore.get();
const breakpointValues = Array.from(breakpoints.values());
@@ -64,9 +56,14 @@ const insertTemplateByOp = (
baseBreakpoint.id
);
- // Occasionally the LLM picks a component name as the insertion point.
+ // @todo Find a way to avoid the workaround below, peharps improving the prompt.
+ // Occasionally the LLM picks a component name or the entire data-ws-id attribute as the insertion point.
// Instead of throwing the otherwise correct operation we try to fix this here.
- if (metas.has(operation.addTo)) {
+ if (
+ [...metas.keys()].some((componentName) =>
+ componentName.includes(operation.addTo)
+ )
+ ) {
const selectedInstance = selectedInstanceStore.get();
if (selectedInstance) {
operation.addTo = selectedInstance.id;
@@ -87,12 +84,12 @@ const insertTemplateByOp = (
};
const deleteInstanceByOp = (
- operation: operations.deleteInstance.wsOperation
+ operation: operations.deleteInstanceWsOperation
) => {
_deleteInstance([operation.wsId]);
};
-const applyStylesByOp = (operation: operations.editStyles.wsOperation) => {
+const applyStylesByOp = (operation: operations.editStylesWsOperation) => {
serverSyncStore.createTransaction(
[
instancesStore,
@@ -151,3 +148,32 @@ const applyStylesByOp = (operation: operations.editStyles.wsOperation) => {
}
);
};
+
+export const patchTextInstance = (textInstance: copywriter.TextInstance) => {
+ serverSyncStore.createTransaction([instancesStore], (instances) => {
+ const currentInstance = instances.get(textInstance.instanceId);
+
+ if (
+ currentInstance === undefined ||
+ currentInstance.children.length === 0
+ ) {
+ 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;
+ }
+ });
+};
diff --git a/apps/builder/app/builder/features/ai/commands-bar.tsx b/apps/builder/app/builder/features/ai/commands-bar.tsx
new file mode 100644
index 0000000000..c9c04310b9
--- /dev/null
+++ b/apps/builder/app/builder/features/ai/commands-bar.tsx
@@ -0,0 +1,273 @@
+import { z } from "zod";
+import { copywriter, type operations, request } 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,
+} 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";
+import untruncateJson from "untruncate-json";
+import { traverseTemplate } from "@webstudio-is/jsx-utils";
+
+const handleSubmit = async (
+ event: React.FormEvent<HTMLFormElement>,
+ abortSignal: AbortSignal
+) => {
+ const requestParams = Object.fromEntries(
+ new FormData(event.currentTarget).entries()
+ );
+
+ requestParams.components =
+ typeof requestParams.components === "string"
+ ? JSON.parse(requestParams.components)
+ : undefined;
+
+ if (RequestParamsSchema.safeParse(requestParams).success === false) {
+ throw new Error("Invalid prompt data");
+ }
+
+ const result = await request<operations.Response>(
+ [
+ restAi(),
+ {
+ method: "POST",
+ body: JSON.stringify(requestParams),
+ 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 {
+ /**/
+ }
+ }
+ },
+ }
+ );
+
+ if (result.success === true) {
+ if (result.type === "json" && result.id === "operations") {
+ restoreComponentsNamespace(result.data);
+ applyOperations(result.data);
+ }
+ return;
+ } else {
+ if (abortSignal.aborted === false) {
+ throw new Error(result.data.message);
+ }
+ }
+};
+
+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 {
+ await handleSubmit(event, abort.current.signal);
+ } catch (error) {
+ setError(
+ error instanceof Error ? error.message : "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] : ""}
+ />
+ <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(`${idAttribute}="[^"]+"`, "g"), "")
+ .replace(/\n(data-)/g, " $1")}`;
+ }
+);
diff --git a/apps/builder/app/builder/features/ai/copywriter.tsx b/apps/builder/app/builder/features/ai/copywriter.tsx
deleted file mode 100644
index 2b273ed348..0000000000
--- a/apps/builder/app/builder/features/ai/copywriter.tsx
+++ /dev/null
@@ -1,184 +0,0 @@
-import { z } from "zod";
-import untruncateJson from "untruncate-json";
-import {
- copywriter,
- request,
- type StreamingTextResponseType,
-} 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";
-import { serverSyncStore } from "~/shared/sync";
-
-const patchTextInstance = (textInstance: copywriter.TextInstance) => {
- serverSyncStore.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 $textInstances = computed(
- [instancesStore, selectedInstanceStore],
- (instances, selectedInstance) => {
- if (selectedInstance) {
- return copywriter.collectTextInstances({
- instances,
- 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);
- request<StreamingTextResponseType>(
- [
- restAiCopy(),
- {
- method: "POST",
- body: JSON.stringify({
- prompt,
- projectId,
- textInstances: JSON.parse(textInstances),
- }),
- signal: abort.current.signal,
- },
- ],
- {
- onChunk: (id, { completion }) => {
- try {
- const jsonResponse = z
- .array(copywriter.TextInstanceSchema)
- .parse(JSON.parse(untruncateJson(completion)));
-
- const currenTextInstance = jsonResponse.pop();
-
- if (currenTextInstance === undefined) {
- return;
- }
-
- patchTextInstance(currenTextInstance);
- } catch {
- /**/
- }
- },
- }
- ).then((result) => {
- abort.current = null;
- if (result.success === false) {
- alert(result.data.message);
- }
- 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
index dbbff85340..47ae2f742b 100644
--- a/apps/builder/app/builder/features/ai/index.ts
+++ b/apps/builder/app/builder/features/ai/index.ts
@@ -1,2 +1 @@
-export * from "./copywriter";
-export { Operations } from "./operations";
+export { CommandsBar } from "./commands-bar";
diff --git a/apps/builder/app/builder/features/ai/operations.tsx b/apps/builder/app/builder/features/ai/operations.tsx
deleted file mode 100644
index df1bf51f86..0000000000
--- a/apps/builder/app/builder/features/ai/operations.tsx
+++ /dev/null
@@ -1,205 +0,0 @@
-import { operations, request } 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,
-} 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,
- propsStore,
- registeredComponentMetasStore,
- selectedInstanceSelectorStore,
- styleSourceSelectionsStore,
- stylesStore,
-} from "~/shared/nano-states";
-import { applyOperations } from "./apply-operations";
-import { useStore } from "@nanostores/react";
-import { restAiOperations } from "~/shared/router-utils";
-
-export const Operations = () => {
- const abort = useRef<AbortController>();
- const [isLoading, setIsLoading] = useState(false);
- const [error, setError] = useState<string>();
-
- const jsx = useStore($jsx);
-
- return (
- <form
- method="POST"
- action={restAiOperations()}
- onSubmit={(event) => {
- event.preventDefault();
-
- if (isLoading) {
- return;
- }
-
- const formData = new FormData(event.currentTarget);
-
- const prompt = formData.get("prompt");
- let data = formData.get("data");
-
- if (typeof data === "string") {
- try {
- data = JSON.parse(data);
- } catch {
- /**/
- }
- }
-
- if (typeof prompt !== "string" || typeof data !== "string") {
- alert("Invalid data");
- return;
- }
-
- const metas = registeredComponentMetasStore.get();
- const exclude = ["Body", "Slot"];
-
- const components = [...metas.keys()]
- .filter((name) => !exclude.includes(name))
- .map((name) =>
- name.replace("@webstudio-is/sdk-components-react-radix:", "Radix.")
- );
-
- abort.current = new AbortController();
-
- request<operations.Response>([
- restAiOperations(),
- {
- method: "POST",
- body: JSON.stringify({
- prompt,
- components,
- jsx: data,
- }),
- signal: abort.current.signal,
- },
- ])
- .then((result) => {
- if (result.success === true) {
- if (result.type === "json" && result.id === "operations") {
- applyOperations(result.data);
- }
- return;
- }
-
- setError(result.data.message);
- })
- .finally(() => {
- abort.current = undefined;
- setIsLoading(false);
- });
-
- setError(undefined);
- setIsLoading(true);
- }}
- >
- <Label>
- Prompt
- <InputField name="prompt" />
- </Label>
- <input type="hidden" name="data" value={jsx ? JSON.stringify(jsx) : ""} />
- <Button
- onClick={(event) => {
- if (isLoading) {
- event.preventDefault();
- abort.current?.abort();
- }
- }}
- >
- {isLoading ? "stop" : "go"}
- </Button>{" "}
- {isLoading ? <SpinnerIcon /> : null}
- {error ? <Text>{error}</Text> : null}
- </form>
- );
-};
-
-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(`${idAttribute}="[^"]+"`, "g"), "")
- .replace(/\n(data-)/g, " $1")}`;
- }
-);
diff --git a/apps/builder/app/builder/features/inspector/inspector.tsx b/apps/builder/app/builder/features/inspector/inspector.tsx
index 61b1d7f838..c3ab8dcd5c 100644
--- a/apps/builder/app/builder/features/inspector/inspector.tsx
+++ b/apps/builder/app/builder/features/inspector/inspector.tsx
@@ -14,7 +14,6 @@ import {
EnhancedTooltipProvider,
Flex,
ScrollArea,
- Separator,
} from "@webstudio-is/design-system";
import type { Publish } from "~/shared/pubsub";
import { StylePanel } from "~/builder/features/style-panel";
@@ -29,9 +28,8 @@ 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";
-import { Operations } from "../ai";
+import { CommandsBar } from "../ai";
const InstanceInfo = ({ instance }: { instance: Instance }) => {
const metas = useStore(registeredComponentMetasStore);
@@ -150,21 +148,14 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {
{isFeatureEnabled("ai") ? (
<PanelTabsContent value="ai" css={contentStyle} tabIndex={-1}>
<Flex direction="column" gap="4" css={{ padding: 10 }}>
- {isFeatureEnabled("aiOperations") ? (
+ {isFeatureEnabled("ai") ? (
<Flex direction="column" gap="2">
<Text variant="titles">Commands Bar</Text>
<Text>
You can edit styles, remove instances or ask to generate
something
</Text>
- <Operations />
- </Flex>
- ) : null}
- <Separator />
- {isFeatureEnabled("aiCopy") ? (
- <Flex direction="column" gap="2">
- <Text variant="titles">Text Generation</Text>
- <Copywriter />
+ <CommandsBar />
</Flex>
) : null}
</Flex>
diff --git a/apps/builder/app/routes/rest.ai.copy.ts b/apps/builder/app/routes/rest.ai.copy.ts
index f9cb6cbdcb..3e792ba2fc 100644
--- a/apps/builder/app/routes/rest.ai.copy.ts
+++ b/apps/builder/app/routes/rest.ai.copy.ts
@@ -4,19 +4,21 @@ import {
createErrorResponse,
createGptModel,
type GPTModelMessageFormat,
-} from "@webstudio-is/ai";
+} from "@webstudio-is/ai/index.server";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";
import { z } from "zod";
import env from "~/env/env.server";
import { createContext } from "~/shared/context.server";
-const RequestSchema = copywriter.ContextSchema.extend({
+export const RequestSchema = z.object({
projectId: z.string(),
+ prompt: z.string().max(1200),
+ textInstances: z.array(copywriter.TextInstanceSchema),
});
export const action = async ({ request }: ActionArgs) => {
- if (isFeatureEnabled("aiCopy") === false) {
+ if (isFeatureEnabled("ai") === false) {
return {
id: "ai",
...createErrorResponse({
diff --git a/apps/builder/app/routes/rest.ai.op.ts b/apps/builder/app/routes/rest.ai.op.ts
deleted file mode 100644
index 6d6b7a3d7e..0000000000
--- a/apps/builder/app/routes/rest.ai.op.ts
+++ /dev/null
@@ -1,161 +0,0 @@
-import type { ActionArgs } from "@remix-run/node";
-import {
- operations,
- templateGenerator,
- createGptModel,
- type GPTModelMessageFormat,
- createErrorResponse,
-} from "@webstudio-is/ai";
-import { isFeatureEnabled } from "@webstudio-is/feature-flags";
-
-import env from "~/env/env.server";
-import { createContext } from "~/shared/context.server";
-
-const RequestSchema = operations.ContextSchema;
-
-export const maxDuration = 120;
-
-export const action = async ({ request }: ActionArgs) => {
- if (isFeatureEnabled("aiCopy") === false) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "featureDisabled",
- status: 503,
- message: "The feature is not available",
- debug: "aiCopy feature disabled",
- }),
- };
- }
-
- if (env.OPENAI_KEY === undefined) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "ai.invalidApiKey",
- status: 401,
- debug: "Invalid OpenAI API key",
- }),
- };
- }
-
- if (
- env.OPENAI_ORG === undefined ||
- env.OPENAI_ORG.startsWith("org-") === false
- ) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "ai.invalidOrg",
- status: 401,
- debug: "Invalid OpenAI API organization",
- }),
- };
- }
-
- const parsed = RequestSchema.safeParse(await request.json());
-
- if (parsed.success === false) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "ai.invalidRequest",
- status: 401,
- debug: "Invalid request data",
- }),
- };
- }
-
- const { prompt, components, jsx } = parsed.data;
-
- const requestContext = await createContext(request);
-
- if (requestContext.authorization.userId === undefined) {
- return {
- id: "ai",
- ...createErrorResponse({
- error: "unauthorized",
- status: 401,
- message: "You don't have edit access to this project",
- debug: "Unauthorized access attempt",
- }),
- };
- }
-
- const model = createGptModel({
- apiKey: env.OPENAI_KEY,
- organization: env.OPENAI_ORG,
- temperature: 0,
- model: "gpt-3.5-turbo-16k",
- });
-
- const chain = operations.createChain<GPTModelMessageFormat>();
-
- const response = await chain({
- model,
- context: {
- prompt,
- components,
- jsx,
- },
- });
-
- if (response.success === false) {
- return response;
- }
-
- const { llmMessages } = response;
-
- const generateTemplatePrompts = response.data.filter(
- (operation) => operation.operation === "generateTemplatePrompt"
- ) as operations.generateTemplatePrompt.wsOperation[];
-
- if (generateTemplatePrompts.length > 0) {
- const generationModel = createGptModel({
- apiKey: env.OPENAI_KEY,
- organization: env.OPENAI_ORG,
- temperature: 0,
- model: "gpt-4",
- });
-
- const generationChain =
- templateGenerator.createChain<GPTModelMessageFormat>();
-
- const results = await Promise.all(
- generateTemplatePrompts.map((operation, index) =>
- generationChain({
- model: generationModel,
- context: {
- prompt: operation.llmPrompt,
- components,
- },
- }).then((result) => [index, result] as const)
- )
- );
-
- for (const [index, result] of results) {
- llmMessages.push(...result.llmMessages);
-
- if (result.success === false) {
- return {
- ...result,
- llmMessages,
- };
- }
-
- // Replace generateTemplatePrompt.wsOperation with actual generated template
- const generateTemplatePrompt = generateTemplatePrompts[index];
- response.data[index] = {
- operation: "insertTemplate",
- addTo: generateTemplatePrompt.addTo,
- addAtIndex: generateTemplatePrompt.addAtIndex,
- template: result.data,
- };
- }
- }
-
- return {
- ...response,
- llmMessages,
- };
-};
diff --git a/apps/builder/app/routes/rest.ai.ts b/apps/builder/app/routes/rest.ai.ts
new file mode 100644
index 0000000000..e4d9accb5a
--- /dev/null
+++ b/apps/builder/app/routes/rest.ai.ts
@@ -0,0 +1,265 @@
+import { z } from "zod";
+import type { ActionArgs } from "@remix-run/node";
+import {
+ copywriter,
+ commandDetect,
+ operations,
+ templateGenerator,
+ createGptModel,
+ type GPTModelMessageFormat,
+ createErrorResponse,
+ type ModelMessage,
+} from "@webstudio-is/ai/index.server";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
+
+import env from "~/env/env.server";
+import { createContext } from "~/shared/context.server";
+import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";
+import { loadBuildByProjectId } from "@webstudio-is/project-build/index.server";
+
+export const RequestParamsSchema = z.object({
+ projectId: z.string(),
+ instanceId: z.string(),
+ prompt: z.string().max(1200),
+ components: z.array(z.string()),
+ jsx: z.string(),
+});
+
+export const maxDuration = 120;
+
+export const action = async ({ request }: ActionArgs) => {
+ const llmMessages: ModelMessage[] = [];
+
+ if (isFeatureEnabled("ai") === false) {
+ return {
+ id: "ai",
+ ...createErrorResponse({
+ error: "featureDisabled",
+ status: 503,
+ message: "The feature is not available",
+ debug: "aiCopy feature disabled",
+ }),
+ llmMessages,
+ };
+ }
+
+ if (env.OPENAI_KEY === undefined) {
+ return {
+ id: "ai",
+ ...createErrorResponse({
+ error: "ai.invalidApiKey",
+ status: 401,
+ debug: "Invalid OpenAI API key",
+ }),
+ llmMessages,
+ };
+ }
+
+ if (
+ env.OPENAI_ORG === undefined ||
+ env.OPENAI_ORG.startsWith("org-") === false
+ ) {
+ return {
+ id: "ai",
+ ...createErrorResponse({
+ error: "ai.invalidOrg",
+ status: 401,
+ debug: "Invalid OpenAI API organization",
+ }),
+ llmMessages,
+ };
+ }
+
+ const parsed = RequestParamsSchema.safeParse(await request.json());
+
+ if (parsed.success === false) {
+ return {
+ id: "ai",
+ ...createErrorResponse({
+ error: "ai.invalidRequest",
+ status: 401,
+ debug: "Invalid request data",
+ }),
+ llmMessages,
+ };
+ }
+
+ const { prompt, components, jsx, projectId, instanceId } = parsed.data;
+
+ const requestContext = await createContext(request);
+
+ if (requestContext.authorization.userId === undefined) {
+ return {
+ id: "ai",
+ ...createErrorResponse({
+ error: "unauthorized",
+ status: 401,
+ message: "You don't have edit access to this project",
+ debug: "Unauthorized access attempt",
+ }),
+ llmMessages,
+ };
+ }
+
+ let model = createGptModel({
+ apiKey: env.OPENAI_KEY,
+ organization: env.OPENAI_ORG,
+ temperature: 0,
+ model: "gpt-3.5-turbo",
+ });
+
+ // 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 commandDetectResponse = await commandDetectChain({
+ model,
+ context: {
+ prompt,
+ commands: [
+ // @todo Find a good way to pass only the features we want/need to detect
+ // without passing them all. As a safety measure we are currently passing them all
+ // so that the LLM won't miscategorize the task and for example handle an "edit-style" request as a "write-copy" one.
+ // During testing the LLM misdetected a stylistic request like "make it bold" as a request to rewrite the text to sound more "bold".
+ "write-copy",
+ "generate-images",
+ "edit-styles",
+ "generate-ui",
+ "delete-elements",
+ ],
+ },
+ });
+
+ llmMessages.push(...commandDetectResponse.llmMessages);
+
+ // If this is indeed a copy request use the copywriter chain.
+ if (
+ commandDetectResponse.success &&
+ commandDetectResponse.data.some((command) => command === "write-copy")
+ ) {
+ const canEdit = await authorizeProject.hasProjectPermit(
+ { projectId: projectId, permit: "edit" },
+ requestContext
+ );
+
+ if (canEdit === false) {
+ return {
+ id: commandDetectResponse.id,
+ ...createErrorResponse({
+ error: "unauthorized",
+ status: 401,
+ message: "You don't have edit access to this project",
+ debug: "Unauthorized access attempt",
+ }),
+ llmMessages,
+ };
+ }
+
+ const { instances } = await loadBuildByProjectId(projectId);
+ const copywriterChain = copywriter.createChain<GPTModelMessageFormat>();
+ const copywriterResponse = await copywriterChain({
+ model,
+ context: {
+ prompt,
+ textInstances: copywriter.collectTextInstances({
+ instances: new Map(instances),
+ rootInstanceId: instanceId,
+ }),
+ },
+ });
+
+ if (copywriterResponse.success) {
+ // Return the copywriter generation stream.
+ return copywriterResponse.data;
+ }
+
+ return {
+ ...copywriterResponse,
+ llmMessages,
+ };
+ }
+
+ // If the request requires context about the instances tree use the Operations chain.
+
+ model = createGptModel({
+ apiKey: env.OPENAI_KEY,
+ organization: env.OPENAI_ORG,
+ temperature: 0,
+ model: "gpt-3.5-turbo-16k",
+ });
+
+ const chain = operations.createChain<GPTModelMessageFormat>();
+
+ const response = await chain({
+ model,
+ context: {
+ prompt,
+ components,
+ jsx,
+ },
+ });
+
+ if (response.success === false) {
+ return response;
+ }
+
+ llmMessages.push(...response.llmMessages);
+
+ // The operations chain can detect a user interface generation request.
+ // In such cases we let this chain select the insertion point
+ // and then handle the generation request with a standalone chain called template-generator
+ // that has a dedicate and comprehensive prompt.
+
+ const generateTemplatePrompts = response.data.filter(
+ (operation) => operation.operation === "generateTemplatePrompt"
+ ) as operations.generateTemplatePrompt.wsOperation[];
+
+ if (generateTemplatePrompts.length > 0) {
+ const generationModel = createGptModel({
+ apiKey: env.OPENAI_KEY,
+ organization: env.OPENAI_ORG,
+ temperature: 0,
+ model: "gpt-4",
+ });
+
+ const generationChain =
+ templateGenerator.createChain<GPTModelMessageFormat>();
+
+ const results = await Promise.all(
+ generateTemplatePrompts.map((operation, index) =>
+ generationChain({
+ model: generationModel,
+ context: {
+ prompt: operation.llmPrompt,
+ components,
+ },
+ }).then((result) => [index, result] as const)
+ )
+ );
+
+ for (const [index, result] of results) {
+ llmMessages.push(...result.llmMessages);
+
+ if (result.success === false) {
+ return {
+ ...result,
+ llmMessages,
+ };
+ }
+
+ // Replace generateTemplatePrompt.wsOperation with the AI-generated Webstudio template.
+ const generateTemplatePrompt = generateTemplatePrompts[index];
+ response.data[index] = {
+ operation: "insertTemplate",
+ addTo: generateTemplatePrompt.addTo,
+ addAtIndex: generateTemplatePrompt.addAtIndex,
+ template: result.data,
+ };
+ }
+ }
+
+ return {
+ ...response,
+ llmMessages,
+ };
+};
diff --git a/apps/builder/app/shared/router-utils/path-utils.ts b/apps/builder/app/shared/router-utils/path-utils.ts
index e061743dc7..db57a34da5 100644
--- a/apps/builder/app/shared/router-utils/path-utils.ts
+++ b/apps/builder/app/shared/router-utils/path-utils.ts
@@ -163,5 +163,4 @@ export const getPublishedUrl = (domain: string) => {
return `${protocol}//${domain}.${host}`;
};
-export const restAiCopy = () => `/rest/ai/copy`;
-export const restAiOperations = () => `/rest/ai/op`;
+export const restAi = () => `/rest/ai`;
diff --git a/packages/ai/package.json b/packages/ai/package.json
index 926f6dd156..1cf44c6c8c 100644
--- a/packages/ai/package.json
+++ b/packages/ai/package.json
@@ -24,9 +24,16 @@
"typescript": "5.2.2"
},
"exports": {
- "webstudio": "./src/index.ts",
- "types": "./lib/types/index.d.ts",
- "import": "./lib/index.js"
+ ".": {
+ "webstudio": "./src/index.ts",
+ "types": "./lib/types/index.d.ts",
+ "import": "./lib/index.js"
+ },
+ "./index.server": {
+ "webstudio": "./src/index.server.ts",
+ "types": "./lib/types/index.server.d.ts",
+ "import": "./lib/index.server.js"
+ }
},
"files": [
"lib/*",
diff --git a/packages/ai/src/chains/command-detect/README.md b/packages/ai/src/chains/command-detect/README.md
new file mode 100644
index 0000000000..a20a45dbe1
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/README.md
@@ -0,0 +1,45 @@
+# Command Detection Chain
+
+Uses Streaming: `false`.
+
+Given a prompt and a list of possible commands and descriptions, it returns an array of operations matching the prompt request.
+
+## Usage
+
+```typescript
+import {
+ commandDetect,
+ createGptModel
+ type GPTModelMessageFormat
+} from "@webstudio-is/ai";
+
+export async function handler({ request }) {
+ const { prompt, } = await request.json();
+
+ const model = createGptModel({
+ apiKey: process.env.OPENAI_KEY,
+ organization: process.env.OPENAI_ORG,
+ temperature: 0.5,
+ model: "gpt-3.5-turbo",
+ });
+
+ const chain = commandDetect.createChain<GPTModelMessageFormat>();
+
+ const result = await chain({
+ model,
+ context: {
+ prompt,
+ commands: [
+ "write-text",
+ "generate-images",
+ // ... a list of descriptive command names that you want to detect
+ ]
+ }
+ });
+
+ if (result.succes === true) {
+ // An array of detected command names.
+ console.log(result.data);
+ }
+}
+```
diff --git a/packages/ai/src/chains/command-detect/__generated__/command-detect.system.prompt.ts b/packages/ai/src/chains/command-detect/__generated__/command-detect.system.prompt.ts
new file mode 100644
index 0000000000..74e54aca45
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/__generated__/command-detect.system.prompt.ts
@@ -0,0 +1,12 @@
+export const prompt = `Given a user prompt and a list of available operations, your task is to infer what is the user request about and return a list with one or multiple operation names that fit the prompt.
+
+Available operations:
+
+\`\`\`
+{commands}
+\`\`\`
+
+For example if the user is asking to translate the operation should be a copywrite and you should respond with ["copywrite"]
+
+Respond with a valid JSON array of operation names or an empty array if you think that none of the operations is relevant for the user request. Start with [
+`;
diff --git a/packages/ai/src/chains/command-detect/__generated__/command-detect.user.prompt.ts b/packages/ai/src/chains/command-detect/__generated__/command-detect.user.prompt.ts
new file mode 100644
index 0000000000..5f09af2799
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/__generated__/command-detect.user.prompt.ts
@@ -0,0 +1,6 @@
+export const prompt = `User request:
+
+\`\`\`
+{prompt}
+\`\`\`
+`;
diff --git a/packages/ai/src/chains/command-detect/chain.ts b/packages/ai/src/chains/command-detect/chain.ts
new file mode 100644
index 0000000000..f4b1ee7476
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/chain.ts
@@ -0,0 +1,93 @@
+import { z } from "zod";
+import type { Model as BaseModel, ModelMessage, Chain } from "../../types";
+import { formatPrompt } from "../../utils/format-prompt";
+import { prompt as promptSystemTemplate } from "./__generated__/command-detect.system.prompt";
+import { prompt as promptUserTemplate } from "./__generated__/command-detect.user.prompt";
+import { createErrorResponse } from "../../utils/create-error-response";
+
+/**
+ * Command Detect Chain
+ *
+ * Given a prompt and a list of possible commands and descriptions, it returns an array of operations matching the prompt request.
+ */
+
+export const ContextSchema = z.object({
+ // The prompt provides the original user request.
+ prompt: z.string(),
+ commands: z.array(z.string()),
+});
+export type Context = z.infer<typeof ContextSchema>;
+
+export const ResponseSchema = z.array(z.string());
+export type Response = z.infer<typeof ResponseSchema>;
+
+export const createChain = <ModelMessageFormat>(): Chain<
+ BaseModel<ModelMessageFormat>,
+ Context,
+ Response
+> =>
+ async function chain({ model, context }) {
+ const id = "command-detect";
+
+ const { prompt, commands } = context;
+
+ const llmMessages: ModelMessage[] = [
+ [
+ "system",
+ formatPrompt(
+ {
+ commands: JSON.stringify(commands),
+ },
+ promptSystemTemplate
+ ),
+ ],
+ ["user", formatPrompt({ prompt }, promptUserTemplate)],
+ ];
+
+ const messages = model.generateMessages(llmMessages);
+
+ const completion = await model.completion({
+ id,
+ messages,
+ });
+
+ if (completion.success === false) {
+ return {
+ ...completion,
+ llmMessages,
+ };
+ }
+
+ const completionText = completion.data.choices[0];
+ llmMessages.push(["assistant", completionText]);
+
+ let detectedCommands = [];
+ try {
+ detectedCommands = ResponseSchema.parse(JSON.parse(completionText));
+ const expectedCommands = new Set(commands);
+ for (const command of detectedCommands) {
+ if (expectedCommands.has(command) === false) {
+ throw new Error("Invalid command name detected " + command);
+ }
+ }
+ } catch (error) {
+ return {
+ id,
+ ...createErrorResponse({
+ status: 500,
+ error: "ai.parseError",
+ debug: (
+ "Failed to parse the completion " +
+ (error instanceof Error ? error.message : "")
+ ).trim(),
+ }),
+ llmMessages,
+ };
+ }
+
+ return {
+ ...completion,
+ data: detectedCommands,
+ llmMessages,
+ };
+ };
diff --git a/packages/ai/src/chains/command-detect/command-detect.system.prompt.md b/packages/ai/src/chains/command-detect/command-detect.system.prompt.md
new file mode 100644
index 0000000000..c52ad388a9
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/command-detect.system.prompt.md
@@ -0,0 +1,11 @@
+Given a user prompt and a list of available operations, your task is to infer what is the user request about and return a list with one or multiple operation names that fit the prompt.
+
+Available operations:
+
+```
+{commands}
+```
+
+For example if the user is asking to translate the operation should be a copywrite and you should respond with ["copywrite"]
+
+Respond with a valid JSON array of operation names or an empty array if you think that none of the operations is relevant for the user request. Start with [
diff --git a/packages/ai/src/chains/command-detect/command-detect.user.prompt.md b/packages/ai/src/chains/command-detect/command-detect.user.prompt.md
new file mode 100644
index 0000000000..90f3c42d65
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/command-detect.user.prompt.md
@@ -0,0 +1,5 @@
+User request:
+
+```
+{prompt}
+```
diff --git a/packages/ai/src/chains/command-detect/index.server.ts b/packages/ai/src/chains/command-detect/index.server.ts
new file mode 100644
index 0000000000..c815182484
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/index.server.ts
@@ -0,0 +1 @@
+export * from "./chain";
diff --git a/packages/ai/src/chains/command-detect/index.ts b/packages/ai/src/chains/command-detect/index.ts
new file mode 100644
index 0000000000..9c0952f12b
--- /dev/null
+++ b/packages/ai/src/chains/command-detect/index.ts
@@ -0,0 +1 @@
+export { type Response, ResponseSchema } from "./chain";
diff --git a/packages/ai/src/chains/copywriter/chain.server.ts b/packages/ai/src/chains/copywriter/chain.server.ts
new file mode 100644
index 0000000000..f9b5a9f9f2
--- /dev/null
+++ b/packages/ai/src/chains/copywriter/chain.server.ts
@@ -0,0 +1,166 @@
+import { z } from "zod";
+import type { Model as BaseModel, ModelMessage, Chain } from "../../types";
+import { formatPrompt } from "../../utils/format-prompt";
+import { prompt as promptSystemTemplate } from "./__generated__/copy.system.prompt";
+import { prompt as promptUserTemplate } from "./__generated__/copy.user.prompt";
+import type { Instance, Instances } from "@webstudio-is/sdk";
+import { createErrorResponse } from "../../utils/create-error-response";
+import type { StreamingTextResponseType } from "../../utils/streaming-text-response";
+
+/**
+ * Copywriter chain.
+ *
+ * Given a description and an instance id,
+ * this chain generates copy for the instance and all its descendant text nodes.
+ */
+
+export const TextInstanceSchema = z.object({
+ instanceId: z.string(),
+ index: z.number(),
+ type: z.union([
+ z.literal("Heading"),
+ z.literal("Paragraph"),
+ z.literal("Text"),
+ ]),
+ text: z.string(),
+});
+
+export type TextInstance = z.infer<typeof TextInstanceSchema>;
+
+export const ContextSchema = z.object({
+ // The prompt provides context about the copy to generate and comes from the user.
+ prompt: z.string(),
+ // An array of text nodes to generate copy for.
+ textInstances: z.array(TextInstanceSchema),
+});
+export type Context = z.infer<typeof ContextSchema>;
+
+export const ResponseSchema = z.array(TextInstanceSchema);
+export type Response = z.infer<typeof ResponseSchema>;
+
+export const createChain = <ModelMessageFormat>(): Chain<
+ BaseModel<ModelMessageFormat>,
+ Context,
+ StreamingTextResponseType
+> =>
+ async function chain({ model, context }) {
+ const { prompt, textInstances } = context;
+
+ const id = "copywriter";
+
+ const llmMessages: ModelMessage[] = [
+ ["system", promptSystemTemplate],
+ [
+ "user",
+ formatPrompt(
+ {
+ prompt,
+ text_nodes: JSON.stringify(textInstances),
+ },
+ promptUserTemplate
+ ),
+ ],
+ ];
+
+ if (textInstances.length === 0) {
+ const message = "No text nodes found for the instance";
+ return {
+ id,
+ ...createErrorResponse({
+ status: 404,
+ error: "ai.copywriter.textNodesNotFound",
+ message,
+ debug: message,
+ }),
+ llmMessages,
+ };
+ }
+
+ try {
+ z.array(TextInstanceSchema).parse(textInstances);
+ } catch (error) {
+ const message = "Invalid nodes list";
+ return {
+ id,
+ ...createErrorResponse({
+ status: 404,
+ error: "ai.parseError",
+ message,
+ debug: message,
+ }),
+ llmMessages,
+ };
+ }
+
+ const messages = model.generateMessages(llmMessages);
+
+ const response = await model.completionStream({
+ id,
+ messages,
+ });
+
+ return {
+ ...response,
+ llmMessages,
+ };
+ };
+
+export const collectTextInstances = ({
+ instances,
+ rootInstanceId,
+ textComponents = new Set(["Heading", "Paragraph", "Text"]),
+}: {
+ instances: Instances;
+ rootInstanceId: Instance["id"];
+ textComponents?: Set<string>;
+}) => {
+ const textInstances: TextInstance[] = [];
+
+ const rootInstance = instances.get(rootInstanceId);
+
+ if (rootInstance === undefined) {
+ return textInstances;
+ }
+
+ 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,
+ })
+ );
+ }
+ });
+ }
+
+ return textInstances;
+};
diff --git a/packages/ai/src/chains/copywriter/chain.ts b/packages/ai/src/chains/copywriter/chain.ts
index 96f64bf7dc..f9b5a9f9f2 100644
--- a/packages/ai/src/chains/copywriter/chain.ts
+++ b/packages/ai/src/chains/copywriter/chain.ts
@@ -29,7 +29,7 @@ export type TextInstance = z.infer<typeof TextInstanceSchema>;
export const ContextSchema = z.object({
// The prompt provides context about the copy to generate and comes from the user.
- prompt: z.string().max(1200),
+ prompt: z.string(),
// An array of text nodes to generate copy for.
textInstances: z.array(TextInstanceSchema),
});
diff --git a/packages/ai/src/chains/copywriter/index.server.ts b/packages/ai/src/chains/copywriter/index.server.ts
new file mode 100644
index 0000000000..c815182484
--- /dev/null
+++ b/packages/ai/src/chains/copywriter/index.server.ts
@@ -0,0 +1 @@
+export * from "./chain";
diff --git a/packages/ai/src/chains/copywriter/index.ts b/packages/ai/src/chains/copywriter/index.ts
new file mode 100644
index 0000000000..2523eb8356
--- /dev/null
+++ b/packages/ai/src/chains/copywriter/index.ts
@@ -0,0 +1,7 @@
+export {
+ type Response,
+ ResponseSchema,
+ type TextInstance,
+ TextInstanceSchema,
+ collectTextInstances,
+} from "./chain";
diff --git a/packages/ai/src/chains/operations/__generated__/edit.system.prompt.ts b/packages/ai/src/chains/operations/__generated__/operations.system.prompt.ts
similarity index 100%
rename from packages/ai/src/chains/operations/__generated__/edit.system.prompt.ts
rename to packages/ai/src/chains/operations/__generated__/operations.system.prompt.ts
diff --git a/packages/ai/src/chains/operations/__generated__/edit.user.prompt.ts b/packages/ai/src/chains/operations/__generated__/operations.user.prompt.ts
similarity index 100%
rename from packages/ai/src/chains/operations/__generated__/edit.user.prompt.ts
rename to packages/ai/src/chains/operations/__generated__/operations.user.prompt.ts
diff --git a/packages/ai/src/chains/operations/chain.ts b/packages/ai/src/chains/operations/chain.ts
index 52954e9b8c..f390a2afc6 100644
--- a/packages/ai/src/chains/operations/chain.ts
+++ b/packages/ai/src/chains/operations/chain.ts
@@ -1,8 +1,8 @@
import { z } from "zod";
import type { Model as BaseModel, ModelMessage, Chain } from "../../types";
import { formatPrompt } from "../../utils/format-prompt";
-import { prompt as promptSystemTemplate } from "./__generated__/edit.system.prompt";
-import { prompt as promptUserTemplate } from "./__generated__/edit.user.prompt";
+import { prompt as promptSystemTemplate } from "./__generated__/operations.system.prompt";
+import { prompt as promptUserTemplate } from "./__generated__/operations.user.prompt";
import {
AiOperationsSchema,
@@ -11,7 +11,7 @@ import {
type WsOperations,
} from "./shared";
import { zodToJsonSchema } from "zod-to-json-schema";
-import { postProcessTemplate } from "../../utils/jsx-to-template";
+import { postProcessTemplate } from "../../utils/jsx-to-template.server";
import { createErrorResponse } from "../../utils/create-error-response";
/**
@@ -22,7 +22,7 @@ import { createErrorResponse } from "../../utils/create-error-response";
*/
export const ContextSchema = z.object({
- prompt: z.string().max(1200).describe("Edit request from the user"),
+ prompt: z.string().describe("Edit request from the user"),
components: z.array(z.string()).describe("Available Webstudio components"),
jsx: z.string().describe("Input JSX to edit"),
});
@@ -94,6 +94,7 @@ export const createChain = <ModelMessageFormat>(): Chain<
id,
...createErrorResponse({
status: 500,
+ error: "ai.parseError",
debug: `Failed to parse completion ${parsedCompletion.error.message}`,
}),
tokens: completion.tokens,
@@ -118,6 +119,7 @@ export const createChain = <ModelMessageFormat>(): Chain<
id,
...createErrorResponse({
status: 500,
+ error: "ai.parseError",
debug: (
"Failed to convert operations. " +
(error instanceof Error ? error.message : "")
diff --git a/packages/ai/src/chains/operations/generate-insert-template.ts b/packages/ai/src/chains/operations/generate-insert-template.ts
index f0f60d2a10..a27eb3948f 100644
--- a/packages/ai/src/chains/operations/generate-insert-template.ts
+++ b/packages/ai/src/chains/operations/generate-insert-template.ts
@@ -1,6 +1,6 @@
import { z } from "zod";
import { WsEmbedTemplate } from "@webstudio-is/react-sdk";
-import { jsxToTemplate } from "../../utils/jsx-to-template";
+import { jsxToTemplate } from "../../utils/jsx-to-template.server";
// Currently this operation is used is a separate LLM call after the main one has returned an insert-instance operation. This is to produce better results.
// Effectively the aiOperation from this module is not used. The separate chain will call the LLM and we use the resulting completion to construct an aiOperation by hand
diff --git a/packages/ai/src/chains/operations/index.server.ts b/packages/ai/src/chains/operations/index.server.ts
new file mode 100644
index 0000000000..f904abf7fd
--- /dev/null
+++ b/packages/ai/src/chains/operations/index.server.ts
@@ -0,0 +1,7 @@
+export * from "./chain";
+export * from "./shared";
+
+export * as editStyles from "./edit-styles";
+export * as generateTemplatePrompt from "./generate-template-prompt";
+export * as generateInsertTemplate from "./generate-insert-template";
+export * as deleteInstance from "./delete-instance";
diff --git a/packages/ai/src/chains/operations/index.ts b/packages/ai/src/chains/operations/index.ts
index f904abf7fd..e39fbb6892 100644
--- a/packages/ai/src/chains/operations/index.ts
+++ b/packages/ai/src/chains/operations/index.ts
@@ -1,7 +1,30 @@
-export * from "./chain";
-export * from "./shared";
+export { type Response, ResponseSchema } from "./chain";
-export * as editStyles from "./edit-styles";
-export * as generateTemplatePrompt from "./generate-template-prompt";
-export * as generateInsertTemplate from "./generate-insert-template";
-export * as deleteInstance from "./delete-instance";
+export {
+ AiOperationsSchema,
+ type AiOperations,
+ WsOperationsSchema,
+ type WsOperations,
+} from "./shared";
+
+// Operations.
+
+export {
+ aiOperation as editStylesAiOperation,
+ wsOperation as editStylesWsOperation,
+} from "./edit-styles";
+
+export {
+ aiOperation as generateTemplatePromptAiOperation,
+ wsOperation as generateTemplatePromptWsOperation,
+} from "./generate-template-prompt";
+
+export {
+ aiOperation as generateInsertTemplateAiOperation,
+ wsOperation as generateInsertTemplateWsOperation,
+} from "./generate-insert-template";
+
+export {
+ aiOperation as deleteInstanceAiOperation,
+ wsOperation as deleteInstanceWsOperation,
+} from "./delete-instance";
diff --git a/packages/ai/src/chains/operations/edit.system.prompt.md b/packages/ai/src/chains/operations/operations.system.prompt.md
similarity index 100%
rename from packages/ai/src/chains/operations/edit.system.prompt.md
rename to packages/ai/src/chains/operations/operations.system.prompt.md
diff --git a/packages/ai/src/chains/operations/edit.user.prompt.md b/packages/ai/src/chains/operations/operations.user.prompt.md
similarity index 100%
rename from packages/ai/src/chains/operations/edit.user.prompt.md
rename to packages/ai/src/chains/operations/operations.user.prompt.md
diff --git a/packages/ai/src/chains/template-generator/__generated__/template-generator.system.prompt.ts b/packages/ai/src/chains/template-generator/__generated__/template-generator.system.prompt.ts
index 28e59e83ee..e85aa7367f 100644
--- a/packages/ai/src/chains/template-generator/__generated__/template-generator.system.prompt.ts
+++ b/packages/ai/src/chains/template-generator/__generated__/template-generator.system.prompt.ts
@@ -2,7 +2,7 @@ export const prompt = `You are a design system engineer and your task is to gene
The user will provide a description of the design request and you will generate the JSX using the design system components below and Tailwind CSS classes for styling.
-The resulting JSX must represent a high-end, polished and detailed user interface. Meticulosly style every element and ensure proper layout, spacing. Add interesting touches like round corners and shadows. Unless explicitly asked by the user, use a black and white Tailwind CSS palette and use light mode.
+The resulting JSX must represent a high-end, polished and detailed user interface. Meticulosly style every element and ensure proper layout, spacing. Add interesting touches like round corners and shadows. Use the Tailwind CSS palette. Unless otherwise asked by the user, use a black and white colors and light mode.
Exclusively use components below and absolutely no other JSX element:
diff --git a/packages/ai/src/chains/template-generator/chain.ts b/packages/ai/src/chains/template-generator/chain.ts
index 5979d497c5..077037872e 100644
--- a/packages/ai/src/chains/template-generator/chain.ts
+++ b/packages/ai/src/chains/template-generator/chain.ts
@@ -7,7 +7,7 @@ import { WsEmbedTemplate } from "@webstudio-is/react-sdk";
import {
jsxToTemplate,
postProcessTemplate,
-} from "../../utils/jsx-to-template";
+} from "../../utils/jsx-to-template.server";
import { createErrorResponse } from "../../utils/create-error-response";
/**
@@ -18,7 +18,7 @@ import { createErrorResponse } from "../../utils/create-error-response";
export const ContextSchema = z.object({
// The prompt provides the original user request.
- prompt: z.string().max(1200),
+ prompt: z.string(),
components: z.array(z.string()),
});
export type Context = z.infer<typeof ContextSchema>;
diff --git a/packages/ai/src/chains/template-generator/index.server.ts b/packages/ai/src/chains/template-generator/index.server.ts
new file mode 100644
index 0000000000..c815182484
--- /dev/null
+++ b/packages/ai/src/chains/template-generator/index.server.ts
@@ -0,0 +1 @@
+export * from "./chain";
diff --git a/packages/ai/src/chains/template-generator/index.ts b/packages/ai/src/chains/template-generator/index.ts
new file mode 100644
index 0000000000..9c0952f12b
--- /dev/null
+++ b/packages/ai/src/chains/template-generator/index.ts
@@ -0,0 +1 @@
+export { type Response, ResponseSchema } from "./chain";
diff --git a/packages/ai/src/chains/template-generator/template-generator.system.prompt.md b/packages/ai/src/chains/template-generator/template-generator.system.prompt.md
index d135299a41..7e26aebab9 100644
--- a/packages/ai/src/chains/template-generator/template-generator.system.prompt.md
+++ b/packages/ai/src/chains/template-generator/template-generator.system.prompt.md
@@ -2,7 +2,7 @@ You are a design system engineer and your task is to generate a JSX tree for a d
The user will provide a description of the design request and you will generate the JSX using the design system components below and Tailwind CSS classes for styling.
-The resulting JSX must represent a high-end, polished and detailed user interface. Meticulosly style every element and ensure proper layout, spacing. Add interesting touches like round corners and shadows. Unless explicitly asked by the user, use a black and white Tailwind CSS palette and use light mode.
+The resulting JSX must represent a high-end, polished and detailed user interface. Meticulosly style every element and ensure proper layout, spacing. Add interesting touches like round corners and shadows. Use the Tailwind CSS palette. Unless otherwise asked by the user, use a black and white colors and light mode.
Exclusively use components below and absolutely no other JSX element:
diff --git a/packages/ai/src/index.server.ts b/packages/ai/src/index.server.ts
new file mode 100644
index 0000000000..346f77e6c1
--- /dev/null
+++ b/packages/ai/src/index.server.ts
@@ -0,0 +1,21 @@
+/** Chains */
+export * as commandDetect from "./chains/command-detect/index.server";
+export * as copywriter from "./chains/copywriter/index.server";
+export * as operations from "./chains/operations/index.server";
+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,
+} from "./models/gpt";
+
+/** Utils */
+export * from "./utils/request";
+export * from "./utils/create-error-response";
+export * from "./utils/streaming-text-response";
+
+/** Types */
+export * from "./types";
diff --git a/packages/ai/src/index.ts b/packages/ai/src/index.ts
index 9cc97dc8d3..2a3b96c82b 100644
--- a/packages/ai/src/index.ts
+++ b/packages/ai/src/index.ts
@@ -1,15 +1,8 @@
/** Chains */
-export * as copywriter from "./chains/copywriter/chain";
-export * as operations from "./chains/operations";
-export * as templateGenerator from "./chains/template-generator/chain";
-
-/** Models */
-export {
- create as createGptModel,
- type Model as GPTModel,
- type ModelConfig as GPTModelConfig,
- type ModelMessageFormat as GPTModelMessageFormat,
-} from "./models/gpt";
+export * as commandDetect from "./chains/command-detect/index";
+export * as copywriter from "./chains/copywriter/index";
+export * as operations from "./chains/operations/index";
+export * as templateGenerator from "./chains/template-generator/index";
/** Utils */
export * from "./utils/request";
diff --git a/packages/ai/src/types.ts b/packages/ai/src/types.ts
index 16eaa691aa..ad1a6af88e 100644
--- a/packages/ai/src/types.ts
+++ b/packages/ai/src/types.ts
@@ -77,11 +77,11 @@ export type ModelCompletionStream<ModelMessageFormat> = (args: {
* zod types must have a Schema suffix. For example ResponseSchema.
*/
-export type LlmResponse<ResponseData> = Response<ResponseData> & {
+export type ModelResponse<ResponseData> = Response<ResponseData> & {
llmMessages: ModelMessage[];
};
export type Chain<Model, Context, ResponseData> = (args: {
model: Model;
context: Context;
-}) => Promise<LlmResponse<ResponseData>>;
+}) => Promise<ModelResponse<ResponseData>>;
diff --git a/packages/ai/src/utils/jsx-to-template.ts b/packages/ai/src/utils/jsx-to-template.server.ts
similarity index 100%
rename from packages/ai/src/utils/jsx-to-template.ts
rename to packages/ai/src/utils/jsx-to-template.server.ts
diff --git a/packages/ai/src/utils/request.ts b/packages/ai/src/utils/request.ts
index f68d643dd5..919bfc674c 100644
--- a/packages/ai/src/utils/request.ts
+++ b/packages/ai/src/utils/request.ts
@@ -1,5 +1,5 @@
import { createChunkDecoder } from "ai";
-import type { LlmResponse } from "../types";
+import type { ModelResponse } from "../types";
import { createErrorResponse } from "./create-error-response";
import { StreamingTextResponse } from "./streaming-text-response";
@@ -84,7 +84,7 @@ export const request = <ResponseData>(
// @todo Convert the response types to Zod
// so that responses can be parsed and validated on the client.
- return (await response.json()) as LlmResponse<ResponseData>;
+ return (await response.json()) as ModelResponse<ResponseData>;
})
.catch((error) => {
return {
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index e681e7411c..15d374323e 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -4,7 +4,4 @@ export const unsupportedBrowsers = false;
export const displayContents = false;
// @todo this should be pro users check
export const adminRole = false;
-// A general flag to enable/disable all the AI features.
-export const ai = process.env.NODE_ENV !== "production";
-export const aiCopy = ai;
-export const aiOperations = ai;
+export const ai = false;
diff --git a/packages/jsx-utils/package.json b/packages/jsx-utils/package.json
index 636e164e68..6eb7df94ca 100644
--- a/packages/jsx-utils/package.json
+++ b/packages/jsx-utils/package.json
@@ -23,9 +23,16 @@
"typescript": "5.2.2"
},
"exports": {
- "webstudio": "./src/index.ts",
- "types": "./lib/types/index.d.ts",
- "import": "./lib/index.js"
+ ".": {
+ "webstudio": "./src/index.ts",
+ "types": "./lib/types/index.d.ts",
+ "import": "./lib/index.js"
+ },
+ "./index.server": {
+ "webstudio": "./src/index.server.ts",
+ "types": "./lib/types/index.server.d.ts",
+ "import": "./lib/index.server.js"
+ }
},
"files": [
"lib/*",
diff --git a/packages/jsx-utils/src/index.server.ts b/packages/jsx-utils/src/index.server.ts
new file mode 100644
index 0000000000..55e016148b
--- /dev/null
+++ b/packages/jsx-utils/src/index.server.ts
@@ -0,0 +1 @@
+export { jsxToWSEmbedTemplate } from "./jsx";