All files / src/flow types.ts

100% Statements 505/505
100% Branches 0/0
100% Functions 0/0
100% Lines 505/505

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x  
import { TemplateResult } from 'lit-html';
import { Action, Node, NodeUI } from '../store/flow-definition';
 
export interface ValidationResult {
  valid: boolean;
  errors: { [key: string]: string };
}
 
/**
 * Flow types - defines the type of flow being edited
 */
export const FlowTypes = {
  MESSAGE: 'message',
  VOICE: 'voice',
  BACKGROUND: 'background'
} as const;
 
export type FlowType = (typeof FlowTypes)[keyof typeof FlowTypes];
 
/**
 * Shortcut entry shown in the canvas context menu. The `type` is the
 * action or node type to open the editor with when selected.
 */
export interface ContextMenuShortcut {
  type: string;
  name: string;
  icon: string;
}
 
/**
 * Per-flow-type context menu shortcuts.
 */
export const CONTEXT_MENU_SHORTCUTS: Record<FlowType, ContextMenuShortcut[]> = {
  [FlowTypes.MESSAGE]: [
    { type: 'send_msg', name: 'Send Message', icon: 'send' },
    { type: 'wait_for_response', name: 'Wait for Response', icon: 'message' }
  ],
  [FlowTypes.VOICE]: [
    { type: 'say_msg', name: 'Say Message', icon: 'recording' },
    { type: 'wait_for_menu', name: 'Wait for Menu', icon: 'dots-grid' }
  ],
  [FlowTypes.BACKGROUND]: [
    { type: 'set_contact_field', name: 'Update Contact', icon: 'contact' }
  ]
};
 
/**
 * Features - defines the features available in the account
 */
export const Features = {
  AI: 'ai',
  AIRTIME: 'airtime',
  LOCATIONS: 'locations'
} as const;
 
export type Feature = (typeof Features)[keyof typeof Features];
 
// Component attribute interfaces - these define what's allowed for each component type
export interface TextInputAttributes {
  type?: 'text' | 'email' | 'number' | 'url' | 'tel';
  placeholder?: string;
  clearable?: boolean;
  maxlength?: number;
  gsm?: boolean;
  autogrow?: boolean;
  textarea?: boolean;
  submitOnEnter?: boolean;
}
 
export interface CompletionAttributes {
  placeholder?: string;
  clearable?: boolean;
  maxlength?: number;
  gsm?: boolean;
  autogrow?: boolean;
  textarea?: boolean;
  expressions?: string;
  counter?: string;
  minHeight?: number;
}
 
export interface SelectAttributes {
  placeholder?: string;
  multi?: boolean;
  searchable?: boolean;
  tags?: boolean;
  emails?: boolean;
  clearable?: boolean;
  endpoint?: string;
  valueKey?: string;
  nameKey?: string;
  queryParam?: string;
  maxItems?: number;
  maxItemsText?: string;
  expressions?: string;
  options?: Array<{ name: string; value: any }>;
  sorted?: boolean;
  allowCreate?: boolean;
  jsonValue?: boolean;
  spaceSelect?: boolean;
  infoText?: string;
}
 
export interface CheckboxAttributes {
  label?: string;
  size?: number;
  disabled?: boolean;
  animateChange?: string;
}
 
export interface SliderAttributes {
  min?: number;
  max?: number;
  range?: boolean;
}
 
export interface FormData extends Record<string, any> {}
 
export interface FormConfig {
  form?: Record<string, FieldConfig>;
  layout?: LayoutItem[];
  gutter?: LayoutItem[];
  sanitize?: (formData: FormData) => void;
  validate?: (formData: FormData) => ValidationResult;
 
  // Async hook to resolve additional data after form initialization.
  // Called once when the form opens. Useful for fetching metadata (e.g. parent_refs)
  // that isn't available from the node data alone.
  resolveFormData?: (formData: FormData) => Promise<FormData>;
}
 
export interface NodeConfig extends FormConfig {
  type: string;
  name?: string;
  aliases?: string[]; // alternate type names for backwards compatibility (won't show in selector)
  group?: ActionGroup | SplitGroup; // Nodes can use either when showAsAction is true
  dialogSize?: 'small' | 'medium' | 'large' | 'xlarge';
  action?: ActionConfig;
  showAsAction?: boolean; // if true, show in action dialog instead of splits (default: false - nodes show in splits)
  hideFromSplits?: boolean; // if true, don't show in split dialog (e.g. promoted to context menu)
  flowTypes?: FlowType[]; // which flow types this node is available for (defaults to all if not specified)
  features?: Feature[]; // which features are required for this node (all must be present)
  router?: {
    type: 'switch' | 'random';
    defaultCategory?: string;
    operand?: string;
    configurable?: boolean; // can the rules be configured in the UI
    rules?: {
      type:
        | 'has_number_between'
        | 'has_number_eq'
        | 'has_only_text'
        | 'has_string'
        | 'has_value'
        | 'has_not_value'
        | 'has_text';
      arguments: string[];
      categoryName: string;
    }[];
  };
 
  toFormData?: (node: Node, nodeUI?: any) => FormData;
  fromFormData?: (formData: FormData, originalNode: Node) => Node;
  toUIConfig?: (formData: FormData) => Record<string, any>;
  render?: (node: Node, nodeUI?: any) => TemplateResult;
  renderTitle?: (node: Node, nodeUI?: NodeUI) => TemplateResult;
 
  // Localization support for router categories
  localizable?: 'categories'; // Only categories are localizable for routers
  // Categories that should not be translated. 'all' means all system categories
  // are non-translatable (except Other/No Response). A string[] lists specific names.
  nonTranslatableCategories?: 'all' | string[];
  toLocalizationFormData?: (
    node: Node,
    localization: Record<string, any>
  ) => FormData;
  fromLocalizationFormData?: (
    formData: FormData,
    node: Node
  ) => Record<string, any>;
}
 
// New field configuration system for generic form generation
export interface BaseFieldConfig {
  label?: string | ((formData: Record<string, any>) => string);
  required?: boolean;
  evaluated?: boolean;
  dependsOn?: string[];
  computeValue?: (
    values: Record<string, any>,
    currentValue: any,
    originalValues?: Record<string, any>
  ) => any;
 
  // Validation properties
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  helpText?: string | ((formData: Record<string, any>) => string);
 
  // Layout properties
  maxWidth?: string;
  width?: string;
 
  // Conditional rendering
  conditions?: {
    visible?: (formData: Record<string, any>) => boolean;
    disabled?: (formData: Record<string, any>) => boolean;
  };
 
  // Optional field with reveal link
  // When set, the field is hidden by default and a link with this text is shown
  // Clicking the link reveals the field permanently (can't be hidden again)
  optionalLink?: string;
}
 
export interface TextFieldConfig extends BaseFieldConfig {
  type: 'text';
  placeholder?: string | ((formData: Record<string, any>) => string);
  flavor?: 'xsmall' | 'small' | 'large';
}
 
export interface TextareaFieldConfig extends BaseFieldConfig {
  type: 'textarea';
  placeholder?: string;
  rows?: number;
  minHeight?: number;
}
 
export interface SelectFieldConfig extends BaseFieldConfig {
  type: 'select';
  options?: string[] | { value: string; name: string }[];
  multi?: boolean;
  clearable?: boolean;
  searchable?: boolean;
  tags?: boolean;
  placeholder?: string;
  maxItems?: number;
  valueKey?: string;
  nameKey?: string;
  endpoint?: string;
  queryParam?: string;
  emails?: boolean;
  expressions?: string;
  getName?: (item: any) => string;
  flavor?: 'xsmall' | 'small' | 'large';
  createArbitraryOption?: (input: string, options: any[]) => any;
  allowCreate?: boolean;
  getDynamicOptions?: () => Array<{ value: string; name: string }>;
  shouldExclude?: (option: any) => boolean;
}
 
export interface KeyValueFieldConfig extends BaseFieldConfig {
  type: 'key-value';
  sortable?: boolean;
  keyPlaceholder?: string;
  valuePlaceholder?: string;
  minRows?: number;
  readOnlyKeys?: boolean;
}
 
export interface ArrayFieldConfig extends BaseFieldConfig {
  type: 'array';
  itemConfig: Record<string, FieldConfig>;
  sortable?: boolean;
  minItems?: number;
  maxItems?: number;
  itemLabel?: string;
  maintainEmptyItem?: boolean;
  onItemChange?: (
    itemIndex: number,
    field: string,
    value: any,
    allItems: any[]
  ) => any[];
  isEmptyItem?: (item: any) => boolean;
  createEmptyItem?: (items: any[]) => any;
}
 
export interface CheckboxFieldConfig extends BaseFieldConfig {
  type: 'checkbox';
  size?: number;
  animateChange?: string;
  labelPadding?: string;
}
 
export interface MessageEditorFieldConfig extends BaseFieldConfig {
  type: 'message-editor';
  placeholder?: string;
  minHeight?: number;
  maxAttachments?: number;
  accept?: string;
  endpoint?: string;
  counter?: string;
  gsm?: boolean;
  autogrow?: boolean;
  disableCompletion?: boolean;
}
 
export interface MediaFieldConfig extends BaseFieldConfig {
  type: 'media';
  accept?: string; // MIME filter, e.g. 'audio/*'
  endpoint?: string; // upload endpoint, defaults to DEFAULT_MEDIA_ENDPOINT
}
 
export interface TemplateEditorFieldConfig extends BaseFieldConfig {
  type: 'template-editor';
  endpoint?: string; // endpoint for fetching templates
}
 
export type FieldConfig =
  | TextFieldConfig
  | TextareaFieldConfig
  | SelectFieldConfig
  | KeyValueFieldConfig
  | ArrayFieldConfig
  | CheckboxFieldConfig
  | MessageEditorFieldConfig
  | MediaFieldConfig
  | TemplateEditorFieldConfig;
 
// Layout configurations for better form organization
// Recursive layout system - any layout item can contain other layout items
 
export interface FieldItemConfig {
  type: 'field';
  field: string; // field name to render
}
 
export interface RowLayoutConfig {
  type: 'row';
  items: LayoutItem[]; // can contain fields, groups, or other rows
  gap?: string; // CSS gap value, defaults to '1rem'
  label?: string; // optional label for the entire row
  helpText?: string; // optional help text for the entire row
  inlineLabels?: Record<string, string>; // map of field name to inline label text
  marginBottom?: string; // CSS margin-bottom for spacing below the row
}
 
export interface GroupLayoutConfig {
  type: 'group';
  label: string;
  icon?: string; // optional icon name to display before the label
  items: LayoutItem[]; // can contain fields, rows, or other groups
  collapsible?: boolean;
  collapsed?: boolean | ((formData: FormData) => boolean); // initial state if collapsible - can be a function
  helpText?: string;
  contentPadding?: string; // CSS padding for group content area
  gap?: string; // CSS gap between items (default: '15px')
  bordered?: boolean; // whether to show border around the group (default: true)
  reveal?: boolean; // one-way expand: once clicked, header disappears and items show directly
  getGroupValueCount?: (formData: FormData) => number | boolean; // optional function to get count for bubble display
}
 
export interface SpacerLayoutConfig {
  type: 'spacer';
}
 
export interface TextLayoutConfig {
  type: 'text';
  text: string;
}
 
export interface AccordionSection {
  label: string;
  icon?: string;
  items: LayoutItem[];
  collapsed?: boolean | ((formData: FormData) => boolean);
  getValueCount?: (formData: FormData) => number | boolean;
  localizable?: boolean;
}
 
export interface AccordionLayoutConfig {
  type: 'accordion';
  sections: AccordionSection[];
  multi?: boolean; // allow multiple sections open at once (default: false)
}
 
export type LayoutItem =
  | FieldItemConfig
  | RowLayoutConfig
  | GroupLayoutConfig
  | AccordionLayoutConfig
  | SpacerLayoutConfig
  | TextLayoutConfig
  | string; // string is shorthand for field
 
/**
 * Action group constants - single source of truth for action categorization
 * Use as const for compile-time type checking
 */
export const ACTION_GROUPS = {
  send: 'send',
  contacts: 'contacts',
  save: 'save',
  services: 'services',
  broadcast: 'broadcast',
  trigger: 'trigger'
} as const;
 
/**
 * Split group constants - single source of truth for split categorization
 * Use as const for compile-time type checking
 */
export const SPLIT_GROUPS = {
  wait: 'wait',
  split: 'split'
} as const;
 
// Extract types from const objects for compile-time checking
export type ActionGroup = (typeof ACTION_GROUPS)[keyof typeof ACTION_GROUPS];
export type SplitGroup = (typeof SPLIT_GROUPS)[keyof typeof SPLIT_GROUPS];
 
/**
 * Metadata for group display
 */
export interface GroupMetadata {
  color: string;
  title: string;
  description: string;
}
 
/**
 * Action group metadata - defines display properties for each action group
 * Order in this object determines display order in action selector (top to bottom)
 */
export const ACTION_GROUP_METADATA: Record<ActionGroup, GroupMetadata> = {
  [ACTION_GROUPS.send]: {
    color: '#3498db',
    title: 'Send',
    description: 'Actions that send messages or content to contacts'
  },
  [ACTION_GROUPS.contacts]: {
    color: '#01c1af',
    title: 'Contact',
    description: 'Actions that update contact information'
  },
  [ACTION_GROUPS.save]: {
    color: '#1a777c',
    title: 'Save',
    description: 'Actions that save or store data'
  },
  [ACTION_GROUPS.services]: {
    color: '#f79035ff',
    title: 'Services',
    description: 'Call external services and APIs'
  },
  [ACTION_GROUPS.broadcast]: {
    color: '#8e5ea7',
    title: 'Other People',
    description: 'Actions that apply to others instead of the contact'
  },
  [ACTION_GROUPS.trigger]: {
    color: '#df419f',
    title: 'Trigger',
    description: 'Actions that trigger other behavior'
  }
};
 
/**
 * Split group metadata - defines display properties for each split group
 * Order in this object determines display order in split selector (top to bottom)
 */
export const SPLIT_GROUP_METADATA: Record<SplitGroup, GroupMetadata> = {
  [SPLIT_GROUPS.wait]: {
    color: '#4d7dad',
    title: 'Wait',
    description: 'Wait for user and split on their response'
  },
  [SPLIT_GROUPS.split]: {
    color: '#aaaaaa',
    title: 'Split',
    description: 'Split the flow based on conditions'
  }
};
 
export interface ActionConfig extends FormConfig {
  name: string;
  aliases?: string[]; // alternate type names for backwards compatibility (won't show in selector)
  group: ActionGroup;
  dialogSize?: 'small' | 'medium' | 'large' | 'xlarge';
  evaluated?: string[];
  hideFromActions?: boolean; // if true, don't show in action dialog (default: false - actions show in actions)
  flowTypes?: FlowType[]; // which flow types this action is available for (defaults to all if not specified)
  features?: Feature[]; // which features are required for this action (all must be present)
  render?: (node: any, action: any) => TemplateResult;
 
  form?: Record<string, FieldConfig>;
  layout?: LayoutItem[]; // optional layout configuration - array of layout items
  gutter?: LayoutItem[]; // fields to render in the dialog gutter (left side of buttons)
 
  toFormData?: (action: Action) => FormData;
  fromFormData?: (formData: FormData) => Action;
 
  // Localization support
  localizable?: string[]; // array of field names that can be localized
  toLocalizationFormData?: (
    action: Action,
    localization: Record<string, any>
  ) => FormData;
  fromLocalizationFormData?: (
    formData: FormData,
    action: Action
  ) => Record<string, any>;
}