- AI generates: ````formedible\n{JSON config}\n`````` code blocks containing Formedible configuration
- Extraction:
extractFormedibleCode()extracts the JSON config from markdown code blocks - Form Generation: JSON config gets passed to
useFormedible(config) - Sandbox Preview: Shows the actual working Formedible form
- The
sandbox-code-injector.tshas garbage logic trying to handle "JSON as React components" with base64 encoding - The sandbox preview tries to parse JSON as React component code instead of passing it to
useFormedible - The
createSafeFormComponentfunction is completely wrong - it should just useuseFormedible
Current problem: The extractFormComponent() function tries to detect JSON and create a "safe component" with base64 encoding.
Fix:
- Remove all the
createSafeFormComponentgarbage - The
extractFormComponent()should simply return the JSON config as-is - The sandbox should receive this JSON config and pass it to
useFormedible
Current: Has complex form generation logic trying to build React components from fields
Fix:
- FormComponent should be simple: takes config prop, calls
useFormedible(config), returns<Form /> - Remove all the field generation logic -
useFormediblehandles everything
Current: Complex logic in files useMemo that tries to inject React component code
Fix:
- Parse
formCodeas JSON to get the config - Pass config to simple FormComponent template
- Let
useFormediblehandle all the form rendering
Lines to change: extractFormComponent() function (lines 568-629)
Current: Complex JSON detection + base64 encoding + createSafeFormComponent
Replace with: Simple function that returns the JSON config string as-is
Lines to change: generateFormComponentFromFields() function
Current: Generates React component code with field definitions
Replace with: Simple template that takes config prop and calls useFormedible(config)
Lines to change: files useMemo (lines 264-369)
Current: Complex injection logic with code splitting and caching
Replace with: Simple logic that passes JSON config to FormComponent
import React from 'react';
import { useFormedible } from './use-formedible';
interface FormComponentProps {
onSubmit?: (data: Record<string, any>) => void;
onError?: (error: Error) => void;
onChange?: (data: Record<string, any>) => void;
}
// The config will be injected here as a constant
const FORM_CONFIG = {CONFIG_JSON_HERE};
export default function FormComponent({ onSubmit, onError, onChange }: FormComponentProps) {
const { Form } = useFormedible({
...FORM_CONFIG,
formOptions: {
...FORM_CONFIG.formOptions,
onSubmit: async ({ value }) => {
onSubmit?.(value);
}
}
});
return <Form />;
}- Keep all Sandpack infrastructure: Dependencies, provider setup, toolbar, console, etc.
- Only change form rendering logic: Don't touch Sandpack setup or UI components
- Test with simple config first: Start with basic field config to verify it works
- Preserve error handling: Maintain error boundaries and error messaging
/packages/ai-builder/src/lib/sandbox-code-injector.ts- Fix extractFormComponent/packages/ai-builder/src/lib/sandbox-templates.ts- Simplify FormComponent template/packages/ai-builder/src/components/formedible/ai/sandpack-preview.tsx- Fix injection logic- Copy changes to
/apps/web/equivalents using sync script
- AI generates JSON config → Sandpack shows working Formedible form
- All field types work correctly (text, select, checkbox, etc.)
- Multi-page forms work
- Conditional logic works
- Validation works
- Form submission works