/** The length a written field should land in, as the agent aims for it. */ export interface Band { min?: number; max: number; unit: 'characters' | 'words'; } export const countIn = (unit: Band['unit'], text: string): number => unit === 'words' ? text.split(/\s+/).filter(Boolean).length : text.length; /** "50–60 characters", "up to 75 characters". */ export const bandLabel = (band: Band): string => band.min !== undefined ? `${band.min}–${band.max} ${band.unit}` : `up to ${band.max} ${band.unit}`; /** How long the text is, counted the way its band counts: "58 characters", "3 words". */ export const measure = (band: Band, text: string): string => { const count = countIn(band.unit, text); return `${count} ${count === 1 ? band.unit.slice(0, -1) : band.unit}`; }; export const outsideBand = (band: Band, text: string): boolean => { const count = countIn(band.unit, text); return count > band.max || (band.min !== undefined && count < band.min); };