agent-rag-mcp 0.6.0 → 0.7.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
@@ -89,7 +89,7 @@ The extension adds a `rag` section to the agent system prompt describing the nam
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.
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. `rag_read` accepts `startLine`, the zero-based first line to return (default `0`, maximum `10,000`), and returns `returnedBytes` and `returnedLines` for the page, `chunkBytes` for the full chunk, and `truncatedBy` (`"bytes"`, `"lines"`, or `null`) alongside `truncated`. For a chunk that exceeds the line limit, ensure each page fits within `maxBytes`, then repeat calls advancing `startLine` by `returnedLines`; this covers every line without overlap or gaps, and `truncatedBy: null` marks the last page.
93
93
 
94
94
  ## Requirements
95
95
 
@@ -1,10 +1,4 @@
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 He=w(()=>{"use strict"});import*as be from"node:path";import{DatabaseSync as Ir}from"node:sqlite";function Wt(e){if(e==="."||e===".."||!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(e))throw new ye(e)}function Q(e,t){return Wt(t),be.join(e,"clients",t,"rag.db")}var ye,Se=w(()=>{"use strict";He();ye=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"}}});var Re=w(()=>{"use strict"});import{convertToHtml as rn}from"mammoth";function on(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()===Ne;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.docx$/i.test(r)}function Ye(e){let t={amp:"&",apos:"'",gt:">",lt:"<",nbsp:"\xA0",quot:'"'};return e.replace(/<br\b[^>]*>/gi,`
2
- `).replace(/<[^>]*>/g,"").replace(/&(#x[\da-f]+|#\d+|[a-z]+);/gi,(n,r)=>{if(r[0]!=="#")return t[r.toLowerCase()]??n;let o=r[1]?.toLowerCase()==="x"?Number.parseInt(r.slice(2),16):Number.parseInt(r.slice(1),10);return Number.isInteger(o)&&o>=0&&o<=1114111?String.fromCodePoint(o):n})}function sn(e){let t=[],n=/<(h([1-6])|p|li)\b[^>]*>([\s\S]*?)<\/\1\s*>/gi;for(let r of e.matchAll(n)){let o=r[2]===void 0?null:Number(r[2]);t.push({text:Ye(r[3]??""),heading:o===null?null:Ye(r[3]??""),level:o})}return t.filter(r=>r.text.length>0)}function Je(e){return e!==void 0&&/\s/u.test(e)}function an(e){let t=[],n=0;for(;e.length-n>8192;){let r=Math.min(n+8192,e.length-1);for(;r>n&&!Je(e[r]);)r-=1;if(r===n){for(r=n+8192+1;r<e.length&&!Je(e[r]);)r+=1;if(r===e.length)break}t.push(e.slice(n,r)),n=r+1}return(n<e.length||t.length===0)&&t.push(e.slice(n)),t}function We(e){let t=[],n=null,r=()=>{n!==null&&t.push(n),n=null};for(let o of e){if(o.length>8192){r();let s=an(o);t.push(...s.slice(0,-1)),n=s.at(-1)??null,n!==null&&n.length>8192&&r();continue}let i=n===null?o:`${n}
3
- ${o}`;i.length<=8192?n=i:(r(),n=o)}return r(),t}function cn(e){let t=[],n=null,r=null,o=[],i=()=>{if(o.length>0){let s=n!==null&&o.length>1?[`${o[0]}
4
- ${o[1]}`,...o.slice(2)]:o;for(let[a,c]of We(s).entries())t.push({ordinal:t.length,heading:a===0?n:null,level:a===0?r:null,text:c})}o=[]};for(let s of e)s.heading!==null?(i(),n=s.heading,r=s.level):o.length===0&&(n=null,r=null),o.push(s.text);return i(),t}async function ln(e){let t=await rn({buffer:Buffer.from(e)}),n=sn(t.value);return{sections:n.some(o=>o.heading!==null)?cn(n):We(n.map(o=>o.text)).map((o,i)=>({ordinal:i,heading:null,level:null,text:o})),versionLabel:null,mediaType:Ne}}var Ne,Ke,Ze=w(()=>{"use strict";Re();Ne="application/vnd.openxmlformats-officedocument.wordprocessingml.document";Ke={name:"docx",version:"1.13.0",mediaTypes:[Ne],supports:on,extract:ln}});var b,D=w(()=>{"use strict";b=class extends Error{code;constructor(t,n){super(n),this.name="ResourceLimitError",this.code=t}}});import{createRequire as dn}from"node:module";import Ae from"tesseract.js";function fn(e){if(typeof e=="string"){let o=e.split(";")[0]?.trim().toLowerCase();return qe.has(o??"")||Qe.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 qe.has(n.split(";")[0]?.trim().toLowerCase()??"");let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&Qe.test(r)}function mn(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 ne;if(e.byteLength>=3&&e[0]===255&&e[1]===216&&e[2]===255)return Le;throw new Error("Unsupported image data; expected PNG or JPEG bytes.")}function pn(e,t){let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(t===ne){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 gn(e){let t=mn(e),{width:n,height:r}=pn(e,t);if(n===0||r===0||n>et/r)throw new b("image_too_large",`Image exceeds MAX_IMAGE_PIXELS (${et} pixels).`);let o=await Ae.createWorker("eng",Ae.OEM.LSTM_ONLY,Oe);try{await o.setParameters({tessedit_pageseg_mode:Ae.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 ne,Le,qe,Qe,et,tt,un,Oe,re,Ie=w(()=>{"use strict";D();ne="image/png",Le="image/jpeg",qe=new Set([ne,Le]),Qe=/\.(?:png|jpe?g)$/i,et=5e7,tt=dn(import.meta.url)("@tesseract.js-data/eng"),un=tt.langPath,Oe={langPath:un,cacheMethod:"none",gzip:tt.gzip};re={name:"image-ocr",version:"7.0.0",mediaTypes:[ne,Le],supports:fn,extract:gn}});import{createCanvas as En}from"@napi-rs/canvas";import Pe from"tesseract.js";import{getDocument as hn}from"pdfjs-dist/legacy/build/pdf.mjs";function xn(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()===oe;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.pdf$/i.test(r)}async function Tn(e){let t=hn({data:Uint8Array.from(e),maxImageSize:1e7,disableFontFace:!0,useWorkerFetch:!1,verbosity:0}),n=await t.promise;try{if(n.numPages>nt)throw new b("too_many_pages",`PDF exceeds MAX_PDF_PAGES (${nt} pages).`);let r=[];for(let i=1;i<=n.numPages;i+=1){let c=(await(await n.getPage(i)).getTextContent()).items.filter(d=>"str"in d).map(d=>"str"in d?d.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:oe};if(n.numPages>100)throw new b("too_many_pages",`PDF exceeds MAX_OCR_PDF_PAGES (${100} pages) for OCR.`);let o=await Pe.createWorker("eng",Pe.OEM.LSTM_ONLY,Oe);try{await o.setParameters({tessedit_pageseg_mode:Pe.PSM.SINGLE_BLOCK});for(let i=1;i<=n.numPages;i+=1){let s=await n.getPage(i),a=s.getViewport({scale:_n}),c=Math.ceil(a.width),d=Math.ceil(a.height);if(c===0||d===0||c>5e6/d)throw new b("image_too_large",`PDF page exceeds MAX_PDF_OCR_RENDER_PIXELS (${5e6} pixels).`);let l=En(c,d),u=l.getContext("2d");await s.render({canvas:l,canvasContext:u,viewport:a}).promise;let{data:g}=await o.recognize(l.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:oe}}finally{await t.destroy()}}var oe,nt,_n,it,st=w(()=>{"use strict";Ie();Re();D();oe="application/pdf",nt=2e3,_n=2;it={name:"pdf",version:"6.4.299",mediaTypes:[oe],supports:xn,extract:Tn}});function at(e){let t=e.match(/^(.*?)[-_ ]v(\d+(?:\.\d+)*)(\.[^./\\]+)?$/);return t?{prefix:t[1].replace(/[-_ ]+$/,""),version:t[2],suffix:t[3]??null}:null}var ct=w(()=>{"use strict"});function lt(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 Sn(e){let t=lt(e,["mediaType","mimeType","contentType"]);if(t!==null)return bn.has(t.split(";")[0].trim().toLowerCase());let n=lt(e,["filename","name","path","extension"]);return n===null?!1:/(?:^|[\\/])[^\\/]+\.(?:txt|md)$/i.test(n)||/^\.(?:txt|md)$/i.test(n)}function wn(e){let t=e.split(`
5
- `),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(`
6
- `).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 Rn(e){let t=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}).decode(e),n=t.startsWith("\uFEFF")?t.slice(1):t,r=wn(n),o=r.some(s=>s.heading!==null),i=r.find(s=>s.heading!==null&&at(s.heading)!==null)?.heading??null;return{sections:r,versionLabel:i,mediaType:o?"text/markdown":n.length>0?"text/plain":null}}var bn,Ce,dt=w(()=>{"use strict";ct();bn=new Set(["text/plain","text/markdown"]);Ce={name:"plain-text",version:"1.0.0",supports:Sn,extract:Rn}});function Nn(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 Xe(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 j;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":Xe(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(Nn).map(o=>`${JSON.stringify(o)}:${Xe(n[o],t)}`).join(",")}}`}finally{t.delete(n)}}return"null"}function ie(e){return Xe(e,new Set)}var j,ut=w(()=>{"use strict";j=class extends Error{code="non_finite_number";constructor(){super("Canonical structured content contains a non-finite number."),this.name="NonFiniteNumberCanonicalizationError"}}});import{parseDocument as An}from"yaml";function ft(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 Ln(e,t){if(typeof e!="string"){let o=ft(e,["mediaType","mimeType","contentType"]);if(o!==null)return t.mediaTypes.includes(o.split(";")[0].trim().toLowerCase())}let n=typeof e=="string"?e:ft(e,["filename","name","path","extension"]);if(n===null)return!1;let r=n.toLowerCase();return t.extensions.some(o=>r.endsWith(o))}function mt(e){return typeof e=="string"?e:JSON.stringify(e,(t,n)=>typeof n=="bigint"?n.toString():n,2)??"null"}function gt(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 ke(e){try{return{canonicalContent:ie(e)}}catch(t){if(t instanceof j)return{canonicalizationError:t.code};throw t}}function On(e){let t;try{t=gt(e)}catch{throw new F("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:mt(o)})):[{ordinal:0,heading:null,level:null,text:mt(t)}],...ke(t)}}function In(e){let t=[],n=[];for(let r of e.split(/\r?\n/)){let o=r.trim();if(o.length!==0){try{n.push(gt(o))}catch{throw new F("extraction_invalid_json")}t.push({ordinal:t.length,heading:null,level:null,text:o})}}return{sections:t,...ke(n)}}function U(e,t=new Set){if(typeof e!="object"||e===null)return e;if(t.has(e))throw new F("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:ie(U(o,t));r[s]=U(i,t)}return r}if(Array.isArray(e))return e.map(r=>U(r,t));let n=Object.create(null);for(let[r,o]of Object.entries(e))n[r]=U(o,t);return n}finally{t.delete(e)}}function pt(e){return typeof e=="string"?e:JSON.stringify(e,(t,n)=>typeof n=="bigint"?n.toString():n,2)??"null"}function Pn(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=U(i),a=typeof s=="string"?s:ie(s);n.set(a,o)}return n}function Cn(e){let t,n,r;try{let i=An(e,{schema:"core",uniqueKeys:!1,intAsBigInt:!0});if(i.errors.length>0)throw new Error("invalid YAML document");t=U(i.toJS({mapAsMap:!0})),n=Pn(i)}catch(i){if(!(i instanceof j))throw new F("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:pt(t)}])}else o=[{ordinal:0,heading:null,level:null,text:pt(t)}];return{sections:o,...ke(t)}}function ve(e){return{name:e.name,version:"1.0.0",mediaTypes:e.mediaTypes,supports(t){return Ln(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=On(r);break;case"jsonl":o=In(r);break;case"yaml":o=Cn(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 F,Et,ht,Me,_t=w(()=>{"use strict";ut();F=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}};Et=ve({kind:"json",name:"json",extensions:[".json"],mediaTypes:["application/json"]}),ht=ve({kind:"jsonl",name:"jsonl",extensions:[".jsonl"],mediaTypes:["application/x-ndjson"]}),Me=ve({kind:"yaml",name:"yaml",extensions:[".yaml",".yml"],mediaTypes:["application/yaml"]})});import{PassThrough as Xn}from"node:stream";import{StringDecoder as kn}from"node:string_decoder";import vn from"jszip";function Mn(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()===De;let r=t.filename??t.name??t.path??t.extension;return typeof r=="string"&&/(?:^|[\\/])[^\\/]+\.pptx$/i.test(r)}function Dn(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 xt(e){return Number(e.match(/\/slide(\d+)\.xml$/)?.[1]??Number.MAX_SAFE_INTEGER)}function T(e){throw new b("archive_too_large",e)}function jn(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 B(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 V(e,t,n){return t>=0&&t+4<=e.byteLength&&e.getUint32(t,!0)===n}function Un(e,t){let n=B(e,t+8,t+20),r=o=>{if(!V(e,o,101075792)||o+12>t)return!1;let i=B(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 Fn(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),d=e.getUint16(s+2,!0),l=s+4,u=l+d;if((u>a||u>n)&&T("PPTX central-directory extra field is truncated."),c===1)return B(e,l,u);s=u}T("PPTX ZIP64 entry size is missing from its central-directory extra field.")}function Bn(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}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=V(t,a,117853008);(o===65535||i===4294967295||s===4294967295)&&!c&&T("PPTX ZIP64 end record is missing.");let l=o,u=i,g=s,m=r;if(c){let E=Un(t,a),R=a;l=B(t,E+32,R),u=B(t,E+40,R),g=B(t,E+48,R),m=E}l>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,H=new Map;for(;f+4<=p&&V(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),_e=t.getUint16(f+32,!0),G=f+46+E+R+_e;G>p&&T("PPTX central-directory entry is truncated."),x+=1,x>L&&T(`PPTX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`);let C=Fn(t,f,p,E,R),xe=new TextDecoder().decode(e.subarray(f+46,f+46+E));H.set(xe,C),C>O-A&&T(`PPTX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`),A+=C,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!==l)&&T("PPTX central-directory count or bounds cannot be checked safely."),H}async function $n(e,t,n){let r=[],o=new kn("utf8"),i=new Xn,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 d=Buffer.isBuffer(c)?c:Buffer.from(c);a+=d.byteLength,jn(e.name,a,n,t+a),r.push(o.write(d))}}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 zn(e){let t=Bn(e),r=(await vn.loadAsync(e)).file(/^ppt\/slides\/slide\d+\.xml$/).sort((s,a)=>xt(s.name)-xt(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 $n(s,i,a);i+=c.bytesRead;let d=[...c.xml.matchAll(/<a:t(?:\s[^>]*)?>([\s\S]*?)<\/a:t>/g)].map(l=>Dn(l[1]??"").trim()).filter(l=>l.length>0).join(" ");d.length>0&&o.push({ordinal:o.length,heading:null,level:null,text:d})}return{sections:o,versionLabel:null,mediaType:De}}var De,L,O,Tt,je=w(()=>{"use strict";D();De="application/vnd.openxmlformats-officedocument.presentationml.presentation",L=5e3,O=128*1024*1024;Tt={name:"pptx",version:"3.10.2",mediaTypes:[De],supports:Mn,extract:zn}});import{posix as yt}from"node:path";import{PassThrough as Hn}from"node:stream";import{StringDecoder as Gn}from"node:string_decoder";import Vn from"jszip";function Jn(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()===Ue;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 Nt(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<=Yn?{row:r,column:n}:null}function wt(e,t){(!Number.isSafeInteger(e)||e<1||!Number.isSafeInteger(t)||t<1)&&y("Worksheet contains an invalid row or column reference."),(e>J||e*t>ce)&&y(`Worksheet exceeds MAX_SHEET_ROWS (${J}) or MAX_SHEET_CELLS (${ce}).`)}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 Y(e,t,n){return t>=0&&t+4<=e.byteLength&&e.getUint32(t,!0)===n}function Wn(e,t){let n=$(e,t+8,t+20),r=i=>{if(!Y(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>=St&&_(`XLSX ZIP64 recovery scan exceeds MAX_ZIP64_RECOVERY_SCAN_ITERATIONS (${St} iterations).`),o+=1,r(i))return i;_("XLSX ZIP64 end record cannot be located safely.")}function Kn(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),d=e.getUint16(s+2,!0),l=s+4,u=l+d;if((u>a||u>n)&&_("XLSX central-directory extra field is truncated."),c===1)return $(e,l,u);s=u}_("XLSX ZIP64 entry size is missing from its central-directory extra field.")}function Zn(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(Y(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=Y(t,a,117853008);(o===65535||i===4294967295||s===4294967295)&&!c&&_("XLSX ZIP64 end record is missing.");let l=o,u=i,g=s,m=r;if(c){let E=Wn(t,a),R=a;l=$(t,E+32,R),u=$(t,E+40,R),g=$(t,E+48,R),m=E}l>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,H=new Map;for(;f+4<=p&&Y(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),_e=t.getUint16(f+32,!0),G=f+46+E+R+_e;G>p&&_("XLSX central-directory entry is truncated."),x+=1,x>L&&_(`XLSX exceeds MAX_PPTX_ARCHIVE_ENTRIES (${L}).`);let C=Kn(t,f,p,E,R),xe=new TextDecoder().decode(e.subarray(f+46,f+46+E));H.set(xe,C),C>O-A&&_(`XLSX exceeds MAX_PPTX_UNCOMPRESSED_BYTES (${O} bytes).`),A+=C,f=G}if(f<p&&Y(t,f,84233040)&&f+6<=p){let E=t.getUint16(f+4,!0);f+6+E===p&&(f=p)}return(f!==p||x!==l)&&_("XLSX central-directory count or bounds cannot be checked safely."),H}function qn(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 At(e,t,n,r){let o=[],i=new Gn("utf8"),s=new Hn,a=e.nodeStream("nodebuffer");a.on("error",l=>s.destroy(l)),a.pipe(s);let c=0;try{for await(let l of s){let u=Buffer.isBuffer(l)?l:Buffer.from(l);c+=u.byteLength,qn(e.name,c,t,n.bytes+c);let g=i.write(u);o.push(g),r?.(g,u.byteLength)}}catch(l){throw l instanceof Error&&l.message.includes("uncompressed data size mismatch")&&_(`XLSX entry ${e.name} decompressed size differs from its declared size (${t} bytes).`),l}let d=i.end();return o.push(d),r?.(d,0),c!==t&&_(`XLSX entry ${e.name} decompressed size differs from its declared size (${t} bytes).`),n.bytes+=c,o.join("")}function Qn(e,t,n,r){n.bytes+=t,n.bytes>bt&&y(`Worksheet XML scan exceeds MAX_SHEET_SCAN_BYTES (${bt} 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)>se&&y(`Worksheet metadata exceeds MAX_SHEET_TAG_BYTES (${se} bytes).`);break}let c=o.slice(s,a+1);if(i=a+1,Buffer.byteLength(c)>se&&y(`Worksheet metadata exceeds MAX_SHEET_TAG_BYTES (${se} bytes).`),/^<(?:[\w.-]+:)?row\b/i.test(c)){r.rowCount+=1;let d=S(c,"r");d!==void 0&&!/^\d+$/.test(d)&&y("Worksheet contains an invalid row reference.");let l=Number(d??r.rowCount);(!Number.isSafeInteger(l)||l<1)&&y("Worksheet contains an invalid row reference."),(l>J||r.rowCount>J)&&y(`Worksheet exceeds MAX_SHEET_ROWS (${J}).`),r.maxRow=Math.max(r.maxRow,l),wt(r.maxRow,r.maxColumn)}else if(/^<(?:[\w.-]+:)?c\b/i.test(c)){r.cellCount+=1,r.cellCount>ce&&y(`Worksheet exceeds MAX_SHEET_CELLS (${ce}).`);let d=S(c,"r"),l=d===void 0?null:Nt(d);d!==void 0&&!l&&y("Worksheet contains an invalid cell reference."),l&&(r.maxRow=Math.max(r.maxRow,l.row),r.maxColumn=Math.max(r.maxColumn,l.column),wt(r.maxRow,r.maxColumn))}}}function K(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 Lt(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=>K(n[1]??"")).join("")}function er(e){return[...e.matchAll(/<(?:[\w.-]+:)?si\b[^>]*\/>|<(?:[\w.-]+:)?si\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?si\s*>/gi)].map(t=>t[0].endsWith("/>")?"":Lt(t[1]??""))}function tr(e){let t;try{t=decodeURIComponent(K(e))}catch(o){if(o instanceof b)throw o;return null}let n=t.split(/[?#]/,1)[0]??"",r=yt.normalize(n.startsWith("/")?n.slice(1):yt.join("xl",n));return r.startsWith("xl/")?r:null}function nr(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?tr(i):null;o&&s&&t.set(o,s)}return t}function rr(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:K(i),part:a})}return n}function or(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 ir(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,K(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}:or(t.get(o)??"")})}function sr(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 ar(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 Lt(u).trim()}let s=e.match(/<(?:[\w.-]+:)?v\b[^>]*>([\s\S]*?)<\/(?:[\w.-]+:)?v\s*>/i)?.[1];if(s===void 0)return"";let a=K(s).trim(),c=S(o,"s"),d=c===void 0?0:Number(c);(!Number.isSafeInteger(d)||d<0||c!==void 0&&n[d]===void 0)&&y("Worksheet contains an invalid cell style reference.");let l=n[d];return l?.date?sr(a,l.dateTime):a}function cr(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),d=1;for(let l of(s[1]??"").matchAll(/<(?:[\w.-]+:)?c\b[^>]*?(?:\/>|>([\s\S]*?)<\/(?:[\w.-]+:)?c\s*>)/gi)){let u=l[0],g=u.slice(0,u.indexOf(">")+1),m=Nt(S(g,"r")??""),h=m?.column??d,f=m?.row??c;if(d=h+1,!Number.isSafeInteger(f)||f<1||!Number.isSafeInteger(h)||h<1)continue;let p=r.get(f)??new Map;p.set(h,ar(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 l of a.keys())c=Math.max(c,l);let d=Array.from({length:c},(l,u)=>a.get(u+1)??"");for(;d.length>0&&d[d.length-1]==="";)d.pop();d.some(l=>l.length>0)&&i.push(d.join(" "))}return i.join(`
7
- `)}function W(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 lr(e){return/<(?:[\w.-]+:)?c\b[^>]*\bt\s*=\s*["']s["']/i.test(e)}async function ae(e,t,n,r){let o=W(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.`),At(o,i,r)}async function Rt(e,t,n,r){let o=W(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 At(o,i,r,(c,d)=>{Qn(c,d,s,a)})}async function dr(e){try{let t=Zn(e),n=await Vn.loadAsync(e),r={bytes:0};if(!W(n,"xl/workbook.xml")){for(let[h,f]of Object.entries(n.files))!f.dir&&/^xl\/worksheets\/[^/]+\.xml$/i.test(h)&&await Rt(n,h,t,r);y("XLSX package is missing xl/workbook.xml.")}let o=await ae(n,"xl/workbook.xml",t,r),i=await ae(n,"xl/_rels/workbook.xml.rels",t,r),s=rr(o,nr(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 Rt(n,h.part,t,r);p!==null&&a.set(f,p)}let c=W(n,"xl/sharedStrings.xml"),d=[...a.values()].some(lr);!c&&d&&_("XLSX worksheet references shared strings, but the shared-strings part is missing.");let l=c?er(await ae(n,c.name,t,r)):[],u=W(n,"xl/styles.xml"),g=u?ir(await ae(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=cr(f,l,g);p.trim().length!==0&&m.push({ordinal:m.length,heading:h.name,level:1,text:p})}return{sections:m,versionLabel:null,mediaType:Ue}}catch(t){if(t instanceof b)throw t;_("XLSX package could not be parsed safely.")}}var Ue,J,ce,se,bt,Yn,St,Ot,It=w(()=>{"use strict";je();D();Ue="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",J=1e5,ce=1e6,se=64*1024,bt=16*1024*1024,Yn=16384,St=1e6;Ot={name:"xlsx",version:"4.4.0",mediaTypes:[Ue],supports:Jn,extract:dr}});import*as Be from"node:path";function Pt(e){let t=Be.extname(e).toLowerCase(),n=ur.get(t);if(!n||!n.supports({filename:e}))throw new Fe(e);return n}var Fe,ur,Ct=w(()=>{"use strict";Ze();Ie();st();dt();_t();je();It();Fe=class extends Error{code="unsupported_media_type";extension;constructor(t){let n=Be.extname(t).toLowerCase()||"(none)";super(`Unsupported media type for extension "${n}".`),this.name="UnsupportedMediaTypeError",this.extension=n}},ur=new Map([[".pdf",it],[".docx",Ke],[".png",re],[".jpg",re],[".jpeg",re],[".xlsx",Ot],[".pptx",Tt],[".txt",Ce],[".md",Ce],[".yaml",Me],[".yml",Me],[".json",Et],[".jsonl",ht]])});import*as N from"node:fs/promises";import*as le from"node:path";function mr(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 le.join("objects",e.slice(0,2),e.slice(2,4),e)}async function Xt(e,t){return N.readFile(le.join(e,mr(t)))}var Ao,Lo,fr,Oo,kt=w(()=>{"use strict";D();Ao=64*1024,Lo=1440*60*1e3,fr=["EPERM","ENOTSUP","EXDEV","EACCES"],Oo=new Set(fr)});import*as v from"node:fs";import*as X from"node:path";var q=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 Jt(e){let t;try{t=v.readFileSync(e,"utf8")}catch(n){throw new q(e,"config_unreadable",n)}try{return JSON.parse(t)}catch(n){throw new q(e,"config_invalid",n)}}function Te({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=X.join(e,"rag.config.json"),a=X.join(X.dirname(e),"rag.config.json"),c=X.join(t,".rag","config.json"),d=v.existsSync(s)?s:v.existsSync(a)?a:v.existsSync(c)?c:null,l=d===null?{}:Jt(d),u=typeof l.client=="string"?l.client:void 0,g=typeof l.project=="string"?l.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?d===s?"cwd":d===a?"parent":"home":"unscoped";return{namespace:f,source:p,configPath:p==="cwd"||p==="parent"||p==="home"?d:null,failClosed:typeof l.failClosed=="boolean"?l.failClosed:!0,root:o.root??i.root??(typeof l.root=="string"?l.root:X.join(t,".rag"))}}Se();import*as te from"node:fs";import{DatabaseSync as Kt}from"node:sqlite";var Zt=800,we=/[\p{L}\p{M}\p{N}]+/gu;function qt(e){return e.match(we)??[]}function Qt(e){return e.map(t=>`"${t.replaceAll('"','""')}"`).join(" AND ")}function Ge(e){return e.normalize("NFD").replace(new RegExp("\\p{M}","gu"),"").toLowerCase()}function en(e,t){let n=new Set(t.map(Ge));for(let r of e.matchAll(we)){let o=r[0];if(!n.has(Ge(o)))continue;let i=r.index;return{start:i,end:i+o.length}}return null}function tn(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,d=a,l=Array.from(e.matchAll(we),m=>({start:m.index,end:m.index+m[0].length})),u=l.find(m=>m.start<s&&s<m.end);u&&(s=l.find(m=>m.start>=u.end)?.start??e.length);let g=l.find(m=>m.start<a&&a<m.end);if(g){let m=l.filter(h=>h.end<=g.start);a=m[m.length-1]?.end??0}return a<=s?e.slice(c,d):e.slice(s,a)}function nn(e,t){return e<t?-1:e>t?1:0}function ee(e,t,n){return{namespace:e,error:{code:t,message:n}}}function Ve(e){let{root:t,namespace:n,query:r}=e,o=e.excerptLength??Zt;if(!Number.isInteger(o)||o<1)return ee(n,"invalid_excerpt_length","excerptLength must be a positive integer.");let i=qt(r);if(i.length===0)return{namespace:n,result:{citations:[],total:0}};let s;try{let a=Q(t,n.client);if(!te.existsSync(a)||!te.statSync(a).isFile())return ee(n,"database_not_found",`No database exists for namespace ${n.client}/${n.project}.`);s=new Kt(a,{readOnly:!0});let c=s.prepare(`SELECT dv.doc_version_id AS docVersionId, d.document_id AS documentId,
1
+ import*as h from"node:fs";import*as N from"node:path";var I=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 ie(e){let t;try{t=h.readFileSync(e,"utf8")}catch(n){throw new I(e,"config_unreadable",n)}try{return JSON.parse(t)}catch(n){throw new I(e,"config_invalid",n)}}function D({cwd:e,homeDir:t,argv:n,env:r}){let o={};for(let f=0;f<n.length;f+=1){let F=/^--(client|project|root)=(.*)$/.exec(n[f]);F?o[F[1]]=F[2]:/^--(client|project|root)$/.test(n[f])&&n[f+1]!==void 0&&(o[n[f].slice(2)]=n[f+1],f+=1)}let c={client:r.RAG_CLIENT,project:r.RAG_PROJECT,root:r.RAG_ROOT},d=N.join(e,"rag.config.json"),a=N.join(N.dirname(e),"rag.config.json"),u=N.join(t,".rag","config.json"),l=h.existsSync(d)?d:h.existsSync(a)?a:h.existsSync(u)?u:null,i=l===null?{}:ie(l),p=typeof i.client=="string"?i.client:void 0,E=typeof i.project=="string"?i.project:void 0,s=o.client??c.client??p??null,m=o.project??c.project??E??null,T=s===null&&m===null?null:{client:s,project:m},y=o.client!==void 0||o.project!==void 0?"args":c.client!==void 0||c.project!==void 0?"env":p!==void 0||E!==void 0?l===d?"cwd":l===a?"parent":"home":"unscoped";return{namespace:T,source:y,configPath:y==="cwd"||y==="parent"||y==="home"?l:null,failClosed:typeof i.failClosed=="boolean"?i.failClosed:!0,root:o.root??c.root??(typeof i.root=="string"?i.root:N.join(t,".rag"))}}import*as O from"node:fs";import{DatabaseSync as ae}from"node:sqlite";import*as B from"node:path";import{DatabaseSync as xe}from"node:sqlite";var M=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"}};function oe(e){if(e==="."||e===".."||!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(e))throw new M(e)}function v(e,t){return oe(t),B.join(e,"clients",t,"rag.db")}var se=800,P=/[\p{L}\p{M}\p{N}]+/gu;function ce(e){return e.match(P)??[]}function de(e){return e.map(t=>`"${t.replaceAll('"','""')}"`).join(" AND ")}function K(e){return e.normalize("NFD").replace(new RegExp("\\p{M}","gu"),"").toLowerCase()}function ue(e,t){let n=new Set(t.map(K));for(let r of e.matchAll(P)){let o=r[0];if(!n.has(K(o)))continue;let c=r.index;return{start:c,end:c+o.length}}return null}function le(e,t,n,r){let o=Math.min(r,e.length);if(o===0)return"";let c=Math.min(n-t,o),d=Math.max(0,Math.min(t-Math.floor((o-c)/2),e.length-o)),a=d+o,u=d,l=a,i=Array.from(e.matchAll(P),s=>({start:s.index,end:s.index+s[0].length})),p=i.find(s=>s.start<d&&d<s.end);p&&(d=i.find(s=>s.start>=p.end)?.start??e.length);let E=i.find(s=>s.start<a&&a<s.end);if(E){let s=i.filter(m=>m.end<=E.start);a=s[s.length-1]?.end??0}return a<=d?e.slice(u,l):e.slice(d,a)}function pe(e,t){return e<t?-1:e>t?1:0}function b(e,t,n){return{namespace:e,error:{code:t,message:n}}}function z(e){let{root:t,namespace:n,query:r}=e,o=e.excerptLength??se;if(!Number.isInteger(o)||o<1)return b(n,"invalid_excerpt_length","excerptLength must be a positive integer.");let c=ce(r);if(c.length===0)return{namespace:n,result:{citations:[],total:0}};let d;try{let a=v(t,n.client);if(!O.existsSync(a)||!O.statSync(a).isFile())return b(n,"database_not_found",`No database exists for namespace ${n.client}/${n.project}.`);d=new ae(a,{readOnly:!0});let u=d.prepare(`SELECT dv.doc_version_id AS docVersionId, d.document_id AS documentId,
8
2
  e.entity_key AS entityKey, d.label AS label, dv.version_label AS versionLabel,
9
3
  s.ordinal AS sectionOrdinal, c.ordinal AS chunkOrdinal, c.text AS text,
10
4
  c.char_start AS charStart, c.char_end AS charEnd, bm25(chunk_fts) AS score
@@ -18,13 +12,17 @@ ${o[1]}`,...o.slice(2)]:o;for(let[a,c]of We(s).entries())t.push({ordinal:t.lengt
18
12
  LEFT JOIN section AS s ON s.section_id = c.section_id
19
13
  WHERE chunk_fts MATCH ? AND cl.name = ? AND p.slug = ?
20
14
  AND d.deleted_at IS NULL AND e.deleted_at IS NULL
21
- ORDER BY score ASC, dv.doc_version_id COLLATE BINARY ASC, c.ordinal ASC`).all(Qt(i),n.client,n.project),d=[];for(let l of c){let u=en(l.text,i);if(!u)return ee(n,"match_offsets_unavailable","A matched FTS token could not be located in its stored chunk text.");d.push({docVersionId:l.docVersionId,documentId:l.documentId,entityKey:l.entityKey,label:l.label,versionLabel:l.versionLabel,sectionOrdinal:l.sectionOrdinal,chunkOrdinal:l.chunkOrdinal,charStart:l.charStart,charEnd:l.charEnd,matchStart:u.start,matchEnd:u.end,score:l.score,excerpt:tn(l.text,u.start,u.end,o)})}return d.sort((l,u)=>l.score-u.score||nn(l.docVersionId,u.docVersionId)||l.chunkOrdinal-u.chunkOrdinal),{namespace:n,result:{citations:d,total:d.length}}}catch(a){let c=a instanceof Error?a.message:String(a);return ee(n,"database_unavailable",c)}finally{s?.close()}}Ct();Se();kt();import*as de from"node:fs";import*as Z from"node:path";import{DatabaseSync as pr}from"node:sqlite";function I(e,t,n){return{namespace:e,error:{code:t,message:n}}}function gr(e,t){let n=Buffer.from(e,"utf8");if(n.byteLength<=t)return{text:e,truncated:!1};let r=t;for(;r>0;)try{return{text:new TextDecoder("utf-8",{fatal:!0}).decode(n.subarray(0,r)),truncated:!0}}catch{r-=1}return{text:"",truncated:!0}}function Er(e,t){if(e.length===0)return{text:e,truncated:!1};if(t===0)return{text:"",truncated:!0};let n=1;for(let r=0;r<e.length;r+=1){if(e[r]!=="\r"&&e[r]!==`
15
+ ORDER BY score ASC, dv.doc_version_id COLLATE BINARY ASC, c.ordinal ASC`).all(de(c),n.client,n.project),l=[];for(let i of u){let p=ue(i.text,c);if(!p)return b(n,"match_offsets_unavailable","A matched FTS token could not be located in its stored chunk text.");l.push({docVersionId:i.docVersionId,documentId:i.documentId,entityKey:i.entityKey,label:i.label,versionLabel:i.versionLabel,sectionOrdinal:i.sectionOrdinal,chunkOrdinal:i.chunkOrdinal,charStart:i.charStart,charEnd:i.charEnd,matchStart:p.start,matchEnd:p.end,score:i.score,excerpt:le(i.text,p.start,p.end,o)})}return l.sort((i,p)=>i.score-p.score||pe(i.docVersionId,p.docVersionId)||i.chunkOrdinal-p.chunkOrdinal),{namespace:n,result:{citations:l,total:l.length}}}catch(a){let u=a instanceof Error?a.message:String(a);return b(n,"database_unavailable",u)}finally{d?.close()}}import*as x from"node:fs";import{DatabaseSync as Ee}from"node:sqlite";function C(e,t,n){return{namespace:e,error:{code:t,message:n}}}function me(e,t){let n=Buffer.from(e,"utf8");if(n.byteLength<=t)return{text:e,truncated:!1};let r=t;for(;r>0;)try{return{text:new TextDecoder("utf-8",{fatal:!0}).decode(n.subarray(0,r)),truncated:!0}}catch{r-=1}return{text:"",truncated:!0}}function _e(e,t){if(t===0)return 0;let n=0;for(let r=0;r<e.length;r+=1)if(!(e[r]!=="\r"&&e[r]!==`
16
+ `)&&(e[r]==="\r"&&e[r+1]===`
17
+ `&&(r+=1),n+=1,n===t))return r+1;return e.length}function Te(e){if(e.length===0)return 0;let t=1;for(let n=0;n<e.length;n+=1)e[n]==="\r"&&e[n+1]===`
18
+ `?(t+=1,n+=1):(e[n]==="\r"||e[n]===`
19
+ `)&&(t+=1);return t}function fe(e,t){if(e.length===0)return{text:e,truncated:!1};if(t===0)return{text:"",truncated:!0};let n=1;for(let r=0;r<e.length;r+=1){if(e[r]!=="\r"&&e[r]!==`
22
20
  `)continue;let o=r;if(e[r]==="\r"&&e[r+1]===`
23
- `&&(r+=1),n===t)return{text:e.slice(0,o),truncated:!0};n+=1}return{text:e,truncated:!1}}async function vt(e){let{root:t,namespace:n,docVersionId:r,chunkOrdinal:o,maxBytes:i,maxLines:s}=e;if(!Number.isInteger(i)||i<0||!Number.isInteger(s)||s<0)return I(n,"invalid_read_limit","maxBytes and maxLines must be non-negative integers.");let a;try{let c=Q(t,n.client);if(!de.existsSync(c)||!de.statSync(c).isFile())return I(n,"database_not_found",`No database exists for namespace ${n.client}/${n.project}.`);a=new pr(c,{readOnly:!0});let d=a.prepare(`SELECT o.sha256 AS sha256, o.rel_path AS relPath, d.label AS label
21
+ `&&(r+=1),n===t)return{text:e.slice(0,o),truncated:!0};n+=1}return{text:e,truncated:!1}}async function V(e){let{root:t,namespace:n,docVersionId:r,chunkOrdinal:o,maxBytes:c,maxLines:d,startLine:a=0}=e;if(!Number.isInteger(c)||c<0||!Number.isInteger(d)||d<0||!Number.isInteger(a)||a<0)return C(n,"invalid_read_limit","maxBytes, maxLines, and startLine must be non-negative integers.");let u;try{let l=v(t,n.client);if(!x.existsSync(l)||!x.statSync(l).isFile())return C(n,"database_not_found",`No database exists for namespace ${n.client}/${n.project}.`);u=new Ee(l,{readOnly:!0});let i=u.prepare(`SELECT c.text AS text,
22
+ (SELECT COUNT(*) FROM chunk AS total_chunks WHERE total_chunks.doc_version_id = c.doc_version_id) AS totalChunks
24
23
  FROM chunk AS c
25
24
  JOIN doc_version AS dv ON dv.doc_version_id = c.doc_version_id
26
25
  JOIN document AS d ON d.document_id = dv.document_id
27
26
  JOIN project AS p ON p.project_id = d.project_id
28
27
  JOIN client AS cl ON cl.client_id = p.client_id
29
- LEFT JOIN object AS o ON o.sha256 = dv.sha256
30
- WHERE c.doc_version_id = ? AND c.ordinal = ? AND cl.name = ? AND p.slug = ?`).get(r,o,n.client,n.project);if(!d)return I(n,"citation_not_found","The citation does not exist in the requested document version and namespace.");if(!d.sha256||!d.relPath)return I(n,"object_missing","The citation's recorded object is missing.");let l;try{let p=Z.resolve(t,d.relPath),x=Z.resolve(t);if(p!==x&&!p.startsWith(`${x}${Z.sep}`))return I(n,"object_path_invalid","The recorded object path escapes the configured root.");l=await Xt(t,d.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 Pt(d.label).extract(l),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=gr(g,i),h=Er(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 d=c instanceof Error?c.message:String(c);return I(n,"object_read_failed",d)}finally{a?.close()}}import{z as k}from"zod";var Mt=100,Dt=2e3,jt=1048576,Ut=1e4,hr=10,_r=800,xr=65536,Tr=200;function Ft(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 fe=k.string().min(1).refine(e=>e.trim().length>0),$e=k.object({client:fe,project:fe}).strict();function me(e,t,n){return k.number().int().min(e).max(t).optional().transform(r=>r??n)}var Bt=k.object({namespace:$e,query:fe,limit:me(1,Mt,hr),excerptChars:me(1,Dt,_r)}).strict(),$t=k.object({namespace:$e,docVersionId:fe,chunkOrdinal:k.number().multipleOf(1).min(0).refine(Number.isSafeInteger),maxBytes:me(0,jt,xr),maxLines:me(0,Ut,Tr)}).strict();function yr(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 ue(e,t,n){return{code:"invalid_limit",message:`${e} must be an integer from ${t} to ${n}.`}}function zt(e,t){let n=yr(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 ue("limit",1,Mt);if(P(e,"excerptChars"))return ue("excerptChars",1,Dt)}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 ue("maxBytes",0,jt);if(P(e,"maxLines"))return ue("maxLines",0,Ut)}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 br(e){let t=$e.safeParse(e);return t.success?t.data:null}function Ht(e){let t=Bt.safeParse(e);return t.success?{ok:!0,input:t.data,namespace:t.data.namespace}:{ok:!1,namespace:pe(e),error:zt(t.error.issues,"find")}}function Gt(e){let t=$t.safeParse(e);return t.success?{ok:!0,input:t.data,namespace:t.data.namespace}:{ok:!1,namespace:pe(e),error:zt(t.error.issues,"read")}}function Sr(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function pe(e){return Sr(e)?br(e.namespace):null}function Vt(e){return k.toJSONSchema(e,{io:"input"})}var ge={name:"rag_find",description:"Search a client/project knowledge base and return bounded citations.",inputSchema:Vt(Bt)},Ee={name:"rag_read",description:"Read bounded text from a cited document chunk in the explicit namespace.",inputSchema:Vt($t)};function ze(e,t=!1){return{content:[{type:"text",text:JSON.stringify(e)}],details:e,...t?{isError:!0}:{}}}function z(e,t){return ze({namespace:e,error:t},!0)}function he(e){return z(e,{code:"cancelled",message:"Tool call was cancelled."})}function Yt(e,t){let n=t instanceof Error?t.message:String(t);return z(pe(e),{code:"retrieval_failed",message:n})}var wr="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 $o(e){e.on("before_agent_start",n=>{if(process.env.RAG_PROMPT==="off")return;let r=n.systemPromptOptions.sections??(n.systemPromptOptions.sections={});r.rag=wr});let t=Ft(process.env.RAG_CAPABILITIES);t.has("find")&&e.registerTool({name:ge.name,label:"RAG Find",description:ge.description,parameters:ge.inputSchema,async execute(n,r,o,i,s){try{let a=Ht(r);if(!a.ok)return z(a.namespace,a.error);let{namespace:c,query:d,limit:l,excerptChars:u}=a.input;if(o?.aborted)return he(c);let g=Te({cwd:s.cwd,homeDir:s.cwd,argv:[],env:process.env}).root,m=Ve({root:g,namespace:c,query:d,excerptLength:u});return o?.aborted?he(c):"error"in m?z(c,m.error):ze({namespace:c,result:{...m.result,citations:m.result.citations.slice(0,l)}})}catch(a){return Yt(r,a)}}}),t.has("read")&&e.registerTool({name:Ee.name,label:"RAG Read",description:Ee.description,parameters:Ee.inputSchema,async execute(n,r,o,i,s){try{let a=Gt(r);if(!a.ok)return z(a.namespace,a.error);let{namespace:c,docVersionId:d,chunkOrdinal:l,maxBytes:u,maxLines:g}=a.input;if(o?.aborted)return he(c);let m=Te({cwd:s.cwd,homeDir:s.cwd,argv:[],env:process.env}).root,h=await vt({root:m,namespace:c,docVersionId:d,chunkOrdinal:l,maxBytes:u,maxLines:g});return o?.aborted?he(c):"error"in h?z(c,h.error):ze({namespace:c,result:h.result})}catch(a){return Yt(r,a)}}})}export{$o as default};
28
+ WHERE c.doc_version_id = ? AND c.ordinal = ? AND cl.name = ? AND p.slug = ?`).get(r,o,n.client,n.project);if(!i)return C(n,"citation_not_found","The citation does not exist in the requested document version and namespace.");let p=i.text.slice(_e(i.text,a)),E=me(p,c),s=fe(E.text,d),m=E.truncated?"bytes":s.truncated?"lines":null,T=i.totalChunks;return{namespace:n,result:{text:s.text,truncated:m!==null,totalChunks:T,nextChunkOrdinal:o+1<T?o+1:null,chunkBytes:Buffer.byteLength(i.text,"utf8"),returnedBytes:Buffer.byteLength(s.text,"utf8"),returnedLines:Te(s.text),truncatedBy:m}}}catch(l){let i=l instanceof Error?l.message:String(l);return C(n,"object_read_failed",i)}finally{u?.close()}}import{z as g}from"zod";var $=100,q=2e3,H=1048576,S=1e4,Ne=10,ge=800,he=65536,Re=200;function J(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 k=g.string().min(1).refine(e=>e.trim().length>0),G=g.object({client:k,project:k}).strict();function A(e,t,n){return g.number().int().min(e).max(t).optional().transform(r=>r??n)}var Q=g.object({namespace:G,query:k,limit:A(1,$,Ne),excerptChars:A(1,q,ge)}).strict(),W=g.object({namespace:G,docVersionId:k,chunkOrdinal:g.number().multipleOf(1).min(0).refine(Number.isSafeInteger),maxBytes:A(0,H,he),maxLines:A(0,S,Re),startLine:A(0,S,0)}).strict();function Le(e){let t=e.find(c=>c.code==="unrecognized_keys"&&c.path.length===0)??e.find(c=>c.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 _(e,t){return e.some(n=>n.path[0]===t)}function L(e,t,n){return{code:"invalid_limit",message:`${e} must be an integer from ${t} to ${n}.`}}function Z(e,t){let n=Le(e);if(n)return n;if(_(e,"namespace"))return{code:"namespace_required",message:"namespace must contain non-empty client and project strings."};if(t==="find"){if(_(e,"query"))return{code:"invalid_query",message:"query must be a non-empty string."};if(_(e,"limit"))return L("limit",1,$);if(_(e,"excerptChars"))return L("excerptChars",1,q)}else{if(_(e,"docVersionId"))return{code:"invalid_input",message:"docVersionId must be a non-empty string."};if(_(e,"chunkOrdinal"))return{code:"invalid_input",message:"chunkOrdinal must be a non-negative integer."};if(_(e,"maxBytes"))return L("maxBytes",0,H);if(_(e,"maxLines"))return L("maxLines",0,S);if(_(e,"startLine"))return L("startLine",0,S)}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 Ae(e){let t=G.safeParse(e);return t.success?t.data:null}function ee(e){let t=Q.safeParse(e);return t.success?{ok:!0,input:t.data,namespace:t.data.namespace}:{ok:!1,namespace:U(e),error:Z(t.error.issues,"find")}}function ne(e){let t=W.safeParse(e);return t.success?{ok:!0,input:t.data,namespace:t.data.namespace}:{ok:!1,namespace:U(e),error:Z(t.error.issues,"read")}}function ye(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function U(e){return ye(e)?Ae(e.namespace):null}function te(e){return g.toJSONSchema(e,{io:"input"})}var X={name:"rag_find",description:"Search a client/project knowledge base and return bounded citations.",inputSchema:te(Q)},j={name:"rag_read",description:"Read bounded text from a cited document chunk in the explicit namespace.",inputSchema:te(W)};function Y(e,t=!1){return{content:[{type:"text",text:JSON.stringify(e)}],details:e,...t?{isError:!0}:{}}}function R(e,t){return Y({namespace:e,error:t},!0)}function w(e){return R(e,{code:"cancelled",message:"Tool call was cancelled."})}function re(e,t){let n=t instanceof Error?t.message:String(t);return R(U(e),{code:"retrieval_failed",message:n})}var Ie="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 ze(e){e.on("before_agent_start",n=>{if(process.env.RAG_PROMPT==="off")return;let r=n.systemPromptOptions.sections??(n.systemPromptOptions.sections={});r.rag=Ie});let t=J(process.env.RAG_CAPABILITIES);t.has("find")&&e.registerTool({name:X.name,label:"RAG Find",description:X.description,parameters:X.inputSchema,async execute(n,r,o,c,d){try{let a=ee(r);if(!a.ok)return R(a.namespace,a.error);let{namespace:u,query:l,limit:i,excerptChars:p}=a.input;if(o?.aborted)return w(u);let E=D({cwd:d.cwd,homeDir:d.cwd,argv:[],env:process.env}).root,s=z({root:E,namespace:u,query:l,excerptLength:p});return o?.aborted?w(u):"error"in s?R(u,s.error):Y({namespace:u,result:{...s.result,citations:s.result.citations.slice(0,i)}})}catch(a){return re(r,a)}}}),t.has("read")&&e.registerTool({name:j.name,label:"RAG Read",description:j.description,parameters:j.inputSchema,async execute(n,r,o,c,d){try{let a=ne(r);if(!a.ok)return R(a.namespace,a.error);let{namespace:u,docVersionId:l,chunkOrdinal:i,maxBytes:p,maxLines:E,startLine:s}=a.input;if(o?.aborted)return w(u);let m=D({cwd:d.cwd,homeDir:d.cwd,argv:[],env:process.env}).root,T=await V({root:m,namespace:u,docVersionId:l,chunkOrdinal:i,maxBytes:p,maxLines:E,startLine:s});return o?.aborted?w(u):"error"in T?R(u,T.error):Y({namespace:u,result:T.result})}catch(a){return re(r,a)}}})}export{ze as default};
@@ -1,9 +1,6 @@
1
1
  import * as fs from "node:fs";
2
- import * as path from "node:path";
3
2
  import { DatabaseSync } from "node:sqlite";
4
- import { getExtractorForPath } from "../adapters/registry.mjs";
5
3
  import { clientDbPath } from "../store/db.mjs";
6
- import { readObject } from "../store/objects.mjs";
7
4
  function failure(namespace, code, message) {
8
5
  return { namespace, error: { code, message } };
9
6
  }
@@ -20,6 +17,30 @@ function limitUtf8(text, maxBytes) {
20
17
  }
21
18
  return { text: "", truncated: true };
22
19
  }
20
+ function startLineOffset(text, startLine) {
21
+ if (startLine === 0) return 0;
22
+ let line = 0;
23
+ for (let index = 0; index < text.length; index += 1) {
24
+ if (text[index] !== "\r" && text[index] !== "\n") continue;
25
+ if (text[index] === "\r" && text[index + 1] === "\n") index += 1;
26
+ line += 1;
27
+ if (line === startLine) return index + 1;
28
+ }
29
+ return text.length;
30
+ }
31
+ function countLines(text) {
32
+ if (text.length === 0) return 0;
33
+ let lines = 1;
34
+ for (let index = 0; index < text.length; index += 1) {
35
+ if (text[index] === "\r" && text[index + 1] === "\n") {
36
+ lines += 1;
37
+ index += 1;
38
+ } else if (text[index] === "\r" || text[index] === "\n") {
39
+ lines += 1;
40
+ }
41
+ }
42
+ return lines;
43
+ }
23
44
  function limitLines(text, maxLines) {
24
45
  if (text.length === 0) return { text, truncated: false };
25
46
  if (maxLines === 0) return { text: "", truncated: true };
@@ -34,9 +55,9 @@ function limitLines(text, maxLines) {
34
55
  return { text, truncated: false };
35
56
  }
36
57
  async function readFromCitation(input) {
37
- const { root, namespace, docVersionId, chunkOrdinal, maxBytes, maxLines } = input;
38
- if (!Number.isInteger(maxBytes) || maxBytes < 0 || !Number.isInteger(maxLines) || maxLines < 0) {
39
- return failure(namespace, "invalid_read_limit", "maxBytes and maxLines must be non-negative integers.");
58
+ const { root, namespace, docVersionId, chunkOrdinal, maxBytes, maxLines, startLine = 0 } = input;
59
+ if (!Number.isInteger(maxBytes) || maxBytes < 0 || !Number.isInteger(maxLines) || maxLines < 0 || !Number.isInteger(startLine) || startLine < 0) {
60
+ return failure(namespace, "invalid_read_limit", "maxBytes, maxLines, and startLine must be non-negative integers.");
40
61
  }
41
62
  let db;
42
63
  try {
@@ -45,45 +66,31 @@ async function readFromCitation(input) {
45
66
  return failure(namespace, "database_not_found", `No database exists for namespace ${namespace.client}/${namespace.project}.`);
46
67
  }
47
68
  db = new DatabaseSync(dbPath, { readOnly: true });
48
- const citation = db.prepare(`SELECT o.sha256 AS sha256, o.rel_path AS relPath, d.label AS label
69
+ const citation = db.prepare(`SELECT c.text AS text,
70
+ (SELECT COUNT(*) FROM chunk AS total_chunks WHERE total_chunks.doc_version_id = c.doc_version_id) AS totalChunks
49
71
  FROM chunk AS c
50
72
  JOIN doc_version AS dv ON dv.doc_version_id = c.doc_version_id
51
73
  JOIN document AS d ON d.document_id = dv.document_id
52
74
  JOIN project AS p ON p.project_id = d.project_id
53
75
  JOIN client AS cl ON cl.client_id = p.client_id
54
- LEFT JOIN object AS o ON o.sha256 = dv.sha256
55
76
  WHERE c.doc_version_id = ? AND c.ordinal = ? AND cl.name = ? AND p.slug = ?`).get(docVersionId, chunkOrdinal, namespace.client, namespace.project);
56
77
  if (!citation) return failure(namespace, "citation_not_found", "The citation does not exist in the requested document version and namespace.");
57
- if (!citation.sha256 || !citation.relPath) return failure(namespace, "object_missing", "The citation's recorded object is missing.");
58
- let bytes;
59
- try {
60
- const objectPath = path.resolve(root, citation.relPath);
61
- const rootPath = path.resolve(root);
62
- if (objectPath !== rootPath && !objectPath.startsWith(`${rootPath}${path.sep}`)) {
63
- return failure(namespace, "object_path_invalid", "The recorded object path escapes the configured root.");
64
- }
65
- bytes = await readObject(root, citation.sha256);
66
- } catch (error) {
67
- if (error.code === "ENOENT") {
68
- return failure(namespace, "object_missing", "The citation's recorded object file is missing.");
69
- }
70
- throw error;
71
- }
72
- const extracted = await getExtractorForPath(citation.label).extract(bytes);
73
- const chunkText = extracted.sections[chunkOrdinal]?.text;
74
- if (chunkText === void 0) {
75
- return failure(namespace, "citation_not_found", "The stored object does not contain the requested extracted section.");
76
- }
78
+ const chunkText = citation.text.slice(startLineOffset(citation.text, startLine));
77
79
  const byteLimited = limitUtf8(chunkText, maxBytes);
78
80
  const lineLimited = limitLines(byteLimited.text, maxLines);
79
- const totalChunks = extracted.sections.length;
81
+ const truncatedBy = byteLimited.truncated ? "bytes" : lineLimited.truncated ? "lines" : null;
82
+ const totalChunks = citation.totalChunks;
80
83
  return {
81
84
  namespace,
82
85
  result: {
83
86
  text: lineLimited.text,
84
- truncated: byteLimited.truncated || lineLimited.truncated,
87
+ truncated: truncatedBy !== null,
85
88
  totalChunks,
86
- nextChunkOrdinal: chunkOrdinal + 1 < totalChunks ? chunkOrdinal + 1 : null
89
+ nextChunkOrdinal: chunkOrdinal + 1 < totalChunks ? chunkOrdinal + 1 : null,
90
+ chunkBytes: Buffer.byteLength(citation.text, "utf8"),
91
+ returnedBytes: Buffer.byteLength(lineLimited.text, "utf8"),
92
+ returnedLines: countLines(lineLimited.text),
93
+ truncatedBy
87
94
  }
88
95
  };
89
96
  } catch (error) {
@@ -49,8 +49,8 @@ async function main() {
49
49
  }
50
50
  const check = validateRagReadInput(args);
51
51
  if (!check.ok) return toolError(check.namespace, check.error);
52
- const { namespace, docVersionId, chunkOrdinal, maxBytes, maxLines } = check.input;
53
- const read = await readFromCitation({ root, namespace, docVersionId, chunkOrdinal, maxBytes, maxLines });
52
+ const { namespace, docVersionId, chunkOrdinal, maxBytes, maxLines, startLine } = check.input;
53
+ const read = await readFromCitation({ root, namespace, docVersionId, chunkOrdinal, maxBytes, maxLines, startLine });
54
54
  if ("error" in read) return toolError(namespace, read.error);
55
55
  return toolResult({ namespace, result: read.result });
56
56
  } catch (error) {
@@ -36,7 +36,8 @@ const ragReadInputSchema = z.object({
36
36
  docVersionId: nonBlankString,
37
37
  chunkOrdinal: z.number().multipleOf(1).min(0).refine(Number.isSafeInteger),
38
38
  maxBytes: defaultedInteger(0, MAX_BYTES, DEFAULT_MAX_BYTES),
39
- maxLines: defaultedInteger(0, MAX_LINES, DEFAULT_MAX_LINES)
39
+ maxLines: defaultedInteger(0, MAX_LINES, DEFAULT_MAX_LINES),
40
+ startLine: defaultedInteger(0, MAX_LINES, 0)
40
41
  }).strict();
41
42
  function unknownField(issues) {
42
43
  const issue = issues.find((item) => item.code === "unrecognized_keys" && item.path.length === 0) ?? issues.find((item) => item.code === "unrecognized_keys");
@@ -77,6 +78,7 @@ function inputFailure(issues, kind) {
77
78
  }
78
79
  if (hasIssue(issues, "maxBytes")) return limitFailure("maxBytes", 0, MAX_BYTES);
79
80
  if (hasIssue(issues, "maxLines")) return limitFailure("maxLines", 0, MAX_LINES);
81
+ if (hasIssue(issues, "startLine")) return limitFailure("startLine", 0, MAX_LINES);
80
82
  }
81
83
  if (issues.some((issue) => issue.path.length === 0)) {
82
84
  return { code: "invalid_input", message: "Tool arguments must be an object." };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-rag-mcp",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Deterministic, namespace-isolated document retrieval with a CLI and MCP server.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -7,6 +7,7 @@ description: "Trigger: search or read the indexed knowledge base with explicit c
7
7
 
8
8
  - Use `rag_find` to search a client/project namespace and get a bounded catalog of matching citations.
9
9
  - Use `rag_read` only when a citation needs more surrounding text; pass its `docVersionId` and `chunkOrdinal` from `rag_find`.
10
+ - When a chunk exceeds the line limit, ensure each page fits `maxBytes` and repeat `rag_read` with `startLine` advanced by `returnedLines` to cover every line without overlap or gaps; `truncatedBy: null` marks the last page.
10
11
  - The orchestrator only receives `rag_find`; delegate exploration when a citation needs reading.
11
12
 
12
13
  ## Evidence and scope