@toolpack-sdk/knowledge 1.4.0 → 2.0.0-alpha.1

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
@@ -122,6 +122,8 @@ const webSource = new WebUrlSource(['https://docs.example.com'], {
122
122
  userAgent: 'MyApp/1.0', // Custom user agent
123
123
  maxChunkSize: 1500, // Chunk size for web content
124
124
  timeoutMs: 30000, // Request timeout
125
+ sameDomainOnly: true, // Only follow links on the same domain (default: true)
126
+ maxPagesPerDomain: 20, // Cap pages per domain (default: 10)
125
127
  });
126
128
 
127
129
  const kb = await Knowledge.create({
@@ -285,6 +287,8 @@ new WebUrlSource(['https://example.com', 'https://docs.example.com'], {
285
287
  maxChunkSize: 2000, // Max tokens per chunk
286
288
  chunkOverlap: 200, // Overlap between chunks
287
289
  timeoutMs: 30000, // Request timeout (default: 30000ms)
290
+ sameDomainOnly: true, // Only follow links on the same domain (default: true)
291
+ maxPagesPerDomain: 10, // Max pages crawled per domain (default: 10)
288
292
  namespace: 'web', // Chunk ID prefix
289
293
  metadata: { source: 'web' }, // Added to all chunks
290
294
  })
@@ -337,6 +341,67 @@ new ApiDataSource('https://api.example.com/data', {
337
341
  - JSON path support
338
342
  - Flexible content transformation
339
343
 
344
+ ### JSONSource
345
+
346
+ Index data from local JSON files.
347
+
348
+ ```typescript
349
+ import { JSONSource } from '@toolpack-sdk/knowledge';
350
+
351
+ new JSONSource('./data/products.json', {
352
+ toContent: (item: any) => `${item.name}\n\n${item.description}`, // Required
353
+ filter: (item: any) => item.active === true, // Optional: filter items
354
+ chunkSize: 100, // Items per chunk (default: 100)
355
+ namespace: 'products',
356
+ metadata: { source: 'products-db' },
357
+ })
358
+ ```
359
+
360
+ **Features:**
361
+ - Parses JSON arrays (or single objects)
362
+ - Optional item-level filtering
363
+ - Required `toContent` callback to control what gets embedded
364
+
365
+ ### SQLiteSource
366
+
367
+ Index rows from a SQLite database. Requires `better-sqlite3`.
368
+
369
+ ```typescript
370
+ import { SQLiteSource } from '@toolpack-sdk/knowledge';
371
+
372
+ new SQLiteSource('./data/app.db', {
373
+ query: 'SELECT id, title, body FROM articles WHERE published = 1', // Optional: defaults to all rows
374
+ toContent: (row) => `${row.title}\n\n${row.body}`, // Required
375
+ chunkSize: 50, // Rows per chunk (default: 100)
376
+ namespace: 'articles',
377
+ metadata: { source: 'sqlite' },
378
+ preLoadCSV: { // Optional: load a CSV into the DB before querying
379
+ tableName: 'articles',
380
+ csvPath: './data/articles.csv',
381
+ delimiter: ',',
382
+ headers: true,
383
+ },
384
+ })
385
+ ```
386
+
387
+ ### PostgresSource
388
+
389
+ Index rows from a PostgreSQL database. Requires `pg`.
390
+
391
+ ```typescript
392
+ import { PostgresSource } from '@toolpack-sdk/knowledge';
393
+
394
+ new PostgresSource({
395
+ connectionString: process.env.DATABASE_URL, // or use host/port/database/user/password
396
+ query: 'SELECT id, title, content FROM docs WHERE status = $1',
397
+ toContent: (row) => `${row.title}\n\n${row.content}`, // Required
398
+ chunkSize: 50,
399
+ namespace: 'docs',
400
+ metadata: { source: 'postgres' },
401
+ ssl: true,
402
+ })
403
+ ```
404
+
340
405
  ## Embedders
341
406
 
342
407
  ### OllamaEmbedder
@@ -345,11 +410,34 @@ Local embeddings via Ollama. Zero API cost.
345
410
 
346
411
  ```typescript
347
412
  new OllamaEmbedder({
348
- model: 'nomic-embed-text', // or 'mxbai-embed-large'
413
+ model: 'nomic-embed-text', // or 'mxbai-embed-large', 'all-minilm', 'bge-m3', etc.
349
414
  baseUrl: 'http://localhost:11434', // default
415
+ dimensions: 768, // optional: override auto-detected dimensions
416
+ retries: 3, // default
417
+ retryDelay: 1000, // ms, default
418
+ })
419
+ ```
420
+
421
+ Known models: `nomic-embed-text` (768), `mxbai-embed-large` (1024), `all-minilm` (384), `snowflake-arctic-embed` (1024), `bge-m3` (1024), `bge-large` (1024). Pass `dimensions` for any other model.
422
+
423
+ ### OpenRouterEmbedder
424
+
425
+ Embeddings via OpenRouter, giving access to OpenAI embedding models through a single API key.
426
+
427
+ ```typescript
428
+ import { OpenRouterEmbedder } from '@toolpack-sdk/knowledge';
429
+
430
+ new OpenRouterEmbedder({
431
+ model: 'openai/text-embedding-3-small', // or 'openai/text-embedding-3-large', 'openai/text-embedding-ada-002'
432
+ apiKey: process.env.OPENROUTER_API_KEY!,
433
+ dimensions: 1536, // optional: override auto-detected dimensions
434
+ retries: 3, // default
435
+ retryDelay: 1000, // ms, default
350
436
  })
351
437
  ```
352
438
 
439
+ Known models: `openai/text-embedding-3-small` (1536), `openai/text-embedding-3-large` (3072), `openai/text-embedding-ada-002` (1536). Pass `dimensions` for any other model.
440
+
353
441
  ### OpenAIEmbedder
354
442
 
355
443
  OpenAI text-embedding models with retry logic.
package/dist/index.cjs CHANGED
@@ -1,4 +1,12 @@
1
- "use strict";var ne=Object.create;var T=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var oe=Object.getPrototypeOf,ie=Object.prototype.hasOwnProperty;var ae=(a,e)=>{for(var t in e)T(a,t,{get:e[t],enumerable:!0})},B=(a,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of se(e))!ie.call(a,n)&&n!==t&&T(a,n,{get:()=>e[n],enumerable:!(r=re(e,n))||r.enumerable});return a};var g=(a,e,t)=>(t=a!=null?ne(oe(a)):{},B(e||!a||!a.__esModule?T(t,"default",{value:a,enumerable:!0}):t,a)),ce=a=>B(T({},"__esModule",{value:!0}),a);var me={};ae(me,{ApiDataSource:()=>L,ChunkTooLargeError:()=>K,DimensionMismatchError:()=>C,EmbeddingError:()=>k,IngestionError:()=>b,Knowledge:()=>z,KnowledgeError:()=>w,KnowledgeProviderError:()=>v,MarkdownSource:()=>N,MemoryProvider:()=>I,OllamaEmbedder:()=>Q,OpenAIEmbedder:()=>U,PersistentKnowledgeProvider:()=>D,WebUrlSource:()=>$,combineScores:()=>A,keywordSearch:()=>E});module.exports=ce(me);var w=class extends Error{constructor(t,r){super(t);this.code=r;this.name="KnowledgeError"}code},k=class extends w{constructor(t,r){super(t,"EMBEDDING_ERROR");this.statusCode=r;this.name="EmbeddingError"}statusCode},b=class extends w{constructor(t,r){super(t,"INGESTION_ERROR");this.file=r;this.name="IngestionError"}file},K=class extends w{constructor(t,r){super(t,"CHUNK_TOO_LARGE");this.chunkSize=r;this.name="ChunkTooLargeError"}chunkSize},C=class extends w{expected;actual;constructor(e,t){super(`Dimension mismatch: expected ${e}, got ${t}`,"DIMENSION_MISMATCH"),this.name="DimensionMismatchError",this.expected=e,this.actual=t}},v=class extends w{constructor(e){super(e,"PROVIDER_ERROR"),this.name="KnowledgeProviderError"}};function E(a,e){let t=a.toLowerCase(),r=e.toLowerCase();if(t.includes(r))return 1;let n=r.split(/\s+/).filter(o=>o.length>2);if(n.length===0)return 0;let s=0;for(let o of n)t.includes(o)&&s++;return s/n.length}function A(a,e,t=.7){let r=1-t;return a*t+e*r}function M(a,e){if(a.length!==e.length)throw new Error("Vectors must have same dimensions");let t=0,r=0,n=0;for(let o=0;o<a.length;o++)t+=a[o]*e[o],r+=a[o]*a[o],n+=e[o]*e[o];let s=Math.sqrt(r)*Math.sqrt(n);return s===0?0:t/s}function x(a,e){if(!e)return!0;for(let[t,r]of Object.entries(e)){let n=a[t];if(typeof r=="object"&&r!==null&&!Array.isArray(r)){if("$in"in r){if(!r.$in.includes(n))return!1}else if("$gt"in r){let s=r.$gt;if(typeof n!="number"||n<=s)return!1}else if("$lt"in r){let s=r.$lt;if(typeof n!="number"||n>=s)return!1}}else if(n!==r)return!1}return!0}var z=class a{constructor(e,t,r,n,s){this.provider=e;this.embedder=t;this.description=r;this.sources=n;this.options=s}provider;embedder;description;sources;options;static async create(e){await e.provider.validateDimensions(e.embedder.dimensions);let t=new a(e.provider,e.embedder,e.description,e.sources,e),r=e.reSync!==!1;return!r&&"shouldReSync"in e.provider?(e.provider.shouldReSync()&&await t.sync(),t):(r&&await t.sync(),t)}async query(e,t){let r=t?.searchType??"semantic",n=t?.semanticWeight??.7;if(r==="keyword")return this.keywordQuery(e,t);if(r==="hybrid"){let[s,o]=await Promise.all([this.semanticQuery(e,t),this.keywordQuery(e,t)]);return this.combineHybridResults(s,o,n,t)}else return this.semanticQuery(e,t)}async semanticQuery(e,t){let r=await this.embedder.embed(e);return this.provider.query(r,t)}async keywordQuery(e,t){let{limit:r=10,threshold:n=.1,filter:s,includeMetadata:o=!0,includeVectors:c=!1}=t||{};if(typeof this.provider.keywordQuery=="function")return this.provider.keywordQuery(e,t);let m=await this.getAllChunks(),i=[];for(let d of m){if(s&&!x(d.metadata,s))continue;let u=E(d.content,e);u>=n&&i.push({chunk:{id:d.id,content:d.content,metadata:o?d.metadata:{},vector:c?d.vector:void 0},score:u,distance:1-u})}return i.sort((d,u)=>u.score-d.score),i.slice(0,r)}combineHybridResults(e,t,r,n){let{limit:s=10,threshold:o=.5,includeMetadata:c=!0,includeVectors:m=!1}=n||{},i=new Map(e.map(l=>[l.chunk.id,l])),d=new Map(t.map(l=>[l.chunk.id,l])),u=[],h=new Set([...i.keys(),...d.keys()]);for(let l of h){let p=i.get(l),P=d.get(l);if(!p&&!P)continue;let ee=p?.score??0,te=P?.score??0,F=A(ee,te,r);F>=o&&u.push({chunk:{id:l,content:p?.chunk.content??P.chunk.content,metadata:c?p?.chunk.metadata??P.chunk.metadata:{},vector:m?p?.chunk.vector??P.chunk.vector:void 0},score:F,distance:1-F})}return u.sort((l,p)=>p.score-l.score),u.slice(0,s)}async getAllChunks(){if(typeof this.provider.getAllChunks=="function")return this.provider.getAllChunks();let e=new Array(this.embedder.dimensions).fill(0);return(await this.provider.query(e,{limit:1e4,threshold:0})).map(t=>t.chunk)}async sync(){this.options.onSync?.({type:"start"});try{let e=this.embedder.dimensions;await this.provider.clear(),await this.provider.validateDimensions(e);let t=this.options.streamingBatchSize??100,r=0,n=[];for(let s of this.sources)for await(let o of s.load())if(n.push(o),n.length>=t){let c=await this.embedChunks(n);c.length>0&&(await this.provider.add(c),r+=c.length),n.length=0}if(n.length>0){let s=await this.embedChunks(n);s.length>0&&(await this.provider.add(s),r+=s.length)}this.options.onSync?.({type:"complete",chunksAffected:r})}catch(e){throw this.options.onSync?.({type:"error",error:e}),e}}async embedChunks(e){if(e.length===0)return[];let t=[];try{let r=e.map(s=>s.content),n=await this.embedder.embedBatch(r);for(let s=0;s<e.length;s++)t.push({...e[s],vector:n[s]}),this.options.onEmbeddingProgress?.({source:"sync",current:s+1,total:e.length,percent:Math.round((s+1)/e.length*100)})}catch(r){if(this.options.onError?.(r,{})==="abort")throw r;for(let s=0;s<e.length;s++)try{let o=await this.embedder.embed(e[s].content);t.push({...e[s],vector:o}),this.options.onEmbeddingProgress?.({source:"sync",current:s+1,total:e.length,percent:Math.round((s+1)/e.length*100)})}catch(o){if(this.options.onError?.(o,{chunk:e[s]})==="abort")throw o}}return t}async stop(){this.provider.close&&this.provider.close()}toTool(){return{name:"knowledge_search",displayName:"Knowledge Search",description:this.description||"Search the knowledge base for relevant information",category:"search",cacheable:!1,parameters:{type:"object",properties:{query:{type:"string",description:"Search query to find relevant information"},limit:{type:"number",description:"Maximum number of results to return (default: 10)"},threshold:{type:"number",description:"Minimum similarity threshold 0-1 (default: 0.7)"},filter:{type:"object",description:"Optional metadata filters"}},required:["query"]},execute:async e=>(await this.query(e.query,{limit:e.limit,threshold:e.threshold,filter:e.filter})).map(r=>({content:r.chunk.content,score:r.score,metadata:r.chunk.metadata}))}}};var I=class{constructor(e={}){this.options=e}options;chunks=new Map;dimensions;async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new C(this.dimensions,e);this.dimensions=e}async add(e){for(let t of e){if(!t.vector)throw new v("Chunk missing vector");if(this.options.maxChunks&&this.chunks.size>=this.options.maxChunks)throw new v(`Max chunks limit reached: ${this.options.maxChunks}`);this.chunks.set(t.id,{chunk:{id:t.id,content:t.content,metadata:t.metadata},vector:t.vector})}}async query(e,t={}){let{limit:r=10,threshold:n=.7,filter:s,includeMetadata:o=!0,includeVectors:c=!1}=t,m=[];for(let{chunk:i,vector:d}of this.chunks.values()){if(s&&!x(i.metadata,s))continue;let u=M(e,d);u>=n&&m.push({chunk:{id:i.id,content:i.content,metadata:o?i.metadata:{},vector:c?d:void 0},score:u,distance:1-u})}return m.sort((i,d)=>d.score-i.score),m.slice(0,r)}async keywordQuery(e,t={}){let{limit:r=10,threshold:n=.1,filter:s,includeMetadata:o=!0,includeVectors:c=!1}=t,m=[];for(let{chunk:i,vector:d}of this.chunks.values()){if(s&&!x(i.metadata,s))continue;let u=E(i.content,e);u>=n&&m.push({chunk:{id:i.id,content:i.content,metadata:o?i.metadata:{},vector:c?d:void 0},score:u,distance:1-u})}return m.sort((i,d)=>d.score-i.score),m.slice(0,r)}async delete(e){for(let t of e)this.chunks.delete(t)}async clear(){this.chunks.clear(),this.dimensions=void 0}async getAllChunks(){return Array.from(this.chunks.values()).map(({chunk:e,vector:t})=>({...e,vector:t}))}};var q=g(require("better-sqlite3"),1),W=g(require("fs"),1),_=g(require("path"),1),V=g(require("os"),1);var D=class{constructor(e){this.options=e;let t=e.storagePath||_.join(V.homedir(),".toolpack","knowledge");this.dbPath=_.join(t,`${e.namespace}.db`),W.mkdirSync(t,{recursive:!0}),this.db=new q.default(this.dbPath),this.db.pragma("journal_mode = WAL"),this.initSchema(),this.loadDimensions()}options;db;dimensions;dbPath;initSchema(){this.db.exec(`
1
+ "use strict";var ws=Object.create;var we=Object.defineProperty;var Ss=Object.getOwnPropertyDescriptor;var vs=Object.getOwnPropertyNames;var Es=Object.getPrototypeOf,_s=Object.prototype.hasOwnProperty;var ar=(r,e)=>()=>(r&&(e=r(r=0)),e);var g=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports),cr=(r,e)=>{for(var t in e)we(r,t,{get:e[t],enumerable:!0})},ur=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of vs(e))!_s.call(r,s)&&s!==t&&we(r,s,{get:()=>e[s],enumerable:!(n=Ss(e,s))||n.enumerable});return r};var _=(r,e,t)=>(t=r!=null?ws(Es(r)):{},ur(e||!r||!r.__esModule?we(t,"default",{value:r,enumerable:!0}):t,r)),ks=r=>ur(we({},"__esModule",{value:!0}),r);var u=ar(()=>{"use strict"});var nt=g(Sr=>{"use strict";u();Sr.parse=function(r,e){return new rt(r,e).parse()};var rt=class r{constructor(e,t){this.source=e,this.transform=t||Ps,this.position=0,this.entries=[],this.recorded=[],this.dimension=0}isEof(){return this.position>=this.source.length}nextCharacter(){var e=this.source[this.position++];return e==="\\"?{value:this.source[this.position++],escaped:!0}:{value:e,escaped:!1}}record(e){this.recorded.push(e)}newEntry(e){var t;(this.recorded.length>0||e)&&(t=this.recorded.join(""),t==="NULL"&&!e&&(t=null),t!==null&&(t=this.transform(t)),this.entries.push(t),this.recorded=[])}consumeDimensions(){if(this.source[0]==="[")for(;!this.isEof();){var e=this.nextCharacter();if(e.value==="=")break}}parse(e){var t,n,s;for(this.consumeDimensions();!this.isEof();)if(t=this.nextCharacter(),t.value==="{"&&!s)this.dimension++,this.dimension>1&&(n=new r(this.source.substr(this.position-1),this.transform),this.entries.push(n.parse(!0)),this.position+=n.position-2);else if(t.value==="}"&&!s){if(this.dimension--,!this.dimension&&(this.newEntry(),e))return this.entries}else t.value==='"'&&!t.escaped?(s&&this.newEntry(!0),s=!s):t.value===","&&!s?this.newEntry():this.record(t.value);if(this.dimension!==0)throw new Error("array dimension not balanced");return this.entries}};function Ps(r){return r}});var st=g((au,vr)=>{"use strict";u();var As=nt();vr.exports={create:function(r,e){return{parse:function(){return As.parse(r,e)}}}}});var kr=g((uu,_r)=>{"use strict";u();var Rs=/(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?.*?( BC)?$/,Ts=/^(\d{1,})-(\d{2})-(\d{2})( BC)?$/,Is=/([Z+-])(\d{2})?:?(\d{2})?:?(\d{2})?/,Os=/^-?infinity$/;_r.exports=function(e){if(Os.test(e))return Number(e.replace("i","I"));var t=Rs.exec(e);if(!t)return Ms(e)||null;var n=!!t[8],s=parseInt(t[1],10);n&&(s=Er(s));var i=parseInt(t[2],10)-1,o=t[3],a=parseInt(t[4],10),l=parseInt(t[5],10),c=parseInt(t[6],10),h=t[7];h=h?1e3*parseFloat(h):0;var d,p=qs(e);return p!=null?(d=new Date(Date.UTC(s,i,o,a,l,c,h)),it(s)&&d.setUTCFullYear(s),p!==0&&d.setTime(d.getTime()-p)):(d=new Date(s,i,o,a,l,c,h),it(s)&&d.setFullYear(s)),d};function Ms(r){var e=Ts.exec(r);if(e){var t=parseInt(e[1],10),n=!!e[4];n&&(t=Er(t));var s=parseInt(e[2],10)-1,i=e[3],o=new Date(t,s,i);return it(t)&&o.setFullYear(t),o}}function qs(r){if(r.endsWith("+00"))return 0;var e=Is.exec(r.split(" ")[1]);if(e){var t=e[1];if(t==="Z")return 0;var n=t==="-"?-1:1,s=parseInt(e[2],10)*3600+parseInt(e[3]||0,10)*60+parseInt(e[4]||0,10);return s*n*1e3}}function Er(r){return-(r-1)}function it(r){return r>=0&&r<100}});var xr=g((hu,Cr)=>{"use strict";u();Cr.exports=Ds;var Ls=Object.prototype.hasOwnProperty;function Ds(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Ls.call(t,n)&&(r[n]=t[n])}return r}});var Rr=g((fu,Ar)=>{"use strict";u();var Qs=xr();Ar.exports=Y;function Y(r){if(!(this instanceof Y))return new Y(r);Qs(this,Ws(r))}var Ns=["seconds","minutes","hours","days","months","years"];Y.prototype.toPostgres=function(){var r=Ns.filter(this.hasOwnProperty,this);return this.milliseconds&&r.indexOf("seconds")<0&&r.push("seconds"),r.length===0?"0":r.map(function(e){var t=this[e]||0;return e==="seconds"&&this.milliseconds&&(t=(t+this.milliseconds/1e3).toFixed(6).replace(/\.?0+$/,"")),t+" "+e},this).join(" ")};var Bs={years:"Y",months:"M",days:"D",hours:"H",minutes:"M",seconds:"S"},Fs=["years","months","days"],Us=["hours","minutes","seconds"];Y.prototype.toISOString=Y.prototype.toISO=function(){var r=Fs.map(t,this).join(""),e=Us.map(t,this).join("");return"P"+r+"T"+e;function t(n){var s=this[n]||0;return n==="seconds"&&this.milliseconds&&(s=(s+this.milliseconds/1e3).toFixed(6).replace(/0+$/,"")),s+Bs[n]}};var ot="([+-]?\\d+)",js=ot+"\\s+years?",$s=ot+"\\s+mons?",Hs=ot+"\\s+days?",zs="([+-])?([\\d]*):(\\d\\d):(\\d\\d)\\.?(\\d{1,6})?",Ks=new RegExp([js,$s,Hs,zs].map(function(r){return"("+r+")?"}).join("\\s*")),Pr={years:2,months:4,days:6,hours:9,minutes:10,seconds:11,milliseconds:12},Gs=["hours","minutes","seconds","milliseconds"];function Vs(r){var e=r+"000000".slice(r.length);return parseInt(e,10)/1e3}function Ws(r){if(!r)return{};var e=Ks.exec(r),t=e[8]==="-";return Object.keys(Pr).reduce(function(n,s){var i=Pr[s],o=e[i];return!o||(o=s==="milliseconds"?Vs(o):parseInt(o,10),!o)||(t&&~Gs.indexOf(s)&&(o*=-1),n[s]=o),n},{})}});var Or=g((mu,Ir)=>{"use strict";u();var Tr=Buffer.from||Buffer;Ir.exports=function(e){if(/^\\x/.test(e))return Tr(e.substr(2),"hex");for(var t="",n=0;n<e.length;)if(e[n]!=="\\")t+=e[n],++n;else if(/[0-7]{3}/.test(e.substr(n+1,3)))t+=String.fromCharCode(parseInt(e.substr(n+1,3),8)),n+=4;else{for(var s=1;n+s<e.length&&e[n+s]==="\\";)s++;for(var i=0;i<Math.floor(s/2);++i)t+="\\";n+=Math.floor(s/2)*2}return Tr(t,"binary")}});var Br=g((gu,Nr)=>{"use strict";u();var ce=nt(),ue=st(),Re=kr(),qr=Rr(),Lr=Or();function Te(r){return function(t){return t===null?t:r(t)}}function Dr(r){return r===null?r:r==="TRUE"||r==="t"||r==="true"||r==="y"||r==="yes"||r==="on"||r==="1"}function Ys(r){return r?ce.parse(r,Dr):null}function Js(r){return parseInt(r,10)}function at(r){return r?ce.parse(r,Te(Js)):null}function Xs(r){return r?ce.parse(r,Te(function(e){return Qr(e).trim()})):null}var Zs=function(r){if(!r)return null;var e=ue.create(r,function(t){return t!==null&&(t=ht(t)),t});return e.parse()},ct=function(r){if(!r)return null;var e=ue.create(r,function(t){return t!==null&&(t=parseFloat(t)),t});return e.parse()},M=function(r){if(!r)return null;var e=ue.create(r);return e.parse()},ut=function(r){if(!r)return null;var e=ue.create(r,function(t){return t!==null&&(t=Re(t)),t});return e.parse()},ei=function(r){if(!r)return null;var e=ue.create(r,function(t){return t!==null&&(t=qr(t)),t});return e.parse()},ti=function(r){return r?ce.parse(r,Te(Lr)):null},lt=function(r){return parseInt(r,10)},Qr=function(r){var e=String(r);return/^\d+$/.test(e)?e:r},Mr=function(r){return r?ce.parse(r,Te(JSON.parse)):null},ht=function(r){return r[0]!=="("?null:(r=r.substring(1,r.length-1).split(","),{x:parseFloat(r[0]),y:parseFloat(r[1])})},ri=function(r){if(r[0]!=="<"&&r[1]!=="(")return null;for(var e="(",t="",n=!1,s=2;s<r.length-1;s++){if(n||(e+=r[s]),r[s]===")"){n=!0;continue}else if(!n)continue;r[s]!==","&&(t+=r[s])}var i=ht(e);return i.radius=parseFloat(t),i},ni=function(r){r(20,Qr),r(21,lt),r(23,lt),r(26,lt),r(700,parseFloat),r(701,parseFloat),r(16,Dr),r(1082,Re),r(1114,Re),r(1184,Re),r(600,ht),r(651,M),r(718,ri),r(1e3,Ys),r(1001,ti),r(1005,at),r(1007,at),r(1028,at),r(1016,Xs),r(1017,Zs),r(1021,ct),r(1022,ct),r(1231,ct),r(1014,M),r(1015,M),r(1008,M),r(1009,M),r(1040,M),r(1041,M),r(1115,ut),r(1182,ut),r(1185,ut),r(1186,qr),r(1187,ei),r(17,Lr),r(114,JSON.parse.bind(JSON)),r(3802,JSON.parse.bind(JSON)),r(199,Mr),r(3807,Mr),r(3907,M),r(2951,M),r(791,M),r(1183,M),r(1270,M)};Nr.exports={init:ni}});var Ur=g((wu,Fr)=>{"use strict";u();var A=1e6;function si(r){var e=r.readInt32BE(0),t=r.readUInt32BE(4),n="";e<0&&(e=~e+(t===0),t=~t+1>>>0,n="-");var s="",i,o,a,l,c,h;{if(i=e%A,e=e/A>>>0,o=4294967296*i+t,t=o/A>>>0,a=""+(o-A*t),t===0&&e===0)return n+a+s;for(l="",c=6-a.length,h=0;h<c;h++)l+="0";s=l+a+s}{if(i=e%A,e=e/A>>>0,o=4294967296*i+t,t=o/A>>>0,a=""+(o-A*t),t===0&&e===0)return n+a+s;for(l="",c=6-a.length,h=0;h<c;h++)l+="0";s=l+a+s}{if(i=e%A,e=e/A>>>0,o=4294967296*i+t,t=o/A>>>0,a=""+(o-A*t),t===0&&e===0)return n+a+s;for(l="",c=6-a.length,h=0;h<c;h++)l+="0";s=l+a+s}return i=e%A,o=4294967296*i+t,a=""+o%A,n+a+s}Fr.exports=si});var Kr=g((vu,zr)=>{"use strict";u();var ii=Ur(),S=function(r,e,t,n,s){t=t||0,n=n||!1,s=s||function(m,b,R){return m*Math.pow(2,R)+b};var i=t>>3,o=function(m){return n?~m&255:m},a=255,l=8-t%8;e<l&&(a=255<<8-e&255,l=e),t&&(a=a>>t%8);var c=0;t%8+e>=8&&(c=s(0,o(r[i])&a,l));for(var h=e+t>>3,d=i+1;d<h;d++)c=s(c,o(r[d]),8);var p=(e+t)%8;return p>0&&(c=s(c,o(r[h])>>8-p,p)),c},Hr=function(r,e,t){var n=Math.pow(2,t-1)-1,s=S(r,1),i=S(r,t,1);if(i===0)return 0;var o=1,a=function(c,h,d){c===0&&(c=1);for(var p=1;p<=d;p++)o/=2,(h&1<<d-p)>0&&(c+=o);return c},l=S(r,e,t+1,!1,a);return i==Math.pow(2,t+1)-1?l===0?s===0?1/0:-1/0:NaN:(s===0?1:-1)*Math.pow(2,i-n)*l},oi=function(r){return S(r,1)==1?-1*(S(r,15,1,!0)+1):S(r,15,1)},jr=function(r){return S(r,1)==1?-1*(S(r,31,1,!0)+1):S(r,31,1)},ai=function(r){return Hr(r,23,8)},ci=function(r){return Hr(r,52,11)},ui=function(r){var e=S(r,16,32);if(e==49152)return NaN;for(var t=Math.pow(1e4,S(r,16,16)),n=0,s=[],i=S(r,16),o=0;o<i;o++)n+=S(r,16,64+16*o)*t,t/=1e4;var a=Math.pow(10,S(r,16,48));return(e===0?1:-1)*Math.round(n*a)/a},$r=function(r,e){var t=S(e,1),n=S(e,63,1),s=new Date((t===0?1:-1)*n/1e3+9466848e5);return r||s.setTime(s.getTime()+s.getTimezoneOffset()*6e4),s.usec=n%1e3,s.getMicroSeconds=function(){return this.usec},s.setMicroSeconds=function(i){this.usec=i},s.getUTCMicroSeconds=function(){return this.usec},s},le=function(r){for(var e=S(r,32),t=S(r,32,32),n=S(r,32,64),s=96,i=[],o=0;o<e;o++)i[o]=S(r,32,s),s+=32,s+=32;var a=function(c){var h=S(r,32,s);if(s+=32,h==4294967295)return null;var d;if(c==23||c==20)return d=S(r,h*8,s),s+=h*8,d;if(c==25)return d=r.toString(this.encoding,s>>3,(s+=h<<3)>>3),d;console.log("ERROR: ElementType not implemented: "+c)},l=function(c,h){var d=[],p;if(c.length>1){var m=c.shift();for(p=0;p<m;p++)d[p]=l(c,h);c.unshift(m)}else for(p=0;p<c[0];p++)d[p]=a(h);return d};return l(i,n)},li=function(r){return r.toString("utf8")},hi=function(r){return r===null?null:S(r,8)>0},di=function(r){r(20,ii),r(21,oi),r(23,jr),r(26,jr),r(1700,ui),r(700,ai),r(701,ci),r(16,hi),r(1114,$r.bind(null,!1)),r(1184,$r.bind(null,!0)),r(1e3,le),r(1007,le),r(1016,le),r(1008,le),r(1009,le),r(25,li)};zr.exports={init:di}});var Vr=g((_u,Gr)=>{"use strict";u();Gr.exports={BOOL:16,BYTEA:17,CHAR:18,INT8:20,INT2:21,INT4:23,REGPROC:24,TEXT:25,OID:26,TID:27,XID:28,CID:29,JSON:114,XML:142,PG_NODE_TREE:194,SMGR:210,PATH:602,POLYGON:604,CIDR:650,FLOAT4:700,FLOAT8:701,ABSTIME:702,RELTIME:703,TINTERVAL:704,CIRCLE:718,MACADDR8:774,MONEY:790,MACADDR:829,INET:869,ACLITEM:1033,BPCHAR:1042,VARCHAR:1043,DATE:1082,TIME:1083,TIMESTAMP:1114,TIMESTAMPTZ:1184,INTERVAL:1186,TIMETZ:1266,BIT:1560,VARBIT:1562,NUMERIC:1700,REFCURSOR:1790,REGPROCEDURE:2202,REGOPER:2203,REGOPERATOR:2204,REGCLASS:2205,REGTYPE:2206,UUID:2950,TXID_SNAPSHOT:2970,PG_LSN:3220,PG_NDISTINCT:3361,PG_DEPENDENCIES:3402,TSVECTOR:3614,TSQUERY:3615,GTSVECTOR:3642,REGCONFIG:3734,REGDICTIONARY:3769,JSONB:3802,REGNAMESPACE:4089,REGROLE:4096}});var fe=g(de=>{"use strict";u();var fi=Br(),pi=Kr(),mi=st(),yi=Vr();de.getTypeParser=gi;de.setTypeParser=bi;de.arrayParser=mi;de.builtins=yi;var he={text:{},binary:{}};function Wr(r){return String(r)}function gi(r,e){return e=e||"text",he[e]&&he[e][r]||Wr}function bi(r,e,t){typeof e=="function"&&(t=e,e="text"),he[e][r]=t}fi.init(function(r,e){he.text[r]=e});pi.init(function(r,e){he.binary[r]=e})});var pe=g((Pu,dt)=>{"use strict";u();var Yr;try{Yr=process.platform==="win32"?process.env.USERNAME:process.env.USER}catch{}dt.exports={host:"localhost",user:Yr,database:void 0,password:null,connectionString:void 0,port:5432,rows:0,binary:!1,max:10,idleTimeoutMillis:3e4,client_encoding:"",ssl:!1,application_name:void 0,fallback_application_name:void 0,options:void 0,parseInputDatesAsUTC:!1,statement_timeout:!1,lock_timeout:!1,idle_in_transaction_session_timeout:!1,query_timeout:!1,connect_timeout:0,keepalives:1,keepalives_idle:0};var J=fe(),wi=J.getTypeParser(20,"text"),Si=J.getTypeParser(1016,"text");dt.exports.__defineSetter__("parseInt8",function(r){J.setTypeParser(20,"text",r?J.getTypeParser(23,"text"):wi),J.setTypeParser(1016,"text",r?J.getTypeParser(1007,"text"):Si)})});var X=g((Ru,Zr)=>{"use strict";u();var vi=pe(),Jr=require("util"),{isDate:Ei}=Jr.types||Jr;function _i(r){return'"'+r.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+'"'}function Xr(r){let e="{";for(let t=0;t<r.length;t++)if(t>0&&(e=e+","),r[t]===null||typeof r[t]>"u")e=e+"NULL";else if(Array.isArray(r[t]))e=e+Xr(r[t]);else if(ArrayBuffer.isView(r[t])){let n=r[t];if(!(n instanceof Buffer)){let s=Buffer.from(n.buffer,n.byteOffset,n.byteLength);s.length===n.byteLength?n=s:n=s.slice(n.byteOffset,n.byteOffset+n.byteLength)}e+="\\\\x"+n.toString("hex")}else e+=_i(Ie(r[t]));return e=e+"}",e}var Ie=function(r,e){if(r==null)return null;if(typeof r=="object"){if(r instanceof Buffer)return r;if(ArrayBuffer.isView(r)){let t=Buffer.from(r.buffer,r.byteOffset,r.byteLength);return t.length===r.byteLength?t:t.slice(r.byteOffset,r.byteOffset+r.byteLength)}return Ei(r)?vi.parseInputDatesAsUTC?xi(r):Ci(r):Array.isArray(r)?Xr(r):ki(r,e)}return r.toString()};function ki(r,e){if(r&&typeof r.toPostgres=="function"){if(e=e||[],e.indexOf(r)!==-1)throw new Error('circular reference detected while preparing "'+r+'" for query');return e.push(r),Ie(r.toPostgres(Ie),e)}return JSON.stringify(r)}function Ci(r){let e=-r.getTimezoneOffset(),t=r.getFullYear(),n=t<1;n&&(t=Math.abs(t)+1);let s=String(t).padStart(4,"0")+"-"+String(r.getMonth()+1).padStart(2,"0")+"-"+String(r.getDate()).padStart(2,"0")+"T"+String(r.getHours()).padStart(2,"0")+":"+String(r.getMinutes()).padStart(2,"0")+":"+String(r.getSeconds()).padStart(2,"0")+"."+String(r.getMilliseconds()).padStart(3,"0");return e<0?(s+="-",e*=-1):s+="+",s+=String(Math.floor(e/60)).padStart(2,"0")+":"+String(e%60).padStart(2,"0"),n&&(s+=" BC"),s}function xi(r){let e=r.getUTCFullYear(),t=e<1;t&&(e=Math.abs(e)+1);let n=String(e).padStart(4,"0")+"-"+String(r.getUTCMonth()+1).padStart(2,"0")+"-"+String(r.getUTCDate()).padStart(2,"0")+"T"+String(r.getUTCHours()).padStart(2,"0")+":"+String(r.getUTCMinutes()).padStart(2,"0")+":"+String(r.getUTCSeconds()).padStart(2,"0")+"."+String(r.getUTCMilliseconds()).padStart(3,"0");return n+="+00:00",t&&(n+=" BC"),n}function Pi(r,e,t){return r=typeof r=="string"?{text:r}:r,e&&(typeof e=="function"?r.callback=e:r.values=e),t&&(r.callback=t),r}var Ai=function(r){return'"'+r.replace(/"/g,'""')+'"'},Ri=function(r){let e=!1,t="'";if(r==null||typeof r!="string")return"''";for(let n=0;n<r.length;n++){let s=r[n];s==="'"?t+=s+s:s==="\\"?(t+=s+s,e=!0):t+=s}return t+="'",e===!0&&(t=" E"+t),t};Zr.exports={prepareValue:function(e){return Ie(e)},normalizeQueryConfig:Pi,escapeIdentifier:Ai,escapeLiteral:Ri}});var tn=g((Iu,en)=>{"use strict";u();var Z=require("crypto");function ft(r){return Z.createHash("md5").update(r,"utf-8").digest("hex")}function Ti(r,e,t){let n=ft(e+r);return"md5"+ft(Buffer.concat([Buffer.from(n),t]))}function Ii(r){return Z.createHash("sha256").update(r).digest()}function Oi(r,e){return r=r.replace(/(\D)-/,"$1"),Z.createHash(r).update(e).digest()}function Mi(r,e){return Z.createHmac("sha256",r).update(e).digest()}async function qi(r,e,t){return Z.pbkdf2Sync(r,e,t,32,"sha256")}en.exports={postgresMd5PasswordHash:Ti,randomBytes:Z.randomBytes,deriveKey:qi,sha256:Ii,hashByName:Oi,hmacSha256:Mi,md5:ft}});var on=g((Mu,sn)=>{"use strict";u();var rn=require("crypto");sn.exports={postgresMd5PasswordHash:Di,randomBytes:Li,deriveKey:Fi,sha256:Qi,hashByName:Ni,hmacSha256:Bi,md5:pt};var nn=rn.webcrypto||globalThis.crypto,H=nn.subtle,mt=new TextEncoder;function Li(r){return nn.getRandomValues(Buffer.alloc(r))}async function pt(r){try{return rn.createHash("md5").update(r,"utf-8").digest("hex")}catch{let t=typeof r=="string"?mt.encode(r):r,n=await H.digest("MD5",t);return Array.from(new Uint8Array(n)).map(s=>s.toString(16).padStart(2,"0")).join("")}}async function Di(r,e,t){let n=await pt(e+r);return"md5"+await pt(Buffer.concat([Buffer.from(n),t]))}async function Qi(r){return await H.digest("SHA-256",r)}async function Ni(r,e){return await H.digest(r,e)}async function Bi(r,e){let t=await H.importKey("raw",r,{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return await H.sign("HMAC",t,mt.encode(e))}async function Fi(r,e,t){let n=await H.importKey("raw",mt.encode(r),"PBKDF2",!1,["deriveBits"]),s={name:"PBKDF2",hash:"SHA-256",salt:e,iterations:t};return await H.deriveBits(s,n,256,["deriveBits"])}});var gt=g((Lu,yt)=>{"use strict";u();var Ui=parseInt(process.versions&&process.versions.node&&process.versions.node.split(".")[0])<15;Ui?yt.exports=tn():yt.exports=on()});var un=g((Qu,cn)=>{"use strict";u();function z(r,e){return new Error("SASL channel binding: "+r+" when parsing public certificate "+e.toString("base64"))}function bt(r,e){let t=r[e++];if(t<128)return{length:t,index:e};let n=t&127;if(n>4)throw z("bad length",r);t=0;for(let s=0;s<n;s++)t=t<<8|r[e++];return{length:t,index:e}}function an(r,e){if(r[e++]!==6)throw z("non-OID data",r);let{length:t,index:n}=bt(r,e);e=n;let s=e+t,i=r[e++],o=(i/40>>0)+"."+i%40;for(;e<s;){let a=0;for(;e<s;){let l=r[e++];if(a=a<<7|l&127,l<128)break}o+="."+a}return{oid:o,index:e}}function me(r,e){if(r[e++]!==48)throw z("non-sequence data",r);return bt(r,e)}function ji(r,e){e===void 0&&(e=0),e=me(r,e).index;let{length:t,index:n}=me(r,e);e=n+t,e=me(r,e).index;let{oid:s,index:i}=an(r,e);switch(s){case"1.2.840.113549.1.1.4":return"MD5";case"1.2.840.113549.1.1.5":return"SHA-1";case"1.2.840.113549.1.1.11":return"SHA-256";case"1.2.840.113549.1.1.12":return"SHA-384";case"1.2.840.113549.1.1.13":return"SHA-512";case"1.2.840.113549.1.1.14":return"SHA-224";case"1.2.840.113549.1.1.15":return"SHA512-224";case"1.2.840.113549.1.1.16":return"SHA512-256";case"1.2.840.10045.4.1":return"SHA-1";case"1.2.840.10045.4.3.1":return"SHA-224";case"1.2.840.10045.4.3.2":return"SHA-256";case"1.2.840.10045.4.3.3":return"SHA-384";case"1.2.840.10045.4.3.4":return"SHA-512";case"1.2.840.113549.1.1.10":{if(e=i,e=me(r,e).index,r[e++]!==160)throw z("non-tag data",r);e=bt(r,e).index,e=me(r,e).index;let{oid:o}=an(r,e);switch(o){case"1.2.840.113549.2.5":return"MD5";case"1.3.14.3.2.26":return"SHA-1";case"2.16.840.1.101.3.4.2.1":return"SHA-256";case"2.16.840.1.101.3.4.2.2":return"SHA-384";case"2.16.840.1.101.3.4.2.3":return"SHA-512"}throw z("unknown hash OID "+o,r)}case"1.3.101.110":case"1.3.101.112":return"SHA-512";case"1.3.101.111":case"1.3.101.113":throw z("Ed448 certificate channel binding is not currently supported by Postgres")}throw z("unknown OID "+s,r)}cn.exports={signatureAlgorithmHashFromCertificate:ji}});var fn=g((Bu,dn)=>{"use strict";u();var F=gt(),{signatureAlgorithmHashFromCertificate:$i}=un();function Hi(r,e){let t=["SCRAM-SHA-256"];e&&t.unshift("SCRAM-SHA-256-PLUS");let n=t.find(o=>r.includes(o));if(!n)throw new Error("SASL: Only mechanism(s) "+t.join(" and ")+" are supported");if(n==="SCRAM-SHA-256-PLUS"&&typeof e.getPeerCertificate!="function")throw new Error("SASL: Mechanism SCRAM-SHA-256-PLUS requires a certificate");let s=F.randomBytes(18).toString("base64");return{mechanism:n,clientNonce:s,response:(n==="SCRAM-SHA-256-PLUS"?"p=tls-server-end-point":e?"y":"n")+",,n=*,r="+s,message:"SASLInitialResponse"}}async function zi(r,e,t,n){if(r.message!=="SASLInitialResponse")throw new Error("SASL: Last message was not SASLInitialResponse");if(typeof e!="string")throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string");if(e==="")throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a non-empty string");if(typeof t!="string")throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: serverData must be a string");let s=Vi(t);if(s.nonce.startsWith(r.clientNonce)){if(s.nonce.length===r.clientNonce.length)throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce is too short")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce does not start with client nonce");let i="n=*,r="+r.clientNonce,o="r="+s.nonce+",s="+s.salt+",i="+s.iteration,a=n?"eSws":"biws";if(r.mechanism==="SCRAM-SHA-256-PLUS"){let K=n.getPeerCertificate().raw,be=$i(K);(be==="MD5"||be==="SHA-1")&&(be="SHA-256");let bs=await F.hashByName(be,K);a=Buffer.concat([Buffer.from("p=tls-server-end-point,,"),Buffer.from(bs)]).toString("base64")}let l="c="+a+",r="+s.nonce,c=i+","+o+","+l,h=Buffer.from(s.salt,"base64"),d=await F.deriveKey(e,h,s.iteration),p=await F.hmacSha256(d,"Client Key"),m=await F.sha256(p),b=await F.hmacSha256(m,c),R=Yi(Buffer.from(p),Buffer.from(b)).toString("base64"),We=await F.hmacSha256(d,"Server Key"),Ye=await F.hmacSha256(We,c);r.message="SASLResponse",r.serverSignature=Buffer.from(Ye).toString("base64"),r.response=l+",p="+R}function Ki(r,e){if(r.message!=="SASLResponse")throw new Error("SASL: Last message was not SASLResponse");if(typeof e!="string")throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: serverData must be a string");let{serverSignature:t}=Wi(e);if(t!==r.serverSignature)throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature does not match")}function Gi(r){if(typeof r!="string")throw new TypeError("SASL: text must be a string");return r.split("").map((e,t)=>r.charCodeAt(t)).every(e=>e>=33&&e<=43||e>=45&&e<=126)}function ln(r){return/^(?:[a-zA-Z0-9+/]{4})*(?:[a-zA-Z0-9+/]{2}==|[a-zA-Z0-9+/]{3}=)?$/.test(r)}function hn(r){if(typeof r!="string")throw new TypeError("SASL: attribute pairs text must be a string");return new Map(r.split(",").map(e=>{if(!/^.=/.test(e))throw new Error("SASL: Invalid attribute pair entry");let t=e[0],n=e.substring(2);return[t,n]}))}function Vi(r){let e=hn(r),t=e.get("r");if(t){if(!Gi(t))throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce must only contain printable characters")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing");let n=e.get("s");if(n){if(!ln(n))throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: salt must be base64")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing");let s=e.get("i");if(s){if(!/^[1-9][0-9]*$/.test(s))throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: invalid iteration count")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing");let i=parseInt(s,10);return{nonce:t,salt:n,iteration:i}}function Wi(r){let t=hn(r).get("v");if(t){if(!ln(t))throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature must be base64")}else throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature is missing");return{serverSignature:t}}function Yi(r,e){if(!Buffer.isBuffer(r))throw new TypeError("first argument must be a Buffer");if(!Buffer.isBuffer(e))throw new TypeError("second argument must be a Buffer");if(r.length!==e.length)throw new Error("Buffer lengths must match");if(r.length===0)throw new Error("Buffers cannot be empty");return Buffer.from(r.map((t,n)=>r[n]^e[n]))}dn.exports={startSession:Hi,continueSession:zi,finalizeSession:Ki}});var Me=g((Uu,pn)=>{"use strict";u();var Ji=fe();function Oe(r){this._types=r||Ji,this.text={},this.binary={}}Oe.prototype.getOverrides=function(r){switch(r){case"text":return this.text;case"binary":return this.binary;default:return{}}};Oe.prototype.setTypeParser=function(r,e,t){typeof e=="function"&&(t=e,e="text"),this.getOverrides(e)[r]=t};Oe.prototype.getTypeParser=function(r,e){return e=e||"text",this.getOverrides(e)[r]||this._types.getTypeParser(r,e)};pn.exports=Oe});var gn=g(($u,yn)=>{"use strict";u();function ee(r,e={}){if(r.charAt(0)==="/"){let l=r.split(" ");return{host:l[0],database:l[1]}}let t={},n,s=!1;/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(r)&&(r=encodeURI(r).replace(/%25(\d\d)/g,"%$1"));try{try{n=new URL(r,"postgres://base")}catch{n=new URL(r.replace("@/","@___DUMMY___/"),"postgres://base"),s=!0}}catch(l){throw l.input&&(l.input="*****REDACTED*****"),l}for(let l of n.searchParams.entries())t[l[0]]=l[1];if(t.user=t.user||decodeURIComponent(n.username),t.password=t.password||decodeURIComponent(n.password),n.protocol=="socket:")return t.host=decodeURI(n.pathname),t.database=n.searchParams.get("db"),t.client_encoding=n.searchParams.get("encoding"),t;let i=s?"":n.hostname;t.host?i&&/^%2f/i.test(i)&&(n.pathname=i+n.pathname):t.host=decodeURIComponent(i),t.port||(t.port=n.port);let o=n.pathname.slice(1)||null;t.database=o?decodeURI(o):null,(t.ssl==="true"||t.ssl==="1")&&(t.ssl=!0),t.ssl==="0"&&(t.ssl=!1),(t.sslcert||t.sslkey||t.sslrootcert||t.sslmode)&&(t.ssl={});let a=t.sslcert||t.sslkey||t.sslrootcert?require("fs"):null;if(t.sslcert&&(t.ssl.cert=a.readFileSync(t.sslcert).toString()),t.sslkey&&(t.ssl.key=a.readFileSync(t.sslkey).toString()),t.sslrootcert&&(t.ssl.ca=a.readFileSync(t.sslrootcert).toString()),e.useLibpqCompat&&t.uselibpqcompat)throw new Error("Both useLibpqCompat and uselibpqcompat are set. Please use only one of them.");if(t.uselibpqcompat==="true"||e.useLibpqCompat)switch(t.sslmode){case"disable":{t.ssl=!1;break}case"prefer":{t.ssl.rejectUnauthorized=!1;break}case"require":{t.sslrootcert?t.ssl.checkServerIdentity=function(){}:t.ssl.rejectUnauthorized=!1;break}case"verify-ca":{if(!t.ssl.ca)throw new Error("SECURITY WARNING: Using sslmode=verify-ca requires specifying a CA with sslrootcert. If a public CA is used, verify-ca allows connections to a server that somebody else may have registered with the CA, making you vulnerable to Man-in-the-Middle attacks. Either specify a custom CA certificate with sslrootcert parameter or use sslmode=verify-full for proper security.");t.ssl.checkServerIdentity=function(){};break}case"verify-full":break}else switch(t.sslmode){case"disable":{t.ssl=!1;break}case"prefer":case"require":case"verify-ca":case"verify-full":{t.sslmode!=="verify-full"&&wt(t.sslmode);break}case"no-verify":{t.ssl.rejectUnauthorized=!1;break}}return t}function Xi(r){return Object.entries(r).reduce((t,[n,s])=>(s!=null&&(t[n]=s),t),{})}function mn(r){return Object.entries(r).reduce((t,[n,s])=>{if(n==="ssl"){let i=s;typeof i=="boolean"&&(t[n]=i),typeof i=="object"&&(t[n]=Xi(i))}else if(s!=null)if(n==="port"){if(s!==""){let i=parseInt(s,10);if(isNaN(i))throw new Error(`Invalid ${n}: ${s}`);t[n]=i}}else t[n]=s;return t},{})}function Zi(r){return mn(ee(r))}function wt(r){!wt.warned&&typeof process<"u"&&process.emitWarning&&(wt.warned=!0,process.emitWarning(`SECURITY WARNING: The SSL modes 'prefer', 'require', and 'verify-ca' are treated as aliases for 'verify-full'.
2
+ In the next major version (pg-connection-string v3.0.0 and pg v9.0.0), these modes will adopt standard libpq semantics, which have weaker security guarantees.
3
+
4
+ To prepare for this change:
5
+ - If you want the current behavior, explicitly use 'sslmode=verify-full'
6
+ - If you want libpq compatibility now, use 'uselibpqcompat=true&sslmode=${r}'
7
+
8
+ See https://www.postgresql.org/docs/current/libpq-ssl.html for libpq SSL mode definitions.`))}yn.exports=ee;ee.parse=ee;ee.toClientConfig=mn;ee.parseIntoClientConfig=Zi});var vt=g((zu,Sn)=>{"use strict";u();var eo=require("dns"),wn=pe(),bn=gn().parse,x=function(r,e,t){return e[r]?e[r]:(t===void 0?t=process.env["PG"+r.toUpperCase()]:t===!1||(t=process.env[t]),t||wn[r])},to=function(){switch(process.env.PGSSLMODE){case"disable":return!1;case"prefer":case"require":case"verify-ca":case"verify-full":return!0;case"no-verify":return{rejectUnauthorized:!1}}return wn.ssl},te=function(r){return"'"+(""+r).replace(/\\/g,"\\\\").replace(/'/g,"\\'")+"'"},q=function(r,e,t){let n=e[t];n!=null&&r.push(t+"="+te(n))},St=class{constructor(e){e=typeof e=="string"?bn(e):e||{},e.connectionString&&(e=Object.assign({},e,bn(e.connectionString))),this.user=x("user",e),this.database=x("database",e),this.database===void 0&&(this.database=this.user),this.port=parseInt(x("port",e),10),this.host=x("host",e),Object.defineProperty(this,"password",{configurable:!0,enumerable:!1,writable:!0,value:x("password",e)}),this.binary=x("binary",e),this.options=x("options",e),this.ssl=typeof e.ssl>"u"?to():e.ssl,typeof this.ssl=="string"&&this.ssl==="true"&&(this.ssl=!0),this.ssl==="no-verify"&&(this.ssl={rejectUnauthorized:!1}),this.ssl&&this.ssl.key&&Object.defineProperty(this.ssl,"key",{enumerable:!1}),this.client_encoding=x("client_encoding",e),this.replication=x("replication",e),this.isDomainSocket=!(this.host||"").indexOf("/"),this.application_name=x("application_name",e,"PGAPPNAME"),this.fallback_application_name=x("fallback_application_name",e,!1),this.statement_timeout=x("statement_timeout",e,!1),this.lock_timeout=x("lock_timeout",e,!1),this.idle_in_transaction_session_timeout=x("idle_in_transaction_session_timeout",e,!1),this.query_timeout=x("query_timeout",e,!1),e.connectionTimeoutMillis===void 0?this.connect_timeout=process.env.PGCONNECT_TIMEOUT||0:this.connect_timeout=Math.floor(e.connectionTimeoutMillis/1e3),e.keepAlive===!1?this.keepalives=0:e.keepAlive===!0&&(this.keepalives=1),typeof e.keepAliveInitialDelayMillis=="number"&&(this.keepalives_idle=Math.floor(e.keepAliveInitialDelayMillis/1e3))}getLibpqConnectionString(e){let t=[];q(t,this,"user"),q(t,this,"password"),q(t,this,"port"),q(t,this,"application_name"),q(t,this,"fallback_application_name"),q(t,this,"connect_timeout"),q(t,this,"options");let n=typeof this.ssl=="object"?this.ssl:this.ssl?{sslmode:this.ssl}:{};if(q(t,n,"sslmode"),q(t,n,"sslca"),q(t,n,"sslkey"),q(t,n,"sslcert"),q(t,n,"sslrootcert"),this.database&&t.push("dbname="+te(this.database)),this.replication&&t.push("replication="+te(this.replication)),this.host&&t.push("host="+te(this.host)),this.isDomainSocket)return e(null,t.join(" "));this.client_encoding&&t.push("client_encoding="+te(this.client_encoding)),eo.lookup(this.host,function(s,i){return s?e(s,null):(t.push("hostaddr="+te(i)),e(null,t.join(" ")))})}};Sn.exports=St});var _t=g((Gu,En)=>{"use strict";u();var ro=fe(),vn=/^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/,Et=class{constructor(e,t){this.command=null,this.rowCount=null,this.oid=null,this.rows=[],this.fields=[],this._parsers=void 0,this._types=t,this.RowCtor=null,this.rowAsArray=e==="array",this.rowAsArray&&(this.parseRow=this._parseRowAsArray),this._prebuiltEmptyResultObject=null}addCommandComplete(e){let t;e.text?t=vn.exec(e.text):t=vn.exec(e.command),t&&(this.command=t[1],t[3]?(this.oid=parseInt(t[2],10),this.rowCount=parseInt(t[3],10)):t[2]&&(this.rowCount=parseInt(t[2],10)))}_parseRowAsArray(e){let t=new Array(e.length);for(let n=0,s=e.length;n<s;n++){let i=e[n];i!==null?t[n]=this._parsers[n](i):t[n]=null}return t}parseRow(e){let t={...this._prebuiltEmptyResultObject};for(let n=0,s=e.length;n<s;n++){let i=e[n],o=this.fields[n].name;if(i!==null){let a=this.fields[n].format==="binary"?Buffer.from(i):i;t[o]=this._parsers[n](a)}else t[o]=null}return t}addRow(e){this.rows.push(e)}addFields(e){this.fields=e,this.fields.length&&(this._parsers=new Array(e.length));let t={};for(let n=0;n<e.length;n++){let s=e[n];t[s.name]=null,this._types?this._parsers[n]=this._types.getTypeParser(s.dataTypeID,s.format||"text"):this._parsers[n]=ro.getTypeParser(s.dataTypeID,s.format||"text")}this._prebuiltEmptyResultObject={...t}}};En.exports=Et});var xn=g((Wu,Cn)=>{"use strict";u();var{EventEmitter:no}=require("events"),_n=_t(),kn=X(),kt=class extends no{constructor(e,t,n){super(),e=kn.normalizeQueryConfig(e,t,n),this.text=e.text,this.values=e.values,this.rows=e.rows,this.types=e.types,this.name=e.name,this.queryMode=e.queryMode,this.binary=e.binary,this.portal=e.portal||"",this.callback=e.callback,this._rowMode=e.rowMode,process.domain&&e.callback&&(this.callback=process.domain.bind(e.callback)),this._result=new _n(this._rowMode,this.types),this._results=this._result,this._canceledDueToError=!1}requiresPreparation(){return this.queryMode==="extended"||this.name||this.rows?!0:!this.text||!this.values?!1:this.values.length>0}_checkForMultirow(){this._result.command&&(Array.isArray(this._results)||(this._results=[this._result]),this._result=new _n(this._rowMode,this._result._types),this._results.push(this._result))}handleRowDescription(e){this._checkForMultirow(),this._result.addFields(e.fields),this._accumulateRows=this.callback||!this.listeners("row").length}handleDataRow(e){let t;if(!this._canceledDueToError){try{t=this._result.parseRow(e.fields)}catch(n){this._canceledDueToError=n;return}this.emit("row",t,this._result),this._accumulateRows&&this._result.addRow(t)}}handleCommandComplete(e,t){this._checkForMultirow(),this._result.addCommandComplete(e),this.rows&&t.sync()}handleEmptyQuery(e){this.rows&&e.sync()}handleError(e,t){if(this._canceledDueToError&&(e=this._canceledDueToError,this._canceledDueToError=!1),this.callback)return this.callback(e);this.emit("error",e)}handleReadyForQuery(e){if(this._canceledDueToError)return this.handleError(this._canceledDueToError,e);if(this.callback)try{this.callback(null,this._results)}catch(t){process.nextTick(()=>{throw t})}this.emit("end",this._results)}submit(e){if(typeof this.text!="string"&&typeof this.name!="string")return new Error("A query must have either text or a name. Supplying neither is unsupported.");let t=e.parsedStatements[this.name];if(this.text&&t&&this.text!==t)return new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`);if(this.values&&!Array.isArray(this.values))return new Error("Query values must be an array");if(this.requiresPreparation()){e.stream.cork&&e.stream.cork();try{this.prepare(e)}finally{e.stream.uncork&&e.stream.uncork()}}else e.query(this.text);return null}hasBeenParsed(e){return this.name&&e.parsedStatements[this.name]}handlePortalSuspended(e){this._getRows(e,this.rows)}_getRows(e,t){e.execute({portal:this.portal,rows:t}),t?e.flush():e.sync()}prepare(e){this.hasBeenParsed(e)||e.parse({text:this.text,name:this.name,types:this.types});try{e.bind({portal:this.portal,statement:this.name,values:this.values,binary:this.binary,valueMapper:kn.prepareValue})}catch(t){this.handleError(t,e);return}e.describe({type:"P",name:this.portal||""}),this._getRows(e,this.rows)}handleCopyInResponse(e){e.sendCopyFail("No source stream defined")}handleCopyData(e,t){}};Cn.exports=kt});var Bt=g(y=>{"use strict";u();Object.defineProperty(y,"__esModule",{value:!0});y.NoticeMessage=y.DataRowMessage=y.CommandCompleteMessage=y.ReadyForQueryMessage=y.NotificationResponseMessage=y.BackendKeyDataMessage=y.AuthenticationMD5Password=y.ParameterStatusMessage=y.ParameterDescriptionMessage=y.RowDescriptionMessage=y.Field=y.CopyResponse=y.CopyDataMessage=y.DatabaseError=y.copyDone=y.emptyQuery=y.replicationStart=y.portalSuspended=y.noData=y.closeComplete=y.bindComplete=y.parseComplete=void 0;y.parseComplete={name:"parseComplete",length:5};y.bindComplete={name:"bindComplete",length:5};y.closeComplete={name:"closeComplete",length:5};y.noData={name:"noData",length:5};y.portalSuspended={name:"portalSuspended",length:5};y.replicationStart={name:"replicationStart",length:4};y.emptyQuery={name:"emptyQuery",length:4};y.copyDone={name:"copyDone",length:4};var Ct=class extends Error{constructor(e,t,n){super(e),this.length=t,this.name=n}};y.DatabaseError=Ct;var xt=class{constructor(e,t){this.length=e,this.chunk=t,this.name="copyData"}};y.CopyDataMessage=xt;var Pt=class{constructor(e,t,n,s){this.length=e,this.name=t,this.binary=n,this.columnTypes=new Array(s)}};y.CopyResponse=Pt;var At=class{constructor(e,t,n,s,i,o,a){this.name=e,this.tableID=t,this.columnID=n,this.dataTypeID=s,this.dataTypeSize=i,this.dataTypeModifier=o,this.format=a}};y.Field=At;var Rt=class{constructor(e,t){this.length=e,this.fieldCount=t,this.name="rowDescription",this.fields=new Array(this.fieldCount)}};y.RowDescriptionMessage=Rt;var Tt=class{constructor(e,t){this.length=e,this.parameterCount=t,this.name="parameterDescription",this.dataTypeIDs=new Array(this.parameterCount)}};y.ParameterDescriptionMessage=Tt;var It=class{constructor(e,t,n){this.length=e,this.parameterName=t,this.parameterValue=n,this.name="parameterStatus"}};y.ParameterStatusMessage=It;var Ot=class{constructor(e,t){this.length=e,this.salt=t,this.name="authenticationMD5Password"}};y.AuthenticationMD5Password=Ot;var Mt=class{constructor(e,t,n){this.length=e,this.processID=t,this.secretKey=n,this.name="backendKeyData"}};y.BackendKeyDataMessage=Mt;var qt=class{constructor(e,t,n,s){this.length=e,this.processId=t,this.channel=n,this.payload=s,this.name="notification"}};y.NotificationResponseMessage=qt;var Lt=class{constructor(e,t){this.length=e,this.status=t,this.name="readyForQuery"}};y.ReadyForQueryMessage=Lt;var Dt=class{constructor(e,t){this.length=e,this.text=t,this.name="commandComplete"}};y.CommandCompleteMessage=Dt;var Qt=class{constructor(e,t){this.length=e,this.fields=t,this.name="dataRow",this.fieldCount=t.length}};y.DataRowMessage=Qt;var Nt=class{constructor(e,t){this.length=e,this.message=t,this.name="notice"}};y.NoticeMessage=Nt});var Pn=g(qe=>{"use strict";u();Object.defineProperty(qe,"__esModule",{value:!0});qe.Writer=void 0;var Ft=class{constructor(e=256){this.size=e,this.offset=5,this.headerPosition=0,this.buffer=Buffer.allocUnsafe(e)}ensure(e){if(this.buffer.length-this.offset<e){let n=this.buffer,s=n.length+(n.length>>1)+e;this.buffer=Buffer.allocUnsafe(s),n.copy(this.buffer)}}addInt32(e){return this.ensure(4),this.buffer[this.offset++]=e>>>24&255,this.buffer[this.offset++]=e>>>16&255,this.buffer[this.offset++]=e>>>8&255,this.buffer[this.offset++]=e>>>0&255,this}addInt16(e){return this.ensure(2),this.buffer[this.offset++]=e>>>8&255,this.buffer[this.offset++]=e>>>0&255,this}addCString(e){if(!e)this.ensure(1);else{let t=Buffer.byteLength(e);this.ensure(t+1),this.buffer.write(e,this.offset,"utf-8"),this.offset+=t}return this.buffer[this.offset++]=0,this}addString(e=""){let t=Buffer.byteLength(e);return this.ensure(t),this.buffer.write(e,this.offset),this.offset+=t,this}add(e){return this.ensure(e.length),e.copy(this.buffer,this.offset),this.offset+=e.length,this}join(e){if(e){this.buffer[this.headerPosition]=e;let t=this.offset-(this.headerPosition+1);this.buffer.writeInt32BE(t,this.headerPosition+1)}return this.buffer.slice(e?0:5,this.offset)}flush(e){let t=this.join(e);return this.offset=5,this.headerPosition=0,this.buffer=Buffer.allocUnsafe(this.size),t}};qe.Writer=Ft});var Rn=g(De=>{"use strict";u();Object.defineProperty(De,"__esModule",{value:!0});De.serialize=void 0;var Ut=Pn(),w=new Ut.Writer,so=r=>{w.addInt16(3).addInt16(0);for(let n of Object.keys(r))w.addCString(n).addCString(r[n]);w.addCString("client_encoding").addCString("UTF8");let e=w.addCString("").flush(),t=e.length+4;return new Ut.Writer().addInt32(t).add(e).flush()},io=()=>{let r=Buffer.allocUnsafe(8);return r.writeInt32BE(8,0),r.writeInt32BE(80877103,4),r},oo=r=>w.addCString(r).flush(112),ao=function(r,e){return w.addCString(r).addInt32(Buffer.byteLength(e)).addString(e),w.flush(112)},co=function(r){return w.addString(r).flush(112)},uo=r=>w.addCString(r).flush(81),An=[],lo=r=>{let e=r.name||"";e.length>63&&(console.error("Warning! Postgres only supports 63 characters for query names."),console.error("You supplied %s (%s)",e,e.length),console.error("This can cause conflicts and silent errors executing queries"));let t=r.types||An,n=t.length,s=w.addCString(e).addCString(r.text).addInt16(n);for(let i=0;i<n;i++)s.addInt32(t[i]);return w.flush(80)},re=new Ut.Writer,ho=function(r,e){for(let t=0;t<r.length;t++){let n=e?e(r[t],t):r[t];n==null?(w.addInt16(0),re.addInt32(-1)):n instanceof Buffer?(w.addInt16(1),re.addInt32(n.length),re.add(n)):(w.addInt16(0),re.addInt32(Buffer.byteLength(n)),re.addString(n))}},fo=(r={})=>{let e=r.portal||"",t=r.statement||"",n=r.binary||!1,s=r.values||An,i=s.length;return w.addCString(e).addCString(t),w.addInt16(i),ho(s,r.valueMapper),w.addInt16(i),w.add(re.flush()),w.addInt16(1),w.addInt16(n?1:0),w.flush(66)},po=Buffer.from([69,0,0,0,9,0,0,0,0,0]),mo=r=>{if(!r||!r.portal&&!r.rows)return po;let e=r.portal||"",t=r.rows||0,n=Buffer.byteLength(e),s=4+n+1+4,i=Buffer.allocUnsafe(1+s);return i[0]=69,i.writeInt32BE(s,1),i.write(e,5,"utf-8"),i[n+5]=0,i.writeUInt32BE(t,i.length-4),i},yo=(r,e)=>{let t=Buffer.allocUnsafe(16);return t.writeInt32BE(16,0),t.writeInt16BE(1234,4),t.writeInt16BE(5678,6),t.writeInt32BE(r,8),t.writeInt32BE(e,12),t},jt=(r,e)=>{let n=4+Buffer.byteLength(e)+1,s=Buffer.allocUnsafe(1+n);return s[0]=r,s.writeInt32BE(n,1),s.write(e,5,"utf-8"),s[n]=0,s},go=w.addCString("P").flush(68),bo=w.addCString("S").flush(68),wo=r=>r.name?jt(68,`${r.type}${r.name||""}`):r.type==="P"?go:bo,So=r=>{let e=`${r.type}${r.name||""}`;return jt(67,e)},vo=r=>w.add(r).flush(100),Eo=r=>jt(102,r),Le=r=>Buffer.from([r,0,0,0,4]),_o=Le(72),ko=Le(83),Co=Le(88),xo=Le(99),Po={startup:so,password:oo,requestSsl:io,sendSASLInitialResponseMessage:ao,sendSCRAMClientFinalMessage:co,query:uo,parse:lo,bind:fo,execute:mo,describe:wo,close:So,flush:()=>_o,sync:()=>ko,end:()=>Co,copyData:vo,copyDone:()=>xo,copyFail:Eo,cancel:yo};De.serialize=Po});var Tn=g(Qe=>{"use strict";u();Object.defineProperty(Qe,"__esModule",{value:!0});Qe.BufferReader=void 0;var $t=class{constructor(e=0){this.offset=e,this.buffer=Buffer.allocUnsafe(0),this.encoding="utf-8"}setBuffer(e,t){this.offset=e,this.buffer=t}int16(){let e=this.buffer.readInt16BE(this.offset);return this.offset+=2,e}byte(){let e=this.buffer[this.offset];return this.offset++,e}int32(){let e=this.buffer.readInt32BE(this.offset);return this.offset+=4,e}uint32(){let e=this.buffer.readUInt32BE(this.offset);return this.offset+=4,e}string(e){let t=this.buffer.toString(this.encoding,this.offset,this.offset+e);return this.offset+=e,t}cstring(){let e=this.offset,t=e;for(;this.buffer[t++]!==0;);return this.offset=t,this.buffer.toString(this.encoding,e,t-1)}bytes(e){let t=this.buffer.slice(this.offset,this.offset+e);return this.offset+=e,t}};Qe.BufferReader=$t});var qn=g(Ne=>{"use strict";u();Object.defineProperty(Ne,"__esModule",{value:!0});Ne.Parser=void 0;var v=Bt(),Ao=Tn(),zt=1,Ro=4,In=zt+Ro,T=-1,Ht=Buffer.allocUnsafe(0),Kt=class{constructor(e){if(this.buffer=Ht,this.bufferLength=0,this.bufferOffset=0,this.reader=new Ao.BufferReader,e?.mode==="binary")throw new Error("Binary mode not supported yet");this.mode=e?.mode||"text"}parse(e,t){this.mergeBuffer(e);let n=this.bufferOffset+this.bufferLength,s=this.bufferOffset;for(;s+In<=n;){let i=this.buffer[s],o=this.buffer.readUInt32BE(s+zt),a=zt+o;if(a+s<=n){let l=this.handlePacket(s+In,i,o,this.buffer);t(l),s+=a}else break}s===n?(this.buffer=Ht,this.bufferLength=0,this.bufferOffset=0):(this.bufferLength=n-s,this.bufferOffset=s)}mergeBuffer(e){if(this.bufferLength>0){let t=this.bufferLength+e.byteLength;if(t+this.bufferOffset>this.buffer.byteLength){let s;if(t<=this.buffer.byteLength&&this.bufferOffset>=this.bufferLength)s=this.buffer;else{let i=this.buffer.byteLength*2;for(;t>=i;)i*=2;s=Buffer.allocUnsafe(i)}this.buffer.copy(s,0,this.bufferOffset,this.bufferOffset+this.bufferLength),this.buffer=s,this.bufferOffset=0}e.copy(this.buffer,this.bufferOffset+this.bufferLength),this.bufferLength=t}else this.buffer=e,this.bufferOffset=0,this.bufferLength=e.byteLength}handlePacket(e,t,n,s){let{reader:i}=this;i.setBuffer(e,s);let o;switch(t){case 50:o=v.bindComplete;break;case 49:o=v.parseComplete;break;case 51:o=v.closeComplete;break;case 110:o=v.noData;break;case 115:o=v.portalSuspended;break;case 99:o=v.copyDone;break;case 87:o=v.replicationStart;break;case 73:o=v.emptyQuery;break;case 68:o=Bo(i);break;case 67:o=Io(i);break;case 90:o=To(i);break;case 65:o=Lo(i);break;case 82:o=jo(i,n);break;case 83:o=Fo(i);break;case 75:o=Uo(i);break;case 69:o=On(i,"error");break;case 78:o=On(i,"notice");break;case 84:o=Do(i);break;case 116:o=No(i);break;case 71:o=Mo(i);break;case 72:o=qo(i);break;case 100:o=Oo(i,n);break;default:return new v.DatabaseError("received invalid response: "+t.toString(16),n,"error")}return i.setBuffer(0,Ht),o.length=n,o}};Ne.Parser=Kt;var To=r=>{let e=r.string(1);return new v.ReadyForQueryMessage(T,e)},Io=r=>{let e=r.cstring();return new v.CommandCompleteMessage(T,e)},Oo=(r,e)=>{let t=r.bytes(e-4);return new v.CopyDataMessage(T,t)},Mo=r=>Mn(r,"copyInResponse"),qo=r=>Mn(r,"copyOutResponse"),Mn=(r,e)=>{let t=r.byte()!==0,n=r.int16(),s=new v.CopyResponse(T,e,t,n);for(let i=0;i<n;i++)s.columnTypes[i]=r.int16();return s},Lo=r=>{let e=r.int32(),t=r.cstring(),n=r.cstring();return new v.NotificationResponseMessage(T,e,t,n)},Do=r=>{let e=r.int16(),t=new v.RowDescriptionMessage(T,e);for(let n=0;n<e;n++)t.fields[n]=Qo(r);return t},Qo=r=>{let e=r.cstring(),t=r.uint32(),n=r.int16(),s=r.uint32(),i=r.int16(),o=r.int32(),a=r.int16()===0?"text":"binary";return new v.Field(e,t,n,s,i,o,a)},No=r=>{let e=r.int16(),t=new v.ParameterDescriptionMessage(T,e);for(let n=0;n<e;n++)t.dataTypeIDs[n]=r.int32();return t},Bo=r=>{let e=r.int16(),t=new Array(e);for(let n=0;n<e;n++){let s=r.int32();t[n]=s===-1?null:r.string(s)}return new v.DataRowMessage(T,t)},Fo=r=>{let e=r.cstring(),t=r.cstring();return new v.ParameterStatusMessage(T,e,t)},Uo=r=>{let e=r.int32(),t=r.int32();return new v.BackendKeyDataMessage(T,e,t)},jo=(r,e)=>{let t=r.int32(),n={name:"authenticationOk",length:e};switch(t){case 0:break;case 3:n.length===8&&(n.name="authenticationCleartextPassword");break;case 5:if(n.length===12){n.name="authenticationMD5Password";let s=r.bytes(4);return new v.AuthenticationMD5Password(T,s)}break;case 10:{n.name="authenticationSASL",n.mechanisms=[];let s;do s=r.cstring(),s&&n.mechanisms.push(s);while(s)}break;case 11:n.name="authenticationSASLContinue",n.data=r.string(e-8);break;case 12:n.name="authenticationSASLFinal",n.data=r.string(e-8);break;default:throw new Error("Unknown authenticationOk message type "+t)}return n},On=(r,e)=>{let t={},n=r.string(1);for(;n!=="\0";)t[n]=r.cstring(),n=r.string(1);let s=t.M,i=e==="notice"?new v.NoticeMessage(T,s):new v.DatabaseError(s,T,e);return i.severity=t.S,i.code=t.C,i.detail=t.D,i.hint=t.H,i.position=t.P,i.internalPosition=t.p,i.internalQuery=t.q,i.where=t.W,i.schema=t.s,i.table=t.t,i.column=t.c,i.dataType=t.d,i.constraint=t.n,i.file=t.F,i.line=t.L,i.routine=t.R,i}});var Gt=g(U=>{"use strict";u();Object.defineProperty(U,"__esModule",{value:!0});U.DatabaseError=U.serialize=U.parse=void 0;var $o=Bt();Object.defineProperty(U,"DatabaseError",{enumerable:!0,get:function(){return $o.DatabaseError}});var Ho=Rn();Object.defineProperty(U,"serialize",{enumerable:!0,get:function(){return Ho.serialize}});var zo=qn();function Ko(r,e){let t=new zo.Parser;return r.on("data",n=>t.parse(n,e)),new Promise(n=>r.on("end",()=>n()))}U.parse=Ko});var Ln=g(Vt=>{"use strict";u();Object.defineProperty(Vt,"__esModule",{value:!0});Vt.default={}});var Qn=g((hl,Dn)=>{"use strict";u();var{getStream:Go,getSecureStream:Vo}=Xo();Dn.exports={getStream:Go,getSecureStream:Vo};function Wo(){function r(t){let n=require("net");return new n.Socket}function e(t){return require("tls").connect(t)}return{getStream:r,getSecureStream:e}}function Yo(){function r(t){let{CloudflareSocket:n}=Ln();return new n(t)}function e(t){return t.socket.startTls(t),t.socket}return{getStream:r,getSecureStream:e}}function Jo(){if(typeof navigator=="object"&&navigator!==null&&typeof navigator.userAgent=="string")return navigator.userAgent==="Cloudflare-Workers";if(typeof Response=="function"){let r=new Response(null,{cf:{thing:!0}});if(typeof r.cf=="object"&&r.cf!==null&&r.cf.thing)return!0}return!1}function Xo(){return Jo()?Yo():Wo()}});var Yt=g((fl,Nn)=>{"use strict";u();var Zo=require("events").EventEmitter,{parse:ea,serialize:k}=Gt(),{getStream:ta,getSecureStream:ra}=Qn(),na=k.flush(),sa=k.sync(),ia=k.end(),Wt=class extends Zo{constructor(e){super(),e=e||{},this.stream=e.stream||ta(e.ssl),typeof this.stream=="function"&&(this.stream=this.stream(e)),this._keepAlive=e.keepAlive,this._keepAliveInitialDelayMillis=e.keepAliveInitialDelayMillis,this.parsedStatements={},this.ssl=e.ssl||!1,this._ending=!1,this._emitMessage=!1;let t=this;this.on("newListener",function(n){n==="message"&&(t._emitMessage=!0)})}connect(e,t){let n=this;this._connecting=!0,this.stream.setNoDelay(!0),this.stream.connect(e,t),this.stream.once("connect",function(){n._keepAlive&&n.stream.setKeepAlive(!0,n._keepAliveInitialDelayMillis),n.emit("connect")});let s=function(i){n._ending&&(i.code==="ECONNRESET"||i.code==="EPIPE")||n.emit("error",i)};if(this.stream.on("error",s),this.stream.on("close",function(){n.emit("end")}),!this.ssl)return this.attachListeners(this.stream);this.stream.once("data",function(i){switch(i.toString("utf8")){case"S":break;case"N":return n.stream.end(),n.emit("error",new Error("The server does not support SSL connections"));default:return n.stream.end(),n.emit("error",new Error("There was an error establishing an SSL connection"))}let a={socket:n.stream};n.ssl!==!0&&(Object.assign(a,n.ssl),"key"in n.ssl&&(a.key=n.ssl.key));let l=require("net");l.isIP&&l.isIP(t)===0&&(a.servername=t);try{n.stream=ra(a)}catch(c){return n.emit("error",c)}n.attachListeners(n.stream),n.stream.on("error",s),n.emit("sslconnect")})}attachListeners(e){ea(e,t=>{let n=t.name==="error"?"errorMessage":t.name;this._emitMessage&&this.emit("message",t),this.emit(n,t)})}requestSsl(){this.stream.write(k.requestSsl())}startup(e){this.stream.write(k.startup(e))}cancel(e,t){this._send(k.cancel(e,t))}password(e){this._send(k.password(e))}sendSASLInitialResponseMessage(e,t){this._send(k.sendSASLInitialResponseMessage(e,t))}sendSCRAMClientFinalMessage(e){this._send(k.sendSCRAMClientFinalMessage(e))}_send(e){return this.stream.writable?this.stream.write(e):!1}query(e){this._send(k.query(e))}parse(e){this._send(k.parse(e))}bind(e){this._send(k.bind(e))}execute(e){this._send(k.execute(e))}flush(){this.stream.writable&&this.stream.write(na)}sync(){this._ending=!0,this._send(sa)}ref(){this.stream.ref()}unref(){this.stream.unref()}end(){if(this._ending=!0,!this._connecting||!this.stream.writable){this.stream.end();return}return this.stream.write(ia,()=>{this.stream.end()})}close(e){this._send(k.close(e))}describe(e){this._send(k.describe(e))}sendCopyFromChunk(e){this._send(k.copyData(e))}endCopyFrom(){this._send(k.copyDone())}sendCopyFail(e){this._send(k.copyFail(e))}};Nn.exports=Wt});var jn=g((ml,Un)=>{"use strict";u();var{Transform:oa}=require("stream"),{StringDecoder:aa}=require("string_decoder"),j=Symbol("last"),Be=Symbol("decoder");function ca(r,e,t){let n;if(this.overflow){if(n=this[Be].write(r).split(this.matcher),n.length===1)return t();n.shift(),this.overflow=!1}else this[j]+=this[Be].write(r),n=this[j].split(this.matcher);this[j]=n.pop();for(let s=0;s<n.length;s++)try{Fn(this,this.mapper(n[s]))}catch(i){return t(i)}if(this.overflow=this[j].length>this.maxLength,this.overflow&&!this.skipOverflow){t(new Error("maximum buffer reached"));return}t()}function ua(r){if(this[j]+=this[Be].end(),this[j])try{Fn(this,this.mapper(this[j]))}catch(e){return r(e)}r()}function Fn(r,e){e!==void 0&&r.push(e)}function Bn(r){return r}function la(r,e,t){switch(r=r||/\r?\n/,e=e||Bn,t=t||{},arguments.length){case 1:typeof r=="function"?(e=r,r=/\r?\n/):typeof r=="object"&&!(r instanceof RegExp)&&!r[Symbol.split]&&(t=r,r=/\r?\n/);break;case 2:typeof r=="function"?(t=e,e=r,r=/\r?\n/):typeof e=="object"&&(t=e,e=Bn)}t=Object.assign({},t),t.autoDestroy=!0,t.transform=ca,t.flush=ua,t.readableObjectMode=!0;let n=new oa(t);return n[j]="",n[Be]=new aa("utf8"),n.matcher=r,n.mapper=e,n.maxLength=t.maxLength,n.skipOverflow=t.skipOverflow||!1,n.overflow=!1,n._destroy=function(s,i){this._writableState.errorEmitted=!1,i(s)},n}Un.exports=la});var zn=g((gl,L)=>{"use strict";u();var $n=require("path"),ha=require("stream").Stream,da=jn(),Hn=require("util"),fa=5432,Fe=process.platform==="win32",ye=process.stderr,pa=56,ma=7,ya=61440,ga=32768;function ba(r){return(r&ya)==ga}var ne=["host","port","database","user","password"],Jt=ne.length,wa=ne[Jt-1];function Xt(){var r=ye instanceof ha&&ye.writable===!0;if(r){var e=Array.prototype.slice.call(arguments).concat(`
9
+ `);ye.write(Hn.format.apply(Hn,e))}}Object.defineProperty(L.exports,"isWin",{get:function(){return Fe},set:function(r){Fe=r}});L.exports.warnTo=function(r){var e=ye;return ye=r,e};L.exports.getFileName=function(r){var e=r||process.env,t=e.PGPASSFILE||(Fe?$n.join(e.APPDATA||"./","postgresql","pgpass.conf"):$n.join(e.HOME||"./",".pgpass"));return t};L.exports.usePgPass=function(r,e){return Object.prototype.hasOwnProperty.call(process.env,"PGPASSWORD")?!1:Fe?!0:(e=e||"<unkn>",ba(r.mode)?r.mode&(pa|ma)?(Xt('WARNING: password file "%s" has group or world access; permissions should be u=rw (0600) or less',e),!1):!0:(Xt('WARNING: password file "%s" is not a plain file',e),!1))};var Sa=L.exports.match=function(r,e){return ne.slice(0,-1).reduce(function(t,n,s){return s==1&&Number(r[n]||fa)===Number(e[n])?t&&!0:t&&(e[n]==="*"||e[n]===r[n])},!0)};L.exports.getPassword=function(r,e,t){var n,s=e.pipe(da());function i(l){var c=va(l);c&&Ea(c)&&Sa(r,c)&&(n=c[wa],s.end())}var o=function(){e.destroy(),t(n)},a=function(l){e.destroy(),Xt("WARNING: error on reading file: %s",l),t(void 0)};e.on("error",a),s.on("data",i).on("end",o).on("error",a)};var va=L.exports.parseLine=function(r){if(r.length<11||r.match(/^\s+#/))return null;for(var e="",t="",n=0,s=0,i=0,o={},a=!1,l=function(h,d,p){var m=r.substring(d,p);Object.hasOwnProperty.call(process.env,"PGPASS_NO_DEESCAPE")||(m=m.replace(/\\([:\\])/g,"$1")),o[ne[h]]=m},c=0;c<r.length-1;c+=1){if(e=r.charAt(c+1),t=r.charAt(c),a=n==Jt-1,a){l(n,s);break}c>=0&&e==":"&&t!=="\\"&&(l(n,s,c+1),s=c+2,n+=1)}return o=Object.keys(o).length===Jt?o:null,o},Ea=L.exports.isValidEntry=function(r){for(var e={0:function(o){return o.length>0},1:function(o){return o==="*"?!0:(o=Number(o),isFinite(o)&&o>0&&o<9007199254740992&&Math.floor(o)===o)},2:function(o){return o.length>0},3:function(o){return o.length>0},4:function(o){return o.length>0}},t=0;t<ne.length;t+=1){var n=e[t],s=r[ne[t]]||"",i=n(s);if(!i)return!1}return!0}});var Gn=g((Sl,Zt)=>{"use strict";u();var wl=require("path"),Kn=require("fs"),Ue=zn();Zt.exports=function(r,e){var t=Ue.getFileName();Kn.stat(t,function(n,s){if(n||!Ue.usePgPass(s,t))return e(void 0);var i=Kn.createReadStream(t);Ue.getPassword(r,i,e)})};Zt.exports.warnTo=Ue.warnTo});var Xn=g((El,Jn)=>{"use strict";u();var _a=require("events").EventEmitter,Vn=X(),ge=require("util"),er=fn(),ka=Me(),Ca=vt(),Yn=xn(),xa=pe(),Pa=Yt(),Aa=gt(),Wn=ge.deprecate(()=>{},"Client.activeQuery is deprecated and will be removed in pg@9.0"),Ra=ge.deprecate(()=>{},"Client.queryQueue is deprecated and will be removed in pg@9.0."),Ta=ge.deprecate(()=>{},"pgpass support is deprecated and will be removed in pg@9.0. You can provide an async function as the password property to the Client/Pool constructor that returns a password instead. Within this function you can call the pgpass module in your own code."),Ia=ge.deprecate(()=>{},"Passing a custom Promise implementation to the Client/Pool constructor is deprecated and will be removed in pg@9.0."),Oa=ge.deprecate(()=>{},"Calling client.query() when the client is already executing a query is deprecated and will be removed in pg@9.0. Use async/await or an external async flow control mechanism instead."),je=class extends _a{constructor(e){super(),this.connectionParameters=new Ca(e),this.user=this.connectionParameters.user,this.database=this.connectionParameters.database,this.port=this.connectionParameters.port,this.host=this.connectionParameters.host,Object.defineProperty(this,"password",{configurable:!0,enumerable:!1,writable:!0,value:this.connectionParameters.password}),this.replication=this.connectionParameters.replication;let t=e||{};t.Promise&&Ia(),this._Promise=t.Promise||global.Promise,this._types=new ka(t.types),this._ending=!1,this._ended=!1,this._connecting=!1,this._connected=!1,this._connectionError=!1,this._queryable=!0,this._activeQuery=null,this.enableChannelBinding=!!t.enableChannelBinding,this.connection=t.connection||new Pa({stream:t.stream,ssl:this.connectionParameters.ssl,keepAlive:t.keepAlive||!1,keepAliveInitialDelayMillis:t.keepAliveInitialDelayMillis||0,encoding:this.connectionParameters.client_encoding||"utf8"}),this._queryQueue=[],this.binary=t.binary||xa.binary,this.processID=null,this.secretKey=null,this.ssl=this.connectionParameters.ssl||!1,this.ssl&&this.ssl.key&&Object.defineProperty(this.ssl,"key",{enumerable:!1}),this._connectionTimeoutMillis=t.connectionTimeoutMillis||0}get activeQuery(){return Wn(),this._activeQuery}set activeQuery(e){Wn(),this._activeQuery=e}_getActiveQuery(){return this._activeQuery}_errorAllQueries(e){let t=s=>{process.nextTick(()=>{s.handleError(e,this.connection)})},n=this._getActiveQuery();n&&(t(n),this._activeQuery=null),this._queryQueue.forEach(t),this._queryQueue.length=0}_connect(e){let t=this,n=this.connection;if(this._connectionCallback=e,this._connecting||this._connected){let s=new Error("Client has already been connected. You cannot reuse a client.");process.nextTick(()=>{e(s)});return}this._connecting=!0,this._connectionTimeoutMillis>0&&(this.connectionTimeoutHandle=setTimeout(()=>{n._ending=!0,n.stream.destroy(new Error("timeout expired"))},this._connectionTimeoutMillis),this.connectionTimeoutHandle.unref&&this.connectionTimeoutHandle.unref()),this.host&&this.host.indexOf("/")===0?n.connect(this.host+"/.s.PGSQL."+this.port):n.connect(this.port,this.host),n.on("connect",function(){t.ssl?n.requestSsl():n.startup(t.getStartupConf())}),n.on("sslconnect",function(){n.startup(t.getStartupConf())}),this._attachListeners(n),n.once("end",()=>{let s=this._ending?new Error("Connection terminated"):new Error("Connection terminated unexpectedly");clearTimeout(this.connectionTimeoutHandle),this._errorAllQueries(s),this._ended=!0,this._ending||(this._connecting&&!this._connectionError?this._connectionCallback?this._connectionCallback(s):this._handleErrorEvent(s):this._connectionError||this._handleErrorEvent(s)),process.nextTick(()=>{this.emit("end")})})}connect(e){if(e){this._connect(e);return}return new this._Promise((t,n)=>{this._connect(s=>{s?n(s):t(this)})})}_attachListeners(e){e.on("authenticationCleartextPassword",this._handleAuthCleartextPassword.bind(this)),e.on("authenticationMD5Password",this._handleAuthMD5Password.bind(this)),e.on("authenticationSASL",this._handleAuthSASL.bind(this)),e.on("authenticationSASLContinue",this._handleAuthSASLContinue.bind(this)),e.on("authenticationSASLFinal",this._handleAuthSASLFinal.bind(this)),e.on("backendKeyData",this._handleBackendKeyData.bind(this)),e.on("error",this._handleErrorEvent.bind(this)),e.on("errorMessage",this._handleErrorMessage.bind(this)),e.on("readyForQuery",this._handleReadyForQuery.bind(this)),e.on("notice",this._handleNotice.bind(this)),e.on("rowDescription",this._handleRowDescription.bind(this)),e.on("dataRow",this._handleDataRow.bind(this)),e.on("portalSuspended",this._handlePortalSuspended.bind(this)),e.on("emptyQuery",this._handleEmptyQuery.bind(this)),e.on("commandComplete",this._handleCommandComplete.bind(this)),e.on("parseComplete",this._handleParseComplete.bind(this)),e.on("copyInResponse",this._handleCopyInResponse.bind(this)),e.on("copyData",this._handleCopyData.bind(this)),e.on("notification",this._handleNotification.bind(this))}_getPassword(e){let t=this.connection;if(typeof this.password=="function")this._Promise.resolve().then(()=>this.password(this.connectionParameters)).then(n=>{if(n!==void 0){if(typeof n!="string"){t.emit("error",new TypeError("Password must be a string"));return}this.connectionParameters.password=this.password=n}else this.connectionParameters.password=this.password=null;e()}).catch(n=>{t.emit("error",n)});else if(this.password!==null)e();else try{Gn()(this.connectionParameters,s=>{s!==void 0&&(Ta(),this.connectionParameters.password=this.password=s),e()})}catch(n){this.emit("error",n)}}_handleAuthCleartextPassword(e){this._getPassword(()=>{this.connection.password(this.password)})}_handleAuthMD5Password(e){this._getPassword(async()=>{try{let t=await Aa.postgresMd5PasswordHash(this.user,this.password,e.salt);this.connection.password(t)}catch(t){this.emit("error",t)}})}_handleAuthSASL(e){this._getPassword(()=>{try{this.saslSession=er.startSession(e.mechanisms,this.enableChannelBinding&&this.connection.stream),this.connection.sendSASLInitialResponseMessage(this.saslSession.mechanism,this.saslSession.response)}catch(t){this.connection.emit("error",t)}})}async _handleAuthSASLContinue(e){try{await er.continueSession(this.saslSession,this.password,e.data,this.enableChannelBinding&&this.connection.stream),this.connection.sendSCRAMClientFinalMessage(this.saslSession.response)}catch(t){this.connection.emit("error",t)}}_handleAuthSASLFinal(e){try{er.finalizeSession(this.saslSession,e.data),this.saslSession=null}catch(t){this.connection.emit("error",t)}}_handleBackendKeyData(e){this.processID=e.processID,this.secretKey=e.secretKey}_handleReadyForQuery(e){this._connecting&&(this._connecting=!1,this._connected=!0,clearTimeout(this.connectionTimeoutHandle),this._connectionCallback&&(this._connectionCallback(null,this),this._connectionCallback=null),this.emit("connect"));let t=this._getActiveQuery();this._activeQuery=null,this.readyForQuery=!0,t&&t.handleReadyForQuery(this.connection),this._pulseQueryQueue()}_handleErrorWhileConnecting(e){if(!this._connectionError){if(this._connectionError=!0,clearTimeout(this.connectionTimeoutHandle),this._connectionCallback)return this._connectionCallback(e);this.emit("error",e)}}_handleErrorEvent(e){if(this._connecting)return this._handleErrorWhileConnecting(e);this._queryable=!1,this._errorAllQueries(e),this.emit("error",e)}_handleErrorMessage(e){if(this._connecting)return this._handleErrorWhileConnecting(e);let t=this._getActiveQuery();if(!t){this._handleErrorEvent(e);return}this._activeQuery=null,t.handleError(e,this.connection)}_handleRowDescription(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected rowDescription message from backend.");this._handleErrorEvent(n);return}t.handleRowDescription(e)}_handleDataRow(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected dataRow message from backend.");this._handleErrorEvent(n);return}t.handleDataRow(e)}_handlePortalSuspended(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected portalSuspended message from backend.");this._handleErrorEvent(n);return}t.handlePortalSuspended(this.connection)}_handleEmptyQuery(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected emptyQuery message from backend.");this._handleErrorEvent(n);return}t.handleEmptyQuery(this.connection)}_handleCommandComplete(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected commandComplete message from backend.");this._handleErrorEvent(n);return}t.handleCommandComplete(e,this.connection)}_handleParseComplete(){let e=this._getActiveQuery();if(e==null){let t=new Error("Received unexpected parseComplete message from backend.");this._handleErrorEvent(t);return}e.name&&(this.connection.parsedStatements[e.name]=e.text)}_handleCopyInResponse(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected copyInResponse message from backend.");this._handleErrorEvent(n);return}t.handleCopyInResponse(this.connection)}_handleCopyData(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected copyData message from backend.");this._handleErrorEvent(n);return}t.handleCopyData(e,this.connection)}_handleNotification(e){this.emit("notification",e)}_handleNotice(e){this.emit("notice",e)}getStartupConf(){let e=this.connectionParameters,t={user:e.user,database:e.database},n=e.application_name||e.fallback_application_name;return n&&(t.application_name=n),e.replication&&(t.replication=""+e.replication),e.statement_timeout&&(t.statement_timeout=String(parseInt(e.statement_timeout,10))),e.lock_timeout&&(t.lock_timeout=String(parseInt(e.lock_timeout,10))),e.idle_in_transaction_session_timeout&&(t.idle_in_transaction_session_timeout=String(parseInt(e.idle_in_transaction_session_timeout,10))),e.options&&(t.options=e.options),t}cancel(e,t){if(e.activeQuery===t){let n=this.connection;this.host&&this.host.indexOf("/")===0?n.connect(this.host+"/.s.PGSQL."+this.port):n.connect(this.port,this.host),n.on("connect",function(){n.cancel(e.processID,e.secretKey)})}else e._queryQueue.indexOf(t)!==-1&&e._queryQueue.splice(e._queryQueue.indexOf(t),1)}setTypeParser(e,t,n){return this._types.setTypeParser(e,t,n)}getTypeParser(e,t){return this._types.getTypeParser(e,t)}escapeIdentifier(e){return Vn.escapeIdentifier(e)}escapeLiteral(e){return Vn.escapeLiteral(e)}_pulseQueryQueue(){if(this.readyForQuery===!0){this._activeQuery=this._queryQueue.shift();let e=this._getActiveQuery();if(e){this.readyForQuery=!1,this.hasExecuted=!0;let t=e.submit(this.connection);t&&process.nextTick(()=>{e.handleError(t,this.connection),this.readyForQuery=!0,this._pulseQueryQueue()})}else this.hasExecuted&&(this._activeQuery=null,this.emit("drain"))}}query(e,t,n){let s,i,o,a,l;if(e==null)throw new TypeError("Client was passed a null or undefined query");return typeof e.submit=="function"?(o=e.query_timeout||this.connectionParameters.query_timeout,i=s=e,s.callback||(typeof t=="function"?s.callback=t:n&&(s.callback=n))):(o=e.query_timeout||this.connectionParameters.query_timeout,s=new Yn(e,t,n),s.callback||(i=new this._Promise((c,h)=>{s.callback=(d,p)=>d?h(d):c(p)}).catch(c=>{throw Error.captureStackTrace(c),c}))),o&&(l=s.callback||(()=>{}),a=setTimeout(()=>{let c=new Error("Query read timeout");process.nextTick(()=>{s.handleError(c,this.connection)}),l(c),s.callback=()=>{};let h=this._queryQueue.indexOf(s);h>-1&&this._queryQueue.splice(h,1),this._pulseQueryQueue()},o),s.callback=(c,h)=>{clearTimeout(a),l(c,h)}),this.binary&&!s.binary&&(s.binary=!0),s._result&&!s._result._types&&(s._result._types=this._types),this._queryable?this._ending?(process.nextTick(()=>{s.handleError(new Error("Client was closed and is not queryable"),this.connection)}),i):(this._queryQueue.length>0&&Oa(),this._queryQueue.push(s),this._pulseQueryQueue(),i):(process.nextTick(()=>{s.handleError(new Error("Client has encountered a connection error and is not queryable"),this.connection)}),i)}ref(){this.connection.ref()}unref(){this.connection.unref()}end(e){if(this._ending=!0,!this.connection._connecting||this._ended)if(e)e();else return this._Promise.resolve();if(this._getActiveQuery()||!this._queryable?this.connection.stream.destroy():this.connection.end(),e)this.connection.once("end",e);else return new this._Promise(t=>{this.connection.once("end",t)})}get queryQueue(){return Ra(),this._queryQueue}};je.Query=Yn;Jn.exports=je});var ts=g((kl,es)=>{"use strict";u();var Ma=require("events").EventEmitter,tr=function(){},Zn=(r,e)=>{let t=r.findIndex(e);return t===-1?void 0:r.splice(t,1)[0]},rr=class{constructor(e,t,n){this.client=e,this.idleListener=t,this.timeoutId=n}},se=class{constructor(e){this.callback=e}};function qa(){throw new Error("Release called on client which has already been released to the pool.")}function $e(r,e){if(e)return{callback:e,result:void 0};let t,n,s=function(o,a){o?t(o):n(a)},i=new r(function(o,a){n=o,t=a}).catch(o=>{throw Error.captureStackTrace(o),o});return{callback:s,result:i}}function La(r,e){return function t(n){n.client=e,e.removeListener("error",t),e.on("error",()=>{r.log("additional client error after disconnection due to error",n)}),r._remove(e),r.emit("error",n,e)}}var nr=class extends Ma{constructor(e,t){super(),this.options=Object.assign({},e),e!=null&&"password"in e&&Object.defineProperty(this.options,"password",{configurable:!0,enumerable:!1,writable:!0,value:e.password}),e!=null&&e.ssl&&e.ssl.key&&Object.defineProperty(this.options.ssl,"key",{enumerable:!1}),this.options.max=this.options.max||this.options.poolSize||10,this.options.min=this.options.min||0,this.options.maxUses=this.options.maxUses||1/0,this.options.allowExitOnIdle=this.options.allowExitOnIdle||!1,this.options.maxLifetimeSeconds=this.options.maxLifetimeSeconds||0,this.log=this.options.log||function(){},this.Client=this.options.Client||t||sr().Client,this.Promise=this.options.Promise||global.Promise,typeof this.options.idleTimeoutMillis>"u"&&(this.options.idleTimeoutMillis=1e4),this._clients=[],this._idle=[],this._expired=new WeakSet,this._pendingQueue=[],this._endCallback=void 0,this.ending=!1,this.ended=!1}_promiseTry(e){let t=this.Promise;return typeof t.try=="function"?t.try(e):new t(n=>n(e()))}_isFull(){return this._clients.length>=this.options.max}_isAboveMin(){return this._clients.length>this.options.min}_pulseQueue(){if(this.log("pulse queue"),this.ended){this.log("pulse queue ended");return}if(this.ending){this.log("pulse queue on ending"),this._idle.length&&this._idle.slice().map(t=>{this._remove(t.client)}),this._clients.length||(this.ended=!0,this._endCallback());return}if(!this._pendingQueue.length){this.log("no queued requests");return}if(!this._idle.length&&this._isFull())return;let e=this._pendingQueue.shift();if(this._idle.length){let t=this._idle.pop();clearTimeout(t.timeoutId);let n=t.client;n.ref&&n.ref();let s=t.idleListener;return this._acquireClient(n,e,s,!1)}if(!this._isFull())return this.newClient(e);throw new Error("unexpected condition")}_remove(e,t){let n=Zn(this._idle,i=>i.client===e);n!==void 0&&clearTimeout(n.timeoutId),this._clients=this._clients.filter(i=>i!==e);let s=this;e.end(()=>{s.emit("remove",e),typeof t=="function"&&t()})}connect(e){if(this.ending){let s=new Error("Cannot use a pool after calling end on the pool");return e?e(s):this.Promise.reject(s)}let t=$e(this.Promise,e),n=t.result;if(this._isFull()||this._idle.length){if(this._idle.length&&process.nextTick(()=>this._pulseQueue()),!this.options.connectionTimeoutMillis)return this._pendingQueue.push(new se(t.callback)),n;let s=(a,l,c)=>{clearTimeout(o),t.callback(a,l,c)},i=new se(s),o=setTimeout(()=>{Zn(this._pendingQueue,a=>a.callback===s),i.timedOut=!0,t.callback(new Error("timeout exceeded when trying to connect"))},this.options.connectionTimeoutMillis);return o.unref&&o.unref(),this._pendingQueue.push(i),n}return this.newClient(new se(t.callback)),n}newClient(e){let t=new this.Client(this.options);this._clients.push(t);let n=La(this,t);this.log("checking client timeout");let s,i=!1;this.options.connectionTimeoutMillis&&(s=setTimeout(()=>{t.connection?(this.log("ending client due to timeout"),i=!0,t.connection.stream.destroy()):t.isConnected()||(this.log("ending client due to timeout"),i=!0,t.end())},this.options.connectionTimeoutMillis)),this.log("connecting new client"),t.connect(o=>{if(s&&clearTimeout(s),t.on("error",n),o)this.log("client failed to connect",o),this._clients=this._clients.filter(a=>a!==t),i&&(o=new Error("Connection terminated due to connection timeout",{cause:o})),this._pulseQueue(),e.timedOut||e.callback(o,void 0,tr);else{if(this.log("new client connected"),this.options.onConnect){this._promiseTry(()=>this.options.onConnect(t)).then(()=>{this._afterConnect(t,e,n)},a=>{this._clients=this._clients.filter(l=>l!==t),t.end(()=>{this._pulseQueue(),e.timedOut||e.callback(a,void 0,tr)})});return}return this._afterConnect(t,e,n)}})}_afterConnect(e,t,n){if(this.options.maxLifetimeSeconds!==0){let s=setTimeout(()=>{this.log("ending client due to expired lifetime"),this._expired.add(e),this._idle.findIndex(o=>o.client===e)!==-1&&this._acquireClient(e,new se((o,a,l)=>l()),n,!1)},this.options.maxLifetimeSeconds*1e3);s.unref(),e.once("end",()=>clearTimeout(s))}return this._acquireClient(e,t,n,!0)}_acquireClient(e,t,n,s){s&&this.emit("connect",e),this.emit("acquire",e),e.release=this._releaseOnce(e,n),e.removeListener("error",n),t.timedOut?s&&this.options.verify?this.options.verify(e,e.release):e.release():s&&this.options.verify?this.options.verify(e,i=>{if(i)return e.release(i),t.callback(i,void 0,tr);t.callback(void 0,e,e.release)}):t.callback(void 0,e,e.release)}_releaseOnce(e,t){let n=!1;return s=>{n&&qa(),n=!0,this._release(e,t,s)}}_release(e,t,n){if(e.on("error",t),e._poolUseCount=(e._poolUseCount||0)+1,this.emit("release",n,e),n||this.ending||!e._queryable||e._ending||e._poolUseCount>=this.options.maxUses)return e._poolUseCount>=this.options.maxUses&&this.log("remove expended client"),this._remove(e,this._pulseQueue.bind(this));if(this._expired.has(e))return this.log("remove expired client"),this._expired.delete(e),this._remove(e,this._pulseQueue.bind(this));let i;this.options.idleTimeoutMillis&&this._isAboveMin()&&(i=setTimeout(()=>{this._isAboveMin()&&(this.log("remove idle client"),this._remove(e,this._pulseQueue.bind(this)))},this.options.idleTimeoutMillis),this.options.allowExitOnIdle&&i.unref()),this.options.allowExitOnIdle&&e.unref(),this._idle.push(new rr(e,t,i)),this._pulseQueue()}query(e,t,n){if(typeof e=="function"){let i=$e(this.Promise,e);return setImmediate(function(){return i.callback(new Error("Passing a function as the first parameter to pool.query is not supported"))}),i.result}typeof t=="function"&&(n=t,t=void 0);let s=$e(this.Promise,n);return n=s.callback,this.connect((i,o)=>{if(i)return n(i);let a=!1,l=c=>{a||(a=!0,o.release(c),n(c))};o.once("error",l),this.log("dispatching query");try{o.query(e,t,(c,h)=>{if(this.log("query dispatched"),o.removeListener("error",l),!a)return a=!0,o.release(c),c?n(c):n(void 0,h)})}catch(c){return o.release(c),n(c)}}),s.result}end(e){if(this.log("ending"),this.ending){let n=new Error("Called end on pool more than once");return e?e(n):this.Promise.reject(n)}this.ending=!0;let t=$e(this.Promise,e);return this._endCallback=t.callback,this._pulseQueue(),t.result}get waitingCount(){return this._pendingQueue.length}get idleCount(){return this._idle.length}get expiredCount(){return this._clients.reduce((e,t)=>e+(this._expired.has(t)?1:0),0)}get totalCount(){return this._clients.length}};es.exports=nr});var ss=g((xl,ns)=>{"use strict";u();var rs=require("events").EventEmitter,Da=require("util"),ir=X(),ie=ns.exports=function(r,e,t){rs.call(this),r=ir.normalizeQueryConfig(r,e,t),this.text=r.text,this.values=r.values,this.name=r.name,this.queryMode=r.queryMode,this.callback=r.callback,this.state="new",this._arrayMode=r.rowMode==="array",this._emitRowEvents=!1,this.on("newListener",function(n){n==="row"&&(this._emitRowEvents=!0)}.bind(this))};Da.inherits(ie,rs);var Qa={sqlState:"code",statementPosition:"position",messagePrimary:"message",context:"where",schemaName:"schema",tableName:"table",columnName:"column",dataTypeName:"dataType",constraintName:"constraint",sourceFile:"file",sourceLine:"line",sourceFunction:"routine"};ie.prototype.handleError=function(r){let e=this.native.pq.resultErrorFields();if(e)for(let t in e){let n=Qa[t]||t;r[n]=e[t]}this.callback?this.callback(r):this.emit("error",r),this.state="error"};ie.prototype.then=function(r,e){return this._getPromise().then(r,e)};ie.prototype.catch=function(r){return this._getPromise().catch(r)};ie.prototype._getPromise=function(){return this._promise?this._promise:(this._promise=new Promise(function(r,e){this._once("end",r),this._once("error",e)}.bind(this)),this._promise)};ie.prototype.submit=function(r){this.state="running";let e=this;this.native=r.native,r.native.arrayMode=this._arrayMode;let t=function(n,s,i){if(r.native.arrayMode=!1,setImmediate(function(){e.emit("_done")}),n)return e.handleError(n);e._emitRowEvents&&(i.length>1?s.forEach((o,a)=>{o.forEach(l=>{e.emit("row",l,i[a])})}):s.forEach(function(o){e.emit("row",o,i)})),e.state="end",e.emit("end",i),e.callback&&e.callback(null,i)};if(process.domain&&(t=process.domain.bind(t)),this.name){this.name.length>63&&(console.error("Warning! Postgres only supports 63 characters for query names."),console.error("You supplied %s (%s)",this.name,this.name.length),console.error("This can cause conflicts and silent errors executing queries"));let n=(this.values||[]).map(ir.prepareValue);if(r.namedQueries[this.name]){if(this.text&&r.namedQueries[this.name]!==this.text){let s=new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`);return t(s)}return r.native.execute(this.name,n,t)}return r.native.prepare(this.name,this.text,n.length,function(s){return s?t(s):(r.namedQueries[e.name]=e.text,e.native.execute(e.name,n,t))})}else if(this.values){if(!Array.isArray(this.values)){let s=new Error("Query values must be an array");return t(s)}let n=this.values.map(ir.prepareValue);r.native.query(this.text,n,t)}else this.queryMode==="extended"?r.native.query(this.text,[],t):r.native.query(this.text,t)}});var us=g((Al,cs)=>{"use strict";u();var Na=require("util"),is;try{is=require("pg-native")}catch(r){throw r}var Ba=Me(),os=require("events").EventEmitter,Fa=require("util"),Ua=vt(),as=ss(),ja=Na.deprecate(()=>{},"Calling client.query() when the client is already executing a query is deprecated and will be removed in pg@9.0. Use async/await or an external async flow control mechanism instead."),P=cs.exports=function(r){os.call(this),r=r||{},this._Promise=r.Promise||global.Promise,this._types=new Ba(r.types),this.native=new is({types:this._types}),this._queryQueue=[],this._ending=!1,this._connecting=!1,this._connected=!1,this._queryable=!0;let e=this.connectionParameters=new Ua(r);r.nativeConnectionString&&(e.nativeConnectionString=r.nativeConnectionString),this.user=e.user,Object.defineProperty(this,"password",{configurable:!0,enumerable:!1,writable:!0,value:e.password}),this.database=e.database,this.host=e.host,this.port=e.port,this.namedQueries={}};P.Query=as;Fa.inherits(P,os);P.prototype._errorAllQueries=function(r){let e=t=>{process.nextTick(()=>{t.native=this.native,t.handleError(r)})};this._hasActiveQuery()&&(e(this._activeQuery),this._activeQuery=null),this._queryQueue.forEach(e),this._queryQueue.length=0};P.prototype._connect=function(r){let e=this;if(this._connecting){process.nextTick(()=>r(new Error("Client has already been connected. You cannot reuse a client.")));return}this._connecting=!0,this.connectionParameters.getLibpqConnectionString(function(t,n){if(e.connectionParameters.nativeConnectionString&&(n=e.connectionParameters.nativeConnectionString),t)return r(t);e.native.connect(n,function(s){if(s)return e.native.end(),r(s);e._connected=!0,e.native.on("error",function(i){e._queryable=!1,e._errorAllQueries(i),e.emit("error",i)}),e.native.on("notification",function(i){e.emit("notification",{channel:i.relname,payload:i.extra})}),e.emit("connect"),e._pulseQueryQueue(!0),r(null,this)})})};P.prototype.connect=function(r){if(r){this._connect(r);return}return new this._Promise((e,t)=>{this._connect(n=>{n?t(n):e(this)})})};P.prototype.query=function(r,e,t){let n,s,i,o,a;if(r==null)throw new TypeError("Client was passed a null or undefined query");if(typeof r.submit=="function")i=r.query_timeout||this.connectionParameters.query_timeout,s=n=r,typeof e=="function"&&(r.callback=e);else if(i=r.query_timeout||this.connectionParameters.query_timeout,n=new as(r,e,t),!n.callback){let l,c;s=new this._Promise((h,d)=>{l=h,c=d}).catch(h=>{throw Error.captureStackTrace(h),h}),n.callback=(h,d)=>h?c(h):l(d)}return i&&(a=n.callback||(()=>{}),o=setTimeout(()=>{let l=new Error("Query read timeout");process.nextTick(()=>{n.handleError(l,this.connection)}),a(l),n.callback=()=>{};let c=this._queryQueue.indexOf(n);c>-1&&this._queryQueue.splice(c,1),this._pulseQueryQueue()},i),n.callback=(l,c)=>{clearTimeout(o),a(l,c)}),this._queryable?this._ending?(n.native=this.native,process.nextTick(()=>{n.handleError(new Error("Client was closed and is not queryable"))}),s):(this._queryQueue.length>0&&ja(),this._queryQueue.push(n),this._pulseQueryQueue(),s):(n.native=this.native,process.nextTick(()=>{n.handleError(new Error("Client has encountered a connection error and is not queryable"))}),s)};P.prototype.end=function(r){let e=this;this._ending=!0,this._connected||this.once("connect",this.end.bind(this,r));let t;return r||(t=new this._Promise(function(n,s){r=i=>i?s(i):n()})),this.native.end(function(){e._connected=!1,e._errorAllQueries(new Error("Connection terminated")),process.nextTick(()=>{e.emit("end"),r&&r()})}),t};P.prototype._hasActiveQuery=function(){return this._activeQuery&&this._activeQuery.state!=="error"&&this._activeQuery.state!=="end"};P.prototype._pulseQueryQueue=function(r){if(!this._connected||this._hasActiveQuery())return;let e=this._queryQueue.shift();if(!e){r||this.emit("drain");return}this._activeQuery=e,e.submit(this);let t=this;e.once("_done",function(){t._pulseQueryQueue()})};P.prototype.cancel=function(r){this._activeQuery===r?this.native.cancel(function(){}):this._queryQueue.indexOf(r)!==-1&&this._queryQueue.splice(this._queryQueue.indexOf(r),1)};P.prototype.ref=function(){};P.prototype.unref=function(){};P.prototype.setTypeParser=function(r,e,t){return this._types.setTypeParser(r,e,t)};P.prototype.getTypeParser=function(r,e){return this._types.getTypeParser(r,e)};P.prototype.isConnected=function(){return this._connected}});var or=g((Tl,ls)=>{"use strict";u();ls.exports=us()});var sr=g((Ml,He)=>{"use strict";u();var $a=Xn(),Ha=pe(),za=Yt(),Ka=_t(),Ga=X(),Va=ts(),Wa=Me(),{DatabaseError:Ya}=Gt(),{escapeIdentifier:Ja,escapeLiteral:Xa}=X(),Za=r=>class extends Va{constructor(t){super(t,r)}},hs=function(r){this.defaults=Ha,this.Client=r,this.Query=this.Client.Query,this.Pool=Za(this.Client),this._pools=[],this.Connection=za,this.types=fe(),this.DatabaseError=Ya,this.TypeOverrides=Wa,this.escapeIdentifier=Ja,this.escapeLiteral=Xa,this.Result=Ka,this.utils=Ga},ds=$a,fs=!1;try{fs=!!process.env.NODE_PG_FORCE_NATIVE}catch{}fs&&(ds=or());He.exports=new hs(ds);Object.defineProperty(He.exports,"native",{configurable:!0,enumerable:!1,get(){let r=null;try{r=new hs(or())}catch(e){if(e.code!=="MODULE_NOT_FOUND")throw e}return Object.defineProperty(He.exports,"native",{value:r}),r}})});var ps={};cr(ps,{Client:()=>ec,Connection:()=>rc,DatabaseError:()=>ic,Pool:()=>tc,Query:()=>sc,Result:()=>cc,TypeOverrides:()=>uc,default:()=>hc,defaults:()=>lc,escapeIdentifier:()=>oc,escapeLiteral:()=>ac,types:()=>nc});var I,ec,tc,rc,nc,sc,ic,oc,ac,cc,uc,lc,hc,ms=ar(()=>{"use strict";u();I=_(sr(),1),ec=I.default.Client,tc=I.default.Pool,rc=I.default.Connection,nc=I.default.types,sc=I.default.Query,ic=I.default.DatabaseError,oc=I.default.escapeIdentifier,ac=I.default.escapeLiteral,cc=I.default.Result,uc=I.default.TypeOverrides,lc=I.default.defaults,hc=I.default});var fc={};cr(fc,{ApiDataSource:()=>xe,ChunkTooLargeError:()=>Je,DimensionMismatchError:()=>$,EmbeddingError:()=>C,IngestionError:()=>E,JSONSource:()=>Pe,Knowledge:()=>Xe,KnowledgeError:()=>D,KnowledgeProviderError:()=>Q,MarkdownSource:()=>ke,MemoryProvider:()=>Ee,OllamaEmbedder:()=>Ke,OpenAIEmbedder:()=>Ge,OpenRouterEmbedder:()=>Ve,PersistentKnowledgeProvider:()=>_e,PostgresSource:()=>ze,SQLiteSource:()=>Ae,WebUrlSource:()=>Ce,combineScores:()=>Se,keywordSearch:()=>N});module.exports=ks(fc);u();u();u();var D=class extends Error{constructor(t,n){super(t);this.code=n;this.name="KnowledgeError"}code},C=class extends D{constructor(t,n){super(t,"EMBEDDING_ERROR");this.statusCode=n;this.name="EmbeddingError"}statusCode},E=class extends D{constructor(t,n){super(t,"INGESTION_ERROR");this.file=n;this.name="IngestionError"}file},Je=class extends D{constructor(t,n){super(t,"CHUNK_TOO_LARGE");this.chunkSize=n;this.name="ChunkTooLargeError"}chunkSize},$=class extends D{expected;actual;constructor(e,t){super(`Dimension mismatch: expected ${e}, got ${t}`,"DIMENSION_MISMATCH"),this.name="DimensionMismatchError",this.expected=e,this.actual=t}},Q=class extends D{constructor(e){super(e,"PROVIDER_ERROR"),this.name="KnowledgeProviderError"}};u();var lr=require("crypto");u();function N(r,e){let t=r.toLowerCase(),n=e.toLowerCase();if(t.includes(n))return 1;let s=n.split(/\s+/).filter(o=>o.length>2);if(s.length===0)return 0;let i=0;for(let o of s)t.includes(o)&&i++;return i/s.length}function Se(r,e,t=.7){let n=1-t;return r*t+e*n}u();function ve(r,e){if(r.length!==e.length)throw new Error("Vectors must have same dimensions");let t=0,n=0,s=0;for(let o=0;o<r.length;o++)t+=r[o]*e[o],n+=r[o]*r[o],s+=e[o]*e[o];let i=Math.sqrt(n)*Math.sqrt(s);return i===0?0:t/i}function B(r,e){if(!e)return!0;for(let[t,n]of Object.entries(e)){let s=r[t];if(typeof n=="object"&&n!==null&&!Array.isArray(n)){if("$in"in n){if(!n.$in.includes(s))return!1}else if("$gt"in n){let i=n.$gt;if(typeof s!="number"||s<=i)return!1}else if("$lt"in n){let i=n.$lt;if(typeof s!="number"||s>=i)return!1}}else if(s!==n)return!1}return!0}var Xe=class r{constructor(e,t,n,s,i){this.provider=e;this.embedder=t;this.description=n;this.sources=s;this.options=i}provider;embedder;description;sources;options;static async create(e){await e.provider.validateDimensions(e.embedder.dimensions);let t=new r(e.provider,e.embedder,e.description,e.sources,e),n=e.reSync!==!1;return!n&&"shouldReSync"in e.provider?(e.provider.shouldReSync()&&await t.sync(),t):(n&&await t.sync(),t)}async query(e,t){let n=t?.searchType??"semantic",s=t?.semanticWeight??.7;if(n==="keyword")return this.keywordQuery(e,t);if(n==="hybrid"){let[i,o]=await Promise.all([this.semanticQuery(e,t),this.keywordQuery(e,t)]);return this.combineHybridResults(i,o,s,t)}else return this.semanticQuery(e,t)}async semanticQuery(e,t){let n=await this.embedder.embed(e);return this.provider.query(n,t)}async keywordQuery(e,t){let{limit:n=10,threshold:s=.1,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t||{};if(typeof this.provider.keywordQuery=="function")return this.provider.keywordQuery(e,t);let l=await this.getAllChunks(),c=[];for(let h of l){if(i&&!B(h.metadata,i))continue;let d=N(h.content,e);d>=s&&c.push({chunk:{id:h.id,content:h.content,metadata:o?h.metadata:{},vector:a?h.vector:void 0},score:d,distance:1-d})}return c.sort((h,d)=>d.score-h.score),c.slice(0,n)}combineHybridResults(e,t,n,s){let{limit:i=10,threshold:o=.5,includeMetadata:a=!0,includeVectors:l=!1}=s||{},c=new Map(e.map(m=>[m.chunk.id,m])),h=new Map(t.map(m=>[m.chunk.id,m])),d=[],p=new Set([...c.keys(),...h.keys()]);for(let m of p){let b=c.get(m),R=h.get(m);if(!b&&!R)continue;let We=b?.score??0,Ye=R?.score??0,K=Se(We,Ye,n);K>=o&&d.push({chunk:{id:m,content:b?.chunk.content??R.chunk.content,metadata:a?b?.chunk.metadata??R.chunk.metadata:{},vector:l?b?.chunk.vector??R.chunk.vector:void 0},score:K,distance:1-K})}return d.sort((m,b)=>b.score-m.score),d.slice(0,i)}async getAllChunks(){if(typeof this.provider.getAllChunks=="function")return this.provider.getAllChunks();let e=new Array(this.embedder.dimensions).fill(0);return(await this.provider.query(e,{limit:1e4,threshold:0})).map(t=>t.chunk)}async sync(){this.options.onSync?.({type:"start"});try{let e=this.embedder.dimensions;await this.provider.clear(),await this.provider.validateDimensions(e);let t=this.options.streamingBatchSize??100,n=0,s=[];for(let i of this.sources)for await(let o of i.load())if(s.push(o),s.length>=t){let a=await this.embedChunks(s);a.length>0&&(await this.provider.add(a),n+=a.length),s.length=0}if(s.length>0){let i=await this.embedChunks(s);i.length>0&&(await this.provider.add(i),n+=i.length)}this.options.onSync?.({type:"complete",chunksAffected:n})}catch(e){throw this.options.onSync?.({type:"error",error:e}),e}}async embedChunks(e){if(e.length===0)return[];let t=[];try{let n=e.map(i=>i.content),s=await this.embedder.embedBatch(n);for(let i=0;i<e.length;i++)t.push({...e[i],vector:s[i]}),this.options.onEmbeddingProgress?.({source:"sync",current:i+1,total:e.length,percent:Math.round((i+1)/e.length*100)})}catch(n){if(this.options.onError?.(n,{})==="abort")throw n;for(let i=0;i<e.length;i++)try{let o=await this.embedder.embed(e[i].content);t.push({...e[i],vector:o}),this.options.onEmbeddingProgress?.({source:"sync",current:i+1,total:e.length,percent:Math.round((i+1)/e.length*100)})}catch(o){if(this.options.onError?.(o,{chunk:e[i]})==="abort")throw o}}return t}async add(e,t){try{let n=(0,lr.randomUUID)(),s=await this.embedder.embed(e),i={id:n,content:e,metadata:t||{},vector:s};return await this.provider.add([i]),n}catch(n){throw new E(`Failed to add content to knowledge base: ${n.message}`,"add")}}async stop(){this.provider.close&&this.provider.close()}toTool(){return{name:"knowledge_search",displayName:"Knowledge Search",description:this.description||"Search the knowledge base for relevant information",category:"search",cacheable:!1,parameters:{type:"object",properties:{query:{type:"string",description:"Search query to find relevant information"},limit:{type:"number",description:"Maximum number of results to return (default: 10)"},threshold:{type:"number",description:"Minimum similarity threshold 0-1 (default: 0.3)"},filter:{type:"object",description:"Optional metadata filters"}},required:["query"]},execute:async e=>(await this.query(e.query,{limit:e.limit,threshold:e.threshold??.3,filter:e.filter,searchType:"hybrid"})).map(n=>({content:n.chunk.content,score:n.score,metadata:n.chunk.metadata}))}}};u();var Ee=class{constructor(e={}){this.options=e}options;chunks=new Map;dimensions;async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new $(this.dimensions,e);this.dimensions=e}async add(e){for(let t of e){if(!t.vector)throw new Q("Chunk missing vector");if(this.options.maxChunks&&this.chunks.size>=this.options.maxChunks)throw new Q(`Max chunks limit reached: ${this.options.maxChunks}`);this.chunks.set(t.id,{chunk:{id:t.id,content:t.content,metadata:t.metadata},vector:t.vector})}}async query(e,t={}){let{limit:n=10,threshold:s=.7,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=[];for(let{chunk:c,vector:h}of this.chunks.values()){if(i&&!B(c.metadata,i))continue;let d=ve(e,h);d>=s&&l.push({chunk:{id:c.id,content:c.content,metadata:o?c.metadata:{},vector:a?h:void 0},score:d,distance:1-d})}return l.sort((c,h)=>h.score-c.score),l.slice(0,n)}async keywordQuery(e,t={}){let{limit:n=10,threshold:s=.1,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=[];for(let{chunk:c,vector:h}of this.chunks.values()){if(i&&!B(c.metadata,i))continue;let d=N(c.content,e);d>=s&&l.push({chunk:{id:c.id,content:c.content,metadata:o?c.metadata:{},vector:a?h:void 0},score:d,distance:1-d})}return l.sort((c,h)=>h.score-c.score),l.slice(0,n)}async delete(e){for(let t of e)this.chunks.delete(t)}async clear(){this.chunks.clear(),this.dimensions=void 0}async getAllChunks(){return Array.from(this.chunks.values()).map(({chunk:e,vector:t})=>({...e,vector:t}))}};u();var hr=_(require("better-sqlite3"),1),oe=_(require("fs"),1),G=_(require("path"),1),dr=_(require("os"),1);var _e=class{constructor(e){this.options=e;let t=dr.homedir(),n=`${e.namespace}.db`,s;if(e.storagePath)s=e.storagePath;else{let i=G.join(t,".toolpack","db","knowledge"),o=G.join(t,".toolpack","knowledge"),a=G.join(i,n),l=G.join(o,n);oe.existsSync(l)&&!oe.existsSync(a)?s=o:s=i}this.dbPath=G.join(s,n),oe.mkdirSync(s,{recursive:!0}),this.db=new hr.default(this.dbPath),this.db.pragma("journal_mode = WAL"),this.initSchema(),this.loadDimensions()}options;db;dimensions;dbPath;initSchema(){this.db.exec(`
2
10
  CREATE TABLE IF NOT EXISTS chunks (
3
11
  id TEXT PRIMARY KEY,
4
12
  content TEXT NOT NULL,
@@ -30,22 +38,35 @@
30
38
  BEGIN
31
39
  UPDATE chunks_fts SET content = new.content, metadata = new.metadata WHERE id = new.id;
32
40
  END;
33
- `)}loadDimensions(){let e=this.db.prepare("SELECT value FROM provider_meta WHERE key = ?").get("dimensions");e&&(this.dimensions=parseInt(e.value,10))}async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new C(this.dimensions,e);this.dimensions||(this.db.prepare("INSERT OR REPLACE INTO provider_meta (key, value) VALUES (?, ?)").run("dimensions",e.toString()),this.dimensions=e)}async add(e){let t=this.db.prepare(`
41
+ `)}loadDimensions(){let e=this.db.prepare("SELECT value FROM provider_meta WHERE key = ?").get("dimensions");e&&(this.dimensions=parseInt(e.value,10))}async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new $(this.dimensions,e);this.dimensions||(this.db.prepare("INSERT OR REPLACE INTO provider_meta (key, value) VALUES (?, ?)").run("dimensions",e.toString()),this.dimensions=e)}async add(e){let t=this.db.prepare(`
34
42
  INSERT OR REPLACE INTO chunks (id, content, metadata, vector, synced_at)
35
43
  VALUES (?, ?, ?, ?, ?)
36
- `);this.db.transaction(n=>{for(let s of n){if(!s.vector)throw new v("Chunk missing vector");let o=Buffer.from(new Float32Array(s.vector).buffer);t.run(s.id,s.content,JSON.stringify(s.metadata),o,Date.now())}})(e)}async query(e,t={}){let{limit:r=10,threshold:n=.7,filter:s,includeMetadata:o=!0,includeVectors:c=!1}=t,m=this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all(),i=[];for(let d of m){let u=JSON.parse(d.metadata);if(s&&!x(u,s))continue;let h=new Float32Array(d.vector.buffer,d.vector.byteOffset,d.vector.byteLength/4),l=Array.from(h),p=M(e,l);p>=n&&i.push({chunk:{id:d.id,content:d.content,metadata:o?u:{},vector:c?l:void 0},score:p,distance:1-p})}return i.sort((d,u)=>u.score-d.score),i.slice(0,r)}async keywordQuery(e,t={}){let{limit:r=10,threshold:n=.1,filter:s,includeMetadata:o=!0,includeVectors:c=!1}=t,m=e.split(/\s+/).map(u=>`"${u}"`).join(" OR "),i=this.db.prepare(`
44
+ `);this.db.transaction(s=>{for(let i of s){if(!i.vector)throw new Q("Chunk missing vector");let o=Buffer.from(new Float32Array(i.vector).buffer);t.run(i.id,i.content,JSON.stringify(i.metadata),o,Date.now())}})(e)}async query(e,t={}){let{limit:n=10,threshold:s=.7,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all(),c=[];for(let h of l){let d=JSON.parse(h.metadata);if(i&&!B(d,i))continue;let p=new Float32Array(h.vector.buffer,h.vector.byteOffset,h.vector.byteLength/4),m=Array.from(p),b=ve(e,m);b>=s&&c.push({chunk:{id:h.id,content:h.content,metadata:o?d:{},vector:a?m:void 0},score:b,distance:1-b})}return c.sort((h,d)=>d.score-h.score),c.slice(0,n)}async keywordQuery(e,t={}){let{limit:n=10,threshold:s=.1,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=e.split(/\s+/).map(d=>`"${d}"`).join(" OR "),c=this.db.prepare(`
37
45
  SELECT c.id, c.content, c.metadata, c.vector, highlight(chunks_fts, 1, '<mark>', '</mark>') as highlighted
38
46
  FROM chunks_fts fts
39
47
  JOIN chunks c ON fts.id = c.id
40
48
  WHERE chunks_fts MATCH ?
41
49
  ORDER BY bm25(chunks_fts) DESC
42
50
  LIMIT ?
43
- `).all(m,r*2),d=[];for(let u of i){let h=JSON.parse(u.metadata);if(s&&!x(h,s))continue;let l=E(u.content,e);if(l>=n){let p=new Float32Array(u.vector.buffer,u.vector.byteOffset,u.vector.byteLength/4);d.push({chunk:{id:u.id,content:u.content,metadata:o?h:{},vector:c?Array.from(p):void 0},score:l,distance:1-l})}}return d.sort((u,h)=>h.score-u.score),d.slice(0,r)}async delete(e){let t=this.db.prepare("DELETE FROM chunks WHERE id = ?");this.db.transaction(n=>{for(let s of n)t.run(s)})(e)}async clear(){this.db.prepare("DELETE FROM chunks").run(),this.db.prepare("DELETE FROM provider_meta WHERE key = ?").run("dimensions"),this.dimensions=void 0}async getAllChunks(){return this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all().map(t=>{let r=JSON.parse(t.metadata),n=new Float32Array(t.vector.buffer,t.vector.byteOffset,t.vector.byteLength/4);return{id:t.id,content:t.content,metadata:r,vector:Array.from(n)}})}shouldReSync(){return this.options.reSync===!1?this.db.prepare("SELECT COUNT(*) as count FROM chunks").get().count===0:!0}close(){this.db.close()}};var G=g(require("fs/promises"),1),R=g(require("path"),1),X=g(require("crypto"),1),J=g(require("fast-glob"),1);function y(a){return Math.ceil(a.length/4)}function ue(a,e){let t=a.split(/\n\n+/),r=[],n="";for(let s of t){let o=y(s);y(n)+o>e&&n?(r.push(n.trim()),n=s):n+=(n?`
51
+ `).all(l,n*2),h=[];for(let d of c){let p=JSON.parse(d.metadata);if(i&&!B(p,i))continue;let m=N(d.content,e);if(m>=s){let b=new Float32Array(d.vector.buffer,d.vector.byteOffset,d.vector.byteLength/4);h.push({chunk:{id:d.id,content:d.content,metadata:o?p:{},vector:a?Array.from(b):void 0},score:m,distance:1-m})}}return h.sort((d,p)=>p.score-d.score),h.slice(0,n)}async delete(e){let t=this.db.prepare("DELETE FROM chunks WHERE id = ?");this.db.transaction(s=>{for(let i of s)t.run(i)})(e)}async clear(){this.db.prepare("DELETE FROM chunks").run(),this.db.prepare("DELETE FROM provider_meta WHERE key = ?").run("dimensions"),this.dimensions=void 0}async getAllChunks(){return this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all().map(t=>{let n=JSON.parse(t.metadata),s=new Float32Array(t.vector.buffer,t.vector.byteOffset,t.vector.byteLength/4);return{id:t.id,content:t.content,metadata:n,vector:Array.from(s)}})}shouldReSync(){return this.options.reSync===!1?this.db.prepare("SELECT COUNT(*) as count FROM chunks").get().count===0:!0}close(){this.db.close()}};u();var fr=_(require("fs/promises"),1),ae=_(require("path"),1),pr=_(require("crypto"),1),mr=_(require("fast-glob"),1);u();function O(r){return Math.ceil(r.length/4)}function Cs(r,e){let t=r.split(/\n\n+/),n=[],s="";for(let i of t){let o=O(i);O(s)+o>e&&s?(n.push(s.trim()),s=i):s+=(s?`
52
+
53
+ `:"")+i}return s&&n.push(s.trim()),n}function xs(r,e){let t=r.match(/[^.!?]+[.!?]+/g)||[r],n=[],s="";for(let i of t){let o=O(i);O(s)+o>e&&s?(n.push(s.trim()),s=i):s+=(s?" ":"")+i}return s&&n.push(s.trim()),n}function V(r,e){if(r.length<=1||e===0)return r;let t=[];for(let n=0;n<r.length;n++){let s=r[n];if(n>0){let o=r[n-1].split(/\s+/),a=Math.ceil(e/4);s=o.slice(-a).join(" ")+" "+s}t.push(s)}return t}function W(r,e){if(O(r)<=e)return[r];let n=Cs(r,e),s=[];for(let i of n)O(i)>e?s.push(...xs(i,e)):s.push(i);return s}var ke=class{constructor(e,t={}){this.pattern=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"markdown",metadata:t.metadata??{}}}pattern;options;async*load(){let e=this.pattern.replace(/\\/g,"/"),t=await(0,mr.default)(e,{absolute:!0});for(let n of t)try{let s=await fr.readFile(n,"utf-8"),i=this.chunkMarkdown(s,n);for(let o of i)yield o}catch(s){throw new E(`Failed to process file: ${s.message}`,n)}}chunkMarkdown(e,t){let n=this.extractFrontmatter(e),s=this.removeFrontmatter(e),i=this.parseHeadings(s),o=[],a=0;for(let l of i){let c=/```[\s\S]*?```/.test(l.content),h=O(l.content);if(h<this.options.minChunkSize&&o.length>0){let p=o[o.length-1];p.content+=`
54
+
55
+ `+l.content,c&&(p.metadata.hasCode=!0);continue}let d;h>this.options.maxChunkSize?d=W(l.content,this.options.maxChunkSize):d=[l.content],this.options.chunkOverlap>0&&d.length>1&&(d=V(d,this.options.chunkOverlap));for(let p=0;p<d.length;p++){let m=d[p],b=this.generateChunkId(t,m,a);o.push({id:b,content:m,metadata:{...this.options.metadata,...n,heading:l.heading,hasCode:c,source:ae.basename(t),sourcePath:t,chunkIndex:a,totalChunks:d.length}}),a++}}return o}parseHeadings(e){let t=e.split(`
56
+ `),n=[],s=[],i=[];for(let o of t){let a=o.match(/^(#{1,6})\s+(.+)$/);if(a){if(i.length>0){let h=s.map(d=>d.text);n.push({heading:h.length>0?[...h]:[""],content:i.join(`
57
+ `).trim(),level:s.length>0?s[s.length-1].level:0}),i=[]}let l=a[1].length,c=a[2].trim();for(;s.length>0&&s[s.length-1].level>=l;)s.pop();s.push({level:l,text:c}),i.push(o)}else i.push(o)}if(i.length>0){let o=s.map(a=>a.text);n.push({heading:o.length>0?[...o]:[""],content:i.join(`
58
+ `).trim(),level:s.length>0?s[s.length-1].level:0})}return n.filter(o=>o.content.length>0)}extractFrontmatter(e){let t=e.match(/^---\n([\s\S]*?)\n---/);if(!t)return{};let n=t[1],s={},i=n.split(`
59
+ `);for(let o of i){let a=o.match(/^(\w+):\s*(.+)$/);if(a){let l=a[1],c=a[2].trim();c==="true"?c=!0:c==="false"?c=!1:isNaN(Number(c))?typeof c=="string"&&c.startsWith("[")&&c.endsWith("]")&&(c=c.slice(1,-1).split(",").map(h=>h.trim())):c=Number(c),s[l]=c}}return s}removeFrontmatter(e){return e.replace(/^---\n[\s\S]*?\n---\n/,"")}generateChunkId(e,t,n){let s=pr.createHash("md5").update(t).digest("hex").substring(0,8),i=ae.basename(e,ae.extname(e));return`${this.options.namespace}:${i}:${n}:${s}`}};u();var Ze=_(require("crypto"),1),yr=_(require("cheerio"),1);var Ce=class{constructor(e,t={}){this.urls=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"web",metadata:t.metadata??{},maxDepth:t.maxDepth??1,userAgent:t.userAgent??"Toolpack-Knowledge/1.0",delayMs:t.delayMs??1e3,timeoutMs:t.timeoutMs??3e4,sameDomainOnly:t.sameDomainOnly??!0,maxPagesPerDomain:t.maxPagesPerDomain??10}}urls;options;crawledUrls=new Set;domainPageCount=new Map;lastRequestTime=new Map;async*load(){let e=await this.crawlUrls(this.urls,0);for(let t of e)try{let n=this.chunkPage(t);for(let s of n)yield s}catch(n){throw new E(`Failed to process URL ${t.url}: ${n.message}`,t.url)}}async crawlUrls(e,t){if(t>=this.options.maxDepth)return[];let n=[],s=[],i=new Set(e.map(o=>new URL(o).hostname));for(let o of e){if(this.crawledUrls.has(o))continue;let a=new URL(o).hostname,l=this.domainPageCount.get(a)??0;if(!(this.options.sameDomainOnly&&!i.has(a))&&!(l>=this.options.maxPagesPerDomain)){this.crawledUrls.add(o),this.domainPageCount.set(a,l+1);try{let c=this.lastRequestTime.get(a)??0,h=Date.now()-c;h<this.options.delayMs&&await new Promise(p=>setTimeout(p,this.options.delayMs-h));let d=await this.fetchPage(o);n.push(d),this.lastRequestTime.set(a,Date.now()),t<this.options.maxDepth-1&&s.push(...d.links)}catch(c){console.warn(`Failed to crawl ${o}: ${c.message}`)}}}if(s.length>0){let o=await this.crawlUrls(s,t+1);n.push(...o)}return n}async fetchPage(e){let t=new AbortController,n=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let s=await fetch(e,{signal:t.signal,headers:{"User-Agent":this.options.userAgent}});if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);let i=await s.text(),o=yr.load(i);o("script, style, nav, header, footer, aside").remove();let a=o("title").text().trim()||o("h1").first().text().trim()||"Untitled",l=["main","article",".content","#content","body"],c="";for(let d of l){let p=o(d);if(p.length>0){c=p.text().trim();break}}c||(c=o("body").text().trim()),c=c.replace(/\s+/g," ").trim();let h=[];return o("a[href]").each((d,p)=>{let m=o(p).attr("href");if(m)try{let b=new URL(m,e).href;b.startsWith("http")&&!b.includes("#")&&h.push(b)}catch{}}),{url:e,title:a,content:c,links:[...new Set(h)]}}finally{clearTimeout(n)}}chunkPage(e){let t=[],n=O(e.content),s;n>this.options.maxChunkSize?s=W(e.content,this.options.maxChunkSize):s=[e.content],this.options.chunkOverlap>0&&s.length>1&&(s=V(s,this.options.chunkOverlap));for(let i=0;i<s.length;i++){let o=s[i],a=this.generateChunkId(e.url,o,i);t.push({id:a,content:o,metadata:{...this.options.metadata,title:e.title,url:e.url,source:"web",chunkIndex:i,totalChunks:s.length}})}return t}generateChunkId(e,t,n){let s=Ze.createHash("md5").update(t).digest("hex").substring(0,8),i=Ze.createHash("md5").update(e).digest("hex").substring(0,8);return`${this.options.namespace}:${i}:${n}:${s}`}};u();var et=_(require("crypto"),1);var xe=class{constructor(e,t={}){this.url=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"api",metadata:t.metadata??{},headers:t.headers??{},method:t.method??"GET",timeoutMs:t.timeoutMs??3e4,pagination:t.pagination,dataPath:t.dataPath??"",contentExtractor:t.contentExtractor??this.defaultContentExtractor,metadataExtractor:t.metadataExtractor??this.defaultMetadataExtractor}}url;options;async*load(){let e=await this.fetchData();for(let t of e)try{let n=this.chunkItem(t);for(let s of n)yield s}catch(n){throw new E(`Failed to process API item: ${n.message}`,this.url)}}async fetchData(){let e=[],t=this.options.pagination?.start??0,n=this.options.pagination?.maxPages??1;for(;t<n;){let s=this.buildUrl(t),i=await this.fetchPage(s);if(i.length===0||(e.push(...i),t++,!this.options.pagination))break}return e}buildUrl(e){if(!this.options.pagination)return this.url;let t=new URL(this.url);return t.searchParams.set(this.options.pagination.param,e.toString()),t.href}async fetchPage(e){let t=new AbortController,n=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let s=await fetch(e,{method:this.options.method,headers:{"Content-Type":"application/json",...this.options.headers},body:this.options.body?JSON.stringify(this.options.body):void 0,signal:t.signal});if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);let i=await s.json();return this.extractItems(i)}finally{clearTimeout(n)}}extractItems(e){if(!this.options.dataPath)return Array.isArray(e)?e:[e];let t=this.options.dataPath.split("."),n=e;for(let s of t)if(n&&typeof n=="object"&&s in n)n=n[s];else throw new Error(`Data path '${this.options.dataPath}' not found in response`);return Array.isArray(n)?n:[n]}chunkItem(e){let t=this.options.contentExtractor(e),n=this.options.metadataExtractor(e),s=O(t),i;s>(this.options.maxChunkSize??2e3)?i=W(t,this.options.maxChunkSize??2e3):i=[t],(this.options.chunkOverlap??200)>0&&i.length>1&&(i=V(i,this.options.chunkOverlap??200));let o=[];for(let a=0;a<i.length;a++){let l=i[a],c=this.generateChunkId(e,l,a);o.push({id:c,content:l,metadata:{...this.options.metadata,...n,source:"api",apiUrl:this.url,chunkIndex:a,totalChunks:i.length}})}return o}defaultContentExtractor(e){if(typeof e=="string")return e;if(typeof e=="object"&&e!==null){let t=["content","text","description","body","message"];for(let n of t)if(n in e&&typeof e[n]=="string")return e[n];return JSON.stringify(e)}return String(e)}defaultMetadataExtractor(e){if(typeof e=="object"&&e!==null){let t={},n=["id","title","name","created_at","updated_at","author","tags"];for(let s of n)s in e&&(t[s]=e[s]);return t}return{}}generateChunkId(e,t,n){let s=et.createHash("md5").update(t).digest("hex").substring(0,8),i=et.createHash("md5").update(JSON.stringify(e)).digest("hex").substring(0,8);return`${this.options.namespace}:${i}:${n}:${s}`}};u();var gr=_(require("fs/promises"),1),tt=_(require("path"),1);var Pe=class{constructor(e,t){this.filePath=e;if(!t.toContent)throw new E("JSONSource requires a toContent callback. Example: toContent: (item) => `Name: ${item.name}`",this.filePath);this.options={namespace:t.namespace??"json",metadata:t.metadata??{},filter:t.filter??(()=>!0),chunkSize:t.chunkSize??100,toContent:t.toContent}}filePath;options;async*load(){let e;try{let t=await gr.readFile(this.filePath,"utf-8");e=JSON.parse(t)}catch(t){throw new E(`Failed to parse JSON file: ${t.message}`,this.filePath)}if(Array.isArray(e)){let n=e.filter(this.options.filter).map(this.options.toContent);for(let s=0;s<n.length;s+=this.options.chunkSize){let o=n.slice(s,s+this.options.chunkSize).join(`
60
+
61
+ ---
62
+
63
+ `);yield{id:`json:${this.options.namespace}:${s}`,content:o,metadata:{...this.options.metadata,source:tt.basename(this.filePath),type:"json_array_chunk",startIndex:s,endIndex:Math.min(s+this.options.chunkSize,n.length),totalItems:n.length}}}}else{let t=typeof e=="object"&&e!==null?this.options.toContent(e):typeof e=="string"?e:JSON.stringify(e);yield{id:`json:${this.options.namespace}:0`,content:t,metadata:{...this.options.metadata,source:tt.basename(this.filePath),type:"json_object"}}}}};u();var br=_(require("fs/promises"),1),wr=_(require("path"),1);var Ae=class{constructor(e,t){this.dbPath=e;if(!t.toContent)throw new E("SQLiteSource requires a toContent callback. Example: toContent: (row) => `Name: ${row.name}`",this.dbPath);this.options={namespace:t.namespace??"sqlite",metadata:t.metadata??{},chunkSize:t.chunkSize??100,toContent:t.toContent,query:t.query,preLoadCSV:t.preLoadCSV}}dbPath;options;async*load(){let e;try{e=(await import("better-sqlite3")).default}catch{throw new E('SQLite source requires "better-sqlite3" package. Install with: npm install better-sqlite3',this.dbPath)}try{await br.access(this.dbPath)}catch{throw new E("SQLite database file not found",this.dbPath)}let t=new e(this.dbPath);try{this.options.preLoadCSV&&await this.loadCSV(t,this.options.preLoadCSV);let n=this.options.query??'SELECT * FROM sqlite_master WHERE type = "table"',o=t.prepare(n).all().map(a=>this.options.toContent(a));for(let a=0;a<o.length;a+=this.options.chunkSize){let c=o.slice(a,a+this.options.chunkSize).join(`
64
+
65
+ ---
66
+
67
+ `);yield{id:`sqlite:${this.options.namespace}:${a}`,content:c,metadata:{...this.options.metadata,source:wr.basename(this.dbPath),type:"sqlite_query_result",query:n,startIndex:a,endIndex:Math.min(a+this.options.chunkSize,o.length),totalRows:o.length}}}}finally{t.exec("VACUUM;")}}async loadCSV(e,t){let s=await(await import("fs")).promises.readFile(t.csvPath,"utf-8"),i=t.delimiter??",",o=s.split(`
68
+ `).filter(m=>m.trim());if(o.length===0)return;let a,l;t.headers!==!1?(a=o[0].split(i).map(m=>m.trim().replace(/^["']|["']$/g,"")),l=1):(a=o[0].split(i).map((b,R)=>`col${R}`),l=0);let c=t.tableName.replace(/[^a-zA-Z0-9_]/g,"_"),h=a.map(m=>`"${m.replace(/"/g,'""')}" TEXT`).join(", ");e.exec(`DROP TABLE IF EXISTS "${c}";`),e.exec(`CREATE TABLE "${c}" (${h});`);let d=a.map(()=>"?").join(", "),p=e.prepare(`INSERT INTO "${c}" VALUES (${d})`);for(let m=l;m<o.length;m++){let b=o[m].split(i).map(R=>R.trim().replace(/^["']|["']$/g,""));p.run(b)}}};u();var ze=class{options;constructor(e){if(!e.query)throw new E("PostgresSource requires a query","config");if(!e.toContent)throw new E("PostgresSource requires a toContent callback. Example: toContent: (row) => `Name: ${row.name}`","config");this.options={namespace:e.namespace??"postgres",metadata:e.metadata??{},chunkSize:e.chunkSize??100,toContent:e.toContent,query:e.query,connectionString:e.connectionString,host:e.host,port:e.port,database:e.database,user:e.user,password:e.password,ssl:e.ssl}}async*load(){let e;try{e=(await Promise.resolve().then(()=>(ms(),ps))).Client}catch{throw new E('PostgreSQL source requires "pg" package. Install with: npm install pg',"config")}let t=this.options.connectionString?{connectionString:this.options.connectionString}:{host:this.options.host??"localhost",port:this.options.port??5432,database:this.options.database,user:this.options.user,password:this.options.password,ssl:this.options.ssl},n=new e(t);try{await n.connect();let o=(await n.query(this.options.query)).rows.map(a=>this.options.toContent(a));for(let a=0;a<o.length;a+=this.options.chunkSize){let c=o.slice(a,a+this.options.chunkSize).join(`
44
69
 
45
- `:"")+s}return n&&r.push(n.trim()),r}function de(a,e){let t=a.match(/[^.!?]+[.!?]+/g)||[a],r=[],n="";for(let s of t){let o=y(s);y(n)+o>e&&n?(r.push(n.trim()),n=s):n+=(n?" ":"")+s}return n&&r.push(n.trim()),r}function S(a,e){if(a.length<=1||e===0)return a;let t=[];for(let r=0;r<a.length;r++){let n=a[r];if(r>0){let o=a[r-1].split(/\s+/),c=Math.ceil(e/4);n=o.slice(-c).join(" ")+" "+n}t.push(n)}return t}function O(a,e){if(y(a)<=e)return[a];let r=ue(a,e),n=[];for(let s of r)y(s)>e?n.push(...de(s,e)):n.push(s);return n}var N=class{constructor(e,t={}){this.pattern=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"markdown",metadata:t.metadata??{}}}pattern;options;async*load(){let e=this.pattern.replace(/\\/g,"/"),t=await(0,J.default)(e,{absolute:!0});for(let r of t)try{let n=await G.readFile(r,"utf-8"),s=this.chunkMarkdown(n,r);for(let o of s)yield o}catch(n){throw new b(`Failed to process file: ${n.message}`,r)}}chunkMarkdown(e,t){let r=this.extractFrontmatter(e),n=this.removeFrontmatter(e),s=this.parseHeadings(n),o=[],c=0;for(let m of s){let i=/```[\s\S]*?```/.test(m.content),d=y(m.content);if(d<this.options.minChunkSize&&o.length>0){let h=o[o.length-1];h.content+=`
70
+ ---
46
71
 
47
- `+m.content,i&&(h.metadata.hasCode=!0);continue}let u;d>this.options.maxChunkSize?u=O(m.content,this.options.maxChunkSize):u=[m.content],this.options.chunkOverlap>0&&u.length>1&&(u=S(u,this.options.chunkOverlap));for(let h=0;h<u.length;h++){let l=u[h],p=this.generateChunkId(t,l,c);o.push({id:p,content:l,metadata:{...this.options.metadata,...r,heading:m.heading,hasCode:i,source:R.basename(t),sourcePath:t,chunkIndex:c,totalChunks:u.length}}),c++}}return o}parseHeadings(e){let t=e.split(`
48
- `),r=[],n=[],s=[];for(let o of t){let c=o.match(/^(#{1,6})\s+(.+)$/);if(c){if(s.length>0){let d=n.map(u=>u.text);r.push({heading:d.length>0?[...d]:[""],content:s.join(`
49
- `).trim(),level:n.length>0?n[n.length-1].level:0}),s=[]}let m=c[1].length,i=c[2].trim();for(;n.length>0&&n[n.length-1].level>=m;)n.pop();n.push({level:m,text:i}),s.push(o)}else s.push(o)}if(s.length>0){let o=n.map(c=>c.text);r.push({heading:o.length>0?[...o]:[""],content:s.join(`
50
- `).trim(),level:n.length>0?n[n.length-1].level:0})}return r.filter(o=>o.content.length>0)}extractFrontmatter(e){let t=e.match(/^---\n([\s\S]*?)\n---/);if(!t)return{};let r=t[1],n={},s=r.split(`
51
- `);for(let o of s){let c=o.match(/^(\w+):\s*(.+)$/);if(c){let m=c[1],i=c[2].trim();i==="true"?i=!0:i==="false"?i=!1:isNaN(Number(i))?typeof i=="string"&&i.startsWith("[")&&i.endsWith("]")&&(i=i.slice(1,-1).split(",").map(d=>d.trim())):i=Number(i),n[m]=i}}return n}removeFrontmatter(e){return e.replace(/^---\n[\s\S]*?\n---\n/,"")}generateChunkId(e,t,r){let n=X.createHash("md5").update(t).digest("hex").substring(0,8),s=R.basename(e,R.extname(e));return`${this.options.namespace}:${s}:${r}:${n}`}};var H=g(require("crypto"),1),Y=g(require("cheerio"),1);var $=class{constructor(e,t={}){this.urls=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"web",metadata:t.metadata??{},maxDepth:t.maxDepth??1,userAgent:t.userAgent??"Toolpack-Knowledge/1.0",delayMs:t.delayMs??1e3,timeoutMs:t.timeoutMs??3e4,sameDomainOnly:t.sameDomainOnly??!0,maxPagesPerDomain:t.maxPagesPerDomain??10}}urls;options;crawledUrls=new Set;domainPageCount=new Map;lastRequestTime=new Map;async*load(){let e=await this.crawlUrls(this.urls,0);for(let t of e)try{let r=this.chunkPage(t);for(let n of r)yield n}catch(r){throw new b(`Failed to process URL ${t.url}: ${r.message}`,t.url)}}async crawlUrls(e,t){if(t>=this.options.maxDepth)return[];let r=[],n=[],s=new Set(e.map(o=>new URL(o).hostname));for(let o of e){if(this.crawledUrls.has(o))continue;let c=new URL(o).hostname,m=this.domainPageCount.get(c)??0;if(!(this.options.sameDomainOnly&&!s.has(c))&&!(m>=this.options.maxPagesPerDomain)){this.crawledUrls.add(o),this.domainPageCount.set(c,m+1);try{let i=this.lastRequestTime.get(c)??0,d=Date.now()-i;d<this.options.delayMs&&await new Promise(h=>setTimeout(h,this.options.delayMs-d));let u=await this.fetchPage(o);r.push(u),this.lastRequestTime.set(c,Date.now()),t<this.options.maxDepth-1&&n.push(...u.links)}catch(i){console.warn(`Failed to crawl ${o}: ${i.message}`)}}}if(n.length>0){let o=await this.crawlUrls(n,t+1);r.push(...o)}return r}async fetchPage(e){let t=new AbortController,r=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let n=await fetch(e,{signal:t.signal,headers:{"User-Agent":this.options.userAgent}});if(!n.ok)throw new Error(`HTTP ${n.status}: ${n.statusText}`);let s=await n.text(),o=Y.load(s);o("script, style, nav, header, footer, aside").remove();let c=o("title").text().trim()||o("h1").first().text().trim()||"Untitled",m=["main","article",".content","#content","body"],i="";for(let u of m){let h=o(u);if(h.length>0){i=h.text().trim();break}}i||(i=o("body").text().trim()),i=i.replace(/\s+/g," ").trim();let d=[];return o("a[href]").each((u,h)=>{let l=o(h).attr("href");if(l)try{let p=new URL(l,e).href;p.startsWith("http")&&!p.includes("#")&&d.push(p)}catch{}}),{url:e,title:c,content:i,links:[...new Set(d)]}}finally{clearTimeout(r)}}chunkPage(e){let t=[],r=y(e.content),n;r>this.options.maxChunkSize?n=O(e.content,this.options.maxChunkSize):n=[e.content],this.options.chunkOverlap>0&&n.length>1&&(n=S(n,this.options.chunkOverlap));for(let s=0;s<n.length;s++){let o=n[s],c=this.generateChunkId(e.url,o,s);t.push({id:c,content:o,metadata:{...this.options.metadata,title:e.title,url:e.url,source:"web",chunkIndex:s,totalChunks:n.length}})}return t}generateChunkId(e,t,r){let n=H.createHash("md5").update(t).digest("hex").substring(0,8),s=H.createHash("md5").update(e).digest("hex").substring(0,8);return`${this.options.namespace}:${s}:${r}:${n}`}};var j=g(require("crypto"),1);var L=class{constructor(e,t={}){this.url=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"api",metadata:t.metadata??{},headers:t.headers??{},method:t.method??"GET",timeoutMs:t.timeoutMs??3e4,pagination:t.pagination,dataPath:t.dataPath??"",contentExtractor:t.contentExtractor??this.defaultContentExtractor,metadataExtractor:t.metadataExtractor??this.defaultMetadataExtractor}}url;options;async*load(){let e=await this.fetchData();for(let t of e)try{let r=this.chunkItem(t);for(let n of r)yield n}catch(r){throw new b(`Failed to process API item: ${r.message}`,this.url)}}async fetchData(){let e=[],t=this.options.pagination?.start??0,r=this.options.pagination?.maxPages??1;for(;t<r;){let n=this.buildUrl(t),s=await this.fetchPage(n);if(s.length===0||(e.push(...s),t++,!this.options.pagination))break}return e}buildUrl(e){if(!this.options.pagination)return this.url;let t=new URL(this.url);return t.searchParams.set(this.options.pagination.param,e.toString()),t.href}async fetchPage(e){let t=new AbortController,r=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let n=await fetch(e,{method:this.options.method,headers:{"Content-Type":"application/json",...this.options.headers},body:this.options.body?JSON.stringify(this.options.body):void 0,signal:t.signal});if(!n.ok)throw new Error(`HTTP ${n.status}: ${n.statusText}`);let s=await n.json();return this.extractItems(s)}finally{clearTimeout(r)}}extractItems(e){if(!this.options.dataPath)return Array.isArray(e)?e:[e];let t=this.options.dataPath.split("."),r=e;for(let n of t)if(r&&typeof r=="object"&&n in r)r=r[n];else throw new Error(`Data path '${this.options.dataPath}' not found in response`);return Array.isArray(r)?r:[r]}chunkItem(e){let t=this.options.contentExtractor(e),r=this.options.metadataExtractor(e),n=y(t),s;n>(this.options.maxChunkSize??2e3)?s=O(t,this.options.maxChunkSize??2e3):s=[t],(this.options.chunkOverlap??200)>0&&s.length>1&&(s=S(s,this.options.chunkOverlap??200));let o=[];for(let c=0;c<s.length;c++){let m=s[c],i=this.generateChunkId(e,m,c);o.push({id:i,content:m,metadata:{...this.options.metadata,...r,source:"api",apiUrl:this.url,chunkIndex:c,totalChunks:s.length}})}return o}defaultContentExtractor(e){if(typeof e=="string")return e;if(typeof e=="object"&&e!==null){let t=["content","text","description","body","message"];for(let r of t)if(r in e&&typeof e[r]=="string")return e[r];return JSON.stringify(e)}return String(e)}defaultMetadataExtractor(e){if(typeof e=="object"&&e!==null){let t={},r=["id","title","name","created_at","updated_at","author","tags"];for(let n of r)n in e&&(t[n]=e[n]);return t}return{}}generateChunkId(e,t,r){let n=j.createHash("md5").update(t).digest("hex").substring(0,8),s=j.createHash("md5").update(JSON.stringify(e)).digest("hex").substring(0,8);return`${this.options.namespace}:${s}:${r}:${n}`}};var Q=class{constructor(e){this.options=e;this.baseUrl=e.baseUrl||"http://localhost:11434",this.dimensions=e.dimensions||this.getModelDimensions(e.model)}options;dimensions;baseUrl;getModelDimensions(e){let t={"nomic-embed-text":768,"mxbai-embed-large":1024,"all-minilm":384,"snowflake-arctic-embed":1024,"bge-m3":1024,"bge-large":1024,"all-minilm:l6-v2":384,"all-minilm:l12-v2":384},r=t[e];if(!r)throw new k(`Unknown Ollama model '${e}'. Provide 'dimensions' in OllamaEmbedderOptions or use a known model: ${Object.keys(t).join(", ")}`);return r}async embed(e){let t=null,r=this.options.retries||3,n=this.options.retryDelay||1e3;for(let s=0;s<r;s++)try{let o=await fetch(`${this.baseUrl}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:this.options.model,prompt:e})});if(!o.ok)throw new k(`Ollama embedding failed: ${o.statusText}`,o.status);return(await o.json()).embedding}catch(o){if(t=o,o instanceof k&&o.statusCode&&o.statusCode>=400&&o.statusCode<500)throw o;s<r-1&&await new Promise(c=>setTimeout(c,n))}throw new k(`Ollama embedding failed after ${r} retries: ${t?.message}`)}async embedBatch(e){let t=[];for(let r of e)t.push(await this.embed(r));return t}};var Z=g(require("openai"),1);var U=class{constructor(e){this.options=e;this.client=new Z.default({apiKey:e.apiKey,timeout:e.timeout||3e4}),this.dimensions=this.getModelDimensions(e.model)}options;dimensions;client;getModelDimensions(e){return{"text-embedding-3-small":1536,"text-embedding-3-large":3072,"text-embedding-ada-002":1536}[e]||1536}async embed(e){let t=null,r=this.options.retries||3;for(let n=0;n<r;n++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data[0].embedding}catch(s){t=s,n<r-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new k(`OpenAI embedding failed after ${r} retries: ${t?.message}`)}async embedBatch(e){let t=null,r=this.options.retries||3;for(let n=0;n<r;n++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data.map(o=>o.embedding)}catch(s){t=s,n<r-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new k(`OpenAI batch embedding failed after ${r} retries: ${t?.message}`)}};0&&(module.exports={ApiDataSource,ChunkTooLargeError,DimensionMismatchError,EmbeddingError,IngestionError,Knowledge,KnowledgeError,KnowledgeProviderError,MarkdownSource,MemoryProvider,OllamaEmbedder,OpenAIEmbedder,PersistentKnowledgeProvider,WebUrlSource,combineScores,keywordSearch});
72
+ `);yield{id:`postgres:${this.options.namespace}:${a}`,content:c,metadata:{...this.options.metadata,type:"postgres_query_result",query:this.options.query,startIndex:a,endIndex:Math.min(a+this.options.chunkSize,o.length),totalRows:o.length}}}}finally{await n.end()}}};u();var Ke=class{constructor(e){this.options=e;this.baseUrl=e.baseUrl||"http://localhost:11434",this.dimensions=e.dimensions||this.getModelDimensions(e.model)}options;dimensions;baseUrl;getModelDimensions(e){let t={"nomic-embed-text":768,"mxbai-embed-large":1024,"all-minilm":384,"snowflake-arctic-embed":1024,"bge-m3":1024,"bge-large":1024,"all-minilm:l6-v2":384,"all-minilm:l12-v2":384},n=t[e];if(!n)throw new C(`Unknown Ollama model '${e}'. Provide 'dimensions' in OllamaEmbedderOptions or use a known model: ${Object.keys(t).join(", ")}`);return n}async embed(e){let t=null,n=this.options.retries||3,s=this.options.retryDelay||1e3;for(let i=0;i<n;i++)try{let o=await fetch(`${this.baseUrl}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:this.options.model,prompt:e})});if(!o.ok)throw new C(`Ollama embedding failed: ${o.statusText}`,o.status);return(await o.json()).embedding}catch(o){if(t=o,o instanceof C&&o.statusCode&&o.statusCode>=400&&o.statusCode<500)throw o;i<n-1&&await new Promise(a=>setTimeout(a,s))}throw new C(`Ollama embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=[];for(let n of e)t.push(await this.embed(n));return t}};u();var ys=_(require("openai"),1);var Ge=class{constructor(e){this.options=e;this.client=new ys.default({apiKey:e.apiKey,timeout:e.timeout||3e4}),this.dimensions=this.getModelDimensions(e.model)}options;dimensions;client;getModelDimensions(e){return{"text-embedding-3-small":1536,"text-embedding-3-large":3072,"text-embedding-ada-002":1536}[e]||1536}async embed(e){let t=null,n=this.options.retries||3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data[0].embedding}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new C(`OpenAI embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=null,n=this.options.retries||3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data.map(o=>o.embedding)}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new C(`OpenAI batch embedding failed after ${n} retries: ${t?.message}`)}};u();var gs=_(require("openai"),1);var dc="https://openrouter.ai/api/v1",Ve=class{constructor(e){this.options=e;this.client=new gs.default({apiKey:e.apiKey,baseURL:dc,timeout:e.timeout||3e4}),this.dimensions=e.dimensions??this.getModelDimensions(e.model)}options;dimensions;client;getModelDimensions(e){let t={"nvidia/llama-nemotron-embed-vl-1b-v2":4096,"nvidia/llama-nemotron-embed-vl-1b-v2:free":4096,"openai/text-embedding-3-small":1536,"openai/text-embedding-3-large":3072,"openai/text-embedding-ada-002":1536},n=t[e];if(n===void 0)throw new C(`Unknown OpenRouter embedding model '${e}'. Pass 'dimensions' in OpenRouterEmbedderOptions or use a known model: ${Object.keys(t).join(", ")}`);return n}async embed(e){let t=null,n=this.options.retries??3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data[0].embedding}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay??1e3))}throw new C(`OpenRouter embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=null,n=this.options.retries??3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data.map(o=>o.embedding)}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay??1e3))}throw new C(`OpenRouter batch embedding failed after ${n} retries: ${t?.message}`)}};0&&(module.exports={ApiDataSource,ChunkTooLargeError,DimensionMismatchError,EmbeddingError,IngestionError,JSONSource,Knowledge,KnowledgeError,KnowledgeProviderError,MarkdownSource,MemoryProvider,OllamaEmbedder,OpenAIEmbedder,OpenRouterEmbedder,PersistentKnowledgeProvider,PostgresSource,SQLiteSource,WebUrlSource,combineScores,keywordSearch});
package/dist/index.d.cts CHANGED
@@ -120,6 +120,14 @@ declare class Knowledge {
120
120
  private getAllChunks;
121
121
  sync(): Promise<void>;
122
122
  private embedChunks;
123
+ /**
124
+ * Add a single content item to the knowledge base without triggering a full re-sync.
125
+ * This is useful for runtime additions like conversation history or agent state.
126
+ * @param content The text content to add
127
+ * @param metadata Optional metadata to attach to the chunk
128
+ * @returns The ID of the added chunk
129
+ */
130
+ add(content: string, metadata?: Record<string, unknown>): Promise<string>;
123
131
  stop(): Promise<void>;
124
132
  toTool(): KnowledgeTool;
125
133
  }
@@ -276,6 +284,78 @@ declare class ApiDataSource implements KnowledgeSource {
276
284
  private generateChunkId;
277
285
  }
278
286
 
287
+ interface JSONSourceOptions {
288
+ namespace?: string;
289
+ metadata?: Record<string, unknown>;
290
+ filter?: (item: unknown) => boolean;
291
+ chunkSize?: number;
292
+ /** Required. Transform each JSON item into a human-readable string for AI embedding. */
293
+ toContent: (item: unknown) => string;
294
+ }
295
+ /**
296
+ * Knowledge source for JSON files.
297
+ * Supports jq-like filtering and chunking of large arrays.
298
+ */
299
+ declare class JSONSource implements KnowledgeSource {
300
+ private filePath;
301
+ private options;
302
+ constructor(filePath: string, options: JSONSourceOptions);
303
+ load(): AsyncIterable<Chunk>;
304
+ }
305
+
306
+ interface SQLiteSourceOptions {
307
+ namespace?: string;
308
+ metadata?: Record<string, unknown>;
309
+ query?: string;
310
+ chunkSize?: number;
311
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
312
+ toContent: (row: Record<string, unknown>) => string;
313
+ preLoadCSV?: {
314
+ tableName: string;
315
+ csvPath: string;
316
+ delimiter?: string;
317
+ headers?: boolean;
318
+ };
319
+ }
320
+ /**
321
+ * Knowledge source for SQLite databases.
322
+ * Supports SQL queries and optional CSV/TSV pre-loading.
323
+ * Note: This requires the 'better-sqlite3' package to be installed.
324
+ */
325
+ declare class SQLiteSource implements KnowledgeSource {
326
+ private dbPath;
327
+ private options;
328
+ constructor(dbPath: string, options: SQLiteSourceOptions);
329
+ load(): AsyncIterable<Chunk>;
330
+ private loadCSV;
331
+ }
332
+
333
+ interface PostgresSourceOptions {
334
+ namespace?: string;
335
+ metadata?: Record<string, unknown>;
336
+ query: string;
337
+ chunkSize?: number;
338
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
339
+ toContent: (row: Record<string, unknown>) => string;
340
+ connectionString?: string;
341
+ host?: string;
342
+ port?: number;
343
+ database?: string;
344
+ user?: string;
345
+ password?: string;
346
+ ssl?: boolean;
347
+ }
348
+ /**
349
+ * Knowledge source for PostgreSQL databases.
350
+ * Supports SQL queries with optional chunking.
351
+ * Note: This requires the 'pg' package to be installed.
352
+ */
353
+ declare class PostgresSource implements KnowledgeSource {
354
+ private options;
355
+ constructor(options: PostgresSourceOptions);
356
+ load(): AsyncIterable<Chunk>;
357
+ }
358
+
279
359
  interface OllamaEmbedderOptions {
280
360
  model: string;
281
361
  baseUrl?: string;
@@ -311,7 +391,26 @@ declare class OpenAIEmbedder implements Embedder {
311
391
  embedBatch(texts: string[]): Promise<number[][]>;
312
392
  }
313
393
 
394
+ interface OpenRouterEmbedderOptions {
395
+ model: string;
396
+ apiKey: string;
397
+ /** Override output dimensions for models not in the built-in map */
398
+ dimensions?: number;
399
+ retries?: number;
400
+ retryDelay?: number;
401
+ timeout?: number;
402
+ }
403
+ declare class OpenRouterEmbedder implements Embedder {
404
+ private options;
405
+ readonly dimensions: number;
406
+ private client;
407
+ constructor(options: OpenRouterEmbedderOptions);
408
+ private getModelDimensions;
409
+ embed(text: string): Promise<number[]>;
410
+ embedBatch(texts: string[]): Promise<number[][]>;
411
+ }
412
+
314
413
  declare function keywordSearch(text: string, query: string): number;
315
414
  declare function combineScores(semanticScore: number, keywordScore: number, semanticWeight?: number): number;
316
415
 
317
- export { ApiDataSource, type ApiDataSourceOptions, type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, type QueryOptions, type QueryResult, type SyncEvent, type SyncEventHandler, WebUrlSource, type WebUrlSourceOptions, combineScores, keywordSearch };
416
+ export { ApiDataSource, type ApiDataSourceOptions, type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, JSONSource, type JSONSourceOptions, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, OpenRouterEmbedder, type OpenRouterEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, PostgresSource, type PostgresSourceOptions, type QueryOptions, type QueryResult, SQLiteSource, type SQLiteSourceOptions, type SyncEvent, type SyncEventHandler, WebUrlSource, type WebUrlSourceOptions, combineScores, keywordSearch };
package/dist/index.d.ts CHANGED
@@ -120,6 +120,14 @@ declare class Knowledge {
120
120
  private getAllChunks;
121
121
  sync(): Promise<void>;
122
122
  private embedChunks;
123
+ /**
124
+ * Add a single content item to the knowledge base without triggering a full re-sync.
125
+ * This is useful for runtime additions like conversation history or agent state.
126
+ * @param content The text content to add
127
+ * @param metadata Optional metadata to attach to the chunk
128
+ * @returns The ID of the added chunk
129
+ */
130
+ add(content: string, metadata?: Record<string, unknown>): Promise<string>;
123
131
  stop(): Promise<void>;
124
132
  toTool(): KnowledgeTool;
125
133
  }
@@ -276,6 +284,78 @@ declare class ApiDataSource implements KnowledgeSource {
276
284
  private generateChunkId;
277
285
  }
278
286
 
287
+ interface JSONSourceOptions {
288
+ namespace?: string;
289
+ metadata?: Record<string, unknown>;
290
+ filter?: (item: unknown) => boolean;
291
+ chunkSize?: number;
292
+ /** Required. Transform each JSON item into a human-readable string for AI embedding. */
293
+ toContent: (item: unknown) => string;
294
+ }
295
+ /**
296
+ * Knowledge source for JSON files.
297
+ * Supports jq-like filtering and chunking of large arrays.
298
+ */
299
+ declare class JSONSource implements KnowledgeSource {
300
+ private filePath;
301
+ private options;
302
+ constructor(filePath: string, options: JSONSourceOptions);
303
+ load(): AsyncIterable<Chunk>;
304
+ }
305
+
306
+ interface SQLiteSourceOptions {
307
+ namespace?: string;
308
+ metadata?: Record<string, unknown>;
309
+ query?: string;
310
+ chunkSize?: number;
311
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
312
+ toContent: (row: Record<string, unknown>) => string;
313
+ preLoadCSV?: {
314
+ tableName: string;
315
+ csvPath: string;
316
+ delimiter?: string;
317
+ headers?: boolean;
318
+ };
319
+ }
320
+ /**
321
+ * Knowledge source for SQLite databases.
322
+ * Supports SQL queries and optional CSV/TSV pre-loading.
323
+ * Note: This requires the 'better-sqlite3' package to be installed.
324
+ */
325
+ declare class SQLiteSource implements KnowledgeSource {
326
+ private dbPath;
327
+ private options;
328
+ constructor(dbPath: string, options: SQLiteSourceOptions);
329
+ load(): AsyncIterable<Chunk>;
330
+ private loadCSV;
331
+ }
332
+
333
+ interface PostgresSourceOptions {
334
+ namespace?: string;
335
+ metadata?: Record<string, unknown>;
336
+ query: string;
337
+ chunkSize?: number;
338
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
339
+ toContent: (row: Record<string, unknown>) => string;
340
+ connectionString?: string;
341
+ host?: string;
342
+ port?: number;
343
+ database?: string;
344
+ user?: string;
345
+ password?: string;
346
+ ssl?: boolean;
347
+ }
348
+ /**
349
+ * Knowledge source for PostgreSQL databases.
350
+ * Supports SQL queries with optional chunking.
351
+ * Note: This requires the 'pg' package to be installed.
352
+ */
353
+ declare class PostgresSource implements KnowledgeSource {
354
+ private options;
355
+ constructor(options: PostgresSourceOptions);
356
+ load(): AsyncIterable<Chunk>;
357
+ }
358
+
279
359
  interface OllamaEmbedderOptions {
280
360
  model: string;
281
361
  baseUrl?: string;
@@ -311,7 +391,26 @@ declare class OpenAIEmbedder implements Embedder {
311
391
  embedBatch(texts: string[]): Promise<number[][]>;
312
392
  }
313
393
 
394
+ interface OpenRouterEmbedderOptions {
395
+ model: string;
396
+ apiKey: string;
397
+ /** Override output dimensions for models not in the built-in map */
398
+ dimensions?: number;
399
+ retries?: number;
400
+ retryDelay?: number;
401
+ timeout?: number;
402
+ }
403
+ declare class OpenRouterEmbedder implements Embedder {
404
+ private options;
405
+ readonly dimensions: number;
406
+ private client;
407
+ constructor(options: OpenRouterEmbedderOptions);
408
+ private getModelDimensions;
409
+ embed(text: string): Promise<number[]>;
410
+ embedBatch(texts: string[]): Promise<number[][]>;
411
+ }
412
+
314
413
  declare function keywordSearch(text: string, query: string): number;
315
414
  declare function combineScores(semanticScore: number, keywordScore: number, semanticWeight?: number): number;
316
415
 
317
- export { ApiDataSource, type ApiDataSourceOptions, type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, type QueryOptions, type QueryResult, type SyncEvent, type SyncEventHandler, WebUrlSource, type WebUrlSourceOptions, combineScores, keywordSearch };
416
+ export { ApiDataSource, type ApiDataSourceOptions, type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, JSONSource, type JSONSourceOptions, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, OpenRouterEmbedder, type OpenRouterEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, PostgresSource, type PostgresSourceOptions, type QueryOptions, type QueryResult, SQLiteSource, type SQLiteSourceOptions, type SyncEvent, type SyncEventHandler, WebUrlSource, type WebUrlSourceOptions, combineScores, keywordSearch };
package/dist/index.js CHANGED
@@ -1,4 +1,12 @@
1
- var v=class extends Error{constructor(t,n){super(t);this.code=n;this.name="KnowledgeError"}code},k=class extends v{constructor(t,n){super(t,"EMBEDDING_ERROR");this.statusCode=n;this.name="EmbeddingError"}statusCode},b=class extends v{constructor(t,n){super(t,"INGESTION_ERROR");this.file=n;this.name="IngestionError"}file},_=class extends v{constructor(t,n){super(t,"CHUNK_TOO_LARGE");this.chunkSize=n;this.name="ChunkTooLargeError"}chunkSize},C=class extends v{expected;actual;constructor(e,t){super(`Dimension mismatch: expected ${e}, got ${t}`,"DIMENSION_MISMATCH"),this.name="DimensionMismatchError",this.expected=e,this.actual=t}},E=class extends v{constructor(e){super(e,"PROVIDER_ERROR"),this.name="KnowledgeProviderError"}};function x(d,e){let t=d.toLowerCase(),n=e.toLowerCase();if(t.includes(n))return 1;let r=n.split(/\s+/).filter(o=>o.length>2);if(r.length===0)return 0;let s=0;for(let o of r)t.includes(o)&&s++;return s/r.length}function M(d,e,t=.7){let n=1-t;return d*t+e*n}function T(d,e){if(d.length!==e.length)throw new Error("Vectors must have same dimensions");let t=0,n=0,r=0;for(let o=0;o<d.length;o++)t+=d[o]*e[o],n+=d[o]*d[o],r+=e[o]*e[o];let s=Math.sqrt(n)*Math.sqrt(r);return s===0?0:t/s}function w(d,e){if(!e)return!0;for(let[t,n]of Object.entries(e)){let r=d[t];if(typeof n=="object"&&n!==null&&!Array.isArray(n)){if("$in"in n){if(!n.$in.includes(r))return!1}else if("$gt"in n){let s=n.$gt;if(typeof r!="number"||r<=s)return!1}else if("$lt"in n){let s=n.$lt;if(typeof r!="number"||r>=s)return!1}}else if(r!==n)return!1}return!0}var H=class d{constructor(e,t,n,r,s){this.provider=e;this.embedder=t;this.description=n;this.sources=r;this.options=s}provider;embedder;description;sources;options;static async create(e){await e.provider.validateDimensions(e.embedder.dimensions);let t=new d(e.provider,e.embedder,e.description,e.sources,e),n=e.reSync!==!1;return!n&&"shouldReSync"in e.provider?(e.provider.shouldReSync()&&await t.sync(),t):(n&&await t.sync(),t)}async query(e,t){let n=t?.searchType??"semantic",r=t?.semanticWeight??.7;if(n==="keyword")return this.keywordQuery(e,t);if(n==="hybrid"){let[s,o]=await Promise.all([this.semanticQuery(e,t),this.keywordQuery(e,t)]);return this.combineHybridResults(s,o,r,t)}else return this.semanticQuery(e,t)}async semanticQuery(e,t){let n=await this.embedder.embed(e);return this.provider.query(n,t)}async keywordQuery(e,t){let{limit:n=10,threshold:r=.1,filter:s,includeMetadata:o=!0,includeVectors:a=!1}=t||{};if(typeof this.provider.keywordQuery=="function")return this.provider.keywordQuery(e,t);let m=await this.getAllChunks(),i=[];for(let u of m){if(s&&!w(u.metadata,s))continue;let c=x(u.content,e);c>=r&&i.push({chunk:{id:u.id,content:u.content,metadata:o?u.metadata:{},vector:a?u.vector:void 0},score:c,distance:1-c})}return i.sort((u,c)=>c.score-u.score),i.slice(0,n)}combineHybridResults(e,t,n,r){let{limit:s=10,threshold:o=.5,includeMetadata:a=!0,includeVectors:m=!1}=r||{},i=new Map(e.map(l=>[l.chunk.id,l])),u=new Map(t.map(l=>[l.chunk.id,l])),c=[],h=new Set([...i.keys(),...u.keys()]);for(let l of h){let p=i.get(l),P=u.get(l);if(!p&&!P)continue;let G=p?.score??0,X=P?.score??0,A=M(G,X,n);A>=o&&c.push({chunk:{id:l,content:p?.chunk.content??P.chunk.content,metadata:a?p?.chunk.metadata??P.chunk.metadata:{},vector:m?p?.chunk.vector??P.chunk.vector:void 0},score:A,distance:1-A})}return c.sort((l,p)=>p.score-l.score),c.slice(0,s)}async getAllChunks(){if(typeof this.provider.getAllChunks=="function")return this.provider.getAllChunks();let e=new Array(this.embedder.dimensions).fill(0);return(await this.provider.query(e,{limit:1e4,threshold:0})).map(t=>t.chunk)}async sync(){this.options.onSync?.({type:"start"});try{let e=this.embedder.dimensions;await this.provider.clear(),await this.provider.validateDimensions(e);let t=this.options.streamingBatchSize??100,n=0,r=[];for(let s of this.sources)for await(let o of s.load())if(r.push(o),r.length>=t){let a=await this.embedChunks(r);a.length>0&&(await this.provider.add(a),n+=a.length),r.length=0}if(r.length>0){let s=await this.embedChunks(r);s.length>0&&(await this.provider.add(s),n+=s.length)}this.options.onSync?.({type:"complete",chunksAffected:n})}catch(e){throw this.options.onSync?.({type:"error",error:e}),e}}async embedChunks(e){if(e.length===0)return[];let t=[];try{let n=e.map(s=>s.content),r=await this.embedder.embedBatch(n);for(let s=0;s<e.length;s++)t.push({...e[s],vector:r[s]}),this.options.onEmbeddingProgress?.({source:"sync",current:s+1,total:e.length,percent:Math.round((s+1)/e.length*100)})}catch(n){if(this.options.onError?.(n,{})==="abort")throw n;for(let s=0;s<e.length;s++)try{let o=await this.embedder.embed(e[s].content);t.push({...e[s],vector:o}),this.options.onEmbeddingProgress?.({source:"sync",current:s+1,total:e.length,percent:Math.round((s+1)/e.length*100)})}catch(o){if(this.options.onError?.(o,{chunk:e[s]})==="abort")throw o}}return t}async stop(){this.provider.close&&this.provider.close()}toTool(){return{name:"knowledge_search",displayName:"Knowledge Search",description:this.description||"Search the knowledge base for relevant information",category:"search",cacheable:!1,parameters:{type:"object",properties:{query:{type:"string",description:"Search query to find relevant information"},limit:{type:"number",description:"Maximum number of results to return (default: 10)"},threshold:{type:"number",description:"Minimum similarity threshold 0-1 (default: 0.7)"},filter:{type:"object",description:"Optional metadata filters"}},required:["query"]},execute:async e=>(await this.query(e.query,{limit:e.limit,threshold:e.threshold,filter:e.filter})).map(n=>({content:n.chunk.content,score:n.score,metadata:n.chunk.metadata}))}}};var I=class{constructor(e={}){this.options=e}options;chunks=new Map;dimensions;async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new C(this.dimensions,e);this.dimensions=e}async add(e){for(let t of e){if(!t.vector)throw new E("Chunk missing vector");if(this.options.maxChunks&&this.chunks.size>=this.options.maxChunks)throw new E(`Max chunks limit reached: ${this.options.maxChunks}`);this.chunks.set(t.id,{chunk:{id:t.id,content:t.content,metadata:t.metadata},vector:t.vector})}}async query(e,t={}){let{limit:n=10,threshold:r=.7,filter:s,includeMetadata:o=!0,includeVectors:a=!1}=t,m=[];for(let{chunk:i,vector:u}of this.chunks.values()){if(s&&!w(i.metadata,s))continue;let c=T(e,u);c>=r&&m.push({chunk:{id:i.id,content:i.content,metadata:o?i.metadata:{},vector:a?u:void 0},score:c,distance:1-c})}return m.sort((i,u)=>u.score-i.score),m.slice(0,n)}async keywordQuery(e,t={}){let{limit:n=10,threshold:r=.1,filter:s,includeMetadata:o=!0,includeVectors:a=!1}=t,m=[];for(let{chunk:i,vector:u}of this.chunks.values()){if(s&&!w(i.metadata,s))continue;let c=x(i.content,e);c>=r&&m.push({chunk:{id:i.id,content:i.content,metadata:o?i.metadata:{},vector:a?u:void 0},score:c,distance:1-c})}return m.sort((i,u)=>u.score-i.score),m.slice(0,n)}async delete(e){for(let t of e)this.chunks.delete(t)}async clear(){this.chunks.clear(),this.dimensions=void 0}async getAllChunks(){return Array.from(this.chunks.values()).map(({chunk:e,vector:t})=>({...e,vector:t}))}};import J from"better-sqlite3";import*as j from"fs";import*as D from"path";import*as B from"os";var N=class{constructor(e){this.options=e;let t=e.storagePath||D.join(B.homedir(),".toolpack","knowledge");this.dbPath=D.join(t,`${e.namespace}.db`),j.mkdirSync(t,{recursive:!0}),this.db=new J(this.dbPath),this.db.pragma("journal_mode = WAL"),this.initSchema(),this.loadDimensions()}options;db;dimensions;dbPath;initSchema(){this.db.exec(`
1
+ var fs=Object.create;var Be=Object.defineProperty;var ps=Object.getOwnPropertyDescriptor;var ms=Object.getOwnPropertyNames;var ys=Object.getPrototypeOf,gs=Object.prototype.hasOwnProperty;var S=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var or=(r,e)=>()=>(r&&(e=r(r=0)),e);var b=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports),bs=(r,e)=>{for(var t in e)Be(r,t,{get:e[t],enumerable:!0})},ws=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ms(e))!gs.call(r,s)&&s!==t&&Be(r,s,{get:()=>e[s],enumerable:!(n=ps(e,s))||n.enumerable});return r};var Ss=(r,e,t)=>(t=r!=null?fs(ys(r)):{},ws(e||!r||!r.__esModule?Be(t,"default",{value:r,enumerable:!0}):t,r));import gc from"path";import{fileURLToPath as wc}from"url";var u=or(()=>{"use strict"});var Xe=b(yr=>{"use strict";u();yr.parse=function(r,e){return new Je(r,e).parse()};var Je=class r{constructor(e,t){this.source=e,this.transform=t||xs,this.position=0,this.entries=[],this.recorded=[],this.dimension=0}isEof(){return this.position>=this.source.length}nextCharacter(){var e=this.source[this.position++];return e==="\\"?{value:this.source[this.position++],escaped:!0}:{value:e,escaped:!1}}record(e){this.recorded.push(e)}newEntry(e){var t;(this.recorded.length>0||e)&&(t=this.recorded.join(""),t==="NULL"&&!e&&(t=null),t!==null&&(t=this.transform(t)),this.entries.push(t),this.recorded=[])}consumeDimensions(){if(this.source[0]==="[")for(;!this.isEof();){var e=this.nextCharacter();if(e.value==="=")break}}parse(e){var t,n,s;for(this.consumeDimensions();!this.isEof();)if(t=this.nextCharacter(),t.value==="{"&&!s)this.dimension++,this.dimension>1&&(n=new r(this.source.substr(this.position-1),this.transform),this.entries.push(n.parse(!0)),this.position+=n.position-2);else if(t.value==="}"&&!s){if(this.dimension--,!this.dimension&&(this.newEntry(),e))return this.entries}else t.value==='"'&&!t.escaped?(s&&this.newEntry(!0),s=!s):t.value===","&&!s?this.newEntry():this.record(t.value);if(this.dimension!==0)throw new Error("array dimension not balanced");return this.entries}};function xs(r){return r}});var Ze=b((pu,gr)=>{"use strict";u();var Ps=Xe();gr.exports={create:function(r,e){return{parse:function(){return Ps.parse(r,e)}}}}});var Sr=b((yu,wr)=>{"use strict";u();var As=/(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?.*?( BC)?$/,Rs=/^(\d{1,})-(\d{2})-(\d{2})( BC)?$/,Ts=/([Z+-])(\d{2})?:?(\d{2})?:?(\d{2})?/,Os=/^-?infinity$/;wr.exports=function(e){if(Os.test(e))return Number(e.replace("i","I"));var t=As.exec(e);if(!t)return Is(e)||null;var n=!!t[8],s=parseInt(t[1],10);n&&(s=br(s));var i=parseInt(t[2],10)-1,o=t[3],a=parseInt(t[4],10),l=parseInt(t[5],10),c=parseInt(t[6],10),h=t[7];h=h?1e3*parseFloat(h):0;var d,m=Ms(e);return m!=null?(d=new Date(Date.UTC(s,i,o,a,l,c,h)),et(s)&&d.setUTCFullYear(s),m!==0&&d.setTime(d.getTime()-m)):(d=new Date(s,i,o,a,l,c,h),et(s)&&d.setFullYear(s)),d};function Is(r){var e=Rs.exec(r);if(e){var t=parseInt(e[1],10),n=!!e[4];n&&(t=br(t));var s=parseInt(e[2],10)-1,i=e[3],o=new Date(t,s,i);return et(t)&&o.setFullYear(t),o}}function Ms(r){if(r.endsWith("+00"))return 0;var e=Ts.exec(r.split(" ")[1]);if(e){var t=e[1];if(t==="Z")return 0;var n=t==="-"?-1:1,s=parseInt(e[2],10)*3600+parseInt(e[3]||0,10)*60+parseInt(e[4]||0,10);return s*n*1e3}}function br(r){return-(r-1)}function et(r){return r>=0&&r<100}});var Er=b((bu,vr)=>{"use strict";u();vr.exports=Ls;var qs=Object.prototype.hasOwnProperty;function Ls(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)qs.call(t,n)&&(r[n]=t[n])}return r}});var Cr=b((Su,kr)=>{"use strict";u();var Ds=Er();kr.exports=J;function J(r){if(!(this instanceof J))return new J(r);Ds(this,Vs(r))}var Qs=["seconds","minutes","hours","days","months","years"];J.prototype.toPostgres=function(){var r=Qs.filter(this.hasOwnProperty,this);return this.milliseconds&&r.indexOf("seconds")<0&&r.push("seconds"),r.length===0?"0":r.map(function(e){var t=this[e]||0;return e==="seconds"&&this.milliseconds&&(t=(t+this.milliseconds/1e3).toFixed(6).replace(/\.?0+$/,"")),t+" "+e},this).join(" ")};var Ns={years:"Y",months:"M",days:"D",hours:"H",minutes:"M",seconds:"S"},Bs=["years","months","days"],Fs=["hours","minutes","seconds"];J.prototype.toISOString=J.prototype.toISO=function(){var r=Bs.map(t,this).join(""),e=Fs.map(t,this).join("");return"P"+r+"T"+e;function t(n){var s=this[n]||0;return n==="seconds"&&this.milliseconds&&(s=(s+this.milliseconds/1e3).toFixed(6).replace(/0+$/,"")),s+Ns[n]}};var tt="([+-]?\\d+)",Us=tt+"\\s+years?",js=tt+"\\s+mons?",$s=tt+"\\s+days?",Hs="([+-])?([\\d]*):(\\d\\d):(\\d\\d)\\.?(\\d{1,6})?",zs=new RegExp([Us,js,$s,Hs].map(function(r){return"("+r+")?"}).join("\\s*")),_r={years:2,months:4,days:6,hours:9,minutes:10,seconds:11,milliseconds:12},Ks=["hours","minutes","seconds","milliseconds"];function Gs(r){var e=r+"000000".slice(r.length);return parseInt(e,10)/1e3}function Vs(r){if(!r)return{};var e=zs.exec(r),t=e[8]==="-";return Object.keys(_r).reduce(function(n,s){var i=_r[s],o=e[i];return!o||(o=s==="milliseconds"?Gs(o):parseInt(o,10),!o)||(t&&~Ks.indexOf(s)&&(o*=-1),n[s]=o),n},{})}});var Ar=b((Eu,Pr)=>{"use strict";u();var xr=Buffer.from||Buffer;Pr.exports=function(e){if(/^\\x/.test(e))return xr(e.substr(2),"hex");for(var t="",n=0;n<e.length;)if(e[n]!=="\\")t+=e[n],++n;else if(/[0-7]{3}/.test(e.substr(n+1,3)))t+=String.fromCharCode(parseInt(e.substr(n+1,3),8)),n+=4;else{for(var s=1;n+s<e.length&&e[n+s]==="\\";)s++;for(var i=0;i<Math.floor(s/2);++i)t+="\\";n+=Math.floor(s/2)*2}return xr(t,"binary")}});var Lr=b((ku,qr)=>{"use strict";u();var ue=Xe(),le=Ze(),ve=Sr(),Tr=Cr(),Or=Ar();function Ee(r){return function(t){return t===null?t:r(t)}}function Ir(r){return r===null?r:r==="TRUE"||r==="t"||r==="true"||r==="y"||r==="yes"||r==="on"||r==="1"}function Ws(r){return r?ue.parse(r,Ir):null}function Ys(r){return parseInt(r,10)}function rt(r){return r?ue.parse(r,Ee(Ys)):null}function Js(r){return r?ue.parse(r,Ee(function(e){return Mr(e).trim()})):null}var Xs=function(r){if(!r)return null;var e=le.create(r,function(t){return t!==null&&(t=ot(t)),t});return e.parse()},nt=function(r){if(!r)return null;var e=le.create(r,function(t){return t!==null&&(t=parseFloat(t)),t});return e.parse()},q=function(r){if(!r)return null;var e=le.create(r);return e.parse()},st=function(r){if(!r)return null;var e=le.create(r,function(t){return t!==null&&(t=ve(t)),t});return e.parse()},Zs=function(r){if(!r)return null;var e=le.create(r,function(t){return t!==null&&(t=Tr(t)),t});return e.parse()},ei=function(r){return r?ue.parse(r,Ee(Or)):null},it=function(r){return parseInt(r,10)},Mr=function(r){var e=String(r);return/^\d+$/.test(e)?e:r},Rr=function(r){return r?ue.parse(r,Ee(JSON.parse)):null},ot=function(r){return r[0]!=="("?null:(r=r.substring(1,r.length-1).split(","),{x:parseFloat(r[0]),y:parseFloat(r[1])})},ti=function(r){if(r[0]!=="<"&&r[1]!=="(")return null;for(var e="(",t="",n=!1,s=2;s<r.length-1;s++){if(n||(e+=r[s]),r[s]===")"){n=!0;continue}else if(!n)continue;r[s]!==","&&(t+=r[s])}var i=ot(e);return i.radius=parseFloat(t),i},ri=function(r){r(20,Mr),r(21,it),r(23,it),r(26,it),r(700,parseFloat),r(701,parseFloat),r(16,Ir),r(1082,ve),r(1114,ve),r(1184,ve),r(600,ot),r(651,q),r(718,ti),r(1e3,Ws),r(1001,ei),r(1005,rt),r(1007,rt),r(1028,rt),r(1016,Js),r(1017,Xs),r(1021,nt),r(1022,nt),r(1231,nt),r(1014,q),r(1015,q),r(1008,q),r(1009,q),r(1040,q),r(1041,q),r(1115,st),r(1182,st),r(1185,st),r(1186,Tr),r(1187,Zs),r(17,Or),r(114,JSON.parse.bind(JSON)),r(3802,JSON.parse.bind(JSON)),r(199,Rr),r(3807,Rr),r(3907,q),r(2951,q),r(791,q),r(1183,q),r(1270,q)};qr.exports={init:ri}});var Qr=b((xu,Dr)=>{"use strict";u();var R=1e6;function ni(r){var e=r.readInt32BE(0),t=r.readUInt32BE(4),n="";e<0&&(e=~e+(t===0),t=~t+1>>>0,n="-");var s="",i,o,a,l,c,h;{if(i=e%R,e=e/R>>>0,o=4294967296*i+t,t=o/R>>>0,a=""+(o-R*t),t===0&&e===0)return n+a+s;for(l="",c=6-a.length,h=0;h<c;h++)l+="0";s=l+a+s}{if(i=e%R,e=e/R>>>0,o=4294967296*i+t,t=o/R>>>0,a=""+(o-R*t),t===0&&e===0)return n+a+s;for(l="",c=6-a.length,h=0;h<c;h++)l+="0";s=l+a+s}{if(i=e%R,e=e/R>>>0,o=4294967296*i+t,t=o/R>>>0,a=""+(o-R*t),t===0&&e===0)return n+a+s;for(l="",c=6-a.length,h=0;h<c;h++)l+="0";s=l+a+s}return i=e%R,o=4294967296*i+t,a=""+o%R,n+a+s}Dr.exports=ni});var jr=b((Au,Ur)=>{"use strict";u();var si=Qr(),E=function(r,e,t,n,s){t=t||0,n=n||!1,s=s||function(y,w,T){return y*Math.pow(2,T)+w};var i=t>>3,o=function(y){return n?~y&255:y},a=255,l=8-t%8;e<l&&(a=255<<8-e&255,l=e),t&&(a=a>>t%8);var c=0;t%8+e>=8&&(c=s(0,o(r[i])&a,l));for(var h=e+t>>3,d=i+1;d<h;d++)c=s(c,o(r[d]),8);var m=(e+t)%8;return m>0&&(c=s(c,o(r[h])>>8-m,m)),c},Fr=function(r,e,t){var n=Math.pow(2,t-1)-1,s=E(r,1),i=E(r,t,1);if(i===0)return 0;var o=1,a=function(c,h,d){c===0&&(c=1);for(var m=1;m<=d;m++)o/=2,(h&1<<d-m)>0&&(c+=o);return c},l=E(r,e,t+1,!1,a);return i==Math.pow(2,t+1)-1?l===0?s===0?1/0:-1/0:NaN:(s===0?1:-1)*Math.pow(2,i-n)*l},ii=function(r){return E(r,1)==1?-1*(E(r,15,1,!0)+1):E(r,15,1)},Nr=function(r){return E(r,1)==1?-1*(E(r,31,1,!0)+1):E(r,31,1)},oi=function(r){return Fr(r,23,8)},ai=function(r){return Fr(r,52,11)},ci=function(r){var e=E(r,16,32);if(e==49152)return NaN;for(var t=Math.pow(1e4,E(r,16,16)),n=0,s=[],i=E(r,16),o=0;o<i;o++)n+=E(r,16,64+16*o)*t,t/=1e4;var a=Math.pow(10,E(r,16,48));return(e===0?1:-1)*Math.round(n*a)/a},Br=function(r,e){var t=E(e,1),n=E(e,63,1),s=new Date((t===0?1:-1)*n/1e3+9466848e5);return r||s.setTime(s.getTime()+s.getTimezoneOffset()*6e4),s.usec=n%1e3,s.getMicroSeconds=function(){return this.usec},s.setMicroSeconds=function(i){this.usec=i},s.getUTCMicroSeconds=function(){return this.usec},s},he=function(r){for(var e=E(r,32),t=E(r,32,32),n=E(r,32,64),s=96,i=[],o=0;o<e;o++)i[o]=E(r,32,s),s+=32,s+=32;var a=function(c){var h=E(r,32,s);if(s+=32,h==4294967295)return null;var d;if(c==23||c==20)return d=E(r,h*8,s),s+=h*8,d;if(c==25)return d=r.toString(this.encoding,s>>3,(s+=h<<3)>>3),d;console.log("ERROR: ElementType not implemented: "+c)},l=function(c,h){var d=[],m;if(c.length>1){var y=c.shift();for(m=0;m<y;m++)d[m]=l(c,h);c.unshift(y)}else for(m=0;m<c[0];m++)d[m]=a(h);return d};return l(i,n)},ui=function(r){return r.toString("utf8")},li=function(r){return r===null?null:E(r,8)>0},hi=function(r){r(20,si),r(21,ii),r(23,Nr),r(26,Nr),r(1700,ci),r(700,oi),r(701,ai),r(16,li),r(1114,Br.bind(null,!1)),r(1184,Br.bind(null,!0)),r(1e3,he),r(1007,he),r(1016,he),r(1008,he),r(1009,he),r(25,ui)};Ur.exports={init:hi}});var Hr=b((Tu,$r)=>{"use strict";u();$r.exports={BOOL:16,BYTEA:17,CHAR:18,INT8:20,INT2:21,INT4:23,REGPROC:24,TEXT:25,OID:26,TID:27,XID:28,CID:29,JSON:114,XML:142,PG_NODE_TREE:194,SMGR:210,PATH:602,POLYGON:604,CIDR:650,FLOAT4:700,FLOAT8:701,ABSTIME:702,RELTIME:703,TINTERVAL:704,CIRCLE:718,MACADDR8:774,MONEY:790,MACADDR:829,INET:869,ACLITEM:1033,BPCHAR:1042,VARCHAR:1043,DATE:1082,TIME:1083,TIMESTAMP:1114,TIMESTAMPTZ:1184,INTERVAL:1186,TIMETZ:1266,BIT:1560,VARBIT:1562,NUMERIC:1700,REFCURSOR:1790,REGPROCEDURE:2202,REGOPER:2203,REGOPERATOR:2204,REGCLASS:2205,REGTYPE:2206,UUID:2950,TXID_SNAPSHOT:2970,PG_LSN:3220,PG_NDISTINCT:3361,PG_DEPENDENCIES:3402,TSVECTOR:3614,TSQUERY:3615,GTSVECTOR:3642,REGCONFIG:3734,REGDICTIONARY:3769,JSONB:3802,REGNAMESPACE:4089,REGROLE:4096}});var pe=b(fe=>{"use strict";u();var di=Lr(),fi=jr(),pi=Ze(),mi=Hr();fe.getTypeParser=yi;fe.setTypeParser=gi;fe.arrayParser=pi;fe.builtins=mi;var de={text:{},binary:{}};function zr(r){return String(r)}function yi(r,e){return e=e||"text",de[e]&&de[e][r]||zr}function gi(r,e,t){typeof e=="function"&&(t=e,e="text"),de[e][r]=t}di.init(function(r,e){de.text[r]=e});fi.init(function(r,e){de.binary[r]=e})});var me=b((qu,at)=>{"use strict";u();var Kr;try{Kr=process.platform==="win32"?process.env.USERNAME:process.env.USER}catch{}at.exports={host:"localhost",user:Kr,database:void 0,password:null,connectionString:void 0,port:5432,rows:0,binary:!1,max:10,idleTimeoutMillis:3e4,client_encoding:"",ssl:!1,application_name:void 0,fallback_application_name:void 0,options:void 0,parseInputDatesAsUTC:!1,statement_timeout:!1,lock_timeout:!1,idle_in_transaction_session_timeout:!1,query_timeout:!1,connect_timeout:0,keepalives:1,keepalives_idle:0};var X=pe(),bi=X.getTypeParser(20,"text"),wi=X.getTypeParser(1016,"text");at.exports.__defineSetter__("parseInt8",function(r){X.setTypeParser(20,"text",r?X.getTypeParser(23,"text"):bi),X.setTypeParser(1016,"text",r?X.getTypeParser(1007,"text"):wi)})});var Z=b((Du,Wr)=>{"use strict";u();var Si=me(),Gr=S("util"),{isDate:vi}=Gr.types||Gr;function Ei(r){return'"'+r.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+'"'}function Vr(r){let e="{";for(let t=0;t<r.length;t++)if(t>0&&(e=e+","),r[t]===null||typeof r[t]>"u")e=e+"NULL";else if(Array.isArray(r[t]))e=e+Vr(r[t]);else if(ArrayBuffer.isView(r[t])){let n=r[t];if(!(n instanceof Buffer)){let s=Buffer.from(n.buffer,n.byteOffset,n.byteLength);s.length===n.byteLength?n=s:n=s.slice(n.byteOffset,n.byteOffset+n.byteLength)}e+="\\\\x"+n.toString("hex")}else e+=Ei(_e(r[t]));return e=e+"}",e}var _e=function(r,e){if(r==null)return null;if(typeof r=="object"){if(r instanceof Buffer)return r;if(ArrayBuffer.isView(r)){let t=Buffer.from(r.buffer,r.byteOffset,r.byteLength);return t.length===r.byteLength?t:t.slice(r.byteOffset,r.byteOffset+r.byteLength)}return vi(r)?Si.parseInputDatesAsUTC?Ci(r):ki(r):Array.isArray(r)?Vr(r):_i(r,e)}return r.toString()};function _i(r,e){if(r&&typeof r.toPostgres=="function"){if(e=e||[],e.indexOf(r)!==-1)throw new Error('circular reference detected while preparing "'+r+'" for query');return e.push(r),_e(r.toPostgres(_e),e)}return JSON.stringify(r)}function ki(r){let e=-r.getTimezoneOffset(),t=r.getFullYear(),n=t<1;n&&(t=Math.abs(t)+1);let s=String(t).padStart(4,"0")+"-"+String(r.getMonth()+1).padStart(2,"0")+"-"+String(r.getDate()).padStart(2,"0")+"T"+String(r.getHours()).padStart(2,"0")+":"+String(r.getMinutes()).padStart(2,"0")+":"+String(r.getSeconds()).padStart(2,"0")+"."+String(r.getMilliseconds()).padStart(3,"0");return e<0?(s+="-",e*=-1):s+="+",s+=String(Math.floor(e/60)).padStart(2,"0")+":"+String(e%60).padStart(2,"0"),n&&(s+=" BC"),s}function Ci(r){let e=r.getUTCFullYear(),t=e<1;t&&(e=Math.abs(e)+1);let n=String(e).padStart(4,"0")+"-"+String(r.getUTCMonth()+1).padStart(2,"0")+"-"+String(r.getUTCDate()).padStart(2,"0")+"T"+String(r.getUTCHours()).padStart(2,"0")+":"+String(r.getUTCMinutes()).padStart(2,"0")+":"+String(r.getUTCSeconds()).padStart(2,"0")+"."+String(r.getUTCMilliseconds()).padStart(3,"0");return n+="+00:00",t&&(n+=" BC"),n}function xi(r,e,t){return r=typeof r=="string"?{text:r}:r,e&&(typeof e=="function"?r.callback=e:r.values=e),t&&(r.callback=t),r}var Pi=function(r){return'"'+r.replace(/"/g,'""')+'"'},Ai=function(r){let e=!1,t="'";if(r==null||typeof r!="string")return"''";for(let n=0;n<r.length;n++){let s=r[n];s==="'"?t+=s+s:s==="\\"?(t+=s+s,e=!0):t+=s}return t+="'",e===!0&&(t=" E"+t),t};Wr.exports={prepareValue:function(e){return _e(e)},normalizeQueryConfig:xi,escapeIdentifier:Pi,escapeLiteral:Ai}});var Jr=b((Nu,Yr)=>{"use strict";u();var ee=S("crypto");function ct(r){return ee.createHash("md5").update(r,"utf-8").digest("hex")}function Ri(r,e,t){let n=ct(e+r);return"md5"+ct(Buffer.concat([Buffer.from(n),t]))}function Ti(r){return ee.createHash("sha256").update(r).digest()}function Oi(r,e){return r=r.replace(/(\D)-/,"$1"),ee.createHash(r).update(e).digest()}function Ii(r,e){return ee.createHmac("sha256",r).update(e).digest()}async function Mi(r,e,t){return ee.pbkdf2Sync(r,e,t,32,"sha256")}Yr.exports={postgresMd5PasswordHash:Ri,randomBytes:ee.randomBytes,deriveKey:Mi,sha256:Ti,hashByName:Oi,hmacSha256:Ii,md5:ct}});var tn=b((Fu,en)=>{"use strict";u();var Xr=S("crypto");en.exports={postgresMd5PasswordHash:Li,randomBytes:qi,deriveKey:Bi,sha256:Di,hashByName:Qi,hmacSha256:Ni,md5:ut};var Zr=Xr.webcrypto||globalThis.crypto,H=Zr.subtle,lt=new TextEncoder;function qi(r){return Zr.getRandomValues(Buffer.alloc(r))}async function ut(r){try{return Xr.createHash("md5").update(r,"utf-8").digest("hex")}catch{let t=typeof r=="string"?lt.encode(r):r,n=await H.digest("MD5",t);return Array.from(new Uint8Array(n)).map(s=>s.toString(16).padStart(2,"0")).join("")}}async function Li(r,e,t){let n=await ut(e+r);return"md5"+await ut(Buffer.concat([Buffer.from(n),t]))}async function Di(r){return await H.digest("SHA-256",r)}async function Qi(r,e){return await H.digest(r,e)}async function Ni(r,e){let t=await H.importKey("raw",r,{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return await H.sign("HMAC",t,lt.encode(e))}async function Bi(r,e,t){let n=await H.importKey("raw",lt.encode(r),"PBKDF2",!1,["deriveBits"]),s={name:"PBKDF2",hash:"SHA-256",salt:e,iterations:t};return await H.deriveBits(s,n,256,["deriveBits"])}});var dt=b((ju,ht)=>{"use strict";u();var Fi=parseInt(process.versions&&process.versions.node&&process.versions.node.split(".")[0])<15;Fi?ht.exports=Jr():ht.exports=tn()});var sn=b((Hu,nn)=>{"use strict";u();function z(r,e){return new Error("SASL channel binding: "+r+" when parsing public certificate "+e.toString("base64"))}function ft(r,e){let t=r[e++];if(t<128)return{length:t,index:e};let n=t&127;if(n>4)throw z("bad length",r);t=0;for(let s=0;s<n;s++)t=t<<8|r[e++];return{length:t,index:e}}function rn(r,e){if(r[e++]!==6)throw z("non-OID data",r);let{length:t,index:n}=ft(r,e);e=n;let s=e+t,i=r[e++],o=(i/40>>0)+"."+i%40;for(;e<s;){let a=0;for(;e<s;){let l=r[e++];if(a=a<<7|l&127,l<128)break}o+="."+a}return{oid:o,index:e}}function ye(r,e){if(r[e++]!==48)throw z("non-sequence data",r);return ft(r,e)}function Ui(r,e){e===void 0&&(e=0),e=ye(r,e).index;let{length:t,index:n}=ye(r,e);e=n+t,e=ye(r,e).index;let{oid:s,index:i}=rn(r,e);switch(s){case"1.2.840.113549.1.1.4":return"MD5";case"1.2.840.113549.1.1.5":return"SHA-1";case"1.2.840.113549.1.1.11":return"SHA-256";case"1.2.840.113549.1.1.12":return"SHA-384";case"1.2.840.113549.1.1.13":return"SHA-512";case"1.2.840.113549.1.1.14":return"SHA-224";case"1.2.840.113549.1.1.15":return"SHA512-224";case"1.2.840.113549.1.1.16":return"SHA512-256";case"1.2.840.10045.4.1":return"SHA-1";case"1.2.840.10045.4.3.1":return"SHA-224";case"1.2.840.10045.4.3.2":return"SHA-256";case"1.2.840.10045.4.3.3":return"SHA-384";case"1.2.840.10045.4.3.4":return"SHA-512";case"1.2.840.113549.1.1.10":{if(e=i,e=ye(r,e).index,r[e++]!==160)throw z("non-tag data",r);e=ft(r,e).index,e=ye(r,e).index;let{oid:o}=rn(r,e);switch(o){case"1.2.840.113549.2.5":return"MD5";case"1.3.14.3.2.26":return"SHA-1";case"2.16.840.1.101.3.4.2.1":return"SHA-256";case"2.16.840.1.101.3.4.2.2":return"SHA-384";case"2.16.840.1.101.3.4.2.3":return"SHA-512"}throw z("unknown hash OID "+o,r)}case"1.3.101.110":case"1.3.101.112":return"SHA-512";case"1.3.101.111":case"1.3.101.113":throw z("Ed448 certificate channel binding is not currently supported by Postgres")}throw z("unknown OID "+s,r)}nn.exports={signatureAlgorithmHashFromCertificate:Ui}});var un=b((Ku,cn)=>{"use strict";u();var N=dt(),{signatureAlgorithmHashFromCertificate:ji}=sn();function $i(r,e){let t=["SCRAM-SHA-256"];e&&t.unshift("SCRAM-SHA-256-PLUS");let n=t.find(o=>r.includes(o));if(!n)throw new Error("SASL: Only mechanism(s) "+t.join(" and ")+" are supported");if(n==="SCRAM-SHA-256-PLUS"&&typeof e.getPeerCertificate!="function")throw new Error("SASL: Mechanism SCRAM-SHA-256-PLUS requires a certificate");let s=N.randomBytes(18).toString("base64");return{mechanism:n,clientNonce:s,response:(n==="SCRAM-SHA-256-PLUS"?"p=tls-server-end-point":e?"y":"n")+",,n=*,r="+s,message:"SASLInitialResponse"}}async function Hi(r,e,t,n){if(r.message!=="SASLInitialResponse")throw new Error("SASL: Last message was not SASLInitialResponse");if(typeof e!="string")throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string");if(e==="")throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a non-empty string");if(typeof t!="string")throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: serverData must be a string");let s=Gi(t);if(s.nonce.startsWith(r.clientNonce)){if(s.nonce.length===r.clientNonce.length)throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce is too short")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce does not start with client nonce");let i="n=*,r="+r.clientNonce,o="r="+s.nonce+",s="+s.salt+",i="+s.iteration,a=n?"eSws":"biws";if(r.mechanism==="SCRAM-SHA-256-PLUS"){let K=n.getPeerCertificate().raw,we=ji(K);(we==="MD5"||we==="SHA-1")&&(we="SHA-256");let ds=await N.hashByName(we,K);a=Buffer.concat([Buffer.from("p=tls-server-end-point,,"),Buffer.from(ds)]).toString("base64")}let l="c="+a+",r="+s.nonce,c=i+","+o+","+l,h=Buffer.from(s.salt,"base64"),d=await N.deriveKey(e,h,s.iteration),m=await N.hmacSha256(d,"Client Key"),y=await N.sha256(m),w=await N.hmacSha256(y,c),T=Wi(Buffer.from(m),Buffer.from(w)).toString("base64"),Qe=await N.hmacSha256(d,"Server Key"),Ne=await N.hmacSha256(Qe,c);r.message="SASLResponse",r.serverSignature=Buffer.from(Ne).toString("base64"),r.response=l+",p="+T}function zi(r,e){if(r.message!=="SASLResponse")throw new Error("SASL: Last message was not SASLResponse");if(typeof e!="string")throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: serverData must be a string");let{serverSignature:t}=Vi(e);if(t!==r.serverSignature)throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature does not match")}function Ki(r){if(typeof r!="string")throw new TypeError("SASL: text must be a string");return r.split("").map((e,t)=>r.charCodeAt(t)).every(e=>e>=33&&e<=43||e>=45&&e<=126)}function on(r){return/^(?:[a-zA-Z0-9+/]{4})*(?:[a-zA-Z0-9+/]{2}==|[a-zA-Z0-9+/]{3}=)?$/.test(r)}function an(r){if(typeof r!="string")throw new TypeError("SASL: attribute pairs text must be a string");return new Map(r.split(",").map(e=>{if(!/^.=/.test(e))throw new Error("SASL: Invalid attribute pair entry");let t=e[0],n=e.substring(2);return[t,n]}))}function Gi(r){let e=an(r),t=e.get("r");if(t){if(!Ki(t))throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce must only contain printable characters")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing");let n=e.get("s");if(n){if(!on(n))throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: salt must be base64")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing");let s=e.get("i");if(s){if(!/^[1-9][0-9]*$/.test(s))throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: invalid iteration count")}else throw new Error("SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing");let i=parseInt(s,10);return{nonce:t,salt:n,iteration:i}}function Vi(r){let t=an(r).get("v");if(t){if(!on(t))throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature must be base64")}else throw new Error("SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature is missing");return{serverSignature:t}}function Wi(r,e){if(!Buffer.isBuffer(r))throw new TypeError("first argument must be a Buffer");if(!Buffer.isBuffer(e))throw new TypeError("second argument must be a Buffer");if(r.length!==e.length)throw new Error("Buffer lengths must match");if(r.length===0)throw new Error("Buffers cannot be empty");return Buffer.from(r.map((t,n)=>r[n]^e[n]))}cn.exports={startSession:$i,continueSession:Hi,finalizeSession:zi}});var Ce=b((Vu,ln)=>{"use strict";u();var Yi=pe();function ke(r){this._types=r||Yi,this.text={},this.binary={}}ke.prototype.getOverrides=function(r){switch(r){case"text":return this.text;case"binary":return this.binary;default:return{}}};ke.prototype.setTypeParser=function(r,e,t){typeof e=="function"&&(t=e,e="text"),this.getOverrides(e)[r]=t};ke.prototype.getTypeParser=function(r,e){return e=e||"text",this.getOverrides(e)[r]||this._types.getTypeParser(r,e)};ln.exports=ke});var fn=b((Yu,dn)=>{"use strict";u();function te(r,e={}){if(r.charAt(0)==="/"){let l=r.split(" ");return{host:l[0],database:l[1]}}let t={},n,s=!1;/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(r)&&(r=encodeURI(r).replace(/%25(\d\d)/g,"%$1"));try{try{n=new URL(r,"postgres://base")}catch{n=new URL(r.replace("@/","@___DUMMY___/"),"postgres://base"),s=!0}}catch(l){throw l.input&&(l.input="*****REDACTED*****"),l}for(let l of n.searchParams.entries())t[l[0]]=l[1];if(t.user=t.user||decodeURIComponent(n.username),t.password=t.password||decodeURIComponent(n.password),n.protocol=="socket:")return t.host=decodeURI(n.pathname),t.database=n.searchParams.get("db"),t.client_encoding=n.searchParams.get("encoding"),t;let i=s?"":n.hostname;t.host?i&&/^%2f/i.test(i)&&(n.pathname=i+n.pathname):t.host=decodeURIComponent(i),t.port||(t.port=n.port);let o=n.pathname.slice(1)||null;t.database=o?decodeURI(o):null,(t.ssl==="true"||t.ssl==="1")&&(t.ssl=!0),t.ssl==="0"&&(t.ssl=!1),(t.sslcert||t.sslkey||t.sslrootcert||t.sslmode)&&(t.ssl={});let a=t.sslcert||t.sslkey||t.sslrootcert?S("fs"):null;if(t.sslcert&&(t.ssl.cert=a.readFileSync(t.sslcert).toString()),t.sslkey&&(t.ssl.key=a.readFileSync(t.sslkey).toString()),t.sslrootcert&&(t.ssl.ca=a.readFileSync(t.sslrootcert).toString()),e.useLibpqCompat&&t.uselibpqcompat)throw new Error("Both useLibpqCompat and uselibpqcompat are set. Please use only one of them.");if(t.uselibpqcompat==="true"||e.useLibpqCompat)switch(t.sslmode){case"disable":{t.ssl=!1;break}case"prefer":{t.ssl.rejectUnauthorized=!1;break}case"require":{t.sslrootcert?t.ssl.checkServerIdentity=function(){}:t.ssl.rejectUnauthorized=!1;break}case"verify-ca":{if(!t.ssl.ca)throw new Error("SECURITY WARNING: Using sslmode=verify-ca requires specifying a CA with sslrootcert. If a public CA is used, verify-ca allows connections to a server that somebody else may have registered with the CA, making you vulnerable to Man-in-the-Middle attacks. Either specify a custom CA certificate with sslrootcert parameter or use sslmode=verify-full for proper security.");t.ssl.checkServerIdentity=function(){};break}case"verify-full":break}else switch(t.sslmode){case"disable":{t.ssl=!1;break}case"prefer":case"require":case"verify-ca":case"verify-full":{t.sslmode!=="verify-full"&&pt(t.sslmode);break}case"no-verify":{t.ssl.rejectUnauthorized=!1;break}}return t}function Ji(r){return Object.entries(r).reduce((t,[n,s])=>(s!=null&&(t[n]=s),t),{})}function hn(r){return Object.entries(r).reduce((t,[n,s])=>{if(n==="ssl"){let i=s;typeof i=="boolean"&&(t[n]=i),typeof i=="object"&&(t[n]=Ji(i))}else if(s!=null)if(n==="port"){if(s!==""){let i=parseInt(s,10);if(isNaN(i))throw new Error(`Invalid ${n}: ${s}`);t[n]=i}}else t[n]=s;return t},{})}function Xi(r){return hn(te(r))}function pt(r){!pt.warned&&typeof process<"u"&&process.emitWarning&&(pt.warned=!0,process.emitWarning(`SECURITY WARNING: The SSL modes 'prefer', 'require', and 'verify-ca' are treated as aliases for 'verify-full'.
2
+ In the next major version (pg-connection-string v3.0.0 and pg v9.0.0), these modes will adopt standard libpq semantics, which have weaker security guarantees.
3
+
4
+ To prepare for this change:
5
+ - If you want the current behavior, explicitly use 'sslmode=verify-full'
6
+ - If you want libpq compatibility now, use 'uselibpqcompat=true&sslmode=${r}'
7
+
8
+ See https://www.postgresql.org/docs/current/libpq-ssl.html for libpq SSL mode definitions.`))}dn.exports=te;te.parse=te;te.toClientConfig=hn;te.parseIntoClientConfig=Xi});var yt=b((Xu,yn)=>{"use strict";u();var Zi=S("dns"),mn=me(),pn=fn().parse,x=function(r,e,t){return e[r]?e[r]:(t===void 0?t=process.env["PG"+r.toUpperCase()]:t===!1||(t=process.env[t]),t||mn[r])},eo=function(){switch(process.env.PGSSLMODE){case"disable":return!1;case"prefer":case"require":case"verify-ca":case"verify-full":return!0;case"no-verify":return{rejectUnauthorized:!1}}return mn.ssl},re=function(r){return"'"+(""+r).replace(/\\/g,"\\\\").replace(/'/g,"\\'")+"'"},L=function(r,e,t){let n=e[t];n!=null&&r.push(t+"="+re(n))},mt=class{constructor(e){e=typeof e=="string"?pn(e):e||{},e.connectionString&&(e=Object.assign({},e,pn(e.connectionString))),this.user=x("user",e),this.database=x("database",e),this.database===void 0&&(this.database=this.user),this.port=parseInt(x("port",e),10),this.host=x("host",e),Object.defineProperty(this,"password",{configurable:!0,enumerable:!1,writable:!0,value:x("password",e)}),this.binary=x("binary",e),this.options=x("options",e),this.ssl=typeof e.ssl>"u"?eo():e.ssl,typeof this.ssl=="string"&&this.ssl==="true"&&(this.ssl=!0),this.ssl==="no-verify"&&(this.ssl={rejectUnauthorized:!1}),this.ssl&&this.ssl.key&&Object.defineProperty(this.ssl,"key",{enumerable:!1}),this.client_encoding=x("client_encoding",e),this.replication=x("replication",e),this.isDomainSocket=!(this.host||"").indexOf("/"),this.application_name=x("application_name",e,"PGAPPNAME"),this.fallback_application_name=x("fallback_application_name",e,!1),this.statement_timeout=x("statement_timeout",e,!1),this.lock_timeout=x("lock_timeout",e,!1),this.idle_in_transaction_session_timeout=x("idle_in_transaction_session_timeout",e,!1),this.query_timeout=x("query_timeout",e,!1),e.connectionTimeoutMillis===void 0?this.connect_timeout=process.env.PGCONNECT_TIMEOUT||0:this.connect_timeout=Math.floor(e.connectionTimeoutMillis/1e3),e.keepAlive===!1?this.keepalives=0:e.keepAlive===!0&&(this.keepalives=1),typeof e.keepAliveInitialDelayMillis=="number"&&(this.keepalives_idle=Math.floor(e.keepAliveInitialDelayMillis/1e3))}getLibpqConnectionString(e){let t=[];L(t,this,"user"),L(t,this,"password"),L(t,this,"port"),L(t,this,"application_name"),L(t,this,"fallback_application_name"),L(t,this,"connect_timeout"),L(t,this,"options");let n=typeof this.ssl=="object"?this.ssl:this.ssl?{sslmode:this.ssl}:{};if(L(t,n,"sslmode"),L(t,n,"sslca"),L(t,n,"sslkey"),L(t,n,"sslcert"),L(t,n,"sslrootcert"),this.database&&t.push("dbname="+re(this.database)),this.replication&&t.push("replication="+re(this.replication)),this.host&&t.push("host="+re(this.host)),this.isDomainSocket)return e(null,t.join(" "));this.client_encoding&&t.push("client_encoding="+re(this.client_encoding)),Zi.lookup(this.host,function(s,i){return s?e(s,null):(t.push("hostaddr="+re(i)),e(null,t.join(" ")))})}};yn.exports=mt});var bt=b((el,bn)=>{"use strict";u();var to=pe(),gn=/^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/,gt=class{constructor(e,t){this.command=null,this.rowCount=null,this.oid=null,this.rows=[],this.fields=[],this._parsers=void 0,this._types=t,this.RowCtor=null,this.rowAsArray=e==="array",this.rowAsArray&&(this.parseRow=this._parseRowAsArray),this._prebuiltEmptyResultObject=null}addCommandComplete(e){let t;e.text?t=gn.exec(e.text):t=gn.exec(e.command),t&&(this.command=t[1],t[3]?(this.oid=parseInt(t[2],10),this.rowCount=parseInt(t[3],10)):t[2]&&(this.rowCount=parseInt(t[2],10)))}_parseRowAsArray(e){let t=new Array(e.length);for(let n=0,s=e.length;n<s;n++){let i=e[n];i!==null?t[n]=this._parsers[n](i):t[n]=null}return t}parseRow(e){let t={...this._prebuiltEmptyResultObject};for(let n=0,s=e.length;n<s;n++){let i=e[n],o=this.fields[n].name;if(i!==null){let a=this.fields[n].format==="binary"?Buffer.from(i):i;t[o]=this._parsers[n](a)}else t[o]=null}return t}addRow(e){this.rows.push(e)}addFields(e){this.fields=e,this.fields.length&&(this._parsers=new Array(e.length));let t={};for(let n=0;n<e.length;n++){let s=e[n];t[s.name]=null,this._types?this._parsers[n]=this._types.getTypeParser(s.dataTypeID,s.format||"text"):this._parsers[n]=to.getTypeParser(s.dataTypeID,s.format||"text")}this._prebuiltEmptyResultObject={...t}}};bn.exports=gt});var En=b((rl,vn)=>{"use strict";u();var{EventEmitter:ro}=S("events"),wn=bt(),Sn=Z(),wt=class extends ro{constructor(e,t,n){super(),e=Sn.normalizeQueryConfig(e,t,n),this.text=e.text,this.values=e.values,this.rows=e.rows,this.types=e.types,this.name=e.name,this.queryMode=e.queryMode,this.binary=e.binary,this.portal=e.portal||"",this.callback=e.callback,this._rowMode=e.rowMode,process.domain&&e.callback&&(this.callback=process.domain.bind(e.callback)),this._result=new wn(this._rowMode,this.types),this._results=this._result,this._canceledDueToError=!1}requiresPreparation(){return this.queryMode==="extended"||this.name||this.rows?!0:!this.text||!this.values?!1:this.values.length>0}_checkForMultirow(){this._result.command&&(Array.isArray(this._results)||(this._results=[this._result]),this._result=new wn(this._rowMode,this._result._types),this._results.push(this._result))}handleRowDescription(e){this._checkForMultirow(),this._result.addFields(e.fields),this._accumulateRows=this.callback||!this.listeners("row").length}handleDataRow(e){let t;if(!this._canceledDueToError){try{t=this._result.parseRow(e.fields)}catch(n){this._canceledDueToError=n;return}this.emit("row",t,this._result),this._accumulateRows&&this._result.addRow(t)}}handleCommandComplete(e,t){this._checkForMultirow(),this._result.addCommandComplete(e),this.rows&&t.sync()}handleEmptyQuery(e){this.rows&&e.sync()}handleError(e,t){if(this._canceledDueToError&&(e=this._canceledDueToError,this._canceledDueToError=!1),this.callback)return this.callback(e);this.emit("error",e)}handleReadyForQuery(e){if(this._canceledDueToError)return this.handleError(this._canceledDueToError,e);if(this.callback)try{this.callback(null,this._results)}catch(t){process.nextTick(()=>{throw t})}this.emit("end",this._results)}submit(e){if(typeof this.text!="string"&&typeof this.name!="string")return new Error("A query must have either text or a name. Supplying neither is unsupported.");let t=e.parsedStatements[this.name];if(this.text&&t&&this.text!==t)return new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`);if(this.values&&!Array.isArray(this.values))return new Error("Query values must be an array");if(this.requiresPreparation()){e.stream.cork&&e.stream.cork();try{this.prepare(e)}finally{e.stream.uncork&&e.stream.uncork()}}else e.query(this.text);return null}hasBeenParsed(e){return this.name&&e.parsedStatements[this.name]}handlePortalSuspended(e){this._getRows(e,this.rows)}_getRows(e,t){e.execute({portal:this.portal,rows:t}),t?e.flush():e.sync()}prepare(e){this.hasBeenParsed(e)||e.parse({text:this.text,name:this.name,types:this.types});try{e.bind({portal:this.portal,statement:this.name,values:this.values,binary:this.binary,valueMapper:Sn.prepareValue})}catch(t){this.handleError(t,e);return}e.describe({type:"P",name:this.portal||""}),this._getRows(e,this.rows)}handleCopyInResponse(e){e.sendCopyFail("No source stream defined")}handleCopyData(e,t){}};vn.exports=wt});var qt=b(g=>{"use strict";u();Object.defineProperty(g,"__esModule",{value:!0});g.NoticeMessage=g.DataRowMessage=g.CommandCompleteMessage=g.ReadyForQueryMessage=g.NotificationResponseMessage=g.BackendKeyDataMessage=g.AuthenticationMD5Password=g.ParameterStatusMessage=g.ParameterDescriptionMessage=g.RowDescriptionMessage=g.Field=g.CopyResponse=g.CopyDataMessage=g.DatabaseError=g.copyDone=g.emptyQuery=g.replicationStart=g.portalSuspended=g.noData=g.closeComplete=g.bindComplete=g.parseComplete=void 0;g.parseComplete={name:"parseComplete",length:5};g.bindComplete={name:"bindComplete",length:5};g.closeComplete={name:"closeComplete",length:5};g.noData={name:"noData",length:5};g.portalSuspended={name:"portalSuspended",length:5};g.replicationStart={name:"replicationStart",length:4};g.emptyQuery={name:"emptyQuery",length:4};g.copyDone={name:"copyDone",length:4};var St=class extends Error{constructor(e,t,n){super(e),this.length=t,this.name=n}};g.DatabaseError=St;var vt=class{constructor(e,t){this.length=e,this.chunk=t,this.name="copyData"}};g.CopyDataMessage=vt;var Et=class{constructor(e,t,n,s){this.length=e,this.name=t,this.binary=n,this.columnTypes=new Array(s)}};g.CopyResponse=Et;var _t=class{constructor(e,t,n,s,i,o,a){this.name=e,this.tableID=t,this.columnID=n,this.dataTypeID=s,this.dataTypeSize=i,this.dataTypeModifier=o,this.format=a}};g.Field=_t;var kt=class{constructor(e,t){this.length=e,this.fieldCount=t,this.name="rowDescription",this.fields=new Array(this.fieldCount)}};g.RowDescriptionMessage=kt;var Ct=class{constructor(e,t){this.length=e,this.parameterCount=t,this.name="parameterDescription",this.dataTypeIDs=new Array(this.parameterCount)}};g.ParameterDescriptionMessage=Ct;var xt=class{constructor(e,t,n){this.length=e,this.parameterName=t,this.parameterValue=n,this.name="parameterStatus"}};g.ParameterStatusMessage=xt;var Pt=class{constructor(e,t){this.length=e,this.salt=t,this.name="authenticationMD5Password"}};g.AuthenticationMD5Password=Pt;var At=class{constructor(e,t,n){this.length=e,this.processID=t,this.secretKey=n,this.name="backendKeyData"}};g.BackendKeyDataMessage=At;var Rt=class{constructor(e,t,n,s){this.length=e,this.processId=t,this.channel=n,this.payload=s,this.name="notification"}};g.NotificationResponseMessage=Rt;var Tt=class{constructor(e,t){this.length=e,this.status=t,this.name="readyForQuery"}};g.ReadyForQueryMessage=Tt;var Ot=class{constructor(e,t){this.length=e,this.text=t,this.name="commandComplete"}};g.CommandCompleteMessage=Ot;var It=class{constructor(e,t){this.length=e,this.fields=t,this.name="dataRow",this.fieldCount=t.length}};g.DataRowMessage=It;var Mt=class{constructor(e,t){this.length=e,this.message=t,this.name="notice"}};g.NoticeMessage=Mt});var _n=b(xe=>{"use strict";u();Object.defineProperty(xe,"__esModule",{value:!0});xe.Writer=void 0;var Lt=class{constructor(e=256){this.size=e,this.offset=5,this.headerPosition=0,this.buffer=Buffer.allocUnsafe(e)}ensure(e){if(this.buffer.length-this.offset<e){let n=this.buffer,s=n.length+(n.length>>1)+e;this.buffer=Buffer.allocUnsafe(s),n.copy(this.buffer)}}addInt32(e){return this.ensure(4),this.buffer[this.offset++]=e>>>24&255,this.buffer[this.offset++]=e>>>16&255,this.buffer[this.offset++]=e>>>8&255,this.buffer[this.offset++]=e>>>0&255,this}addInt16(e){return this.ensure(2),this.buffer[this.offset++]=e>>>8&255,this.buffer[this.offset++]=e>>>0&255,this}addCString(e){if(!e)this.ensure(1);else{let t=Buffer.byteLength(e);this.ensure(t+1),this.buffer.write(e,this.offset,"utf-8"),this.offset+=t}return this.buffer[this.offset++]=0,this}addString(e=""){let t=Buffer.byteLength(e);return this.ensure(t),this.buffer.write(e,this.offset),this.offset+=t,this}add(e){return this.ensure(e.length),e.copy(this.buffer,this.offset),this.offset+=e.length,this}join(e){if(e){this.buffer[this.headerPosition]=e;let t=this.offset-(this.headerPosition+1);this.buffer.writeInt32BE(t,this.headerPosition+1)}return this.buffer.slice(e?0:5,this.offset)}flush(e){let t=this.join(e);return this.offset=5,this.headerPosition=0,this.buffer=Buffer.allocUnsafe(this.size),t}};xe.Writer=Lt});var Cn=b(Ae=>{"use strict";u();Object.defineProperty(Ae,"__esModule",{value:!0});Ae.serialize=void 0;var Dt=_n(),v=new Dt.Writer,no=r=>{v.addInt16(3).addInt16(0);for(let n of Object.keys(r))v.addCString(n).addCString(r[n]);v.addCString("client_encoding").addCString("UTF8");let e=v.addCString("").flush(),t=e.length+4;return new Dt.Writer().addInt32(t).add(e).flush()},so=()=>{let r=Buffer.allocUnsafe(8);return r.writeInt32BE(8,0),r.writeInt32BE(80877103,4),r},io=r=>v.addCString(r).flush(112),oo=function(r,e){return v.addCString(r).addInt32(Buffer.byteLength(e)).addString(e),v.flush(112)},ao=function(r){return v.addString(r).flush(112)},co=r=>v.addCString(r).flush(81),kn=[],uo=r=>{let e=r.name||"";e.length>63&&(console.error("Warning! Postgres only supports 63 characters for query names."),console.error("You supplied %s (%s)",e,e.length),console.error("This can cause conflicts and silent errors executing queries"));let t=r.types||kn,n=t.length,s=v.addCString(e).addCString(r.text).addInt16(n);for(let i=0;i<n;i++)s.addInt32(t[i]);return v.flush(80)},ne=new Dt.Writer,lo=function(r,e){for(let t=0;t<r.length;t++){let n=e?e(r[t],t):r[t];n==null?(v.addInt16(0),ne.addInt32(-1)):n instanceof Buffer?(v.addInt16(1),ne.addInt32(n.length),ne.add(n)):(v.addInt16(0),ne.addInt32(Buffer.byteLength(n)),ne.addString(n))}},ho=(r={})=>{let e=r.portal||"",t=r.statement||"",n=r.binary||!1,s=r.values||kn,i=s.length;return v.addCString(e).addCString(t),v.addInt16(i),lo(s,r.valueMapper),v.addInt16(i),v.add(ne.flush()),v.addInt16(1),v.addInt16(n?1:0),v.flush(66)},fo=Buffer.from([69,0,0,0,9,0,0,0,0,0]),po=r=>{if(!r||!r.portal&&!r.rows)return fo;let e=r.portal||"",t=r.rows||0,n=Buffer.byteLength(e),s=4+n+1+4,i=Buffer.allocUnsafe(1+s);return i[0]=69,i.writeInt32BE(s,1),i.write(e,5,"utf-8"),i[n+5]=0,i.writeUInt32BE(t,i.length-4),i},mo=(r,e)=>{let t=Buffer.allocUnsafe(16);return t.writeInt32BE(16,0),t.writeInt16BE(1234,4),t.writeInt16BE(5678,6),t.writeInt32BE(r,8),t.writeInt32BE(e,12),t},Qt=(r,e)=>{let n=4+Buffer.byteLength(e)+1,s=Buffer.allocUnsafe(1+n);return s[0]=r,s.writeInt32BE(n,1),s.write(e,5,"utf-8"),s[n]=0,s},yo=v.addCString("P").flush(68),go=v.addCString("S").flush(68),bo=r=>r.name?Qt(68,`${r.type}${r.name||""}`):r.type==="P"?yo:go,wo=r=>{let e=`${r.type}${r.name||""}`;return Qt(67,e)},So=r=>v.add(r).flush(100),vo=r=>Qt(102,r),Pe=r=>Buffer.from([r,0,0,0,4]),Eo=Pe(72),_o=Pe(83),ko=Pe(88),Co=Pe(99),xo={startup:no,password:io,requestSsl:so,sendSASLInitialResponseMessage:oo,sendSCRAMClientFinalMessage:ao,query:co,parse:uo,bind:ho,execute:po,describe:bo,close:wo,flush:()=>Eo,sync:()=>_o,end:()=>ko,copyData:So,copyDone:()=>Co,copyFail:vo,cancel:mo};Ae.serialize=xo});var xn=b(Re=>{"use strict";u();Object.defineProperty(Re,"__esModule",{value:!0});Re.BufferReader=void 0;var Nt=class{constructor(e=0){this.offset=e,this.buffer=Buffer.allocUnsafe(0),this.encoding="utf-8"}setBuffer(e,t){this.offset=e,this.buffer=t}int16(){let e=this.buffer.readInt16BE(this.offset);return this.offset+=2,e}byte(){let e=this.buffer[this.offset];return this.offset++,e}int32(){let e=this.buffer.readInt32BE(this.offset);return this.offset+=4,e}uint32(){let e=this.buffer.readUInt32BE(this.offset);return this.offset+=4,e}string(e){let t=this.buffer.toString(this.encoding,this.offset,this.offset+e);return this.offset+=e,t}cstring(){let e=this.offset,t=e;for(;this.buffer[t++]!==0;);return this.offset=t,this.buffer.toString(this.encoding,e,t-1)}bytes(e){let t=this.buffer.slice(this.offset,this.offset+e);return this.offset+=e,t}};Re.BufferReader=Nt});var Tn=b(Te=>{"use strict";u();Object.defineProperty(Te,"__esModule",{value:!0});Te.Parser=void 0;var _=qt(),Po=xn(),Ft=1,Ao=4,Pn=Ft+Ao,O=-1,Bt=Buffer.allocUnsafe(0),Ut=class{constructor(e){if(this.buffer=Bt,this.bufferLength=0,this.bufferOffset=0,this.reader=new Po.BufferReader,e?.mode==="binary")throw new Error("Binary mode not supported yet");this.mode=e?.mode||"text"}parse(e,t){this.mergeBuffer(e);let n=this.bufferOffset+this.bufferLength,s=this.bufferOffset;for(;s+Pn<=n;){let i=this.buffer[s],o=this.buffer.readUInt32BE(s+Ft),a=Ft+o;if(a+s<=n){let l=this.handlePacket(s+Pn,i,o,this.buffer);t(l),s+=a}else break}s===n?(this.buffer=Bt,this.bufferLength=0,this.bufferOffset=0):(this.bufferLength=n-s,this.bufferOffset=s)}mergeBuffer(e){if(this.bufferLength>0){let t=this.bufferLength+e.byteLength;if(t+this.bufferOffset>this.buffer.byteLength){let s;if(t<=this.buffer.byteLength&&this.bufferOffset>=this.bufferLength)s=this.buffer;else{let i=this.buffer.byteLength*2;for(;t>=i;)i*=2;s=Buffer.allocUnsafe(i)}this.buffer.copy(s,0,this.bufferOffset,this.bufferOffset+this.bufferLength),this.buffer=s,this.bufferOffset=0}e.copy(this.buffer,this.bufferOffset+this.bufferLength),this.bufferLength=t}else this.buffer=e,this.bufferOffset=0,this.bufferLength=e.byteLength}handlePacket(e,t,n,s){let{reader:i}=this;i.setBuffer(e,s);let o;switch(t){case 50:o=_.bindComplete;break;case 49:o=_.parseComplete;break;case 51:o=_.closeComplete;break;case 110:o=_.noData;break;case 115:o=_.portalSuspended;break;case 99:o=_.copyDone;break;case 87:o=_.replicationStart;break;case 73:o=_.emptyQuery;break;case 68:o=No(i);break;case 67:o=To(i);break;case 90:o=Ro(i);break;case 65:o=qo(i);break;case 82:o=Uo(i,n);break;case 83:o=Bo(i);break;case 75:o=Fo(i);break;case 69:o=An(i,"error");break;case 78:o=An(i,"notice");break;case 84:o=Lo(i);break;case 116:o=Qo(i);break;case 71:o=Io(i);break;case 72:o=Mo(i);break;case 100:o=Oo(i,n);break;default:return new _.DatabaseError("received invalid response: "+t.toString(16),n,"error")}return i.setBuffer(0,Bt),o.length=n,o}};Te.Parser=Ut;var Ro=r=>{let e=r.string(1);return new _.ReadyForQueryMessage(O,e)},To=r=>{let e=r.cstring();return new _.CommandCompleteMessage(O,e)},Oo=(r,e)=>{let t=r.bytes(e-4);return new _.CopyDataMessage(O,t)},Io=r=>Rn(r,"copyInResponse"),Mo=r=>Rn(r,"copyOutResponse"),Rn=(r,e)=>{let t=r.byte()!==0,n=r.int16(),s=new _.CopyResponse(O,e,t,n);for(let i=0;i<n;i++)s.columnTypes[i]=r.int16();return s},qo=r=>{let e=r.int32(),t=r.cstring(),n=r.cstring();return new _.NotificationResponseMessage(O,e,t,n)},Lo=r=>{let e=r.int16(),t=new _.RowDescriptionMessage(O,e);for(let n=0;n<e;n++)t.fields[n]=Do(r);return t},Do=r=>{let e=r.cstring(),t=r.uint32(),n=r.int16(),s=r.uint32(),i=r.int16(),o=r.int32(),a=r.int16()===0?"text":"binary";return new _.Field(e,t,n,s,i,o,a)},Qo=r=>{let e=r.int16(),t=new _.ParameterDescriptionMessage(O,e);for(let n=0;n<e;n++)t.dataTypeIDs[n]=r.int32();return t},No=r=>{let e=r.int16(),t=new Array(e);for(let n=0;n<e;n++){let s=r.int32();t[n]=s===-1?null:r.string(s)}return new _.DataRowMessage(O,t)},Bo=r=>{let e=r.cstring(),t=r.cstring();return new _.ParameterStatusMessage(O,e,t)},Fo=r=>{let e=r.int32(),t=r.int32();return new _.BackendKeyDataMessage(O,e,t)},Uo=(r,e)=>{let t=r.int32(),n={name:"authenticationOk",length:e};switch(t){case 0:break;case 3:n.length===8&&(n.name="authenticationCleartextPassword");break;case 5:if(n.length===12){n.name="authenticationMD5Password";let s=r.bytes(4);return new _.AuthenticationMD5Password(O,s)}break;case 10:{n.name="authenticationSASL",n.mechanisms=[];let s;do s=r.cstring(),s&&n.mechanisms.push(s);while(s)}break;case 11:n.name="authenticationSASLContinue",n.data=r.string(e-8);break;case 12:n.name="authenticationSASLFinal",n.data=r.string(e-8);break;default:throw new Error("Unknown authenticationOk message type "+t)}return n},An=(r,e)=>{let t={},n=r.string(1);for(;n!=="\0";)t[n]=r.cstring(),n=r.string(1);let s=t.M,i=e==="notice"?new _.NoticeMessage(O,s):new _.DatabaseError(s,O,e);return i.severity=t.S,i.code=t.C,i.detail=t.D,i.hint=t.H,i.position=t.P,i.internalPosition=t.p,i.internalQuery=t.q,i.where=t.W,i.schema=t.s,i.table=t.t,i.column=t.c,i.dataType=t.d,i.constraint=t.n,i.file=t.F,i.line=t.L,i.routine=t.R,i}});var jt=b(B=>{"use strict";u();Object.defineProperty(B,"__esModule",{value:!0});B.DatabaseError=B.serialize=B.parse=void 0;var jo=qt();Object.defineProperty(B,"DatabaseError",{enumerable:!0,get:function(){return jo.DatabaseError}});var $o=Cn();Object.defineProperty(B,"serialize",{enumerable:!0,get:function(){return $o.serialize}});var Ho=Tn();function zo(r,e){let t=new Ho.Parser;return r.on("data",n=>t.parse(n,e)),new Promise(n=>r.on("end",()=>n()))}B.parse=zo});var On=b($t=>{"use strict";u();Object.defineProperty($t,"__esModule",{value:!0});$t.default={}});var Mn=b((bl,In)=>{"use strict";u();var{getStream:Ko,getSecureStream:Go}=Jo();In.exports={getStream:Ko,getSecureStream:Go};function Vo(){function r(t){let n=S("net");return new n.Socket}function e(t){return S("tls").connect(t)}return{getStream:r,getSecureStream:e}}function Wo(){function r(t){let{CloudflareSocket:n}=On();return new n(t)}function e(t){return t.socket.startTls(t),t.socket}return{getStream:r,getSecureStream:e}}function Yo(){if(typeof navigator=="object"&&navigator!==null&&typeof navigator.userAgent=="string")return navigator.userAgent==="Cloudflare-Workers";if(typeof Response=="function"){let r=new Response(null,{cf:{thing:!0}});if(typeof r.cf=="object"&&r.cf!==null&&r.cf.thing)return!0}return!1}function Jo(){return Yo()?Wo():Vo()}});var zt=b((Sl,qn)=>{"use strict";u();var Xo=S("events").EventEmitter,{parse:Zo,serialize:C}=jt(),{getStream:ea,getSecureStream:ta}=Mn(),ra=C.flush(),na=C.sync(),sa=C.end(),Ht=class extends Xo{constructor(e){super(),e=e||{},this.stream=e.stream||ea(e.ssl),typeof this.stream=="function"&&(this.stream=this.stream(e)),this._keepAlive=e.keepAlive,this._keepAliveInitialDelayMillis=e.keepAliveInitialDelayMillis,this.parsedStatements={},this.ssl=e.ssl||!1,this._ending=!1,this._emitMessage=!1;let t=this;this.on("newListener",function(n){n==="message"&&(t._emitMessage=!0)})}connect(e,t){let n=this;this._connecting=!0,this.stream.setNoDelay(!0),this.stream.connect(e,t),this.stream.once("connect",function(){n._keepAlive&&n.stream.setKeepAlive(!0,n._keepAliveInitialDelayMillis),n.emit("connect")});let s=function(i){n._ending&&(i.code==="ECONNRESET"||i.code==="EPIPE")||n.emit("error",i)};if(this.stream.on("error",s),this.stream.on("close",function(){n.emit("end")}),!this.ssl)return this.attachListeners(this.stream);this.stream.once("data",function(i){switch(i.toString("utf8")){case"S":break;case"N":return n.stream.end(),n.emit("error",new Error("The server does not support SSL connections"));default:return n.stream.end(),n.emit("error",new Error("There was an error establishing an SSL connection"))}let a={socket:n.stream};n.ssl!==!0&&(Object.assign(a,n.ssl),"key"in n.ssl&&(a.key=n.ssl.key));let l=S("net");l.isIP&&l.isIP(t)===0&&(a.servername=t);try{n.stream=ta(a)}catch(c){return n.emit("error",c)}n.attachListeners(n.stream),n.stream.on("error",s),n.emit("sslconnect")})}attachListeners(e){Zo(e,t=>{let n=t.name==="error"?"errorMessage":t.name;this._emitMessage&&this.emit("message",t),this.emit(n,t)})}requestSsl(){this.stream.write(C.requestSsl())}startup(e){this.stream.write(C.startup(e))}cancel(e,t){this._send(C.cancel(e,t))}password(e){this._send(C.password(e))}sendSASLInitialResponseMessage(e,t){this._send(C.sendSASLInitialResponseMessage(e,t))}sendSCRAMClientFinalMessage(e){this._send(C.sendSCRAMClientFinalMessage(e))}_send(e){return this.stream.writable?this.stream.write(e):!1}query(e){this._send(C.query(e))}parse(e){this._send(C.parse(e))}bind(e){this._send(C.bind(e))}execute(e){this._send(C.execute(e))}flush(){this.stream.writable&&this.stream.write(ra)}sync(){this._ending=!0,this._send(na)}ref(){this.stream.ref()}unref(){this.stream.unref()}end(){if(this._ending=!0,!this._connecting||!this.stream.writable){this.stream.end();return}return this.stream.write(sa,()=>{this.stream.end()})}close(e){this._send(C.close(e))}describe(e){this._send(C.describe(e))}sendCopyFromChunk(e){this._send(C.copyData(e))}endCopyFrom(){this._send(C.copyDone())}sendCopyFail(e){this._send(C.copyFail(e))}};qn.exports=Ht});var Nn=b((El,Qn)=>{"use strict";u();var{Transform:ia}=S("stream"),{StringDecoder:oa}=S("string_decoder"),F=Symbol("last"),Oe=Symbol("decoder");function aa(r,e,t){let n;if(this.overflow){if(n=this[Oe].write(r).split(this.matcher),n.length===1)return t();n.shift(),this.overflow=!1}else this[F]+=this[Oe].write(r),n=this[F].split(this.matcher);this[F]=n.pop();for(let s=0;s<n.length;s++)try{Dn(this,this.mapper(n[s]))}catch(i){return t(i)}if(this.overflow=this[F].length>this.maxLength,this.overflow&&!this.skipOverflow){t(new Error("maximum buffer reached"));return}t()}function ca(r){if(this[F]+=this[Oe].end(),this[F])try{Dn(this,this.mapper(this[F]))}catch(e){return r(e)}r()}function Dn(r,e){e!==void 0&&r.push(e)}function Ln(r){return r}function ua(r,e,t){switch(r=r||/\r?\n/,e=e||Ln,t=t||{},arguments.length){case 1:typeof r=="function"?(e=r,r=/\r?\n/):typeof r=="object"&&!(r instanceof RegExp)&&!r[Symbol.split]&&(t=r,r=/\r?\n/);break;case 2:typeof r=="function"?(t=e,e=r,r=/\r?\n/):typeof e=="object"&&(t=e,e=Ln)}t=Object.assign({},t),t.autoDestroy=!0,t.transform=aa,t.flush=ca,t.readableObjectMode=!0;let n=new ia(t);return n[F]="",n[Oe]=new oa("utf8"),n.matcher=r,n.mapper=e,n.maxLength=t.maxLength,n.skipOverflow=t.skipOverflow||!1,n.overflow=!1,n._destroy=function(s,i){this._writableState.errorEmitted=!1,i(s)},n}Qn.exports=ua});var Un=b((kl,D)=>{"use strict";u();var Bn=S("path"),la=S("stream").Stream,ha=Nn(),Fn=S("util"),da=5432,Ie=process.platform==="win32",ge=process.stderr,fa=56,pa=7,ma=61440,ya=32768;function ga(r){return(r&ma)==ya}var se=["host","port","database","user","password"],Kt=se.length,ba=se[Kt-1];function Gt(){var r=ge instanceof la&&ge.writable===!0;if(r){var e=Array.prototype.slice.call(arguments).concat(`
9
+ `);ge.write(Fn.format.apply(Fn,e))}}Object.defineProperty(D.exports,"isWin",{get:function(){return Ie},set:function(r){Ie=r}});D.exports.warnTo=function(r){var e=ge;return ge=r,e};D.exports.getFileName=function(r){var e=r||process.env,t=e.PGPASSFILE||(Ie?Bn.join(e.APPDATA||"./","postgresql","pgpass.conf"):Bn.join(e.HOME||"./",".pgpass"));return t};D.exports.usePgPass=function(r,e){return Object.prototype.hasOwnProperty.call(process.env,"PGPASSWORD")?!1:Ie?!0:(e=e||"<unkn>",ga(r.mode)?r.mode&(fa|pa)?(Gt('WARNING: password file "%s" has group or world access; permissions should be u=rw (0600) or less',e),!1):!0:(Gt('WARNING: password file "%s" is not a plain file',e),!1))};var wa=D.exports.match=function(r,e){return se.slice(0,-1).reduce(function(t,n,s){return s==1&&Number(r[n]||da)===Number(e[n])?t&&!0:t&&(e[n]==="*"||e[n]===r[n])},!0)};D.exports.getPassword=function(r,e,t){var n,s=e.pipe(ha());function i(l){var c=Sa(l);c&&va(c)&&wa(r,c)&&(n=c[ba],s.end())}var o=function(){e.destroy(),t(n)},a=function(l){e.destroy(),Gt("WARNING: error on reading file: %s",l),t(void 0)};e.on("error",a),s.on("data",i).on("end",o).on("error",a)};var Sa=D.exports.parseLine=function(r){if(r.length<11||r.match(/^\s+#/))return null;for(var e="",t="",n=0,s=0,i=0,o={},a=!1,l=function(h,d,m){var y=r.substring(d,m);Object.hasOwnProperty.call(process.env,"PGPASS_NO_DEESCAPE")||(y=y.replace(/\\([:\\])/g,"$1")),o[se[h]]=y},c=0;c<r.length-1;c+=1){if(e=r.charAt(c+1),t=r.charAt(c),a=n==Kt-1,a){l(n,s);break}c>=0&&e==":"&&t!=="\\"&&(l(n,s,c+1),s=c+2,n+=1)}return o=Object.keys(o).length===Kt?o:null,o},va=D.exports.isValidEntry=function(r){for(var e={0:function(o){return o.length>0},1:function(o){return o==="*"?!0:(o=Number(o),isFinite(o)&&o>0&&o<9007199254740992&&Math.floor(o)===o)},2:function(o){return o.length>0},3:function(o){return o.length>0},4:function(o){return o.length>0}},t=0;t<se.length;t+=1){var n=e[t],s=r[se[t]]||"",i=n(s);if(!i)return!1}return!0}});var $n=b((Pl,Vt)=>{"use strict";u();var xl=S("path"),jn=S("fs"),Me=Un();Vt.exports=function(r,e){var t=Me.getFileName();jn.stat(t,function(n,s){if(n||!Me.usePgPass(s,t))return e(void 0);var i=jn.createReadStream(t);Me.getPassword(r,i,e)})};Vt.exports.warnTo=Me.warnTo});var Vn=b((Rl,Gn)=>{"use strict";u();var Ea=S("events").EventEmitter,Hn=Z(),be=S("util"),Wt=un(),_a=Ce(),ka=yt(),Kn=En(),Ca=me(),xa=zt(),Pa=dt(),zn=be.deprecate(()=>{},"Client.activeQuery is deprecated and will be removed in pg@9.0"),Aa=be.deprecate(()=>{},"Client.queryQueue is deprecated and will be removed in pg@9.0."),Ra=be.deprecate(()=>{},"pgpass support is deprecated and will be removed in pg@9.0. You can provide an async function as the password property to the Client/Pool constructor that returns a password instead. Within this function you can call the pgpass module in your own code."),Ta=be.deprecate(()=>{},"Passing a custom Promise implementation to the Client/Pool constructor is deprecated and will be removed in pg@9.0."),Oa=be.deprecate(()=>{},"Calling client.query() when the client is already executing a query is deprecated and will be removed in pg@9.0. Use async/await or an external async flow control mechanism instead."),qe=class extends Ea{constructor(e){super(),this.connectionParameters=new ka(e),this.user=this.connectionParameters.user,this.database=this.connectionParameters.database,this.port=this.connectionParameters.port,this.host=this.connectionParameters.host,Object.defineProperty(this,"password",{configurable:!0,enumerable:!1,writable:!0,value:this.connectionParameters.password}),this.replication=this.connectionParameters.replication;let t=e||{};t.Promise&&Ta(),this._Promise=t.Promise||global.Promise,this._types=new _a(t.types),this._ending=!1,this._ended=!1,this._connecting=!1,this._connected=!1,this._connectionError=!1,this._queryable=!0,this._activeQuery=null,this.enableChannelBinding=!!t.enableChannelBinding,this.connection=t.connection||new xa({stream:t.stream,ssl:this.connectionParameters.ssl,keepAlive:t.keepAlive||!1,keepAliveInitialDelayMillis:t.keepAliveInitialDelayMillis||0,encoding:this.connectionParameters.client_encoding||"utf8"}),this._queryQueue=[],this.binary=t.binary||Ca.binary,this.processID=null,this.secretKey=null,this.ssl=this.connectionParameters.ssl||!1,this.ssl&&this.ssl.key&&Object.defineProperty(this.ssl,"key",{enumerable:!1}),this._connectionTimeoutMillis=t.connectionTimeoutMillis||0}get activeQuery(){return zn(),this._activeQuery}set activeQuery(e){zn(),this._activeQuery=e}_getActiveQuery(){return this._activeQuery}_errorAllQueries(e){let t=s=>{process.nextTick(()=>{s.handleError(e,this.connection)})},n=this._getActiveQuery();n&&(t(n),this._activeQuery=null),this._queryQueue.forEach(t),this._queryQueue.length=0}_connect(e){let t=this,n=this.connection;if(this._connectionCallback=e,this._connecting||this._connected){let s=new Error("Client has already been connected. You cannot reuse a client.");process.nextTick(()=>{e(s)});return}this._connecting=!0,this._connectionTimeoutMillis>0&&(this.connectionTimeoutHandle=setTimeout(()=>{n._ending=!0,n.stream.destroy(new Error("timeout expired"))},this._connectionTimeoutMillis),this.connectionTimeoutHandle.unref&&this.connectionTimeoutHandle.unref()),this.host&&this.host.indexOf("/")===0?n.connect(this.host+"/.s.PGSQL."+this.port):n.connect(this.port,this.host),n.on("connect",function(){t.ssl?n.requestSsl():n.startup(t.getStartupConf())}),n.on("sslconnect",function(){n.startup(t.getStartupConf())}),this._attachListeners(n),n.once("end",()=>{let s=this._ending?new Error("Connection terminated"):new Error("Connection terminated unexpectedly");clearTimeout(this.connectionTimeoutHandle),this._errorAllQueries(s),this._ended=!0,this._ending||(this._connecting&&!this._connectionError?this._connectionCallback?this._connectionCallback(s):this._handleErrorEvent(s):this._connectionError||this._handleErrorEvent(s)),process.nextTick(()=>{this.emit("end")})})}connect(e){if(e){this._connect(e);return}return new this._Promise((t,n)=>{this._connect(s=>{s?n(s):t(this)})})}_attachListeners(e){e.on("authenticationCleartextPassword",this._handleAuthCleartextPassword.bind(this)),e.on("authenticationMD5Password",this._handleAuthMD5Password.bind(this)),e.on("authenticationSASL",this._handleAuthSASL.bind(this)),e.on("authenticationSASLContinue",this._handleAuthSASLContinue.bind(this)),e.on("authenticationSASLFinal",this._handleAuthSASLFinal.bind(this)),e.on("backendKeyData",this._handleBackendKeyData.bind(this)),e.on("error",this._handleErrorEvent.bind(this)),e.on("errorMessage",this._handleErrorMessage.bind(this)),e.on("readyForQuery",this._handleReadyForQuery.bind(this)),e.on("notice",this._handleNotice.bind(this)),e.on("rowDescription",this._handleRowDescription.bind(this)),e.on("dataRow",this._handleDataRow.bind(this)),e.on("portalSuspended",this._handlePortalSuspended.bind(this)),e.on("emptyQuery",this._handleEmptyQuery.bind(this)),e.on("commandComplete",this._handleCommandComplete.bind(this)),e.on("parseComplete",this._handleParseComplete.bind(this)),e.on("copyInResponse",this._handleCopyInResponse.bind(this)),e.on("copyData",this._handleCopyData.bind(this)),e.on("notification",this._handleNotification.bind(this))}_getPassword(e){let t=this.connection;if(typeof this.password=="function")this._Promise.resolve().then(()=>this.password(this.connectionParameters)).then(n=>{if(n!==void 0){if(typeof n!="string"){t.emit("error",new TypeError("Password must be a string"));return}this.connectionParameters.password=this.password=n}else this.connectionParameters.password=this.password=null;e()}).catch(n=>{t.emit("error",n)});else if(this.password!==null)e();else try{$n()(this.connectionParameters,s=>{s!==void 0&&(Ra(),this.connectionParameters.password=this.password=s),e()})}catch(n){this.emit("error",n)}}_handleAuthCleartextPassword(e){this._getPassword(()=>{this.connection.password(this.password)})}_handleAuthMD5Password(e){this._getPassword(async()=>{try{let t=await Pa.postgresMd5PasswordHash(this.user,this.password,e.salt);this.connection.password(t)}catch(t){this.emit("error",t)}})}_handleAuthSASL(e){this._getPassword(()=>{try{this.saslSession=Wt.startSession(e.mechanisms,this.enableChannelBinding&&this.connection.stream),this.connection.sendSASLInitialResponseMessage(this.saslSession.mechanism,this.saslSession.response)}catch(t){this.connection.emit("error",t)}})}async _handleAuthSASLContinue(e){try{await Wt.continueSession(this.saslSession,this.password,e.data,this.enableChannelBinding&&this.connection.stream),this.connection.sendSCRAMClientFinalMessage(this.saslSession.response)}catch(t){this.connection.emit("error",t)}}_handleAuthSASLFinal(e){try{Wt.finalizeSession(this.saslSession,e.data),this.saslSession=null}catch(t){this.connection.emit("error",t)}}_handleBackendKeyData(e){this.processID=e.processID,this.secretKey=e.secretKey}_handleReadyForQuery(e){this._connecting&&(this._connecting=!1,this._connected=!0,clearTimeout(this.connectionTimeoutHandle),this._connectionCallback&&(this._connectionCallback(null,this),this._connectionCallback=null),this.emit("connect"));let t=this._getActiveQuery();this._activeQuery=null,this.readyForQuery=!0,t&&t.handleReadyForQuery(this.connection),this._pulseQueryQueue()}_handleErrorWhileConnecting(e){if(!this._connectionError){if(this._connectionError=!0,clearTimeout(this.connectionTimeoutHandle),this._connectionCallback)return this._connectionCallback(e);this.emit("error",e)}}_handleErrorEvent(e){if(this._connecting)return this._handleErrorWhileConnecting(e);this._queryable=!1,this._errorAllQueries(e),this.emit("error",e)}_handleErrorMessage(e){if(this._connecting)return this._handleErrorWhileConnecting(e);let t=this._getActiveQuery();if(!t){this._handleErrorEvent(e);return}this._activeQuery=null,t.handleError(e,this.connection)}_handleRowDescription(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected rowDescription message from backend.");this._handleErrorEvent(n);return}t.handleRowDescription(e)}_handleDataRow(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected dataRow message from backend.");this._handleErrorEvent(n);return}t.handleDataRow(e)}_handlePortalSuspended(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected portalSuspended message from backend.");this._handleErrorEvent(n);return}t.handlePortalSuspended(this.connection)}_handleEmptyQuery(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected emptyQuery message from backend.");this._handleErrorEvent(n);return}t.handleEmptyQuery(this.connection)}_handleCommandComplete(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected commandComplete message from backend.");this._handleErrorEvent(n);return}t.handleCommandComplete(e,this.connection)}_handleParseComplete(){let e=this._getActiveQuery();if(e==null){let t=new Error("Received unexpected parseComplete message from backend.");this._handleErrorEvent(t);return}e.name&&(this.connection.parsedStatements[e.name]=e.text)}_handleCopyInResponse(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected copyInResponse message from backend.");this._handleErrorEvent(n);return}t.handleCopyInResponse(this.connection)}_handleCopyData(e){let t=this._getActiveQuery();if(t==null){let n=new Error("Received unexpected copyData message from backend.");this._handleErrorEvent(n);return}t.handleCopyData(e,this.connection)}_handleNotification(e){this.emit("notification",e)}_handleNotice(e){this.emit("notice",e)}getStartupConf(){let e=this.connectionParameters,t={user:e.user,database:e.database},n=e.application_name||e.fallback_application_name;return n&&(t.application_name=n),e.replication&&(t.replication=""+e.replication),e.statement_timeout&&(t.statement_timeout=String(parseInt(e.statement_timeout,10))),e.lock_timeout&&(t.lock_timeout=String(parseInt(e.lock_timeout,10))),e.idle_in_transaction_session_timeout&&(t.idle_in_transaction_session_timeout=String(parseInt(e.idle_in_transaction_session_timeout,10))),e.options&&(t.options=e.options),t}cancel(e,t){if(e.activeQuery===t){let n=this.connection;this.host&&this.host.indexOf("/")===0?n.connect(this.host+"/.s.PGSQL."+this.port):n.connect(this.port,this.host),n.on("connect",function(){n.cancel(e.processID,e.secretKey)})}else e._queryQueue.indexOf(t)!==-1&&e._queryQueue.splice(e._queryQueue.indexOf(t),1)}setTypeParser(e,t,n){return this._types.setTypeParser(e,t,n)}getTypeParser(e,t){return this._types.getTypeParser(e,t)}escapeIdentifier(e){return Hn.escapeIdentifier(e)}escapeLiteral(e){return Hn.escapeLiteral(e)}_pulseQueryQueue(){if(this.readyForQuery===!0){this._activeQuery=this._queryQueue.shift();let e=this._getActiveQuery();if(e){this.readyForQuery=!1,this.hasExecuted=!0;let t=e.submit(this.connection);t&&process.nextTick(()=>{e.handleError(t,this.connection),this.readyForQuery=!0,this._pulseQueryQueue()})}else this.hasExecuted&&(this._activeQuery=null,this.emit("drain"))}}query(e,t,n){let s,i,o,a,l;if(e==null)throw new TypeError("Client was passed a null or undefined query");return typeof e.submit=="function"?(o=e.query_timeout||this.connectionParameters.query_timeout,i=s=e,s.callback||(typeof t=="function"?s.callback=t:n&&(s.callback=n))):(o=e.query_timeout||this.connectionParameters.query_timeout,s=new Kn(e,t,n),s.callback||(i=new this._Promise((c,h)=>{s.callback=(d,m)=>d?h(d):c(m)}).catch(c=>{throw Error.captureStackTrace(c),c}))),o&&(l=s.callback||(()=>{}),a=setTimeout(()=>{let c=new Error("Query read timeout");process.nextTick(()=>{s.handleError(c,this.connection)}),l(c),s.callback=()=>{};let h=this._queryQueue.indexOf(s);h>-1&&this._queryQueue.splice(h,1),this._pulseQueryQueue()},o),s.callback=(c,h)=>{clearTimeout(a),l(c,h)}),this.binary&&!s.binary&&(s.binary=!0),s._result&&!s._result._types&&(s._result._types=this._types),this._queryable?this._ending?(process.nextTick(()=>{s.handleError(new Error("Client was closed and is not queryable"),this.connection)}),i):(this._queryQueue.length>0&&Oa(),this._queryQueue.push(s),this._pulseQueryQueue(),i):(process.nextTick(()=>{s.handleError(new Error("Client has encountered a connection error and is not queryable"),this.connection)}),i)}ref(){this.connection.ref()}unref(){this.connection.unref()}end(e){if(this._ending=!0,!this.connection._connecting||this._ended)if(e)e();else return this._Promise.resolve();if(this._getActiveQuery()||!this._queryable?this.connection.stream.destroy():this.connection.end(),e)this.connection.once("end",e);else return new this._Promise(t=>{this.connection.once("end",t)})}get queryQueue(){return Aa(),this._queryQueue}};qe.Query=Kn;Gn.exports=qe});var Jn=b((Ol,Yn)=>{"use strict";u();var Ia=S("events").EventEmitter,Yt=function(){},Wn=(r,e)=>{let t=r.findIndex(e);return t===-1?void 0:r.splice(t,1)[0]},Jt=class{constructor(e,t,n){this.client=e,this.idleListener=t,this.timeoutId=n}},ie=class{constructor(e){this.callback=e}};function Ma(){throw new Error("Release called on client which has already been released to the pool.")}function Le(r,e){if(e)return{callback:e,result:void 0};let t,n,s=function(o,a){o?t(o):n(a)},i=new r(function(o,a){n=o,t=a}).catch(o=>{throw Error.captureStackTrace(o),o});return{callback:s,result:i}}function qa(r,e){return function t(n){n.client=e,e.removeListener("error",t),e.on("error",()=>{r.log("additional client error after disconnection due to error",n)}),r._remove(e),r.emit("error",n,e)}}var Xt=class extends Ia{constructor(e,t){super(),this.options=Object.assign({},e),e!=null&&"password"in e&&Object.defineProperty(this.options,"password",{configurable:!0,enumerable:!1,writable:!0,value:e.password}),e!=null&&e.ssl&&e.ssl.key&&Object.defineProperty(this.options.ssl,"key",{enumerable:!1}),this.options.max=this.options.max||this.options.poolSize||10,this.options.min=this.options.min||0,this.options.maxUses=this.options.maxUses||1/0,this.options.allowExitOnIdle=this.options.allowExitOnIdle||!1,this.options.maxLifetimeSeconds=this.options.maxLifetimeSeconds||0,this.log=this.options.log||function(){},this.Client=this.options.Client||t||Zt().Client,this.Promise=this.options.Promise||global.Promise,typeof this.options.idleTimeoutMillis>"u"&&(this.options.idleTimeoutMillis=1e4),this._clients=[],this._idle=[],this._expired=new WeakSet,this._pendingQueue=[],this._endCallback=void 0,this.ending=!1,this.ended=!1}_promiseTry(e){let t=this.Promise;return typeof t.try=="function"?t.try(e):new t(n=>n(e()))}_isFull(){return this._clients.length>=this.options.max}_isAboveMin(){return this._clients.length>this.options.min}_pulseQueue(){if(this.log("pulse queue"),this.ended){this.log("pulse queue ended");return}if(this.ending){this.log("pulse queue on ending"),this._idle.length&&this._idle.slice().map(t=>{this._remove(t.client)}),this._clients.length||(this.ended=!0,this._endCallback());return}if(!this._pendingQueue.length){this.log("no queued requests");return}if(!this._idle.length&&this._isFull())return;let e=this._pendingQueue.shift();if(this._idle.length){let t=this._idle.pop();clearTimeout(t.timeoutId);let n=t.client;n.ref&&n.ref();let s=t.idleListener;return this._acquireClient(n,e,s,!1)}if(!this._isFull())return this.newClient(e);throw new Error("unexpected condition")}_remove(e,t){let n=Wn(this._idle,i=>i.client===e);n!==void 0&&clearTimeout(n.timeoutId),this._clients=this._clients.filter(i=>i!==e);let s=this;e.end(()=>{s.emit("remove",e),typeof t=="function"&&t()})}connect(e){if(this.ending){let s=new Error("Cannot use a pool after calling end on the pool");return e?e(s):this.Promise.reject(s)}let t=Le(this.Promise,e),n=t.result;if(this._isFull()||this._idle.length){if(this._idle.length&&process.nextTick(()=>this._pulseQueue()),!this.options.connectionTimeoutMillis)return this._pendingQueue.push(new ie(t.callback)),n;let s=(a,l,c)=>{clearTimeout(o),t.callback(a,l,c)},i=new ie(s),o=setTimeout(()=>{Wn(this._pendingQueue,a=>a.callback===s),i.timedOut=!0,t.callback(new Error("timeout exceeded when trying to connect"))},this.options.connectionTimeoutMillis);return o.unref&&o.unref(),this._pendingQueue.push(i),n}return this.newClient(new ie(t.callback)),n}newClient(e){let t=new this.Client(this.options);this._clients.push(t);let n=qa(this,t);this.log("checking client timeout");let s,i=!1;this.options.connectionTimeoutMillis&&(s=setTimeout(()=>{t.connection?(this.log("ending client due to timeout"),i=!0,t.connection.stream.destroy()):t.isConnected()||(this.log("ending client due to timeout"),i=!0,t.end())},this.options.connectionTimeoutMillis)),this.log("connecting new client"),t.connect(o=>{if(s&&clearTimeout(s),t.on("error",n),o)this.log("client failed to connect",o),this._clients=this._clients.filter(a=>a!==t),i&&(o=new Error("Connection terminated due to connection timeout",{cause:o})),this._pulseQueue(),e.timedOut||e.callback(o,void 0,Yt);else{if(this.log("new client connected"),this.options.onConnect){this._promiseTry(()=>this.options.onConnect(t)).then(()=>{this._afterConnect(t,e,n)},a=>{this._clients=this._clients.filter(l=>l!==t),t.end(()=>{this._pulseQueue(),e.timedOut||e.callback(a,void 0,Yt)})});return}return this._afterConnect(t,e,n)}})}_afterConnect(e,t,n){if(this.options.maxLifetimeSeconds!==0){let s=setTimeout(()=>{this.log("ending client due to expired lifetime"),this._expired.add(e),this._idle.findIndex(o=>o.client===e)!==-1&&this._acquireClient(e,new ie((o,a,l)=>l()),n,!1)},this.options.maxLifetimeSeconds*1e3);s.unref(),e.once("end",()=>clearTimeout(s))}return this._acquireClient(e,t,n,!0)}_acquireClient(e,t,n,s){s&&this.emit("connect",e),this.emit("acquire",e),e.release=this._releaseOnce(e,n),e.removeListener("error",n),t.timedOut?s&&this.options.verify?this.options.verify(e,e.release):e.release():s&&this.options.verify?this.options.verify(e,i=>{if(i)return e.release(i),t.callback(i,void 0,Yt);t.callback(void 0,e,e.release)}):t.callback(void 0,e,e.release)}_releaseOnce(e,t){let n=!1;return s=>{n&&Ma(),n=!0,this._release(e,t,s)}}_release(e,t,n){if(e.on("error",t),e._poolUseCount=(e._poolUseCount||0)+1,this.emit("release",n,e),n||this.ending||!e._queryable||e._ending||e._poolUseCount>=this.options.maxUses)return e._poolUseCount>=this.options.maxUses&&this.log("remove expended client"),this._remove(e,this._pulseQueue.bind(this));if(this._expired.has(e))return this.log("remove expired client"),this._expired.delete(e),this._remove(e,this._pulseQueue.bind(this));let i;this.options.idleTimeoutMillis&&this._isAboveMin()&&(i=setTimeout(()=>{this._isAboveMin()&&(this.log("remove idle client"),this._remove(e,this._pulseQueue.bind(this)))},this.options.idleTimeoutMillis),this.options.allowExitOnIdle&&i.unref()),this.options.allowExitOnIdle&&e.unref(),this._idle.push(new Jt(e,t,i)),this._pulseQueue()}query(e,t,n){if(typeof e=="function"){let i=Le(this.Promise,e);return setImmediate(function(){return i.callback(new Error("Passing a function as the first parameter to pool.query is not supported"))}),i.result}typeof t=="function"&&(n=t,t=void 0);let s=Le(this.Promise,n);return n=s.callback,this.connect((i,o)=>{if(i)return n(i);let a=!1,l=c=>{a||(a=!0,o.release(c),n(c))};o.once("error",l),this.log("dispatching query");try{o.query(e,t,(c,h)=>{if(this.log("query dispatched"),o.removeListener("error",l),!a)return a=!0,o.release(c),c?n(c):n(void 0,h)})}catch(c){return o.release(c),n(c)}}),s.result}end(e){if(this.log("ending"),this.ending){let n=new Error("Called end on pool more than once");return e?e(n):this.Promise.reject(n)}this.ending=!0;let t=Le(this.Promise,e);return this._endCallback=t.callback,this._pulseQueue(),t.result}get waitingCount(){return this._pendingQueue.length}get idleCount(){return this._idle.length}get expiredCount(){return this._clients.reduce((e,t)=>e+(this._expired.has(t)?1:0),0)}get totalCount(){return this._clients.length}};Yn.exports=Xt});var es=b((Ml,Zn)=>{"use strict";u();var Xn=S("events").EventEmitter,La=S("util"),er=Z(),oe=Zn.exports=function(r,e,t){Xn.call(this),r=er.normalizeQueryConfig(r,e,t),this.text=r.text,this.values=r.values,this.name=r.name,this.queryMode=r.queryMode,this.callback=r.callback,this.state="new",this._arrayMode=r.rowMode==="array",this._emitRowEvents=!1,this.on("newListener",function(n){n==="row"&&(this._emitRowEvents=!0)}.bind(this))};La.inherits(oe,Xn);var Da={sqlState:"code",statementPosition:"position",messagePrimary:"message",context:"where",schemaName:"schema",tableName:"table",columnName:"column",dataTypeName:"dataType",constraintName:"constraint",sourceFile:"file",sourceLine:"line",sourceFunction:"routine"};oe.prototype.handleError=function(r){let e=this.native.pq.resultErrorFields();if(e)for(let t in e){let n=Da[t]||t;r[n]=e[t]}this.callback?this.callback(r):this.emit("error",r),this.state="error"};oe.prototype.then=function(r,e){return this._getPromise().then(r,e)};oe.prototype.catch=function(r){return this._getPromise().catch(r)};oe.prototype._getPromise=function(){return this._promise?this._promise:(this._promise=new Promise(function(r,e){this._once("end",r),this._once("error",e)}.bind(this)),this._promise)};oe.prototype.submit=function(r){this.state="running";let e=this;this.native=r.native,r.native.arrayMode=this._arrayMode;let t=function(n,s,i){if(r.native.arrayMode=!1,setImmediate(function(){e.emit("_done")}),n)return e.handleError(n);e._emitRowEvents&&(i.length>1?s.forEach((o,a)=>{o.forEach(l=>{e.emit("row",l,i[a])})}):s.forEach(function(o){e.emit("row",o,i)})),e.state="end",e.emit("end",i),e.callback&&e.callback(null,i)};if(process.domain&&(t=process.domain.bind(t)),this.name){this.name.length>63&&(console.error("Warning! Postgres only supports 63 characters for query names."),console.error("You supplied %s (%s)",this.name,this.name.length),console.error("This can cause conflicts and silent errors executing queries"));let n=(this.values||[]).map(er.prepareValue);if(r.namedQueries[this.name]){if(this.text&&r.namedQueries[this.name]!==this.text){let s=new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`);return t(s)}return r.native.execute(this.name,n,t)}return r.native.prepare(this.name,this.text,n.length,function(s){return s?t(s):(r.namedQueries[e.name]=e.text,e.native.execute(e.name,n,t))})}else if(this.values){if(!Array.isArray(this.values)){let s=new Error("Query values must be an array");return t(s)}let n=this.values.map(er.prepareValue);r.native.query(this.text,n,t)}else this.queryMode==="extended"?r.native.query(this.text,[],t):r.native.query(this.text,t)}});var is=b((Ll,ss)=>{"use strict";u();var Qa=S("util"),ts;try{ts=S("pg-native")}catch(r){throw r}var Na=Ce(),rs=S("events").EventEmitter,Ba=S("util"),Fa=yt(),ns=es(),Ua=Qa.deprecate(()=>{},"Calling client.query() when the client is already executing a query is deprecated and will be removed in pg@9.0. Use async/await or an external async flow control mechanism instead."),P=ss.exports=function(r){rs.call(this),r=r||{},this._Promise=r.Promise||global.Promise,this._types=new Na(r.types),this.native=new ts({types:this._types}),this._queryQueue=[],this._ending=!1,this._connecting=!1,this._connected=!1,this._queryable=!0;let e=this.connectionParameters=new Fa(r);r.nativeConnectionString&&(e.nativeConnectionString=r.nativeConnectionString),this.user=e.user,Object.defineProperty(this,"password",{configurable:!0,enumerable:!1,writable:!0,value:e.password}),this.database=e.database,this.host=e.host,this.port=e.port,this.namedQueries={}};P.Query=ns;Ba.inherits(P,rs);P.prototype._errorAllQueries=function(r){let e=t=>{process.nextTick(()=>{t.native=this.native,t.handleError(r)})};this._hasActiveQuery()&&(e(this._activeQuery),this._activeQuery=null),this._queryQueue.forEach(e),this._queryQueue.length=0};P.prototype._connect=function(r){let e=this;if(this._connecting){process.nextTick(()=>r(new Error("Client has already been connected. You cannot reuse a client.")));return}this._connecting=!0,this.connectionParameters.getLibpqConnectionString(function(t,n){if(e.connectionParameters.nativeConnectionString&&(n=e.connectionParameters.nativeConnectionString),t)return r(t);e.native.connect(n,function(s){if(s)return e.native.end(),r(s);e._connected=!0,e.native.on("error",function(i){e._queryable=!1,e._errorAllQueries(i),e.emit("error",i)}),e.native.on("notification",function(i){e.emit("notification",{channel:i.relname,payload:i.extra})}),e.emit("connect"),e._pulseQueryQueue(!0),r(null,this)})})};P.prototype.connect=function(r){if(r){this._connect(r);return}return new this._Promise((e,t)=>{this._connect(n=>{n?t(n):e(this)})})};P.prototype.query=function(r,e,t){let n,s,i,o,a;if(r==null)throw new TypeError("Client was passed a null or undefined query");if(typeof r.submit=="function")i=r.query_timeout||this.connectionParameters.query_timeout,s=n=r,typeof e=="function"&&(r.callback=e);else if(i=r.query_timeout||this.connectionParameters.query_timeout,n=new ns(r,e,t),!n.callback){let l,c;s=new this._Promise((h,d)=>{l=h,c=d}).catch(h=>{throw Error.captureStackTrace(h),h}),n.callback=(h,d)=>h?c(h):l(d)}return i&&(a=n.callback||(()=>{}),o=setTimeout(()=>{let l=new Error("Query read timeout");process.nextTick(()=>{n.handleError(l,this.connection)}),a(l),n.callback=()=>{};let c=this._queryQueue.indexOf(n);c>-1&&this._queryQueue.splice(c,1),this._pulseQueryQueue()},i),n.callback=(l,c)=>{clearTimeout(o),a(l,c)}),this._queryable?this._ending?(n.native=this.native,process.nextTick(()=>{n.handleError(new Error("Client was closed and is not queryable"))}),s):(this._queryQueue.length>0&&Ua(),this._queryQueue.push(n),this._pulseQueryQueue(),s):(n.native=this.native,process.nextTick(()=>{n.handleError(new Error("Client has encountered a connection error and is not queryable"))}),s)};P.prototype.end=function(r){let e=this;this._ending=!0,this._connected||this.once("connect",this.end.bind(this,r));let t;return r||(t=new this._Promise(function(n,s){r=i=>i?s(i):n()})),this.native.end(function(){e._connected=!1,e._errorAllQueries(new Error("Connection terminated")),process.nextTick(()=>{e.emit("end"),r&&r()})}),t};P.prototype._hasActiveQuery=function(){return this._activeQuery&&this._activeQuery.state!=="error"&&this._activeQuery.state!=="end"};P.prototype._pulseQueryQueue=function(r){if(!this._connected||this._hasActiveQuery())return;let e=this._queryQueue.shift();if(!e){r||this.emit("drain");return}this._activeQuery=e,e.submit(this);let t=this;e.once("_done",function(){t._pulseQueryQueue()})};P.prototype.cancel=function(r){this._activeQuery===r?this.native.cancel(function(){}):this._queryQueue.indexOf(r)!==-1&&this._queryQueue.splice(this._queryQueue.indexOf(r),1)};P.prototype.ref=function(){};P.prototype.unref=function(){};P.prototype.setTypeParser=function(r,e,t){return this._types.setTypeParser(r,e,t)};P.prototype.getTypeParser=function(r,e){return this._types.getTypeParser(r,e)};P.prototype.isConnected=function(){return this._connected}});var tr=b((Ql,os)=>{"use strict";u();os.exports=is()});var Zt=b((Fl,De)=>{"use strict";u();var ja=Vn(),$a=me(),Ha=zt(),za=bt(),Ka=Z(),Ga=Jn(),Va=Ce(),{DatabaseError:Wa}=jt(),{escapeIdentifier:Ya,escapeLiteral:Ja}=Z(),Xa=r=>class extends Ga{constructor(t){super(t,r)}},as=function(r){this.defaults=$a,this.Client=r,this.Query=this.Client.Query,this.Pool=Xa(this.Client),this._pools=[],this.Connection=Ha,this.types=pe(),this.DatabaseError=Wa,this.TypeOverrides=Va,this.escapeIdentifier=Ya,this.escapeLiteral=Ja,this.Result=za,this.utils=Ka},cs=ja,us=!1;try{us=!!process.env.NODE_PG_FORCE_NATIVE}catch{}us&&(cs=tr());De.exports=new as(cs);Object.defineProperty(De.exports,"native",{configurable:!0,enumerable:!1,get(){let r=null;try{r=new as(tr())}catch(e){if(e.code!=="MODULE_NOT_FOUND")throw e}return Object.defineProperty(De.exports,"native",{value:r}),r}})});var ls={};bs(ls,{Client:()=>Za,Connection:()=>tc,DatabaseError:()=>sc,Pool:()=>ec,Query:()=>nc,Result:()=>ac,TypeOverrides:()=>cc,default:()=>lc,defaults:()=>uc,escapeIdentifier:()=>ic,escapeLiteral:()=>oc,types:()=>rc});var I,Za,ec,tc,rc,nc,sc,ic,oc,ac,cc,uc,lc,hs=or(()=>{"use strict";u();I=Ss(Zt(),1),Za=I.default.Client,ec=I.default.Pool,tc=I.default.Connection,rc=I.default.types,nc=I.default.Query,sc=I.default.DatabaseError,ic=I.default.escapeIdentifier,oc=I.default.escapeLiteral,ac=I.default.Result,cc=I.default.TypeOverrides,uc=I.default.defaults,lc=I.default});u();u();u();var U=class extends Error{constructor(t,n){super(t);this.code=n;this.name="KnowledgeError"}code},A=class extends U{constructor(t,n){super(t,"EMBEDDING_ERROR");this.statusCode=n;this.name="EmbeddingError"}statusCode},k=class extends U{constructor(t,n){super(t,"INGESTION_ERROR");this.file=n;this.name="IngestionError"}file},ar=class extends U{constructor(t,n){super(t,"CHUNK_TOO_LARGE");this.chunkSize=n;this.name="ChunkTooLargeError"}chunkSize},G=class extends U{expected;actual;constructor(e,t){super(`Dimension mismatch: expected ${e}, got ${t}`,"DIMENSION_MISMATCH"),this.name="DimensionMismatchError",this.expected=e,this.actual=t}},j=class extends U{constructor(e){super(e,"PROVIDER_ERROR"),this.name="KnowledgeProviderError"}};u();import{randomUUID as vs}from"crypto";u();function $(r,e){let t=r.toLowerCase(),n=e.toLowerCase();if(t.includes(n))return 1;let s=n.split(/\s+/).filter(o=>o.length>2);if(s.length===0)return 0;let i=0;for(let o of s)t.includes(o)&&i++;return i/s.length}function Fe(r,e,t=.7){let n=1-t;return r*t+e*n}u();function Se(r,e){if(r.length!==e.length)throw new Error("Vectors must have same dimensions");let t=0,n=0,s=0;for(let o=0;o<r.length;o++)t+=r[o]*e[o],n+=r[o]*r[o],s+=e[o]*e[o];let i=Math.sqrt(n)*Math.sqrt(s);return i===0?0:t/i}function Q(r,e){if(!e)return!0;for(let[t,n]of Object.entries(e)){let s=r[t];if(typeof n=="object"&&n!==null&&!Array.isArray(n)){if("$in"in n){if(!n.$in.includes(s))return!1}else if("$gt"in n){let i=n.$gt;if(typeof s!="number"||s<=i)return!1}else if("$lt"in n){let i=n.$lt;if(typeof s!="number"||s>=i)return!1}}else if(s!==n)return!1}return!0}var cr=class r{constructor(e,t,n,s,i){this.provider=e;this.embedder=t;this.description=n;this.sources=s;this.options=i}provider;embedder;description;sources;options;static async create(e){await e.provider.validateDimensions(e.embedder.dimensions);let t=new r(e.provider,e.embedder,e.description,e.sources,e),n=e.reSync!==!1;return!n&&"shouldReSync"in e.provider?(e.provider.shouldReSync()&&await t.sync(),t):(n&&await t.sync(),t)}async query(e,t){let n=t?.searchType??"semantic",s=t?.semanticWeight??.7;if(n==="keyword")return this.keywordQuery(e,t);if(n==="hybrid"){let[i,o]=await Promise.all([this.semanticQuery(e,t),this.keywordQuery(e,t)]);return this.combineHybridResults(i,o,s,t)}else return this.semanticQuery(e,t)}async semanticQuery(e,t){let n=await this.embedder.embed(e);return this.provider.query(n,t)}async keywordQuery(e,t){let{limit:n=10,threshold:s=.1,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t||{};if(typeof this.provider.keywordQuery=="function")return this.provider.keywordQuery(e,t);let l=await this.getAllChunks(),c=[];for(let h of l){if(i&&!Q(h.metadata,i))continue;let d=$(h.content,e);d>=s&&c.push({chunk:{id:h.id,content:h.content,metadata:o?h.metadata:{},vector:a?h.vector:void 0},score:d,distance:1-d})}return c.sort((h,d)=>d.score-h.score),c.slice(0,n)}combineHybridResults(e,t,n,s){let{limit:i=10,threshold:o=.5,includeMetadata:a=!0,includeVectors:l=!1}=s||{},c=new Map(e.map(y=>[y.chunk.id,y])),h=new Map(t.map(y=>[y.chunk.id,y])),d=[],m=new Set([...c.keys(),...h.keys()]);for(let y of m){let w=c.get(y),T=h.get(y);if(!w&&!T)continue;let Qe=w?.score??0,Ne=T?.score??0,K=Fe(Qe,Ne,n);K>=o&&d.push({chunk:{id:y,content:w?.chunk.content??T.chunk.content,metadata:a?w?.chunk.metadata??T.chunk.metadata:{},vector:l?w?.chunk.vector??T.chunk.vector:void 0},score:K,distance:1-K})}return d.sort((y,w)=>w.score-y.score),d.slice(0,i)}async getAllChunks(){if(typeof this.provider.getAllChunks=="function")return this.provider.getAllChunks();let e=new Array(this.embedder.dimensions).fill(0);return(await this.provider.query(e,{limit:1e4,threshold:0})).map(t=>t.chunk)}async sync(){this.options.onSync?.({type:"start"});try{let e=this.embedder.dimensions;await this.provider.clear(),await this.provider.validateDimensions(e);let t=this.options.streamingBatchSize??100,n=0,s=[];for(let i of this.sources)for await(let o of i.load())if(s.push(o),s.length>=t){let a=await this.embedChunks(s);a.length>0&&(await this.provider.add(a),n+=a.length),s.length=0}if(s.length>0){let i=await this.embedChunks(s);i.length>0&&(await this.provider.add(i),n+=i.length)}this.options.onSync?.({type:"complete",chunksAffected:n})}catch(e){throw this.options.onSync?.({type:"error",error:e}),e}}async embedChunks(e){if(e.length===0)return[];let t=[];try{let n=e.map(i=>i.content),s=await this.embedder.embedBatch(n);for(let i=0;i<e.length;i++)t.push({...e[i],vector:s[i]}),this.options.onEmbeddingProgress?.({source:"sync",current:i+1,total:e.length,percent:Math.round((i+1)/e.length*100)})}catch(n){if(this.options.onError?.(n,{})==="abort")throw n;for(let i=0;i<e.length;i++)try{let o=await this.embedder.embed(e[i].content);t.push({...e[i],vector:o}),this.options.onEmbeddingProgress?.({source:"sync",current:i+1,total:e.length,percent:Math.round((i+1)/e.length*100)})}catch(o){if(this.options.onError?.(o,{chunk:e[i]})==="abort")throw o}}return t}async add(e,t){try{let n=vs(),s=await this.embedder.embed(e),i={id:n,content:e,metadata:t||{},vector:s};return await this.provider.add([i]),n}catch(n){throw new k(`Failed to add content to knowledge base: ${n.message}`,"add")}}async stop(){this.provider.close&&this.provider.close()}toTool(){return{name:"knowledge_search",displayName:"Knowledge Search",description:this.description||"Search the knowledge base for relevant information",category:"search",cacheable:!1,parameters:{type:"object",properties:{query:{type:"string",description:"Search query to find relevant information"},limit:{type:"number",description:"Maximum number of results to return (default: 10)"},threshold:{type:"number",description:"Minimum similarity threshold 0-1 (default: 0.3)"},filter:{type:"object",description:"Optional metadata filters"}},required:["query"]},execute:async e=>(await this.query(e.query,{limit:e.limit,threshold:e.threshold??.3,filter:e.filter,searchType:"hybrid"})).map(n=>({content:n.chunk.content,score:n.score,metadata:n.chunk.metadata}))}}};u();var Ue=class{constructor(e={}){this.options=e}options;chunks=new Map;dimensions;async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new G(this.dimensions,e);this.dimensions=e}async add(e){for(let t of e){if(!t.vector)throw new j("Chunk missing vector");if(this.options.maxChunks&&this.chunks.size>=this.options.maxChunks)throw new j(`Max chunks limit reached: ${this.options.maxChunks}`);this.chunks.set(t.id,{chunk:{id:t.id,content:t.content,metadata:t.metadata},vector:t.vector})}}async query(e,t={}){let{limit:n=10,threshold:s=.7,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=[];for(let{chunk:c,vector:h}of this.chunks.values()){if(i&&!Q(c.metadata,i))continue;let d=Se(e,h);d>=s&&l.push({chunk:{id:c.id,content:c.content,metadata:o?c.metadata:{},vector:a?h:void 0},score:d,distance:1-d})}return l.sort((c,h)=>h.score-c.score),l.slice(0,n)}async keywordQuery(e,t={}){let{limit:n=10,threshold:s=.1,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=[];for(let{chunk:c,vector:h}of this.chunks.values()){if(i&&!Q(c.metadata,i))continue;let d=$(c.content,e);d>=s&&l.push({chunk:{id:c.id,content:c.content,metadata:o?c.metadata:{},vector:a?h:void 0},score:d,distance:1-d})}return l.sort((c,h)=>h.score-c.score),l.slice(0,n)}async delete(e){for(let t of e)this.chunks.delete(t)}async clear(){this.chunks.clear(),this.dimensions=void 0}async getAllChunks(){return Array.from(this.chunks.values()).map(({chunk:e,vector:t})=>({...e,vector:t}))}};u();import Es from"better-sqlite3";import*as ae from"fs";import*as V from"path";import*as ur from"os";var je=class{constructor(e){this.options=e;let t=ur.homedir(),n=`${e.namespace}.db`,s;if(e.storagePath)s=e.storagePath;else{let i=V.join(t,".toolpack","db","knowledge"),o=V.join(t,".toolpack","knowledge"),a=V.join(i,n),l=V.join(o,n);ae.existsSync(l)&&!ae.existsSync(a)?s=o:s=i}this.dbPath=V.join(s,n),ae.mkdirSync(s,{recursive:!0}),this.db=new Es(this.dbPath),this.db.pragma("journal_mode = WAL"),this.initSchema(),this.loadDimensions()}options;db;dimensions;dbPath;initSchema(){this.db.exec(`
2
10
  CREATE TABLE IF NOT EXISTS chunks (
3
11
  id TEXT PRIMARY KEY,
4
12
  content TEXT NOT NULL,
@@ -30,22 +38,35 @@ var v=class extends Error{constructor(t,n){super(t);this.code=n;this.name="Knowl
30
38
  BEGIN
31
39
  UPDATE chunks_fts SET content = new.content, metadata = new.metadata WHERE id = new.id;
32
40
  END;
33
- `)}loadDimensions(){let e=this.db.prepare("SELECT value FROM provider_meta WHERE key = ?").get("dimensions");e&&(this.dimensions=parseInt(e.value,10))}async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new C(this.dimensions,e);this.dimensions||(this.db.prepare("INSERT OR REPLACE INTO provider_meta (key, value) VALUES (?, ?)").run("dimensions",e.toString()),this.dimensions=e)}async add(e){let t=this.db.prepare(`
41
+ `)}loadDimensions(){let e=this.db.prepare("SELECT value FROM provider_meta WHERE key = ?").get("dimensions");e&&(this.dimensions=parseInt(e.value,10))}async validateDimensions(e){if(this.dimensions&&this.dimensions!==e)throw new G(this.dimensions,e);this.dimensions||(this.db.prepare("INSERT OR REPLACE INTO provider_meta (key, value) VALUES (?, ?)").run("dimensions",e.toString()),this.dimensions=e)}async add(e){let t=this.db.prepare(`
34
42
  INSERT OR REPLACE INTO chunks (id, content, metadata, vector, synced_at)
35
43
  VALUES (?, ?, ?, ?, ?)
36
- `);this.db.transaction(r=>{for(let s of r){if(!s.vector)throw new E("Chunk missing vector");let o=Buffer.from(new Float32Array(s.vector).buffer);t.run(s.id,s.content,JSON.stringify(s.metadata),o,Date.now())}})(e)}async query(e,t={}){let{limit:n=10,threshold:r=.7,filter:s,includeMetadata:o=!0,includeVectors:a=!1}=t,m=this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all(),i=[];for(let u of m){let c=JSON.parse(u.metadata);if(s&&!w(c,s))continue;let h=new Float32Array(u.vector.buffer,u.vector.byteOffset,u.vector.byteLength/4),l=Array.from(h),p=T(e,l);p>=r&&i.push({chunk:{id:u.id,content:u.content,metadata:o?c:{},vector:a?l:void 0},score:p,distance:1-p})}return i.sort((u,c)=>c.score-u.score),i.slice(0,n)}async keywordQuery(e,t={}){let{limit:n=10,threshold:r=.1,filter:s,includeMetadata:o=!0,includeVectors:a=!1}=t,m=e.split(/\s+/).map(c=>`"${c}"`).join(" OR "),i=this.db.prepare(`
44
+ `);this.db.transaction(s=>{for(let i of s){if(!i.vector)throw new j("Chunk missing vector");let o=Buffer.from(new Float32Array(i.vector).buffer);t.run(i.id,i.content,JSON.stringify(i.metadata),o,Date.now())}})(e)}async query(e,t={}){let{limit:n=10,threshold:s=.7,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all(),c=[];for(let h of l){let d=JSON.parse(h.metadata);if(i&&!Q(d,i))continue;let m=new Float32Array(h.vector.buffer,h.vector.byteOffset,h.vector.byteLength/4),y=Array.from(m),w=Se(e,y);w>=s&&c.push({chunk:{id:h.id,content:h.content,metadata:o?d:{},vector:a?y:void 0},score:w,distance:1-w})}return c.sort((h,d)=>d.score-h.score),c.slice(0,n)}async keywordQuery(e,t={}){let{limit:n=10,threshold:s=.1,filter:i,includeMetadata:o=!0,includeVectors:a=!1}=t,l=e.split(/\s+/).map(d=>`"${d}"`).join(" OR "),c=this.db.prepare(`
37
45
  SELECT c.id, c.content, c.metadata, c.vector, highlight(chunks_fts, 1, '<mark>', '</mark>') as highlighted
38
46
  FROM chunks_fts fts
39
47
  JOIN chunks c ON fts.id = c.id
40
48
  WHERE chunks_fts MATCH ?
41
49
  ORDER BY bm25(chunks_fts) DESC
42
50
  LIMIT ?
43
- `).all(m,n*2),u=[];for(let c of i){let h=JSON.parse(c.metadata);if(s&&!w(h,s))continue;let l=x(c.content,e);if(l>=r){let p=new Float32Array(c.vector.buffer,c.vector.byteOffset,c.vector.byteLength/4);u.push({chunk:{id:c.id,content:c.content,metadata:o?h:{},vector:a?Array.from(p):void 0},score:l,distance:1-l})}}return u.sort((c,h)=>h.score-c.score),u.slice(0,n)}async delete(e){let t=this.db.prepare("DELETE FROM chunks WHERE id = ?");this.db.transaction(r=>{for(let s of r)t.run(s)})(e)}async clear(){this.db.prepare("DELETE FROM chunks").run(),this.db.prepare("DELETE FROM provider_meta WHERE key = ?").run("dimensions"),this.dimensions=void 0}async getAllChunks(){return this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all().map(t=>{let n=JSON.parse(t.metadata),r=new Float32Array(t.vector.buffer,t.vector.byteOffset,t.vector.byteLength/4);return{id:t.id,content:t.content,metadata:n,vector:Array.from(r)}})}shouldReSync(){return this.options.reSync===!1?this.db.prepare("SELECT COUNT(*) as count FROM chunks").get().count===0:!0}close(){this.db.close()}};import*as q from"fs/promises";import*as R from"path";import*as W from"crypto";import ee from"fast-glob";function y(d){return Math.ceil(d.length/4)}function Y(d,e){let t=d.split(/\n\n+/),n=[],r="";for(let s of t){let o=y(s);y(r)+o>e&&r?(n.push(r.trim()),r=s):r+=(r?`
51
+ `).all(l,n*2),h=[];for(let d of c){let m=JSON.parse(d.metadata);if(i&&!Q(m,i))continue;let y=$(d.content,e);if(y>=s){let w=new Float32Array(d.vector.buffer,d.vector.byteOffset,d.vector.byteLength/4);h.push({chunk:{id:d.id,content:d.content,metadata:o?m:{},vector:a?Array.from(w):void 0},score:y,distance:1-y})}}return h.sort((d,m)=>m.score-d.score),h.slice(0,n)}async delete(e){let t=this.db.prepare("DELETE FROM chunks WHERE id = ?");this.db.transaction(s=>{for(let i of s)t.run(i)})(e)}async clear(){this.db.prepare("DELETE FROM chunks").run(),this.db.prepare("DELETE FROM provider_meta WHERE key = ?").run("dimensions"),this.dimensions=void 0}async getAllChunks(){return this.db.prepare("SELECT id, content, metadata, vector FROM chunks").all().map(t=>{let n=JSON.parse(t.metadata),s=new Float32Array(t.vector.buffer,t.vector.byteOffset,t.vector.byteLength/4);return{id:t.id,content:t.content,metadata:n,vector:Array.from(s)}})}shouldReSync(){return this.options.reSync===!1?this.db.prepare("SELECT COUNT(*) as count FROM chunks").get().count===0:!0}close(){this.db.close()}};u();import*as lr from"fs/promises";import*as ce from"path";import*as hr from"crypto";import Cs from"fast-glob";u();function M(r){return Math.ceil(r.length/4)}function _s(r,e){let t=r.split(/\n\n+/),n=[],s="";for(let i of t){let o=M(i);M(s)+o>e&&s?(n.push(s.trim()),s=i):s+=(s?`
52
+
53
+ `:"")+i}return s&&n.push(s.trim()),n}function ks(r,e){let t=r.match(/[^.!?]+[.!?]+/g)||[r],n=[],s="";for(let i of t){let o=M(i);M(s)+o>e&&s?(n.push(s.trim()),s=i):s+=(s?" ":"")+i}return s&&n.push(s.trim()),n}function W(r,e){if(r.length<=1||e===0)return r;let t=[];for(let n=0;n<r.length;n++){let s=r[n];if(n>0){let o=r[n-1].split(/\s+/),a=Math.ceil(e/4);s=o.slice(-a).join(" ")+" "+s}t.push(s)}return t}function Y(r,e){if(M(r)<=e)return[r];let n=_s(r,e),s=[];for(let i of n)M(i)>e?s.push(...ks(i,e)):s.push(i);return s}var $e=class{constructor(e,t={}){this.pattern=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"markdown",metadata:t.metadata??{}}}pattern;options;async*load(){let e=this.pattern.replace(/\\/g,"/"),t=await Cs(e,{absolute:!0});for(let n of t)try{let s=await lr.readFile(n,"utf-8"),i=this.chunkMarkdown(s,n);for(let o of i)yield o}catch(s){throw new k(`Failed to process file: ${s.message}`,n)}}chunkMarkdown(e,t){let n=this.extractFrontmatter(e),s=this.removeFrontmatter(e),i=this.parseHeadings(s),o=[],a=0;for(let l of i){let c=/```[\s\S]*?```/.test(l.content),h=M(l.content);if(h<this.options.minChunkSize&&o.length>0){let m=o[o.length-1];m.content+=`
54
+
55
+ `+l.content,c&&(m.metadata.hasCode=!0);continue}let d;h>this.options.maxChunkSize?d=Y(l.content,this.options.maxChunkSize):d=[l.content],this.options.chunkOverlap>0&&d.length>1&&(d=W(d,this.options.chunkOverlap));for(let m=0;m<d.length;m++){let y=d[m],w=this.generateChunkId(t,y,a);o.push({id:w,content:y,metadata:{...this.options.metadata,...n,heading:l.heading,hasCode:c,source:ce.basename(t),sourcePath:t,chunkIndex:a,totalChunks:d.length}}),a++}}return o}parseHeadings(e){let t=e.split(`
56
+ `),n=[],s=[],i=[];for(let o of t){let a=o.match(/^(#{1,6})\s+(.+)$/);if(a){if(i.length>0){let h=s.map(d=>d.text);n.push({heading:h.length>0?[...h]:[""],content:i.join(`
57
+ `).trim(),level:s.length>0?s[s.length-1].level:0}),i=[]}let l=a[1].length,c=a[2].trim();for(;s.length>0&&s[s.length-1].level>=l;)s.pop();s.push({level:l,text:c}),i.push(o)}else i.push(o)}if(i.length>0){let o=s.map(a=>a.text);n.push({heading:o.length>0?[...o]:[""],content:i.join(`
58
+ `).trim(),level:s.length>0?s[s.length-1].level:0})}return n.filter(o=>o.content.length>0)}extractFrontmatter(e){let t=e.match(/^---\n([\s\S]*?)\n---/);if(!t)return{};let n=t[1],s={},i=n.split(`
59
+ `);for(let o of i){let a=o.match(/^(\w+):\s*(.+)$/);if(a){let l=a[1],c=a[2].trim();c==="true"?c=!0:c==="false"?c=!1:isNaN(Number(c))?typeof c=="string"&&c.startsWith("[")&&c.endsWith("]")&&(c=c.slice(1,-1).split(",").map(h=>h.trim())):c=Number(c),s[l]=c}}return s}removeFrontmatter(e){return e.replace(/^---\n[\s\S]*?\n---\n/,"")}generateChunkId(e,t,n){let s=hr.createHash("md5").update(t).digest("hex").substring(0,8),i=ce.basename(e,ce.extname(e));return`${this.options.namespace}:${i}:${n}:${s}`}};u();import*as He from"crypto";import*as dr from"cheerio";var ze=class{constructor(e,t={}){this.urls=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"web",metadata:t.metadata??{},maxDepth:t.maxDepth??1,userAgent:t.userAgent??"Toolpack-Knowledge/1.0",delayMs:t.delayMs??1e3,timeoutMs:t.timeoutMs??3e4,sameDomainOnly:t.sameDomainOnly??!0,maxPagesPerDomain:t.maxPagesPerDomain??10}}urls;options;crawledUrls=new Set;domainPageCount=new Map;lastRequestTime=new Map;async*load(){let e=await this.crawlUrls(this.urls,0);for(let t of e)try{let n=this.chunkPage(t);for(let s of n)yield s}catch(n){throw new k(`Failed to process URL ${t.url}: ${n.message}`,t.url)}}async crawlUrls(e,t){if(t>=this.options.maxDepth)return[];let n=[],s=[],i=new Set(e.map(o=>new URL(o).hostname));for(let o of e){if(this.crawledUrls.has(o))continue;let a=new URL(o).hostname,l=this.domainPageCount.get(a)??0;if(!(this.options.sameDomainOnly&&!i.has(a))&&!(l>=this.options.maxPagesPerDomain)){this.crawledUrls.add(o),this.domainPageCount.set(a,l+1);try{let c=this.lastRequestTime.get(a)??0,h=Date.now()-c;h<this.options.delayMs&&await new Promise(m=>setTimeout(m,this.options.delayMs-h));let d=await this.fetchPage(o);n.push(d),this.lastRequestTime.set(a,Date.now()),t<this.options.maxDepth-1&&s.push(...d.links)}catch(c){console.warn(`Failed to crawl ${o}: ${c.message}`)}}}if(s.length>0){let o=await this.crawlUrls(s,t+1);n.push(...o)}return n}async fetchPage(e){let t=new AbortController,n=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let s=await fetch(e,{signal:t.signal,headers:{"User-Agent":this.options.userAgent}});if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);let i=await s.text(),o=dr.load(i);o("script, style, nav, header, footer, aside").remove();let a=o("title").text().trim()||o("h1").first().text().trim()||"Untitled",l=["main","article",".content","#content","body"],c="";for(let d of l){let m=o(d);if(m.length>0){c=m.text().trim();break}}c||(c=o("body").text().trim()),c=c.replace(/\s+/g," ").trim();let h=[];return o("a[href]").each((d,m)=>{let y=o(m).attr("href");if(y)try{let w=new URL(y,e).href;w.startsWith("http")&&!w.includes("#")&&h.push(w)}catch{}}),{url:e,title:a,content:c,links:[...new Set(h)]}}finally{clearTimeout(n)}}chunkPage(e){let t=[],n=M(e.content),s;n>this.options.maxChunkSize?s=Y(e.content,this.options.maxChunkSize):s=[e.content],this.options.chunkOverlap>0&&s.length>1&&(s=W(s,this.options.chunkOverlap));for(let i=0;i<s.length;i++){let o=s[i],a=this.generateChunkId(e.url,o,i);t.push({id:a,content:o,metadata:{...this.options.metadata,title:e.title,url:e.url,source:"web",chunkIndex:i,totalChunks:s.length}})}return t}generateChunkId(e,t,n){let s=He.createHash("md5").update(t).digest("hex").substring(0,8),i=He.createHash("md5").update(e).digest("hex").substring(0,8);return`${this.options.namespace}:${i}:${n}:${s}`}};u();import*as Ke from"crypto";var Ge=class{constructor(e,t={}){this.url=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"api",metadata:t.metadata??{},headers:t.headers??{},method:t.method??"GET",timeoutMs:t.timeoutMs??3e4,pagination:t.pagination,dataPath:t.dataPath??"",contentExtractor:t.contentExtractor??this.defaultContentExtractor,metadataExtractor:t.metadataExtractor??this.defaultMetadataExtractor}}url;options;async*load(){let e=await this.fetchData();for(let t of e)try{let n=this.chunkItem(t);for(let s of n)yield s}catch(n){throw new k(`Failed to process API item: ${n.message}`,this.url)}}async fetchData(){let e=[],t=this.options.pagination?.start??0,n=this.options.pagination?.maxPages??1;for(;t<n;){let s=this.buildUrl(t),i=await this.fetchPage(s);if(i.length===0||(e.push(...i),t++,!this.options.pagination))break}return e}buildUrl(e){if(!this.options.pagination)return this.url;let t=new URL(this.url);return t.searchParams.set(this.options.pagination.param,e.toString()),t.href}async fetchPage(e){let t=new AbortController,n=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let s=await fetch(e,{method:this.options.method,headers:{"Content-Type":"application/json",...this.options.headers},body:this.options.body?JSON.stringify(this.options.body):void 0,signal:t.signal});if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);let i=await s.json();return this.extractItems(i)}finally{clearTimeout(n)}}extractItems(e){if(!this.options.dataPath)return Array.isArray(e)?e:[e];let t=this.options.dataPath.split("."),n=e;for(let s of t)if(n&&typeof n=="object"&&s in n)n=n[s];else throw new Error(`Data path '${this.options.dataPath}' not found in response`);return Array.isArray(n)?n:[n]}chunkItem(e){let t=this.options.contentExtractor(e),n=this.options.metadataExtractor(e),s=M(t),i;s>(this.options.maxChunkSize??2e3)?i=Y(t,this.options.maxChunkSize??2e3):i=[t],(this.options.chunkOverlap??200)>0&&i.length>1&&(i=W(i,this.options.chunkOverlap??200));let o=[];for(let a=0;a<i.length;a++){let l=i[a],c=this.generateChunkId(e,l,a);o.push({id:c,content:l,metadata:{...this.options.metadata,...n,source:"api",apiUrl:this.url,chunkIndex:a,totalChunks:i.length}})}return o}defaultContentExtractor(e){if(typeof e=="string")return e;if(typeof e=="object"&&e!==null){let t=["content","text","description","body","message"];for(let n of t)if(n in e&&typeof e[n]=="string")return e[n];return JSON.stringify(e)}return String(e)}defaultMetadataExtractor(e){if(typeof e=="object"&&e!==null){let t={},n=["id","title","name","created_at","updated_at","author","tags"];for(let s of n)s in e&&(t[s]=e[s]);return t}return{}}generateChunkId(e,t,n){let s=Ke.createHash("md5").update(t).digest("hex").substring(0,8),i=Ke.createHash("md5").update(JSON.stringify(e)).digest("hex").substring(0,8);return`${this.options.namespace}:${i}:${n}:${s}`}};u();import*as fr from"fs/promises";import*as Ve from"path";var We=class{constructor(e,t){this.filePath=e;if(!t.toContent)throw new k("JSONSource requires a toContent callback. Example: toContent: (item) => `Name: ${item.name}`",this.filePath);this.options={namespace:t.namespace??"json",metadata:t.metadata??{},filter:t.filter??(()=>!0),chunkSize:t.chunkSize??100,toContent:t.toContent}}filePath;options;async*load(){let e;try{let t=await fr.readFile(this.filePath,"utf-8");e=JSON.parse(t)}catch(t){throw new k(`Failed to parse JSON file: ${t.message}`,this.filePath)}if(Array.isArray(e)){let n=e.filter(this.options.filter).map(this.options.toContent);for(let s=0;s<n.length;s+=this.options.chunkSize){let o=n.slice(s,s+this.options.chunkSize).join(`
60
+
61
+ ---
62
+
63
+ `);yield{id:`json:${this.options.namespace}:${s}`,content:o,metadata:{...this.options.metadata,source:Ve.basename(this.filePath),type:"json_array_chunk",startIndex:s,endIndex:Math.min(s+this.options.chunkSize,n.length),totalItems:n.length}}}}else{let t=typeof e=="object"&&e!==null?this.options.toContent(e):typeof e=="string"?e:JSON.stringify(e);yield{id:`json:${this.options.namespace}:0`,content:t,metadata:{...this.options.metadata,source:Ve.basename(this.filePath),type:"json_object"}}}}};u();import*as pr from"fs/promises";import*as mr from"path";var Ye=class{constructor(e,t){this.dbPath=e;if(!t.toContent)throw new k("SQLiteSource requires a toContent callback. Example: toContent: (row) => `Name: ${row.name}`",this.dbPath);this.options={namespace:t.namespace??"sqlite",metadata:t.metadata??{},chunkSize:t.chunkSize??100,toContent:t.toContent,query:t.query,preLoadCSV:t.preLoadCSV}}dbPath;options;async*load(){let e;try{e=(await import("better-sqlite3")).default}catch{throw new k('SQLite source requires "better-sqlite3" package. Install with: npm install better-sqlite3',this.dbPath)}try{await pr.access(this.dbPath)}catch{throw new k("SQLite database file not found",this.dbPath)}let t=new e(this.dbPath);try{this.options.preLoadCSV&&await this.loadCSV(t,this.options.preLoadCSV);let n=this.options.query??'SELECT * FROM sqlite_master WHERE type = "table"',o=t.prepare(n).all().map(a=>this.options.toContent(a));for(let a=0;a<o.length;a+=this.options.chunkSize){let c=o.slice(a,a+this.options.chunkSize).join(`
64
+
65
+ ---
66
+
67
+ `);yield{id:`sqlite:${this.options.namespace}:${a}`,content:c,metadata:{...this.options.metadata,source:mr.basename(this.dbPath),type:"sqlite_query_result",query:n,startIndex:a,endIndex:Math.min(a+this.options.chunkSize,o.length),totalRows:o.length}}}}finally{t.exec("VACUUM;")}}async loadCSV(e,t){let s=await(await import("fs")).promises.readFile(t.csvPath,"utf-8"),i=t.delimiter??",",o=s.split(`
68
+ `).filter(y=>y.trim());if(o.length===0)return;let a,l;t.headers!==!1?(a=o[0].split(i).map(y=>y.trim().replace(/^["']|["']$/g,"")),l=1):(a=o[0].split(i).map((w,T)=>`col${T}`),l=0);let c=t.tableName.replace(/[^a-zA-Z0-9_]/g,"_"),h=a.map(y=>`"${y.replace(/"/g,'""')}" TEXT`).join(", ");e.exec(`DROP TABLE IF EXISTS "${c}";`),e.exec(`CREATE TABLE "${c}" (${h});`);let d=a.map(()=>"?").join(", "),m=e.prepare(`INSERT INTO "${c}" VALUES (${d})`);for(let y=l;y<o.length;y++){let w=o[y].split(i).map(T=>T.trim().replace(/^["']|["']$/g,""));m.run(w)}}};u();var rr=class{options;constructor(e){if(!e.query)throw new k("PostgresSource requires a query","config");if(!e.toContent)throw new k("PostgresSource requires a toContent callback. Example: toContent: (row) => `Name: ${row.name}`","config");this.options={namespace:e.namespace??"postgres",metadata:e.metadata??{},chunkSize:e.chunkSize??100,toContent:e.toContent,query:e.query,connectionString:e.connectionString,host:e.host,port:e.port,database:e.database,user:e.user,password:e.password,ssl:e.ssl}}async*load(){let e;try{e=(await Promise.resolve().then(()=>(hs(),ls))).Client}catch{throw new k('PostgreSQL source requires "pg" package. Install with: npm install pg',"config")}let t=this.options.connectionString?{connectionString:this.options.connectionString}:{host:this.options.host??"localhost",port:this.options.port??5432,database:this.options.database,user:this.options.user,password:this.options.password,ssl:this.options.ssl},n=new e(t);try{await n.connect();let o=(await n.query(this.options.query)).rows.map(a=>this.options.toContent(a));for(let a=0;a<o.length;a+=this.options.chunkSize){let c=o.slice(a,a+this.options.chunkSize).join(`
44
69
 
45
- `:"")+s}return r&&n.push(r.trim()),n}function Z(d,e){let t=d.match(/[^.!?]+[.!?]+/g)||[d],n=[],r="";for(let s of t){let o=y(s);y(r)+o>e&&r?(n.push(r.trim()),r=s):r+=(r?" ":"")+s}return r&&n.push(r.trim()),n}function S(d,e){if(d.length<=1||e===0)return d;let t=[];for(let n=0;n<d.length;n++){let r=d[n];if(n>0){let o=d[n-1].split(/\s+/),a=Math.ceil(e/4);r=o.slice(-a).join(" ")+" "+r}t.push(r)}return t}function O(d,e){if(y(d)<=e)return[d];let n=Y(d,e),r=[];for(let s of n)y(s)>e?r.push(...Z(s,e)):r.push(s);return r}var $=class{constructor(e,t={}){this.pattern=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"markdown",metadata:t.metadata??{}}}pattern;options;async*load(){let e=this.pattern.replace(/\\/g,"/"),t=await ee(e,{absolute:!0});for(let n of t)try{let r=await q.readFile(n,"utf-8"),s=this.chunkMarkdown(r,n);for(let o of s)yield o}catch(r){throw new b(`Failed to process file: ${r.message}`,n)}}chunkMarkdown(e,t){let n=this.extractFrontmatter(e),r=this.removeFrontmatter(e),s=this.parseHeadings(r),o=[],a=0;for(let m of s){let i=/```[\s\S]*?```/.test(m.content),u=y(m.content);if(u<this.options.minChunkSize&&o.length>0){let h=o[o.length-1];h.content+=`
70
+ ---
46
71
 
47
- `+m.content,i&&(h.metadata.hasCode=!0);continue}let c;u>this.options.maxChunkSize?c=O(m.content,this.options.maxChunkSize):c=[m.content],this.options.chunkOverlap>0&&c.length>1&&(c=S(c,this.options.chunkOverlap));for(let h=0;h<c.length;h++){let l=c[h],p=this.generateChunkId(t,l,a);o.push({id:p,content:l,metadata:{...this.options.metadata,...n,heading:m.heading,hasCode:i,source:R.basename(t),sourcePath:t,chunkIndex:a,totalChunks:c.length}}),a++}}return o}parseHeadings(e){let t=e.split(`
48
- `),n=[],r=[],s=[];for(let o of t){let a=o.match(/^(#{1,6})\s+(.+)$/);if(a){if(s.length>0){let u=r.map(c=>c.text);n.push({heading:u.length>0?[...u]:[""],content:s.join(`
49
- `).trim(),level:r.length>0?r[r.length-1].level:0}),s=[]}let m=a[1].length,i=a[2].trim();for(;r.length>0&&r[r.length-1].level>=m;)r.pop();r.push({level:m,text:i}),s.push(o)}else s.push(o)}if(s.length>0){let o=r.map(a=>a.text);n.push({heading:o.length>0?[...o]:[""],content:s.join(`
50
- `).trim(),level:r.length>0?r[r.length-1].level:0})}return n.filter(o=>o.content.length>0)}extractFrontmatter(e){let t=e.match(/^---\n([\s\S]*?)\n---/);if(!t)return{};let n=t[1],r={},s=n.split(`
51
- `);for(let o of s){let a=o.match(/^(\w+):\s*(.+)$/);if(a){let m=a[1],i=a[2].trim();i==="true"?i=!0:i==="false"?i=!1:isNaN(Number(i))?typeof i=="string"&&i.startsWith("[")&&i.endsWith("]")&&(i=i.slice(1,-1).split(",").map(u=>u.trim())):i=Number(i),r[m]=i}}return r}removeFrontmatter(e){return e.replace(/^---\n[\s\S]*?\n---\n/,"")}generateChunkId(e,t,n){let r=W.createHash("md5").update(t).digest("hex").substring(0,8),s=R.basename(e,R.extname(e));return`${this.options.namespace}:${s}:${n}:${r}`}};import*as L from"crypto";import*as V from"cheerio";var Q=class{constructor(e,t={}){this.urls=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"web",metadata:t.metadata??{},maxDepth:t.maxDepth??1,userAgent:t.userAgent??"Toolpack-Knowledge/1.0",delayMs:t.delayMs??1e3,timeoutMs:t.timeoutMs??3e4,sameDomainOnly:t.sameDomainOnly??!0,maxPagesPerDomain:t.maxPagesPerDomain??10}}urls;options;crawledUrls=new Set;domainPageCount=new Map;lastRequestTime=new Map;async*load(){let e=await this.crawlUrls(this.urls,0);for(let t of e)try{let n=this.chunkPage(t);for(let r of n)yield r}catch(n){throw new b(`Failed to process URL ${t.url}: ${n.message}`,t.url)}}async crawlUrls(e,t){if(t>=this.options.maxDepth)return[];let n=[],r=[],s=new Set(e.map(o=>new URL(o).hostname));for(let o of e){if(this.crawledUrls.has(o))continue;let a=new URL(o).hostname,m=this.domainPageCount.get(a)??0;if(!(this.options.sameDomainOnly&&!s.has(a))&&!(m>=this.options.maxPagesPerDomain)){this.crawledUrls.add(o),this.domainPageCount.set(a,m+1);try{let i=this.lastRequestTime.get(a)??0,u=Date.now()-i;u<this.options.delayMs&&await new Promise(h=>setTimeout(h,this.options.delayMs-u));let c=await this.fetchPage(o);n.push(c),this.lastRequestTime.set(a,Date.now()),t<this.options.maxDepth-1&&r.push(...c.links)}catch(i){console.warn(`Failed to crawl ${o}: ${i.message}`)}}}if(r.length>0){let o=await this.crawlUrls(r,t+1);n.push(...o)}return n}async fetchPage(e){let t=new AbortController,n=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let r=await fetch(e,{signal:t.signal,headers:{"User-Agent":this.options.userAgent}});if(!r.ok)throw new Error(`HTTP ${r.status}: ${r.statusText}`);let s=await r.text(),o=V.load(s);o("script, style, nav, header, footer, aside").remove();let a=o("title").text().trim()||o("h1").first().text().trim()||"Untitled",m=["main","article",".content","#content","body"],i="";for(let c of m){let h=o(c);if(h.length>0){i=h.text().trim();break}}i||(i=o("body").text().trim()),i=i.replace(/\s+/g," ").trim();let u=[];return o("a[href]").each((c,h)=>{let l=o(h).attr("href");if(l)try{let p=new URL(l,e).href;p.startsWith("http")&&!p.includes("#")&&u.push(p)}catch{}}),{url:e,title:a,content:i,links:[...new Set(u)]}}finally{clearTimeout(n)}}chunkPage(e){let t=[],n=y(e.content),r;n>this.options.maxChunkSize?r=O(e.content,this.options.maxChunkSize):r=[e.content],this.options.chunkOverlap>0&&r.length>1&&(r=S(r,this.options.chunkOverlap));for(let s=0;s<r.length;s++){let o=r[s],a=this.generateChunkId(e.url,o,s);t.push({id:a,content:o,metadata:{...this.options.metadata,title:e.title,url:e.url,source:"web",chunkIndex:s,totalChunks:r.length}})}return t}generateChunkId(e,t,n){let r=L.createHash("md5").update(t).digest("hex").substring(0,8),s=L.createHash("md5").update(e).digest("hex").substring(0,8);return`${this.options.namespace}:${s}:${n}:${r}`}};import*as U from"crypto";var F=class{constructor(e,t={}){this.url=e;this.options={maxChunkSize:t.maxChunkSize??2e3,chunkOverlap:t.chunkOverlap??200,minChunkSize:t.minChunkSize??100,namespace:t.namespace??"api",metadata:t.metadata??{},headers:t.headers??{},method:t.method??"GET",timeoutMs:t.timeoutMs??3e4,pagination:t.pagination,dataPath:t.dataPath??"",contentExtractor:t.contentExtractor??this.defaultContentExtractor,metadataExtractor:t.metadataExtractor??this.defaultMetadataExtractor}}url;options;async*load(){let e=await this.fetchData();for(let t of e)try{let n=this.chunkItem(t);for(let r of n)yield r}catch(n){throw new b(`Failed to process API item: ${n.message}`,this.url)}}async fetchData(){let e=[],t=this.options.pagination?.start??0,n=this.options.pagination?.maxPages??1;for(;t<n;){let r=this.buildUrl(t),s=await this.fetchPage(r);if(s.length===0||(e.push(...s),t++,!this.options.pagination))break}return e}buildUrl(e){if(!this.options.pagination)return this.url;let t=new URL(this.url);return t.searchParams.set(this.options.pagination.param,e.toString()),t.href}async fetchPage(e){let t=new AbortController,n=setTimeout(()=>t.abort(),this.options.timeoutMs);try{let r=await fetch(e,{method:this.options.method,headers:{"Content-Type":"application/json",...this.options.headers},body:this.options.body?JSON.stringify(this.options.body):void 0,signal:t.signal});if(!r.ok)throw new Error(`HTTP ${r.status}: ${r.statusText}`);let s=await r.json();return this.extractItems(s)}finally{clearTimeout(n)}}extractItems(e){if(!this.options.dataPath)return Array.isArray(e)?e:[e];let t=this.options.dataPath.split("."),n=e;for(let r of t)if(n&&typeof n=="object"&&r in n)n=n[r];else throw new Error(`Data path '${this.options.dataPath}' not found in response`);return Array.isArray(n)?n:[n]}chunkItem(e){let t=this.options.contentExtractor(e),n=this.options.metadataExtractor(e),r=y(t),s;r>(this.options.maxChunkSize??2e3)?s=O(t,this.options.maxChunkSize??2e3):s=[t],(this.options.chunkOverlap??200)>0&&s.length>1&&(s=S(s,this.options.chunkOverlap??200));let o=[];for(let a=0;a<s.length;a++){let m=s[a],i=this.generateChunkId(e,m,a);o.push({id:i,content:m,metadata:{...this.options.metadata,...n,source:"api",apiUrl:this.url,chunkIndex:a,totalChunks:s.length}})}return o}defaultContentExtractor(e){if(typeof e=="string")return e;if(typeof e=="object"&&e!==null){let t=["content","text","description","body","message"];for(let n of t)if(n in e&&typeof e[n]=="string")return e[n];return JSON.stringify(e)}return String(e)}defaultMetadataExtractor(e){if(typeof e=="object"&&e!==null){let t={},n=["id","title","name","created_at","updated_at","author","tags"];for(let r of n)r in e&&(t[r]=e[r]);return t}return{}}generateChunkId(e,t,n){let r=U.createHash("md5").update(t).digest("hex").substring(0,8),s=U.createHash("md5").update(JSON.stringify(e)).digest("hex").substring(0,8);return`${this.options.namespace}:${s}:${n}:${r}`}};var K=class{constructor(e){this.options=e;this.baseUrl=e.baseUrl||"http://localhost:11434",this.dimensions=e.dimensions||this.getModelDimensions(e.model)}options;dimensions;baseUrl;getModelDimensions(e){let t={"nomic-embed-text":768,"mxbai-embed-large":1024,"all-minilm":384,"snowflake-arctic-embed":1024,"bge-m3":1024,"bge-large":1024,"all-minilm:l6-v2":384,"all-minilm:l12-v2":384},n=t[e];if(!n)throw new k(`Unknown Ollama model '${e}'. Provide 'dimensions' in OllamaEmbedderOptions or use a known model: ${Object.keys(t).join(", ")}`);return n}async embed(e){let t=null,n=this.options.retries||3,r=this.options.retryDelay||1e3;for(let s=0;s<n;s++)try{let o=await fetch(`${this.baseUrl}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:this.options.model,prompt:e})});if(!o.ok)throw new k(`Ollama embedding failed: ${o.statusText}`,o.status);return(await o.json()).embedding}catch(o){if(t=o,o instanceof k&&o.statusCode&&o.statusCode>=400&&o.statusCode<500)throw o;s<n-1&&await new Promise(a=>setTimeout(a,r))}throw new k(`Ollama embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=[];for(let n of e)t.push(await this.embed(n));return t}};import te from"openai";var z=class{constructor(e){this.options=e;this.client=new te({apiKey:e.apiKey,timeout:e.timeout||3e4}),this.dimensions=this.getModelDimensions(e.model)}options;dimensions;client;getModelDimensions(e){return{"text-embedding-3-small":1536,"text-embedding-3-large":3072,"text-embedding-ada-002":1536}[e]||1536}async embed(e){let t=null,n=this.options.retries||3;for(let r=0;r<n;r++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data[0].embedding}catch(s){t=s,r<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new k(`OpenAI embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=null,n=this.options.retries||3;for(let r=0;r<n;r++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data.map(o=>o.embedding)}catch(s){t=s,r<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new k(`OpenAI batch embedding failed after ${n} retries: ${t?.message}`)}};export{F as ApiDataSource,_ as ChunkTooLargeError,C as DimensionMismatchError,k as EmbeddingError,b as IngestionError,H as Knowledge,v as KnowledgeError,E as KnowledgeProviderError,$ as MarkdownSource,I as MemoryProvider,K as OllamaEmbedder,z as OpenAIEmbedder,N as PersistentKnowledgeProvider,Q as WebUrlSource,M as combineScores,x as keywordSearch};
72
+ `);yield{id:`postgres:${this.options.namespace}:${a}`,content:c,metadata:{...this.options.metadata,type:"postgres_query_result",query:this.options.query,startIndex:a,endIndex:Math.min(a+this.options.chunkSize,o.length),totalRows:o.length}}}}finally{await n.end()}}};u();var nr=class{constructor(e){this.options=e;this.baseUrl=e.baseUrl||"http://localhost:11434",this.dimensions=e.dimensions||this.getModelDimensions(e.model)}options;dimensions;baseUrl;getModelDimensions(e){let t={"nomic-embed-text":768,"mxbai-embed-large":1024,"all-minilm":384,"snowflake-arctic-embed":1024,"bge-m3":1024,"bge-large":1024,"all-minilm:l6-v2":384,"all-minilm:l12-v2":384},n=t[e];if(!n)throw new A(`Unknown Ollama model '${e}'. Provide 'dimensions' in OllamaEmbedderOptions or use a known model: ${Object.keys(t).join(", ")}`);return n}async embed(e){let t=null,n=this.options.retries||3,s=this.options.retryDelay||1e3;for(let i=0;i<n;i++)try{let o=await fetch(`${this.baseUrl}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:this.options.model,prompt:e})});if(!o.ok)throw new A(`Ollama embedding failed: ${o.statusText}`,o.status);return(await o.json()).embedding}catch(o){if(t=o,o instanceof A&&o.statusCode&&o.statusCode>=400&&o.statusCode<500)throw o;i<n-1&&await new Promise(a=>setTimeout(a,s))}throw new A(`Ollama embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=[];for(let n of e)t.push(await this.embed(n));return t}};u();import hc from"openai";var sr=class{constructor(e){this.options=e;this.client=new hc({apiKey:e.apiKey,timeout:e.timeout||3e4}),this.dimensions=this.getModelDimensions(e.model)}options;dimensions;client;getModelDimensions(e){return{"text-embedding-3-small":1536,"text-embedding-3-large":3072,"text-embedding-ada-002":1536}[e]||1536}async embed(e){let t=null,n=this.options.retries||3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data[0].embedding}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new A(`OpenAI embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=null,n=this.options.retries||3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data.map(o=>o.embedding)}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay||1e3))}throw new A(`OpenAI batch embedding failed after ${n} retries: ${t?.message}`)}};u();import dc from"openai";var fc="https://openrouter.ai/api/v1",ir=class{constructor(e){this.options=e;this.client=new dc({apiKey:e.apiKey,baseURL:fc,timeout:e.timeout||3e4}),this.dimensions=e.dimensions??this.getModelDimensions(e.model)}options;dimensions;client;getModelDimensions(e){let t={"nvidia/llama-nemotron-embed-vl-1b-v2":4096,"nvidia/llama-nemotron-embed-vl-1b-v2:free":4096,"openai/text-embedding-3-small":1536,"openai/text-embedding-3-large":3072,"openai/text-embedding-ada-002":1536},n=t[e];if(n===void 0)throw new A(`Unknown OpenRouter embedding model '${e}'. Pass 'dimensions' in OpenRouterEmbedderOptions or use a known model: ${Object.keys(t).join(", ")}`);return n}async embed(e){let t=null,n=this.options.retries??3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data[0].embedding}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay??1e3))}throw new A(`OpenRouter embedding failed after ${n} retries: ${t?.message}`)}async embedBatch(e){let t=null,n=this.options.retries??3;for(let s=0;s<n;s++)try{return(await this.client.embeddings.create({model:this.options.model,input:e})).data.map(o=>o.embedding)}catch(i){t=i,s<n-1&&await new Promise(o=>setTimeout(o,this.options.retryDelay??1e3))}throw new A(`OpenRouter batch embedding failed after ${n} retries: ${t?.message}`)}};export{Ge as ApiDataSource,ar as ChunkTooLargeError,G as DimensionMismatchError,A as EmbeddingError,k as IngestionError,We as JSONSource,cr as Knowledge,U as KnowledgeError,j as KnowledgeProviderError,$e as MarkdownSource,Ue as MemoryProvider,nr as OllamaEmbedder,sr as OpenAIEmbedder,ir as OpenRouterEmbedder,je as PersistentKnowledgeProvider,rr as PostgresSource,Ye as SQLiteSource,ze as WebUrlSource,Fe as combineScores,$ as keywordSearch};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolpack-sdk/knowledge",
3
- "version": "1.4.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "description": "RAG (Retrieval-Augmented Generation) package for Toolpack SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -15,6 +15,10 @@
15
15
  "dist",
16
16
  "README.md"
17
17
  ],
18
+ "publishConfig": {
19
+ "tag": "alpha",
20
+ "access": "public"
21
+ },
18
22
  "scripts": {
19
23
  "build": "tsup",
20
24
  "build:dev": "tsup",