{"version":3,"file":"AIUIHelpers.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIHelpers.ts"],"sourcesContent":["// What the generated UI layer and its renderers share. Free of React and of any renderer's\n// packages, so what writes an agent's instructions on a server reads it too.\nimport type { AIUIHost } from './AIUIHost';\n\n/** Whether `value` is a JSON object, rather than an array, `null` or a scalar. */\nexport const isRecord = (value: unknown): value is Record<string, unknown> =>\n  typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/**\n * The type an app's component is placed by in a format's UI, such as `PlaceCard` for\n * `place_card`: named as A2UI's and OpenUI's own components are.\n */\nexport const aiUITypeName = (name: string) =>\n  name\n    .split(/[_\\- ]/)\n    .filter(Boolean)\n    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n    .join('');\n\n/**\n * A name as words, such as \"Submit form\" for `submit_form` or `submitForm`: what the person's\n * message says when what they used has no words of its own.\n */\nexport const aiUIWords = (name: string) => {\n  const words = name\n    .replace(/([a-z0-9])([A-Z])/g, '$1 $2')\n    .split(/[_\\-\\s]+/)\n    .filter(Boolean)\n    .join(' ')\n    .toLowerCase();\n  return words.charAt(0).toUpperCase() + words.slice(1);\n};\n\n/**\n * `value` without the person's secrets, such as a password they typed: each string that holds\n * one has it taken out, wherever it is, so none reaches the agent or what is kept.\n */\nexport const withoutSecretValues = <T>(value: T, secrets: readonly string[]): T => {\n  const shown = secrets.filter((secret) => secret !== '');\n  if (!shown.length) return value;\n  const clean = (item: unknown): unknown => {\n    if (typeof item === 'string') {\n      return shown.reduce((text, secret) => text.split(secret).join(''), item);\n    }\n    if (Array.isArray(item)) return item.map(clean);\n    if (isRecord(item)) {\n      return Object.fromEntries(\n        Object.entries(item).map(([key, entry]) => [key, clean(entry)]),\n      );\n    }\n    return item;\n  };\n  return clean(value) as T;\n};\n\n/** `text` as a sentence, ending in a full stop unless it ends otherwise. */\nexport const aiUISentence = (text: string) => {\n  const trimmed = text.trim();\n  return /[.!?]$/.test(trimmed) ? trimmed : `${trimmed}.`;\n};\n\ntype ClickEvent = {\n  /** Which button was pressed: 0 the main one, 1 the middle one, 2 the one for a menu. */\n  button?: number;\n  preventDefault: () => void;\n  stopPropagation: () => void;\n  target: EventTarget | null;\n  type?: string;\n};\n\n/**\n * A click handler that opens the links of a reply's UI the way `host` allows, rather than\n * wherever they point, since the agent writes them. Give it to the element around them as its\n * capturing click and auxclick handler: of the other buttons, only the middle one opens a link,\n * and the one for a menu shows the menu.\n */\nexport const aiUILinkOpener = (host: AIUIHost | undefined) => (event: ClickEvent) => {\n  if (event.type === 'auxclick' && event.button !== 1) return;\n  const link = (event.target as Element | null)?.closest?.('a[href]');\n  if (!link) return;\n  event.preventDefault();\n  event.stopPropagation();\n  host?.open(link.getAttribute('href') ?? '');\n};\n"],"mappings":";;AAKA,IAAa,YAAY,UACvB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;;;;AAMrE,IAAa,gBAAgB,SAC3B,KACG,MAAM,QAAQ,CAAC,CACf,OAAO,OAAO,CAAC,CACf,KAAK,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,CAC3D,KAAK,EAAE;;;;;AAMZ,IAAa,aAAa,SAAiB;CACzC,MAAM,QAAQ,KACX,QAAQ,sBAAsB,OAAO,CAAC,CACtC,MAAM,UAAU,CAAC,CACjB,OAAO,OAAO,CAAC,CACf,KAAK,GAAG,CAAC,CACT,YAAY;CACf,OAAO,MAAM,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,MAAM,MAAM,CAAC;AACtD;;;;;AAMA,IAAa,uBAA0B,OAAU,YAAkC;CACjF,MAAM,QAAQ,QAAQ,QAAQ,WAAW,WAAW,EAAE;CACtD,IAAI,CAAC,MAAM,QAAQ,OAAO;CAC1B,MAAM,SAAS,SAA2B;EACxC,IAAI,OAAO,SAAS,UAClB,OAAO,MAAM,QAAQ,MAAM,WAAW,KAAK,MAAM,MAAM,CA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