wiki-graph 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1249 -1243
- package/dist/cli.js.map +1 -1
- package/dist/data/help/commands/library-predicate.jinja +1 -1
- package/dist/data/help/commands/maintenance/chapter/add.jinja +2 -2
- package/dist/data/help/commands/maintenance/chapter/set-source.jinja +3 -20
- package/dist/data/help/commands/maintenance/chapter.jinja +3 -2
- package/dist/data/help/commands/maintenance/upgrade.jinja +2 -2
- package/dist/data/help/commands/maintenance.jinja +1 -1
- package/dist/data/help/commands/predicate.jinja +35 -21
- package/dist/data/help/commands/root.jinja +3 -1
- package/dist/data/help/commands/shared/chapter-source-set.jinja +21 -0
- package/dist/data/help/commands/uri.jinja +43 -4
- package/dist/data/help/topics/file-import.jinja +49 -0
- package/dist/data/help/topics/format.jinja +6 -4
- package/dist/data/help/topics/readiness.jinja +3 -1
- package/dist/data/help/topics/recipe.jinja +10 -10
- package/dist/data/help/topics/runtime.jinja +1 -1
- package/dist/data/help/topics/source-locators.jinja +45 -0
- package/dist/data/help/topics/uri.jinja +9 -1
- package/dist/gc-worker.js +482 -252
- package/dist/gc-worker.js.map +1 -1
- package/dist/index.cjs +1905 -622
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1659 -391
- package/dist/index.js.map +1 -1
- package/dist/queue-worker.js +1639 -646
- package/dist/queue-worker.js.map +1 -1
- package/package.json +9 -8
- package/LICENSE +0 -201
package/dist/gc-worker.js
CHANGED
|
@@ -3,66 +3,398 @@ import { fileURLToPath as __WIKIGRAPH_FILE_URL_TO_PATH__ } from "url";
|
|
|
3
3
|
import { resolve as __WIKIGRAPH_RESOLVE__ } from "path";
|
|
4
4
|
globalThis.__WIKIGRAPH_CLI_DIST_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL(".", import.meta.url)));
|
|
5
5
|
globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL("./data", import.meta.url)));
|
|
6
|
-
var i=(e,t)=>()=>(e&&(t=e(e=0)),t);import{AsyncLocalStorage as cd}from"async_hooks";import{homedir as my}from"os";import{join as Be,resolve as dd}from"path";function he(){let e=py.getStore()?.path;if(e!==void 0&&e.trim()!=="")return dd(e);let t=ud.getStore()?.path;return t!==void 0&&t.trim()!==""?dd(t):Be(my(),".wikigraph")}async function Fi(e,t){return await ud.run({path:e},t)}function G(){return Be(he(),"core.sqlite")}function Ue(){return Be(he(),"cache")}function Gt(){return Be(Ue(),"cache.sqlite")}function pt(){return Be(he(),"jobs")}function $(){return Be(he(),"staging")}function ht(){return Be(he(),"tmp")}var py,ud,D=i(()=>{"use strict";py=new cd,ud=new cd});import{mkdir as uR,mkdtemp as lR}from"fs/promises";import{join as hy}from"path";function ft(){return ht()}function ld(e){let t=ft();return e===void 0?t:hy(t,e)}var fe=i(()=>{"use strict";D()});var fR,Bt=i(()=>{"use strict";fR=new RegExp("\\p{Script=Han}","u")});var Wi=i(()=>{"use strict"});import{AsyncLocalStorage as fy}from"async_hooks";import{stat as yy}from"fs/promises";import{resolve as gy}from"path";async function Sy(e){let t=await yy(e).catch(n=>{if(!(typeof n=="object"&&n!==null&&"code"in n&&n.code==="ENOENT"))throw n});return t===void 0||t.size===0}function g(e,t){let n=e[t];if(typeof n!="number")throw new TypeError(`Expected ${t} to be a number`);return n}function E(e,t){let n=e[t];if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}async function wy(e,t={}){let n=await Ey(),r=(t.readonly===!0?n.OPEN_READONLY:n.OPEN_READWRITE|n.OPEN_CREATE)|n.OPEN_FULLMUTEX;return await new Promise((a,o)=>{let s=new n.Database(e,r,d=>{if(d!==null){o(d);return}a(s)})})}async function Ey(){let e=await import("sqlite3");return Iy(e)}function Iy(e){if(typeof e=="object"&&e!==null&&"default"in e&&typeof e.default=="object"&&e.default!==null&&"Database"in e.default)return e.default;if(typeof e=="object"&&e!==null&&"Database"in e)return e;throw new TypeError("Could not load sqlite3")}function ji(e){return e??[]}var by,k,f=i(()=>{"use strict";by=900*1e3;k=class e{#e;#t;#n=new fy;#r;#i=!1;#c=Promise.resolve();#d=0;constructor(t,n={}){this.#e=t,this.#t=n.onWrite}static async open(t,n="",r={}){let a=gy(t),o=r.readonly!==!0&&n.trim()!==""&&await Sy(a),s=await wy(a,r),d=new e(s,r);return await d.#o(`PRAGMA busy_timeout = ${by}`),r.readonly!==!0&&n.trim()!==""&&(await d.#o(n),o&&d.#u()),d}static async initialize(t,n){let r=await e.open(t);try{n.trim()!==""&&await r.#o(n)}finally{await r.close()}}async queryAll(t,n,r){return await this.#a(async()=>(this.#s(),(await this.#m(t,n)).map(r)))}async queryOne(t,n,r){return await this.#a(async()=>{this.#s();let a=await this.#p(t,n);return a===void 0?void 0:r(a)})}async run(t,n){await this.#a(async()=>{this.#s(),await this.#h(t,n),this.#u()})}async transaction(t){return await this.#a(async()=>{this.#s();let n=this.#d===0,r=this.#r??Symbol("database transaction scope");n&&(await this.#o("BEGIN IMMEDIATE"),this.#r=r),this.#d+=1;try{let a=await this.#n.run(r,t);return n&&await this.#o("COMMIT"),a}catch(a){throw n&&await this.#o("ROLLBACK"),a}finally{this.#d-=1,n&&(this.#r=void 0)}})}async flush(){await this.#a(()=>{this.#s()})}async close(){await this.#a(async()=>{this.#i||(await this.#l(),this.#i=!0)})}async getLastInsertRowId(){let t=await this.queryOne("SELECT last_insert_rowid() AS row_id",void 0,n=>g(n,"row_id"));if(t===void 0)throw new Error("Could not read last_insert_rowid()");return t}#s(){if(this.#i)throw new Error("Database is already closed")}async#a(t){let n=this.#n.getStore();if(n!==void 0&&n===this.#r)return await t();let r=this.#c.then(t);return this.#c=r.then(()=>{},()=>{}),await r}#u(){this.#t?.()}async#l(){await new Promise((t,n)=>{this.#e.close(r=>{if(r!==null){n(r);return}t()})})}async#o(t){await new Promise((n,r)=>{this.#e.exec(t,a=>{if(a!==null){r(a);return}n()})})}async#m(t,n){return await new Promise((r,a)=>{this.#e.all(t,ji(n),(o,s)=>{if(o!==null){a(o);return}r(s)})})}async#p(t,n){return await new Promise((r,a)=>{this.#e.get(t,ji(n),(o,s)=>{if(o!==null){a(o);return}r(s)})})}async#h(t,n){await new Promise((r,a)=>{this.#e.run(t,ji(n),o=>{if(o!==null){a(o);return}r()})})}}});function h(e){return e instanceof Error}function He(e){let t=[],n=new Set,r=e;for(;r!==void 0&&!n.has(r);){if(n.add(r),r instanceof Error){md(t,xy(r)),r=r.cause;continue}md(t,String(r));break}return t.join(": ")}function xy(e){let t=e,n=e.message.trim(),r=typeof t.code=="string"&&t.code!==""?t.code:void 0,a=typeof t.path=="string"&&t.path!==""?t.path:void 0;return r==="ENOENT"?a===void 0?"File not found (ENOENT)":`File not found: ${a} (ENOENT)`:r==="EACCES"||r==="EPERM"?a===void 0?`Permission denied (${r})`:`Permission denied: ${a} (${r})`:n===""?r===void 0?e.name:`${e.name} (${r})`:r===void 0?n:`${n} (${r})`}function md(e,t){let n=t.trim();if(n==="")return;let r=e.at(-1);if(r===void 0){e.push(n);return}if(!(r===n||r.includes(n))){if(n.includes(r)){e[e.length-1]=n;return}e.push(n)}}var b=i(()=>{"use strict"});import{mkdir as ky,rm as Ry,stat as fd}from"fs/promises";import{dirname as vy,join as L,resolve as Bi}from"path";async function K(){let e=G(),t=Bi(e),n=await or(e);if(Gi(t,n))return;if(qe!==void 0){await qe;let a=await or(e);if(Gi(t,a))return}let r=await or(e);if(qe!==void 0)return await qe,K();if(!Gi(t,r)){qe=(async()=>{if(!await ye(e)){await hd(e,ar),await pd(t);return}let a=await cr(e);if(a>ar)throw new Error(`Unsupported Wiki Graph home schema version: ${a}.`);a<ar&&(await Ly(),await Ay(),await My(),await hd(e,ar)),await pd(t)})();try{await qe;return}finally{qe=void 0}}}async function pd(e){let t=await or(e);if(t===void 0){Ut=void 0;return}Ut={fingerprint:t,path:e}}async function or(e){try{let t=await fd(e);return{dev:t.dev,ino:t.ino,mtimeMs:t.mtimeMs,size:t.size}}catch(t){if(h(t)&&t.code==="ENOENT")return;throw t}}function Gi(e,t){return t===void 0||Ut===void 0?!1:Ut.path===e&&Cy(Ut.fingerprint,t)}function Cy(e,t){return e.dev===t.dev&&e.ino===t.ino&&e.mtimeMs===t.mtimeMs&&e.size===t.size}async function cr(e=G()){if(!await ye(e))return 0;let t=await k.open(e,"",{readonly:!0});try{return await ne(t,"schema_versions")?await t.queryOne("SELECT version FROM schema_versions WHERE scope = ?",["home"],r=>Number(r.version))??1:1}finally{await t.close()}}function yd(e){let t=Bi(e);return Py().some(n=>t===Bi(n))}function Py(){let e=Ue();return[G(),L(e,"search-sessions.sqlite"),L(e,"continuation-cursors.sqlite"),Gt(),L(pt(),"job.sqlite"),L(ht(),"gc.sqlite"),L($(),"staging.sqlite")]}async function hd(e,t){await ky(vy(e),{recursive:!0});let n=await k.open(e);try{await n.run(Ty),await n.run(`
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
SELECT owner_pid, heartbeat_at
|
|
17
|
-
FROM state_locks
|
|
18
|
-
WHERE scope = 'library'
|
|
19
|
-
`,void 0,r=>({heartbeatAt:Number(r.heartbeat_at),ownerPid:Number(r.owner_pid)}))).some(r=>Ht(r.ownerPid,r.heartbeatAt)))throw new Error("Cannot upgrade home with active library locks.")}finally{await t.close()}}async function Ny(){let e=L(ht(),"gc.sqlite");if(!await ye(e))return;let t=await k.open(e,"",{readonly:!0});try{if(!await ne(t,"gc_locks"))return;if((await t.queryAll("SELECT owner_pid, heartbeat_at FROM gc_locks",void 0,r=>({heartbeatAt:Number(r.heartbeat_at),ownerPid:Number(r.owner_pid)}))).some(r=>Ht(r.ownerPid,r.heartbeatAt)))throw new Error("Cannot upgrade home with an active GC lock.")}finally{await t.close()}}async function Dy(){let e=L($(),"staging.sqlite");if(!await ye(e))return;let t=await k.open(e,"",{readonly:!0});try{for(let n of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){if(!await ne(t,n))continue;if((await t.queryAll(`
|
|
20
|
-
SELECT owner_pid, heartbeat_at
|
|
21
|
-
FROM ${n}
|
|
22
|
-
`,void 0,a=>({heartbeatAt:Number(a.heartbeat_at),ownerPid:Number(a.owner_pid)}))).some(a=>Ht(a.ownerPid,a.heartbeatAt)))throw new Error(`Cannot upgrade home with active coordinator state: ${n}.`)}}finally{await t.close()}}async function My(){let e=L($(),"staging.sqlite");if(!await ye(e))return;let t=await k.open(e,"",{readonly:!0});try{if(!await ne(t,"entry_overlays"))return;if((await t.queryAll(`
|
|
23
|
-
SELECT entry_path
|
|
24
|
-
FROM entry_overlays
|
|
25
|
-
`,void 0,a=>String(a.entry_path))).find(a=>!Gy(a))!==void 0)throw new Error("Cannot upgrade home with non-derived coordinator overlays.")}finally{await t.close()}}async function Fy(){let e=L(pt(),"job.sqlite");if(!await ye(e))return;let t=await k.open(e,"",{readonly:!0});try{if(await ne(t,"build_jobs")&&(await t.queryAll(`
|
|
26
|
-
SELECT job_id
|
|
27
|
-
FROM build_jobs
|
|
28
|
-
WHERE state IN ('queued', 'running', 'canceling', 'paused')
|
|
29
|
-
`,void 0,r=>String(r.job_id))).length>0)throw new Error("Cannot upgrade home with active build jobs.");if(await ne(t,"build_worker_lease")){let n=await t.queryOne(`
|
|
30
|
-
SELECT owner_pid, heartbeat_at
|
|
31
|
-
FROM build_worker_lease
|
|
32
|
-
WHERE id = 1
|
|
33
|
-
`,void 0,r=>({heartbeatAt:r.heartbeat_at===null?void 0:Number(r.heartbeat_at),ownerPid:r.owner_pid===null?void 0:Number(r.owner_pid)}));if(n?.ownerPid!==void 0&&n.heartbeatAt!==void 0&&Ht(n.ownerPid,n.heartbeatAt))throw new Error("Cannot upgrade home with an active build worker lease.")}}finally{await t.close()}}async function Wy(e,t){let n=await k.open(e);try{if(!await ne(n,"entry_overlays"))return;let r=t===void 0?"WHERE entry_path IN (?, ?)":"WHERE archive_key = ? AND entry_path IN (?, ?)",a=t===void 0?[sr,dr]:[t,sr,dr],o=await n.queryAll(`
|
|
34
|
-
SELECT archive_key, workspace_path
|
|
35
|
-
FROM entry_overlays
|
|
36
|
-
${r}
|
|
37
|
-
`,a,s=>({archiveKey:String(s.archive_key),workspacePath:s.workspace_path===null?void 0:String(s.workspace_path)}));for(let s of o)s.workspacePath!==void 0&&await te(s.workspacePath);await n.run(`
|
|
38
|
-
DELETE FROM entry_overlays
|
|
39
|
-
${r}
|
|
40
|
-
`,a)}finally{await n.close()}}async function jy(e){let t=await k.open(e);try{if(!await ne(t,"entry_overlays"))return;let n=await t.queryAll(`
|
|
41
|
-
SELECT archive_key, archive_path, entry_path, workspace_path
|
|
42
|
-
FROM entry_overlays
|
|
43
|
-
WHERE entry_path IN (?, ?, ?)
|
|
44
|
-
`,["database.db",sr,dr],r=>({archiveKey:String(r.archive_key),archivePath:String(r.archive_path),entryPath:String(r.entry_path),workspacePath:r.workspace_path===null?void 0:String(r.workspace_path)}));for(let r of n)await ye(r.archivePath)||(r.workspacePath!==void 0&&await te(r.workspacePath),await t.run(`
|
|
45
|
-
DELETE FROM entry_overlays
|
|
46
|
-
WHERE archive_key = ? AND entry_path = ?
|
|
47
|
-
`,[r.archiveKey,r.entryPath]))}finally{await t.close()}}function Gy(e){return e===sr||e===dr}async function ne(e,t){return await e.queryOne(`
|
|
48
|
-
SELECT 1 AS present
|
|
49
|
-
FROM sqlite_master
|
|
50
|
-
WHERE type = 'table' AND name = ?
|
|
51
|
-
`,[t],()=>!0)===!0}function Ht(e,t){return Date.now()-t<=_y&&By(e)}function By(e){try{return process.kill(e,0),!0}catch{return!1}}async function te(e){await Ry(e,{force:!0,recursive:!0})}async function ye(e){try{return await fd(e),!0}catch{return!1}}var ar,_y,sr,dr,qe,Ut,Ty,$e=i(()=>{"use strict";D();b();f();ar=3,_y=300*1e3,sr="index.db",dr="fts.db",Ty=`
|
|
6
|
+
var _y=Object.defineProperty;var i=(e,t)=>()=>(e&&(t=e(e=0)),t);var Ty=(e,t)=>{for(var n in t)_y(e,n,{get:t[n],enumerable:!0})};async function Q(e){let t=await e.read({encoding:"utf8"});return typeof t=="string"?t:new TextDecoder().decode(t)}async function V(e,t){let n=await e.openWriter();try{await n.write(t),await n.commit()}catch(r){throw await n.abort().catch(()=>{}),r}}async function pe(e){if(G(e)){let t=0;for(let n of await e.list())t+=await pe(n);return t}return e.getSize===void 0?e.size??0:await e.getSize()}async function Le(e){return e.getLastModified===void 0?e.lastModified:await e.getLastModified()}function G(e){return"list"in e}var ic=i(()=>{"use strict"});function Ci(e){Ht=e}function L(){if(Ht===void 0)throw new Error("No WikiGraph runtime platform has been installed. Provide a runtime adapter before using wiki-graph-core.");return Ht}async function he(e){if(typeof e!="string")return e;let t=await L().resources.getFile(e);if(t===void 0)throw new Error("Host file is unavailable");return t}async function K(e,t){let n=t.replaceAll("\\","/").split("/").filter(Boolean);if(n.some(o=>o==="."||o===".."))throw new TypeError(`Directory path must remain relative: ${t}`);let r=n.pop();if(!r)return;let a=e;for(let o of n){let s=await a.getDirectory(o);if(!s)return;a=s}return await a.getFile(r)}async function q(e,t){let n=ac(t),r=e;for(let a of n){let o=await r.getDirectory(a);if(o!==void 0){r=o;continue}try{r=await r.createDirectory(a)}catch(s){let c=await r.getDirectory(a);if(c===void 0)throw s;r=c}}return r}async function j(e,t){let n=ac(t),r=n.pop();if(!r)throw new TypeError("File name must remain relative");let a=await q(e,n.join("/")),o=await a.getFile(r);if(o!==void 0)return o;try{return await a.createFile(r)}catch(s){let c=await a.getFile(r);if(c!==void 0)return c;throw s}}function ac(e){let t=e.replaceAll("\\","/");if(t.startsWith("/"))throw new TypeError(`Directory path must remain relative: ${e}`);let n=t.split("/").filter(Boolean);if(n.some(r=>r==="."||r===".."))throw new TypeError(`Directory path must remain relative: ${e}`);return n}function Ai(e){sc=e}async function Pi(e,t){return e===void 0?await t():await oc.run(e,t)}function I(){let e=oc.getStore()??sc;if(e===void 0)throw new Error("No WikiGraph storage roots have been configured. Pass storage to WikiGraph or install process-default storage.");return e}var Ht,Ti,oc,sc,p=i(()=>{"use strict";ic();Ti=class{#e;#t;run(t,n){let r=this.#n();if(r!==void 0)return r.run(t,n);let a=this.#t;this.#t=t;try{return n()}finally{this.#t=a}}enterWith(t){let n=this.#n();n!==void 0?n.enterWith(t):this.#t=t}getStore(){return this.#n()?.getStore()??this.#t}#n(){if(this.#e!==void 0)return this.#e;if(Ht!==void 0)return this.#e=Ht.asyncContext.create(),this.#t!==void 0&&(this.#e.enterWith(this.#t),this.#t=void 0),this.#e}},oc=new Ti});function cc(e){if(Ay.test(e))throw new Error(`invalid template name ${e}`);let n=e.replace(Py,"").replace(Cy,"");if(n.split("/").includes(".."))throw new Error(`invalid template name ${e}`);return`${n}.jinja`}var Cy,Ay,Py,$t=i(()=>{"use strict";p();Cy=/\.jinja$/iu,Ay=/^\.+\//u,Py=/^\/+/u});var Re=i(()=>{"use strict"});function Ic(e){return new TextEncoder().encode(e)}function Qe(e){let t="";for(let n of e)t+=n.toString(16).padStart(2,"0");return t}var fe=i(()=>{"use strict"});import{sha1 as Vy}from"@noble/hashes/legacy.js";import{sha256 as Yy,sha512 as Zy}from"@noble/hashes/sha2.js";function X(e){let t=eg(e);return{update(n){return t.update(typeof n=="string"?Ic(n):n),this},digest(n){let r=t.digest();return n==="hex"?Qe(r):r}}}function zt(e){let t=new Uint8Array(e);return globalThis.crypto.getRandomValues(t),t}function Ne(){if(typeof globalThis.crypto.randomUUID=="function")return globalThis.crypto.randomUUID();let e=zt(16);e[6]=e[6]&15|64,e[8]=e[8]&63|128;let t=Qe(e);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`}function eg(e){switch(e){case"sha1":return Vy.create();case"sha256":return Yy.create();case"sha512":return Zy.create()}}var O=i(()=>{"use strict";fe()});var mr=i(()=>{"use strict";O()});var Rc=i(()=>{"use strict";p()});var vc=i(()=>{"use strict";Rc()});var _c=i(()=>{"use strict"});function Ve(e){return e instanceof Error}function Ye(e){let t=[],n=new Set,r=e;for(;r!==void 0&&!n.has(r);){if(n.add(r),r instanceof Error){Tc(t,og(r)),r=r.cause;continue}Tc(t,String(r));break}return t.join(": ")}function og(e){let t=e,n=e.message.trim(),r=typeof t.code=="string"&&t.code!==""?t.code:void 0;return r==="ENOENT"?"File not found (ENOENT)":r==="EACCES"||r==="EPERM"?`Permission denied (${r})`:r!==void 0&&sg(n)?`${e.name} (${r})`:n===""?e.name:r===void 0?n:`${n} (${r})`}function sg(e){return/(?:^|[\s:'"(=])\/(?:[^\s/]+\/)*[^\s/]+/u.test(e)||/(?:^|[\s:'"(=])[A-Za-z]:[\\/]/u.test(e)||/\bfile:\/\//iu.test(e)}function Tc(e,t){let n=t.trim();if(n==="")return;let r=e.at(-1);if(r===void 0){e.push(n);return}if(!(r===n||r.includes(n))){if(n.includes(r)){e[e.length-1]=n;return}e.push(n)}}var ee=i(()=>{"use strict";p()});var Cc=i(()=>{"use strict";ee()});var Ac=i(()=>{"use strict"});import{APICallError as iR}from"ai";var Pc=i(()=>{"use strict";p()});var ye=i(()=>{"use strict";p()});var Ze,Qt=i(()=>{"use strict";Ze=class{#e=0;#t;#n=[];constructor(t){if(!Number.isInteger(t)||t<1)throw new RangeError("concurrency must be a positive integer");this.#t=t}async use(t){await this.#r();try{return await t()}finally{this.#i()}}async#r(){if(this.#e<this.#t){this.#e+=1;return}await new Promise(t=>{this.#n.push(()=>{this.#e+=1,t()})})}#i(){this.#e-=1,this.#n.shift()?.()}}});var Wi=i(()=>{"use strict";ye()});var ji=i(()=>{"use strict"});var Lc=i(()=>{"use strict";p()});var Gi=i(()=>{"use strict"});import{generateText as PR,streamText as LR}from"ai";var Dc=i(()=>{"use strict";vc();_c();Cc();Ac();Pc();ye();$t();Qt();mr();ee();Wi();ji();Lc();Gi()});var Fc=i(()=>{"use strict";Dc()});var Ui=i(()=>{"use strict";Fc();Wi();ji();Gi()});var lg,Hi,Mc,Yt=i(()=>{"use strict";lg=Object.freeze(["serial-generation/editor-compress","serial-generation/editor-review","serial-generation/editor-review-guide","serial-generation/reader-choice","serial-generation/reader-extraction"]),Hi=Object.freeze({compress:"serial-generation/editor-compress",review:"serial-generation/editor-review",reviewGuide:"serial-generation/editor-review-guide"}),Mc=Object.freeze({choice:"serial-generation/reader-choice",extraction:"serial-generation/reader-extraction"})});var qi=i(()=>{"use strict"});async function pg(e){if(e.size!==void 0)return e.size===0;if(e.getSize!==void 0)return await e.getSize()===0;let t=await e.read();return typeof t=="string"?t.length===0:t.byteLength===0}function h(e,t){let n=e[t];if(typeof n!="number")throw new TypeError(`Expected ${t} to be a number`);return n}function S(e,t){let n=e[t];if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}var mg,N,$i,y=i(()=>{"use strict";p();mg=900*1e3;N=class e{#e;#t;#n;#r;#i=!1;#u=Promise.resolve();#c=0;constructor(t,n={}){this.#e=t,this.#t=n.onWrite,this.#n=L().asyncContext.create()}static async open(t,n="",r={}){let a=await he(t),o=r.readonly!==!0&&n.trim()!==""&&await pg(a),s=new $i(await L().database.open(a,r)),c=new e(s,r);return await c.#o(`PRAGMA busy_timeout = ${mg}`),r.readonly!==!0&&n.trim()!==""&&(await c.#o(n),o&&c.#d()),c}static async initialize(t,n){let r=await e.open(t);try{n.trim()!==""&&await r.#o(n)}finally{await r.close()}}async queryAll(t,n,r){return await this.#a(async()=>(this.#s(),(await this.#m(t,n)).map(r)))}async queryOne(t,n,r){return await this.#a(async()=>{this.#s();let a=await this.#p(t,n);return a===void 0?void 0:r(a)})}async run(t,n){await this.#a(async()=>{this.#s(),await this.#h(t,n),this.#d()})}async execute(t){await this.#a(async()=>{this.#s(),await this.#o(t),this.#d()})}async transaction(t){return await this.#a(async()=>{this.#s();let n=this.#c===0,r=this.#r??Symbol("database transaction scope");n&&(await this.#o("BEGIN IMMEDIATE"),this.#r=r),this.#c+=1;try{let a=await this.#n.run(r,t);return n&&await this.#o("COMMIT"),a}catch(a){throw n&&await this.#o("ROLLBACK"),a}finally{this.#c-=1,n&&(this.#r=void 0)}})}async flush(){await this.#a(()=>{this.#s()})}async close(){await this.#a(async()=>{this.#i||(await this.#l(),this.#i=!0)})}async getLastInsertRowId(){let t=await this.queryOne("SELECT last_insert_rowid() AS row_id",void 0,n=>h(n,"row_id"));if(t===void 0)throw new Error("Could not read last_insert_rowid()");return t}#s(){if(this.#i)throw new Error("Database is already closed")}async#a(t){let n=this.#n.getStore();if(n!==void 0&&n===this.#r)return await t();let r=this.#u.then(t);return this.#u=r.then(()=>{},()=>{}),await r}#d(){this.#t?.()}async#l(){await this.#e.close()}async#o(t){await this.#e.execute(t)}async#m(t,n){return await this.#e.queryAll(t,n)}async#p(t,n){return await this.#e.queryOne(t,n)}async#h(t,n){await this.#e.run(t,n)}};$i=class{#e;constructor(t){this.#e=t}async close(){await this.#e.close()}async execute(t){await this.#e.execute(t)}async queryAll(t,n){return[...await this.#e.queryAll(t,n)]}async queryOne(t,n){return await this.#e.queryOne(t,n)}async run(t,n){await this.#e.run(t,n)}}});async function It(){let e=I().library,t=pr.get(e.identity);if(t!==void 0){await t;return}let n=yg(e);pr.set(e.identity,n);try{await n}finally{pr.get(e.identity)===n&&pr.delete(e.identity)}}async function br(){let e=await K(I().library,"core.sqlite");if(e===void 0||await Sr(e))return 0;let t=await N.open(e,"",{readonly:!0});try{return await xt(t,"schema_versions")?await t.queryOne("SELECT version FROM schema_versions WHERE scope = ?",["home"],n=>h(n,"version"))??1:1}finally{await t.close()}}async function yg(e){let t=await br();if(t>Ki)throw new Error(`Unsupported Wiki Graph home schema version: ${t}.`);let n=await j(e,"core.sqlite");if(t===0){await hr(n,Ki);return}for(;t<Ki;)switch(t){case 1:await Wc(e),await hr(n,2),t=2;break;case 2:await Wc(e),await hr(n,3),t=3;break;case 3:await gg(n),await hr(n,4),t=4;break;default:throw new Error(`Unsupported Wiki Graph home schema version: ${t}.`)}}async function Wc(e){await Hc(e),await Uc(e)}async function gg(e){let t=I().library;await Hc(t),await bg(e),await Uc(t)}async function bg(e){let t=await N.open(e);try{if(!await xt(t,"libraries"))return;let n=await yr(t,"libraries"),r=n.has("folder_identity")?"folder_identity":n.has("folder_path")?"folder_path":void 0;if(r===void 0)throw new Error("Cannot migrate library registry without a folder reference.");let a=await t.queryAll(`SELECT id, ${r} AS folder_reference FROM libraries ORDER BY id`,void 0,s=>({id:h(s,"id"),reference:S(s,"folder_reference")})),o=[];for(let s of a){let c=L().resources,d=r==="folder_identity"||c.resolveLegacyDirectory===void 0?await c.getDirectory(s.reference):await c.resolveLegacyDirectory(s.reference);if(d===void 0)throw new Error(`Cannot migrate unavailable library Directory for registry id ${s.id}.`);o.push({id:s.id,identity:d.identity})}await t.transaction(async()=>{n.has("folder_identity")||await t.run("ALTER TABLE libraries RENAME COLUMN folder_path TO folder_identity");for(let s of o)await t.run("UPDATE libraries SET folder_identity = ? WHERE id = ?",[s.identity,s.id])})}finally{await t.close()}}async function hr(e,t){let n=await N.open(e);try{await n.execute(fg),await n.run(`INSERT INTO schema_versions (scope, version, updated_at)
|
|
7
|
+
VALUES (?, ?, ?)
|
|
8
|
+
ON CONFLICT(scope) DO UPDATE SET
|
|
9
|
+
version = excluded.version,
|
|
10
|
+
updated_at = excluded.updated_at`,["home",t,new Date().toISOString()])}finally{await n.close()}}async function Uc(e){await Zt(await e.getDirectory("cache"),["search-sessions.sqlite","continuation-cursors.sqlite","cache.sqlite"]);let t=await e.getDirectory("staging");await Zt(t,["library","work"]),await fr(t,"staging.sqlite");let n=await e.getDirectory("tmp");await fr(n,"gc.sqlite"),await fr(n,"wikg-coordinator.sqlite"),await Zt(n,["gc.last-run"]);let r=await e.getDirectory("jobs");await Zt(r,["cache","work"]),await fr(r,"job.sqlite");let a=I().documentStore,o;try{o=await a.list()}catch(s){if(Ve(s)&&s.code==="ENOENT")return;throw s}for(let s of o)(s.name===".wikg-work"||s.name===".wikg-cache"||s.name.startsWith(".wikg-session-")||s.name.startsWith(".wikg-upgrade-"))&&await a.remove(s.name,{recursive:G(s)})}async function fr(e,t){await Zt(e,[t,`${t}-wal`,`${t}-shm`])}async function Zt(e,t){if(e===void 0)return;let n=new Map((await e.list()).map(r=>[r.name,r]));for(let r of t){let a=n.get(r);a!==void 0&&await e.remove(r,{recursive:G(a)})}}async function Hc(e){await Gc(await K(e,"core.sqlite"),[["library_locks","Cannot upgrade home with active library locks."],["state_locks","Cannot upgrade home with active state locks."]]),await Gc(await K(e,"tmp/gc.sqlite"),[["gc_locks","Cannot upgrade home with an active GC lock."]]),await Sg(await K(e,"jobs/job.sqlite")),await jc(await K(e,"staging/staging.sqlite"),"legacy coordinator"),await jc(await K(e,"tmp/wikg-coordinator.sqlite"),"coordinator")}async function Sg(e){if(e===void 0||await Sr(e))return;let t=await N.open(e,"",{readonly:!0});try{if(await xt(t,"build_worker_lease")){let n=await yr(t,"build_worker_lease"),a=["owner_id","owner_pid"].filter(s=>n.has(s)).map(s=>`${s} IS NOT NULL`).join(" OR ");if(await t.queryOne(`SELECT 1 AS active FROM build_worker_lease
|
|
11
|
+
WHERE heartbeat_at IS NOT NULL
|
|
12
|
+
AND heartbeat_at >= ?
|
|
13
|
+
${a===""?"":`AND (${a})`}
|
|
14
|
+
LIMIT 1`,[Date.now()-gr],()=>!0)===!0)throw en("Cannot upgrade home with an active build worker.")}}finally{await t.close()}}async function jc(e,t){if(e===void 0||await Sr(e))return;let n=await N.open(e,"",{readonly:!0});try{if(await xt(n,"archive_owners")){let r=await yr(n,"archive_owners"),a=await n.queryAll(`SELECT heartbeat_at${r.has("host_instance_id")?", host_instance_id":""}
|
|
15
|
+
FROM archive_owners`,void 0,o=>({heartbeatAt:h(o,"heartbeat_at"),hostInstanceId:typeof o.host_instance_id=="string"?o.host_instance_id:void 0}));for(let o of a){if(o.hostInstanceId===void 0){if(Bc(o.heartbeatAt))throw en(`Cannot upgrade home with active ${t} state.`);continue}let s=await L().lifecycle.isInstanceAlive(o.hostInstanceId);if(s===!0||s===void 0&&Bc(o.heartbeatAt))throw en(`Cannot upgrade home with active ${t} state.`)}}for(let r of["entry_locks","entry_sqlite_leases","archive_commit_locks"]){if(!await xt(n,r)||!(await yr(n,r)).has("heartbeat_at"))continue;if(await n.queryOne(`SELECT 1 AS active FROM ${r} WHERE heartbeat_at >= ? LIMIT 1`,[Date.now()-gr],()=>!0)===!0)throw en(`Cannot upgrade home with active ${t} state.`)}}finally{await n.close()}}async function Gc(e,t){if(e===void 0||await Sr(e))return;let n=await N.open(e,"",{readonly:!0});try{for(let[r,a]of t){if(!await xt(n,r))continue;if(await n.queryOne(`SELECT 1 AS active FROM ${r} WHERE heartbeat_at >= ? LIMIT 1`,[Date.now()-gr],()=>!0)===!0)throw en(a)}}finally{await n.close()}}function en(e){return new Error(`${e} ${hg}`)}function Bc(e){return e!==void 0&&Date.now()-e<=gr}async function Sr(e){if(e.getSize!==void 0)return await e.getSize()===0;if(e.size!==void 0)return e.size===0;let t=await e.read();return typeof t=="string"?t.length===0:t.byteLength===0}async function yr(e,t){return new Set(await e.queryAll(`PRAGMA table_info(${t})`,void 0,n=>S(n,"name")))}async function xt(e,t){return await e.queryOne("SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = ?",[t],()=>!0)===!0}var Ki,gr,hg,fg,pr,wr=i(()=>{"use strict";p();ee();y();Ki=4,gr=300*1e3,hg="Stop the active operation, then run `wg maintenance upgrade home`. See: `wg maintenance upgrade --help`.",fg=`
|
|
52
16
|
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
53
17
|
scope TEXT PRIMARY KEY,
|
|
54
18
|
version INTEGER NOT NULL,
|
|
55
19
|
updated_at TEXT NOT NULL
|
|
56
20
|
);
|
|
57
|
-
|
|
58
|
-
`,
|
|
59
|
-
|
|
60
|
-
|
|
21
|
+
`,pr=new Map});async function B(e,t,n={}){await It();let r=await j(I().library,e);return await Ji(r,t,n)}async function Ji(e,t,n={}){return await N.open(await he(e),t,n)}var qc=i(()=>{"use strict";p();wr();y()});function Er(...e){let t=[];for(let n of e)for(let r of n.replaceAll("\\","/").split("/"))if(!(r===""||r==="."))if(r===".."){if(t.length===0)throw new TypeError("Relative path escapes its root");t.pop()}else t.push(r);return t.join("/")}function zi(e){return Er(e).split("/").at(-1)??""}var et=i(()=>{"use strict"});var tn=i(()=>{"use strict";et()});var $c=i(()=>{"use strict"});function kt(e){let t=new Set(Object.values(e));return n=>t.has(n)}function nn(e,t){let n=kt(e);return r=>{if(n(r))return r;throw new Error(`Unknown ${t}: ${r}`)}}var Xi=i(()=>{"use strict"});var ae,Kc,Jc,ge,zc,Qi,be=i(()=>{"use strict";Xi();ae=(a=>(a.Verbatim="verbatim",a.Detailed="detailed",a.Focused="focused",a.Relevant="relevant",a))(ae||{}),Kc=kt(ae),Jc=nn(ae,"chunk retention"),ge=(r=>(r.Critical="critical",r.Important="important",r.Helpful="helpful",r))(ge||{}),zc=kt(ge),Qi=nn(ge,"chunk importance")});var Vi=i(()=>{"use strict";be()});function tt(e){let t=0,n=!1;for(let r of e.trim()){if(wg.test(r)){t+=1,n=!1;continue}if(Eg.test(r)){n||(t+=1),n=!0;continue}n&&xg.test(r)||(n=!1)}return t}var wg,Eg,xg,nt=i(()=>{"use strict";wg=new RegExp("\\p{Script=Han}","u"),Eg=/[\p{Letter}\p{Number}]/u,xg=new RegExp("\\p{Mark}","u")});var Qc=i(()=>{"use strict";nt();be()});var xr=i(()=>{"use strict"});var Zi=i(()=>{"use strict";tn();be();Vi();Qc();xr()});var Vc=i(()=>{"use strict";tn();$c();Zi()});var ea=i(()=>{"use strict";Vc();Vi();Zi();xr()});var Ir=i(()=>{"use strict";p();et()});var ta=i(()=>{"use strict"});var ed=i(()=>{"use strict";et();Ir()});var td=i(()=>{"use strict";nt()});var na=i(()=>{"use strict"});var ra=i(()=>{"use strict";et();ee();ta();Ir();ed();td();na()});var nd=i(()=>{"use strict";et();Ir();ra()});var ia=i(()=>{"use strict";nd();ta();ra();na()});import{z as oe}from"zod";var rd,De,id,ad,rn=i(()=>{"use strict";rd=["epub","pdf","txt","markdown"],De=1,id=oe.enum(rd),ad=oe.object({version:oe.literal(De),sourceFormat:id,title:oe.string().min(1).nullable(),authors:oe.array(oe.string().min(1)),language:oe.string().min(1).nullable(),identifier:oe.string().min(1).nullable(),publisher:oe.string().min(1).nullable(),publishedAt:oe.string().min(1).nullable(),description:oe.string().min(1).nullable()})});function od(e,t="chapter key"){if(e===""||e==="."||e===".."||e.includes("/"))throw new Error(`Invalid ${t}: chapter paths do not support empty, '.', '..', or relative segments.`);if(!/^[a-z0-9][a-z0-9-]*$/u.test(e))throw new Error(`Invalid ${t}: ${e}. Use lowercase letters, numbers, and hyphens.`);if(/^\d+$/u.test(e))throw new Error(`Invalid ${t}: numeric chapter keys are not accepted.`);if(Ag.has(e))throw new Error(`Invalid ${t}: ${e} is reserved.`);return e}var Ag,se=i(()=>{"use strict";fe();O();Ag=new Set(["tree","source","summary","title","state","chunk","entity","triple","cover","meta","job","help"])});import{z as ve}from"zod";var rt,aa,sd,oa=i(()=>{"use strict";se();rt=1,aa=ve.object({key:ve.string().min(1).transform(e=>od(e)).optional(),title:ve.string().min(1).nullable().optional(),serialId:ve.number().int().nonnegative().optional(),children:ve.lazy(()=>ve.array(aa))}),sd=ve.object({version:ve.literal(rt),items:ve.array(aa)})});function Se(e){let t=[];for(let n of e.replaceAll("\\","/").trim().split("/"))n===""||n==="."||(n===".."?t.pop():t.push(n));return t.join("/")}function Pg(e){let[t]=e.split("#",1);return Se(t??"")}function sa(e){if(e===void 0)return;let t=e.startsWith("#")?e.slice(1):e;return t===""?void 0:t}function an(e){let[t,n]=e.split("#",2);return{path:Se(t??""),fragment:sa(n)}}function Lg(e){return e.split("/").slice(0,-1).join("/")}function Og(...e){return e.filter(Boolean).join("/")}var kr,on=i(()=>{"use strict";p();O();kr=class e{#e;#t;#n;#r;constructor(t,n,r,a){this.#e=t,this.#n=n,this.#t=r,this.#r=a}static async open(t){let n=await t.read(),r=typeof n=="string"?new TextEncoder().encode(n):n,a=X("sha256").update(r).digest("hex"),o=await L().zip.open(t);try{let s=new Map;for(let c of await o.listEntries()){let d=Se(c);d!==""&&s.set(d,c)}return new e(t,o,s,a)}catch(s){throw await o.close(),s}}close(){return this.#n.close()}hasEntry(t){return this.#t.has(Se(t))}listEntries(){return[...this.#t.keys()]}async readText(t){return new TextDecoder().decode(await this.readBuffer(t))}async readBuffer(t){let n=this.#i(t),r=await this.#n.readEntry(n);if(r===void 0)throw new Error(`EPUB entry does not exist: ${Se(t)}`);return r}resolveRelativePath(t,n){let r=Pg(n);if(r==="")throw new Error(`Invalid EPUB href: ${n}`);return Se(Og(Lg(Se(t)),r))}createSectionId(t,n){let r=Se(t);return n===void 0||n===""?r:`${r}#${n}`}createSyntheticSectionId(t,n){let r=Se(t);return`toc:${X("sha1").update(`${r}:${n??""}`).digest("hex").slice(0,10)}`}get name(){return this.#e.name}get digest(){return this.#r}#i(t){let n=Se(t),r=this.#t.get(n);if(r===void 0)throw new Error(`EPUB entry does not exist: ${n}`);return r}}});import{parseDocument as Ng}from"htmlparser2";async function pd(e,t){let n=new Map;for(let[r,a]of t.entries()){let o=await hd(e,r),s=fd(o,a);for(let c of a){let d=s.get(c.id)??"";n.set(c.id,{hasContent:d.trim()!=="",wordsCount:tt(d)})}}return n}function Mg(e){let t=new Map;for(let[n,r]of e.entries())for(let a of r)t.set(a.id,{path:n,targets:r});return t}async function hd(e,t){return await e.readText(t)}function fd(e,t){let n=yd(e,t,!1);return new Map([...n.entries()].map(([r,a])=>[t[r].id,a.text]))}function Wg(e,t,n){let r=yd(e,t,!0),a=t.findIndex(s=>s.id===n),o=r.get(a);if(o===void 0)throw new Error("EPUB section content could not be parsed.");return o}function yd(e,t,n){let r=new Map,a=new Map,o=[...t],c=o[0]?.fragment===void 0&&o[0]!==void 0?0:-1;o.forEach((g,v)=>{a.set(v,[]),g.fragment!==void 0&&!r.has(g.fragment)&&r.set(g.fragment,v)});let d=Ng(e,{decodeEntities:!0}),u=(d.children??[]).find(ud);if(u===void 0){if(n)throw new Error("EPUB XHTML document has no root element for CFI.");f(d,"")}else f(u,"");return new Map([...a.entries()].map(([g,v])=>{let P=Gg(v);return[g,{segments:P.segments,text:P.text}]}));function f(g,v){if(dd(g)){if(c<0||c>=o.length)return;let W=g.data??"";if(W==="")return;let Z=o[c],Xe=Z?.spineIndex;if(n&&Xe===void 0)throw new Error(`EPUB section ${Z?.id??"unknown"} cannot produce a CFI locator.`);let ur=n&&Xe!==void 0?md(Xe,v,0,W.length):ca;w(c,W,ur,n?0:void 0,n?W.length:void 0);return}let P=Hg(g);if(P===void 0){x(g,v);return}if(Fg.has(P))return;let H=v,M=g.attribs?.id??g.attribs?.["xml:id"];if(M!==void 0){let W=r.get(M);W!==void 0&&W>c&&(c=W)}P==="br"&&R(c,`
|
|
22
|
+
`,H),x(g,H),Dg.has(P)&&R(c,`
|
|
23
|
+
|
|
24
|
+
`,H)}function x(g,v){let P=0,H=new Map;for(let M of g.children??[]){if(dd(M)){if(c<0||c>=o.length)continue;let Z=M.data??"";if(Z==="")continue;let Xe=P*2+1,ur=H.get(Xe)??0;H.set(Xe,ur+Z.length);let vy=ld(v,Xe,!0);D(M,vy,ur,Z.length);continue}if(!ud(M))continue;let W=ld(v,P,!1,M.attribs?.id??M.attribs?.["xml:id"]);P+=1,f(M,W)}}function D(g,v,P,H){if(c<0||c>=o.length)return;let M=g.data??"",W=o[c],Z=W?.spineIndex;if(n&&Z===void 0)throw new Error(`EPUB section ${W?.id??"unknown"} cannot produce a CFI locator.`);w(c,M,n&&Z!==void 0?md(Z,v,P,P+H):ca,n?P:void 0,n?P+H:void 0)}function w(g,v,P,H,M){let W=a.get(g);W!==void 0&&W.push({locator:P,...M===void 0?{}:{locatorEnd:M},...H===void 0?{}:{locatorStart:H},text:v})}function R(g,v,P){if(g<0||g>=o.length)return;let H=o[g],M=H?.spineIndex;if(n&&M===void 0)throw new Error(`EPUB section ${H?.id??"unknown"} cannot produce a CFI locator.`);w(g,v,n&&M!==void 0?$g(M,P):ca)}}function jg(e,t){let n=[],r=0;for(let a of e){let o=r;r+=Array.from(a.text).length,o!==r&&n.push({artifactDigest:t,locator:a.locator,sourceEnd:r,sourceStart:o})}return n}function Gg(e){let t=[];for(let d of e){let u=Array.from(d.text),f=0;for(let x=0;x<u.length;x+=1){let D=u[x],w=d.locatorStart===void 0?void 0:d.locatorStart+f;f+=D.length;let R=d.locatorEnd===void 0||d.locatorStart===void 0?void 0:d.locatorStart+f;D==="\r"&&u[x+1]===`
|
|
25
|
+
`?(f+=1,R=d.locatorStart===void 0?void 0:d.locatorStart+f,t.push({baseLocator:d.locator,char:`
|
|
26
|
+
`,locator:da(d.locator,w,R),locatorEnd:R,locatorStart:w}),x+=1):t.push({baseLocator:d.locator,char:D,locator:da(d.locator,w,R),locatorEnd:R,locatorStart:w})}}let n=[],r=0;for(let d of t){if(d.char===`
|
|
27
|
+
`){if(r+=1,r>2)continue}else r=0;n.push(d)}let a=0;for(;a<n.length&&n[a].char.trim()==="";)a+=1;let o=n.length;for(;o>a&&n[o-1].char.trim()==="";)o-=1;let s=n.slice(a,o),c=[];for(let d of s){let u=c.at(-1);u!==void 0&&u.locatorBase===d.baseLocator&&u.locatorEnd===d.locatorStart&&u.locatorStart!==void 0&&d.locatorEnd!==void 0?c[c.length-1]={locatorBase:u.locatorBase,locator:da(u.locatorBase??u.locator,u.locatorStart,d.locatorEnd),locatorEnd:d.locatorEnd,locatorStart:u.locatorStart,text:u.text+d.char}:u!==void 0&&u.locatorBase===d.baseLocator&&u.locatorStart===void 0&&d.locatorStart===void 0?c[c.length-1]={locatorBase:u.locatorBase,locator:u.locator,text:u.text+d.char}:c.push({locatorBase:d.baseLocator,locator:d.locator,...d.locatorEnd===void 0?{}:{locatorEnd:d.locatorEnd},...d.locatorStart===void 0?{}:{locatorStart:d.locatorStart},text:d.char})}return{segments:c,text:s.map(d=>d.char).join("")}}function da(e,t,n){if(t===void 0||n===void 0)return e;let r=e.cfi;if(typeof r!="string")throw new Error("EPUB text mapping is missing a CFI locator.");let a=Bg(r);if(a===void 0)throw new Error("EPUB text mapping has an invalid CFI range locator.");return{...e,cfi:`epubcfi(${a.parentPath},${cd(a.startPath,t)},${cd(a.endPath,n)})`}}function Bg(e){if(!e.startsWith("epubcfi(")||!e.endsWith(")"))return;let t=Ug(e.slice(8,-1));if(t.length!==3||t.some(o=>o===""))return;let[n,r,a]=t;if(!(n===void 0||r===void 0||a===void 0))return{endPath:a,parentPath:n,startPath:r}}function Ug(e){let t=[],n=0,r=0,a=!1;for(let o=0;o<e.length;o+=1){let s=e[o];if(a){a=!1;continue}if(s==="^"){a=!0;continue}if(s==="["){r+=1;continue}if(s==="]"){r-=1;continue}s===","&&r===0&&(t.push(e.slice(n,o)),n=o+1)}return t.push(e.slice(n)),t}function cd(e,t){if(!/:\d+$/u.test(e))throw new Error("EPUB text mapping has an invalid CFI offset.");return e.replace(/:\d+$/u,`:${t}`)}function Hg(e){return e.name?.toLowerCase()}function dd(e){return e.type==="text"}function ud(e){return e.name!==void 0&&e.type!=="directive"}function ld(e,t,n,r){let a=n?t:(t+1)*2,o=n||r===void 0?"":`[${Jg(r)}]`;return`${e}/${a}${o}`}function md(e,t,n,r){let a=gd(e),{parentPath:o,textPath:s}=qg(t),c=o===""?a:`${a}!${o}`,d=o===""?`!${s}:${n}`:`${s}:${n}`,u=o===""?`!${s}:${r}`:`${s}:${r}`;return{cfi:`epubcfi(${c},${d},${u})`}}function qg(e){let t=e.lastIndexOf("/");if(t<0||t===e.length-1)throw new Error(`EPUB text mapping has an invalid CFI path: ${e}`);return{parentPath:e.slice(0,t),textPath:e.slice(t)}}function $g(e,t){return{cfi:`epubcfi(${Kg(e)}${t})`}}function Kg(e){return`${gd(e)}!`}function gd(e){return`/6/${(e+1)*2}`}function Jg(e){return[...e].map(t=>"[]^(),;=".includes(t)?`^${t}`:t).join("")}var Dg,Fg,ca,Rr,bd=i(()=>{"use strict";nt();Dg=new Set(["address","article","aside","blockquote","br","dd","div","dl","dt","figcaption","figure","footer","h1","h2","h3","h4","h5","h6","header","hr","li","main","nav","ol","p","pre","section","table","td","th","tr","ul"]),Fg=new Set(["script","style"]),ca={},Rr=class{#e;#t;constructor(t,n){this.#e=t,this.#t=Mg(n)}async openSection(t){let n=this.#t.get(t);return n===void 0?[]:[(await this.#n(n.path,n.targets)).get(t)??""]}async openSectionWithProvenance(t,n){let r=this.#t.get(t);if(r===void 0)return{stream:[]};if(r.targets.find(d=>d.id===t)?.spineIndex===void 0)throw new Error(`EPUB section ${t} is not associated with a spine item.`);let o=await this.#e.readText(r.path),s=Wg(o,r.targets,t),c=jg(s.segments,n.digest);if(s.text.length>0&&c.length===0)throw new Error(`EPUB section ${t} produced text without provenance mappings.`);return{provenance:{artifacts:[n],mappings:c},stream:[s.text]}}async#n(t,n){let r=await hd(this.#e,t);return fd(r,n)}}});import{SaxesParser as zg}from"saxes";function sn(e){let t=new zg({xmlns:!1}),n=[],r;if(t.on("opentag",a=>{let o={name:a.name,attributes:Object.fromEntries(Object.entries(a.attributes).map(([c,d])=>[c,String(d)])),children:[],text:""},s=n[n.length-1];s===void 0?r=o:s.children.push(o),n.push(o)}),t.on("text",a=>{let o=n[n.length-1];o!==void 0&&(o.text+=a)}),t.on("cdata",a=>{let o=n[n.length-1];o!==void 0&&(o.text+=a)}),t.on("closetag",()=>{n.pop()}),t.write(e).close(),r===void 0)throw new Error("XML document is empty");return Sd(r)}function ce(e,t){return e.children.find(n=>dn(n.name)===t)}function Fe(e,t){return e.children.filter(n=>dn(n.name)===t)}function J(e,t){return e.attributes[t]}function cn(e){let t=e.text;for(let n of e.children)t+=cn(n);return t}function dn(e){let t=e.indexOf(":");return t===-1?e:e.slice(t+1)}function Sd(e){return{name:e.name,attributes:Object.freeze({...e.attributes}),children:Object.freeze(e.children.map(Sd)),text:e.text}}var ua=i(()=>{"use strict"});async function Ed(e){let t=await Vg(e),n=sn(await e.readText(t)),r=Yg(J(n,"version")),a=ce(n,"metadata"),o=ce(n,"manifest"),s=ce(n,"spine"),c=ce(n,"guide"),d=eb(e,t,o),u=new Map([...d.values()].map(v=>[v.path,v])),f=tb(s,d),x=nb(e,t,c),D=Zg(a),w=rb(d),R=ib(d,s),g=ab(d,a);return{version:r,opfPath:t,metadata:{...D,version:De,sourceFormat:"epub"},manifest:d,manifestByPath:u,spine:f,navPath:w,ncxPath:R,coverPath:g?.path,coverMediaType:g?.mediaType,guideNonContentPaths:x}}async function Vg(e){let t=sn(await e.readText("META-INF/container.xml")),r=ce(t,"rootfiles")?.children.find(o=>dn(o.name)==="rootfile"),a=r===void 0?void 0:J(r,"full-path");if(a===void 0||a.trim()==="")throw new Error("EPUB container.xml does not contain an OPF rootfile");return a}function Yg(e){return e?.trim().startsWith("3")===!0?3:2}function Zg(e){if(e===void 0)return wd();let t=it(e,"title"),n=it(e,"creator"),r=it(e,"language"),a=it(e,"identifier"),o=it(e,"publisher"),s=it(e,"date"),c=it(e,"description");return{...wd(),title:t[0]??null,authors:[...n],language:r[0]??null,identifier:a[0]??null,publisher:o[0]??null,publishedAt:s[0]??null,description:c[0]??null}}function wd(){return{version:De,sourceFormat:"epub",title:null,authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null}}function it(e,t){return e.children.filter(n=>dn(n.name)===t).map(n=>sb(cn(n))).filter(n=>n!==void 0)}function eb(e,t,n){let r=new Map;if(n===void 0)return r;for(let a of Fe(n,"item")){let o=J(a,"id"),s=J(a,"href"),c=J(a,"media-type")??"";o===void 0||s===void 0||r.set(o,{id:o,path:e.resolveRelativePath(t,s),mediaType:c,properties:new Set((J(a,"properties")??"").split(/\s+/u).map(d=>d.trim().toLowerCase()).filter(d=>d!==""))})}return r}function tb(e,t){return e===void 0?[]:Fe(e,"itemref").map((n,r)=>{let a=J(n,"idref");if(a===void 0)return;let o=t.get(a);if(!(o===void 0||o.mediaType!=="application/xhtml+xml"&&o.mediaType!=="text/html"))return{idref:a,path:o.path,mediaType:o.mediaType,spineIndex:r}}).filter(n=>n!==void 0)}function nb(e,t,n){let r=new Set;if(n===void 0)return r;for(let a of Fe(n,"reference")){let o=(J(a,"type")??"").trim().toLowerCase();if(!Qg.has(o))continue;let s=J(a,"href");s===void 0||s.trim()===""||r.add(e.resolveRelativePath(t,an(s).path))}return r}function rb(e){for(let t of e.values())if(t.properties.has("nav"))return t.path}function ib(e,t){let n=t===void 0?void 0:J(t,"toc");if(n!==void 0)return e.get(n)?.path;for(let r of e.values())if(r.mediaType==="application/x-dtbncx+xml")return r.path}function ab(e,t){for(let r of e.values())if(r.properties.has("cover-image")&&r.mediaType.startsWith("image/"))return r;let n=ob(t);if(n!==void 0){let r=e.get(n);if(r?.mediaType.startsWith("image/")===!0)return r}for(let r of Xg){let a=e.get(r);if(a?.mediaType.startsWith("image/")===!0)return a}for(let r of e.values())if(r.mediaType.startsWith("image/")&&zi(r.path).toLowerCase().includes("cover"))return r}function ob(e){if(e!==void 0)for(let t of Fe(e,"meta")){if((J(t,"name")??"").trim().toLowerCase()!=="cover")continue;let r=J(t,"content");if(r!==void 0&&r.trim()!=="")return r.trim()}}function sb(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var Xg,Qg,la=i(()=>{"use strict";et();rn();on();ua();Xg=["cover-image","coverimg","cover-img","cover"],Qg=new Set(["cover","titlepage","other.titlepage"])});import{parseDocument as cb}from"htmlparser2";async function Id(e,t){let n=await ub(e,t)??await lb(e,t)??[];if(n.length===0)return xd(t,new Set);let r=vd(n),a=xd(t,r);return a.length===0?n:[...n,...a]}async function ub(e,t){if(t.navPath===void 0)return;let n=cb(await e.readText(t.navPath),{decodeEntities:!0}),r=ma(n,s=>Me(s)!=="nav"?!1:(s.attribs?.["epub:type"]??s.attribs?.type??"").split(/\s+/u).map(d=>d.trim().toLowerCase()).includes("toc"));if(r===void 0)return;let a=ma(r,s=>Me(s)==="ol");if(a===void 0)return;let o=un(a).filter(s=>Me(s)==="li").map(s=>kd(e,t.navPath,s)).filter(s=>s!==void 0);return o.length===0?void 0:o}async function lb(e,t){if(t.ncxPath===void 0)return;let n=sn(await e.readText(t.ncxPath)),r=ce(n,"navMap");if(r===void 0)return;let a=Fe(r,"navPoint").map(o=>Rd(e,t.ncxPath,o)).filter(o=>o!==void 0);return a.length===0?void 0:a}function kd(e,t,n){let r=_d(n),a=r===void 0?void 0:Ad(Td(r)),o=r!==void 0&&Me(r)==="a"?r.attribs?.href:void 0,s=pb(e,t,o),c=un(n).filter(d=>Me(d)==="ol").flatMap(d=>un(d)).filter(d=>Me(d)==="li").map(d=>kd(e,t,d)).filter(d=>d!==void 0);if(!(a===void 0&&s.path===void 0&&c.length===0))return{title:a,path:s.path,fragment:s.fragment,children:c}}function Rd(e,t,n){let r=ce(n,"navLabel"),a=r===void 0?void 0:ce(r,"text"),o=a===void 0?void 0:Ad(cn(a)),s=ce(n,"content"),c=hb(e,t,J(s??n,"src")),d=Fe(n,"navPoint").map(u=>Rd(e,t,u)).filter(u=>u!==void 0);if(!(o===void 0&&c.path===void 0&&d.length===0))return{title:o,path:c.path,fragment:c.fragment,children:d}}function xd(e,t){return e.spine.filter(n=>!mb(e,n.path,t)).filter(n=>!t.has(n.path)).map(n=>({title:fb(n.path),path:n.path,fragment:void 0,children:[]}))}function mb(e,t,n){if(e.guideNonContentPaths.has(t))return!0;let r=pa(t).toLowerCase();return e.navPath===t?!0:db.has(r)&&!n.has(t)}function vd(e){let t=new Set;for(let n of e){n.path!==void 0&&t.add(n.path);for(let r of vd(n.children))t.add(r)}return t}function pb(e,t,n){if(n===void 0||n.trim()==="")return{path:void 0,fragment:void 0};let{path:r,fragment:a}=an(n);return{path:r===""?void 0:e.resolveRelativePath(t,r),fragment:a}}function hb(e,t,n){if(n===void 0||n.trim()==="")return{path:void 0,fragment:void 0};let{path:r,fragment:a}=an(n);return{path:r===""?void 0:e.resolveRelativePath(t,r),fragment:a}}function ma(e,t){if(Cd(e)&&t(e))return e;for(let n of e.children??[]){let r=ma(n,t);if(r!==void 0)return r}}function _d(e){for(let t of un(e)){let n=Me(t);if(n==="a"||n==="span")return t}for(let t of un(e)){if(Me(t)==="ol")continue;let n=_d(t);if(n!==void 0)return n}}function Td(e){return e.type==="text"?e.data??"":(e.children??[]).map(Td).join("")}function un(e){return(e.children??[]).filter(Cd)}function Cd(e){return e.type==="tag"||e.type==="script"||e.type==="style"}function Me(e){return e.name?.toLowerCase()}function fb(e){let t=yb(e),n=pa(e).slice(0,-t.length||void 0).trim();return n===""?void 0:n}function pa(e){return e.replaceAll("\\","/").split("/").at(-1)??""}function yb(e){let t=pa(e),n=t.lastIndexOf(".");return n<=0?"":t.slice(n)}function Ad(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var db,Pd=i(()=>{"use strict";on();la();ua();db=new Set(["toc.xhtml","nav.xhtml","contents.xhtml","tableofcontents.xhtml","cover.xhtml","titlepage.xhtml"])});function gb(e){if(e.hasEntry("META-INF/encryption.xml"))throw new Error("Encrypted EPUB is not supported: found META-INF/encryption.xml.")}async function bb(e,t){if(!(t.coverPath===void 0||t.coverMediaType===void 0))return{path:t.coverPath,mediaType:t.coverMediaType,data:await e.readBuffer(t.coverPath)}}function Sb(e,t){let n=new Map;return t.map((r,a)=>Ld(e,r,[a],n))}function Ld(e,t,n,r){let a=sa(t.fragment),o=t.path===void 0?`toc:${n.join(".")}`:e.createSectionId(t.path,a),s=wb(o,r);return{hasContent:t.path!==void 0,id:s,title:t.title,path:t.path,fragment:a,wordsCount:0,children:t.children.map((c,d)=>Ld(e,c,[...n,d],r))}}function Od(e,t){return e.map(n=>{let r=t.get(n.id);return{...n,hasContent:r?.hasContent??!1,wordsCount:r?.wordsCount??0,children:Od(n.children,t)}})}function wb(e,t){let n=t.get(e)??0;return t.set(e,n+1),n===0?e:`${e}~${n+1}`}function Eb(e,t){let n=new Map,r=new Map(t.spine.map(a=>[a.path,a.spineIndex]));for(let a of Nd(e)){if(a.path===void 0)continue;let o=n.get(a.path)??[];o.push({id:a.id,path:a.path,fragment:a.fragment,spineIndex:r.get(a.path)}),n.set(a.path,o)}return n}function Nd(e){let t=[];for(let n of e)t.push(n),t.push(...Nd(n.children));return t}var ha,fa,ln,ya,Dd=i(()=>{"use strict";on();on();bd();Pd();la();ha=class e{#e;#t;constructor(t,n){this.#e=t,this.#t=n}get id(){return this.#t.id}get hasContent(){return this.#t.hasContent}get title(){return this.#t.title}get wordsCount(){return this.#t.wordsCount}get children(){return this.#t.children.map(t=>new e(this.#e,t))}async open(){return await this.#e.openSection(this.#t.id)}async openWithProvenance(){return await this.#e.openSectionWithProvenance(this.#t.id)}},fa=class e{#e;#t;#n;#r;#i;constructor(t,n,r,a,o){this.#e=t,this.#t=n,this.#n=r,this.#r=a,this.#i=o}static async open(t){gb(t);let n=await Ed(t),r={digest:t.digest,mediaType:"application/epub+zip",name:t.name,...n.metadata.identifier===null?{}:{identifier:n.metadata.identifier}},a=await Id(t,n),o=Sb(t,a),s=Eb(o,n),c=await pd(t,s),d=Od(o,c),u=await bb(t,n);return new e(r,n,u,d,new Rr(t,s))}readMeta(){return Promise.resolve(this.#t.metadata)}readCover(){return Promise.resolve(this.#n)}readSections(){return Promise.resolve(this.#r.map(t=>new ha(this,t)))}async openSection(t){return await this.#i.openSection(t)}async openSectionWithProvenance(t){return await this.#i.openSectionWithProvenance(t,this.#e)}},ln=class{get format(){return"epub"}async openSession(t,n){let r=await kr.open(t);try{let a=await fa.open(r);return await n(a)}finally{await r.close()}}},ya=new ln});var Fd=i(()=>{"use strict";Dd()});async function*Ib(e){for(let t of e.match(/.*(?:\r?\n|$)/gu)??[])t!==""&&(yield t)}function kb(e){let t=e.lastIndexOf("."),n=(t<=0?e:e.slice(0,t)).trim();return n===""?null:n}var xb,ga,ba,mn,Md,Wd,jd=i(()=>{"use strict";rn();xb="root",ga=class{#e;#t;constructor(t,n=xb){this.#e=t,this.#t=n}get id(){return this.#t}get hasContent(){return!0}get title(){}get children(){return[]}open(){return Promise.resolve(Ib(this.#e))}},ba=class{#e;#t;#n;constructor(t,n,r){this.#t=t,this.#n=n,this.#e=new ga(typeof r=="string"?r:new TextDecoder().decode(r))}readMeta(){return Promise.resolve({version:De,sourceFormat:this.#n,title:kb(this.#t.name),authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null})}readCover(){return Promise.resolve(void 0)}readSections(){return Promise.resolve([this.#e])}},mn=class{#e;constructor(t){this.#e=t}get format(){return this.#e}async openSession(t,n){let r=await t.read({encoding:"utf8"});return await n(new ba(t,this.#e,r))}},Md=new mn("txt"),Wd=new mn("markdown")});var pn=i(()=>{"use strict"});import{z as te}from"zod";var mT,pT,Gd=i(()=>{"use strict";pn();mT=te.object({type:te.literal("artifact"),digest:te.string().regex(/^[0-9a-f]{64}$/iu),mediaType:te.string().min(1),name:te.string().optional(),identifier:te.string().max(1024).optional()}),pT=te.object({type:te.literal("text"),text:te.string(),locator:te.record(te.string(),te.unknown())})});var at=i(()=>{"use strict";rn();Fd();jd();Gd();oa()});var wa=i(()=>{"use strict"});var hn=i(()=>{"use strict";at();se();wa()});var vb,Bd,Ud,Hd,fn=i(()=>{"use strict";y();pn();vb=`
|
|
28
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
29
|
+
digest BLOB NOT NULL UNIQUE,
|
|
30
|
+
short_uid TEXT NOT NULL UNIQUE
|
|
31
|
+
CHECK(length(short_uid) BETWEEN 12 AND 64)
|
|
32
|
+
CHECK(short_uid NOT GLOB '*[^0-9a-f]*')
|
|
33
|
+
CHECK(short_uid = substr(lower(hex(digest)), 1, length(short_uid))),
|
|
34
|
+
media_type TEXT NOT NULL,
|
|
35
|
+
name TEXT,
|
|
36
|
+
identifier TEXT
|
|
37
|
+
`,Bd=`
|
|
38
|
+
CREATE TABLE IF NOT EXISTS serials (
|
|
39
|
+
id INTEGER PRIMARY KEY,
|
|
40
|
+
document_order INTEGER NOT NULL DEFAULT 0
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
CREATE TABLE IF NOT EXISTS serial_states (
|
|
44
|
+
serial_id INTEGER PRIMARY KEY,
|
|
45
|
+
revision INTEGER NOT NULL DEFAULT 0,
|
|
46
|
+
topology_ready INTEGER NOT NULL DEFAULT 0,
|
|
47
|
+
topology_parameter_hash TEXT,
|
|
48
|
+
knowledge_graph_ready INTEGER NOT NULL DEFAULT 0,
|
|
49
|
+
knowledge_graph_parameter_hash TEXT,
|
|
50
|
+
FOREIGN KEY (serial_id) REFERENCES serials(id)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
CREATE TABLE IF NOT EXISTS archive_revisions (
|
|
54
|
+
key TEXT PRIMARY KEY,
|
|
55
|
+
value INTEGER NOT NULL
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
CREATE TABLE IF NOT EXISTS graph_build_parameters (
|
|
59
|
+
hash TEXT PRIMARY KEY,
|
|
60
|
+
prompt TEXT NOT NULL,
|
|
61
|
+
language TEXT,
|
|
62
|
+
created_at TEXT NOT NULL
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
CREATE TABLE IF NOT EXISTS index_artifacts (
|
|
66
|
+
serial_id INTEGER NOT NULL,
|
|
67
|
+
kind TEXT NOT NULL,
|
|
68
|
+
source_revision INTEGER NOT NULL,
|
|
69
|
+
created_at TEXT NOT NULL,
|
|
70
|
+
metadata_json TEXT NOT NULL DEFAULT '{}',
|
|
71
|
+
PRIMARY KEY (serial_id, kind),
|
|
72
|
+
FOREIGN KEY (serial_id) REFERENCES serials(id)
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
CREATE INDEX IF NOT EXISTS idx_index_artifacts_kind
|
|
76
|
+
ON index_artifacts(kind, serial_id);
|
|
77
|
+
|
|
78
|
+
CREATE TABLE IF NOT EXISTS index_artifact_lexical_rows (
|
|
79
|
+
serial_id INTEGER NOT NULL,
|
|
80
|
+
kind TEXT NOT NULL,
|
|
81
|
+
row_id TEXT NOT NULL,
|
|
82
|
+
object_kind TEXT NOT NULL,
|
|
83
|
+
object_id TEXT NOT NULL,
|
|
84
|
+
sentence_index INTEGER,
|
|
85
|
+
text TEXT NOT NULL,
|
|
86
|
+
tokens_json TEXT NOT NULL,
|
|
87
|
+
metadata_json TEXT NOT NULL DEFAULT '{}',
|
|
88
|
+
PRIMARY KEY (serial_id, kind, row_id),
|
|
89
|
+
FOREIGN KEY (serial_id, kind) REFERENCES index_artifacts(serial_id, kind)
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
CREATE INDEX IF NOT EXISTS idx_index_artifact_lexical_rows_object
|
|
93
|
+
ON index_artifact_lexical_rows(serial_id, kind, object_kind, object_id);
|
|
94
|
+
|
|
95
|
+
CREATE TABLE IF NOT EXISTS index_artifact_embedding_segments (
|
|
96
|
+
serial_id INTEGER NOT NULL,
|
|
97
|
+
kind TEXT NOT NULL,
|
|
98
|
+
segment_index INTEGER NOT NULL,
|
|
99
|
+
start_sentence_index INTEGER NOT NULL,
|
|
100
|
+
end_sentence_index INTEGER NOT NULL,
|
|
101
|
+
words_count INTEGER NOT NULL,
|
|
102
|
+
text TEXT NOT NULL,
|
|
103
|
+
vector_json TEXT NOT NULL,
|
|
104
|
+
PRIMARY KEY (serial_id, kind, segment_index),
|
|
105
|
+
FOREIGN KEY (serial_id, kind) REFERENCES index_artifacts(serial_id, kind)
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
CREATE TABLE IF NOT EXISTS chunks (
|
|
109
|
+
id INTEGER PRIMARY KEY,
|
|
110
|
+
generation INTEGER NOT NULL,
|
|
111
|
+
serial_id INTEGER NOT NULL,
|
|
112
|
+
sentence_index INTEGER NOT NULL,
|
|
113
|
+
label TEXT NOT NULL,
|
|
114
|
+
content TEXT NOT NULL,
|
|
115
|
+
retention TEXT,
|
|
116
|
+
importance TEXT,
|
|
117
|
+
wordsCount INTEGER NOT NULL DEFAULT 0,
|
|
118
|
+
weight REAL NOT NULL DEFAULT 0.0
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
CREATE INDEX IF NOT EXISTS idx_chunks_sentence
|
|
122
|
+
ON chunks(serial_id, sentence_index);
|
|
123
|
+
|
|
124
|
+
CREATE INDEX IF NOT EXISTS idx_chunks_serial_id
|
|
125
|
+
ON chunks(serial_id, id);
|
|
126
|
+
|
|
127
|
+
CREATE TABLE IF NOT EXISTS chunk_sentences (
|
|
128
|
+
chunk_id INTEGER NOT NULL,
|
|
129
|
+
serial_id INTEGER NOT NULL,
|
|
130
|
+
sentence_index INTEGER NOT NULL,
|
|
131
|
+
FOREIGN KEY (chunk_id) REFERENCES chunks(id),
|
|
132
|
+
PRIMARY KEY (chunk_id, serial_id, sentence_index)
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
CREATE TABLE IF NOT EXISTS reading_edges (
|
|
136
|
+
from_id INTEGER NOT NULL,
|
|
137
|
+
to_id INTEGER NOT NULL,
|
|
138
|
+
strength TEXT,
|
|
139
|
+
weight REAL NOT NULL DEFAULT 0.1,
|
|
140
|
+
PRIMARY KEY (from_id, to_id),
|
|
141
|
+
FOREIGN KEY (from_id) REFERENCES chunks(id),
|
|
142
|
+
FOREIGN KEY (to_id) REFERENCES chunks(id)
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
CREATE TABLE IF NOT EXISTS snakes (
|
|
146
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
147
|
+
serial_id INTEGER NOT NULL,
|
|
148
|
+
group_id INTEGER NOT NULL,
|
|
149
|
+
local_snake_id INTEGER NOT NULL,
|
|
150
|
+
size INTEGER NOT NULL,
|
|
151
|
+
first_label TEXT NOT NULL,
|
|
152
|
+
last_label TEXT NOT NULL,
|
|
153
|
+
wordsCount INTEGER NOT NULL DEFAULT 0,
|
|
154
|
+
weight REAL NOT NULL DEFAULT 0.0,
|
|
155
|
+
UNIQUE(serial_id, group_id, local_snake_id)
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
CREATE TABLE IF NOT EXISTS snake_chunks (
|
|
159
|
+
snake_id INTEGER NOT NULL,
|
|
160
|
+
chunk_id INTEGER NOT NULL,
|
|
161
|
+
position INTEGER NOT NULL,
|
|
162
|
+
FOREIGN KEY (snake_id) REFERENCES snakes(id),
|
|
163
|
+
FOREIGN KEY (chunk_id) REFERENCES chunks(id),
|
|
164
|
+
PRIMARY KEY (snake_id, chunk_id)
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
CREATE TABLE IF NOT EXISTS snake_edges (
|
|
168
|
+
from_snake_id INTEGER NOT NULL,
|
|
169
|
+
to_snake_id INTEGER NOT NULL,
|
|
170
|
+
weight REAL NOT NULL DEFAULT 0.1,
|
|
171
|
+
PRIMARY KEY (from_snake_id, to_snake_id),
|
|
172
|
+
FOREIGN KEY (from_snake_id) REFERENCES snakes(id),
|
|
173
|
+
FOREIGN KEY (to_snake_id) REFERENCES snakes(id)
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
CREATE TABLE IF NOT EXISTS sentence_groups (
|
|
177
|
+
serial_id INTEGER NOT NULL,
|
|
178
|
+
group_id INTEGER NOT NULL,
|
|
179
|
+
start_sentence_index INTEGER NOT NULL,
|
|
180
|
+
end_sentence_index INTEGER NOT NULL,
|
|
181
|
+
PRIMARY KEY (serial_id, group_id, start_sentence_index, end_sentence_index)
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
CREATE TABLE IF NOT EXISTS mentions (
|
|
185
|
+
id TEXT PRIMARY KEY,
|
|
186
|
+
chapter_id INTEGER NOT NULL,
|
|
187
|
+
sentence_index INTEGER,
|
|
188
|
+
range_start INTEGER NOT NULL,
|
|
189
|
+
range_end INTEGER NOT NULL,
|
|
190
|
+
surface TEXT NOT NULL,
|
|
191
|
+
qid TEXT NOT NULL,
|
|
192
|
+
confidence REAL,
|
|
193
|
+
note TEXT,
|
|
194
|
+
FOREIGN KEY (chapter_id) REFERENCES serials(id)
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_chapter
|
|
198
|
+
ON mentions(chapter_id);
|
|
199
|
+
|
|
200
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_chapter_position
|
|
201
|
+
ON mentions(chapter_id, sentence_index, range_start, range_end, id);
|
|
202
|
+
|
|
203
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_chapter_qid
|
|
204
|
+
ON mentions(chapter_id, qid);
|
|
205
|
+
|
|
206
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_qid
|
|
207
|
+
ON mentions(qid);
|
|
208
|
+
|
|
209
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_qid_position
|
|
210
|
+
ON mentions(qid, chapter_id, sentence_index, range_start, range_end, id);
|
|
211
|
+
|
|
212
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_surface
|
|
213
|
+
ON mentions(surface);
|
|
214
|
+
|
|
215
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_surface_position
|
|
216
|
+
ON mentions(surface, chapter_id, sentence_index, range_start, range_end, id);
|
|
217
|
+
|
|
218
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_sentence
|
|
219
|
+
ON mentions(chapter_id, sentence_index);
|
|
220
|
+
|
|
221
|
+
CREATE TABLE IF NOT EXISTS mention_links (
|
|
222
|
+
id TEXT PRIMARY KEY,
|
|
223
|
+
source_mention_id TEXT NOT NULL,
|
|
224
|
+
target_mention_id TEXT NOT NULL,
|
|
225
|
+
predicate TEXT NOT NULL,
|
|
226
|
+
confidence REAL,
|
|
227
|
+
note TEXT,
|
|
228
|
+
FOREIGN KEY (source_mention_id) REFERENCES mentions(id),
|
|
229
|
+
FOREIGN KEY (target_mention_id) REFERENCES mentions(id)
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
CREATE TABLE IF NOT EXISTS mention_link_evidence_sentences (
|
|
233
|
+
link_id TEXT NOT NULL,
|
|
234
|
+
chapter_id INTEGER NOT NULL,
|
|
235
|
+
sentence_index INTEGER NOT NULL,
|
|
236
|
+
FOREIGN KEY (link_id) REFERENCES mention_links(id),
|
|
237
|
+
PRIMARY KEY (link_id, chapter_id, sentence_index)
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
CREATE INDEX IF NOT EXISTS idx_mention_link_evidence_sentences_sentence
|
|
241
|
+
ON mention_link_evidence_sentences(chapter_id, sentence_index);
|
|
61
242
|
|
|
62
|
-
|
|
63
|
-
|
|
243
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_source
|
|
244
|
+
ON mention_links(source_mention_id);
|
|
64
245
|
|
|
65
|
-
`).trim()}function Pg(e){if(typeof e=="string")return e;if(Buffer.isBuffer(e))return e.toString("utf8");throw new Error("Unexpected HTML stream chunk type")}var vg,Ud,hr,$d=i(()=>{"use strict";Je();vg=new Set(["address","article","aside","blockquote","br","dd","div","dl","dt","figcaption","figure","footer","h1","h2","h3","h4","h5","h6","header","hr","li","main","nav","ol","p","pre","section","table","td","th","tr","ul"]),Ud=new Set(["script","style"]),hr=class{#e;#t;constructor(t,n){this.#e=t,this.#t=_g(n)}async openSection(t){let n=this.#t.get(t);return n===void 0?[]:[(await this.#n(n.path,n.targets)).get(t)??""]}async#n(t,n){let r=await this.#e.openReadStream(t);return r.setEncoding("utf8"),await Tg(r,n)}}});import{SaxesParser as Ag}from"saxes";function zt(e){let t=new Ag({xmlns:!1}),n=[],r;if(t.on("opentag",a=>{let o={name:a.name,attributes:Object.fromEntries(Object.entries(a.attributes).map(([d,c])=>[d,String(c)])),children:[],text:""},s=n[n.length-1];s===void 0?r=o:s.children.push(o),n.push(o)}),t.on("text",a=>{let o=n[n.length-1];o!==void 0&&(o.text+=a)}),t.on("cdata",a=>{let o=n[n.length-1];o!==void 0&&(o.text+=a)}),t.on("closetag",()=>{n.pop()}),t.write(e).close(),r===void 0)throw new Error("XML document is empty");return Kd(r)}function X(e,t){return e.children.find(n=>Qt(n.name)===t)}function Ce(e,t){return e.children.filter(n=>Qt(n.name)===t)}function M(e,t){return e.attributes[t]}function Xt(e){let t=e.text;for(let n of e.children)t+=Xt(n);return t}function Qt(e){let t=e.indexOf(":");return t===-1?e:e.slice(t+1)}function Kd(e){return{name:e.name,attributes:Object.freeze({...e.attributes}),children:Object.freeze(e.children.map(Kd)),text:e.text}}var ra=i(()=>{"use strict"});import{posix as Lg}from"path";async function zd(e){let t=await Dg(e),n=zt(await e.readText(t)),r=Mg(M(n,"version")),a=X(n,"metadata"),o=X(n,"manifest"),s=X(n,"spine"),d=X(n,"guide"),c=Wg(e,t,o),m=new Map([...c.values()].map(ve=>[ve.path,ve])),p=jg(s,c),w=Gg(e,t,d),P=Fg(a),I=Bg(c),T=Ug(c,s),N=Hg(c,a);return{version:r,opfPath:t,metadata:{...P,version:Te,sourceFormat:"epub"},manifest:c,manifestByPath:m,spine:p,navPath:I,ncxPath:T,coverPath:N?.path,coverMediaType:N?.mediaType,guideNonContentPaths:w}}async function Dg(e){let t=zt(await e.readText("META-INF/container.xml")),r=X(t,"rootfiles")?.children.find(o=>Qt(o.name)==="rootfile"),a=r===void 0?void 0:M(r,"full-path");if(a===void 0||a.trim()==="")throw new Error("EPUB container.xml does not contain an OPF rootfile");return a}function Mg(e){return e?.trim().startsWith("3")===!0?3:2}function Fg(e){if(e===void 0)return Jd();let t=Xe(e,"title"),n=Xe(e,"creator"),r=Xe(e,"language"),a=Xe(e,"identifier"),o=Xe(e,"publisher"),s=Xe(e,"date"),d=Xe(e,"description");return{...Jd(),title:t[0]??null,authors:[...n],language:r[0]??null,identifier:a[0]??null,publisher:o[0]??null,publishedAt:s[0]??null,description:d[0]??null}}function Jd(){return{version:Te,sourceFormat:"epub",title:null,authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null}}function Xe(e,t){return e.children.filter(n=>Qt(n.name)===t).map(n=>$g(Xt(n))).filter(n=>n!==void 0)}function Wg(e,t,n){let r=new Map;if(n===void 0)return r;for(let a of Ce(n,"item")){let o=M(a,"id"),s=M(a,"href"),d=M(a,"media-type")??"";o===void 0||s===void 0||r.set(o,{id:o,path:e.resolveRelativePath(t,s),mediaType:d,properties:new Set((M(a,"properties")??"").split(/\s+/u).map(c=>c.trim().toLowerCase()).filter(c=>c!==""))})}return r}function jg(e,t){return e===void 0?[]:Ce(e,"itemref").map(n=>M(n,"idref")).filter(n=>n!==void 0).map(n=>{let r=t.get(n);if(!(r===void 0||r.mediaType!=="application/xhtml+xml"&&r.mediaType!=="text/html"))return{idref:n,path:r.path,mediaType:r.mediaType}}).filter(n=>n!==void 0)}function Gg(e,t,n){let r=new Set;if(n===void 0)return r;for(let a of Ce(n,"reference")){let o=(M(a,"type")??"").trim().toLowerCase();if(!Ng.has(o))continue;let s=M(a,"href");s===void 0||s.trim()===""||r.add(e.resolveRelativePath(t,Kt(s).path))}return r}function Bg(e){for(let t of e.values())if(t.properties.has("nav"))return t.path}function Ug(e,t){let n=t===void 0?void 0:M(t,"toc");if(n!==void 0)return e.get(n)?.path;for(let r of e.values())if(r.mediaType==="application/x-dtbncx+xml")return r.path}function Hg(e,t){for(let r of e.values())if(r.properties.has("cover-image")&&r.mediaType.startsWith("image/"))return r;let n=qg(t);if(n!==void 0){let r=e.get(n);if(r?.mediaType.startsWith("image/")===!0)return r}for(let r of Og){let a=e.get(r);if(a?.mediaType.startsWith("image/")===!0)return a}for(let r of e.values())if(r.mediaType.startsWith("image/")&&Lg.basename(r.path).toLowerCase().includes("cover"))return r}function qg(e){if(e!==void 0)for(let t of Ce(e,"meta")){if((M(t,"name")??"").trim().toLowerCase()!=="cover")continue;let r=M(t,"content");if(r!==void 0&&r.trim()!=="")return r.trim()}}function $g(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var Og,Ng,ia=i(()=>{"use strict";$t();Jt();ra();Og=["cover-image","coverimg","cover-img","cover"],Ng=new Set(["cover","titlepage","other.titlepage"])});import{basename as Kg,extname as Jg,posix as zg}from"path";import{parseDocument as Xg}from"htmlparser2";async function Qd(e,t){let n=await Vg(e,t)??await Yg(e,t)??[];if(n.length===0)return Xd(t,new Set);let r=Zd(n),a=Xd(t,r);return a.length===0?n:[...n,...a]}async function Vg(e,t){if(t.navPath===void 0)return;let n=Xg(await e.readText(t.navPath),{decodeEntities:!0}),r=aa(n,s=>Pe(s)!=="nav"?!1:(s.attribs?.["epub:type"]??s.attribs?.type??"").split(/\s+/u).map(c=>c.trim().toLowerCase()).includes("toc"));if(r===void 0)return;let a=aa(r,s=>Pe(s)==="ol");if(a===void 0)return;let o=Vt(a).filter(s=>Pe(s)==="li").map(s=>Vd(e,t.navPath,s)).filter(s=>s!==void 0);return o.length===0?void 0:o}async function Yg(e,t){if(t.ncxPath===void 0)return;let n=zt(await e.readText(t.ncxPath)),r=X(n,"navMap");if(r===void 0)return;let a=Ce(r,"navPoint").map(o=>Yd(e,t.ncxPath,o)).filter(o=>o!==void 0);return a.length===0?void 0:a}function Vd(e,t,n){let r=ec(n),a=r===void 0?void 0:rc(tc(r)),o=r!==void 0&&Pe(r)==="a"?r.attribs?.href:void 0,s=eb(e,t,o),d=Vt(n).filter(c=>Pe(c)==="ol").flatMap(c=>Vt(c)).filter(c=>Pe(c)==="li").map(c=>Vd(e,t,c)).filter(c=>c!==void 0);if(!(a===void 0&&s.path===void 0&&d.length===0))return{title:a,path:s.path,fragment:s.fragment,children:d}}function Yd(e,t,n){let r=X(n,"navLabel"),a=r===void 0?void 0:X(r,"text"),o=a===void 0?void 0:rc(Xt(a)),s=X(n,"content"),d=tb(e,t,M(s??n,"src")),c=Ce(n,"navPoint").map(m=>Yd(e,t,m)).filter(m=>m!==void 0);if(!(o===void 0&&d.path===void 0&&c.length===0))return{title:o,path:d.path,fragment:d.fragment,children:c}}function Xd(e,t){return e.spine.filter(n=>!Zg(e,n.path,t)).filter(n=>!t.has(n.path)).map(n=>({title:nb(n.path),path:n.path,fragment:void 0,children:[]}))}function Zg(e,t,n){if(e.guideNonContentPaths.has(t))return!0;let r=zg.basename(t).toLowerCase();return e.navPath===t?!0:Qg.has(r)&&!n.has(t)}function Zd(e){let t=new Set;for(let n of e){n.path!==void 0&&t.add(n.path);for(let r of Zd(n.children))t.add(r)}return t}function eb(e,t,n){if(n===void 0||n.trim()==="")return{path:void 0,fragment:void 0};let{path:r,fragment:a}=Kt(n);return{path:r===""?void 0:e.resolveRelativePath(t,r),fragment:a}}function tb(e,t,n){if(n===void 0||n.trim()==="")return{path:void 0,fragment:void 0};let{path:r,fragment:a}=Kt(n);return{path:r===""?void 0:e.resolveRelativePath(t,r),fragment:a}}function aa(e,t){if(nc(e)&&t(e))return e;for(let n of e.children??[]){let r=aa(n,t);if(r!==void 0)return r}}function ec(e){for(let t of Vt(e)){let n=Pe(t);if(n==="a"||n==="span")return t}for(let t of Vt(e)){if(Pe(t)==="ol")continue;let n=ec(t);if(n!==void 0)return n}}function tc(e){return e.type==="text"?e.data??"":(e.children??[]).map(tc).join("")}function Vt(e){return(e.children??[]).filter(nc)}function nc(e){return e.type==="tag"||e.type==="script"||e.type==="style"}function Pe(e){return e.name?.toLowerCase()}function nb(e){let t=Kg(e,Jg(e)).trim();return t===""?void 0:t}function rc(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var Qg,ic=i(()=>{"use strict";Jt();ia();ra();Qg=new Set(["toc.xhtml","nav.xhtml","contents.xhtml","tableofcontents.xhtml","cover.xhtml","titlepage.xhtml"])});function rb(e){if(e.hasEntry("META-INF/encryption.xml"))throw new Error("Encrypted EPUB is not supported: found META-INF/encryption.xml.")}async function ib(e,t){if(!(t.coverPath===void 0||t.coverMediaType===void 0))return{path:t.coverPath,mediaType:t.coverMediaType,data:await e.readBuffer(t.coverPath)}}function ab(e,t){let n=new Map;return t.map((r,a)=>ac(e,r,[a],n))}function ac(e,t,n,r){let a=ta(t.fragment),o=t.path===void 0?`toc:${n.join(".")}`:e.createSectionId(t.path,a),s=ob(o,r);return{hasContent:t.path!==void 0,id:s,title:t.title,path:t.path,fragment:a,wordsCount:0,children:t.children.map((d,c)=>ac(e,d,[...n,c],r))}}function oc(e,t){return e.map(n=>{let r=t.get(n.id);return{...n,hasContent:r?.hasContent??!1,wordsCount:r?.wordsCount??0,children:oc(n.children,t)}})}function ob(e,t){let n=t.get(e)??0;return t.set(e,n+1),n===0?e:`${e}~${n+1}`}function sb(e){let t=new Map;for(let n of sc(e)){if(n.path===void 0)continue;let r=t.get(n.path)??[];r.push({id:n.id,path:n.path,fragment:n.fragment}),t.set(n.path,r)}return t}function sc(e){let t=[];for(let n of e)t.push(n),t.push(...sc(n.children));return t}var oa,sa,Yt,da,dc=i(()=>{"use strict";Jt();Jt();$d();ic();ia();oa=class e{#e;#t;constructor(t,n){this.#e=t,this.#t=n}get id(){return this.#t.id}get hasContent(){return this.#t.hasContent}get title(){return this.#t.title}get wordsCount(){return this.#t.wordsCount}get children(){return this.#t.children.map(t=>new e(this.#e,t))}async open(){return await this.#e.openSection(this.#t.id)}},sa=class e{#e;#t;#n;#r;constructor(t,n,r,a){this.#e=t,this.#t=n,this.#n=r,this.#r=a}static async open(t){rb(t);let n=await zd(t),r=await Qd(t,n),a=ab(t,r),o=sb(a),s=await Hd(t,o),d=oc(a,s),c=await ib(t,n);return new e(n,c,d,new hr(t,o))}readMeta(){return Promise.resolve(this.#e.metadata)}readCover(){return Promise.resolve(this.#t)}readSections(){return Promise.resolve(this.#n.map(t=>new oa(this,t)))}async openSection(t){return await this.#r.openSection(t)}},Yt=class{get format(){return"epub"}async openSession(t,n){let r=await pr.open(t);try{let a=await sa.open(r);return await n(a)}finally{await r.close()}}},da=new Yt});var cc=i(()=>{"use strict";dc()});import{createReadStream as db}from"fs";import{stat as cb}from"fs/promises";import{basename as ub,extname as lb,resolve as la}from"path";function pb(e){let t=ub(e,lb(e)).trim();return t===""?null:t}var mb,ca,ua,Zt,uc,lc,mc=i(()=>{"use strict";$t();mb="root",ca=class{#e;#t;constructor(t,n=mb){this.#e=la(t),this.#t=n}get id(){return this.#t}get hasContent(){return!0}get title(){}get children(){return[]}open(){return Promise.resolve(db(this.#e,{encoding:"utf8"}))}},ua=class{#e;#t;#n;constructor(t,n){this.#e=la(t),this.#t=n,this.#n=new ca(this.#e)}readMeta(){return Promise.resolve({version:Te,sourceFormat:this.#t,title:pb(this.#e),authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null})}readCover(){return Promise.resolve(void 0)}readSections(){return Promise.resolve([this.#n])}},Zt=class{#e;constructor(t){this.#e=t}get format(){return this.#e}async openSession(t,n){let r=la(t);if(!(await cb(r)).isFile())throw new Error(`Source file is not a regular file: ${r}`);return await n(new ua(r,this.#e))}},uc=new Zt("txt"),lc=new Zt("markdown")});var Qe=i(()=>{"use strict";$t();cc();mc();mr()});var ma=i(()=>{"use strict"});var en=i(()=>{"use strict";Qe();ge();ma()});var pc,hc,tn=i(()=>{"use strict";pc=`
|
|
246
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_target
|
|
247
|
+
ON mention_links(target_mention_id);
|
|
248
|
+
|
|
249
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_predicate
|
|
250
|
+
ON mention_links(predicate);
|
|
251
|
+
|
|
252
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_predicate_source_target
|
|
253
|
+
ON mention_links(predicate, source_mention_id, target_mention_id);
|
|
254
|
+
|
|
255
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_predicate_target_source
|
|
256
|
+
ON mention_links(predicate, target_mention_id, source_mention_id);
|
|
257
|
+
|
|
258
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_source_predicate_target
|
|
259
|
+
ON mention_links(source_mention_id, predicate, target_mention_id);
|
|
260
|
+
|
|
261
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_target_predicate_source
|
|
262
|
+
ON mention_links(target_mention_id, predicate, source_mention_id);
|
|
263
|
+
|
|
264
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_source_target_predicate
|
|
265
|
+
ON mention_links(source_mention_id, target_mention_id, predicate);
|
|
266
|
+
|
|
267
|
+
CREATE INDEX IF NOT EXISTS idx_reading_edges_target
|
|
268
|
+
ON reading_edges(to_id, from_id);
|
|
269
|
+
|
|
270
|
+
CREATE INDEX IF NOT EXISTS idx_snake_edges_target
|
|
271
|
+
ON snake_edges(to_snake_id, from_snake_id);
|
|
272
|
+
|
|
273
|
+
CREATE VIEW IF NOT EXISTS chapter_entities AS
|
|
274
|
+
SELECT
|
|
275
|
+
chapter_id,
|
|
276
|
+
qid,
|
|
277
|
+
COUNT(*) AS mention_count
|
|
278
|
+
FROM mentions
|
|
279
|
+
GROUP BY chapter_id, qid;
|
|
280
|
+
|
|
281
|
+
CREATE VIEW IF NOT EXISTS book_entities AS
|
|
282
|
+
SELECT
|
|
283
|
+
qid,
|
|
284
|
+
SUM(mention_count) AS mention_count
|
|
285
|
+
FROM chapter_entities
|
|
286
|
+
GROUP BY qid;
|
|
287
|
+
|
|
288
|
+
CREATE VIEW IF NOT EXISTS chapter_entity_relations AS
|
|
289
|
+
SELECT
|
|
290
|
+
source_mentions.chapter_id AS chapter_id,
|
|
291
|
+
source_mentions.qid AS subject_qid,
|
|
292
|
+
mention_links.predicate AS predicate,
|
|
293
|
+
target_mentions.qid AS object_qid,
|
|
294
|
+
COUNT(*) AS evidence_count
|
|
295
|
+
FROM mention_links
|
|
296
|
+
JOIN mentions AS source_mentions
|
|
297
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
298
|
+
JOIN mentions AS target_mentions
|
|
299
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
300
|
+
WHERE source_mentions.chapter_id = target_mentions.chapter_id
|
|
301
|
+
GROUP BY
|
|
302
|
+
source_mentions.chapter_id,
|
|
303
|
+
source_mentions.qid,
|
|
304
|
+
mention_links.predicate,
|
|
305
|
+
target_mentions.qid;
|
|
306
|
+
|
|
307
|
+
CREATE VIEW IF NOT EXISTS book_entity_relations AS
|
|
308
|
+
SELECT
|
|
309
|
+
subject_qid,
|
|
310
|
+
predicate,
|
|
311
|
+
object_qid,
|
|
312
|
+
SUM(evidence_count) AS evidence_count
|
|
313
|
+
FROM chapter_entity_relations
|
|
314
|
+
GROUP BY subject_qid, predicate, object_qid;
|
|
315
|
+
|
|
316
|
+
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
317
|
+
id INTEGER PRIMARY KEY,
|
|
318
|
+
kind INTEGER NOT NULL,
|
|
319
|
+
chapter_id INTEGER NOT NULL,
|
|
320
|
+
sentence_index INTEGER NOT NULL,
|
|
321
|
+
words_count INTEGER NOT NULL DEFAULT 0,
|
|
322
|
+
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
323
|
+
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
324
|
+
UNIQUE(kind, chapter_id, sentence_index)
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
328
|
+
ON text_sentence_records(kind, chapter_id, sentence_index);
|
|
329
|
+
|
|
330
|
+
CREATE TABLE IF NOT EXISTS source_artifacts (
|
|
331
|
+
${vb}
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
CREATE TABLE IF NOT EXISTS source_locators (
|
|
335
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
336
|
+
artifact_id INTEGER NOT NULL,
|
|
337
|
+
fragment TEXT NOT NULL,
|
|
338
|
+
value_json TEXT NOT NULL,
|
|
339
|
+
UNIQUE(artifact_id, fragment),
|
|
340
|
+
FOREIGN KEY (artifact_id) REFERENCES source_artifacts(id)
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
CREATE INDEX IF NOT EXISTS idx_source_locators_artifact
|
|
344
|
+
ON source_locators(artifact_id);
|
|
345
|
+
|
|
346
|
+
CREATE TABLE IF NOT EXISTS source_text_maps (
|
|
347
|
+
serial_id INTEGER NOT NULL,
|
|
348
|
+
source_revision INTEGER NOT NULL,
|
|
349
|
+
source_start INTEGER NOT NULL,
|
|
350
|
+
source_end INTEGER NOT NULL,
|
|
351
|
+
locator_id INTEGER NOT NULL,
|
|
352
|
+
PRIMARY KEY (serial_id, source_start, source_end, locator_id),
|
|
353
|
+
FOREIGN KEY (serial_id) REFERENCES serials(id),
|
|
354
|
+
FOREIGN KEY (locator_id) REFERENCES source_locators(id)
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
CREATE INDEX IF NOT EXISTS idx_source_text_maps_serial_range
|
|
358
|
+
ON source_text_maps(serial_id, source_start, source_end);
|
|
359
|
+
|
|
360
|
+
CREATE INDEX IF NOT EXISTS idx_source_text_maps_locator
|
|
361
|
+
ON source_text_maps(locator_id);
|
|
362
|
+
|
|
363
|
+
CREATE TABLE IF NOT EXISTS object_metadata (
|
|
364
|
+
id INTEGER PRIMARY KEY,
|
|
365
|
+
object_kind INTEGER NOT NULL,
|
|
366
|
+
object_path TEXT NOT NULL,
|
|
367
|
+
key TEXT NOT NULL,
|
|
368
|
+
value_json TEXT NOT NULL,
|
|
369
|
+
updated_at TEXT NOT NULL,
|
|
370
|
+
chapter_id INTEGER,
|
|
371
|
+
chunk_id INTEGER,
|
|
372
|
+
entity_qid TEXT,
|
|
373
|
+
triple_subject_qid TEXT,
|
|
374
|
+
triple_predicate TEXT,
|
|
375
|
+
triple_object_qid TEXT,
|
|
376
|
+
UNIQUE(object_path, key)
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_object
|
|
380
|
+
ON object_metadata(object_path);
|
|
381
|
+
|
|
382
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_chapter
|
|
383
|
+
ON object_metadata(chapter_id);
|
|
384
|
+
|
|
385
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_chunk
|
|
386
|
+
ON object_metadata(chunk_id);
|
|
387
|
+
|
|
388
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_entity
|
|
389
|
+
ON object_metadata(entity_qid);
|
|
390
|
+
|
|
391
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_triple
|
|
392
|
+
ON object_metadata(
|
|
393
|
+
triple_subject_qid,
|
|
394
|
+
triple_predicate,
|
|
395
|
+
triple_object_qid
|
|
396
|
+
);
|
|
397
|
+
`,Ud=`
|
|
66
398
|
id INTEGER PRIMARY KEY,
|
|
67
399
|
archive_id INTEGER NOT NULL,
|
|
68
400
|
kind INTEGER NOT NULL,
|
|
@@ -72,14 +404,14 @@ var i=(e,t)=>()=>(e&&(t=e(e=0)),t);import{AsyncLocalStorage as cd}from"async_hoo
|
|
|
72
404
|
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
73
405
|
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
74
406
|
UNIQUE(archive_id, kind, chapter_id, sentence_index)
|
|
75
|
-
`,
|
|
407
|
+
`,Hd=`
|
|
76
408
|
CREATE TABLE IF NOT EXISTS search_index_state (
|
|
77
409
|
key TEXT PRIMARY KEY,
|
|
78
410
|
value TEXT NOT NULL
|
|
79
411
|
);
|
|
80
412
|
|
|
81
413
|
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
82
|
-
${
|
|
414
|
+
${Ud}
|
|
83
415
|
);
|
|
84
416
|
|
|
85
417
|
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
@@ -136,7 +468,46 @@ ${pc}
|
|
|
136
468
|
updated_at INTEGER NOT NULL,
|
|
137
469
|
PRIMARY KEY (archive_id, chapter_id)
|
|
138
470
|
);
|
|
139
|
-
`});var
|
|
471
|
+
`});var ot=i(()=>{"use strict";y();be()});var qd=i(()=>{"use strict";y();ot()});var $d=i(()=>{"use strict";y()});var Kd=i(()=>{"use strict";y();mr()});var Jd=i(()=>{"use strict";y();be()});var zd=i(()=>{"use strict";y();ot()});var Xd=i(()=>{"use strict";y();ot()});var Qd=i(()=>{"use strict";y();ot()});var Vd=i(()=>{"use strict";y();ot()});var Yd=i(()=>{"use strict";y();ot()});var tu=i(()=>{"use strict";fe();y();pn()});var nu=i(()=>{"use strict";y()});var ru=i(()=>{"use strict";y()});var iu=i(()=>{"use strict";y()});var au=i(()=>{"use strict";nu();ru();iu()});var ka=i(()=>{"use strict";qd();$d();Kd();Jd();zd();Xd();Qd();Vd();Yd();tu();au()});var Ra=i(()=>{"use strict"});var Su=i(()=>{"use strict";p()});var wu=i(()=>{"use strict";ee();tn()});var st=i(()=>{"use strict"});var Eu=i(()=>{"use strict";y();fn();st()});var xu=i(()=>{"use strict";ee();Ra()});var Iu=i(()=>{"use strict"});import{z as va}from"zod";var UA,vu=i(()=>{"use strict";p();rn();oa();hn();y();ea();fn();ka();be();Ra();Su();wu();Eu();tn();xu();Iu();UA=va.object({mediaType:va.string().min(1),path:va.string()})});var _u=i(()=>{"use strict"});var _a=i(()=>{"use strict";vu();_u()});var Cu=i(()=>{"use strict";ia()});var k=i(()=>{"use strict";y();qc();ea();ia();_a();Cu();fn();pn();ka();be()});var Ca=i(()=>{"use strict"});var _e=i(()=>{"use strict";Ca()});async function Ou(e){let t=await Nb(e);try{return await Ob(t,e),await t.queryOne(`
|
|
472
|
+
SELECT 1 AS active
|
|
473
|
+
FROM state_locks
|
|
474
|
+
WHERE scope = ? AND resource_key = ?
|
|
475
|
+
LIMIT 1
|
|
476
|
+
`,[e.scope,e.resourceKey],()=>!0)===!0}finally{await t.close()}}async function Ob(e,t){let n=[],r=[];t.scope!==void 0&&(n.push("scope = ?"),r.push(t.scope)),t.resourceKey!==void 0&&(n.push("resource_key = ?"),r.push(t.resourceKey));let a=await e.queryAll(`
|
|
477
|
+
SELECT scope, resource_key, mode, owner_id, owner_pid, heartbeat_at
|
|
478
|
+
FROM state_locks
|
|
479
|
+
${n.length===0?"":`WHERE ${n.join(" AND ")}`}
|
|
480
|
+
`,r,o=>({lock:Db(o),resourceKey:S(o,"resource_key"),scope:S(o,"scope")}));for(let{lock:o,resourceKey:s,scope:c}of a)Mb(o,t.staleMs)&&await e.run(`
|
|
481
|
+
DELETE FROM state_locks
|
|
482
|
+
WHERE scope = ?
|
|
483
|
+
AND resource_key = ?
|
|
484
|
+
AND owner_id = ?
|
|
485
|
+
AND heartbeat_at = ?
|
|
486
|
+
`,[c,s,o.ownerId,o.heartbeatAt])}async function Nb(e){if(e.databaseFile!==void 0)return await N.open(e.databaseFile,Lu);if(e.stateDatabaseName!==void 0)return await B(e.stateDatabaseName,Lu);throw new Error("State lock requires a database File or state database name.")}function Db(e){return{heartbeatAt:h(e,"heartbeat_at"),mode:Fb(S(e,"mode")),ownerId:S(e,"owner_id")}}function Fb(e){return e==="read"||e==="write"?e:"write"}function Mb(e,t=Lb){return Date.now()-e.heartbeatAt>t}var Lu,Lb,Nu=i(()=>{"use strict";O();y();k();Lu=`
|
|
487
|
+
CREATE TABLE IF NOT EXISTS state_locks (
|
|
488
|
+
scope TEXT NOT NULL,
|
|
489
|
+
resource_key TEXT NOT NULL,
|
|
490
|
+
mode TEXT NOT NULL,
|
|
491
|
+
owner_id TEXT NOT NULL,
|
|
492
|
+
owner_pid INTEGER NOT NULL,
|
|
493
|
+
heartbeat_at INTEGER NOT NULL,
|
|
494
|
+
created_at INTEGER NOT NULL,
|
|
495
|
+
PRIMARY KEY (scope, resource_key, owner_id)
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
CREATE INDEX IF NOT EXISTS idx_state_locks_resource
|
|
499
|
+
ON state_locks(scope, resource_key);
|
|
500
|
+
`,Lb=300*1e3});async function Fu(e){return await Ou({stateDatabaseName:"core.sqlite",resourceKey:Gb(e),scope:jb,staleMs:Du})||await Bb(e)}function Gb(e){return String(e)}async function Bb(e){let t=await Ub();try{let n=await t.queryOne("SELECT owner_pid, heartbeat_at FROM library_locks WHERE library_id = ?",[e],r=>({heartbeatAt:h(r,"heartbeat_at"),ownerPid:h(r,"owner_pid")}));return n!==void 0&&qb(n)}finally{await t.close()}}async function Ub(){let e=await B("core.sqlite",Wb);return await Hb(e),e}async function Hb(e){new Set(await e.queryAll("PRAGMA table_info(library_locks)",void 0,n=>S(n,"name"))).has("heartbeat_at")||await e.run("ALTER TABLE library_locks ADD COLUMN heartbeat_at INTEGER NOT NULL DEFAULT 0")}function qb(e){return Date.now()-e.heartbeatAt<=Du}var Wb,jb,Du,yn=i(()=>{"use strict";y();k();Nu();Wb=`
|
|
501
|
+
CREATE TABLE IF NOT EXISTS library_locks (
|
|
502
|
+
library_id INTEGER PRIMARY KEY,
|
|
503
|
+
mode TEXT NOT NULL,
|
|
504
|
+
owner_id TEXT NOT NULL,
|
|
505
|
+
owner_pid INTEGER NOT NULL,
|
|
506
|
+
heartbeat_at INTEGER NOT NULL,
|
|
507
|
+
created_at INTEGER NOT NULL
|
|
508
|
+
);
|
|
509
|
+
`,jb="library",Du=300*1e3});var TP,vt=i(()=>{"use strict";p();y();k();_e();fe();O();yn();Ca();TP=Promise.resolve()});var Te=i(()=>{"use strict"});var Ge=i(()=>{"use strict";Te()});import{z as La}from"zod";var bn,vr,Oa,FP,Be=i(()=>{"use strict";Te();bn=1,vr=4,Oa=`${JSON.stringify({formatVersion:bn,schemaVersion:vr})}
|
|
510
|
+
`,FP=La.object({formatVersion:La.literal(bn),schemaVersion:La.number().int().positive().optional()})});var Bu=i(()=>{"use strict";p();Ge();Te();Be()});var Tr=i(()=>{"use strict";p();Te();Be();Ge()});var Hu=i(()=>{"use strict";Te()});var $u=i(()=>{"use strict";p();Te();Hu();Be();Ge()});var zu=i(()=>{"use strict";Bu();Tr();Be();$u()});var al=i(()=>{"use strict";_e()});var ol=i(()=>{"use strict"});var Cr=i(()=>{"use strict"});var En=i(()=>{"use strict";qi();$t()});var Ga=i(()=>{"use strict";p();Cr();En()});var sl=i(()=>{"use strict";En()});var cl=i(()=>{"use strict";Cr();En()});var Ba=i(()=>{"use strict";En()});var dl=i(()=>{"use strict";Ga();sl();cl();Ba();Cr()});var ul=i(()=>{"use strict";Ga();dl();Ba()});var ll=i(()=>{"use strict";ol();ul()});var Ar=i(()=>{"use strict";ll();Ce()});var nO,xn=i(()=>{"use strict";nO=new RegExp("\\p{Script=Han}","u")});var Ua=i(()=>{"use strict";fe()});var Ha,ct=i(()=>{"use strict";Ha=`
|
|
140
511
|
CREATE TABLE IF NOT EXISTS search_sessions (
|
|
141
512
|
session_id TEXT PRIMARY KEY,
|
|
142
513
|
archive_key TEXT NOT NULL,
|
|
@@ -240,43 +611,46 @@ ON search_sessions(expires_at);
|
|
|
240
611
|
|
|
241
612
|
CREATE INDEX IF NOT EXISTS idx_search_sessions_prune
|
|
242
613
|
ON search_sessions(accessed_at DESC, created_at DESC, session_id);
|
|
243
|
-
`});
|
|
614
|
+
`});async function Tt(){let e=await B("cache/search-sessions.sqlite",Ha);return qa?e:await iS(e)?(qa=!0,e):(await rS(e),qa=!0,e)}async function rS(e){await e.execute(`
|
|
615
|
+
DROP TABLE IF EXISTS search_evidence_hit_events;
|
|
616
|
+
DROP TABLE IF EXISTS search_triple_hits;
|
|
617
|
+
DROP TABLE IF EXISTS search_entity_hits;
|
|
618
|
+
DROP TABLE IF EXISTS search_chunk_hits;
|
|
619
|
+
DROP TABLE IF EXISTS search_sessions;
|
|
620
|
+
${Ha}
|
|
621
|
+
`)}async function iS(e){return await Pr(e,"search_entity_hits","archive_id")&&await Pr(e,"search_chunk_hits","archive_id")&&await Pr(e,"search_triple_hits","archive_id")&&await Pr(e,"search_evidence_hit_events","archive_id")}async function Pr(e,t,n){return(await e.queryAll(`PRAGMA table_info(${t})`,void 0,a=>String(a.name))).includes(n)}var qa,Ct=i(()=>{"use strict";k();ct();qa=!1});var Lr=i(()=>{"use strict"});async function hl(e,t){await fl(e,t),await e.run("DELETE FROM search_sessions WHERE session_id = ?",[t])}async function fl(e,t){await e.run("DELETE FROM search_results WHERE session_id = ?",[t]),await e.run("DELETE FROM search_evidence_hit_events WHERE session_id = ?",[t]),await e.run("DELETE FROM search_entity_hits WHERE session_id = ?",[t]),await e.run("DELETE FROM search_triple_hits WHERE session_id = ?",[t]),await e.run("DELETE FROM search_chunk_hits WHERE session_id = ?",[t])}async function yl(e){await e.run(`
|
|
244
622
|
DELETE FROM predicate_dictionary
|
|
245
623
|
WHERE NOT EXISTS (
|
|
246
624
|
SELECT 1
|
|
247
625
|
FROM search_triple_hits
|
|
248
626
|
WHERE search_triple_hits.predicate_id = predicate_dictionary.id
|
|
249
627
|
)
|
|
250
|
-
`)}var
|
|
628
|
+
`)}var $a=i(()=>{"use strict";y();Ct();Lr();ct()});async function Or(e){let t=await Tt();try{let n=await gl(t),r=await t.queryOne("SELECT COUNT(*) AS count FROM search_sessions",void 0,d=>h(d,"count")),a=e.force?await cS(t):await sS(t,e.now),o=e.force?[]:await dS(t),s=[...new Set([...a,...o])].sort();!e.dryRun&&s.length>0&&(await t.transaction(async()=>{for(let d of s)await hl(t,d);await yl(t)}),await t.run("VACUUM"));let c=await gl(t);return{freedBytes:Math.max(0,n-c),removed:s.length,scanned:r??0}}finally{await t.close()}}async function sS(e,t){return await e.queryAll(`
|
|
251
629
|
SELECT session_id
|
|
252
630
|
FROM search_sessions
|
|
253
631
|
WHERE expires_at < ?
|
|
254
|
-
`,[t],n=>
|
|
632
|
+
`,[t],n=>S(n,"session_id"))}async function cS(e){return await e.queryAll("SELECT session_id FROM search_sessions",void 0,t=>S(t,"session_id"))}async function dS(e){return await e.queryAll(`
|
|
255
633
|
SELECT session_id
|
|
256
634
|
FROM search_sessions
|
|
257
635
|
ORDER BY accessed_at DESC, created_at DESC, session_id
|
|
258
636
|
LIMIT -1 OFFSET ?
|
|
259
|
-
`,[500],t=>
|
|
260
|
-
SELECT value
|
|
261
|
-
FROM search_index_state
|
|
262
|
-
WHERE key = 'fingerprint'
|
|
263
|
-
`,void 0,t=>String(t.value))}var vr=i(()=>{"use strict"});var Na=i(()=>{"use strict";Ye()});var Tr=i(()=>{"use strict";f();Na()});var Lu=i(()=>{"use strict";Oe();vr();Tr();Ye()});var Nu=i(()=>{"use strict";Ye()});var Du=i(()=>{"use strict";Oe()});var Mu=i(()=>{"use strict"});var ja=i(()=>{"use strict";f();Mu();vr()});var Fu=i(()=>{"use strict";f();Oe();Du();Na();Ye();ja();Tr()});var Wu=i(()=>{"use strict";Lu();Nu();vr();Fu();ja();Ye();Tr()});var De=i(()=>{"use strict";Wu();Oe()});var nt=i(()=>{"use strict";W();De();Oe()});var O=i(()=>{"use strict";Le();v()});var Q=i(()=>{"use strict";Le();v()});var se=i(()=>{"use strict";Je();v();O()});var V=i(()=>{"use strict";W();De();v();O();Q();se();v();nt()});var pn=i(()=>{"use strict";et();De();V();Q()});var Ha=i(()=>{"use strict";W();Bt();v();O();se();Q()});var hn=i(()=>{"use strict"});var ce=i(()=>{"use strict";W();hn();v();O();se()});var fn=i(()=>{"use strict";se()});var yn=i(()=>{"use strict";O();se();ce()});var qa=i(()=>{"use strict";v();ce();fn();yn()});var Yu=i(()=>{"use strict";De();nt();v();V();ce();fn();qa();yn();yn();fn()});var Zu=i(()=>{"use strict";Yu();qa();fn();yn()});var Sn=i(()=>{"use strict";Zu()});var tl=i(()=>{"use strict";et();v();O();se();ce();Q();Sn()});var En=i(()=>{"use strict";O();ce();v();Q();Sn();tl()});var Ja=i(()=>{"use strict";W();v();O();se();ce();En();In();Q()});var In=i(()=>{"use strict";v();O();se();Q();Ja()});var Nr=i(()=>{"use strict";v();V();Q()});var Dr=i(()=>{"use strict";v();V()});var Mr=i(()=>{"use strict";W();Bt();et();De();v();pn();Ha();En();In();V();Nr();Dr()});var ol=i(()=>{"use strict";Bt();et();v();nt();pn();Ha();En();In();Mr();V()});var sl=i(()=>{"use strict";ol();Nr();Dr();Mr();pn();V()});var Fr=i(()=>{"use strict";ce();Sn();O()});var xn=i(()=>{"use strict";v()});var Wr=i(()=>{"use strict";v();nt();O();V();pn();De();xn()});var Xa=i(()=>{"use strict";v();Q();Fr();Wr();xn()});import{stat as ES}from"fs/promises";async function Qa(e){let t=Gt(),n=await dl(t);if(n===void 0)return{freedBytes:0,removed:0,scanned:0};let r=await _(t,IS);try{await RS(r);let a=await TS(r),o=new Date(e.now-xS).toISOString(),s=await CS(r,o);!e.dryRun&&s>0&&(await r.transaction(async()=>{await r.run("DELETE FROM qid_cache WHERE checked_at < ?",[o]),await r.run("DELETE FROM disambiguation_cache WHERE checked_at < ?",[o])}),await r.run("VACUUM"));let d=await dl(t);return{freedBytes:e.dryRun?kS(n,a,s):Math.max(0,n-(d??0)),removed:s,scanned:a}}finally{await r.close()}}function kS(e,t,n){return t===0||n===0?0:Math.round(e*(n/t))}async function RS(e){await vS(e),await _S(e)}async function vS(e){(await kn(e,"qid_cache")).has("language")||await e.transaction(async()=>{(await kn(e,"qid_cache")).has("language")||(await e.run("ALTER TABLE qid_cache RENAME TO qid_cache_legacy"),await e.run(cl),await e.run(`
|
|
637
|
+
`,[500],t=>S(t,"session_id"))}async function gl(e){let t=await e.queryOne("PRAGMA page_count",void 0,r=>h(r,"page_count"))??0,n=await e.queryOne("PRAGMA page_size",void 0,r=>h(r,"page_size"))??0;return t*n}var bl=i(()=>{"use strict";y();Ct();ct();$a()});var Sl=i(()=>{"use strict";ct()});var Ka=i(()=>{"use strict"});var Nr=i(()=>{"use strict";y();Ct();Lr();Sl();Ka()});var El=i(()=>{"use strict";O();ct()});var xl=i(()=>{"use strict";y();Ua();Ct();Nr();El();Lr();ct();$a()});var Il=i(()=>{"use strict";y();Ct();Nr()});var dt=i(()=>{"use strict";Ua();bl();Nr();xl();Il();Ka()});var Fr=i(()=>{"use strict"});var Pl=i(()=>{"use strict"});var za=i(()=>{"use strict";fe()});var Mr=i(()=>{"use strict"});var Xa=i(()=>{"use strict";Mr()});var Qa=i(()=>{"use strict";Mr()});var Wr=i(()=>{"use strict";Fr();za();Xa();Qa()});var Ml=i(()=>{"use strict";_e()});var Wl=i(()=>{"use strict";Fr();Pl();za();Mr();Wr();Xa();Qa();Ml()});var C=i(()=>{"use strict";Wl()});var Ya=i(()=>{"use strict"});var Za=i(()=>{"use strict";hn();wa();Ya()});var jl=i(()=>{"use strict";Za()});var $=i(()=>{"use strict";Za();jl();Ya()});var SD,wD,Ue=i(()=>{"use strict";O();SD=new RegExp("\\p{Script=Han}+","gu"),wD=new RegExp("^\\p{Script=Han}+$","u")});var Gr=i(()=>{"use strict";O()});var to=i(()=>{"use strict";st()});var Ur=i(()=>{"use strict";y();to()});var Jl=i(()=>{"use strict";Ue();Gr();Ur();st()});var Xl=i(()=>{"use strict";st()});var Ql=i(()=>{"use strict";Ue()});var Vl=i(()=>{"use strict"});var oo=i(()=>{"use strict";y();Vl();Gr()});var Zl=i(()=>{"use strict";y();Ue();Ql();to();st();oo();Ur()});var em=i(()=>{"use strict";Jl();Xl();Gr();Zl();oo();st();Ur()});var qe=i(()=>{"use strict";em();Ue()});var lt=i(()=>{"use strict";$();qe();Ue()});var z=i(()=>{"use strict";se();_e();C()});var de=i(()=>{"use strict";_e();C()});var ue=i(()=>{"use strict";$();nt();C();z()});var le=i(()=>{"use strict";$();qe();C();z();de();ue();C();lt()});var Tn=i(()=>{"use strict";dt();qe();le();de()});var ho=i(()=>{"use strict";$();xn();C();z();ue();de()});var Cn=i(()=>{"use strict"});var xe=i(()=>{"use strict";$();Cn();C();z();ue()});var An=i(()=>{"use strict";ue()});var Pn=i(()=>{"use strict";ue();xe()});var fo=i(()=>{"use strict";C();xe();An();Pn()});var hm=i(()=>{"use strict";qe();lt();C();le();xe();An();fo();Pn();Pn();An()});var fm=i(()=>{"use strict";hm();fo();An();Pn()});var Nn=i(()=>{"use strict";fm()});var gm=i(()=>{"use strict";dt();C();z();ue();xe();de();Nn()});var Dn=i(()=>{"use strict";z();xe();C();de();Nn();gm()});var bo=i(()=>{"use strict";$();C();z();ue();xe();Dn();Fn();de()});var Fn=i(()=>{"use strict";C();z();ue();de();bo()});var Xr=i(()=>{"use strict";C();le();de()});var Qr=i(()=>{"use strict";C();le()});var Vr=i(()=>{"use strict";$();xn();dt();qe();C();Tn();ho();Dn();Fn();le();Xr();Qr()});var xm=i(()=>{"use strict";xn();dt();C();lt();Tn();ho();Dn();Fn();Vr();le()});var Im=i(()=>{"use strict";xm();Xr();Qr();Vr();Tn();le()});var Yr=i(()=>{"use strict";xe();Nn();z()});var Mn=i(()=>{"use strict";C()});var Zr=i(()=>{"use strict";C();lt();z();le();Tn();qe();Mn()});var wo=i(()=>{"use strict";C();de();Yr();Zr();Mn()});async function Eo(e){let t=await B("cache/cache.sqlite",rw);try{await ow(t);let n=await km(t),r=await dw(t),a=new Date(e.now-iw).toISOString(),o=await uw(t,a);!e.dryRun&&o>0&&(await t.transaction(async()=>{await t.run("DELETE FROM qid_cache WHERE checked_at < ?",[a]),await t.run("DELETE FROM disambiguation_cache WHERE checked_at < ?",[a])}),await t.run("VACUUM"));let s=await km(t);return{freedBytes:e.dryRun?aw(n,r,o):Math.max(0,n-s),removed:o,scanned:r}}finally{await t.close()}}function aw(e,t,n){return t===0||n===0?0:Math.round(e*(n/t))}async function ow(e){await sw(e),await cw(e)}async function sw(e){(await Wn(e,"qid_cache")).has("language")||await e.transaction(async()=>{(await Wn(e,"qid_cache")).has("language")||(await e.run("ALTER TABLE qid_cache RENAME TO qid_cache_legacy"),await e.run(Rm),await e.run(`
|
|
264
638
|
INSERT OR IGNORE INTO qid_cache (
|
|
265
639
|
qid, language, label, description, pages_json, checked_at, updated_at
|
|
266
640
|
)
|
|
267
641
|
SELECT qid, 'en', label, description, pages_json, checked_at, updated_at
|
|
268
642
|
FROM qid_cache_legacy
|
|
269
|
-
`),await e.run("DROP TABLE qid_cache_legacy"))})}async function
|
|
643
|
+
`),await e.run("DROP TABLE qid_cache_legacy"))})}async function cw(e){(await Wn(e,"disambiguation_cache")).has("wiki")||await e.transaction(async()=>{(await Wn(e,"disambiguation_cache")).has("wiki")||(await e.run("ALTER TABLE disambiguation_cache RENAME TO disambiguation_cache_legacy"),await e.run(vm),await e.run(`
|
|
270
644
|
INSERT OR IGNORE INTO disambiguation_cache (
|
|
271
645
|
qid, wiki, pages_json, profile_json, checked_at
|
|
272
646
|
)
|
|
273
647
|
SELECT qid, 'enwiki', pages_json, profile_json, checked_at
|
|
274
648
|
FROM disambiguation_cache_legacy
|
|
275
|
-
`),await e.run("DROP TABLE disambiguation_cache_legacy"))}),(await
|
|
649
|
+
`),await e.run("DROP TABLE disambiguation_cache_legacy"))}),(await Wn(e,"disambiguation_cache")).has("profile_error_json")||await e.run("ALTER TABLE disambiguation_cache ADD COLUMN profile_error_json TEXT")}async function Wn(e,t){let n=await e.queryAll(`PRAGMA table_info(${t})`,void 0,r=>lw(r.name,"name"));return new Set(n)}async function dw(e){let t=await ei(e,"qid_cache"),n=await ei(e,"disambiguation_cache");return t+n}async function uw(e,t){let n=await ei(e,"qid_cache",t),r=await ei(e,"disambiguation_cache",t);return n+r}async function ei(e,t,n){return await e.queryOne(`
|
|
276
650
|
SELECT COUNT(*) AS count
|
|
277
651
|
FROM ${t}
|
|
278
652
|
${n===void 0?"":"WHERE checked_at < ?"}
|
|
279
|
-
`,n===void 0?void 0:[n],r=>
|
|
653
|
+
`,n===void 0?void 0:[n],r=>h(r,"count"))??0}async function km(e){let t=await e.queryOne("PRAGMA page_count",void 0,r=>h(r,"page_count"))??0,n=await e.queryOne("PRAGMA page_size",void 0,r=>h(r,"page_size"))??0;return t*n}function lw(e,t){if(typeof e!="string")throw new Error(`Expected ${t} to be a string.`);return e}var Rm,vm,rw,iw,xo=i(()=>{"use strict";y();k();p();Rm=`
|
|
280
654
|
CREATE TABLE IF NOT EXISTS qid_cache (
|
|
281
655
|
qid TEXT NOT NULL,
|
|
282
656
|
language TEXT NOT NULL,
|
|
@@ -287,7 +661,7 @@ CREATE TABLE IF NOT EXISTS qid_cache (
|
|
|
287
661
|
updated_at TEXT NOT NULL,
|
|
288
662
|
PRIMARY KEY (qid, language)
|
|
289
663
|
);
|
|
290
|
-
`,
|
|
664
|
+
`,vm=`
|
|
291
665
|
CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
292
666
|
qid TEXT NOT NULL,
|
|
293
667
|
wiki TEXT NOT NULL,
|
|
@@ -297,130 +671,47 @@ CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
|
297
671
|
checked_at TEXT NOT NULL,
|
|
298
672
|
PRIMARY KEY (qid, wiki)
|
|
299
673
|
);
|
|
300
|
-
`,
|
|
301
|
-
${
|
|
674
|
+
`,rw=`
|
|
675
|
+
${Rm}
|
|
302
676
|
|
|
303
677
|
CREATE INDEX IF NOT EXISTS idx_qid_cache_checked_at
|
|
304
678
|
ON qid_cache(checked_at);
|
|
305
679
|
|
|
306
|
-
${
|
|
680
|
+
${vm}
|
|
307
681
|
|
|
308
682
|
CREATE INDEX IF NOT EXISTS idx_disambiguation_cache_checked_at
|
|
309
683
|
ON disambiguation_cache(checked_at);
|
|
310
|
-
`,
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
`,
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
DELETE FROM entry_locks
|
|
328
|
-
WHERE owner_pid IN (${Un(n.size)})
|
|
329
|
-
`,[...n]),await e.run(`
|
|
330
|
-
DELETE FROM entry_sqlite_leases
|
|
331
|
-
WHERE owner_pid IN (${Un(n.size)})
|
|
332
|
-
`,[...n]),await e.run(`
|
|
333
|
-
DELETE FROM archive_commit_locks
|
|
334
|
-
WHERE owner_pid IN (${Un(n.size)})
|
|
335
|
-
`,[...n]))}function Un(e){return Array.from({length:e},()=>"?").join(", ")}function xE(e){try{return process.kill(e,0),!0}catch(t){return!(t instanceof Error&&"code"in t&&t.code==="ESRCH")}}async function F(e){return await IE.use(async()=>{let t=await kE();try{return await e(t)}finally{await t.close()}})}async function kE(){let e=await _(wE(fs(),"staging.sqlite"),EE);return await RE(e),e}async function RE(e){let t=await e.queryAll("PRAGMA table_info(entry_overlays)",void 0,r=>B(r,"name"));t.includes("archive_signature")||await e.run(`
|
|
336
|
-
ALTER TABLE entry_overlays
|
|
337
|
-
ADD COLUMN archive_signature TEXT
|
|
338
|
-
`),t.includes("mutation_token")||await e.run(`
|
|
339
|
-
ALTER TABLE entry_overlays
|
|
340
|
-
ADD COLUMN mutation_token TEXT
|
|
341
|
-
`),(await e.queryAll("PRAGMA table_info(entry_sqlite_leases)",void 0,r=>B(r,"name"))).includes("mode")||await e.run(`
|
|
342
|
-
ALTER TABLE entry_sqlite_leases
|
|
343
|
-
ADD COLUMN mode TEXT NOT NULL DEFAULT 'read'
|
|
344
|
-
`)}function fs(){return $()}function At(e){let t=ps(e,"workspace_path"),n=ps(e,"archive_signature"),r=ps(e,"mutation_token");return{archiveKey:B(e,"archive_key"),archivePath:B(e,"archive_path"),...n===void 0?{}:{archiveSignature:n},entryPath:B(e,"entry_path"),kind:_E(e),...r===void 0?{}:{mutationToken:r},updatedAt:Ie(e,"updated_at"),...t===void 0?{}:{workspacePath:t}}}function Wp(e){return{entryPath:B(e,"entry_path"),mode:vE(e),ownerId:B(e,"owner_id")}}function vE(e){let t=B(e,"mode");if(t==="read"||t==="state"||t==="write")return t;throw new Error(`Unsupported entry lock mode: ${t}.`)}function _E(e){let t=B(e,"kind");if(t==="deleted"||t==="file")return t;throw new Error(`Unsupported entry overlay kind: ${t}.`)}function B(e,t){let n=e[t];if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}function Ie(e,t){let n=e[t];if(typeof n!="number")throw new TypeError(`Expected ${t} to be a number`);return n}function ps(e,t){let n=e[t];if(n!=null){if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}}var EE,IE,xe=i(()=>{"use strict";D();x();Cn();ee();EE=`
|
|
345
|
-
CREATE TABLE IF NOT EXISTS entry_overlays (
|
|
346
|
-
archive_key TEXT NOT NULL,
|
|
347
|
-
archive_path TEXT NOT NULL,
|
|
348
|
-
entry_path TEXT NOT NULL,
|
|
349
|
-
kind TEXT NOT NULL,
|
|
350
|
-
workspace_path TEXT,
|
|
351
|
-
archive_signature TEXT,
|
|
352
|
-
mutation_token TEXT,
|
|
353
|
-
updated_at INTEGER NOT NULL,
|
|
354
|
-
PRIMARY KEY (archive_key, entry_path)
|
|
355
|
-
);
|
|
356
|
-
|
|
357
|
-
CREATE TABLE IF NOT EXISTS entry_locks (
|
|
358
|
-
archive_key TEXT NOT NULL,
|
|
359
|
-
entry_path TEXT NOT NULL,
|
|
360
|
-
mode TEXT NOT NULL,
|
|
361
|
-
owner_id TEXT NOT NULL,
|
|
362
|
-
owner_pid INTEGER NOT NULL,
|
|
363
|
-
heartbeat_at INTEGER NOT NULL,
|
|
364
|
-
created_at INTEGER NOT NULL,
|
|
365
|
-
PRIMARY KEY (archive_key, entry_path, owner_id)
|
|
366
|
-
);
|
|
367
|
-
|
|
368
|
-
CREATE TABLE IF NOT EXISTS archive_owners (
|
|
369
|
-
archive_key TEXT NOT NULL,
|
|
370
|
-
owner_id TEXT NOT NULL,
|
|
371
|
-
owner_pid INTEGER NOT NULL,
|
|
372
|
-
heartbeat_at INTEGER NOT NULL,
|
|
373
|
-
created_at INTEGER NOT NULL,
|
|
374
|
-
PRIMARY KEY (archive_key, owner_id)
|
|
375
|
-
);
|
|
684
|
+
`,iw=720*60*60*1e3});var Io=i(()=>{"use strict"});var ko=i(()=>{"use strict"});import{jsonrepair as _G}from"jsonrepair";var _m=i(()=>{"use strict"});var Tm=i(()=>{"use strict";ko();Io();_m()});var re=i(()=>{"use strict";Io();ko();Tm()});import{z as ie}from"zod";var fw,QG,Cm=i(()=>{"use strict";re();fw=ie.object({category:ie.string().min(1).optional(),information:ie.string(),name:ie.string().min(1),priority:ie.enum(["primary","secondary","other"]),qid:ie.union([ie.string(),ie.null()])}).strict(),QG=ie.object({meanings:ie.array(fw),sourceQid:ie.string().regex(/^Q[1-9]\d*$/u),surface:ie.string().min(1).optional()}).strict()});var _o=i(()=>{"use strict"});var Am=i(()=>{"use strict";p();O();ye();ee()});var ti=i(()=>{"use strict"});import{DomUtils as s0,parseDocument as c0}from"htmlparser2";var To=i(()=>{"use strict";ti()});var Pm=i(()=>{"use strict"});var Co=i(()=>{"use strict";ti()});var Nm=i(()=>{"use strict";ye();ee();_o();To();ti();Pm();Co()});var Ao=i(()=>{"use strict";Nm();To();Co()});var $0,Mm=i(()=>{"use strict";xo();Am();re();Ao();$0=360*60*1e3});var jn=i(()=>{"use strict";xo();Cm();_o();Mm();Ao()});var Lo=i(()=>{"use strict";jn()});var jm=i(()=>{"use strict";Cn();C();xe();Yr();wo();Zr();Mn();Lo();z()});var ni=i(()=>{"use strict";jm();wo();Yr();Zr();Mn();Lo()});var Oo=i(()=>{"use strict";k();_e();$();Cn();C();xe();Nn();Fn();de();ni();z();ue()});var Gm=i(()=>{"use strict";k();$();se();_e();fe();C();ue()});var Bm=i(()=>{"use strict";Oo();ni();z()});var Jo=i(()=>{"use strict";Im();Oo();ni();lt();z();bo();Dn();Gm();Bm()});var qm=i(()=>{"use strict";Jo()});var kw,$m=i(()=>{"use strict";fe();O();y();k();kw=10080*60*1e3});var Km=i(()=>{"use strict"});var Jm=i(()=>{"use strict";y();$m();Km()});var zm=i(()=>{"use strict"});var Xm=i(()=>{"use strict";Jm();zm()});var Pt=i(()=>{"use strict";qm();Xm();dt()});var ht=i(()=>{"use strict"});var HU,Un=i(()=>{"use strict";p();y();Qt();ht();HU=new Ze(1)});var Xo=i(()=>{"use strict";y();O();ht();Un()});var oi=i(()=>{"use strict";p();O()});var Qo=i(()=>{"use strict";O();Un();oi()});var np=i(()=>{"use strict";p();Be();Te();Ge();Tr();ht();Xo();Qo();Un()});var rp=i(()=>{"use strict";ht();Ge()});var ip=i(()=>{"use strict";p();O();Un()});var Zo=i(()=>{"use strict";p();O();Tr();Be();Ge();ht();np();rp();Xo();ip();Qo();oi()});var ap=i(()=>{"use strict";Zo()});async function si(e){let t=I().documentStore,n=0,r=0,a=0,o;try{o=await t.list()}catch(c){if(Ve(c)&&c.code==="ENOENT")return{freedBytes:a,removed:r,scanned:n};throw c}for(let c of o){if(!G(c)||!c.name.startsWith(".wikg-session-"))continue;n+=1;let d=await Le(c);!e.force&&(d===void 0||e.now-d<Aw)||(a+=await pe(c),e.dryRun||await t.remove(c.name,{recursive:!0}),r+=1)}let s=await t.getDirectory(".wikg-cache");if(s!==void 0)for(let c of await s.list())G(c)&&(n+=1,e.force&&(a+=await pe(c),e.dryRun||await s.remove(c.name,{recursive:!0}),r+=1));return{freedBytes:a,removed:r,scanned:n}}var Aw,op=i(()=>{"use strict";p();ee();Aw=3600*1e3});var sp=i(()=>{"use strict";y();p();O();ht();oi()});var es=i(()=>{"use strict";ap();Zo();op();sp()});var ts=i(()=>{"use strict";es()});var dp=i(()=>{"use strict";k();Ar();Pt();ts()});var Ce=i(()=>{"use strict";zu();al();dp();ts()});var qn=i(()=>{"use strict";qe()});async function lp(e){let n=await(await I().library.getDirectory("staging"))?.getDirectory("library");if(n===void 0)return{freedBytes:0,removed:0,scanned:0};let r=await Nw();if(r===void 0)return{freedBytes:0,removed:0,scanned:0};let a=new Set(r.map(u=>String(u))),o=await n.list(),s=0,c=0,d=0;for(let u of o){if(!G(u)||(s+=1,a.has(u.name)))continue;let f=Number(u.name);if(Number.isInteger(f)&&await Fu(f))continue;let x=await pe(u);e.dryRun||await n.remove(u.name,{recursive:!0}),c+=1,d+=x}return{freedBytes:d,removed:c,scanned:s}}async function Nw(){let e=await B("core.sqlite","");try{return await e.queryAll("SELECT id FROM libraries",void 0,t=>h(t,"id"))}catch{return}finally{await e.close()}}var Kn=i(()=>{"use strict";O();p();y();fn();k();Ce();lt();qn();Ue();Jn();vt();yn()});var Fw,S$,Jn=i(()=>{"use strict";p();y();k();_e();Ce();fe();O();vt();Kn();yn();Fw=`
|
|
685
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
686
|
+
library_id INTEGER NOT NULL,
|
|
687
|
+
public_id TEXT NOT NULL,
|
|
688
|
+
relative_path TEXT NOT NULL,
|
|
689
|
+
status TEXT NOT NULL DEFAULT 'present',
|
|
690
|
+
last_seen_mutation_token TEXT,
|
|
691
|
+
last_seen_size INTEGER,
|
|
692
|
+
last_seen_mtime_ms INTEGER,
|
|
693
|
+
last_scanned_at TEXT,
|
|
694
|
+
created_at TEXT NOT NULL,
|
|
695
|
+
updated_at TEXT NOT NULL,
|
|
696
|
+
UNIQUE(library_id, public_id)
|
|
697
|
+
`,S$=`
|
|
698
|
+
CREATE TABLE IF NOT EXISTS library_archives (
|
|
699
|
+
${Fw}
|
|
700
|
+
);
|
|
376
701
|
|
|
377
|
-
CREATE
|
|
378
|
-
|
|
379
|
-
entry_path TEXT NOT NULL,
|
|
380
|
-
mode TEXT NOT NULL DEFAULT 'read',
|
|
381
|
-
owner_id TEXT NOT NULL,
|
|
382
|
-
owner_pid INTEGER NOT NULL,
|
|
383
|
-
heartbeat_at INTEGER NOT NULL,
|
|
384
|
-
created_at INTEGER NOT NULL,
|
|
385
|
-
PRIMARY KEY (archive_key, entry_path, owner_id)
|
|
386
|
-
);
|
|
702
|
+
CREATE INDEX IF NOT EXISTS idx_library_archives_library
|
|
703
|
+
ON library_archives(library_id);
|
|
387
704
|
|
|
388
|
-
CREATE
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
);
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
WHERE archive_key = ?
|
|
399
|
-
`,[e],Wp)).some(d=>d.ownerId!==r&&PE(t,d.entryPath)&&CE(n,d.mode))?!1:(await o.run(`
|
|
400
|
-
INSERT INTO entry_locks (
|
|
401
|
-
archive_key, entry_path, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
402
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
403
|
-
`,[e,t,n,r,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await F(async o=>{await o.run(`
|
|
404
|
-
DELETE FROM entry_locks
|
|
405
|
-
WHERE archive_key = ? AND entry_path = ? AND owner_id = ?
|
|
406
|
-
`,[e,t,r])})};await Ip(100)}}function CE(e,t){return e==="state"||t==="state"?e==="state"&&t==="state":!(e==="read"&&t==="read")}function PE(e,t){return e===t||jp(e,t)||jp(t,e)}function jp(e,t){return e.endsWith("/")&&t.startsWith(e)}var qn=i(()=>{"use strict";Ee();ee();xe()});async function gi(e,t){return await F(async n=>await n.queryOne("SELECT * FROM entry_overlays WHERE archive_key = ? AND entry_path = ?",[e,t],At))}async function bi(e){return await F(async t=>await t.queryAll(`
|
|
407
|
-
SELECT *
|
|
408
|
-
FROM entry_overlays
|
|
409
|
-
WHERE archive_key = ?
|
|
410
|
-
ORDER BY entry_path ASC
|
|
411
|
-
`,[e],At))}async function Si(e,t){await F(async n=>{await n.run("DELETE FROM entry_overlays WHERE archive_key = ? AND entry_path = ?",[e,t])})}var Lt=i(()=>{"use strict";We();Ee();ee();xe()});async function ys(e,t){return await F(async n=>{await le(n);let r=await n.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[e],o=>Ie(o,"count")),a=await n.queryOne(`
|
|
412
|
-
SELECT COUNT(*) AS count
|
|
413
|
-
FROM entry_sqlite_leases
|
|
414
|
-
WHERE archive_key = ?
|
|
415
|
-
AND entry_path = ?
|
|
416
|
-
`,[e,t],o=>Ie(o,"count"));return(r??0)>0||(a??0)>0})}async function gs(e){return await F(async t=>{await le(t);let n=await t.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[e],o=>Ie(o,"count")),r=await t.queryOne("SELECT COUNT(*) AS count FROM entry_sqlite_leases WHERE archive_key = ?",[e],o=>Ie(o,"count")),a=await t.queryOne("SELECT COUNT(*) AS count FROM entry_locks WHERE archive_key = ?",[e],o=>Ie(o,"count"));return(n??0)>0||(r??0)>0||(a??0)>0})}var bs=i(()=>{"use strict";xe()});import{mkdir as cY,readdir as AE}from"fs/promises";import{basename as lY,dirname as mY,join as Gp}from"path";function Ss(){return Gp(fs(),"work")}async function ws(e){let t;try{t=await AE(e,{withFileTypes:!0})}catch(r){if(h(r)&&r.code==="ENOENT")return[];throw r}let n=[];for(let r of t){let a=Gp(e,r.name);r.isDirectory()?n.push(...await ws(a)):r.isFile()&&n.push(a)}return n}var $n=i(()=>{"use strict";b();xe()});import{mkdir as gY,rename as bY,rm as SY}from"fs/promises";import{dirname as EY}from"path";async function Bp(e){if(e.workspacePath===void 0||!await Pt(e.workspacePath))return"missing";if(!await Pt(e.archivePath))return"dirty";try{let t=await OE(e.workspacePath),n=await mi(e.archivePath);return t===void 0?"dirty":e.archiveSignature===n?"current":"dirty"}catch{return"dirty"}}async function OE(e){await K();let t=await k.open(e,"",{readonly:!0});try{return await sn(t)}finally{await t.close()}}var Es=i(()=>{"use strict";x();$e();Tt();We();Ee();bs();ee();qn();Lt();xe();$n()});import{mkdir as HY,readFile as qY,rename as $Y,rm as KY,writeFile as JY}from"fs/promises";import{dirname as XY,posix as QY,resolve as VY}from"path";var Is=i(()=>{"use strict";$e();We();Ee();ee();qn();Lt();Es();$n()});import{mkdir as m2,mkdtemp as p2,rename as h2,rm as f2}from"fs/promises";import{tmpdir as g2}from"os";import{basename as S2,dirname as w2,join as E2,resolve as I2}from"path";var wi=i(()=>{"use strict";We();Ee();ee();qn();Lt();xe()});var Up=i(()=>{"use strict";ee();wi();xe()});import{mkdir as U2,rm as H2,writeFile as q2}from"fs/promises";import{dirname as K2,isAbsolute as J2,relative as z2,resolve as X2}from"path";var xs=i(()=>{"use strict";fe();We();Ee();ee();Is();wi();Up();Lt();$n()});import{rm as s8}from"fs/promises";import{resolve as c8}from"path";var Hp=i(()=>{"use strict";fe();We();Is();xs()});import{readdir as Ei,rm as jE,rmdir as GE,stat as ks}from"fs/promises";import{join as Ii}from"path";function Rs(e){return BE.has(e)}async function xi(e){let t=await Ei(e).catch(r=>{if(!(h(r)&&r.code==="ENOENT"))throw r});if(t===void 0||t.some(r=>!Rs(r)))return 0;let n=0;for(let r of t){let a=Ii(e,r);n+=await j(a),await jE(a,{force:!0,recursive:!0})}return await GE(e).catch(r=>{if(!(h(r)&&r.code==="ENOENT"))throw r}),n}async function ki(e){let t=await Ei(e,{withFileTypes:!0}).catch(o=>{if(h(o)&&o.code==="ENOENT")return[];throw o}),n=0,r=0,a=0;for(let o of t){if(!o.isDirectory())continue;a+=1;let s=Ii(e,o.name),d=await ks(s).catch(m=>{if(!(h(m)&&m.code==="ENOENT"))throw m});if(d===void 0||Date.now()-d.mtimeMs<UE)continue;let c=await xi(s);if(await qp(s)){n+=c;continue}n+=c,r+=1}return{freedBytes:n,removed:r,scanned:a}}async function vs(e){let t=await Ei(e,{withFileTypes:!0}).catch(o=>{if(h(o)&&o.code==="ENOENT")return[];throw o}),n=0,r=0,a=0;for(let o of t){if(!o.isDirectory())continue;let s=Ii(e,o.name),d=await vs(s);n+=d.freedBytes,r+=d.removed,a+=d.scanned+1;let c=await xi(s);if(await qp(s)){n+=c;continue}n+=c,r+=1}return{freedBytes:n,removed:r,scanned:a}}async function j(e){try{let t=await ks(e);if(t.isDirectory()){let n=await Ei(e),r=0;for(let a of n)r+=await j(Ii(e,a));return r}return t.size}catch(t){if(h(t)&&t.code==="ENOENT")return 0;throw t}}async function qp(e){try{return await ks(e),!0}catch(t){if(h(t)&&t.code==="ENOENT")return!1;throw t}}var BE,UE,Jn=i(()=>{"use strict";b();BE=new Set([".DS_Store"]),UE=6e4});import{readdir as qE,rm as Kp}from"fs/promises";import{basename as $E,dirname as KE,join as $p}from"path";async function Ri(e){let t=await JE(),n=0,r=0;for(let s of t){let d=await zE(s,e);d.removed&&(n+=1,r+=d.freedBytes)}let a=await VE(e),o=e.dryRun?{freedBytes:0,removed:0,scanned:0}:await ki(Ss());return{freedBytes:r+a.freedBytes+o.freedBytes,removed:n+a.removed+o.removed,scanned:t.length+a.scanned+o.scanned}}async function JE(){return await F(async e=>(await le(e),await e.queryAll(`
|
|
417
|
-
SELECT overlay.*
|
|
418
|
-
FROM entry_overlays AS overlay
|
|
419
|
-
WHERE overlay.entry_path IN (?, ?, ?)
|
|
420
|
-
AND overlay.kind = 'file'
|
|
421
|
-
AND overlay.workspace_path IS NOT NULL
|
|
422
|
-
ORDER BY overlay.updated_at ASC
|
|
423
|
-
`,[Gn,dt,Bn],At)))}async function zE(e,t){let n=await Hn(e.archiveKey,e.entryPath,"write");try{let r=await Hn(e.archiveKey,e.entryPath,"state");try{let a=await gi(e.archiveKey,e.entryPath);if(a?.workspacePath===void 0||a.workspacePath!==e.workspacePath||!await XE(a,t))return{freedBytes:0,removed:!1};let o=await j(a.workspacePath);return t.dryRun||(await Si(a.archiveKey,a.entryPath),await Kp(a.workspacePath,{force:!0}),o+=await xi(KE(a.workspacePath))),{freedBytes:o,removed:!0}}finally{await r()}}finally{await n()}}async function XE(e,t){return await ys(e.archiveKey,e.entryPath)||e.workspacePath===void 0?!1:await QE(e)?!0:!(!t.force&&t.now-e.updatedAt<36e5||Jp(e.entryPath)&&!t.force)}async function QE(e){return e.workspacePath===void 0||!await Pt(e.workspacePath)||!await Pt(e.archivePath)?!0:Jp(e.entryPath)?await Bp(e)!=="current":!1}function Jp(e){return e===dt||e===Bn}async function VE(e){let t=Ss(),n;try{n=await qE(t,{withFileTypes:!0})}catch(s){if(h(s)&&s.code==="ENOENT")return{freedBytes:0,removed:0,scanned:0};throw s}let r=0,a=0,o=0;for(let s of n){if(!s.isDirectory())continue;let d=s.name;if(await gs(d))continue;let c=new Set((await bi(d)).map(m=>m.workspacePath).filter(m=>m!==void 0));for(let m of await ws($p(t,d))){if(o+=1,Rs($E(m))||c.has(m))continue;let p=await j(m);e.dryRun||await Kp(m,{force:!0}),r+=p,a+=1}if(!e.dryRun){let m=await vs($p(t,d));r+=m.freedBytes,a+=m.removed,o+=m.scanned}}return{freedBytes:r,removed:a,scanned:o}}var zp=i(()=>{"use strict";Jn();b();bs();Ee();ee();qn();Lt();Es();xe();$n()});var Qp=i(()=>{"use strict";Hp();xs();wi();zp()});var _s=i(()=>{"use strict";Qp()});import{resolve as B8}from"path";var Vp=i(()=>{"use strict";x();_s();wt();vi()});var me=i(()=>{"use strict";We();Fp();Vp();_s()});var vi=i(()=>{"use strict";hp();me()});import{rm as r3}from"fs/promises";import{resolve as a3}from"path";var Yp=i(()=>{"use strict";fe();Qe();Qe();x();ge();Vl();vt();dp();vi()});var g3,Cs=i(()=>{"use strict";Nn();g3=Object.freeze({"serial-generation/editor-compress":Object.freeze({temperature:.7,topP:.9}),"serial-generation/editor-review":Object.freeze({temperature:[.3,.95],topP:[.4,.8]}),"serial-generation/editor-review-guide":Object.freeze({temperature:.4,topP:.6}),"serial-generation/reader-choice":Object.freeze({temperature:[.3,.95],topP:[.4,.8]}),"serial-generation/reader-extraction":Object.freeze({temperature:[.3,.95],topP:[.4,.8]})})});var Zp,je=i(()=>{"use strict";Zp=["Recall only source mentions that should become stable searchable knowledge graph objects.","Prefer named entities: historical persons, places, organizations, dynasties, political entities, named works, named events, battles, titles, and explicitly named domain concepts.","Recall a non-named common concept only when it is a central recurring topic of the chapter and would be useful as an entity search result across multiple evidence passages.","Do not recall a surface merely because it is useful for summarizing the story or explaining a detail.","Do not recall pure numbers, standalone dates without a named event role, units of measurement, dimensions, ordinal labels, chapter numbers, punctuation fragments, common function words, generic verbs, generic adjectives, generic roles, generic objects, or incidental attributes.","Measurements, quantities, dimensions, tactical details, and descriptive attributes should remain evidence text, not entity mentions.","When uncertain, skip the mention rather than creating a noisy knowledge graph object."].join(" ")});var Ps=i(()=>{"use strict";je()});var O3,eh=i(()=>{"use strict";Kr();Y();vo();Yp();Cs();me();Ps();O3=$r()});var As,Ls=i(()=>{"use strict";As=["queued","running","canceling","paused","succeeded","failed","canceled"]});import{mkdir as F3}from"fs/promises";import{join as th}from"path";function nh(){return th(ih(),"job.sqlite")}function rh(){return th(ih(),"work")}function ih(){return pt()}var _i=i(()=>{"use strict";D()});import{createHash as U3}from"crypto";import{resolve as q3}from"path";var Nt=i(()=>{"use strict"});var Os=i(()=>{"use strict"});import{appendFile as z3,readFile as X3}from"fs/promises";var Xn=i(()=>{"use strict"});var ah,oh=i(()=>{"use strict";ah=`
|
|
705
|
+
CREATE INDEX IF NOT EXISTS idx_library_archives_library_path
|
|
706
|
+
ON library_archives(library_id, relative_path);
|
|
707
|
+
`});var is=i(()=>{"use strict";Ce();Jn();vt()});var fp=i(()=>{"use strict";Fr();Wr();xn();Qr();Xr();le();Vr();dt();qn();Kn();is()});var yp=i(()=>{"use strict";Jo();Wr();fp();le();Jn();is();vt();Kn()});var ci=i(()=>{"use strict";vt();Jn();yp();Kn();yn()});function di(e){let t=e.trim();return t===""?[]:Array.from(zw.segment(t)).map(n=>n.segment.trim()).filter(n=>n!=="")}function me(e){let t=e.replace(/\s+/gu," ").trim(),n=Xw(t),r="";for(let a of n)if(!Qw(a)){if(!zn(a)){r+=a;continue}r+=a.toLowerCase().normalize("NFD").replace(new RegExp("\\p{M}","gu"),"")}return r}function Xw(e){let t=[],n=0;for(;n<e.length;){let r=e[n];if(r===void 0)break;if(r!==" "){t.push(r),n+=1;continue}if(t.length>=2&&Jw.has(t[t.length-1]??"")&&zn(t[t.length-2]??"")&&n<e.length-1&&zn(e[n+1]??"")){t.pop(),n+=1;continue}let a=t[t.length-1]??"",o=e[n+1]??"";a!==""&&o!==""&&zn(a)&&zn(o)&&t.push(r),n+=1}return t}function zn(e){return e!==""&&new RegExp("\\p{Script=Latin}","u").test(e)&&new RegExp("\\p{L}","u").test(e)}function Qw(e){return/[\p{P}\p{S}]/u.test(e)}var Jw,zw,Xn=i(()=>{"use strict";Jw=new Set(["\u2010","\u2011","\u2012","\u2013","\u2014","\u2015"]),zw=new Intl.Segmenter(void 0,{granularity:"sentence"})});function os(){return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:-1,matchStart:-1,score:0}}function bp(e,t){let n=ui(e,2),r=ui(t,2),a=ui(e,3),o=ui(t,3);return(gp(n,r)+gp(a,o))/2}function ui(e,t){if(e.length<=t)return e===""?new Set:new Set([e]);let n=new Set;for(let r=0;r<=e.length-t;r+=1)n.add(e.slice(r,r+t));return n}function gp(e,t){if(e.size===0||t.size===0)return 0;let n=0;for(let r of e)t.has(r)&&(n+=1);return 2*n/(e.size+t.size)}function Sp(e,t){if(e===t)return 1;if(e===""||t==="")return 0;let n=Array.from({length:t.length+1},(o,s)=>s),r=new Array(t.length+1).fill(0);for(let[o,s]of[...e].entries()){r[0]=o+1;for(let[c,d]of[...t].entries()){let u=(r[c]??0)+1,f=(n[c+1]??0)+1,x=(n[c]??0)+(s===d?0:1);r[c+1]=Math.min(u,f,x)}[n,r]=[r,n]}return 1-(n[t.length]??Math.max(e.length,t.length))/Math.max(e.length,t.length)}function wp(e,t){return 1-Math.abs(e.length-t.length)/Math.max(e.length,t.length)}function Ep(e,t){if(e===t)return 1;let n=e.length,r=t.length;if(n===0||r===0)return 0;let a=Array.from({length:n+1},()=>new Array(r+1).fill(0)),o=0;for(let s=1;s<=n;s+=1)for(let c=1;c<=r;c+=1){if(e[s-1]!==t[c-1])continue;let d=a[s];d!==void 0&&(d[c]=(a[s-1]?.[c-1]??0)+1,o=Math.max(o,d[c]??0))}return 2*o/(n+r)}function xp(e){return` - ${e.occurrenceId} score=${e.score.toFixed(3)}
|
|
708
|
+
prev: ${as(e.prevText)}
|
|
709
|
+
text: ${as(e.text,120)}
|
|
710
|
+
next: ${as(e.nextText)}`}function as(e,t=80){let n=e.replace(/\s+/gu," ").trim();return n===""?"(none)":n.length<=t?n:`${n.slice(0,t-3)}...`}var ss=i(()=>{"use strict"});var cs=i(()=>{"use strict"});function kp(e,t,n){return e.mode==="head_tail"?Zw(e,t):ds(e.text??"",t,n)}function Zw(e,t){let n=e.head??"",r=e.tail??"",a=me(t),o=me(n),s=me(r);if(o===""||s==="")return os();let c=a.indexOf(o),d=a.lastIndexOf(s);if(c!==-1&&d!==-1&&c<=d){let D=o.length+s.length,w=Math.min(1,D/Math.max(1,a.length));return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:d+s.length,matchStart:c,score:.82+.18*w}}let u=ds(n,t,"start_anchor"),f=ds(r,t,"end_anchor"),x=u.matchStart<=f.matchStart?1:.85;return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:Math.max(u.matchEnd,f.matchEnd),matchStart:Math.min(u.matchStart,f.matchStart),score:(u.score+f.score)/2*x}}function ds(e,t,n){let r=e.trim(),a=t.trim(),o=me(r),s=me(a);if(o===""||s==="")return os();if(r===a)return{exactNormalized:!0,exactRaw:!0,exactSubstring:!0,matchEnd:s.length,matchStart:0,score:1};if(o===s)return{exactNormalized:!0,exactRaw:!1,exactSubstring:!1,matchEnd:s.length,matchStart:0,score:.995};if(s.includes(o)){let w=s.indexOf(o),R=o.length/Math.max(1,s.length),g=Ip({candidateNormalized:s,label:n,matchEnd:w+o.length,matchStart:w,queryNormalized:o});return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:w+o.length,matchStart:w,score:Math.min(1,.75+.25*R+g)}}if(o.includes(s)){let w=s.length/Math.max(1,o.length);return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:s.length,matchStart:0,score:Math.min(.92,.78+.14*w)}}let c=0,d=-1,u=-1,f=Math.min(s.length,Math.max(o.length,Math.floor(o.length*1.2))),x=Math.min(f,Math.max(1,Math.floor(o.length*.8))),D=new Set(o);for(let w=x;w<=f;w+=1)for(let R=0;R<=s.length-w;R+=1){let g=s.slice(R,R+w),v=new Set(g),P=new Set([...D,...v]).size,H=[...D].filter(Z=>v.has(Z)).length;if((P===0?0:H/P)<.35)continue;let W=.35*bp(o,g)+.35*Ep(o,g)+.2*Sp(o,g)+.1*wp(o,g)+Ip({candidateNormalized:s,label:n,matchEnd:R+w,matchStart:R,queryNormalized:o});W>c&&(c=Math.min(1,W),d=R,u=R+w)}return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:u,matchStart:d,score:c}}function Ip(e){if(e.matchStart<0||e.matchEnd<0||e.queryNormalized.length<12||e.label==="start_anchor"&&e.matchStart!==0||e.label==="end_anchor"&&e.matchEnd!==e.candidateNormalized.length)return 0;let t=e.queryNormalized.length/Math.max(1,e.candidateNormalized.length);return .08*(.5+.5*Math.min(1,t))}var Rp=i(()=>{"use strict";Xn();ss();cs()});function vp(e){if(e==null)return[void 0,"anchor is required"];if(typeof e=="string"){let o=e.trim();if(o==="")return[void 0,"anchor string is empty"];if(o.includes("...")){let s=o.split("...").map(c=>c.trim()).filter(c=>c!=="");if(s.length>=2){let c=s[0],d=s[s.length-1];return c===void 0||d===void 0?[void 0,"anchor string is empty"]:[{head:c,mode:"head_tail",tail:d},void 0]}}return[{mode:"full",text:o},void 0]}if(typeof e!="object")return[void 0,`expected string or object, got ${typeof e}`];let t=e;if((t.mode==="head_tail"||t.head!==void 0?"head_tail":"full")==="full"){let o=typeof t.text=="string"?t.text.trim():"";return o===""?[void 0,"full anchor requires non-empty 'text'"]:[{mode:"full",text:o},void 0]}let r=typeof t.head=="string"?t.head.trim():"",a=typeof t.tail=="string"?t.tail.trim():"";return r!==""&&a===""?[{mode:"full",text:r},void 0]:r===""&&a!==""?[{mode:"full",text:a},void 0]:r===""||a===""?[void 0,"head_tail anchor requires non-empty 'head' and 'tail'"]:[{head:r,mode:"head_tail",tail:a},void 0]}function _p(e,t){if(e===void 0)return;if(e.mode==="head_tail"){let r=t==="end_anchor"?us(e.tail??"","end_anchor"):us(e.head??"","start_anchor");return r===""?void 0:{mode:"full",text:r}}return{mode:"full",text:us(e.text??"",t)}}function ls(e){return e.mode==="head_tail"?me(e.head??"").length+me(e.tail??"").length:me(e.text??"").length}function us(e,t){let n=di(e);return n.length<=1?e.trim():t==="end_anchor"?n[n.length-1]??e.trim():n[0]??e.trim()}var Tp=i(()=>{"use strict";Xn()});var ft,Lp=i(()=>{"use strict";Rp();Tp();ss();cs();ft=class{resolve(t,n,r){let a=t.start_anchor??t.start,o=t.end_anchor??t.end,[s,c]=this.parseAnchor(a,"start_anchor");if(c!==void 0)return[void 0,c];let d;if(o!==void 0){let[g,v]=this.parseAnchor(o,"end_anchor");if(v!==void 0)return[void 0,v];d=g}let[u,f,x]=this.resolveAnchor({anchor:s,candidateSentenceIds:n,candidateTexts:r,label:"start_anchor"});if(x!==void 0)return[void 0,x];if(u===void 0)return[void 0,{candidates:[],code:"none",fieldName:"start_anchor",message:"start_anchor could not be matched: no candidate selected"}];if(d===void 0)return[{confidence:u.score,sentenceIds:[u.sentenceId],strategy:f},void 0];let[D,w,R]=this.resolveAnchor({anchor:d,candidateSentenceIds:n,candidateTexts:r,label:"end_anchor",minIndex:u.index});return R!==void 0?[void 0,R]:D===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:D.index<u.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${u.occurrenceId} -> ${D.occurrenceId}).`}]:[{confidence:Math.min(u.score,D.score),sentenceIds:n.slice(u.index,D.index+1),strategy:`${f}+${w}`},void 0]}resolveWithOverrides(t){let n=t.evidence.start_anchor??t.evidence.start,r=t.evidence.end_anchor??t.evidence.end,[a,o]=this.parseAnchor(n,"start_anchor");if(o!==void 0)return[void 0,o];let s=t.overrides.start_anchor,[c,d,u]=s===void 0?this.resolveAnchor({anchor:a,candidateSentenceIds:t.candidateSentenceIds,candidateTexts:t.candidateTexts,label:"start_anchor"}):[s,"choice_final",void 0];if(u!==void 0)return[void 0,u];if(c===void 0)return[void 0,{candidates:[],code:"none",fieldName:"start_anchor",message:"start_anchor could not be matched: no candidate selected"}];if(r===void 0)return[{confidence:c.score,sentenceIds:[c.sentenceId],strategy:d},void 0];let[f,x]=this.parseAnchor(r,"end_anchor");if(x!==void 0)return[void 0,x];let D=t.overrides.end_anchor,[w,R,g]=D===void 0?this.resolveAnchor({anchor:f,candidateSentenceIds:t.candidateSentenceIds,candidateTexts:t.candidateTexts,label:"end_anchor",minIndex:c.index}):[D,"choice_final",void 0];return g!==void 0?[void 0,g]:w===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:w.index<c.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${c.occurrenceId} -> ${w.occurrenceId}).`}]:[{confidence:Math.min(c.score,w.score),sentenceIds:t.candidateSentenceIds.slice(c.index,w.index+1),strategy:`${d}+${R}`},void 0]}parseAnchor(t,n){let[r,a]=vp(t);return a!==void 0?[void 0,{candidates:[],code:"invalid_anchor",fieldName:n,message:`Invalid evidence.${n}: ${a}`}]:[_p(r,n),void 0]}resolveAnchor(t){if(t.anchor===void 0)return[void 0,"invalid",{candidates:[],code:"invalid",fieldName:t.label,message:`${t.label} is missing`}];let n=this.rankAnchor(t.anchor,t.candidateSentenceIds,t.candidateTexts,t.label),r=t.minIndex;if(r!==void 0&&(n=n.filter(f=>f.index>=r)),n.length===0)return[void 0,"none",{candidates:[],code:"none",fieldName:t.label,message:`${t.label} could not be matched: no candidates available`}];let a=n.filter(f=>f.exactRaw);if(a.length===1)return[a[0],"exact_raw",void 0];if(a.length>1)return[void 0,"ambiguous_exact_raw",this.#t(t.label,"ambiguous_exact_raw",a)];let o=n.filter(f=>f.exactSubstring);if(o.length===1&&ls(t.anchor)>=8)return[o[0],"exact_substring",void 0];if(o.length>1&&ls(t.anchor)>=8){let f=this.#e(o);return f!==void 0?[f,"exact_substring_scored",void 0]:[void 0,"ambiguous_exact_substring",this.#t(t.label,"ambiguous_exact_substring",o)]}let s=n.filter(f=>f.exactNormalized);if(s.length===1)return[s[0],"exact_normalized",void 0];if(s.length>1){let f=this.#e(s);return f!==void 0?[f,"exact_normalized_scored",void 0]:[void 0,"ambiguous_exact_normalized",this.#t(t.label,"ambiguous_exact_normalized",s)]}let c=n[0];if(c===void 0)return[void 0,"none",{candidates:[],code:"none",fieldName:t.label,message:`${t.label} could not be matched: no candidates available`}];let d=n[1]?.score??0,u=c.score-d;return c.score<.55?[void 0,"low_confidence",{candidates:n.slice(0,3),code:"low_confidence",fieldName:t.label,message:`${t.label} could not be matched confidently. Best candidate score=${c.score.toFixed(3)}.`}]:n.length===1?[c,"auto_top1_single_candidate",void 0]:c.score>=.97?[c,"auto_top1_very_high",void 0]:c.score>=.9&&u>=.07?[c,"auto_top1",void 0]:[void 0,"ambiguous_ranked",this.#t(t.label,"ambiguous_ranked",n)]}rankAnchor(t,n,r,a){let o=[];for(let[s,c]of n.entries()){let d=r[s]??"",u=s>0?r[s-1]??"":"",f=s<r.length-1?r[s+1]??"":"",x=kp(t,d,a);o.push({exactNormalized:x.exactNormalized,exactRaw:x.exactRaw,exactSubstring:x.exactSubstring,index:s,nextText:f,occurrenceId:`S${s+1}`,prevText:u,score:x.score,sentenceId:c,text:d})}return o.sort((s,c)=>s.score!==c.score?c.score-s.score:s.exactRaw!==c.exactRaw?s.exactRaw?-1:1:s.exactSubstring!==c.exactSubstring?s.exactSubstring?-1:1:s.exactNormalized!==c.exactNormalized?s.exactNormalized?-1:1:s.index-c.index),o}#e(t){if(t.length===0)return;if(t.length===1)return t[0];let n=t[0];if(n===void 0)return;let r=t[1]?.score??0;if(n.score>=.9&&n.score-r>=.07)return n}#t(t,n,r){let a=r.slice(0,3),o=a.map(s=>xp(s)).join(`
|
|
711
|
+
`);return{candidates:a,code:n,fieldName:t,message:`${t} is ambiguous. Top candidates:
|
|
712
|
+
${o}
|
|
713
|
+
Please provide a more specific anchor or surrounding context.`}}}});var Op=i(()=>{"use strict"});var Np=i(()=>{"use strict";Lp();Op()});var li,Dp=i(()=>{"use strict";li=["Evidence selection:",'- Return evidence as [{"sentence_id":"S1","quote":"exact short source quote"}].',"- evidence.quote is a locator for the source sentence, not a proof excerpt.","- Choose the shortest exact quote that still uniquely identifies the intended sentence.","- If the quote is too short, it may match multiple sentences; if it is too long, it may include unnecessary text or cross sentence boundaries.","- Do not use a whole sentence by default.","- evidence is an array. Use multiple items when the claim needs multiple source sentences.","- A source sentence means exactly one displayed source line labeled S1, S2, S3, etc.","- Each evidence item must point to exactly one source sentence.","- Each evidence item must quote one continuous span from exactly one labeled source line, for example only from S2.","- Never merge text from two labeled source lines into one evidence quote.","- sentence_id must be one of the sentence IDs shown in the source context.","- quote must be copied from the untagged original source sentence.","- If the source context contains XML-like tags, do not copy the tags into quote; copy only the visible text."].join(`
|
|
714
|
+
`)});var ms=i(()=>{"use strict"});var Fp=i(()=>{"use strict";ms();Xn()});var Lt=i(()=>{"use strict";Np();Dp();ms();Fp();Xn()});var Qn=i(()=>{"use strict"});import{z as Ae}from"zod";var sE,cE,F1,fs=i(()=>{"use strict";re();Qn();sE=Ae.object({candidateId:Ae.string().min(1),decision:Ae.enum(["continue","recall","skip_this_time","never_recall"]),qid:Ae.string().nullable().optional()}).strict(),cE=Ae.object({decisions:Ae.array(sE),groupId:Ae.string().min(1)}).strict(),F1=Ae.object({groups:Ae.array(cE)}).strict()});import{z as Vn}from"zod";var lE,U1,jp=i(()=>{"use strict";re();Qn();fs();lE=Vn.object({decision:Vn.enum(["keep","reject"]),qid:Vn.string().regex(/^Q[1-9][0-9]*$/)}).strict(),U1=Vn.object({qids:Vn.array(lE)}).strict()});var Gp=i(()=>{"use strict";jn()});import{z as Yn}from"zod";var mE,z1,Bp=i(()=>{"use strict";re();mE=Yn.object({note:Yn.string().max(24).optional(),surfaceId:Yn.string().min(1)}).strict(),z1=Yn.object({protectedSurfaces:Yn.array(mE)}).strict()});var ys=i(()=>{"use strict"});var Up=i(()=>{"use strict";ys()});var Hp=i(()=>{"use strict"});var gs=i(()=>{"use strict"});var qp=i(()=>{"use strict";gs();Qn()});var yt=i(()=>{"use strict";fs();jp();Qn();Gp();Bp();Up();ys();Hp();qp();gs()});var RK,bs=i(()=>{"use strict";Yt();RK=Object.freeze({"serial-generation/editor-compress":Object.freeze({temperature:.7,topP:.9}),"serial-generation/editor-review":Object.freeze({temperature:[.3,.95],topP:[.4,.8]}),"serial-generation/editor-review-guide":Object.freeze({temperature:.4,topP:.6}),"serial-generation/reader-choice":Object.freeze({temperature:[.3,.95],topP:[.4,.8]}),"serial-generation/reader-extraction":Object.freeze({temperature:[.3,.95],topP:[.4,.8]})})});var Ss=i(()=>{"use strict";ye()});var Jp=i(()=>{"use strict";Ss()});var zp=i(()=>{"use strict";Jp();Ss()});var Xp=i(()=>{"use strict"});var Qp=i(()=>{"use strict"});var Vp=i(()=>{"use strict";Xp();Qp()});var Zn=i(()=>{"use strict";re()});var Yp=i(()=>{"use strict";k();Zn()});var Zp=i(()=>{"use strict";Zn()});var eh=i(()=>{"use strict"});import{z as b}from"zod";var th,CE,nh,AE,PE,LE,OE,NE,DE,pi=i(()=>{"use strict";k();th=b.object({from:b.union([b.number().int(),b.string()]),strength:b.string().optional(),to:b.union([b.number().int(),b.string()])}),CE=b.object({quote:b.string().optional(),sentence_id:b.string().optional()}).passthrough(),nh=b.union([b.record(b.string(),b.unknown()),b.array(CE)]),AE=b.object({content:b.string(),evidence:nh.nullish(),label:b.string(),retention:b.enum(["verbatim","detailed","focused","relevant"]),temp_id:b.string()}).passthrough(),PE=b.object({chunks:b.array(AE),fragment_summary:b.string(),links:b.array(th)}),LE=b.object({content:b.string(),evidence:nh.nullish(),importance:b.enum(["critical","important","helpful"]),label:b.string(),temp_id:b.string()}).passthrough(),OE=b.object({chunk_id:b.number().int(),importance:b.enum(["critical","important","helpful"])}),NE=b.object({chunks:b.array(LE),importance_annotations:b.array(OE),links:b.array(th)}),DE=b.object({choice:b.string()})});var rh=i(()=>{"use strict";re();Lt();eh();Zn();pi()});var ih=i(()=>{"use strict";re();k();Yp();Zn();Zp();rh();pi()});var ah=i(()=>{"use strict";ih();pi()});var hi=i(()=>{"use strict"});import{detect as DJ,validateISO2 as FJ}from"tinyld";var ws=i(()=>{"use strict";Re()});var oh=i(()=>{"use strict";ws();Re()});var sh=i(()=>{"use strict"});import{z as er}from"zod";var GE,iz,dh=i(()=>{"use strict";re();Lt();ah();hi();hi();oh();sh();GE=er.object({content:er.string(),id:er.number(),label:er.string()}),iz=er.array(GE)});var Es=i(()=>{"use strict";dh()});var uh=i(()=>{"use strict";nt()});var xs=i(()=>{"use strict";uh()});var lh=i(()=>{"use strict";Vp();Es();xs()});var fi=i(()=>{"use strict";lh();xs()});var yi=i(()=>{"use strict"});var gi=i(()=>{"use strict";fi();yi()});var mh=i(()=>{"use strict";gi()});var ph=i(()=>{"use strict"});var bi=i(()=>{"use strict"});var hh=i(()=>{"use strict";bi()});var Ot,Nz,KE,Si=i(()=>{"use strict";Xi();Ot=(r=>(r.Critical="critical",r.Major="major",r.Minor="minor",r))(Ot||{}),Nz=kt(Ot),KE=nn(Ot,"review severity")});var jz,ks=i(()=>{"use strict";bi();Si();jz=Object.freeze({critical:9,major:3,minor:1})});var fh=i(()=>{"use strict";ws();Re();Si()});var yh=i(()=>{"use strict";p();ye();ks()});var Rs=i(()=>{"use strict";k()});var gh=i(()=>{"use strict"});import{z as Nt}from"zod";var JE,zE,dX,bh=i(()=>{"use strict";ye();re();bi();Rs();Si();JE=["critical","major","minor"],zE=Nt.object({problem:Nt.string(),severity:Nt.enum(JE),suggestion:Nt.string().default("")}),dX=Nt.object({issues:Nt.array(zE)})});var Sh=i(()=>{"use strict";ph();hh();ks();fh();yh();Rs();gh();bh()});var vs=i(()=>{"use strict";Sh()});var Eh=i(()=>{"use strict"});var xh=i(()=>{"use strict"});var Ih=i(()=>{"use strict";Eh();xh()});var tr=i(()=>{"use strict"});var HX,qX,Dt=i(()=>{"use strict";k();HX={critical:9,helpful:1,important:3},qX={detailed:9,focused:3,relevant:1,verbatim:27}});var Ft=i(()=>{"use strict"});var nr=i(()=>{"use strict"});var _s=i(()=>{"use strict";Ft()});var Ts=i(()=>{"use strict";Dt();Ft();tr();nr();_s()});var Cs=i(()=>{"use strict";tr();Ts();nr()});var Rh=i(()=>{"use strict";Dt();Ft();nr()});var vh=i(()=>{"use strict";Dt();Ft();tr();nr();_s()});var _h=i(()=>{"use strict";Cs();Ft();tr();Rh();Ts();vh()});var Th=i(()=>{"use strict";_h();Cs()});var Ch=i(()=>{"use strict";Dt()});var Ah=i(()=>{"use strict";Th();Ch()});var Ph=i(()=>{"use strict";Ih();Ah();Dt()});var Lh=i(()=>{"use strict";Ph()});var As=i(()=>{"use strict"});var Oh=i(()=>{"use strict"});var Ps=i(()=>{"use strict";gi()});var Ei=i(()=>{"use strict"});var Nh=i(()=>{"use strict";Qt();Yt();vs();fi();Lh();gi();yi();As();Oh();Ps();Ei()});var Dh=i(()=>{"use strict";As();Ei()});var Fh=i(()=>{"use strict";mh();Nh();yi();Dh();Ps();Ei()});var Mt=i(()=>{"use strict";Fh()});var Mh=i(()=>{"use strict";se();Qt();Mt();at()});var Wh=i(()=>{"use strict";at();at();k();se();zp();Mt();Mh();Ar();p()});var jh,Ke=i(()=>{"use strict";jh=["Recall only source mentions that should become stable searchable knowledge graph objects.","Prefer named entities: historical persons, places, organizations, dynasties, political entities, named works, named events, battles, titles, and explicitly named domain concepts.","Recall a non-named common concept only when it is a central recurring topic of the chapter and would be useful as an entity search result across multiple evidence passages.","Do not recall a surface merely because it is useful for summarizing the story or explaining a detail.","Do not recall pure numbers, standalone dates without a named event role, units of measurement, dimensions, ordinal labels, chapter numbers, punctuation fragments, common function words, generic verbs, generic adjectives, generic roles, generic objects, or incidental attributes.","Measurements, quantities, dimensions, tactical details, and descriptive attributes should remain evidence text, not entity mentions.","When uncertain, skip the mention rather than creating a noisy knowledge graph object."].join(" ")});var Ns=i(()=>{"use strict";Ke()});var Gh=i(()=>{"use strict";ye();Ui();Wh();bs();Ce();Ns();p()});var Ds,Fs=i(()=>{"use strict";Ds=["queued","running","canceling","paused","succeeded","failed","canceled"]});async function Bh(){return await q(I().library,"jobs/work")}async function Uh(e){let t=await I().library.getDirectory("jobs");if(t!==void 0)for(let[n,r,a]of[["work",e,!0],["cache",e,!0],["logs",e,!0],["events",`${e}.ndjson`,!1]]){let o=await t.getDirectory(n);o!==void 0&&await o.remove(r,{recursive:a})}}var rr=i(()=>{"use strict";p()});var ir=i(()=>{"use strict";O()});var Ms=i(()=>{"use strict"});var ar=i(()=>{"use strict";p()});var Hh,qh=i(()=>{"use strict";Hh=`
|
|
424
715
|
CREATE TABLE IF NOT EXISTS build_jobs (
|
|
425
716
|
job_id TEXT PRIMARY KEY,
|
|
426
717
|
archive_key TEXT NOT NULL,
|
|
@@ -479,86 +770,25 @@ CREATE TABLE IF NOT EXISTS build_worker_lease (
|
|
|
479
770
|
|
|
480
771
|
INSERT OR IGNORE INTO build_worker_lease (id)
|
|
481
772
|
VALUES (1);
|
|
482
|
-
`});function
|
|
773
|
+
`});function bt(e){let t=cx(gt(e,"current_step"),"current_step"),n=gt(e,"owner_id"),r=e.owner_pid===null?void 0:Y(e,"owner_pid"),a=e.reading_summary_started_at===null?void 0:Y(e,"reading_summary_started_at"),o=e.finished_at===null?void 0:Y(e,"finished_at"),s=gt(e,"error_json"),c=e.input_revision===null?void 0:Y(e,"input_revision"),d=gt(e,"llm_json"),u=gt(e,"prompt");return{archiveKey:ke(e,"archive_key"),archiveIdentity:ke(e,"archive_path"),cacheIdentity:ke(e,"cache_path"),chapterId:Y(e,"chapter_id"),createdAt:Y(e,"created_at"),...t===void 0?{}:{currentStep:t},...s===void 0?{}:{errorJSON:s},eventsIdentity:ke(e,"events_path"),...o===void 0?{}:{finishedAt:o},jobId:ke(e,"job_id"),...c===void 0?{}:{inputRevision:c},logIdentity:ke(e,"log_path"),...d===void 0?{}:{llmJSON:d},...n===void 0?{}:{ownerId:n},...r===void 0?{}:{ownerPid:r},...u===void 0?{}:{prompt:u},queueRank:Y(e,"queue_rank"),state:sx(ke(e,"state")),...a===void 0?{}:{readingSummaryStartedAt:a},target:$h(ke(e,"target"),"target"),updatedAt:Y(e,"updated_at"),workspaceIdentity:ke(e,"workspace_path")}}async function sr(e){let t=L().resources,[n,r,a,o,s]=await Promise.all([t.getFile(e.archiveIdentity),t.getDirectory(e.cacheIdentity),t.getFile(e.eventsIdentity),t.getDirectory(e.logIdentity),t.getDirectory(e.workspaceIdentity)]);if(n===void 0)throw or("archive");if(r===void 0)throw or("cache");if(a===void 0)throw or("events");if(o===void 0)throw or("log");if(s===void 0)throw or("workspace");let{archiveIdentity:c,cacheIdentity:d,eventsIdentity:u,logIdentity:f,ownerPid:x,workspaceIdentity:D,...w}=e;return{...w,archive:n,cache:r,events:a,log:o,workspace:s}}function or(e){return new Error(`Build job ${e} resource is unavailable`)}function sx(e){if(Ds.includes(e))return e;throw new Error(`Invalid build job state: ${e}`)}function $h(e,t){if(e==="index-embedding-source"||e==="index-embedding-summary"||e==="index-fts"||e==="reading-graph"||e==="knowledge-graph"||e==="reading-summary")return e;throw new Error(`Invalid ${t}: ${e}`)}function cx(e,t){return e===void 0?void 0:$h(e,t)}function ke(e,t){let n=e[t];if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}function Y(e,t){let n=e[t];if(typeof n!="number")throw new TypeError(`Expected ${t} to be a number`);return n}function gt(e,t){let n=e[t];if(n!=null){if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}}var St=i(()=>{"use strict";Fs();p()});async function jt(){return await B("jobs/job.sqlite",Hh)}var Gt=i(()=>{"use strict";k();qh();St()});var xi=i(()=>{"use strict";ir();ar();Gt();rr()});var Ii=i(()=>{"use strict";St();xi();rr()});var Ws=i(()=>{"use strict";O();rr();ir();Ms();ar();Gt();Ii();xi();St()});var Xh=i(()=>{"use strict";ir();Gt();Ii();St()});var Qh=i(()=>{"use strict";O();ir();Ms();ar();Gt();Ii();xi();Ws();St()});function fx(e){return px.has(e)}async function yx(e,t){if((await t.list()).some(a=>!fx(a.name)))return 0;let r=await Je(t);return await e.remove(t.name,{recursive:!0}),r}async function Vh(e){let t=0,n=0,r=0;for(let a of await e.list()){if(!G(a))continue;r+=1;let o=await Le(a);if(o===void 0||Date.now()-o<hx)continue;let s=await yx(e,a);(s>0||await e.getDirectory(a.name)===void 0)&&(t+=s,n+=1)}return{freedBytes:t,removed:n,scanned:r}}async function Je(e){return await pe(e)}var px,hx,js=i(()=>{"use strict";p();px=new Set([".DS_Store"]),hx=6e4});async function ki(e){let t=await jt(),n=e.force?e.now:e.now-10080*60*1e3;try{let r=await t.queryOne("SELECT COUNT(*) AS count FROM build_jobs",void 0,d=>Y(d,"count")),a=await t.queryAll(`
|
|
483
774
|
SELECT *
|
|
484
775
|
FROM build_jobs
|
|
485
776
|
WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
486
777
|
AND updated_at <= ?
|
|
487
|
-
`,[n],ct),o=e.dryRun?{freedBytes:0,removed:0,scanned:0}:await ki(rh()),s=0;for(let d of a)s+=await j(d.workspacePath),s+=await j(d.cachePath),s+=await j(d.logPath),s+=await j(d.eventsPath),e.dryRun||(await Pi(d.workspacePath,{force:!0,recursive:!0}),await Pi(d.cachePath,{force:!0,recursive:!0}),await Pi(d.logPath,{force:!0,recursive:!0}),await Pi(d.eventsPath,{force:!0}),await t.run("DELETE FROM build_jobs WHERE job_id = ?",[d.jobId]));return{freedBytes:s+o.freedBytes,removed:a.length+o.removed,scanned:(r??0)+o.scanned}}finally{await t.close()}}var ph=i(()=>{"use strict";Jn();_i();Ft();ut()});var Ph=i(()=>{"use strict";Ls();Ns();lh();mh();Xn();ph()});var Li=i(()=>{"use strict"});import{createReadStream as T6,createWriteStream as C6}from"fs";import{createInterface as A6}from"readline";import{z as u}from"zod";var Ah,A,dI,cI,Lh,uI,lI,mI,pI,hI,fI,yI,gI,bI,SI,wI,EI,II,xI,D6,Wt=i(()=>{"use strict";ie();Li();Ah=1,A=u.number().int().nonnegative(),dI=u.literal(Ah),cI=u.string().regex(/^Q[1-9][0-9]*$/u),Lh=u.number().min(0).max(1).optional(),uI=u.object({text:u.string(),wordsCount:A}).strict(),lI=u.object({type:u.literal("meta"),schemaVersion:dI,chapterId:u.number().int(),stream:u.enum(["knowledge-graph","reading-graph","summary"])}).strict(),mI=u.object({type:u.literal("parameter"),scope:u.enum(["knowledge-graph","reading-graph","summary"]),language:u.string().optional(),prompt:u.string()}).strict(),pI=u.object({type:u.literal("source-fragment"),fragmentId:A,summary:u.string(),sentences:u.array(uI)}).strict(),hI=u.object({type:u.literal("reading-chunk"),id:u.string().min(1),generation:u.number().int(),sentenceIndex:A,label:u.string(),content:u.string(),sentenceIndexes:u.array(A).min(1),retention:u.enum(J).optional(),importance:u.enum(re).optional(),wordsCount:A,weight:u.number()}).strict(),fI=u.object({type:u.literal("reading-edge"),fromChunkId:u.string().min(1),toChunkId:u.string().min(1),strength:u.string().optional(),weight:u.number()}).strict(),yI=u.object({type:u.literal("fragment-group"),groupId:A,startSentenceIndex:A,endSentenceIndex:A}).strict(),gI=u.object({type:u.literal("snake"),id:u.string().min(1),groupId:A,localSnakeId:A,size:A,firstLabel:u.string(),lastLabel:u.string(),wordsCount:A,weight:u.number()}).strict(),bI=u.object({type:u.literal("snake-chunk"),snakeId:u.string().min(1),chunkId:u.string().min(1),position:A}).strict(),SI=u.object({type:u.literal("snake-edge"),fromSnakeId:u.string().min(1),toSnakeId:u.string().min(1),weight:u.number()}).strict(),wI=u.object({type:u.literal("summary"),text:u.string()}).strict(),EI=u.object({type:u.literal("mention"),id:u.string().min(1),fragmentId:A.optional(),sentenceIndex:A.optional(),rangeStart:A,rangeEnd:A,surface:u.string().min(1),qid:cI,confidence:Lh,note:u.string().optional()}).strict(),II=u.object({type:u.literal("mention-link"),id:u.string().min(1),sourceMentionId:u.string().min(1),targetMentionId:u.string().min(1),predicate:u.string().min(1),evidenceSentenceIndexes:u.array(A).min(1),confidence:Lh,note:u.string().optional()}).strict(),xI=u.object({type:u.literal("end")}).strict(),D6=u.discriminatedUnion("type",[lI,mI,pI,hI,fI,yI,gI,bI,SI,wI,EI,II,xI])});var Oh=i(()=>{"use strict";we();je()});var Nh=i(()=>{"use strict";x();ei()});import{mkdir as J6,rm as z6}from"fs/promises";import{join as Q6}from"path";var Mh=i(()=>{"use strict";x();W();vt();Wt();Ct();Oh();Nh()});var Oi=i(()=>{"use strict"});var Fh=i(()=>{"use strict";Oi()});var Wh=i(()=>{"use strict"});var jh=i(()=>{"use strict";Oi()});var Gh=i(()=>{"use strict";Oi()});var Bh=i(()=>{"use strict";Fh();Wh();jh();Gh()});import{z as l}from"zod";var Uh,vI,_I,TI,CI,PI,AI,LI,OI,NI,DI,Hh=i(()=>{"use strict";ie();Uh=l.tuple([l.number(),l.number()]),vI=l.object({text:l.string(),wordsCount:l.number()}),_I=l.object({serialId:l.number(),fragmentId:l.number(),summary:l.string(),sentences:l.array(vI)}),TI=l.object({documentOrder:l.number().optional(),id:l.number(),knowledgeGraphReady:l.boolean(),knowledgeGraphParameterHash:l.string().optional(),revision:l.number().optional(),topologyParameterHash:l.string().optional(),topologyReady:l.boolean()}).transform(e=>({documentOrder:e.documentOrder??e.id,id:e.id,knowledgeGraphReady:e.knowledgeGraphReady,...e.knowledgeGraphParameterHash===void 0?{}:{knowledgeGraphParameterHash:e.knowledgeGraphParameterHash},revision:e.revision??0,topologyReady:e.topologyReady,...e.topologyParameterHash===void 0?{}:{topologyParameterHash:e.topologyParameterHash}})),CI=l.object({id:l.number(),generation:l.number(),sentenceId:Uh,label:l.string(),content:l.string(),sentenceIds:l.array(Uh),retention:l.enum(J).optional(),importance:l.enum(re).optional(),wordsCount:l.number(),weight:l.number()}),PI=l.object({fromId:l.number(),toId:l.number(),strength:l.string().optional(),weight:l.number()}),AI=l.object({id:l.number(),serialId:l.number(),groupId:l.number(),localSnakeId:l.number(),size:l.number(),firstLabel:l.string(),lastLabel:l.string(),wordsCount:l.number(),weight:l.number()}),LI=l.object({snakeId:l.number(),chunkId:l.number(),position:l.number()}),OI=l.object({fromSnakeId:l.number(),toSnakeId:l.number(),weight:l.number()}),NI=l.object({serialId:l.number(),groupId:l.number(),startSentenceIndex:l.number(),endSentenceIndex:l.number()}),DI=l.object({chunks:l.array(CI),fragmentGroups:l.array(NI),fragments:l.array(_I),readingEdges:l.array(PI),serial:TI,snakeChunks:l.array(LI),snakeEdges:l.array(OI),snakes:l.array(AI)})});import{readFile as KZ,writeFile as JZ}from"fs/promises";var qh=i(()=>{"use strict";Hh()});var $h=i(()=>{"use strict";Nn();qo()});import{mkdir as a9}from"fs/promises";import{join as s9}from"path";var Kh=i(()=>{"use strict";x();W();Bh();qh();Li();$h();Wt();Ct()});var Jh=i(()=>{"use strict"});var Ws=i(()=>{"use strict";Kh();Li();Jh()});var zh=i(()=>{"use strict";W();Ws()});var Xh=i(()=>{"use strict"});var Qh=i(()=>{"use strict";Mh();zh();Xh()});var Vh=i(()=>{"use strict";Qh();Ws()});import{createReadStream as N9,createWriteStream as D9}from"fs";import{createInterface as F9}from"readline";import{z as C}from"zod";var j9,FI,G9,js=i(()=>{"use strict";j9=C.object({id:C.string().min(1),chapterId:C.number().int(),sentenceIndex:C.number().int().nonnegative().optional(),rangeStart:C.number().int().nonnegative(),rangeEnd:C.number().int().nonnegative(),surface:C.string().min(1),qid:C.string().regex(/^Q[1-9][0-9]*$/),confidence:C.number().min(0).max(1).optional(),note:C.string().optional()}),FI=C.tuple([C.number().int().nonnegative(),C.number().int().nonnegative()]).readonly(),G9=C.object({id:C.string().min(1),sourceMentionId:C.string().min(1),targetMentionId:C.string().min(1),predicate:C.string().min(1),evidenceSentenceIds:C.array(FI).min(1),confidence:C.number().min(0).max(1).optional(),note:C.string().optional()})});import{mkdir as K9,rm as J9}from"fs/promises";import{join as X9}from"path";var Gs=i(()=>{"use strict";we();Wt();js()});var Zh=i(()=>{"use strict";Wt();Ct();js()});var Qn=i(()=>{"use strict"});import{z as ke}from"zod";var GI,BI,c4,Us=i(()=>{"use strict";U();Qn();GI=ke.object({candidateId:ke.string().min(1),decision:ke.enum(["continue","recall","skip_this_time","never_recall"]),qid:ke.string().nullable().optional()}).strict(),BI=ke.object({decisions:ke.array(GI),groupId:ke.string().min(1)}).strict(),c4=ke.object({groups:ke.array(BI)}).strict()});import{z as Vn}from"zod";var qI,f4,tf=i(()=>{"use strict";U();Qn();Us();qI=Vn.object({decision:Vn.enum(["keep","reject"]),qid:Vn.string().regex(/^Q[1-9][0-9]*$/)}).strict(),f4=Vn.object({qids:Vn.array(qI)}).strict()});var nf=i(()=>{"use strict";vn()});import{z as Yn}from"zod";var $I,E4,rf=i(()=>{"use strict";U();$I=Yn.object({note:Yn.string().max(24).optional(),surfaceId:Yn.string().min(1)}).strict(),E4=Yn.object({protectedSurfaces:Yn.array($I)}).strict()});var Hs=i(()=>{"use strict"});var af=i(()=>{"use strict";Hs()});import{spawn as _4}from"child_process";var of=i(()=>{"use strict"});var qs=i(()=>{"use strict"});var sf=i(()=>{"use strict";qs();Qn()});var lt=i(()=>{"use strict";Us();tf();Qn();nf();rf();af();Hs();of();sf();qs()});var Ni=i(()=>{"use strict"});var uf=i(()=>{"use strict";lt();Ni()});var $s=i(()=>{"use strict"});var lf=i(()=>{"use strict";lt()});var Ks=i(()=>{"use strict"});var Js=i(()=>{"use strict";lt();Ni();lf();Ks()});var pf=i(()=>{"use strict";Js()});var hf=i(()=>{"use strict";we()});var zs=i(()=>{"use strict"});var ff=i(()=>{"use strict";Wo();Et();Zr()});import{z as R}from"zod";var yf,ix,ax,ox,C7,P7,gf=i(()=>{"use strict";U();Et();ff();yf=R.object({head:R.string().optional(),mode:R.enum(["full","head_tail"]).optional(),tail:R.string().optional(),text:R.string().optional()}).strict(),ix=R.object({quote:R.string().optional(),sentence_id:R.string().optional()}).strict(),ax=R.union([R.object({end_anchor:R.union([R.string(),yf]).optional(),quote:R.string().optional(),sentence_id:R.string().optional(),start_anchor:R.union([R.string(),yf]).optional()}).strict(),R.array(ix)]),ox=R.object({confidence:R.number().min(0).max(1).optional(),evidence:ax,predicate:R.string().min(1).max(64),sourceMentionId:R.string().min(1),targetMentionId:R.string().min(1)}).strict(),C7=R.object({relations:R.array(ox)}).strict(),P7=new ot});var bf=i(()=>{"use strict"});var Sf=i(()=>{"use strict";gf();bf()});var wf=i(()=>{"use strict";Sf();Ni();Ks()});var Xs=i(()=>{"use strict";W();$s()});var Rf=i(()=>{"use strict";lt();je();Gs();uf();$s();pf();hf();zs();wf();Xs()});var vf=i(()=>{"use strict"});var _f=i(()=>{"use strict";Gs();Zh();Rf();Js();zs();Xs();vf()});var Tf=i(()=>{"use strict";_f()});var Zn=i(()=>{"use strict";W()});var Qs=i(()=>{"use strict";je()});var Vs=i(()=>{"use strict";we();je();vt();Zn();Qs()});var Af=i(()=>{"use strict";je();Zn();Vs()});var Ys=i(()=>{"use strict";ge()});var Lf=i(()=>{"use strict";ge();en();Qe();Ys()});var Of=i(()=>{"use strict";vt();ge();Zn();Lf();Ys()});import{z as mt}from"zod";var Nf,ute,Df=i(()=>{"use strict";Nf=mt.object({children:mt.lazy(()=>mt.array(Nf)),uri:mt.string().min(1),title:mt.string().nullable().optional()}).strict(),ute=mt.object({chapters:mt.array(Nf)}).strict()});var Mf=i(()=>{"use strict";W()});var Ff=i(()=>{"use strict";Af();Zn();Vs();Of();Qs();Df();Mf()});var Wf=i(()=>{"use strict";hn()});var Ge=i(()=>{"use strict";eh();wt();Ph();Vh();Tf();wt();Ps();Ff();Wf();vi()});import{randomUUID as Bte}from"crypto";async function jf(e){let t=await Bx(e.databasePath);try{return await Gx(t,e),await t.queryOne(`
|
|
488
|
-
SELECT 1 AS active
|
|
489
|
-
FROM state_locks
|
|
490
|
-
WHERE scope = ? AND resource_key = ?
|
|
491
|
-
LIMIT 1
|
|
492
|
-
`,[e.scope,e.resourceKey],()=>!0)===!0}finally{await t.close()}}async function Gx(e,t){let n=[],r=[];t.scope!==void 0&&(n.push("scope = ?"),r.push(t.scope)),t.resourceKey!==void 0&&(n.push("resource_key = ?"),r.push(t.resourceKey));let a=await e.queryAll(`
|
|
493
|
-
SELECT scope, resource_key, mode, owner_id, owner_pid, heartbeat_at
|
|
494
|
-
FROM state_locks
|
|
495
|
-
${n.length===0?"":`WHERE ${n.join(" AND ")}`}
|
|
496
|
-
`,r,o=>({lock:Ux(o),resourceKey:E(o,"resource_key"),scope:E(o,"scope")}));for(let{lock:o,resourceKey:s,scope:d}of a)qx(o,t.staleMs)&&await e.run(`
|
|
497
|
-
DELETE FROM state_locks
|
|
498
|
-
WHERE scope = ?
|
|
499
|
-
AND resource_key = ?
|
|
500
|
-
AND owner_id = ?
|
|
501
|
-
AND heartbeat_at = ?
|
|
502
|
-
`,[d,s,o.ownerId,o.heartbeatAt])}async function Bx(e){return await _(e,Wx)}function Ux(e){return{heartbeatAt:g(e,"heartbeat_at"),mode:Hx(E(e,"mode")),ownerId:E(e,"owner_id"),ownerPid:g(e,"owner_pid")}}function Hx(e){return e==="read"||e==="write"?e:"write"}function qx(e,t=jx){return Date.now()-e.heartbeatAt>t&&!$x(e.ownerPid)}function $x(e){try{return process.kill(e,0),!0}catch(t){return!(h(t)&&t.code==="ESRCH")}}var Wx,jx,Gf=i(()=>{"use strict";f();x();b();Wx=`
|
|
503
|
-
CREATE TABLE IF NOT EXISTS state_locks (
|
|
504
|
-
scope TEXT NOT NULL,
|
|
505
|
-
resource_key TEXT NOT NULL,
|
|
506
|
-
mode TEXT NOT NULL,
|
|
507
|
-
owner_id TEXT NOT NULL,
|
|
508
|
-
owner_pid INTEGER NOT NULL,
|
|
509
|
-
heartbeat_at INTEGER NOT NULL,
|
|
510
|
-
created_at INTEGER NOT NULL,
|
|
511
|
-
PRIMARY KEY (scope, resource_key, owner_id)
|
|
512
|
-
);
|
|
513
|
-
|
|
514
|
-
CREATE INDEX IF NOT EXISTS idx_state_locks_resource
|
|
515
|
-
ON state_locks(scope, resource_key);
|
|
516
|
-
`,jx=300*1e3});async function Uf(e){return await jf({databasePath:G(),resourceKey:zx(e),scope:Jx,staleMs:Bf})||await Xx(e)}function zx(e){return String(e)}async function Xx(e){let t=await Qx();try{let n=await t.queryOne("SELECT owner_pid, heartbeat_at FROM library_locks WHERE library_id = ?",[e],r=>({heartbeatAt:g(r,"heartbeat_at"),ownerPid:g(r,"owner_pid")}));return n!==void 0&&Yx(n)}finally{await t.close()}}async function Qx(){let e=await _(G(),Kx);return await Vx(e),e}async function Vx(e){new Set(await e.queryAll("PRAGMA table_info(library_locks)",void 0,n=>E(n,"name"))).has("heartbeat_at")||await e.run("ALTER TABLE library_locks ADD COLUMN heartbeat_at INTEGER NOT NULL DEFAULT 0")}function Yx(e){return Date.now()-e.heartbeatAt<=Bf&&Zx(e.ownerPid)}function Zx(e){try{return process.kill(e,0),!0}catch(t){return!(h(t)&&t.code==="ESRCH")}}var Kx,Jx,Bf,er=i(()=>{"use strict";f();x();D();Gf();b();Kx=`
|
|
517
|
-
CREATE TABLE IF NOT EXISTS library_locks (
|
|
518
|
-
library_id INTEGER PRIMARY KEY,
|
|
519
|
-
mode TEXT NOT NULL,
|
|
520
|
-
owner_id TEXT NOT NULL,
|
|
521
|
-
owner_pid INTEGER NOT NULL,
|
|
522
|
-
heartbeat_at INTEGER NOT NULL,
|
|
523
|
-
created_at INTEGER NOT NULL
|
|
524
|
-
);
|
|
525
|
-
`,Jx="library",Bf=300*1e3});import{randomBytes as tne}from"crypto";import{constants as rne}from"fs";import{access as ane,mkdir as one,stat as sne}from"fs/promises";import{join as cne,resolve as une}from"path";var jt=i(()=>{"use strict";f();x();D();Le();b();er();Pa()});import{createHash as kne}from"crypto";import{mkdir as vne,readdir as ek,rm as tk}from"fs/promises";import{join as Hf}from"path";async function qf(e){let t=Hf($(),"library"),n=await nk();if(n===void 0)return{freedBytes:0,removed:0,scanned:0};let r=new Set(n.map(c=>String(c))),a=await ek(t,{withFileTypes:!0}).catch(c=>{if(h(c)&&c.code==="ENOENT")return[];throw c}),o=0,s=0,d=0;for(let c of a){if(!c.isDirectory()||(o+=1,r.has(c.name)))continue;let m=Number(c.name);if(Number.isInteger(m)&&await Uf(m))continue;let p=Hf(t,c.name),w=await j(p);e.dryRun||await tk(p,{force:!0,recursive:!0}),s+=1,d+=w}return{freedBytes:d,removed:s,scanned:o}}async function nk(){let e=await _(G(),"");try{return await e.queryAll("SELECT id FROM libraries",void 0,t=>g(t,"id"))}catch{return}finally{await e.close()}}var nr=i(()=>{"use strict";f();$e();tn();x();me();nt();Tt();Oe();Jn();D();b();rr();jt();er()});import{randomBytes as Kne}from"crypto";import{constants as zne,copyFile as Xne,mkdir as Qne,opendir as Vne,realpath as Yne,rename as Zne,rm as ere,stat as tre}from"fs/promises";import{dirname as rre,isAbsolute as ire,join as are,relative as ore,resolve as sre}from"path";var ik,gre,rr=i(()=>{"use strict";f();x();D();Le();me();b();jt();nr();er();ik=`
|
|
526
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
527
|
-
library_id INTEGER NOT NULL,
|
|
528
|
-
public_id TEXT NOT NULL,
|
|
529
|
-
relative_path TEXT NOT NULL,
|
|
530
|
-
status TEXT NOT NULL DEFAULT 'present',
|
|
531
|
-
last_seen_mutation_token TEXT,
|
|
532
|
-
last_seen_size INTEGER,
|
|
533
|
-
last_seen_mtime_ms INTEGER,
|
|
534
|
-
last_scanned_at TEXT,
|
|
535
|
-
created_at TEXT NOT NULL,
|
|
536
|
-
updated_at TEXT NOT NULL,
|
|
537
|
-
UNIQUE(library_id, public_id)
|
|
538
|
-
`,gre=`
|
|
539
|
-
CREATE TABLE IF NOT EXISTS library_archives (
|
|
540
|
-
${ik}
|
|
541
|
-
);
|
|
542
|
-
|
|
543
|
-
CREATE INDEX IF NOT EXISTS idx_library_archives_library
|
|
544
|
-
ON library_archives(library_id);
|
|
545
|
-
|
|
546
|
-
CREATE INDEX IF NOT EXISTS idx_library_archives_library_path
|
|
547
|
-
ON library_archives(library_id, relative_path);
|
|
548
|
-
`});var nd=i(()=>{"use strict";me();rr();jt()});var Jf=i(()=>{"use strict";Er();kr();Bt();Dr();Nr();V();Mr();et();Tt();nr();nd()});var zf=i(()=>{"use strict";wo();kr();Jf();V();rr();nd();jt();nr()});var Di=i(()=>{"use strict";jt();rr();zf();nr();er()});import{randomUUID as fk}from"crypto";import{join as yk}from"path";async function Xf(e="global"){let t=await rd(),n=`${process.pid}-${fk()}`,r=Date.now(),a=!1;try{await t.transaction(async()=>{await Ek(t),await t.run(`
|
|
778
|
+
`,[n],bt),o=await Promise.all(a.map(sr)),s=e.dryRun?{freedBytes:0,removed:0,scanned:0}:await Vh(await Bh()),c=0;for(let d of o)c+=await Je(d.workspace),c+=await Je(d.cache),c+=await Je(d.log),c+=await Je(d.events),e.dryRun||(await Uh(d.jobId),await t.run("DELETE FROM build_jobs WHERE job_id = ?",[d.jobId]));return{freedBytes:c+s.freedBytes,removed:o.length+s.removed,scanned:(r??0)+s.scanned}}finally{await t.close()}}var Yh=i(()=>{"use strict";js();rr();Gt();St()});var yf=i(()=>{"use strict";Fs();Ws();Xh();Qh();ar();Yh()});var Ri=i(()=>{"use strict"});import{z as l}from"zod";var gf,U,bx,Sx,bf,wx,Ex,xx,Ix,kx,Rx,vx,_x,Tx,Cx,Ax,Px,Lx,Ox,r8,Bt=i(()=>{"use strict";p();be();Ri();gf=1,U=l.number().int().nonnegative(),bx=l.literal(gf),Sx=l.string().regex(/^Q[1-9][0-9]*$/u),bf=l.number().min(0).max(1).optional(),wx=l.object({text:l.string(),wordsCount:U}).strict(),Ex=l.object({type:l.literal("meta"),schemaVersion:bx,chapterId:l.number().int(),stream:l.enum(["knowledge-graph","reading-graph","summary"])}).strict(),xx=l.object({type:l.literal("parameter"),scope:l.enum(["knowledge-graph","reading-graph","summary"]),language:l.string().optional(),prompt:l.string()}).strict(),Ix=l.object({type:l.literal("source-fragment"),fragmentId:U,summary:l.string(),sentences:l.array(wx)}).strict(),kx=l.object({type:l.literal("reading-chunk"),id:l.string().min(1),generation:l.number().int(),sentenceIndex:U,label:l.string(),content:l.string(),sentenceIndexes:l.array(U).min(1),retention:l.enum(ae).optional(),importance:l.enum(ge).optional(),wordsCount:U,weight:l.number()}).strict(),Rx=l.object({type:l.literal("reading-edge"),fromChunkId:l.string().min(1),toChunkId:l.string().min(1),strength:l.string().optional(),weight:l.number()}).strict(),vx=l.object({type:l.literal("fragment-group"),groupId:U,startSentenceIndex:U,endSentenceIndex:U}).strict(),_x=l.object({type:l.literal("snake"),id:l.string().min(1),groupId:U,localSnakeId:U,size:U,firstLabel:l.string(),lastLabel:l.string(),wordsCount:U,weight:l.number()}).strict(),Tx=l.object({type:l.literal("snake-chunk"),snakeId:l.string().min(1),chunkId:l.string().min(1),position:U}).strict(),Cx=l.object({type:l.literal("snake-edge"),fromSnakeId:l.string().min(1),toSnakeId:l.string().min(1),weight:l.number()}).strict(),Ax=l.object({type:l.literal("summary"),text:l.string()}).strict(),Px=l.object({type:l.literal("mention"),id:l.string().min(1),fragmentId:U.optional(),sentenceIndex:U.optional(),rangeStart:U,rangeEnd:U,surface:l.string().min(1),qid:Sx,confidence:bf,note:l.string().optional()}).strict(),Lx=l.object({type:l.literal("mention-link"),id:l.string().min(1),sourceMentionId:l.string().min(1),targetMentionId:l.string().min(1),predicate:l.string().min(1),evidenceSentenceIndexes:l.array(U).min(1),confidence:bf,note:l.string().optional()}).strict(),Ox=l.object({type:l.literal("end")}).strict(),r8=l.discriminatedUnion("type",[Ex,xx,Ix,kx,Rx,vx,_x,Tx,Cx,Ax,Px,Lx,Ox])});var Sf=i(()=>{"use strict";qn();Ue()});import{z as E}from"zod";var Nx,Dx,u8,l8,m8,wf=i(()=>{"use strict";p();Nx="wikg-index-output/v1",Dx=E.enum(["fts","embedding-source","embedding-summary"]),u8=E.object({artifactKind:Dx,chapterId:E.number().int().nonnegative(),embedding:E.object({dimensions:E.number().int().nonnegative(),identity:E.string().optional(),model:E.string()}).optional(),inputRevision:E.number().int().nonnegative(),protocol:E.literal(Nx),type:E.literal("manifest")}),l8=E.object({metadata:E.record(E.string(),E.unknown()).optional(),objectId:E.string().min(1),objectKind:E.string().min(1),rowId:E.string().min(1),sentenceIndex:E.number().int().nonnegative().optional(),text:E.string(),tokens:E.array(E.string()),type:E.literal("lexical-row")}),m8=E.object({endSentenceIndex:E.number().int().nonnegative(),segmentIndex:E.number().int().nonnegative(),startSentenceIndex:E.number().int().nonnegative(),text:E.string(),vector:E.array(E.number()),wordsCount:E.number().int().nonnegative(),type:E.literal("segment")})});var Ut=i(()=>{"use strict";Sf();wf()});var Ef=i(()=>{"use strict";Re();Ke()});var xf=i(()=>{"use strict";k();fi()});var kf=i(()=>{"use strict";p();k();$();Mt();Bt();Ut();Ef();xf()});var vi=i(()=>{"use strict"});var Rf=i(()=>{"use strict";vi()});var vf=i(()=>{"use strict"});var _f=i(()=>{"use strict";vi()});var Tf=i(()=>{"use strict";vi()});var Cf=i(()=>{"use strict";Rf();vf();_f();Tf()});import{z as m}from"zod";var Af,Jx,zx,Xx,Qx,Vx,Yx,Zx,eI,tI,nI,Pf=i(()=>{"use strict";be();Af=m.tuple([m.number(),m.number()]),Jx=m.object({text:m.string(),wordsCount:m.number()}),zx=m.object({serialId:m.number(),fragmentId:m.number(),summary:m.string(),sentences:m.array(Jx)}),Xx=m.object({documentOrder:m.number().optional(),id:m.number(),knowledgeGraphReady:m.boolean(),knowledgeGraphParameterHash:m.string().optional(),revision:m.number().optional(),topologyParameterHash:m.string().optional(),topologyReady:m.boolean()}).transform(e=>({documentOrder:e.documentOrder??e.id,id:e.id,knowledgeGraphReady:e.knowledgeGraphReady,...e.knowledgeGraphParameterHash===void 0?{}:{knowledgeGraphParameterHash:e.knowledgeGraphParameterHash},revision:e.revision??0,topologyReady:e.topologyReady,...e.topologyParameterHash===void 0?{}:{topologyParameterHash:e.topologyParameterHash}})),Qx=m.object({id:m.number(),generation:m.number(),sentenceId:Af,label:m.string(),content:m.string(),sentenceIds:m.array(Af),retention:m.enum(ae).optional(),importance:m.enum(ge).optional(),wordsCount:m.number(),weight:m.number()}),Vx=m.object({fromId:m.number(),toId:m.number(),strength:m.string().optional(),weight:m.number()}),Yx=m.object({id:m.number(),serialId:m.number(),groupId:m.number(),localSnakeId:m.number(),size:m.number(),firstLabel:m.string(),lastLabel:m.string(),wordsCount:m.number(),weight:m.number()}),Zx=m.object({snakeId:m.number(),chunkId:m.number(),position:m.number()}),eI=m.object({fromSnakeId:m.number(),toSnakeId:m.number(),weight:m.number()}),tI=m.object({serialId:m.number(),groupId:m.number(),startSentenceIndex:m.number(),endSentenceIndex:m.number()}),nI=m.object({chunks:m.array(Qx),fragmentGroups:m.array(tI),fragments:m.array(zx),readingEdges:m.array(Vx),serial:Xx,snakeChunks:m.array(Zx),snakeEdges:m.array(eI),snakes:m.array(Yx)})});var Lf=i(()=>{"use strict";p();Pf()});var Of=i(()=>{"use strict";Yt();vs()});var Nf=i(()=>{"use strict";p();k();$();Cf();Lf();Ri();Of();Bt();Ut()});var Df=i(()=>{"use strict"});var Hs=i(()=>{"use strict";Nf();Ri();Df()});var Ff=i(()=>{"use strict";$();Hs()});var Mf=i(()=>{"use strict"});var Wf=i(()=>{"use strict";kf();Ff();Mf()});var jf=i(()=>{"use strict";Wf();Hs()});import{z as F}from"zod";var s6,iI,c6,qs=i(()=>{"use strict";p();s6=F.object({id:F.string().min(1),chapterId:F.number().int(),sentenceIndex:F.number().int().nonnegative().optional(),rangeStart:F.number().int().nonnegative(),rangeEnd:F.number().int().nonnegative(),surface:F.string().min(1),qid:F.string().regex(/^Q[1-9][0-9]*$/),confidence:F.number().min(0).max(1).optional(),note:F.string().optional()}),iI=F.tuple([F.number().int().nonnegative(),F.number().int().nonnegative()]).readonly(),c6=F.object({id:F.string().min(1),sourceMentionId:F.string().min(1),targetMentionId:F.string().min(1),predicate:F.string().min(1),evidenceSentenceIds:F.array(iI).min(1),confidence:F.number().min(0).max(1).optional(),note:F.string().optional()})});var $s=i(()=>{"use strict";p();Re();Bt();qs()});var Bf=i(()=>{"use strict";Bt();Ut();qs()});var _i=i(()=>{"use strict"});var Uf=i(()=>{"use strict";yt();_i()});var Ks=i(()=>{"use strict"});var Hf=i(()=>{"use strict";yt()});var Js=i(()=>{"use strict"});var zs=i(()=>{"use strict";yt();_i();Hf();Js()});var $f=i(()=>{"use strict";zs()});var Kf=i(()=>{"use strict";Re()});var Xs=i(()=>{"use strict"});var Jf=i(()=>{"use strict";Es();Lt();hi()});import{z as A}from"zod";var zf,cI,dI,uI,Z6,eZ,Xf=i(()=>{"use strict";re();Lt();Jf();zf=A.object({head:A.string().optional(),mode:A.enum(["full","head_tail"]).optional(),tail:A.string().optional(),text:A.string().optional()}).strict(),cI=A.object({quote:A.string().optional(),sentence_id:A.string().optional()}).strict(),dI=A.union([A.object({end_anchor:A.union([A.string(),zf]).optional(),quote:A.string().optional(),sentence_id:A.string().optional(),start_anchor:A.union([A.string(),zf]).optional()}).strict(),A.array(cI)]),uI=A.object({confidence:A.number().min(0).max(1).optional(),evidence:dI,predicate:A.string().min(1).max(64),sourceMentionId:A.string().min(1),targetMentionId:A.string().min(1)}).strict(),Z6=A.object({relations:A.array(uI)}).strict(),eZ=new ft});var Qf=i(()=>{"use strict"});var Vf=i(()=>{"use strict";Xf();Qf()});var Yf=i(()=>{"use strict";Vf();_i();Js()});var Qs=i(()=>{"use strict";$();Ks()});var ry=i(()=>{"use strict";yt();Ke();$s();Uf();Ks();$f();Kf();Xs();Yf();Qs()});var iy=i(()=>{"use strict"});var ay=i(()=>{"use strict";$s();Bf();ry();zs();Xs();Qs();iy()});var oy=i(()=>{"use strict";ay()});var dr=i(()=>{"use strict";$()});var Vs=i(()=>{"use strict";Ke()});var Ys=i(()=>{"use strict";Re();Ke();Mt();dr();Vs()});var dy=i(()=>{"use strict";Ke();dr();Ys()});var Zs=i(()=>{"use strict";se()});var uy=i(()=>{"use strict";se();hn();at();Zs()});var ly=i(()=>{"use strict";Mt();se();dr();uy();Zs()});import{z as wt}from"zod";var my,N5,py=i(()=>{"use strict";my=wt.object({children:wt.lazy(()=>wt.array(my)),uri:wt.string().min(1),title:wt.string().nullable().optional()}).strict(),N5=wt.object({chapters:wt.array(my)}).strict()});var hy=i(()=>{"use strict";$()});var fy=i(()=>{"use strict";dy();dr();Ys();ly();Vs();py();hy()});var yy=i(()=>{"use strict";Cn()});var ze=i(()=>{"use strict";Gh();Pt();yf();jf();oy();Pt();Ns();fy();yy();Ar()});async function by(e="global"){let t=await tc(),n=Ne(),r=Date.now(),a=!1;try{await t.transaction(async()=>{await ek(t),await t.run(`
|
|
549
779
|
INSERT OR IGNORE INTO gc_locks (
|
|
550
780
|
scope, owner_id, owner_pid, heartbeat_at, created_at
|
|
551
781
|
) VALUES (?, ?, ?, ?, ?)
|
|
552
|
-
`,[e,n,
|
|
782
|
+
`,[e,n,0,r,r]),a=await t.queryOne("SELECT owner_id FROM gc_locks WHERE scope = ?",[e],s=>S(s,"owner_id"))===n})}finally{await t.close()}if(!a)return;let o=setInterval(()=>{ZI(e,n).catch(()=>{})},YI);return async()=>{clearInterval(o);let s=await tc();try{await s.run("DELETE FROM gc_locks WHERE scope = ? AND owner_id = ?",[e,n])}finally{await s.close()}}}async function ZI(e,t){let n=await tc();try{await n.run(`
|
|
553
783
|
UPDATE gc_locks
|
|
554
784
|
SET heartbeat_at = ?, owner_pid = ?
|
|
555
785
|
WHERE scope = ? AND owner_id = ?
|
|
556
|
-
`,[Date.now(),
|
|
786
|
+
`,[Date.now(),0,e,t])}finally{await n.close()}}async function ek(e){let t=await e.queryAll("SELECT * FROM gc_locks",void 0,rk);for(let{lock:n,scope:r}of t.filter(({lock:a})=>tk(a)))await e.run(`
|
|
557
787
|
DELETE FROM gc_locks
|
|
558
788
|
WHERE scope = ?
|
|
559
789
|
AND owner_id = ?
|
|
560
790
|
AND heartbeat_at = ?
|
|
561
|
-
`,[r,n.ownerId,n.heartbeatAt])}function
|
|
791
|
+
`,[r,n.ownerId,n.heartbeatAt])}function tk(e){return!(Date.now()-e.heartbeatAt<VI)}async function tc(){return await B("tmp/gc.sqlite",QI)}function nk(e){return{createdAt:h(e,"created_at"),heartbeatAt:h(e,"heartbeat_at"),ownerId:S(e,"owner_id"),ownerPid:h(e,"owner_pid")}}function rk(e){return{lock:nk(e),scope:S(e,"scope")}}var QI,VI,YI,Sy=i(()=>{"use strict";O();y();k();QI=`
|
|
562
792
|
CREATE TABLE IF NOT EXISTS gc_locks (
|
|
563
793
|
scope TEXT PRIMARY KEY,
|
|
564
794
|
owner_id TEXT NOT NULL,
|
|
@@ -566,8 +796,8 @@ CREATE TABLE IF NOT EXISTS gc_locks (
|
|
|
566
796
|
heartbeat_at INTEGER NOT NULL,
|
|
567
797
|
created_at INTEGER NOT NULL
|
|
568
798
|
);
|
|
569
|
-
`,
|
|
570
|
-
`,"utf8")}function ty(e){return Ok(e,"gc.last-run")}var Nk,Dk,ny=i(()=>{"use strict";fe();wt();Ge();me();vn();Di();b();Qf();ey();Nk=[{name:"wikg-coordinator",run:Ri},{name:"search-cache",run:br},{name:"library-index",run:qf},{name:"wikipage-cache",run:Qa},{name:"build-queue",run:Ai},{name:"tmp",run:Zf}],Dk=600*1e3});var ry=i(()=>{"use strict";ny()});ry();we();fr();b();import{mkdir as Pie,readFile as Aie,rm as Lie,writeFile as Oie}from"fs/promises";import{join as Die,resolve as Mie}from"path";import{z as ir}from"zod";var jk=1,Gie=ir.object({completedAt:ir.string().optional(),startedAt:ir.string(),status:ir.enum(["running","succeeded"]),version:ir.literal(jk)});vo();Nn();Y();Kr();Jr();Di();D();fe();Et();vn();lt();Cs();Ge();ge();me();x();Qe();wt();hi();Di();D();import{rm as Toe}from"fs/promises";import{homedir as Poe}from"os";import{resolve as Loe}from"path";fe();x();me();import{rm as foe}from"fs/promises";import{join as goe,resolve as boe}from"path";import{createWriteStream as oae}from"fs";import{mkdir as dae}from"fs/promises";import{dirname as uae,resolve as lae}from"path";import{pipeline as pae}from"stream/promises";import{posix as Kie,resolve as Jie,sep as zie}from"path";import{open as Qie}from"yauzl";f();f();import{rm as eoe}from"fs/promises";import{join as noe}from"path";b();import{readFile as Iae,readdir as xae}from"fs/promises";import{join as Rae}from"path";f();import{join as Cae}from"path";Sa();b();import{readFile as Nae,readdir as Dae}from"fs/promises";import{join as Fae}from"path";import{mkdir as Uae,writeFile as Hae}from"fs/promises";import{join as $ae}from"path";me();hi();Tt();Ct();b();Ge();Ge();Ge();Ge();Ge();je();lt();x();Wt();var zk=["WIKIGRAPH_DEV","WIKIGRAPH_ENV_POLICY","WIKIGRAPH_QUEUE_DISABLE_AUTOSTART","WIKIGRAPH_STATE_DIR"];function ay(e){let t=Xk(e.env);if(e.envPolicy==="production"){let o=zk.filter(s=>t[s]!==void 0);if(o.length>0)throw new Error(`Wiki Graph production entries do not support runtime env overrides: ${o.join(", ")}. Use the development entry or explicit SDK options instead.`);return{env:t,envPolicy:e.envPolicy,stateDir:e.stateDir}}let n=t.WIKIGRAPH_DEV??t.WIKIGRAPH_STATE_DIR,r=iy(e.stateDir??n);if(r===void 0)throw new Error("Wiki Graph development entries require an explicit runtime state directory.");let a=iy(e.devProjectRoot);if(a===void 0)throw new Error("Wiki Graph development entries require an explicit project root.");return{devProjectRoot:a,env:t,envPolicy:e.envPolicy,stateDir:r}}function Xk(e){return e===void 0?{...process.env}:{...process.env,...e}}function iy(e){if(!(e===void 0||e.trim()===""))return e}var oy="--wikigraph-internal-child",sy="--wikigraph-state-dir";async function dy(e,t){let n=Qk(process.argv.slice(2));if(n.internalChild!==e)throw new Error("This Wiki Graph worker entry is internal.");let r=ay({argv:n.argv,envPolicy:"production",stateDir:n.stateDir});return await Fi(r.stateDir,()=>t(n))}function Qk(e){let t=[],n,r;for(let a=0;a<e.length;a+=1){let o=e[a];if(o===oy){if(a+1>=e.length)throw new Error(`${oy} requires a value.`);n=e[a+1],a+=1;continue}if(o!==sy){t.push(o);continue}if(a+1>=e.length)throw new Error(`${sy} requires a value.`);r=e[a+1],a+=1}return{argv:t,internalChild:n??"",stateDir:r}}var Vk={command:`${"wg"} --help`,config:`${"wg"} help config`,format:`${"wg"} help format`,readiness:`${"wg"} help readiness`,root:`${"wg"} --help`,runtime:`${"wg"} help runtime`,uri:`${"wg"} help uri`};import{extname as Ede}from"path";import{createReadStream as Lde}from"fs";import{rm as Nde}from"fs/promises";import{join as Mde}from"path";import{AsyncLocalStorage as Yk}from"async_hooks";var Rde=new Yk;var Zk=new Map(["at","seq","uri","id","jobId","type","target","label","labels","title","state","name","qid","predicate","subjectLabel","objectLabel","subjectQid","objectQid","score","queueRank","chapter","chapterId","fragment","fragmentId","position","range","start","end","shown","total","count","mentionCount","nodeCount","edgeCount","summaryCount","limit","order","match","lens","terms","types","chapters","query","objects","items","nextCursor","archiveKey","archivePath","workspacePath","eventsPath","createdAt","updatedAt","finishedAt","currentStep","step","phase","phaseDetail","phaseDone","phaseTotal","phaseUnit","words","totalWords","outputTokens","authors","language","identifier","publisher","publishedAt","sourceFormat","snippet","summary","description","recommendation","risk","error","message","children","nodes","links","neighbors","incoming","outgoing","nodeGroups","sourceFragments","sources","tree","meta","evidence","content","source","text","prompt","llmJSON","errorJSON"].map((e,t)=>[e,t]));function cy(e){return`${JSON.stringify(ad(e),null,2)}
|
|
571
|
-
`}function
|
|
799
|
+
`,VI=6e4,YI=2e4});async function wy(e){let t=await I().library.getDirectory("tmp"),n=0,r=0,a=0;if(t===void 0)return{freedBytes:a,removed:r,scanned:n};for(let o of await ak(t)){n+=1;let s=await Le(o.value);if(!e.force&&(s===void 0||e.now-s<ik))continue;let c=await Je(o.value);e.dryRun||await o.parent.remove(o.value.name,{recursive:!0}),r+=1,a+=c}return{freedBytes:a,removed:r,scanned:n}}async function ak(e){let t=[];for(let n of await e.list())if(G(n))for(let r of await n.list())t.push({parent:n,value:r});return t}var ik,Ey=i(()=>{"use strict";p();js();ik=3600*1e3});async function rc(e={}){let t=Date.now(),n=await by();if(n===void 0)return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:t};try{if(e.opportunistic===!0&&!await dk(t))return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:t};let r={dryRun:e.dryRun===!0,force:e.force===!0,now:t},a=[];for(let s of ok)a.push(await ck(s,r));let o={finishedAt:Date.now(),freedBytes:nc(a,"freedBytes"),jobs:a,removed:nc(a,"removed"),scanned:nc(a,"scanned"),skipped:!1,startedAt:t};return await uk(o.finishedAt).catch(()=>{}),o}finally{await n()}}async function ck(e,t){try{return{name:e.name,...await e.run(t)}}catch(n){return{error:Ye(n),freedBytes:0,name:e.name,removed:0,scanned:0}}}function nc(e,t){return e.reduce((n,r)=>{let a=r[t];return n+(typeof a=="number"?a:0)},0)}async function dk(e){let t=await K(I().library,"tmp/gc.last-run"),n=Number((t===void 0?"":await Q(t).catch(()=>"")).trim());return!Number.isFinite(n)||e-n>=sk}async function uk(e){let t=await j(I().library,"tmp/gc.last-run");await V(t,`${e}
|
|
800
|
+
`)}var ok,sk,xy=i(()=>{"use strict";p();Pt();ze();Ce();jn();ci();ee();Sy();Ey();ok=[{name:"wikg-coordinator",run:si},{name:"search-cache",run:Or},{name:"library-index",run:lp},{name:"wikipage-cache",run:Eo},{name:"build-queue",run:ki},{name:"tmp",run:wy}],sk=600*1e3});var Iy=i(()=>{"use strict";xy()});var ky={};Ty(ky,{tryRunWikiGraphGc:()=>rc});var Ry=i(()=>{"use strict";Iy()});p();$t();import*as dc from"async_hooks";import*as uc from"crypto";import*as lr from"fs";import*as T from"fs/promises";import*as lc from"os";import*as _ from"path";import*as Fi from"process";import{Writable as Ly}from"stream";import{pipeline as Oy}from"stream/promises";import*as Mi from"sqlite3";import*as mc from"yauzl";import*as pc from"yazl";import{Environment as Ny,Loader as Dy}from"nunjucks";var Oi=Fi.default??Fi,Jt=Mi.default??Mi,Oe=class{constructor(t,n=_.basename(t)){this.path=t;this.identity=hc("file",t),this.name=n}path;identity;name;async read(t){return await T.readFile(this.path,t?.encoding===void 0?void 0:{encoding:t.encoding})}async getSize(){try{return(await T.stat(this.path)).size}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return 0;throw t}}async getLastModified(){try{return(await T.stat(this.path)).mtimeMs}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return;throw t}}async openWriter(){await T.mkdir(_.dirname(this.path),{recursive:!0});let t=`${this.path}.tmp-${uc.randomUUID()}`,n=await T.open(t,"wx"),r=!1;return{write:async a=>{if(r)throw new Error("Cannot write to a closed FileWriter");typeof a=="string"?await n.write(a):await n.write(Buffer.from(a))},commit:async()=>{r||(await n.close(),await T.rename(t,this.path)),r=!0},abort:async()=>{r||(await n.close(),await T.rm(t,{force:!0})),r=!0}}}},Et=class e{constructor(t){this.path=t;this.identity=hc("directory",t),this.name=_.basename(t)}path;identity;name;async getFile(t){Kt(t);let n=new Oe(_.join(this.path,t));try{return(await T.stat(n.path)).isFile()?n:void 0}catch{return}}async getLastModified(){try{return(await T.stat(this.path)).mtimeMs}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return;throw t}}async getDirectory(t){Kt(t);let n=new e(_.join(this.path,t));try{return(await T.stat(n.path)).isDirectory()?n:void 0}catch{return}}async list(){return(await T.readdir(this.path,{withFileTypes:!0})).map(n=>n.isDirectory()?new e(_.join(this.path,n.name)):new Oe(_.join(this.path,n.name),n.name))}async createFile(t){Kt(t),await T.mkdir(this.path,{recursive:!0});let n=new Oe(_.join(this.path,t));return await T.writeFile(n.path,"",{flag:"wx"}),n}async createDirectory(t){Kt(t),await T.mkdir(this.path,{recursive:!0});let n=new e(_.join(this.path,t));return await T.mkdir(n.path),n}async remove(t,n){Kt(t),await T.rm(_.join(this.path,t),{force:!0,recursive:n?.recursive===!0})}};function hc(e,t){return`node-${e}:${Buffer.from(_.resolve(t),"utf8").toString("base64url")}`}function Li(e,t){let n=`node-${t}:`;if(!e.startsWith(n))return _.isAbsolute(e)?e:void 0;try{return Buffer.from(e.slice(n.length),"base64url").toString("utf8")}catch{return}}function Kt(e){if(e.length===0||e==="."||e===".."||_.isAbsolute(e)||e.includes("/")||e.includes("\\"))throw new TypeError(`Directory child must be a relative name: ${e}`)}var Ni=class{constructor(t){this.database=t}database;async close(){await new Promise((t,n)=>{this.database.close(r=>{r?n(r):t()})})}async execute(t){await new Promise((n,r)=>{this.database.exec(t,a=>{a?r(a):n()})})}async queryAll(t,n=[]){return await new Promise((r,a)=>{this.database.all(t,[...n],(o,s)=>{o?a(o):r(s)})})}async queryOne(t,n=[]){return await new Promise((r,a)=>{this.database.get(t,[...n],(o,s)=>{o?a(o):r(s)})})}async run(t,n=[]){await new Promise((r,a)=>{this.database.run(t,[...n],o=>{o?a(o):r()})})}};async function Fy(e,t={}){let n=(t.readonly===!0?Jt.OPEN_READONLY:Jt.OPEN_READWRITE|Jt.OPEN_CREATE)|Jt.OPEN_FULLMUTEX;return new Ni(await My(e,n))}async function My(e,t){if(!(e instanceof Oe))throw new TypeError("The Node database adapter requires a NodeFile");return await new Promise((n,r)=>{let a=new Jt.Database(e.path,t,o=>{o?r(o):n(a)})})}async function Wy(e){if(!(e instanceof Oe))throw new TypeError("The Node ZIP adapter requires a NodeFile");let t=await new Promise((a,o)=>{mc.open(e.path,{autoClose:!1,lazyEntries:!0},(s,c)=>{s||c===void 0?o(s??new Error("Cannot open ZIP")):a(c)})}),n=await new Promise((a,o)=>{let s=new Map,c=u=>{t.close(),o(u instanceof Error?u:new Error("Cannot scan ZIP central directory"))},d=()=>{t.off("error",c),a(s)};t.on("entry",u=>{u.fileName.endsWith("/")||s.set(u.fileName,u),t.readEntry()}),t.once("error",c),t.once("end",d),t.readEntry()}),r=!1;return{close:()=>(r||t.close(),r=!0,Promise.resolve()),listEntries:()=>Promise.resolve([...n.keys()]),readEntry:async a=>{if(r)throw new Error("Cannot read a closed ZIP archive");let o=n.get(a);if(o!==void 0)return await jy(t,o)}}}async function jy(e,t){let n=await new Promise((r,a)=>{e.openReadStream(t,(o,s)=>{o||s===void 0?a(o??new Error(`Cannot read ZIP entry: ${t.fileName}`)):r(s)})});return await $y(n)}async function Gy(e,t){let n=new pc.ZipFile,r=await e.openWriter(),a=n.outputStream,o=qy(a,r),s={cancelled:!1,complete:!1},c;try{c=Uy(t);let d=By(n,c,s);if(await Promise.race([d,o]),!s.complete)throw new Error("ZIP output ended before entry production completed");n.end(),await o,await r.commit()}catch(d){throw s.cancelled=!0,!s.complete&&c!==void 0&&Hy(c),a.destroyed||a.destroy(d instanceof Error?d:new Error("Cannot write ZIP archive")),await o.catch(()=>{}),await r.abort().catch(()=>{}),d}}async function By(e,t,n){for(;!n.cancelled;){let r=await t.next();if(n.cancelled)return;if(r.done){n.complete=!0;return}e.addBuffer(Buffer.from(r.value.data),r.value.name,{compress:!1})}}function Uy(e){let t=e[Symbol.asyncIterator];return t===void 0?e[Symbol.iterator]():t.call(e)}function Hy(e){if(e.return!==void 0)try{Promise.resolve(e.return()).catch(()=>{})}catch{}}async function qy(e,t){await Oy(e,new Ly({write:(n,r,a)=>{t.write(n).then(()=>a(),o=>a(o instanceof Error?o:new Error("Cannot stream ZIP archive")))}}))}async function $y(e){return await new Promise((t,n)=>{let r=[];e.on("data",a=>r.push(a)),e.once("error",n),e.once("end",()=>t(Buffer.concat(r)))})}var Ky={asyncContext:{create:()=>new dc.AsyncLocalStorage},database:{open:Fy},lifecycle:{instanceId:`node-process:${Oi.pid}`,isInstanceAlive:e=>{let t=/^node-process:(\d+)$/u.exec(e);if(t===null)return Promise.resolve(void 0);try{return Oi.kill(Number(t[1]),0),Promise.resolve(!0)}catch(n){return typeof n=="object"&&n!==null&&"code"in n&&n.code==="ESRCH"?Promise.resolve(!1):Promise.resolve(!0)}}},resources:{getDirectory:e=>{let t=Li(e,"directory");return Promise.resolve(t===void 0?void 0:new Et(t))},getFile:e=>{let t=Li(e,"file");return Promise.resolve(t===void 0?void 0:new Oe(t))},resolveLegacyDirectory:e=>{let t=Li(e,"directory");return Promise.resolve(t===void 0?void 0:new Et(t))}},templates:{createEnvironment:e=>new Ny(new Di,e)},zip:{open:Wy,write:Gy}},Di=class extends Dy{getSource(t){let n=cc(t),r=_.join(Jy(),n);return{noCache:!1,path:n,src:lr.readFileSync(r,"utf8")}}};function Jy(){let e=globalThis.__WIKIGRAPH_DATA_DIR__;if(typeof e=="string"&&e!=="")return e;let t=Oi.cwd();for(;;){for(let r of[_.join(t,"data"),_.join(t,"packages","core","data")])try{if(lr.statSync(r).isDirectory())return r}catch{}let n=_.dirname(t);if(n===t)throw new Error("Could not locate data directory");t=n}}function fc(e=_.join(lc.homedir(),".wikigraph")){return{library:new Et(e),documentStore:new Et(_.join(e,"documents"))}}function yc(e){Ci(Ky),Ai(fc(e))}async function gc(e,t){return await Pi(e===void 0?void 0:fc(e),t)}var zy=["WIKIGRAPH_DEV","WIKIGRAPH_ENV_POLICY","WIKIGRAPH_QUEUE_DISABLE_AUTOSTART","WIKIGRAPH_STATE_DIR"];function Sc(e){let t=Xy(e.env);if(e.envPolicy==="production"){let o=zy.filter(s=>t[s]!==void 0);if(o.length>0)throw new Error(`Wiki Graph production entries do not support runtime env overrides: ${o.join(", ")}. Use the development entry or explicit SDK options instead.`);return{env:t,envPolicy:e.envPolicy,stateDir:e.stateDir}}let n=t.WIKIGRAPH_DEV??t.WIKIGRAPH_STATE_DIR,r=bc(e.stateDir??n);if(r===void 0)throw new Error("Wiki Graph development entries require an explicit runtime state directory.");let a=bc(e.devProjectRoot);if(a===void 0)throw new Error("Wiki Graph development entries require an explicit project root.");return{devProjectRoot:a,env:t,envPolicy:e.envPolicy,stateDir:r}}function Xy(e){return e===void 0?{...process.env}:{...process.env,...e}}function bc(e){if(!(e===void 0||e.trim()===""))return e}var wc="--wikigraph-internal-child",Ec="--wikigraph-state-dir";async function xc(e,t){let n=Qy(process.argv.slice(2));if(n.internalChild!==e)throw new Error("This Wiki Graph worker entry is internal.");let r=Sc({argv:n.argv,envPolicy:"production",stateDir:n.stateDir});return await gc(r.stateDir,()=>t(n))}function Qy(e){let t=[],n,r;for(let a=0;a<e.length;a+=1){let o=e[a];if(o===wc){if(a+1>=e.length)throw new Error(`${wc} requires a value.`);n=e[a+1],a+=1;continue}if(o!==Ec){t.push(o);continue}if(a+1>=e.length)throw new Error(`${Ec} requires a value.`);r=e[a+1],a+=1}return{argv:t,internalChild:n??"",stateDir:r}}Re();p();mr();import{z as Xt}from"zod";var tg=1,Bk=Xt.object({completedAt:Xt.string().optional(),startedAt:Xt.string(),status:Xt.enum(["running","succeeded"]),version:Xt.literal(tg)});Ui();Yt();ye();qi();$t();ci();Lt();jn();yt();bs();ze();se();Ce();k();at();Pt();k();hn();xr();wr();Ut();p();nt();Te();Be();Ge();es();wr();ci();qn();Ut();k();p();Ce();p();y();y();p();y();p();_a();p();ee();ze();ze();ze();ze();ze();Ke();yt();k();Bt();p();var $I={command:`${"wg"} --help`,config:`${"wg"} help config`,format:`${"wg"} help format`,readiness:`${"wg"} help readiness`,root:`${"wg"} --help`,runtime:`${"wg"} help runtime`,uri:`${"wg"} help uri`};import{extname as $7}from"path";import{createReadStream as nee}from"fs";import{mkdtemp as iee,rm as aee}from"fs/promises";import{join as see}from"path";import{tmpdir as dee}from"os";import{AsyncLocalStorage as KI}from"async_hooks";var X7=new KI;var JI=new Map(["at","seq","uri","id","jobId","type","target","label","labels","title","state","name","qid","predicate","subjectLabel","objectLabel","subjectQid","objectQid","score","queueRank","chapter","chapterId","fragment","fragmentId","position","range","start","end","shown","total","count","mentionCount","nodeCount","edgeCount","summaryCount","limit","order","match","lens","terms","types","chapters","query","objects","items","nextCursor","archiveKey","archivePath","workspacePath","eventsPath","createdAt","updatedAt","finishedAt","currentStep","step","phase","phaseDetail","phaseDone","phaseTotal","phaseUnit","words","totalWords","outputTokens","authors","language","identifier","publisher","publishedAt","sourceFormat","snippet","summary","description","recommendation","risk","error","message","children","nodes","links","neighbors","incoming","outgoing","nodeGroups","sourceFragments","sources","tree","meta","evidence","content","source","text","prompt","llmJSON","errorJSON"].map((e,t)=>[e,t]));function gy(e){return`${JSON.stringify(ec(e),null,2)}
|
|
801
|
+
`}function ec(e){if(e===null||typeof e=="boolean"||typeof e=="number"||typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>ec(t)??null);if(typeof e=="object")return zI(e)}function zI(e){let t=Object.entries(e).map(([r,a],o)=>({index:o,key:r,priority:JI.get(r)??Number.MAX_SAFE_INTEGER,value:a})),n={};for(let r of t.sort((a,o)=>a.priority-o.priority||a.index-o.index)){let a=ec(r.value);a!==void 0&&(n[r.key]=a)}return n}import{homedir as bee}from"os";import{isAbsolute as wee,relative as Eee,resolve as xee}from"path";import{existsSync as _ee,readFileSync as Tee}from"fs";import{dirname as Aee,join as Pee,parse as Lee,resolve as Oee}from"path";yc();var{tryRunWikiGraphGc:lk}=await Promise.resolve().then(()=>(Ry(),ky));async function mk(){await xc("gc-worker",async({argv:e})=>{let t=pk(e);process.stdout.write(gy(await lk({dryRun:t.dryRun,force:t.force})))})}function pk(e){return{dryRun:e.includes("--dry-run"),force:e.includes("--force")}}mk().catch(e=>{process.stderr.write(`${e instanceof Error?e.stack??e.message:String(e)}
|
|
572
802
|
`),process.exitCode=1});
|
|
573
803
|
//# sourceMappingURL=gc-worker.js.map
|