apps/web/src/lib/salesConfirmation.ts

1import type { UserProfile } from '@local/ai'
2import { confirmedHandoff } from '@local/ai/components/workflows'
3import { HANDOFF_CONSENT, HANDOFF_QUESTIONS } from '@local/config/ai-consent'
4import { missingContactQuestion } from './handoffContact'
5
6export const SALES_CONFIRMATION =
7	'Shall I send this sponsorship or exhibiting enquiry to the team?'
8
9/** Preserve the visitor's stated commercial objective without inventing or extracting one from an email. */
10export function commercialRequestDetails(
11	history: { role: 'user' | 'assistant'; text: string }[],
12	message: string,
13): string | undefined {
14	return [...history, { role: 'user', text: message }]
15		.findLast(
16			(turn) =>
17				turn.role === 'user' &&
18				/\b(?:to (?:connect|showcase|generate|increase|meet|promote)|objectives?|interested in (?:sponsor|exhibit)|want (?:an? |only an? )?exhibition stand|looking to (?:sponsor|exhibit))\b/i.test(
19					turn.text,
20				),
21		)
22		?.text.trim()
23		.slice(0, 800)
24}
25
26/** Permission to start a conversation is not permission to dispatch a new enquiry. */
27export function salesAcceptanceReply(
28	profile: UserProfile | undefined,
29	event: 'paris' | 'miami',
30	_consentGiven: boolean,
31	consentStatement: string = HANDOFF_CONSENT.sponsorship,
32	requestDetails?: string,
33): string {
34	if (!profile?.company)
35		return 'What company are you with, and what would you like to achieve through sponsoring or exhibiting?'
36	const question = missingContactQuestion(profile)
37	if (question) return question
38	return `I’ll ask the team to discuss sponsorship or exhibiting opportunities at UNLEASH in ${event === 'paris' ? 'Paris' : 'Miami'} with you.\n\nContact: ${profile.name}, ${profile.company}${profile.role ? ` (${profile.role})` : ''}, ${profile.email}.${requestDetails ? `\n\nYour request: ${requestDetails}` : ''}\n\n${consentStatement}`
39}
40
41/** Existing session consent is retained; this confirms the particular new request. */
42export function confirmedSalesRequest(
43	history: { role: 'user' | 'assistant'; text: string }[],
44	message: string,
45): boolean {
46	const previous = history.findLast((t) => t.role === 'assistant')?.text
47	return Boolean(
48		(previous?.endsWith(SALES_CONFIRMATION) ||
49			previous?.endsWith(HANDOFF_QUESTIONS.sponsorship)) &&
50			confirmedHandoff(
51				[
52					{
53						role: 'assistant',
54						text: 'Shall I send this request with your consent?',
55					},
56				],
57				message,
58			),
59	)
60}
61