{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/services/readImage/index.ts"],"sourcesContent":["import { loadPiImageSettings, type PiImageSettings } from '@agimon-ai/doompi-config/pi-config';\n\nconst OMITTED_NOTE = '[Image omitted: could not be resized below the inline image size limit.]';\n\nexport interface ReadTextPart {\n  type: 'text';\n  text: string;\n}\n\nexport interface ReadImagePart {\n  type: 'image';\n  data: string;\n  mimeType: string;\n}\n\nexport type ReadContentPart = ReadTextPart | ReadImagePart;\n\nexport interface ReadImageResizeResult {\n  readonly data: string;\n  readonly mimeType: string;\n  readonly originalWidth: number;\n  readonly originalHeight: number;\n  readonly width: number;\n  readonly height: number;\n  readonly wasResized: boolean;\n}\n\nexport interface ReadImageResizeOperations {\n  resize(\n    inputBytes: Uint8Array,\n    mimeType: string,\n    options: { readonly maxWidth: number; readonly maxHeight: number },\n  ): Promise<ReadImageResizeResult | null>;\n  formatDimensionNote(result: ReadImageResizeResult): string | undefined;\n}\n\nexport function imageLimits(): PiImageSettings {\n  return loadPiImageSettings();\n}\n\nexport async function applyImageLimits(\n  content: ReadContentPart[],\n  limits: PiImageSettings,\n  operations: ReadImageResizeOperations,\n): Promise<ReadContentPart[]> {\n  if (!limits.autoResize || !content.some((part) => part.type === 'image')) return content;\n  const resolved: ReadContentPart[] = [];\n  for (const part of content) {\n    if (part.type !== 'image') {\n      resolved.push(part);\n      continue;\n    }\n    const resized = await operations.resize(Buffer.from(part.data, 'base64'), part.mimeType, {\n      maxWidth: limits.maxDimension,\n      maxHeight: limits.maxDimension,\n    });\n    if (!resized) {\n      resolved.push({ type: 'text', text: OMITTED_NOTE });\n      continue;\n    }\n    resolved.push({ type: 'image', data: resized.data, mimeType: resized.mimeType });\n    const note = operations.formatDimensionNote(resized);\n    if (note) resolved.push({ type: 'text', text: note });\n  }\n  return resolved;\n}\n"],"mappings":";;AAEA,MAAM,eAAe;AAkCrB,SAAgB,cAA+B;CAC7C,OAAO,oBAAoB;AAC7B;AAEA,eAAsB,iBACpB,SACA,QACA,YAC4B;CAC5B,IAAI,CAAC,OAAO,cAAc,CAAC,QAAQ,MAAM,SAAS,KAAK,SAAS,OAAO,GAAG,OAAO;CACjF,MAAM,WAA8B,CAAC;CACrC,KAAK,MAAM,QAAQ,SAAS;EAC1B,IAAI,KAAK,SAAS,SAAS;GACzB,SAAS,KAAK,IAAI;GAClB;EACF;EACA,MAAM,UAAU,MAAM,WAAW,OAAO,OAAO,KAAK,KAAK,MAAM,QAAQ,GAAG,KAAK,UAAU;GACvF,UAAU,OAAO;GACjB,WAAW,OAAO;EACpB,CAAC;EACD,IAAI,CAAC,SAAS;GACZ,SAAS,KAAK;IAAE,MAAM;IAAQ,MAAM;GAAa,CAAC;GAClD;EACF;EACA,SAAS,KAAK;GAAE,MAAM;GAAS,MAAM,QAAQ;GAAM,UAAU,QAAQ;EAAS,CAAC;EAC/E,MAAM,OAAO,WAAW,oBAAoB,OAAO;EACnD,IAAI,MAAM,SAAS,KAAK;GAAE,MAAM;GAAQ,MAAM;EAAK,CAAC;CACtD;CACA,OAAO;AACT"}