wiki-graph 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +917 -578
- package/dist/cli.js.map +1 -1
- package/dist/data/help/commands/library-predicate.jinja +19 -19
- package/dist/data/help/commands/library.jinja +20 -15
- package/dist/data/help/commands/predicate.jinja +108 -98
- package/dist/data/help/commands/root.jinja +1 -1
- package/dist/data/help/commands/uri.jinja +46 -19
- package/dist/data/help/topics/config.jinja +27 -0
- package/dist/data/help/topics/library.jinja +19 -12
- package/dist/data/help/topics/readiness.jinja +71 -34
- package/dist/data/help/topics/recipe.jinja +11 -8
- package/dist/data/help/topics/uri.jinja +12 -3
- package/dist/gc-worker.js +109 -84
- package/dist/gc-worker.js.map +1 -1
- package/dist/index.cjs +2681 -1283
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3886 -2468
- package/dist/index.js.map +1 -1
- package/dist/queue-worker.js +571 -355
- package/dist/queue-worker.js.map +1 -1
- package/package.json +3 -3
- package/dist/cli.d.ts +0 -1
- package/dist/gc-worker.d.ts +0 -1
- package/dist/index.d.cts +0 -36
- package/dist/queue-worker.d.ts +0 -1
package/dist/gc-worker.js
CHANGED
|
@@ -3,66 +3,66 @@ 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
|
|
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
7
|
INSERT INTO schema_versions (scope, version, updated_at)
|
|
8
8
|
VALUES (?, ?, ?)
|
|
9
9
|
ON CONFLICT(scope) DO UPDATE SET
|
|
10
10
|
version = excluded.version,
|
|
11
11
|
updated_at = excluded.updated_at
|
|
12
|
-
`,["home",t,new Date().toISOString()])}finally{await
|
|
12
|
+
`,["home",t,new Date().toISOString()])}finally{await n.close()}}async function Ay(){let e=Ue(),t=$(),n=pt(),r=ht();await te(L(e,"search-sessions.sqlite")),await te(L(e,"continuation-cursors.sqlite")),await te(Gt()),await te(L(t,"library")),await te(L(r,"gc.sqlite")),await te(L(r,"gc.last-run")),await te(L(n,"cache")),await te(L(n,"job.sqlite"));let a=L(t,"staging.sqlite");await ye(a)&&(await Wy(a),await jy(a))}async function Ly(){await Oy(),await Ny(),await Dy(),await Fy()}async function Oy(){let e=G();if(!await ye(e))return;let t=await k.open(e,"",{readonly:!0});try{if(await ne(t,"library_locks")&&(await t.queryAll(`
|
|
13
13
|
SELECT owner_pid, heartbeat_at
|
|
14
14
|
FROM library_locks
|
|
15
|
-
`,void 0,a=>({heartbeatAt:Number(a.heartbeat_at),ownerPid:Number(a.owner_pid)}))).some(a=>
|
|
15
|
+
`,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 library locks.");if(!await ne(t,"state_locks"))return;if((await t.queryAll(`
|
|
16
16
|
SELECT owner_pid, heartbeat_at
|
|
17
17
|
FROM state_locks
|
|
18
18
|
WHERE scope = 'library'
|
|
19
|
-
`,void 0,
|
|
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
20
|
SELECT owner_pid, heartbeat_at
|
|
21
|
-
FROM ${
|
|
22
|
-
`,void 0,a=>({heartbeatAt:Number(a.heartbeat_at),ownerPid:Number(a.owner_pid)}))).some(a=>
|
|
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
23
|
SELECT entry_path
|
|
24
24
|
FROM entry_overlays
|
|
25
|
-
`,void 0,a=>String(a.entry_path))).find(a
|
|
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
26
|
SELECT job_id
|
|
27
27
|
FROM build_jobs
|
|
28
28
|
WHERE state IN ('queued', 'running', 'canceling', 'paused')
|
|
29
|
-
`,void 0,
|
|
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
30
|
SELECT owner_pid, heartbeat_at
|
|
31
31
|
FROM build_worker_lease
|
|
32
32
|
WHERE id = 1
|
|
33
|
-
`,void 0,
|
|
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
34
|
SELECT archive_key, workspace_path
|
|
35
35
|
FROM entry_overlays
|
|
36
|
-
${
|
|
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
|
|
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
38
|
DELETE FROM entry_overlays
|
|
39
|
-
${
|
|
40
|
-
`,a)}finally{await
|
|
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
41
|
SELECT archive_key, archive_path, entry_path, workspace_path
|
|
42
42
|
FROM entry_overlays
|
|
43
|
-
WHERE entry_path IN (?, ?)
|
|
44
|
-
`,["database.db",
|
|
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
45
|
DELETE FROM entry_overlays
|
|
46
46
|
WHERE archive_key = ? AND entry_path = ?
|
|
47
|
-
`,[
|
|
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
48
|
SELECT 1 AS present
|
|
49
49
|
FROM sqlite_master
|
|
50
50
|
WHERE type = 'table' AND name = ?
|
|
51
|
-
`,[t],()=>!0)===!0}function
|
|
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=`
|
|
52
52
|
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
53
53
|
scope TEXT PRIMARY KEY,
|
|
54
54
|
version INTEGER NOT NULL,
|
|
55
55
|
updated_at TEXT NOT NULL
|
|
56
56
|
);
|
|
57
|
-
`});import{createHash as
|
|
58
|
-
`,{encoding:"utf8",mode:384}),await
|
|
59
|
-
`,{encoding:"utf8",mode:384})}async function
|
|
60
|
-
`)},ontext(p){
|
|
57
|
+
`});import{createHash as Uy}from"crypto";import{chmod as Hy,mkdir as Sd,readFile as wd,rename as qy,rm as Ed,stat as Id,writeFile as xd}from"fs/promises";import{dirname as $y,resolve as Ky}from"path";import{setTimeout as Jy}from"timers/promises";async function _(e,t,n={}){return await Qy(e),await kd(e,t),await k.open(e,"",n)}function Qy(e){return yd(e)?K():Promise.resolve()}async function kd(e,t){let n=Ky(e),r=ig(n),a=og(t);if(await bd(n,r,a)){await _e(n),await _e(r);return}await Sd($y(n),{recursive:!0}),await Yy(n,async()=>{if(await bd(n,r,a)){await _e(n),await _e(r);return}await k.initialize(n,t),await _e(n),await Vy(r,a)}),await _e(n),await _e(r)}async function Vy(e,t){let n=`${e}.${process.pid}.tmp`;await xd(n,`${t}
|
|
58
|
+
`,{encoding:"utf8",mode:384}),await qy(n,e),await _e(e)}async function Yy(e,t){let n=`${e}.init.lock`;for(;;)try{await Sd(n,{mode:448});break}catch(r){if(!h(r)||r.code!=="EEXIST")throw r;await Zy(n),await Jy(zy)}try{await gd(n);let r=setInterval(()=>{gd(n).catch(()=>{})},Xy);try{return await t()}finally{clearInterval(r)}}finally{await Ed(n,{force:!0,recursive:!0})}}async function gd(e){await xd(`${e}/owner.json`,`${JSON.stringify({at:Date.now(),pid:process.pid},null,2)}
|
|
59
|
+
`,{encoding:"utf8",mode:384})}async function Zy(e){try{let t=await eg(e);if(t!==void 0){if(rg(t.pid)||Date.now()-t.at<Ui)return}else if(!await ng(e,Ui))return;await Ed(e,{force:!0,recursive:!0})}catch(t){if(h(t)&&t.code==="ENOENT")return;throw t}}async function eg(e){try{let t=JSON.parse(await wd(`${e}/owner.json`,"utf8"));return tg(t)?t:void 0}catch(t){if(h(t)&&t.code==="ENOENT"||t instanceof SyntaxError)return;throw t}}function tg(e){if(typeof e!="object"||e===null)return!1;let t=e;return typeof t.pid=="number"&&Number.isInteger(t.pid)&&t.pid>0&&typeof t.at=="number"}async function ng(e,t){let n=await Id(e);return Date.now()-n.mtimeMs>=t}function rg(e){try{return process.kill(e,0),!0}catch(t){return!(h(t)&&t.code==="ESRCH")}}async function bd(e,t,n){if(!await ag(e))return!1;try{return(await wd(t,"utf8")).trim()===n}catch(r){if(h(r)&&r.code==="ENOENT")return!1;throw r}}function ig(e){return`${e}.initialized`}async function ag(e){try{return(await Id(e)).isFile()}catch(t){if(h(t)&&t.code==="ENOENT")return!1;throw t}}async function _e(e){try{await Hy(e,384)}catch(t){if(h(t)&&t.code==="ENOENT")return;throw t}}function og(e){return Uy("sha256").update(e).digest("hex")}var zy,Ui,Xy,Rd=i(()=>{"use strict";b();$e();f();zy=50,Ui=300*1e3,Xy=Math.floor(Ui/2)});import{mkdir as WR,readFile as jR,readdir as GR,rm as BR,writeFile as UR}from"fs/promises";var vd=i(()=>{"use strict";b()});function yt(e){let t=new Set(Object.values(e));return n=>t.has(n)}function qt(e,t){let n=yt(e);return r=>{if(n(r))return r;throw new Error(`Unknown ${t}: ${r}`)}}var Hi=i(()=>{"use strict"});var J,_d,Td,re,Cd,qi,ie=i(()=>{"use strict";Hi();J=(a=>(a.Verbatim="verbatim",a.Detailed="detailed",a.Focused="focused",a.Relevant="relevant",a))(J||{}),_d=yt(J),Td=qt(J,"chunk retention"),re=(r=>(r.Critical="critical",r.Important="important",r.Helpful="helpful",r))(re||{}),Cd=yt(re),qi=qt(re,"chunk importance")});var $i=i(()=>{"use strict";ie()});function Ke(e){let t=0,n=!1;for(let r of e.trim()){if(sg.test(r)){t+=1,n=!1;continue}if(dg.test(r)){n||(t+=1),n=!0;continue}n&&cg.test(r)||(n=!1)}return t}var sg,dg,cg,Je=i(()=>{"use strict";sg=new RegExp("\\p{Script=Han}","u"),dg=/[\p{Letter}\p{Number}]/u,cg=new RegExp("\\p{Mark}","u")});var Ad=i(()=>{"use strict";Je();ie()});var ur=i(()=>{"use strict"});import{join as cv,resolve as uv}from"path";var Ki=i(()=>{"use strict";ie();$i();Ad();ur()});import{join as bv,resolve as Sv}from"path";var Ld=i(()=>{"use strict";vd();Ki()});var Ji=i(()=>{"use strict";Ld();$i();Ki();ur()});import{mkdir as Cv,readFile as Pv,readdir as Av,writeFile as Lv}from"fs/promises";var lr=i(()=>{"use strict";b()});var zi=i(()=>{"use strict"});import{join as Fv,resolve as Wv}from"path";var Nd=i(()=>{"use strict";lr()});var Dd=i(()=>{"use strict";Je()});var Xi=i(()=>{"use strict"});import{join as Vv,resolve as Yv}from"path";var Qi=i(()=>{"use strict";b();zi();lr();Nd();Dd();Xi()});import{join as s_,resolve as d_}from"path";var Md=i(()=>{"use strict";lr();Qi()});var Vi=i(()=>{"use strict";Md();zi();Qi();Xi()});import{z}from"zod";var Fd,Te,Wd,jd,$t=i(()=>{"use strict";Fd=["epub","pdf","txt","markdown"],Te=1,Wd=z.enum(Fd),jd=z.object({version:z.literal(Te),sourceFormat:Wd,title:z.string().min(1).nullable(),authors:z.array(z.string().min(1)),language:z.string().min(1).nullable(),identifier:z.string().min(1).nullable(),publisher:z.string().min(1).nullable(),publishedAt:z.string().min(1).nullable(),description:z.string().min(1).nullable()})});import{randomBytes as w_}from"crypto";function Gd(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(fg.has(e))throw new Error(`Invalid ${t}: ${e} is reserved.`);return e}var fg,ge=i(()=>{"use strict";fg=new Set(["tree","source","summary","title","state","chunk","entity","triple","cover","meta","job","help"])});import{z as be}from"zod";var ze,Yi,Zi,mr=i(()=>{"use strict";ge();ze=1,Yi=be.object({key:be.string().min(1).transform(e=>Gd(e)).optional(),title:be.string().min(1).nullable().optional(),serialId:be.number().int().nonnegative().optional(),children:be.lazy(()=>be.array(Yi))}),Zi=be.object({version:be.literal(ze),items:be.array(Yi)})});import{createHash as yg}from"crypto";import{posix as ea}from"path";import{PassThrough as gg}from"stream";import{open as bg}from"yauzl";function Se(e){let t=e.replaceAll("\\","/").trim(),n=t.startsWith("/")?t.slice(1):t;return ea.normalize(n).replace(/^(\.\/)+/,"").replace(/^\/+/,"")}function Sg(e){let[t]=e.split("#",1);return Se(t??"")}function ta(e){if(e===void 0)return;let t=e.startsWith("#")?e.slice(1):e;return t===""?void 0:t}function Kt(e){let[t,n]=e.split("#",2);return{path:Se(t??""),fragment:ta(n)}}async function wg(e){return await new Promise((t,n)=>{bg(e,{autoClose:!1,lazyEntries:!0},(r,a)=>{if(r!==null||a===void 0){n(r??new Error(`Cannot open EPUB archive: ${e}`));return}t(a)})})}async function Eg(e){return await new Promise((t,n)=>{let r=new Map;e.on("entry",a=>{if(a.fileName.endsWith("/")){e.readEntry();return}r.set(Se(a.fileName),a),e.readEntry()}),e.once("end",()=>{t(r)}),e.once("error",a=>{n(a)}),e.readEntry()})}function Ig(e){if(Buffer.isBuffer(e))return e;if(typeof e=="string")return Buffer.from(e,"utf8");throw new Error("Unexpected ZIP stream chunk type")}async function Bd(e){return await new Promise((t,n)=>{let r=[];e.on("data",a=>{r.push(Ig(a))}),e.once("end",()=>{t(Buffer.concat(r))}),e.once("error",a=>{n(a)}),e.resume()})}async function xg(e,t){return await new Promise((n,r)=>{e.openReadStream(t,(a,o)=>{if(a!==null||o===void 0){r(a??new Error(`Cannot open EPUB entry: ${t.fileName}`));return}n(kg(o))})})}function kg(e){let t=new gg;return e.once("error",n=>{t.destroy(n)}),t.once("close",()=>{e.unpipe(t),e.destroyed||e.destroy()}),e.pipe(t),t}var pr,Jt=i(()=>{"use strict";pr=class e{#e;#t;#n;#r=!1;constructor(t,n,r){this.#e=t,this.#t=n,this.#n=r}static async open(t){let n=await wg(t),r=await Eg(n);return new e(t,n,r)}close(){return this.#r||(this.#r=!0,this.#t.close()),Promise.resolve()}hasEntry(t){return this.#n.has(Se(t))}listEntries(){return[...this.#n.keys()]}async openReadStream(t){let n=this.#i(t);return await xg(this.#t,n)}async readText(t){let n=await this.openReadStream(t);return(await Bd(n)).toString("utf8")}async readBuffer(t){let n=await this.openReadStream(t);return await Bd(n)}resolveRelativePath(t,n){let r=Sg(n);if(r==="")throw new Error(`Invalid EPUB href: ${n}`);return Se(ea.join(ea.dirname(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:${yg("sha1").update(`${r}:${n??""}`).digest("hex").slice(0,10)}`}get path(){return this.#e}#i(t){let n=Se(t),r=this.#n.get(n);if(r===void 0)throw new Error(`EPUB entry does not exist: ${n}`);return r}}});import{Parser as Rg}from"htmlparser2";async function Hd(e,t){let n=new Map;for(let[r,a]of t.entries()){let o=await e.openReadStream(r);o.setEncoding("utf8");let s=await qd(o,a);for(let d of a){let c=s.get(d.id)??"";n.set(d.id,{hasContent:c.trim()!=="",wordsCount:Ke(c)})}}return n}function _g(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 Tg(e,t){return await qd(e,t)}async function qd(e,t){let n=new Map,r=new Map,a=[...t],o=a.map(()=>""),d=a[0]?.fragment===void 0&&a[0]!==void 0?0:-1,c=0;a.forEach((p,w)=>{r.set(p.id,""),p.fragment!==void 0&&!n.has(p.fragment)&&n.set(p.fragment,w)});let m=new Rg({onopentag(p,w){let P=p.toLowerCase();if(Ud.has(P)){c+=1;return}let I=w.id??w["xml:id"];if(I!==void 0){let T=n.get(I);T!==void 0&&T>d&&(d=T)}P==="br"&&na(o,d,`
|
|
60
|
+
`)},ontext(p){c>0||na(o,d,p)},onclosetag(p){let w=p.toLowerCase();if(Ud.has(w)){c=Math.max(0,c-1);return}vg.has(w)&&na(o,d,`
|
|
61
61
|
|
|
62
|
-
`)}},{decodeEntities:!0});for await(let p of e)m.write(
|
|
62
|
+
`)}},{decodeEntities:!0});for await(let p of e)m.write(Pg(p));return m.end(),a.forEach((p,w)=>{r.set(p.id,Cg(o[w]??""))}),r}function na(e,t,n){t<0||t>=e.length||(e[t]+=n)}function Cg(e){return e.replace(/\r\n/gu,`
|
|
63
63
|
`).replace(/\n{3,}/gu,`
|
|
64
64
|
|
|
65
|
-
`).trim()}function Zy(e){if(typeof e=="string")return e;if(Buffer.isBuffer(e))return e.toString("utf8");throw new Error("Unexpected HTML stream chunk type")}function eg(e){return[...tg().segment(e)].filter(t=>t.isWordLike).length}function tg(){return Ji===void 0&&(Ji=new Intl.Segmenter(void 0,{granularity:"word"})),Ji}var Qy,_c,rn,Ji,Ac=i(()=>{"use strict";Qy=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"]),_c=new Set(["script","style"]),rn=class{#e;#t;constructor(t,r){this.#e=t,this.#t=Xy(r)}async openSection(t){let r=this.#t.get(t);return r===void 0?[]:[(await this.#r(r.path,r.targets)).get(t)??""]}async#r(t,r){let n=await this.#e.openReadStream(t);return n.setEncoding("utf8"),await Vy(n,r)}}});import{SaxesParser as rg}from"saxes";function Ht(e){let t=new rg({xmlns:!1}),r=[],n;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=r[r.length-1];s===void 0?n=o:s.children.push(o),r.push(o)}),t.on("text",a=>{let o=r[r.length-1];o!==void 0&&(o.text+=a)}),t.on("cdata",a=>{let o=r[r.length-1];o!==void 0&&(o.text+=a)}),t.on("closetag",()=>{r.pop()}),t.write(e).close(),n===void 0)throw new Error("XML document is empty");return Lc(n)}function z(e,t){return e.children.find(r=>$t(r.name)===t)}function _e(e,t){return e.children.filter(r=>$t(r.name)===t)}function D(e,t){return e.attributes[t]}function qt(e){let t=e.text;for(let r of e.children)t+=qt(r);return t}function $t(e){let t=e.indexOf(":");return t===-1?e:e.slice(t+1)}function Lc(e){return{name:e.name,attributes:Object.freeze({...e.attributes}),children:Object.freeze(e.children.map(Lc)),text:e.text}}var zi=i(()=>{"use strict"});import{posix as ng}from"path";async function Nc(e){let t=await og(e),r=Ht(await e.readText(t)),n=sg(D(r,"version")),a=z(r,"metadata"),o=z(r,"manifest"),s=z(r,"spine"),c=z(r,"guide"),d=dg(e,t,o),m=new Map([...d.values()].map(Re=>[Re.path,Re])),p=ug(s,d),S=lg(e,t,c),C=cg(a),E=mg(d),T=pg(d,s),O=hg(d,a);return{version:n,opfPath:t,metadata:{...C,version:Te,sourceFormat:"epub"},manifest:d,manifestByPath:m,spine:p,navPath:E,ncxPath:T,coverPath:O?.path,coverMediaType:O?.mediaType,guideNonContentPaths:S}}async function og(e){let t=Ht(await e.readText("META-INF/container.xml")),n=z(t,"rootfiles")?.children.find(o=>$t(o.name)==="rootfile"),a=n===void 0?void 0:D(n,"full-path");if(a===void 0||a.trim()==="")throw new Error("EPUB container.xml does not contain an OPF rootfile");return a}function sg(e){return e?.trim().startsWith("3")===!0?3:2}function cg(e){if(e===void 0)return Oc();let t=$e(e,"title"),r=$e(e,"creator"),n=$e(e,"language"),a=$e(e,"identifier"),o=$e(e,"publisher"),s=$e(e,"date"),c=$e(e,"description");return{...Oc(),title:t[0]??null,authors:[...r],language:n[0]??null,identifier:a[0]??null,publisher:o[0]??null,publishedAt:s[0]??null,description:c[0]??null}}function Oc(){return{version:Te,sourceFormat:"epub",title:null,authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null}}function $e(e,t){return e.children.filter(r=>$t(r.name)===t).map(r=>yg(qt(r))).filter(r=>r!==void 0)}function dg(e,t,r){let n=new Map;if(r===void 0)return n;for(let a of _e(r,"item")){let o=D(a,"id"),s=D(a,"href"),c=D(a,"media-type")??"";o===void 0||s===void 0||n.set(o,{id:o,path:e.resolveRelativePath(t,s),mediaType:c,properties:new Set((D(a,"properties")??"").split(/\s+/u).map(d=>d.trim().toLowerCase()).filter(d=>d!==""))})}return n}function ug(e,t){return e===void 0?[]:_e(e,"itemref").map(r=>D(r,"idref")).filter(r=>r!==void 0).map(r=>{let n=t.get(r);if(!(n===void 0||n.mediaType!=="application/xhtml+xml"&&n.mediaType!=="text/html"))return{idref:r,path:n.path,mediaType:n.mediaType}}).filter(r=>r!==void 0)}function lg(e,t,r){let n=new Set;if(r===void 0)return n;for(let a of _e(r,"reference")){let o=(D(a,"type")??"").trim().toLowerCase();if(!ag.has(o))continue;let s=D(a,"href");s===void 0||s.trim()===""||n.add(e.resolveRelativePath(t,Bt(s).path))}return n}function mg(e){for(let t of e.values())if(t.properties.has("nav"))return t.path}function pg(e,t){let r=t===void 0?void 0:D(t,"toc");if(r!==void 0)return e.get(r)?.path;for(let n of e.values())if(n.mediaType==="application/x-dtbncx+xml")return n.path}function hg(e,t){for(let n of e.values())if(n.properties.has("cover-image")&&n.mediaType.startsWith("image/"))return n;let r=fg(t);if(r!==void 0){let n=e.get(r);if(n?.mediaType.startsWith("image/")===!0)return n}for(let n of ig){let a=e.get(n);if(a?.mediaType.startsWith("image/")===!0)return a}for(let n of e.values())if(n.mediaType.startsWith("image/")&&ng.basename(n.path).toLowerCase().includes("cover"))return n}function fg(e){if(e!==void 0)for(let t of _e(e,"meta")){if((D(t,"name")??"").trim().toLowerCase()!=="cover")continue;let n=D(t,"content");if(n!==void 0&&n.trim()!=="")return n.trim()}}function yg(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var ig,ag,Qi=i(()=>{"use strict";Gt();Ut();zi();ig=["cover-image","coverimg","cover-img","cover"],ag=new Set(["cover","titlepage","other.titlepage"])});import{basename as gg,extname as bg,posix as Sg}from"path";import{parseDocument as wg}from"htmlparser2";async function Mc(e,t){let r=await kg(e,t)??await Ig(e,t)??[];if(r.length===0)return Dc(t,new Set);let n=jc(r),a=Dc(t,n);return a.length===0?r:[...r,...a]}async function kg(e,t){if(t.navPath===void 0)return;let r=wg(await e.readText(t.navPath),{decodeEntities:!0}),n=Xi(r,s=>Ce(s)!=="nav"?!1:(s.attribs?.["epub:type"]??s.attribs?.type??"").split(/\s+/u).map(d=>d.trim().toLowerCase()).includes("toc"));if(n===void 0)return;let a=Xi(n,s=>Ce(s)==="ol");if(a===void 0)return;let o=Kt(a).filter(s=>Ce(s)==="li").map(s=>Wc(e,t.navPath,s)).filter(s=>s!==void 0);return o.length===0?void 0:o}async function Ig(e,t){if(t.ncxPath===void 0)return;let r=Ht(await e.readText(t.ncxPath)),n=z(r,"navMap");if(n===void 0)return;let a=_e(n,"navPoint").map(o=>Fc(e,t.ncxPath,o)).filter(o=>o!==void 0);return a.length===0?void 0:a}function Wc(e,t,r){let n=Gc(r),a=n===void 0?void 0:Hc(Bc(n)),o=n!==void 0&&Ce(n)==="a"?n.attribs?.href:void 0,s=Rg(e,t,o),c=Kt(r).filter(d=>Ce(d)==="ol").flatMap(d=>Kt(d)).filter(d=>Ce(d)==="li").map(d=>Wc(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 Fc(e,t,r){let n=z(r,"navLabel"),a=n===void 0?void 0:z(n,"text"),o=a===void 0?void 0:Hc(qt(a)),s=z(r,"content"),c=vg(e,t,D(s??r,"src")),d=_e(r,"navPoint").map(m=>Fc(e,t,m)).filter(m=>m!==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 Dc(e,t){return e.spine.filter(r=>!xg(e,r.path,t)).filter(r=>!t.has(r.path)).map(r=>({title:Tg(r.path),path:r.path,fragment:void 0,children:[]}))}function xg(e,t,r){if(e.guideNonContentPaths.has(t))return!0;let n=Sg.basename(t).toLowerCase();return e.navPath===t?!0:Eg.has(n)&&!r.has(t)}function jc(e){let t=new Set;for(let r of e){r.path!==void 0&&t.add(r.path);for(let n of jc(r.children))t.add(n)}return t}function Rg(e,t,r){if(r===void 0||r.trim()==="")return{path:void 0,fragment:void 0};let{path:n,fragment:a}=Bt(r);return{path:n===""?void 0:e.resolveRelativePath(t,n),fragment:a}}function vg(e,t,r){if(r===void 0||r.trim()==="")return{path:void 0,fragment:void 0};let{path:n,fragment:a}=Bt(r);return{path:n===""?void 0:e.resolveRelativePath(t,n),fragment:a}}function Xi(e,t){if(Uc(e)&&t(e))return e;for(let r of e.children??[]){let n=Xi(r,t);if(n!==void 0)return n}}function Gc(e){for(let t of Kt(e)){let r=Ce(t);if(r==="a"||r==="span")return t}for(let t of Kt(e)){if(Ce(t)==="ol")continue;let r=Gc(t);if(r!==void 0)return r}}function Bc(e){return e.type==="text"?e.data??"":(e.children??[]).map(Bc).join("")}function Kt(e){return(e.children??[]).filter(Uc)}function Uc(e){return e.type==="tag"||e.type==="script"||e.type==="style"}function Ce(e){return e.name?.toLowerCase()}function Tg(e){let t=gg(e,bg(e)).trim();return t===""?void 0:t}function Hc(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var Eg,qc=i(()=>{"use strict";Ut();Qi();zi();Eg=new Set(["toc.xhtml","nav.xhtml","contents.xhtml","tableofcontents.xhtml","cover.xhtml","titlepage.xhtml"])});function _g(e){if(e.hasEntry("META-INF/encryption.xml"))throw new Error("Encrypted EPUB is not supported: found META-INF/encryption.xml.")}async function Cg(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 Pg(e,t){let r=new Map;return t.map((n,a)=>$c(e,n,[a],r))}function $c(e,t,r,n){let a=$i(t.fragment),o=t.path===void 0?`toc:${r.join(".")}`:e.createSectionId(t.path,a),s=Ag(o,n);return{hasContent:t.path!==void 0,id:s,title:t.title,path:t.path,fragment:a,wordsCount:0,children:t.children.map((c,d)=>$c(e,c,[...r,d],n))}}function Kc(e,t){return e.map(r=>{let n=t.get(r.id);return{...r,hasContent:n?.hasContent??!1,wordsCount:n?.wordsCount??0,children:Kc(r.children,t)}})}function Ag(e,t){let r=t.get(e)??0;return t.set(e,r+1),r===0?e:`${e}~${r+1}`}function Lg(e){let t=new Map;for(let r of Jc(e)){if(r.path===void 0)continue;let n=t.get(r.path)??[];n.push({id:r.id,path:r.path,fragment:r.fragment}),t.set(r.path,n)}return t}function Jc(e){let t=[];for(let r of e)t.push(r),t.push(...Jc(r.children));return t}var Vi,Yi,Jt,Zi,zc=i(()=>{"use strict";Ut();Ut();Ac();qc();Qi();Vi=class e{#e;#t;constructor(t,r){this.#e=t,this.#t=r}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)}},Yi=class e{#e;#t;#r;#n;constructor(t,r,n,a){this.#e=t,this.#t=r,this.#r=n,this.#n=a}static async open(t){_g(t);let r=await Nc(t),n=await Mc(t,r),a=Pg(t,n),o=Lg(a),s=await Cc(t,o),c=Kc(a,s),d=await Cg(t,r);return new e(r,d,c,new rn(t,o))}readMeta(){return Promise.resolve(this.#e.metadata)}readCover(){return Promise.resolve(this.#t)}readSections(){return Promise.resolve(this.#r.map(t=>new Vi(this,t)))}async openSection(t){return await this.#n.openSection(t)}},Jt=class{get format(){return"epub"}async openSession(t,r){let n=await tn.open(t);try{let a=await Yi.open(n);return await r(a)}finally{await n.close()}}},Zi=new Jt});var Qc=i(()=>{"use strict";zc()});import{createReadStream as Og}from"fs";import{stat as Ng}from"fs/promises";import{basename as Dg,extname as Mg,resolve as ra}from"path";function Fg(e){let t=Dg(e,Mg(e)).trim();return t===""?null:t}var Wg,ea,ta,zt,Xc,Vc,Yc=i(()=>{"use strict";Gt();Wg="root",ea=class{#e;#t;constructor(t,r=Wg){this.#e=ra(t),this.#t=r}get id(){return this.#t}get hasContent(){return!0}get title(){}get children(){return[]}open(){return Promise.resolve(Og(this.#e,{encoding:"utf8"}))}},ta=class{#e;#t;#r;constructor(t,r){this.#e=ra(t),this.#t=r,this.#r=new ea(this.#e)}readMeta(){return Promise.resolve({version:Te,sourceFormat:this.#t,title:Fg(this.#e),authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null})}readCover(){return Promise.resolve(void 0)}readSections(){return Promise.resolve([this.#r])}},zt=class{#e;constructor(t){this.#e=t}get format(){return this.#e}async openSession(t,r){let n=ra(t);if(!(await Ng(n)).isFile())throw new Error(`Source file is not a regular file: ${n}`);return await r(new ta(n,this.#e))}},Xc=new zt("txt"),Vc=new zt("markdown")});var Ke=i(()=>{"use strict";Gt();Qc();Yc();en()});var na=i(()=>{"use strict"});var Qt=i(()=>{"use strict";Ke();ye();na()});var Zc,ed,Xt=i(()=>{"use strict";Zc=`
|
|
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=`
|
|
66
66
|
id INTEGER PRIMARY KEY,
|
|
67
67
|
archive_id INTEGER NOT NULL,
|
|
68
68
|
kind INTEGER NOT NULL,
|
|
@@ -72,14 +72,14 @@ var i=(e,t)=>()=>(e&&(t=e(e=0)),t);import{AsyncLocalStorage as Qs}from"async_hoo
|
|
|
72
72
|
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
73
73
|
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
74
74
|
UNIQUE(archive_id, kind, chapter_id, sentence_index)
|
|
75
|
-
`,
|
|
75
|
+
`,hc=`
|
|
76
76
|
CREATE TABLE IF NOT EXISTS search_index_state (
|
|
77
77
|
key TEXT PRIMARY KEY,
|
|
78
78
|
value TEXT NOT NULL
|
|
79
79
|
);
|
|
80
80
|
|
|
81
81
|
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
82
|
-
${
|
|
82
|
+
${pc}
|
|
83
83
|
);
|
|
84
84
|
|
|
85
85
|
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
@@ -92,6 +92,22 @@ ${Zc}
|
|
|
92
92
|
tokenize='ascii'
|
|
93
93
|
);
|
|
94
94
|
|
|
95
|
+
CREATE TABLE IF NOT EXISTS text_embedding_segments (
|
|
96
|
+
id INTEGER PRIMARY KEY,
|
|
97
|
+
archive_id INTEGER NOT NULL,
|
|
98
|
+
kind INTEGER NOT NULL,
|
|
99
|
+
chapter_id INTEGER NOT NULL,
|
|
100
|
+
start_sentence_index INTEGER NOT NULL,
|
|
101
|
+
end_sentence_index INTEGER NOT NULL,
|
|
102
|
+
words_count INTEGER NOT NULL,
|
|
103
|
+
model TEXT NOT NULL,
|
|
104
|
+
dimensions INTEGER NOT NULL,
|
|
105
|
+
vector BLOB NOT NULL
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
CREATE INDEX IF NOT EXISTS idx_text_embedding_segments_scope
|
|
109
|
+
ON text_embedding_segments(archive_id, kind, chapter_id, start_sentence_index, end_sentence_index);
|
|
110
|
+
|
|
95
111
|
CREATE TABLE IF NOT EXISTS search_object_properties_records (
|
|
96
112
|
id INTEGER PRIMARY KEY,
|
|
97
113
|
archive_id INTEGER NOT NULL,
|
|
@@ -120,7 +136,7 @@ ${Zc}
|
|
|
120
136
|
updated_at INTEGER NOT NULL,
|
|
121
137
|
PRIMARY KEY (archive_id, chapter_id)
|
|
122
138
|
);
|
|
123
|
-
`});var
|
|
139
|
+
`});var Ve=i(()=>{"use strict";f();ie()});var fc=i(()=>{"use strict";f();Ve()});var yc=i(()=>{"use strict";f()});import{createHash as PT}from"crypto";var fr=i(()=>{"use strict"});var bc=i(()=>{"use strict";f();fr()});var Sc=i(()=>{"use strict";f();ie()});var wc=i(()=>{"use strict";f();Ve()});var Ec=i(()=>{"use strict";f();Ve()});var Ic=i(()=>{"use strict";f();Ve()});var xc=i(()=>{"use strict";f();Ve()});var kc=i(()=>{"use strict";f();Ve()});var Rc=i(()=>{"use strict";f()});var vc=i(()=>{"use strict";f()});var _c=i(()=>{"use strict";f()});var Tc=i(()=>{"use strict";Rc();vc();_c()});var fa=i(()=>{"use strict";fc();yc();bc();Sc();wc();Ec();Ic();xc();kc();Tc()});var ya=i(()=>{"use strict"});import{mkdir as AC,readFile as LC,readdir as OC,rm as NC,unlink as DC,writeFile as MC}from"fs/promises";import{join as WC}from"path";var ga=i(()=>{"use strict";b()});import{join as UC}from"path";var Bc=i(()=>{"use strict";b()});var Ye=i(()=>{"use strict"});import{stat as JC}from"fs/promises";import{join as XC,resolve as QC}from"path";var Uc=i(()=>{"use strict";b();f();tn();Ye()});var Hc=i(()=>{"use strict";b();ya()});var qc=i(()=>{"use strict"});import{AsyncLocalStorage as vP}from"async_hooks";import{join as TP,resolve as CP}from"path";import{z as ba}from"zod";var $P,Jc=i(()=>{"use strict";$t();mr();en();f();Ji();tn();fa();ie();ya();ga();Bc();Uc();Hc();qc();$P=ba.object({mediaType:ba.string().min(1),path:ba.string()})});var zc=i(()=>{"use strict"});var Sa=i(()=>{"use strict";Jc();ga();zc()});var Qc=i(()=>{"use strict";Vi()});var x=i(()=>{"use strict";f();Rd();Ji();Vi();Sa();Qc();tn();fa();ie()});var wa,Ze=i(()=>{"use strict";wa=`
|
|
124
140
|
CREATE TABLE IF NOT EXISTS search_sessions (
|
|
125
141
|
session_id TEXT PRIMARY KEY,
|
|
126
142
|
archive_key TEXT NOT NULL,
|
|
@@ -224,43 +240,43 @@ ON search_sessions(expires_at);
|
|
|
224
240
|
|
|
225
241
|
CREATE INDEX IF NOT EXISTS idx_search_sessions_prune
|
|
226
242
|
ON search_sessions(accessed_at DESC, created_at DESC, session_id);
|
|
227
|
-
`});import{join as
|
|
243
|
+
`});import{join as bb}from"path";import{rm as Yc}from"fs/promises";async function gt(){let e=Ia(),t=await _(e,wa);return Ea===e?t:await wb(t)?(Ea=e,t):(await t.close(),await Yc(e,{force:!0}),await Yc(`${e}.initialized`,{force:!0}),Ea=e,await _(e,wa))}function Ia(){return bb(Sb(),"search-sessions.sqlite")}function Sb(){return Ue()}async function wb(e){return await yr(e,"search_entity_hits","archive_id")&&await yr(e,"search_chunk_hits","archive_id")&&await yr(e,"search_triple_hits","archive_id")&&await yr(e,"search_evidence_hit_events","archive_id")}async function yr(e,t,n){return(await e.queryAll(`PRAGMA table_info(${t})`,void 0,a=>String(a.name))).includes(n)}var Ea,bt=i(()=>{"use strict";D();x();Ze()});var gr=i(()=>{"use strict"});async function Zc(e,t){await eu(e,t),await e.run("DELETE FROM search_sessions WHERE session_id = ?",[t])}async function eu(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 tu(e){await e.run(`
|
|
228
244
|
DELETE FROM predicate_dictionary
|
|
229
245
|
WHERE NOT EXISTS (
|
|
230
246
|
SELECT 1
|
|
231
247
|
FROM search_triple_hits
|
|
232
248
|
WHERE search_triple_hits.predicate_id = predicate_dictionary.id
|
|
233
249
|
)
|
|
234
|
-
`)}var
|
|
250
|
+
`)}var xa=i(()=>{"use strict";f();bt();gr();Ze()});import{stat as xb}from"fs/promises";async function br(e){let t=Ia(),n=await nu(t),r=await gt();try{let a=await r.queryOne("SELECT COUNT(*) AS count FROM search_sessions",void 0,m=>g(m,"count")),o=e.force?await Rb(r):await kb(r,e.now),s=e.force?[]:await vb(r),d=[...new Set([...o,...s])].sort();!e.dryRun&&d.length>0&&(await r.transaction(async()=>{for(let m of d)await Zc(r,m);await tu(r)}),await r.run("VACUUM"));let c=await nu(t);return{freedBytes:Math.max(0,n-c),removed:d.length,scanned:a??0}}finally{await r.close()}}async function kb(e,t){return await e.queryAll(`
|
|
235
251
|
SELECT session_id
|
|
236
252
|
FROM search_sessions
|
|
237
253
|
WHERE expires_at < ?
|
|
238
|
-
`,[t],
|
|
254
|
+
`,[t],n=>E(n,"session_id"))}async function Rb(e){return await e.queryAll("SELECT session_id FROM search_sessions",void 0,t=>E(t,"session_id"))}async function vb(e){return await e.queryAll(`
|
|
239
255
|
SELECT session_id
|
|
240
256
|
FROM search_sessions
|
|
241
257
|
ORDER BY accessed_at DESC, created_at DESC, session_id
|
|
242
258
|
LIMIT -1 OFFSET ?
|
|
243
|
-
`,[500],t=>
|
|
259
|
+
`,[500],t=>E(t,"session_id"))}async function nu(e){try{return(await xb(e)).size}catch(t){if(h(t)&&t.code==="ENOENT")return 0;throw t}}var ru=i(()=>{"use strict";f();b();bt();Ze();xa()});var iu=i(()=>{"use strict";Ze()});var ka=i(()=>{"use strict"});var Sr=i(()=>{"use strict";f();bt();gr();iu();ka()});import{createHash as KA}from"crypto";var ou=i(()=>{"use strict";Ze()});var su=i(()=>{"use strict";f();Wi();bt();Sr();ou();gr();Ze();xa()});var du=i(()=>{"use strict";f();bt();Sr()});var et=i(()=>{"use strict";Wi();ru();Sr();su();du();ka()});var Er=i(()=>{"use strict"});var yu=i(()=>{"use strict"});var va=i(()=>{"use strict"});var Ir=i(()=>{"use strict"});var _a=i(()=>{"use strict";Ir()});var Ta=i(()=>{"use strict";Ir()});var kr=i(()=>{"use strict";Er();va();_a();Ta()});var Pa=i(()=>{"use strict"});import{isAbsolute as ZL,relative as eO,resolve as tO}from"path";import{homedir as rO}from"os";var Le=i(()=>{"use strict";Pa()});var Iu=i(()=>{"use strict";Le()});var xu=i(()=>{"use strict";Er();yu();va();Ir();kr();_a();Ta();Iu()});var v=i(()=>{"use strict";xu()});var Aa=i(()=>{"use strict"});var La=i(()=>{"use strict";en();ma();Aa()});var ku=i(()=>{"use strict";La()});var W=i(()=>{"use strict";La();ku();Aa()});import{createHash as MO}from"crypto";var FO,WO,Oe=i(()=>{"use strict";FO=new RegExp("\\p{Script=Han}+","gu"),WO=new RegExp("^\\p{Script=Han}+$","u")});import{createHash as BO}from"crypto";async function sn(e){return await e.queryOne(`
|
|
244
260
|
SELECT value
|
|
245
261
|
FROM search_index_state
|
|
246
262
|
WHERE key = 'fingerprint'
|
|
247
|
-
`,void 0,t=>String(t.value))}var
|
|
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(`
|
|
248
264
|
INSERT OR IGNORE INTO qid_cache (
|
|
249
265
|
qid, language, label, description, pages_json, checked_at, updated_at
|
|
250
266
|
)
|
|
251
267
|
SELECT qid, 'en', label, description, pages_json, checked_at, updated_at
|
|
252
268
|
FROM qid_cache_legacy
|
|
253
|
-
`),await e.run("DROP TABLE qid_cache_legacy"))})}async function
|
|
269
|
+
`),await e.run("DROP TABLE qid_cache_legacy"))})}async function _S(e){(await kn(e,"disambiguation_cache")).has("wiki")||await e.transaction(async()=>{(await kn(e,"disambiguation_cache")).has("wiki")||(await e.run("ALTER TABLE disambiguation_cache RENAME TO disambiguation_cache_legacy"),await e.run(ul),await e.run(`
|
|
254
270
|
INSERT OR IGNORE INTO disambiguation_cache (
|
|
255
271
|
qid, wiki, pages_json, profile_json, checked_at
|
|
256
272
|
)
|
|
257
273
|
SELECT qid, 'enwiki', pages_json, profile_json, checked_at
|
|
258
274
|
FROM disambiguation_cache_legacy
|
|
259
|
-
`),await e.run("DROP TABLE disambiguation_cache_legacy"))}),(await
|
|
275
|
+
`),await e.run("DROP TABLE disambiguation_cache_legacy"))}),(await kn(e,"disambiguation_cache")).has("profile_error_json")||await e.run("ALTER TABLE disambiguation_cache ADD COLUMN profile_error_json TEXT")}async function kn(e,t){let n=await e.queryAll(`PRAGMA table_info(${t})`,void 0,r=>PS(r.name,"name"));return new Set(n)}async function TS(e){let t=await jr(e,"qid_cache"),n=await jr(e,"disambiguation_cache");return t+n}async function CS(e,t){let n=await jr(e,"qid_cache",t),r=await jr(e,"disambiguation_cache",t);return n+r}async function jr(e,t,n){return await e.queryOne(`
|
|
260
276
|
SELECT COUNT(*) AS count
|
|
261
277
|
FROM ${t}
|
|
262
|
-
${
|
|
263
|
-
`,
|
|
278
|
+
${n===void 0?"":"WHERE checked_at < ?"}
|
|
279
|
+
`,n===void 0?void 0:[n],r=>g(r,"count"))??0}async function dl(e){try{return(await ES(e)).size}catch(t){if(h(t)&&t.code==="ENOENT")return;throw t}}function PS(e,t){if(typeof e!="string")throw new Error(`Expected ${t} to be a string.`);return e}var cl,ul,IS,xS,Va=i(()=>{"use strict";D();f();x();b();cl=`
|
|
264
280
|
CREATE TABLE IF NOT EXISTS qid_cache (
|
|
265
281
|
qid TEXT NOT NULL,
|
|
266
282
|
language TEXT NOT NULL,
|
|
@@ -271,7 +287,7 @@ CREATE TABLE IF NOT EXISTS qid_cache (
|
|
|
271
287
|
updated_at TEXT NOT NULL,
|
|
272
288
|
PRIMARY KEY (qid, language)
|
|
273
289
|
);
|
|
274
|
-
`,
|
|
290
|
+
`,ul=`
|
|
275
291
|
CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
276
292
|
qid TEXT NOT NULL,
|
|
277
293
|
wiki TEXT NOT NULL,
|
|
@@ -281,51 +297,51 @@ CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
|
281
297
|
checked_at TEXT NOT NULL,
|
|
282
298
|
PRIMARY KEY (qid, wiki)
|
|
283
299
|
);
|
|
284
|
-
`,
|
|
285
|
-
${
|
|
300
|
+
`,IS=`
|
|
301
|
+
${cl}
|
|
286
302
|
|
|
287
303
|
CREATE INDEX IF NOT EXISTS idx_qid_cache_checked_at
|
|
288
304
|
ON qid_cache(checked_at);
|
|
289
305
|
|
|
290
|
-
${
|
|
306
|
+
${ul}
|
|
291
307
|
|
|
292
308
|
CREATE INDEX IF NOT EXISTS idx_disambiguation_cache_checked_at
|
|
293
309
|
ON disambiguation_cache(checked_at);
|
|
294
|
-
|
|
295
|
-
prev: ${
|
|
296
|
-
text: ${
|
|
297
|
-
next: ${
|
|
298
|
-
`);return{candidates:a,code:
|
|
310
|
+
`,xS=720*60*60*1e3});var Ya=i(()=>{"use strict"});var Za=i(()=>{"use strict"});import{jsonrepair as KW}from"jsonrepair";var ll=i(()=>{"use strict"});var ml=i(()=>{"use strict";Za();Ya();ll()});var U=i(()=>{"use strict";Ya();Za();ml()});import{z as H}from"zod";var NS,fj,pl=i(()=>{"use strict";U();NS=H.object({category:H.string().min(1).optional(),information:H.string(),name:H.string().min(1),priority:H.enum(["primary","secondary","other"]),qid:H.union([H.string(),H.null()])}).strict(),fj=H.object({meanings:H.array(NS),sourceQid:H.string().regex(/^Q[1-9]\d*$/u),surface:H.string().min(1).optional()}).strict()});var no=i(()=>{"use strict"});import{AsyncLocalStorage as DS}from"async_hooks";import{randomUUID as wj}from"crypto";import{existsSync as Ij,mkdirSync as xj}from"fs";import{dirname as Rj,join as vj,resolve as _j}from"path";import MS,{multistream as Cj}from"pino";import Aj from"pino-pretty";var Lj,Oj,Y=i(()=>{"use strict";Lj=new DS,Oj=MS({enabled:!1})});import{createHash as Fj}from"crypto";import{appendFile as jj}from"fs/promises";var hl=i(()=>{"use strict";Y();b()});var Gr=i(()=>{"use strict"});import{DomUtils as $j,parseDocument as Kj}from"htmlparser2";var ro=i(()=>{"use strict";Gr()});var fl=i(()=>{"use strict"});var io=i(()=>{"use strict";Gr()});var bl=i(()=>{"use strict";Y();b();no();ro();Gr();fl();io()});var ao=i(()=>{"use strict";bl();ro();io()});var CG,El=i(()=>{"use strict";Va();hl();U();ao();CG=360*60*1e3});var vn=i(()=>{"use strict";Va();pl();no();El();ao()});var so=i(()=>{"use strict";vn()});var xl=i(()=>{"use strict";hn();v();ce();Fr();Xa();Wr();xn();so();O()});var Br=i(()=>{"use strict";xl();Xa();Fr();Wr();xn();so()});var co=i(()=>{"use strict";Le();W();hn();v();ce();Sn();In();Q();Br();O();se()});var kl=i(()=>{"use strict";co();Br();O()});var wo=i(()=>{"use strict";sl();co();Br();nt();O();Ja();En();kl()});var _l=i(()=>{"use strict";wo()});import{randomBytes as H0}from"crypto";import{join as $0}from"path";var $S,Tl=i(()=>{"use strict";D();f();x();$S=10080*60*1e3});var Cl=i(()=>{"use strict"});var Pl=i(()=>{"use strict";f();Tl();Cl()});var Al=i(()=>{"use strict"});var Ll=i(()=>{"use strict";Pl();Al()});var wt=i(()=>{"use strict";_l();Ll();et()});import{existsSync as Ml,statSync as Fl}from"fs";import{fileURLToPath as KS}from"url";import{dirname as Wl,join as Dl,parse as JS,resolve as Eo}from"path";function $r(){let e=globalThis.__WIKIGRAPH_DATA_DIR__;if(typeof e=="string"&&e!=="")return e;let t=zS();return t!==void 0?t:XS()}function zS(){let e=Wl(KS(import.meta.url));for(let t of[Eo(e,"../../../data"),Eo(e,"../../data"),Eo(e,"../data")])if(Ml(t)&&Fl(t).isDirectory())return t}function XS(){let e=process.cwd(),t=JS(e).root;for(;;){for(let n of[Dl(e,"data"),Dl(e,"packages","core","data")])if(Ml(n)&&Fl(n).isDirectory())return n;if(e===t)throw new Error("Could not locate data directory");e=Wl(e)}}var Kr=i(()=>{"use strict"});import{readFile as SB,writeFile as wB}from"fs/promises";import{join as IB}from"path";var jl=i(()=>{"use strict";b()});import{existsSync as _B,mkdirSync as TB,statSync as CB}from"fs";import{resolve as AB}from"path";var Gl=i(()=>{"use strict";jl()});var Bl=i(()=>{"use strict"});var Ul=i(()=>{"use strict";b()});var Hl=i(()=>{"use strict"});import{APICallError as jB}from"ai";var ql=i(()=>{"use strict"});import{readFileSync as UB,statSync as HB}from"fs";import{resolve as $B,sep as KB}from"path";import{Environment as zB,Loader as XB}from"nunjucks";var Jr=i(()=>{"use strict"});var at,Cn=i(()=>{"use strict";at=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 Io=i(()=>{"use strict";Y()});var xo=i(()=>{"use strict"});import{appendFile as nU}from"fs/promises";var $l=i(()=>{"use strict";Y()});var ko=i(()=>{"use strict"});import{setTimeout as bU}from"timers/promises";import{generateText as RU,streamText as vU}from"ai";var zl=i(()=>{"use strict";Gl();Bl();Ul();Hl();ql();Y();Jr();Cn();fr();b();Io();xo();$l();ko()});var Xl=i(()=>{"use strict";zl()});var vo=i(()=>{"use strict";Xl();Io();xo();ko()});var _o=i(()=>{"use strict";Y()});var Ql=i(()=>{"use strict";_o()});var Vl=i(()=>{"use strict";Ql();_o()});var Yl=i(()=>{"use strict"});var Zl=i(()=>{"use strict"});var em=i(()=>{"use strict";Yl();Zl()});function zr(e){let t=e.trim();return t===""?[]:Array.from(rw.segment(t)).map(n=>n.segment.trim()).filter(n=>n!=="")}function Z(e){let t=e.replace(/\s+/gu," ").trim(),n=iw(t),r="";for(let a of n)if(!aw(a)){if(!Pn(a)){r+=a;continue}r+=a.toLowerCase().normalize("NFD").replace(new RegExp("\\p{M}","gu"),"")}return r}function iw(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&&nw.has(t[t.length-1]??"")&&Pn(t[t.length-2]??"")&&n<e.length-1&&Pn(e[n+1]??"")){t.pop(),n+=1;continue}let a=t[t.length-1]??"",o=e[n+1]??"";a!==""&&o!==""&&Pn(a)&&Pn(o)&&t.push(r),n+=1}return t}function Pn(e){return e!==""&&new RegExp("\\p{Script=Latin}","u").test(e)&&new RegExp("\\p{L}","u").test(e)}function aw(e){return/[\p{P}\p{S}]/u.test(e)}var nw,rw,An=i(()=>{"use strict";nw=new Set(["\u2010","\u2011","\u2012","\u2013","\u2014","\u2015"]),rw=new Intl.Segmenter(void 0,{granularity:"sentence"})});function Co(){return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:-1,matchStart:-1,score:0}}function nm(e,t){let n=Xr(e,2),r=Xr(t,2),a=Xr(e,3),o=Xr(t,3);return(tm(n,r)+tm(a,o))/2}function Xr(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 tm(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 rm(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[d,c]of[...t].entries()){let m=(r[d]??0)+1,p=(n[d+1]??0)+1,w=(n[d]??0)+(s===c?0:1);r[d+1]=Math.min(m,p,w)}[n,r]=[r,n]}return 1-(n[t.length]??Math.max(e.length,t.length))/Math.max(e.length,t.length)}function im(e,t){return 1-Math.abs(e.length-t.length)/Math.max(e.length,t.length)}function am(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 d=1;d<=r;d+=1){if(e[s-1]!==t[d-1])continue;let c=a[s];c!==void 0&&(c[d]=(a[s-1]?.[d-1]??0)+1,o=Math.max(o,c[d]??0))}return 2*o/(n+r)}function om(e){return` - ${e.occurrenceId} score=${e.score.toFixed(3)}
|
|
311
|
+
prev: ${To(e.prevText)}
|
|
312
|
+
text: ${To(e.text,120)}
|
|
313
|
+
next: ${To(e.nextText)}`}function To(e,t=80){let n=e.replace(/\s+/gu," ").trim();return n===""?"(none)":n.length<=t?n:`${n.slice(0,t-3)}...`}var Po=i(()=>{"use strict"});var Ao=i(()=>{"use strict"});function dm(e,t,n){return e.mode==="head_tail"?dw(e,t):Lo(e.text??"",t,n)}function dw(e,t){let n=e.head??"",r=e.tail??"",a=Z(t),o=Z(n),s=Z(r);if(o===""||s==="")return Co();let d=a.indexOf(o),c=a.lastIndexOf(s);if(d!==-1&&c!==-1&&d<=c){let P=o.length+s.length,I=Math.min(1,P/Math.max(1,a.length));return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:c+s.length,matchStart:d,score:.82+.18*I}}let m=Lo(n,t,"start_anchor"),p=Lo(r,t,"end_anchor"),w=m.matchStart<=p.matchStart?1:.85;return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:Math.max(m.matchEnd,p.matchEnd),matchStart:Math.min(m.matchStart,p.matchStart),score:(m.score+p.score)/2*w}}function Lo(e,t,n){let r=e.trim(),a=t.trim(),o=Z(r),s=Z(a);if(o===""||s==="")return Co();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 I=s.indexOf(o),T=o.length/Math.max(1,s.length),N=sm({candidateNormalized:s,label:n,matchEnd:I+o.length,matchStart:I,queryNormalized:o});return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:I+o.length,matchStart:I,score:Math.min(1,.75+.25*T+N)}}if(o.includes(s)){let I=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*I)}}let d=0,c=-1,m=-1,p=Math.min(s.length,Math.max(o.length,Math.floor(o.length*1.2))),w=Math.min(p,Math.max(1,Math.floor(o.length*.8))),P=new Set(o);for(let I=w;I<=p;I+=1)for(let T=0;T<=s.length-I;T+=1){let N=s.slice(T,T+I),ve=new Set(N),od=new Set([...P,...ve]).size,uy=[...P].filter(ly=>ve.has(ly)).length;if((od===0?0:uy/od)<.35)continue;let sd=.35*nm(o,N)+.35*am(o,N)+.2*rm(o,N)+.1*im(o,N)+sm({candidateNormalized:s,label:n,matchEnd:T+I,matchStart:T,queryNormalized:o});sd>d&&(d=Math.min(1,sd),c=T,m=T+I)}return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:m,matchStart:c,score:d}}function sm(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 cm=i(()=>{"use strict";An();Po();Ao()});function um(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(d=>d.trim()).filter(d=>d!=="");if(s.length>=2){let d=s[0],c=s[s.length-1];return d===void 0||c===void 0?[void 0,"anchor string is empty"]:[{head:d,mode:"head_tail",tail:c},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 lm(e,t){if(e===void 0)return;if(e.mode==="head_tail"){let r=t==="end_anchor"?Oo(e.tail??"","end_anchor"):Oo(e.head??"","start_anchor");return r===""?void 0:{mode:"full",text:r}}return{mode:"full",text:Oo(e.text??"",t)}}function No(e){return e.mode==="head_tail"?Z(e.head??"").length+Z(e.tail??"").length:Z(e.text??"").length}function Oo(e,t){let n=zr(e);return n.length<=1?e.trim():t==="end_anchor"?n[n.length-1]??e.trim():n[0]??e.trim()}var mm=i(()=>{"use strict";An()});var ot,ym=i(()=>{"use strict";cm();mm();Po();Ao();ot=class{resolve(t,n,r){let a=t.start_anchor??t.start,o=t.end_anchor??t.end,[s,d]=this.parseAnchor(a,"start_anchor");if(d!==void 0)return[void 0,d];let c;if(o!==void 0){let[N,ve]=this.parseAnchor(o,"end_anchor");if(ve!==void 0)return[void 0,ve];c=N}let[m,p,w]=this.resolveAnchor({anchor:s,candidateSentenceIds:n,candidateTexts:r,label:"start_anchor"});if(w!==void 0)return[void 0,w];if(m===void 0)return[void 0,{candidates:[],code:"none",fieldName:"start_anchor",message:"start_anchor could not be matched: no candidate selected"}];if(c===void 0)return[{confidence:m.score,sentenceIds:[m.sentenceId],strategy:p},void 0];let[P,I,T]=this.resolveAnchor({anchor:c,candidateSentenceIds:n,candidateTexts:r,label:"end_anchor",minIndex:m.index});return T!==void 0?[void 0,T]:P===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:P.index<m.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${m.occurrenceId} -> ${P.occurrenceId}).`}]:[{confidence:Math.min(m.score,P.score),sentenceIds:n.slice(m.index,P.index+1),strategy:`${p}+${I}`},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,[d,c,m]=s===void 0?this.resolveAnchor({anchor:a,candidateSentenceIds:t.candidateSentenceIds,candidateTexts:t.candidateTexts,label:"start_anchor"}):[s,"choice_final",void 0];if(m!==void 0)return[void 0,m];if(d===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:d.score,sentenceIds:[d.sentenceId],strategy:c},void 0];let[p,w]=this.parseAnchor(r,"end_anchor");if(w!==void 0)return[void 0,w];let P=t.overrides.end_anchor,[I,T,N]=P===void 0?this.resolveAnchor({anchor:p,candidateSentenceIds:t.candidateSentenceIds,candidateTexts:t.candidateTexts,label:"end_anchor",minIndex:d.index}):[P,"choice_final",void 0];return N!==void 0?[void 0,N]:I===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:I.index<d.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${d.occurrenceId} -> ${I.occurrenceId}).`}]:[{confidence:Math.min(d.score,I.score),sentenceIds:t.candidateSentenceIds.slice(d.index,I.index+1),strategy:`${c}+${T}`},void 0]}parseAnchor(t,n){let[r,a]=um(t);return a!==void 0?[void 0,{candidates:[],code:"invalid_anchor",fieldName:n,message:`Invalid evidence.${n}: ${a}`}]:[lm(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(p=>p.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(p=>p.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(p=>p.exactSubstring);if(o.length===1&&No(t.anchor)>=8)return[o[0],"exact_substring",void 0];if(o.length>1&&No(t.anchor)>=8){let p=this.#e(o);return p!==void 0?[p,"exact_substring_scored",void 0]:[void 0,"ambiguous_exact_substring",this.#t(t.label,"ambiguous_exact_substring",o)]}let s=n.filter(p=>p.exactNormalized);if(s.length===1)return[s[0],"exact_normalized",void 0];if(s.length>1){let p=this.#e(s);return p!==void 0?[p,"exact_normalized_scored",void 0]:[void 0,"ambiguous_exact_normalized",this.#t(t.label,"ambiguous_exact_normalized",s)]}let d=n[0];if(d===void 0)return[void 0,"none",{candidates:[],code:"none",fieldName:t.label,message:`${t.label} could not be matched: no candidates available`}];let c=n[1]?.score??0,m=d.score-c;return d.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=${d.score.toFixed(3)}.`}]:n.length===1?[d,"auto_top1_single_candidate",void 0]:d.score>=.97?[d,"auto_top1_very_high",void 0]:d.score>=.9&&m>=.07?[d,"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,d]of n.entries()){let c=r[s]??"",m=s>0?r[s-1]??"":"",p=s<r.length-1?r[s+1]??"":"",w=dm(t,c,a);o.push({exactNormalized:w.exactNormalized,exactRaw:w.exactRaw,exactSubstring:w.exactSubstring,index:s,nextText:p,occurrenceId:`S${s+1}`,prevText:m,score:w.score,sentenceId:d,text:c})}return o.sort((s,d)=>s.score!==d.score?d.score-s.score:s.exactRaw!==d.exactRaw?s.exactRaw?-1:1:s.exactSubstring!==d.exactSubstring?s.exactSubstring?-1:1:s.exactNormalized!==d.exactNormalized?s.exactNormalized?-1:1:s.index-d.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=>om(s)).join(`
|
|
314
|
+
`);return{candidates:a,code:n,fieldName:t,message:`${t} is ambiguous. Top candidates:
|
|
299
315
|
${o}
|
|
300
|
-
Please provide a more specific anchor or surrounding context.`}}}});var
|
|
301
|
-
`)});var
|
|
302
|
-
`,
|
|
316
|
+
Please provide a more specific anchor or surrounding context.`}}}});var gm=i(()=>{"use strict"});var bm=i(()=>{"use strict";ym();gm()});var Qr,Sm=i(()=>{"use strict";Qr=["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(`
|
|
317
|
+
`)});var Do=i(()=>{"use strict"});var wm=i(()=>{"use strict";Do();An()});var Et=i(()=>{"use strict";bm();Sm();Do();wm();An()});var Ln=i(()=>{"use strict";U()});var Im=i(()=>{"use strict";x();Ln()});var xm=i(()=>{"use strict";Ln()});var km=i(()=>{"use strict"});import{z as y}from"zod";var Rm,bw,vm,Sw,ww,Ew,Iw,xw,kw,Yr=i(()=>{"use strict";x();Rm=y.object({from:y.union([y.number().int(),y.string()]),strength:y.string().optional(),to:y.union([y.number().int(),y.string()])}),bw=y.object({quote:y.string().optional(),sentence_id:y.string().optional()}).passthrough(),vm=y.union([y.record(y.string(),y.unknown()),y.array(bw)]),Sw=y.object({content:y.string(),evidence:vm.nullish(),label:y.string(),retention:y.enum(["verbatim","detailed","focused","relevant"]),temp_id:y.string()}).passthrough(),ww=y.object({chunks:y.array(Sw),fragment_summary:y.string(),links:y.array(Rm)}),Ew=y.object({content:y.string(),evidence:vm.nullish(),importance:y.enum(["critical","important","helpful"]),label:y.string(),temp_id:y.string()}).passthrough(),Iw=y.object({chunk_id:y.number().int(),importance:y.enum(["critical","important","helpful"])}),xw=y.object({chunks:y.array(Ew),importance_annotations:y.array(Iw),links:y.array(Rm)}),kw=y.object({choice:y.string()})});var _m=i(()=>{"use strict";U();Et();km();Ln();Yr()});var Tm=i(()=>{"use strict";U();x();Im();Ln();xm();_m();Yr()});var Cm=i(()=>{"use strict";Tm();Yr()});var Zr=i(()=>{"use strict"});var we=i(()=>{"use strict"});import{detect as Pq,validateISO2 as Aq}from"tinyld";var Fo=i(()=>{"use strict";we()});var Pm=i(()=>{"use strict";Fo();we()});var Am=i(()=>{"use strict"});import{z as On}from"zod";var Cw,Zq,Om=i(()=>{"use strict";U();Et();Cm();Zr();Zr();Pm();Am();Cw=On.object({content:On.string(),id:On.number(),label:On.string()}),Zq=On.array(Cw)});var Wo=i(()=>{"use strict";Om()});var Nm=i(()=>{"use strict";Je()});var jo=i(()=>{"use strict";Nm()});var Dm=i(()=>{"use strict";em();Wo();jo()});var ei=i(()=>{"use strict";Dm();jo()});var ti=i(()=>{"use strict"});var ni=i(()=>{"use strict";ei();ti()});var Mm=i(()=>{"use strict";ni()});var Dw,Bo,Fm,Nn=i(()=>{"use strict";Dw=Object.freeze(["serial-generation/editor-compress","serial-generation/editor-review","serial-generation/editor-review-guide","serial-generation/reader-choice","serial-generation/reader-extraction"]),Bo=Object.freeze({compress:"serial-generation/editor-compress",review:"serial-generation/editor-review",reviewGuide:"serial-generation/editor-review-guide"}),Fm=Object.freeze({choice:"serial-generation/reader-choice",extraction:"serial-generation/reader-extraction"})});var Wm=i(()=>{"use strict"});var ri=i(()=>{"use strict"});var jm=i(()=>{"use strict";ri()});var It,P$,Mw,ii=i(()=>{"use strict";Hi();It=(r=>(r.Critical="critical",r.Major="major",r.Minor="minor",r))(It||{}),P$=yt(It),Mw=qt(It,"review severity")});var D$,Uo=i(()=>{"use strict";ri();ii();D$=Object.freeze({critical:9,major:3,minor:1})});var Gm=i(()=>{"use strict";Fo();we();ii()});import{appendFile as H$,writeFile as q$}from"fs/promises";var Bm=i(()=>{"use strict";Y();Uo()});var Ho=i(()=>{"use strict";x()});var Um=i(()=>{"use strict"});import{z as xt}from"zod";var Fw,Ww,o1,Hm=i(()=>{"use strict";Y();U();ri();Ho();ii();Fw=["critical","major","minor"],Ww=xt.object({problem:xt.string(),severity:xt.enum(Fw),suggestion:xt.string().default("")}),o1=xt.object({issues:xt.array(Ww)})});var qm=i(()=>{"use strict";Wm();jm();Uo();Gm();Bm();Ho();Um();Hm()});var qo=i(()=>{"use strict";qm()});var Km=i(()=>{"use strict"});var Jm=i(()=>{"use strict"});var zm=i(()=>{"use strict";Km();Jm()});var Dn=i(()=>{"use strict"});var G1,B1,kt=i(()=>{"use strict";x();G1={critical:9,helpful:1,important:3},B1={detailed:9,focused:3,relevant:1,verbatim:27}});var Rt=i(()=>{"use strict"});var Mn=i(()=>{"use strict"});var $o=i(()=>{"use strict";Rt()});var Ko=i(()=>{"use strict";kt();Rt();Dn();Mn();$o()});var Jo=i(()=>{"use strict";Dn();Ko();Mn()});var Qm=i(()=>{"use strict";kt();Rt();Mn()});var Vm=i(()=>{"use strict";kt();Rt();Dn();Mn();$o()});var Ym=i(()=>{"use strict";Jo();Rt();Dn();Qm();Ko();Vm()});var Zm=i(()=>{"use strict";Ym();Jo()});var ep=i(()=>{"use strict";kt()});var tp=i(()=>{"use strict";Zm();ep()});var np=i(()=>{"use strict";zm();tp();kt()});var rp=i(()=>{"use strict";np()});var zo=i(()=>{"use strict"});var ip=i(()=>{"use strict"});var Xo=i(()=>{"use strict";ni()});var oi=i(()=>{"use strict"});var ap=i(()=>{"use strict";Cn();Nn();qo();ei();rp();ni();ti();zo();ip();Xo();oi()});var op=i(()=>{"use strict";zo();oi()});var sp=i(()=>{"use strict";Mm();ap();ti();op();Xo();oi()});var vt=i(()=>{"use strict";sp()});var dp=i(()=>{"use strict";ge();Cn();vt();Qe()});import{mkdir as XJ,writeFile as QJ}from"fs/promises";import{dirname as YJ}from"path";var cp=i(()=>{"use strict"});var si=i(()=>{"use strict"});var Fn=i(()=>{"use strict";Kr();Jr()});import{createWriteStream as oz}from"fs";import{dirname as dz,extname as cz}from"path";import{finished as lz}from"stream/promises";import{mkdir as pz}from"fs/promises";import{ZipFile as fz}from"yazl";var Yo=i(()=>{"use strict";si();Fn()});var up=i(()=>{"use strict";Fn()});var lp=i(()=>{"use strict";si();Fn()});var Zo=i(()=>{"use strict";Fn()});var mp=i(()=>{"use strict";Yo();up();lp();Zo();si()});var pp=i(()=>{"use strict";Yo();mp();Zo()});var hp=i(()=>{"use strict";cp();pp()});var Fe=i(()=>{"use strict"});import{posix as eX,resolve as tX,sep as nX}from"path";var st=i(()=>{"use strict";Fe()});import{randomBytes as oX}from"crypto";import{z as es}from"zod";var ui,ts,Zw,cX,_t=i(()=>{"use strict";Fe();ui=1,ts=3,Zw=`${JSON.stringify({formatVersion:ui,schemaVersion:ts})}
|
|
318
|
+
`,cX=es.object({formatVersion:es.literal(ui),schemaVersion:es.number().int().positive().optional()})});import{open as mX}from"fs/promises";import{inflateRaw as hX}from"zlib";import{open as yX}from"yauzl";var Wn=i(()=>{"use strict";Fe();_t();st()});import{mkdir as xX,open as kX,writeFile as RX}from"fs/promises";import{dirname as _X,resolve as TX}from"path";var is=i(()=>{"use strict";st();Wn()});var Tt=i(()=>{"use strict";De()});var bp=i(()=>{"use strict";Tt();Oe()});import{createReadStream as qX,createWriteStream as $X}from"fs";import{mkdir as JX}from"fs/promises";import{dirname as XX}from"path";import{createInterface as VX}from"readline";import{z as S}from"zod";var nE,rE,ZX,eQ,tQ,Sp=i(()=>{"use strict";nE="wikg-index-output/v1",rE=S.enum(["fts","embedding-source","embedding-summary"]),ZX=S.object({artifactKind:rE,chapterId:S.number().int().nonnegative(),embedding:S.object({dimensions:S.number().int().nonnegative(),identity:S.string().optional(),model:S.string()}).optional(),inputRevision:S.number().int().nonnegative(),protocol:S.literal(nE),type:S.literal("manifest")}),eQ=S.object({metadata:S.record(S.string(),S.unknown()).optional(),objectId:S.string().min(1),objectKind:S.string().min(1),rowId:S.string().min(1),sentenceIndex:S.number().int().nonnegative().optional(),text:S.string(),tokens:S.array(S.string()),type:S.literal("lexical-row")}),tQ=S.object({endSentenceIndex:S.number().int().nonnegative(),segmentIndex:S.number().int().nonnegative(),startSentenceIndex:S.number().int().nonnegative(),text:S.string(),vector:S.array(S.number()),wordsCount:S.number().int().nonnegative(),type:S.literal("segment")})});var Ct=i(()=>{"use strict";bp();Sp()});var Gn,dt,Bn,ee=i(()=>{"use strict";Gn="database.db",dt="index.db",Bn="fts.db"});import{readdir as dQ}from"fs/promises";import{join as uQ,posix as lQ,relative as mQ,sep as pQ}from"path";var wp=i(()=>{"use strict";Fe();st()});import{createWriteStream as xQ}from"fs";import{mkdir as RQ,open as vQ}from"fs/promises";import{dirname as TQ}from"path";import{finished as PQ}from"stream/promises";import{ZipFile as LQ}from"yazl";var as=i(()=>{"use strict";Fe();wp();_t();st();Wn()});import{createHash as GQ,randomUUID as fE}from"crypto";import{stat as Ep}from"fs/promises";import{resolve as HQ}from"path";async function mi(e){let t=await Ep(e);return`${t.size}:${t.mtimeMs}`}function pi(){return`${process.pid}-${fE()}`}async function Ip(e){await new Promise(t=>{setTimeout(t,e)})}async function Pt(e){try{return await Ep(e),!0}catch(t){if(h(t)&&t.code==="ENOENT")return!1;throw t}}var Ee=i(()=>{"use strict";b()});import{randomUUID as zQ}from"crypto";import{mkdtemp as QQ,rename as VQ,rm as YQ,stat as ZQ,writeFile as eV}from"fs/promises";import{tmpdir as nV}from"os";import{dirname as iV,join as aV,resolve as oV}from"path";var xV,hi=i(()=>{"use strict";x();en();ur();$e();Ct();D();Je();Fe();ee();is();_t();st();Wn();as();Ee();mr();$e();xV=300*1e3});import{open as _V,readFile as TV}from"fs/promises";import{join as PV}from"path";var xp=i(()=>{"use strict";Fe();_t();st();Wn();hi()});var We=i(()=>{"use strict";is();xp();_t();as()});var Fp=i(()=>{"use strict";Le()});import{join as wE}from"path";async function le(e){let t=Date.now(),n=new Set;for(let r of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){let a=await e.queryAll(`
|
|
303
319
|
SELECT DISTINCT owner_pid
|
|
304
|
-
FROM ${
|
|
320
|
+
FROM ${r}
|
|
305
321
|
WHERE owner_pid IS NOT NULL
|
|
306
322
|
AND heartbeat_at <= ?
|
|
307
|
-
`,[t-6e4],o=>
|
|
323
|
+
`,[t-6e4],o=>Ie(o,"owner_pid"));for(let o of a)xE(o)||n.add(o)}n.size!==0&&(await e.run(`
|
|
308
324
|
DELETE FROM archive_owners
|
|
309
|
-
WHERE owner_pid IN (${
|
|
310
|
-
`,[...
|
|
325
|
+
WHERE owner_pid IN (${Un(n.size)})
|
|
326
|
+
`,[...n]),await e.run(`
|
|
311
327
|
DELETE FROM entry_locks
|
|
312
|
-
WHERE owner_pid IN (${
|
|
313
|
-
`,[...
|
|
328
|
+
WHERE owner_pid IN (${Un(n.size)})
|
|
329
|
+
`,[...n]),await e.run(`
|
|
314
330
|
DELETE FROM entry_sqlite_leases
|
|
315
|
-
WHERE owner_pid IN (${
|
|
316
|
-
`,[...
|
|
331
|
+
WHERE owner_pid IN (${Un(n.size)})
|
|
332
|
+
`,[...n]),await e.run(`
|
|
317
333
|
DELETE FROM archive_commit_locks
|
|
318
|
-
WHERE owner_pid IN (${
|
|
319
|
-
`,[...
|
|
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(`
|
|
320
336
|
ALTER TABLE entry_overlays
|
|
321
337
|
ADD COLUMN archive_signature TEXT
|
|
322
338
|
`),t.includes("mutation_token")||await e.run(`
|
|
323
339
|
ALTER TABLE entry_overlays
|
|
324
340
|
ADD COLUMN mutation_token TEXT
|
|
325
|
-
`),(await e.queryAll("PRAGMA table_info(entry_sqlite_leases)",void 0,
|
|
341
|
+
`),(await e.queryAll("PRAGMA table_info(entry_sqlite_leases)",void 0,r=>B(r,"name"))).includes("mode")||await e.run(`
|
|
326
342
|
ALTER TABLE entry_sqlite_leases
|
|
327
343
|
ADD COLUMN mode TEXT NOT NULL DEFAULT 'read'
|
|
328
|
-
`)}function
|
|
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=`
|
|
329
345
|
CREATE TABLE IF NOT EXISTS entry_overlays (
|
|
330
346
|
archive_key TEXT NOT NULL,
|
|
331
347
|
archive_path TEXT NOT NULL,
|
|
@@ -376,35 +392,35 @@ CREATE TABLE IF NOT EXISTS archive_commit_locks (
|
|
|
376
392
|
heartbeat_at INTEGER NOT NULL,
|
|
377
393
|
created_at INTEGER NOT NULL
|
|
378
394
|
);
|
|
379
|
-
`,
|
|
395
|
+
`,IE=new at(1)});async function Hn(e,t,n){let r=pi();for(;;){if(await F(async o=>(await le(o),await o.transaction(async()=>(await o.queryAll(`
|
|
380
396
|
SELECT *
|
|
381
397
|
FROM entry_locks
|
|
382
398
|
WHERE archive_key = ?
|
|
383
|
-
`,[e],
|
|
399
|
+
`,[e],Wp)).some(d=>d.ownerId!==r&&PE(t,d.entryPath)&&CE(n,d.mode))?!1:(await o.run(`
|
|
384
400
|
INSERT INTO entry_locks (
|
|
385
401
|
archive_key, entry_path, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
386
402
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
387
|
-
`,[e,t,r,
|
|
403
|
+
`,[e,t,n,r,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await F(async o=>{await o.run(`
|
|
388
404
|
DELETE FROM entry_locks
|
|
389
405
|
WHERE archive_key = ? AND entry_path = ? AND owner_id = ?
|
|
390
|
-
`,[e,t,
|
|
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(`
|
|
391
407
|
SELECT *
|
|
392
408
|
FROM entry_overlays
|
|
393
409
|
WHERE archive_key = ?
|
|
394
410
|
ORDER BY entry_path ASC
|
|
395
|
-
`,[e],
|
|
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(`
|
|
396
412
|
SELECT COUNT(*) AS count
|
|
397
413
|
FROM entry_sqlite_leases
|
|
398
414
|
WHERE archive_key = ?
|
|
399
415
|
AND entry_path = ?
|
|
400
|
-
`,[e,t],o=>
|
|
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(`
|
|
401
417
|
SELECT overlay.*
|
|
402
418
|
FROM entry_overlays AS overlay
|
|
403
|
-
WHERE overlay.entry_path IN (?, ?)
|
|
419
|
+
WHERE overlay.entry_path IN (?, ?, ?)
|
|
404
420
|
AND overlay.kind = 'file'
|
|
405
421
|
AND overlay.workspace_path IS NOT NULL
|
|
406
422
|
ORDER BY overlay.updated_at ASC
|
|
407
|
-
`,[
|
|
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=`
|
|
408
424
|
CREATE TABLE IF NOT EXISTS build_jobs (
|
|
409
425
|
job_id TEXT PRIMARY KEY,
|
|
410
426
|
archive_key TEXT NOT NULL,
|
|
@@ -442,6 +458,15 @@ ON build_jobs(archive_key, chapter_id)
|
|
|
442
458
|
WHERE target IN ('reading-graph', 'reading-summary')
|
|
443
459
|
AND state IN ('queued', 'running', 'canceling', 'paused');
|
|
444
460
|
|
|
461
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_build_jobs_active_index_chapter_target
|
|
462
|
+
ON build_jobs(archive_key, chapter_id, target)
|
|
463
|
+
WHERE target IN (
|
|
464
|
+
'index-fts',
|
|
465
|
+
'index-embedding-source',
|
|
466
|
+
'index-embedding-summary'
|
|
467
|
+
)
|
|
468
|
+
AND state IN ('queued', 'running', 'canceling', 'paused');
|
|
469
|
+
|
|
445
470
|
CREATE INDEX IF NOT EXISTS idx_build_jobs_queue
|
|
446
471
|
ON build_jobs(state, queue_rank, updated_at);
|
|
447
472
|
|
|
@@ -454,27 +479,27 @@ CREATE TABLE IF NOT EXISTS build_worker_lease (
|
|
|
454
479
|
|
|
455
480
|
INSERT OR IGNORE INTO build_worker_lease (id)
|
|
456
481
|
VALUES (1);
|
|
457
|
-
`});function
|
|
482
|
+
`});function ct(e){let t=nI(Dt(e,"current_step"),"current_step"),n=Dt(e,"owner_id"),r=e.owner_pid===null?void 0:q(e,"owner_pid"),a=e.reading_summary_started_at===null?void 0:q(e,"reading_summary_started_at"),o=e.finished_at===null?void 0:q(e,"finished_at"),s=Dt(e,"error_json"),d=e.input_revision===null?void 0:q(e,"input_revision"),c=Dt(e,"llm_json"),m=Dt(e,"prompt");return{archiveKey:pe(e,"archive_key"),archivePath:pe(e,"archive_path"),cachePath:pe(e,"cache_path"),chapterId:q(e,"chapter_id"),createdAt:q(e,"created_at"),...t===void 0?{}:{currentStep:t},...s===void 0?{}:{errorJSON:s},eventsPath:pe(e,"events_path"),...o===void 0?{}:{finishedAt:o},jobId:pe(e,"job_id"),...d===void 0?{}:{inputRevision:d},logPath:pe(e,"log_path"),...c===void 0?{}:{llmJSON:c},...n===void 0?{}:{ownerId:n},...r===void 0?{}:{ownerPid:r},...m===void 0?{}:{prompt:m},queueRank:q(e,"queue_rank"),state:tI(pe(e,"state")),...a===void 0?{}:{readingSummaryStartedAt:a},target:sh(pe(e,"target"),"target"),updatedAt:q(e,"updated_at"),workspacePath:pe(e,"workspace_path")}}function tI(e){if(As.includes(e))return e;throw new Error(`Invalid build job state: ${e}`)}function sh(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 nI(e,t){return e===void 0?void 0:sh(e,t)}function pe(e,t){let n=e[t];if(typeof n!="string")throw new TypeError(`Expected ${t} to be a string`);return n}function q(e,t){let n=e[t];if(typeof n!="number")throw new TypeError(`Expected ${t} to be a number`);return n}function Dt(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 ut=i(()=>{"use strict";Ls()});async function Mt(){return await _(nh(),ah)}var Ft=i(()=>{"use strict";x();oh();_i();ut()});import{rm as s5}from"fs/promises";var Ti=i(()=>{"use strict";Nt();Xn();Ft()});import{rm as h5}from"fs/promises";var Ci=i(()=>{"use strict";Nt();ut();Ti()});import{randomUUID as P5}from"crypto";import{resolve as L5}from"path";var Ns=i(()=>{"use strict";_i();Nt();Os();Xn();Ft();Ci();Ti();ut()});import{resolve as H5}from"path";var lh=i(()=>{"use strict";Nt();Ft();Ci();ut()});import{randomUUID as r6}from"crypto";var mh=i(()=>{"use strict";Nt();Os();Xn();Ft();Ci();Ti();Ns();ut()});import{rm as Pi}from"fs/promises";async function Ai(e){let t=await Mt(),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=>q(d,"count")),a=await t.queryAll(`
|
|
458
483
|
SELECT *
|
|
459
484
|
FROM build_jobs
|
|
460
485
|
WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
461
486
|
AND updated_at <= ?
|
|
462
|
-
`,[r],rt),o=e.dryRun?{freedBytes:0,removed:0,scanned:0}:await pi(Ap()),s=0;for(let c of a)s+=await F(c.workspacePath),s+=await F(c.cachePath),s+=await F(c.logPath),s+=await F(c.eventsPath),e.dryRun||(await Si(c.workspacePath,{force:!0,recursive:!0}),await Si(c.cachePath,{force:!0,recursive:!0}),await Si(c.logPath,{force:!0,recursive:!0}),await Si(c.eventsPath,{force:!0}),await t.run("DELETE FROM build_jobs WHERE job_id = ?",[c.jobId]));return{freedBytes:s+o.freedBytes,removed:a.length+o.removed,scanned:(n??0)+o.scanned}}finally{await t.close()}}var Bp=i(()=>{"use strict";Wr();yi();Lt();nt()});var ih=i(()=>{"use strict";bs();ws();jp();Gp();jr();Bp()});var Ei=i(()=>{"use strict"});import{createReadStream as r5,createWriteStream as n5}from"fs";import{createInterface as a5}from"readline";import{z as u}from"zod";var ah,P,kE,IE,oh,xE,RE,vE,TE,_E,CE,PE,AE,LE,OE,NE,DE,ME,WE,d5,Ot=i(()=>{"use strict";fe();Ei();ah=1,P=u.number().int().nonnegative(),kE=u.literal(ah),IE=u.string().regex(/^Q[1-9][0-9]*$/u),oh=u.number().min(0).max(1).optional(),xE=u.object({text:u.string(),wordsCount:P}).strict(),RE=u.object({type:u.literal("meta"),schemaVersion:kE,chapterId:u.number().int(),stream:u.enum(["knowledge-graph","reading-graph","summary"])}).strict(),vE=u.object({type:u.literal("parameter"),scope:u.enum(["knowledge-graph","reading-graph","summary"]),language:u.string().optional(),prompt:u.string()}).strict(),TE=u.object({type:u.literal("source-fragment"),fragmentId:P,summary:u.string(),sentences:u.array(xE)}).strict(),_E=u.object({type:u.literal("reading-chunk"),id:u.string().min(1),generation:u.number().int(),sentenceIndex:P,label:u.string(),content:u.string(),sentenceIndexes:u.array(P).min(1),retention:u.enum(K).optional(),importance:u.enum(te).optional(),wordsCount:P,weight:u.number()}).strict(),CE=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(),PE=u.object({type:u.literal("fragment-group"),groupId:P,startSentenceIndex:P,endSentenceIndex:P}).strict(),AE=u.object({type:u.literal("snake"),id:u.string().min(1),groupId:P,localSnakeId:P,size:P,firstLabel:u.string(),lastLabel:u.string(),wordsCount:P,weight:u.number()}).strict(),LE=u.object({type:u.literal("snake-chunk"),snakeId:u.string().min(1),chunkId:u.string().min(1),position:P}).strict(),OE=u.object({type:u.literal("snake-edge"),fromSnakeId:u.string().min(1),toSnakeId:u.string().min(1),weight:u.number()}).strict(),NE=u.object({type:u.literal("summary"),text:u.string()}).strict(),DE=u.object({type:u.literal("mention"),id:u.string().min(1),fragmentId:P.optional(),sentenceIndex:P.optional(),rangeStart:P,rangeEnd:P,surface:u.string().min(1),qid:IE,confidence:oh,note:u.string().optional()}).strict(),ME=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(P).min(1),confidence:oh,note:u.string().optional()}).strict(),WE=u.object({type:u.literal("end")}).strict(),d5=u.discriminatedUnion("type",[RE,vE,TE,_E,CE,PE,AE,LE,OE,NE,DE,ME,WE])});var sh=i(()=>{"use strict";Se();We()});var ch=i(()=>{"use strict";I();Un()});import{mkdir as E5,rm as k5}from"fs/promises";import{join as x5}from"path";var uh=i(()=>{"use strict";I();W();kt();Ot();sh();ch()});var ki=i(()=>{"use strict"});var lh=i(()=>{"use strict";ki()});var mh=i(()=>{"use strict"});var ph=i(()=>{"use strict";ki()});var hh=i(()=>{"use strict";ki()});var fh=i(()=>{"use strict";lh();mh();ph();hh()});import{z as l}from"zod";var yh,GE,BE,UE,HE,qE,$E,KE,JE,zE,QE,gh=i(()=>{"use strict";fe();yh=l.tuple([l.number(),l.number()]),GE=l.object({text:l.string(),wordsCount:l.number()}),BE=l.object({serialId:l.number(),fragmentId:l.number(),summary:l.string(),sentences:l.array(GE)}),UE=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}})),HE=l.object({id:l.number(),generation:l.number(),sentenceId:yh,label:l.string(),content:l.string(),sentenceIds:l.array(yh),retention:l.enum(K).optional(),importance:l.enum(te).optional(),wordsCount:l.number(),weight:l.number()}),qE=l.object({fromId:l.number(),toId:l.number(),strength:l.string().optional(),weight:l.number()}),$E=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()}),KE=l.object({snakeId:l.number(),chunkId:l.number(),position:l.number()}),JE=l.object({fromSnakeId:l.number(),toSnakeId:l.number(),weight:l.number()}),zE=l.object({serialId:l.number(),groupId:l.number(),startSentenceIndex:l.number(),endSentenceIndex:l.number()}),QE=l.object({chunks:l.array(HE),fragmentGroups:l.array(zE),fragments:l.array(BE),readingEdges:l.array(qE),serial:UE,snakeChunks:l.array(KE),snakeEdges:l.array(JE),snakes:l.array($E)})});import{readFile as b6,writeFile as S6}from"fs/promises";var bh=i(()=>{"use strict";gh()});var Sh=i(()=>{"use strict";Tr();Lo()});import{mkdir as A6}from"fs/promises";import{join as O6}from"path";var wh=i(()=>{"use strict";I();W();fh();bh();Ei();Sh();Ot()});var Eh=i(()=>{"use strict"});var xs=i(()=>{"use strict";wh();Ei();Eh()});var kh=i(()=>{"use strict";W();xs()});var Ih=i(()=>{"use strict"});var xh=i(()=>{"use strict";uh();kh();Ih()});var Rh=i(()=>{"use strict";xh();xs()});import{createReadStream as aZ,createWriteStream as oZ}from"fs";import{createInterface as cZ}from"readline";import{z as _}from"zod";var uZ,VE,lZ,Rs=i(()=>{"use strict";uZ=_.object({id:_.string().min(1),chapterId:_.number().int(),sentenceIndex:_.number().int().nonnegative().optional(),rangeStart:_.number().int().nonnegative(),rangeEnd:_.number().int().nonnegative(),surface:_.string().min(1),qid:_.string().regex(/^Q[1-9][0-9]*$/),confidence:_.number().min(0).max(1).optional(),note:_.string().optional()}),VE=_.tuple([_.number().int().nonnegative(),_.number().int().nonnegative()]).readonly(),lZ=_.object({id:_.string().min(1),sourceMentionId:_.string().min(1),targetMentionId:_.string().min(1),predicate:_.string().min(1),evidenceSentenceIds:_.array(VE).min(1),confidence:_.number().min(0).max(1).optional(),note:_.string().optional()})});import{mkdir as gZ,rm as bZ}from"fs/promises";import{join as wZ}from"path";var vs=i(()=>{"use strict";Se();Ot();Rs()});var Th=i(()=>{"use strict";Ot();Rs()});var Gr=i(()=>{"use strict"});import{z as Ie}from"zod";var ek,tk,OZ,_s=i(()=>{"use strict";G();Gr();ek=Ie.object({candidateId:Ie.string().min(1),decision:Ie.enum(["continue","recall","skip_this_time","never_recall"]),qid:Ie.string().nullable().optional()}).strict(),tk=Ie.object({decisions:Ie.array(ek),groupId:Ie.string().min(1)}).strict(),OZ=Ie.object({groups:Ie.array(tk)}).strict()});import{z as Br}from"zod";var ik,jZ,Ch=i(()=>{"use strict";G();Gr();_s();ik=Br.object({decision:Br.enum(["keep","reject"]),qid:Br.string().regex(/^Q[1-9][0-9]*$/)}).strict(),jZ=Br.object({qids:Br.array(ik)}).strict()});var Ph=i(()=>{"use strict";Sr()});import{z as Ur}from"zod";var ak,$Z,Ah=i(()=>{"use strict";G();ak=Ur.object({note:Ur.string().max(24).optional(),surfaceId:Ur.string().min(1)}).strict(),$Z=Ur.object({protectedSurfaces:Ur.array(ak)}).strict()});var Cs=i(()=>{"use strict"});var Lh=i(()=>{"use strict";Cs()});import{spawn as VZ}from"child_process";var Oh=i(()=>{"use strict"});var Ps=i(()=>{"use strict"});var Nh=i(()=>{"use strict";Ps();Gr()});var it=i(()=>{"use strict";_s();Ch();Gr();Ph();Ah();Lh();Cs();Oh();Nh();Ps()});var Ii=i(()=>{"use strict"});var Wh=i(()=>{"use strict";it();Ii()});var As=i(()=>{"use strict"});var Fh=i(()=>{"use strict";it()});var Ls=i(()=>{"use strict"});var Os=i(()=>{"use strict";it();Ii();Fh();Ls()});var Gh=i(()=>{"use strict";Os()});var Bh=i(()=>{"use strict";Se()});var Ns=i(()=>{"use strict"});var Uh=i(()=>{"use strict";vo();gt();Bn()});import{z as x}from"zod";var Hh,bk,Sk,wk,Z9,e4,qh=i(()=>{"use strict";G();gt();Uh();Hh=x.object({head:x.string().optional(),mode:x.enum(["full","head_tail"]).optional(),tail:x.string().optional(),text:x.string().optional()}).strict(),bk=x.object({quote:x.string().optional(),sentence_id:x.string().optional()}).strict(),Sk=x.union([x.object({end_anchor:x.union([x.string(),Hh]).optional(),quote:x.string().optional(),sentence_id:x.string().optional(),start_anchor:x.union([x.string(),Hh]).optional()}).strict(),x.array(bk)]),wk=x.object({confidence:x.number().min(0).max(1).optional(),evidence:Sk,predicate:x.string().min(1).max(64),sourceMentionId:x.string().min(1),targetMentionId:x.string().min(1)}).strict(),Z9=x.object({relations:x.array(wk)}).strict(),e4=new et});var $h=i(()=>{"use strict"});var Kh=i(()=>{"use strict";qh();$h()});var Jh=i(()=>{"use strict";Kh();Ii();Ls()});var Ds=i(()=>{"use strict";W();As()});var Yh=i(()=>{"use strict";it();We();vs();Wh();As();Gh();Bh();Ns();Jh();Ds()});var Zh=i(()=>{"use strict"});var ef=i(()=>{"use strict";vs();Th();Yh();Os();Ns();Ds();Zh()});var tf=i(()=>{"use strict";ef()});var Hr=i(()=>{"use strict";W()});var Ms=i(()=>{"use strict";We()});var Ws=i(()=>{"use strict";Se();We();kt();Hr();Ms()});var af=i(()=>{"use strict";We();Hr();Ws()});var Fs=i(()=>{"use strict";ye()});var of=i(()=>{"use strict";ye();Qt();Ke();Fs()});var sf=i(()=>{"use strict";kt();ye();Hr();of();Fs()});import{z as at}from"zod";var cf,N7,df=i(()=>{"use strict";cf=at.object({children:at.lazy(()=>at.array(cf)),uri:at.string().min(1),title:at.string().nullable().optional()}).strict(),N7=at.object({chapters:at.array(cf)}).strict()});var uf=i(()=>{"use strict";W()});var lf=i(()=>{"use strict";af();Hr();Ws();sf();Ms();df();uf()});var mf=i(()=>{"use strict";cr()});var Fe=i(()=>{"use strict";_p();yt();ih();Rh();tf();yt();ys();lf();mf();fi()});import{randomUUID as lee}from"crypto";async function pf(e){let t=await tI(e.databasePath);try{return await eI(t,e),await t.queryOne(`
|
|
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(`
|
|
463
488
|
SELECT 1 AS active
|
|
464
489
|
FROM state_locks
|
|
465
490
|
WHERE scope = ? AND resource_key = ?
|
|
466
491
|
LIMIT 1
|
|
467
|
-
`,[e.scope,e.resourceKey],()=>!0)===!0}finally{await t.close()}}async function
|
|
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(`
|
|
468
493
|
SELECT scope, resource_key, mode, owner_id, owner_pid, heartbeat_at
|
|
469
494
|
FROM state_locks
|
|
470
|
-
${
|
|
471
|
-
`,
|
|
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(`
|
|
472
497
|
DELETE FROM state_locks
|
|
473
498
|
WHERE scope = ?
|
|
474
499
|
AND resource_key = ?
|
|
475
500
|
AND owner_id = ?
|
|
476
501
|
AND heartbeat_at = ?
|
|
477
|
-
`,[
|
|
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=`
|
|
478
503
|
CREATE TABLE IF NOT EXISTS state_locks (
|
|
479
504
|
scope TEXT NOT NULL,
|
|
480
505
|
resource_key TEXT NOT NULL,
|
|
@@ -488,7 +513,7 @@ WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
|
488
513
|
|
|
489
514
|
CREATE INDEX IF NOT EXISTS idx_state_locks_resource
|
|
490
515
|
ON state_locks(scope, resource_key);
|
|
491
|
-
`,
|
|
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=`
|
|
492
517
|
CREATE TABLE IF NOT EXISTS library_locks (
|
|
493
518
|
library_id INTEGER PRIMARY KEY,
|
|
494
519
|
mode TEXT NOT NULL,
|
|
@@ -497,7 +522,7 @@ WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
|
497
522
|
heartbeat_at INTEGER NOT NULL,
|
|
498
523
|
created_at INTEGER NOT NULL
|
|
499
524
|
);
|
|
500
|
-
`,
|
|
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=`
|
|
501
526
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
502
527
|
library_id INTEGER NOT NULL,
|
|
503
528
|
public_id TEXT NOT NULL,
|
|
@@ -510,9 +535,9 @@ WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
|
510
535
|
created_at TEXT NOT NULL,
|
|
511
536
|
updated_at TEXT NOT NULL,
|
|
512
537
|
UNIQUE(library_id, public_id)
|
|
513
|
-
`,
|
|
538
|
+
`,gre=`
|
|
514
539
|
CREATE TABLE IF NOT EXISTS library_archives (
|
|
515
|
-
${
|
|
540
|
+
${ik}
|
|
516
541
|
);
|
|
517
542
|
|
|
518
543
|
CREATE INDEX IF NOT EXISTS idx_library_archives_library
|
|
@@ -520,20 +545,20 @@ ${bI}
|
|
|
520
545
|
|
|
521
546
|
CREATE INDEX IF NOT EXISTS idx_library_archives_library_path
|
|
522
547
|
ON library_archives(library_id, relative_path);
|
|
523
|
-
`});var
|
|
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(`
|
|
524
549
|
INSERT OR IGNORE INTO gc_locks (
|
|
525
550
|
scope, owner_id, owner_pid, heartbeat_at, created_at
|
|
526
551
|
) VALUES (?, ?, ?, ?, ?)
|
|
527
|
-
`,[e,
|
|
552
|
+
`,[e,n,process.pid,r,r]),a=await t.queryOne("SELECT owner_id FROM gc_locks WHERE scope = ?",[e],s=>E(s,"owner_id"))===n})}finally{await t.close()}if(!a)return;let o=setInterval(()=>{wk(e,n).catch(()=>{})},Sk);return async()=>{clearInterval(o);let s=await rd();try{await s.run("DELETE FROM gc_locks WHERE scope = ? AND owner_id = ?",[e,n])}finally{await s.close()}}}async function wk(e,t){let n=await rd();try{await n.run(`
|
|
528
553
|
UPDATE gc_locks
|
|
529
554
|
SET heartbeat_at = ?, owner_pid = ?
|
|
530
555
|
WHERE scope = ? AND owner_id = ?
|
|
531
|
-
`,[Date.now(),process.pid,e,t])}finally{await
|
|
556
|
+
`,[Date.now(),process.pid,e,t])}finally{await n.close()}}async function Ek(e){let t=await e.queryAll("SELECT * FROM gc_locks",void 0,kk);for(let{lock:n,scope:r}of t.filter(({lock:a})=>Ik(a)))await e.run(`
|
|
532
557
|
DELETE FROM gc_locks
|
|
533
558
|
WHERE scope = ?
|
|
534
559
|
AND owner_id = ?
|
|
535
560
|
AND heartbeat_at = ?
|
|
536
|
-
`,[n
|
|
561
|
+
`,[r,n.ownerId,n.heartbeatAt])}function Ik(e){return Date.now()-e.heartbeatAt<bk?!1:!Rk(e.ownerPid)}async function rd(){return await _(yk(ft(),"gc.sqlite"),gk)}function xk(e){return{createdAt:g(e,"created_at"),heartbeatAt:g(e,"heartbeat_at"),ownerId:E(e,"owner_id"),ownerPid:g(e,"owner_pid")}}function kk(e){return{lock:xk(e),scope:E(e,"scope")}}function Rk(e){try{return process.kill(e,0),!0}catch(t){return!(h(t)&&t.code==="ESRCH")}}var gk,bk,Sk,Qf=i(()=>{"use strict";fe();f();x();b();gk=`
|
|
537
562
|
CREATE TABLE IF NOT EXISTS gc_locks (
|
|
538
563
|
scope TEXT PRIMARY KEY,
|
|
539
564
|
owner_id TEXT NOT NULL,
|
|
@@ -541,8 +566,8 @@ CREATE TABLE IF NOT EXISTS gc_locks (
|
|
|
541
566
|
heartbeat_at INTEGER NOT NULL,
|
|
542
567
|
created_at INTEGER NOT NULL
|
|
543
568
|
);
|
|
544
|
-
`,
|
|
545
|
-
`,"utf8")}function
|
|
546
|
-
`}function
|
|
569
|
+
`,bk=6e4,Sk=2e4});import{readdir as Vf,rm as vk,stat as _k}from"fs/promises";import{join as Yf}from"path";async function Zf(e){let t=ld(),n=0,r=0,a=0;for(let o of await Ck(t)){n+=1;let s=await _k(o).catch(c=>{if(!(h(c)&&c.code==="ENOENT"))throw c});if(s===void 0||!e.force&&e.now-s.mtimeMs<Tk)continue;let d=await j(o);e.dryRun||await vk(o,{force:!0,recursive:!0}),r+=1,a+=d}return{freedBytes:a,removed:r,scanned:n}}async function Ck(e){try{let t=await Vf(e,{withFileTypes:!0}),n=[];for(let r of t){if(!r.isDirectory())continue;let a=Yf(e,r.name),o=await Vf(a,{withFileTypes:!0});for(let s of o)n.push(Yf(a,s.name))}return n}catch(t){if(h(t)&&t.code==="ENOENT")return[];throw t}}var Tk,ey=i(()=>{"use strict";fe();b();Jn();Tk=3600*1e3});import{mkdir as Pk,readFile as Ak,writeFile as Lk}from"fs/promises";import{join as Ok}from"path";async function Mi(e={}){let t=Date.now(),n=ft(),r=await Xf();if(r===void 0)return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:t};try{if(e.opportunistic===!0&&!await Fk(n,t))return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:t};let a={dryRun:e.dryRun===!0,force:e.force===!0,now:t,stateDirectoryPath:n},o=[];for(let d of Nk)o.push(await Mk(d,a));let s={finishedAt:Date.now(),freedBytes:id(o,"freedBytes"),jobs:o,removed:id(o,"removed"),scanned:id(o,"scanned"),skipped:!1,startedAt:t};return await Wk(n,s.finishedAt).catch(()=>{}),s}finally{await r()}}async function Mk(e,t){try{return{name:e.name,...await e.run(t)}}catch(n){return{error:He(n),freedBytes:0,name:e.name,removed:0,scanned:0}}}function id(e,t){return e.reduce((n,r)=>{let a=r[t];return n+(typeof a=="number"?a:0)},0)}async function Fk(e,t){let n=Number((await Ak(ty(e),"utf8").catch(()=>"")).trim());return!Number.isFinite(n)||t-n>=Dk}async function Wk(e,t){await Pk(e,{recursive:!0}),await Lk(ty(e),`${t}
|
|
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 ad(e){if(e===null||typeof e=="boolean"||typeof e=="number"||typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>ad(t)??null);if(typeof e=="object")return eR(e)}function eR(e){let t=Object.entries(e).map(([r,a],o)=>({index:o,key:r,priority:Zk.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=ad(r.value);a!==void 0&&(n[r.key]=a)}return n}import{existsSync as Kde,readFileSync as Jde}from"fs";import{dirname as Xde,join as Qde,parse as Vde,resolve as Yde}from"path";async function tR(){await dy("gc-worker",async({argv:e})=>{let t=nR(e);process.stdout.write(cy(await Mi({dryRun:t.dryRun,force:t.force})))})}function nR(e){return{dryRun:e.includes("--dry-run"),force:e.includes("--force")}}tR().catch(e=>{process.stderr.write(`${e instanceof Error?e.stack??e.message:String(e)}
|
|
547
572
|
`),process.exitCode=1});
|
|
548
573
|
//# sourceMappingURL=gc-worker.js.map
|