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: Miscellaneous AI fixes (#2483)

Giuseppe0cefb0385c04
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 6fcb5dc382..5cb36b4e4a 100644
--- a/apps/builder/app/builder/features/ai/ai-fetch-result.ts
+++ b/apps/builder/app/builder/features/ai/ai-fetch-result.ts
@@ -38,6 +38,7 @@ import {
RateLimitException,
textToRateLimitMeta,
} from "./api-exceptions";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
const unknownArray = z.array(z.unknown());
@@ -208,8 +209,12 @@ const $availableComponentsNames = computed(
"Body",
"Slot",
// @todo Remove Radix exclusion when the model has been fine-tuned to understand them.
- "@webstudio-is/sdk-components-react-radix:",
- ];
+ isFeatureEnabled("aiRadixComponents")
+ ? "@webstudio-is/sdk-components-react-radix:"
+ : undefined,
+ ].filter(function <T>(value: T): value is NonNullable<T> {
+ return value !== undefined;
+ });
return [...metas.keys()]
.filter((name) => !exclude.some((excluded) => name.startsWith(excluded)))
diff --git a/apps/builder/app/builder/features/ai/apply-operations.ts b/apps/builder/app/builder/features/ai/apply-operations.ts
index 4035c58fca..354901487a 100644
--- a/apps/builder/app/builder/features/ai/apply-operations.ts
+++ b/apps/builder/app/builder/features/ai/apply-operations.ts
@@ -75,56 +75,25 @@ const insertTemplateByOp = (
.filter((child) => child.type === "id")
.map((child) => child.value);
- const dropTarget: DroppableTarget = {
- parentSelector: [operation.addTo],
- position: operation.addAtIndex + 1,
- };
-
- insertTemplateData(templateData, dropTarget);
- return rootInstanceIds;
+ const instanceSelector = computeSelectorForInstanceId(operation.addTo);
+ if (instanceSelector) {
+ const dropTarget: DroppableTarget = {
+ parentSelector: instanceSelector,
+ position: operation.addAtIndex + 1,
+ };
+
+ insertTemplateData(templateData, dropTarget);
+ return rootInstanceIds;
+ }
};
const deleteInstanceByOp = (
operation: operations.deleteInstanceWsOperation
) => {
- const selectedInstanceSelector = selectedInstanceSelectorStore.get();
- if (selectedInstanceSelector === undefined) {
- return;
+ const instanceSelector = computeSelectorForInstanceId(operation.wsId);
+ if (instanceSelector) {
+ _deleteInstance(instanceSelector);
}
-
- // For a given instance to delete we compute the subtree selector between
- // that instance and the selected instance (a parent).
- let subtreeSelector: InstanceSelector = [];
- const parentInstancesById = new Map<Instance["id"], Instance["id"]>();
- for (const instance of instancesStore.get().values()) {
- for (const child of instance.children) {
- if (child.type === "id") {
- parentInstancesById.set(child.value, instance.id);
- }
- }
- }
- const selector: InstanceSelector = [];
- let currentInstanceId: undefined | Instance["id"] = operation.wsId;
- while (currentInstanceId) {
- selector.push(currentInstanceId);
- currentInstanceId = parentInstancesById.get(currentInstanceId);
- if (currentInstanceId === selectedInstanceSelector[0]) {
- subtreeSelector = [...selector, ...selectedInstanceSelector];
- break;
- }
- }
-
- if (subtreeSelector.length === 0) {
- return;
- }
-
- const parentSelector = selectedInstanceSelector.slice(1);
- // Combine the subtree selector with the selected instance one
- // to get the full and final selector.
- const combinedSelector = [...subtreeSelector, ...parentSelector];
-
- // Finally delete the instance by selector.
- _deleteInstance(combinedSelector);
};
const applyStylesByOp = (operation: operations.editStylesWsOperation) => {
@@ -187,6 +156,50 @@ const applyStylesByOp = (operation: operations.editStylesWsOperation) => {
);
};
+const computeSelectorForInstanceId = (instanceId: Instance["id"]) => {
+ const selectedInstanceSelector = selectedInstanceSelectorStore.get();
+ if (selectedInstanceSelector === undefined) {
+ return;
+ }
+
+ // When the instance is the selected instance return selectedInstanceSelector right away.
+ if (instanceId === selectedInstanceSelector[0]) {
+ return selectedInstanceSelector;
+ }
+
+ // For a given instance to delete we compute the subtree selector between
+ // that instance and the selected instance (a parent).
+ let subtreeSelector: InstanceSelector = [];
+ const parentInstancesById = new Map<Instance["id"], Instance["id"]>();
+ for (const instance of instancesStore.get().values()) {
+ for (const child of instance.children) {
+ if (child.type === "id") {
+ parentInstancesById.set(child.value, instance.id);
+ }
+ }
+ }
+ const selector: InstanceSelector = [];
+ let currentInstanceId: undefined | Instance["id"] = instanceId;
+ while (currentInstanceId) {
+ selector.push(currentInstanceId);
+ currentInstanceId = parentInstancesById.get(currentInstanceId);
+ if (currentInstanceId === selectedInstanceSelector[0]) {
+ subtreeSelector = [...selector, ...selectedInstanceSelector];
+ break;
+ }
+ }
+
+ if (subtreeSelector.length === 0) {
+ return;
+ }
+
+ const parentSelector = selectedInstanceSelector.slice(1);
+ // Combine the subtree selector with the selected instance one
+ // to get the full and final selector.
+ const combinedSelector = [...subtreeSelector, ...parentSelector];
+ return combinedSelector;
+};
+
export const patchTextInstance = (textInstance: copywriter.TextInstance) => {
serverSyncStore.createTransaction([instancesStore], (instances) => {
const currentInstance = instances.get(textInstance.instanceId);
diff --git a/apps/builder/app/routes/rest.ai._index.ts b/apps/builder/app/routes/rest.ai._index.ts
index c0c27e19e7..c5a70dbc6f 100644
--- a/apps/builder/app/routes/rest.ai._index.ts
+++ b/apps/builder/app/routes/rest.ai._index.ts
@@ -229,7 +229,7 @@ export const action = async ({ request }: ActionArgs) => {
context: {
prompt:
operation.llmPrompt +
- (operation.classNames.length > 0
+ (operation.classNames && operation.classNames.length > 0
? `.\nSuggested Tailwind classes: ${operation.classNames}`
: ""),
diff --git a/packages/ai/src/chains/operations/__generated__/operations.system.prompt.ts b/packages/ai/src/chains/operations/__generated__/operations.system.prompt.ts
index 8d25ffc980..bf273162f1 100644
--- a/packages/ai/src/chains/operations/__generated__/operations.system.prompt.ts
+++ b/packages/ai/src/chains/operations/__generated__/operations.system.prompt.ts
@@ -6,7 +6,9 @@ The available operations are defined by the following JSON schema which you shou
{operationsSchema}
\`\`\`
-Properties value might contain descriptions with instructions on how to fill them out. When they do please keep those in mind when generating a completion.
+All the required schema properties must have values.
+
+Properties value contain descriptions with instructions on how to fill them out. When they do please keep those in mind when generating a completion.
Respond with an array of operations as JSON and no other text. Start with [{"operation":
`;
diff --git a/packages/ai/src/chains/operations/generate-template-prompt.ts b/packages/ai/src/chains/operations/generate-template-prompt.ts
index 63420f1894..39e0f72cbc 100644
--- a/packages/ai/src/chains/operations/generate-template-prompt.ts
+++ b/packages/ai/src/chains/operations/generate-template-prompt.ts
@@ -29,6 +29,7 @@ export const aiOperation = z.object({
),
classNames: z
.string()
+ .optional()
.describe(
"A list of suggested Tailwind CSS classes matching the style of the request code. Always use the square brackets notation eg. mb-[10px] instead of mb-10"
),
diff --git a/packages/ai/src/chains/operations/operations.system.prompt.md b/packages/ai/src/chains/operations/operations.system.prompt.md
index f217c9bc75..b4f82a9558 100644
--- a/packages/ai/src/chains/operations/operations.system.prompt.md
+++ b/packages/ai/src/chains/operations/operations.system.prompt.md
@@ -6,6 +6,8 @@ The available operations are defined by the following JSON schema which you shou
{operationsSchema}
```
-Properties value might contain descriptions with instructions on how to fill them out. When they do please keep those in mind when generating a completion.
+All the required schema properties must have values.
+
+Properties value contain descriptions with instructions on how to fill them out. When they do please keep those in mind when generating a completion.
Respond with an array of operations as JSON and no other text. Start with [{"operation":
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index 15d374323e..d2d960851a 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -5,3 +5,4 @@ export const displayContents = false;
// @todo this should be pro users check
export const adminRole = false;
export const ai = false;
+export const aiRadixComponents = false;
diff --git a/packages/jsx-utils/src/jsx.ts b/packages/jsx-utils/src/jsx.ts
index 519299a7ce..ec75a08a0c 100644
--- a/packages/jsx-utils/src/jsx.ts
+++ b/packages/jsx-utils/src/jsx.ts
@@ -1,4 +1,5 @@
import * as parser from "@babel/parser";
+import type { ParseError } from "@babel/parser";
import {
type JSXElement,
type JSXOpeningElement,
@@ -16,11 +17,25 @@ export const jsxToWSEmbedTemplate = async (
): Promise<WsEmbedTemplate> => {
code = code.trim();
- const ast = parser.parseExpression(code, {
- plugins: ["jsx"],
- });
+ let ast;
+
+ try {
+ ast = parser.parseExpression(code, {
+ plugins: ["jsx"],
+ });
+ } catch (error) {
+ if (
+ error &&
+ (error as ParseError).reasonCode === "UnwrappedAdjacentJSXElements"
+ ) {
+ code = `<Fragment>${code}</Fragment>`;
+ ast = parser.parseExpression(code, {
+ plugins: ["jsx"],
+ });
+ }
+ }
- if (ast.type === "JSXElement") {
+ if (ast && ast.type === "JSXElement") {
const template = await transform(ast, code);
if (template !== null) {
// Validate template