{"version":3,"file":"context.mjs","names":["Arr"],"sources":["../src/context.ts"],"sourcesContent":["import { Array as Arr } from 'effect'\nimport type { KnowledgeDocument } from './documents.ts'\n\nexport type BuildKnowledgeContextInput = {\n  readonly documents: ReadonlyArray<KnowledgeDocument>\n  readonly maxCharacters: number\n}\n\nconst truncationMarker = '\\n\\n[truncated: pinned knowledge exceeded context budget]'\n\nconst documentHeader = (document: KnowledgeDocument) =>\n  [\n    `## ${document.title}`,\n    `slug: ${document.slug}`,\n    `purpose: ${document.purpose}`,\n    `origin: ${document.origin}`,\n    `status: ${document.status}`,\n    `availability: ${document.availability}`,\n    document.reviewedAt === undefined ? undefined : `reviewed_at: ${document.reviewedAt.toString()}`\n  ]\n    .filter(line => line !== undefined && line.trim().length > 0)\n    .join('\\n')\n\nconst documentBody = (document: KnowledgeDocument) =>\n  [documentHeader(document), document.summary, document.content]\n    .filter(section => section !== undefined && section.trim().length > 0)\n    .join('\\n')\n\nexport const buildKnowledgeContext = (input: BuildKnowledgeContextInput) => {\n  const sections = Arr.map(input.documents, documentBody).filter(\n    section => section.trim().length > 0\n  )\n\n  const header = '# Pinned knowledge\\nUse this durable knowledge as high-priority context.'\n  const body = sections.join('\\n\\n')\n  const context = body.length === 0 ? '' : `${header}\\n\\n${body}`\n\n  if (context.length <= input.maxCharacters) {\n    return context\n  }\n\n  if (input.maxCharacters <= truncationMarker.length) {\n    return context.slice(0, input.maxCharacters).trimEnd()\n  }\n\n  return `${context.slice(0, input.maxCharacters - truncationMarker.length).trimEnd()}${truncationMarker}`\n}\n"],"mappings":";;AAQA,MAAM,mBAAmB;AAEzB,MAAM,kBAAkB,aACtB;CACE,MAAM,SAAS;CACf,SAAS,SAAS;CAClB,YAAY,SAAS;CACrB,WAAW,SAAS;CACpB,WAAW,SAAS;CACpB,iBAAiB,SAAS;CAC1B,SAAS,eAAe,KAAA,IAAY,KAAA,IAAY,gBAAgB,SAAS,WAAW,SAAS;AAC/F,EACG,QAAO,SAAQ,SAAS,KAAA,KAAa,KAAK,KAAK,EAAE,SAAS,CAAC,EAC3D,KAAK,IAAI;AAEd,MAAM,gBAAgB,aACpB;CAAC,eAAe,QAAQ;CAAG,SAAS;CAAS,SAAS;AAAO,EAC1D,QAAO,YAAW,YAAY,KAAA,KAAa,QAAQ,KAAK,EAAE,SAAS,CAAC,EACpE,KAAK,IAAI;AAEd,MAAa,yBAAyB,UAAsC;CAC1E,MAAM,WAAWA,MAAI,IAAI,MAAM,WAAW,YAAY,EAAE,QACtD,YAAW,QAAQ,KAAK,EAAE,SAAS,CACrC;CAEA,MAAM,SAAS;CACf,MAAM,OAAO,SAAS,KAAK,MAAM;CACjC,MAAM,UAAU,KAAK,WAAW,IAAI,KAAK,GAAG,OAAO,MAAM;CAEzD,IAAI,QAAQ,UAAU,MAAM,eAC1B,OAAO;CAGT,IAAI,MAAM,iBAAiB,IACzB,OAAO,QAAQ,MAAM,GAAG,MAAM,aAAa,EAAE,QAAQ;CAGvD,OAAO,GAAG,QAAQ,MAAM,GAAG,MAAM,gBAAgB,EAAuB,EAAE,QAAQ,IAAI;AACxF"}