{"version":3,"file":"index.cjs","names":["readAsset","Output","z"],"sources":["../../../src/auditDictionaryMetadata/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { generateText, Output } from 'ai';\nimport { z } from 'zod';\nimport type { AIConfig, AIOptions } from '../aiSdk';\n\ntype Tag = {\n  key: string;\n  description?: string;\n};\n\nexport type AuditDictionaryMetadataOptions = {\n  fileContent: string;\n  tags?: Tag[];\n  aiConfig: AIConfig;\n  applicationContext?: string;\n};\n\nexport type AuditFileResultData = {\n  fileContent: {\n    title: string;\n    description: string;\n    tags: string[];\n  };\n  tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n  // Keep default options\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const auditDictionaryMetadata = async ({\n  fileContent,\n  tags,\n  aiConfig,\n  applicationContext,\n}: AuditDictionaryMetadataOptions): Promise<\n  AuditFileResultData | undefined\n> => {\n  const CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n  const EXAMPLE_REQUEST = readAsset('./EXAMPLE_REQUEST.md');\n  const EXAMPLE_RESPONSE = readAsset('./EXAMPLE_RESPONSE.md');\n\n  // Prepare the prompt for AI by replacing placeholders with actual values.\n  const prompt = CHAT_GPT_PROMPT.replace(\n    '{{applicationContext}}',\n    applicationContext ?? ''\n  ).replace(\n    '{{tags}}',\n    tags\n      ? JSON.stringify(\n          tags\n            .map(({ key, description }) => `- ${key}: ${description}`)\n            .join('\\n\\n'),\n          null,\n          2\n        )\n      : ''\n  );\n\n  const { dataSerialization, ...restAiConfig } = aiConfig;\n  const { output: _unusedOutput, ...validAiConfig } = restAiConfig;\n\n  // Use the AI SDK to generate the completion\n  const { output, usage } = await generateText({\n    ...validAiConfig,\n    output: Output.object({\n      schema: z.object({\n        title: z.string(),\n        description: z.string(),\n        tags: z.array(z.string()),\n      }),\n    }),\n    system: prompt,\n    messages: [\n      { role: 'user', content: EXAMPLE_REQUEST },\n      { role: 'assistant', content: EXAMPLE_RESPONSE },\n      {\n        role: 'user',\n        content: fileContent,\n      },\n    ],\n  });\n\n  return {\n    fileContent: output,\n    tokenUsed: usage?.totalTokens ?? 0,\n  };\n};\n"],"mappings":";;;;;;;AA0BA,MAAa,mBAA8B,EAE1C;;;;;;AAOD,MAAa,0BAA0B,OAAO,EAC5C,aACA,MACA,UACA,yBAGG;CACH,MAAM,kBAAkBA,+BAAU,cAAc;CAChD,MAAM,kBAAkBA,+BAAU,uBAAuB;CACzD,MAAM,mBAAmBA,+BAAU,wBAAwB;CAG3D,MAAM,SAAS,gBAAgB,QAC7B,0BACA,sBAAsB,GACvB,CAAC,QACA,YACA,OACI,KAAK,UACH,KACG,KAAK,EAAE,KAAK,kBAAkB,KAAK,IAAI,IAAI,cAAc,CACzD,KAAK,OAAO,EACf,MACA,EACD,GACD,GACL;CAED,MAAM,EAAE,mBAAmB,GAAG,iBAAiB;CAC/C,MAAM,EAAE,QAAQ,eAAe,GAAG,kBAAkB;CAGpD,MAAM,EAAE,QAAQ,UAAU,2BAAmB;EAC3C,GAAG;EACH,QAAQC,UAAO,OAAO,EACpB,QAAQC,MAAE,OAAO;GACf,OAAOA,MAAE,QAAQ;GACjB,aAAaA,MAAE,QAAQ;GACvB,MAAMA,MAAE,MAAMA,MAAE,QAAQ,CAAC;GAC1B,CAAC,EACH,CAAC;EACF,QAAQ;EACR,UAAU;GACR;IAAE,MAAM;IAAQ,SAAS;IAAiB;GAC1C;IAAE,MAAM;IAAa,SAAS;IAAkB;GAChD;IACE,MAAM;IACN,SAAS;IACV;GACF;EACF,CAAC;AAEF,QAAO;EACL,aAAa;EACb,WAAW,OAAO,eAAe;EAClC"}