/** * The AI form builder (Funnelforms Pro): a new form from a description of the business and the * goal (a step of the new-form dialog), and changes to the open form by an instruction (the AI bar * above the workspace, applied as one change that can be undone). The bar is one way in for every * change: questions and answers, paths, calculations, quizzes, texts and the design. Calculations, * the quiz and the selected step open it with a start of the instruction (AiAsk). The plugin sends * the text through the Funnelforms server to the AI and checks what comes back; nothing is * published by itself. */ import React, { createContext, useContext, useEffect, useRef, useState } from 'react'; import type { AiConfig, FormsApi } from '../api'; import { useBuilder, useEnv } from './context'; import { __, sprintf } from './i18n'; import { languageLabel } from './Languages'; import { Button, Field, IconButton, icons, Notice } from './ui'; /** Longest description or instruction the plugin takes (AiForms::MAX_PROMPT). */ export const AI_MAX_PROMPT = 2000; const AI_MIN_PROMPT = 3; function createExamples(): string[] { return [ __( 'Quote requests for a roofing company: kind of roof, size, timeline and a callback', 'funnelforms-pro' ), __( 'Price calculator for window cleaning by number of windows and floors', 'funnelforms-pro' ), __( 'Free first consultation for a tax advisor, with a choice of topics', 'funnelforms-pro' ), __( 'Job application for nurses with preferred shifts and a CV upload', 'funnelforms-pro' ), __( 'Product finder quiz for an online shop that sells mattresses', 'funnelforms-pro' ), ]; } /** The topics of the AI bar, each with an example instruction. */ function barTopics(): Array<{ label: string; example: string }> { return [ { label: __( 'Questions and answers', 'funnelforms-pro' ), example: __( 'Ask for the budget after the second question and add the answer "Not sure yet"', 'funnelforms-pro' ) }, { label: __( 'Paths', 'funnelforms-pro' ), example: __( 'Visitors who choose "Rent" skip the question about the budget', 'funnelforms-pro' ) }, { label: __( 'Calculation', 'funnelforms-pro' ), example: __( 'Calculate a price: 50 euros per square metre plus 200 euros for the visit, shown before the contact step', 'funnelforms-pro' ) }, { label: __( 'Quiz', 'funnelforms-pro' ), example: __( 'Turn the form into a quiz: each right answer scores one point, with three results', 'funnelforms-pro' ) }, { label: __( 'Design', 'funnelforms-pro' ), example: __( 'Make the buttons blue and the corners rounder', 'funnelforms-pro' ) }, { label: __( 'Texts', 'funnelforms-pro' ), example: __( 'Write all texts in a friendlier tone', 'funnelforms-pro' ) }, ]; } function progressTexts(): string[] { return [ __( 'Reading your description…', 'funnelforms-pro' ), __( 'Planning the questions and paths…', 'funnelforms-pro' ), __( 'Writing the texts…', 'funnelforms-pro' ), __( 'Checking the form…', 'funnelforms-pro' ) ]; } /** Where the text goes; shown before it is sent. */ export function aiPrivacyText(): string { return __( 'Your text is sent to the Funnelforms server and to Anthropic, which writes the form. It is not stored.', 'funnelforms-pro' ); } function message( e: unknown ): string { return e instanceof Error ? e.message : String( e ); } /** The text of the request with example chips, the note on privacy and the progress while the AI works. */ function PromptBox( { label, placeholder, value, onChange, examples, busy, onSubmit }: { label: string; placeholder: string; value: string; onChange: ( value: string ) => void; examples: string[]; busy: boolean; onSubmit: () => void } ) { const texts = progressTexts(); const [ phase, setPhase ] = useState( 0 ); useEffect( () => { if ( ! busy ) { setPhase( 0 ); return undefined; } const timer = setInterval( () => setPhase( ( p ) => Math.min( p + 1, texts.length - 1 ) ), 8000 ); return () => clearInterval( timer ); }, [ busy, texts.length ] ); return (
{ ( id ) => (