agent-rag-mcp 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -85,10 +85,12 @@ Register two separate server processes in the adapter configuration (this exampl
|
|
|
85
85
|
|
|
86
86
|
The orchestrator receives only `rag_find`, which returns citations and bounded excerpts. The explorer also receives `rag_read`, which reads bounded text from a citation. Keeping them separate applies least privilege. The server reads `RAG_ROOT` to locate the knowledge base and `RAG_CAPABILITIES` to decide which tools to announce.
|
|
87
87
|
|
|
88
|
-
The extension adds a `rag` section to the agent system prompt describing the namespace and delegation rules;
|
|
88
|
+
The extension adds a `rag` section to the agent system prompt describing the namespace and delegation rules; it contains no client, project, or path value. For truncated excerpts, coverage questions, or longer verbatim quotes, it requires deeper text to be read by a subagent that exposes `rag_read` and forbids asserting absence from a catalog. Set `RAG_PROMPT=off` (exactly that value) to suppress it; any other value, including unset, keeps it.
|
|
89
89
|
|
|
90
90
|
The namespace travels in every `rag_find` or `rag_read` call; it does not come from the environment. For example, include `"namespace": {"client":"acme","project":"research"}` in each tool call's arguments. Do not configure `RAG_CLIENT` or `RAG_PROJECT` as a substitute for the per-call namespace.
|
|
91
91
|
|
|
92
|
+
`rag_find` returns `total`, the number of matching chunks before the `limit` slice; `citations.length` is how many citations were actually returned, so comparing them shows how many matches were cut by the limit. Its `excerptChars` input defaults to 800. Every successful `rag_read` result includes `totalChunks` and `nextChunkOrdinal`; the latter is the next zero-based chunk ordinal, or `null` when there is no following chunk. `truncated` means this chunk was not delivered whole, so re-read the same ordinal with higher limits; it does not indicate whether another chunk exists.
|
|
93
|
+
|
|
92
94
|
## Requirements
|
|
93
95
|
|
|
94
96
|
- Node.js >= 22.13.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var w=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(r){throw n=[r],r}};var
|
|
1
|
+
var w=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(r){throw n=[r],r}};var Be=w(()=>{"use strict"});import*as ye from"node:path";import{DatabaseSync as Sr}from"node:sqlite";function Ht(e){if(e==="."||e===".."||!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(e))throw new Te(e)}function q(e,t){return Ht(t),ye.join(e,"clients",t,"rag.db")}var Te,be=w(()=>{"use strict";Be();Te=class extends Error{code="invalid_client_name";constructor(t){super(`Invalid client name "${t}". Client names must match [A-Za-z0-9][A-Za-z0-9._-]* and cannot be "." or "..".`),this.name="InvalidClientNameError"}}});import{extractRawText as Qt}from"mammoth";function en(e){if(typeof e=="string")return/\.docx$/i.test(e);if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return n.split(";")[0].trim().toLowerCase()===we;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.docx$/i.test(r)}async function tn(e){let n=(await Qt({buffer:Buffer.from(e)})).value.trim();return{sections:n.length>0?[{ordinal:0,heading:null,level:null,text:n}]:[],versionLabel:null,mediaType:we}}var we,He,Ve=w(()=>{"use strict";we="application/vnd.openxmlformats-officedocument.wordprocessingml.document";He={name:"docx",version:"1.13.0",mediaTypes:[we],supports:en,extract:tn}});var b,M=w(()=>{"use strict";b=class extends Error{code;constructor(t,n){super(n),this.name="ResourceLimitError",this.code=t}}});import{createRequire as nn}from"node:module";import Re from"tesseract.js";function on(e){if(typeof e=="string"){let o=e.split(";")[0]?.trim().toLowerCase();return Ye.has(o??"")||Je.test(e)}if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return Ye.has(n.split(";")[0]?.trim().toLowerCase()??"");let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&Je.test(r)}function sn(e){if(e.byteLength>=8&&e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71&&e[4]===13&&e[5]===10&&e[6]===26&&e[7]===10)return te;if(e.byteLength>=3&&e[0]===255&&e[1]===216&&e[2]===255)return Ne;throw new Error("Unsupported image data; expected PNG or JPEG bytes.")}function an(e,t){let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(t===te){if(e.byteLength<24||String.fromCharCode(...e.subarray(12,16))!=="IHDR")throw new Error("Invalid PNG image header.");return{width:n.getUint32(16),height:n.getUint32(20)}}let r=2;for(;r+3<e.byteLength;){if(e[r]!==255){r+=1;continue}for(;e[r]===255;)r+=1;let o=e[r];if(r+=1,o===217||o===218)break;if(o===216||o===1||o!==void 0&&o>=208&&o<=215)continue;if(r+2>e.byteLength)break;let i=n.getUint16(r);if(i<2||r+i>e.byteLength)break;if(o!==void 0&&(o>=192&&o<=195||o>=197&&o<=199||o>=201&&o<=203||o>=205&&o<=207)){if(i<7)break;return{height:n.getUint16(r+3),width:n.getUint16(r+5)}}r+=i}throw new Error("JPEG image dimensions could not be read.")}async function cn(e){let t=sn(e),{width:n,height:r}=an(e,t);if(n===0||r===0||n>We/r)throw new b("image_too_large",`Image exceeds MAX_IMAGE_PIXELS (${We} pixels).`);let o=await Re.createWorker("eng",Re.OEM.LSTM_ONLY,Ae);try{await o.setParameters({tessedit_pageseg_mode:Re.PSM.SINGLE_BLOCK});let{data:i}=await o.recognize(Buffer.from(e)),s=i.text.replace(/\s+/g," ").trim();return{sections:s.length>0?[{ordinal:0,heading:null,level:null,text:s}]:[],versionLabel:null,mediaType:t}}finally{await o.terminate()}}var te,Ne,Ye,Je,We,Ke,rn,Ae,ne,Le=w(()=>{"use strict";M();te="image/png",Ne="image/jpeg",Ye=new Set([te,Ne]),Je=/\.(?:png|jpe?g)$/i,We=5e7,Ke=nn(import.meta.url)("@tesseract.js-data/eng"),rn=Ke.langPath,Ae={langPath:rn,cacheMethod:"none",gzip:Ke.gzip};ne={name:"image-ocr",version:"7.0.0",mediaTypes:[te,Ne],supports:on,extract:cn}});var Ze=w(()=>{"use strict"});import{createCanvas as ln}from"@napi-rs/canvas";import Oe from"tesseract.js";import{getDocument as un}from"pdfjs-dist/legacy/build/pdf.mjs";function mn(e){if(typeof e=="string")return/\.pdf$/i.test(e);if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return n.split(";")[0].trim().toLowerCase()===re;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.pdf$/i.test(r)}async function pn(e){let t=un({data:Uint8Array.from(e),maxImageSize:1e7,disableFontFace:!0,useWorkerFetch:!1,verbosity:0}),n=await t.promise;try{if(n.numPages>et)throw new b("too_many_pages",`PDF exceeds MAX_PDF_PAGES (${et} pages).`);let r=[];for(let i=1;i<=n.numPages;i+=1){let c=(await(await n.getPage(i)).getTextContent()).items.filter(l=>"str"in l).map(l=>"str"in l?l.str:"").join(" ").replace(/\s+/g," ").trim();c.length>0&&r.push({ordinal:r.length,heading:null,level:null,text:c})}if(r.length>0)return{sections:r,versionLabel:null,mediaType:re};if(n.numPages>100)throw new b("too_many_pages",`PDF exceeds MAX_OCR_PDF_PAGES (${100} pages) for OCR.`);let o=await Oe.createWorker("eng",Oe.OEM.LSTM_ONLY,Ae);try{await o.setParameters({tessedit_pageseg_mode:Oe.PSM.SINGLE_BLOCK});for(let i=1;i<=n.numPages;i+=1){let s=await n.getPage(i),a=s.getViewport({scale:fn}),c=Math.ceil(a.width),l=Math.ceil(a.height);if(c===0||l===0||c>5e6/l)throw new b("image_too_large",`PDF page exceeds MAX_PDF_OCR_RENDER_PIXELS (${5e6} pixels).`);let d=ln(c,l),u=d.getContext("2d");await s.render({canvas:d,canvasContext:u,viewport:a}).promise;let{data:g}=await o.recognize(d.toBuffer("image/png")),m=g.text.replace(/\s+/g," ").trim();m.length>0&&r.push({ordinal:i-1,heading:`Page ${i}`,level:null,text:m})}}finally{await o.terminate()}return{sections:r,versionLabel:null,mediaType:re}}finally{await t.destroy()}}var re,et,fn,tt,nt=w(()=>{"use strict";Le();Ze();M();re="application/pdf",et=2e3,fn=2;tt={name:"pdf",version:"6.4.299",mediaTypes:[re],supports:mn,extract:pn}});function rt(e){let t=e.match(/^(.*?)[-_ ]v(\d+(?:\.\d+)*)(\.[^./\\]+)?$/);return t?{prefix:t[1].replace(/[-_ ]+$/,""),version:t[2],suffix:t[3]??null}:null}var ot=w(()=>{"use strict"});function it(e,t){if(typeof e=="string")return e;if(typeof e!="object"||e===null)return null;let n=e;for(let r of t)if(typeof n[r]=="string")return n[r];return null}function En(e){let t=it(e,["mediaType","mimeType","contentType"]);if(t!==null)return gn.has(t.split(";")[0].trim().toLowerCase());let n=it(e,["filename","name","path","extension"]);return n===null?!1:/(?:^|[\\/])[^\\/]+\.(?:txt|md)$/i.test(n)||/^\.(?:txt|md)$/i.test(n)}function hn(e){let t=e.split(`
|
|
2
2
|
`),n=[],r=null,o=/^ {0,3}(#{1,6})(?:[ \t]+|$)(.*)$/;if(!t.some(s=>o.test(s.replace(/\r$/,""))))return e.length===0?[]:[{ordinal:0,heading:null,level:null,text:e}];let i=()=>{r&&n.push({ordinal:n.length,heading:r.heading,level:r.level,text:r.lines.join(`
|
|
3
|
-
`).trim()})};for(let s of t){let a=o.exec(s.replace(/\r$/,""));if(!a){r?r.lines.push(s):s.length>0&&(r={heading:null,level:null,lines:[s]});continue}i(),r={heading:a[2].replace(/[ \t]+#+[ \t]*$/,"").trim(),level:a[1].length,lines:[]}}return i(),n}async function _n(e){let t=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}).decode(e),n=t.startsWith("\uFEFF")?t.slice(1):t,r=hn(n),o=r.some(s=>s.heading!==null),i=r.find(s=>s.heading!==null&&rt(s.heading)!==null)?.heading??null;return{sections:r,versionLabel:i,mediaType:o?"text/markdown":n.length>0?"text/plain":null}}var gn,Oe,st=w(()=>{"use strict";ot();gn=new Set(["text/plain","text/markdown"]);Oe={name:"plain-text",version:"1.0.0",supports:En,extract:_n}});function xn(e,t){let n=Array.from(e,i=>i.codePointAt(0)),r=Array.from(t,i=>i.codePointAt(0)),o=Math.min(n.length,r.length);for(let i=0;i<o;i+=1){let s=n[i],a=r[i];if(s!==a)return s-a}return n.length-r.length}function Ie(e,t){if(e===null)return"null";if(typeof e=="string")return JSON.stringify(e);if(typeof e=="boolean")return e?"true":"false";if(typeof e=="number"){if(!Number.isFinite(e))throw new D;return JSON.stringify(e)}if(typeof e=="bigint")return e<=BigInt(Number.MAX_SAFE_INTEGER)&&e>=BigInt(Number.MIN_SAFE_INTEGER)?JSON.stringify(Number(e)):JSON.stringify(e.toString());if(Array.isArray(e)){if(t.has(e))throw new TypeError("Structured content must be acyclic.");t.add(e);try{return`[${e.map(n=>n===void 0?"null":Ie(n,t)).join(",")}]`}finally{t.delete(e)}}if(typeof e=="object"){let n=e;if(t.has(n))throw new TypeError("Structured content must be acyclic.");t.add(n);try{return`{${Object.keys(n).filter(o=>n[o]!==void 0).sort(xn).map(o=>`${JSON.stringify(o)}:${Ie(n[o],t)}`).join(",")}}`}finally{t.delete(n)}}return"null"}function oe(e){return Ie(e,new Set)}var D,at=w(()=>{"use strict";D=class extends Error{code="non_finite_number";constructor(){super("Canonical structured content contains a non-finite number."),this.name="NonFiniteNumberCanonicalizationError"}}});import{parseDocument as Tn}from"yaml";function ct(e,t){if(typeof e=="string")return e;if(typeof e!="object"||e===null)return null;let n=e;for(let r of t)if(typeof n[r]=="string")return n[r];return null}function yn(e,t){if(typeof e!="string"){let o=ct(e,["mediaType","mimeType","contentType"]);if(o!==null)return t.mediaTypes.includes(o.split(";")[0].trim().toLowerCase())}let n=typeof e=="string"?e:ct(e,["filename","name","path","extension"]);if(n===null)return!1;let r=n.toLowerCase();return t.extensions.some(o=>r.endsWith(o))}function dt(e){return typeof e=="string"?e:JSON.stringify(e,(t,n)=>typeof n=="bigint"?n.toString():n,2)??"null"}function ut(e){return JSON.parse(e,(t,n,r)=>{let o=r?.source;return typeof n=="number"&&!Number.isSafeInteger(n)&&o!==void 0&&/^-?(?:0|[1-9][0-9]*)$/.test(o)?BigInt(o):n})}function Pe(e){try{return{canonicalContent:oe(e)}}catch(t){if(t instanceof D)return{canonicalizationError:t.code};throw t}}function bn(e){let t;try{t=ut(e)}catch{throw new U("extraction_invalid_json")}return{sections:typeof t=="object"&&t!==null&&!Array.isArray(t)?Object.entries(t).map(([r,o],i)=>({ordinal:i,heading:r,level:1,text:dt(o)})):[{ordinal:0,heading:null,level:null,text:dt(t)}],...Pe(t)}}function Sn(e){let t=[],n=[];for(let r of e.split(/\r?\n/)){let o=r.trim();if(o.length!==0){try{n.push(ut(o))}catch{throw new U("extraction_invalid_json")}t.push({ordinal:t.length,heading:null,level:null,text:o})}}return{sections:t,...Pe(n)}}function j(e,t=new Set){if(typeof e!="object"||e===null)return e;if(t.has(e))throw new U("extraction_invalid_yaml");t.add(e);try{if(e instanceof Map){let r=Object.create(null);for(let[o,i]of e){let s=typeof o=="string"?o:oe(j(o,t));r[s]=j(i,t)}return r}if(Array.isArray(e))return e.map(r=>j(r,t));let n=Object.create(null);for(let[r,o]of Object.entries(e))n[r]=j(o,t);return n}finally{t.delete(e)}}function lt(e){return typeof e=="string"?e:JSON.stringify(e,(t,n)=>typeof n=="bigint"?n.toString():n,2)??"null"}function wn(e){let t=e.contents,n=new Map;for(let r of t?.items??[]){let o=r.key?.range?.[0];if(o===void 0)continue;let i=r.key&&Object.hasOwn(r.key,"value")?r.key.value:r.key?.toJSON?.(),s=j(i),a=typeof s=="string"?s:oe(s);n.set(a,o)}return n}function Rn(e){let t,n,r;try{let i=Tn(e,{schema:"core",uniqueKeys:!1,intAsBigInt:!0});if(i.errors.length>0)throw new Error("invalid YAML document");t=j(i.toJS({mapAsMap:!0})),n=wn(i)}catch(i){if(!(i instanceof D))throw new U("extraction_invalid_yaml");r=i.code,t=null,n=new Map}if(r!==void 0)return{sections:[{ordinal:0,heading:null,level:null,text:e.trim()}],canonicalizationError:r};let o;if(typeof t=="object"&&t!==null&&!Array.isArray(t)){let i=Object.entries(t).map(([s,a])=>({heading:s,value:a,start:n.get(s)})).filter(s=>s.start!==void 0).sort((s,a)=>s.start-a.start);o=i.map(({heading:s,start:a},c)=>({ordinal:c,heading:s,level:1,text:e.slice(a,i[c+1]?.start??e.length).trim()})),o.length===0&&(o=[{ordinal:0,heading:null,level:null,text:lt(t)}])}else o=[{ordinal:0,heading:null,level:null,text:lt(t)}];return{sections:o,...Pe(t)}}function Xe(e){return{name:e.name,version:"1.0.0",mediaTypes:e.mediaTypes,supports(t){return yn(t,e)},async extract(t){let n=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}).decode(t),r=n.startsWith("\uFEFF")?n.slice(1):n;if(r.length===0)return{sections:[],versionLabel:null,mediaType:null};let o;switch(e.kind){case"json":o=bn(r);break;case"jsonl":o=Sn(r);break;case"yaml":o=Rn(r);break}return{sections:o.sections,versionLabel:null,mediaType:e.mediaTypes[0]??null,...o.canonicalContent===void 0?{}:{canonicalContent:o.canonicalContent},...o.canonicalizationError===void 0?{}:{canonicalizationError:o.canonicalizationError}}}}}var U,ft,mt,Ce,pt=w(()=>{"use strict";at();U=class extends Error{code;constructor(t){super(t==="extraction_invalid_json"?"Structured JSON is invalid.":"Structured YAML is invalid."),this.name="StructuredTextExtractionError",this.code=t}};ft=Xe({kind:"json",name:"json",extensions:[".json"],mediaTypes:["application/json"]}),mt=Xe({kind:"jsonl",name:"jsonl",extensions:[".jsonl"],mediaTypes:["application/x-ndjson"]}),Ce=Xe({kind:"yaml",name:"yaml",extensions:[".yaml",".yml"],mediaTypes:["application/yaml"]})});import{PassThrough as Nn}from"node:stream";import{StringDecoder as An}from"node:string_decoder";import Ln from"jszip";function On(e){if(typeof e=="string")return/\.pptx$/i.test(e);if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return n.split(";")[0].trim().toLowerCase()===ve;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.pptx$/i.test(r)}function In(e){return e.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi,(t,n)=>{switch(n.toLowerCase()){case"amp":return"&";case"lt":return"<";case"gt":return">";case"quot":return'"';case"apos":return"'";default:{let r=n.startsWith("#x")||n.startsWith("#X")?Number.parseInt(n.slice(2),16):Number.parseInt(n.slice(1),10);return String.fromCodePoint(r)}}})}function gt(e){return Number(e.match(/\/slide(\d+)\.xml$/)?.[1]??Number.MAX_SAFE_INTEGER)}function x(e){throw new b("archive_too_large",e)}function Pn(e,t,n,r){t>n&&x(`PPTX entry ${e} decompressed size differs from its declared size (${n} bytes).`),r>O&&x(`PPTX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`)}function F(e,t,n){t+8>n&&x("PPTX ZIP64 metadata is truncated.");let r=e.getBigUint64(t,!0);return r>BigInt(Number.MAX_SAFE_INTEGER)&&x("PPTX ZIP64 metadata exceeds safe bounds."),Number(r)}function H(e,t,n){return t>=0&&t+4<=e.byteLength&&e.getUint32(t,!0)===n}function Xn(e,t){let n=F(e,t+8,t+20),r=o=>{if(!H(e,o,101075792)||o+12>t)return!1;let i=F(e,o+4,t);return i>=44&&o+12+i===t};if(r(n))return n;for(let o=t-56;o>=0;o-=1)if(r(o))return o;x("PPTX ZIP64 end record cannot be located safely.")}function Cn(e,t,n,r,o){let i=e.getUint32(t+24,!0);if(i!==4294967295)return i;let s=t+46+r,a=s+o;for(;s+4<=a;){let c=e.getUint16(s,!0),l=e.getUint16(s+2,!0),d=s+4,u=d+l;if((u>a||u>n)&&x("PPTX central-directory extra field is truncated."),c===1)return F(e,d,u);s=u}x("PPTX ZIP64 entry size is missing from its central-directory extra field.")}function vn(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=Math.max(0,e.byteLength-22-65535),r=-1;for(let p=e.byteLength-22;p>=n;p-=1)if(H(t,p,101010256)&&p+22+t.getUint16(p+20,!0)===e.byteLength){r=p;break}if(r<0)return new Map;let o=t.getUint16(r+10,!0),i=t.getUint32(r+12,!0),s=t.getUint32(r+16,!0),a=r-20,c=H(t,a,117853008);(o===65535||i===4294967295||s===4294967295)&&!c&&x("PPTX ZIP64 end record is missing.");let d=o,u=i,g=s,E=r;if(c){let p=Xn(t,a),R=a;d=F(t,p+32,R),u=F(t,p+40,R),g=F(t,p+48,R),E=p}d>L&&x(`PPTX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`),(u>E||g>E||u+g>E)&&x("PPTX central-directory bounds cannot be checked safely.");let h=E-u-g,f=g+h,m=f+u,y=0,A=0,z=new Map;for(;f+4<=m&&H(t,f,33639248);){f+46>m&&x("PPTX central-directory bounds cannot be checked safely.");let p=t.getUint16(f+28,!0),R=t.getUint16(f+30,!0),he=t.getUint16(f+32,!0),G=f+46+p+R+he;G>m&&x("PPTX central-directory entry is truncated."),y+=1,y>L&&x(`PPTX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`);let X=Cn(t,f,m,p,R),_e=new TextDecoder().decode(e.subarray(f+46,f+46+p));z.set(_e,X),X>O-A&&x(`PPTX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`),A+=X,f=G}if(f<m&&H(t,f,84233040)&&f+6<=m){let p=t.getUint16(f+4,!0);f+6+p===m&&(f=m)}return(f!==m||y!==d)&&x("PPTX central-directory count or bounds cannot be checked safely."),z}async function kn(e,t,n){let r=[],o=new An("utf8"),i=new Nn,s=e.nodeStream("nodebuffer");s.on("error",c=>i.destroy(c)),s.pipe(i);let a=0;try{for await(let c of i){let l=Buffer.isBuffer(c)?c:Buffer.from(c);a+=l.byteLength,Pn(e.name,a,n,t+a),r.push(o.write(l))}}catch(c){throw c instanceof Error&&c.message.includes("uncompressed data size mismatch")&&x(`PPTX entry ${e.name} decompressed size differs from its declared size (${n} bytes).`),c}return r.push(o.end()),{xml:r.join(""),bytesRead:a}}async function Mn(e){let t=vn(e),r=(await Ln.loadAsync(e)).file(/^ppt\/slides\/slide\d+\.xml$/).sort((s,a)=>gt(s.name)-gt(a.name)),o=[],i=0;for(let s of r){let a=t.get(s.name);a===void 0&&x(`PPTX central-directory size for ${s.name} cannot be checked safely.`);let c=await kn(s,i,a);i+=c.bytesRead;let l=[...c.xml.matchAll(/<a:t(?:\s[^>]*)?>([\s\S]*?)<\/a:t>/g)].map(d=>In(d[1]??"").trim()).filter(d=>d.length>0).join(" ");l.length>0&&o.push({ordinal:o.length,heading:null,level:null,text:l})}return{sections:o,versionLabel:null,mediaType:ve}}var ve,L,O,Et,ke=w(()=>{"use strict";M();ve="application/vnd.openxmlformats-officedocument.presentationml.presentation",L=5e3,O=128*1024*1024;Et={name:"pptx",version:"3.10.2",mediaTypes:[ve],supports:On,extract:Mn}});import{posix as ht}from"node:path";import{PassThrough as Dn}from"node:stream";import{StringDecoder as jn}from"node:string_decoder";import Un from"jszip";function $n(e){if(typeof e=="string")return/\.xlsx$/i.test(e);if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return n.split(";")[0].trim().toLowerCase()===Me;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.xlsx$/i.test(r)}function T(e){throw new b("sheet_too_large",e)}function _(e){throw new b("archive_too_large",e)}function bt(e){let t=/^\$?([A-Z]{1,3})\$?(\d+)$/i.exec(e);if(!t?.[1]||!t[2])return null;let n=0;for(let o of t[1].toUpperCase())n=n*26+o.charCodeAt(0)-64;let r=Number(t[2]);return Number.isSafeInteger(r)&&r>0&&n<=Fn?{row:r,column:n}:null}function Tt(e,t){(!Number.isSafeInteger(e)||e<1||!Number.isSafeInteger(t)||t<1)&&T("Worksheet contains an invalid row or column reference."),(e>Y||e*t>ae)&&T(`Worksheet exceeds MAX_SHEET_ROWS (${Y}) or MAX_SHEET_CELLS (${ae}).`)}function S(e,t){return e.match(new RegExp(`\\b${t}\\s*=\\s*[\\"']([^\\"']*)[\\"']`,"i"))?.[1]}function $(e,t,n){t+8>n&&_("XLSX ZIP64 metadata is truncated.");let r=e.getBigUint64(t,!0);return r>BigInt(Number.MAX_SAFE_INTEGER)&&_("XLSX ZIP64 metadata exceeds safe bounds."),Number(r)}function V(e,t,n){return t>=0&&t+4<=e.byteLength&&e.getUint32(t,!0)===n}function Bn(e,t){let n=$(e,t+8,t+20),r=i=>{if(!V(e,i,101075792)||i+12>t)return!1;let s=$(e,i+4,t);return s>=44&&i+12+s===t};if(r(n))return n;let o=0;for(let i=t-56;i>=0;i-=1)if(o>=xt&&_(`XLSX ZIP64 recovery scan exceeds MAX_ZIP64_RECOVERY_SCAN_ITERATIONS (${xt} iterations).`),o+=1,r(i))return i;_("XLSX ZIP64 end record cannot be located safely.")}function zn(e,t,n,r,o){let i=e.getUint32(t+24,!0);if(i!==4294967295)return i;let s=t+46+r,a=s+o;for(;s+4<=a;){let c=e.getUint16(s,!0),l=e.getUint16(s+2,!0),d=s+4,u=d+l;if((u>a||u>n)&&_("XLSX central-directory extra field is truncated."),c===1)return $(e,d,u);s=u}_("XLSX ZIP64 entry size is missing from its central-directory extra field.")}function Gn(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=Math.max(0,e.byteLength-22-65535),r=-1;for(let p=e.byteLength-22;p>=n;p-=1)if(V(t,p,101010256)&&p+22+t.getUint16(p+20,!0)===e.byteLength){r=p;break}r<0&&_("XLSX end-of-central-directory record is missing.");let o=t.getUint16(r+10,!0),i=t.getUint32(r+12,!0),s=t.getUint32(r+16,!0),a=r-20,c=V(t,a,117853008);(o===65535||i===4294967295||s===4294967295)&&!c&&_("XLSX ZIP64 end record is missing.");let d=o,u=i,g=s,E=r;if(c){let p=Bn(t,a),R=a;d=$(t,p+32,R),u=$(t,p+40,R),g=$(t,p+48,R),E=p}d>L&&_(`XLSX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`),(u>E||g>E||u+g>E)&&_("XLSX central-directory bounds cannot be checked safely.");let h=E-u-g,f=g+h,m=f+u,y=0,A=0,z=new Map;for(;f+4<=m&&V(t,f,33639248);){f+46>m&&_("XLSX central-directory bounds cannot be checked safely.");let p=t.getUint16(f+28,!0),R=t.getUint16(f+30,!0),he=t.getUint16(f+32,!0),G=f+46+p+R+he;G>m&&_("XLSX central-directory entry is truncated."),y+=1,y>L&&_(`XLSX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`);let X=zn(t,f,m,p,R),_e=new TextDecoder().decode(e.subarray(f+46,f+46+p));z.set(_e,X),X>O-A&&_(`XLSX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`),A+=X,f=G}if(f<m&&V(t,f,84233040)&&f+6<=m){let p=t.getUint16(f+4,!0);f+6+p===m&&(f=m)}return(f!==m||y!==d)&&_("XLSX central-directory count or bounds cannot be checked safely."),z}function Hn(e,t,n,r){t>n&&_(`XLSX entry ${e} decompressed size differs from its declared size (${n} bytes).`),r>O&&_(`XLSX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`)}async function St(e,t,n,r){let o=[],i=new jn("utf8"),s=new Dn,a=e.nodeStream("nodebuffer");a.on("error",d=>s.destroy(d)),a.pipe(s);let c=0;try{for await(let d of s){let u=Buffer.isBuffer(d)?d:Buffer.from(d);c+=u.byteLength,Hn(e.name,c,t,n.bytes+c);let g=i.write(u);o.push(g),r?.(g,u.byteLength)}}catch(d){throw d instanceof Error&&d.message.includes("uncompressed data size mismatch")&&_(`XLSX entry ${e.name} decompressed size differs from its declared size (${t} bytes).`),d}let l=i.end();return o.push(l),r?.(l,0),c!==t&&_(`XLSX entry ${e.name} decompressed size differs from its declared size (${t} bytes).`),n.bytes+=c,o.join("")}function Vn(e,t,n,r){n.bytes+=t,n.bytes>_t&&T(`Worksheet XML scan exceeds MAX_SHEET_SCAN_BYTES (${_t} bytes).`);let o=r.pending+e,i=0;for(r.pending="";;){let s=o.indexOf("<",i);if(s<0)break;let a=o.indexOf(">",s+1);if(a<0){r.pending=o.slice(s),Buffer.byteLength(r.pending)>ie&&T(`Worksheet metadata exceeds MAX_SHEET_TAG_BYTES (${ie} bytes).`);break}let c=o.slice(s,a+1);if(i=a+1,Buffer.byteLength(c)>ie&&T(`Worksheet metadata exceeds MAX_SHEET_TAG_BYTES (${ie} bytes).`),/^<(?:[\w.-]+:)?row\b/i.test(c)){r.rowCount+=1;let l=S(c,"r");l!==void 0&&!/^\d+$/.test(l)&&T("Worksheet contains an invalid row reference.");let d=Number(l??r.rowCount);(!Number.isSafeInteger(d)||d<1)&&T("Worksheet contains an invalid row reference."),(d>Y||r.rowCount>Y)&&T(`Worksheet exceeds MAX_SHEET_ROWS (${Y}).`),r.maxRow=Math.max(r.maxRow,d),Tt(r.maxRow,r.maxColumn)}else if(/^<(?:[\w.-]+:)?c\b/i.test(c)){r.cellCount+=1,r.cellCount>ae&&T(`Worksheet exceeds MAX_SHEET_CELLS (${ae}).`);let l=S(c,"r"),d=l===void 0?null:bt(l);l!==void 0&&!d&&T("Worksheet contains an invalid cell reference."),d&&(r.maxRow=Math.max(r.maxRow,d.row),r.maxColumn=Math.max(r.maxColumn,d.column),Tt(r.maxRow,r.maxColumn))}}}function W(e){return e.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi,(t,n)=>{switch(n.toLowerCase()){case"amp":return"&";case"lt":return"<";case"gt":return">";case"quot":return'"';case"apos":return"'";default:{let r=n.startsWith("#x")||n.startsWith("#X")?Number.parseInt(n.slice(2),16):Number.parseInt(n.slice(1),10),o=r===9||r===10||r===13||r>=32&&r<=55295||r>=57344&&r<=65533||r>=65536&&r<=1114111;return(!Number.isInteger(r)||!o)&&T("XLSX contains an invalid XML character reference."),String.fromCodePoint(r)}}})}function wt(e){return[...e.replace(/<(?:[\w.-]+:)?rPh\b[\s\S]*?<\/(?:[\w.-]+:)?rPh\s*>/gi,"").matchAll(/<(?:[\w.-]+:)?t\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?t\s*>/gi)].map(n=>W(n[1]??"")).join("")}function Yn(e){return[...e.matchAll(/<(?:[\w.-]+:)?si\b[^>]*\/>|<(?:[\w.-]+:)?si\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?si\s*>/gi)].map(t=>t[0].endsWith("/>")?"":wt(t[1]??""))}function Jn(e){let t;try{t=decodeURIComponent(W(e))}catch(o){if(o instanceof b)throw o;return null}let n=t.split(/[?#]/,1)[0]??"",r=ht.normalize(n.startsWith("/")?n.slice(1):ht.join("xl",n));return r.startsWith("xl/")?r:null}function Wn(e){let t=new Map;for(let n of e.matchAll(/<(?:[\w.-]+:)?Relationship\b[^>]*\/?>/gi)){let r=n[0];if(!/\/worksheet$/i.test(S(r,"Type")??"")||/^external$/i.test(S(r,"TargetMode")??""))continue;let o=S(r,"Id"),i=S(r,"Target"),s=i?Jn(i):null;o&&s&&t.set(o,s)}return t}function Kn(e,t){let n=[];for(let r of e.matchAll(/<(?:[\w.-]+:)?sheet\b[^>]*\/?>/gi)){let o=r[0],i=S(o,"name"),s=S(o,"r:id")??S(o,"id"),a=s?t.get(s):void 0;(i===void 0||a===void 0)&&_("XLSX workbook contains an unresolved worksheet relationship."),n.push({name:W(i),part:a})}return n}function Zn(e){let t=e.replace(/"(?:""|[^"])*"/g,"").replace(/\\./g,"").replace(/_.|\*./g,"").replace(/\[[^\]]*\]/g,""),n=/y/i.test(t)&&/m/i.test(t)&&/d/i.test(t);return{date:n,dateTime:n&&(/h|s/i.test(t)||/am\/pm|\[(?:h+|s+)\]/i.test(e))}}function qn(e){let t=new Map;for(let r of e.matchAll(/<(?:[\w.-]+:)?numFmt\b[^>]*\/?\s*>/gi)){let o=Number(S(r[0],"numFmtId")),i=S(r[0],"formatCode");Number.isSafeInteger(o)&&i!==void 0&&t.set(o,W(i))}return[...(e.match(/<(?:[\w.-]+:)?cellXfs\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?cellXfs\s*>/i)?.[1]??"").matchAll(/<(?:[\w.-]+:)?xf\b[^>]*\/?\s*>/gi)].map(r=>{let o=Number(S(r[0],"numFmtId")??"0");return[14,15,16,17].includes(o)?{date:!0,dateTime:!1}:o===22?{date:!0,dateTime:!0}:Zn(t.get(o)??"")})}function Qn(e,t){let n=Number(e);(e.trim()===""||!Number.isFinite(n))&&T("Worksheet contains an invalid date serial.");let r=Math.floor(n),o=n-r,i=r>=60?r-1:r,s=Date.UTC(1899,11,31)+i*864e5+Math.round(o*864e5),a=new Date(s);return Number.isFinite(a.getTime())||T("Worksheet date serial is outside the supported range."),t?a.toISOString():a.toISOString().slice(0,10)}function er(e,t,n){let r=e.indexOf(">"),o=r<0?e:e.slice(0,r+1),i=S(o,"t");if(i==="s"){let u=e.match(/<(?:[\w.-]+:)?v\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?v\s*>/i)?.[1]?.trim();(u===void 0||!/^\d+$/.test(u))&&T("Worksheet contains an invalid shared-string index.");let g=Number(u);return(!Number.isSafeInteger(g)||t[g]===void 0)&&T("Worksheet shared-string index is outside the shared-strings table."),t[g]??""}if(i==="inlineStr"){let u=e.match(/<(?:[\w.-]+:)?is\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?is\s*>/i)?.[1]??"";return wt(u).trim()}let s=e.match(/<(?:[\w.-]+:)?v\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?v\s*>/i)?.[1];if(s===void 0)return"";let a=W(s).trim(),c=S(o,"s"),l=c===void 0?0:Number(c);(!Number.isSafeInteger(l)||l<0||c!==void 0&&n[l]===void 0)&&T("Worksheet contains an invalid cell style reference.");let d=n[l];return d?.date?Qn(a,d.dateTime):a}function tr(e,t,n){let r=new Map,o=0;for(let s of e.matchAll(/<(?:[\w.-]+:)?row\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?row\s*>/gi)){o+=1;let a=s[0].slice(0,s[0].indexOf(">")+1),c=Number(S(a,"r")??o),l=1;for(let d of(s[1]??"").matchAll(/<(?:[\w.-]+:)?c\b[^>]*?(?:\/>|>([\s\S]*?)<\/(?:[\w.-]+:)?c\s*>)/gi)){let u=d[0],g=u.slice(0,u.indexOf(">")+1),E=bt(S(g,"r")??""),h=E?.column??l,f=E?.row??c;if(l=h+1,!Number.isSafeInteger(f)||f<1||!Number.isSafeInteger(h)||h<1)continue;let m=r.get(f)??new Map;m.set(h,er(u,t,n)),r.set(f,m)}}let i=[];for(let s of[...r].sort(([a],[c])=>a-c)){let a=s[1],c=0;for(let d of a.keys())c=Math.max(c,d);let l=Array.from({length:c},(d,u)=>a.get(u+1)??"");for(;l.length>0&&l[l.length-1]==="";)l.pop();l.some(d=>d.length>0)&&i.push(l.join(" "))}return i.join(`
|
|
4
|
-
`)}function J(e,t){let n=Object.values(e.files).filter(r=>!r.dir&&r.name.toLowerCase()===t.toLowerCase());return n.length>1&&_(`XLSX package contains ambiguous case variants of ${t}.`),n[0]}function nr(e){return/<(?:[\w.-]+:)?c\b[^>]*\bt\s*=\s*["']s["']/i.test(e)}async function se(e,t,n,r){let o=J(e,t);if(!o)throw new Error(`XLSX package is missing ${t}.`);let i=n.get(o.name);return i===void 0&&_(`XLSX central-directory size for ${o.name} cannot be checked safely.`),St(o,i,r)}async function yt(e,t,n,r){let o=J(e,t);if(!o)return null;let i=n.get(o.name);i===void 0&&_(`XLSX central-directory size for ${o.name} cannot be checked safely.`);let s={bytes:0},a={pending:"",rowCount:0,cellCount:0,maxRow:0,maxColumn:1};return St(o,i,r,(c,l)=>{Vn(c,l,s,a)})}async function rr(e){try{let t=Gn(e),n=await Un.loadAsync(e),r={bytes:0};if(!J(n,"xl/workbook.xml")){for(let[h,f]of Object.entries(n.files))!f.dir&&/^xl\/worksheets\/[^/]+\.xml$/i.test(h)&&await yt(n,h,t,r);
|
|
3
|
+
`).trim()})};for(let s of t){let a=o.exec(s.replace(/\r$/,""));if(!a){r?r.lines.push(s):s.length>0&&(r={heading:null,level:null,lines:[s]});continue}i(),r={heading:a[2].replace(/[ \t]+#+[ \t]*$/,"").trim(),level:a[1].length,lines:[]}}return i(),n}async function _n(e){let t=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}).decode(e),n=t.startsWith("\uFEFF")?t.slice(1):t,r=hn(n),o=r.some(s=>s.heading!==null),i=r.find(s=>s.heading!==null&&rt(s.heading)!==null)?.heading??null;return{sections:r,versionLabel:i,mediaType:o?"text/markdown":n.length>0?"text/plain":null}}var gn,Ie,st=w(()=>{"use strict";ot();gn=new Set(["text/plain","text/markdown"]);Ie={name:"plain-text",version:"1.0.0",supports:En,extract:_n}});function xn(e,t){let n=Array.from(e,i=>i.codePointAt(0)),r=Array.from(t,i=>i.codePointAt(0)),o=Math.min(n.length,r.length);for(let i=0;i<o;i+=1){let s=n[i],a=r[i];if(s!==a)return s-a}return n.length-r.length}function Pe(e,t){if(e===null)return"null";if(typeof e=="string")return JSON.stringify(e);if(typeof e=="boolean")return e?"true":"false";if(typeof e=="number"){if(!Number.isFinite(e))throw new D;return JSON.stringify(e)}if(typeof e=="bigint")return e<=BigInt(Number.MAX_SAFE_INTEGER)&&e>=BigInt(Number.MIN_SAFE_INTEGER)?JSON.stringify(Number(e)):JSON.stringify(e.toString());if(Array.isArray(e)){if(t.has(e))throw new TypeError("Structured content must be acyclic.");t.add(e);try{return`[${e.map(n=>n===void 0?"null":Pe(n,t)).join(",")}]`}finally{t.delete(e)}}if(typeof e=="object"){let n=e;if(t.has(n))throw new TypeError("Structured content must be acyclic.");t.add(n);try{return`{${Object.keys(n).filter(o=>n[o]!==void 0).sort(xn).map(o=>`${JSON.stringify(o)}:${Pe(n[o],t)}`).join(",")}}`}finally{t.delete(n)}}return"null"}function oe(e){return Pe(e,new Set)}var D,at=w(()=>{"use strict";D=class extends Error{code="non_finite_number";constructor(){super("Canonical structured content contains a non-finite number."),this.name="NonFiniteNumberCanonicalizationError"}}});import{parseDocument as Tn}from"yaml";function ct(e,t){if(typeof e=="string")return e;if(typeof e!="object"||e===null)return null;let n=e;for(let r of t)if(typeof n[r]=="string")return n[r];return null}function yn(e,t){if(typeof e!="string"){let o=ct(e,["mediaType","mimeType","contentType"]);if(o!==null)return t.mediaTypes.includes(o.split(";")[0].trim().toLowerCase())}let n=typeof e=="string"?e:ct(e,["filename","name","path","extension"]);if(n===null)return!1;let r=n.toLowerCase();return t.extensions.some(o=>r.endsWith(o))}function dt(e){return typeof e=="string"?e:JSON.stringify(e,(t,n)=>typeof n=="bigint"?n.toString():n,2)??"null"}function ut(e){return JSON.parse(e,(t,n,r)=>{let o=r?.source;return typeof n=="number"&&!Number.isSafeInteger(n)&&o!==void 0&&/^-?(?:0|[1-9][0-9]*)$/.test(o)?BigInt(o):n})}function Xe(e){try{return{canonicalContent:oe(e)}}catch(t){if(t instanceof D)return{canonicalizationError:t.code};throw t}}function bn(e){let t;try{t=ut(e)}catch{throw new U("extraction_invalid_json")}return{sections:typeof t=="object"&&t!==null&&!Array.isArray(t)?Object.entries(t).map(([r,o],i)=>({ordinal:i,heading:r,level:1,text:dt(o)})):[{ordinal:0,heading:null,level:null,text:dt(t)}],...Xe(t)}}function Sn(e){let t=[],n=[];for(let r of e.split(/\r?\n/)){let o=r.trim();if(o.length!==0){try{n.push(ut(o))}catch{throw new U("extraction_invalid_json")}t.push({ordinal:t.length,heading:null,level:null,text:o})}}return{sections:t,...Xe(n)}}function j(e,t=new Set){if(typeof e!="object"||e===null)return e;if(t.has(e))throw new U("extraction_invalid_yaml");t.add(e);try{if(e instanceof Map){let r=Object.create(null);for(let[o,i]of e){let s=typeof o=="string"?o:oe(j(o,t));r[s]=j(i,t)}return r}if(Array.isArray(e))return e.map(r=>j(r,t));let n=Object.create(null);for(let[r,o]of Object.entries(e))n[r]=j(o,t);return n}finally{t.delete(e)}}function lt(e){return typeof e=="string"?e:JSON.stringify(e,(t,n)=>typeof n=="bigint"?n.toString():n,2)??"null"}function wn(e){let t=e.contents,n=new Map;for(let r of t?.items??[]){let o=r.key?.range?.[0];if(o===void 0)continue;let i=r.key&&Object.hasOwn(r.key,"value")?r.key.value:r.key?.toJSON?.(),s=j(i),a=typeof s=="string"?s:oe(s);n.set(a,o)}return n}function Rn(e){let t,n,r;try{let i=Tn(e,{schema:"core",uniqueKeys:!1,intAsBigInt:!0});if(i.errors.length>0)throw new Error("invalid YAML document");t=j(i.toJS({mapAsMap:!0})),n=wn(i)}catch(i){if(!(i instanceof D))throw new U("extraction_invalid_yaml");r=i.code,t=null,n=new Map}if(r!==void 0)return{sections:[{ordinal:0,heading:null,level:null,text:e.trim()}],canonicalizationError:r};let o;if(typeof t=="object"&&t!==null&&!Array.isArray(t)){let i=Object.entries(t).map(([s,a])=>({heading:s,value:a,start:n.get(s)})).filter(s=>s.start!==void 0).sort((s,a)=>s.start-a.start);o=i.map(({heading:s,start:a},c)=>({ordinal:c,heading:s,level:1,text:e.slice(a,i[c+1]?.start??e.length).trim()})),o.length===0&&(o=[{ordinal:0,heading:null,level:null,text:lt(t)}])}else o=[{ordinal:0,heading:null,level:null,text:lt(t)}];return{sections:o,...Xe(t)}}function Ce(e){return{name:e.name,version:"1.0.0",mediaTypes:e.mediaTypes,supports(t){return yn(t,e)},async extract(t){let n=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}).decode(t),r=n.startsWith("\uFEFF")?n.slice(1):n;if(r.length===0)return{sections:[],versionLabel:null,mediaType:null};let o;switch(e.kind){case"json":o=bn(r);break;case"jsonl":o=Sn(r);break;case"yaml":o=Rn(r);break}return{sections:o.sections,versionLabel:null,mediaType:e.mediaTypes[0]??null,...o.canonicalContent===void 0?{}:{canonicalContent:o.canonicalContent},...o.canonicalizationError===void 0?{}:{canonicalizationError:o.canonicalizationError}}}}}var U,ft,mt,ve,pt=w(()=>{"use strict";at();U=class extends Error{code;constructor(t){super(t==="extraction_invalid_json"?"Structured JSON is invalid.":"Structured YAML is invalid."),this.name="StructuredTextExtractionError",this.code=t}};ft=Ce({kind:"json",name:"json",extensions:[".json"],mediaTypes:["application/json"]}),mt=Ce({kind:"jsonl",name:"jsonl",extensions:[".jsonl"],mediaTypes:["application/x-ndjson"]}),ve=Ce({kind:"yaml",name:"yaml",extensions:[".yaml",".yml"],mediaTypes:["application/yaml"]})});import{PassThrough as Nn}from"node:stream";import{StringDecoder as An}from"node:string_decoder";import Ln from"jszip";function On(e){if(typeof e=="string")return/\.pptx$/i.test(e);if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return n.split(";")[0].trim().toLowerCase()===ke;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.pptx$/i.test(r)}function In(e){return e.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi,(t,n)=>{switch(n.toLowerCase()){case"amp":return"&";case"lt":return"<";case"gt":return">";case"quot":return'"';case"apos":return"'";default:{let r=n.startsWith("#x")||n.startsWith("#X")?Number.parseInt(n.slice(2),16):Number.parseInt(n.slice(1),10);return String.fromCodePoint(r)}}})}function gt(e){return Number(e.match(/\/slide(\d+)\.xml$/)?.[1]??Number.MAX_SAFE_INTEGER)}function T(e){throw new b("archive_too_large",e)}function Pn(e,t,n,r){t>n&&T(`PPTX entry ${e} decompressed size differs from its declared size (${n} bytes).`),r>O&&T(`PPTX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`)}function F(e,t,n){t+8>n&&T("PPTX ZIP64 metadata is truncated.");let r=e.getBigUint64(t,!0);return r>BigInt(Number.MAX_SAFE_INTEGER)&&T("PPTX ZIP64 metadata exceeds safe bounds."),Number(r)}function H(e,t,n){return t>=0&&t+4<=e.byteLength&&e.getUint32(t,!0)===n}function Xn(e,t){let n=F(e,t+8,t+20),r=o=>{if(!H(e,o,101075792)||o+12>t)return!1;let i=F(e,o+4,t);return i>=44&&o+12+i===t};if(r(n))return n;for(let o=t-56;o>=0;o-=1)if(r(o))return o;T("PPTX ZIP64 end record cannot be located safely.")}function Cn(e,t,n,r,o){let i=e.getUint32(t+24,!0);if(i!==4294967295)return i;let s=t+46+r,a=s+o;for(;s+4<=a;){let c=e.getUint16(s,!0),l=e.getUint16(s+2,!0),d=s+4,u=d+l;if((u>a||u>n)&&T("PPTX central-directory extra field is truncated."),c===1)return F(e,d,u);s=u}T("PPTX ZIP64 entry size is missing from its central-directory extra field.")}function vn(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=Math.max(0,e.byteLength-22-65535),r=-1;for(let E=e.byteLength-22;E>=n;E-=1)if(H(t,E,101010256)&&E+22+t.getUint16(E+20,!0)===e.byteLength){r=E;break}if(r<0)return new Map;let o=t.getUint16(r+10,!0),i=t.getUint32(r+12,!0),s=t.getUint32(r+16,!0),a=r-20,c=H(t,a,117853008);(o===65535||i===4294967295||s===4294967295)&&!c&&T("PPTX ZIP64 end record is missing.");let d=o,u=i,g=s,m=r;if(c){let E=Xn(t,a),R=a;d=F(t,E+32,R),u=F(t,E+40,R),g=F(t,E+48,R),m=E}d>L&&T(`PPTX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`),(u>m||g>m||u+g>m)&&T("PPTX central-directory bounds cannot be checked safely.");let h=m-u-g,f=g+h,p=f+u,x=0,A=0,z=new Map;for(;f+4<=p&&H(t,f,33639248);){f+46>p&&T("PPTX central-directory bounds cannot be checked safely.");let E=t.getUint16(f+28,!0),R=t.getUint16(f+30,!0),he=t.getUint16(f+32,!0),G=f+46+E+R+he;G>p&&T("PPTX central-directory entry is truncated."),x+=1,x>L&&T(`PPTX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`);let X=Cn(t,f,p,E,R),_e=new TextDecoder().decode(e.subarray(f+46,f+46+E));z.set(_e,X),X>O-A&&T(`PPTX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`),A+=X,f=G}if(f<p&&H(t,f,84233040)&&f+6<=p){let E=t.getUint16(f+4,!0);f+6+E===p&&(f=p)}return(f!==p||x!==d)&&T("PPTX central-directory count or bounds cannot be checked safely."),z}async function kn(e,t,n){let r=[],o=new An("utf8"),i=new Nn,s=e.nodeStream("nodebuffer");s.on("error",c=>i.destroy(c)),s.pipe(i);let a=0;try{for await(let c of i){let l=Buffer.isBuffer(c)?c:Buffer.from(c);a+=l.byteLength,Pn(e.name,a,n,t+a),r.push(o.write(l))}}catch(c){throw c instanceof Error&&c.message.includes("uncompressed data size mismatch")&&T(`PPTX entry ${e.name} decompressed size differs from its declared size (${n} bytes).`),c}return r.push(o.end()),{xml:r.join(""),bytesRead:a}}async function Mn(e){let t=vn(e),r=(await Ln.loadAsync(e)).file(/^ppt\/slides\/slide\d+\.xml$/).sort((s,a)=>gt(s.name)-gt(a.name)),o=[],i=0;for(let s of r){let a=t.get(s.name);a===void 0&&T(`PPTX central-directory size for ${s.name} cannot be checked safely.`);let c=await kn(s,i,a);i+=c.bytesRead;let l=[...c.xml.matchAll(/<a:t(?:\s[^>]*)?>([\s\S]*?)<\/a:t>/g)].map(d=>In(d[1]??"").trim()).filter(d=>d.length>0).join(" ");l.length>0&&o.push({ordinal:o.length,heading:null,level:null,text:l})}return{sections:o,versionLabel:null,mediaType:ke}}var ke,L,O,Et,Me=w(()=>{"use strict";M();ke="application/vnd.openxmlformats-officedocument.presentationml.presentation",L=5e3,O=128*1024*1024;Et={name:"pptx",version:"3.10.2",mediaTypes:[ke],supports:On,extract:Mn}});import{posix as ht}from"node:path";import{PassThrough as Dn}from"node:stream";import{StringDecoder as jn}from"node:string_decoder";import Un from"jszip";function $n(e){if(typeof e=="string")return/\.xlsx$/i.test(e);if(typeof e!="object"||e===null)return!1;let t=e,n=t.mediaType??t.mimeType??t.contentType;if(typeof n=="string")return n.split(";")[0].trim().toLowerCase()===De;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.xlsx$/i.test(r)}function y(e){throw new b("sheet_too_large",e)}function _(e){throw new b("archive_too_large",e)}function bt(e){let t=/^\$?([A-Z]{1,3})\$?(\d+)$/i.exec(e);if(!t?.[1]||!t[2])return null;let n=0;for(let o of t[1].toUpperCase())n=n*26+o.charCodeAt(0)-64;let r=Number(t[2]);return Number.isSafeInteger(r)&&r>0&&n<=Fn?{row:r,column:n}:null}function Tt(e,t){(!Number.isSafeInteger(e)||e<1||!Number.isSafeInteger(t)||t<1)&&y("Worksheet contains an invalid row or column reference."),(e>Y||e*t>ae)&&y(`Worksheet exceeds MAX_SHEET_ROWS (${Y}) or MAX_SHEET_CELLS (${ae}).`)}function S(e,t){return e.match(new RegExp(`\\b${t}\\s*=\\s*[\\"']([^\\"']*)[\\"']`,"i"))?.[1]}function $(e,t,n){t+8>n&&_("XLSX ZIP64 metadata is truncated.");let r=e.getBigUint64(t,!0);return r>BigInt(Number.MAX_SAFE_INTEGER)&&_("XLSX ZIP64 metadata exceeds safe bounds."),Number(r)}function V(e,t,n){return t>=0&&t+4<=e.byteLength&&e.getUint32(t,!0)===n}function Bn(e,t){let n=$(e,t+8,t+20),r=i=>{if(!V(e,i,101075792)||i+12>t)return!1;let s=$(e,i+4,t);return s>=44&&i+12+s===t};if(r(n))return n;let o=0;for(let i=t-56;i>=0;i-=1)if(o>=xt&&_(`XLSX ZIP64 recovery scan exceeds MAX_ZIP64_RECOVERY_SCAN_ITERATIONS (${xt} iterations).`),o+=1,r(i))return i;_("XLSX ZIP64 end record cannot be located safely.")}function zn(e,t,n,r,o){let i=e.getUint32(t+24,!0);if(i!==4294967295)return i;let s=t+46+r,a=s+o;for(;s+4<=a;){let c=e.getUint16(s,!0),l=e.getUint16(s+2,!0),d=s+4,u=d+l;if((u>a||u>n)&&_("XLSX central-directory extra field is truncated."),c===1)return $(e,d,u);s=u}_("XLSX ZIP64 entry size is missing from its central-directory extra field.")}function Gn(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=Math.max(0,e.byteLength-22-65535),r=-1;for(let E=e.byteLength-22;E>=n;E-=1)if(V(t,E,101010256)&&E+22+t.getUint16(E+20,!0)===e.byteLength){r=E;break}r<0&&_("XLSX end-of-central-directory record is missing.");let o=t.getUint16(r+10,!0),i=t.getUint32(r+12,!0),s=t.getUint32(r+16,!0),a=r-20,c=V(t,a,117853008);(o===65535||i===4294967295||s===4294967295)&&!c&&_("XLSX ZIP64 end record is missing.");let d=o,u=i,g=s,m=r;if(c){let E=Bn(t,a),R=a;d=$(t,E+32,R),u=$(t,E+40,R),g=$(t,E+48,R),m=E}d>L&&_(`XLSX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`),(u>m||g>m||u+g>m)&&_("XLSX central-directory bounds cannot be checked safely.");let h=m-u-g,f=g+h,p=f+u,x=0,A=0,z=new Map;for(;f+4<=p&&V(t,f,33639248);){f+46>p&&_("XLSX central-directory bounds cannot be checked safely.");let E=t.getUint16(f+28,!0),R=t.getUint16(f+30,!0),he=t.getUint16(f+32,!0),G=f+46+E+R+he;G>p&&_("XLSX central-directory entry is truncated."),x+=1,x>L&&_(`XLSX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`);let X=zn(t,f,p,E,R),_e=new TextDecoder().decode(e.subarray(f+46,f+46+E));z.set(_e,X),X>O-A&&_(`XLSX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`),A+=X,f=G}if(f<p&&V(t,f,84233040)&&f+6<=p){let E=t.getUint16(f+4,!0);f+6+E===p&&(f=p)}return(f!==p||x!==d)&&_("XLSX central-directory count or bounds cannot be checked safely."),z}function Hn(e,t,n,r){t>n&&_(`XLSX entry ${e} decompressed size differs from its declared size (${n} bytes).`),r>O&&_(`XLSX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`)}async function St(e,t,n,r){let o=[],i=new jn("utf8"),s=new Dn,a=e.nodeStream("nodebuffer");a.on("error",d=>s.destroy(d)),a.pipe(s);let c=0;try{for await(let d of s){let u=Buffer.isBuffer(d)?d:Buffer.from(d);c+=u.byteLength,Hn(e.name,c,t,n.bytes+c);let g=i.write(u);o.push(g),r?.(g,u.byteLength)}}catch(d){throw d instanceof Error&&d.message.includes("uncompressed data size mismatch")&&_(`XLSX entry ${e.name} decompressed size differs from its declared size (${t} bytes).`),d}let l=i.end();return o.push(l),r?.(l,0),c!==t&&_(`XLSX entry ${e.name} decompressed size differs from its declared size (${t} bytes).`),n.bytes+=c,o.join("")}function Vn(e,t,n,r){n.bytes+=t,n.bytes>_t&&y(`Worksheet XML scan exceeds MAX_SHEET_SCAN_BYTES (${_t} bytes).`);let o=r.pending+e,i=0;for(r.pending="";;){let s=o.indexOf("<",i);if(s<0)break;let a=o.indexOf(">",s+1);if(a<0){r.pending=o.slice(s),Buffer.byteLength(r.pending)>ie&&y(`Worksheet metadata exceeds MAX_SHEET_TAG_BYTES (${ie} bytes).`);break}let c=o.slice(s,a+1);if(i=a+1,Buffer.byteLength(c)>ie&&y(`Worksheet metadata exceeds MAX_SHEET_TAG_BYTES (${ie} bytes).`),/^<(?:[\w.-]+:)?row\b/i.test(c)){r.rowCount+=1;let l=S(c,"r");l!==void 0&&!/^\d+$/.test(l)&&y("Worksheet contains an invalid row reference.");let d=Number(l??r.rowCount);(!Number.isSafeInteger(d)||d<1)&&y("Worksheet contains an invalid row reference."),(d>Y||r.rowCount>Y)&&y(`Worksheet exceeds MAX_SHEET_ROWS (${Y}).`),r.maxRow=Math.max(r.maxRow,d),Tt(r.maxRow,r.maxColumn)}else if(/^<(?:[\w.-]+:)?c\b/i.test(c)){r.cellCount+=1,r.cellCount>ae&&y(`Worksheet exceeds MAX_SHEET_CELLS (${ae}).`);let l=S(c,"r"),d=l===void 0?null:bt(l);l!==void 0&&!d&&y("Worksheet contains an invalid cell reference."),d&&(r.maxRow=Math.max(r.maxRow,d.row),r.maxColumn=Math.max(r.maxColumn,d.column),Tt(r.maxRow,r.maxColumn))}}}function W(e){return e.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi,(t,n)=>{switch(n.toLowerCase()){case"amp":return"&";case"lt":return"<";case"gt":return">";case"quot":return'"';case"apos":return"'";default:{let r=n.startsWith("#x")||n.startsWith("#X")?Number.parseInt(n.slice(2),16):Number.parseInt(n.slice(1),10),o=r===9||r===10||r===13||r>=32&&r<=55295||r>=57344&&r<=65533||r>=65536&&r<=1114111;return(!Number.isInteger(r)||!o)&&y("XLSX contains an invalid XML character reference."),String.fromCodePoint(r)}}})}function wt(e){return[...e.replace(/<(?:[\w.-]+:)?rPh\b[\s\S]*?<\/(?:[\w.-]+:)?rPh\s*>/gi,"").matchAll(/<(?:[\w.-]+:)?t\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?t\s*>/gi)].map(n=>W(n[1]??"")).join("")}function Yn(e){return[...e.matchAll(/<(?:[\w.-]+:)?si\b[^>]*\/>|<(?:[\w.-]+:)?si\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?si\s*>/gi)].map(t=>t[0].endsWith("/>")?"":wt(t[1]??""))}function Jn(e){let t;try{t=decodeURIComponent(W(e))}catch(o){if(o instanceof b)throw o;return null}let n=t.split(/[?#]/,1)[0]??"",r=ht.normalize(n.startsWith("/")?n.slice(1):ht.join("xl",n));return r.startsWith("xl/")?r:null}function Wn(e){let t=new Map;for(let n of e.matchAll(/<(?:[\w.-]+:)?Relationship\b[^>]*\/?>/gi)){let r=n[0];if(!/\/worksheet$/i.test(S(r,"Type")??"")||/^external$/i.test(S(r,"TargetMode")??""))continue;let o=S(r,"Id"),i=S(r,"Target"),s=i?Jn(i):null;o&&s&&t.set(o,s)}return t}function Kn(e,t){let n=[];for(let r of e.matchAll(/<(?:[\w.-]+:)?sheet\b[^>]*\/?>/gi)){let o=r[0],i=S(o,"name"),s=S(o,"r:id")??S(o,"id"),a=s?t.get(s):void 0;(i===void 0||a===void 0)&&_("XLSX workbook contains an unresolved worksheet relationship."),n.push({name:W(i),part:a})}return n}function Zn(e){let t=e.replace(/"(?:""|[^"])*"/g,"").replace(/\\./g,"").replace(/_.|\*./g,"").replace(/\[[^\]]*\]/g,""),n=/y/i.test(t)&&/m/i.test(t)&&/d/i.test(t);return{date:n,dateTime:n&&(/h|s/i.test(t)||/am\/pm|\[(?:h+|s+)\]/i.test(e))}}function qn(e){let t=new Map;for(let r of e.matchAll(/<(?:[\w.-]+:)?numFmt\b[^>]*\/?\s*>/gi)){let o=Number(S(r[0],"numFmtId")),i=S(r[0],"formatCode");Number.isSafeInteger(o)&&i!==void 0&&t.set(o,W(i))}return[...(e.match(/<(?:[\w.-]+:)?cellXfs\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?cellXfs\s*>/i)?.[1]??"").matchAll(/<(?:[\w.-]+:)?xf\b[^>]*\/?\s*>/gi)].map(r=>{let o=Number(S(r[0],"numFmtId")??"0");return[14,15,16,17].includes(o)?{date:!0,dateTime:!1}:o===22?{date:!0,dateTime:!0}:Zn(t.get(o)??"")})}function Qn(e,t){let n=Number(e);(e.trim()===""||!Number.isFinite(n))&&y("Worksheet contains an invalid date serial.");let r=Math.floor(n),o=n-r,i=r>=60?r-1:r,s=Date.UTC(1899,11,31)+i*864e5+Math.round(o*864e5),a=new Date(s);return Number.isFinite(a.getTime())||y("Worksheet date serial is outside the supported range."),t?a.toISOString():a.toISOString().slice(0,10)}function er(e,t,n){let r=e.indexOf(">"),o=r<0?e:e.slice(0,r+1),i=S(o,"t");if(i==="s"){let u=e.match(/<(?:[\w.-]+:)?v\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?v\s*>/i)?.[1]?.trim();(u===void 0||!/^\d+$/.test(u))&&y("Worksheet contains an invalid shared-string index.");let g=Number(u);return(!Number.isSafeInteger(g)||t[g]===void 0)&&y("Worksheet shared-string index is outside the shared-strings table."),t[g]??""}if(i==="inlineStr"){let u=e.match(/<(?:[\w.-]+:)?is\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?is\s*>/i)?.[1]??"";return wt(u).trim()}let s=e.match(/<(?:[\w.-]+:)?v\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?v\s*>/i)?.[1];if(s===void 0)return"";let a=W(s).trim(),c=S(o,"s"),l=c===void 0?0:Number(c);(!Number.isSafeInteger(l)||l<0||c!==void 0&&n[l]===void 0)&&y("Worksheet contains an invalid cell style reference.");let d=n[l];return d?.date?Qn(a,d.dateTime):a}function tr(e,t,n){let r=new Map,o=0;for(let s of e.matchAll(/<(?:[\w.-]+:)?row\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?row\s*>/gi)){o+=1;let a=s[0].slice(0,s[0].indexOf(">")+1),c=Number(S(a,"r")??o),l=1;for(let d of(s[1]??"").matchAll(/<(?:[\w.-]+:)?c\b[^>]*?(?:\/>|>([\s\S]*?)<\/(?:[\w.-]+:)?c\s*>)/gi)){let u=d[0],g=u.slice(0,u.indexOf(">")+1),m=bt(S(g,"r")??""),h=m?.column??l,f=m?.row??c;if(l=h+1,!Number.isSafeInteger(f)||f<1||!Number.isSafeInteger(h)||h<1)continue;let p=r.get(f)??new Map;p.set(h,er(u,t,n)),r.set(f,p)}}let i=[];for(let s of[...r].sort(([a],[c])=>a-c)){let a=s[1],c=0;for(let d of a.keys())c=Math.max(c,d);let l=Array.from({length:c},(d,u)=>a.get(u+1)??"");for(;l.length>0&&l[l.length-1]==="";)l.pop();l.some(d=>d.length>0)&&i.push(l.join(" "))}return i.join(`
|
|
4
|
+
`)}function J(e,t){let n=Object.values(e.files).filter(r=>!r.dir&&r.name.toLowerCase()===t.toLowerCase());return n.length>1&&_(`XLSX package contains ambiguous case variants of ${t}.`),n[0]}function nr(e){return/<(?:[\w.-]+:)?c\b[^>]*\bt\s*=\s*["']s["']/i.test(e)}async function se(e,t,n,r){let o=J(e,t);if(!o)throw new Error(`XLSX package is missing ${t}.`);let i=n.get(o.name);return i===void 0&&_(`XLSX central-directory size for ${o.name} cannot be checked safely.`),St(o,i,r)}async function yt(e,t,n,r){let o=J(e,t);if(!o)return null;let i=n.get(o.name);i===void 0&&_(`XLSX central-directory size for ${o.name} cannot be checked safely.`);let s={bytes:0},a={pending:"",rowCount:0,cellCount:0,maxRow:0,maxColumn:1};return St(o,i,r,(c,l)=>{Vn(c,l,s,a)})}async function rr(e){try{let t=Gn(e),n=await Un.loadAsync(e),r={bytes:0};if(!J(n,"xl/workbook.xml")){for(let[h,f]of Object.entries(n.files))!f.dir&&/^xl\/worksheets\/[^/]+\.xml$/i.test(h)&&await yt(n,h,t,r);y("XLSX package is missing xl/workbook.xml.")}let o=await se(n,"xl/workbook.xml",t,r),i=await se(n,"xl/_rels/workbook.xml.rels",t,r),s=Kn(o,Wn(i));s.length===0&&_("XLSX workbook has no resolvable worksheets.");let a=new Map;for(let h of s){let f=h.part.toLowerCase();if(a.has(f))continue;let p=await yt(n,h.part,t,r);p!==null&&a.set(f,p)}let c=J(n,"xl/sharedStrings.xml"),l=[...a.values()].some(nr);!c&&l&&_("XLSX worksheet references shared strings, but the shared-strings part is missing.");let d=c?Yn(await se(n,c.name,t,r)):[],u=J(n,"xl/styles.xml"),g=u?qn(await se(n,u.name,t,r)):[],m=[];for(let h of s){let f=a.get(h.part.toLowerCase());f===void 0&&_(`XLSX package is missing worksheet ${h.part}.`);let p=tr(f,d,g);p.trim().length!==0&&m.push({ordinal:m.length,heading:h.name,level:1,text:p})}return{sections:m,versionLabel:null,mediaType:De}}catch(t){if(t instanceof b)throw t;_("XLSX package could not be parsed safely.")}}var De,Y,ae,ie,_t,Fn,xt,Rt,Nt=w(()=>{"use strict";Me();M();De="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",Y=1e5,ae=1e6,ie=64*1024,_t=16*1024*1024,Fn=16384,xt=1e6;Rt={name:"xlsx",version:"4.4.0",mediaTypes:[De],supports:$n,extract:rr}});import*as Ue from"node:path";function At(e){let t=Ue.extname(e).toLowerCase(),n=or.get(t);if(!n||!n.supports({filename:e}))throw new je(e);return n}var je,or,Lt=w(()=>{"use strict";Ve();Le();nt();st();pt();Me();Nt();je=class extends Error{code="unsupported_media_type";extension;constructor(t){let n=Ue.extname(t).toLowerCase()||"(none)";super(`Unsupported media type for extension "${n}".`),this.name="UnsupportedMediaTypeError",this.extension=n}},or=new Map([[".pdf",tt],[".docx",He],[".png",ne],[".jpg",ne],[".jpeg",ne],[".xlsx",Rt],[".pptx",Et],[".txt",Ie],[".md",Ie],[".yaml",ve],[".yml",ve],[".json",ft],[".jsonl",mt]])});import*as N from"node:fs/promises";import*as ce from"node:path";function sr(e){if(!/^[0-9a-f]{64}$/.test(e)||e.includes("..")||e.includes("/")||e.includes("\\"))throw new Error("Invalid SHA-256 hash: expected 64 lowercase hexadecimal characters.");return ce.join("objects",e.slice(0,2),e.slice(2,4),e)}async function Ot(e,t){return N.readFile(ce.join(e,sr(t)))}var xo,To,ir,yo,It=w(()=>{"use strict";M();xo=64*1024,To=1440*60*1e3,ir=["EPERM","ENOTSUP","EXDEV","EACCES"],yo=new Set(ir)});import*as k from"node:fs";import*as C from"node:path";var Z=class extends Error{code;path;constructor(t,n,r){let o=r instanceof Error?r.message:String(r);super(n==="config_invalid"?`Invalid JSON in config file ${t}: ${o}`:`Unable to read config file ${t}: ${o}`,{cause:r}),this.name="ConfigDiscoveryError",this.code=n,this.path=t}};function Gt(e){let t;try{t=k.readFileSync(e,"utf8")}catch(n){throw new Z(e,"config_unreadable",n)}try{return JSON.parse(t)}catch(n){throw new Z(e,"config_invalid",n)}}function xe({cwd:e,homeDir:t,argv:n,env:r}){let o={};for(let x=0;x<n.length;x+=1){let A=/^--(client|project|root)=(.*)$/.exec(n[x]);A?o[A[1]]=A[2]:/^--(client|project|root)$/.test(n[x])&&n[x+1]!==void 0&&(o[n[x].slice(2)]=n[x+1],x+=1)}let i={client:r.RAG_CLIENT,project:r.RAG_PROJECT,root:r.RAG_ROOT},s=C.join(e,"rag.config.json"),a=C.join(C.dirname(e),"rag.config.json"),c=C.join(t,".rag","config.json"),l=k.existsSync(s)?s:k.existsSync(a)?a:k.existsSync(c)?c:null,d=l===null?{}:Gt(l),u=typeof d.client=="string"?d.client:void 0,g=typeof d.project=="string"?d.project:void 0,m=o.client??i.client??u??null,h=o.project??i.project??g??null,f=m===null&&h===null?null:{client:m,project:h},p=o.client!==void 0||o.project!==void 0?"args":i.client!==void 0||i.project!==void 0?"env":u!==void 0||g!==void 0?l===s?"cwd":l===a?"parent":"home":"unscoped";return{namespace:f,source:p,configPath:p==="cwd"||p==="parent"||p==="home"?l:null,failClosed:typeof d.failClosed=="boolean"?d.failClosed:!0,root:o.root??i.root??(typeof d.root=="string"?d.root:C.join(t,".rag"))}}be();import*as ee from"node:fs";import{DatabaseSync as Vt}from"node:sqlite";var Yt=800,Se=/[\p{L}\p{M}\p{N}]+/gu;function Jt(e){return e.match(Se)??[]}function Wt(e){return e.map(t=>`"${t.replaceAll('"','""')}"`).join(" AND ")}function ze(e){return e.normalize("NFD").replace(new RegExp("\\p{M}","gu"),"").toLowerCase()}function Kt(e,t){let n=new Set(t.map(ze));for(let r of e.matchAll(Se)){let o=r[0];if(!n.has(ze(o)))continue;let i=r.index;return{start:i,end:i+o.length}}return null}function Zt(e,t,n,r){let o=Math.min(r,e.length);if(o===0)return"";let i=Math.min(n-t,o),s=Math.max(0,Math.min(t-Math.floor((o-i)/2),e.length-o)),a=s+o,c=s,l=a,d=Array.from(e.matchAll(Se),m=>({start:m.index,end:m.index+m[0].length})),u=d.find(m=>m.start<s&&s<m.end);u&&(s=d.find(m=>m.start>=u.end)?.start??e.length);let g=d.find(m=>m.start<a&&a<m.end);if(g){let m=d.filter(h=>h.end<=g.start);a=m[m.length-1]?.end??0}return a<=s?e.slice(c,l):e.slice(s,a)}function qt(e,t){return e<t?-1:e>t?1:0}function Q(e,t,n){return{namespace:e,error:{code:t,message:n}}}function Ge(e){let{root:t,namespace:n,query:r}=e,o=e.excerptLength??Yt;if(!Number.isInteger(o)||o<1)return Q(n,"invalid_excerpt_length","excerptLength must be a positive integer.");let i=Jt(r);if(i.length===0)return{namespace:n,result:{citations:[],total:0}};let s;try{let a=q(t,n.client);if(!ee.existsSync(a)||!ee.statSync(a).isFile())return Q(n,"database_not_found",`No database exists for namespace ${n.client}/${n.project}.`);s=new Vt(a,{readOnly:!0});let c=s.prepare(`SELECT dv.doc_version_id AS docVersionId, d.document_id AS documentId,
|
|
5
5
|
e.entity_key AS entityKey, d.label AS label, dv.version_label AS versionLabel,
|
|
6
6
|
s.ordinal AS sectionOrdinal, c.ordinal AS chunkOrdinal, c.text AS text,
|
|
7
7
|
c.char_start AS charStart, c.char_end AS charEnd, bm25(chunk_fts) AS score
|
|
@@ -24,4 +24,4 @@ var w=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(r){throw n=
|
|
|
24
24
|
JOIN project AS p ON p.project_id = d.project_id
|
|
25
25
|
JOIN client AS cl ON cl.client_id = p.client_id
|
|
26
26
|
LEFT JOIN object AS o ON o.sha256 = dv.sha256
|
|
27
|
-
WHERE c.doc_version_id = ? AND c.ordinal = ? AND cl.name = ? AND p.slug = ?`).get(r,o,n.client,n.project);if(!l)return I(n,"citation_not_found","The citation does not exist in the requested document version and namespace.");if(!l.sha256||!l.relPath)return I(n,"object_missing","The citation's recorded object is missing.");let d;try{let
|
|
27
|
+
WHERE c.doc_version_id = ? AND c.ordinal = ? AND cl.name = ? AND p.slug = ?`).get(r,o,n.client,n.project);if(!l)return I(n,"citation_not_found","The citation does not exist in the requested document version and namespace.");if(!l.sha256||!l.relPath)return I(n,"object_missing","The citation's recorded object is missing.");let d;try{let p=K.resolve(t,l.relPath),x=K.resolve(t);if(p!==x&&!p.startsWith(`${x}${K.sep}`))return I(n,"object_path_invalid","The recorded object path escapes the configured root.");d=await Ot(t,l.sha256)}catch(p){if(p.code==="ENOENT")return I(n,"object_missing","The citation's recorded object file is missing.");throw p}let u=await At(l.label).extract(d),g=u.sections[o]?.text;if(g===void 0)return I(n,"citation_not_found","The stored object does not contain the requested extracted section.");let m=cr(g,i),h=dr(m.text,s),f=u.sections.length;return{namespace:n,result:{text:h.text,truncated:m.truncated||h.truncated,totalChunks:f,nextChunkOrdinal:o+1<f?o+1:null}}}catch(c){let l=c instanceof Error?c.message:String(c);return I(n,"object_read_failed",l)}finally{a?.close()}}import{z as v}from"zod";var Xt=100,Ct=2e3,vt=1048576,kt=1e4,lr=10,ur=800,fr=65536,mr=200;function Mt(e){let t=new Set;for(let n of(e??"find,read").split(",").map(r=>r.trim())){if(n!=="find"&&n!=="read")throw new Error(`invalid_capability: Unknown RAG_CAPABILITIES value "${n}".`);t.add(n)}return t}var ue=v.string().min(1).refine(e=>e.trim().length>0),Fe=v.object({client:ue,project:ue}).strict();function fe(e,t,n){return v.number().int().min(e).max(t).optional().transform(r=>r??n)}var Dt=v.object({namespace:Fe,query:ue,limit:fe(1,Xt,lr),excerptChars:fe(1,Ct,ur)}).strict(),jt=v.object({namespace:Fe,docVersionId:ue,chunkOrdinal:v.number().multipleOf(1).min(0).refine(Number.isSafeInteger),maxBytes:fe(0,vt,fr),maxLines:fe(0,kt,mr)}).strict();function pr(e){let t=e.find(i=>i.code==="unrecognized_keys"&&i.path.length===0)??e.find(i=>i.code==="unrecognized_keys");if(!t||t.code!=="unrecognized_keys")return null;let n=t.keys[0];if(n===void 0)return null;let r=t.path.map(String).join(".");return{code:"unknown_field",message:`Unknown field "${r.length>0?`${r}.${n}`:n}".`}}function P(e,t){return e.some(n=>n.path[0]===t)}function le(e,t,n){return{code:"invalid_limit",message:`${e} must be an integer from ${t} to ${n}.`}}function Ut(e,t){let n=pr(e);if(n)return n;if(P(e,"namespace"))return{code:"namespace_required",message:"namespace must contain non-empty client and project strings."};if(t==="find"){if(P(e,"query"))return{code:"invalid_query",message:"query must be a non-empty string."};if(P(e,"limit"))return le("limit",1,Xt);if(P(e,"excerptChars"))return le("excerptChars",1,Ct)}else{if(P(e,"docVersionId"))return{code:"invalid_input",message:"docVersionId must be a non-empty string."};if(P(e,"chunkOrdinal"))return{code:"invalid_input",message:"chunkOrdinal must be a non-negative integer."};if(P(e,"maxBytes"))return le("maxBytes",0,vt);if(P(e,"maxLines"))return le("maxLines",0,kt)}return e.some(r=>r.path.length===0)?{code:"invalid_input",message:"Tool arguments must be an object."}:{code:"invalid_input",message:"Tool arguments are invalid."}}function gr(e){let t=Fe.safeParse(e);return t.success?t.data:null}function Ft(e){let t=Dt.safeParse(e);return t.success?{ok:!0,input:t.data,namespace:t.data.namespace}:{ok:!1,namespace:me(e),error:Ut(t.error.issues,"find")}}function $t(e){let t=jt.safeParse(e);return t.success?{ok:!0,input:t.data,namespace:t.data.namespace}:{ok:!1,namespace:me(e),error:Ut(t.error.issues,"read")}}function Er(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function me(e){return Er(e)?gr(e.namespace):null}function Bt(e){return v.toJSONSchema(e,{io:"input"})}var pe={name:"rag_find",description:"Search a client/project knowledge base and return bounded citations.",inputSchema:Bt(Dt)},ge={name:"rag_read",description:"Read bounded text from a cited document chunk in the explicit namespace.",inputSchema:Bt(jt)};function $e(e,t=!1){return{content:[{type:"text",text:JSON.stringify(e)}],details:e,...t?{isError:!0}:{}}}function B(e,t){return $e({namespace:e,error:t},!0)}function Ee(e){return B(e,{code:"cancelled",message:"Tool call was cancelled."})}function zt(e,t){let n=t instanceof Error?t.message:String(t);return B(me(e),{code:"retrieval_failed",message:n})}var hr="The namespace travels on every call: never infer `client` or `project` from the cwd, repo name, config, env, or a placeholder like `default`. If the user has not stated it, ask before the first call.\nUse the RAG tool the role actually exposes; when only `rag_find` is available, delegate for deeper text instead of paraphrasing a catalog entry.\nWhen an excerpt is truncated, when the user asks whether the document covers something, or when a longer verbatim quote is needed, hand off to the subagent that exposes `rag_read` with the namespace and the citation's `docVersionId`; never assert absence from a catalog.";function vo(e){e.on("before_agent_start",n=>{if(process.env.RAG_PROMPT==="off")return;let r=n.systemPromptOptions.sections??(n.systemPromptOptions.sections={});r.rag=hr});let t=Mt(process.env.RAG_CAPABILITIES);t.has("find")&&e.registerTool({name:pe.name,label:"RAG Find",description:pe.description,parameters:pe.inputSchema,async execute(n,r,o,i,s){try{let a=Ft(r);if(!a.ok)return B(a.namespace,a.error);let{namespace:c,query:l,limit:d,excerptChars:u}=a.input;if(o?.aborted)return Ee(c);let g=xe({cwd:s.cwd,homeDir:s.cwd,argv:[],env:process.env}).root,m=Ge({root:g,namespace:c,query:l,excerptLength:u});return o?.aborted?Ee(c):"error"in m?B(c,m.error):$e({namespace:c,result:{...m.result,citations:m.result.citations.slice(0,d)}})}catch(a){return zt(r,a)}}}),t.has("read")&&e.registerTool({name:ge.name,label:"RAG Read",description:ge.description,parameters:ge.inputSchema,async execute(n,r,o,i,s){try{let a=$t(r);if(!a.ok)return B(a.namespace,a.error);let{namespace:c,docVersionId:l,chunkOrdinal:d,maxBytes:u,maxLines:g}=a.input;if(o?.aborted)return Ee(c);let m=xe({cwd:s.cwd,homeDir:s.cwd,argv:[],env:process.env}).root,h=await Pt({root:m,namespace:c,docVersionId:l,chunkOrdinal:d,maxBytes:u,maxLines:g});return o?.aborted?Ee(c):"error"in h?B(c,h.error):$e({namespace:c,result:h.result})}catch(a){return zt(r,a)}}})}export{vo as default};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import { DatabaseSync } from "node:sqlite";
|
|
3
3
|
import { clientDbPath } from "../store/db.mjs";
|
|
4
|
-
const DEFAULT_EXCERPT_LENGTH =
|
|
4
|
+
const DEFAULT_EXCERPT_LENGTH = 800;
|
|
5
5
|
const WORDS = /[\p{L}\p{M}\p{N}]+/gu;
|
|
6
6
|
function tokenizeQuery(query) {
|
|
7
7
|
return query.match(WORDS) ?? [];
|
|
@@ -23,11 +23,23 @@ function matchRange(text, queryTokens) {
|
|
|
23
23
|
return null;
|
|
24
24
|
}
|
|
25
25
|
function makeExcerpt(text, matchStart, matchEnd, limit) {
|
|
26
|
-
const excerptLength = Math.min(limit,
|
|
26
|
+
const excerptLength = Math.min(limit, text.length);
|
|
27
27
|
if (excerptLength === 0) return "";
|
|
28
28
|
const matchLength = Math.min(matchEnd - matchStart, excerptLength);
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
let start = Math.max(0, Math.min(matchStart - Math.floor((excerptLength - matchLength) / 2), text.length - excerptLength));
|
|
30
|
+
let end = start + excerptLength;
|
|
31
|
+
const unsnappedStart = start;
|
|
32
|
+
const unsnappedEnd = end;
|
|
33
|
+
const words = Array.from(text.matchAll(WORDS), (word) => ({ start: word.index, end: word.index + word[0].length }));
|
|
34
|
+
const startWord = words.find((word) => word.start < start && start < word.end);
|
|
35
|
+
if (startWord) start = words.find((word) => word.start >= startWord.end)?.start ?? text.length;
|
|
36
|
+
const endWord = words.find((word) => word.start < end && end < word.end);
|
|
37
|
+
if (endWord) {
|
|
38
|
+
const previousWords = words.filter((word) => word.end <= endWord.start);
|
|
39
|
+
end = previousWords[previousWords.length - 1]?.end ?? 0;
|
|
40
|
+
}
|
|
41
|
+
if (end <= start) return text.slice(unsnappedStart, unsnappedEnd);
|
|
42
|
+
return text.slice(start, end);
|
|
31
43
|
}
|
|
32
44
|
function compareText(left, right) {
|
|
33
45
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
@@ -76,9 +76,15 @@ async function readFromCitation(input) {
|
|
|
76
76
|
}
|
|
77
77
|
const byteLimited = limitUtf8(chunkText, maxBytes);
|
|
78
78
|
const lineLimited = limitLines(byteLimited.text, maxLines);
|
|
79
|
+
const totalChunks = extracted.sections.length;
|
|
79
80
|
return {
|
|
80
81
|
namespace,
|
|
81
|
-
result: {
|
|
82
|
+
result: {
|
|
83
|
+
text: lineLimited.text,
|
|
84
|
+
truncated: byteLimited.truncated || lineLimited.truncated,
|
|
85
|
+
totalChunks,
|
|
86
|
+
nextChunkOrdinal: chunkOrdinal + 1 < totalChunks ? chunkOrdinal + 1 : null
|
|
87
|
+
}
|
|
82
88
|
};
|
|
83
89
|
} catch (error) {
|
|
84
90
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -4,7 +4,7 @@ const MAX_EXCERPT_CHARS = 2e3;
|
|
|
4
4
|
const MAX_BYTES = 1048576;
|
|
5
5
|
const MAX_LINES = 1e4;
|
|
6
6
|
const DEFAULT_LIMIT = 10;
|
|
7
|
-
const DEFAULT_EXCERPT_CHARS =
|
|
7
|
+
const DEFAULT_EXCERPT_CHARS = 800;
|
|
8
8
|
const DEFAULT_MAX_BYTES = 65536;
|
|
9
9
|
const DEFAULT_MAX_LINES = 200;
|
|
10
10
|
function parseCapabilities(value) {
|