/** * The action "AI Phone Agent" (aiCall) in the builder: the form only links to an agent of * Funnelforms → AI Phone Agent, which brings voice, number, questions and call hours. Here the * agent is chosen, the phone and consent fields are found (or the consent field is added), and the * form says what the agent learns from it: all answers, chosen ones or none, and what it is about. */ import React, { useEffect, useState, type ReactNode } from 'react'; import type { AiCallConfig, FormDocument } from '@funnelforms/form-schema'; import { consentFields, phoneFields, questionFields, type AiAgentSummary } from '../aicall'; import { addField, text } from '../document'; import { setText } from '../languages'; import type { Recipe } from '../store'; import { useEnv } from './context'; import { fieldTitle } from './describe'; import { __, sprintf } from './i18n'; import { Badge, Button, Notice, Select, TextInput } from './ui'; /** Funnelforms → AI Phone Agent, relative to wp-admin where the builder runs. */ export const LEADCALLER_URL = 'admin.php?page=funnelforms-leadcaller'; export function goalName( goal: string ): string { const names: Record = { qualify: __( 'Qualify the lead', 'funnelforms-pro' ), appointment: __( 'Make an appointment', 'funnelforms-pro' ), confirm: __( 'Confirm the request', 'funnelforms-pro' ), followUp: __( 'Follow up on an offer', 'funnelforms-pro' ), }; return names[ goal ] ?? goal; } /** * A consent box by what the lead reads: its label, else the start of its text, never its key. */ export function consentTitle( field: { key: string; label?: Record; props?: unknown }, locale: string ): string { const label = text( field.label, locale ).replace( /<[^>]+>/g, '' ).trim(); if ( label ) { return label; } const said = text( ( field.props as { html?: Record } | undefined )?.html, locale ) .replace( /<[^>]+>/g, '' ) .replace( /\s+/g, ' ' ) .trim(); return said ? ( said.length > 70 ? said.slice( 0, 69 ).trimEnd() + '…' : said ) : __( 'Consent', 'funnelforms-pro' ); } export function AiCallEditor( { doc, id, config, change, common, }: { doc: FormDocument; id: string; config: AiCallConfig; change: ( recipe: Recipe, key?: string ) => void; /** When and for which entries. */ common: ReactNode; } ) { const { api, rights, readOnly, ctx } = useEnv(); const locale = doc.locales.default; const [ agents, setAgents ] = useState( null ); const [ error, setError ] = useState( null ); const phones = phoneFields( doc ); const consents = consentFields( doc ); const questions = questionFields( doc ); const share = config.share ?? 'all'; const shared = config.fields ?? []; const agent = agents?.find( ( a ) => a.id === config.agent ); const set = ( apply: ( c: AiCallConfig ) => void ): void => change( ( d ) => { const a = d.actions.find( ( x ) => x.id === id ); if ( a && 'aiCall' === a.kind ) { apply( a.config ); } } ); useEffect( () => { let live = true; if ( api.aiAgents ) { api.aiAgents().then( ( list ) => { if ( ! live ) { return; } setAgents( list ); // One agent: it is the one, so the action is complete right away. const only = list[ 0 ]; if ( 1 === list.length && only && ! config.agent && ! readOnly ) { set( ( c ) => { c.agent = only.id; } ); } }, ( e: unknown ) => live && setError( e instanceof Error ? e.message : String( e ) ) ); } else { setAgents( [] ); } return () => { live = false; }; // Once per opened action. }, [ api ] ); /** Adds a consent box to the contact step with the phone field (or the first) and picks it. */ const addConsent = (): void => change( ( d ) => { const node = d.nodes.find( ( n ) => 'contact' === n.type && n.fields.some( ( f ) => f.id === ( config.phoneField || phones[ 0 ]?.id ) ) ) ?? d.nodes.find( ( n ) => 'contact' === n.type ); if ( ! node ) { return; } const created = addField( d, node.id, 'consent', ctx ); const field = 'contact' === node.type ? node.fields.find( ( f ) => f.id === created ) : undefined; if ( ! created || ! field ) { return; } const props = ( field.props ?? {} ) as { html?: Record }; props.html = props.html ?? {}; setText( props.html, locale, __( 'Yes, you may call me about my request, also with an AI assistant.', 'funnelforms-pro' ), locale ); field.props = props as typeof field.props; field.required = false; const a = d.actions.find( ( x ) => x.id === id ); if ( a && 'aiCall' === a.kind ) { a.config.consentField = created; } } ); const agentOptions = [ { value: '', label: __( 'Choose…', 'funnelforms-pro' ) }, ...( agents ?? [] ).map( ( a ) => ( { value: a.id, label: `${ a.name } · ${ goalName( a.goal ) }` } ) ) ]; if ( config.agent && agents && ! agent ) { agentOptions.push( { value: config.agent, label: __( 'Deleted agent', 'funnelforms-pro' ) } ); } return ( <> { error ? { error } : null } { agents && ! agents.length ? ( { __( 'Open AI Phone Agent', 'funnelforms-pro' ) } ) : undefined } > { __( 'Under Funnelforms → AI Phone Agent you connect ElevenLabs, pick a number and create an agent: what it asks, its voice and when it calls. Here you only link it.', 'funnelforms-pro' ) } ) : ( <> ( { value: f.id, label: fieldTitle( f as never, locale ) } ) ) } disabled={ readOnly } hint={ __( 'The phone field of the form whose number the agent calls.', 'funnelforms-pro' ) } onChange={ ( v ) => set( ( c ) => { c.phoneField = v; } ) } /> ) : ( { __( 'Add a phone field to the contact step; the agent calls that number.', 'funnelforms-pro' ) } ) } { consents.length ? ( set( ( c ) => { c.share = v; if ( 'some' === v && ! c.fields ) { c.fields = questions.map( ( f ) => f.id ); } } ) } /> { 'some' === share ? ( questions.length ? (
{ questions.map( ( f ) => ( ) ) }
) : (

{ __( 'The form has no questions yet.', 'funnelforms-pro' ) }

) ) : null }

{ __( 'Of the contact details the agent only gets the name, to address the lead. E-mail address, phone number and the rest stay on your website.', 'funnelforms-pro' ) }

{ readOnly ? null : ( set( ( c ) => { c.context = v.slice( 0, 1000 ); } ) } /> ) }

{ sprintf( /* translators: %s: name of the service, ElevenLabs */ __( 'Calls, voice and transcript are processed by %s for you. Mention it in your privacy policy and conclude its data processing agreement.', 'funnelforms-pro' ), 'ElevenLabs' ) }

{ common } ); }