import{logger as e}from"../../../logger/index.mjs";import{streamText as t}from"@intlayer/ai";import{readFileSync as n}from"node:fs";import{dirname as r,join as i}from"node:path";import{fileURLToPath as a}from"node:url";import{getMarkdownMetadata as o}from"@intlayer/core/markdown";import{getBlogs as s,getDocs as c,getFrequentQuestions as l}from"@intlayer/docs";import{OpenAI as u}from"openai";const d=r(a(import.meta.url)),f=e=>{try{return JSON.parse(n(i(d,`./embeddings/${e.replace(`.md`,`.json`)}`),`utf-8`))}catch{return{}}},p=[],m=4.15;800*m,200*m;const h=process.env.SKIP_DOC_EMBEDDINGS_INDEX===`true`,g=e=>{let t=[],n=0;for(;n<e.length;){let r=Math.min(n+3320.0000000000005,e.length);if(r<e.length){let t=e.lastIndexOf(` `,r);t>n&&(r=t)}t.push(e.substring(n,r));let i=r-830.0000000000001;n=i<=n?r:i}return t},_=async e=>(await new u({apiKey:process.env.OPENAI_API_KEY}).embeddings.create({model:`text-embedding-3-large`,input:e})).data[0].embedding,v=(e,t)=>e.reduce((e,n,r)=>e+n*t[r],0)/(Math.sqrt(e.reduce((e,t)=>e+t*t,0))*Math.sqrt(t.reduce((e,t)=>e+t*t,0))),y=async()=>{let t=await l(),n=await c(),r=await s(),i={...n,...r,...t};for await(let t of Object.keys(i)){let n=o(i[t]),r=g(i[t]),a=f(t),s=Object.keys(a),c=r.length,l=s.length,u=!1;c!==l&&(e.info(`File "${t}" chunk count changed: ${l} -> ${c}. Regenerating embeddings.`),u=!h);let d={};for await(let i of Object.keys(r)){let o=Number(i)+1,s=r.length,c=r[i],l=`chunk_${o}`,f=u?void 0:a[l];d={...d,[l]:f},p.push({fileKey:t,chunkNumber:o,embedding:f,content:c,docUrl:n.url,docName:n.title}),e.info(`- Loaded: ${t}/${l}/${s}`)}}};y();const b=async(e,t=20,n=.42)=>{let r=await _(e),i=p.filter(e=>e.embedding).map(e=>({...e,similarity:v(r,e.embedding)})).filter(e=>e.similarity>n).sort((e,t)=>t.similarity-e.similarity).slice(0,t),a=new Set(i.map(e=>e.fileKey));return p.sort((e,t)=>a.has(e.fileKey)?-1:1).filter(e=>i.some(t=>t.fileKey===e.fileKey&&t.chunkNumber===e.chunkNumber))},x={role:`system`,content:n(i(d,`./PROMPT.md`),`utf-8`)},S=async(e,n,r)=>{let i=await b(e.filter(e=>e.role===`user`).map(e=>`- ${e.content}`).join(`
`)),a=x.content.replace(`{{relevantFilesReferences}}`,i.length===0?`Not relevant file found related to the question.`:i.map((e,t)=>[`-----`,`---`,`chunkId: ${t}`,`docChunk: "${e.chunkNumber}/${e.fileKey.length}"`,`docName: "${e.docName}"`,`docUrl: "${e.docUrl}"`,`---`,e.content,`-----`].join(`
`)).join(`

`)),o=e;if(e.length>8){let t={role:`system`,content:`(truncated discussion, ${e.length-8} more messages)`};o=[...e.slice(0,3),t,...e.slice(-5)]}let s=[{role:`system`,content:a},...o];if(!n)throw Error(`Failed to initialize AI configuration`);let c=``,l=t({...n,messages:s});for await(let e of l.textStream)c+=e,r?.onMessage?.(e);let u=[...new Set(i.map(e=>e.fileKey))];return{response:c??`Error: No result found`,relatedFiles:u}};export{S as askDocQuestion,x as initPrompt,y as loadMarkdownFiles,b as searchChunkReference};
//# sourceMappingURL=askDocQuestion.mjs.map