wiki-graph 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -271
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +2983 -4
- package/dist/cli.js.map +1 -0
- package/{data → dist/data}/editor/text_compressor.jinja +17 -13
- package/{data → dist/data}/help/commands/archive/next.jinja +7 -2
- package/{data → dist/data}/help/commands/gc.jinja +2 -1
- package/{data → dist/data}/help/commands/legacy/migrate.jinja +6 -1
- package/{data → dist/data}/help/commands/legacy.jinja +3 -2
- package/dist/data/help/commands/library-predicate.jinja +296 -0
- package/dist/data/help/commands/library.jinja +235 -0
- package/{data → dist/data}/help/commands/maintenance/chapter/add.jinja +3 -3
- package/{data → dist/data}/help/commands/maintenance/chapter/list.jinja +4 -4
- package/{data → dist/data}/help/commands/maintenance/chapter/move.jinja +4 -4
- package/{data → dist/data}/help/commands/maintenance/chapter/remove.jinja +1 -1
- package/{data → dist/data}/help/commands/maintenance/chapter/reset.jinja +1 -1
- package/{data → dist/data}/help/commands/maintenance/chapter/set-source.jinja +2 -2
- package/{data → dist/data}/help/commands/maintenance/chapter/set-summary.jinja +1 -1
- package/{data → dist/data}/help/commands/maintenance/chapter/set-title.jinja +3 -3
- package/{data → dist/data}/help/commands/maintenance/chapter/tree.jinja +6 -6
- package/{data → dist/data}/help/commands/maintenance/chapter.jinja +10 -10
- package/{data → dist/data}/help/commands/maintenance/meta.jinja +7 -7
- package/dist/data/help/commands/maintenance/upgrade.jinja +35 -0
- package/dist/data/help/commands/maintenance.jinja +23 -0
- package/{data → dist/data}/help/commands/predicate.jinja +204 -28
- package/{data → dist/data}/help/commands/root.jinja +10 -3
- package/{data → dist/data}/help/commands/uri.jinja +165 -6
- package/{data → dist/data}/help/topics/config.jinja +10 -3
- package/dist/data/help/topics/library.jinja +87 -0
- package/{data → dist/data}/help/topics/readiness.jinja +32 -2
- package/{data → dist/data}/help/topics/recipe.jinja +42 -17
- package/{data → dist/data}/help/topics/runtime.jinja +20 -3
- package/{data → dist/data}/help/topics/uri.jinja +51 -25
- package/dist/gc-worker.d.ts +1 -0
- package/dist/gc-worker.js +548 -0
- package/dist/gc-worker.js.map +1 -0
- package/dist/index.cjs +12463 -19690
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +36 -842
- package/dist/index.d.ts +36 -842
- package/dist/index.js +13871 -9
- package/dist/index.js.map +1 -0
- package/dist/queue-worker.d.ts +1 -0
- package/dist/queue-worker.js +2110 -0
- package/dist/queue-worker.js.map +1 -0
- package/package.json +18 -45
- package/dist/cli.cjs +0 -41734
- package/dist/cli.cjs.map +0 -1
- package/docs/images/flowchart.excalidraw +0 -3293
- package/docs/images/flowchart.svg +0 -4
- package/docs/images/terminal-cn.png +0 -0
- package/docs/images/terminal-en.png +0 -0
- package/docs/wikispine-runtime.md +0 -143
- /package/{data → dist/data}/editor/clue_reviewer.jinja +0 -0
- /package/{data → dist/data}/editor/clue_reviewer_generator.jinja +0 -0
- /package/{data → dist/data}/editor/revision_feedback.jinja +0 -0
- /package/{data → dist/data}/guaranteed/response_intent_classifier.jinja +0 -0
- /package/{data → dist/data}/help/commands/maintenance/cover.jinja +0 -0
- /package/{data → dist/data}/help/commands/transform.jinja +0 -0
- /package/{data → dist/data}/help/topics/format.jinja +0 -0
- /package/{data → dist/data}/output/epub/cover.xhtml.jinja +0 -0
- /package/{data → dist/data}/output/epub/nav.xhtml.jinja +0 -0
- /package/{data → dist/data}/output/epub/package.opf.xml.jinja +0 -0
- /package/{data → dist/data}/output/epub/section.xhtml.jinja +0 -0
- /package/{data → dist/data}/topologization/book_coherence_extraction.jinja +0 -0
- /package/{data → dist/data}/topologization/chunk_extraction.jinja +0 -0
- /package/{data → dist/data}/topologization/evidence_choice.jinja +0 -0
- /package/{data → dist/data}/topologization/translate_chunks.jinja +0 -0
- /package/{data → dist/data}/topologization/user_focused_extraction.jinja +0 -0
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,2986 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { fileURLToPath } from "
|
|
3
|
-
import { resolve } from "
|
|
2
|
+
import { fileURLToPath as __WIKIGRAPH_FILE_URL_TO_PATH__ } from "url";
|
|
3
|
+
import { resolve as __WIKIGRAPH_RESOLVE__ } from "path";
|
|
4
|
+
globalThis.__WIKIGRAPH_CLI_DIST_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL(".", import.meta.url)));
|
|
5
|
+
globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL("./data", import.meta.url)));
|
|
6
|
+
var bB=Object.defineProperty;var l=(e,t)=>()=>(e&&(t=e(e=0)),t);var Qe=(e,t)=>{for(var r in t)bB(e,r,{get:t[r],enumerable:!0})};function Mg(e){return wB[e]}function Ua(e){let t=e?.trim().toLowerCase().replaceAll("_","-");if(!(t===void 0||t===""))return SB.get(t)}var wB,SB,Nr=l(()=>{"use strict";wB={Arabic:"ar",Danish:"da",Dutch:"nl",English:"en",Finnish:"fi",French:"fr",German:"de",Hindi:"hi",Indonesian:"id",Italian:"it",Japanese:"ja",Korean:"ko",Norwegian:"no",Polish:"pl",Portuguese:"pt",Russian:"ru","Simplified Chinese":"zh",Spanish:"es",Swedish:"sv",Thai:"th","Traditional Chinese":"zh",Turkish:"tr",Vietnamese:"vi"},SB=new Map([["ar","ar"],["ara","ar"],["arabic","ar"],["da","da"],["danish","da"],["de","de"],["deu","de"],["dut","nl"],["dutch","nl"],["en","en"],["eng","en"],["english","en"],["fi","fi"],["fin","fi"],["finnish","fi"],["fr","fr"],["fra","fr"],["fre","fr"],["french","fr"],["ger","de"],["german","de"],["hi","hi"],["hin","hi"],["hindi","hi"],["id","id"],["ind","id"],["indonesian","id"],["it","it"],["ita","it"],["italian","it"],["ja","ja"],["japanese","ja"],["jpn","ja"],["ko","ko"],["kor","ko"],["korean","ko"],["nl","nl"],["no","no"],["nor","no"],["norwegian","no"],["pl","pl"],["pol","pl"],["polish","pl"],["por","pt"],["portuguese","pt"],["pt","pt"],["ru","ru"],["rus","ru"],["russian","ru"],["spa","es"],["spanish","es"],["es","es"],["sv","sv"],["swe","sv"],["swedish","sv"],["th","th"],["tha","th"],["thai","th"],["tr","tr"],["tur","tr"],["turkish","tr"],["vi","vi"],["vie","vi"],["vietnamese","vi"],["zh","zh"],["zh-cn","zh"],["zh-hans","zh"],["zh-sg","zh"],["zh-tw","zh"],["zh-hant","zh"],["zh-hk","zh"],["cn","zh"],["chi","zh"],["chinese","zh"],["simplified chinese","zh"],["traditional chinese","zh"],["zho","zh"]])});import{createHash as kB}from"crypto";function Zs(e){return new Wg().run(e)}function EB(e){return"toJSON"in e&&typeof e.toJSON=="function"}var jl,Wg,Dl=l(()=>{"use strict";jl=Symbol("UNSERIALIZABLE");Wg=class{#e=new Set;#t=kB("sha512");#r=new Set;run(t){if(!this.#n(t,""))throw new TypeError("Hash input must be JSON-serializable");return this.#t.digest("hex")}#n(t,r){let n=this.#s(t,r);return n===jl?!1:(this.#i(n),!0)}#i(t){if(t===null){this.#t.update("null","utf8");return}if(typeof t=="string"){this.#t.update(JSON.stringify(t),"utf8");return}if(typeof t=="number"){this.#t.update(Number.isFinite(t)?String(t):"null","utf8");return}if(typeof t=="boolean"){this.#t.update(t?"true":"false","utf8");return}if(this.#e.has(t))throw new TypeError("Hash input must be JSON-serializable");this.#e.add(t);try{if(Array.isArray(t)){this.#a(t);return}this.#o(t)}finally{this.#e.delete(t)}}#a(t){this.#t.update("[","utf8");for(let[r,n]of t.entries()){r>0&&this.#t.update(",","utf8");let i=this.#s(n,String(r));if(i===jl){this.#t.update("null","utf8");continue}this.#i(i)}this.#t.update("]","utf8")}#o(t){this.#t.update("{","utf8");let r=!1;for(let n of Object.keys(t)){let i=t[n],a=this.#s(i,n);a!==jl&&(r&&this.#t.update(",","utf8"),this.#t.update(JSON.stringify(n),"utf8"),this.#t.update(":","utf8"),this.#i(a),r=!0)}this.#t.update("}","utf8")}#s(t,r){if(t===null)return null;if(typeof t=="string"||typeof t=="boolean")return t;if(typeof t=="number")return Number.isFinite(t)?t:null;if(t===void 0||typeof t=="function"||typeof t=="symbol")return jl;if(typeof t=="bigint")throw new TypeError("Hash input must be JSON-serializable");if(t instanceof String||t instanceof Number||t instanceof Boolean)return this.#s(t.valueOf(),r);if(!EB(t))return t;if(this.#r.has(t))throw new TypeError("Hash input must be JSON-serializable");this.#r.add(t);try{return this.#s(t.toJSON(r),r)}finally{this.#r.delete(t)}}}});function R(e){return e instanceof Error}function Ce(e){let t=[],r=new Set,n=e;for(;n!==void 0&&!r.has(n);){if(r.add(n),n instanceof Error){Xx(t,IB(n)),n=n.cause;continue}Xx(t,String(n));break}return t.join(": ")}function IB(e){let t=e,r=e.message.trim(),n=typeof t.code=="string"&&t.code!==""?t.code:void 0,i=typeof t.path=="string"&&t.path!==""?t.path:void 0;return n==="ENOENT"?i===void 0?"File not found (ENOENT)":`File not found: ${i} (ENOENT)`:n==="EACCES"||n==="EPERM"?i===void 0?`Permission denied (${n})`:`Permission denied: ${i} (${n})`:r===""?n===void 0?e.name:`${e.name} (${n})`:n===void 0?r:`${r} (${n})`}function Xx(e,t){let r=t.trim();if(r==="")return;let n=e.at(-1);if(n===void 0){e.push(r);return}if(!(n===r||n.includes(r))){if(r.includes(n)){e[e.length-1]=r;return}e.push(r)}}var H=l(()=>{"use strict"});import{mkdir as Oie,readFile as Nie,rm as jie,writeFile as Die}from"fs/promises";import{join as Wie,resolve as Fie}from"path";import{z as ec}from"zod";var xB,Bie,Yx=l(()=>{"use strict";Dl();H();xB=1,Bie=ec.object({completedAt:ec.string().optional(),startedAt:ec.string(),status:ec.enum(["running","succeeded"]),version:ec.literal(xB)})});var Zx=l(()=>{"use strict";Yx()});import{readFile as PB,writeFile as _B}from"fs/promises";import{join as AB}from"path";var Ml,ev=l(()=>{"use strict";H();Ml=class{#e;constructor(t){this.#e=t}createEntry(t,r){return{cacheKey:t,response:r}}async read(t){try{return await PB(this.#t(t),"utf8")}catch(r){if(R(r)&&r.code==="ENOENT")return;throw r}}async write(t){await _B(this.#t(t.cacheKey),t.response,"utf8")}#t(t){return AB(this.#e,`${t}.txt`)}}});import{existsSync as LB,mkdirSync as OB,statSync as NB}from"fs";import{resolve as jB}from"path";function Fg(e){if(e===void 0)return;let t=jB(e);if(!LB(t))return OB(t,{recursive:!0}),t;if(NB(t).isDirectory())return t}function tv(e){let t=Fg(e);if(t!==void 0)return new Ml(t)}var rv=l(()=>{"use strict";ev()});function nv(e){let t=[],r=0;for(;r<e.messages.length;){let n=e.messages[r];if(n===void 0||n.role!=="system")break;t.push(n),r+=1}if(t.length===0)return e;if(t.length===1){let[n]=t;return n===void 0?e:{...e,messages:e.messages.slice(r),system:n}}return{...e,messages:e.messages.slice(r),system:t}}var iv=l(()=>{"use strict"});function av(e){return e.some(t=>t.role!=="system"&&(typeof t.content!="string"||t.content.trim()!==""))}function ov(e){let t=["[[Parameters]]:",` model=${e.modelIdentity}`,` temperature=${String(e.resolvedTemperature)}`,` top_p=${String(e.resolvedTopP)}`];return e.modelProvider!==void 0&&t.push(` provider=${e.modelProvider}`),t.push(` model_id=${e.modelId}`),e.scope!==void 0&&t.push(` scope=${e.scope}`),Array.isArray(e.temperature)&&t.push(` temperature_schedule=${JSON.stringify(e.temperature)}`),Array.isArray(e.topP)&&t.push(` top_p_schedule=${JSON.stringify(e.topP)}`),e.retryIndex!==void 0&&e.retryMax!==void 0&&t.push(` retry_progress=${e.retryIndex}/${e.retryMax}`),e.cacheKey!==void 0&&t.push(` cache_key=${e.cacheKey}`),e.sessionId!==void 0&&t.push(` session_id=${e.sessionId}`),`${t.join(`
|
|
7
|
+
`)}
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
`}function sv(e){return`[[Request]]:
|
|
10
|
+
${e.map(r=>`${GB(r.role)}:
|
|
11
|
+
${FB(r.content)}`).join(`
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
`)}
|
|
14
|
+
|
|
15
|
+
`}function $a(e,t){return[DB(e),...t===void 0?[]:[MB(t)]].join("")}function DB(e){return e==="cache-hit"?`[[Usage]]:
|
|
16
|
+
cache-hit
|
|
17
|
+
|
|
18
|
+
`:["[[Usage]]:",`input: ${Gg(e?.inputTokens)}`,`cache: ${Gg(e?.inputTokenDetails.cacheReadTokens)}`,`output: ${Gg(e?.outputTokens)}`,"",""].join(`
|
|
19
|
+
`)}function Gg(e){return e===void 0?"unavailable":String(e)}function MB(e){return`[[Error Stack]]:
|
|
20
|
+
${WB(e)}
|
|
21
|
+
|
|
22
|
+
`}function WB(e){return e instanceof Error?e.stack??Ce(e):Ce(e)}function FB(e){if(typeof e=="string")return e;let t=JSON.stringify(e,null,2);return typeof t=="string"?t:""}function GB(e){return e.length===0?e:`${e[0]?.toUpperCase()??""}${e.slice(1)}`}var cv=l(()=>{"use strict";H()});function dv(e){return typeof e=="string"?{identity:e,modelId:e}:UB(e)?{identity:e.provider===void 0?e.modelId:`${e.provider}:${e.modelId}`,modelId:e.modelId,...e.provider===void 0?{}:{provider:e.provider}}:{identity:"unknown-model",modelId:"unknown-model"}}function UB(e){return typeof e=="object"&&e!==null&&"modelId"in e&&typeof e.modelId=="string"&&(!("provider"in e)||typeof e.provider=="string")}var uv=l(()=>{"use strict"});import{APICallError as lv}from"ai";function pv(e){return lv.isInstance(e)?Ug(e)?!1:e.isRetryable?!0:KB(e.statusCode):!JB(e)&&zB(e)}function Ug(e){return lv.isInstance(e)&&e.statusCode===402}function KB(e){return typeof e=="number"&&BB.has(e)}function JB(e){return mv(e,t=>$B.has(t.name))}function zB(e){return mv(e,t=>{let r=t,n=typeof r.code=="string"?r.code.toUpperCase():void 0;if(n!==void 0&&HB.has(n))return!0;let i=t.message.toLowerCase();return qB.some(a=>i.includes(a))})}function mv(e,t){let r=new Set,n=e;for(;n instanceof Error&&!r.has(n);){if(t(n))return!0;r.add(n),n=n.cause}return!1}var $B,BB,HB,qB,hv=l(()=>{"use strict";$B=new Set(["AbortError","ResponseAborted","TimeoutError"]),BB=new Set([408,409,425,429,500,502,503,504,520,522,524,529]),HB=new Set(["ECONNRESET","ECONNREFUSED","EAI_AGAIN","ETIMEDOUT","EPIPE","UND_ERR_CONNECT_TIMEOUT","UND_ERR_HEADERS_TIMEOUT","UND_ERR_SOCKET"]),qB=["connection","terminated","timeout","network","rate limit"]});import{AsyncLocalStorage as QB}from"async_hooks";import{randomUUID as VB}from"crypto";import{existsSync as XB,mkdirSync as Wl}from"fs";import{dirname as YB,join as Ba,resolve as $g}from"path";import yv,{multistream as ZB}from"pino";import fv from"pino-pretty";async function rc(e,t){let r=e.logDirPath===void 0?void 0:$g(e.logDirPath),n=rH(),i=r===void 0?void 0:Ba(r,n),a=i===void 0?void 0:Ba(i,"artifacts");i!==void 0&&Wl(i,{recursive:!0});let o=tH({operation:e.operation,runId:n,verbose:e.verbose??!1,...i===void 0?{}:{eventLogPath:Ba(i,"run.log")}});return await Fl.run({artifactCounters:new Map,logger:o,runId:n,...a===void 0?{}:{artifactRootDirPath:a},...r===void 0?{}:{rootLogDirPath:r}},t)}function st(e){let t=Fl.getStore()?.logger??bv;return e===void 0?t:t.child(e)}function Bg(e){if(e.logDirPath===void 0)return;let t=$g(e.logDirPath),r=Fl.getStore();if(r?.rootLogDirPath===t&&r.artifactRootDirPath!==void 0){let n=Ba(r.artifactRootDirPath,e.category);return Wl(n,{recursive:!0}),Ba(n,e.fileName)}return Wl(t,{recursive:!0}),Ba(t,e.fileName)}function Gl(e){if(e.logDirPath===void 0)return;let t=$g(e.logDirPath),r=Fl.getStore(),n=e.extension??".log",i=r?.artifactCounters??eH,a=`${t}:${e.category}:${e.prefix}:${n}`,o=i.get(a),s=o===void 0?1:o+1;for(let c=s;;c+=1){let d=e.alwaysNumbered===!0?`${e.prefix}-${c}${n}`:c===1?`${e.prefix}${n}`:`${e.prefix}-${c}${n}`,u=Bg({category:e.category,fileName:d,logDirPath:e.logDirPath});if(u===void 0)return;if(!XB(u))return i.set(a,c),u}}function tH(e){let t=[];return e.eventLogPath!==void 0&&(Wl(YB(e.eventLogPath),{recursive:!0}),t.push({level:"info",stream:fv(gv(e.eventLogPath))})),e.verbose&&t.push({level:"info",stream:fv(gv(process.stderr))}),t.length===0?bv:yv({base:null,level:"info"},ZB(t)).child({operation:e.operation,runId:e.runId})}function gv(e){return{colorize:!1,destination:e,ignore:"pid,hostname,operation,runId,component,scope,sessionId",mkdir:typeof e=="string",singleLine:!0,sync:!0,translateTime:"SYS:yyyy-mm-dd HH:MM:ss"}}function rH(){let e=new Date,t=String(e.getUTCFullYear()),r=tc(e.getUTCMonth()+1),n=tc(e.getUTCDate()),i=tc(e.getUTCHours()),a=tc(e.getUTCMinutes()),o=tc(e.getUTCSeconds());return`${t}${r}${n}-${i}${a}${o}-${VB().slice(0,8)}`}function tc(e){return String(e).padStart(2,"0")}var Fl,eH,bv,jt=l(()=>{"use strict";Fl=new QB,eH=new Map,bv=yv({enabled:!1})});import{readFileSync as nH,statSync as wv}from"fs";import{resolve as kv,sep as Sv}from"path";import{Environment as iH,Loader as aH}from"nunjucks";function Ri(e,t={}){return new iH(new Hg(kv(e)),{autoescape:t.autoescape??!0,trimBlocks:t.trimBlocks??!0})}function dH(e){if(sH.test(e))throw new Error(`invalid path ${e}`);return`${e.replace(cH,"").replace(oH,"")}.jinja`}function uH(e,t,r){let n=e.endsWith(Sv)?e:`${e}${Sv}`;if(!(t===e||t.startsWith(n)))throw new Error(`cannot find ${r}`)}var oH,sH,cH,Hg,Ul=l(()=>{"use strict";oH=/\.jinja$/i,sH=/^\.+\//,cH=/^\/+/;Hg=class extends aH{#e;constructor(t){super(),this.#e=t}getSource(t){let r=dH(t),n=kv(this.#e,r);uH(this.#e,n,r);let i=nH(n,"utf8"),a=wv(n).mtimeMs;return{noCache:!1,path:n,src:i,upToDate:()=>wv(n).mtimeMs===a}}}});var xt,nc=l(()=>{"use strict";xt=class{#e=0;#t;#r=[];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.#n();try{return await t()}finally{this.#i()}}async#n(){if(this.#e<this.#t){this.#e+=1;return}await new Promise(t=>{this.#r.push(()=>{this.#e+=1,t()})})}#i(){this.#e-=1,this.#r.shift()?.()}}});var ic,qg=l(()=>{"use strict";jt();ic=class{#e;#t=new Map;#r;#n=[];#i=!1;sessionId;constructor(t,r,n){this.sessionId=t,this.#e=n,this.#r=r}async request(t,r={}){return this.#a(),await this.#r({logFiles:this.#n,messages:t,options:r,pendingCacheEntries:this.#t})}async commit(){if(!this.#i){for(let t of this.#t.values())this.#e!==void 0&&await this.#e.write(t);this.#t.clear(),this.#i=!0}}rollback(){return this.#i||(this.#t.clear(),this.#i=!0),Promise.resolve()}async run(t){try{let r=await t(this);return await this.commit(),r}catch(r){throw await this.rollback(),this.#o(),r}}#a(){if(this.#i)throw new Error("LLMContext is already finalized")}#o(){if(this.#n.length===0)return;let t=st({component:"llm-context",sessionId:this.sessionId});t.warn(`
|
|
23
|
+
[LLMContext] Failed with ${this.#n.length} log file(s):`);for(let[r,n]of this.#n.entries())t.warn(` ${r+1}. ${n}`)}}});var jr,Kg=l(()=>{"use strict";jr=class extends Error{isRetryable=!1;statusCode=402;constructor(t="LLM payment required.",r){super(t,r),this.name="LLMPaymentRequiredError"}}});import{appendFile as lH}from"fs/promises";function Ev(e){return e===void 0?new $l:new $l(Gl({alwaysNumbered:!0,category:"llm",logDirPath:e,prefix:"request"}))}var $l,Iv=l(()=>{"use strict";jt();$l=class{#e;constructor(t){this.#e=t}get filePath(){return this.#e}async append(t){this.#e!==void 0&&await lH(this.#e,t,"utf8")}}});function Bl(e,t,r,n){if(e===void 0)return;if(typeof e=="number")return e;if(e.length===1)return e[0];if(e.length!==2)throw new RangeError(`${t} must be a number or a 2-item range like [0.6, 0.98]`);let[i,a]=e;if(i===void 0||a===void 0)throw new RangeError(`${t} must be a number or a 2-item range like [0.6, 0.98]`);if(r===void 0||n===void 0||n<=0)return i;let s=Math.min(Math.max(r,0),n)/n;return i+(a-i)*s}function Jg(e,t,r,n){if(e===void 0)return{temperature:r,topP:n};if(t===void 0)return{temperature:r,topP:n};let i=t[e];return i===void 0?{temperature:r,topP:n}:{temperature:i.temperature??r,topP:i.topP??n}}var zg=l(()=>{"use strict"});import{setTimeout as pH}from"timers/promises";import{generateText as mH,streamText as hH}from"ai";var fH,gH,xv,Dr,vv=l(()=>{"use strict";rv();iv();cv();uv();hv();jt();Ul();nc();Dl();H();qg();Kg();Iv();zg();fH=36e4,gH=6,xv=0,Dr=class{#e;#t;#r;#n;#i;#a;#o;#s;#c;#l;#u;#d;#m;#p;#h;#f;#b;#g;config;constructor(t){let r=t.concurrent??gH,n=t.timeout??fH,i=t.temperature??.6,a=t.topP??.6,o=t.sampling,s=t.stream??!1,c=dv(t.model);this.config=Object.freeze({concurrent:r,modelId:c.modelId,temperature:i,stream:s,timeout:n,topP:a,...c.provider===void 0?{}:{provider:c.provider},...o===void 0?{}:{sampling:o}}),this.#e=tv(t.cacheDirPath),this.#t=new xt(r),this.#r=Fg(t.logDirPath),this.#n=t.model,this.#i=c.provider,this.#a=c.modelId,this.#o=c.identity,this.#s=t.onStreamProgress,this.#c=t.onTokenUsage,this.#l=new xt(r),this.#u=t.retryIntervalSeconds??6,this.#d=t.retryTimes??5,this.#m=o,this.#p=s,this.#h=Ri(t.dataDirPath),this.#f=i,this.#b=n,this.#g=a}context(){xv+=1;let t=xv;return new ic(t,async r=>await this.#w({...r.options,messages:r.messages,sessionId:t,...r.logFiles===void 0?{}:{logFiles:r.logFiles},...r.pendingCacheEntries===void 0?{}:{pendingCacheEntries:r.pendingCacheEntries}}),this.#e)}async withContext(t){return await this.context().run(t)}async request(t,r={}){return typeof t=="function"?await this.#t.use(async()=>await t(this.#y)):await this.#y(t,r)}#y=async(t,r={})=>await this.#w({messages:t,...r});loadSystemPrompt(t,r={}){return this.#h.render(t,r)}async#w(t){let r=Jg(t.scope,this.#m,this.#f,this.#g),n=t.temperature??r.temperature,i=t.topP??r.topP,a=Bl(n,"temperature",t.retryIndex,t.retryMax),o=Bl(i,"top_p",t.retryIndex,t.retryMax),s=(t.useCache??!0)&&av(t.messages),c=this.#e!==void 0&&s?Zs({messages:t.messages.map(f=>({content:f.content,role:f.role})),modelId:this.#a,provider:this.#i??null,temperature:a??null,topP:o??null}):void 0,d=Ev(this.#r);if(d.filePath!==void 0&&t.logFiles!==void 0&&t.logFiles.push(d.filePath),await d.append(ov({cacheKey:c,modelId:this.#a,modelIdentity:this.#o,modelProvider:this.#i,resolvedTemperature:a,resolvedTopP:o,retryIndex:t.retryIndex,retryMax:t.retryMax,scope:t.scope,sessionId:t.sessionId,temperature:n,topP:i})),await d.append(sv(t.messages)),c!==void 0&&this.#e!==void 0&&s){let f=await this.#e.read(c);if(f!==void 0)return st({component:"llm",scope:t.scope,sessionId:t.sessionId}).info(`[Cache Hit] Using cached response (key: ${c.slice(0,12)}...)`),await d.append(`[[Response]] (from cache):
|
|
24
|
+
${f}
|
|
25
|
+
|
|
26
|
+
`),await d.append($a("cache-hit")),f}let u,p,m;for(let f=0;f<=this.#d;f+=1)try{u=await this.#l.use(async()=>{let w=nv({maxRetries:0,messages:[...t.messages],model:this.#n,timeout:this.#b});if(t.signal!==void 0&&(w.abortSignal=t.signal),a!==void 0&&(w.temperature=a),o!==void 0&&(w.topP=o),this.#p){let b=[],E=hH(w);for await(let N of E.textStream)b.push(N),await this.#S(N.length);return p=await E.totalUsage,await this.#k(p),b.join("")}else{let b=await mH(w);return p=b.usage,await this.#S(b.text.length),await this.#k(p),b.text}}),await d.append(`[[Response]]:
|
|
27
|
+
${u}
|
|
28
|
+
|
|
29
|
+
`);break}catch(w){if(m=w,Ug(w)){let b=new jr(void 0,{cause:w});throw await d.append(`[[Error]]:
|
|
30
|
+
${Ce(b)}
|
|
31
|
+
|
|
32
|
+
`),await d.append($a(p,w)),b}if(!pv(w))throw await d.append(`[[Error]]:
|
|
33
|
+
${Ce(w)}
|
|
34
|
+
|
|
35
|
+
`),await d.append($a(p,w)),w;await d.append(`[[Warning]]:
|
|
36
|
+
Request failed with connection error, retrying... (${f+1} times)
|
|
37
|
+
|
|
38
|
+
`),f<this.#d&&this.#u>0&&await pH(this.#u*1e3,void 0,{signal:t.signal})}if(u===void 0){let f=m===void 0?`LLM request failed after ${this.#d+1} attempts`:`LLM request failed after ${this.#d+1} attempts: ${Ce(m)}`;throw await d.append(`[[Error]]:
|
|
39
|
+
${f}
|
|
40
|
+
|
|
41
|
+
`),await d.append($a(p,m)),new Error(f,{...m===void 0?{}:{cause:m}})}if(c!==void 0&&this.#e!==void 0&&s){let f=this.#e.createEntry(c,u);t.sessionId!==void 0&&t.pendingCacheEntries!==void 0?t.pendingCacheEntries.set(f.cacheKey,f):await this.#e.write(f)}return await d.append($a(p)),u}async#S(t){if(this.#s!==void 0)try{await this.#s({outputCharacters:t})}catch{return}}async#k(t){if(!(this.#c===void 0||t===void 0))try{await this.#c({...t.inputTokenDetails.cacheReadTokens===void 0?{}:{cacheReadTokens:t.inputTokenDetails.cacheReadTokens},...t.inputTokens===void 0?{}:{inputTokens:t.inputTokens},...t.outputTokens===void 0?{}:{outputTokens:t.outputTokens}})}catch{return}}}});var Cv=l(()=>{"use strict";vv()});var Qg=l(()=>{"use strict";Cv();qg();Kg();zg()});var yH,ac,Vg,oc=l(()=>{"use strict";yH=Object.freeze(["serial-generation/editor-compress","serial-generation/editor-review","serial-generation/editor-review-guide","serial-generation/reader-choice","serial-generation/reader-extraction"]),ac=Object.freeze({compress:"serial-generation/editor-compress",review:"serial-generation/editor-review",reviewGuide:"serial-generation/editor-review-guide"}),Vg=Object.freeze({choice:"serial-generation/reader-choice",extraction:"serial-generation/reader-extraction"})});var Xg,Tv=l(()=>{"use strict";Xg="wikigraph"});import{existsSync as Pv,statSync as _v}from"fs";import{fileURLToPath as bH}from"url";import{dirname as Av,join as Rv,parse as wH,resolve as Yg}from"path";function Mr(){let e=globalThis.__WIKIGRAPH_DATA_DIR__;if(typeof e=="string"&&e!=="")return e;let t=SH();return t!==void 0?t:kH()}function SH(){let e=Av(bH(import.meta.url));for(let t of[Yg(e,"../../../data"),Yg(e,"../../data"),Yg(e,"../data")])if(Pv(t)&&_v(t).isDirectory())return t}function kH(){let e=process.cwd(),t=wH(e).root;for(;;){for(let r of[Rv(e,"data"),Rv(e,"packages","core","data")])if(Pv(r)&&_v(r).isDirectory())return r;if(e===t)throw new Error("Could not locate data directory");e=Av(e)}}var Hl=l(()=>{"use strict"});import{AsyncLocalStorage as EH}from"async_hooks";import{stat as IH}from"fs/promises";import{resolve as xH}from"path";async function CH(e){let t=await IH(e).catch(r=>{if(!(typeof r=="object"&&r!==null&&"code"in r&&r.code==="ENOENT"))throw r});return t===void 0||t.size===0}function y(e,t){let r=e[t];if(typeof r!="number")throw new TypeError(`Expected ${t} to be a number`);return r}function k(e,t){let r=e[t];if(typeof r!="string")throw new TypeError(`Expected ${t} to be a string`);return r}function Te(e,t){let r=e[t];if(r!=null){if(typeof r!="string")throw new TypeError(`Expected ${t} to be a string`);return r}}async function TH(e,t={}){let r=await RH(),n=(t.readonly===!0?r.OPEN_READONLY:r.OPEN_READWRITE|r.OPEN_CREATE)|r.OPEN_FULLMUTEX;return await new Promise((i,a)=>{let o=new r.Database(e,n,s=>{if(s!==null){a(s);return}i(o)})})}async function RH(){let e=await import("sqlite3");return PH(e)}function PH(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 Zg(e){return e??[]}var vH,$,M=l(()=>{"use strict";vH=900*1e3;$=class e{#e;#t;#r=new EH;#n;#i=!1;#a=Promise.resolve();#o=0;constructor(t,r={}){this.#e=t,this.#t=r.onWrite}static async open(t,r="",n={}){let i=xH(t),a=n.readonly!==!0&&r.trim()!==""&&await CH(i),o=await TH(i,n),s=new e(o,n);return await s.#d(`PRAGMA busy_timeout = ${vH}`),n.readonly!==!0&&r.trim()!==""&&(await s.#d(r),a&&s.#l()),s}static async initialize(t,r){let n=await e.open(t);try{r.trim()!==""&&await n.#d(r)}finally{await n.close()}}async queryAll(t,r,n){return await this.#c(async()=>(this.#s(),(await this.#m(t,r)).map(n)))}async queryOne(t,r,n){return await this.#c(async()=>{this.#s();let i=await this.#p(t,r);return i===void 0?void 0:n(i)})}async run(t,r){await this.#c(async()=>{this.#s(),await this.#h(t,r),this.#l()})}async transaction(t){return await this.#c(async()=>{this.#s();let r=this.#o===0,n=this.#n??Symbol("database transaction scope");r&&(await this.#d("BEGIN IMMEDIATE"),this.#n=n),this.#o+=1;try{let i=await this.#r.run(n,t);return r&&await this.#d("COMMIT"),i}catch(i){throw r&&await this.#d("ROLLBACK"),i}finally{this.#o-=1,r&&(this.#n=void 0)}})}async flush(){await this.#c(()=>{this.#s()})}async close(){await this.#c(async()=>{this.#i||(await this.#u(),this.#i=!0)})}async getLastInsertRowId(){let t=await this.queryOne("SELECT last_insert_rowid() AS row_id",void 0,r=>y(r,"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#c(t){let r=this.#r.getStore();if(r!==void 0&&r===this.#n)return await t();let n=this.#a.then(t);return this.#a=n.then(()=>{},()=>{}),await n}#l(){this.#t?.()}async#u(){await new Promise((t,r)=>{this.#e.close(n=>{if(n!==null){r(n);return}t()})})}async#d(t){await new Promise((r,n)=>{this.#e.exec(t,i=>{if(i!==null){n(i);return}r()})})}async#m(t,r){return await new Promise((n,i)=>{this.#e.all(t,Zg(r),(a,o)=>{if(a!==null){i(a);return}n(o)})})}async#p(t,r){return await new Promise((n,i)=>{this.#e.get(t,Zg(r),(a,o)=>{if(a!==null){i(a);return}n(o)})})}async#h(t,r){await new Promise((n,i)=>{this.#e.run(t,Zg(r),a=>{if(a!==null){i(a);return}n()})})}}});import{AsyncLocalStorage as Ov}from"async_hooks";import{homedir as _H}from"os";import{join as Pi,resolve as Lv}from"path";function ct(){let e=AH.getStore()?.path;if(e!==void 0&&e.trim()!=="")return Lv(e);let t=Nv.getStore()?.path;return t!==void 0&&t.trim()!==""?Lv(t):Pi(_H(),".wikigraph")}async function ey(e,t){return await Nv.run({path:e},t)}function Se(){return Pi(ct(),"core.sqlite")}function Rn(){return Pi(ct(),"cache")}function Ha(){return Pi(Rn(),"cache.sqlite")}function qa(){return Pi(ct(),"jobs")}function Ve(){return Pi(ct(),"staging")}function Ka(){return Pi(ct(),"tmp")}var AH,Nv,Ue=l(()=>{"use strict";AH=new Ov,Nv=new Ov});import{mkdir as LH,rm as OH,stat as Mv}from"fs/promises";import{dirname as NH,join as Re,resolve as ry}from"path";async function ke(){let e=Se(),t=ry(e),r=await Kl(e);if(ty(t,r))return;if(_i!==void 0){await _i;let i=await Kl(e);if(ty(t,i))return}let n=await Kl(e);if(_i!==void 0)return await _i,ke();if(!ty(t,n)){_i=(async()=>{if(!await Wr(e)){await Dv(e,ql),await jv(t);return}let i=await Ja(e);if(i>ql)throw new Error(`Unsupported Wiki Graph home schema version: ${i}.`);i<ql&&(await GH(),await FH(),await HH(),await Dv(e,ql)),await jv(t)})();try{await _i;return}finally{_i=void 0}}}async function jv(e){let t=await Kl(e);if(t===void 0){sc=void 0;return}sc={fingerprint:t,path:e}}async function Kl(e){try{let t=await Mv(e);return{dev:t.dev,ino:t.ino,mtimeMs:t.mtimeMs,size:t.size}}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}}function ty(e,t){return t===void 0||sc===void 0?!1:sc.path===e&&MH(sc.fingerprint,t)}function MH(e,t){return e.dev===t.dev&&e.ino===t.ino&&e.mtimeMs===t.mtimeMs&&e.size===t.size}async function Ja(e=Se()){if(!await Wr(e))return 0;let t=await $.open(e,"",{readonly:!0});try{return await ar(t,"schema_versions")?await t.queryOne("SELECT version FROM schema_versions WHERE scope = ?",["home"],n=>Number(n.version))??1:1}finally{await t.close()}}function Wv(e){let t=ry(e);return WH().some(r=>t===ry(r))}function WH(){let e=Rn();return[Se(),Re(e,"search-sessions.sqlite"),Re(e,"continuation-cursors.sqlite"),Ha(),Re(qa(),"job.sqlite"),Re(Ka(),"gc.sqlite"),Re(Ve(),"staging.sqlite")]}async function Dv(e,t){await LH(NH(e),{recursive:!0});let r=await $.open(e);try{await r.run(DH),await r.run(`
|
|
42
|
+
INSERT INTO schema_versions (scope, version, updated_at)
|
|
43
|
+
VALUES (?, ?, ?)
|
|
44
|
+
ON CONFLICT(scope) DO UPDATE SET
|
|
45
|
+
version = excluded.version,
|
|
46
|
+
updated_at = excluded.updated_at
|
|
47
|
+
`,["home",t,new Date().toISOString()])}finally{await r.close()}}async function FH(){let e=Rn(),t=Ve(),r=qa(),n=Ka();await ir(Re(e,"search-sessions.sqlite")),await ir(Re(e,"continuation-cursors.sqlite")),await ir(Ha()),await ir(Re(t,"library")),await ir(Re(n,"gc.sqlite")),await ir(Re(n,"gc.last-run")),await ir(Re(r,"cache")),await ir(Re(r,"job.sqlite"));let i=Re(t,"staging.sqlite");await Wr(i)&&(await KH(i),await JH(i))}async function GH(){await UH(),await $H(),await BH(),await qH()}async function UH(){let e=Se();if(!await Wr(e))return;let t=await $.open(e,"",{readonly:!0});try{if(await ar(t,"library_locks")&&(await t.queryAll(`
|
|
48
|
+
SELECT owner_pid, heartbeat_at
|
|
49
|
+
FROM library_locks
|
|
50
|
+
`,void 0,i=>({heartbeatAt:Number(i.heartbeat_at),ownerPid:Number(i.owner_pid)}))).some(i=>cc(i.ownerPid,i.heartbeatAt)))throw new Error("Cannot upgrade home with active library locks.");if(!await ar(t,"state_locks"))return;if((await t.queryAll(`
|
|
51
|
+
SELECT owner_pid, heartbeat_at
|
|
52
|
+
FROM state_locks
|
|
53
|
+
WHERE scope = 'library'
|
|
54
|
+
`,void 0,n=>({heartbeatAt:Number(n.heartbeat_at),ownerPid:Number(n.owner_pid)}))).some(n=>cc(n.ownerPid,n.heartbeatAt)))throw new Error("Cannot upgrade home with active library locks.")}finally{await t.close()}}async function $H(){let e=Re(Ka(),"gc.sqlite");if(!await Wr(e))return;let t=await $.open(e,"",{readonly:!0});try{if(!await ar(t,"gc_locks"))return;if((await t.queryAll("SELECT owner_pid, heartbeat_at FROM gc_locks",void 0,n=>({heartbeatAt:Number(n.heartbeat_at),ownerPid:Number(n.owner_pid)}))).some(n=>cc(n.ownerPid,n.heartbeatAt)))throw new Error("Cannot upgrade home with an active GC lock.")}finally{await t.close()}}async function BH(){let e=Re(Ve(),"staging.sqlite");if(!await Wr(e))return;let t=await $.open(e,"",{readonly:!0});try{for(let r of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){if(!await ar(t,r))continue;if((await t.queryAll(`
|
|
55
|
+
SELECT owner_pid, heartbeat_at
|
|
56
|
+
FROM ${r}
|
|
57
|
+
`,void 0,i=>({heartbeatAt:Number(i.heartbeat_at),ownerPid:Number(i.owner_pid)}))).some(i=>cc(i.ownerPid,i.heartbeatAt)))throw new Error(`Cannot upgrade home with active coordinator state: ${r}.`)}}finally{await t.close()}}async function HH(){let e=Re(Ve(),"staging.sqlite");if(!await Wr(e))return;let t=await $.open(e,"",{readonly:!0});try{if(!await ar(t,"entry_overlays"))return;if((await t.queryAll(`
|
|
58
|
+
SELECT entry_path
|
|
59
|
+
FROM entry_overlays
|
|
60
|
+
`,void 0,i=>String(i.entry_path))).find(i=>i!==Jl)!==void 0)throw new Error("Cannot upgrade home with non-derived coordinator overlays.")}finally{await t.close()}}async function qH(){let e=Re(qa(),"job.sqlite");if(!await Wr(e))return;let t=await $.open(e,"",{readonly:!0});try{if(await ar(t,"build_jobs")&&(await t.queryAll(`
|
|
61
|
+
SELECT job_id
|
|
62
|
+
FROM build_jobs
|
|
63
|
+
WHERE state IN ('queued', 'running', 'canceling', 'paused')
|
|
64
|
+
`,void 0,n=>String(n.job_id))).length>0)throw new Error("Cannot upgrade home with active build jobs.");if(await ar(t,"build_worker_lease")){let r=await t.queryOne(`
|
|
65
|
+
SELECT owner_pid, heartbeat_at
|
|
66
|
+
FROM build_worker_lease
|
|
67
|
+
WHERE id = 1
|
|
68
|
+
`,void 0,n=>({heartbeatAt:n.heartbeat_at===null?void 0:Number(n.heartbeat_at),ownerPid:n.owner_pid===null?void 0:Number(n.owner_pid)}));if(r?.ownerPid!==void 0&&r.heartbeatAt!==void 0&&cc(r.ownerPid,r.heartbeatAt))throw new Error("Cannot upgrade home with an active build worker lease.")}}finally{await t.close()}}async function KH(e,t){let r=await $.open(e);try{if(!await ar(r,"entry_overlays"))return;let n=t===void 0?"WHERE entry_path = ?":"WHERE archive_key = ? AND entry_path = ?",i=t===void 0?[Jl]:[t,Jl],a=await r.queryAll(`
|
|
69
|
+
SELECT archive_key, workspace_path
|
|
70
|
+
FROM entry_overlays
|
|
71
|
+
${n}
|
|
72
|
+
`,i,o=>({archiveKey:String(o.archive_key),workspacePath:o.workspace_path===null?void 0:String(o.workspace_path)}));for(let o of a)o.workspacePath!==void 0&&await ir(o.workspacePath);await r.run(`
|
|
73
|
+
DELETE FROM entry_overlays
|
|
74
|
+
${n}
|
|
75
|
+
`,i)}finally{await r.close()}}async function JH(e){let t=await $.open(e);try{if(!await ar(t,"entry_overlays"))return;let r=await t.queryAll(`
|
|
76
|
+
SELECT archive_key, archive_path, entry_path, workspace_path
|
|
77
|
+
FROM entry_overlays
|
|
78
|
+
WHERE entry_path IN (?, ?)
|
|
79
|
+
`,["database.db",Jl],n=>({archiveKey:String(n.archive_key),archivePath:String(n.archive_path),entryPath:String(n.entry_path),workspacePath:n.workspace_path===null?void 0:String(n.workspace_path)}));for(let n of r)await Wr(n.archivePath)||(n.workspacePath!==void 0&&await ir(n.workspacePath),await t.run(`
|
|
80
|
+
DELETE FROM entry_overlays
|
|
81
|
+
WHERE archive_key = ? AND entry_path = ?
|
|
82
|
+
`,[n.archiveKey,n.entryPath]))}finally{await t.close()}}async function ar(e,t){return await e.queryOne(`
|
|
83
|
+
SELECT 1 AS present
|
|
84
|
+
FROM sqlite_master
|
|
85
|
+
WHERE type = 'table' AND name = ?
|
|
86
|
+
`,[t],()=>!0)===!0}function cc(e,t){return Date.now()-t<=jH&&zH(e)}function zH(e){try{return process.kill(e,0),!0}catch{return!1}}async function ir(e){await OH(e,{force:!0,recursive:!0})}async function Wr(e){try{return await Mv(e),!0}catch{return!1}}var ql,jH,Jl,_i,sc,DH,Ai=l(()=>{"use strict";Ue();H();M();ql=2,jH=300*1e3,Jl="fts.db",DH=`
|
|
87
|
+
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
88
|
+
scope TEXT PRIMARY KEY,
|
|
89
|
+
version INTEGER NOT NULL,
|
|
90
|
+
updated_at TEXT NOT NULL
|
|
91
|
+
);
|
|
92
|
+
`});import{createHash as QH}from"crypto";import{chmod as VH,mkdir as Uv,readFile as $v,rename as XH,rm as Bv,stat as Hv,writeFile as qv}from"fs/promises";import{dirname as YH,resolve as ZH}from"path";import{setTimeout as eq}from"timers/promises";async function V(e,t,r={}){return await nq(e),await Kv(e,t),await $.open(e,"",r)}function nq(e){return Wv(e)?ke():Promise.resolve()}async function Kv(e,t){let r=ZH(e),n=lq(r),i=mq(t);if(await Gv(r,n,i)){await Pn(r),await Pn(n);return}await Uv(YH(r),{recursive:!0}),await aq(r,async()=>{if(await Gv(r,n,i)){await Pn(r),await Pn(n);return}await $.initialize(r,t),await Pn(r),await iq(n,i)}),await Pn(r),await Pn(n)}async function iq(e,t){let r=`${e}.${process.pid}.tmp`;await qv(r,`${t}
|
|
93
|
+
`,{encoding:"utf8",mode:384}),await XH(r,e),await Pn(e)}async function aq(e,t){let r=`${e}.init.lock`;for(;;)try{await Uv(r,{mode:448});break}catch(n){if(!R(n)||n.code!=="EEXIST")throw n;await oq(r),await eq(tq)}try{await Fv(r);let n=setInterval(()=>{Fv(r).catch(()=>{})},rq);try{return await t()}finally{clearInterval(n)}}finally{await Bv(r,{force:!0,recursive:!0})}}async function Fv(e){await qv(`${e}/owner.json`,`${JSON.stringify({at:Date.now(),pid:process.pid},null,2)}
|
|
94
|
+
`,{encoding:"utf8",mode:384})}async function oq(e){try{let t=await sq(e);if(t!==void 0){if(uq(t.pid)||Date.now()-t.at<ny)return}else if(!await dq(e,ny))return;await Bv(e,{force:!0,recursive:!0})}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}}async function sq(e){try{let t=JSON.parse(await $v(`${e}/owner.json`,"utf8"));return cq(t)?t:void 0}catch(t){if(R(t)&&t.code==="ENOENT"||t instanceof SyntaxError)return;throw t}}function cq(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 dq(e,t){let r=await Hv(e);return Date.now()-r.mtimeMs>=t}function uq(e){try{return process.kill(e,0),!0}catch(t){return!(R(t)&&t.code==="ESRCH")}}async function Gv(e,t,r){if(!await pq(e))return!1;try{return(await $v(t,"utf8")).trim()===r}catch(n){if(R(n)&&n.code==="ENOENT")return!1;throw n}}function lq(e){return`${e}.initialized`}async function pq(e){try{return(await Hv(e)).isFile()}catch(t){if(R(t)&&t.code==="ENOENT")return!1;throw t}}async function Pn(e){try{await VH(e,384)}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}}function mq(e){return QH("sha256").update(e).digest("hex")}var tq,ny,rq,Jv=l(()=>{"use strict";H();Ai();M();tq=50,ny=300*1e3,rq=Math.floor(ny/2)});import{mkdir as hq,readFile as fq,readdir as gq,rm as yq,writeFile as bq}from"fs/promises";var zv,Qv=l(()=>{"use strict";H();zv={deleteTree:async e=>{await yq(e,{force:!0,recursive:!0})},ensureDirectory:async e=>{await hq(e,{recursive:!0})},listFiles:async e=>(await gq(e,{withFileTypes:!0})).filter(t=>t.isFile()).map(t=>t.name),readFile:async e=>{try{return await fq(e)}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}},writeFile:async(e,t,r)=>{await bq(e,t,{...typeof t=="string"?{encoding:"utf8"}:{},flag:r.overwrite===!0?"w":"wx"})}}});function za(e){let t=new Set(Object.values(e));return r=>t.has(r)}function dc(e,t){let r=za(e);return n=>{if(r(n))return n;throw new Error(`Unknown ${t}: ${n}`)}}var iy=l(()=>{"use strict"});function wq(e){return e.replace(/\s+/gu," ").trim()}var Dt,ay,oy,or,sy,Qa,Fr,Gr=l(()=>{"use strict";iy();Dt=(i=>(i.Verbatim="verbatim",i.Detailed="detailed",i.Focused="focused",i.Relevant="relevant",i))(Dt||{}),ay=za(Dt),oy=dc(Dt,"chunk retention"),or=(n=>(n.Critical="critical",n.Important="important",n.Helpful="helpful",n))(or||{}),sy=za(or),Qa=dc(or,"chunk importance"),Fr=class{#e;rawText;wordsCount;constructor(t,r){this.rawText=t,this.wordsCount=r}get text(){return this.#e??=wq(this.rawText),this.#e}toJSON(){return{text:this.text,wordsCount:this.wordsCount}}}});var zl,cy=l(()=>{"use strict";Gr();zl=class{#e=!1;#t;#r;#n=[];#i;#a;#o="";constructor(t,r,n){this.#t=n.discard,this.#r=n.finalize,this.#i=t,this.#a=r}addSentence(t,r,n){this.#s();let i=this.#a+this.#n.length,a=new Fr(t,r);return n!==void 0&&Object.assign(a,{byteLength:n.byteLength,byteOffset:n.byteOffset}),this.#n.push(a),[this.#i,i]}async commit(){return this.#s(),this.#e=!0,await this.#r(this.#a,this.#o,this.#n)}async commitWithText(t){return this.#s(),this.#e=!0,await this.#r(this.#a,t,this.#n)}discard(){this.#s(),this.#e=!0,this.#t()}setSummary(t){this.#s(),this.#o=t}get fragmentId(){return this.#a}#s(){if(this.#e)throw new Error("Text stream draft is already finalized")}}});async function Xv(e,t){if(t!==void 0)return await Sq(e,t);let r=[];for(let n of kq().segment(e)){let i=n.segment;if(i.trim()==="")continue;let a=new Fr(i,Eq(i));Object.assign(a,{byteLength:Buffer.byteLength(i,"utf8"),byteOffset:Buffer.byteLength(e.slice(0,n.index),"utf8")}),r.push(a)}return r}async function Sq(e,t){let r=[];for await(let n of t.pipe([e])){let i=e.slice(n.offset,n.offset+n.text.length);if(i.trim()==="")continue;let a=new Fr(i,n.wordsCount);Object.assign(a,{byteLength:Buffer.byteLength(i,"utf8"),byteOffset:Buffer.byteLength(e.slice(0,n.offset),"utf8")}),r.push(a)}return r}function kq(){return Vv??=new Intl.Segmenter(void 0,{granularity:"sentence"}),Vv}function Eq(e){let t=e.trim();return t===""?0:t.split(/\s+/u).length}function Yv(e){let t=e.byteOffset;return typeof t=="number"?t:0}function Zv(e){return typeof e.byteOffset=="number"}function eC(e){let t=e.byteLength;return typeof t=="number"?t:Buffer.byteLength(dy(e),"utf8")}function dy(e){let t=e.rawText;return typeof t=="string"?t:e.text}var Vv,tC=l(()=>{"use strict";Gr()});var _n,uy=l(()=>{"use strict";_n={source:1,summary:2}});import{join as rC,resolve as xq}from"path";function Ql(e){return{byteLength:Number(e.byte_length),byteOffset:Number(e.byte_offset),sentenceIndex:Number(e.sentence_index),wordsCount:Number(e.words_count)}}var Va,ly=l(()=>{"use strict";Gr();cy();tC();uy();Va=class e{static#e=new Map;#t;#r;#n;#i;#a;constructor(t,r,n,i,a){this.#t=r,this.#r=xq(t),this.#n=n,this.#i=i,this.#a=a}async createDraft(){let t=this.#f();if(t.draftOpen)throw new Error("Only one text stream draft can be open at a time");return await this.#n.ensureDirectory(this.#p()),t.draftOpen=!0,new zl(this.#a,await this.#d(),{discard:()=>{t.draftOpen=!1},finalize:async(r,n,i)=>await this.#l(r,n,i)})}async getFragment(t){let n=(await this.#u()).find(i=>i.fragmentId===t);if(n===void 0)throw new Error(`Fragment ${t} does not exist`);return n}async getSentence(t){let r=await this.#o(t);if(r===void 0)throw new RangeError(`Sentence ${t} does not exist`);return this.#c(r,await this.#m())}async listFragmentIds(){return(await this.#u()).map(t=>t.fragmentId)}async listSentences(){let t=await this.#s(),r=await this.#m();return t.map(n=>this.#c(n,r))}async listSentencesInRange(t,r){if(r<t)return[];let n=await this.#t.queryAll(`
|
|
95
|
+
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
96
|
+
FROM text_sentence_records
|
|
97
|
+
WHERE kind = ? AND chapter_id = ?
|
|
98
|
+
AND sentence_index BETWEEN ? AND ?
|
|
99
|
+
ORDER BY sentence_index
|
|
100
|
+
`,[_n[this.#i],this.#a,t,r],Ql),i=await this.#m();return n.map(a=>this.#c(a,i))}async readTextInRange(t,r){if(r<t)return"";let n=await this.#t.queryAll(`
|
|
101
|
+
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
102
|
+
FROM text_sentence_records
|
|
103
|
+
WHERE kind = ? AND chapter_id = ?
|
|
104
|
+
AND sentence_index BETWEEN ? AND ?
|
|
105
|
+
ORDER BY sentence_index
|
|
106
|
+
`,[_n[this.#i],this.#a,t,r],Ql),i=n[0],a=n[n.length-1];return i===void 0||a===void 0?void 0:(await this.#m()).subarray(i.byteOffset,a.byteOffset+a.byteLength).toString("utf8")}async#o(t){return await this.#t.queryOne(`
|
|
107
|
+
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
108
|
+
FROM text_sentence_records
|
|
109
|
+
WHERE kind = ? AND chapter_id = ? AND sentence_index = ?
|
|
110
|
+
`,[_n[this.#i],this.#a,t],Ql)}async#s(){return await this.#t.queryAll(`
|
|
111
|
+
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
112
|
+
FROM text_sentence_records
|
|
113
|
+
WHERE kind = ? AND chapter_id = ?
|
|
114
|
+
ORDER BY sentence_index
|
|
115
|
+
`,[_n[this.#i],this.#a],Ql)}#c(t,r){return new Fr(r.subarray(t.byteOffset,t.byteOffset+t.byteLength).toString("utf8"),t.wordsCount)}async readText(){let t=await this.#n.readFile(this.#h());return t===void 0?void 0:Buffer.from(t).toString("utf8")}async writeTextStream(t,r={}){await this.delete();let n=await Xv(t,r.segmenter),i=await this.createDraft();for(let a of n)i.addSentence(a.text,a.wordsCount,{byteOffset:a.byteOffset,byteLength:a.byteLength});await i.commitWithText(t)}async delete(){await this.#n.deleteTree(this.#h()),await this.#t.run(`
|
|
116
|
+
DELETE FROM text_sentence_records
|
|
117
|
+
WHERE kind = ? AND chapter_id = ?
|
|
118
|
+
`,[_n[this.#i],this.#a]),delete this.#f().nextSentenceIndex}get path(){return this.#h()}get serialId(){return this.#a}async#l(t,r,n){let i=this.#f();i.draftOpen=!1;let a=await this.#n.readFile(this.#h()),o=a===void 0?Buffer.alloc(0):Buffer.from(a),s=r===""?n.map(dy).join(""):r,c=Buffer.from(s,"utf8"),d=o.length;if(await this.#n.ensureDirectory(this.#p()),await this.#n.writeFile(this.#h(),Buffer.concat([o,c]),{overwrite:!0}),n.length!==0){for(let u=0;u<n.length;u+=1){let p=n[u];if(p===void 0)continue;let m=eC(p),f=Yv(p),w=d+f;await this.#t.run(`
|
|
119
|
+
INSERT OR REPLACE INTO text_sentence_records (
|
|
120
|
+
kind,
|
|
121
|
+
chapter_id,
|
|
122
|
+
sentence_index,
|
|
123
|
+
words_count,
|
|
124
|
+
byte_offset,
|
|
125
|
+
byte_length
|
|
126
|
+
)
|
|
127
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
128
|
+
`,[_n[this.#i],this.#a,t+u,p.wordsCount,w,m]),Zv(p)||(d+=m)}return i.nextSentenceIndex=t+n.length,{fragmentId:t,sentences:n,serialId:this.#a,summary:""}}}async#u(){let t=await this.listSentences(),r=[],n=[],i=0,a=0;for(let o=0;o<t.length;o+=1){let s=t[o];s!==void 0&&(n.length>0&&i+s.wordsCount>600&&(r.push({fragmentId:a,sentences:n,serialId:this.#a,summary:""}),a=o,n=[],i=0),n.push(s),i+=s.wordsCount)}return n.length>0&&r.push({fragmentId:a,sentences:n,serialId:this.#a,summary:""}),r}async#d(){let t=this.#f();return t.nextSentenceIndex!==void 0||(t.nextSentenceIndex=await this.#t.queryOne(`
|
|
129
|
+
SELECT COALESCE(MAX(sentence_index), -1) + 1 AS next_index
|
|
130
|
+
FROM text_sentence_records
|
|
131
|
+
WHERE kind = ? AND chapter_id = ?
|
|
132
|
+
`,[_n[this.#i],this.#a],r=>Number(r.next_index))??0),t.nextSentenceIndex}async#m(){let t=await this.#n.readFile(this.#h());return Buffer.from(t??new Uint8Array)}#p(){return rC(this.#r,"texts",this.#i)}#h(){return rC(this.#p(),`${this.#a}.txt`)}#f(){let t=`${this.#r}\0${this.#i}\0${this.#a}`,r=e.#e.get(t);return r===void 0&&(r={draftOpen:!1},e.#e.set(t,r)),r}}});import{join as vq,resolve as Cq}from"path";var uc,nC=l(()=>{"use strict";Qv();ly();uc=class{#e;#t;#r;constructor(t,r,n=zv){this.#e=r,this.#t=Cq(t),this.#r=n}async ensureCreated(){await this.#r.ensureDirectory(this.#n("source")),await this.#r.ensureDirectory(this.#n("summary"))}async getSentence(t){return(await this.getSerial(t[0]).getSentence(t[1])).text}getSerial(t){return new Va(this.#t,this.#e,this.#r,"source",t)}getSummarySerial(t){return new Va(this.#t,this.#e,this.#r,"summary",t)}#n(t){return vq(this.#t,"texts",t)}}});var py=l(()=>{"use strict";nC();cy();ly();uy()});import{mkdir as Tq,readFile as Rq,readdir as Pq,writeFile as _q}from"fs/promises";var Vl,Xa,Xl=l(()=>{"use strict";H();Vl={write:async(e,t)=>{await _q(e,t,"utf8")}},Xa={ensureDirectory:async e=>{await Tq(e,{recursive:!0})},listFiles:async e=>(await Pq(e,{withFileTypes:!0})).filter(t=>t.isFile()).map(t=>t.name),readFile:async e=>{try{return await Rq(e)}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}}}});var lc,my=l(()=>{"use strict";lc=class{#e=!1;#t;#r;#n;#i;#a=[];#o="";#s;constructor(t,r,n,i){this.#t=t,this.#r=i.discard,this.#n=i.finalize,this.#i=r,this.#s=n}addSentence(t,r){this.#c();let n=this.#s+this.#a.length;return this.#a.push({text:t,wordsCount:r}),[this.#t,n]}async commit(){return this.#c(),this.#e=!0,await this.#n(this.#i,this.#s,this.#o,this.#a)}discard(){this.#c(),this.#e=!0,this.#r()}setSummary(t){this.#c(),this.#o=t}get fragmentId(){return this.#i}#c(){if(this.#e)throw new Error("Fragment draft is already finalized")}}});import{join as Aq,resolve as Lq}from"path";async function iC(e,t=Xa){return hy(e,await Oq(e,t))}function hy(e,t){if(t===void 0)throw new Error(`Fragment file does not exist: ${e}`);let r=JSON.parse(Buffer.from(t).toString("utf8"));if(typeof r!="object"||r===null)throw new TypeError("Fragment file must be an object");if(!("summary"in r)||typeof r.summary!="string")throw new TypeError("Fragment file summary must be a string");if(!("sentences"in r)||!Array.isArray(r.sentences))throw new TypeError("Fragment file sentences must be an array");return{sentences:r.sentences.map(Nq),summary:r.summary}}async function Oq(e,t){if(t.listFileContents===void 0)return await t.readFile(e);let r=Lq(Aq(e,"..")),n=e.slice(r.length+1);return(await t.listFileContents(r)).get(n)}function Nq(e){if(typeof e!="object"||e===null)throw new TypeError("Sentence entry must be an object");let{text:t,wordsCount:r}=e;if(typeof t!="string"||typeof r!="number")throw new TypeError("Sentence entry is invalid");return{text:t,wordsCount:r}}var aC=l(()=>{"use strict";Xl()});function oC(e){return e.split(/\n+/u).map(t=>t.trim()).filter(t=>t!=="").map(t=>({text:t,wordsCount:jq(t)}))}function jq(e){let t=e.trim();return t===""?0:t.split(/\s+/u).length}var sC=l(()=>{"use strict"});var cC,dC,fy=l(()=>{"use strict";cC="serial-",dC=/^fragment_(\d+)\.json$/});import{join as uC,resolve as Mq}from"path";var Ya,gy=l(()=>{"use strict";H();my();Xl();aC();sC();fy();Ya=class{#e;#t=!1;#r;#n;#i;#a;#o;#s;#c;constructor(t,r,n="fragments",i,a){this.#r=Mq(t),this.#e=r,this.#n=a??Xa,this.#a=n,this.#c=i??Vl}async createDraft(){if(this.#t)throw new Error("Only one fragment draft can be open at a time");return await this.#n.ensureDirectory(this.path),this.#t=!0,new lc(this.#e,await this.#d(),await this.#m(),{discard:()=>{this.#u()},finalize:async(t,r,n,i)=>await this.#l(t,r,n,i)})}async getFragment(t){let r=this.#n.listFileContents===void 0?await iC(this.#p(t),this.#n):hy(this.#p(t),(await this.#h()).get(`fragment_${t}.json`));return{serialId:this.#e,fragmentId:t,summary:r.summary,sentences:r.sentences}}async listFragmentIds(){try{return(this.#n.listFileContents===void 0?await this.#n.listFiles(this.path):[...(await this.#h()).keys()]).map(r=>dC.exec(r)).filter(r=>r!==null).map(r=>Number(r[1])).sort((r,n)=>r-n)}catch(t){if(R(t)&&t.code==="ENOENT")return[];throw t}}async writeTextStream(t,r={}){await this.#n.ensureDirectory(this.path);let n=oC(t);if(n.length===0){let s=await this.#d();await this.#c.write(this.#p(s),JSON.stringify({sentences:[],summary:""},void 0,2)),this.#o=s+1;return}let i=r.maxWordsCount??600,a=await this.createDraft(),o=0;for(let s of n)o>0&&o+s.wordsCount>i&&(await a.commit(),a=await this.createDraft(),o=0),a.addSentence(s.text,s.wordsCount),o+=s.wordsCount;await a.commit()}get serialId(){return this.#e}get path(){return uC(this.#r,this.#a,`${cC}${this.#e}`)}async#l(t,r,n,i){if(this.#t=!1,i.length!==0)return await this.#n.ensureDirectory(this.path),await this.#c.write(this.#p(t),JSON.stringify({sentences:i,summary:n},void 0,2)),this.#o=t+1,this.#s=r+i.length,{serialId:this.#e,fragmentId:t,summary:n,sentences:i}}#u(){this.#t=!1}async#d(){if(this.#o!==void 0)return this.#o;let t=await this.listFragmentIds(),r=t[t.length-1];return this.#o=r===void 0?0:r+1,this.#o}async#m(){if(this.#s!==void 0)return this.#s;let t=0;for(let r of await this.listFragmentIds())t+=(await this.getFragment(r)).sentences.length;return this.#s=t,this.#s}#p(t){return uC(this.path,`fragment_${t}.json`)}async#h(){if(this.#n.listFileContents===void 0)throw new Error("Fragment file access does not support batch reads.");return this.#i??=this.#n.listFileContents(this.path),await this.#i}}});import{join as Wq,resolve as Fq}from"path";var Li,lC=l(()=>{"use strict";Xl();gy();Li=class{#e;#t;#r;constructor(t,r,n){this.#e=Fq(t),this.#t=n??Xa,this.#r=r??Vl}async ensureCreated(){await this.#t.ensureDirectory(this.path)}getSerial(t){return new Ya(this.#e,t,"fragments",this.#r,this.#t)}getSummarySerial(t){return new Ya(this.#e,t,"summaries",this.#r,this.#t)}async getSentence(t){let[r,n]=t,i=await this.getSerial(r).listFragmentIds(),a=n;for(let o of i){let s=await this.getSerial(r).getFragment(o),c=s.sentences[a];if(c!==void 0)return c.text;a-=s.sentences.length}throw new RangeError(`Sentence ${n} does not exist`)}async getSummary(t,r){return(await this.getSerial(t).getFragment(r)).summary}async getWordsCount(t,r){return(await this.getSerial(t).getFragment(r)).sentences.reduce((i,a)=>i+a.wordsCount,0)}get path(){return Wq(this.#e,"fragments")}}});var yy=l(()=>{"use strict";lC();my();gy();fy()});import{z as Mt}from"zod";var pC,Ur,mC,by,pc=l(()=>{"use strict";pC=["epub","pdf","txt","markdown"],Ur=1,mC=Mt.enum(pC),by=Mt.object({version:Mt.literal(Ur),sourceFormat:mC,title:Mt.string().min(1).nullable(),authors:Mt.array(Mt.string().min(1)),language:Mt.string().min(1).nullable(),identifier:Mt.string().min(1).nullable(),publisher:Mt.string().min(1).nullable(),publishedAt:Mt.string().min(1).nullable(),description:Mt.string().min(1).nullable()})});import{randomBytes as Gq}from"crypto";function sr(e){return`wikg://chapter/${e}`}function Oi(e,t="chapter path"){let r=e.trim();if(r==="")throw new Error(`Invalid ${t}: expected an absolute chapter path.`);if(/^\d+$/u.test(r))throw new Error(`Invalid ${t}: numeric chapter ids are internal and are not accepted.`);if(r.startsWith("/")||r.endsWith("/"))throw new Error(`Invalid ${t}: use <key>[/<key>]* without leading or trailing slash.`);let n=r.split("/");for(let i of n)Yl(i,t);return n.join("/")}function mc(e){let t=/^wikg:\/\/chapter\/(.+)$/u.exec(e.trim());if(t?.[1]!==void 0)return Oi(t[1],"chapter URI")}function Yl(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(Uq.has(e))throw new Error(`Invalid ${t}: ${e} is reserved.`);return e}function An(e){for(let t=0;t<hC;t+=1){let r=Gq($q).toString("hex");try{Yl(r)}catch{continue}if(!e.has(r))return r}throw new Error(`Unable to create a unique chapter key after ${hC} attempts.`)}function wy(e,t){let r=Oi(t).split("/"),n=e,i;for(let a of r){if(i=n.find(o=>o.key===a),i===void 0)return;n=i.children}return i?.serialId}function Sy(e,t=new Set){for(let r of e)r.key!==void 0&&t.add(r.key),Sy(r.children,t);return t}function ky(e,t,r=[]){for(let n of e){if(n.key===void 0)continue;let i=[...r,n.key];if(n.serialId===t)return i.join("/");let a=ky(n.children,t,i);if(a!==void 0)return a}}var Uq,$q,hC,$r=l(()=>{"use strict";Uq=new Set(["tree","source","summary","title","state","chunk","entity","triple","cover","meta","job","help"]),$q=6,hC=100});import{z as Br}from"zod";var Xe,Ey,hc,Zl=l(()=>{"use strict";$r();Xe=1,Ey=Br.object({key:Br.string().min(1).transform(e=>Yl(e)).optional(),title:Br.string().min(1).nullable().optional(),serialId:Br.number().int().nonnegative().optional(),children:Br.lazy(()=>Br.array(Ey))}),hc=Br.object({version:Br.literal(Xe),items:Br.array(Ey)})});import{createHash as Bq}from"crypto";import{posix as Iy}from"path";import{PassThrough as Hq}from"stream";import{open as qq}from"yauzl";function Hr(e){let t=e.replaceAll("\\","/").trim(),r=t.startsWith("/")?t.slice(1):t;return Iy.normalize(r).replace(/^(\.\/)+/,"").replace(/^\/+/,"")}function Kq(e){let[t]=e.split("#",1);return Hr(t??"")}function xy(e){if(e===void 0)return;let t=e.startsWith("#")?e.slice(1):e;return t===""?void 0:t}function fc(e){let[t,r]=e.split("#",2);return{path:Hr(t??""),fragment:xy(r)}}async function Jq(e){return await new Promise((t,r)=>{qq(e,{autoClose:!1,lazyEntries:!0},(n,i)=>{if(n!==null||i===void 0){r(n??new Error(`Cannot open EPUB archive: ${e}`));return}t(i)})})}async function zq(e){return await new Promise((t,r)=>{let n=new Map;e.on("entry",i=>{if(i.fileName.endsWith("/")){e.readEntry();return}n.set(Hr(i.fileName),i),e.readEntry()}),e.once("end",()=>{t(n)}),e.once("error",i=>{r(i)}),e.readEntry()})}function Qq(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 fC(e){return await new Promise((t,r)=>{let n=[];e.on("data",i=>{n.push(Qq(i))}),e.once("end",()=>{t(Buffer.concat(n))}),e.once("error",i=>{r(i)}),e.resume()})}async function Vq(e,t){return await new Promise((r,n)=>{e.openReadStream(t,(i,a)=>{if(i!==null||a===void 0){n(i??new Error(`Cannot open EPUB entry: ${t.fileName}`));return}r(Xq(a))})})}function Xq(e){let t=new Hq;return e.once("error",r=>{t.destroy(r)}),t.once("close",()=>{e.unpipe(t),e.destroyed||e.destroy()}),e.pipe(t),t}var ep,gc=l(()=>{"use strict";ep=class e{#e;#t;#r;#n=!1;constructor(t,r,n){this.#e=t,this.#t=r,this.#r=n}static async open(t){let r=await Jq(t),n=await zq(r);return new e(t,r,n)}close(){return this.#n||(this.#n=!0,this.#t.close()),Promise.resolve()}hasEntry(t){return this.#r.has(Hr(t))}listEntries(){return[...this.#r.keys()]}async openReadStream(t){let r=this.#i(t);return await Vq(this.#t,r)}async readText(t){let r=await this.openReadStream(t);return(await fC(r)).toString("utf8")}async readBuffer(t){let r=await this.openReadStream(t);return await fC(r)}resolveRelativePath(t,r){let n=Kq(r);if(n==="")throw new Error(`Invalid EPUB href: ${r}`);return Hr(Iy.join(Iy.dirname(Hr(t)),n))}createSectionId(t,r){let n=Hr(t);return r===void 0||r===""?n:`${n}#${r}`}createSyntheticSectionId(t,r){let n=Hr(t);return`toc:${Bq("sha1").update(`${n}:${r??""}`).digest("hex").slice(0,10)}`}get path(){return this.#e}#i(t){let r=Hr(t),n=this.#r.get(r);if(n===void 0)throw new Error(`EPUB entry does not exist: ${r}`);return n}}});import{Parser as Yq}from"htmlparser2";async function yC(e,t){let r=new Map;for(let[n,i]of t.entries()){let a=await e.openReadStream(n);a.setEncoding("utf8");let o=await bC(a,i);for(let s of i){let c=o.get(s.id)??"";r.set(s.id,{hasContent:c.trim()!=="",wordsCount:i1(c)})}}return r}function e1(e){let t=new Map;for(let[r,n]of e.entries())for(let i of n)t.set(i.id,{path:r,targets:n});return t}async function t1(e,t){return await bC(e,t)}async function bC(e,t){let r=new Map,n=new Map,i=[...t],a=i.map(()=>""),s=i[0]?.fragment===void 0&&i[0]!==void 0?0:-1,c=0;i.forEach((u,p)=>{n.set(u.id,""),u.fragment!==void 0&&!r.has(u.fragment)&&r.set(u.fragment,p)});let d=new Yq({onopentag(u,p){let m=u.toLowerCase();if(gC.has(m)){c+=1;return}let f=p.id??p["xml:id"];if(f!==void 0){let w=r.get(f);w!==void 0&&w>s&&(s=w)}m==="br"&&vy(a,s,`
|
|
133
|
+
`)},ontext(u){c>0||vy(a,s,u)},onclosetag(u){let p=u.toLowerCase();if(gC.has(p)){c=Math.max(0,c-1);return}Zq.has(p)&&vy(a,s,`
|
|
134
|
+
|
|
135
|
+
`)}},{decodeEntities:!0});for await(let u of e)d.write(n1(u));return d.end(),i.forEach((u,p)=>{n.set(u.id,r1(a[p]??""))}),n}function vy(e,t,r){t<0||t>=e.length||(e[t]+=r)}function r1(e){return e.replace(/\r\n/gu,`
|
|
136
|
+
`).replace(/\n{3,}/gu,`
|
|
137
|
+
|
|
138
|
+
`).trim()}function n1(e){if(typeof e=="string")return e;if(Buffer.isBuffer(e))return e.toString("utf8");throw new Error("Unexpected HTML stream chunk type")}function i1(e){return[...a1().segment(e)].filter(t=>t.isWordLike).length}function a1(){return Cy===void 0&&(Cy=new Intl.Segmenter(void 0,{granularity:"word"})),Cy}var Zq,gC,tp,Cy,wC=l(()=>{"use strict";Zq=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"]),gC=new Set(["script","style"]),tp=class{#e;#t;constructor(t,r){this.#e=t,this.#t=e1(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 t1(n,r)}}});import{SaxesParser as o1}from"saxes";function yc(e){let t=new o1({xmlns:!1}),r=[],n;if(t.on("opentag",i=>{let a={name:i.name,attributes:Object.fromEntries(Object.entries(i.attributes).map(([s,c])=>[s,String(c)])),children:[],text:""},o=r[r.length-1];o===void 0?n=a:o.children.push(a),r.push(a)}),t.on("text",i=>{let a=r[r.length-1];a!==void 0&&(a.text+=i)}),t.on("cdata",i=>{let a=r[r.length-1];a!==void 0&&(a.text+=i)}),t.on("closetag",()=>{r.pop()}),t.write(e).close(),n===void 0)throw new Error("XML document is empty");return SC(n)}function Wt(e,t){return e.children.find(r=>wc(r.name)===t)}function Ln(e,t){return e.children.filter(r=>wc(r.name)===t)}function $e(e,t){return e.attributes[t]}function bc(e){let t=e.text;for(let r of e.children)t+=bc(r);return t}function wc(e){let t=e.indexOf(":");return t===-1?e:e.slice(t+1)}function SC(e){return{name:e.name,attributes:Object.freeze({...e.attributes}),children:Object.freeze(e.children.map(SC)),text:e.text}}var Ty=l(()=>{"use strict"});import{posix as s1}from"path";async function EC(e){let t=await u1(e),r=yc(await e.readText(t)),n=l1($e(r,"version")),i=Wt(r,"metadata"),a=Wt(r,"manifest"),o=Wt(r,"spine"),s=Wt(r,"guide"),c=m1(e,t,a),d=new Map([...c.values()].map(E=>[E.path,E])),u=h1(o,c),p=f1(e,t,s),m=p1(i),f=g1(c),w=y1(c,o),b=b1(c,i);return{version:n,opfPath:t,metadata:{...m,version:Ur,sourceFormat:"epub"},manifest:c,manifestByPath:d,spine:u,navPath:f,ncxPath:w,coverPath:b?.path,coverMediaType:b?.mediaType,guideNonContentPaths:p}}async function u1(e){let t=yc(await e.readText("META-INF/container.xml")),n=Wt(t,"rootfiles")?.children.find(a=>wc(a.name)==="rootfile"),i=n===void 0?void 0:$e(n,"full-path");if(i===void 0||i.trim()==="")throw new Error("EPUB container.xml does not contain an OPF rootfile");return i}function l1(e){return e?.trim().startsWith("3")===!0?3:2}function p1(e){if(e===void 0)return kC();let t=Ni(e,"title"),r=Ni(e,"creator"),n=Ni(e,"language"),i=Ni(e,"identifier"),a=Ni(e,"publisher"),o=Ni(e,"date"),s=Ni(e,"description");return{...kC(),title:t[0]??null,authors:[...r],language:n[0]??null,identifier:i[0]??null,publisher:a[0]??null,publishedAt:o[0]??null,description:s[0]??null}}function kC(){return{version:Ur,sourceFormat:"epub",title:null,authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null}}function Ni(e,t){return e.children.filter(r=>wc(r.name)===t).map(r=>S1(bc(r))).filter(r=>r!==void 0)}function m1(e,t,r){let n=new Map;if(r===void 0)return n;for(let i of Ln(r,"item")){let a=$e(i,"id"),o=$e(i,"href"),s=$e(i,"media-type")??"";a===void 0||o===void 0||n.set(a,{id:a,path:e.resolveRelativePath(t,o),mediaType:s,properties:new Set(($e(i,"properties")??"").split(/\s+/u).map(c=>c.trim().toLowerCase()).filter(c=>c!==""))})}return n}function h1(e,t){return e===void 0?[]:Ln(e,"itemref").map(r=>$e(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 f1(e,t,r){let n=new Set;if(r===void 0)return n;for(let i of Ln(r,"reference")){let a=($e(i,"type")??"").trim().toLowerCase();if(!d1.has(a))continue;let o=$e(i,"href");o===void 0||o.trim()===""||n.add(e.resolveRelativePath(t,fc(o).path))}return n}function g1(e){for(let t of e.values())if(t.properties.has("nav"))return t.path}function y1(e,t){let r=t===void 0?void 0:$e(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 b1(e,t){for(let n of e.values())if(n.properties.has("cover-image")&&n.mediaType.startsWith("image/"))return n;let r=w1(t);if(r!==void 0){let n=e.get(r);if(n?.mediaType.startsWith("image/")===!0)return n}for(let n of c1){let i=e.get(n);if(i?.mediaType.startsWith("image/")===!0)return i}for(let n of e.values())if(n.mediaType.startsWith("image/")&&s1.basename(n.path).toLowerCase().includes("cover"))return n}function w1(e){if(e!==void 0)for(let t of Ln(e,"meta")){if(($e(t,"name")??"").trim().toLowerCase()!=="cover")continue;let n=$e(t,"content");if(n!==void 0&&n.trim()!=="")return n.trim()}}function S1(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var c1,d1,Ry=l(()=>{"use strict";pc();gc();Ty();c1=["cover-image","coverimg","cover-img","cover"],d1=new Set(["cover","titlepage","other.titlepage"])});import{basename as k1,extname as E1,posix as I1}from"path";import{parseDocument as x1}from"htmlparser2";async function xC(e,t){let r=await C1(e,t)??await T1(e,t)??[];if(r.length===0)return IC(t,new Set);let n=TC(r),i=IC(t,n);return i.length===0?r:[...r,...i]}async function C1(e,t){if(t.navPath===void 0)return;let r=x1(await e.readText(t.navPath),{decodeEntities:!0}),n=Py(r,o=>On(o)!=="nav"?!1:(o.attribs?.["epub:type"]??o.attribs?.type??"").split(/\s+/u).map(c=>c.trim().toLowerCase()).includes("toc"));if(n===void 0)return;let i=Py(n,o=>On(o)==="ol");if(i===void 0)return;let a=Sc(i).filter(o=>On(o)==="li").map(o=>vC(e,t.navPath,o)).filter(o=>o!==void 0);return a.length===0?void 0:a}async function T1(e,t){if(t.ncxPath===void 0)return;let r=yc(await e.readText(t.ncxPath)),n=Wt(r,"navMap");if(n===void 0)return;let i=Ln(n,"navPoint").map(a=>CC(e,t.ncxPath,a)).filter(a=>a!==void 0);return i.length===0?void 0:i}function vC(e,t,r){let n=RC(r),i=n===void 0?void 0:AC(PC(n)),a=n!==void 0&&On(n)==="a"?n.attribs?.href:void 0,o=P1(e,t,a),s=Sc(r).filter(c=>On(c)==="ol").flatMap(c=>Sc(c)).filter(c=>On(c)==="li").map(c=>vC(e,t,c)).filter(c=>c!==void 0);if(!(i===void 0&&o.path===void 0&&s.length===0))return{title:i,path:o.path,fragment:o.fragment,children:s}}function CC(e,t,r){let n=Wt(r,"navLabel"),i=n===void 0?void 0:Wt(n,"text"),a=i===void 0?void 0:AC(bc(i)),o=Wt(r,"content"),s=_1(e,t,$e(o??r,"src")),c=Ln(r,"navPoint").map(d=>CC(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 IC(e,t){return e.spine.filter(r=>!R1(e,r.path,t)).filter(r=>!t.has(r.path)).map(r=>({title:A1(r.path),path:r.path,fragment:void 0,children:[]}))}function R1(e,t,r){if(e.guideNonContentPaths.has(t))return!0;let n=I1.basename(t).toLowerCase();return e.navPath===t?!0:v1.has(n)&&!r.has(t)}function TC(e){let t=new Set;for(let r of e){r.path!==void 0&&t.add(r.path);for(let n of TC(r.children))t.add(n)}return t}function P1(e,t,r){if(r===void 0||r.trim()==="")return{path:void 0,fragment:void 0};let{path:n,fragment:i}=fc(r);return{path:n===""?void 0:e.resolveRelativePath(t,n),fragment:i}}function _1(e,t,r){if(r===void 0||r.trim()==="")return{path:void 0,fragment:void 0};let{path:n,fragment:i}=fc(r);return{path:n===""?void 0:e.resolveRelativePath(t,n),fragment:i}}function Py(e,t){if(_C(e)&&t(e))return e;for(let r of e.children??[]){let n=Py(r,t);if(n!==void 0)return n}}function RC(e){for(let t of Sc(e)){let r=On(t);if(r==="a"||r==="span")return t}for(let t of Sc(e)){if(On(t)==="ol")continue;let r=RC(t);if(r!==void 0)return r}}function PC(e){return e.type==="text"?e.data??"":(e.children??[]).map(PC).join("")}function Sc(e){return(e.children??[]).filter(_C)}function _C(e){return e.type==="tag"||e.type==="script"||e.type==="style"}function On(e){return e.name?.toLowerCase()}function A1(e){let t=k1(e,E1(e)).trim();return t===""?void 0:t}function AC(e){let t=e.replace(/\s+/gu," ").trim();return t===""?void 0:t}var v1,LC=l(()=>{"use strict";gc();Ry();Ty();v1=new Set(["toc.xhtml","nav.xhtml","contents.xhtml","tableofcontents.xhtml","cover.xhtml","titlepage.xhtml"])});function L1(e){if(e.hasEntry("META-INF/encryption.xml"))throw new Error("Encrypted EPUB is not supported: found META-INF/encryption.xml.")}async function O1(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 N1(e,t){let r=new Map;return t.map((n,i)=>OC(e,n,[i],r))}function OC(e,t,r,n){let i=xy(t.fragment),a=t.path===void 0?`toc:${r.join(".")}`:e.createSectionId(t.path,i),o=j1(a,n);return{hasContent:t.path!==void 0,id:o,title:t.title,path:t.path,fragment:i,wordsCount:0,children:t.children.map((s,c)=>OC(e,s,[...r,c],n))}}function NC(e,t){return e.map(r=>{let n=t.get(r.id);return{...r,hasContent:n?.hasContent??!1,wordsCount:n?.wordsCount??0,children:NC(r.children,t)}})}function j1(e,t){let r=t.get(e)??0;return t.set(e,r+1),r===0?e:`${e}~${r+1}`}function D1(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 _y,Ay,kc,rp,DC=l(()=>{"use strict";gc();gc();wC();LC();Ry();_y=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)}},Ay=class e{#e;#t;#r;#n;constructor(t,r,n,i){this.#e=t,this.#t=r,this.#r=n,this.#n=i}static async open(t){L1(t);let r=await EC(t),n=await xC(t,r),i=N1(t,n),a=D1(i),o=await yC(t,a),s=NC(i,o),c=await O1(t,r);return new e(r,c,s,new tp(t,a))}readMeta(){return Promise.resolve(this.#e.metadata)}readCover(){return Promise.resolve(this.#t)}readSections(){return Promise.resolve(this.#r.map(t=>new _y(this,t)))}async openSection(t){return await this.#n.openSection(t)}},kc=class{get format(){return"epub"}async openSession(t,r){let n=await ep.open(t);try{let i=await Ay.open(n);return await r(i)}finally{await n.close()}}},rp=new kc});var MC=l(()=>{"use strict";DC()});import{createReadStream as M1}from"fs";import{stat as W1}from"fs/promises";import{basename as F1,extname as G1,resolve as Ny}from"path";function $1(e){let t=F1(e,G1(e)).trim();return t===""?null:t}var U1,Ly,Oy,Ec,jy,Dy,WC=l(()=>{"use strict";pc();U1="root",Ly=class{#e;#t;constructor(t,r=U1){this.#e=Ny(t),this.#t=r}get id(){return this.#t}get hasContent(){return!0}get title(){}get children(){return[]}open(){return Promise.resolve(M1(this.#e,{encoding:"utf8"}))}},Oy=class{#e;#t;#r;constructor(t,r){this.#e=Ny(t),this.#t=r,this.#r=new Ly(this.#e)}readMeta(){return Promise.resolve({version:Ur,sourceFormat:this.#t,title:$1(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])}},Ec=class{#e;constructor(t){this.#e=t}get format(){return this.#e}async openSession(t,r){let n=Ny(t);if(!(await W1(n)).isFile())throw new Error(`Source file is not a regular file: ${n}`);return await r(new Oy(n,this.#e))}},jy=new Ec("txt"),Dy=new Ec("markdown")});var ji=l(()=>{"use strict";pc();MC();WC();Zl()});function My(e){return{children:e.children.map(My),...e.key===void 0?{}:{key:e.key},...e.serialId===void 0?{}:{serialId:e.serialId},title:e.title}}function B1(e,t){if(e.serialId===void 0)throw new Error("Internal error: normalized chapter tree has no id.");let r=e.key??`chapter-${e.serialId}`,n=[...t,r];return{children:e.children.flatMap(i=>np(i,n)),title:ip(e.title)??null,uri:`wikg://chapter/${n.join("/")}`}}function np(e,t=[]){let r=e.key??`chapter-${e.serialId??"group"}`,n=[...t,r];return e.serialId===void 0?e.children.flatMap(i=>np(i,n)):[B1(e,t)]}function ip(e){let t=e?.trim();return t===void 0||t===""?void 0:t}var Wy=l(()=>{"use strict"});async function op(e){let t=await e.readToc(),r=t?.items.map(My)??[];return H1(r),t===void 0?{items:[],version:Xe}:{items:r,version:t.version}}function H1(e){let t=new Set,r=n=>{for(let i of n){if(i.key===void 0)throw new Error("Missing chapter key in TOC. Run a writable chapter operation or `wg maintenance upgrade` before using read-only chapter paths.");if(t.has(i.key))throw new Error(`Duplicate chapter key: ${i.key}.`);t.add(i.key),r(i.children)}};r(e)}function Za(e){let t=new Set,r=!1,n=a=>{for(let o of a){if(o.key!==void 0){if(t.has(o.key))throw new Error(`Duplicate chapter key: ${o.key}.`);t.add(o.key)}n(o.children)}},i=a=>{for(let o of a)o.key===void 0&&(o.key=An(t),t.add(o.key),r=!0),i(o.children)};return n(e),i(e),r}async function Fy(e,t,r,n=[],i=[],a=0){for(let o of t){let s=ip(o.title)??null,c=o.key??`chapter-${o.serialId??"group"}`,d=o.serialId===void 0?[...n,...s===null?[]:[s]]:[...n,s??`Chapter ${o.serialId}`],u=[...i,c];if(o.serialId===r)return await FC(e,o,o.serialId,{chapterPath:u,depth:a,title:s,tocPath:d});let p=await Fy(e,o.children,r,d,u,a+1);if(p!==void 0)return p}}async function ap(e,t,r=[],n=[],i=0){let a=[];for(let o of t){let s=ip(o.title)??null,c=o.key??`chapter-${o.serialId??"group"}`,d=o.serialId===void 0?[...r,...s===null?[]:[s]]:[...r,s??`Chapter ${o.serialId}`],u=[...n,c];if(o.serialId===void 0){a.push(...await ap(e,o.children,d,u,i+1));continue}a.push(await FC(e,o,o.serialId,{chapterPath:u,depth:i,title:s,tocPath:d})),a.push(...await ap(e,o.children,d,u,i+1))}return a}async function FC(e,t,r,n){let[i,a]=await Promise.all([e.serials.getById(r),q1(e,r)]);return{chapterId:r,childCount:t.children.length,depth:n.depth,documentOrder:i?.documentOrder??r,fragmentCount:a.fragmentCount,key:n.chapterPath.at(-1)??`chapter-${r}`,path:n.chapterPath.join("/"),stage:await K1(e,r,a.fragmentCount),title:n.title,tocPath:n.tocPath,uri:sr(n.chapterPath.join("/")),words:a.words}}async function q1(e,t){let r=await e.readDatabase(async o=>await o.queryAll(`
|
|
139
|
+
SELECT words_count
|
|
140
|
+
FROM text_sentence_records
|
|
141
|
+
WHERE kind = 1 AND chapter_id = ?
|
|
142
|
+
ORDER BY sentence_index
|
|
143
|
+
`,[t],s=>Number(s.words_count))),n=0,i=0,a=0;for(let o of r)i>0&&i+o>600&&(n+=1,i=0),i+=o,a+=o;return i>0&&(n+=1),{fragmentCount:n,words:a}}async function K1(e,t,r){return await e.readDatabase(async a=>await a.queryOne(`
|
|
144
|
+
SELECT COUNT(*) AS count
|
|
145
|
+
FROM text_sentence_records
|
|
146
|
+
WHERE kind = 2 AND chapter_id = ?
|
|
147
|
+
`,[t],o=>Number(o.count))??0)>0?"summarized":(await e.serials.getById(t))?.topologyReady===!0?"graphed":r>0?"sourced":"planned"}var Ic=l(()=>{"use strict";ji();$r();Wy()});async function GC(e){await J1(e),await V1(e),await Q1(e),await Y1(e),await Z1(e),await z1(e),await X1(e)}async function J1(e){(await Nn(e,"serials")).has("document_order")||await e.transaction(async()=>{(await Nn(e,"serials")).has("document_order")||await e.run(`
|
|
148
|
+
ALTER TABLE serials
|
|
149
|
+
ADD COLUMN document_order INTEGER NOT NULL DEFAULT 0
|
|
150
|
+
`)})}async function z1(e){await e.run(`
|
|
151
|
+
CREATE INDEX IF NOT EXISTS idx_serials_document_order
|
|
152
|
+
ON serials(document_order, id)
|
|
153
|
+
`)}async function Q1(e){(await Nn(e,"serial_states")).has("revision")||await e.transaction(async()=>{(await Nn(e,"serial_states")).has("revision")||await e.run(`
|
|
154
|
+
ALTER TABLE serial_states
|
|
155
|
+
ADD COLUMN revision INTEGER NOT NULL DEFAULT 0
|
|
156
|
+
`)})}async function V1(e){await e.run(`
|
|
157
|
+
CREATE TABLE IF NOT EXISTS graph_build_parameters (
|
|
158
|
+
hash TEXT PRIMARY KEY,
|
|
159
|
+
prompt TEXT NOT NULL,
|
|
160
|
+
language TEXT,
|
|
161
|
+
created_at TEXT NOT NULL
|
|
162
|
+
)
|
|
163
|
+
`)}async function X1(e){await e.run(`
|
|
164
|
+
CREATE INDEX IF NOT EXISTS idx_serial_states_topology_parameter_hash
|
|
165
|
+
ON serial_states(topology_parameter_hash)
|
|
166
|
+
`),await e.run(`
|
|
167
|
+
CREATE INDEX IF NOT EXISTS idx_serial_states_knowledge_graph_parameter_hash
|
|
168
|
+
ON serial_states(knowledge_graph_parameter_hash)
|
|
169
|
+
`)}async function Y1(e){(await Nn(e,"serial_states")).has("knowledge_graph_ready")||await e.transaction(async()=>{(await Nn(e,"serial_states")).has("knowledge_graph_ready")||await e.run(`
|
|
170
|
+
ALTER TABLE serial_states
|
|
171
|
+
ADD COLUMN knowledge_graph_ready INTEGER NOT NULL DEFAULT 0
|
|
172
|
+
`)})}async function Z1(e){let t=await Nn(e,"serial_states"),r=[["topology_parameter_hash","TEXT"],["knowledge_graph_parameter_hash","TEXT"]],n=[];for(let i of r)t.has(i[0])||n.push(i);n.length!==0&&await e.transaction(async()=>{let i=await Nn(e,"serial_states");for(let[a,o]of n)i.has(a)||await e.run(`
|
|
173
|
+
ALTER TABLE serial_states
|
|
174
|
+
ADD COLUMN ${a} ${o}
|
|
175
|
+
`)})}async function Nn(e,t){let r=await e.queryAll(`PRAGMA table_info(${t})`,void 0,n=>String(n.name));return new Set(r)}var Gy,Uy,sp,xc=l(()=>{"use strict";Gy=`
|
|
176
|
+
CREATE TABLE IF NOT EXISTS serials (
|
|
177
|
+
id INTEGER PRIMARY KEY,
|
|
178
|
+
document_order INTEGER NOT NULL DEFAULT 0
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
CREATE TABLE IF NOT EXISTS serial_states (
|
|
182
|
+
serial_id INTEGER PRIMARY KEY,
|
|
183
|
+
revision INTEGER NOT NULL DEFAULT 0,
|
|
184
|
+
topology_ready INTEGER NOT NULL DEFAULT 0,
|
|
185
|
+
topology_parameter_hash TEXT,
|
|
186
|
+
knowledge_graph_ready INTEGER NOT NULL DEFAULT 0,
|
|
187
|
+
knowledge_graph_parameter_hash TEXT,
|
|
188
|
+
FOREIGN KEY (serial_id) REFERENCES serials(id)
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
CREATE TABLE IF NOT EXISTS archive_revisions (
|
|
192
|
+
key TEXT PRIMARY KEY,
|
|
193
|
+
value INTEGER NOT NULL
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
CREATE TABLE IF NOT EXISTS archive_index_settings (
|
|
197
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
198
|
+
fts_embedded INTEGER NOT NULL DEFAULT 0
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
CREATE TABLE IF NOT EXISTS graph_build_parameters (
|
|
202
|
+
hash TEXT PRIMARY KEY,
|
|
203
|
+
prompt TEXT NOT NULL,
|
|
204
|
+
language TEXT,
|
|
205
|
+
created_at TEXT NOT NULL
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
CREATE TABLE IF NOT EXISTS chunks (
|
|
209
|
+
id INTEGER PRIMARY KEY,
|
|
210
|
+
generation INTEGER NOT NULL,
|
|
211
|
+
serial_id INTEGER NOT NULL,
|
|
212
|
+
sentence_index INTEGER NOT NULL,
|
|
213
|
+
label TEXT NOT NULL,
|
|
214
|
+
content TEXT NOT NULL,
|
|
215
|
+
retention TEXT,
|
|
216
|
+
importance TEXT,
|
|
217
|
+
wordsCount INTEGER NOT NULL DEFAULT 0,
|
|
218
|
+
weight REAL NOT NULL DEFAULT 0.0
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
CREATE INDEX IF NOT EXISTS idx_chunks_sentence
|
|
222
|
+
ON chunks(serial_id, sentence_index);
|
|
223
|
+
|
|
224
|
+
CREATE INDEX IF NOT EXISTS idx_chunks_serial_id
|
|
225
|
+
ON chunks(serial_id, id);
|
|
226
|
+
|
|
227
|
+
CREATE TABLE IF NOT EXISTS chunk_sentences (
|
|
228
|
+
chunk_id INTEGER NOT NULL,
|
|
229
|
+
serial_id INTEGER NOT NULL,
|
|
230
|
+
sentence_index INTEGER NOT NULL,
|
|
231
|
+
FOREIGN KEY (chunk_id) REFERENCES chunks(id),
|
|
232
|
+
PRIMARY KEY (chunk_id, serial_id, sentence_index)
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
CREATE TABLE IF NOT EXISTS reading_edges (
|
|
236
|
+
from_id INTEGER NOT NULL,
|
|
237
|
+
to_id INTEGER NOT NULL,
|
|
238
|
+
strength TEXT,
|
|
239
|
+
weight REAL NOT NULL DEFAULT 0.1,
|
|
240
|
+
PRIMARY KEY (from_id, to_id),
|
|
241
|
+
FOREIGN KEY (from_id) REFERENCES chunks(id),
|
|
242
|
+
FOREIGN KEY (to_id) REFERENCES chunks(id)
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
CREATE TABLE IF NOT EXISTS snakes (
|
|
246
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
247
|
+
serial_id INTEGER NOT NULL,
|
|
248
|
+
group_id INTEGER NOT NULL,
|
|
249
|
+
local_snake_id INTEGER NOT NULL,
|
|
250
|
+
size INTEGER NOT NULL,
|
|
251
|
+
first_label TEXT NOT NULL,
|
|
252
|
+
last_label TEXT NOT NULL,
|
|
253
|
+
wordsCount INTEGER NOT NULL DEFAULT 0,
|
|
254
|
+
weight REAL NOT NULL DEFAULT 0.0,
|
|
255
|
+
UNIQUE(serial_id, group_id, local_snake_id)
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
CREATE TABLE IF NOT EXISTS snake_chunks (
|
|
259
|
+
snake_id INTEGER NOT NULL,
|
|
260
|
+
chunk_id INTEGER NOT NULL,
|
|
261
|
+
position INTEGER NOT NULL,
|
|
262
|
+
FOREIGN KEY (snake_id) REFERENCES snakes(id),
|
|
263
|
+
FOREIGN KEY (chunk_id) REFERENCES chunks(id),
|
|
264
|
+
PRIMARY KEY (snake_id, chunk_id)
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
CREATE TABLE IF NOT EXISTS snake_edges (
|
|
268
|
+
from_snake_id INTEGER NOT NULL,
|
|
269
|
+
to_snake_id INTEGER NOT NULL,
|
|
270
|
+
weight REAL NOT NULL DEFAULT 0.1,
|
|
271
|
+
PRIMARY KEY (from_snake_id, to_snake_id),
|
|
272
|
+
FOREIGN KEY (from_snake_id) REFERENCES snakes(id),
|
|
273
|
+
FOREIGN KEY (to_snake_id) REFERENCES snakes(id)
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
CREATE TABLE IF NOT EXISTS sentence_groups (
|
|
277
|
+
serial_id INTEGER NOT NULL,
|
|
278
|
+
group_id INTEGER NOT NULL,
|
|
279
|
+
start_sentence_index INTEGER NOT NULL,
|
|
280
|
+
end_sentence_index INTEGER NOT NULL,
|
|
281
|
+
PRIMARY KEY (serial_id, group_id, start_sentence_index, end_sentence_index)
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
CREATE TABLE IF NOT EXISTS mentions (
|
|
285
|
+
id TEXT PRIMARY KEY,
|
|
286
|
+
chapter_id INTEGER NOT NULL,
|
|
287
|
+
sentence_index INTEGER,
|
|
288
|
+
range_start INTEGER NOT NULL,
|
|
289
|
+
range_end INTEGER NOT NULL,
|
|
290
|
+
surface TEXT NOT NULL,
|
|
291
|
+
qid TEXT NOT NULL,
|
|
292
|
+
confidence REAL,
|
|
293
|
+
note TEXT,
|
|
294
|
+
FOREIGN KEY (chapter_id) REFERENCES serials(id)
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_chapter
|
|
298
|
+
ON mentions(chapter_id);
|
|
299
|
+
|
|
300
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_chapter_position
|
|
301
|
+
ON mentions(chapter_id, sentence_index, range_start, range_end, id);
|
|
302
|
+
|
|
303
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_chapter_qid
|
|
304
|
+
ON mentions(chapter_id, qid);
|
|
305
|
+
|
|
306
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_qid
|
|
307
|
+
ON mentions(qid);
|
|
308
|
+
|
|
309
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_qid_position
|
|
310
|
+
ON mentions(qid, chapter_id, sentence_index, range_start, range_end, id);
|
|
311
|
+
|
|
312
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_surface
|
|
313
|
+
ON mentions(surface);
|
|
314
|
+
|
|
315
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_surface_position
|
|
316
|
+
ON mentions(surface, chapter_id, sentence_index, range_start, range_end, id);
|
|
317
|
+
|
|
318
|
+
CREATE INDEX IF NOT EXISTS idx_mentions_sentence
|
|
319
|
+
ON mentions(chapter_id, sentence_index);
|
|
320
|
+
|
|
321
|
+
CREATE TABLE IF NOT EXISTS mention_links (
|
|
322
|
+
id TEXT PRIMARY KEY,
|
|
323
|
+
source_mention_id TEXT NOT NULL,
|
|
324
|
+
target_mention_id TEXT NOT NULL,
|
|
325
|
+
predicate TEXT NOT NULL,
|
|
326
|
+
confidence REAL,
|
|
327
|
+
note TEXT,
|
|
328
|
+
FOREIGN KEY (source_mention_id) REFERENCES mentions(id),
|
|
329
|
+
FOREIGN KEY (target_mention_id) REFERENCES mentions(id)
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
CREATE TABLE IF NOT EXISTS mention_link_evidence_sentences (
|
|
333
|
+
link_id TEXT NOT NULL,
|
|
334
|
+
chapter_id INTEGER NOT NULL,
|
|
335
|
+
sentence_index INTEGER NOT NULL,
|
|
336
|
+
FOREIGN KEY (link_id) REFERENCES mention_links(id),
|
|
337
|
+
PRIMARY KEY (link_id, chapter_id, sentence_index)
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
CREATE INDEX IF NOT EXISTS idx_mention_link_evidence_sentences_sentence
|
|
341
|
+
ON mention_link_evidence_sentences(chapter_id, sentence_index);
|
|
342
|
+
|
|
343
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_source
|
|
344
|
+
ON mention_links(source_mention_id);
|
|
345
|
+
|
|
346
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_target
|
|
347
|
+
ON mention_links(target_mention_id);
|
|
348
|
+
|
|
349
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_predicate
|
|
350
|
+
ON mention_links(predicate);
|
|
351
|
+
|
|
352
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_predicate_source_target
|
|
353
|
+
ON mention_links(predicate, source_mention_id, target_mention_id);
|
|
354
|
+
|
|
355
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_predicate_target_source
|
|
356
|
+
ON mention_links(predicate, target_mention_id, source_mention_id);
|
|
357
|
+
|
|
358
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_source_predicate_target
|
|
359
|
+
ON mention_links(source_mention_id, predicate, target_mention_id);
|
|
360
|
+
|
|
361
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_target_predicate_source
|
|
362
|
+
ON mention_links(target_mention_id, predicate, source_mention_id);
|
|
363
|
+
|
|
364
|
+
CREATE INDEX IF NOT EXISTS idx_mention_links_source_target_predicate
|
|
365
|
+
ON mention_links(source_mention_id, target_mention_id, predicate);
|
|
366
|
+
|
|
367
|
+
CREATE INDEX IF NOT EXISTS idx_reading_edges_target
|
|
368
|
+
ON reading_edges(to_id, from_id);
|
|
369
|
+
|
|
370
|
+
CREATE INDEX IF NOT EXISTS idx_snake_edges_target
|
|
371
|
+
ON snake_edges(to_snake_id, from_snake_id);
|
|
372
|
+
|
|
373
|
+
CREATE VIEW IF NOT EXISTS chapter_entities AS
|
|
374
|
+
SELECT
|
|
375
|
+
chapter_id,
|
|
376
|
+
qid,
|
|
377
|
+
COUNT(*) AS mention_count
|
|
378
|
+
FROM mentions
|
|
379
|
+
GROUP BY chapter_id, qid;
|
|
380
|
+
|
|
381
|
+
CREATE VIEW IF NOT EXISTS book_entities AS
|
|
382
|
+
SELECT
|
|
383
|
+
qid,
|
|
384
|
+
SUM(mention_count) AS mention_count
|
|
385
|
+
FROM chapter_entities
|
|
386
|
+
GROUP BY qid;
|
|
387
|
+
|
|
388
|
+
CREATE VIEW IF NOT EXISTS chapter_entity_relations AS
|
|
389
|
+
SELECT
|
|
390
|
+
source_mentions.chapter_id AS chapter_id,
|
|
391
|
+
source_mentions.qid AS subject_qid,
|
|
392
|
+
mention_links.predicate AS predicate,
|
|
393
|
+
target_mentions.qid AS object_qid,
|
|
394
|
+
COUNT(*) AS evidence_count
|
|
395
|
+
FROM mention_links
|
|
396
|
+
JOIN mentions AS source_mentions
|
|
397
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
398
|
+
JOIN mentions AS target_mentions
|
|
399
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
400
|
+
WHERE source_mentions.chapter_id = target_mentions.chapter_id
|
|
401
|
+
GROUP BY
|
|
402
|
+
source_mentions.chapter_id,
|
|
403
|
+
source_mentions.qid,
|
|
404
|
+
mention_links.predicate,
|
|
405
|
+
target_mentions.qid;
|
|
406
|
+
|
|
407
|
+
CREATE VIEW IF NOT EXISTS book_entity_relations AS
|
|
408
|
+
SELECT
|
|
409
|
+
subject_qid,
|
|
410
|
+
predicate,
|
|
411
|
+
object_qid,
|
|
412
|
+
SUM(evidence_count) AS evidence_count
|
|
413
|
+
FROM chapter_entity_relations
|
|
414
|
+
GROUP BY subject_qid, predicate, object_qid;
|
|
415
|
+
|
|
416
|
+
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
417
|
+
id INTEGER PRIMARY KEY,
|
|
418
|
+
kind INTEGER NOT NULL,
|
|
419
|
+
chapter_id INTEGER NOT NULL,
|
|
420
|
+
sentence_index INTEGER NOT NULL,
|
|
421
|
+
words_count INTEGER NOT NULL DEFAULT 0,
|
|
422
|
+
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
423
|
+
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
424
|
+
UNIQUE(kind, chapter_id, sentence_index)
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
428
|
+
ON text_sentence_records(kind, chapter_id, sentence_index);
|
|
429
|
+
|
|
430
|
+
CREATE TABLE IF NOT EXISTS object_metadata (
|
|
431
|
+
id INTEGER PRIMARY KEY,
|
|
432
|
+
object_kind INTEGER NOT NULL,
|
|
433
|
+
object_path TEXT NOT NULL,
|
|
434
|
+
key TEXT NOT NULL,
|
|
435
|
+
value_json TEXT NOT NULL,
|
|
436
|
+
updated_at TEXT NOT NULL,
|
|
437
|
+
chapter_id INTEGER,
|
|
438
|
+
chunk_id INTEGER,
|
|
439
|
+
entity_qid TEXT,
|
|
440
|
+
triple_subject_qid TEXT,
|
|
441
|
+
triple_predicate TEXT,
|
|
442
|
+
triple_object_qid TEXT,
|
|
443
|
+
UNIQUE(object_path, key)
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_object
|
|
447
|
+
ON object_metadata(object_path);
|
|
448
|
+
|
|
449
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_chapter
|
|
450
|
+
ON object_metadata(chapter_id);
|
|
451
|
+
|
|
452
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_chunk
|
|
453
|
+
ON object_metadata(chunk_id);
|
|
454
|
+
|
|
455
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_entity
|
|
456
|
+
ON object_metadata(entity_qid);
|
|
457
|
+
|
|
458
|
+
CREATE INDEX IF NOT EXISTS idx_object_metadata_triple
|
|
459
|
+
ON object_metadata(
|
|
460
|
+
triple_subject_qid,
|
|
461
|
+
triple_predicate,
|
|
462
|
+
triple_object_qid
|
|
463
|
+
);
|
|
464
|
+
`,Uy=`
|
|
465
|
+
id INTEGER PRIMARY KEY,
|
|
466
|
+
archive_id INTEGER NOT NULL,
|
|
467
|
+
kind INTEGER NOT NULL,
|
|
468
|
+
chapter_id INTEGER NOT NULL,
|
|
469
|
+
sentence_index INTEGER NOT NULL,
|
|
470
|
+
words_count INTEGER NOT NULL DEFAULT 0,
|
|
471
|
+
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
472
|
+
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
473
|
+
UNIQUE(archive_id, kind, chapter_id, sentence_index)
|
|
474
|
+
`,sp=`
|
|
475
|
+
CREATE TABLE IF NOT EXISTS search_index_state (
|
|
476
|
+
key TEXT PRIMARY KEY,
|
|
477
|
+
value TEXT NOT NULL
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
481
|
+
${Uy}
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
485
|
+
ON text_sentence_records(archive_id, kind, chapter_id, sentence_index);
|
|
486
|
+
|
|
487
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS text_sentence_fts USING fts5(
|
|
488
|
+
tier1,
|
|
489
|
+
tier2,
|
|
490
|
+
tier3,
|
|
491
|
+
tokenize='ascii'
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
CREATE TABLE IF NOT EXISTS search_object_properties_records (
|
|
495
|
+
id INTEGER PRIMARY KEY,
|
|
496
|
+
archive_id INTEGER NOT NULL,
|
|
497
|
+
owner_kind INTEGER NOT NULL,
|
|
498
|
+
owner_id TEXT NOT NULL,
|
|
499
|
+
property_kind INTEGER NOT NULL,
|
|
500
|
+
chapter_id INTEGER
|
|
501
|
+
);
|
|
502
|
+
|
|
503
|
+
CREATE INDEX IF NOT EXISTS idx_search_object_properties_records_owner
|
|
504
|
+
ON search_object_properties_records(archive_id, owner_kind, owner_id);
|
|
505
|
+
|
|
506
|
+
CREATE INDEX IF NOT EXISTS idx_search_object_properties_records_chapter
|
|
507
|
+
ON search_object_properties_records(archive_id, chapter_id, owner_kind, owner_id);
|
|
508
|
+
|
|
509
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS search_object_properties_fts USING fts5(
|
|
510
|
+
tier1,
|
|
511
|
+
tier2,
|
|
512
|
+
tier3,
|
|
513
|
+
tokenize='ascii'
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
CREATE TABLE IF NOT EXISTS index_dirty_chapters (
|
|
517
|
+
archive_id INTEGER NOT NULL,
|
|
518
|
+
chapter_id INTEGER NOT NULL,
|
|
519
|
+
updated_at INTEGER NOT NULL,
|
|
520
|
+
PRIMARY KEY (archive_id, chapter_id)
|
|
521
|
+
);
|
|
522
|
+
`});function $C(e,t){return e-t}function BC(e){return typeof e=="object"&&e!==null&&"code"in e&&typeof e.code=="string"&&e.code.startsWith("SQLITE_CONSTRAINT")}function HC(e,t){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}function cp(e){let t=Te(e,"strength");return{fromId:y(e,"from_id"),toId:y(e,"to_id"),weight:y(e,"weight"),...t===void 0?{}:{strength:t}}}function Di(e){let t=$y(e,"sentence_index"),r=$y(e,"confidence"),n=Te(e,"note");return{chapterId:y(e,"chapter_id"),...r===void 0?{}:{confidence:r},id:k(e,"id"),...n===void 0?{}:{note:n},qid:k(e,"qid"),rangeEnd:y(e,"range_end"),rangeStart:y(e,"range_start"),...t===void 0?{}:{sentenceIndex:t},surface:k(e,"surface")}}function qC(e){let t=new Set,r=[];for(let n of e)t.has(n.id)||(t.add(n.id),r.push(n));return r}function vc(e){let t=$y(e,"confidence"),r=Te(e,"note");return{...t===void 0?{}:{confidence:t},evidenceSentenceIds:[],id:k(e,"id"),...r===void 0?{}:{note:r},predicate:k(e,"predicate"),sourceMentionId:k(e,"source_mention_id"),targetMentionId:k(e,"target_mention_id")}}function KC(e){try{return JSON.parse(e)}catch(t){throw new Error(`Invalid object metadata JSON: ${eK(t)}`)}}function $y(e,t){let r=e[t];if(r!=null){if(typeof r!="number")throw new TypeError(`Expected ${t} to be a number`);return r}}function eK(e){return e instanceof Error?e.message:String(e)}function JC(e){return e.replace(/[\\%_]/gu,t=>`\\${t}`)}function zC(e){return e!==void 0&&sy(e)?e:void 0}function QC(e){return e!==void 0&&ay(e)?e:void 0}var UC,Mi=l(()=>{"use strict";M();Gr();UC=900});var eo,VC=l(()=>{"use strict";M();Mi();eo=class{#e;constructor(t){this.#e=t}async countAll(){return await this.#e.queryOne(`
|
|
523
|
+
SELECT COUNT(*) AS count
|
|
524
|
+
FROM chunks
|
|
525
|
+
`,void 0,t=>y(t,"count"))??0}async create(t){return await this.#e.transaction(async()=>{await this.#e.run(`
|
|
526
|
+
INSERT INTO chunks (
|
|
527
|
+
generation,
|
|
528
|
+
serial_id,
|
|
529
|
+
sentence_index,
|
|
530
|
+
label,
|
|
531
|
+
content,
|
|
532
|
+
retention,
|
|
533
|
+
importance,
|
|
534
|
+
wordsCount,
|
|
535
|
+
weight
|
|
536
|
+
)
|
|
537
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
538
|
+
`,[t.generation,t.sentenceId[0],t.sentenceId[1],t.label,t.content,t.retention??null,t.importance??null,t.wordsCount,t.weight]);let r=await this.#e.getLastInsertRowId(),n={...t,id:r};return await this.#r(n),n})}async save(t){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
539
|
+
INSERT OR REPLACE INTO chunks (
|
|
540
|
+
id,
|
|
541
|
+
generation,
|
|
542
|
+
serial_id,
|
|
543
|
+
sentence_index,
|
|
544
|
+
label,
|
|
545
|
+
content,
|
|
546
|
+
retention,
|
|
547
|
+
importance,
|
|
548
|
+
wordsCount,
|
|
549
|
+
weight
|
|
550
|
+
)
|
|
551
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
552
|
+
`,[t.id,t.generation,t.sentenceId[0],t.sentenceId[1],t.label,t.content,t.retention??null,t.importance??null,t.wordsCount,t.weight]),await this.#r(t)})}async getById(t){let r=await this.#e.queryOne(`
|
|
553
|
+
SELECT
|
|
554
|
+
id,
|
|
555
|
+
generation,
|
|
556
|
+
serial_id,
|
|
557
|
+
sentence_index,
|
|
558
|
+
label,
|
|
559
|
+
content,
|
|
560
|
+
retention,
|
|
561
|
+
importance,
|
|
562
|
+
wordsCount,
|
|
563
|
+
weight
|
|
564
|
+
FROM chunks
|
|
565
|
+
WHERE id = ?
|
|
566
|
+
`,[t],n=>n);if(r!==void 0)return await this.#n(r)}async listAll(){let t=await this.#e.queryAll(`
|
|
567
|
+
SELECT
|
|
568
|
+
id,
|
|
569
|
+
generation,
|
|
570
|
+
serial_id,
|
|
571
|
+
sentence_index,
|
|
572
|
+
label,
|
|
573
|
+
content,
|
|
574
|
+
retention,
|
|
575
|
+
importance,
|
|
576
|
+
wordsCount,
|
|
577
|
+
weight
|
|
578
|
+
FROM chunks
|
|
579
|
+
ORDER BY id
|
|
580
|
+
`,void 0,r=>r);return await Promise.all(t.map(async r=>await this.#n(r)))}async listBySentenceStartIndexes(t,r){if(r.length===0)return[];let n=await Promise.all(r.map(async i=>await this.listBySentenceRange(t,i,i)));return qC(n.flat())}async listBySentenceRange(t,r,n){let i=await this.#e.queryAll(`
|
|
581
|
+
SELECT
|
|
582
|
+
id,
|
|
583
|
+
generation,
|
|
584
|
+
serial_id,
|
|
585
|
+
sentence_index,
|
|
586
|
+
label,
|
|
587
|
+
content,
|
|
588
|
+
retention,
|
|
589
|
+
importance,
|
|
590
|
+
wordsCount,
|
|
591
|
+
weight
|
|
592
|
+
FROM chunks
|
|
593
|
+
WHERE serial_id = ?
|
|
594
|
+
AND sentence_index BETWEEN ? AND ?
|
|
595
|
+
ORDER BY id
|
|
596
|
+
`,[t,r,n],a=>a);return await Promise.all(i.map(async a=>await this.#n(a)))}async listBySerial(t){let r=await this.#e.queryAll(`
|
|
597
|
+
SELECT
|
|
598
|
+
id,
|
|
599
|
+
generation,
|
|
600
|
+
serial_id,
|
|
601
|
+
sentence_index,
|
|
602
|
+
label,
|
|
603
|
+
content,
|
|
604
|
+
retention,
|
|
605
|
+
importance,
|
|
606
|
+
wordsCount,
|
|
607
|
+
weight
|
|
608
|
+
FROM chunks
|
|
609
|
+
WHERE serial_id = ?
|
|
610
|
+
ORDER BY id
|
|
611
|
+
`,[t],n=>n);return await Promise.all(r.map(async n=>await this.#n(n)))}async getMaxId(){return await this.#e.queryOne(`
|
|
612
|
+
SELECT MAX(id) AS id
|
|
613
|
+
FROM chunks
|
|
614
|
+
`,void 0,t=>{let r=t.id;return typeof r=="number"?r:0})??0}async listFragmentPairs(){return await this.#e.queryAll(`
|
|
615
|
+
SELECT DISTINCT serial_id, sentence_index
|
|
616
|
+
FROM chunks
|
|
617
|
+
ORDER BY serial_id, sentence_index
|
|
618
|
+
`,void 0,t=>[y(t,"serial_id"),y(t,"sentence_index")])}async#t(t){return await this.#e.queryAll(`
|
|
619
|
+
SELECT serial_id, sentence_index
|
|
620
|
+
FROM chunk_sentences
|
|
621
|
+
WHERE chunk_id = ?
|
|
622
|
+
ORDER BY serial_id, sentence_index
|
|
623
|
+
`,[t],r=>[y(r,"serial_id"),y(r,"sentence_index")])}async#r(t){await this.#e.run(`
|
|
624
|
+
DELETE FROM chunk_sentences
|
|
625
|
+
WHERE chunk_id = ?
|
|
626
|
+
`,[t.id]);for(let r of t.sentenceIds)await this.#e.run(`
|
|
627
|
+
INSERT INTO chunk_sentences (
|
|
628
|
+
chunk_id,
|
|
629
|
+
serial_id,
|
|
630
|
+
sentence_index
|
|
631
|
+
)
|
|
632
|
+
VALUES (?, ?, ?)
|
|
633
|
+
`,[t.id,r[0],r[1]])}async#n(t){let r=y(t,"id"),n=zC(Te(t,"importance")),i=QC(Te(t,"retention"));return{content:k(t,"content"),generation:y(t,"generation"),id:r,label:k(t,"label"),sentenceId:[y(t,"serial_id"),y(t,"sentence_index")],sentenceIds:await this.#t(r),wordsCount:y(t,"wordsCount"),weight:y(t,"weight"),...n===void 0?{}:{importance:n},...i===void 0?{}:{retention:i}}}}});var to,XC=l(()=>{"use strict";M();to=class{#e;constructor(t){this.#e=t}async save(t){await this.#e.run(`
|
|
634
|
+
INSERT OR REPLACE INTO sentence_groups (
|
|
635
|
+
serial_id,
|
|
636
|
+
group_id,
|
|
637
|
+
start_sentence_index,
|
|
638
|
+
end_sentence_index
|
|
639
|
+
)
|
|
640
|
+
VALUES (?, ?, ?, ?)
|
|
641
|
+
`,[t.serialId,t.groupId,t.startSentenceIndex,t.endSentenceIndex])}async saveMany(t){await this.#e.transaction(async()=>{for(let r of t)await this.save(r)})}async listBySerial(t){return await this.#e.queryAll(`
|
|
642
|
+
SELECT serial_id, group_id, start_sentence_index, end_sentence_index
|
|
643
|
+
FROM sentence_groups
|
|
644
|
+
WHERE serial_id = ?
|
|
645
|
+
ORDER BY group_id, start_sentence_index
|
|
646
|
+
`,[t],r=>({serialId:y(r,"serial_id"),groupId:y(r,"group_id"),startSentenceIndex:y(r,"start_sentence_index"),endSentenceIndex:y(r,"end_sentence_index")}))}async listSerialIds(){return await this.#e.queryAll(`
|
|
647
|
+
SELECT DISTINCT serial_id
|
|
648
|
+
FROM sentence_groups
|
|
649
|
+
ORDER BY serial_id
|
|
650
|
+
`,void 0,t=>y(t,"serial_id"))}async listGroupIdsForSerial(t){return await this.#e.queryAll(`
|
|
651
|
+
SELECT DISTINCT group_id
|
|
652
|
+
FROM sentence_groups
|
|
653
|
+
WHERE serial_id = ?
|
|
654
|
+
ORDER BY group_id
|
|
655
|
+
`,[t],r=>y(r,"group_id"))}}});var ro,YC=l(()=>{"use strict";M();Dl();ro=class{#e;constructor(t){this.#e=t}async save(t){let r=Zs({language:t.language??null,prompt:t.prompt}),n=new Date().toISOString();return await this.#e.run(`
|
|
656
|
+
INSERT OR IGNORE INTO graph_build_parameters (
|
|
657
|
+
hash, prompt, language, created_at
|
|
658
|
+
)
|
|
659
|
+
VALUES (?, ?, ?, ?)
|
|
660
|
+
`,[r,t.prompt,t.language??null,n]),await this.getByHash(r)}async getByHash(t){return await this.#e.queryOne(`
|
|
661
|
+
SELECT hash, prompt, language, created_at
|
|
662
|
+
FROM graph_build_parameters
|
|
663
|
+
WHERE hash = ?
|
|
664
|
+
`,[t],r=>{let n=Te(r,"language");return{createdAt:k(r,"created_at"),hash:k(r,"hash"),...n===void 0?{}:{language:n},prompt:k(r,"prompt")}})}async deleteUnreferenced(){await this.#e.run(`
|
|
665
|
+
DELETE FROM graph_build_parameters
|
|
666
|
+
WHERE hash NOT IN (
|
|
667
|
+
SELECT topology_parameter_hash
|
|
668
|
+
FROM serial_states
|
|
669
|
+
WHERE topology_parameter_hash IS NOT NULL
|
|
670
|
+
UNION
|
|
671
|
+
SELECT knowledge_graph_parameter_hash
|
|
672
|
+
FROM serial_states
|
|
673
|
+
WHERE knowledge_graph_parameter_hash IS NOT NULL
|
|
674
|
+
)
|
|
675
|
+
`)}}});var no,ZC=l(()=>{"use strict";M();Mi();no=class{#e;constructor(t){this.#e=t}async save(t){await this.#e.transaction(async()=>{await this.#t(t)})}async#t(t){await this.#e.run(`
|
|
676
|
+
INSERT OR REPLACE INTO mention_links (
|
|
677
|
+
id,
|
|
678
|
+
source_mention_id,
|
|
679
|
+
target_mention_id,
|
|
680
|
+
predicate,
|
|
681
|
+
confidence,
|
|
682
|
+
note
|
|
683
|
+
)
|
|
684
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
685
|
+
`,[t.id,t.sourceMentionId,t.targetMentionId,t.predicate,t.confidence??null,t.note??null]),await this.#e.run(`
|
|
686
|
+
DELETE FROM mention_link_evidence_sentences
|
|
687
|
+
WHERE link_id = ?
|
|
688
|
+
`,[t.id]);for(let[r,n]of t.evidenceSentenceIds)await this.#e.run(`
|
|
689
|
+
INSERT INTO mention_link_evidence_sentences (
|
|
690
|
+
link_id,
|
|
691
|
+
chapter_id,
|
|
692
|
+
sentence_index
|
|
693
|
+
)
|
|
694
|
+
VALUES (?, ?, ?)
|
|
695
|
+
`,[t.id,r,n])}async saveMany(t){await this.#e.transaction(async()=>{for(let r of t)await this.#t(r)})}async getById(t){let r=await this.#e.queryOne(`
|
|
696
|
+
SELECT
|
|
697
|
+
id,
|
|
698
|
+
source_mention_id,
|
|
699
|
+
target_mention_id,
|
|
700
|
+
predicate,
|
|
701
|
+
confidence,
|
|
702
|
+
note
|
|
703
|
+
FROM mention_links
|
|
704
|
+
WHERE id = ?
|
|
705
|
+
`,[t],vc);return r===void 0?void 0:await this.#n(r)}async listAll(){let t=await this.#e.queryAll(`
|
|
706
|
+
SELECT
|
|
707
|
+
id,
|
|
708
|
+
source_mention_id,
|
|
709
|
+
target_mention_id,
|
|
710
|
+
predicate,
|
|
711
|
+
confidence,
|
|
712
|
+
note
|
|
713
|
+
FROM mention_links
|
|
714
|
+
ORDER BY id
|
|
715
|
+
`,[],vc);return await this.#r(t)}async countByTriple(t){return await this.#e.queryOne(`
|
|
716
|
+
SELECT count(*) AS count
|
|
717
|
+
FROM mention_links
|
|
718
|
+
INNER JOIN mentions AS source_mentions
|
|
719
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
720
|
+
INNER JOIN mentions AS target_mentions
|
|
721
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
722
|
+
WHERE source_mentions.qid = ?
|
|
723
|
+
AND mention_links.predicate = ?
|
|
724
|
+
AND target_mentions.qid = ?
|
|
725
|
+
${t.chapterId===void 0?"":"AND source_mentions.chapter_id = ?"}
|
|
726
|
+
`,[t.subjectQid,t.predicate,t.objectQid,...t.chapterId===void 0?[]:[t.chapterId]],r=>y(r,"count"))??0}async listByTriple(t){let r=t.order==="desc"?"DESC":"ASC",n=t.limit===void 0?t.offset===void 0?"":"LIMIT -1":"LIMIT ?",i=await this.#e.queryAll(`
|
|
727
|
+
SELECT
|
|
728
|
+
mention_links.id AS id,
|
|
729
|
+
mention_links.source_mention_id AS source_mention_id,
|
|
730
|
+
mention_links.target_mention_id AS target_mention_id,
|
|
731
|
+
mention_links.predicate AS predicate,
|
|
732
|
+
mention_links.confidence AS confidence,
|
|
733
|
+
mention_links.note AS note
|
|
734
|
+
FROM mention_links
|
|
735
|
+
INNER JOIN mentions AS source_mentions
|
|
736
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
737
|
+
INNER JOIN mentions AS target_mentions
|
|
738
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
739
|
+
INNER JOIN serials AS source_serials
|
|
740
|
+
ON source_serials.id = source_mentions.chapter_id
|
|
741
|
+
WHERE source_mentions.qid = ?
|
|
742
|
+
AND mention_links.predicate = ?
|
|
743
|
+
AND target_mentions.qid = ?
|
|
744
|
+
${t.chapterId===void 0?"":"AND source_mentions.chapter_id = ?"}
|
|
745
|
+
ORDER BY
|
|
746
|
+
source_serials.document_order ${r},
|
|
747
|
+
source_mentions.chapter_id ${r},
|
|
748
|
+
source_mentions.sentence_index ${r},
|
|
749
|
+
mention_links.id ${r}
|
|
750
|
+
${n}
|
|
751
|
+
${t.offset===void 0?"":"OFFSET ?"}
|
|
752
|
+
`,[t.subjectQid,t.predicate,t.objectQid,...t.chapterId===void 0?[]:[t.chapterId],...t.limit===void 0?[]:[t.limit],...t.offset===void 0?[]:[t.offset]],vc);return await this.#r(i)}async listByChapter(t){let r=await this.#e.queryAll(`
|
|
753
|
+
SELECT
|
|
754
|
+
mention_links.id AS id,
|
|
755
|
+
mention_links.source_mention_id AS source_mention_id,
|
|
756
|
+
mention_links.target_mention_id AS target_mention_id,
|
|
757
|
+
mention_links.predicate AS predicate,
|
|
758
|
+
mention_links.confidence AS confidence,
|
|
759
|
+
mention_links.note AS note
|
|
760
|
+
FROM mention_links
|
|
761
|
+
INNER JOIN mentions AS source_mentions
|
|
762
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
763
|
+
INNER JOIN mentions AS target_mentions
|
|
764
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
765
|
+
WHERE source_mentions.chapter_id = ?
|
|
766
|
+
OR target_mentions.chapter_id = ?
|
|
767
|
+
ORDER BY mention_links.id
|
|
768
|
+
`,[t,t],vc);return await this.#r(r)}async deleteByChapter(t){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
769
|
+
DELETE FROM mention_link_evidence_sentences
|
|
770
|
+
WHERE link_id IN (
|
|
771
|
+
SELECT mention_links.id
|
|
772
|
+
FROM mention_links
|
|
773
|
+
INNER JOIN mentions AS source_mentions
|
|
774
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
775
|
+
INNER JOIN mentions AS target_mentions
|
|
776
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
777
|
+
WHERE source_mentions.chapter_id = ?
|
|
778
|
+
OR target_mentions.chapter_id = ?
|
|
779
|
+
)
|
|
780
|
+
`,[t,t]),await this.#e.run(`
|
|
781
|
+
DELETE FROM mention_links
|
|
782
|
+
WHERE source_mention_id IN (
|
|
783
|
+
SELECT id
|
|
784
|
+
FROM mentions
|
|
785
|
+
WHERE chapter_id = ?
|
|
786
|
+
)
|
|
787
|
+
OR target_mention_id IN (
|
|
788
|
+
SELECT id
|
|
789
|
+
FROM mentions
|
|
790
|
+
WHERE chapter_id = ?
|
|
791
|
+
)
|
|
792
|
+
`,[t,t])})}async#r(t){if(t.length===0)return[];let r=new Map,n=[...new Set(t.map(i=>i.id))];for(let i of HC(n,UC)){let a=i.map(()=>"?").join(", "),o=await this.#e.queryAll(`
|
|
793
|
+
SELECT link_id, chapter_id, sentence_index
|
|
794
|
+
FROM mention_link_evidence_sentences
|
|
795
|
+
WHERE link_id IN (${a})
|
|
796
|
+
ORDER BY link_id, chapter_id, sentence_index
|
|
797
|
+
`,i,s=>({linkId:k(s,"link_id"),sentenceId:[y(s,"chapter_id"),y(s,"sentence_index")]}));for(let s of o){let c=r.get(s.linkId)??[];c.push(s.sentenceId),r.set(s.linkId,c)}}return t.map(i=>({...i,evidenceSentenceIds:r.get(i.id)??[]}))}async#n(t){let r=await this.#e.queryAll(`
|
|
798
|
+
SELECT chapter_id, sentence_index
|
|
799
|
+
FROM mention_link_evidence_sentences
|
|
800
|
+
WHERE link_id = ?
|
|
801
|
+
ORDER BY chapter_id, sentence_index
|
|
802
|
+
`,[t.id],n=>[y(n,"chapter_id"),y(n,"sentence_index")]);return{...t,evidenceSentenceIds:r}}}});var io,eT=l(()=>{"use strict";M();Mi();io=class{#e;constructor(t){this.#e=t}async save(t){await this.#e.run(`
|
|
803
|
+
INSERT OR REPLACE INTO mentions (
|
|
804
|
+
id,
|
|
805
|
+
chapter_id,
|
|
806
|
+
sentence_index,
|
|
807
|
+
range_start,
|
|
808
|
+
range_end,
|
|
809
|
+
surface,
|
|
810
|
+
qid,
|
|
811
|
+
confidence,
|
|
812
|
+
note
|
|
813
|
+
)
|
|
814
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
815
|
+
`,[t.id,t.chapterId,t.sentenceIndex??null,t.rangeStart,t.rangeEnd,t.surface,t.qid,t.confidence??null,t.note??null])}async saveMany(t){await this.#e.transaction(async()=>{for(let r of t)await this.save(r)})}async getById(t){return await this.#e.queryOne(`
|
|
816
|
+
SELECT
|
|
817
|
+
id,
|
|
818
|
+
chapter_id,
|
|
819
|
+
sentence_index,
|
|
820
|
+
range_start,
|
|
821
|
+
range_end,
|
|
822
|
+
surface,
|
|
823
|
+
qid,
|
|
824
|
+
confidence,
|
|
825
|
+
note
|
|
826
|
+
FROM mentions
|
|
827
|
+
WHERE id = ?
|
|
828
|
+
`,[t],Di)}async listAll(){return await this.#e.queryAll(`
|
|
829
|
+
SELECT
|
|
830
|
+
id,
|
|
831
|
+
chapter_id,
|
|
832
|
+
sentence_index,
|
|
833
|
+
range_start,
|
|
834
|
+
range_end,
|
|
835
|
+
surface,
|
|
836
|
+
qid,
|
|
837
|
+
confidence,
|
|
838
|
+
note
|
|
839
|
+
FROM mentions
|
|
840
|
+
ORDER BY chapter_id, sentence_index, range_start, range_end, id
|
|
841
|
+
`,void 0,Di)}async countByQid(t,r={}){return await this.#e.queryOne(`
|
|
842
|
+
SELECT count(*) AS count
|
|
843
|
+
FROM mentions
|
|
844
|
+
WHERE qid = ?
|
|
845
|
+
${r.chapterId===void 0?"":"AND chapter_id = ?"}
|
|
846
|
+
`,r.chapterId===void 0?[t]:[t,r.chapterId],n=>y(n,"count"))??0}async listByQid(t,r={}){let n=r.order==="desc"?"DESC":"ASC",i=r.limit===void 0?r.offset===void 0?"":"LIMIT -1":"LIMIT ?";return await this.#e.queryAll(`
|
|
847
|
+
SELECT
|
|
848
|
+
mentions.id AS id,
|
|
849
|
+
mentions.chapter_id AS chapter_id,
|
|
850
|
+
mentions.sentence_index AS sentence_index,
|
|
851
|
+
mentions.range_start AS range_start,
|
|
852
|
+
mentions.range_end AS range_end,
|
|
853
|
+
mentions.surface AS surface,
|
|
854
|
+
mentions.qid AS qid,
|
|
855
|
+
mentions.confidence AS confidence,
|
|
856
|
+
mentions.note AS note
|
|
857
|
+
FROM mentions
|
|
858
|
+
INNER JOIN serials
|
|
859
|
+
ON serials.id = mentions.chapter_id
|
|
860
|
+
WHERE mentions.qid = ?
|
|
861
|
+
${r.chapterId===void 0?"":"AND mentions.chapter_id = ?"}
|
|
862
|
+
ORDER BY
|
|
863
|
+
serials.document_order ${n},
|
|
864
|
+
mentions.chapter_id ${n},
|
|
865
|
+
mentions.sentence_index ${n},
|
|
866
|
+
mentions.range_start ${n},
|
|
867
|
+
mentions.range_end ${n},
|
|
868
|
+
mentions.id ${n}
|
|
869
|
+
${i}
|
|
870
|
+
${r.offset===void 0?"":"OFFSET ?"}
|
|
871
|
+
`,[t,...r.chapterId===void 0?[]:[r.chapterId],...r.limit===void 0?[]:[r.limit],...r.offset===void 0?[]:[r.offset]],Di)}async listLabelsByQid(t,r={}){return await this.#e.queryAll(`
|
|
872
|
+
SELECT surface
|
|
873
|
+
FROM mentions
|
|
874
|
+
WHERE qid = ?
|
|
875
|
+
${r.chapterId===void 0?"":"AND chapter_id = ?"}
|
|
876
|
+
GROUP BY surface
|
|
877
|
+
ORDER BY count(*) DESC, surface
|
|
878
|
+
`,r.chapterId===void 0?[t]:[t,r.chapterId],n=>k(n,"surface"))}async listBySurfaces(t){let r=[...new Set(t.map(n=>n.trim()))].filter(n=>n!=="");return r.length===0?[]:await this.#e.queryAll(`
|
|
879
|
+
SELECT
|
|
880
|
+
id,
|
|
881
|
+
chapter_id,
|
|
882
|
+
sentence_index,
|
|
883
|
+
range_start,
|
|
884
|
+
range_end,
|
|
885
|
+
surface,
|
|
886
|
+
qid,
|
|
887
|
+
confidence,
|
|
888
|
+
note
|
|
889
|
+
FROM mentions
|
|
890
|
+
WHERE surface IN (${r.map(()=>"?").join(", ")})
|
|
891
|
+
ORDER BY chapter_id, sentence_index, range_start, range_end, id
|
|
892
|
+
`,r,Di)}async listBySurfaceTerms(t){let r=[...new Set(t.map(i=>i.trim().toLowerCase()))].filter(i=>i!=="");if(r.length===0)return[];let n=r.map(()=>"lower(surface) LIKE ? ESCAPE '\\'").join(" OR ");return await this.#e.queryAll(`
|
|
893
|
+
SELECT
|
|
894
|
+
id,
|
|
895
|
+
chapter_id,
|
|
896
|
+
sentence_index,
|
|
897
|
+
range_start,
|
|
898
|
+
range_end,
|
|
899
|
+
surface,
|
|
900
|
+
qid,
|
|
901
|
+
confidence,
|
|
902
|
+
note
|
|
903
|
+
FROM mentions
|
|
904
|
+
WHERE ${n}
|
|
905
|
+
ORDER BY chapter_id, sentence_index, range_start, range_end, id
|
|
906
|
+
`,r.map(i=>`%${JC(i)}%`),Di)}async listByChapter(t){return await this.#e.queryAll(`
|
|
907
|
+
SELECT
|
|
908
|
+
id,
|
|
909
|
+
chapter_id,
|
|
910
|
+
sentence_index,
|
|
911
|
+
range_start,
|
|
912
|
+
range_end,
|
|
913
|
+
surface,
|
|
914
|
+
qid,
|
|
915
|
+
confidence,
|
|
916
|
+
note
|
|
917
|
+
FROM mentions
|
|
918
|
+
WHERE chapter_id = ?
|
|
919
|
+
ORDER BY sentence_index, range_start, range_end, id
|
|
920
|
+
`,[t],Di)}async deleteByChapter(t){await this.#e.run(`
|
|
921
|
+
DELETE FROM mentions
|
|
922
|
+
WHERE chapter_id = ?
|
|
923
|
+
`,[t])}}});var ao,tT=l(()=>{"use strict";M();Mi();ao=class{#e;constructor(t){this.#e=t}async getMap(t){let r=await this.#e.queryAll(`
|
|
924
|
+
SELECT key, value_json
|
|
925
|
+
FROM object_metadata
|
|
926
|
+
WHERE object_path = ?
|
|
927
|
+
ORDER BY key
|
|
928
|
+
`,[t],i=>({key:k(i,"key"),value:KC(k(i,"value_json"))})),n={};for(let i of r)n[i.key]=i.value;return n}async replaceMap(t,r){await this.#e.transaction(async()=>{await this.clear(t.objectPath);for(let[n,i]of Object.entries(r))await this.put(t,n,i)})}async put(t,r,n){await this.#e.run(`
|
|
929
|
+
INSERT INTO object_metadata (
|
|
930
|
+
object_kind,
|
|
931
|
+
object_path,
|
|
932
|
+
key,
|
|
933
|
+
value_json,
|
|
934
|
+
updated_at,
|
|
935
|
+
chapter_id,
|
|
936
|
+
chunk_id,
|
|
937
|
+
entity_qid,
|
|
938
|
+
triple_subject_qid,
|
|
939
|
+
triple_predicate,
|
|
940
|
+
triple_object_qid
|
|
941
|
+
)
|
|
942
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
943
|
+
ON CONFLICT(object_path, key) DO UPDATE SET
|
|
944
|
+
object_kind = excluded.object_kind,
|
|
945
|
+
value_json = excluded.value_json,
|
|
946
|
+
updated_at = excluded.updated_at,
|
|
947
|
+
chapter_id = excluded.chapter_id,
|
|
948
|
+
chunk_id = excluded.chunk_id,
|
|
949
|
+
entity_qid = excluded.entity_qid,
|
|
950
|
+
triple_subject_qid = excluded.triple_subject_qid,
|
|
951
|
+
triple_predicate = excluded.triple_predicate,
|
|
952
|
+
triple_object_qid = excluded.triple_object_qid
|
|
953
|
+
`,[t.kind,t.objectPath,r,JSON.stringify(n),new Date().toISOString(),t.chapterId??null,t.chunkId??null,t.entityQid??null,t.tripleSubjectQid??null,t.triplePredicate??null,t.tripleObjectQid??null])}async deleteKey(t,r){await this.#e.run(`
|
|
954
|
+
DELETE FROM object_metadata
|
|
955
|
+
WHERE object_path = ?
|
|
956
|
+
AND key = ?
|
|
957
|
+
`,[t,r])}async clear(t){await this.#e.run(`
|
|
958
|
+
DELETE FROM object_metadata
|
|
959
|
+
WHERE object_path = ?
|
|
960
|
+
`,[t])}async deleteChapterSubtree(t){await this.#e.run(`
|
|
961
|
+
DELETE FROM object_metadata
|
|
962
|
+
WHERE chapter_id = ?
|
|
963
|
+
`,[t])}async deleteDeletedChunks(){await this.#e.run(`
|
|
964
|
+
DELETE FROM object_metadata
|
|
965
|
+
WHERE chunk_id IS NOT NULL
|
|
966
|
+
AND NOT EXISTS (
|
|
967
|
+
SELECT 1
|
|
968
|
+
FROM chunks
|
|
969
|
+
WHERE chunks.id = object_metadata.chunk_id
|
|
970
|
+
)
|
|
971
|
+
`)}async deleteDeletedEntitiesAndTriples(){await this.#e.run(`
|
|
972
|
+
DELETE FROM object_metadata
|
|
973
|
+
WHERE entity_qid IS NOT NULL
|
|
974
|
+
AND NOT EXISTS (
|
|
975
|
+
SELECT 1
|
|
976
|
+
FROM mentions
|
|
977
|
+
WHERE mentions.qid = object_metadata.entity_qid
|
|
978
|
+
)
|
|
979
|
+
`),await this.#e.run(`
|
|
980
|
+
DELETE FROM object_metadata
|
|
981
|
+
WHERE triple_subject_qid IS NOT NULL
|
|
982
|
+
AND NOT EXISTS (
|
|
983
|
+
SELECT 1
|
|
984
|
+
FROM mention_links
|
|
985
|
+
JOIN mentions AS source_mentions
|
|
986
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
987
|
+
JOIN mentions AS target_mentions
|
|
988
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
989
|
+
WHERE source_mentions.qid = object_metadata.triple_subject_qid
|
|
990
|
+
AND mention_links.predicate = object_metadata.triple_predicate
|
|
991
|
+
AND target_mentions.qid = object_metadata.triple_object_qid
|
|
992
|
+
)
|
|
993
|
+
`)}}});var oo,rT=l(()=>{"use strict";M();Mi();oo=class{#e;constructor(t){this.#e=t}async countAll(){return await this.#e.queryOne(`
|
|
994
|
+
SELECT COUNT(*) AS count
|
|
995
|
+
FROM reading_edges
|
|
996
|
+
`,void 0,t=>y(t,"count"))??0}async save(t){await this.#e.run(`
|
|
997
|
+
INSERT OR REPLACE INTO reading_edges (from_id, to_id, strength, weight)
|
|
998
|
+
VALUES (?, ?, ?, ?)
|
|
999
|
+
`,[t.fromId,t.toId,t.strength??null,t.weight])}async listAll(){return await this.#e.queryAll(`
|
|
1000
|
+
SELECT from_id, to_id, strength, weight
|
|
1001
|
+
FROM reading_edges
|
|
1002
|
+
ORDER BY from_id, to_id
|
|
1003
|
+
`,void 0,t=>cp(t))}async listBySerial(t){return await this.#e.queryAll(`
|
|
1004
|
+
SELECT
|
|
1005
|
+
reading_edges.from_id AS from_id,
|
|
1006
|
+
reading_edges.to_id AS to_id,
|
|
1007
|
+
reading_edges.strength AS strength,
|
|
1008
|
+
reading_edges.weight AS weight
|
|
1009
|
+
FROM reading_edges
|
|
1010
|
+
INNER JOIN chunks AS from_chunks
|
|
1011
|
+
ON from_chunks.id = reading_edges.from_id
|
|
1012
|
+
INNER JOIN chunks AS to_chunks
|
|
1013
|
+
ON to_chunks.id = reading_edges.to_id
|
|
1014
|
+
WHERE from_chunks.serial_id = ? AND to_chunks.serial_id = ?
|
|
1015
|
+
ORDER BY reading_edges.from_id, reading_edges.to_id
|
|
1016
|
+
`,[t,t],r=>cp(r))}async listIncoming(t){return await this.#t("to_id",t)}async listOutgoing(t){return await this.#t("from_id",t)}async#t(t,r){return await this.#e.queryAll(`
|
|
1017
|
+
SELECT from_id, to_id, strength, weight
|
|
1018
|
+
FROM reading_edges
|
|
1019
|
+
WHERE ${t} = ?
|
|
1020
|
+
ORDER BY from_id, to_id
|
|
1021
|
+
`,[r],n=>cp(n))}}});function tK(e){let t=Te(e,"topology_parameter_hash"),r=Te(e,"knowledge_graph_parameter_hash");return{documentOrder:y(e,"document_order"),id:y(e,"id"),knowledgeGraphReady:y(e,"knowledge_graph_ready")!==0,...r===void 0?{}:{knowledgeGraphParameterHash:r},revision:y(e,"revision"),topologyReady:y(e,"topology_ready")!==0,...t===void 0?{}:{topologyParameterHash:t}}}var so,nT=l(()=>{"use strict";M();Mi();so=class{#e;constructor(t){this.#e=t}async create(){return await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1022
|
+
INSERT INTO serials DEFAULT VALUES
|
|
1023
|
+
`);let t=await this.#e.getLastInsertRowId();return await this.#e.run(`
|
|
1024
|
+
INSERT INTO serial_states (
|
|
1025
|
+
serial_id, revision, topology_ready, knowledge_graph_ready
|
|
1026
|
+
)
|
|
1027
|
+
VALUES (?, ?, ?, ?)
|
|
1028
|
+
`,[t,0,0,0]),t})}async createWithId(t){try{await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1029
|
+
INSERT INTO serials (id)
|
|
1030
|
+
VALUES (?)
|
|
1031
|
+
`,[t]),await this.#e.run(`
|
|
1032
|
+
INSERT INTO serial_states (
|
|
1033
|
+
serial_id, revision, topology_ready, knowledge_graph_ready
|
|
1034
|
+
)
|
|
1035
|
+
VALUES (?, ?, ?, ?)
|
|
1036
|
+
`,[t,0,0,0])})}catch(r){throw BC(r)?new Error(`Serial ${t} already exists`):r}}async ensure(t){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1037
|
+
INSERT OR IGNORE INTO serials (id)
|
|
1038
|
+
VALUES (?)
|
|
1039
|
+
`,[t]),await this.#e.run(`
|
|
1040
|
+
INSERT OR IGNORE INTO serial_states (
|
|
1041
|
+
serial_id, revision, topology_ready, knowledge_graph_ready
|
|
1042
|
+
)
|
|
1043
|
+
VALUES (?, ?, ?, ?)
|
|
1044
|
+
`,[t,0,0,0])})}async getById(t){return await this.#e.queryOne(`
|
|
1045
|
+
SELECT
|
|
1046
|
+
serials.id AS id,
|
|
1047
|
+
serials.document_order AS document_order,
|
|
1048
|
+
COALESCE(serial_states.revision, 0) AS revision,
|
|
1049
|
+
COALESCE(serial_states.topology_ready, 0) AS topology_ready,
|
|
1050
|
+
serial_states.topology_parameter_hash AS topology_parameter_hash,
|
|
1051
|
+
COALESCE(serial_states.knowledge_graph_ready, 0) AS knowledge_graph_ready,
|
|
1052
|
+
serial_states.knowledge_graph_parameter_hash AS knowledge_graph_parameter_hash
|
|
1053
|
+
FROM serials
|
|
1054
|
+
LEFT JOIN serial_states
|
|
1055
|
+
ON serial_states.serial_id = serials.id
|
|
1056
|
+
WHERE serials.id = ?
|
|
1057
|
+
`,[t],tK)}async getRevision(t){return await this.#e.queryOne(`
|
|
1058
|
+
SELECT COALESCE(revision, 0) AS revision
|
|
1059
|
+
FROM serial_states
|
|
1060
|
+
WHERE serial_id = ?
|
|
1061
|
+
`,[t],r=>y(r,"revision"))??0}async getRevisions(t){let r=[...new Set(t)].sort($C);if(r.length===0)return new Map;let n=await this.#e.queryAll(`
|
|
1062
|
+
SELECT serial_id, COALESCE(revision, 0) AS revision
|
|
1063
|
+
FROM serial_states
|
|
1064
|
+
WHERE serial_id IN (${r.map(()=>"?").join(", ")})
|
|
1065
|
+
ORDER BY serial_id
|
|
1066
|
+
`,r,i=>[y(i,"serial_id"),y(i,"revision")]);return new Map(n)}async bumpRevision(t){await this.ensure(t),await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1067
|
+
UPDATE serial_states
|
|
1068
|
+
SET revision = revision + 1
|
|
1069
|
+
WHERE serial_id = ?
|
|
1070
|
+
`,[t]),await this.bumpChaptersRevision()})}async bumpChaptersRevision(){await this.#e.run(`
|
|
1071
|
+
INSERT INTO archive_revisions (key, value)
|
|
1072
|
+
VALUES ('chapters', 1)
|
|
1073
|
+
ON CONFLICT(key) DO UPDATE SET value = value + 1
|
|
1074
|
+
`)}async getChaptersRevision(){return await this.#e.queryOne(`
|
|
1075
|
+
SELECT value
|
|
1076
|
+
FROM archive_revisions
|
|
1077
|
+
WHERE key = 'chapters'
|
|
1078
|
+
`,void 0,t=>y(t,"value"))??0}async getMaxId(){return await this.#e.queryOne(`
|
|
1079
|
+
SELECT COALESCE(MAX(id), 0) AS max_id
|
|
1080
|
+
FROM serials
|
|
1081
|
+
`,void 0,r=>y(r,"max_id"))??0}async setTopologyReady(t,r=!0,n){if(await this.ensure(t),r){await this.#e.run(`
|
|
1082
|
+
UPDATE serial_states
|
|
1083
|
+
SET
|
|
1084
|
+
topology_ready = ?,
|
|
1085
|
+
topology_parameter_hash = COALESCE(?, topology_parameter_hash)
|
|
1086
|
+
WHERE serial_id = ?
|
|
1087
|
+
`,[1,n??null,t]);return}await this.#e.run(`
|
|
1088
|
+
UPDATE serial_states
|
|
1089
|
+
SET
|
|
1090
|
+
topology_ready = ?,
|
|
1091
|
+
topology_parameter_hash = NULL
|
|
1092
|
+
WHERE serial_id = ?
|
|
1093
|
+
`,[0,t])}async setKnowledgeGraphReady(t,r=!0,n){if(await this.ensure(t),r){await this.#e.run(`
|
|
1094
|
+
UPDATE serial_states
|
|
1095
|
+
SET
|
|
1096
|
+
knowledge_graph_ready = ?,
|
|
1097
|
+
knowledge_graph_parameter_hash = COALESCE(?, knowledge_graph_parameter_hash)
|
|
1098
|
+
WHERE serial_id = ?
|
|
1099
|
+
`,[1,n??null,t]);return}await this.#e.run(`
|
|
1100
|
+
UPDATE serial_states
|
|
1101
|
+
SET
|
|
1102
|
+
knowledge_graph_ready = ?,
|
|
1103
|
+
knowledge_graph_parameter_hash = NULL
|
|
1104
|
+
WHERE serial_id = ?
|
|
1105
|
+
`,[0,t])}async listIds(){return await this.#e.queryAll(`
|
|
1106
|
+
SELECT id
|
|
1107
|
+
FROM serials
|
|
1108
|
+
ORDER BY id
|
|
1109
|
+
`,void 0,t=>y(t,"id"))}async listDocumentOrders(){let t=await this.#e.queryAll(`
|
|
1110
|
+
SELECT id, document_order
|
|
1111
|
+
FROM serials
|
|
1112
|
+
ORDER BY id
|
|
1113
|
+
`,void 0,r=>[y(r,"id"),y(r,"document_order")]);return new Map(t)}async setDocumentOrders(t){await this.#e.transaction(async()=>{for(let r of t)await this.#e.run(`
|
|
1114
|
+
INSERT OR IGNORE INTO serials (id)
|
|
1115
|
+
VALUES (?)
|
|
1116
|
+
`,[r.serialId]),await this.#e.run(`
|
|
1117
|
+
INSERT OR IGNORE INTO serial_states (
|
|
1118
|
+
serial_id, revision, topology_ready, knowledge_graph_ready
|
|
1119
|
+
)
|
|
1120
|
+
VALUES (?, ?, ?, ?)
|
|
1121
|
+
`,[r.serialId,0,0,0]),await this.#e.run(`
|
|
1122
|
+
UPDATE serials
|
|
1123
|
+
SET document_order = ?
|
|
1124
|
+
WHERE id = ?
|
|
1125
|
+
`,[r.documentOrder,r.serialId])})}}});var co,iT=l(()=>{"use strict";M();co=class{#e;constructor(t){this.#e=t}async save(t){await this.#e.run(`
|
|
1126
|
+
INSERT OR REPLACE INTO snake_chunks (snake_id, chunk_id, position)
|
|
1127
|
+
VALUES (?, ?, ?)
|
|
1128
|
+
`,[t.snakeId,t.chunkId,t.position])}async listChunkIds(t){return await this.#e.queryAll(`
|
|
1129
|
+
SELECT chunk_id
|
|
1130
|
+
FROM snake_chunks
|
|
1131
|
+
WHERE snake_id = ?
|
|
1132
|
+
ORDER BY position
|
|
1133
|
+
`,[t],r=>y(r,"chunk_id"))}async listBySnake(t){return await this.#e.queryAll(`
|
|
1134
|
+
SELECT snake_id, chunk_id, position
|
|
1135
|
+
FROM snake_chunks
|
|
1136
|
+
WHERE snake_id = ?
|
|
1137
|
+
ORDER BY position
|
|
1138
|
+
`,[t],r=>({chunkId:y(r,"chunk_id"),position:y(r,"position"),snakeId:y(r,"snake_id")}))}}});var uo,aT=l(()=>{"use strict";M();uo=class{#e;constructor(t){this.#e=t}async create(t){return await this.#e.run(`
|
|
1139
|
+
INSERT INTO snakes (
|
|
1140
|
+
serial_id,
|
|
1141
|
+
group_id,
|
|
1142
|
+
local_snake_id,
|
|
1143
|
+
size,
|
|
1144
|
+
first_label,
|
|
1145
|
+
last_label,
|
|
1146
|
+
wordsCount,
|
|
1147
|
+
weight
|
|
1148
|
+
)
|
|
1149
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1150
|
+
`,[t.serialId,t.groupId,t.localSnakeId,t.size,t.firstLabel,t.lastLabel,t.wordsCount??0,t.weight??0]),await this.#e.getLastInsertRowId()}async getById(t){return await this.#e.queryOne(`
|
|
1151
|
+
SELECT
|
|
1152
|
+
id,
|
|
1153
|
+
serial_id,
|
|
1154
|
+
group_id,
|
|
1155
|
+
local_snake_id,
|
|
1156
|
+
size,
|
|
1157
|
+
first_label,
|
|
1158
|
+
last_label,
|
|
1159
|
+
wordsCount,
|
|
1160
|
+
weight
|
|
1161
|
+
FROM snakes
|
|
1162
|
+
WHERE id = ?
|
|
1163
|
+
`,[t],r=>({serialId:y(r,"serial_id"),firstLabel:k(r,"first_label"),groupId:y(r,"group_id"),id:y(r,"id"),lastLabel:k(r,"last_label"),localSnakeId:y(r,"local_snake_id"),size:y(r,"size"),wordsCount:y(r,"wordsCount"),weight:y(r,"weight")}))}async listIdsByGroup(t,r){return await this.#e.queryAll(`
|
|
1164
|
+
SELECT id
|
|
1165
|
+
FROM snakes
|
|
1166
|
+
WHERE serial_id = ? AND group_id = ?
|
|
1167
|
+
ORDER BY id
|
|
1168
|
+
`,[t,r],n=>y(n,"id"))}async listBySerial(t){return await this.#e.queryAll(`
|
|
1169
|
+
SELECT
|
|
1170
|
+
id,
|
|
1171
|
+
serial_id,
|
|
1172
|
+
group_id,
|
|
1173
|
+
local_snake_id,
|
|
1174
|
+
size,
|
|
1175
|
+
first_label,
|
|
1176
|
+
last_label,
|
|
1177
|
+
wordsCount,
|
|
1178
|
+
weight
|
|
1179
|
+
FROM snakes
|
|
1180
|
+
WHERE serial_id = ?
|
|
1181
|
+
ORDER BY group_id, id
|
|
1182
|
+
`,[t],r=>({serialId:y(r,"serial_id"),firstLabel:k(r,"first_label"),groupId:y(r,"group_id"),id:y(r,"id"),lastLabel:k(r,"last_label"),localSnakeId:y(r,"local_snake_id"),size:y(r,"size"),wordsCount:y(r,"wordsCount"),weight:y(r,"weight")}))}}});var lo,oT=l(()=>{"use strict";M();lo=class{#e;constructor(t){this.#e=t}async save(t){await this.#e.run(`
|
|
1183
|
+
INSERT OR REPLACE INTO snake_edges (from_snake_id, to_snake_id, weight)
|
|
1184
|
+
VALUES (?, ?, ?)
|
|
1185
|
+
`,[t.fromSnakeId,t.toSnakeId,t.weight])}async listIncoming(t){return await this.#t("to_snake_id",t)}async listOutgoing(t){return await this.#t("from_snake_id",t)}async listWithin(t){if(t.length===0)return[];let r=t.map(()=>"?").join(", ");return await this.#e.queryAll(`
|
|
1186
|
+
SELECT from_snake_id, to_snake_id, weight
|
|
1187
|
+
FROM snake_edges
|
|
1188
|
+
WHERE from_snake_id IN (${r})
|
|
1189
|
+
AND to_snake_id IN (${r})
|
|
1190
|
+
ORDER BY from_snake_id, to_snake_id
|
|
1191
|
+
`,[...t,...t],n=>({fromSnakeId:y(n,"from_snake_id"),toSnakeId:y(n,"to_snake_id"),weight:y(n,"weight")}))}async listBySerial(t){return await this.#e.queryAll(`
|
|
1192
|
+
SELECT
|
|
1193
|
+
snake_edges.from_snake_id AS from_snake_id,
|
|
1194
|
+
snake_edges.to_snake_id AS to_snake_id,
|
|
1195
|
+
snake_edges.weight AS weight
|
|
1196
|
+
FROM snake_edges
|
|
1197
|
+
INNER JOIN snakes AS from_snakes
|
|
1198
|
+
ON from_snakes.id = snake_edges.from_snake_id
|
|
1199
|
+
INNER JOIN snakes AS to_snakes
|
|
1200
|
+
ON to_snakes.id = snake_edges.to_snake_id
|
|
1201
|
+
WHERE from_snakes.serial_id = ? AND to_snakes.serial_id = ?
|
|
1202
|
+
ORDER BY snake_edges.from_snake_id, snake_edges.to_snake_id
|
|
1203
|
+
`,[t,t],r=>({fromSnakeId:y(r,"from_snake_id"),toSnakeId:y(r,"to_snake_id"),weight:y(r,"weight")}))}async#t(t,r){return await this.#e.queryAll(`
|
|
1204
|
+
SELECT from_snake_id, to_snake_id, weight
|
|
1205
|
+
FROM snake_edges
|
|
1206
|
+
WHERE ${t} = ?
|
|
1207
|
+
ORDER BY from_snake_id, to_snake_id
|
|
1208
|
+
`,[r],n=>({fromSnakeId:y(n,"from_snake_id"),toSnakeId:y(n,"to_snake_id"),weight:y(n,"weight")}))}}});var sT=l(()=>{"use strict";iT();aT();oT()});var By=l(()=>{"use strict";VC();XC();YC();ZC();eT();tT();rT();nT();sT()});function cT(e,t){return t-e}var Cc,Hy=l(()=>{"use strict";Cc=class{#e=[];#t;#r=new Set;#n=!1;#i=!1;constructor(t){this.#t=t}complete(){this.#a(),this.#n=!0}async dispose(){this.#i||(this.#i=!0,!this.#n&&await this.#t.rollbackContext(this))}ownSerial(t){this.#a(),this.#r.add(t)}async run(t){return this.#a(),await this.#t.runWithContext(this,t)}listCreatedFilePaths(){return[...this.#e]}listOwnedSerialIds(){return[...this.#r]}registerCreatedFile(t){this.#e.push(t)}#a(){if(this.#i)throw new Error("DocumentContext is already disposed")}}});import{mkdir as rK,readFile as nK,readdir as iK,rm as aK,unlink as oK,writeFile as dT}from"fs/promises";import{join as uT}from"path";var qy,Ky=l(()=>{"use strict";H();qy={close:()=>Promise.resolve(),deleteFile:async e=>{await oK(e)},deleteTree:async e=>{await aK(e,{force:!0,recursive:!0})},ensureDirectory:async e=>{await rK(e,{recursive:!0})},initializeDatabaseSchema:()=>!0,markDatabaseDirty:()=>{},markSearchIndexDatabaseDirty:()=>{},openDatabaseReadonly:()=>!1,listFiles:async e=>(await iK(e,{withFileTypes:!0})).filter(t=>t.isFile()).map(t=>t.name),readFile:async e=>{try{return await nK(e)}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}},resolveDatabasePath:e=>Promise.resolve(uT(e,"database.db")),resolveSearchIndexDatabasePath:e=>Promise.resolve(uT(e,"fts.db")),writeFile:async(e,t,r)=>{typeof t=="string"?await dT(e,t,{encoding:"utf8",flag:r.overwrite===!0?"w":"wx"}):await dT(e,t,{flag:r.overwrite===!0?"w":"wx"})}}});import{join as dp}from"path";async function Jy(e){let t=await sK(e.fileStore,e.path);if(t!==void 0)return e.parse(JSON.parse(t))}async function lT(e,t){return await e.readFile(t)}async function up(e){await mT({context:e.context,content:`${JSON.stringify(e.value,null,2)}
|
|
1209
|
+
`,fileStore:e.fileStore,options:e.options??{},path:e.path})}async function pT(e){await mT({context:e.context,content:e.content,fileStore:e.fileStore,options:{overwrite:!1},path:e.path})}function zy(e){return dp(lp(e),"data.bin")}function lp(e){return dp(e,"cover")}function Qy(e){return dp(lp(e),"info.json")}function pp(e){return dp(e,"toc.json")}async function sK(e,t){let r=await e.readFile(t);return r===void 0?void 0:Buffer.from(r).toString("utf8")}async function mT(e){try{await e.fileStore.writeFile(e.path,e.content,e.options)}catch(t){throw R(t)&&t.code==="EEXIST"?new Error(`File already exists: ${e.path}`):t}e.options.overwrite!==!0&&e.context?.registerCreatedFile(e.path)}var hT=l(()=>{"use strict";H()});import{stat as cK}from"fs/promises";import{join as dK}from"path";async function Vy(e){let t=e.fileStore.resolveSearchIndexDatabasePath===void 0?dK(e.documentPath,"fts.db"):await e.fileStore.resolveSearchIndexDatabasePath(e.documentPath),r=!e.readonly&&await uK(t),n=await $.open(t,r?sp:"",{onWrite:()=>{e.fileStore.markSearchIndexDatabaseDirty?.()},readonly:e.readonly});e.readonly||await lK(n);try{return await e.operation(n)}finally{await n.close()}}async function uK(e){let t=await cK(e).catch(r=>{if(!(R(r)&&r.code==="ENOENT"))throw r});return t===void 0||t.size===0}async function lK(e){await fT(e,"text_sentence_records","archive_id","INTEGER NOT NULL DEFAULT 0"),await mK(e,"text_sentence_records",["archive_id","kind","chapter_id","sentence_index"])||await pK(e),await fT(e,"search_object_properties_records","archive_id","INTEGER NOT NULL DEFAULT 0"),await e.run(`
|
|
1210
|
+
CREATE TABLE IF NOT EXISTS index_dirty_chapters (
|
|
1211
|
+
archive_id INTEGER NOT NULL,
|
|
1212
|
+
chapter_id INTEGER NOT NULL,
|
|
1213
|
+
updated_at INTEGER NOT NULL,
|
|
1214
|
+
PRIMARY KEY (archive_id, chapter_id)
|
|
1215
|
+
)
|
|
1216
|
+
`)}async function pK(e){await e.transaction(async()=>{await e.run(`
|
|
1217
|
+
CREATE TABLE text_sentence_records_next (
|
|
1218
|
+
${Uy}
|
|
1219
|
+
)
|
|
1220
|
+
`),await e.run(`
|
|
1221
|
+
INSERT INTO text_sentence_records_next (
|
|
1222
|
+
id,
|
|
1223
|
+
archive_id,
|
|
1224
|
+
kind,
|
|
1225
|
+
chapter_id,
|
|
1226
|
+
sentence_index,
|
|
1227
|
+
words_count,
|
|
1228
|
+
byte_offset,
|
|
1229
|
+
byte_length
|
|
1230
|
+
)
|
|
1231
|
+
SELECT
|
|
1232
|
+
id,
|
|
1233
|
+
archive_id,
|
|
1234
|
+
kind,
|
|
1235
|
+
chapter_id,
|
|
1236
|
+
sentence_index,
|
|
1237
|
+
words_count,
|
|
1238
|
+
byte_offset,
|
|
1239
|
+
byte_length
|
|
1240
|
+
FROM text_sentence_records
|
|
1241
|
+
`),await e.run("DROP TABLE text_sentence_records"),await e.run(`
|
|
1242
|
+
ALTER TABLE text_sentence_records_next
|
|
1243
|
+
RENAME TO text_sentence_records
|
|
1244
|
+
`),await e.run(`
|
|
1245
|
+
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
1246
|
+
ON text_sentence_records(archive_id, kind, chapter_id, sentence_index)
|
|
1247
|
+
`)})}async function mK(e,t,r){let n=await e.queryAll(`PRAGMA index_list(${t})`,void 0,i=>({name:String(i.name),unique:Number(i.unique)===1}));for(let i of n){if(!i.unique)continue;let o=(await e.queryAll(`PRAGMA index_info(${i.name})`,void 0,s=>({name:String(s.name),seqno:Number(s.seqno)}))).sort((s,c)=>s.seqno-c.seqno).map(s=>s.name);if(o.length===r.length&&o.every((s,c)=>s===r[c]))return!0}return!1}async function fT(e,t,r,n){(await e.queryAll(`PRAGMA table_info(${t})`,void 0,a=>String(a.name))).includes(r)||await e.run(`ALTER TABLE ${t} ADD COLUMN ${r} ${n}`)}var gT=l(()=>{"use strict";H();M();xc()});async function yT(e){await fK(e.context.listOwnedSerialIds(),e.deleteSerialResources),await hK(e.fileStore,e.context.listCreatedFilePaths())}async function bT(e){await e.metadata.deleteChapterSubtree(e.serialId),await Xy({database:e.database,metadata:e.metadata,serialId:e.serialId}),await e.database.transaction(async()=>{await e.database.run(`
|
|
1248
|
+
DELETE FROM serial_states
|
|
1249
|
+
WHERE serial_id = ?
|
|
1250
|
+
`,[e.serialId]),await e.database.run(`
|
|
1251
|
+
DELETE FROM serials
|
|
1252
|
+
WHERE id = ?
|
|
1253
|
+
`,[e.serialId])}),await e.textStreams.getSerial(e.serialId).delete(),await e.deleteSummary(e.serialId),await e.serials.bumpChaptersRevision(),await e.graphBuildParameters.deleteUnreferenced()}async function Xy(e){await e.database.transaction(async()=>{await e.database.run(`
|
|
1254
|
+
DELETE FROM mention_link_evidence_sentences
|
|
1255
|
+
WHERE link_id IN (
|
|
1256
|
+
SELECT mention_links.id
|
|
1257
|
+
FROM mention_links
|
|
1258
|
+
INNER JOIN mentions AS source_mentions
|
|
1259
|
+
ON source_mentions.id = mention_links.source_mention_id
|
|
1260
|
+
INNER JOIN mentions AS target_mentions
|
|
1261
|
+
ON target_mentions.id = mention_links.target_mention_id
|
|
1262
|
+
WHERE source_mentions.chapter_id = ?
|
|
1263
|
+
OR target_mentions.chapter_id = ?
|
|
1264
|
+
)
|
|
1265
|
+
`,[e.serialId,e.serialId]),await e.database.run(`
|
|
1266
|
+
DELETE FROM mention_links
|
|
1267
|
+
WHERE source_mention_id IN (
|
|
1268
|
+
SELECT id
|
|
1269
|
+
FROM mentions
|
|
1270
|
+
WHERE chapter_id = ?
|
|
1271
|
+
) OR target_mention_id IN (
|
|
1272
|
+
SELECT id
|
|
1273
|
+
FROM mentions
|
|
1274
|
+
WHERE chapter_id = ?
|
|
1275
|
+
)
|
|
1276
|
+
`,[e.serialId,e.serialId]),await e.database.run(`
|
|
1277
|
+
DELETE FROM mentions
|
|
1278
|
+
WHERE chapter_id = ?
|
|
1279
|
+
`,[e.serialId]),await e.database.run(`
|
|
1280
|
+
DELETE FROM snake_edges
|
|
1281
|
+
WHERE from_snake_id IN (
|
|
1282
|
+
SELECT id
|
|
1283
|
+
FROM snakes
|
|
1284
|
+
WHERE serial_id = ?
|
|
1285
|
+
) OR to_snake_id IN (
|
|
1286
|
+
SELECT id
|
|
1287
|
+
FROM snakes
|
|
1288
|
+
WHERE serial_id = ?
|
|
1289
|
+
)
|
|
1290
|
+
`,[e.serialId,e.serialId]),await e.database.run(`
|
|
1291
|
+
DELETE FROM snake_chunks
|
|
1292
|
+
WHERE snake_id IN (
|
|
1293
|
+
SELECT id
|
|
1294
|
+
FROM snakes
|
|
1295
|
+
WHERE serial_id = ?
|
|
1296
|
+
)
|
|
1297
|
+
`,[e.serialId]),await e.database.run(`
|
|
1298
|
+
DELETE FROM snakes
|
|
1299
|
+
WHERE serial_id = ?
|
|
1300
|
+
`,[e.serialId]),await e.database.run(`
|
|
1301
|
+
DELETE FROM sentence_groups
|
|
1302
|
+
WHERE serial_id = ?
|
|
1303
|
+
`,[e.serialId]),await e.database.run(`
|
|
1304
|
+
DELETE FROM reading_edges
|
|
1305
|
+
WHERE from_id IN (
|
|
1306
|
+
SELECT id
|
|
1307
|
+
FROM chunks
|
|
1308
|
+
WHERE serial_id = ?
|
|
1309
|
+
) OR to_id IN (
|
|
1310
|
+
SELECT id
|
|
1311
|
+
FROM chunks
|
|
1312
|
+
WHERE serial_id = ?
|
|
1313
|
+
)
|
|
1314
|
+
`,[e.serialId,e.serialId]),await e.database.run(`
|
|
1315
|
+
DELETE FROM chunk_sentences
|
|
1316
|
+
WHERE serial_id = ?
|
|
1317
|
+
`,[e.serialId]),await e.database.run(`
|
|
1318
|
+
DELETE FROM chunks
|
|
1319
|
+
WHERE serial_id = ?
|
|
1320
|
+
`,[e.serialId]),await e.metadata.deleteDeletedChunks(),await e.metadata.deleteDeletedEntitiesAndTriples()})}async function hK(e,t){for(let r of[...t].reverse())try{await e.deleteFile(r)}catch(n){if(R(n)&&n.code==="ENOENT")continue;throw n}}async function fK(e,t){for(let r of[...e].sort(cT))await t(r)}var wT=l(()=>{"use strict";H();Hy()});function Yy(e){let t=[];for(let r of e)r.serialId!==void 0&&t.push(r.serialId),t.push(...Yy(r.children));return t}var ST=l(()=>{"use strict"});import{AsyncLocalStorage as gK}from"async_hooks";import{join as yK,resolve as bK}from"path";import{z as Zy}from"zod";function kT(e){let t={items:e.items.map(ET),version:e.version};return Za(t.items),t}function ET(e){return{children:e.children.map(ET),...e.key===void 0?{}:{key:e.key},...e.serialId===void 0?{}:{serialId:e.serialId},...e.title===void 0?{}:{title:e.title}}}var wK,ye,IT=l(()=>{"use strict";pc();Zl();Ic();M();py();xc();By();Gr();Hy();Ky();hT();gT();wT();ST();wK=Zy.object({mediaType:Zy.string().min(1),path:Zy.string()}),ye=class e{chunks;fragmentGroups;graphBuildParameters;readingEdges;mentionLinks;mentions;metadata;path;serials;snakeChunks;snakeEdges;snakes;#e;#t;#r;#n=new gK;constructor(t,r,n,i=qy){this.#e=t,this.#t=i,this.#r=r,this.chunks=new eo(t),this.fragmentGroups=new to(t),this.graphBuildParameters=new ro(t),this.readingEdges=new oo(t),this.mentionLinks=new no(t),this.mentions=new io(t),this.metadata=new ao(t),this.path=n,this.serials=new so(t),this.snakeChunks=new co(t),this.snakeEdges=new lo(t),this.snakes=new uo(t)}static async open(t,r={}){let n=bK(t),i=r.fileStore??qy;try{let a=await i.resolveDatabasePath(n);await i.ensureDirectory(n);let o=i.initializeDatabaseSchema(),s=await $.open(a,o?Gy:"",{onWrite:()=>{i.markDatabaseDirty?.()},readonly:i.openDatabaseReadonly()});o&&await GC(s);let c=new uc(n,s,{deleteTree:async u=>{await i.deleteTree(u)},ensureDirectory:async u=>{await i.ensureDirectory(u)},listFiles:async u=>await i.listFiles(u),readFile:async u=>await i.readFile(u),writeFile:async(u,p,m)=>{await i.writeFile(u,p,m)}});return await c.ensureCreated(),new e(s,c,n,i)}catch(a){throw await i.close(),a}}static async openSession(t,r){let n=await e.open(t);try{return await n.openSession(async()=>await r(n))}finally{await n.release()}}getSerialFragments(t){return this.#r.getSerial(t)}getSummaryFragments(t){return this.#r.getSummarySerial(t)}createContext(){return new Cc(this)}async createSerial(){let t=await this.serials.create();return this.#n.getStore()?.ownSerial(t),t}async clearSerialGraph(t){await this.deleteSummary(t),await Xy({database:this.#e,metadata:this.metadata,serialId:t}),await this.serials.setTopologyReady(t,!1),await this.serials.setKnowledgeGraphReady(t,!1),await this.serials.bumpRevision(t),await this.graphBuildParameters.deleteUnreferenced()}async clearSerialSource(t){await this.clearSerialGraph(t),await this.#r.getSerial(t).delete(),await this.serials.bumpRevision(t)}async deleteSerial(t){await this.#a(t)}async deleteSummary(t){await this.#r.getSummarySerial(t).delete()}async getSentence(t){return await this.#r.getSentence(t)}async openSession(t){if(this.#n.getStore()!==void 0)return await t(this);let n=new Cc(this);try{let i=await this.#e.transaction(async()=>await n.run(async()=>await t(this)));return n.complete(),i}finally{await n.dispose()}}async readDatabase(t){return await t(this.#e)}async readSearchIndexDatabase(t){return await Vy({documentPath:this.path,fileStore:this.#t,operation:t,readonly:!0})}async writeSearchIndexDatabase(t){return await Vy({documentPath:this.path,fileStore:this.#t,operation:t,readonly:!1})}async deleteSearchIndexDatabase(){await this.#t.deleteFile(yK(this.path,"fts.db"))}async peekNextSerialId(){return await this.serials.getMaxId()+1}async readBookMeta(){let t=await this.metadata.getMap("");return Object.keys(t).length===0?void 0:by.parse(t)}async readCover(){let t=await Jy({fileStore:this.#t,parse:n=>wK.parse(n),path:Qy(this.path)});if(t===void 0)return;let r=await lT(this.#t,zy(this.path));if(r===void 0)throw new Error("Cover data is missing");return{data:r,mediaType:t.mediaType,path:t.path}}async readSummary(t){return await this.#r.getSummarySerial(t).readText()}async readToc(){return await Jy({fileStore:this.#t,parse:t=>hc.parse(t),path:pp(this.path)})}async writeBookMeta(t){if(Object.keys(await this.metadata.getMap("")).length>0)throw new Error("Archive metadata already exists.");await this.replaceBookMeta(t)}async replaceBookMeta(t){await this.metadata.replaceMap({kind:1,objectPath:""},{...t})}async writeCover(t){await this.#t.ensureDirectory(lp(this.path)),await up({context:this.#n.getStore(),fileStore:this.#t,path:Qy(this.path),value:{mediaType:t.mediaType,path:t.path}}),await pT({content:t.data,context:this.#n.getStore(),fileStore:this.#t,path:zy(this.path)})}async writeSummary(t,r){await this.deleteSummary(t),await this.#r.getSummarySerial(t).writeTextStream(r),await this.serials.bumpRevision(t)}async writeToc(t){let r=kT(t);await up({context:this.#n.getStore(),fileStore:this.#t,path:pp(this.path),value:r}),await this.#i(r),await this.serials.bumpChaptersRevision()}async replaceToc(t){let r=kT(t);await up({context:this.#n.getStore(),fileStore:this.#t,options:{overwrite:!0},path:pp(this.path),value:r}),await this.#i(r),await this.serials.bumpChaptersRevision()}async flush(){await this.#e.flush()}async release(){try{await this.flush(),await this.#e.close()}finally{await this.#t.close()}}async close(){await this.release()}async runWithContext(t,r){return await this.#n.run(t,r)}async rollbackContext(t){await yT({context:t,deleteSerialResources:async r=>{await this.#a(r)},fileStore:this.#t})}async#i(t){await this.serials.setDocumentOrders(Yy(t.items).map((r,n)=>({documentOrder:n,serialId:r})))}async#a(t){await bT({database:this.#e,deleteSummary:async r=>{await this.deleteSummary(r)},graphBuildParameters:this.graphBuildParameters,metadata:this.metadata,serialId:t,serials:this.serials,textStreams:this.#r})}}});var xT=l(()=>{"use strict"});var tb=l(()=>{"use strict";IT();Ky();xT()});function Tc(e,t){let r=new Li(t);return new Proxy(e,{get(n,i,a){if(i==="getSerialFragments")return s=>r.getSerial(s);if(i==="getSummaryFragments")return s=>r.getSummarySerial(s);if(i==="getSentence")return async s=>await r.getSentence(s);let o=Reflect.get(n,i,a);return typeof o!="function"?o:o.bind(n)}})}var vT=l(()=>{"use strict";yy()});var X=l(()=>{"use strict";M();Jv();py();yy();tb();vT();xc();By();Gr()});var Rc,rb=l(()=>{"use strict";Rc=new Set(["registry","arc","path","tree","meta","index","chapter","chunk","entity","triple"])});import{isAbsolute as CT,relative as SK,resolve as Pc}from"path";import{homedir as kK}from"os";function B(e){let t=dt(e);if(t.protocol!=="wikg")throw new Error(po(e));let r=xK(t.path,t.fragment);if(r!==void 0)return r;let n=t.path,i=n.findIndex(s=>s.endsWith(cr));if(i<0)return{objectUri:jn(n.join("/"),nb(t.fragment))};let a=vK(n.slice(0,i+1)),o=n.slice(i+1).join("/");if(a==="")throw new Error(`Invalid Wiki Graph archive URI: ${e}`);return{archivePath:TK(a),...o===""?{}:{objectUri:jn(o,nb(t.fragment))}}}function dt(e){let t=e.indexOf("://");if(t<=0)throw new Error(po(e));let r=e.slice(0,t),n=e.slice(t+3),i=n.indexOf("#"),a=i<0?n:n.slice(0,i),o=i<0?void 0:n.slice(i+1);if(o?.includes("#")===!0)throw new Error(`Invalid Wiki Graph URI fragment: ${e}`);return{protocol:r,path:EK(a,e),...IK(o,e)}}function EK(e,t){let r=e.replace(/\/+$/u,"");if(r==="")return[];if(r.startsWith("/")){let i=r.slice(1);if(i==="")return["/"];let a=i.split("/");return TT(a,t),["/",...a]}let n=r.split("/");return TT(n,t),n}function TT(e,t){if(e.includes(""))throw new Error(`Invalid Wiki Graph URI path: empty path segment in ${t}`)}function IK(e,t){if(e===void 0)return{};if(e==="")throw new Error(`Invalid Wiki Graph URI fragment: ${t}`);let r=/^(?<begin>[0-9]+)\.\.(?<end>[0-9]+)$/u.exec(e);if(r?.groups!==void 0){let n=Number(r.groups.begin),i=Number(r.groups.end);return{fragment:{begin:n,end:i}}}if(/^[0-9]+$/u.test(e))return{fragment:Number(e)};throw new Error(`Invalid Wiki Graph URI fragment: ${t}`)}function xK(e,t){if(e[0]!=="lib")return;let r=e[1]==="arc"?1:e[2]==="arc"?2:-1;if(r<0)return;let n=r===2?e[1]:void 0,i=e[r+1];if(i===void 0||i.endsWith(cr)||CK(i))return;let a=`${ae}lib/${n===void 0?"":`${n}/`}arc/${i}`,o=e.slice(r+2).join("/");return{archivePath:a,...o===""?{}:{objectUri:jn(o,nb(t))}}}function vK(e){return e[0]==="/"?`/${e.slice(1).join("/")}`:e.join("/")}function nb(e){if(e!==void 0)return typeof e=="number"?String(e):`${e.begin}..${e.end}`}function CK(e){return Rc.has(e)}function TK(e){return e.startsWith("~/")?Pc(kK(),e.slice(2)):Pc(e)}function Le(e,t){let r=e.replace(/\\/gu,"/");return t===void 0||t===ae?`${ae}${r}`:`${ae}${r}/${PK(t)}`}function Ft(e,t,r=process.cwd()){return Le(RK(e,r),t)}function RK(e,t){let r=Pc(t),n=CT(e)?Pc(e):Pc(r,e),i=SK(r,n);return i!==""&&!i.startsWith("..")&&!CT(i)?i:n}function qr(e,t){return Le(e,jn(`chapter/${t}`))}function _c(e,t,r){return Le(e,jn(`chapter/${t}/${r}`))}function Ac(e,t){return Le(e,jn(`chapter/${t}/source`))}function jn(e,t){let r=e.replace(/^\/+/u,"").replace(/\/+$/u,"");return`${ae}${r}${t===void 0?"":`#${t}`}`}function Lc(e){let t=B(e);if(t.archivePath===void 0||t.objectUri!==void 0)throw new Error(`${po(e)} Expected a .wikg archive locator.`);return t.archivePath}function Wi(e){let t=B(e);if(t.archivePath===void 0)throw new Error(po(e));return{archivePath:t.archivePath,...t.objectUri===void 0?{}:{objectUri:t.objectUri}}}function po(e){let t=e.endsWith(cr)&&e.startsWith("/")?`${ae}${e}`:"wikg:///absolute/path/book.wikg";return[`Expected a Wiki Graph URI with a .wikg archive locator: ${e}`,`Example: ${t}`,"See: wg help uri"].join(`
|
|
1321
|
+
`)}function PK(e){let t=_K(e);if(t===void 0)throw new Error(`Expected a Wiki Graph object URI: ${e}`);return e.slice(t.length).replace(/^\/+/u,"")}function _K(e){if(e.startsWith(ae))return ae}var ae,cr,Dn=l(()=>{"use strict";rb();ae="wikg://",cr=".wikg"});import{randomUUID as AK}from"crypto";async function RT(e,t){let r=await PT({...e,wait:!0});if(r===void 0)throw new Error("State lock was not acquired.");try{return await t()}finally{await r()}}async function PT(e){let t=`${process.pid}-${AK()}`,r=e.pollMs??OK;for(;;){if(await DK(e,t))return MK(e,t);if(e.wait===!1)return;await BK(r)}}async function _T(e){let t=await mp(e.databasePath);try{return await AT(t,e),await t.queryOne(`
|
|
1322
|
+
SELECT 1 AS active
|
|
1323
|
+
FROM state_locks
|
|
1324
|
+
WHERE scope = ? AND resource_key = ?
|
|
1325
|
+
LIMIT 1
|
|
1326
|
+
`,[e.scope,e.resourceKey],()=>!0)===!0}finally{await t.close()}}async function DK(e,t){let r=await mp(e.databasePath),n=Date.now();try{return await r.transaction(async()=>(await AT(r,e),(await r.queryAll(`
|
|
1327
|
+
SELECT mode, owner_id, owner_pid, heartbeat_at
|
|
1328
|
+
FROM state_locks
|
|
1329
|
+
WHERE scope = ? AND resource_key = ?
|
|
1330
|
+
`,[e.scope,e.resourceKey],LT)).some(a=>a.ownerId!==t&&GK(e.mode,a.mode))?!1:(await r.run(`
|
|
1331
|
+
INSERT INTO state_locks (
|
|
1332
|
+
scope, resource_key, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
1333
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1334
|
+
`,[e.scope,e.resourceKey,e.mode,t,process.pid,n,n]),!0)))}finally{await r.close()}}function MK(e,t){let r=e.heartbeatMs??jK,n=setInterval(()=>{WK(e,t).catch(()=>{})},r);return n.unref?.(),async()=>{clearInterval(n);let i=await mp(e.databasePath);try{await i.run(`
|
|
1335
|
+
DELETE FROM state_locks
|
|
1336
|
+
WHERE scope = ? AND resource_key = ? AND owner_id = ?
|
|
1337
|
+
`,[e.scope,e.resourceKey,t])}finally{await i.close()}}}async function WK(e,t){let r=await mp(e.databasePath);try{await r.run(`
|
|
1338
|
+
UPDATE state_locks
|
|
1339
|
+
SET heartbeat_at = ?, owner_pid = ?
|
|
1340
|
+
WHERE scope = ? AND resource_key = ? AND owner_id = ?
|
|
1341
|
+
`,[Date.now(),process.pid,e.scope,e.resourceKey,t])}finally{await r.close()}}async function AT(e,t){let r=[],n=[];t.scope!==void 0&&(r.push("scope = ?"),n.push(t.scope)),t.resourceKey!==void 0&&(r.push("resource_key = ?"),n.push(t.resourceKey));let i=await e.queryAll(`
|
|
1342
|
+
SELECT scope, resource_key, mode, owner_id, owner_pid, heartbeat_at
|
|
1343
|
+
FROM state_locks
|
|
1344
|
+
${r.length===0?"":`WHERE ${r.join(" AND ")}`}
|
|
1345
|
+
`,n,a=>({lock:LT(a),resourceKey:k(a,"resource_key"),scope:k(a,"scope")}));for(let{lock:a,resourceKey:o,scope:s}of i)UK(a,t.staleMs)&&await e.run(`
|
|
1346
|
+
DELETE FROM state_locks
|
|
1347
|
+
WHERE scope = ?
|
|
1348
|
+
AND resource_key = ?
|
|
1349
|
+
AND owner_id = ?
|
|
1350
|
+
AND heartbeat_at = ?
|
|
1351
|
+
`,[s,o,a.ownerId,a.heartbeatAt])}async function mp(e){return await V(e,LK)}function LT(e){return{heartbeatAt:y(e,"heartbeat_at"),mode:FK(k(e,"mode")),ownerId:k(e,"owner_id"),ownerPid:y(e,"owner_pid")}}function FK(e){return e==="read"||e==="write"?e:"write"}function GK(e,t){return e==="write"||t==="write"}function UK(e,t=NK){return Date.now()-e.heartbeatAt>t&&!$K(e.ownerPid)}function $K(e){try{return process.kill(e,0),!0}catch(t){return!(R(t)&&t.code==="ESRCH")}}async function BK(e){await new Promise(t=>{setTimeout(t,e)})}var LK,OK,NK,jK,OT=l(()=>{"use strict";M();X();H();LK=`
|
|
1352
|
+
CREATE TABLE IF NOT EXISTS state_locks (
|
|
1353
|
+
scope TEXT NOT NULL,
|
|
1354
|
+
resource_key TEXT NOT NULL,
|
|
1355
|
+
mode TEXT NOT NULL,
|
|
1356
|
+
owner_id TEXT NOT NULL,
|
|
1357
|
+
owner_pid INTEGER NOT NULL,
|
|
1358
|
+
heartbeat_at INTEGER NOT NULL,
|
|
1359
|
+
created_at INTEGER NOT NULL,
|
|
1360
|
+
PRIMARY KEY (scope, resource_key, owner_id)
|
|
1361
|
+
);
|
|
1362
|
+
|
|
1363
|
+
CREATE INDEX IF NOT EXISTS idx_state_locks_resource
|
|
1364
|
+
ON state_locks(scope, resource_key);
|
|
1365
|
+
`,OK=100,NK=300*1e3,jK=15e3});async function Pe(e,t,r){return await RT(qK(e,t),r)}async function jT(e){return await _T({databasePath:Se(),resourceKey:DT(e),scope:NT,staleMs:ib})||await KK(e)}function qK(e,t){return{databasePath:Se(),mode:t,resourceKey:DT(e),scope:NT,staleMs:ib}}function DT(e){return String(e)}async function KK(e){let t=await JK();try{let r=await t.queryOne("SELECT owner_pid, heartbeat_at FROM library_locks WHERE library_id = ?",[e],n=>({heartbeatAt:y(n,"heartbeat_at"),ownerPid:y(n,"owner_pid")}));return r!==void 0&&QK(r)}finally{await t.close()}}async function JK(){let e=await V(Se(),HK);return await zK(e),e}async function zK(e){new Set(await e.queryAll("PRAGMA table_info(library_locks)",void 0,r=>k(r,"name"))).has("heartbeat_at")||await e.run("ALTER TABLE library_locks ADD COLUMN heartbeat_at INTEGER NOT NULL DEFAULT 0")}function QK(e){return Date.now()-e.heartbeatAt<=ib&&VK(e.ownerPid)}function VK(e){try{return process.kill(e,0),!0}catch(t){return!(R(t)&&t.code==="ESRCH")}}var HK,NT,ib,Oc=l(()=>{"use strict";M();X();Ue();OT();H();HK=`
|
|
1366
|
+
CREATE TABLE IF NOT EXISTS library_locks (
|
|
1367
|
+
library_id INTEGER PRIMARY KEY,
|
|
1368
|
+
mode TEXT NOT NULL,
|
|
1369
|
+
owner_id TEXT NOT NULL,
|
|
1370
|
+
owner_pid INTEGER NOT NULL,
|
|
1371
|
+
heartbeat_at INTEGER NOT NULL,
|
|
1372
|
+
created_at INTEGER NOT NULL
|
|
1373
|
+
);
|
|
1374
|
+
`,NT="library",ib=300*1e3});import{randomBytes as XK}from"crypto";import{constants as YK}from"fs";import{access as ZK,mkdir as WT,stat as FT}from"fs/promises";import{join as GT,resolve as UT}from"path";function ab(e){if(e===void 0)return!1;try{let t=dt(e);if(t.protocol!=="wikg"||t.path[0]!=="lib")return!1;if(t.path.some(n=>n.endsWith(".lib")))return!0;let r=de(e);return r!==void 0&&(r.kind!=="archive"||r.objectUri===void 0)}catch{return!1}}function de(e){if(!e.includes("://"))return;let t=dt(e);if(t.protocol!=="wikg"||t.path[0]!=="lib")return;let r=t.path.slice(1),n=iJ(t.fragment);if(r.length===0)return Gt(e,n),{isDefault:!0,kind:"scope"};if(r.some(d=>d.endsWith(cr)))return;if(r.some(d=>d.endsWith(".lib")))throw new Error(`Invalid Wiki Graph library URI: ${e}. Use wikg://lib/<lib-id> and wikg://lib/<lib-id>/arc/<arc-id>; .lib suffixes are no longer supported.`);let[i,a,o,...s]=r;if(i==="registry"&&a===void 0)return Gt(e,n),{isDefault:!0,kind:"registry"};if(i==="meta"&&a===void 0)return Gt(e,n),{isDefault:!0,kind:"metadata"};if(i==="path"&&a===void 0)return Gt(e,n),{isDefault:!0,kind:"path"};if(/^(index|chapter|chunk|entity|triple)$/u.exec(i??"")?.[1]!==void 0)return{isDefault:!0,kind:"scope",objectUri:hp([i,a,o,...s].filter(Boolean).join("/"),n)};if(i==="arc")return MT(e,void 0,r.slice(1),n);if(i===void 0||Rc.has(i))throw new Error(`Invalid Wiki Graph library URI: ${e}. Reserved library URI segment: ${i??""}.`);if(a===void 0)return Gt(e,n),{isDefault:!1,kind:"scope",publicId:i};if(a==="meta"&&o===void 0)return Gt(e,n),{isDefault:!1,kind:"metadata",publicId:i};if(a==="path"&&o===void 0)return Gt(e,n),{isDefault:!1,kind:"path",publicId:i};if(/^(index|chapter|chunk|entity|triple)$/u.test(a))return{isDefault:!1,kind:"scope",objectUri:hp([a,o,...s].filter(Boolean).join("/"),n),publicId:i};if(a==="arc")return MT(e,i,r.slice(2),n);throw new Error(`Invalid Wiki Graph library URI: ${e}. Expected wikg://lib, wikg://lib/registry, wikg://lib/<lib-id>, or <lib-scope>/arc/<arc-id>.`)}function MT(e,t,r,n){let i=t===void 0,[a,o,...s]=r,c={isDefault:i,...t===void 0?{}:{publicId:t}};if(a===void 0)return Gt(e,n),{...c,kind:"archive-collection"};if(/^(chapter|chunk|entity|triple)$/u.test(a))return{...c,kind:"scope",objectUri:hp([a,o,...s].filter(Boolean).join("/"),n)};if(a==="tree"&&o===void 0)return Gt(e,n),{...c,kind:"archive-tree"};if(Rc.has(a))throw new Error(`Invalid Wiki Graph library URI: ${e}. Reserved archive member URI segment: ${a}.`);return o===void 0?(Gt(e,n),{...c,archivePublicId:a,kind:"archive"}):o==="path"&&s.length===0?(Gt(e,n),{...c,archivePublicId:a,kind:"archive-path"}):{...c,archivePublicId:a,kind:"archive",objectUri:hp([o,...s].join("/"),n)}}function Gt(e,t){if(t!==void 0)throw new Error(`Invalid Wiki Graph library URI: ${e}. This library URI target does not support fragments.`)}function hp(e,t){return`wikg://${e.replace(/^\/+|\/+$/gu,"")}${t===void 0?"":`#${t}`}`}function iJ(e){if(e!==void 0)return typeof e=="number"?String(e):`${e.begin}..${e.end}`}function fp(e){return e===void 0?"wikg://lib":`wikg://lib/${e}`}function $T(){return GT(ct(),eJ)}function BT(e){return GT(Ve(),"library",String(e))}async function ob(){return await vt(async e=>{let t=await e.transaction(async()=>{let r=await qT(e);if(r!==void 0)return r;let n=new Date().toISOString(),i=await QT(e),a=$T();return await e.run(`
|
|
1375
|
+
INSERT INTO libraries (public_id, folder_path, is_default, created_at, updated_at)
|
|
1376
|
+
VALUES (?, ?, 1, ?, ?)
|
|
1377
|
+
`,[i,a,n,n]),await aJ(e)});return await WT(t.folderPath,{recursive:!0}),t})}async function sb(e){let t=UT(e.folderPath);if(await oJ(t))throw new Error(`Library folder already exists: ${t}`);return await vt(async r=>await r.transaction(async()=>{let n=new Date().toISOString(),i=await QT(r);return await r.run(`
|
|
1378
|
+
INSERT INTO libraries (public_id, folder_path, is_default, created_at, updated_at)
|
|
1379
|
+
VALUES (?, ?, 0, ?, ?)
|
|
1380
|
+
`,[i,t,n,n]),await WT(t),await KT(r,i)}))}async function cb(){return await ob(),await vt(async e=>await e.queryAll(`
|
|
1381
|
+
SELECT id, public_id, folder_path, is_default, created_at, updated_at
|
|
1382
|
+
FROM libraries
|
|
1383
|
+
ORDER BY is_default DESC, public_id
|
|
1384
|
+
`,void 0,gp))}async function U(e){if(e.isDefault)return await ob();if(e.publicId===void 0)throw new Error("Missing library id.");return await vt(async t=>await KT(t,e.publicId))}async function db(e){return await vt(async t=>await JT(t,e))}async function ub(e){let t=await U(e);if(t.isDefault)throw new Error("The default library is managed by the system and cannot be removed.");return await Pe(t.id,"write",async()=>{await vt(async r=>{await r.transaction(async()=>{await r.run("DELETE FROM library_metadata WHERE library_id = ?",[t.id]),await r.run("DELETE FROM libraries WHERE id = ?",[t.id])})})}),t}async function HT(e,t){let r=UT(t),n;try{n=await FT(r)}catch(i){throw R(i)&&i.code==="ENOENT"?new Error(`Library path set does not exist: ${r}`):new Error(`Library path set is not accessible: ${r}: ${i instanceof Error?i.message:String(i)}`)}if(!n.isDirectory())throw new Error(`Library path set must be an existing directory: ${r}`);try{await ZK(r,YK.R_OK)}catch(i){throw new Error(`Library path set is not accessible: ${r}: ${i instanceof Error?i.message:String(i)}`)}return await vt(async i=>await i.transaction(async()=>{let a=await i.queryOne("SELECT id FROM libraries WHERE folder_path = ?",[r],o=>y(o,"id"));if(a!==void 0&&a!==e.id)throw new Error(`Library path set is already bound to another library: ${r}`);return await i.run("UPDATE libraries SET folder_path = ?, updated_at = ? WHERE id = ?",[r,new Date().toISOString(),e.id]),await JT(i,e.id)}))}async function lb(e){let t=await U(e);return await vt(async r=>await Nc(r,t.id))}async function pb(e,t){VT(Object.keys(t));let r=await U(e);return await vt(async n=>(await n.transaction(async()=>{await n.run("DELETE FROM library_metadata WHERE library_id = ?",[r.id]);for(let[i,a]of Object.entries(t))await zT(n,r.id,i,a)}),await Nc(n,r.id)))}async function mb(e,t,r){VT([t]);let n=await U(e);return await vt(async i=>(await zT(i,n.id,t,r),await Nc(i,n.id)))}async function hb(e,t){let r=await U(e);return await vt(async n=>(await n.run("DELETE FROM library_metadata WHERE library_id = ? AND key = ?",[r.id,t]),await Nc(n,r.id)))}async function fb(e){let t=await U(e);return await vt(async r=>(await r.run("DELETE FROM library_metadata WHERE library_id = ?",[t.id]),await Nc(r,t.id)))}async function vt(e){let t=await V(Se(),nJ);try{return await e(t)}finally{await t.close()}}async function qT(e){return await e.queryOne(`
|
|
1385
|
+
SELECT id, public_id, folder_path, is_default, created_at, updated_at
|
|
1386
|
+
FROM libraries
|
|
1387
|
+
WHERE is_default = 1
|
|
1388
|
+
`,void 0,gp)}async function aJ(e){let t=await qT(e);if(t===void 0)throw new Error("Default library registry record is missing.");return t}async function KT(e,t){let r=await e.queryOne(`
|
|
1389
|
+
SELECT id, public_id, folder_path, is_default, created_at, updated_at
|
|
1390
|
+
FROM libraries
|
|
1391
|
+
WHERE public_id = ?
|
|
1392
|
+
`,[t],gp);if(r===void 0)throw new Error(`Unknown Wiki Graph library: ${t}`);return r}async function JT(e,t){let r=await e.queryOne(`
|
|
1393
|
+
SELECT id, public_id, folder_path, is_default, created_at, updated_at
|
|
1394
|
+
FROM libraries
|
|
1395
|
+
WHERE id = ?
|
|
1396
|
+
`,[t],gp);if(r===void 0)throw new Error(`Unknown Wiki Graph library: ${t}`);return r}function gp(e){let t=y(e,"id"),r=k(e,"public_id");return{createdAt:k(e,"created_at"),folderPath:k(e,"folder_path"),id:t,isDefault:y(e,"is_default")===1,publicId:r,stagingPath:BT(t),updatedAt:k(e,"updated_at"),uri:fp(y(e,"is_default")===1?void 0:r)}}async function Nc(e,t){let r=await e.queryAll(`
|
|
1397
|
+
SELECT key, value_json
|
|
1398
|
+
FROM library_metadata
|
|
1399
|
+
WHERE library_id = ?
|
|
1400
|
+
ORDER BY key
|
|
1401
|
+
`,[t],i=>({key:k(i,"key"),value:JSON.parse(k(i,"value_json"))})),n={};for(let i of r)n[i.key]=i.value;return n}async function zT(e,t,r,n){await e.run(`
|
|
1402
|
+
INSERT INTO library_metadata (library_id, key, value_json, updated_at)
|
|
1403
|
+
VALUES (?, ?, ?, ?)
|
|
1404
|
+
ON CONFLICT(library_id, key) DO UPDATE SET
|
|
1405
|
+
value_json = excluded.value_json,
|
|
1406
|
+
updated_at = excluded.updated_at
|
|
1407
|
+
`,[t,r,JSON.stringify(n),new Date().toISOString()])}async function QT(e){for(let t=0;t<20;t+=1){let r=XK(tJ).toString("hex");if(await e.queryOne("SELECT public_id FROM libraries WHERE public_id = ?",[r],i=>k(i,"public_id"))===void 0)return r}throw new Error("Could not generate a unique library id.")}async function oJ(e){try{return await FT(e),!0}catch(t){if(R(t)&&t.code==="ENOENT")return!1;throw t}}function VT(e){let t=e.find(r=>rJ.has(r));if(t!==void 0)throw new Error(`Library metadata cannot modify system field: ${t}`)}var eJ,tJ,rJ,nJ,mo=l(()=>{"use strict";M();X();Ue();Dn();H();Oc();rb();eJ="default-library",tJ=6,rJ=new Set(["id","public_id","publicId","folder_path","folderPath","is_default","isDefault","staging_path","stagingPath","created_at","createdAt","updated_at","updatedAt","uri"]),nJ=`
|
|
1408
|
+
PRAGMA journal_mode = WAL;
|
|
1409
|
+
|
|
1410
|
+
CREATE TABLE IF NOT EXISTS libraries (
|
|
1411
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1412
|
+
public_id TEXT NOT NULL UNIQUE,
|
|
1413
|
+
folder_path TEXT NOT NULL UNIQUE,
|
|
1414
|
+
is_default INTEGER NOT NULL DEFAULT 0 CHECK (is_default IN (0, 1)),
|
|
1415
|
+
created_at TEXT NOT NULL,
|
|
1416
|
+
updated_at TEXT NOT NULL
|
|
1417
|
+
);
|
|
1418
|
+
|
|
1419
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_libraries_single_default
|
|
1420
|
+
ON libraries(is_default)
|
|
1421
|
+
WHERE is_default = 1;
|
|
1422
|
+
|
|
1423
|
+
CREATE TABLE IF NOT EXISTS library_metadata (
|
|
1424
|
+
library_id INTEGER NOT NULL,
|
|
1425
|
+
key TEXT NOT NULL,
|
|
1426
|
+
value_json TEXT NOT NULL,
|
|
1427
|
+
updated_at TEXT NOT NULL,
|
|
1428
|
+
PRIMARY KEY (library_id, key)
|
|
1429
|
+
);
|
|
1430
|
+
|
|
1431
|
+
CREATE INDEX IF NOT EXISTS idx_library_metadata_library
|
|
1432
|
+
ON library_metadata(library_id);
|
|
1433
|
+
`});var Oe,Be,Fi,XT,Mn=l(()=>{"use strict";Oe=".wikg-mutation-token",Be="manifest.json",Fi="fts.db",XT=[/^\.wikg-mutation-token$/u,/^manifest\.json$/u,/^database\.db$/u,/^fts\.db$/u,/^toc\.json$/u,/^cover\/(?:data\.bin|info\.json)$/u,/^texts\/(?:source|summary)\/\d+\.txt$/u]});import{posix as sJ,resolve as cJ,sep as YT}from"path";function _e(e){let t=e.replaceAll("\\","/").trim(),r=t.startsWith("/")?t.slice(1):t;return sJ.normalize(r).replace(/^(\.\/)+/u,"").replace(/^\/+/u,"")}function Kr(e){return XT.some(t=>t.test(e))}function ZT(e,t,r){let n=cJ(e),i=n.endsWith(YT)?n:`${n}${YT}`;if(!(t===n||t.startsWith(i)))throw new Error(`Invalid archive entry path: ${r}`)}function eR(e){return[...e].sort((t,r)=>rR(t.archivePath,r.archivePath))}function tR(e){return[...e].sort(rR)}function rR(e,t){return e===Oe?t===Oe?0:-1:t===Oe?1:e.localeCompare(t)}var Gi=l(()=>{"use strict";Mn()});import{randomBytes as dJ}from"crypto";import{z as gb}from"zod";function ho(e){let t;try{t=JSON.parse(e)}catch{throw new Error(`Invalid WIKG manifest: ${Be}.`)}let r=lJ.safeParse(t);if(!r.success)throw new Error(`Unsupported WIKG format version in ${Be}.`);return{formatVersion:r.data.formatVersion,schemaVersion:r.data.schemaVersion??1}}function wb(){let e=dJ(32).toString("base64url");return Buffer.from(`${nR}
|
|
1434
|
+
${e}
|
|
1435
|
+
`,"utf8")}function bp(e){let t=e.split(/\r?\n/u),r=t[0],n=t[1];if(r!==nR||n===void 0||!uJ.test(n))throw new Error(`Invalid WIKG mutation token: ${Oe}.`);return n}var yp,yb,bb,nR,uJ,lJ,fo=l(()=>{"use strict";Mn();yp=1,yb=2,bb=`${JSON.stringify({formatVersion:yp,schemaVersion:yb})}
|
|
1436
|
+
`,nR="wikg-mutation-token:v1",uJ=/^[A-Za-z0-9_-]{43}$/u,lJ=gb.object({formatVersion:gb.literal(yp),schemaVersion:gb.number().int().positive().optional()})});import{open as pJ}from"fs/promises";import{inflateRaw as mJ}from"zlib";import{open as hJ}from"yauzl";async function Jr(e){let t=await yJ(e);try{let r=await fJ(t);return await bJ(e,r),{entries:r,zipFile:t}}catch(r){throw t.close(),r}}async function fJ(e){return await new Promise((t,r)=>{let n=[];e.on("entry",i=>{if(i.fileName.endsWith("/")){e.readEntry();return}n.push(i),e.readEntry()}),e.once("end",()=>{t(n)}),e.once("error",i=>{r(i)}),e.readEntry()})}async function jc(e,t){return(await gJ(e,t)).toString("utf8")}async function gJ(e,t){let r=await pJ(e,"r");try{return await Wn(r,t)}finally{await r.close()}}async function Wn(e,t){let r=await SJ(e,t);if(t.compressionMethod===0)return r;if(t.compressionMethod===8)return await kJ(r);throw new Error(`Unsupported ZIP compression method: ${t.fileName}`)}async function yJ(e){return await new Promise((t,r)=>{hJ(e,{autoClose:!1,lazyEntries:!0},(n,i)=>{if(n!==null||i===void 0){r(n??new Error(`Cannot open archive: ${e}`));return}t(i)})})}async function bJ(e,t){await wJ(e,t);let r=t.find(n=>_e(n.fileName)===Be);if(r===void 0)throw new Error(`Missing WIKG manifest: ${Be}.`);ho(await jc(e,r))}async function wJ(e,t){if(_e(t[0]?.fileName??"")!==Oe)throw new Error(`Missing WIKG mutation token: ${Oe}.`);bp(await jc(e,t[0]))}async function SJ(e,t){let r=Buffer.alloc(30);if(await e.read(r,0,r.length,t.relativeOffsetOfLocalHeader),r.readUInt32LE(0)!==67324752)throw new Error(`Invalid ZIP local file header: ${t.fileName}`);let n=r.readUInt16LE(26),i=r.readUInt16LE(28),a=t.relativeOffsetOfLocalHeader+30+n+i,o=Buffer.alloc(t.compressedSize);return await e.read(o,0,o.length,a),o}async function kJ(e){return await new Promise((t,r)=>{mJ(e,(n,i)=>{if(n!==null){r(n);return}t(i)})})}var Dc=l(()=>{"use strict";Mn();fo();Gi()});import{mkdir as EJ,open as IJ,writeFile as xJ}from"fs/promises";import{dirname as vJ,resolve as CJ}from"path";async function Mc(e,t){let{entries:r,zipFile:n}=await Jr(e),i=await IJ(e,"r");try{for(let a of r){let o=_e(a.fileName);if(o==="")throw new Error(`Invalid archive entry path: ${a.fileName}`);if(!Kr(o))continue;let s=CJ(t,o);ZT(t,s,o),await EJ(vJ(s),{recursive:!0}),await xJ(s,await Wn(i,a))}}finally{await i.close(),n.close()}}var iR=l(()=>{"use strict";Gi();Dc()});import{readdir as TJ}from"fs/promises";import{join as aR,posix as RJ,relative as PJ,sep as _J}from"path";async function Sb(e,t=e){let r=await TJ(t,{withFileTypes:!0}),n=[];for(let i of[...r].sort(AJ)){let a=aR(t,i.name);if(i.isDirectory()){n.push(...await Sb(e,a));continue}i.isFile()&&n.push({absolutePath:a,archivePath:PJ(e,a).split(_J).join(RJ.sep)})}return n.filter(i=>Kr(i.archivePath))}async function oR(e){let t=await $.open(aR(e,"database.db"),"",{readonly:!0}).catch(()=>{});if(t===void 0)return!1;try{return await t.queryOne(`
|
|
1437
|
+
SELECT fts_embedded
|
|
1438
|
+
FROM archive_index_settings
|
|
1439
|
+
WHERE id = 1
|
|
1440
|
+
`,void 0,n=>Number(n.fts_embedded)!==0)??!1}catch{return!1}finally{await t.close()}}function sR(e){return e.archivePath==="manifest.json"||e.archivePath===".wikg-mutation-token"?!1:e.archivePath!==Fi||e.includeSearchIndex}function AJ(e,t){return e.name.localeCompare(t.name)}var cR=l(()=>{"use strict";M();Mn();Gi()});import{createWriteStream as LJ}from"fs";import{mkdir as uR,open as OJ}from"fs/promises";import{dirname as lR}from"path";import{finished as dR}from"stream/promises";import{ZipFile as pR}from"yazl";async function zr(e,t){await uR(lR(t),{recursive:!0});let r=new pR,n=await Sb(e),i=await oR(e),a=eR([{archivePath:Oe,content:wb()},{archivePath:Be,content:Buffer.from(bb,"utf8")},...n.filter(o=>sR({archivePath:o.archivePath,includeSearchIndex:i}))]);for(let o of a)"content"in o?r.addBuffer(o.content,o.archivePath,{compress:!1}):r.addFile(o.absolutePath,o.archivePath,{compress:!1});r.end(),await mR(r,t)}async function go(e,t,r,n={}){await uR(lR(t),{recursive:!0});let{entries:i,zipFile:a}=await Jr(e),o=new Map(r.map(u=>[_e(u.entryPath),u])),s=new Set;for(let u of i){let p=_e(u.fileName);p!==""&&Kr(p)&&s.add(p)}for(let u of o.values()){let p=_e(u.entryPath);p!==""&&Kr(p)&&s.add(p)}s.add(Oe),s.add(Be);let c=new pR,d=await OJ(e,"r");try{for(let u of tR(s)){let p=o.get(u);if(u===Oe){if(n.preserveMutationToken===!0){let f=i.find(w=>_e(w.fileName)===u);if(f!==void 0){c.addBuffer(await Wn(d,f),u,{compress:!1});continue}}c.addBuffer(wb(),u,{compress:!1});continue}if(u===Be){c.addBuffer(Buffer.from(bb,"utf8"),u,{compress:!1});continue}if(p?.kind==="deleted")continue;if(p?.kind==="file"){c.addFile(p.workspacePath,u,{compress:!1});continue}let m=i.find(f=>_e(f.fileName)===u);m!==void 0&&c.addBuffer(await Wn(d,m),u,{compress:!1})}}finally{await d.close(),a.close()}c.end(),await mR(c,t)}async function mR(e,t){let r=LJ(t),n=dR(r),i=dR(e.outputStream);e.outputStream.pipe(r),await Promise.all([n,i])}var kb=l(()=>{"use strict";Mn();cR();fo();Gi();Dc()});import{createHash as NJ,randomUUID as jJ}from"crypto";import{stat as hR}from"fs/promises";import{resolve as DJ}from"path";function Ui(e){return NJ("sha256").update(DJ(e)).digest("hex")}async function $i(e){let t=await hR(e);return`${t.size}:${t.mtimeMs}`}function Bi(){return`${process.pid}-${jJ()}`}async function wp(e){await new Promise(t=>{setTimeout(t,e)})}async function Fn(e){try{return await hR(e),!0}catch(t){if(R(t)&&t.code==="ENOENT")return!1;throw t}}var Qr=l(()=>{"use strict";H()});import{randomUUID as MJ}from"crypto";import{mkdtemp as WJ,rename as FJ,rm as GJ,stat as UJ,writeFile as $J}from"fs/promises";import{tmpdir as BJ}from"os";import{dirname as HJ,join as Gn,resolve as Eb}from"path";async function xb(e){let t=await Vr(e);if(t>Hi)throw new Error(`Unsupported Wiki Graph archive schema version: ${t}.`);if(t<Hi)throw new Error(`This Wiki Graph archive uses schema v${t} and must be upgraded before use.
|
|
1441
|
+
Run: wg maintenance upgrade ${e}`)}async function Vr(e){let{entries:t,zipFile:r}=await Jr(Eb(e));try{let n=t.find(i=>_e(i.fileName)===Be);if(n===void 0)throw new Error(`Missing WIKG manifest: ${Be}.`);return ho(await jc(Eb(e),n)).schemaVersion}finally{r.close()}}async function kp(e){let t=Eb(e),r=await Vr(t);if(r===Hi)return;if(r>Hi)throw new Error(`Unsupported Wiki Graph archive schema version: ${r}.`);await ke();let n=Ui(t);await zJ(n);let i=Gn(HJ(t),`.${YJ(t)}.${process.pid}.${MJ()}.upgrade.tmp`),a=[];try{let o=await KJ(t,a);await go(t,i,[{entryPath:Fi,kind:"deleted"},...o===void 0?[]:[o]],{preserveMutationToken:!0}),await FJ(i,t),await JJ(n)}finally{await Promise.all(a.map(async o=>{await Sp(o)}))}}async function KJ(e,t){let{entries:r,zipFile:n}=await Jr(e);try{let i=r.find(u=>_e(u.fileName)==="toc.json");if(i===void 0)return;let a=await jc(e,i),o;try{let u=hc.safeParse(JSON.parse(a));if(!u.success)return;o=u.data}catch{return}let s=JSON.parse(JSON.stringify(o));if(!Za(s.items))return;let c=await WJ(Gn(BJ(),"wikigraph-toc-upgrade-"));t.push(c);let d=Gn(c,"toc.json");return await $J(d,`${JSON.stringify(s,null,2)}
|
|
1442
|
+
`,"utf8"),{entryPath:"toc.json",kind:"file",workspacePath:d}}finally{n.close()}}async function JJ(e){let t=Gn(ct(),"cache");await Sp(Gn(t,"search-sessions.sqlite")),await Sp(Gn(t,"continuation-cursors.sqlite"));let r=Gn(Ve(),"staging.sqlite");await fR(r)&&await QJ(r,e)}async function zJ(e){let t=Gn(Ve(),"staging.sqlite");if(!await fR(t))return;let r=await $.open(t,"",{readonly:!0});try{for(let n of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){if(!await Ib(r,n))continue;if((await r.queryAll(`
|
|
1443
|
+
SELECT owner_pid, heartbeat_at
|
|
1444
|
+
FROM ${n}
|
|
1445
|
+
WHERE archive_key = ?
|
|
1446
|
+
`,[e],a=>({heartbeatAt:Number(a.heartbeat_at),ownerPid:Number(a.owner_pid)}))).some(a=>VJ(a.ownerPid,a.heartbeatAt)))throw new Error(`Cannot upgrade archive with active coordinator state: ${e}.`)}if(await Ib(r,"entry_overlays")&&(await r.queryAll(`
|
|
1447
|
+
SELECT entry_path
|
|
1448
|
+
FROM entry_overlays
|
|
1449
|
+
WHERE archive_key = ?
|
|
1450
|
+
`,[e],a=>String(a.entry_path))).find(a=>a!==Fi)!==void 0)throw new Error(`Cannot upgrade archive with non-derived overlay state: ${e}.`)}finally{await r.close()}}async function QJ(e,t){let r=await $.open(e);try{if(!await Ib(r,"entry_overlays"))return;let n=t===void 0?"WHERE entry_path = ?":"WHERE archive_key = ? AND entry_path = ?",i=t===void 0?[Fi]:[t,Fi],a=await r.queryAll(`
|
|
1451
|
+
SELECT archive_key, workspace_path
|
|
1452
|
+
FROM entry_overlays
|
|
1453
|
+
${n}
|
|
1454
|
+
`,i,o=>({archiveKey:String(o.archive_key),workspacePath:o.workspace_path===null?void 0:String(o.workspace_path)}));for(let o of a)o.workspacePath!==void 0&&await Sp(o.workspacePath);await r.run(`
|
|
1455
|
+
DELETE FROM entry_overlays
|
|
1456
|
+
${n}
|
|
1457
|
+
`,i)}finally{await r.close()}}async function Ib(e,t){return await e.queryOne(`
|
|
1458
|
+
SELECT 1 AS present
|
|
1459
|
+
FROM sqlite_master
|
|
1460
|
+
WHERE type = 'table' AND name = ?
|
|
1461
|
+
`,[t],()=>!0)===!0}function VJ(e,t){return Date.now()-t<=qJ&&XJ(e)}function XJ(e){try{return process.kill(e,0),!0}catch{return!1}}async function Sp(e){await GJ(e,{force:!0,recursive:!0})}async function fR(e){try{return await UJ(e),!0}catch{return!1}}function YJ(e){return e.split(/[\\/]/u).pop()??"archive.wikg"}var Hi,qJ,Ep=l(()=>{"use strict";M();Ic();Ai();Ue();Mn();fo();Gi();Dc();kb();Qr();Zl();Ai();Hi=2,qJ=300*1e3});import{open as ZJ,readFile as ez}from"fs/promises";import{join as tz}from"path";async function Ip(e){let t=await Xr.open(e);try{return t.listEntries()}finally{t.close()}}async function yo(e,t){let r=await Xr.open(e);try{return await r.readEntry(t)}finally{r.close()}}async function Un(e){let t=await Xr.open(e);try{let r=await t.readEntry(Oe);if(r===void 0)throw new Error(`Missing WIKG mutation token: ${Oe}.`);return bp(r.toString("utf8"))}finally{t.close()}}async function Wc(e){return ho(await ez(tz(e,Be),"utf8")).formatVersion}var Xr,gR=l(()=>{"use strict";Mn();fo();Gi();Dc();Ep();Xr=class e{#e;#t;#r;#n;#i;constructor(t,r,n){this.#n=t,this.#i=r,this.#e=new Map(n.map(i=>[_e(i.fileName),i]).filter(([i])=>i!=="").filter(([i])=>Kr(i))),this.#t=[...this.#e.keys()].sort((i,a)=>i.localeCompare(a))}static async open(t){await xb(t);let{entries:r,zipFile:n}=await Jr(t);return new e(t,n,r)}close(){this.#i.close(),this.#r!==void 0&&(this.#r.then(async t=>{await t.close()}),this.#r=void 0)}listEntries(){return this.#t}async readEntry(t){let r=this.#e.get(_e(t));if(r!==void 0)return await Wn(await this.#a(),r)}async#a(){return this.#r??=ZJ(this.#n,"r"),await this.#r}}});var $n=l(()=>{"use strict";iR();gR();fo();kb()});var kR=l(()=>{"use strict";Dn()});import{mkdir as nz,mkdtemp as iz}from"fs/promises";import{join as ER}from"path";function bo(){return Ka()}function vb(e){let t=bo();return e===void 0?t:ER(t,e)}async function Ye(e){let t=vb(e);return await nz(t,{recursive:!0}),await iz(ER(t,`${e}-`))}var Yr=l(()=>{"use strict";Ue()});var Zr,Z,Cb,dr=l(()=>{"use strict";Zr="database.db",Z="fts.db",Cb=Symbol("WikgArchiveSession constructor token")});import{join as az}from"path";async function Ne(e){let t=Date.now(),r=new Set;for(let n of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){let i=await e.queryAll(`
|
|
1462
|
+
SELECT DISTINCT owner_pid
|
|
1463
|
+
FROM ${n}
|
|
1464
|
+
WHERE owner_pid IS NOT NULL
|
|
1465
|
+
AND heartbeat_at <= ?
|
|
1466
|
+
`,[t-6e4],a=>ur(a,"owner_pid"));for(let a of i)cz(a)||r.add(a)}r.size!==0&&(await e.run(`
|
|
1467
|
+
DELETE FROM archive_owners
|
|
1468
|
+
WHERE owner_pid IN (${qi(r.size)})
|
|
1469
|
+
`,[...r]),await e.run(`
|
|
1470
|
+
DELETE FROM entry_locks
|
|
1471
|
+
WHERE owner_pid IN (${qi(r.size)})
|
|
1472
|
+
`,[...r]),await e.run(`
|
|
1473
|
+
DELETE FROM entry_sqlite_leases
|
|
1474
|
+
WHERE owner_pid IN (${qi(r.size)})
|
|
1475
|
+
`,[...r]),await e.run(`
|
|
1476
|
+
DELETE FROM archive_commit_locks
|
|
1477
|
+
WHERE owner_pid IN (${qi(r.size)})
|
|
1478
|
+
`,[...r]))}function qi(e){return Array.from({length:e},()=>"?").join(", ")}function cz(e){try{return process.kill(e,0),!0}catch(t){return!(t instanceof Error&&"code"in t&&t.code==="ESRCH")}}async function K(e){return await sz.use(async()=>{let t=await dz();try{return await e(t)}finally{await t.close()}})}async function dz(){let e=await V(az(Rb(),"staging.sqlite"),oz);return await uz(e),e}async function uz(e){let t=await e.queryAll("PRAGMA table_info(entry_overlays)",void 0,n=>He(n,"name"));t.includes("archive_signature")||await e.run(`
|
|
1479
|
+
ALTER TABLE entry_overlays
|
|
1480
|
+
ADD COLUMN archive_signature TEXT
|
|
1481
|
+
`),t.includes("mutation_token")||await e.run(`
|
|
1482
|
+
ALTER TABLE entry_overlays
|
|
1483
|
+
ADD COLUMN mutation_token TEXT
|
|
1484
|
+
`),(await e.queryAll("PRAGMA table_info(entry_sqlite_leases)",void 0,n=>He(n,"name"))).includes("mode")||await e.run(`
|
|
1485
|
+
ALTER TABLE entry_sqlite_leases
|
|
1486
|
+
ADD COLUMN mode TEXT NOT NULL DEFAULT 'read'
|
|
1487
|
+
`)}function Rb(){return Ve()}function Bn(e){let t=Tb(e,"workspace_path"),r=Tb(e,"archive_signature"),n=Tb(e,"mutation_token");return{archiveKey:He(e,"archive_key"),archivePath:He(e,"archive_path"),...r===void 0?{}:{archiveSignature:r},entryPath:He(e,"entry_path"),kind:pz(e),...n===void 0?{}:{mutationToken:n},updatedAt:ur(e,"updated_at"),...t===void 0?{}:{workspacePath:t}}}function IR(e){return{entryPath:He(e,"entry_path"),mode:lz(e),ownerId:He(e,"owner_id")}}function xR(e){return{ownerId:He(e,"owner_id")}}function lz(e){let t=He(e,"mode");if(t==="read"||t==="state"||t==="write")return t;throw new Error(`Unsupported entry lock mode: ${t}.`)}function pz(e){let t=He(e,"kind");if(t==="deleted"||t==="file")return t;throw new Error(`Unsupported entry overlay kind: ${t}.`)}function He(e,t){let r=e[t];if(typeof r!="string")throw new TypeError(`Expected ${t} to be a string`);return r}function ur(e,t){let r=e[t];if(typeof r!="number")throw new TypeError(`Expected ${t} to be a number`);return r}function Tb(e,t){let r=e[t];if(r!=null){if(typeof r!="string")throw new TypeError(`Expected ${t} to be a string`);return r}}var oz,sz,en=l(()=>{"use strict";Ue();X();nc();dr();oz=`
|
|
1488
|
+
CREATE TABLE IF NOT EXISTS entry_overlays (
|
|
1489
|
+
archive_key TEXT NOT NULL,
|
|
1490
|
+
archive_path TEXT NOT NULL,
|
|
1491
|
+
entry_path TEXT NOT NULL,
|
|
1492
|
+
kind TEXT NOT NULL,
|
|
1493
|
+
workspace_path TEXT,
|
|
1494
|
+
archive_signature TEXT,
|
|
1495
|
+
mutation_token TEXT,
|
|
1496
|
+
updated_at INTEGER NOT NULL,
|
|
1497
|
+
PRIMARY KEY (archive_key, entry_path)
|
|
1498
|
+
);
|
|
1499
|
+
|
|
1500
|
+
CREATE TABLE IF NOT EXISTS entry_locks (
|
|
1501
|
+
archive_key TEXT NOT NULL,
|
|
1502
|
+
entry_path TEXT NOT NULL,
|
|
1503
|
+
mode TEXT NOT NULL,
|
|
1504
|
+
owner_id TEXT NOT NULL,
|
|
1505
|
+
owner_pid INTEGER NOT NULL,
|
|
1506
|
+
heartbeat_at INTEGER NOT NULL,
|
|
1507
|
+
created_at INTEGER NOT NULL,
|
|
1508
|
+
PRIMARY KEY (archive_key, entry_path, owner_id)
|
|
1509
|
+
);
|
|
1510
|
+
|
|
1511
|
+
CREATE TABLE IF NOT EXISTS archive_owners (
|
|
1512
|
+
archive_key TEXT NOT NULL,
|
|
1513
|
+
owner_id TEXT NOT NULL,
|
|
1514
|
+
owner_pid INTEGER NOT NULL,
|
|
1515
|
+
heartbeat_at INTEGER NOT NULL,
|
|
1516
|
+
created_at INTEGER NOT NULL,
|
|
1517
|
+
PRIMARY KEY (archive_key, owner_id)
|
|
1518
|
+
);
|
|
1519
|
+
|
|
1520
|
+
CREATE TABLE IF NOT EXISTS entry_sqlite_leases (
|
|
1521
|
+
archive_key TEXT NOT NULL,
|
|
1522
|
+
entry_path TEXT NOT NULL,
|
|
1523
|
+
mode TEXT NOT NULL DEFAULT 'read',
|
|
1524
|
+
owner_id TEXT NOT NULL,
|
|
1525
|
+
owner_pid INTEGER NOT NULL,
|
|
1526
|
+
heartbeat_at INTEGER NOT NULL,
|
|
1527
|
+
created_at INTEGER NOT NULL,
|
|
1528
|
+
PRIMARY KEY (archive_key, entry_path, owner_id)
|
|
1529
|
+
);
|
|
1530
|
+
|
|
1531
|
+
CREATE TABLE IF NOT EXISTS archive_commit_locks (
|
|
1532
|
+
archive_key TEXT PRIMARY KEY,
|
|
1533
|
+
owner_id TEXT NOT NULL,
|
|
1534
|
+
owner_pid INTEGER NOT NULL,
|
|
1535
|
+
heartbeat_at INTEGER NOT NULL,
|
|
1536
|
+
created_at INTEGER NOT NULL
|
|
1537
|
+
);
|
|
1538
|
+
`,sz=new xt(1)});async function CR(e){let t=Bi();for(;;){if(await K(async n=>(await Ne(n),await n.transaction(async()=>await n.queryOne("SELECT * FROM archive_commit_locks WHERE archive_key = ?",[e],xR)!==void 0?!1:(await n.run(`
|
|
1539
|
+
INSERT INTO archive_commit_locks (
|
|
1540
|
+
archive_key, owner_id, owner_pid, heartbeat_at, created_at
|
|
1541
|
+
) VALUES (?, ?, ?, ?, ?)
|
|
1542
|
+
`,[e,t,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await K(async n=>{await n.run("DELETE FROM archive_commit_locks WHERE archive_key = ? AND owner_id = ?",[e,t])})};await wp(100)}}async function lr(e,t,r){let n=Bi();for(;;){if(await K(async a=>(await Ne(a),await a.transaction(async()=>(await a.queryAll(`
|
|
1543
|
+
SELECT *
|
|
1544
|
+
FROM entry_locks
|
|
1545
|
+
WHERE archive_key = ?
|
|
1546
|
+
`,[e],IR)).some(s=>s.ownerId!==n&&hz(t,s.entryPath)&&mz(r,s.mode))?!1:(await a.run(`
|
|
1547
|
+
INSERT INTO entry_locks (
|
|
1548
|
+
archive_key, entry_path, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
1549
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1550
|
+
`,[e,t,r,n,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await K(async a=>{await a.run(`
|
|
1551
|
+
DELETE FROM entry_locks
|
|
1552
|
+
WHERE archive_key = ? AND entry_path = ? AND owner_id = ?
|
|
1553
|
+
`,[e,t,n])})};await wp(100)}}async function pr(e,t,r,n){let i=await lr(e,t,r);try{return await n()}finally{await i()}}function mz(e,t){return e==="state"||t==="state"?e==="state"&&t==="state":!(e==="read"&&t==="read")}function hz(e,t){return e===t||vR(e,t)||vR(t,e)}function vR(e,t){return e.endsWith("/")&&t.startsWith(e)}async function TR(e){await K(async t=>{await Ne(t),await t.run(`
|
|
1554
|
+
INSERT OR REPLACE INTO entry_sqlite_leases (
|
|
1555
|
+
archive_key, entry_path, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
1556
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1557
|
+
`,[e.archiveKey,e.entryPath,e.mode,e.ownerId,process.pid,Date.now(),Date.now()])})}async function _b(e){await K(async t=>{await t.run(`
|
|
1558
|
+
DELETE FROM entry_sqlite_leases
|
|
1559
|
+
WHERE archive_key = ? AND entry_path = ? AND owner_id = ?
|
|
1560
|
+
`,[e.archiveKey,e.entryPath,e.ownerId])})}async function Ab(e,t){for(;;){if(await K(async n=>(await Ne(n),await n.queryOne(`
|
|
1561
|
+
SELECT COUNT(*) AS count
|
|
1562
|
+
FROM entry_sqlite_leases AS lease
|
|
1563
|
+
LEFT JOIN archive_owners AS owner
|
|
1564
|
+
ON owner.archive_key = lease.archive_key
|
|
1565
|
+
AND owner.owner_id = lease.owner_id
|
|
1566
|
+
WHERE lease.archive_key = ?
|
|
1567
|
+
AND lease.entry_path = ?
|
|
1568
|
+
AND (
|
|
1569
|
+
owner.owner_id IS NULL
|
|
1570
|
+
OR owner.heartbeat_at > ?
|
|
1571
|
+
)
|
|
1572
|
+
`,[e,t,Date.now()-6e4],i=>ur(i,"count"))))===0)return;await wp(100)}}var Gc=l(()=>{"use strict";Qr();dr();en()});async function xp(e,t){let r=e.filter(a=>a.startsWith(t.prefix)),n=await K(async a=>await a.queryAll(`
|
|
1573
|
+
SELECT *
|
|
1574
|
+
FROM entry_overlays
|
|
1575
|
+
WHERE archive_key = ?
|
|
1576
|
+
`,[t.archiveKey],Bn)),i=new Set(r);for(let a of n)a.entryPath.startsWith(t.prefix)&&(a.kind==="deleted"?i.delete(a.entryPath):i.add(a.entryPath));return[...i].sort((a,o)=>a.localeCompare(o))}async function Lb(e){let t=await Hn(e.archiveKey,e.entryPath);return t?.kind==="deleted"?{kind:"deleted"}:t?.workspacePath!==void 0?{kind:"workspace",path:t.workspacePath}:{kind:"archive"}}async function Hn(e,t){return await K(async r=>await r.queryOne("SELECT * FROM entry_overlays WHERE archive_key = ? AND entry_path = ?",[e,t],Bn))}async function Ki(e){return await K(async t=>await t.queryAll(`
|
|
1577
|
+
SELECT *
|
|
1578
|
+
FROM entry_overlays
|
|
1579
|
+
WHERE archive_key = ?
|
|
1580
|
+
ORDER BY entry_path ASC
|
|
1581
|
+
`,[e],Bn))}async function vp(e){let t=e.entryPath===Z?await Un(e.archivePath):void 0;await K(async r=>{await r.run(`
|
|
1582
|
+
INSERT INTO entry_overlays (
|
|
1583
|
+
archive_key, archive_path, entry_path, kind, workspace_path,
|
|
1584
|
+
archive_signature, mutation_token, updated_at
|
|
1585
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1586
|
+
ON CONFLICT(archive_key, entry_path)
|
|
1587
|
+
DO UPDATE SET kind = excluded.kind,
|
|
1588
|
+
archive_path = excluded.archive_path,
|
|
1589
|
+
workspace_path = excluded.workspace_path,
|
|
1590
|
+
archive_signature = excluded.archive_signature,
|
|
1591
|
+
mutation_token = excluded.mutation_token,
|
|
1592
|
+
updated_at = excluded.updated_at
|
|
1593
|
+
`,[e.archiveKey,e.archivePath,e.entryPath,e.kind,e.workspacePath??null,await $i(e.archivePath),t??null,Date.now()])})}async function wo(e,t){await K(async r=>{await r.run("DELETE FROM entry_overlays WHERE archive_key = ? AND entry_path = ?",[e,t])})}async function RR(e){return await K(async t=>await t.queryOne("SELECT archive_path FROM entry_overlays WHERE archive_key = ? LIMIT 1",[e],r=>He(r,"archive_path")))}function PR(e){if(e.kind==="deleted")return{entryPath:e.entryPath,kind:"deleted"};if(e.workspacePath===void 0)throw new Error(`Missing workspace path for ${e.entryPath}.`);return{entryPath:e.entryPath,kind:"file",workspacePath:e.workspacePath}}var So=l(()=>{"use strict";$n();Qr();dr();en()});import{createHash as fz}from"crypto";function Uc(e){return e.normalize("NFKC").replace(bz,"").replace(wz," ").replace(Sz," ").trim()}function tn(e){let t=Uc(e),r=[],n=[],i=[];for(let a of Cp(t,"phrase"))r.push(Ji(a,"hp",1));for(let a of Cp(t,"bigram"))r.push(Ji(a,"h2",1));for(let a of Cp(t,"trigram"))r.push(Ji(a,"h3",1));for(let a of Cz(t))r.push(Ji(a,"hw",1));for(let a of vz(t)){r.push(Ji(a,"le",1));let o=Tz(a);o!==a&&!_R.has(o)&&n.push(Ji(o,"ls",2))}for(let a of Cp(t,"char"))i.push(Ji(a,"hc",3));return{tier1:Ob(r),tier2:Ob(n),tier3:Ob(i)}}function AR(e){return[...new Set([...e.tier1,...e.tier2,...e.tier3].map(t=>t.raw))]}function Tp(e){return e.tier1.length>0||e.tier2.length>0||e.tier3.length>0}function Cp(e,t){let r=[];for(let n of e.matchAll(gz)){let i=[...n[0]];if(t==="phrase"){i.length>=2&&r.push(i.join(""));continue}if(t==="char"){r.push(...i.filter(o=>!Iz.has(o)));continue}let a=t==="bigram"?2:3;if(!(i.length<a))for(let o=0;o<=i.length-a;o+=1)r.push(i.slice(o,o+a).join(""))}return r}function vz(e){return[...e.toLowerCase().matchAll(yz)].map(t=>t[0]).filter(t=>!_R.has(t))}function Cz(e){if(typeof Intl.Segmenter!="function")return[];let t=new Intl.Segmenter("zh-CN",{granularity:"word"}),r=[];for(let n of t.segment(e)){let i=Uc(n.segment).toLowerCase();n.isWordLike===!0&&[...i].length>=2&&kz.test(i)&&r.push(i)}return r}function Ji(e,t,r){let n=Uc(e).toLowerCase(),i=fz("sha256").update(n).digest("hex").slice(0,20);return{encoded:`${t}${i}`,raw:n,tier:r}}function Ob(e){let t=new Set,r=[];for(let n of e)t.has(n.encoded)||(t.add(n.encoded),r.push(n));return r}function Tz(e){let t=xz.get(e);return t!==void 0?t:!Ez.test(e)||/\d/u.test(e)||e.length<4?e:Rz(e)}function Rz(e){let t=e;return t.endsWith("ies")&&t.length>4?t=`${t.slice(0,-3)}y`:t.endsWith("ing")&&t.length>5?t=t.slice(0,-3):t.endsWith("ed")&&t.length>4||t.endsWith("es")&&t.length>4?t=t.slice(0,-2):t.endsWith("s")&&t.length>4&&(t=t.slice(0,-1)),t.length>3&&t.at(-1)===t.at(-2)&&/[bcdfghjklmnpqrstvwxyz]/u.test(t.at(-1))&&(t=t.slice(0,-1)),t}var gz,yz,bz,wz,Sz,kz,Ez,Iz,_R,xz,$c=l(()=>{"use strict";gz=new RegExp("\\p{Script=Han}+","gu"),yz=/[\p{Script=Latin}\p{Number}]+/gu,bz=/[\u200B-\u200D\u2060\uFEFF]/gu,wz=/[\p{Cc}\p{Cf}]/gu,Sz=/\s+/gu,kz=new RegExp("^\\p{Script=Han}+$","u"),Ez=/^[a-z0-9]+$/u,Iz=new Set(["\u7684","\u5730","\u5F97","\u4E86","\u7740","\u8FC7","\u548C","\u4E0E","\u53CA","\u6216","\u800C","\u5728","\u662F","\u4E3A","\u4EE5","\u4E8E","\u5BF9","\u4E2D","\u4E0A","\u4E0B","\u7B49"]),_R=new Set(["a","an","and","are","as","at","be","been","being","by","can","could","did","do","does","for","from","had","has","have","in","is","may","might","of","on","or","should","the","to","was","were","will","with","would"]),xz=new Map([["am","be"],["are","be"],["is","be"],["was","be"],["were","be"],["been","be"],["being","be"],["has","have"],["had","have"],["does","do"],["did","do"],["done","do"]])});import{createHash as Pz}from"crypto";function mr(e){let t=Pz("sha256");for(let r of e.textSentences)t.update("text"),t.update("\0"),t.update(String(r.archiveId)),t.update("\0"),t.update(String(r.kind)),t.update("\0"),t.update(String(r.chapterId)),t.update("\0"),t.update(String(r.sentenceIndex)),t.update("\0"),t.update(r.text),t.update("\0");for(let r of e.objectProperties)t.update("object-property"),t.update("\0"),t.update(String(r.archiveId)),t.update("\0"),t.update(String(r.ownerKind)),t.update("\0"),t.update(r.ownerId),t.update("\0"),t.update(String(r.propertyKind)),t.update("\0"),t.update(String(r.chapterId??"")),t.update("\0"),t.update(r.text),t.update("\0");return t.digest("hex")}async function hr(e){return await e.queryOne(`
|
|
1594
|
+
SELECT value
|
|
1595
|
+
FROM search_index_state
|
|
1596
|
+
WHERE key = 'fingerprint'
|
|
1597
|
+
`,void 0,t=>String(t.value))}var Rp=l(()=>{"use strict"});var be,ko,J,je,Nb,Ut,LR,jb,Eo=l(()=>{"use strict";be={source:1,summary:2},ko=0,J={chapter:1,chunk:2,entity:3},je={title:1,label:1,content:2,surface:1},Nb="4",Ut=32e3,LR=1e6,jb=[1,.45,.08]});function Pp(e){return e.map(t=>t.encoded).join(" ")}function Db(e){return e===void 0||e.length===0?"":`AND r.chapter_id IN (${e.map(()=>"?").join(", ")})`}function Mb(e){return e===void 0?[]:[...e]}function Wb(e){return e===void 0?"":"LIMIT ?"}function Fb(e){return e===void 0?[]:[e]}function Gb(e){return e==null||e.includes("chapter")||e.includes("chapter-title")||e.includes("node")||e.includes("entity")}function OR(e){let t=jR(e);return t.length===0?"":`AND (${t.map(()=>"(r.owner_kind = ? AND r.property_kind = ?)").join(" OR ")})`}function NR(e){return jR(e).flatMap(t=>[t.ownerKind,t.propertyKind])}function jR(e){if(e==null)return[];let t=[];return(e.includes("chapter")||e.includes("chapter-title"))&&t.push({ownerKind:J.chapter,propertyKind:je.title}),e.includes("node")&&t.push({ownerKind:J.chunk,propertyKind:je.label},{ownerKind:J.chunk,propertyKind:je.content}),e.includes("entity")&&t.push({ownerKind:J.entity,propertyKind:je.surface}),t}function Ub(e){if(e==null)return[be.summary,be.source];let t=[];return e.includes("summary")&&t.push(be.summary),e.includes("source")&&t.push(be.source),t}function $b(e){let t=Math.max(0,-e)*LR;return t/(1+t)}function DR(e){return[e.archiveId,e.ownerKind,e.ownerId,e.propertyKind,e.chapterId??""].join(":")}function MR(e){return[e.archiveId,e.kind,e.chapterId,e.sentenceIndex].join(":")}var Bb=l(()=>{"use strict";Eo()});async function WR(e,t){let r=await e.queryOne(`
|
|
1598
|
+
SELECT id
|
|
1599
|
+
FROM text_sentence_records
|
|
1600
|
+
WHERE archive_id = ? AND kind = ? AND chapter_id = ? AND sentence_index = ?
|
|
1601
|
+
`,[t.archiveId,t.kind,t.chapterId,t.sentenceIndex],n=>y(n,"id"));return r!==void 0?r:(await e.run(`
|
|
1602
|
+
INSERT INTO text_sentence_records (
|
|
1603
|
+
archive_id, kind, chapter_id, sentence_index, words_count, byte_offset, byte_length
|
|
1604
|
+
)
|
|
1605
|
+
VALUES (?, ?, ?, ?, ?, 0, 0)
|
|
1606
|
+
`,[t.archiveId,t.kind,t.chapterId,t.sentenceIndex,t.wordsCount]),await e.getLastInsertRowId())}async function Hb(e,t){return await e.run(`
|
|
1607
|
+
INSERT INTO search_object_properties_records (
|
|
1608
|
+
archive_id, owner_kind, owner_id, property_kind, chapter_id
|
|
1609
|
+
)
|
|
1610
|
+
VALUES (?, ?, ?, ?, ?)
|
|
1611
|
+
`,[t.archiveId,t.ownerKind,t.ownerId,t.propertyKind,t.chapterId??null]),await e.getLastInsertRowId()}async function Bc(e,t,r,n){await e.run(`
|
|
1612
|
+
INSERT INTO ${t}(rowid, tier1, tier2, tier3)
|
|
1613
|
+
VALUES (?, ?, ?, ?)
|
|
1614
|
+
`,[r,Pp(n.tier1),Pp(n.tier2),Pp(n.tier3)])}var FR=l(()=>{"use strict";M();Bb()});async function GR(e,t,r){await qb(e,t,r)}async function qb(e,t,r){let n=await e.serials.getChaptersRevision();await e.writeSearchIndexDatabase(async i=>{let a=mr(t);await hr(i)!==a&&await i.transaction(async()=>{await r?.({phase:"clearing"}),await i.run("DELETE FROM text_sentence_fts"),await i.run("DELETE FROM search_object_properties_fts"),await i.run("DELETE FROM text_sentence_records"),await i.run("DELETE FROM search_object_properties_records"),await i.run("DELETE FROM index_dirty_chapters"),await i.run("DELETE FROM search_index_state");let s=0;for(let d of t.textSentences){let u=tn(d.text),p=await WR(i,d);await Bc(i,"text_sentence_fts",p,u),s+=1,await r?.({done:s,phase:"indexing-text",total:t.textSentences.length,unit:"sentence"})}let c=0;for(let d of t.objectProperties){let u=tn(d.text),p=await Hb(i,d);await Bc(i,"search_object_properties_fts",p,u),c+=1,await r?.({done:c,phase:"indexing-objects",total:t.objectProperties.length,unit:"object"})}await r?.({phase:"finalizing"}),await i.run(`
|
|
1615
|
+
INSERT INTO search_index_state(key, value)
|
|
1616
|
+
VALUES ('version', ?)
|
|
1617
|
+
`,[Nb]),await i.run(`
|
|
1618
|
+
INSERT INTO search_index_state(key, value)
|
|
1619
|
+
VALUES ('fingerprint', ?)
|
|
1620
|
+
`,[a]),await i.run(`
|
|
1621
|
+
INSERT INTO search_index_state(key, value)
|
|
1622
|
+
VALUES ('chaptersRevision', ?)
|
|
1623
|
+
`,[String(n)])})})}async function UR(e,t,r,n){let i=await e.serials.getChaptersRevision();await e.writeSearchIndexDatabase(async a=>{await Hc(a,n);let o={objectDone:0,textDone:0};for await(let s of t)o=await qc(a,s,o,n);await Kc(a,r,i,n)})}async function Hc(e,t){await e.transaction(async()=>{await t?.({phase:"clearing"}),await e.run("DELETE FROM text_sentence_fts"),await e.run("DELETE FROM search_object_properties_fts"),await e.run("DELETE FROM text_sentence_records"),await e.run("DELETE FROM search_object_properties_records"),await e.run("DELETE FROM index_dirty_chapters"),await e.run("DELETE FROM search_index_state"),await e.run(`
|
|
1624
|
+
INSERT INTO index_dirty_chapters(archive_id, chapter_id, updated_at)
|
|
1625
|
+
VALUES (0, 0, ?)
|
|
1626
|
+
`,[Date.now()])})}async function qc(e,t,r,n){let{objectDone:i,textDone:a}=r;return await e.transaction(async()=>{for(let o of t.textSentences){let s=tn(o.text),c=await _z(e,o);await Bc(e,"text_sentence_fts",c,s),a+=1,await n?.({done:a,phase:"indexing-text",unit:"sentence"})}for(let o of t.objectProperties){let s=tn(o.text),c=await Hb(e,o);await Bc(e,"search_object_properties_fts",c,s),i+=1,await n?.({done:i,phase:"indexing-objects",unit:"object"})}}),{objectDone:i,textDone:a}}async function _z(e,t){return await e.run(`
|
|
1627
|
+
INSERT INTO text_sentence_records (
|
|
1628
|
+
archive_id, kind, chapter_id, sentence_index, words_count, byte_offset, byte_length
|
|
1629
|
+
)
|
|
1630
|
+
VALUES (?, ?, ?, ?, ?, 0, 0)
|
|
1631
|
+
`,[t.archiveId,t.kind,t.chapterId,t.sentenceIndex,t.wordsCount]),await e.getLastInsertRowId()}async function Kc(e,t,r,n){await e.transaction(async()=>{await n?.({phase:"finalizing"}),await e.run("DELETE FROM index_dirty_chapters"),await e.run(`
|
|
1632
|
+
INSERT INTO search_index_state(key, value)
|
|
1633
|
+
VALUES ('version', ?)
|
|
1634
|
+
`,[Nb]),await e.run(`
|
|
1635
|
+
INSERT INTO search_index_state(key, value)
|
|
1636
|
+
VALUES ('fingerprint', ?)
|
|
1637
|
+
`,[t]),await e.run(`
|
|
1638
|
+
INSERT INTO search_index_state(key, value)
|
|
1639
|
+
VALUES ('chaptersRevision', ?)
|
|
1640
|
+
`,[String(r)])})}var $R=l(()=>{"use strict";$c();Rp();FR();Eo()});async function _p(e,t,r={}){if(t.length===0)return;let n=r.archiveId??ko,i=r.updatedAt??Date.now(),a=[...new Set(t)];await e.writeSearchIndexDatabase(async o=>{await o.transaction(async()=>{for(let s of a)await o.run(`
|
|
1641
|
+
INSERT INTO index_dirty_chapters(archive_id, chapter_id, updated_at)
|
|
1642
|
+
VALUES (?, ?, ?)
|
|
1643
|
+
ON CONFLICT(archive_id, chapter_id) DO UPDATE SET
|
|
1644
|
+
updated_at = excluded.updated_at
|
|
1645
|
+
`,[n,s,i])})})}var BR=l(()=>{"use strict";Eo()});function HR(e,t,r){return r==="all"?Az(e):[{matchExpression:Kb([{column:"tier1",tokens:t.tier1.map(n=>n.encoded)}])},{matchExpression:Kb([{column:"tier1",tokens:t.tier1.map(n=>n.encoded)},{column:"tier2",tokens:t.tier2.map(n=>n.encoded)}])},{matchExpression:Kb([{column:"tier1",tokens:t.tier1.map(n=>n.encoded)},{column:"tier2",tokens:t.tier2.map(n=>n.encoded)},{column:"tier3",tokens:t.tier3.map(n=>n.encoded)}])}]}function Az(e){let t=Uc(e).split(/\s+/u).map(n=>tn(n)).filter(Tp),r=n=>t.map(i=>{let a=[...new Set(n(i))];return a.length===0?"":`(${a.join(" OR ")})`}).filter(i=>i!=="").join(" AND ");return[{matchExpression:r(n=>n.tier1.map(i=>`tier1:${zi(i.encoded)}`))},{matchExpression:r(n=>[...n.tier1.map(i=>`tier1:${zi(i.encoded)}`),...n.tier2.map(i=>`tier2:${zi(i.encoded)}`)])},{matchExpression:r(n=>[...n.tier1.map(i=>`tier1:${zi(i.encoded)}`),...n.tier2.map(i=>`tier2:${zi(i.encoded)}`),...n.tier3.map(i=>`tier3:${zi(i.encoded)}`)])}]}function Kb(e){return e.map(t=>({...t,tokens:[...new Set(t.tokens)]})).filter(t=>t.tokens.length>0).map(t=>`${t.column}:(${t.tokens.map(zi).join(" OR ")})`).join(" OR ")}function zi(e){return`"${e.replaceAll('"','""')}"`}var qR=l(()=>{"use strict";$c()});function KR(e){return(typeof e=="object"&&e!==null&&"code"in e?e.code:void 0)==="SQLITE_CANTOPEN"||e instanceof Error&&(e.message.includes("Archive SQLite entry is missing: fts.db")||e.message.includes("no such table: search_index_state"))}var JR=l(()=>{"use strict"});async function zR(e,t){return await rn(e,t)==="current"}async function rn(e,t){let r=t===void 0?void 0:mr(t);try{return await e.readSearchIndexDatabase(async n=>{if(await QR(n))return"dirty";let i=await hr(n);return i===void 0?"dirty":r===void 0||i===r?"current":"dirty"})}catch(n){if(KR(n))return"missing";throw n}}async function QR(e){return await Lz(e,"index_dirty_chapters")?await e.queryOne(`
|
|
1646
|
+
SELECT 1 AS found
|
|
1647
|
+
FROM index_dirty_chapters
|
|
1648
|
+
LIMIT 1
|
|
1649
|
+
`,void 0,r=>y(r,"found"))===1:!1}async function Lz(e,t){return await e.queryOne(`
|
|
1650
|
+
SELECT 1 AS found
|
|
1651
|
+
FROM sqlite_master
|
|
1652
|
+
WHERE type = 'table' AND name = ?
|
|
1653
|
+
`,[t],n=>y(n,"found"))===1}async function VR(e){if(await QR(e))throw new Error("Archive search index is dirty; rebuild the index before querying.")}var Jb=l(()=>{"use strict";M();JR();Rp()});async function nn(e,t,r={}){let n=tn(t);if(!Tp(n))return;let i=AR(n);return await e.readSearchIndexDatabase(async a=>{await VR(a);let o=HR(t,n,r.match??"any"),s=r.objectHitLimit??Ut,c=r.textHitLimit??Ut,d=Gb(r.types),u=Ub(r.types).length>0,p=new Map,m=new Map;for(let f of o){if(f.matchExpression==="")continue;let w=Math.max(0,s-p.size),b=Math.max(0,c-m.size);if((!d||w<=0)&&(!u||b<=0))break;let[E,N]=await Promise.all([Oz(a,f.matchExpression,{...r,objectHitLimit:w}),Nz(a,f.matchExpression,{...r,textHitLimit:b})]);for(let C of E){let q=DR(C);p.has(q)||p.set(q,C)}for(let C of N){let q=MR(C);m.has(q)||m.set(q,C)}if((!d||p.size>=s)&&(!u||m.size>=c))break}return{objectHits:[...p.values()],terms:i,textHits:[...m.values()]}})}async function Oz(e,t,r){return!Gb(r.types)||r.objectHitLimit===0?[]:await e.queryAll(`
|
|
1654
|
+
SELECT
|
|
1655
|
+
r.owner_kind AS owner_kind,
|
|
1656
|
+
r.owner_id AS owner_id,
|
|
1657
|
+
r.property_kind AS property_kind,
|
|
1658
|
+
r.archive_id AS archive_id,
|
|
1659
|
+
r.chapter_id AS chapter_id,
|
|
1660
|
+
bm25(search_object_properties_fts, ?, ?, ?) AS rank
|
|
1661
|
+
FROM search_object_properties_fts
|
|
1662
|
+
JOIN search_object_properties_records AS r
|
|
1663
|
+
ON r.id = search_object_properties_fts.rowid
|
|
1664
|
+
WHERE search_object_properties_fts MATCH ?
|
|
1665
|
+
${Db(r.chapters)}
|
|
1666
|
+
${OR(r.types)}
|
|
1667
|
+
ORDER BY rank ASC, r.archive_id, r.chapter_id, r.owner_kind, r.owner_id, r.property_kind
|
|
1668
|
+
${Wb(r.objectHitLimit)}
|
|
1669
|
+
`,[...jb,t,...Mb(r.chapters),...NR(r.types),...Fb(r.objectHitLimit)],n=>({ownerId:String(n.owner_id),archiveId:y(n,"archive_id"),ownerKind:y(n,"owner_kind"),propertyKind:y(n,"property_kind"),score:$b(y(n,"rank")),...n.chapter_id===null?{}:{chapterId:y(n,"chapter_id")}}))}async function Nz(e,t,r){let n=Ub(r.types);if(n.length===0)return[];if(r.textHitLimit===0)return[];let i=r.textAfter;return await e.queryAll(`
|
|
1670
|
+
SELECT
|
|
1671
|
+
kind,
|
|
1672
|
+
archive_id,
|
|
1673
|
+
chapter_id,
|
|
1674
|
+
sentence_index,
|
|
1675
|
+
words_count,
|
|
1676
|
+
rank
|
|
1677
|
+
FROM (
|
|
1678
|
+
SELECT
|
|
1679
|
+
r.kind AS kind,
|
|
1680
|
+
r.archive_id AS archive_id,
|
|
1681
|
+
r.chapter_id AS chapter_id,
|
|
1682
|
+
r.sentence_index AS sentence_index,
|
|
1683
|
+
r.words_count AS words_count,
|
|
1684
|
+
bm25(text_sentence_fts, ?, ?, ?) AS rank
|
|
1685
|
+
FROM text_sentence_fts
|
|
1686
|
+
JOIN text_sentence_records AS r
|
|
1687
|
+
ON r.id = text_sentence_fts.rowid
|
|
1688
|
+
WHERE text_sentence_fts MATCH ?
|
|
1689
|
+
AND r.kind IN (${n.map(()=>"?").join(", ")})
|
|
1690
|
+
${Db(r.chapters)}
|
|
1691
|
+
)
|
|
1692
|
+
${i===void 0?"":`
|
|
1693
|
+
WHERE (
|
|
1694
|
+
rank > ?
|
|
1695
|
+
OR (rank = ? AND archive_id > ?)
|
|
1696
|
+
OR (rank = ? AND archive_id = ? AND chapter_id > ?)
|
|
1697
|
+
OR (
|
|
1698
|
+
rank = ?
|
|
1699
|
+
AND archive_id = ?
|
|
1700
|
+
AND chapter_id = ?
|
|
1701
|
+
AND sentence_index > ?
|
|
1702
|
+
)
|
|
1703
|
+
OR (
|
|
1704
|
+
rank = ?
|
|
1705
|
+
AND archive_id = ?
|
|
1706
|
+
AND chapter_id = ?
|
|
1707
|
+
AND sentence_index = ?
|
|
1708
|
+
AND kind > ?
|
|
1709
|
+
)
|
|
1710
|
+
)
|
|
1711
|
+
`}
|
|
1712
|
+
ORDER BY rank ASC, archive_id, chapter_id, sentence_index, kind
|
|
1713
|
+
${Wb(r.textHitLimit)}
|
|
1714
|
+
`,[...jb,t,...n,...Mb(r.chapters),...i===void 0?[]:[i.rank,i.rank,i.archiveId,i.rank,i.archiveId,i.chapterId,i.rank,i.archiveId,i.chapterId,i.sentenceIndex,i.rank,i.archiveId,i.chapterId,i.sentenceIndex,i.kind],...Fb(r.textHitLimit)],a=>{let o=y(a,"rank");return{chapterId:y(a,"chapter_id"),archiveId:y(a,"archive_id"),kind:y(a,"kind"),rank:o,score:$b(o),sentenceIndex:y(a,"sentence_index"),wordsCount:y(a,"words_count")}})}var XR=l(()=>{"use strict";M();$c();qR();Bb();Eo();Jb()});async function an(e){return await e.readDatabase(async t=>await t.queryOne(`
|
|
1715
|
+
SELECT fts_embedded
|
|
1716
|
+
FROM archive_index_settings
|
|
1717
|
+
WHERE id = 1
|
|
1718
|
+
`,void 0,n=>({ftsEmbedded:y(n,"fts_embedded")!==0}))??{ftsEmbedded:!1})}async function Io(e,t){await e.readDatabase(async r=>{await r.run(`
|
|
1719
|
+
INSERT INTO archive_index_settings(id, fts_embedded)
|
|
1720
|
+
VALUES (1, ?)
|
|
1721
|
+
ON CONFLICT(id)
|
|
1722
|
+
DO UPDATE SET fts_embedded = excluded.fts_embedded
|
|
1723
|
+
`,[t?1:0])})}var YR=l(()=>{"use strict";M()});var ZR=l(()=>{"use strict";$R();BR();Rp();XR();YR();Jb();Eo()});var qn=l(()=>{"use strict";ZR();$c()});var eP={};Qe(eP,{SEARCH_INDEX_FTS_HIT_LIMIT:()=>Ut,SEARCH_OBJECT_PROPERTY_KIND:()=>je,SEARCH_OBJECT_PROPERTY_OWNER_KIND:()=>J,SINGLE_ARCHIVE_INDEX_ID:()=>ko,TEXT_SENTENCE_KIND:()=>be,createSearchIndexFingerprint:()=>mr,ensureSearchIndex:()=>qb,finalizeSearchIndexReplacement:()=>Kc,isSearchIndexCurrent:()=>zR,markDirtySearchIndexChapters:()=>_p,prepareSearchIndexReplacement:()=>Hc,querySearchIndex:()=>nn,readArchiveIndexSettings:()=>an,readSearchIndexFingerprintFromDatabase:()=>hr,readSearchIndexStatus:()=>rn,replaceSearchIndex:()=>UR,setFtsIndexEmbedded:()=>Io,writeSearchIndexBatch:()=>qc});var Qi=l(()=>{"use strict";qn()});async function Ap(e,t){return await K(async r=>{await Ne(r);let n=await r.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[e],a=>ur(a,"count")),i=await r.queryOne(`
|
|
1724
|
+
SELECT COUNT(*) AS count
|
|
1725
|
+
FROM entry_sqlite_leases
|
|
1726
|
+
WHERE archive_key = ?
|
|
1727
|
+
AND entry_path = ?
|
|
1728
|
+
`,[e,t],a=>ur(a,"count"));return(n??0)>0||(i??0)>0})}async function Lp(e){return await K(async t=>{await Ne(t);let r=await t.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[e],a=>ur(a,"count")),n=await t.queryOne("SELECT COUNT(*) AS count FROM entry_sqlite_leases WHERE archive_key = ?",[e],a=>ur(a,"count")),i=await t.queryOne("SELECT COUNT(*) AS count FROM entry_locks WHERE archive_key = ?",[e],a=>ur(a,"count"));return(r??0)>0||(n??0)>0||(i??0)>0})}var zb=l(()=>{"use strict";en()});import{mkdir as tP,readdir as rP}from"fs/promises";import{basename as jz,dirname as Dz,join as Op}from"path";async function Jc(e,t){let r=Op(Np(),e,Dz(t));return await tP(r,{recursive:!0}),Op(r,jz(t))}function Np(){return Op(Rb(),"work")}async function nP(e){if(await tP(e,{recursive:!0}),(await rP(e)).length>0)throw new Error(`Read workspace directory is not empty: ${e}`)}async function Qb(e){let t;try{t=await rP(e,{withFileTypes:!0})}catch(n){if(R(n)&&n.code==="ENOENT")return[];throw n}let r=[];for(let n of t){let i=Op(e,n.name);n.isDirectory()?r.push(...await Qb(i)):n.isFile()&&r.push(i)}return r}function jp(e){return e.replaceAll("\\","/").replace(/^\/+/u,"")}function iP(e){let t=jp(e);return t===""||t.endsWith("/")?t:`${t}/`}var zc=l(()=>{"use strict";H();en()});import{mkdir as Mz,rename as Wz,rm as Fz}from"fs/promises";import{dirname as Gz}from"path";async function aP(e){if(e.workspacePath===void 0||!await Fn(e.workspacePath))return"missing";if(!await Fn(e.archivePath))return"dirty";try{let t=await Uz(e.workspacePath),r=await $i(e.archivePath);return t===void 0?"dirty":e.archiveSignature===r?"current":"dirty"}catch{return"dirty"}}async function Uz(e){await ke();let t=await $.open(e,"",{readonly:!0});try{return await hr(t)}finally{await t.close()}}async function oP(e){let t=await Un(e.targetArchivePath),r=await $z({mutationToken:t,targetArchiveKey:e.targetArchiveKey}),n=[];for(let o of r)o.workspacePath===void 0||await Fn(o.archivePath)||await Lp(o.archiveKey)||n.push(o);if(n.length!==1)return;let i=n[0],a=await lr(i.archiveKey,Z,"write");try{let o=await lr(i.archiveKey,Z,"state");try{let s=await Hn(i.archiveKey,Z),c=await Hn(e.targetArchiveKey,Z);if(s?.workspacePath===void 0||s.workspacePath!==i.workspacePath||s.mutationToken!==t||c!==void 0||await Fn(i.archivePath)||await Ap(i.archiveKey,Z))return;let d=await Jc(e.targetArchiveKey,Z);await Fz(d,{force:!0}).catch(()=>{}),await Mz(Gz(d),{recursive:!0}),await Wz(s.workspacePath,d),await K(async u=>{await u.run(`
|
|
1729
|
+
UPDATE entry_overlays
|
|
1730
|
+
SET archive_key = ?,
|
|
1731
|
+
archive_path = ?,
|
|
1732
|
+
workspace_path = ?,
|
|
1733
|
+
archive_signature = ?,
|
|
1734
|
+
updated_at = ?
|
|
1735
|
+
WHERE archive_key = ?
|
|
1736
|
+
AND entry_path = ?
|
|
1737
|
+
`,[e.targetArchiveKey,e.targetArchivePath,d,await $i(e.targetArchivePath),Date.now(),i.archiveKey,Z])})}finally{await o()}}finally{await a()}}async function $z(e){return await K(async t=>(await Ne(t),await t.queryAll(`
|
|
1738
|
+
SELECT *
|
|
1739
|
+
FROM entry_overlays
|
|
1740
|
+
WHERE entry_path = ?
|
|
1741
|
+
AND kind = 'file'
|
|
1742
|
+
AND workspace_path IS NOT NULL
|
|
1743
|
+
AND mutation_token = ?
|
|
1744
|
+
AND archive_key <> ?
|
|
1745
|
+
ORDER BY updated_at ASC
|
|
1746
|
+
`,[Z,e.mutationToken,e.targetArchiveKey],Bn)))}var Vb=l(()=>{"use strict";X();Ai();Qi();$n();Qr();zb();dr();Gc();So();en();zc()});import{mkdir as sP,readFile as cP,rename as Bz,rm as Dp,writeFile as dP}from"fs/promises";import{dirname as uP,posix as Hz,resolve as lP}from"path";var xo,Xb=l(()=>{"use strict";Ai();$n();Qr();dr();Gc();So();Vb();zc();xo=class{#e;#t;#r;#n;#i;#a;#o=Bi();constructor(t,r){this.#t=lP(t),this.#e=Ui(this.#t),this.#i=r.readonlyDatabase===!0,this.#a=r.searchIndexWritebackPolicy??"archive",this.#c=r.session}async#s(t){let r=await Hn(this.#e,t);if(r===void 0)return;let n=await $i(this.#t);if(r.archiveSignature===n)return r;await wo(this.#e,t),r.workspacePath!==void 0&&await Dp(r.workspacePath,{force:!0}).catch(()=>{}),this.#n?.delete(t)}#c;async close(){this.#r!==void 0&&((await this.#r).close(),this.#r=void 0),await _b({archiveKey:this.#e,entryPath:Zr,ownerId:this.#o}),await _b({archiveKey:this.#e,entryPath:Z,ownerId:this.#o})}async deleteFile(t){let r=this.#u(t);await pr(this.#e,r,"write",async()=>{await pr(this.#e,r,"state",async()=>{let n=await this.#s(r);await vp({archiveKey:this.#e,archivePath:this.#t,entryPath:r,kind:"deleted"}),n?.workspacePath!==void 0&&await Dp(n.workspacePath,{force:!0}).catch(()=>{}),this.#n?.set(r,{kind:"deleted"}),this.#c?.modifyEntry(r)})})}async deleteTree(t){let r=this.#u(t),n=await xp(await this.#d(),{archiveKey:this.#e,prefix:`${r}/`});for(let i of n)await this.deleteFile(i)}ensureDirectory(){return Promise.resolve()}initializeDatabaseSchema(){return!1}markDatabaseDirty(){this.#i||this.#c?.observeDirtyEntry(Zr)}markSearchIndexDatabaseDirty(){!this.#i&&this.#a==="archive"&&this.#c?.observeDirtyEntry(Z)}openDatabaseReadonly(){return this.#i}async listFiles(t){let r=this.#u(t),n=r===""?"":`${r}/`;return(await this.#f(n)).map(([a])=>a).map(a=>a.slice(n.length)).filter(a=>!a.includes("/")).map(a=>Hz.basename(a)).sort((a,o)=>a.localeCompare(o))}async listFileContents(t){let r=this.#u(t),n=r===""?"":`${r}/`;return await pr(this.#e,iP(n),"read",async()=>{let i=new Map;for(let[a,o]of await this.#f(n)){let s=a.slice(n.length);if(s.includes("/"))continue;if(o.kind==="workspace"){i.set(s,await cP(o.path));continue}let c=await this.#m(a);c!==void 0&&i.set(s,c)}return i})}async readFile(t){let r=this.#u(t);return await pr(this.#e,r,"read",async()=>{let n=(await this.#h()).get(r)??await pr(this.#e,r,"state",async()=>await Lb({archiveKey:this.#e,entryPath:r}));if(n.kind==="deleted"){this.#c?.observeDirtyEntry(r);return}return n.kind==="workspace"?(this.#c?.observeDirtyEntry(r),await cP(n.path)):await this.#m(r)})}async resolveDatabasePath(){return await this.#l(Zr,{createIfMissing:!0,readonly:this.#i})}async resolveSearchIndexDatabasePath(){return await this.#l(Z,{createIfMissing:!this.#i,readonly:this.#i})}async#l(t,r){return t===Z&&await ke(),await pr(this.#e,t,"state",async()=>{let n=await this.#s(t);if(t===Z&&n?.kind!=="file"&&(await oP({targetArchiveKey:this.#e,targetArchivePath:this.#t}),n=await this.#s(t)),n?.kind!=="file"){let i=await yo(this.#t,t);if(i===void 0&&!r.createIfMissing)throw new Error(`Archive SQLite entry is missing: ${t}`);let a=await Jc(this.#e,t);await sP(uP(a),{recursive:!0}),await dP(a,i??new Uint8Array),await vp({archiveKey:this.#e,archivePath:this.#t,entryPath:t,kind:"file",workspacePath:a}),n=await this.#s(t)}if(n?.workspacePath===void 0)throw new Error("Could not materialize SQLite database.");return await TR({archiveKey:this.#e,entryPath:t,mode:r.readonly?"read":"write",ownerId:this.#o}),n.workspacePath})}async writeFile(t,r,n){let i=this.#u(t);await pr(this.#e,i,"write",async()=>{let a=await pr(this.#e,i,"state",async()=>await Lb({archiveKey:this.#e,entryPath:i})),o=a.kind==="archive"&&await this.#m(i)!==void 0;if(n.overwrite!==!0&&(a.kind==="workspace"||o))throw new Error(`File already exists: ${t}`);await pr(this.#e,i,"state",async()=>{let s=await Jc(this.#e,i);await sP(uP(s),{recursive:!0});let c=`${s}.${process.pid}.${Date.now()}.tmp`;try{await dP(c,r),await Bz(c,s)}finally{await Dp(c,{force:!0}).catch(()=>{})}await vp({archiveKey:this.#e,archivePath:this.#t,entryPath:i,kind:"file",workspacePath:s}),this.#n?.set(i,{kind:"workspace",path:s}),a.kind==="workspace"&&a.path!==s&&await Dp(a.path,{force:!0}).catch(()=>{}),this.#c?.modifyEntry(i)})})}#u(t){let r=lP(t),n=this.#t.endsWith("/")?this.#t:`${this.#t}/`;return r.startsWith(n)?jp(r.slice(n.length)):jp(t)}async#d(){return(await this.#p()).listEntries()}async#m(t){return await(await this.#p()).readEntry(t)}async#p(){return this.#r??=Xr.open(this.#t),await this.#r}async#h(){if(this.#n!==void 0)return this.#n;let t=new Map;for(let r of await this.#d())t.set(r,{kind:"archive"});for(let r of await Ki(this.#e))r.kind==="deleted"?t.set(r.entryPath,{kind:"deleted"}):r.workspacePath!==void 0&&t.set(r.entryPath,{kind:"workspace",path:r.workspacePath});return this.#n=t,t}async#f(t){let r=[];for(let[n,i]of await this.#h())i.kind==="deleted"||!n.startsWith(t)||r.push([n,i]);return r.sort(([n],[i])=>n.localeCompare(i))}}});import{mkdir as qz,mkdtemp as Kz,rename as Jz,rm as pP}from"fs/promises";import{tmpdir as zz}from"os";import{basename as Qz,dirname as Vz,join as mP,resolve as Vme}from"path";async function Mp(e,t){let r=(await Ki(e)).filter(s=>t===void 0||t.has(s.entryPath)),n=await RR(e);if(r.length===0||n===void 0)return;let i=r.map(s=>s.entryPath).sort((s,c)=>s.localeCompare(c)),a=new Set(i),o=[];try{for(let d of i)o.push(await lr(e,d,"write"));for(let d of i)o.push(await lr(e,d,"state"));i.includes(Zr)&&await Ab(e,Zr),i.includes(Z)&&await Ab(e,Z);let s=(await Ki(e)).filter(d=>a.has(d.entryPath));if(s.length===0)return;let c=await CR(e);try{let d=await Kz(mP(zz(),"wikigraph-flush-")),u=mP(d,Qz(n));try{await go(n,u,s.map(PR)),await qz(Vz(n),{recursive:!0}),await Jz(u,n)}finally{await pP(d,{force:!0,recursive:!0})}}finally{await c()}for(let d of s)await wo(e,d.entryPath),d.workspacePath!==void 0&&await pP(d.workspacePath,{force:!0})}finally{for(let s of o.reverse())await s()}}var Wp=l(()=>{"use strict";$n();Qr();dr();Gc();So();en()});async function hP(e){await K(async t=>{await Ne(t),await t.run(`
|
|
1747
|
+
INSERT OR REPLACE INTO archive_owners (
|
|
1748
|
+
archive_key, owner_id, owner_pid, heartbeat_at, created_at
|
|
1749
|
+
) VALUES (?, ?, ?, ?, ?)
|
|
1750
|
+
`,[e.archiveKey,e.ownerId,process.pid,Date.now(),Date.now()])})}async function fP(e){await K(async t=>{await t.run(`
|
|
1751
|
+
UPDATE archive_owners
|
|
1752
|
+
SET heartbeat_at = ?, owner_pid = ?
|
|
1753
|
+
WHERE archive_key = ? AND owner_id = ?
|
|
1754
|
+
`,[Date.now(),process.pid,e.archiveKey,e.ownerId])})}async function gP(e){await K(async t=>{await t.transaction(async()=>{await t.run("DELETE FROM entry_sqlite_leases WHERE archive_key = ? AND owner_id = ?",[e.archiveKey,e.ownerId]),await t.run("DELETE FROM archive_owners WHERE archive_key = ? AND owner_id = ?",[e.archiveKey,e.ownerId])})})}async function yP(e){await K(async r=>{await Ne(r)});let t=await K(async r=>await r.queryAll(`
|
|
1755
|
+
SELECT owner_id
|
|
1756
|
+
FROM archive_owners
|
|
1757
|
+
WHERE archive_key = ?
|
|
1758
|
+
AND heartbeat_at <= ?
|
|
1759
|
+
`,[e,Date.now()-6e4],n=>He(n,"owner_id")));t.length!==0&&(await K(async r=>{await r.transaction(async()=>{await r.run(`
|
|
1760
|
+
DELETE FROM entry_sqlite_leases
|
|
1761
|
+
WHERE archive_key = ?
|
|
1762
|
+
AND owner_id IN (${qi(t.length)})
|
|
1763
|
+
`,[e,...t]),await r.run(`
|
|
1764
|
+
DELETE FROM archive_owners
|
|
1765
|
+
WHERE archive_key = ?
|
|
1766
|
+
AND owner_id IN (${qi(t.length)})
|
|
1767
|
+
`,[e,...t])})}),await Mp(e))}var bP=l(()=>{"use strict";dr();Wp();en()});function vo(e){let t=e.trim();if(t==="")return;let r=SP(t),n=[...t].filter(o=>Yb.test(o)).map(o=>o.toLowerCase()),i=Zb(t),a=Qc([...r,...i,...n]);if(a.length!==0)return{phrases:r,terms:a}}function Fp(e){let t=e.trim();return Qc([...SP(t),...Zb(t),...[...t].filter(r=>Yb.test(r)).map(r=>r.toLowerCase())])}function Gp(e,t){let r=e.toLowerCase(),n=[],i=[],a=0;for(let s of t.phrases){let c=s.toLowerCase();r.includes(c)?(n.push(s),a+=Yz+eQ(r,c)):i.includes(s)||i.push(s)}let o=new Set(Zb(e));for(let s of t.terms){if(n.includes(s))continue;([...s].every(u=>Yb.test(u))?r.includes(s):o.has(s.toLowerCase()))?(n.push(s),a+=Zz):i.includes(s)||i.push(s)}if(!(n.length===0||a<=0))return a+=tQ(n,t),a+=rQ(r,n),{matchCount:n.length,matchedTerms:Qc(n),missingTerms:Qc(i).filter(s=>!n.includes(s)),score:a,snippetNeedle:nQ(n)}}function wP(e,t){return e.map(r=>{let n=Gp(r.surface,t);return n===void 0?void 0:{match:n,mention:r}}).filter(iQ)}function SP(e){return Qc(e.split(/\s+/u).map(t=>t.trim()).filter(t=>t.length>1))}function Zb(e){return[...e.toLowerCase().matchAll(Xz)].map(t=>t[0])}function eQ(e,t){if(t==="")return 0;let r=0,n=e.indexOf(t);for(;n>=0;)r+=1,n=e.indexOf(t,n+t.length);return r}function tQ(e,t){let r=t.terms.length;return r===0?0:e.length/r}function rQ(e,t){let r=t.map(i=>e.indexOf(i.toLowerCase())).filter(i=>i>=0).sort((i,a)=>i-a);if(r.length<2)return 0;let n=r.at(-1)-r[0];return n<=80?1:n<=240?.5:0}function nQ(e){return[...e].sort((t,r)=>r.length-t.length)[0]}function Qc(e){return[...new Set(e.filter(t=>t!==""))]}function iQ(e){return e!==void 0}var Yb,Xz,Yz,Zz,Vc=l(()=>{"use strict";Yb=new RegExp("\\p{Script=Han}","u"),Xz=/[\p{Script=Latin}\p{Number}]+/gu,Yz=2,Zz=1});function Up(e,t,r){return Buffer.from(JSON.stringify({createdAt:r,offset:t,sessionId:e,v:3})).toString("base64url")}function Xc(e,t,r){return Buffer.from(JSON.stringify({createdAt:r,cursor:t,sessionId:e,v:4})).toString("base64url")}function ew(e){try{let t=JSON.parse(Buffer.from(e,"base64url").toString("utf8"));if(typeof t=="object"&&t!==null&&"createdAt"in t&&"cursor"in t&&"sessionId"in t&&"v"in t&&t.v===4&&typeof t.createdAt=="number"&&Number.isInteger(t.createdAt)&&t.createdAt>=0&&typeof t.sessionId=="string"&&t.sessionId!==""&&aQ(t.cursor))return{createdAt:t.createdAt,cursor:t.cursor,sessionId:t.sessionId}}catch{throw new Error("Invalid search cursor.")}throw new Error("Invalid search cursor.")}function tw(e){try{let t=JSON.parse(Buffer.from(e,"base64url").toString("utf8"));if(typeof t=="object"&&t!==null&&"offset"in t&&"sessionId"in t&&"createdAt"in t&&"v"in t&&t.v===3&&typeof t.sessionId=="string"&&t.sessionId!==""&&typeof t.createdAt=="number"&&Number.isInteger(t.createdAt)&&t.createdAt>=0&&typeof t.offset=="number"&&Number.isInteger(t.offset)&&t.offset>=0)return{createdAt:t.createdAt,offset:t.offset,sessionId:t.sessionId};if(typeof t=="object"&&t!==null&&"offset"in t&&"sessionId"in t&&"v"in t&&t.v===2&&typeof t.sessionId=="string"&&t.sessionId!==""&&typeof t.offset=="number"&&Number.isInteger(t.offset)&&t.offset>=0)return{offset:t.offset,sessionId:t.sessionId}}catch{throw new Error("Invalid search cursor.")}throw new Error("Invalid search cursor.")}function aQ(e){if(typeof e!="object"||e===null||!("bucket"in e))return!1;let t=e;switch(t.bucket){case 0:return t.key===void 0||oQ(t.key);case 1:return t.key===void 0||sQ(t.key);case 2:return t.key===void 0||cQ(t.key);case 3:return t.key===void 0||dQ(t.key);default:return!1}}function oQ(e){return typeof e=="object"&&e!==null&&typeof e.archiveId=="number"&&typeof e.chapterId=="number"&&typeof e.score=="number"}function sQ(e){return typeof e=="object"&&e!==null&&typeof e.archiveId=="number"&&typeof e.id=="string"&&(e.kind==="entity"||e.kind==="triple")&&typeof e.score=="number"}function cQ(e){return typeof e=="object"&&e!==null&&typeof e.archiveId=="number"&&typeof e.chunkId=="number"&&typeof e.score=="number"}function dQ(e){return typeof e=="object"&&e!==null&&typeof e.archiveId=="number"&&typeof e.chapterId=="number"&&typeof e.kind=="number"&&typeof e.rank=="number"&&typeof e.sentenceIndex=="number"}var rw=l(()=>{"use strict"});var nw,Vi=l(()=>{"use strict";nw=`
|
|
1768
|
+
CREATE TABLE IF NOT EXISTS search_sessions (
|
|
1769
|
+
session_id TEXT PRIMARY KEY,
|
|
1770
|
+
archive_key TEXT NOT NULL,
|
|
1771
|
+
query TEXT NOT NULL,
|
|
1772
|
+
options_json TEXT NOT NULL,
|
|
1773
|
+
terms_json TEXT NOT NULL,
|
|
1774
|
+
lens TEXT NOT NULL,
|
|
1775
|
+
match TEXT NOT NULL,
|
|
1776
|
+
object_caches_populated INTEGER NOT NULL DEFAULT 0,
|
|
1777
|
+
created_at INTEGER NOT NULL,
|
|
1778
|
+
expires_at INTEGER NOT NULL,
|
|
1779
|
+
accessed_at INTEGER NOT NULL
|
|
1780
|
+
);
|
|
1781
|
+
|
|
1782
|
+
CREATE TABLE IF NOT EXISTS search_results (
|
|
1783
|
+
session_id TEXT NOT NULL,
|
|
1784
|
+
rank INTEGER NOT NULL,
|
|
1785
|
+
item_json TEXT NOT NULL,
|
|
1786
|
+
PRIMARY KEY (session_id, rank)
|
|
1787
|
+
);
|
|
1788
|
+
|
|
1789
|
+
CREATE TABLE IF NOT EXISTS predicate_dictionary (
|
|
1790
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1791
|
+
value TEXT NOT NULL UNIQUE
|
|
1792
|
+
);
|
|
1793
|
+
|
|
1794
|
+
CREATE TABLE IF NOT EXISTS search_evidence_hit_events (
|
|
1795
|
+
session_id TEXT NOT NULL,
|
|
1796
|
+
archive_id INTEGER NOT NULL,
|
|
1797
|
+
evidence_kind INTEGER NOT NULL,
|
|
1798
|
+
evidence_id TEXT NOT NULL,
|
|
1799
|
+
chapter_id INTEGER NOT NULL,
|
|
1800
|
+
sentence_index INTEGER NOT NULL,
|
|
1801
|
+
score REAL NOT NULL,
|
|
1802
|
+
PRIMARY KEY (
|
|
1803
|
+
session_id,
|
|
1804
|
+
archive_id,
|
|
1805
|
+
evidence_kind,
|
|
1806
|
+
evidence_id,
|
|
1807
|
+
chapter_id,
|
|
1808
|
+
sentence_index
|
|
1809
|
+
)
|
|
1810
|
+
);
|
|
1811
|
+
|
|
1812
|
+
CREATE INDEX IF NOT EXISTS idx_search_evidence_hit_events_evidence_rank
|
|
1813
|
+
ON search_evidence_hit_events(session_id, archive_id, evidence_kind, evidence_id, score DESC, chapter_id, sentence_index);
|
|
1814
|
+
|
|
1815
|
+
CREATE INDEX IF NOT EXISTS idx_search_evidence_hit_events_sentence
|
|
1816
|
+
ON search_evidence_hit_events(session_id, archive_id, chapter_id, sentence_index, evidence_kind, evidence_id);
|
|
1817
|
+
|
|
1818
|
+
CREATE TABLE IF NOT EXISTS search_entity_hits (
|
|
1819
|
+
session_id TEXT NOT NULL,
|
|
1820
|
+
archive_id INTEGER NOT NULL,
|
|
1821
|
+
qid TEXT NOT NULL,
|
|
1822
|
+
property_top_scores_json TEXT NOT NULL DEFAULT '[]',
|
|
1823
|
+
evidence_top_scores_json TEXT NOT NULL DEFAULT '[]',
|
|
1824
|
+
property_score REAL NOT NULL DEFAULT 0,
|
|
1825
|
+
evidence_score REAL NOT NULL DEFAULT 0,
|
|
1826
|
+
result_score REAL NOT NULL DEFAULT 0,
|
|
1827
|
+
PRIMARY KEY (session_id, archive_id, qid)
|
|
1828
|
+
);
|
|
1829
|
+
|
|
1830
|
+
CREATE INDEX IF NOT EXISTS idx_search_entity_hits_rank
|
|
1831
|
+
ON search_entity_hits(session_id, result_score DESC, archive_id, qid);
|
|
1832
|
+
|
|
1833
|
+
CREATE TABLE IF NOT EXISTS search_triple_hits (
|
|
1834
|
+
session_id TEXT NOT NULL,
|
|
1835
|
+
archive_id INTEGER NOT NULL,
|
|
1836
|
+
subject_qid TEXT NOT NULL,
|
|
1837
|
+
predicate_id INTEGER NOT NULL,
|
|
1838
|
+
object_qid TEXT NOT NULL,
|
|
1839
|
+
evidence_top_scores_json TEXT NOT NULL DEFAULT '[]',
|
|
1840
|
+
result_score REAL NOT NULL DEFAULT 0,
|
|
1841
|
+
PRIMARY KEY (session_id, archive_id, subject_qid, predicate_id, object_qid),
|
|
1842
|
+
FOREIGN KEY (predicate_id) REFERENCES predicate_dictionary(id)
|
|
1843
|
+
);
|
|
1844
|
+
|
|
1845
|
+
CREATE INDEX IF NOT EXISTS idx_search_triple_hits_rank
|
|
1846
|
+
ON search_triple_hits(session_id, result_score DESC, archive_id, subject_qid, predicate_id, object_qid);
|
|
1847
|
+
|
|
1848
|
+
CREATE TABLE IF NOT EXISTS search_chunk_hits (
|
|
1849
|
+
session_id TEXT NOT NULL,
|
|
1850
|
+
archive_id INTEGER NOT NULL,
|
|
1851
|
+
chunk_id INTEGER NOT NULL,
|
|
1852
|
+
property_top_scores_json TEXT NOT NULL DEFAULT '[]',
|
|
1853
|
+
evidence_top_scores_json TEXT NOT NULL DEFAULT '[]',
|
|
1854
|
+
property_score REAL NOT NULL DEFAULT 0,
|
|
1855
|
+
evidence_score REAL NOT NULL DEFAULT 0,
|
|
1856
|
+
result_score REAL NOT NULL DEFAULT 0,
|
|
1857
|
+
PRIMARY KEY (session_id, archive_id, chunk_id)
|
|
1858
|
+
);
|
|
1859
|
+
|
|
1860
|
+
CREATE INDEX IF NOT EXISTS idx_search_chunk_hits_rank
|
|
1861
|
+
ON search_chunk_hits(session_id, result_score DESC, archive_id, chunk_id);
|
|
1862
|
+
|
|
1863
|
+
CREATE INDEX IF NOT EXISTS idx_search_sessions_archive
|
|
1864
|
+
ON search_sessions(archive_key, session_id);
|
|
1865
|
+
|
|
1866
|
+
CREATE INDEX IF NOT EXISTS idx_search_sessions_expires
|
|
1867
|
+
ON search_sessions(expires_at);
|
|
1868
|
+
|
|
1869
|
+
CREATE INDEX IF NOT EXISTS idx_search_sessions_prune
|
|
1870
|
+
ON search_sessions(accessed_at DESC, created_at DESC, session_id);
|
|
1871
|
+
`});import{join as uQ}from"path";import{rm as kP}from"fs/promises";async function Ee(){let e=aw(),t=await V(e,nw);return iw===e?t:await pQ(t)?(iw=e,t):(await t.close(),await kP(e,{force:!0}),await kP(`${e}.initialized`,{force:!0}),iw=e,await V(e,nw))}function aw(){return uQ(lQ(),"search-sessions.sqlite")}function lQ(){return Rn()}async function pQ(e){return await $p(e,"search_entity_hits","archive_id")&&await $p(e,"search_chunk_hits","archive_id")&&await $p(e,"search_triple_hits","archive_id")&&await $p(e,"search_evidence_hit_events","archive_id")}async function $p(e,t,r){return(await e.queryAll(`PRAGMA table_info(${t})`,void 0,i=>String(i.name))).includes(r)}var iw,Co=l(()=>{"use strict";Ue();X();Vi()});function EP(e){let t=JSON.parse(e);if(typeof t!="object"||t===null)throw new Error("Invalid cached search result.");return t}function IP(e){let t=JSON.parse(e);if(typeof t=="object"&&t!==null&&"chapters"in t&&"types"in t&&(t.chapters===null||Array.isArray(t.chapters)&&t.chapters.every(r=>typeof r=="number"))&&(t.types===null||Array.isArray(t.types)&&t.types.every(r=>typeof r=="string")))return{chapters:t.chapters,types:t.types};throw new Error("Invalid cached search session.")}function xP(e){let t=JSON.parse(e);if(Array.isArray(t)&&t.every(r=>typeof r=="string"))return t;throw new Error("Invalid cached search session.")}function To(e){let t=JSON.parse(e);return Array.isArray(t)&&t.every(r=>typeof r=="number"&&Number.isFinite(r))?t.map(r=>Number(r)):[]}var Bp=l(()=>{"use strict"});async function ow(e,t){let r=await Ee();try{return await r.queryOne(`
|
|
1872
|
+
SELECT session_id
|
|
1873
|
+
FROM search_sessions
|
|
1874
|
+
WHERE session_id = ? AND archive_key = ?
|
|
1875
|
+
AND expires_at >= ?
|
|
1876
|
+
`,[e,t,Date.now()],()=>!0)===!0}finally{await r.close()}}async function Yc(e,t,r,n){let i=await e.queryOne(`
|
|
1877
|
+
SELECT
|
|
1878
|
+
session_id,
|
|
1879
|
+
query,
|
|
1880
|
+
options_json,
|
|
1881
|
+
terms_json,
|
|
1882
|
+
lens,
|
|
1883
|
+
match,
|
|
1884
|
+
object_caches_populated,
|
|
1885
|
+
created_at,
|
|
1886
|
+
expires_at
|
|
1887
|
+
FROM search_sessions
|
|
1888
|
+
WHERE session_id = ?
|
|
1889
|
+
${r===void 0?"":"AND archive_key = ?"}
|
|
1890
|
+
${n===void 0?"":"AND created_at = ?"}
|
|
1891
|
+
`,[t,...r===void 0?[]:[r],...n===void 0?[]:[n]],a=>({createdAt:y(a,"created_at"),expiresAt:y(a,"expires_at"),lens:k(a,"lens"),match:k(a,"match"),objectCachesPopulated:y(a,"object_caches_populated")!==0,options:IP(k(a,"options_json")),query:k(a,"query"),sessionId:k(a,"session_id"),terms:xP(k(a,"terms_json"))}));if(i===void 0||i.expiresAt<Date.now())throw new Error("Search cursor expired. Run the search again.");return i}async function Zc(e,t){await e.run(`
|
|
1892
|
+
UPDATE search_sessions
|
|
1893
|
+
SET accessed_at = ?, expires_at = ?
|
|
1894
|
+
WHERE session_id = ?
|
|
1895
|
+
`,[Date.now(),Date.now()+6048e5,t])}async function Ro(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]),await e.run("DELETE FROM search_sessions WHERE session_id = ?",[t])}async function sw(e){await e.run(`
|
|
1896
|
+
DELETE FROM predicate_dictionary
|
|
1897
|
+
WHERE NOT EXISTS (
|
|
1898
|
+
SELECT 1
|
|
1899
|
+
FROM search_triple_hits
|
|
1900
|
+
WHERE search_triple_hits.predicate_id = predicate_dictionary.id
|
|
1901
|
+
)
|
|
1902
|
+
`)}var cw=l(()=>{"use strict";M();Co();Bp();Vi()});import{stat as hQ}from"fs/promises";async function $t(e){let t=await Ee();try{let r=await t.queryAll(`
|
|
1903
|
+
SELECT session_id
|
|
1904
|
+
FROM search_sessions
|
|
1905
|
+
WHERE archive_key = ?
|
|
1906
|
+
`,[e],n=>k(n,"session_id"));await t.transaction(async()=>{for(let n of r)await Ro(t,n);await sw(t)})}finally{await t.close()}}async function qp(e){let t=aw(),r=await vP(t),n=await Ee();try{let i=await n.queryOne("SELECT COUNT(*) AS count FROM search_sessions",void 0,d=>y(d,"count")),a=e.force?await gQ(n):await fQ(n,e.now),o=e.force?[]:await yQ(n),s=[...new Set([...a,...o])].sort();!e.dryRun&&s.length>0&&(await n.transaction(async()=>{for(let d of s)await Ro(n,d);await sw(n)}),await n.run("VACUUM"));let c=await vP(t);return{freedBytes:Math.max(0,r-c),removed:s.length,scanned:i??0}}finally{await n.close()}}async function fQ(e,t){return await e.queryAll(`
|
|
1907
|
+
SELECT session_id
|
|
1908
|
+
FROM search_sessions
|
|
1909
|
+
WHERE expires_at < ?
|
|
1910
|
+
`,[t],r=>k(r,"session_id"))}async function gQ(e){return await e.queryAll("SELECT session_id FROM search_sessions",void 0,t=>k(t,"session_id"))}async function yQ(e){return await e.queryAll(`
|
|
1911
|
+
SELECT session_id
|
|
1912
|
+
FROM search_sessions
|
|
1913
|
+
ORDER BY accessed_at DESC, created_at DESC, session_id
|
|
1914
|
+
LIMIT -1 OFFSET ?
|
|
1915
|
+
`,[500],t=>k(t,"session_id"))}async function vP(e){try{return(await hQ(e)).size}catch(t){if(R(t)&&t.code==="ENOENT")return 0;throw t}}var CP=l(()=>{"use strict";M();H();Co();Vi();cw()});function Po(e,t){return[...e,...t].filter(r=>Number.isFinite(r)).sort((r,n)=>n-r).slice(0,10)}function _o(e){return e.slice(0,10).reduce((t,r,n)=>t+r/Math.log2(n+2),0)}var RP=l(()=>{"use strict";Vi()});var Kn,dw=l(()=>{"use strict";Kn={mention:1,mentionLink:2,chunk:3}});async function Kp(e,t,r){await e.run(`
|
|
1916
|
+
INSERT OR REPLACE INTO search_evidence_hit_events (
|
|
1917
|
+
session_id,
|
|
1918
|
+
archive_id,
|
|
1919
|
+
evidence_kind,
|
|
1920
|
+
evidence_id,
|
|
1921
|
+
chapter_id,
|
|
1922
|
+
sentence_index,
|
|
1923
|
+
score
|
|
1924
|
+
)
|
|
1925
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1926
|
+
`,[t,r.archiveId??Bt,r.evidenceKind,r.evidenceId,r.chapterId,r.sentenceIndex,r.score])}async function Jp(e,t,r){let n=await e.queryOne(`
|
|
1927
|
+
SELECT property_top_scores_json, evidence_top_scores_json
|
|
1928
|
+
FROM search_entity_hits
|
|
1929
|
+
WHERE session_id = ? AND archive_id = ? AND qid = ?
|
|
1930
|
+
`,[t,r.archiveId??Bt,r.qid],d=>({evidenceTopScores:To(k(d,"evidence_top_scores_json")),propertyTopScores:To(k(d,"property_top_scores_json"))})),i=Po(n?.propertyTopScores??[],r.propertyTopScores??[]),a=Po(n?.evidenceTopScores??[],r.evidenceTopScores??[]),o=_o(i),s=_o(a),c=o+s;await e.run(`
|
|
1931
|
+
INSERT INTO search_entity_hits (
|
|
1932
|
+
session_id,
|
|
1933
|
+
archive_id,
|
|
1934
|
+
qid,
|
|
1935
|
+
property_top_scores_json,
|
|
1936
|
+
evidence_top_scores_json,
|
|
1937
|
+
property_score,
|
|
1938
|
+
evidence_score,
|
|
1939
|
+
result_score
|
|
1940
|
+
)
|
|
1941
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1942
|
+
ON CONFLICT(session_id, archive_id, qid) DO UPDATE SET
|
|
1943
|
+
property_top_scores_json = excluded.property_top_scores_json,
|
|
1944
|
+
evidence_top_scores_json = excluded.evidence_top_scores_json,
|
|
1945
|
+
property_score = excluded.property_score,
|
|
1946
|
+
evidence_score = excluded.evidence_score,
|
|
1947
|
+
result_score = excluded.result_score
|
|
1948
|
+
`,[t,r.archiveId??Bt,r.qid,JSON.stringify(i),JSON.stringify(a),o,s,c])}async function zp(e,t,r){let n=await bQ(e,r.predicate),i=await e.queryOne(`
|
|
1949
|
+
SELECT evidence_top_scores_json
|
|
1950
|
+
FROM search_triple_hits
|
|
1951
|
+
WHERE session_id = ?
|
|
1952
|
+
AND archive_id = ?
|
|
1953
|
+
AND subject_qid = ?
|
|
1954
|
+
AND predicate_id = ?
|
|
1955
|
+
AND object_qid = ?
|
|
1956
|
+
`,[t,r.archiveId??Bt,r.subjectQid,n,r.objectQid],s=>({evidenceTopScores:To(k(s,"evidence_top_scores_json"))})),a=Po(i?.evidenceTopScores??[],r.evidenceTopScores),o=_o(a);await e.run(`
|
|
1957
|
+
INSERT INTO search_triple_hits (
|
|
1958
|
+
session_id,
|
|
1959
|
+
archive_id,
|
|
1960
|
+
subject_qid,
|
|
1961
|
+
predicate_id,
|
|
1962
|
+
object_qid,
|
|
1963
|
+
evidence_top_scores_json,
|
|
1964
|
+
result_score
|
|
1965
|
+
)
|
|
1966
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1967
|
+
ON CONFLICT(session_id, archive_id, subject_qid, predicate_id, object_qid)
|
|
1968
|
+
DO UPDATE SET
|
|
1969
|
+
evidence_top_scores_json = excluded.evidence_top_scores_json,
|
|
1970
|
+
result_score = excluded.result_score
|
|
1971
|
+
`,[t,r.archiveId??Bt,r.subjectQid,n,r.objectQid,JSON.stringify(a),o])}async function Qp(e,t,r){let n=await e.queryOne(`
|
|
1972
|
+
SELECT property_top_scores_json, evidence_top_scores_json
|
|
1973
|
+
FROM search_chunk_hits
|
|
1974
|
+
WHERE session_id = ? AND archive_id = ? AND chunk_id = ?
|
|
1975
|
+
`,[t,r.archiveId??Bt,r.chunkId],d=>({evidenceTopScores:To(k(d,"evidence_top_scores_json")),propertyTopScores:To(k(d,"property_top_scores_json"))})),i=Po(n?.propertyTopScores??[],r.propertyTopScores??[]),a=Po(n?.evidenceTopScores??[],r.evidenceTopScores??[]),o=_o(i),s=_o(a),c=o+s;await e.run(`
|
|
1976
|
+
INSERT INTO search_chunk_hits (
|
|
1977
|
+
session_id,
|
|
1978
|
+
archive_id,
|
|
1979
|
+
chunk_id,
|
|
1980
|
+
property_top_scores_json,
|
|
1981
|
+
evidence_top_scores_json,
|
|
1982
|
+
property_score,
|
|
1983
|
+
evidence_score,
|
|
1984
|
+
result_score
|
|
1985
|
+
)
|
|
1986
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1987
|
+
ON CONFLICT(session_id, archive_id, chunk_id) DO UPDATE SET
|
|
1988
|
+
property_top_scores_json = excluded.property_top_scores_json,
|
|
1989
|
+
evidence_top_scores_json = excluded.evidence_top_scores_json,
|
|
1990
|
+
property_score = excluded.property_score,
|
|
1991
|
+
evidence_score = excluded.evidence_score,
|
|
1992
|
+
result_score = excluded.result_score
|
|
1993
|
+
`,[t,r.archiveId??Bt,r.chunkId,JSON.stringify(i),JSON.stringify(a),o,s,c])}async function uw(e,t,r){if(t.length===0)return[];let n=await Ee();try{let i=t.map(()=>"?").join(", ");return await n.queryAll(`
|
|
1994
|
+
SELECT
|
|
1995
|
+
event.evidence_id AS mention_id,
|
|
1996
|
+
event.score AS score
|
|
1997
|
+
FROM search_evidence_hit_events AS event
|
|
1998
|
+
WHERE event.session_id = ?
|
|
1999
|
+
AND event.evidence_kind = ?
|
|
2000
|
+
AND event.evidence_id IN (${i})
|
|
2001
|
+
ORDER BY event.score DESC, event.archive_id, event.chapter_id,
|
|
2002
|
+
event.sentence_index, event.evidence_id
|
|
2003
|
+
LIMIT ?
|
|
2004
|
+
`,[e,Kn.mention,...t,r],a=>({mentionId:k(a,"mention_id"),score:y(a,"score")}))}finally{await n.close()}}function lw(e){let t=y(e,"archive_id"),r=k(e,"qid");return{evidence:{nextCursor:null,shown:0,sources:[],total:0},field:"title",id:`wikg://entity/${r}`,...t===Bt?{}:{archiveId:t},score:y(e,"result_score"),snippet:r,title:r,type:"entity"}}async function AP(e,t,r,n){return await e.queryAll(`
|
|
2005
|
+
SELECT
|
|
2006
|
+
archive_id,
|
|
2007
|
+
qid,
|
|
2008
|
+
result_score
|
|
2009
|
+
FROM search_entity_hits
|
|
2010
|
+
WHERE session_id = ?
|
|
2011
|
+
${r===void 0?"":`
|
|
2012
|
+
AND (
|
|
2013
|
+
result_score < ?
|
|
2014
|
+
OR (
|
|
2015
|
+
result_score = ?
|
|
2016
|
+
AND (
|
|
2017
|
+
archive_id > ?
|
|
2018
|
+
OR (
|
|
2019
|
+
archive_id = ?
|
|
2020
|
+
AND (
|
|
2021
|
+
? < ?
|
|
2022
|
+
OR (? = ? AND qid > ?)
|
|
2023
|
+
)
|
|
2024
|
+
)
|
|
2025
|
+
)
|
|
2026
|
+
)
|
|
2027
|
+
)
|
|
2028
|
+
`}
|
|
2029
|
+
ORDER BY result_score DESC, archive_id, qid
|
|
2030
|
+
LIMIT ?
|
|
2031
|
+
`,[t,...r===void 0?[]:[r.score,r.score,r.archiveId,r.archiveId,Ao(r.kind),ed.entity,Ao(r.kind),ed.entity,r.id],n],lw)}async function LP(e,t,r,n){return await e.queryAll(`
|
|
2032
|
+
SELECT
|
|
2033
|
+
search_triple_hits.archive_id AS archive_id,
|
|
2034
|
+
search_triple_hits.subject_qid AS subject_qid,
|
|
2035
|
+
predicate_dictionary.value AS predicate,
|
|
2036
|
+
search_triple_hits.object_qid AS object_qid,
|
|
2037
|
+
search_triple_hits.result_score AS result_score
|
|
2038
|
+
FROM search_triple_hits
|
|
2039
|
+
JOIN predicate_dictionary
|
|
2040
|
+
ON predicate_dictionary.id = search_triple_hits.predicate_id
|
|
2041
|
+
WHERE search_triple_hits.session_id = ?
|
|
2042
|
+
${r===void 0?"":`
|
|
2043
|
+
AND (
|
|
2044
|
+
search_triple_hits.result_score < ?
|
|
2045
|
+
OR (
|
|
2046
|
+
search_triple_hits.result_score = ?
|
|
2047
|
+
AND (
|
|
2048
|
+
search_triple_hits.archive_id > ?
|
|
2049
|
+
OR (
|
|
2050
|
+
search_triple_hits.archive_id = ?
|
|
2051
|
+
AND (
|
|
2052
|
+
? < ?
|
|
2053
|
+
OR (
|
|
2054
|
+
? = ?
|
|
2055
|
+
AND (
|
|
2056
|
+
search_triple_hits.subject_qid || char(31) ||
|
|
2057
|
+
predicate_dictionary.value || char(31) ||
|
|
2058
|
+
search_triple_hits.object_qid
|
|
2059
|
+
) > ?
|
|
2060
|
+
)
|
|
2061
|
+
)
|
|
2062
|
+
)
|
|
2063
|
+
)
|
|
2064
|
+
)
|
|
2065
|
+
)
|
|
2066
|
+
`}
|
|
2067
|
+
ORDER BY search_triple_hits.result_score DESC,
|
|
2068
|
+
search_triple_hits.archive_id,
|
|
2069
|
+
search_triple_hits.subject_qid,
|
|
2070
|
+
predicate_dictionary.value,
|
|
2071
|
+
search_triple_hits.object_qid
|
|
2072
|
+
LIMIT ?
|
|
2073
|
+
`,[t,...r===void 0?[]:[r.score,r.score,r.archiveId,r.archiveId,Ao(r.kind),ed.triple,Ao(r.kind),ed.triple,r.id],n],i=>{let a=y(i,"archive_id"),o=k(i,"subject_qid"),s=k(i,"predicate"),c=k(i,"object_qid"),d=`${o} ${s} ${c}`;return{field:"content",id:`wikg://triple/${o}/${encodeURIComponent(s)}/${c}`,...a===Bt?{}:{archiveId:a},score:y(i,"result_score"),snippet:d,title:d,triple:{objectLabel:c,predicate:s,subjectLabel:o},type:"triple"}})}function OP(e,t){return(t.score??0)-(e.score??0)||(e.archiveId??Bt)-(t.archiveId??Bt)||Ao(PP(e))-Ao(PP(t))||_P(e).localeCompare(_P(t))}async function bQ(e,t){await e.run(`
|
|
2074
|
+
INSERT OR IGNORE INTO predicate_dictionary(value)
|
|
2075
|
+
VALUES (?)
|
|
2076
|
+
`,[t]);let r=await e.queryOne(`
|
|
2077
|
+
SELECT id
|
|
2078
|
+
FROM predicate_dictionary
|
|
2079
|
+
WHERE value = ?
|
|
2080
|
+
`,[t],n=>y(n,"id"));if(r===void 0)throw new Error("Failed to create predicate dictionary entry.");return r}function PP(e){return e.type==="triple"?"triple":"entity"}function _P(e){return e.type!=="triple"?e.id.slice(14):[e.triple?.subjectLabel??"",e.triple?.predicate??"",e.triple?.objectLabel??""].join("")}function Ao(e){return ed[e]}var Bt,ed,Vp=l(()=>{"use strict";M();Co();Bp();RP();dw();Bt=0;ed={entity:1,triple:2}});import{createHash as jP}from"crypto";function pw(e){return jP("sha256").update(JSON.stringify({archiveKey:e.archiveKey,entity:!0,lens:e.lens,match:e.match,order:e.order,rankingVersion:7,revisionScope:e.revisionScope,scope:DP(e.chapters),terms:e.terms,types:MP(e.types)})).digest("hex")}function mw(e){return jP("sha256").update(JSON.stringify({archiveKey:e.archiveKey,entity:!1,lens:e.lens,match:e.match,order:e.order,rankingVersion:7,revisionScope:e.revisionScope,scope:DP(e.chapters),terms:e.terms,types:MP(e.types)})).digest("hex")}function DP(e){return e===null?null:[...new Set(e)].sort(wQ)}function MP(e){return e===null?null:[...new Set(e)].sort()}function wQ(e,t){return e-t}var WP=l(()=>{"use strict";Vi()});async function Xp(e,t,r){let n=mw(e);if(await ow(n,e.archiveKey))return await rd(n,t,r,e.archiveKey)}async function hw(e,t,r){let n=pw(e);if(await ow(n,e.archiveKey))return await nd(n,t,r,e.archiveKey)}async function Xi(e){let t=await Ee();try{let r=Date.now(),n=mw(e),i=JSON.stringify({chapters:e.chapters,order:e.order,types:e.types});return await t.transaction(async()=>{await Ro(t,n),await t.run(`
|
|
2081
|
+
INSERT INTO search_sessions (
|
|
2082
|
+
session_id, archive_key, query, options_json, terms_json, lens,
|
|
2083
|
+
match, created_at, expires_at, accessed_at
|
|
2084
|
+
)
|
|
2085
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2086
|
+
`,[n,e.archiveKey,e.query,i,JSON.stringify(e.terms),e.lens,e.match,r,r+6048e5,r]);for(let[a,o]of(e.items??[]).entries())await t.run(`
|
|
2087
|
+
INSERT INTO search_results (session_id, rank, item_json)
|
|
2088
|
+
VALUES (?, ?, ?)
|
|
2089
|
+
`,[n,a,JSON.stringify(o)]);for(let a of e.evidenceEvents??[])await Kp(t,n,a);for(let a of e.entityHits??[])await Jp(t,n,a);for(let a of e.tripleHits??[])await zp(t,n,a);for(let a of e.chunkHits??[])await Qp(t,n,a)}),n}finally{await t.close()}}async function fw(e){let t=await Ee();try{let r=Date.now(),n=pw(e),i=JSON.stringify({chapters:e.chapters,order:e.order,types:e.types});return await t.transaction(async()=>{await Ro(t,n),await t.run(`
|
|
2090
|
+
INSERT INTO search_sessions (
|
|
2091
|
+
session_id, archive_key, query, options_json, terms_json, lens,
|
|
2092
|
+
match, created_at, expires_at, accessed_at
|
|
2093
|
+
)
|
|
2094
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2095
|
+
`,[n,e.archiveKey,e.query,i,JSON.stringify(e.terms),e.lens,e.match,r,r+6048e5,r]);for(let a of e.evidenceEvents??[])await Kp(t,n,a);for(let a of e.entityHits??[])await Jp(t,n,a);for(let a of e.tripleHits??[])await zp(t,n,a);for(let a of e.chunkHits??[])await Qp(t,n,a)}),n}finally{await t.close()}}async function td(e){let t=await Ee();try{await t.transaction(async()=>{for(let r of e.evidenceEvents??[])await Kp(t,e.sessionId,r);for(let r of e.entityHits??[])await Jp(t,e.sessionId,r);for(let r of e.tripleHits??[])await zp(t,e.sessionId,r);for(let r of e.chunkHits??[])await Qp(t,e.sessionId,r);await t.run(`
|
|
2096
|
+
UPDATE search_sessions
|
|
2097
|
+
SET object_caches_populated = 1
|
|
2098
|
+
WHERE session_id = ?
|
|
2099
|
+
`,[e.sessionId])})}finally{await t.close()}}async function rd(e,t,r,n,i){let a=await Ee();try{let o=await Yc(a,e,n,i);await Zc(a,e);let s=await a.queryAll(`
|
|
2100
|
+
SELECT rank, item_json
|
|
2101
|
+
FROM search_results
|
|
2102
|
+
WHERE session_id = ? AND rank >= ?
|
|
2103
|
+
ORDER BY rank
|
|
2104
|
+
LIMIT ?
|
|
2105
|
+
`,[e,t,r+1],u=>({item:EP(k(u,"item_json")),rank:y(u,"rank")})),c=s.slice(0,r).map(u=>u.item),d=s.at(r-1);return{chapters:o.options.chapters,items:c,lens:o.lens,match:o.match,nextCursor:s.length>r&&d!==void 0?Up(e,d.rank+1,o.createdAt):null,query:o.query,sessionId:e,terms:o.terms,types:o.options.types}}finally{await a.close()}}async function nd(e,t,r,n,i){let a=await Ee();try{let o=await Yc(a,e,n,i);await Zc(a,e);let s=await a.queryAll(`
|
|
2106
|
+
SELECT
|
|
2107
|
+
archive_id,
|
|
2108
|
+
qid,
|
|
2109
|
+
result_score
|
|
2110
|
+
FROM search_entity_hits
|
|
2111
|
+
WHERE session_id = ?
|
|
2112
|
+
ORDER BY result_score DESC, archive_id, qid
|
|
2113
|
+
LIMIT ? OFFSET ?
|
|
2114
|
+
`,[e,r+1,t],lw),c=s.slice(0,r),d=t+c.length;return{chapters:o.options.chapters,items:c,lens:o.lens,match:o.match,nextCursor:s.length>r?Up(e,d,o.createdAt):null,query:o.query,sessionId:e,terms:o.terms,types:o.options.types}}finally{await a.close()}}async function Lo(e,t){let r=await Ee();try{let n=await Yc(r,e,t);return await Zc(r,e),{chapters:n.options.chapters,createdAt:n.createdAt,lens:n.lens,match:n.match,objectCachesPopulated:n.objectCachesPopulated,query:n.query,sessionId:e,terms:n.terms,types:n.options.types}}finally{await r.close()}}async function id(e,t,r){let n=await Ee();try{let i=await Yc(n,e,t,r);return await Zc(n,e),{chapters:i.options.chapters,createdAt:i.createdAt,lens:i.lens,match:i.match,objectCachesPopulated:i.objectCachesPopulated,query:i.query,sessionId:e,terms:i.terms,types:i.options.types}}finally{await n.close()}}var FP=l(()=>{"use strict";M();rw();Co();Vp();WP();Bp();Vi();cw()});async function ad(e,t,r,n){let i=await Ee();try{let a=await AP(i,e,r,n+1),o=await LP(i,e,r,n+1);return[...a,...o].sort(OP).slice(0,n+1)}finally{await i.close()}}async function od(e,t,r){let n=await Ee();try{return await n.queryAll(`
|
|
2115
|
+
SELECT
|
|
2116
|
+
archive_id,
|
|
2117
|
+
chunk_id,
|
|
2118
|
+
result_score
|
|
2119
|
+
FROM search_chunk_hits
|
|
2120
|
+
WHERE session_id = ?
|
|
2121
|
+
${t===void 0?"":`
|
|
2122
|
+
AND (
|
|
2123
|
+
result_score < ?
|
|
2124
|
+
OR (
|
|
2125
|
+
result_score = ?
|
|
2126
|
+
AND (
|
|
2127
|
+
archive_id > ?
|
|
2128
|
+
OR (archive_id = ? AND chunk_id > ?)
|
|
2129
|
+
)
|
|
2130
|
+
)
|
|
2131
|
+
)
|
|
2132
|
+
`}
|
|
2133
|
+
ORDER BY result_score DESC, archive_id, chunk_id
|
|
2134
|
+
LIMIT ?
|
|
2135
|
+
`,[e,...t===void 0?[]:[t.score,t.score,t.archiveId,t.archiveId,t.chunkId],r+1],i=>{let a=y(i,"archive_id"),o=y(i,"chunk_id");return{field:"title",id:`wikg://chunk/${o}`,...a===0?{}:{archiveId:a},score:y(i,"result_score"),snippet:`chunk ${o}`,title:`chunk ${o}`,type:"node"}})}finally{await n.close()}}var GP=l(()=>{"use strict";M();Co();Vp()});var Yi=l(()=>{"use strict";rw();CP();Vp();FP();GP();dw()});var Oo,fr,Yp=l(()=>{"use strict";Oo="meta:root",fr={lenses:{chapter:"book outline and chapter titles",chunk:"source text ranges",entity:"indexed entities",node:"topology / LLM Wiki structure",triple:"knowledge graph statements"},message:"Choose scope URI lenses such as /chapter, /chunk, /entity, or /triple for broad search."}});function ut(e,t){let r=e.replace(/\s+/g," ").trim();if(t===void 0)return r.length>180?`${r.slice(0,177)}...`:r;let n=r.toLowerCase().indexOf(t);if(n<0)return r.length>180?`${r.slice(0,177)}...`:r;let i=Math.max(0,n-60),a=Math.min(r.length,n+t.length+120),o=i===0?"":"...",s=a===r.length?"":"...";return`${o}${r.slice(i,a)}${s}`}function UP(e){return e===void 0?"[missing]":[e.title,e.authors.join(", "),e.publisher].filter(t=>t!==null&&t!=="").join(" / ")}function $P(e){return e?.title??"Archive metadata"}function BP(e){return{...e?.authors===void 0||e.authors.length===0?{}:{authors:e.authors},...e?.description===void 0||e.description===null?{}:{description:e.description},...e?.publisher===void 0||e.publisher===null?{}:{publisher:e.publisher},title:$P(e)}}function No(e){return e!=null}var HP=l(()=>{"use strict"});function gw(e){return e.trim().toLowerCase().split(/\s+/u).filter(t=>t!=="")}function qP(e){return e==="chapter"||e==="chapter-title"||e==="entity"||e==="fragment"||e==="meta"||e==="node"||e==="source"||e==="summary"||e==="triple"}function KP(e){return e==="chapter"||e==="chapter-title"||e==="entity"||e==="fragment"||e==="meta"||e==="node"||e==="source"||e==="summary"||e==="triple"}function jo(e){if(e==="broad"||e==="exact"||e==="typed")return e;throw new Error("Invalid cached search session.")}function on(e){if(e==="all"||e==="any")return e;throw new Error("Invalid cached search session.")}function Do(e){return e===null?null:e.map(t=>{if(t==="entity"||t==="fragment"||t==="meta"||t==="node"||t==="source"||t==="summary"||t==="chapter"||t==="chapter-title"||t==="triple")return t;throw new Error("Invalid cached search session.")})}function Jn(e){return Buffer.from(JSON.stringify({offset:e,v:1})).toString("base64url")}function Ht(e){if(e===void 0)return 0;try{let t=JSON.parse(Buffer.from(e,"base64url").toString("utf8"));if(typeof t=="object"&&t!==null&&"v"in t&&"offset"in t&&t.v===1&&Number.isInteger(t.offset)&&typeof t.offset=="number"&&t.offset>=0)return t.offset}catch{throw new Error("Invalid search cursor.")}throw new Error("Invalid search cursor.")}function JP(e){try{return Ht(e),!0}catch{return!1}}var yw=l(()=>{"use strict"});function sd(e){return e.position?.chapter??Number.MAX_SAFE_INTEGER}function cd(e){return e.position?.documentOrder??e.position?.chapter??Number.MAX_SAFE_INTEGER}function dd(e){return e.position?.fragment??0}function ud(e){return e.position?.sentence??0}function ld(e){switch(e){case"chapter-title":case"chapter":return 0;case"chapter-tree":return 1;case"entity":return 2;case"triple":return 3;case"summary":return 4;case"node":return 5;case"source":return 6;case"fragment":return 6;case"meta":return 7}}function lt(e,t){let[r]=[...e].sort(Zi);return r===void 0?void 0:pd(r,t)}function pd(e,t){return{chapter:e[0],documentOrder:t?.get(e[0])??e[0],fragment:e[1],sentence:e[1]}}function Zi(e,t,r){return D(r?.get(e[0])??e[0],r?.get(t[0])??t[0])||D(e[0],t[0])||D(e[1],t[1])}function Mo(e,t){return D(e.documentOrder??e.chapter,t.documentOrder??t.chapter)||D(e.chapter,t.chapter)||D(e.fragment??0,t.fragment??0)||D(e.sentence??0,t.sentence??0)}function D(e,t){return e===t?0:e<t?-1:1}var Zp=l(()=>{"use strict"});function QP(e,t,r){let n=r==="doc-asc"?1:-1,i=D(zP(e.type),zP(t.type))||D(t.score??0,e.score??0)||D(t.matchCount??0,e.matchCount??0),a=D(cd(e),cd(t))||D(sd(e),sd(t))||D(dd(e),dd(t))||D(ud(e),ud(t))||D(ld(e.type),ld(t.type))||e.id.localeCompare(t.id);return i||a*n}function ea(e,t,r){let n=r==="doc-asc"?1:-1,i=D(em(e.type),em(t.type))||SQ(e,t);return i!==0?i:kQ(e,t)*n}function SQ(e,t){let r=em(e.type);return r!==em(t.type)?0:r===0?D(t.score??0,e.score??0):0}function kQ(e,t){return D(cd(e),cd(t))||D(sd(e),sd(t))||D(dd(e),dd(t))||D(ud(e),ud(t))||D(ld(e.type),ld(t.type))||D(e.archiveId??0,t.archiveId??0)||e.id.localeCompare(t.id)}function em(e){switch(e){case"entity":case"triple":return 0;case"node":return 1;case"summary":return 2;case"source":case"fragment":return 3;case"chapter-title":case"chapter":case"chapter-tree":case"meta":return 4}}function zP(e){switch(e){case"chapter-title":return 0;case"entity":case"triple":return 1;case"node":return 2;case"source":case"summary":return 3;case"chapter":case"chapter-tree":case"meta":case"fragment":throw new Error(`Unsupported search result bucket type: ${e}`)}}var bw=l(()=>{"use strict";Zp()});function ww(e){return{matchCount:e.matchCount,matchedTerms:e.matchedTerms,missingTerms:e.missingTerms,score:e.score}}function zn(e){let t=[...e].filter(i=>i>0).sort((i,a)=>a-i).slice(0,VP),[r]=t;if(r===void 0)return 0;let n=EQ/IQ();return t.reduce((i,a,o)=>i+a*(o===0?1:n/Math.log2(o+2)),0)}function IQ(){let e=0;for(let t=2;t<=VP;t+=1)e+=1/Math.log2(t+1);return e}function tm(e,t){let r=(t.score??0)-(e.score??0);return r!==0?r:e.position===void 0?t.position===void 0?0:1:t.position===void 0?-1:Mo(e.position,t.position)}var VP,EQ,Sw=l(()=>{"use strict";Zp();VP=10,EQ=.3});function Qn(e,t,r,n=gw(e),i=r.types===void 0?"broad":"typed"){let a=rm(e,t,r,n,i),o=Ht(r.cursor),s=a.items.slice(o,o+a.limit),c=o+s.length;return{...a,items:s,nextCursor:c<a.items.length?Jn(c):null}}function rm(e,t,r,n=gw(e),i=r.types===void 0?"broad":"typed"){let a=r.order??"doc-asc",o=r.limit??20,s=r.chapters??null,c=r.match??"any",d=r.types??null,u=r.ids??null,p=xQ(t).filter(m=>XP(m,u)).filter(m=>YP(m,s)).filter(m=>CQ(m,d)).filter(m=>ZP(m,r.triplePattern)).sort((m,f)=>QP(m,f,a));return{chapters:s,items:p,lens:i,lensHint:i==="broad"?fr:null,limit:o,match:c,nextCursor:null,order:a,query:e,terms:n,types:d}}function xQ(e){let t=new Map;for(let r of e){let n=t.get(r.id)??[];n.push(r),t.set(r.id,n)}return[...t.values()].map(vQ)}function vQ(e){let t=[...e].sort(tm),[r]=t;if(r===void 0)throw new Error("Internal error: search result candidate is empty.");return t.length===1?r:{...r,matchCount:Math.max(...t.map(n=>n.matchCount??0)),matchedTerms:md(t.flatMap(n=>n.matchedTerms??[])),missingTerms:md(t.flatMap(n=>n.missingTerms??[])),score:zn(t.map(n=>n.score??0))}}function md(e){return[...new Set(e)]}function nm(e,t){let r=t.order??"doc-asc",n=t.limit??20,i=t.chapters??null,a=t.ids??null,o=t.types??null,s=Ht(t.cursor),c=e.filter(p=>XP(p,a)).filter(p=>YP(p,i)).filter(p=>TQ(p,o)).filter(p=>ZP(p,t.triplePattern)).sort((p,m)=>ea(p,m,r)),d=c.slice(s,s+n),u=s+d.length;return{chapters:i,ids:a,items:d,limit:n,nextCursor:u<c.length?Jn(u):null,order:r,types:o}}function XP(e,t){return t===null||t.includes(e.id)}function YP(e,t){return t===null?!0:e.chapter!==void 0&&t.includes(e.chapter)}function CQ(e,t){return t===null||e.type==="chapter-title"&&t.includes("chapter")?!0:qP(e.type)&&t.includes(e.type)}function TQ(e,t){return t===null||KP(e.type)&&t.includes(e.type)}function ZP(e,t){if(t===void 0||e.type!=="triple")return!0;let r=RQ(e.id);return r===void 0?!1:(t.subjectQid===void 0||t.subjectQid===r.subjectQid)&&(t.predicate===void 0||t.predicate===r.predicate)&&(t.objectQid===void 0||t.objectQid===r.objectQid)}function RQ(e){let t=/^wikg:\/\/triple\/(Q[1-9][0-9]*)\/([^/]+)\/(Q[1-9][0-9]*)$/u.exec(e);if(!(t?.[1]===void 0||t[2]===void 0||t[3]===void 0))return{objectQid:t[3],predicate:decodeURIComponent(t[2]),subjectQid:t[1]}}var im=l(()=>{"use strict";Yp();yw();bw();Sw()});function gr(e){return e.startsWith(ae)}var e_=l(()=>{"use strict";Dn()});var t_=l(()=>{"use strict";Yp();HP();yw();Zp();im();bw();Sw();e_()});var oe=l(()=>{"use strict";t_()});var kw=l(()=>{"use strict"});async function se(e,t){let r=await op(e),n=await Fy(e,r.items,t);if(n===void 0)throw new Error(`Chapter ${t} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);let i=await e.serials.getById(t),a=await e.readSummary(t);return{...n,graphReady:i?.topologyReady===!0,hasSummary:a!==void 0}}async function z(e){let t=await op(e);return await ap(e,t.items)}async function Vn(e){return{chapters:(await op(e)).items.flatMap(r=>np(r))}}async function ta(e,t){return await se(e,t)}var Ew=l(()=>{"use strict";Ic();Wy();kw()});async function Wo(e,t,r){let n=await se(e,t);if(n.stage!==r)throw new Error(`Chapter ${t} is ${n.stage}. Expected ${r} before committing build output.`)}var r_=l(()=>{"use strict";Ew()});var Ze=l(()=>{"use strict";Ew();r_();kw()});async function ra(e,t){for(let r=0;r<PQ;r+=1){let n=await Iw(e,t),i=mr(n);if(await rn(e,n)==="dirty"){let o=await Iw(e);if(mr(o)!==i)continue;await e.deleteSearchIndexDatabase()}await GR(e,n,t);let a=await Iw(e);if(mr(a)===i&&await rn(e,a)==="current")return;await e.deleteSearchIndexDatabase()}throw new Error("Archive changed while rebuilding search index; retry.")}async function qe(e){return await xw(e)==="current"}async function xw(e){return await rn(e)}async function Iw(e,t){let r=[],n=[];for await(let i of vw(e,ko,t))r.push(...i.objectProperties),n.push(...i.textSentences);return{objectProperties:r,textSentences:n}}async function*vw(e,t,r){let n=await z(e),i=0,a=fd();for(let o of n){let s=o.title??`[chapter ${o.chapterId}]`;a=om(a,{archiveId:t,chapterId:o.chapterId,ownerId:String(o.chapterId),ownerKind:J.chapter,propertyKind:je.title,text:s});for await(let c of o_(e,t,o.chapterId,"summary"))a=a_(a,c),gd(a)>=am&&(yield a,a=fd());for await(let c of o_(e,t,o.chapterId,"source"))a=a_(a,c),gd(a)>=am&&(yield a,a=fd());for(let c of await e.chunks.listBySerial(o.chapterId))a=om(a,{archiveId:t,chapterId:c.sentenceId[0],ownerId:String(c.id),ownerKind:J.chunk,propertyKind:je.label,text:c.label}),a=om(a,{archiveId:t,chapterId:c.sentenceId[0],ownerId:String(c.id),ownerKind:J.chunk,propertyKind:je.content,text:c.content}),gd(a)>=am&&(yield a,a=fd());for(let c of await e.mentions.listByChapter(o.chapterId))a=om(a,{archiveId:t,chapterId:c.chapterId,ownerId:c.qid,ownerKind:J.entity,propertyKind:je.surface,text:c.surface}),gd(a)>=am&&(yield a,a=fd());i+=1,await r?.({done:i,phase:"collecting",total:n.length,unit:"chapter"})}gd(a)>0&&(yield a)}function fd(){return{objectProperties:[],textSentences:[]}}function a_(e,t){return e.textSentences.push(t),e}function om(e,t){return e.objectProperties.push(t),e}function gd(e){return e.objectProperties.length+e.textSentences.length}async function*o_(e,t,r,n){let i=n==="summary"?e.getSummaryFragments(r):e.getSerialFragments(r),a=0;for(let o of await i.listFragmentIds()){let s=await i.getFragment(o);for(let c of s.sentences)yield{archiveId:t,chapterId:r,kind:n==="source"?be.source:be.summary,sentenceIndex:a,text:c.text,wordsCount:c.wordsCount},a+=1}}var PQ,am,yd=l(()=>{"use strict";Ze();qn();PQ=2,am=512});function Ct(e){return`chapter:${e}`}function Cw(e){return`chapter-title:${e}`}function he(e){return`node:${e}`}function aa(e){return`summary:${e}`}function wd(e,t){return`fragment:${e}:${t}`}function qt(e,t,r,n){let i=r+1,a=n+1,o=r===n?String(i):`${i}..${a}`;return`wikg://chapter/${e}/${t}#${o}`}function Tt(e){if(e=e,!gr(e)){let i=yr(e);switch(i.type){case"node":return{id:i.id,type:"chunk"};case"chapter":return{chapterId:i.id,type:"chapter"};case"chapter-title":return{chapterId:i.id,type:"chapter-title"};case"summary":return{chapterId:i.id,endSentenceIndex:Number.POSITIVE_INFINITY,stream:"summary",startSentenceIndex:0,type:"text-stream"};case"fragment":case"meta":throw new Error(`Evidence is not available for ${e}.`)}}if(e===ae)return{type:"meta"};let t=dt(e);if(t.protocol!=="wikg")throw new Error(`Invalid Wiki Graph URI: ${e}`);let r=t.path,n=AQ(t.fragment);if(r.length===0)return{type:"meta"};if(r[0]==="chapter-tree"&&r.length===1)return{type:"chapter-tree"};switch(r[0]){case"chapter":if(r.length===2)return{chapterId:ia(r[1],e),type:"chapter"};if(r[1]!==void 0){let i=ia(r[1],e);switch(r[2]){case"state":if(r.length===3)return{chapterId:i,type:"chapter-state"};if(r.length===4)return{chapterId:i,target:_Q(r[3],e),type:"chapter-state"};break;case"title":if(r.length===3)return{chapterId:i,type:"chapter-title"};break;case"chunk":if(r.length===4)return{chapterId:i,id:ia(r[3],e),type:"chunk"};break;case"entity":if(r.length===4)return{chapterId:i,qid:na(r[3],e),type:"entity"};break;case"source":if(r.length===3){let[a,o]=s_(n);return{chapterId:i,endSentenceIndex:o,stream:"source",startSentenceIndex:a,type:"text-stream"}}break;case"summary":if(r.length===3){let[a,o]=s_(n);return{chapterId:i,endSentenceIndex:o,stream:"summary",startSentenceIndex:a,type:"text-stream"}}break;case"tree":if(r.length===3)return{chapterId:i,type:"chapter"};break;case"triple":if(r.length===6)return{chapterId:i,objectQid:na(r[5],e),predicate:decodeURIComponent(r[4]??""),subjectQid:na(r[3],e),type:"triple"};break}}break;case"chunk":if(r.length===2)return{id:ia(r[1],e),type:"chunk"};break;case"entity":if(r.length===2)return{qid:na(r[1],e),type:"entity"};if(r.length===3&&r[2]==="wikipage")return{qid:na(r[1],e),type:"entity-wikipage"};break;case"triple":if(r.length===4)return{objectQid:na(r[3],e),predicate:decodeURIComponent(r[2]??""),subjectQid:na(r[1],e),type:"triple"};break}throw new Error(`Invalid Wiki Graph URI: ${e}`)}function na(e,t){if(e!==void 0&&/^Q[1-9][0-9]*$/u.test(e))return e;throw new Error(`Invalid Wiki Graph URI: ${t}`)}function _Q(e,t){if(e==="source"||e==="reading-graph"||e==="reading-summary"||e==="knowledge-graph")return e;throw new Error(`Invalid Wiki Graph URI: ${t}`)}function s_(e){if(e==="")return[0,Number.POSITIVE_INFINITY];let t=/^([1-9][0-9]*)(?:\.\.([1-9][0-9]*))?$/u.exec(e);if(t?.[1]===void 0)throw new Error(`Invalid source sentence range: ${e}`);let r=Number(t[1])-1,n=Number(t[2]??t[1])-1;if(Number.isInteger(r)&&r>=0&&Number.isInteger(n)&&n>=r)return[r,n];throw new Error(`Invalid source sentence range: ${e}`)}function AQ(e){return e===void 0?"":typeof e=="number"?String(e):`${e.begin}..${e.end}`}function yr(e){let t=e.trim(),[r,n]=t.split(":",2);if(r==="meta"&&(n==="book"||n==="root"))return{type:"meta"};if(r==="chapter"||r==="chapter-title"||r==="summary")return{id:ia(n,t),type:r};if(r==="node")return{id:ia(n,t),type:"node"};if(r==="fragment"){let i=t.slice(9).split(":");if(i.length!==2)throw new Error(`Invalid archive object id: ${e}`);return{fragmentId:LQ(i[1],t),serialId:ia(i[0],t),type:"fragment"}}throw new Error(`Invalid archive object id: ${e}`)}function ia(e,t){let r=Number(e);if(!Number.isInteger(r)||r<=0)throw new Error(`Invalid archive object id: ${t}`);return r}function LQ(e,t){let r=Number(e);if(!Number.isInteger(r)||r<0)throw new Error(`Invalid archive object id: ${t}`);return r}var De=l(()=>{"use strict";Dn();oe()});function Fo(e,t){return D(e.chapterId,t.chapterId)||D(e.sentenceIndex??0,t.sentenceIndex??0)||e.id.localeCompare(t.id)}async function Go(e,t,r){let n=t.get(r);if(n!==void 0)return n;let i=await e.mentions.getById(r);return i!==void 0&&t.set(r,i),i}async function sm(e){return await e.mentions.listAll()}function cm(e){return{match:{matchCount:0,matchedTerms:[],missingTerms:[],score:0},mention:e}}function br(e,t,r){return`wikg://triple/${e}/${encodeURIComponent(t)}/${r}`}function c_(e){return`wikg://entity/${e}`}function oa(e){let t=e,r=`${ae}entity/`;return t.startsWith(r)?t.slice(r.length):void 0}function Xn(e){return OQ(e)[0]??e[0]?.qid??"[entity]"}function OQ(e){let t=new Map;for(let r of e)t.set(r.surface,(t.get(r.surface)??0)+1);return[...t.entries()].sort((r,n)=>{let i=n[1]-r[1];return i===0?r[0].localeCompare(n[0]):i}).map(([r])=>r)}async function d_(e,t){let r=t.chapterId===void 0?{}:{chapterId:t.chapterId},[n,i]=await Promise.all([e.mentions.listLabelsByQid(t.subjectQid,r),e.mentions.listLabelsByQid(t.objectQid,r)]),a=n[0]??t.subjectQid,o=i[0]??t.objectQid;return`${a}(${t.subjectQid}) ${t.predicate} ${o}(${t.objectQid})`}function dm(e,t){return t===void 0?e:e.filter(r=>r.chapterId===t)}function u_(e,t){return t===void 0?e:e.filter(r=>t.has(r.chapterId))}async function l_(e,t,r){if(r===void 0)return t;let n=[];for(let i of t)(await e.mentions.getById(i.sourceMentionId))?.chapterId===r&&n.push(i);return n}var Kt=l(()=>{"use strict";Dn();oe()});function m_(){return{chapters:new Map,streamIndexes:new Map}}function p_(e,t,r){return Math.max(t,Math.min(r,Math.floor(e)))}async function um(e,t,r){let n=await e.getSerialFragments(t).getFragment(r),i=n.sentences.map(a=>a.text).join(`
|
|
2136
|
+
`);return{fragmentId:r,id:wd(t,r),preview:ut(i),sentenceCount:n.sentences.length,text:i,wordsCount:n.sentences.reduce((a,o)=>a+o.wordsCount,0)}}async function h_(e,t){let r=await Yn(e,t.chapterId,t.stream,t.startSentenceIndex,t.endSentenceIndex);return{fragmentId:r.startSentenceIndex,id:r.id,preview:ut(r.text),sentenceCount:r.endSentenceIndex-r.startSentenceIndex+1,text:r.text,wordsCount:DQ(r.text)}}async function Yn(e,t,r,n,i,a=m_()){let o=await Tw(e,t,r,a);if(o.sentences.length===0)throw new Error(`Chapter ${Ct(t)} has no ${r} text.`);let s=o.sentences.length-1;if(n>s)throw new Error(`${r} range ${qt(t,r,n,i)} is out of bounds. Last sentence number is ${s+1}.`);let c=p_(n,0,s),d=p_(i,c,s),u=o.sentences.slice(c,d+1),p=jQ(await NQ(e,t,r,c,d))??y_(u);return{endSentenceIndex:d,id:qt(t,r,c,d),startSentenceIndex:c,text:p}}async function f_(e,t,r){let i=await g_(e,t,r).readText?.();if(i!==void 0)return i;let a=await sn(e,t,r);return y_(a.sentences)}async function NQ(e,t,r,n,i){return await g_(e,t,r).readTextInRange?.(n,i)}function g_(e,t,r){return r==="summary"?e.getSummaryFragments(t):e.getSerialFragments(t)}function y_(e){return e.map(t=>t.text).join("")}function jQ(e){return e?.replace(/^(?:[^\S\r\n]*(?:\r\n|\n|\r))+/u,"").replace(/(?:(?:\r\n|\n|\r)[^\S\r\n]*)+$/u,"")}async function Tw(e,t,r,n=m_()){let i=`${t}:${r}`,a=n.streamIndexes.get(i);return a===void 0&&(a=sn(e,t,r),n.streamIndexes.set(i,a)),await a}async function sn(e,t,r){if(r==="summary"){let a=e.getSummaryFragments(t),o=[];for(let s of await a.listFragmentIds()){let c=await a.getFragment(s);for(let d=0;d<c.sentences.length;d+=1){let u=c.sentences[d];u!==void 0&&o.push({fragmentId:s,globalIndex:o.length,localIndex:d,text:u.text,wordsCount:u.wordsCount})}}return{sentences:o}}let n=e.getSerialFragments(t),i=[];for(let a of await n.listFragmentIds()){let o=await n.getFragment(a);for(let s=0;s<o.sentences.length;s+=1){let c=o.sentences[s];c!==void 0&&i.push({fragmentId:a,globalIndex:i.length,localIndex:s,text:c.text,wordsCount:c.wordsCount})}}return{sentences:i}}function DQ(e){let t=e.trim();return t===""?0:t.split(/\s+/u).length}var wr=l(()=>{"use strict";oe();De()});async function Rw(e,t,r){if(!await qe(e))throw new Error("Wiki Graph search index is missing or outdated. Run `<archive-uri>/index enable` before searching.");let n=await nn(e,t,{...r.chapters===void 0?{}:{chapters:r.chapters},...r.match===void 0?{}:{match:r.match},...FQ(r),types:r.types??null});return n===void 0?void 0:{hits:await $o(e,n,WQ(r)),result:n}}async function Uo(e,t,r){if(!await qe(e))throw new Error("Wiki Graph search index is missing or outdated. Run `<archive-uri>/index enable` before searching.");return await nn(e,t,r)}async function $o(e,t,r={}){let n=new Map((await z(e)).map(s=>[s.chapterId,s])),i=[],a={chapters:new Map,streamIndexes:new Map};for(let s of t.objectHits){let c=await Pw(e,n,s);c!==void 0&&i.push(Sd(c,t.terms))}let o=r.textHitLimit===void 0?t.textHits:t.textHits.slice(0,r.textHitLimit);for(let s of o){let c=await _w(e,n,s,a);c!==void 0&&i.push(Sd(c,t.terms))}return i}function WQ(e){return!Bo(e)||e.limit===void 0?{}:{textHitLimit:b_(e.limit)}}function FQ(e){return!Bo(e)||e.limit===void 0?{}:{textHitLimit:b_(e.limit)}}function b_(e){return Math.max(e+1,100)}function Bo(e){return e.types!==void 0&&e.types.length>0&&e.types.every(t=>t==="source"||t==="summary")}function Sd(e,t){return{...e,matchCount:t.length,matchedTerms:t,missingTerms:[]}}async function Pw(e,t,r){switch(r.ownerKind){case J.chapter:{let n=Sr(r.ownerId),i=t.get(n);if(i===void 0)return;let a=i.title??i.uri;return{chapter:i.chapterId,...lm(r.archiveId),field:"title",id:`${i.uri}/title`,matchCount:1,position:{chapter:i.chapterId},score:r.score,snippet:a,title:a,type:"chapter-title"}}case J.chunk:{let n=Sr(r.ownerId),i=await e.chunks.getById(n);if(i===void 0)return;let a=lt(i.sentenceIds),o=r.propertyKind===je.label;return{chapter:i.sentenceId[0],...lm(r.archiveId),field:o?"title":"content",id:he(i.id),matchCount:1,...a===void 0?{}:{position:a},score:r.score,snippet:o?i.label:ut(i.content),title:i.label,type:"node"}}case J.entity:{let n=await e.mentions.listByQid(r.ownerId),i=n[0];return i===void 0?void 0:{chapter:i.chapterId,...lm(r.archiveId),evidenceMentions:n.map(a=>cm(a)),field:"title",id:`wikg://entity/${r.ownerId}`,matchCount:1,position:{chapter:i.chapterId,sentence:i.sentenceIndex??0},score:r.score,snippet:`${n.length} mentions`,title:Xn(n),type:"entity"}}}}function Sr(e){let t=Number(e);if(!Number.isSafeInteger(t))throw new Error(`Invalid search property owner id: ${e}`);return t}async function _w(e,t,r,n){let i=r.kind===be.source?"source":"summary",a=t.get(r.chapterId);if(a===void 0)return;let o=await Yn(e,r.chapterId,i,r.sentenceIndex,r.sentenceIndex,n);return{chapter:r.chapterId,...lm(r.archiveId),field:i,id:qt(r.chapterId,i,o.startSentenceIndex,o.endSentenceIndex),matchCount:1,position:{chapter:r.chapterId,sentence:r.sentenceIndex},score:r.score,snippet:ut(o.text),title:a.title??a.uri,type:i}}function lm(e){return e===0?{}:{archiveId:e}}var Jt=l(()=>{"use strict";Ze();qn();oe();De();Kt();wr();oe();yd()});function Aw(e){return Lw(e.types??null)}function Lw(e){return e!==null&&e.length===1&&e[0]==="entity"}function pm(e,t){if(e===void 0)return;if(e.length!==(t?.length??0))throw new Error("Search cursor does not match the requested result types.");let r=new Set(t??[]);if(e.some(n=>!r.has(n)))throw new Error("Search cursor does not match the requested result types.")}function kd(e,t){let r=[],n=new Map,i=new Map,a=new Map;for(let s of t?.objectHits??[]){if(s.ownerKind!==J.entity)continue;let c=w_(s.archiveId,s.ownerId),d=i.get(c)??[];d.push(s.score),i.set(c,d),a.set(c,{archiveId:s.archiveId,qid:s.ownerId})}for(let s of e){if(s.type!=="entity"||s.evidenceMentions===void 0)continue;let c=oa(s.id);if(c===void 0)continue;let d=s.archiveId??0,u=w_(d,c),p=n.get(u)??[];for(let m of s.evidenceMentions){let f=m.mention.sentenceIndex;if(f===void 0)continue;let w=m.match.score??s.score??0;r.push({archiveId:d,chapterId:m.mention.chapterId,evidenceId:m.mention.id,evidenceKind:Kn.mention,score:w,sentenceIndex:f}),p.push(w)}n.set(u,p),a.set(u,{archiveId:d,qid:c})}return{entityHits:[...new Set([...n.keys(),...i.keys()])].map(s=>{let c=a.get(s);if(c===void 0)throw new Error("Internal error: missing scoped entity key.");return{archiveId:c.archiveId,evidenceTopScores:n.get(s)??[],propertyTopScores:i.get(s)??[],qid:c.qid}}),evidenceEvents:r}}async function Ow(e,t,r){if(t===void 0||Bo(r))return{chunkHits:[],entityHits:[],evidenceEvents:[],tripleHits:[]};let n=t.textHits.filter(m=>m.kind===be.source);if(n.length===0)return{chunkHits:[],entityHits:[],evidenceEvents:[],tripleHits:[]};let i=new Map,a=new Set,o=new Set;for(let m of n)a.add(m.chapterId),o.add(m.archiveId);let s=o.size===1?[...o][0]??0:0;for(let m of n)i.set(sa(s,m.chapterId,m.sentenceIndex),m.score);let c=[],d=new Map,u=new Map,p=new Map;for(let m of a){for(let w of await e.mentions.listByChapter(m)){if(w.sentenceIndex===void 0)continue;let b=i.get(sa(s,m,w.sentenceIndex));if(b===void 0)continue;c.push({archiveId:s,chapterId:m,evidenceId:w.id,evidenceKind:Kn.mention,score:b,sentenceIndex:w.sentenceIndex});let E=d.get(w.qid)??[];E.push(b),d.set(w.qid,E)}for(let w of await e.chunks.listBySerial(m))for(let[,b]of w.sentenceIds){let E=i.get(sa(s,m,b));if(E===void 0)continue;c.push({archiveId:s,chapterId:m,evidenceId:String(w.id),evidenceKind:Kn.chunk,score:E,sentenceIndex:b});let N=u.get(w.id)??[];N.push(E),u.set(w.id,N)}let f=new Map;for(let w of await e.mentionLinks.listByChapter(m)){let[b,E]=await Promise.all([Go(e,f,w.sourceMentionId),Go(e,f,w.targetMentionId)]);if(!(b===void 0||E===void 0))for(let[N,C]of w.evidenceSentenceIds){let q=i.get(sa(s,N,C));if(q===void 0)continue;c.push({archiveId:s,chapterId:N,evidenceId:w.id,evidenceKind:Kn.mentionLink,score:q,sentenceIndex:C});let Q=br(b.qid,w.predicate,E.qid),x=p.get(Q)??{objectQid:E.qid,predicate:w.predicate,scores:[],subjectQid:b.qid};x.scores.push(q),p.set(Q,x)}}}return{chunkHits:[...u.entries()].map(([m,f])=>({archiveId:s,chunkId:m,evidenceTopScores:f})),entityHits:[...d.entries()].map(([m,f])=>({archiveId:s,evidenceTopScores:f,qid:m})),evidenceEvents:c,tripleHits:[...p.values()].map(m=>({archiveId:s,evidenceTopScores:m.scores,objectQid:m.objectQid,predicate:m.predicate,subjectQid:m.subjectQid}))}}function sa(e,t,r){return`${e}:${t}:${r}`}function w_(e,t){return`${e}:${t}`}var Ed=l(()=>{"use strict";Yi();qn();Jt();Kt()});function mm(e,t){let r=new Map;for(let{match:n,mention:i}of wP(t.mentions,e)){let a=r.get(i.qid)??[];a.push({hit:{chapter:i.chapterId,field:"title",id:`wikg://entity/${i.qid}`,...ww(n),position:{chapter:i.chapterId,sentence:i.sentenceIndex??0},snippet:i.note??i.surface,title:i.surface,type:"entity"},mention:i}),r.set(i.qid,a)}return[...r.values()].map(n=>{let i=[...n].sort((o,s)=>{let c=(s.hit.score??0)-(o.hit.score??0);return c!==0?c:o.hit.position===void 0?s.hit.position===void 0?0:1:s.hit.position===void 0?-1:Mo(o.hit.position,s.hit.position)}),[a]=i;if(a===void 0)throw new Error("Internal error: entity search candidate is empty.");return{...a.hit,score:zn(i.map(o=>o.hit.score??0)),evidenceMentions:i.map(o=>({match:{matchCount:o.hit.matchCount??0,matchedTerms:o.hit.matchedTerms??[],missingTerms:o.hit.missingTerms??[],score:o.hit.score??0},mention:o.mention}))}})}async function hm(e,t,r){let n=new Map(r.mentions.map(a=>[a.id,a])),i=new Map;for(let a of await e.mentionLinks.listAll()){if(!n.has(a.sourceMentionId)&&!n.has(a.targetMentionId)&&Gp(a.predicate,t)===void 0)continue;let[o,s]=await Promise.all([Go(e,n,a.sourceMentionId),Go(e,n,a.targetMentionId)]);if(o===void 0||s===void 0)continue;let c=`${o.surface} ${a.predicate} ${s.surface}`,d=Gp(c,t);if(d===void 0)continue;let u=br(o.qid,a.predicate,s.qid),p={chapter:o.chapterId,evidenceLinks:[a],field:"content",id:u,...ww(d),position:{chapter:o.chapterId,sentence:o.sentenceIndex??0},snippet:a.note??c,title:c,triple:{objectLabel:s.surface,predicate:a.predicate,subjectLabel:o.surface},type:"triple"},m=i.get(u)??[];m.push(p),i.set(u,m)}return[...i.values()].map(GQ)}function GQ(e){let t=[...e].sort(tm),[r]=t;if(r===void 0)throw new Error("Internal error: triple search candidate is empty.");return{...r,evidenceLinks:t.flatMap(n=>n.evidenceLinks??[]),score:zn(t.map(n=>n.score??0))}}function fm(e,t,r){if(r==="any")return e;let n=[...t.phrases];return n.length===0?e:e.filter(i=>n.every(a=>i.matchedTerms?.includes(a)===!0))}var Nw=l(()=>{"use strict";Ze();Vc();oe();De();wr();Kt()});async function Dw(e,t,r){return jw(await gm(e,t,r))}async function Ho(e,t,r){await gm(e,t,r);let[n,i]=await Promise.all([e.readingEdges.listIncoming(r),e.readingEdges.listOutgoing(r)]);return(await Promise.all([...n.map(async o=>({direction:"incoming",edge:S_(o),node:jw(await gm(e,t,o.fromId))})),...i.map(async o=>({direction:"outgoing",edge:S_(o),node:jw(await gm(e,t,o.toId))}))])).sort(UQ)}async function gm(e,t,r){let n=await e.chunks.getById(r);if(n===void 0||n.sentenceId[0]!==t)throw new Error(`Graph node ${r} does not exist in chapter ${t}. Use \`wg <archive-uri>/chapter/${t}/chunk list\` to discover chunk ids.`);return n}function jw(e){return{content:e.content,id:e.id,label:e.label,sentenceIds:e.sentenceIds,weight:e.weight,wordsCount:e.wordsCount,...e.importance===void 0?{}:{importance:e.importance},...e.retention===void 0?{}:{retention:e.retention}}}function S_(e){return{fromId:e.fromId,toId:e.toId,weight:e.weight,...e.strength===void 0?{}:{strength:e.strength}}}function UQ(e,t){return e.direction!==t.direction?e.direction==="incoming"?-1:1:t.edge.weight-e.edge.weight||e.node.id-t.node.id}var Id=l(()=>{"use strict"});async function Zn(e,t){let r=(await z(e)).find(n=>n.chapterId===t);if(r===void 0)throw new Error(`Chapter ${Ct(t)} does not exist.`);return r}async function Mw(e,t){let r=await e.serials.getById(t.chapterId);return{source:t.stage==="planned"?"missing":"ready","reading-graph":t.stage==="graphed"||t.stage==="summarized"?"ready":"missing","reading-summary":t.stage==="summarized"?"ready":"missing","knowledge-graph":r?.knowledgeGraphReady===!0?"ready":"missing"}}async function kr(e,t){let r=await e.chunks.getById(t);if(r===void 0)throw new Error(`Node ${he(t)} does not exist.`);let n=r.sentenceId[0];return{chapterId:n,node:await Dw(e,n,t)}}async function k_(e,t){let r=await $Q(e,t);return await Promise.all(r.map(async([n,i])=>{let a=await um(e,n,i),s=(await sn(e,n,"source")).sentences.filter(p=>p.fragmentId===i),c=s[0],d=s[s.length-1],u=BQ(a.text);return{id:c===void 0||d===void 0?a.id:qt(n,"source",c.globalIndex,d.globalIndex),text:u,truncated:u.length<a.text.length}}))}async function $Q(e,t){let r=new Set,n=[],i=new Map;for(let[a,o]of t.sentenceIds){let s=i.get(a);s===void 0&&(s=sn(e,a,"source"),i.set(a,s));let c=(await s).sentences[o];if(c===void 0)continue;let d=c.fragmentId,u=`${a}:${d}`;r.has(u)||(r.add(u),n.push([a,d]))}return n}function BQ(e){return e.length<=1200?e:`${e.slice(0,1200)}...`}async function E_(e,t,r){return(await e.chunks.listBySentenceStartIndexes(t,[r])).map(qQ).sort(KQ).map(({label:n})=>n)}function HQ(e){return{id:he(e.id),title:e.label}}function qQ(e){return{label:HQ(e),position:lt(e.sentenceIds)}}function KQ(e,t){return e.position!==void 0&&t.position!==void 0?Mo(e.position,t.position):e.position!==void 0?-1:t.position!==void 0?1:e.label.id.localeCompare(t.label.id)}var le,Er=l(()=>{"use strict";Ze();Id();oe();De();wr();le=2});function xd(e){let t=new Map;for(let[r,n]of e.sentenceIds){let i=`${r}`,a=t.get(i);a===void 0?t.set(i,[n,n]):t.set(i,[Math.min(a[0],n),Math.max(a[1],n)])}return[...t.entries()].map(([r,[n,i]])=>({chapterId:Number(r),endSentenceIndex:i,startSentenceIndex:n}))}async function cn(e,t){return await Promise.all(t.map(async r=>{let n=r.sentenceIndex??await I_(e,r.chapterId,r.rangeStart),i=r.sentenceIndex??await I_(e,r.chapterId,Math.max(0,r.rangeEnd-1));return{chapterId:r.chapterId,endSentenceIndex:i,startSentenceIndex:n}}))}function dn(e,t){return t.flatMap(r=>r.evidenceSentenceIds.map(([n,i])=>({chapterId:n,endSentenceIndex:i,startSentenceIndex:i})))}function Ww(e){let t=[];for(let r of e){let n=t.map((s,c)=>JQ(s,r)?c:-1).filter(s=>s>=0);if(n.length===0){t.push(r);continue}let i=n[0]??0,a=n.flatMap(s=>{let c=t[s];return c===void 0?[]:[c]}),o=zQ([...a,r]);t[i]=o;for(let s of n.slice(1).reverse())t.splice(s,1)}return t}function JQ(e,t){return e.chapterId===t.chapterId&&t.startSentenceIndex<=e.endSentenceIndex+1&&e.startSentenceIndex<=t.endSentenceIndex+1}function zQ(e){let[t]=e;if(t===void 0)throw new Error("Internal error: cannot merge empty evidence range group.");let r=e.map(n=>n.score).filter(n=>n!==void 0);return{chapterId:t.chapterId,endSentenceIndex:Math.max(...e.map(n=>n.endSentenceIndex)),...r.length===0?{}:{score:Math.max(...r)},startSentenceIndex:Math.min(...e.map(n=>n.startSentenceIndex))}}async function Fw(e,t,r,n){let i=await Promise.all(t.map(async a=>{if(r<=0)return a;let o=await Tw(e,a.chapterId,"source",n),s=Math.max(0,o.sentences.length-1);return{...a,endSentenceIndex:x_(a.endSentenceIndex+r,a.startSentenceIndex,s),startSentenceIndex:x_(a.startSentenceIndex-r,0,s)}}));return Ww(i)}async function I_(e,t,r){let n=e.getSerialFragments(t),i=n.listSentences===void 0?[]:await n.listSentences();if(i.length===0)return 0;let a=0;for(let o=0;o<i.length;o+=1){let s=i[o];if(s===void 0)continue;let c=a+s.text.length;if(r<=c)return o;a=c+1}return Math.max(0,i.length-1)}function x_(e,t,r){return Math.max(t,Math.min(r,Math.floor(e)))}var vd=l(()=>{"use strict";wr()});async function Gw(e,t,r,n,i=ee(),a){let o=await QQ(e,t,i),s=await Yn(e,t,"source",r,n,i);return{chapterId:t,endSentenceIndex:s.endSentenceIndex,fragmentId:s.startSentenceIndex,id:qt(t,"source",s.startSentenceIndex,s.endSentenceIndex),...a===void 0?{}:{score:a},source:s.text,startSentenceIndex:s.startSentenceIndex,title:o.title??o.uri,type:"source"}}function ee(){return{chapters:new Map,streamIndexes:new Map}}async function QQ(e,t,r){let n=r.chapters.get(t);return n===void 0&&(n=Zn(e,t),r.chapters.set(t,n)),await n}var Cd=l(()=>{"use strict";De();wr();Er()});async function qo(e,t){let r=t.candidates.slice(0,t.limit),n=await v_(e,{...t,candidates:r}),i=(t.offset??0)+r.length,a=t.total??t.candidates.length;return{items:n,limit:t.limit,nextCursor:i<a?Jn(i):null}}async function Ko(e,t){let r=t.candidates.slice(0,t.limit),n=await v_(e,{...t,candidates:r}),i=t.total??t.candidates.length,a=(t.offset??0)+r.length;return{nextCursor:a<i?Jn(a):null,shown:n.length,sources:n,total:i}}async function v_(e,t){let r=(await Promise.all(t.candidates.map(async n=>await t.createRanges(n)))).flat();return await Uw(e,r,{context:t.context,sourceContext:t.sourceContext})}async function Uw(e,t,r){let n=await VQ(e,t,r);return await Promise.all(n.map(async i=>await Gw(e,i.chapterId,i.startSentenceIndex,i.endSentenceIndex,r.context,i.score)))}async function VQ(e,t,r){let n=await Fw(e,t,r.sourceContext??le,r.context);return Ww(n)}var $w=l(()=>{"use strict";oe();Er();vd();Cd()});async function T_(e,t,r=3,n=ee(),i=le,a="doc-asc",o){return await Ko(e,{candidates:await L_(e,t,async s=>await cn(e,[s]),a),context:n,createRanges:async s=>await cn(e,[s]),limit:r,sourceContext:i,total:o})}async function ym(e,t,r=3,n=ee(),i=le,a){return await Ko(e,{candidates:t,context:n,createRanges:async o=>await cn(e,[o]),limit:r,sourceContext:i,total:a})}async function R_(e,t,r){return await qo(e,{candidates:t,context:r.context,createRanges:async n=>await cn(e,[n]),limit:r.limit,offset:r.offset,sourceContext:r.sourceContext,total:r.total})}async function XQ(e,t,r=3,n=ee(),i=le,a){return await Ko(e,{candidates:t,context:n,createRanges:o=>[o],limit:r,sourceContext:i,total:a})}async function YQ(e,t,r=3,n=ee(),i=le,a="doc-asc",o){return await XQ(e,await tV(e,t,a),r,n,i,o)}async function P_(e,t,r=3,n=ee(),i=le,a="doc-asc",o){return await Ko(e,{candidates:await L_(e,t,s=>dn(e,[s]),a),context:n,createRanges:s=>dn(e,[s]),limit:r,sourceContext:i,total:o})}async function bm(e,t,r=3,n=ee(),i=le,a){return await Ko(e,{candidates:t,context:n,createRanges:o=>dn(e,[o]),limit:r,sourceContext:i,total:a})}async function __(e,t,r){return await qo(e,{candidates:t,context:r.context,createRanges:n=>dn(e,[n]),limit:r.limit,offset:r.offset,sourceContext:r.sourceContext,total:r.total})}async function A_(e,t,r){let n=ee(),i=r.limit??20,a=Ht(r.cursor),o=await ZQ(e,t,r.query,r.order??"doc-asc"),s=o.slice(a,a+i),c=a+s.length;return{items:await Uw(e,s,{context:n,sourceContext:r.sourceContext}),limit:i,nextCursor:c<o.length?Jn(c):null}}async function ZQ(e,t,r,n){let i=await e.serials.listDocumentOrders();if(r===void 0)return[...t].sort((c,d)=>Td(c,d,i,n));let a=await Uo(e,r,{chapters:[...new Set(t.map(c=>c.chapterId))],types:["source"]});if(a===void 0)return[];let o=new Map,s=new Map;for(let c of t){let d=s.get(c.chapterId)??[];d.push(c),s.set(c.chapterId,d)}for(let c of a.textHits)if(c.kind===be.source)for(let d of s.get(c.chapterId)??[]){if(c.sentenceIndex<d.startSentenceIndex||c.sentenceIndex>d.endSentenceIndex)continue;let u=eV(d),p=o.get(u);o.set(u,{...d,score:Math.max(p?.score??0,c.score)})}return[...o.values()].sort((c,d)=>{let u=(d.score??0)-(c.score??0);return u!==0?u:Td(c,d,i,"doc-asc")})}async function Bw(e,t,r,n,i){let a=await Promise.all(t.map(async d=>({candidate:d,ranges:await r(d),score:0,stableIdentity:n(d)}))),o=await Uo(e,i,{chapters:[...new Set(a.flatMap(d=>d.ranges.map(u=>u.chapterId)))],types:["source"]});if(o===void 0)return[];let s=new Map;for(let d of o.textHits)if(d.kind===be.source)for(let u of a){if(!u.ranges.some(m=>m.chapterId===d.chapterId&&d.sentenceIndex>=m.startSentenceIndex&&d.sentenceIndex<=m.endSentenceIndex))continue;let p=s.get(u.stableIdentity)??u;p.score=Math.max(p.score,d.score),s.set(u.stableIdentity,p)}let c=await e.serials.listDocumentOrders();return[...s.values()].sort((d,u)=>{let p=u.score-d.score;if(p!==0)return p;let m=Td(C_(d.ranges),C_(u.ranges),c,"doc-asc");return m!==0?m:d.stableIdentity.localeCompare(u.stableIdentity)}).map(d=>({candidate:d.candidate,score:d.score}))}function C_(e){let[t]=e;return t??{chapterId:Number.MAX_SAFE_INTEGER,endSentenceIndex:Number.MAX_SAFE_INTEGER,startSentenceIndex:Number.MAX_SAFE_INTEGER}}function eV(e){return`${e.chapterId}:${e.startSentenceIndex}:${e.endSentenceIndex}`}async function L_(e,t,r,n){let i=await e.serials.listDocumentOrders();return(await Promise.all(t.map(async o=>({candidate:o,range:(await r(o))[0]})))).sort((o,s)=>o.range===void 0||s.range===void 0?o.range===void 0?s.range===void 0?0:1:-1:Td(o.range,s.range,i,n)).map(o=>o.candidate)}function Td(e,t,r,n){let i=n==="doc-asc"?1:-1;return(D(r.get(e.chapterId)??e.chapterId,r.get(t.chapterId)??t.chapterId)||D(e.chapterId,t.chapterId)||D(e.startSentenceIndex,t.startSentenceIndex)||D(e.endSentenceIndex,t.endSentenceIndex))*i}async function O_(e,t,r,n=ee(),i=le,a="doc-asc",o){return await YQ(e,t,r,n,i,a,o)}async function tV(e,t,r){let n=await e.serials.listDocumentOrders();return[...t].sort((i,a)=>Td(i,a,n,r))}var N_=l(()=>{"use strict";qn();oe();Jt();Er();vd();$w();Cd();Cd();vd()});var j_=l(()=>{"use strict";N_();$w();vd();Cd()});var Rd=l(()=>{"use strict";j_()});async function zt(e,t,r={}){let n=ee(),i=await Promise.all(t.map(async a=>{let o=await rV(e,a),s=await nV(e,o,r.sourceContext??le,n);if(s.evidence!==void 0&&s.evidence.sources.length>0)return s;if(s.evidence!==void 0&&s.type==="entity"&&r.sessionId!==void 0)return await cV(e,s,r.sessionId,r.evidenceLimit,r.sourceContext??le,n);if(s.evidenceLinks!==void 0){if(r.evidenceLimit===void 0){let{evidenceLinks:w,...b}=s;return b}let p=await P_(e,s.evidenceLinks,r.evidenceLimit,n,r.sourceContext??le,r.order??"doc-asc"),{evidenceLinks:m,...f}=s;return{...f,evidence:p}}if(s.evidenceMentions===void 0)return s;if(r.evidenceLimit===void 0){let{evidenceMentions:p,...m}=s;return m}let c=await T_(e,s.evidenceMentions.map(p=>p.mention),r.evidenceLimit,n,r.sourceContext??le,r.order??"doc-asc"),{evidenceMentions:d,...u}=s;return{...u,evidence:c}}));return await iV(e,i,n)}async function rV(e,t){if(t.type!=="entity")return t;let r=oa(t.id);if(r===void 0||t.title!==r&&t.snippet!==r)return t;let n=await e.mentions.listByQid(r),[i]=n.sort(Fo);return i===void 0?t:{...t,chapter:i.chapterId,position:{chapter:i.chapterId,sentence:i.sentenceIndex??0},snippet:t.snippet===r?i.note??i.surface:t.snippet,title:t.title===r?Xn(n):t.title}}async function nV(e,t,r,n){if(r<=0||t.type!=="source"&&t.type!=="summary"||t.matchCount===void 0&&t.matchedTerms===void 0)return t;let i=D_(t.id);if(i===void 0)return t;let a=await Yn(e,i.chapterId,i.stream,i.startSentenceIndex-r,i.endSentenceIndex+r,n);return{...t,id:a.id,snippet:a.text}}function D_(e){if(gr(e))try{let t=Tt(e);return t.type==="text-stream"?t:void 0}catch{return}}async function iV(e,t,r){let n=[];for(let i of t){let a=aV(i);if(a===void 0||i.backlinks!==void 0){n.push({hit:i,type:"hit"});continue}let o=n.map((d,u)=>d.type==="range"&&oV(d.range,a)?u:-1).filter(d=>d>=0);if(o.length===0){n.push({range:a,type:"range"});continue}let s=o[0]??0,c=o.flatMap(d=>{let u=n[d];return u?.type==="range"?[u.range]:[]});n[s]={range:await sV(e,[...c,a],r),type:"range"};for(let d of o.slice(1).reverse())n.splice(d,1)}return n.map(i=>i.type==="range"?i.range.hit:i.hit)}function aV(e){if(e.type!=="source"&&e.type!=="summary"||e.matchCount===void 0&&e.matchedTerms===void 0)return;let t=D_(e.id);if(t!==void 0)return{chapterId:t.chapterId,endSentenceIndex:t.endSentenceIndex,hit:e,startSentenceIndex:t.startSentenceIndex,stream:t.stream}}function oV(e,t){return e.chapterId===t.chapterId&&e.stream===t.stream&&t.startSentenceIndex<=e.endSentenceIndex+1&&e.startSentenceIndex<=t.endSentenceIndex+1}async function sV(e,t,r){let[n]=t;if(n===void 0)throw new Error("Internal error: cannot merge empty text stream hit group.");let i=Math.min(...t.map(d=>d.startSentenceIndex)),a=Math.max(...t.map(d=>d.endSentenceIndex)),o=await Yn(e,n.chapterId,n.stream,i,a,r),s=t.map(d=>d.hit),c=s.map(d=>d.score).filter(d=>d!==void 0);return{chapterId:n.chapterId,endSentenceIndex:a,hit:{...n.hit,id:o.id,matchCount:Math.max(...s.map(d=>d.matchCount??0)),matchedTerms:md(s.flatMap(d=>d.matchedTerms??[])),missingTerms:md(s.flatMap(d=>d.missingTerms??[])),...c.length===0?{}:{score:Math.max(...c)},snippet:o.text},startSentenceIndex:i,stream:n.stream}}function un(e,t){return{...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},order:e.order??"doc-asc",...t===void 0?{}:{sessionId:t},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext}}}async function cV(e,t,r,n,i,a=ee()){if(n===void 0){let{evidence:f,...w}=t;return w}let o=oa(t.id);if(o===void 0)return t;let s=(await e.mentions.listByQid(o)).map(f=>f.id),c=await uw(r,s,n+1),d=c.slice(0,n),u=(await Promise.all(d.map(async f=>{let w=await e.mentions.getById(f.mentionId);return w===void 0?[]:[w]}))).flat(),p=c.length>d.length?d.length+1:d.length,m=await ym(e,u,n,a,i,p);return{...t,...u[0]===void 0?{}:{chapter:u[0].chapterId,position:{chapter:u[0].chapterId,sentence:u[0].sentenceIndex??0},snippet:u[0].note??u[0].surface,title:u[0].surface},evidence:m}}var M_=l(()=>{"use strict";Yi();oe();De();wr();Er();Kt();Rd()});async function Qt(e,t,r={}){let n;try{n=Tt(t)}catch(i){throw/^wikg:\/\/chapter\/.+\/(?:source|summary|title|state)(?:[#/]|$)/u.test(t)?new Error(`Evidence is not available for ${t}.`):i}switch(n.type){case"chapter":case"chapter-title":case"chapter-state":case"chapter-tree":case"entity-wikipage":case"meta":case"text-stream":throw new Error(`Evidence is not available for ${t}.`);case"chunk":{let{chapterId:i,node:a}=await kr(e,n.id);if(n.chapterId!==void 0&&n.chapterId!==i)throw new Error(`Chunk ${t} was not found in this archive.`);return await A_(e,xd(a),r)}case"entity":{if(r.query===void 0){let c=r.limit??20,d=Ht(r.cursor),u=n.chapterId===void 0?{}:{chapterId:n.chapterId},[p,m]=await Promise.all([e.mentions.countByQid(n.qid,u),e.mentions.listByQid(n.qid,{...u,limit:c,offset:d,order:r.order==="doc-desc"?"desc":"asc"})]);return await R_(e,m,{context:ee(),limit:c,offset:d,sourceContext:r.sourceContext,total:p})}let i=r.limit??20,a=Ht(r.cursor),o=dm(await e.mentions.listByQid(n.qid),n.chapterId),s=await Bw(e,o,async c=>await cn(e,[c]),c=>c.id,r.query);return await qo(e,{candidates:s.slice(a,a+i),context:ee(),createRanges:async c=>(await cn(e,[c.candidate])).map(d=>({...d,score:c.score})),limit:i,offset:a,sourceContext:r.sourceContext,total:s.length})}case"triple":{if(r.query===void 0){let c=r.limit??20,d=Ht(r.cursor),u={...n.chapterId===void 0?{}:{chapterId:n.chapterId},objectQid:n.objectQid,predicate:n.predicate,subjectQid:n.subjectQid},[p,m]=await Promise.all([e.mentionLinks.countByTriple(u),e.mentionLinks.listByTriple({...u,limit:c,offset:d,order:r.order==="doc-desc"?"desc":"asc"})]);return await __(e,m,{context:ee(),limit:c,offset:d,sourceContext:r.sourceContext,total:p})}let i=r.limit??20,a=Ht(r.cursor),o=await l_(e,await e.mentionLinks.listByTriple({objectQid:n.objectQid,predicate:n.predicate,subjectQid:n.subjectQid}),n.chapterId),s=await Bw(e,o,c=>dn(e,[c]),c=>c.id,r.query);return await qo(e,{candidates:s.slice(a,a+i),context:ee(),createRanges:c=>dn(e,[c.candidate]).map(d=>({...d,score:c.score})),limit:i,offset:a,sourceContext:r.sourceContext,total:s.length})}}}var Pd=l(()=>{"use strict";De();Er();oe();Kt();Rd();M_()});async function Ir(e,t={}){let r=[],n=await e.serials.listDocumentOrders(),i=t.chapters===void 0?void 0:new Set(t.chapters),a=t.types??["meta","chapter-title","entity","node","triple"];if(a.includes("meta")){let c=await e.readBookMeta();c!==void 0&&r.push({field:"metadata",id:Oo,snippet:UP(c),title:c.title??"Archive metadata",type:"meta"})}if(a.includes("chapter")||a.includes("chapter-title"))for(let c of W_(await z(e),i)){let d=c.title??`[chapter ${c.path}]`;(a.includes("chapter")||a.includes("chapter-title"))&&r.push({chapter:c.chapterId,field:"title",id:`${c.uri}/title`,position:{chapter:c.chapterId,documentOrder:c.documentOrder},snippet:d,title:d,type:"chapter-title"})}if(a.includes("node"))for(let c of await e.chunks.listAll()){if(!dV(i,c.sentenceId[0]))continue;let d=lt(c.sentenceIds,n);r.push({chapter:c.sentenceId[0],field:"content",id:he(c.id),...d===void 0?{}:{position:d},snippet:ut(c.content),title:c.label,type:"node"})}a.includes("entity")&&r.push(...Hw(u_(await sm(e),i),n)),a.includes("triple")&&r.push(...await uV(e,i,n));let o=nm(r,t),s=await zt(e,o.items,{...t.evidenceLimit===void 0?{}:{evidenceLimit:t.evidenceLimit},order:t.order??"doc-asc",...t.sourceContext===void 0?{}:{sourceContext:t.sourceContext}});return{...o,items:await Kw(e,s,t)}}function W_(e,t){return t===void 0?e:e.filter(r=>t.has(r.chapterId))}function dV(e,t){return e===void 0||e.has(t)}function Hw(e,t){let r=new Map;for(let n of e){let i=r.get(n.qid)??[];i.push(n),r.set(n.qid,i)}return[...r.entries()].map(([n,i])=>{let[a]=i.sort(Fo);if(a===void 0)throw new Error("Internal error: entity collection candidate is empty.");return{chapter:a.chapterId,evidenceMentions:i.map(o=>cm(o)),field:"title",id:`wikg://entity/${n}`,position:{chapter:a.chapterId,documentOrder:t?.get(a.chapterId)??a.chapterId,sentence:a.sentenceIndex??0},score:i.length,snippet:`${i.length} mentions`,title:Xn(i),type:"entity"}})}async function uV(e,t,r){let n=new Map;for(let i of W_(await z(e),t))for(let a of await e.mentionLinks.listByChapter(i.chapterId)){let[o,s]=await Promise.all([e.mentions.getById(a.sourceMentionId),e.mentions.getById(a.targetMentionId)]);if(o===void 0||s===void 0)continue;let c=br(o.qid,a.predicate,s.qid),d=n.get(c);if(d!==void 0){n.set(c,{...d,evidenceLinks:[...d.evidenceLinks??[],a],score:(d.evidenceLinks?.length??0)+1});continue}n.set(c,{chapter:o.chapterId,evidenceLinks:[a],field:"title",id:c,position:{chapter:o.chapterId,documentOrder:r?.get(o.chapterId)??o.chapterId,sentence:o.sentenceIndex??0},score:1,snippet:`${o.surface} ${a.predicate} ${s.surface}`,title:`${o.qid} ${a.predicate} ${s.qid}`,triple:{objectLabel:s.surface,predicate:a.predicate,subjectLabel:o.surface},type:"triple"})}return[...n.values()]}var qw=l(()=>{"use strict";Ze();oe();De();wr();Er();Pd();_d();Kt()});function F_(e,t,r){return Math.max(t,Math.min(r,Math.floor(e)))}async function ei(e,t,r){return{...t,items:await Kw(e,t.items,r)}}async function Kw(e,t,r){return r.backlinks!==!0?t:await Promise.all(t.map(async n=>{let i=lV(n.id);return i===void 0?n:{...n,backlinks:await Jw(e,i)}}))}function lV(e){if(gr(e))try{let t=Tt(e);return t.type==="text-stream"&&t.stream==="source"?t:void 0}catch{return}}async function Jw(e,t){if(t.stream!=="source")return pV();let r=await mV(e,t),[n,i,a]=await Promise.all([hV(e,r),fV(e,r),gV(e,t.chapterId,r)]);return{chunks:Jo(n),entities:Jo(i),triples:Jo(a)}}function pV(){return{chunks:Jo([]),entities:Jo([]),triples:Jo([])}}function Jo(e){let t=[...e].sort((r,n)=>ea(r,n,"doc-asc"));return{items:t,limit:t.length,nextCursor:null}}async function mV(e,t){let r=await sn(e,t.chapterId,t.stream),n=Math.max(0,r.sentences.length-1),i=F_(t.startSentenceIndex,0,n),a=F_(t.endSentenceIndex,i,n),o=new Set;for(let s of r.sentences.slice(i,a+1))o.add(zw(t.chapterId,s.globalIndex));return o}async function hV(e,t){return(await e.chunks.listAll()).filter(r=>r.sentenceIds.some(n=>t.has(G_(n)))).map(r=>{let n=lt(r.sentenceIds);return{chapter:r.sentenceId[0],field:"content",id:he(r.id),...n===void 0?{}:{position:n},snippet:ut(r.content),title:r.label,type:"node"}})}async function fV(e,t){return Hw((await sm(e)).filter(r=>t.has(zw(r.chapterId,r.sentenceIndex??0))))}async function gV(e,t,r){let n=new Map;for(let i of await e.mentionLinks.listByChapter(t)){let a=i.evidenceSentenceIds.filter(p=>r.has(G_(p)));if(a.length===0)continue;let[o,s]=await Promise.all([e.mentions.getById(i.sourceMentionId),e.mentions.getById(i.targetMentionId)]);if(o===void 0||s===void 0)continue;let c=br(o.qid,i.predicate,s.qid),d=n.get(c),u={...i,evidenceSentenceIds:a};if(d!==void 0){n.set(c,{...d,evidenceLinks:[...d.evidenceLinks??[],u],score:(d.evidenceLinks?.length??0)+1});continue}n.set(c,{chapter:o.chapterId,evidenceLinks:[u],field:"title",id:c,position:pd([...a].sort(Zi)[0]??[o.chapterId,o.sentenceIndex??0]),score:1,snippet:`${o.surface} ${i.predicate} ${s.surface}`,title:`${o.qid} ${i.predicate} ${s.qid}`,triple:{objectLabel:s.surface,predicate:i.predicate,subjectLabel:o.surface},type:"triple"})}return[...n.values()]}function G_(e){return zw(e[0],e[1])}function zw(e,t){return`${e}:${t}`}var _d=l(()=>{"use strict";oe();De();wr();Kt();qw()});async function Sm(e,t){if(t.type==="entity"){let r=oa(t.id);if(r===void 0)return t;let n=await e.mentions.listByQid(r),[i]=[...n].sort(Fo);return i===void 0?t:{...t,chapter:i.chapterId,position:{chapter:i.chapterId,sentence:i.sentenceIndex??0},snippet:i.note??i.surface,title:Xn(n)}}return t.type==="triple",t}async function km(e,t){let r=Sr(t.id.slice(13)),n=await e.chunks.getById(r);if(n===void 0)return;let{position:i,...a}=t,o=lt(n.sentenceIds);return{...a,chapter:n.sentenceId[0],field:"title",...o===void 0?{}:{position:o},snippet:ut(n.content),title:n.label}}var Em=l(()=>{"use strict";oe();Jt();Kt()});function Ad(e,t){return D(t.score,e.score)||D(e.archiveId,t.archiveId)||D(Sr(e.ownerId),Sr(t.ownerId))}function Ld(e,t){return D(e.rank,t.rank)||D(e.archiveId,t.archiveId)||D(e.chapterId,t.chapterId)||D(e.sentenceIndex,t.sentenceIndex)||D(e.kind,t.kind)}function Im(e,t){return t===void 0?!0:Ad({...e,archiveId:t.archiveId,ownerId:String(t.chapterId),score:t.score},e)<0}function xm(e,t){return t===void 0?!0:Ld({archiveId:t.archiveId,chapterId:t.chapterId,kind:t.kind,rank:t.rank,score:0,sentenceIndex:t.sentenceIndex,wordsCount:0},e)<0}function vm(e){return e.type!=="triple"?e.id.replace(/^wikg:\/\/entity\//u,""):yV(e.id)??e.id}function yV(e){let t=/^wikg:\/\/triple\/([^/]+)\/([^/]+)\/([^/]+)$/u.exec(e);if(!(t?.[1]===void 0||t[2]===void 0||t[3]===void 0))return`${t[1]}${decodeURIComponent(t[2])}${t[3]}`}var Cm=l(()=>{"use strict";oe();Jt()});async function Qw(e,t,r){let n=await id(t.sessionId,r.archiveKey??"archive",t.createdAt);pm(r.types,n.types);let i=[],a=t.cursor;for(;a!==void 0&&i.length<r.limit;){let o=r.limit-i.length,s=await bV(e,n,a,o);i.push(...s.items),a=s.nextCursor}return await ei(e,{chapters:n.chapters,items:await zt(e,i,un(r,t.sessionId)),lens:jo(n.lens),lensHint:n.lens==="broad"?fr:null,limit:r.limit,match:on(n.match),nextCursor:a===void 0?null:Xc(t.sessionId,a,n.createdAt),order:r.order??"doc-asc",query:n.query,terms:n.terms,types:Do(n.types)},r)}async function bV(e,t,r,n){switch(r.bucket){case 0:return await wV(e,t,r.key,n);case 1:return await SV(e,t,r.key,n);case 2:return await EV(e,t,r.key,n);case 3:return await IV(e,t,r.key,n)}}async function wV(e,t,r,n){let i=await nn(e,t.query,{...t.chapters===null?{}:{chapters:t.chapters},match:on(t.match),objectHitLimit:Ut,textHitLimit:0,types:["chapter-title"]}),a=new Map((await z(e)).map(u=>[u.chapterId,u])),s=(i?.objectHits??[]).filter(u=>u.ownerKind===J.chapter).sort(Ad).filter(u=>Im(u,r)).slice(0,n+1),c=(await Promise.all(s.slice(0,n).map(async u=>{let p=await Pw(e,a,u);return p===void 0?void 0:Sd(p,t.terms)}))).filter(No),d=s.at(n-1);return{items:c,nextCursor:s.length>n&&d!==void 0?{bucket:0,key:{archiveId:d.archiveId,chapterId:Sr(d.ownerId),score:d.score}}:{bucket:1}}}async function SV(e,t,r,n){t.objectCachesPopulated||await kV(e,t);let i=await ad(t.sessionId,1,r,n),a=i.slice(0,n),o=await Promise.all(a.map(async c=>await Sm(e,c))),s=a.at(-1);return{items:o,nextCursor:i.length>n&&s!==void 0?{bucket:1,key:{archiveId:U_(s),id:vm(s),kind:s.type==="triple"?"triple":"entity",score:s.score??0}}:{bucket:2}}}async function kV(e,t){let r=vo(t.query);if(r===void 0)return;let[n,i]=await Promise.all([e.mentions.listBySurfaceTerms(Fp(t.query)),nn(e,t.query,{...t.chapters===null?{}:{chapters:t.chapters},match:on(t.match),objectHitLimit:Ut,textHitLimit:0,types:null})]),a=[...mm(r,{mentions:n}),...await hm(e,r,{mentions:n})],o=kd(a,i);await td({entityHits:o.entityHits,evidenceEvents:o.evidenceEvents,sessionId:t.sessionId})}async function EV(e,t,r,n){let i=await od(t.sessionId,r,n),a=i.slice(0,n),o=(await Promise.all(a.map(async c=>await km(e,c)))).filter(No),s=a.at(-1);return{items:o,nextCursor:i.length>n&&s!==void 0?{bucket:2,key:{archiveId:U_(s),chunkId:Sr(s.id.slice(13)),score:s.score??0}}:{bucket:3}}}async function IV(e,t,r,n){let i=await nn(e,t.query,{...t.chapters===null?{}:{chapters:t.chapters},match:on(t.match),objectHitLimit:0,...r===void 0?{}:{textAfter:{archiveId:r.archiveId,chapterId:r.chapterId,kind:r.kind,rank:r.rank,sentenceIndex:r.sentenceIndex}},textHitLimit:xV(n),types:["source","summary"]}),a=new Map((await z(e)).map(u=>[u.chapterId,u])),s=[...i?.textHits??[]].sort(Ld).filter(u=>xm(u,r)).slice(0,n+1),c=(await Promise.all(s.slice(0,n).map(async u=>{let p=await _w(e,a,u);return p===void 0?void 0:Sd(p,t.terms)}))).filter(No),d=s.at(n-1);return{items:c,nextCursor:s.length>n&&d!==void 0?{bucket:3,key:{archiveId:d.archiveId,chapterId:d.chapterId,kind:d.kind,rank:d.rank,sentenceIndex:d.sentenceIndex}}:void 0}}function xV(e){return Math.max(e+1,e*3+1,100)}function U_(e){return e.archiveId??0}function Tm(e){try{return ew(e)}catch{return}}var Rm=l(()=>{"use strict";Ze();Vc();Yi();qn();oe();Ed();Nw();Pd();_d();Jt();Em();Cm()});async function ln(e,t,r={}){let n=r.limit??20,i=Bo(r);if(r.cursor!==void 0&&(!i||!JP(r.cursor))){let x=Tm(r.cursor);if(x!==void 0)return await Qw(e,x,{...r,limit:n});let ce=tw(r.cursor),ot=await Lo(ce.sessionId,r.archiveKey??"archive");pm(r.types,ot.types);let ze=Lw(ot.types)?await nd(ce.sessionId,ce.offset,n,r.archiveKey??"archive",ce.createdAt):await rd(ce.sessionId,ce.offset,n,r.archiveKey??"archive",ce.createdAt);return await ei(e,{chapters:ze.chapters,items:await zt(e,ze.items,un(r,ce.sessionId)),lens:jo(ze.lens),lensHint:ze.lens==="broad"?fr:null,limit:n,match:on(ze.match),nextCursor:ze.nextCursor,order:r.order??"doc-asc",query:ze.query,terms:ze.terms,types:Do(ot.types)},r)}let a=r.types??null,o=a===null||a.includes("entity")||a.includes("triple"),s=vo(t);if(s===void 0)return Qn(t,[],r);if(i)return await vV(e,t,r,s);let c=await CV(e,r.chapters),d={archiveKey:r.archiveKey??"archive",chapters:r.chapters??null,lens:r.types===void 0?"broad":"typed",match:r.match??"any",order:r.order??"doc-asc",query:t,revisionScope:c,terms:s.terms,types:r.types??null},u=r.triplePattern===void 0,p=r.types===void 0&&r.triplePattern===void 0;if(u&&Aw(r)){let x=await hw(d,0,n);if(x!==void 0)return await ei(e,{chapters:x.chapters,items:await zt(e,x.items,un(r,x.sessionId)),lens:jo(x.lens),lensHint:x.lens==="broad"?fr:null,limit:n,match:on(x.match),nextCursor:x.nextCursor,order:r.order??"doc-asc",query:x.query,terms:x.terms,types:Do(x.types)},r)}else if(u&&!p){let x=await Xp(d,0,n);if(x!==void 0)return await ei(e,{chapters:x.chapters,items:await zt(e,x.items,un(r)),lens:jo(x.lens),lensHint:x.lens==="broad"?fr:null,limit:n,match:on(x.match),nextCursor:x.nextCursor,order:r.order??"doc-asc",query:x.query,terms:x.terms,types:Do(x.types)},r)}if(p){if(!await qe(e))throw new Error("Wiki Graph search index is missing or outdated. Run `<archive-uri>/index enable` before searching.");let x=await Xi({archiveKey:r.archiveKey??"archive",chapters:r.chapters??null,lens:"broad",match:r.match??"any",order:r.order??"doc-asc",query:t,revisionScope:c,terms:s.terms,types:null}),ce=await Lo(x,r.archiveKey??"archive");return await Qw(e,{createdAt:ce.createdAt,cursor:{bucket:0},sessionId:x},{...r,limit:n})}let m=o?await e.mentions.listBySurfaceTerms(Fp(t)):[],f=await Rw(e,t,r),b=[...o?[...mm(s,{mentions:m}),...await hm(e,s,{mentions:m})]:[],...f?.hits??[]];if(Aw(r)){let x=rm(t,fm(b,s,r.match??"any"),r,s.terms),ce=kd(x.items,f?.result),ot=await Ow(e,f?.result,r),ze=await fw({archiveKey:r.archiveKey??"archive",chapters:x.chapters,chunkHits:ot.chunkHits,entityHits:[...ce.entityHits,...ot.entityHits],evidenceEvents:[...ce.evidenceEvents,...ot.evidenceEvents],lens:x.lens,match:x.match,order:x.order,query:t,revisionScope:c,terms:x.terms,tripleHits:ot.tripleHits,types:x.types}),Ga=await nd(ze,0,n);return await ei(e,{...x,items:await zt(e,Ga.items,un(r,ze)),nextCursor:Ga.nextCursor},r)}let E=rm(t,fm(b,s,r.match??"any"),r,s.terms),N=kd(E.items,f?.result),C=await Ow(e,f?.result,r),q=await Xi({archiveKey:r.archiveKey??"archive",chapters:E.chapters,chunkHits:C.chunkHits,entityHits:[...N.entityHits,...C.entityHits],evidenceEvents:[...N.evidenceEvents,...C.evidenceEvents],items:E.items,lens:E.lens,match:E.match,order:E.order,query:t,revisionScope:c,terms:E.terms,tripleHits:C.tripleHits,types:E.types}),Q=await rd(q,0,n);return await ei(e,{...E,items:await zt(e,Q.items,un(r)),nextCursor:Q.nextCursor},r)}async function vV(e,t,r,n){let i=await Rw(e,t,r),a=i?.hits??[];return Qn(t,fm(a,n,r.match??"any"),r,i?.result.terms??n.terms)}async function CV(e,t){if(t===void 0||t.length===0)return JSON.stringify({chaptersRevision:await e.serials.getChaptersRevision(),scope:"all"});let r=[...new Set(t)].sort(D),n=await e.serials.getRevisions(r);return JSON.stringify({chapters:r.map(i=>[i,n.get(i)??0]),scope:"chapters"})}var $_=l(()=>{"use strict";Vc();Yi();oe();yd();Ed();Nw();Pd();_d();Rm();Jt()});var B_=l(()=>{"use strict";$_();Em();Cm();Rm();Ed();Jt()});async function zo(e,t,r){let n=Od(t,r);if(r.evidenceLimit===void 0)return{...n,items:n.items.map(o=>{if(o.type!=="triple")return o;let{evidenceLinks:s,...c}=o;return c})};let i=ee(),a=r.evidenceLimit;return{...n,items:await Promise.all(n.items.map(async o=>{if(o.evidence!==void 0)return o;if(o.type==="triple"){let s=await bm(e,o.evidenceLinks??[],a,i,r.sourceContext??le),{evidenceLinks:c,...d}=o;return{...d,evidence:s}}if(o.type==="node"){let s=yr(o.id);if(s.type!=="node")return o;let{node:c}=await kr(e,s.id);return{...o,evidence:await O_(e,xd(c),a,i,r.sourceContext??le,r.order??"doc-asc")}}return o}))}}function Od(e,t){let r=t.limit??20,n=TV(t.cursor),i=e.slice(n,n+r),a=n+i.length;return{items:i,limit:r,nextCursor:a>=e.length?null:String(a)}}function TV(e){if(e===void 0)return 0;if(!/^(0|[1-9][0-9]*)$/u.test(e))throw new Error(`Invalid related cursor: ${e}`);return Number(e)}var Pm=l(()=>{"use strict";Er();Rd();De()});function Vw(e,t){let r=(t.score??0)-(e.score??0);return r!==0?r:ea(_m(e),_m(t),"doc-asc")}function q_(e,t="doc-asc",r){return[...e].sort((n,i)=>ea(_m(n,r),_m(i,r),t))}function Xw(e,t,r){let n=r==="doc-asc"?1:-1;return[...e].sort((i,a)=>Zi(H_(i.node),H_(a.node),t)*n)}function _m(e,t){let r=PV(e,t),n=e.type==="triple"?e.evidenceLinks?.length??0:void 0;return{field:"title",id:e.id,...r===void 0?{}:{position:r},...n===void 0?{}:{score:n},snippet:e.summary,title:e.label,type:RV(e.type)}}function RV(e){switch(e){case"edge":case"state":return"meta";default:return e}}function PV(e,t){if(e.type==="triple")return _V(e.evidenceLinks??[],t)}function _V(e,t){let r=e.flatMap(i=>i.evidenceSentenceIds),[n]=r.sort((i,a)=>Zi(i,a,t));return n===void 0?void 0:pd(n,t)}function H_(e){return[...e.sentenceIds].sort(Zi)[0]??[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER]}var Nd=l(()=>{"use strict";oe()});async function Yw(e,t,r){if(r===void 0)return t;let n=await Uo(e,r,{types:["node"]});if(n===void 0)return[];let i=new Map,a=new Set(t.flatMap(o=>{if(o.type!=="node")return[];let s=yr(o.id);return s.type==="node"?[s.id]:[]}));for(let o of n.objectHits){if(o.ownerKind!==J.chunk||!a.has(Number(o.ownerId)))continue;let s=Number(o.ownerId),c=i.get(s)??[];c.push(o.score),i.set(s,c)}return t.flatMap(o=>{if(o.type!=="node")return[];let s=yr(o.id);if(s.type!=="node")return[];let c=i.get(s.id);return c===void 0?[]:[{...o,score:zn(c)}]}).sort(Vw)}async function J_(e,t,r,n){if(n===void 0)return t;let i=await AV(e,t,r),a=await Uo(e,n,{chapters:[...i.chapterIds],types:["entity","source"]});if(a===void 0)return[];let o=new Map(a.textHits.filter(c=>c.kind===be.source).map(c=>[sa(0,c.chapterId,c.sentenceIndex),c.score])),s=new Map;for(let c of a.objectHits){if(c.ownerKind!==J.entity||c.ownerId===r||c.chapterId===void 0)continue;let d=z_(c.ownerId,c.chapterId),u=s.get(d)??[];u.push(c.score),s.set(d,u)}return t.flatMap(c=>{if(c.type!=="triple")return[];let d=[...[...i.endpointKeysByTripleId.get(c.id)??[]].flatMap(u=>s.get(u)??[]),...(c.evidenceLinks??[]).flatMap(u=>u.evidenceSentenceIds.flatMap(([p,m])=>{let f=o.get(sa(0,p,m));return f===void 0?[]:[f]}))];return d.length===0?[]:[{...c,score:zn(d)}]}).sort(Vw)}async function AV(e,t,r){let n=new Set,i=new Map,a=new Map;for(let o of t){if(o.type!=="triple")continue;let s=new Set;for(let c of o.evidenceLinks??[]){for(let[p]of c.evidenceSentenceIds)n.add(p);let[d,u]=await Promise.all([K_(e,a,c.sourceMentionId),K_(e,a,c.targetMentionId)]);for(let p of[d,u])p===void 0||p.qid===r||(n.add(p.chapterId),s.add(z_(p.qid,p.chapterId)))}i.set(o.id,s)}return{chapterIds:n,endpointKeysByTripleId:i}}async function K_(e,t,r){return t.has(r)||t.set(r,await e.mentions.getById(r)),t.get(r)}function z_(e,t){return`${e}:${t}`}var Am=l(()=>{"use strict";oe();De();Jt();Ed();qn();Nd()});async function Q_(e,t,r){let n=dm(await e.mentions.listByQid(t.qid),t.chapterId);if(n.length===0)throw new Error(`Entity ${c_(t.qid)} was not found in this archive.`);let i=[...new Set(n.map(s=>s.chapterId))].sort(D),a=r.role??"any",o=new Map;for(let s of i)for(let c of await e.mentionLinks.listByChapter(s)){let[d,u]=await Promise.all([e.mentions.getById(c.sourceMentionId),e.mentions.getById(c.targetMentionId)]);if(d===void 0||u===void 0||!LV(d.qid,u.qid,t.qid,a))continue;let p=br(d.qid,c.predicate,u.qid),m=o.get(p);if(m!==void 0){o.set(p,{...m,evidenceLinks:[...m.evidenceLinks??[],c]});continue}o.set(p,{evidenceLinks:[c],id:p,label:`${d.surface} ${c.predicate} ${u.surface}`,objectLabel:u.surface,objectQid:u.qid,predicate:c.predicate,subjectLabel:d.surface,subjectQid:d.qid,summary:`${d.qid} ${c.predicate} ${u.qid}`,type:"triple"})}return await zo(e,await J_(e,q_([...o.values()],r.order??"doc-asc",await e.serials.listDocumentOrders()),t.qid,r.query),r)}function LV(e,t,r,n){let i=e===r,a=t===r,o=i&&a;switch(n){case"any":return i||a;case"subject":return i&&!o;case"object":return a&&!o;case"self":return o}}var Zw=l(()=>{"use strict";oe();Kt();Pm();Am();Nd()});import{stat as OV}from"fs/promises";async function eS(e){let t=Ha(),r=await V_(t);if(r===void 0)return{freedBytes:0,removed:0,scanned:0};let n=await V(t,Z_);try{await eA(n);let i=await WV(n),a=new Date(e.now-NV).toISOString(),o=await FV(n,a);!e.dryRun&&o>0&&(await n.transaction(async()=>{await n.run("DELETE FROM qid_cache WHERE checked_at < ?",[a]),await n.run("DELETE FROM disambiguation_cache WHERE checked_at < ?",[a])}),await n.run("VACUUM"));let s=await V_(t);return{freedBytes:e.dryRun?jV(r,i,o):Math.max(0,r-(s??0)),removed:o,scanned:i}}finally{await n.close()}}function jV(e,t,r){return t===0||r===0?0:Math.round(e*(r/t))}async function eA(e){await DV(e),await MV(e)}async function DV(e){(await jd(e,"qid_cache")).has("language")||await e.transaction(async()=>{(await jd(e,"qid_cache")).has("language")||(await e.run("ALTER TABLE qid_cache RENAME TO qid_cache_legacy"),await e.run(X_),await e.run(`
|
|
2137
|
+
INSERT OR IGNORE INTO qid_cache (
|
|
2138
|
+
qid, language, label, description, pages_json, checked_at, updated_at
|
|
2139
|
+
)
|
|
2140
|
+
SELECT qid, 'en', label, description, pages_json, checked_at, updated_at
|
|
2141
|
+
FROM qid_cache_legacy
|
|
2142
|
+
`),await e.run("DROP TABLE qid_cache_legacy"))})}async function MV(e){(await jd(e,"disambiguation_cache")).has("wiki")||await e.transaction(async()=>{(await jd(e,"disambiguation_cache")).has("wiki")||(await e.run("ALTER TABLE disambiguation_cache RENAME TO disambiguation_cache_legacy"),await e.run(Y_),await e.run(`
|
|
2143
|
+
INSERT OR IGNORE INTO disambiguation_cache (
|
|
2144
|
+
qid, wiki, pages_json, profile_json, checked_at
|
|
2145
|
+
)
|
|
2146
|
+
SELECT qid, 'enwiki', pages_json, profile_json, checked_at
|
|
2147
|
+
FROM disambiguation_cache_legacy
|
|
2148
|
+
`),await e.run("DROP TABLE disambiguation_cache_legacy"))}),(await jd(e,"disambiguation_cache")).has("profile_error_json")||await e.run("ALTER TABLE disambiguation_cache ADD COLUMN profile_error_json TEXT")}async function jd(e,t){let r=await e.queryAll(`PRAGMA table_info(${t})`,void 0,n=>ti(n.name,"name"));return new Set(r)}async function WV(e){let t=await Lm(e,"qid_cache"),r=await Lm(e,"disambiguation_cache");return t+r}async function FV(e,t){let r=await Lm(e,"qid_cache",t),n=await Lm(e,"disambiguation_cache",t);return r+n}async function Lm(e,t,r){return await e.queryOne(`
|
|
2149
|
+
SELECT COUNT(*) AS count
|
|
2150
|
+
FROM ${t}
|
|
2151
|
+
${r===void 0?"":"WHERE checked_at < ?"}
|
|
2152
|
+
`,r===void 0?void 0:[r],n=>y(n,"count"))??0}async function V_(e){try{return(await OV(e)).size}catch(t){if(R(t)&&t.code==="ENOENT")return;throw t}}function GV(e){let t=Dd(e.description),r=Dd(e.label);return{checkedAt:ti(e.checked_at,"checked_at"),...t===void 0?{}:{description:t},...r===void 0?{}:{label:r},qid:ti(e.qid,"qid"),sitelinks:BV(ti(e.pages_json,"pages_json")),updatedAt:ti(e.updated_at,"updated_at")}}function UV(e){let t=Dd(e.profile_error_json),r=t===void 0?void 0:zV(t);return{checkedAt:ti(e.checked_at,"checked_at"),disambiguationQid:ti(e.qid,"qid"),pages:HV(ti(e.pages_json,"pages_json")),...r===void 0?{}:{profileError:r},...Dd(e.profile_json)===void 0?{}:{profile:JV(Dd(e.profile_json))}}}function $V(e){if(e===void 0)return!1;let t=Date.parse(e.retryAfter);return Number.isFinite(t)&&t<=Date.now()}function BV(e){let t=JSON.parse(e);return Array.isArray(t)?t.filter(qV):[]}function HV(e){let t=JSON.parse(e);return Array.isArray(t)?t.filter(KV):[]}function qV(e){if(typeof e!="object"||e===null)return!1;let t=e;return typeof t.isDisambiguation=="boolean"&&typeof t.title=="string"&&t.title!==""&&tA(t.wiki)}function KV(e){if(typeof e!="object"||e===null)return!1;let t=e;return Array.isArray(t.linkedQids)&&t.linkedQids.every(YV)&&typeof t.text=="string"&&t.text!==""&&typeof t.title=="string"&&t.title!==""&&tA(t.wiki)}function JV(e){let t=JSON.parse(e);if(!QV(t))throw new Error("Expected profile_json to contain a disambiguation profile.");return t}function zV(e){let t=JSON.parse(e);if(XV(t))return t}function QV(e){if(typeof e!="object"||e===null)return!1;let t=e;return Array.isArray(t.meanings)&&t.meanings.every(VV)&&typeof t.sourceQid=="string"&&/^Q[1-9]\d*$/u.test(t.sourceQid)&&(!("surface"in t)||typeof t.surface=="string")}function VV(e){if(typeof e!="object"||e===null)return!1;let t=e;return(!("category"in t)||typeof t.category=="string")&&typeof t.information=="string"&&typeof t.name=="string"&&t.name!==""&&(t.priority==="primary"||t.priority==="secondary"||t.priority==="other")&&typeof t.qid=="string"&&/^Q[1-9]\d*$/u.test(t.qid)}function XV(e){if(typeof e!="object"||e===null)return!1;let t=e;return typeof t.failedAt=="string"&&typeof t.message=="string"&&typeof t.retryAfter=="string"}function YV(e){if(typeof e!="object"||e===null)return!1;let t=e;return typeof t.qid=="string"&&/^Q[1-9]\d*$/u.test(t.qid)&&typeof t.title=="string"&&t.title!==""}function tA(e){return e==="enwiki"||e==="zhwiki"}function ti(e,t){if(typeof e!="string")throw new Error(`Expected ${t} to be a string.`);return e}function Dd(e){return typeof e=="string"&&e!==""?e:void 0}var X_,Y_,Z_,NV,Qo,tS=l(()=>{"use strict";Ue();M();X();H();X_=`
|
|
2153
|
+
CREATE TABLE IF NOT EXISTS qid_cache (
|
|
2154
|
+
qid TEXT NOT NULL,
|
|
2155
|
+
language TEXT NOT NULL,
|
|
2156
|
+
label TEXT,
|
|
2157
|
+
description TEXT,
|
|
2158
|
+
pages_json TEXT NOT NULL,
|
|
2159
|
+
checked_at TEXT NOT NULL,
|
|
2160
|
+
updated_at TEXT NOT NULL,
|
|
2161
|
+
PRIMARY KEY (qid, language)
|
|
2162
|
+
);
|
|
2163
|
+
`,Y_=`
|
|
2164
|
+
CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
2165
|
+
qid TEXT NOT NULL,
|
|
2166
|
+
wiki TEXT NOT NULL,
|
|
2167
|
+
pages_json TEXT NOT NULL,
|
|
2168
|
+
profile_json TEXT,
|
|
2169
|
+
profile_error_json TEXT,
|
|
2170
|
+
checked_at TEXT NOT NULL,
|
|
2171
|
+
PRIMARY KEY (qid, wiki)
|
|
2172
|
+
);
|
|
2173
|
+
`,Z_=`
|
|
2174
|
+
${X_}
|
|
2175
|
+
|
|
2176
|
+
CREATE INDEX IF NOT EXISTS idx_qid_cache_checked_at
|
|
2177
|
+
ON qid_cache(checked_at);
|
|
2178
|
+
|
|
2179
|
+
${Y_}
|
|
2180
|
+
|
|
2181
|
+
CREATE INDEX IF NOT EXISTS idx_disambiguation_cache_checked_at
|
|
2182
|
+
ON disambiguation_cache(checked_at);
|
|
2183
|
+
`,NV=720*60*60*1e3,Qo=class e{#e;constructor(t){this.#e=t}static async open(t){let r=t??Ha(),n=await V(r,Z_);try{await eA(n)}catch(i){throw await n.close().catch(()=>{}),i}return new e(n)}async close(){await this.#e.close()}async getQids(t,r){if(t.length===0)return new Map;let n=new Map;for(let i of t){let a=await this.#e.queryOne(`
|
|
2184
|
+
SELECT *
|
|
2185
|
+
FROM qid_cache
|
|
2186
|
+
WHERE qid = ? AND language = ?
|
|
2187
|
+
`,[i,r],GV);a!==void 0&&n.set(i,a)}return n}async putQids(t,r){t.length!==0&&await this.#e.transaction(async()=>{for(let n of t)await this.#e.run(`
|
|
2188
|
+
INSERT INTO qid_cache (
|
|
2189
|
+
qid, language, label, description, pages_json, checked_at, updated_at
|
|
2190
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
2191
|
+
ON CONFLICT(qid, language) DO UPDATE SET
|
|
2192
|
+
label = excluded.label,
|
|
2193
|
+
description = excluded.description,
|
|
2194
|
+
pages_json = excluded.pages_json,
|
|
2195
|
+
checked_at = excluded.checked_at,
|
|
2196
|
+
updated_at = excluded.updated_at
|
|
2197
|
+
`,[n.qid,r,n.label??null,n.description??null,JSON.stringify(n.sitelinks),n.checkedAt,n.updatedAt])})}async getDisambiguations(t,r){if(t.length===0)return new Map;let n=new Map;for(let i of t){let a=await this.#e.queryOne(`
|
|
2198
|
+
SELECT *
|
|
2199
|
+
FROM disambiguation_cache
|
|
2200
|
+
WHERE qid = ? AND wiki = ?
|
|
2201
|
+
`,[i,r],UV);if(a!==void 0){if($V(a.profileError))continue;n.set(i,a)}}return n}async putDisambiguations(t,r){t.length!==0&&await this.#e.transaction(async()=>{for(let n of t)await this.#e.run(`
|
|
2202
|
+
INSERT INTO disambiguation_cache (
|
|
2203
|
+
qid, wiki, pages_json, profile_json, profile_error_json, checked_at
|
|
2204
|
+
) VALUES (?, ?, ?, ?, ?, ?)
|
|
2205
|
+
ON CONFLICT(qid, wiki) DO UPDATE SET
|
|
2206
|
+
pages_json = excluded.pages_json,
|
|
2207
|
+
profile_json = excluded.profile_json,
|
|
2208
|
+
profile_error_json = excluded.profile_error_json,
|
|
2209
|
+
checked_at = excluded.checked_at
|
|
2210
|
+
`,[n.disambiguationQid,r,JSON.stringify(n.pages),n.profile===void 0?null:JSON.stringify(n.profile),n.profileError===void 0?null:JSON.stringify(n.profileError),n.checkedAt])})}}});var Y,mt,Md,Vo,Wd,pn,rS=l(()=>{"use strict";Y=class extends Error{issues;constructor(t){super(`Parse failed with ${t.length} issue(s)`),this.name="ParsedJsonError",this.issues=[...t]}},mt=class extends Error{attempts;maxRetries;constructor(t,r,n){super(t),this.name="GuaranteedRequestFailureError",this.attempts=r,this.maxRetries=n}},Md=class extends mt{constructor(t,r){super("LLM returned empty response after all retries",t,r),this.name="GuaranteedEmptyResponseError"}},Vo=class extends mt{response;reason;constructor(t,r,n){super(`Suspected model refusal after ${t} JSON syntax error attempt(s): ${n.reason}. Last response: ${JSON.stringify(n.response)}`,t,r),this.name="SuspectedModelRefusalError",this.response=n.response,this.reason=n.reason}},Wd=class extends mt{issues;response;constructor(t,r,n,i){super("Schema validation failed after all retries",t,r),this.name="GuaranteedSchemaValidationError",this.issues=[...n.issues],this.response=n.response,this.cause=i}},pn=class extends mt{issues;response;constructor(t,r,n,i){super("Parse validation failed after all retries",t,r),this.name="GuaranteedParseValidationError",this.issues=[...n.issues],this.response=n.response,this.cause=i}}});function rA(e,t){return[{role:"system",content:e},{role:"user",content:t}]}function nA(e){let t=e.trim();return t===""?"ambiguous":eX(t)?"malformed_json":nX(t)?"natural_language":nS(t)?"ambiguous":aA(t)?"natural_language":"ambiguous"}function iA(e){let t=e.trim().toLowerCase();return t.includes("malformed_json")?"malformed_json":t.includes("natural_language")?"natural_language":(t.includes("ambiguous"),"ambiguous")}function eX(e){return/^(?:```json\b|```|{|\[)/i.test(e)||/"[^"\r\n]+"\s*:/.test(e)||nS(e)&&rX(e)}function nS(e){let t=(e.match(/[{}[\]:,]/g)??[]).length+(e.match(/"/g)??[]).length;return/:\s/.test(e)||t>=3||tX(e)||/[:,]\s*$/.test(e)}function tX(e){return Om(e,"{")!==Om(e,"}")||Om(e,"[")!==Om(e,"]")}function rX(e){return/[:, "{[]\s*$/.test(e)}function nX(e){return ZV.some(t=>t.test(e))?!0:!nS(e)&&aA(e)}function aA(e){return/[\p{Script=Han}A-Za-z]/u.test(e)&&!/^[{[]/.test(e)&&!/"[^"\r\n]+"\s*:/.test(e)}function Om(e,t){return e.split(t).length-1}var Me,ZV,iS=l(()=>{"use strict";Me="guaranteed/response_intent_classifier",ZV=[/\b(?:sorry|apologies)\b/i,/\b(?:i\s+can(?:not|'t)|unable\s+to|do\s+not\s+understand)\b/i,/\b(?:cannot\s+answer|can't\s+answer|can't\s+help)\b/i,/\bno\s+relevant\s+result/i,/抱歉/,/无法回答/,/无法识别/,/无法给到/,/没有找到相关的结果/]});import{jsonrepair as iX}from"jsonrepair";function oA(e){let t=e.replace(/```(?:json)?\s*\n(.*?)\n```/gs,"$1"),r=t.match(/\[[\s\S]*\]/),n=t.match(/\{[\s\S]*\}/);return r?.index!==void 0&&r[0]!==void 0&&(n?.index===void 0||r.index<n.index)?r[0]:n?.[0]!==void 0?n[0]:t.trim()}function sA(e){try{return iX(e)}catch{return e}}function cA(e){return["Your JSON has a syntax error.",`Error: ${e.message}`,"","Common issues:","- Trailing commas in arrays or objects",'- Unescaped quotes in strings, for example: "He said \\"hi\\""','- Single quotes instead of double quotes, for example: {"key": "value"}',"- Missing commas between items","","Regenerate the complete and valid JSON directly. Do not add explanations or markdown fences."].join(`
|
|
2211
|
+
`)}function dA(e){return["Your previous reply looked like malformed JSON.",`Error: ${e.message}`,"","Return complete and valid JSON directly.","Do not answer conversationally.","Do not add explanations or markdown fences."].join(`
|
|
2212
|
+
`)}function aS(){return["Your previous reply was plain natural language, not a JSON object or array.","","Do not apologize or explain.","Do not answer conversationally.","Return complete and valid JSON directly."].join(`
|
|
2213
|
+
`)}function uA(e){return["Your JSON has structural issues.","",...e.issues.map(mA),"","Regenerate the complete and valid JSON directly. Do not add explanations."].join(`
|
|
2214
|
+
`)}function lA(e){return e.issues.map(mA)}function pA(e){return["Your JSON has the following issues.","",...e.map(r=>`- ${r}`),"","Regenerate the complete and valid JSON directly. Do not add explanations."].join(`
|
|
2215
|
+
`)}function mA(e){return`- **${e.path.length===0?"<root>":e.path.join(".")}**: ${e.message}`}var hA=l(()=>{"use strict"});async function Ae(e){let t=[...e.messages],r=[...e.messages],n=0,i=0,a=e.maxRetries??yA;for(let o=0;o<=a;o+=1){let s=await e.request(r,o,a);if(s===void 0||s.trim()===""){if(o>=a)throw new Md(o+1,a);continue}let c;try{let u=oA(s),p=sA(u);c=JSON.parse(p)}catch(u){let p=await gA(e,s,o);if(p==="natural_language"){if(n+=1,i=0,n>=2||o>=a){let m=o>=a?"last retry still returned natural-language content":"two consecutive retries returned natural-language content";throw new Vo(o+1,a,{response:s,reason:m})}r=Fd(t,s,aS(),!1);continue}n=0,i+=1,r=Fd(t,s,p==="malformed_json"?dA(fA(u)):cA(fA(u)),i<aX);continue}n=0,i=0;let d=await e.schema.safeParseAsync(c);if(!d.success){if(oX(d.error,c)&&await gA(e,s,o)==="natural_language"){if(n+=1,i=0,n>=2||o>=a){let m=o>=a?"last retry still returned natural-language content":"two consecutive retries returned natural-language content";throw new Vo(o+1,a,{response:s,reason:m})}r=Fd(t,s,aS(),!1);continue}n=0;let u=uA(d.error);if(o>=a)throw new Wd(o+1,a,{issues:lA(d.error),response:s},d.error);r=Fd(t,s,u,!0);continue}try{return await e.parse(d.data,o,a)}catch(u){if(!(u instanceof Y))throw u;let p=pA(u.issues);if(o>=a)throw new pn(o+1,a,{issues:u.issues,response:s},u);r=Fd(t,s,p,!0)}}throw new Error("requestGuaranteedJson failed unexpectedly")}function Fd(e,t,r,n){return[...e,...n?[{role:"assistant",content:t}]:[],{role:"user",content:r}]}function fA(e){return e instanceof SyntaxError?e:new SyntaxError(String(e))}async function gA(e,t,r){let n=nA(t);if(n!=="ambiguous")return n;try{let i=await e.request(rA(e.responseIntentClassifierPrompt,t),r,e.maxRetries??yA);if(i===void 0||i.trim()==="")return"natural_language";let a=iA(i);return a==="ambiguous"?"natural_language":a}catch{return"natural_language"}}function oX(e,t){return sX(t)?e.issues.some(r=>r.code==="invalid_type"&&r.path.length===0&&(r.expected==="object"||r.expected==="array")):!1}function sX(e){return e===null||typeof e!="object"&&typeof e!="function"}var yA,aX,bA=l(()=>{"use strict";iS();rS();hA();yA=12,aX=3});var Rt=l(()=>{"use strict";rS();iS();bA()});import{z as Pt}from"zod";function Nm(e){return async t=>await Ae({messages:pX(t,e.maxMeanings??24),parse:r=>lX(t,uX(r)),request:e.request,responseIntentClassifierPrompt:Me,schema:dX,...e.maxRetries===void 0?{}:{maxRetries:e.maxRetries}})}function uX(e){return{meanings:e.meanings.map(t=>({...t.category===void 0?{}:{category:t.category},information:t.information,name:t.name,priority:t.priority,qid:t.qid})),sourceQid:e.sourceQid,...e.surface===void 0?{}:{surface:e.surface}}}function lX(e,t){let r=new Set(e.pageQidLinks.map(o=>o.qid)),n=[],i=new Set,a=[];t.sourceQid!==e.sourceQid&&n.push(`sourceQid must be ${e.sourceQid}, but got ${t.sourceQid}.`);for(let o of t.meanings)if(!(o.qid===null||o.qid==="")){if(!r.has(o.qid)){n.push(`Meaning "${o.name}" selected qid ${o.qid}, but it is not present in the input disambiguation page links.`);continue}if(i.has(o.qid)){n.push(`Duplicate meaning qid ${o.qid}. Return each QID once.`);continue}i.add(o.qid),a.push({...o.category===void 0?{}:{category:o.category},information:o.information,name:o.name,priority:o.priority,qid:o.qid})}if(a.length===0&&n.push("Return at least one meaning from the pageQidLinks list."),n.length>0)throw new Y(n);return{meanings:a,sourceQid:e.sourceQid,...t.surface===void 0?{}:{surface:t.surface}}}function pX(e,t){return[{role:"system",content:["You normalize Wikipedia disambiguation pages for entity grounding.","Return JSON only.","Do not invent QIDs, pages, people, places, works, or meanings.","Only use QIDs from the pageQidLinks list.","Only include meanings whose target has a QID in pageQidLinks.","Omit disambiguation bullets or page items that have no QID link.","Never use null, empty strings, placeholder QIDs, or made-up QIDs.","The information field must only copy or summarize text present on the disambiguation page itself.","Do not use Wikidata descriptions or external knowledge to fill information.","When a bullet contains multiple links, treat administrative divisions, parent locations, categories, and locator/explanatory links as context only.","Do not include context-only links as meanings unless the link itself is the disambiguated target.","Prefer common and context-independent meanings over exhaustive long-tail lists."].join(`
|
|
2216
|
+
`)},{role:"user",content:[`Source QID: ${e.sourceQid}`,e.surface===void 0?"":`Surface: ${e.surface}`,`Maximum meanings: ${t}`,"","Page QID links:",JSON.stringify(e.pageQidLinks.map(mX),null,2),"","Structured disambiguation pages:",JSON.stringify(e.pages.map(hX),null,2),"","Disambiguation page text:",e.pages.map(r=>`--- ${r.wiki}:${r.title} ---
|
|
2217
|
+
${bX(r.text)}`).join(`
|
|
2218
|
+
|
|
2219
|
+
`),"","Return this JSON shape:",JSON.stringify({meanings:[{category:"person | place | organization | work | concept | event | other",information:"short information copied or summarized from the disambiguation page; empty string if no information is present",name:"display name",priority:"primary | secondary | other",qid:"QID from pageQidLinks"}],sourceQid:e.sourceQid,...e.surface===void 0?{}:{surface:e.surface}},null,2)].filter(r=>r!=="").join(`
|
|
2220
|
+
`)}]}function mX(e){return{qid:e.qid,title:e.title}}function hX(e){return{...e.pageId===void 0?{}:{pageid:e.pageId},items:fX(e.text),title:e.title,wiki:e.wiki}}function fX(e){return e.split(`
|
|
2221
|
+
`).map(t=>t.trim()).filter(t=>t.startsWith("* ")).map(t=>({links:gX(t),text:yX(t.slice(2).trim())}))}function gX(e){return[...e.matchAll(wA)].map(t=>({label:t[1],qid:t[2]}))}function yX(e){return e.replace(wA,"$1")}function bX(e){return e.length<=16e3?e:`${e.slice(0,16e3)}
|
|
2222
|
+
|
|
2223
|
+
[truncated]`}var cX,dX,wA,SA=l(()=>{"use strict";Rt();cX=Pt.object({category:Pt.string().min(1).optional(),information:Pt.string(),name:Pt.string().min(1),priority:Pt.enum(["primary","secondary","other"]),qid:Pt.union([Pt.string(),Pt.null()])}).strict(),dX=Pt.object({meanings:Pt.array(cX),sourceQid:Pt.string().regex(/^Q[1-9]\d*$/u),surface:Pt.string().min(1).optional()}).strict();wA=/\[\[([^\]|]+)\|wikg:\/\/qid=(Q[1-9]\d*)\]\]/gu});async function wX(e){await new Promise(t=>{setTimeout(t,e)})}function jm(e){if(e===null||e.trim()==="")return;let t=Number(e);if(Number.isFinite(t))return Math.max(0,t*1e3);let r=Date.parse(e);if(!Number.isNaN(r))return Math.max(0,r-Date.now())}var Xo,oS=l(()=>{"use strict";Xo=class{#e;#t;#r=0;#n=0;#i=0;#a=[];constructor(t){this.#e=Math.max(1,Math.floor(t.concurrency)),this.#t=Math.max(0,Math.floor(t.minRequestIntervalMs))}async use(t){await this.#o();try{return await t()}finally{this.#r-=1,this.#c()}}blockFor(t){!Number.isFinite(t)||t<=0||(this.#n=Math.max(this.#n,Date.now()+t))}async#o(){await new Promise(t=>{this.#a.push(t),this.#c()}),await this.#s(),this.#r+=1,this.#i=Date.now()}async#s(){let t=Math.max(0,this.#n-Date.now()),r=Math.max(0,this.#i+this.#t-Date.now()),n=Math.max(t,r);n>0&&await wX(n)}#c(){if(this.#r>=this.#e)return;let t=this.#a.shift();t!==void 0&&t()}}});import{createHash as SX}from"crypto";import{appendFile as kX}from"fs/promises";function kA(e){if(e===void 0)return new Dm;let t=Bg({category:"wikipage",fileName:"wikipage-fetch.jsonl",logDirPath:e});return t===void 0?new Dm:new dS(t)}function EX(e){return{action:e.url.searchParams.get("action")??null,attempt:e.attempt,batch:IX(e.url),durationMs:e.durationMs,endpoint:`${e.url.origin}${e.url.pathname}`,host:e.url.host,ok:e.response?.ok??!1,startedAt:new Date(e.startedAt).toISOString(),...e.response===void 0?{}:{retryAfter:e.response.headers.get("retry-after"),status:e.response.status},...e.error===void 0?{}:{error:xX(e.error)},...e.responseText===void 0?{}:{responseText:RX(e.responseText)}}}function IX(e){let t=e.searchParams.get("ids");if(t!==null)return cS("qid",t.split("|"));let r=e.searchParams.get("titles");if(r!==null)return cS("title",r.split("|"));let n=e.searchParams.get("page");if(n!==null)return cS("page",[n])}function cS(e,t){let r=t.join("|");return{count:t.length,hash:SX("sha256").update(r).digest("hex").slice(0,16),kind:e,sample:t.slice(0,5)}}function xX(e){return{message:Ce(e),name:e instanceof Error?e.name:typeof e,...vX(e)}}function vX(e){if(!(e instanceof Error)||e.cause===void 0)return{};let t=e.cause;return{cause:t instanceof Error?{message:Ce(t),name:t.name,...TX(t)}:CX(t)}}function CX(e){if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean"||typeof e=="bigint"||typeof e=="symbol")return e.toString();try{return JSON.stringify(e)}catch{return"unserializable error cause"}}function TX(e){let t=e.code;return typeof t=="string"?{code:t}:{}}function RX(e){return e.length<=sS?e:`${e.slice(0,sS)}
|
|
2224
|
+
[truncated ${e.length-sS} chars]`}var sS,Dm,dS,EA=l(()=>{"use strict";jt();H();sS=16384,Dm=class{filePath=void 0;append(){return Promise.resolve()}warnFailed(){}},dS=class{#e;#t=!1;constructor(t){this.#e=t}get filePath(){return this.#e}async append(t){await kX(this.#e,`${JSON.stringify(EX(t))}
|
|
2225
|
+
`,"utf8")}warnFailed(){this.#t||(this.#t=!0,st({component:"wikipage"}).warn(`
|
|
2226
|
+
[Wikipage] Failed with fetch log: ${this.#e}`))}}});function We(e){return typeof e=="object"&&e!==null?e:{}}function IA(e){return Array.isArray(e)?e:[]}function mn(e){return typeof e=="string"&&e!==""?e:void 0}function Gd(e){return typeof e=="number"&&Number.isFinite(e)?e:void 0}function Ud(e,t){let r=e;for(let n of t)r=We(r)[n];return mn(r)}var Mm=l(()=>{"use strict"});import{DomUtils as PX,parseDocument as _X}from"htmlparser2";function xA(e,t){return e.replace(/\[\[([^\]|]+)\|wikigraph-title:\/\/([^\]\s]+)\]\]/gu,(r,n,i)=>{let a=decodeURIComponent(i),o=t.get(a);return o===void 0?n:`[[${n}|wikg://qid=${o}]]`})}function vA(e){if(e===void 0||e.trim()==="")return{linkedTitles:[],text:""};let t=new Set,r=_X(e),n=ca(PX.getChildren(r),{linkedTitles:t,listDepth:0}).replace(/[ \t]+\n/gu,`
|
|
2227
|
+
`).replace(/\n{3,}/gu,`
|
|
2228
|
+
|
|
2229
|
+
`).trim();return{linkedTitles:[...t],text:n}}function ca(e,t){return e.map(r=>LX(r,t)).join("")}function LX(e,t){let r=We(e),n=mn(r.type);if(n==="text")return MX(mn(r.data)??"");if(n!=="tag"&&n!=="script"&&n!=="style")return"";let i=e;if(jX(i))return"";let a=i.name.toLowerCase();if(a==="a")return OX(i,t);if(/^h[1-6]$/u.test(a)){let o=Math.min(6,Math.max(1,Number(a.slice(1)))),s="=".repeat(o),c=ca(i.children??[],t).trim();return c===""?"":`
|
|
2230
|
+
${s} ${c} ${s}
|
|
2231
|
+
`}if(a==="li"){let o=" ".repeat(Math.max(0,t.listDepth-1)),s=ca(i.children??[],{...t,listDepth:t.listDepth+1}).trim();return s===""?"":`
|
|
2232
|
+
${o}* ${s}`}if(a==="ul"||a==="ol")return`${ca(i.children??[],t)}
|
|
2233
|
+
`;if(DX(a)){let o=ca(i.children??[],t).trim();return o===""?"":`
|
|
2234
|
+
${o}
|
|
2235
|
+
`}return ca(i.children??[],t)}function OX(e,t){let r=ca(e.children??[],t).trim(),n=NX(e);return n===void 0?r:(t.linkedTitles.add(n),`[[${r===""?n:r}|${AX}${encodeURIComponent(n)}]]`)}function NX(e){let t=e.attribs.title,r=e.attribs.href;if(t!==void 0&&t!==""&&!t.includes(":"))return t;if(r===void 0||!r.startsWith("/wiki/"))return;let n=decodeURIComponent(r.slice(6)).replaceAll("_"," ");return n===""||n.includes(":")?void 0:n}function jX(e){let t=e.name.toLowerCase();if(["script","style","table","sup"].includes(t))return!0;let r=e.attribs.class??"";return["catlinks","metadata","mw-editsection","mw-empty-elt","navbox","noprint","reference","reflist","shortdescription","toc"].some(n=>r.split(/\s+/u).includes(n))}function DX(e){return["blockquote","br","dd","div","dl","dt","p","section"].includes(e)}function MX(e){return e.replace(/\s+/gu," ")}var AX,uS=l(()=>{"use strict";Mm();AX="wikigraph-title://"});function CA(e){let t=e.headers.get("content-type");return t===null?!1:/(?:json|text|xml|javascript|html)/iu.test(t)}function TA(e){return e instanceof Yo?e.status===429||e.status===500||e.status===502||e.status===503||e.status===504:e instanceof TypeError}function RA(e,t,r){if(e instanceof Yo&&e.retryAfterMs!==void 0)return e.retryAfterMs;if(r<=0)return 0;let n=r*2**t,i=Math.floor(Math.random()*Math.min(r,250));return n+i}async function PA(e){e<=0||await new Promise(t=>{setTimeout(t,e)})}var Yo,_A=l(()=>{"use strict";Yo=class extends Error{retryAfterMs;status;url;constructor(t,r,n){super(`Wikimedia request failed with ${r}: ${t.toString()}`),this.retryAfterMs=n,this.status=r,this.url=t}}});function AA(e){return[...new Set([e,"zh","en"])].join("|")}function $d(e,t){for(let r of[t,"zh","en"]){let n=Ud(e,[r,"value"]);if(n!==void 0)return n}}function pS(e){return e==="zhwiki"?"https://zh.wikipedia.org/":"https://en.wikipedia.org/"}function LA(e){return e==="zhwiki"||e==="enwiki"?e:void 0}var lS,mS=l(()=>{"use strict";Mm();lS=["zhwiki","enwiki"]});var Zo,OA=l(()=>{"use strict";jt();H();oS();uS();Mm();_A();mS();Zo=class{#e;#t;#r;#n;#i;#a;#o;#s;constructor(t){this.#e=t.fetch??fetch,this.#t=t.language,this.#n=t.requestLog,this.#s=t.wiki,this.#o=t.userAgent,this.#i=Math.max(0,Math.floor(t.retryBaseDelayMs)),this.#a=Math.max(0,Math.floor(t.retryTimes)),this.#r=new Xo({concurrency:t.concurrency,minRequestIntervalMs:t.minRequestIntervalMs})}async getEntities(t){if(t.length===0)return new Map;let r=new URL("https://www.wikidata.org/w/api.php");r.searchParams.set("action","wbgetentities"),r.searchParams.set("ids",t.join("|")),r.searchParams.set("props","labels|descriptions|sitelinks"),r.searchParams.set("languages",AA(this.#t)),r.searchParams.set("sitefilter",lS.join("|")),r.searchParams.set("format","json"),r.searchParams.set("formatversion","2");let n=await this.#c(r),i=We(n.entities),a=new Map;for(let o of t){let s=We(i[o]),c=We(s.labels),d=We(s.descriptions);a.set(o,{...$d(c,this.#t)===void 0?{}:{label:$d(c,this.#t)},...$d(d,this.#t)===void 0?{}:{description:$d(d,this.#t)},qid:o,sitelinks:lS.flatMap(u=>{let p=Ud(s,["sitelinks",u,"title"]);return p===void 0?[]:[{title:p,wiki:u}]})})}return a}async getPagesByTitles(t,r=LA(this.#s)??"enwiki"){if(t.length===0)return new Map;let n=new URL(`${pS(r)}w/api.php`);n.searchParams.set("action","query"),n.searchParams.set("titles",t.join("|")),n.searchParams.set("prop","pageprops"),n.searchParams.set("ppprop","disambiguation|wikibase_item"),n.searchParams.set("format","json"),n.searchParams.set("formatversion","2");let i=await this.#c(n),a=IA(We(We(i.query).pages)),o=new Map;for(let s of a){let c=We(s),d=mn(c.title);d!==void 0&&o.set(d,{isDisambiguation:We(c.pageprops).disambiguation!==void 0,...Gd(c.pageid)===void 0?{}:{pageId:Gd(c.pageid)},title:d,wiki:r,...mn(We(c.pageprops).wikibase_item)===void 0?{}:{wikibaseItem:mn(We(c.pageprops).wikibase_item)}})}return o}async parseDisambiguationPage(t,r){let n=new URL(`${pS(r)}w/api.php`);n.searchParams.set("action","parse"),n.searchParams.set("page",t),n.searchParams.set("prop","text|properties"),n.searchParams.set("format","json"),n.searchParams.set("formatversion","2");let i=await this.#c(n),a=We(i.parse),o=vA(Ud(a,["text"]));return{linkedTitles:o.linkedTitles,...Gd(a.pageid)===void 0?{}:{pageId:Gd(a.pageid)},text:o.text,title:mn(a.title)??t,wiki:r}}async#c(t){let r;for(let n=0;n<=this.#a;n+=1)try{return await this.#r.use(async()=>await this.#l(t,n+1))}catch(i){if(r=i,!TA(i)||n>=this.#a)throw this.#n.warnFailed(),i;await PA(RA(i,n,this.#i))}throw r instanceof Error?r:new Error(String(r))}async#l(t,r){let n=Date.now(),i;try{i=await this.#e(t,this.#o===void 0?void 0:{headers:{"User-Agent":this.#o}})}catch(o){throw await this.#u({attempt:r,durationMs:Date.now()-n,error:o,startedAt:n,url:t}),o}let a=jm(i.headers.get("retry-after"));if(a!==void 0&&this.#r.blockFor(a),!i.ok){let o=CA(i)?await i.text():void 0;throw await this.#u({attempt:r,durationMs:Date.now()-n,response:i,...o===void 0?{}:{responseText:o},startedAt:n,url:t}),new Yo(t,i.status,a)}return await this.#u({attempt:r,durationMs:Date.now()-n,response:i,startedAt:n,url:t}),We(await i.json())}async#u(t){try{await this.#n.append(t)}catch(r){st({component:"wikipage"}).warn(`Failed to write wikipage fetch log entry: ${Ce(r)}`)}}}});var hS=l(()=>{"use strict";OA();uS();mS()});function KX(e,t,r,n){return{checkedAt:n,...t?.description===void 0?{}:{description:t.description},...t?.label===void 0?{}:{label:t.label},qid:e,sitelinks:t?.sitelinks.flatMap(i=>{let a=r.get(DA(i.wiki,i.title));return a===void 0?[]:[{isDisambiguation:a.isDisambiguation,...a.pageId===void 0?{}:{pageId:a.pageId},title:a.title,wiki:a.wiki}]})??[],updatedAt:n}}function JX(e){return{checkedAt:e.checkedAt,disambiguationQid:e.disambiguationQid,linkedQids:NA(e.pages),pages:e.pages,...e.profile===void 0?{}:{profile:e.profile}}}function NA(e){let t=new Map;for(let r of e)for(let n of r.linkedQids)t.set(n.qid,n);return[...t.values()]}function zX(e){let t=0;for(let r of e.values())t+=r.size;return t}function QX(e){return e.sitelinks.some(t=>t.isDisambiguation)}function VX(e,t){let r=e.find(n=>n.wiki===t)??e[0];return r===void 0?void 0:jA(r)}function jA(e){return{title:e.title,wiki:e.wiki}}function XX(e){return"close"in e&&typeof e.close=="function"}function DA(e,t){return`${e}:${t}`}function YX(e){return[...new Set(e.map(t=>t.trim().toUpperCase()).filter(t=>/^Q[1-9]\d*$/u.test(t)))]}function ZX(e){let t=e?.trim().toLowerCase();return t==="en"||t==="zh"?t:FX}function eY(e){return e===void 0||!Number.isFinite(e)?GX:Math.max(1,Math.floor(e))}function fS(e,t){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}var WX,FX,GX,UX,$X,BX,HX,qX,ri,MA=l(()=>{"use strict";tS();EA();Rt();hS();WX=3,FX="zh",GX=50,UX=100,$X=1e3,BX=3,HX="WikiGraph/0.3 (https://github.com/oomol-lab/wiki-graph)",qX=360*60*1e3,ri=class e{#e;#t;#r;#n;#i;#a;#o;#s;constructor(t){this.#e=t.client,this.#r=t.language,this.#t=t.maxBatchSize,this.#n=t.normalizer,this.#i=t.ownsStore??!1,this.#a=t.progress,this.#o=t.store,this.#s=t.wiki}static async open(t={}){let r=ZX(t.language),n=t.wiki??`${r}wiki`,i=await Qo.open(t.cacheDatabasePath);return new e({client:new Zo({concurrency:t.concurrency??WX,language:r,requestLog:kA(t.logDirPath),minRequestIntervalMs:t.minRequestIntervalMs??UX,retryBaseDelayMs:t.retryBaseDelayMs??$X,retryTimes:t.retryTimes??BX,userAgent:t.userAgent??HX,wiki:n,...t.fetch===void 0?{}:{fetch:t.fetch}}),language:r,maxBatchSize:eY(t.maxBatchSize),normalizer:t.normalizer,ownsStore:!0,progress:t.progress,store:i,wiki:n})}async close(){this.#i&&XX(this.#o)&&await this.#o.close()}async resolveQids(t){let r=YX(t),n=await this.#c(r),i=await this.#u([...n.values()].filter(QX));return r.map(a=>{let o=n.get(a);if(o===void 0)return{isDisambiguation:!1,qid:a};let s=i.get(a),c=s?.pages??[],d=VX(o.sitelinks,this.#s);return{...o.description===void 0?{}:{description:o.description},...s===void 0?{}:{disambiguation:JX(s)},...c.length===0?{}:{disambiguationPages:c},isDisambiguation:c.length>0,...o.label===void 0?{}:{label:o.label},qid:a,...d===void 0?{}:{sitelink:d},...o.sitelinks.length===0?{}:{sitelinks:o.sitelinks.map(jA)}}})}async#c(t){let r=new Map(await this.#o.getQids(t,this.#r)),n=t.filter(o=>!r.has(o)),i=r.size,a=0;await this.#p("qid",i,t.length);for(let o of fS(n,this.#t)){let s=await this.#e.getEntities(o);a+=o.length,await this.#p("entity",a,n.length);let c=await this.#l(s),d=new Date().toISOString(),u=o.map(p=>KX(p,s.get(p),c,d));await this.#o.putQids(u,this.#r);for(let p of u)r.set(p.qid,p);i+=o.length,await this.#p("qid",i,t.length)}return r}async#l(t){let r=new Map,n=new Map;for(let i of t.values())for(let a of i.sitelinks){let o=n.get(a.wiki)??new Set;o.add(a.title),n.set(a.wiki,o)}for(let[i,a]of n)for(let o of fS([...a],this.#t)){for(let[s,c]of await this.#e.getPagesByTitles(o,i))r.set(DA(i,s),c);await this.#p("page",r.size,zX(n))}return r}async#u(t){let r=t.map(o=>o.qid),n=new Map(await this.#o.getDisambiguations(r,this.#s)),i=t.filter(o=>!n.has(o.qid)),a=n.size;await this.#p("disambiguation-page",a,r.length);for(let o of i){let s=await this.#d(o);await this.#o.putDisambiguations([s],this.#s),n.set(o.qid,s),a+=1,await this.#p("disambiguation-page",a,r.length)}return n}async#d(t){let r=[];for(let a of t.sitelinks.filter(o=>o.isDisambiguation)){let o=await this.#e.parseDisambiguationPage(a.title,a.wiki),s=new Map;for(let u of fS(o.linkedTitles,this.#t)){for(let[p,m]of await this.#e.getPagesByTitles(u,a.wiki))s.set(p,m);await this.#p("linked-page",s.size,o.linkedTitles.length)}let c=new Map,d=[];for(let u of o.linkedTitles){let p=s.get(u)?.wikibaseItem;p!==void 0&&(c.set(u,p),d.push({qid:p,title:u}))}r.push({linkedQids:d,...o.pageId===void 0?{}:{pageId:o.pageId},text:xA(o.text,c),title:o.title,wiki:o.wiki})}let n=NA(r),i=await this.#m(t,r,n);return{checkedAt:new Date().toISOString(),disambiguationQid:t.qid,pages:r,...i}}async#m(t,r,n){if(this.#n===void 0||n.length===0)return{};try{return{profile:await this.#n({pageQidLinks:n,pages:r,sourceQid:t.qid,...t.label===void 0?{}:{surface:t.label}})}}catch(i){if(!(i instanceof mt))throw i;let a=new Date,o=new Date(a.getTime()+qX);return{profileError:{failedAt:a.toISOString(),message:i.message,retryAfter:o.toISOString()}}}}async#p(t,r,n){await this.#a?.({detail:t,done:Math.min(Math.max(0,r),Math.max(0,n)),total:Math.max(0,n)})}}});var Bd=l(()=>{"use strict";tS();SA();oS();MA();hS()});async function gS(e,t){let[r,n]=await Promise.all([WA(e,"en","enwiki",t),WA(e,"zh","zhwiki",t)]);return{en:r,zh:n}}async function WA(e,t,r,n){let i=await ri.open({...n.wikipageResolverOptions,language:t,wiki:r});try{let[a]=await i.resolveQids([e]);return a===void 0?null:tY(a,r)}finally{await i.close()}}function tY(e,t){let r=e.sitelinks?.find(n=>n.wiki===t)??(e.sitelink?.wiki===t?e.sitelink:void 0);return r===void 0?null:{...e.description===void 0?{}:{description:e.description},title:r.title,url:rY(r)}}function rY(e){return`https://${e.wiki==="zhwiki"?"zh":"en"}.wikipedia.org/wiki/${encodeURIComponent(e.title.replaceAll(" ","_"))}`}var yS=l(()=>{"use strict";Bd()});async function _t(e,t,r={}){if(gr(t))return await nY(e,t,r);let n=yr(t);if(n.type!=="node")return es(r.role,t),Od([],r);es(r.role,t);let i=await e.serials.listDocumentOrders(),{chapterId:a}=await kr(e,n.id),o=Xw(await Ho(e,a,n.id),i,r.order??"doc-asc").map(s=>({id:he(s.node.id),label:s.node.label,summary:s.node.content,type:"node"}));return await zo(e,await Yw(e,o,r.query),r)}async function nY(e,t,r){let n=Tt(t);switch(n.type){case"chapter":throw new Error(`Related is not available for scope URI: ${t}`);case"chapter-title":return es(r.role,t),Od([],r);case"chunk":{es(r.role,t);let{chapterId:i}=await kr(e,n.id);if(n.chapterId!==void 0&&n.chapterId!==i)throw new Error(`Chunk ${t} was not found in this archive.`);let a=Xw(await Ho(e,i,n.id),await e.serials.listDocumentOrders(),r.order??"doc-asc").map(o=>({id:he(o.node.id),label:o.node.label,summary:o.node.content,type:"node"}));return await zo(e,await Yw(e,a,r.query),r)}case"text-stream":{FA(r.query,t),es(r.role,t);let i=await Zn(e,n.chapterId);return await zo(e,[{id:Ct(n.chapterId),label:i.title??`[chapter ${n.chapterId}]`,summary:`${i.stage}; ${i.fragmentCount} fragments`,type:"chapter"}],r)}case"entity":return await Q_(e,n,r);case"triple":throw new Error(`Related is only available for chunk and entity objects: ${t}`);case"chapter-tree":case"entity-wikipage":case"meta":case"chapter-state":return FA(r.query,t),es(r.role,t),Od([],r)}}function es(e,t){if(e!==void 0&&e!=="any")throw new Error(`--role is only available for entity related: ${t}`)}function FA(e,t){if(e!==void 0)throw new Error(`Related query is only available for chunk and entity: ${t}`)}var GA=l(()=>{"use strict";Id();oe();Er();Pm();Zw();Am();Nd();yS();De()});var Wm=l(()=>{"use strict";GA();Zw();Pm();Am();Nd();yS()});async function ht(e,t,r={}){if(gr(t)){let i=t;return await oY(e,await iY(e,i),r,i)}let n=yr(t);switch(n.type){case"chapter":throw new Error(`Chapter ${Ct(n.id)} is a scope URI, not a readable object. Use wikg://chapter/${n.id}/title or wikg://chapter/${n.id}/state.`);case"chapter-title":{let i=await Zn(e,n.id);return{id:Cw(n.id),title:i.title??`[chapter ${n.id}]`,type:"chapter-title"}}case"fragment":{let[i,a,o]=await Promise.all([um(e,n.serialId,n.fragmentId),E_(e,n.serialId,n.fragmentId),e.getSerialFragments(n.serialId).listFragmentIds()]),s=o.indexOf(n.fragmentId),c=s>0?o[s-1]:void 0,d=s>=0&&s<o.length-1?o[s+1]:void 0;return{fragment:i,id:i.id,nextFragmentId:d===void 0?void 0:wd(n.serialId,d),nodes:a,previousFragmentId:c===void 0?void 0:wd(n.serialId,c),title:i.id,type:"fragment"}}case"meta":return{...BP(await e.readBookMeta()),id:Oo,type:"meta"};case"node":{let{chapterId:i,node:a}=await kr(e,n.id),[o,s]=await Promise.all([Ho(e,i,n.id),k_(e,a)]),c=o.filter(u=>u.direction==="outgoing"),d=o.filter(u=>u.direction==="incoming");return{generatedNodeSummary:a.content,id:he(a.id),incoming:d,neighbors:o,outgoing:c,position:lt(a.sentenceIds),sourceFragments:s,title:a.label,type:"node"}}case"summary":{let i=await Zn(e,n.id),a=await f_(e,n.id,"summary");if(a.trim()==="")throw new Error(`Summary ${aa(n.id)} is missing.`);return{content:a,id:aa(n.id),title:i.title??`[chapter ${n.id}]`,type:"summary"}}}}async function iY(e,t){let r="wikg://chapter/",n=dt(t),i=`wikg://${n.path.join("/")}`,a=aY(n.fragment);if(!i.startsWith(r)||i==="wikg://chapter/tree")return t;let o=i.slice(r.length),s=(await z(e)).slice().sort((u,p)=>p.path.length-u.path.length).find(u=>o===u.path||o.startsWith(`${u.path}/`));if(s===void 0)return t;let c=o.slice(s.path.length),d=`${r}${s.chapterId}${c}`;return a===void 0?d:`${d}#${a}`}function aY(e){if(e!==void 0)return typeof e=="number"?String(e):`${e.begin}..${e.end}`}async function oY(e,t,r={},n=t){t=t,n=n;let i=Tt(t);switch(i.type){case"meta":return await ht(e,Oo,r);case"chapter":throw new Error(`${n} is a scope URI, not a readable object. Use ${n}/title or ${n}/state.`);case"chapter-title":return await ht(e,Cw(i.chapterId),r);case"chapter-state":{let a=await Zn(e,i.chapterId),o=await Mw(e,a);return{id:i.target===void 0?n:`${n.replace(/\/state(?:\/.*)?$/u,"")}/state/${i.target}`,...i.target===void 0?{state:o}:{target:i.target,value:o[i.target]},type:"state"}}case"chapter-tree":return{id:"chapter-tree",title:"Chapter tree",tree:await Vn(e),type:"chapter-tree"};case"entity":{let a=r.evidenceLimit??3,o=i.chapterId===void 0?{}:{chapterId:i.chapterId},[s,c,d]=await Promise.all([e.mentions.countByQid(i.qid,o),e.mentions.listByQid(i.qid,{...o,limit:a,order:r.order==="doc-desc"?"desc":"asc"}),e.mentions.listLabelsByQid(i.qid,o)]);if(s===0)throw new Error(`Entity ${t} was not found in this archive.`);return{evidence:await ym(e,c,a,ee(),r.sourceContext??le,s),id:n,label:d[0]??i.qid,labels:d,mentionCount:s,qid:i.qid,type:"entity"}}case"entity-wikipage":return{...await gS(i.qid,r),id:n,type:"entity-wikipage"};case"triple":{let a=r.evidenceLimit??3,o={...i.chapterId===void 0?{}:{chapterId:i.chapterId},objectQid:i.objectQid,predicate:i.predicate,subjectQid:i.subjectQid},[s,c,d]=await Promise.all([e.mentionLinks.countByTriple(o),e.mentionLinks.listByTriple({...o,limit:a,order:r.order==="doc-desc"?"desc":"asc"}),d_(e,i)]);if(s===0)throw new Error(`Triple ${t} was not found in this archive.`);return{evidence:await bm(e,c,a,ee(),r.sourceContext??le,s),id:n,label:d,objectQid:i.objectQid,predicate:i.predicate,subjectQid:i.subjectQid,type:"triple"}}case"chunk":{if(i.chapterId!==void 0){let{chapterId:a}=await kr(e,i.id);if(a!==i.chapterId)throw new Error(`Chunk ${t} was not found in this archive.`)}return await ht(e,he(i.id),r)}case"text-stream":{let a=await h_(e,i);return{...r.backlinks===!0?{backlinks:await Jw(e,i)}:{},fragment:{...a,id:n},id:n,nextFragmentId:void 0,nodes:[],previousFragmentId:void 0,title:n,type:"fragment"}}}}var bS=l(()=>{"use strict";Dn();Ze();Id();oe();Er();Rd();_d();Kt();Wm();De();wr()});async function ni(e,t,r){sY(t);let[n,i]=await Promise.all([ht(e,t,{evidenceLimit:3}),_t(e,t,{evidenceLimit:3})]);return{anchor:n,budget:r,related:i.items}}function sY(e){let t;try{t=Tt(e)}catch(r){throw/^wikg:\/\/chapter\/.+\/(?:source|summary|title|state)(?:[#/]|$)/u.test(e)?new Error(`Pack is only available for chunk and entity objects: ${e}`):r}switch(t.type){case"chunk":case"entity":return;case"chapter":case"chapter-title":case"chapter-state":case"chapter-tree":case"entity-wikipage":case"meta":case"text-stream":throw new Error(`Pack is only available for chunk and entity objects: ${e}`)}}var UA=l(()=>{"use strict";bS();Wm();De()});var vS=l(()=>{"use strict";B_();bS();Wm();yd();De();qw();Pd();UA()});var $A=l(()=>{"use strict";vS()});import{randomBytes as uY}from"crypto";import{join as lY}from"path";async function HA(e){for(let t=0;t<10;t+=1){let r=`c_${uY(6).toString("base64url")}`;if(await e.queryOne("SELECT cursor_id FROM continuation_cursors WHERE cursor_id = ?",[r],i=>Te(i,"cursor_id"))===void 0)return r}throw new Error("Failed to create a unique continuation cursor.")}async function CS(e){await e.run("DELETE FROM continuation_cursors WHERE expires_at < ?",[Date.now()])}async function TS(){return await V(lY(mY(),"continuation-cursors.sqlite"),pY)}function mY(){return Rn()}function qA(e){if(e==="json"||e==="jsonl"||e==="text")return e;throw new Error(`Invalid continuation cursor format: ${e}.`)}var pY,BA,KA=l(()=>{"use strict";Ue();M();X();pY=`
|
|
2236
|
+
CREATE TABLE IF NOT EXISTS continuation_cursors (
|
|
2237
|
+
cursor_id TEXT PRIMARY KEY,
|
|
2238
|
+
archive_key TEXT NOT NULL,
|
|
2239
|
+
archive_path TEXT NOT NULL,
|
|
2240
|
+
kind TEXT NOT NULL,
|
|
2241
|
+
payload_json TEXT NOT NULL,
|
|
2242
|
+
format TEXT NOT NULL,
|
|
2243
|
+
limit_value INTEGER NOT NULL,
|
|
2244
|
+
created_at INTEGER NOT NULL,
|
|
2245
|
+
expires_at INTEGER NOT NULL,
|
|
2246
|
+
accessed_at INTEGER NOT NULL
|
|
2247
|
+
);
|
|
2248
|
+
|
|
2249
|
+
CREATE INDEX IF NOT EXISTS idx_continuation_cursors_expires
|
|
2250
|
+
ON continuation_cursors(expires_at);
|
|
2251
|
+
`,BA=10080*60*1e3});function VA(e){switch(e.kind){case"collection":return{...e.backlinks===void 0?{}:{backlinks:e.backlinks},chapters:e.chapters,cursor:e.cursor,...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},ids:e.ids,order:e.order,...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},indexScope:e.indexScope,types:e.types};case"search":return{...e.backlinks===void 0?{}:{backlinks:e.backlinks},chapters:e.chapters,cursor:e.cursor,...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},...e.query===void 0?{}:{query:e.query},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},indexScope:e.indexScope,types:e.types};case"evidence":return{cursor:e.cursor,order:e.order,...e.query===void 0?{}:{query:e.query},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},indexScope:e.indexScope,targetUri:e.targetUri};case"related":return{cursor:e.cursor,...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},order:e.order,...e.query===void 0?{}:{query:e.query},...e.role===void 0?{}:{role:e.role},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},indexScope:e.indexScope,targetUri:e.targetUri}}}function XA(e){let t=fY(e.payloadJSON),r=hY(t,e);if(e.kind==="collection")return{archiveKey:e.archiveKey,archivePath:e.archivePath,...zA(t,"backlinks"),chapters:JA(t,"chapters"),cursor:ii(t,"cursor"),...AS(t,"evidenceLimit"),format:e.format,ids:RS(t,"ids"),indexScope:r,kind:"collection",order:_S(t),...Fm(t,"sourceContext","sourceContext"),...QA(t),types:RS(t,"types")};if(e.kind==="search")return{archiveKey:e.archiveKey,archivePath:e.archivePath,...zA(t,"backlinks"),chapters:JA(t,"chapters"),cursor:ii(t,"cursor"),...AS(t,"evidenceLimit"),format:e.format,indexScope:r,kind:"search",...PS(t,"query"),...Fm(t,"sourceContext","sourceContext"),...QA(t),types:RS(t,"types")};if(e.kind==="evidence")return{archiveKey:e.archiveKey,archivePath:e.archivePath,cursor:ii(t,"cursor"),format:e.format,indexScope:r,kind:"evidence",order:_S(t),...PS(t,"query"),...Fm(t,"sourceContext","sourceContext"),targetUri:ii(t,"targetUri")};if(e.kind==="related")return{archiveKey:e.archiveKey,archivePath:e.archivePath,cursor:ii(t,"cursor"),...AS(t,"evidenceLimit"),format:e.format,indexScope:r,kind:"related",order:_S(t),...PS(t,"query"),...gY(t),...Fm(t,"sourceContext","sourceContext"),targetUri:ii(t,"targetUri")};throw new Error(`Invalid continuation cursor kind: ${e.kind}.`)}function hY(e,t){let r=e.indexScope;if(r===void 0)return{archiveKey:t.archiveKey,archivePath:t.archivePath,kind:"archive-index"};if(typeof r!="object"||r===null||Array.isArray(r))throw new Error("Invalid continuation cursor payload.");let n=r;if(n.kind==="archive-index"){let i=ii(n,"archiveKey"),a=ii(n,"archivePath");if(i!==t.archiveKey||a!==t.archivePath)throw new Error("Invalid continuation cursor payload.");return{archiveKey:i,archivePath:a,kind:"archive-index"}}if(n.kind==="library-index"){let i=n.libraryId;if(typeof i=="number"&&Number.isInteger(i))return{kind:"library-index",libraryId:i}}throw new Error("Invalid continuation cursor payload.")}function fY(e){let t=JSON.parse(e);if(typeof t=="object"&&t!==null&&!Array.isArray(t))return t;throw new Error("Invalid continuation cursor payload.")}function ii(e,t){let r=e[t];if(typeof r=="string")return r;throw new Error("Invalid continuation cursor payload.")}function RS(e,t){let r=e[t];if(r===null)return null;if(Array.isArray(r)&&r.every(n=>typeof n=="string"))return r;throw new Error("Invalid continuation cursor payload.")}function PS(e,t){let r=e[t];if(r===void 0)return{};if(typeof r=="string")return{query:r};throw new Error("Invalid continuation cursor payload.")}function JA(e,t){let r=e[t];if(r==null)return null;if(Array.isArray(r)){let n=[];for(let i of r){if(typeof i!="number"||!Number.isInteger(i))throw new Error("Invalid continuation cursor payload.");n.push(i)}return n}throw new Error("Invalid continuation cursor payload.")}function _S(e){let t=e.order;if(t==="doc-asc"||t==="doc-desc")return t;throw new Error("Invalid continuation cursor payload.")}function AS(e,t){let r=e[t];if(r===void 0)return{};if(typeof r=="number"&&Number.isInteger(r)&&r>0)return{evidenceLimit:r};throw new Error("Invalid continuation cursor payload.")}function Fm(e,t,r){let n=e[t];if(n===void 0)return{};if(typeof n=="number"&&Number.isInteger(n)&&n>=0)return{[r]:n};throw new Error("Invalid continuation cursor payload.")}function zA(e,t){let r=e[t];if(r===void 0)return{};if(typeof r=="boolean")return{backlinks:r};throw new Error("Invalid continuation cursor payload.")}function gY(e){let t=e.role;if(t===void 0)return{};if(t==="any"||t==="object"||t==="self"||t==="subject")return{role:t};throw new Error("Invalid continuation cursor payload.")}function QA(e){let t=e.triplePattern;if(t===void 0)return{};if(typeof t!="object"||t===null||Array.isArray(t))throw new Error("Invalid continuation cursor payload.");let r=t,n=LS(r,"subjectQid"),i=LS(r,"predicate"),a=LS(r,"objectQid");return{triplePattern:{...a===void 0?{}:{objectQid:a},...i===void 0?{}:{predicate:i},...n===void 0?{}:{subjectQid:n}}}}function LS(e,t){let r=e[t];if(r!==void 0){if(typeof r=="string")return r;throw new Error("Invalid continuation cursor payload.")}}var YA=l(()=>{"use strict"});async function Hd(e){let t=await TS();try{await CS(t);let r=Date.now(),n=await HA(t);return await t.run(`
|
|
2252
|
+
INSERT INTO continuation_cursors (
|
|
2253
|
+
cursor_id, archive_key, archive_path, kind, payload_json, format,
|
|
2254
|
+
limit_value, created_at, expires_at, accessed_at
|
|
2255
|
+
)
|
|
2256
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2257
|
+
`,[n,e.archiveKey,e.archivePath,e.kind,JSON.stringify(VA(e)),e.format,1,r,r+BA,r]),n}finally{await t.close()}}async function qd(e){let t=await TS();try{await CS(t);let r=await t.queryOne(`
|
|
2258
|
+
SELECT archive_key, archive_path, kind, payload_json, format
|
|
2259
|
+
FROM continuation_cursors
|
|
2260
|
+
WHERE cursor_id = ?
|
|
2261
|
+
`,[e],n=>({archiveKey:k(n,"archive_key"),archivePath:k(n,"archive_path"),format:qA(k(n,"format")),kind:k(n,"kind"),payloadJSON:k(n,"payload_json")}));if(r===void 0)throw new Error(`Continuation cursor ${e} was not found or has expired.`);return await t.run("UPDATE continuation_cursors SET accessed_at = ? WHERE cursor_id = ?",[Date.now(),e]),XA(r)}finally{await t.close()}}var ZA=l(()=>{"use strict";M();KA();YA()});var eL=l(()=>{"use strict"});var tL=l(()=>{"use strict";ZA();eL()});var ts=l(()=>{"use strict";$A();tL();Yi()});function OS(e,t){return new Gm(e,t)}function yY(e){switch(e.type){case"serials-discovered":return{available:Number(e.available),serials:e.serials.length,totalFragments:e.serials.reduce((t,r)=>t+(r.fragments??0),0),totalWords:e.serials.reduce((t,r)=>t+r.words,0)};case"serial-progress":return{completedFragments:e.completedFragments,completedWords:e.completedWords,id:e.id};case"digest-progress":return{completedWords:e.completedWords,totalWords:e.totalWords}}}function bY(e){switch(e.type){case"serials-discovered":return e.available?`Discovered ${e.serials.length} serials`:"Serial discovery unavailable";case"serial-progress":return`Serial ${e.id} progressed`;case"digest-progress":return"Digest progressed"}}var Gm,NS=l(()=>{"use strict";jt();Gm=class{#e;#t;constructor(t,r){this.#e=r,this.#t=t}async emit(t){if(st({component:"progress",eventType:t.type,operation:this.#t,...yY(t)}).info(bY(t)),this.#e!==void 0)try{await this.#e(t)}catch(r){st({component:"progress",operation:this.#t}).warn({error:r instanceof Error?{message:r.message,name:r.name}:String(r)},"Progress callback failed")}}}});function Bm(e){return new Um(e)}var Um,$m,rL=l(()=>{"use strict";NS();Um=class{#e;#t=0;#r=new Map;#n=0;#i=!1;constructor(t){this.#e=OS(t.operation,t.onProgress)}createSerialTracker(t){return new $m(this,t.id)}async discoverSerials(t){this.#o(),this.#i=!0,this.#n=t.reduce((r,n)=>r+n.words,0),await this.#e.emit({available:!0,serials:t,type:"serials-discovered"}),await this.#a()}async markDiscoveryUnavailable(){this.#o(),this.#i=!0,await this.#e.emit({available:!1,serials:[],type:"serials-discovered"})}async completeSerial(t){let r=this.#r.get(t.id)??0;t.words!==r&&(this.#r.set(t.id,t.words),this.#t+=t.words-r);let n=Math.max(this.#n,this.#t,t.words);n===this.#n&&t.words===r||(this.#n=n,await this.#a())}async emitSerialProgress(t){await this.#e.emit({completedFragments:t.completedFragments,completedWords:t.completedWords,id:t.id,type:"serial-progress"})}async#a(){await this.#e.emit({completedWords:this.#t,totalWords:this.#n,type:"digest-progress"})}#o(){if(this.#i)throw new Error("Serial discovery has already been reported")}},$m=class{#e=0;#t;#r=0;#n;constructor(t,r){this.#t=t,this.#n=r}async begin(t){}async advance(t){this.#r+=t,this.#e+=1,await this.#t.emitSerialProgress({completedFragments:this.#e,completedWords:this.#r,id:this.#n})}async complete(t=0){this.#r+=t,t>0&&(this.#e+=1),await this.#t.emitSerialProgress({completedFragments:this.#e,completedWords:this.#r,id:this.#n}),await this.#t.completeSerial({id:this.#n,words:this.#r})}}});var nL=l(()=>{"use strict";rL();NS()});function iL(e,t){return e-t}function aL(e){let t=Kd();for(let r of e)t[String(r)]=!0;return t}function jS(e,t){return Object.hasOwn(e,String(t))}function wY(e){return Object.keys(e).length}function Kd(){return Object.create(null)}var Hm,oL=l(()=>{"use strict";Hm=class{#e;constructor(t){this.#e=t}selectTopChunks(t){if(t.allChunks.length===0)return[];if(t.latestChunkIds.length===0)return[...t.allChunks].slice(0,t.capacity);let r=aL(t.latestChunkIds),n=t.allChunks.filter(o=>!jS(r,o.id));if(n.length===0)return[];let i=this.#t({getSuccessorChunkIds:t.getSuccessorChunkIds,latestChunkIds:t.latestChunkIds});if(wY(i)===0)return[...n].sort((o,s)=>s.generation-o.generation).slice(0,t.capacity);let a=this.#r({allChunkIds:t.allChunks.map(o=>o.id),forwardScores:i,getSuccessorChunkIds:t.getSuccessorChunkIds});return n.map(o=>({chunk:o,decayedScore:(a[String(o.id)]??0)*this.#e**o.generation})).sort((o,s)=>o.decayedScore!==s.decayedScore?s.decayedScore-o.decayedScore:o.chunk.id-s.chunk.id).slice(0,t.capacity).map(o=>o.chunk)}#t(t){let r=t.latestChunkIds.length;if(r===0)return Kd();let n=1/r,i=Kd(),a=Kd(),o=[...t.latestChunkIds],s=0;for(let c of t.latestChunkIds)i[String(c)]=n,a[String(c)]=!0;for(;s<o.length;){let c=o[s];if(s+=1,c===void 0)continue;let d=i[String(c)];if(d===void 0)continue;let u=[...t.getSuccessorChunkIds(c)].sort(iL);if(u.length===0)continue;let p=d/u.length;for(let m of u){let f=String(m);i[f]=(i[f]??0)+p,!jS(a,m)&&(a[f]=!0,o.push(m))}}return i}#r(t){let r=Kd();for(let s of t.allChunkIds)for(let c of t.getSuccessorChunkIds(s)){let d=String(c),u=r[d]??[];r[d]=[...u,s]}let n={...t.forwardScores},i=Object.keys(t.forwardScores).map(Number),a=aL(i),o=0;for(;o<i.length;){let s=i[o];if(o+=1,s===void 0)continue;let c=n[String(s)];if(c===void 0)continue;let d=[...r[String(s)]??[]].sort(iL);if(d.length===0)continue;let u=c/d.length;for(let p of d){let m=String(p);n[m]=(n[m]??0)+u,!jS(a,p)&&(a[m]=!0,i.push(p))}}return n}}});var qm,sL=l(()=>{"use strict";qm=class{#e;#t=[];#r=[];#n=[];#i=0;constructor(t){this.#e=t}get capacity(){return this.#e}get generation(){return this.#i}addChunks(t){this.#t.push(...t)}setRetainedChunks(t){this.#r.splice(0,this.#r.length,...t.previousFragmentChunks),this.#n.splice(0,this.#n.length,...t.extraChunks)}finalizeFragment(){let t=[...this.#t];return this.#t.splice(0,this.#t.length),this.#i+=1,t}getChunks(){return[...this.#t,...this.#r,...this.#n]}getAllChunksForSaving(){return[...this.#t]}getChunksForPrompt(t=!0){return t?this.getChunks():[...this.#r,...this.#n]}formatForPrompt(t=!0){let r=this.getChunksForPrompt(t);return r.length===0?"(empty)":r.map(n=>`${n.id}. [${n.label}] - ${n.content}`).join(`
|
|
2262
|
+
`)}clear(){this.#t.splice(0,this.#t.length),this.#r.splice(0,this.#r.length),this.#n.splice(0,this.#n.length)}}});async function SY(e){let t=Object.create(null);for(let[i,a]of e.chunkBatch.chunks.entries()){a.id=await e.idGenerator(),a.generation=e.generation;let o=e.chunkBatch.tempIds[i];o===void 0||o===""||(t[o]=a)}let r=[...e.visibleChunks,...e.chunkBatch.chunks],n=[];for(let i of e.chunkBatch.links){let a=cL(i.from,t,r),o=cL(i.to,t,r);if(a===void 0||o===void 0)continue;let[s,c]=a.id>o.id?[a.id,o.id]:[o.id,a.id];if(kY(r,c,s),i.strength===void 0){n.push({fromId:s,toId:c});continue}n.push({fromId:s,strength:i.strength,toId:c})}return{chunks:e.chunkBatch.chunks,edges:n,...e.chunkBatch.importanceAnnotations===void 0?{}:{importanceAnnotations:e.chunkBatch.importanceAnnotations}}}function cL(e,t,r){return typeof e=="string"?t[e]:r.find(n=>n.id===e)}function kY(e,t,r){for(let n of e)if(n.id===t){n.links.includes(r)||n.links.push(r);return}}var Km,dL=l(()=>{"use strict";oL();sL();Km=class{#e;#t;#r;constructor(t,r,n){this.#e=n,this.#t=new Hm(r),this.#r=new qm(t)}get capacity(){return this.#r.capacity}createChunkBatchContext(t){return{visibleChunkIds:this.#r.getChunksForPrompt(t?.includeCurrentFragment??!0).map(n=>n.id),workingMemoryPrompt:this.#r.formatForPrompt(t?.includeCurrentFragment??!0)}}async acceptChunkBatch(t){let r=await SY({chunkBatch:t,generation:this.#r.generation,idGenerator:this.#e,visibleChunks:this.#r.getChunks()});return this.#r.addChunks(r.chunks),r}completeFragment(t){let r=this.#r.getAllChunksForSaving(),n=r.map(a=>a.id),i=this.#t.selectTopChunks({allChunks:t.allChunks,capacity:this.#r.capacity,getSuccessorChunkIds:t.getSuccessorChunkIds,latestChunkIds:n});this.#r.setRetainedChunks({extraChunks:i,previousFragmentChunks:r}),this.#r.finalizeFragment()}clear(){this.#r.clear()}}});function zd(e){let t=e.trim();return t===""?[]:Array.from(IY.segment(t)).map(r=>r.segment.trim()).filter(r=>r!=="")}function Vt(e){let t=e.replace(/\s+/gu," ").trim(),r=xY(t),n="";for(let i of r)if(!vY(i)){if(!Jd(i)){n+=i;continue}n+=i.toLowerCase().normalize("NFD").replace(new RegExp("\\p{M}","gu"),"")}return n}function xY(e){let t=[],r=0;for(;r<e.length;){let n=e[r];if(n===void 0)break;if(n!==" "){t.push(n),r+=1;continue}if(t.length>=2&&EY.has(t[t.length-1]??"")&&Jd(t[t.length-2]??"")&&r<e.length-1&&Jd(e[r+1]??"")){t.pop(),r+=1;continue}let i=t[t.length-1]??"",a=e[r+1]??"";i!==""&&a!==""&&Jd(i)&&Jd(a)&&t.push(n),r+=1}return t}function Jd(e){return e!==""&&new RegExp("\\p{Script=Latin}","u").test(e)&&new RegExp("\\p{L}","u").test(e)}function vY(e){return/[\p{P}\p{S}]/u.test(e)}var EY,IY,Qd=l(()=>{"use strict";EY=new Set(["\u2010","\u2011","\u2012","\u2013","\u2014","\u2015"]),IY=new Intl.Segmenter(void 0,{granularity:"sentence"})});function MS(){return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:-1,matchStart:-1,score:0}}function lL(e,t){let r=Jm(e,2),n=Jm(t,2),i=Jm(e,3),a=Jm(t,3);return(uL(r,n)+uL(i,a))/2}function Jm(e,t){if(e.length<=t)return e===""?new Set:new Set([e]);let r=new Set;for(let n=0;n<=e.length-t;n+=1)r.add(e.slice(n,n+t));return r}function uL(e,t){if(e.size===0||t.size===0)return 0;let r=0;for(let n of e)t.has(n)&&(r+=1);return 2*r/(e.size+t.size)}function pL(e,t){if(e===t)return 1;if(e===""||t==="")return 0;let r=Array.from({length:t.length+1},(a,o)=>o),n=new Array(t.length+1).fill(0);for(let[a,o]of[...e].entries()){n[0]=a+1;for(let[s,c]of[...t].entries()){let d=(n[s]??0)+1,u=(r[s+1]??0)+1,p=(r[s]??0)+(o===c?0:1);n[s+1]=Math.min(d,u,p)}[r,n]=[n,r]}return 1-(r[t.length]??Math.max(e.length,t.length))/Math.max(e.length,t.length)}function mL(e,t){return 1-Math.abs(e.length-t.length)/Math.max(e.length,t.length)}function hL(e,t){if(e===t)return 1;let r=e.length,n=t.length;if(r===0||n===0)return 0;let i=Array.from({length:r+1},()=>new Array(n+1).fill(0)),a=0;for(let o=1;o<=r;o+=1)for(let s=1;s<=n;s+=1){if(e[o-1]!==t[s-1])continue;let c=i[o];c!==void 0&&(c[s]=(i[o-1]?.[s-1]??0)+1,a=Math.max(a,c[s]??0))}return 2*a/(r+n)}function fL(e){return` - ${e.occurrenceId} score=${e.score.toFixed(3)}
|
|
2263
|
+
prev: ${DS(e.prevText)}
|
|
2264
|
+
text: ${DS(e.text,120)}
|
|
2265
|
+
next: ${DS(e.nextText)}`}function DS(e,t=80){let r=e.replace(/\s+/gu," ").trim();return r===""?"(none)":r.length<=t?r:`${r.slice(0,t-3)}...`}var WS=l(()=>{"use strict"});var FS=l(()=>{"use strict"});function yL(e,t,r){return e.mode==="head_tail"?RY(e,t):GS(e.text??"",t,r)}function RY(e,t){let r=e.head??"",n=e.tail??"",i=Vt(t),a=Vt(r),o=Vt(n);if(a===""||o==="")return MS();let s=i.indexOf(a),c=i.lastIndexOf(o);if(s!==-1&&c!==-1&&s<=c){let m=a.length+o.length,f=Math.min(1,m/Math.max(1,i.length));return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:c+o.length,matchStart:s,score:.82+.18*f}}let d=GS(r,t,"start_anchor"),u=GS(n,t,"end_anchor"),p=d.matchStart<=u.matchStart?1:.85;return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:Math.max(d.matchEnd,u.matchEnd),matchStart:Math.min(d.matchStart,u.matchStart),score:(d.score+u.score)/2*p}}function GS(e,t,r){let n=e.trim(),i=t.trim(),a=Vt(n),o=Vt(i);if(a===""||o==="")return MS();if(n===i)return{exactNormalized:!0,exactRaw:!0,exactSubstring:!0,matchEnd:o.length,matchStart:0,score:1};if(a===o)return{exactNormalized:!0,exactRaw:!1,exactSubstring:!1,matchEnd:o.length,matchStart:0,score:.995};if(o.includes(a)){let f=o.indexOf(a),w=a.length/Math.max(1,o.length),b=gL({candidateNormalized:o,label:r,matchEnd:f+a.length,matchStart:f,queryNormalized:a});return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:f+a.length,matchStart:f,score:Math.min(1,.75+.25*w+b)}}if(a.includes(o)){let f=o.length/Math.max(1,a.length);return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:o.length,matchStart:0,score:Math.min(.92,.78+.14*f)}}let s=0,c=-1,d=-1,u=Math.min(o.length,Math.max(a.length,Math.floor(a.length*1.2))),p=Math.min(u,Math.max(1,Math.floor(a.length*.8))),m=new Set(a);for(let f=p;f<=u;f+=1)for(let w=0;w<=o.length-f;w+=1){let b=o.slice(w,w+f),E=new Set(b),N=new Set([...m,...E]).size,C=[...m].filter(x=>E.has(x)).length;if((N===0?0:C/N)<.35)continue;let Q=.35*lL(a,b)+.35*hL(a,b)+.2*pL(a,b)+.1*mL(a,b)+gL({candidateNormalized:o,label:r,matchEnd:w+f,matchStart:w,queryNormalized:a});Q>s&&(s=Math.min(1,Q),c=w,d=w+f)}return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:d,matchStart:c,score:s}}function gL(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 bL=l(()=>{"use strict";Qd();WS();FS()});function wL(e){if(e==null)return[void 0,"anchor is required"];if(typeof e=="string"){let a=e.trim();if(a==="")return[void 0,"anchor string is empty"];if(a.includes("...")){let o=a.split("...").map(s=>s.trim()).filter(s=>s!=="");if(o.length>=2){let s=o[0],c=o[o.length-1];return s===void 0||c===void 0?[void 0,"anchor string is empty"]:[{head:s,mode:"head_tail",tail:c},void 0]}}return[{mode:"full",text:a},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 a=typeof t.text=="string"?t.text.trim():"";return a===""?[void 0,"full anchor requires non-empty 'text'"]:[{mode:"full",text:a},void 0]}let n=typeof t.head=="string"?t.head.trim():"",i=typeof t.tail=="string"?t.tail.trim():"";return n!==""&&i===""?[{mode:"full",text:n},void 0]:n===""&&i!==""?[{mode:"full",text:i},void 0]:n===""||i===""?[void 0,"head_tail anchor requires non-empty 'head' and 'tail'"]:[{head:n,mode:"head_tail",tail:i},void 0]}function SL(e,t){if(e===void 0)return;if(e.mode==="head_tail"){let n=t==="end_anchor"?US(e.tail??"","end_anchor"):US(e.head??"","start_anchor");return n===""?void 0:{mode:"full",text:n}}return{mode:"full",text:US(e.text??"",t)}}function $S(e){return e.mode==="head_tail"?Vt(e.head??"").length+Vt(e.tail??"").length:Vt(e.text??"").length}function US(e,t){let r=zd(e);return r.length<=1?e.trim():t==="end_anchor"?r[r.length-1]??e.trim():r[0]??e.trim()}var kL=l(()=>{"use strict";Qd()});var ai,vL=l(()=>{"use strict";bL();kL();WS();FS();ai=class{resolve(t,r,n){let i=t.start_anchor??t.start,a=t.end_anchor??t.end,[o,s]=this.parseAnchor(i,"start_anchor");if(s!==void 0)return[void 0,s];let c;if(a!==void 0){let[b,E]=this.parseAnchor(a,"end_anchor");if(E!==void 0)return[void 0,E];c=b}let[d,u,p]=this.resolveAnchor({anchor:o,candidateSentenceIds:r,candidateTexts:n,label:"start_anchor"});if(p!==void 0)return[void 0,p];if(d===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:d.score,sentenceIds:[d.sentenceId],strategy:u},void 0];let[m,f,w]=this.resolveAnchor({anchor:c,candidateSentenceIds:r,candidateTexts:n,label:"end_anchor",minIndex:d.index});return w!==void 0?[void 0,w]:m===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:m.index<d.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${d.occurrenceId} -> ${m.occurrenceId}).`}]:[{confidence:Math.min(d.score,m.score),sentenceIds:r.slice(d.index,m.index+1),strategy:`${u}+${f}`},void 0]}resolveWithOverrides(t){let r=t.evidence.start_anchor??t.evidence.start,n=t.evidence.end_anchor??t.evidence.end,[i,a]=this.parseAnchor(r,"start_anchor");if(a!==void 0)return[void 0,a];let o=t.overrides.start_anchor,[s,c,d]=o===void 0?this.resolveAnchor({anchor:i,candidateSentenceIds:t.candidateSentenceIds,candidateTexts:t.candidateTexts,label:"start_anchor"}):[o,"choice_final",void 0];if(d!==void 0)return[void 0,d];if(s===void 0)return[void 0,{candidates:[],code:"none",fieldName:"start_anchor",message:"start_anchor could not be matched: no candidate selected"}];if(n===void 0)return[{confidence:s.score,sentenceIds:[s.sentenceId],strategy:c},void 0];let[u,p]=this.parseAnchor(n,"end_anchor");if(p!==void 0)return[void 0,p];let m=t.overrides.end_anchor,[f,w,b]=m===void 0?this.resolveAnchor({anchor:u,candidateSentenceIds:t.candidateSentenceIds,candidateTexts:t.candidateTexts,label:"end_anchor",minIndex:s.index}):[m,"choice_final",void 0];return b!==void 0?[void 0,b]:f===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:f.index<s.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${s.occurrenceId} -> ${f.occurrenceId}).`}]:[{confidence:Math.min(s.score,f.score),sentenceIds:t.candidateSentenceIds.slice(s.index,f.index+1),strategy:`${c}+${w}`},void 0]}parseAnchor(t,r){let[n,i]=wL(t);return i!==void 0?[void 0,{candidates:[],code:"invalid_anchor",fieldName:r,message:`Invalid evidence.${r}: ${i}`}]:[SL(n,r),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 r=this.rankAnchor(t.anchor,t.candidateSentenceIds,t.candidateTexts,t.label),n=t.minIndex;if(n!==void 0&&(r=r.filter(u=>u.index>=n)),r.length===0)return[void 0,"none",{candidates:[],code:"none",fieldName:t.label,message:`${t.label} could not be matched: no candidates available`}];let i=r.filter(u=>u.exactRaw);if(i.length===1)return[i[0],"exact_raw",void 0];if(i.length>1)return[void 0,"ambiguous_exact_raw",this.#t(t.label,"ambiguous_exact_raw",i)];let a=r.filter(u=>u.exactSubstring);if(a.length===1&&$S(t.anchor)>=8)return[a[0],"exact_substring",void 0];if(a.length>1&&$S(t.anchor)>=8){let u=this.#e(a);return u!==void 0?[u,"exact_substring_scored",void 0]:[void 0,"ambiguous_exact_substring",this.#t(t.label,"ambiguous_exact_substring",a)]}let o=r.filter(u=>u.exactNormalized);if(o.length===1)return[o[0],"exact_normalized",void 0];if(o.length>1){let u=this.#e(o);return u!==void 0?[u,"exact_normalized_scored",void 0]:[void 0,"ambiguous_exact_normalized",this.#t(t.label,"ambiguous_exact_normalized",o)]}let s=r[0];if(s===void 0)return[void 0,"none",{candidates:[],code:"none",fieldName:t.label,message:`${t.label} could not be matched: no candidates available`}];let c=r[1]?.score??0,d=s.score-c;return s.score<.55?[void 0,"low_confidence",{candidates:r.slice(0,3),code:"low_confidence",fieldName:t.label,message:`${t.label} could not be matched confidently. Best candidate score=${s.score.toFixed(3)}.`}]:r.length===1?[s,"auto_top1_single_candidate",void 0]:s.score>=.97?[s,"auto_top1_very_high",void 0]:s.score>=.9&&d>=.07?[s,"auto_top1",void 0]:[void 0,"ambiguous_ranked",this.#t(t.label,"ambiguous_ranked",r)]}rankAnchor(t,r,n,i){let a=[];for(let[o,s]of r.entries()){let c=n[o]??"",d=o>0?n[o-1]??"":"",u=o<n.length-1?n[o+1]??"":"",p=yL(t,c,i);a.push({exactNormalized:p.exactNormalized,exactRaw:p.exactRaw,exactSubstring:p.exactSubstring,index:o,nextText:u,occurrenceId:`S${o+1}`,prevText:d,score:p.score,sentenceId:s,text:c})}return a.sort((o,s)=>o.score!==s.score?s.score-o.score:o.exactRaw!==s.exactRaw?o.exactRaw?-1:1:o.exactSubstring!==s.exactSubstring?o.exactSubstring?-1:1:o.exactNormalized!==s.exactNormalized?o.exactNormalized?-1:1:o.index-s.index),a}#e(t){if(t.length===0)return;if(t.length===1)return t[0];let r=t[0];if(r===void 0)return;let n=t[1]?.score??0;if(r.score>=.9&&r.score-n>=.07)return r}#t(t,r,n){let i=n.slice(0,3),a=i.map(o=>fL(o)).join(`
|
|
2266
|
+
`);return{candidates:i,code:r,fieldName:t,message:`${t} is ambiguous. Top candidates:
|
|
2267
|
+
${a}
|
|
2268
|
+
Please provide a more specific anchor or surrounding context.`}}}});var CL=l(()=>{"use strict"});var TL=l(()=>{"use strict";vL();CL()});var da,zm,RL=l(()=>{"use strict";da=["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(`
|
|
2269
|
+
`),zm=[{quote:"exact short source quote copied from the selected sentence",sentence_id:"sentence id from the source context, such as S1"}]});function oi(e){return e.replace(new RegExp("\\p{Default_Ignorable_Code_Point}","gu"),"").normalize("NFKC").replace(/\s+/gu," ").trim()}function Vm(e){let n=PL(e).replace(new RegExp("\\p{Default_Ignorable_Code_Point}","gu"),"").normalize("NFKC"),a=jY(n).normalize("NFD").replace(new RegExp("\\p{M}","gu"),"");return PL(a).replace(/ +/gu," ").trim()}function LL(e){let t=OL(e).trim();return{normalizedQuote:Vm(t),quoteRaw:t}}function BS(e){let{normalizedQuote:t,quoteRaw:r}=e.preparedQuote,n=OL(e.sentence).trim(),i=Vm(n);if(t===""||i==="")return NL(t,i);if(r===n)return{exactNormalized:!0,exactRaw:!0,exactSubstring:!0,matchEnd:i.length,matchStart:0,normalizedQuote:t,normalizedSentence:i,score:1,strategy:"exact"};if(t===i)return{exactNormalized:!0,exactRaw:!1,exactSubstring:!0,matchEnd:i.length,matchStart:0,normalizedQuote:t,normalizedSentence:i,score:.995,strategy:"normalized_exact"};let a=i.indexOf(t);if(a!==-1){let o=t.length/Math.max(1,i.length);return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:a+t.length,matchStart:a,normalizedQuote:t,normalizedSentence:i,score:Math.min(.99,.95+.04*o),strategy:"normalized_substring"}}return LY(t,i)}function LY(e,t){let r=Math.max(1,Math.floor(e.length*.72)),n=Math.min(t.length,Math.max(r,Math.ceil(e.length*2.25))),i=NL(e,t);for(let a=r;a<=n;a+=1)for(let o=0;o<=t.length-a;o+=1){let s=t.slice(o,o+a),c=OY(e,s);c<=i.score||(i={exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:o+a,matchStart:o,normalizedQuote:e,normalizedSentence:t,score:c,strategy:"fuzzy_window"})}return i}function OY(e,t){let r=GY(e,t),n=UY(e,t),i=$Y(e,t),a=BY(e,t),o=WY(e,t),s=FY(e,t),c=HY(e,t),d=Math.max(r,n,i,a),u=.2*r+.18*n+.24*i+.2*a+.08*o+.05*s+.05*c;return c>=.75&&d<.45&&(u*=.65),Math.max(0,Math.min(1,u))}function NY(e){return AY.get(e)??e.toLocaleLowerCase("und")}function jY(e){let t="";for(let r of e)t+=NY(r);return t}function PL(e){let t="";for(let r of e)t+=DY(r)||!MY(r)?r:" ";return t}function DY(e){return e==="+"||e==="#"||e==="&"}function MY(e){return/\s/u.test(e)||new RegExp("\\p{Separator}","u").test(e)||/[\p{Dash_Punctuation}\p{Connector_Punctuation}\p{Initial_Punctuation}\p{Final_Punctuation}\p{Open_Punctuation}\p{Close_Punctuation}\p{Other_Punctuation}]/u.test(e)||/[\\/.。.。、,,::;;!!??·・•|]/u.test(e)}function OL(e){return e.replace(/<\/?(?:mention|quote)\b[^>]*>/giu,"")}function NL(e,t){return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:-1,matchStart:-1,normalizedQuote:e,normalizedSentence:t,score:0,strategy:"empty"}}function WY(e,t){let r=_L(Qm(e,2),Qm(t,2)),n=_L(Qm(e,3),Qm(t,3));return(r+n)/2}function Qm(e,t){if(e.length<=t)return e===""?new Set:new Set([e]);let r=new Set;for(let n=0;n<=e.length-t;n+=1)r.add(e.slice(n,n+t));return r}function _L(e,t){if(e.size===0||t.size===0)return 0;let r=0;for(let n of e)t.has(n)&&(r+=1);return 2*r/(e.size+t.size)}function FY(e,t){return 1-Math.abs(e.length-t.length)/Math.max(e.length,t.length)}function GY(e,t){if(e===t)return 1;if(e===""||t==="")return 0;let r=Array.from({length:t.length+1},(s,c)=>c),n=new Array(t.length+1).fill(0),i=[...e],a=[...t];for(let[s,c]of i.entries()){n[0]=s+1;for(let[d,u]of a.entries()){let p=(n[d]??0)+1,m=(r[d+1]??0)+1,f=(r[d]??0)+(c===u?0:1);n[d+1]=Math.min(p,m,f)}[r,n]=[n,r]}return 1-(r[a.length]??Math.max(e.length,t.length))/Math.max(e.length,t.length)}function UY(e,t){if(e===""||t==="")return 0;let r=[...e],n=[...t],i=new Array(n.length+1).fill(0),a=new Array(n.length+1).fill(0),o=0;for(let s=1;s<=r.length;s+=1){for(let c=1;c<=n.length;c+=1)a[c]=r[s-1]===n[c-1]?(i[c-1]??0)+1:0,o=Math.max(o,a[c]??0);[i,a]=[a,i],a.fill(0)}return 2*o/(r.length+n.length)}function $Y(e,t){if(e===""||t==="")return 0;let r=[...e],n=[...t],i=new Array(n.length+1).fill(0),a=new Array(n.length+1).fill(0);for(let s=1;s<=r.length;s+=1){for(let c=1;c<=n.length;c+=1)a[c]=r[s-1]===n[c-1]?(i[c-1]??0)+1:Math.max(i[c]??0,a[c-1]??0);[i,a]=[a,i],a.fill(0)}return 2*(i[n.length]??0)/(r.length+n.length)}function BY(e,t){if(e===""||t==="")return 0;let r=[...e],n=[...t],i=new Array(n.length+1).fill(0),a=new Array(n.length+1).fill(0);for(let o=1;o<=r.length;o+=1){for(let s=1;s<=n.length;s+=1)a[s]=r[o-1]===n[s-1]?(i[s-1]??0)+1:Math.max(i[s]??0,a[s-1]??0);[i,a]=[a,i],a.fill(0)}return(i[n.length]??0)/r.length}function HY(e,t){let r=AL(e),n=AL(t),i=0,a=0;for(let[o,s]of r){let c=n.get(o)??0;i+=Math.min(s,c),a+=s}return a===0?0:i/a}function AL(e){let t=new Map;for(let r of e)t.set(r,(t.get(r)??0)+1);return t}var AY,HS=l(()=>{"use strict";AY=new Map([["\xDF","ss"],["\u1E9E","ss"],["\u03C2","\u03C3"]])});function KS(e){let t=r2(e.evidence.quote),r=n2(e.evidence.sentence_id),n;if(t==="")return[void 0,{candidates:[],code:"invalid",message:"Evidence quote is missing or empty."}];let i=LL(t);if(r!==void 0){let u=e.sentences.findIndex(m=>m.id===r),p=e.sentences[u];if(p!==void 0){let m=ML({index:u,scored:BS({preparedQuote:i,sentence:p.text}),sentence:p,sentences:e.sentences});if(n=m,t2(m))return[{candidate:m,confidence:m.score,sentenceIds:[m.sentence.sentenceId],strategy:`sentence_id+${m.strategy}`},void 0]}}let a=XY(i,e.sentences,qS);if(r!==void 0){let u=a.find(m=>m.occurrenceId===r)??n,p=a[0];if(u!==void 0&&p!==void 0&&e2(u,p))return[{candidate:u,confidence:u.score,sentenceIds:[u.sentence.sentenceId],strategy:`sentence_id+${u.strategy}`},void 0]}let o=a[0];if(o===void 0)return[void 0,{candidates:[],code:"none",message:"Evidence quote could not be matched: no candidates available."}];let s=a[1]?.score??0,c=o.score-s,d=zY(t);return o.score<JY?[void 0,{candidates:a.slice(0,qS),code:"low_confidence",message:`Evidence quote could not be matched confidently. Best candidate score=${o.score.toFixed(3)}.`+d}]:o.score>=KY&&c>=DL?[{candidate:o,confidence:o.score,sentenceIds:[o.sentence.sentenceId],strategy:`quote_auto_top1:${o.strategy}`},void 0]:[void 0,{candidates:a.slice(0,qS),code:"ambiguous",message:"Evidence quote is ambiguous. Choose one candidate occurrence ID."+d}]}function zY(e){return zd(e).length<=1?"":" Evidence quote appears to contain more than one sentence. Each evidence item must stay within one source sentence. Recheck whether this evidence should be shortened, split into multiple evidence items, or removed."}function rs(e){let t=QY(e.evidence);if(t.length===0)return[void 0,{candidates:[],code:"invalid",message:"Evidence selection list is empty."}];let r=[],n=[],i=1,a;for(let o of t){let[s,c]=KS({evidence:o,sentences:e.sentences});if(c!==void 0)return[void 0,c];if(s===void 0)return[void 0,{candidates:[],code:"none",message:"Evidence selection could not be resolved."}];a??=s.candidate,i=Math.min(i,s.confidence),r.push(...s.sentenceIds),n.push(s.strategy)}return a===void 0?[void 0,{candidates:[],code:"none",message:"Evidence selection could not be resolved."}]:[{candidate:a,confidence:i,sentenceIds:i2(r),strategy:`list:${n.join(",")}`},void 0]}function QY(e){return VY(e)?e:[e]}function VY(e){return Array.isArray(e)}function XY(e,t,r){let n=[];for(let[i,a]of t.entries()){let o=BS({preparedQuote:e,sentence:a.text}),s=ML({index:i,scored:o,sentence:a,sentences:t});YY(n,s,r)}return n}function YY(e,t,r){let n=e.findIndex(i=>ZY(t,i)<0);if(n===-1){e.length<r&&e.push(t);return}e.splice(n,0,t),e.length>r&&e.pop()}function ML(e){return{exactNormalized:e.scored.exactNormalized,exactRaw:e.scored.exactRaw,exactSubstring:e.scored.exactSubstring,index:e.index,nextText:e.sentences[e.index+1]?.text??"",occurrenceId:e.sentence.id,prevText:e.sentences[e.index-1]?.text??"",score:e.scored.score,sentence:e.sentence,strategy:e.scored.strategy}}function ZY(e,t){return e.score!==t.score?t.score-e.score:e.exactRaw!==t.exactRaw?e.exactRaw?-1:1:e.exactSubstring!==t.exactSubstring?e.exactSubstring?-1:1:e.exactNormalized!==t.exactNormalized?e.exactNormalized?-1:1:e.index-t.index}function e2(e,t){return(e.score>=qY||e.exactSubstring&&e.score>=jL)&&(e.occurrenceId===t.occurrenceId||t.score-e.score<DL)}function t2(e){return e.score>=jL&&e.exactSubstring}function r2(e){return oi(e??"")}function n2(e){let t=e?.trim();return t===""?void 0:t}function i2(e){let t=new Set,r=[];for(let n of e){let i=n.join(":");t.has(i)||(t.add(i),r.push(n))}return r}var qY,jL,KY,DL,JY,qS,WL=l(()=>{"use strict";HS();Qd();qY=.72,jL=.95,KY=.72,DL=.15,JY=.45,qS=5});var ns=l(()=>{"use strict";TL();RL();HS();WL();Qd()});function FL(e){let t=zS();for(let r of e){let n=Vd(r.sentenceId);t[n]=r.wordsCount}return t}function GL(e){let t=zS();for(let r of e.sentences)t[Vd(r.sentenceId)]=r.rawText;return t}function UL(e){return e.map(t=>t.strength===void 0?{from:t.from,to:t.to}:{from:t.from,strength:t.strength,to:t.to})}function $L(e){return[e.occurrenceId,`prev: ${JS(e.prevText)}`,`text: ${JS(e.text)}`,`next: ${JS(e.nextText)}`].join(`
|
|
2270
|
+
`)}function JS(e){let t=e.replace(/\s+/gu," ").trim();return t===""?"(none)":t}function BL(e){return e==="evidence"||e==="start_anchor"||e==="end_anchor"?e:void 0}function HL(e){return typeof e=="object"&&e!==null}function qL(e){return e instanceof Error?e.message:String(e)}function KL(e){return e instanceof pn&&e.cause instanceof Y}function Vd(e){return e.join(":")}function Xd(e){let t=zS();for(let r of e)t[String(r)]=!0;return t}function Yd(e,t){return c2(e,String(t))}function c2(e,t){return Object.hasOwn(e,t)}function zS(){return Object.create(null)}function QS(e){return e.length===1?e[0]:void 0}var Zd=l(()=>{"use strict";Rt()});function JL(e){let{annotations:t,issues:r,validImportanceChunkIds:n}=e;if(t.length===0)return[];if(n===void 0)return t.map(a=>({chunkId:a.chunk_id,importance:Qa(a.importance)}));let i=[];for(let a of t){if(!Yd(n,a.chunk_id)){r.push(`importance_annotations references unknown chunk_id ${a.chunk_id}`);continue}i.push({chunkId:a.chunk_id,importance:Qa(a.importance)})}return i}var zL=l(()=>{"use strict";X();Zd()});function VL(e){let t=Xd(e.tempIds),r=Xd(e.visibleChunkIds),n=[];for(let[i,a]of e.links.entries()){let o=QL({fieldName:"from",index:i+1,issues:e.issues,reference:a.from,validChunkIds:r,validTempIds:t}),s=QL({fieldName:"to",index:i+1,issues:e.issues,reference:a.to,validChunkIds:r,validTempIds:t});o&&s&&n.push(a)}return n}function QL(e){return typeof e.reference=="string"?Yd(e.validTempIds,e.reference):Yd(e.validChunkIds,e.reference)?!0:(e.issues.push(`Link #${e.index}: "${e.fieldName}" chunk_id ${e.reference} does not exist in visible chunks`),!1)}var XL=l(()=>{"use strict";Zd()});function ZL(e){return Array.isArray(e)?e.map(YL):typeof e.quote=="string"||typeof e.sentence_id=="string"?YL(e):void 0}function YL(e){return{...typeof e.quote=="string"?{quote:e.quote}:{},...typeof e.sentence_id=="string"?{sentence_id:e.sentence_id}:{}}}function eO(e,t){return{candidates:e.candidates.map(d2),code:e.code,fieldName:t,message:e.message}}function d2(e){return{exactNormalized:e.exactNormalized,exactRaw:e.exactRaw,exactSubstring:e.exactSubstring,index:e.index,nextText:e.nextText,occurrenceId:e.occurrenceId,prevText:e.prevText,score:e.score,sentenceId:e.sentence.sentenceId,text:e.sentence.text}}var tO=l(()=>{"use strict"});import{z as F}from"zod";var rO,u2,nO,l2,iO,p2,m2,aO,oO,Xm=l(()=>{"use strict";X();rO=F.object({from:F.union([F.number().int(),F.string()]),strength:F.string().optional(),to:F.union([F.number().int(),F.string()])}),u2=F.object({quote:F.string().optional(),sentence_id:F.string().optional()}).passthrough(),nO=F.union([F.record(F.string(),F.unknown()),F.array(u2)]),l2=F.object({content:F.string(),evidence:nO.nullish(),label:F.string(),retention:F.enum(["verbatim","detailed","focused","relevant"]),temp_id:F.string()}).passthrough(),iO=F.object({chunks:F.array(l2),fragment_summary:F.string(),links:F.array(rO)}),p2=F.object({content:F.string(),evidence:nO.nullish(),importance:F.enum(["critical","important","helpful"]),label:F.string(),temp_id:F.string()}).passthrough(),m2=F.object({chunk_id:F.number().int(),importance:F.enum(["critical","important","helpful"])}),aO=F.object({chunks:F.array(p2),importance_annotations:F.array(m2),links:F.array(rO)}),oO=F.object({choice:F.string()})});var h2,Ym,sO=l(()=>{"use strict";Rt();ns();tO();Zd();Xm();h2=3,Ym=class{#e;#t=new ai;#r;#n;#i;constructor(t){this.#e=t.choiceSystemPrompt,this.#r=t.projection,this.#n=t.responseIntentClassifierPrompt,this.#i=t.requestChoice}async resolve(t){let r=t.data.evidence;if(r==null)return[[],void 0];if(!HL(r)&&!Array.isArray(r))return[[],{candidates:[],code:"invalid_evidence",fieldName:"evidence",message:`Chunk #${t.chunkIndex} ("${t.chunkLabel}"): evidence must be an object or array`}];let n=this.#r.sentences,i=n.map(x=>x.sentenceId),a=n.map((x,ce)=>({id:`S${ce+1}`,sentenceId:x.sentenceId,text:x.projectedText})),o=ZL(r),[s,c]=o===void 0?[void 0,void 0]:rs({evidence:o,sentences:a});if(s!==void 0)return[s.sentenceIds,void 0];let d=Array.isArray(r)?[]:this.#a(r);if(d.length>0)return[d,void 0];let u=n.map(x=>x.projectedText),[p,m]=Array.isArray(r)?[void 0,void 0]:this.#t.resolve(r,i,u);if(p!==void 0)return[p.sentenceIds,void 0];let f=c===void 0?m:eO(c,"evidence");if(f===void 0)return[[],void 0];if(!(f.code.startsWith("ambiguous")||f.code==="low_confidence"&&t.isLastGenerationAttempt&&f.candidates.length>0))return[[],f];let b=BL(f.fieldName);if(b===void 0)return[[],f];let[E,N]=await this.#o({candidates:f.candidates,chunkData:t.data,chunkIndex:t.chunkIndex,chunkLabel:t.chunkLabel,fieldName:b});if(N!==void 0)return N.code==="choice_parse_failed_full_fragment"?[i,void 0]:[[],N];if(E===void 0)return[[],{candidates:f.candidates,code:"choice_failed",fieldName:f.fieldName,message:`Second-stage choice failed for ${f.fieldName}: no candidate returned.`}];if(b==="evidence")return[[E.sentenceId],void 0];if(Array.isArray(r))return[[],f];if(t.isLastGenerationAttempt){let[x,ce]=this.#t.resolveWithOverrides({candidateSentenceIds:i,candidateTexts:u,evidence:r,overrides:{[b]:E}});return x!==void 0?[x.sentenceIds,void 0]:[[],ce]}let C={...r,[b]:{mode:"full",text:E.text}},[q,Q]=this.#t.resolve(C,i,u);return q!==void 0?[q.sentenceIds,void 0]:[[],Q]}#a(t){let r=t.start_anchor??t.start,[n,i]=this.#t.parseAnchor(r,"start_anchor");if(i!==void 0||n?.text===void 0)return[];let a=QS(this.#r.findExactMatches(n.text));if(a===void 0)return[];let o=t.end_anchor??t.end;if(o===void 0)return this.#r.resolveSentenceIds(a);let[s,c]=this.#t.parseAnchor(o,"end_anchor");if(c!==void 0||s?.text===void 0)return[];let d=QS(this.#r.findExactMatches(s.text));return d===void 0||d.offset<a.offset?[]:this.#r.resolveSentenceIds({length:d.offset+d.length-a.offset,offset:a.offset})}async#o(t){let r=this.#s(t),n=t.candidates.map(i=>i.occurrenceId);try{return[await Ae({maxRetries:h2,messages:r,parse:a=>{let o=t.candidates.find(s=>s.occurrenceId===a.choice);if(o===void 0)throw new Y([`Invalid choice "${a.choice}". Expected one of: ${n.join(", ")}`]);return o},responseIntentClassifierPrompt:this.#n,request:this.#i,schema:oO}),void 0]}catch(i){return KL(i)?[void 0,{candidates:t.candidates,code:"choice_parse_failed_full_fragment",fieldName:t.fieldName,message:`Second-stage choice parse validation failed for ${t.fieldName}; falling back to the full fragment span.`}]:[void 0,{candidates:t.candidates,code:"choice_failed",fieldName:t.fieldName,message:`Second-stage choice failed for ${t.fieldName}: ${qL(i)}`}]}}#s(t){return[{content:this.#e,role:"system"},{content:`Previously generated chunk (do NOT rewrite it):
|
|
2271
|
+
\`\`\`json
|
|
2272
|
+
${JSON.stringify(t.chunkData,null,2)}
|
|
2273
|
+
\`\`\`
|
|
2274
|
+
|
|
2275
|
+
Resolve only this field: "${t.fieldName}" for chunk #${t.chunkIndex} [${t.chunkLabel}].
|
|
2276
|
+
Choose exactly one candidate occurrence ID from the list below.
|
|
2277
|
+
|
|
2278
|
+
`+t.candidates.map($L).join(`
|
|
2279
|
+
`),role:"user"}]}}});var Zm,cO=l(()=>{"use strict";Rt();X();zL();Zd();XL();sO();Xm();Zm=class{#e;#t;#r;#n;#i;#a;#o;constructor(t){this.#e=new Ym({choiceSystemPrompt:t.choiceSystemPrompt,projection:t.projection,requestChoice:t.requestChoice,responseIntentClassifierPrompt:t.responseIntentClassifierPrompt}),this.#t=t.metadataField,this.#n=t.sentenceTextSource,this.#o=t.visibleChunkIds,this.#i=FL(t.projection.sentences.map(r=>({sentenceId:r.sentenceId,wordsCount:r.wordsCount}))),this.#r=GL(t.projection),this.#a=t.validImportanceChunkIds===void 0?void 0:Xd(t.validImportanceChunkIds)}async parse(t,r){let n=[],i=[],a=[],o="importance_annotations"in t?JL({annotations:t.importance_annotations,issues:n,validImportanceChunkIds:this.#a}):void 0,s="fragment_summary"in t?t.fragment_summary:void 0,c=UL(t.links);for(let[u,p]of t.chunks.entries()){let m=u+1,f=[],w=p.label.trim(),b=p.content.trim();w===""&&f.push(`Chunk #${m}: Missing or empty "label" field`),b===""&&f.push(`Chunk #${m}: Missing or empty "content" field`);let[E,N]=await this.#e.resolve({chunkIndex:m,chunkLabel:w,data:p,isLastGenerationAttempt:r.isLastGenerationAttempt});if(E.length===0&&(N!==void 0?f.push(`Chunk #${m} ("${w}"): ${N.message}`):f.push(`Chunk #${m} ("${w}"): Missing evidence`)),f.length>0){n.push(...f);continue}let C=E[0];if(C===void 0){n.push(`Chunk #${m} ("${w}"): Unable to resolve any sentence IDs`);continue}let q=E.reduce((Q,x)=>Q+(this.#i[Vd(x)]??0),0);if(this.#t==="retention"){let Q=p;i.push({content:b,generation:0,id:0,label:w,links:[],retention:oy(Q.retention),sentenceId:C,sentenceIds:[...E],wordsCount:q})}else{let Q=p;i.push({content:b,generation:0,id:0,importance:Qa(Q.importance),label:w,links:[],sentenceId:C,sentenceIds:[...E],wordsCount:q})}a.push(p.temp_id)}let d=VL({issues:n,links:c,tempIds:a,visibleChunkIds:this.#o});if(n.length>0)throw new Y(n);return{chunkBatch:{chunks:i,links:d,orderCorrect:!0,tempIds:a,...o===void 0?{}:{importanceAnnotations:o}},...s===void 0?{}:{fragmentSummary:s}}}async getChunkSourceSentences(t){let r=[];for(let n of t.sentenceIds){let i=Vd(n),a=this.#r[i];if(a!==void 0){r.push(a);continue}r.push(await this.#n.getSentence(n))}return r}}});var dO=l(()=>{"use strict";cO();Xm()});function eu(e){let t="";for(let r of e)t+=g2[r]??r;return t}var g2,si,eh=l(()=>{"use strict";g2={'"':"\uFF02","\\":"\uFF3C"},si=class{#e;#t;#r;constructor(t){let r=[],n="",i="",a=0;for(let[o,s]of t.entries()){o>0&&(n+=" ",i+=" ",a+=1);let c=eu(s.text),d=a,u=d+c.length;n+=s.text,i+=c,r.push({endOffset:u,projectedText:c,rawText:s.text,sentenceId:s.sentenceId,startOffset:d,wordsCount:s.wordsCount}),a=u}this.#t=n,this.#e=i,this.#r=r}get projectedText(){return this.#e}get rawText(){return this.#t}get sentences(){return this.#r}findExactMatches(t){let r=eu(t).trim();if(r==="")return[];let n=[],i=0;for(;i<=this.#e.length-r.length;){let a=this.#e.indexOf(r,i);if(a===-1)break;n.push({length:r.length,offset:a}),i=a+1}return n}projectText(t){return eu(t)}resolveSentenceIds(t){if(t.length<=0)return[];let r=t.offset,n=t.offset+t.length;return this.#r.filter(i=>i.endOffset>r&&i.startOffset<n).map(i=>i.sentenceId)}}});import{detect as y2,validateISO2 as uO}from"tinyld";function th(e){let t=e.trim();if(t!=="")try{return b2(y2(t))}catch{return}}function rh(e){return Mg(e)}function b2(e){let t=e.trim().toLowerCase(),r=uO(t);if(r!=="")return r;let n=t.split("-")[0];if(n===void 0)return;let i=uO(n);return i===""?void 0:i}var VS=l(()=>{"use strict";Nr()});function pO(e){let t=rh(e.targetLanguage);return lO(e.label,t)||lO(e.content,t)}function lO(e,t){if(e.trim()==="")return!1;let r=th(e);return r===void 0?!0:r!==t}var mO=l(()=>{"use strict";VS();Nr()});var hO,fO,gO,yO,bO=l(()=>{"use strict";hO="topologization/user_focused_extraction",fO="topologization/book_coherence_extraction",gO="topologization/evidence_choice",yO="topologization/translate_chunks"});import{z as tu}from"zod";function wO(e){return e.sentences.map((t,r)=>`S${r+1}: ${eu(oi(t.rawText))}`).join(`
|
|
2280
|
+
`)}function I2(e){return e instanceof pn&&e.cause instanceof Y}function x2(e){return I2(e)||e instanceof mt}function v2(e){let t=Object.create(null);for(let r of e)t[String(r.id)]=r;return t}function C2(e,t){let r=[];t.length!==e.length&&r.push(`Expected ${e.length} translated chunk(s), got ${t.length}`);for(let[n,i]of e.entries()){let a=t[n];if(a===void 0){r.push(`Missing translated chunk at position ${n}`);continue}a.id!==i.id&&r.push(`Translated chunk at position ${n} must keep id ${i.id}, got ${a.id}`)}if(r.length>0)throw new Y(r)}var w2,S2,k2,E2,ru,SO=l(()=>{"use strict";Rt();ns();dO();eh();eh();mO();bO();w2=7,S2=tu.object({content:tu.string(),id:tu.number(),label:tu.string()}),k2=tu.array(S2),E2=3,ru=class{#e;#t;#r;#n;#i;constructor(t){this.#e=t.extractionGuidance,this.#t=t.llm,this.#r=t.scopes,this.#n=t.sentenceTextSource,this.#i=t.userLanguage}async extractUserFocused(t){let r=new si(t.sentences),n=this.#a({promptTemplateName:hO,templateContext:{extraction_guidance:this.#e,evidence_selection_prompt:da,user_language:this.#i,working_memory:t.workingMemoryPrompt},text:wO(r)}),i=await this.#o({emptyChunkBatch:{chunks:[],links:[],orderCorrect:!0,tempIds:[]},messages:n,metadataField:"retention",projection:r,schema:iO,sentences:t.sentences,visibleChunkIds:t.visibleChunkIds});return{chunkBatch:await this.#s(i.result.chunkBatch,i.parser),fragmentSummary:i.result.fragmentSummary??""}}async extractBookCoherence(t){let r=new si(t.sentences),n=this.#a({promptTemplateName:fO,templateContext:{user_focused_chunks:t.userFocusedChunks.map(a=>({content:a.content,id:a.id,label:a.label})),evidence_selection_prompt:da,user_language:this.#i,working_memory:t.workingMemoryPrompt},text:wO(r)}),i=await this.#o({emptyChunkBatch:{chunks:[],importanceAnnotations:[],links:[],orderCorrect:!0,tempIds:[]},messages:n,metadataField:"importance",projection:r,schema:aO,sentences:t.sentences,validImportanceChunkIds:t.userFocusedChunks.map(a=>a.id),visibleChunkIds:t.visibleChunkIds});return await this.#s(i.result.chunkBatch,i.parser)}#a(t){return[{content:this.#t.loadSystemPrompt(t.promptTemplateName,t.templateContext),role:"system"},{content:t.text,role:"user"}]}async#o(t){return await this.#t.withContext(async r=>{let n=new Zm({metadataField:t.metadataField,projection:t.projection,responseIntentClassifierPrompt:this.#t.loadSystemPrompt(Me),sentenceTextSource:this.#n,sentences:t.sentences,visibleChunkIds:t.visibleChunkIds,choiceSystemPrompt:this.#t.loadSystemPrompt(gO,{extraction_guidance:this.#e,metadata_field:t.metadataField,user_language:this.#i}),requestChoice:async(i,a,o)=>await r.request(i,{retryIndex:a,retryMax:o,scope:this.#r.choice,useCache:!1}),...t.validImportanceChunkIds===void 0?{}:{validImportanceChunkIds:t.validImportanceChunkIds}});try{let i=await Ae({messages:t.messages,schema:t.schema,maxRetries:w2,parse:async(a,o,s)=>await n.parse(a,{isLastGenerationAttempt:o>=s}),responseIntentClassifierPrompt:this.#t.loadSystemPrompt(Me),request:async(a,o,s)=>await r.request(a,{retryIndex:o,retryMax:s,scope:this.#r.extraction})});return{parser:n,result:i}}catch(i){if(x2(i))return{parser:n,result:{chunkBatch:t.emptyChunkBatch}};throw i}})}async#s(t,r){if(this.#i===void 0||t.chunks.length===0)return t;let n=[];for(let[i,a]of t.chunks.entries())pO({content:a.content,label:a.label,targetLanguage:this.#i})&&n.push({content:a.content,id:i,label:a.label,sourceSentences:await r.getChunkSourceSentences(a)});if(n.length===0)return t;try{let i=await this.#c(n,this.#i),a=v2(i);return{...t,chunks:t.chunks.map((o,s)=>{let c=a[String(s)];return c===void 0?o:{...o,content:c.content,label:c.label}})}}catch{return t}}async#c(t,r){return await Ae({messages:[{content:this.#t.loadSystemPrompt(yO,{user_language:r}),role:"system"},{content:JSON.stringify(t,void 0,2),role:"user"}],schema:k2,maxRetries:E2,parse:n=>(C2(t,n),n),responseIntentClassifierPrompt:this.#t.loadSystemPrompt(Me),request:async(n,i,a)=>await this.#t.request(n,{retryIndex:i,retryMax:a,scope:this.#r.extraction})})}}});async function XS(e,t){return await new ru(e).extractUserFocused(t)}async function YS(e,t){return await new ru(e).extractBookCoherence(t)}var ZS=l(()=>{"use strict";SO()});function kO(){return new ek}var T2,ek,EO=l(()=>{"use strict";T2=/(?:[.!?。!?…]+|(?:\r?\n)\s*(?:\r?\n)?|[.!?。!?…]+["'”’」』】)》〉〕〗]*)$/u,ek=class{#e=new Intl.Segmenter(void 0,{granularity:"grapheme"});#t=new Intl.Segmenter(void 0,{granularity:"sentence"});#r=new Intl.Segmenter(void 0,{granularity:"word"});async*pipe(t){let r="",n=0;for await(let a of t){if(a==="")continue;r+=a;let{remainder:o,sentences:s}=this.#n(r,!1);r=o;for(let c of s)yield{offset:n,text:c,wordsCount:this.#i(c)},n+=c.length}let{sentences:i}=this.#n(r,!0);for(let a of i)yield{offset:n,text:a,wordsCount:this.#i(a)},n+=a.length}#n(t,r){if(t.trim()==="")return{remainder:"",sentences:[]};let n=Array.from(this.#t.segment(t));if(n.length===0)return{remainder:t,sentences:[]};if(r||this.#o(t))return{remainder:"",sentences:n.map(a=>a.segment.trim()).filter(a=>a!=="")};let i=n[n.length-1];return i===void 0?{remainder:t,sentences:[]}:{remainder:t.slice(i.index),sentences:n.slice(0,-1).map(a=>a.segment.trim()).filter(a=>a!=="")}}#i(t){let r=0;for(let n of this.#r.segment(t))"isWordLike"in n&&n.isWordLike&&(r+=1);return r>0?r:this.#a(t)}#a(t){let r=0;for(let n of this.#e.segment(t))r+=1;return r}#o(t){return T2.test(t.trimEnd())}}});function ci(e,t){return(t?.adapter??kO()).pipe(e)}var tk=l(()=>{"use strict";EO()});var nu,IO=l(()=>{"use strict";dL();ZS();tk();nu=class{#e;#t;#r;constructor(t){this.#e=new Km(t.attention.capacity,t.attention.generationDecayFactor,t.attention.idGenerator),this.#t={extractionGuidance:t.extractionGuidance,llm:t.llm,scopes:t.scopes,sentenceTextSource:t.sentenceTextSource,...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}},this.#r=t.segmenter}get capacity(){return this.#e.capacity}segment(t){return this.#r===void 0?ci(t):ci(t,{adapter:this.#r})}async extractUserFocused(t){let r=this.#e.createChunkBatchContext(),n=await XS(this.#t,{sentences:t.sentences,text:t.text,visibleChunkIds:r.visibleChunkIds,workingMemoryPrompt:r.workingMemoryPrompt});return{delta:await this.#e.acceptChunkBatch(n.chunkBatch),fragmentSummary:n.fragmentSummary}}async extractBookCoherence(t){let r=this.#e.createChunkBatchContext({includeCurrentFragment:!1}),n=[...r.visibleChunkIds,...t.userFocusedChunks.map(a=>a.id)],i=await YS(this.#t,{sentences:t.sentences,text:t.text,userFocusedChunks:t.userFocusedChunks,visibleChunkIds:n,workingMemoryPrompt:r.workingMemoryPrompt});return await this.#e.acceptChunkBatch(i)}completeFragment(t){this.#e.completeFragment(t)}clear(){this.#e.clear()}}});var nh=l(()=>{"use strict";IO();tk()});var ih=l(()=>{"use strict"});async function nk(e){let t=0,r=0;for await(let n of R2({maxWordsCount:320,stream:ci(e.stream,{...e.segmenter===void 0?{}:{adapter:e.segmenter}})}))t+=1,r+=ik(n.sentences);return{fragments:t,words:r}}async function*R2(e){let t=[],r=0;for await(let n of e.stream){let i=n.text.trim();i!==""&&(t.length>0&&r+n.wordsCount>e.maxWordsCount&&(yield{sentences:t},t=[],r=0),t.push({text:i,wordsCount:n.wordsCount}),r+=n.wordsCount)}t.length>0&&(yield{sentences:t})}async function xO(e,t){let r=e.listSentences===void 0?await P2(e):await e.listSentences(),n=[],i=[],a=0,o=0;for(let s=0;s<r.length;s+=1){let c=r[s];c===void 0||c.text.trim()===""||(i.length>0&&o+c.wordsCount>t&&(n.push({startSentenceIndex:a,sentences:i}),i=[],o=0),i.length===0&&(a=s),i.push(c),o+=c.wordsCount)}return i.length>0&&n.push({startSentenceIndex:a,sentences:i}),n}async function vO(e){let t=[];for await(let r of e)t.push(r);return t.join("")}function ik(e){return e.reduce((t,r)=>t+r.wordsCount,0)}async function CO(e,t){return t.length===0?"":(await Promise.all(t.map(async n=>await e.getFragment(n)))).flatMap(n=>n.sentences.map(i=>i.text)).join(" ").trim()}async function P2(e){return(await Promise.all((await e.listFragmentIds()).map(async r=>await e.getFragment(r)))).sort((r,n)=>r.fragmentId-n.fragmentId).flatMap(r=>r.sentences)}var ah=l(()=>{"use strict";nh();ih()});var TO=l(()=>{"use strict";ah()});async function _O(e){let t=await e.document.snakes.listIdsByGroup(e.serialId,e.groupId),r=await Promise.all(t.map(async o=>await e.document.snakes.getById(o))),n=await Promise.all(r.filter(o=>o!==void 0).map(async o=>await O2(o,e.document)));if(n.length<=e.maxClues)return PO(n);let i=[...n],a=-1;for(;i.length>e.maxClues;){i.sort(AO);let o=Math.floor(e.maxClues*.75),s=i.slice(o);if(s.length<2)break;let c=N2(s),[d,u]=c??[...i].sort(A2).slice(0,2);if(d===void 0||u===void 0)break;i=i.filter(p=>p!==d&&p!==u),i.push(j2(d,u,a)),a-=1}return PO(i)}function _2(e,t){let r=RO(e.chunks),n=RO(t.chunks),i=Object.create(null),a=0;for(let o of r)i[String(o)]=!0,a+=1;for(let o of n){let s=String(o);i[s]!==!0&&(i[s]=!0,a+=1)}return r.length+n.length-a}function RO(e){let t=Object.create(null),r=[];for(let n of e)for(let i of n.sentenceIds){let a=i[1],o=String(a);t[o]!==!0&&(t[o]=!0,r.push(a))}return r.sort(LO),r}function A2(e,t){return e.weight-t.weight}function AO(e,t){return t.weight-e.weight}function L2(e,t){let[r,n]=e.sentenceId,[i,a]=t.sentenceId;return r!==i?r-i:n-a}function LO(e,t){return e-t}async function O2(e,t){let r=await t.snakeChunks.listChunkIds(e.id),n=(await Promise.all(r.map(async i=>await t.chunks.getById(i)))).filter(i=>i!==void 0);return{clueId:e.id,chunks:n,isMerged:!1,label:`${e.firstLabel} -> ${e.lastLabel}`,sourceSnakeIds:[e.id],weight:e.weight}}function N2(e){let t,r=-1;for(let n=0;n<e.length;n+=1){let i=e[n];if(i!==void 0)for(let a=n+1;a<e.length;a+=1){let o=e[a];if(o===void 0)continue;let s=_2(i,o);s<=r||(r=s,t=[i,o])}}return t}function j2(e,t,r){let n=[...e.sourceSnakeIds,...t.sourceSnakeIds].sort(LO),i=[...e.chunks,...t.chunks].sort(L2);return{clueId:r,chunks:i,isMerged:!0,label:`Merged minor clues (${n.length})`,sourceSnakeIds:n,weight:e.weight+t.weight}}function PO(e){let t=e.reduce((n,i)=>n+i.weight,0),r=e.map(n=>({...n,weight:t===0?0:n.weight/t}));return r.sort(AO),r}var OO=l(()=>{"use strict"});var NO,jO,DO,MO,oh=l(()=>{"use strict";NO="editor/clue_reviewer",jO="editor/clue_reviewer_generator",DO="editor/revision_feedback",MO="editor/text_compressor"});function D2(e,t,r){let n=[{content:e.systemPrompt,role:"system"},{content:e.markedText,role:"user"}];return t!==void 0&&r!==void 0&&n.push({content:`<final>${t}</final>`,role:"assistant"},{content:r,role:"user"}),n}function M2(e){let t=e.trim(),r=/^<final>([\s\S]*)<\/final>$/.exec(t);if(r===null)throw new Error("Compression response must be exactly one <final>...</final> block.");let n=r[1]?.trim();if(n===void 0||n==="")throw new Error("Compression response contained an empty <final> block.");if(/<\/?[A-Za-z][^>]*>/.test(n))throw new Error("Compressed text must not contain XML or HTML tags.");return n}function W2(e){return["Your previous response was invalid.",e instanceof Error?e.message:String(e),"Return exactly one <final>...</final> block and nothing else.","The content inside <final> must be plain text only, with no tags, headings, fences, or explanatory text."].join(" ")}var ak,sh,WO=l(()=>{"use strict";oh();ak=2,sh=class{#e;#t;#r;#n;constructor(t,r,n,i){this.#e=n,this.#t=t,this.#r=r,this.#n=i}async request(t){let r=Math.floor(t.targetLength*.85),n=Math.floor(t.targetLength*1.15),i=this.#t.loadSystemPrompt(MO,{acceptable_max:n,acceptable_min:r,compression_ratio:Math.floor(this.#e*100),original_length:t.markedText.length,target_length:t.targetLength,user_language:this.#n}),a=D2({markedText:t.markedText,systemPrompt:i},t.previousCompressedText,t.revisionFeedback);return await this.#t.request(async o=>{let s=a;for(let c=0;c<=ak;c+=1){let d=await o(s,{retryIndex:c,retryMax:ak,scope:this.#r,...c===0?{}:{useCache:!1}});try{return M2(d)}catch(u){if(c>=ak)throw u;s=[...s,{content:d,role:"assistant"},{content:W2(u),role:"user"}]}}throw new Error("Compression request failed unexpectedly")})}}});var is,sIe,FO,ch=l(()=>{"use strict";iy();is=(n=>(n.Critical="critical",n.Major="major",n.Minor="minor",n))(is||{}),sIe=za(is),FO=dc(is,"review severity")});function GO(e){let t=0;for(let r of e)for(let n of r.issues)t+=ok[n.severity]*r.weight;return t}function UO(e){let t=HO(e.reviews),r=t.slice(0,9),n=t.length-r.length,i=[];for(let a=0;a<r.length;a+=1){let o=r[a];o!==void 0&&(i.push(`${a+1}. [${o.severity.toUpperCase()}]`,` Problem: ${o.problem}`),o.suggestion!==""&&i.push(` Suggestion: ${o.suggestion}`),i.push(""))}return n>0&&i.push(`... and ${n} more issues hidden (lower priority)`),e.llm.loadSystemPrompt(DO,{issues_description:i.join(`
|
|
2281
|
+
`)})}function $O(e){let t=HO(e);if(t.length===0)return`No issues found - all reviewers are satisfied.
|
|
2282
|
+
`;let r=[];for(let n=0;n<t.length;n+=1){let i=t[n];i!==void 0&&(r.push(`${n+1}. [${i.severity.toUpperCase()}]`,` Problem: ${i.problem}`),i.suggestion!==""&&r.push(` Suggestion: ${i.suggestion}`),r.push(""))}return r.join(`
|
|
2283
|
+
`)}function BO(e){let t=e.reduce((r,n)=>r===void 0||n.score<r.score?n:r,void 0);if(t===void 0)throw new Error("Compression failed: no versions generated");return t}function HO(e){let t=e.flatMap(r=>r.issues.map(n=>({...n,weight:r.weight})));return t.sort((r,n)=>{let i=ok[n.severity]-ok[r.severity];return i!==0?i:n.weight-r.weight}),t}var ok,sk=l(()=>{"use strict";oh();ch();ok=Object.freeze({critical:9,major:3,minor:1})});function qO(e){if(e.userLanguage===void 0)return;let t=rh(e.userLanguage),r=th(e.compressedText);if(!(r===void 0||r===t))return{detectedLanguageCode:r,review:{clueId:-1,issues:[{problem:`Output language error: detected ${r}, but ${t} (${e.userLanguage}) is required.`,severity:"critical",suggestion:`Please translate the entire compressed text to ${e.userLanguage}. Maintain all information integrity and ensure the translation sounds natural and native, not machine-translated.`}],weight:1},targetLanguageCode:t}}var KO=l(()=>{"use strict";VS();Nr();ch()});import{appendFile as dh,writeFile as F2}from"fs/promises";function G2(e,t,r){let n=["=".repeat(80),`CHUNK HIERARCHY - Serial ${r}, Group ${t}`,"=".repeat(80),""];for(let i=0;i<e.length;i+=1){let a=e[i];if(a!==void 0){n.push(`Clue #${i+1} (ID: ${a.clueId})`,`|- Weight: ${a.weight.toFixed(4)}`,`|- Label: ${a.label}`,`|- Source snakes: ${a.sourceSnakeIds.join(", ")}`,`|- Merged: ${a.isMerged?"yes":"no"}`,`\\- Chunks: ${a.chunks.length}`,"");for(let o=0;o<a.chunks.length;o+=1){let s=a.chunks[o];if(s===void 0)continue;let c=s.content.length>60?`${s.content.slice(0,60)}...`:s.content;n.push(` - Chunk ${o+1}/${a.chunks.length} (ID: ${s.id})`,` Label: ${s.label}`,` Retention: ${s.retention??"N/A"}`,` Importance: ${s.importance??"N/A"}`,` Content: ${c}`,"")}}}return n.push("=".repeat(80),""),n.join(`
|
|
2284
|
+
`)}function U2(e){let t=e.getFullYear(),r=String(e.getMonth()+1).padStart(2,"0"),n=String(e.getDate()).padStart(2,"0"),i=String(e.getHours()).padStart(2,"0"),a=String(e.getMinutes()).padStart(2,"0"),o=String(e.getSeconds()).padStart(2,"0");return`${t}-${r}-${n} ${i}-${a}-${o}`}var uh,JO=l(()=>{"use strict";jt();sk();uh=class{#e;#t;#r;#n;#i;#a;constructor(t,r,n){this.#e=n.compressionRatio,this.#t=r,this.#r=n.logDirPath,this.#n=n.maxIterations,this.#i=t}async appendCompressionResult(t){if(this.#a===void 0)return;let r=[];t.thinkingText!==""&&r.push("Thinking:","-".repeat(80),t.thinkingText,"-".repeat(80),""),r.push(`Compressed Text (${t.compressedText.length} characters):`,"-".repeat(80),t.compressedText,"-".repeat(80),"",""),await dh(this.#a,`${r.join(`
|
|
2285
|
+
`)}
|
|
2286
|
+
`,"utf8")}async appendFinalSelection(t,r){if(this.#a===void 0)return;let n=["","=".repeat(80),"FINAL SELECTION","=".repeat(80),"",`Selected: Iteration ${t.iteration}/${this.#n}`,`Score: ${t.score.toFixed(2)}`,`Length: ${t.text.length} characters`,`Compression ratio: ${(t.text.length/r).toFixed(1)}%`,"","=".repeat(80),""];t.score>0&&n.push("REMAINING UNRESOLVED ISSUES","=".repeat(80),"",$O(t.reviews),"=".repeat(80),""),await dh(this.#a,`${n.join(`
|
|
2287
|
+
`)}
|
|
2288
|
+
`,"utf8")}async appendIterationHeader(t,r){if(this.#a===void 0)return;let n=["","=".repeat(80),`ITERATION ${t}/${this.#n}`,"=".repeat(80),""];r!==void 0&&r.trim()!==""&&n.push("Revision Feedback:","-".repeat(80),r,"-".repeat(80),""),await dh(this.#a,`${n.join(`
|
|
2289
|
+
`)}
|
|
2290
|
+
`,"utf8")}async appendLanguageMismatch(t){if(this.#a===void 0)return;let r=t.review.issues[0];if(r===void 0)return;let n=["","!".repeat(80),"LANGUAGE MISMATCH DETECTED","!".repeat(80),`Expected: ${t.targetLanguageCode} (${t.userLanguage??"unknown"})`,`Detected: ${t.detectedLanguageCode}`,`Issue: ${r.problem}`,`Suggestion: ${r.suggestion}`,"!".repeat(80),""];await dh(this.#a,`${n.join(`
|
|
2291
|
+
`)}
|
|
2292
|
+
`,"utf8")}async initialize(t){if(this.#r===void 0)return;let r=U2(new Date);this.#a=Gl({category:"editor",logDirPath:this.#r,prefix:`compression-serial-${this.#i}-group-${this.#t}`}),this.#a!==void 0&&await F2(this.#a,["=== Text Compression Log ===",`Serial: ${this.#i}, Group: ${this.#t}`,`Started at: ${r}`,`Compression ratio target: ${Math.round(this.#e*100)}%`,`Max iterations: ${this.#n}`,"","",G2(t,this.#t,this.#i),""].join(`
|
|
2293
|
+
`),"utf8")}}});async function VO(e){return await dk({chunks:e.chunks,segmentStartIndexes:await K2(e.chunks,e.serialFragments),serialFragments:e.serialFragments,...e.fullMarkup===void 0?{}:{fullMarkup:e.fullMarkup},...e.wrapHighRetention===void 0?{}:{wrapHighRetention:e.wrapHighRetention}})}async function dk(e){if(e.segmentStartIndexes.length===0)return"";let t=H2(e.chunks),r=[...e.segmentStartIndexes].sort(iu),n=[...await e.serialFragments.listFragmentIds()].sort(iu),i=await z2(q2(r,n),e.serialFragments),a=[];for(let o=0;o<r.length;o+=1){let s=r[o];if(s===void 0)continue;let c=i[String(s)];if(c!==void 0){if(o>0){let d=r[o-1];if(d!==void 0&&s>d){let u=B2({fragments:i,skippedStartIndexes:XO(n,d,s)});u!==""&&a.push(u)}}a.push($2({chunkCoverage:t,fragment:c,fullMarkup:e.fullMarkup??!1,wrapHighRetention:e.wrapHighRetention??!1}))}}return a.join(`
|
|
2294
|
+
|
|
2295
|
+
`)}function $2(e){let t=[],{fragment:r}=e,n=0;for(;n<r.sentences.length;){if(r.sentences[n]===void 0){n+=1;continue}let o=ck(r.serialId,r.fragmentId+n),s=e.chunkCoverage[o];if(s===void 0){t.push({endIndex:n+1,startIndex:n}),n+=1;continue}let c=zO(s),d=n+1;for(;d<r.sentences.length;){let u=ck(r.serialId,r.fragmentId+d),p=e.chunkCoverage[u];if(p===void 0||zO(p).label!==c.label)break;d+=1}t.push({chunkAttributes:c,endIndex:d,startIndex:n}),n=d}return t.map(a=>Q2({fragment:r,fullMarkup:e.fullMarkup,range:a,wrapHighRetention:e.wrapHighRetention})).join(" ")}function B2(e){let t=[];for(let r of e.skippedStartIndexes){let n=e.fragments[String(r)]?.summary?.trim()??"";n!==""&&t.push(n)}return t.join(" ")}function H2(e){let t=Object.create(null);for(let r of e)for(let n of r.sentenceIds){let i=ck(...n),a=t[i];t[i]=a===void 0?[r]:[...a,r]}return t}function ck(e,t){return`${e}:${t}`}function q2(e,t){let r=new Set(e);for(let n=1;n<e.length;n+=1){let i=e[n-1],a=e[n];if(!(i===void 0||a===void 0))for(let o of XO(t,i,a))r.add(o)}return[...r].sort(iu)}function XO(e,t,r){return e.filter(n=>n>t&&n<r)}async function K2(e,t){let r=[...await t.listFragmentIds()].sort(iu),n=Object.create(null),i=[];for(let a of e)for(let o of a.sentenceIds){let s=J2(r,o[1]);if(s===void 0)continue;let c=String(s);n[c]!==!0&&(n[c]=!0,i.push(s))}return i.sort(iu),i}function J2(e,t){let r;for(let n of e){if(n>t)break;r=n}return r}async function z2(e,t){let r=Object.create(null);return await Promise.all(e.map(async n=>{r[String(n)]=await t.getFragment(n)})),r}function iu(e,t){return e-t}function zO(e){let t=QO({detailed:3,focused:2,relevant:1,verbatim:4}),r=QO({critical:3,helpful:1,important:2}),n=e[0],i=e[0];for(let o of e)as(o.retention,t)>as(n?.retention,t)&&(n=o),as(o.importance,r)>as(i?.importance,r)&&(i=o);return{label:(as(n?.retention,t)>=as(i?.importance,r)?n:i)?.label??"",...i?.importance===void 0?{}:{importance:i.importance},...n?.retention===void 0?{}:{retention:n.retention}}}function QO(e){return Object.freeze(e)}function as(e,t){return e===void 0?0:t[e]??0}function Q2(e){let t=e.fragment.sentences.slice(e.range.startIndex,e.range.endIndex).map(n=>n.text).join(" "),r=e.range.chunkAttributes;if(r===void 0)return t;if(e.fullMarkup){let n=[`label="${r.label}"`];return r.retention!==void 0&&n.push(`retention="${r.retention}"`),r.importance!==void 0&&n.push(`importance="${r.importance}"`),`<chunk ${n.join(" ")}>${t}</chunk>`}return e.wrapHighRetention&&(r.retention==="verbatim"||r.retention==="detailed")?`<chunk retention="${r.retention}">${t}</chunk>`:t}var uk=l(()=>{"use strict";X()});function ZO(e){return e.replace(/<chunk(?:\s+[^>]*)?>/g,"").replace(/<\/chunk>/g,"")}function eN(e){let t=e.match(/##\s*(?:Compressed\s+Text|压缩文本)\s*\n+(.*?)(?:\n+---|\*\*CRITICAL\*\*|$)/is);return t?.[1]!==void 0?YO(t[1].trim()):YO(e.trim())}function tN(e){let t=e.match(/##\s*(?:Compressed\s+Text|压缩文本)\s*/i);return t?.index===void 0?"":e.slice(0,t.index).trim()}function YO(e){return e.match(/^```(?:\w+)?\s*\n([\s\S]*?)\n```$/)?.[1]?.trim()??e}var rN=l(()=>{"use strict"});import{z as os}from"zod";function Z2(e,t){return t===void 0?[{content:e.systemPrompt,role:"system"},{content:e.compressedText,role:"user"}]:[{content:e.systemPrompt,role:"system"},{content:t[0],role:"user"},{content:t[1],role:"assistant"},{content:e.compressedText,role:"user"}]}var V2,X2,Y2,lh,nN=l(()=>{"use strict";jt();Rt();oh();uk();ch();V2=["critical","major","minor"],X2=os.object({problem:os.string(),severity:os.enum(V2),suggestion:os.string().default("")}),Y2=os.object({issues:os.array(X2)}),lh=class{#e;#t;#r;#n;#i;constructor(t,r,n,i){this.#e=t,this.#t=n.reviewGuide,this.#r=n.review,this.#n=r,this.#i=i}async generateClueReviewers(t){return await Promise.all(t.map(async r=>await this.#e.request(async n=>{let i=await VO({chunks:r.chunks,fullMarkup:!0,serialFragments:this.#n}),a=[{content:this.#e.loadSystemPrompt(jO),role:"system"},{content:i,role:"user"}],o=await n(a,{scope:this.#t});return{clueId:r.clueId,label:r.label,reviewerInfo:o.trim(),weight:r.weight}})))}async reviewCompression(t,r,n){let i=await Promise.all(r.map(async o=>await this.#e.request(async s=>{let c=this.#e.loadSystemPrompt(NO,{thread_info:o.reviewerInfo,user_language:this.#i}),d=n[String(o.clueId)],u=Z2({compressedText:t,systemPrompt:c},d);try{return await Ae({messages:u,parse:p=>({rawResponse:JSON.stringify(p),review:{clueId:o.clueId,issues:p.issues.map(m=>({...m,severity:FO(m.severity)})),weight:o.weight}}),responseIntentClassifierPrompt:this.#e.loadSystemPrompt(Me),request:async(p,m,f)=>await s(p,{retryIndex:m,retryMax:f,scope:this.#r,useCache:!1}),schema:Y2})}catch(p){if(!(p instanceof mt))throw p;return st({clueId:o.clueId,component:"editor-review"}).warn({error:p},"Compression reviewer failed to produce valid JSON; treating it as no issues."),{rawResponse:void 0,review:{clueId:o.clueId,issues:[],weight:o.weight}}}}))),a=Object.create(null);for(let o of i)a[String(o.review.clueId)]=o.rawResponse;return{rawResponses:a,reviews:i.map(o=>o.review)}}}});async function au(e){return await new pk(e).run()}function lk(e,t){return e-t}function e8(e){return e.flatMap(t=>t.chunks)}function t8(e){let t=e.document??e.workspace;if(t===void 0)throw new Error("Editor requires a document");return t}var pk,iN=l(()=>{"use strict";OO();WO();sk();KO();JO();uk();rN();nN();pk=class{#e;#t;#r;#n;#i;#a;#o;#s;#c;#l;#u;#d;#m;constructor(t){let r=t8(t);this.#e=t.compressionRatio??.2,this.#t=t.groupId,this.#r=t.llm,this.#i=t.maxClues??10,this.#a=t.maxIterations??5,this.#c=t.scopes,this.#l=r.getSerialFragments(t.serialId),this.#u=t.serialId,this.#d=t.userLanguage,this.#m=r,this.#o=new sh(this.#r,t.scopes.compress,this.#e,this.#d),this.#n=new uh(this.#u,this.#t,{compressionRatio:this.#e,maxIterations:this.#a,...t.logDirPath===void 0?{}:{logDirPath:t.logDirPath}}),this.#s=new lh(this.#r,this.#l,{review:this.#c.review,reviewGuide:this.#c.reviewGuide},this.#d)}async run(){let t=await this.#p(),r=await this.#h(t);if(r.length===0)return"";let n=await _O({document:this.#m,groupId:this.#t,maxClues:this.#i,serialId:this.#u}),i=await this.#f(t);if(i.trim()==="")return"";await this.#n.initialize(n);let a=await dk({chunks:e8(n),segmentStartIndexes:r,serialFragments:this.#l,wrapHighRetention:!0}),o=await this.#s.generateClueReviewers(n),s=Math.floor(i.length*this.#e),c=[],d=Object.create(null),u,p;for(let f=1;f<=this.#a;f+=1){await this.#n.appendIterationHeader(f,p);let w=await this.#o.request({markedText:a,targetLength:s,...u===void 0?{}:{previousCompressedText:u},...p===void 0?{}:{revisionFeedback:p}}),b=ZO(eN(w)),E=tN(w);await this.#n.appendCompressionResult({compressedText:b,thinkingText:E});let N=await this.#s.reviewCompression(b,o,d),C=[...N.reviews],q=qO({compressedText:b,...this.#d===void 0?{}:{userLanguage:this.#d}});q!==void 0&&(C.push(q.review),await this.#n.appendLanguageMismatch({detectedLanguageCode:q.detectedLanguageCode,review:q.review,targetLanguageCode:q.targetLanguageCode,userLanguage:this.#d}));let Q=GO(C);if(c.push({iteration:f,reviews:C,score:Q,text:b}),Q===0)break;if(!(f>=this.#a)){p=UO({llm:this.#r,reviews:C}),u=b;for(let x of Object.keys(N.rawResponses)){let ce=N.rawResponses[x];ce!==void 0&&(d[x]=[b,ce])}}}let m=BO(c);return await this.#n.appendFinalSelection(m,i.length),m.text}async#p(){return(await this.#m.fragmentGroups.listBySerial(this.#u)).filter(t=>t.groupId===this.#t).map(t=>({endSentenceIndex:t.endSentenceIndex,startSentenceIndex:t.startSentenceIndex}))}async#h(t){let r=[...await this.#l.listFragmentIds()].sort(lk),n=new Set;for(let i of t)for(let a=0;a<r.length;a+=1){let o=r[a];if(o===void 0)continue;let s=r[a+1],c=s===void 0?1/0:s-1;o<=i.endSentenceIndex&&c>=i.startSentenceIndex&&n.add(o)}return[...n].sort(lk)}async#f(t){return(await Promise.all(t.map(async n=>({sentences:await this.#b(n.startSentenceIndex,n.endSentenceIndex)})))).flatMap(n=>n.sentences.map(i=>i.text)).join(" ")}async#b(t,r){if(this.#l.listSentencesInRange!==void 0)return await this.#l.listSentencesInRange(t,r);let n=[...await this.#l.listFragmentIds()].sort(lk);return(await Promise.all(n.filter((a,o)=>{let s=n[o+1],c=s===void 0?1/0:s-1;return a<=r&&c>=t}).map(async a=>await this.#l.getFragment(a)))).flatMap(a=>a.sentences.filter((o,s)=>{let c=a.fragmentId+s;return c>=t&&c<=r}))}}});var mk=l(()=>{"use strict";iN()});async function sN(e){let t=await r8(e.fragments);return i8(n8({chunks:e.chunks,edges:e.edges,segmentWordsCounts:t}))}async function r8(e){let t=dN(),r=await e.listFragmentIds();for(let n of r){let i=await e.getFragment(n);t[String(n)]=i.sentences.reduce((a,o)=>a+o.wordsCount,0)}return t}function n8(e){let t=Object.keys(e.segmentWordsCounts).map(Number).sort(cN),r=s8(),n=dN(),i=o8();for(let o of e.chunks){let s=aN(t,o.sentenceId[1]);if(s===void 0)continue;let c=String(s);n[String(o.id)]=o.weight,r[c]===void 0&&(r[c]=[]),r[c]?.push(o.id)}for(let o of e.edges){let s=String(o.fromId),c=String(o.toId);i[s]===void 0&&(i[s]=[]),i[c]===void 0&&(i[c]=[]),i[s]?.push(o),i[c]?.push(o)}let a=a8();for(let o of e.chunks)a[String(o.id)]=o;return t.filter(o=>r[String(o)]!==void 0).map(o=>{let s=r[String(o)]??[],c=0,d=0;for(let u of s){let p=n[String(u)]??0,f=(i[String(u)]??[]).map(b=>({otherChunkId:b.fromId===u?b.toId:b.fromId,weight:b.weight})).map(b=>{let E=a[String(b.otherChunkId)];if(E!==void 0)return{otherSegmentStartIndex:aN(t,E.sentenceId[1]),weight:b.weight}}).filter(b=>b!==void 0&&b.otherSegmentStartIndex!==void 0&&b.otherSegmentStartIndex!==o);if(f.length===0)continue;let w=f.reduce((b,E)=>b+E.weight,0);if(w!==0)for(let b of f){let E=p/w*b.weight;if(b.otherSegmentStartIndex<o){d+=E;continue}c+=E}}return{endIncision:c,startSentenceIndex:o,startIncision:d,wordsCount:e.segmentWordsCounts[String(o)]??0}})}function aN(e,t){let r;for(let n of e){if(n>t)break;r=n}return r}function i8(e){let t=e.flatMap(u=>[u.startIncision,u.endIncision]).filter(u=>u>0).sort(cN);if(t.length===0)return[...e];let r=Math.min(Math.floor(t.length*.98),t.length-1),n=t[r]??0,i=t.filter(u=>u<n);if(i.length===0)return e.map(u=>({...u,endIncision:u.endIncision>0?10:0,startIncision:u.startIncision>0?10:0}));let a=Math.min(...i),o=Math.max(...i),s=a>0?Math.log(a):0,c=o>0?Math.log(o):0,d=c-s===0?1:c-s;return e.map(u=>({...u,endIncision:oN(u.endIncision,{logRange:d,minLog:s,threshold:n}),startIncision:oN(u.startIncision,{logRange:d,minLog:s,threshold:n})}))}function oN(e,t){if(e===0)return 0;if(e>=t.threshold)return 10;let r=e>0?Math.log(e):t.minLog;return Math.max(1,Math.min(10,Math.round(1+(r-t.minLog)/t.logRange*9)))}function cN(e,t){return e-t}function a8(){return Object.create(null)}function o8(){return Object.create(null)}function s8(){return Object.create(null)}function dN(){return Object.create(null)}var uN=l(()=>{"use strict"});function pN(e){if(e.segmentInfos.length===0)return[];let t=e.segmentInfos.map(n=>({count:n.wordsCount,endIncision:n.endIncision,startSentenceIndex:n.startSentenceIndex,startIncision:n.startIncision}));return c8(t,e.groupWordsCount).map((n,i)=>({endSentenceIndex:Math.max(...n),groupId:i,serialId:e.serialId,startSentenceIndex:Math.min(...n)}))}function c8(e,t){return hk(e,t).map(r=>r.map(n=>n.startSentenceIndex))}function hk(e,t){if(e.length===0)return[];if(t<=0)return e.map(o=>[o]);let r=e.reduce((o,s)=>o+s.count,0);if(e.length===1||r<=t)return[[...e]];let n=d8(e,t);if(n===void 0)return lN(e,t);let i=e.slice(0,n+1),a=e.slice(n+1);return i.length===0||a.length===0||i.length===e.length||a.length===e.length?lN(e,t):[...hk(i,t),...hk(a,t)]}function d8(e,t){let r=e.reduce((c,d)=>c+d.count,0),n=0,i,a=0,o=Number.POSITIVE_INFINITY,s=Number.POSITIVE_INFINITY;for(let c=0;c<e.length-1;c+=1){let d=e[c],u=e[c+1];if(d===void 0||u===void 0)continue;n+=d.count;let p=r-n,m=d.endIncision+u.startIncision;if(m<=0)continue;let f=+(n>t)+ +(p>t),w=Math.max(0,n-t)+Math.max(0,p-t);(f<o||f===o&&m>a||f===o&&m===a&&w<s)&&(a=m,i=c,o=f,s=w)}return i}function lN(e,t){let r=[],n=[],i=0;for(let a of e)n.length>0&&i+a.count>t&&(r.push(n),n=[],i=0),n.push(a),i+=a.count,i>=t&&(r.push(n),n=[],i=0);return n.length>0&&r.push(n),r}var mN=l(()=>{"use strict"});async function hN(e){let t=await sN({chunks:e.chunks,edges:e.edges,fragments:e.fragments});return pN({segmentInfos:t,groupWordsCount:e.groupWordsCount,serialId:e.serialId})}var fN=l(()=>{"use strict";uN();mN()});function fk(e,t){let[r,n]=e.sentenceId,[i,a]=t.sentenceId;return r!==i?r-i:n!==a?n-a:e.id-t.id}function gN(e,t){return e.value!==t.value?t.value-e.value:e.leftClusterId!==t.leftClusterId?e.leftClusterId-t.leftClusterId:e.rightClusterId-t.rightClusterId}function ou(e,t){return e-t}var su=l(()=>{"use strict"});function bN(e){let t=gk();for(let r of e)t[String(r.id)]=h8(r);return t}function wN(e){let t=gk();for(let n of e.edges){let i=yN(n.strength);t[String(n.fromId)]=(t[String(n.fromId)]??0)+i,t[String(n.toId)]=(t[String(n.toId)]??0)+i}let r=gk();for(let n of e.edges){let i=yN(n.strength),a=e.chunkWeights[String(n.fromId)]??0,o=e.chunkWeights[String(n.toId)]??0,s=t[String(n.fromId)]??0,c=t[String(n.toId)]??0,d=s===0?0:a*(i/s),u=c===0?0:o*(i/c);r[te(n.fromId,n.toId)]=Math.max(d+u,u8)}return r}function te(e,t){return`${e}:${t}`}function h8(e){return g8(e.retention)+f8(e.importance)}function f8(e){return e===void 0?0:l8[e]??0}function yN(e){return e===void 0?1:p8[e]??1}function g8(e){return e===void 0?0:m8[e]??0}function gk(){return Object.create(null)}var u8,l8,p8,m8,ss=l(()=>{"use strict";X();u8=.1,l8={critical:9,helpful:1,important:3},p8={critical:9,helpful:1,important:3},m8={detailed:9,focused:3,relevant:1,verbatim:27}});var cs=l(()=>{"use strict"});function di(){return Object.create(null)}function SN(){return Object.create(null)}function kN(){return Object.create(null)}function ph(){return Object.create(null)}function EN(){return Object.create(null)}function ds(){return Object.create(null)}function IN(){return Object.create(null)}function yk(){return Object.create(null)}var cu=l(()=>{"use strict"});function du(e){switch(e){case"critical":return 9;case"important":return 3;case"helpful":return 1;default:return 1}}var bk=l(()=>{"use strict";cs()});function uu(e,t,r){let n=e[String(t)]??[];n.includes(r)||(e[String(t)]=[...n,r])}function xN(e,t){let r=SN(),n=kN(),i=yk(),a=yk(),o=[...e].map(c=>c.id).sort(ou),s=ph();for(let c of e)r[String(c.id)]=c;for(let c of t){if(r[String(c.fromId)]===void 0||r[String(c.toId)]===void 0)continue;let d=te(c.fromId,c.toId);n[d]=c,i[String(c.toId)]===void 0&&(i[String(c.toId)]=[]),a[String(c.fromId)]===void 0&&(a[String(c.fromId)]=[]),i[String(c.toId)]?.push(c),a[String(c.fromId)]?.push(c),uu(s,c.fromId,c.toId),uu(s,c.toId,c.fromId)}return{chunksById:r,edgeByKey:n,fingerprintEdgeWeightsByKey:b8(r,t),incomingEdgesByChunkId:i,outgoingEdgesByChunkId:a,sortedChunkIds:o,undirectedAdjacentChunkIdsByChunkId:s}}function b8(e,t){let r=ds();for(let i of t){if(e[String(i.fromId)]===void 0||e[String(i.toId)]===void 0)continue;let a=du(i.strength);r[String(i.fromId)]=(r[String(i.fromId)]??0)+a,r[String(i.toId)]=(r[String(i.toId)]??0)+a}let n=ds();for(let i of t){let a=e[String(i.fromId)],o=e[String(i.toId)];if(a===void 0||o===void 0)continue;let s=du(i.strength),c=r[String(i.fromId)]??0,d=r[String(i.toId)]??0,u=c===0?0:a.weight*(s/c),p=d===0?0:o.weight*(s/d),m=Math.max(u+p,.1);n[te(i.fromId,i.toId)]=m,n[te(i.toId,i.fromId)]=m}return n}var wk=l(()=>{"use strict";ss();cs();su();cu();bk()});function Sk(e){let t=[...e.chunkIds].sort(ou),r=di(),n=ph(),i=di(),a=[];for(let o of t)r[String(o)]=!0;for(let o of e.edges)r[String(o.fromId)]!==!0||r[String(o.toId)]!==!0||(uu(n,o.fromId,o.toId),uu(n,o.toId,o.fromId));for(let o of t){if(i[String(o)]===!0)continue;let s=[o],c=[];for(i[String(o)]=!0;s.length>0;){let d=s.pop();if(d!==void 0){c.push(d);for(let u of n[String(d)]??[])i[String(u)]!==!0&&(i[String(u)]=!0,s.push(u))}}c.sort(ou),a.push(c)}return a.sort((o,s)=>{let c=o[0]??Number.POSITIVE_INFINITY,d=s[0]??Number.POSITIVE_INFINITY;return c-d}),a}function vN(e){let t=e.sortedChunkIds[0];if(t===void 0)return!0;let r=di(),n=[t];for(r[String(t)]=!0;n.length>0;){let i=n.pop();if(i!==void 0)for(let a of e.undirectedAdjacentChunkIdsByChunkId[String(i)]??[])r[String(a)]!==!0&&(r[String(a)]=!0,n.push(a))}return e.sortedChunkIds.every(i=>r[String(i)]===!0)}var kk=l(()=>{"use strict";su();wk();cu()});function CN(e){let t=EN();for(let r of e.sortedChunkIds){let n=ds(),i=[r],a=di();for(n[String(r)]=1,a[String(r)]=!0;i.length>0;){let s=ds();for(let c of i){let d=n[String(c)]??0,u=0,p=[];for(let m of e.incomingEdgesByChunkId[String(c)]??[]){if(a[String(m.fromId)]===!0)continue;let f=e.fingerprintEdgeWeightsByKey[te(m.fromId,c)]??e.fingerprintEdgeWeightsByKey[te(c,m.fromId)]??1;p.push([m.fromId,f]),u+=f}for(let m of e.outgoingEdgesByChunkId[String(c)]??[]){if(a[String(m.toId)]===!0)continue;let f=e.fingerprintEdgeWeightsByKey[te(c,m.toId)]??e.fingerprintEdgeWeightsByKey[te(m.toId,c)]??1;p.push([m.toId,f]),u+=f}if(!(u<=0))for(let[m,f]of p){let w=d*(f/u)*.75;s[String(m)]=(s[String(m)]??0)+w}}i=Object.keys(s).map(Number);for(let c of i)n[String(c)]=s[String(c)]??0,a[String(c)]=!0}let o=Object.values(n).reduce((s,c)=>s+c,0);if(o>0)for(let s of Object.keys(n))n[s]=(n[s]??0)/o;t[String(r)]=n}return t}function TN(e){let t=Object.keys(e)[0];if(t===void 0)return;let r=Object.keys(e[t]??{}).sort();for(let[n,i]of Object.entries(e)){let a=Object.keys(i).sort();if(a.length!==r.length||a.some((o,s)=>o!==r[s]))throw new Error(`Fingerprint structure mismatch for chunk ${n}`)}}var RN=l(()=>{"use strict";ss();cs();cu()});function Ek(e,t,r,n){let i=n===void 0?new Map(e.sortedChunkIds.map(c=>[c,[c]])):new Map([...n.entries()].map(([c,d])=>[c,[...d]])),a=IN(),o=[],s=di();for(let[c,d]of i.entries())for(let u of d)a[String(u)]=c;for(let c of i.keys())for(let d of i.keys()){if(c>=d)continue;let u=i.get(c),p=i.get(d);u===void 0||p===void 0||_N(e,u,p)&&(o.push(ON(c,d,LN(e,u,p,t))),s[NN(c,d)]=!0)}for(;o.length>0;){o.sort(gN);let c=o.shift();if(c===void 0)continue;let d=a[String(c.leftClusterId)],u=a[String(c.rightClusterId)];if(d===void 0||u===void 0||d===u)continue;let p=i.get(d),m=i.get(u);if(p===void 0||m===void 0||!_N(e,p,m)||r.enableBonus&&p.length>=2&&m.length>=2||AN(e,p)+AN(e,m)>r.snakeWordsCount)continue;p.push(...m),i.delete(u);for(let w of m)a[String(w)]=d;let f=new Set;for(let w of p)for(let b of e.undirectedAdjacentChunkIdsByChunkId[String(w)]??[])f.add(b);for(let w of f){let b=a[String(w)];if(b===void 0||b===d)continue;let E=i.get(b);if(E===void 0)continue;let N=NN(d,b);s[N]!==!0&&(o.push(ON(Math.min(d,b),Math.max(d,b),LN(e,p,E,t))),s[N]=!0)}}return i}function _N(e,t,r){for(let n of t)for(let i of r)if(e.edgeByKey[te(n,i)]!==void 0||e.edgeByKey[te(i,n)]!==void 0)return!0;return!1}function AN(e,t){return t.reduce((r,n)=>r+(e.chunksById[String(n)]?.wordsCount??0),0)}function LN(e,t,r,n){let i=0,a=Number.POSITIVE_INFINITY;for(let o of t)for(let s of r){let c=e.edgeByKey[te(o,s)]??e.edgeByKey[te(s,o)];if(c===void 0)continue;i+=1;let d=n[String(o)]??{},u=n[String(s)]??{},p=0;for(let N of Object.keys(d)){let C=(d[N]??0)-(u[N]??0);p+=C*C}let m=du(c.strength),f=e.chunksById[String(o)],w=e.chunksById[String(s)];f?.retention!==void 0&&w?.retention!==void 0&&(m+=3),f?.importance!==void 0&&w?.importance!==void 0&&(m+=3);let E=(1-Math.sqrt(p)/2)*m;a=Math.min(a,E)}return i===0?Number.NEGATIVE_INFINITY:a}function ON(e,t,r){return{leftClusterId:e,rightClusterId:t,value:r}}function NN(e,t){return e<t?te(e,t):te(t,e)}var jN=l(()=>{"use strict";ss();cs();su();cu();bk()});function Ik(e){if(e.chunks.length===0)return[];let t=xN(e.chunks,e.edges);if(!vN(t))throw new Error("Snake detector requires a weakly connected component input");let r=e.snakeWordsCount??280;if(e.chunks.reduce((s,c)=>s+c.wordsCount,0)<=r)return[[...e.chunks].sort(fk).map(s=>s.id)];let i=CN(t);TN(i);let a=Ek(t,i,{enableBonus:!0,snakeWordsCount:r},void 0);return[...Ek(t,i,{enableBonus:!1,snakeWordsCount:r},a).values()].map(s=>[...s].sort((c,d)=>{let u=t.chunksById[String(c)],p=t.chunksById[String(d)];return u===void 0||p===void 0?c-d:fk(u,p)}))}var DN=l(()=>{"use strict";kk();cs();su();RN();wk();jN()});var MN=l(()=>{"use strict";DN();kk()});function FN(e){let t=I8(),r=WN();for(let[i,a]of e.snakes.entries())for(let o of a)t[String(o)]=i;for(let i of e.edges){let a=t[String(i.fromId)],o=t[String(i.toId)];if(a===void 0||o===void 0||a===o)continue;let s=te(a,o);r[s]=(r[s]??0)+i.weight}let n=WN();for(let[i,a]of Object.entries(r)){let[o="",s=""]=i.split(":"),c=Number(o),d=Number(s),u=e.snakes[c]?.[0],p=e.snakes[d]?.[0];if(u===void 0||p===void 0)continue;let m=e.chunksById[String(u)],f=e.chunksById[String(p)];if(m===void 0||f===void 0)continue;let w=k8(m,f)<0?te(c,d):te(d,c);n[w]=(n[w]??0)+a}return Object.keys(n).sort(E8).map(i=>{let[a="",o=""]=i.split(":");return{fromSnakeIndex:Number(a),toSnakeIndex:Number(o),weight:n[i]??0}})}function k8(e,t){let[r,n]=e.sentenceId,[i,a]=t.sentenceId;return r!==i?r-i:n!==a?n-a:e.id-t.id}function E8(e,t){let[r="",n=""]=e.split(":"),[i="",a=""]=t.split(":"),o=Number(r),s=Number(i);return o!==s?o-s:Number(n)-Number(a)}function WN(){return Object.create(null)}function I8(){return Object.create(null)}var GN=l(()=>{"use strict";ss()});var UN=l(()=>{"use strict";MN();GN()});function v8(e,t){let[r="",n=""]=e.split(":"),[i="",a=""]=t.split(":"),o=Number(r),s=Number(i);return o!==s?o-s:Number(n)-Number(a)}function vk(e,t){return e-t}function $N(){return Object.create(null)}function C8(){return Object.create(null)}function T8(e){let t=$N(),r=P8(),n=_8(),i=[];for(let c of e.sentenceGroups)c.serialId===e.serialId&&i.push(c);for(let c of e.chunks){let d=xk(i,c.sentenceId[1]);t[String(c.id)]=c,d!==void 0&&(r[String(d)]===void 0&&(r[String(d)]=[]),r[String(d)]?.push(c.id))}for(let c of e.edges){let d=t[String(c.fromId)],u=t[String(c.toId)];if(d===void 0||u===void 0)continue;let p=xk(i,d.sentenceId[1]),m=xk(i,u.sentenceId[1]);p===void 0||m===void 0||p===m&&(n[String(p)]===void 0&&(n[String(p)]=[]),n[String(p)]?.push(c))}let a=[],o=[],s=[];for(let c of A8(r)){let d=[...r[String(c)]??[]].sort(vk),u=n[String(c)]??[],p=[];for(let f of Sk({chunkIds:d,edges:u})){let w=R8();for(let b of f)w[String(b)]=!0;p.push(...Ik({chunks:f.map(b=>t[String(b)]).filter(b=>b!==void 0),edges:u.filter(b=>w[String(b.fromId)]===!0&&w[String(b.toId)]===!0),snakeWordsCount:x8}))}let m=a.length;for(let[f,w]of p.entries()){let b=w[0],E=w[w.length-1],N=b===void 0?void 0:t[String(b)],C=E===void 0?void 0:t[String(E)];if(N===void 0||C===void 0)continue;let q=m+f;a.push({firstLabel:N.label,groupId:c,lastLabel:C.label,localSnakeId:f,size:w.length,weight:w.reduce((Q,x)=>Q+(t[String(x)]?.weight??0),0),wordsCount:w.reduce((Q,x)=>Q+(t[String(x)]?.wordsCount??0),0)});for(let[Q,x]of w.entries())o.push({chunkId:x,position:Q,snakeIndex:q})}s.push(...FN({chunksById:t,edges:u,snakes:p}).map(f=>({fromSnakeIndex:m+f.fromSnakeIndex,toSnakeIndex:m+f.toSnakeIndex,weight:f.weight})))}return{snakeChunks:o,snakeEdges:s,snakes:a}}function xk(e,t){return e.find(r=>t>=r.startSentenceIndex&&t<=r.endSentenceIndex)?.groupId}function R8(){return Object.create(null)}function P8(){return Object.create(null)}function _8(){return Object.create(null)}function A8(e){return Object.keys(e).map(Number).sort(vk)}var x8,lu,BN=l(()=>{"use strict";fN();UN();ss();x8=280,lu=class{#e=[];#t=$N();#r;#n=[];#i=C8();#a;#o;constructor(t,r,n){this.#r=t,this.#a=n,this.#o=r}accept(t){for(let r of t.chunks)this.#l(r);if(t.importanceAnnotations!==void 0)for(let r of t.importanceAnnotations){let n=this.#t[String(r.chunkId)];n!==void 0&&(this.#t[String(r.chunkId)]={...n,importance:r.importance})}for(let r of t.edges)this.#u(r)}async finalize(){let t=this.#s(),r=this.#c(),n=bN(t),i=wN({chunkWeights:n,edges:r}),a=t.map(p=>({...p,weight:n[String(p.id)]??0})),o=r.map(p=>({...p,weight:i[te(p.fromId,p.toId)]??0})),s=await hN({chunks:a,edges:o,fragments:this.#r.getSerialFragments(this.#o),groupWordsCount:this.#a,serialId:this.#o}),c=T8({chunks:a,edges:o,sentenceGroups:s,serialId:this.#o});await this.#r.serials.ensure(this.#o);let d=new Map;for(let p of a){let m=await this.#r.chunks.create({content:p.content,generation:p.generation,label:p.label,sentenceId:p.sentenceId,sentenceIds:p.sentenceIds,weight:p.weight,wordsCount:p.wordsCount,...p.importance===void 0?{}:{importance:p.importance},...p.retention===void 0?{}:{retention:p.retention}});d.set(p.id,m.id)}for(let p of o){let m=d.get(p.fromId),f=d.get(p.toId);m===void 0||f===void 0||await this.#r.readingEdges.save({...p,fromId:m,toId:f})}await this.#r.fragmentGroups.saveMany(s);let u=[];for(let p of c.snakes)u.push(await this.#r.snakes.create({firstLabel:p.firstLabel,groupId:p.groupId,lastLabel:p.lastLabel,localSnakeId:p.localSnakeId,serialId:this.#o,size:p.size,weight:p.weight,wordsCount:p.wordsCount}));for(let p of c.snakeChunks){let m=u[p.snakeIndex];m!==void 0&&await this.#r.snakeChunks.save({chunkId:d.get(p.chunkId)??p.chunkId,position:p.position,snakeId:m})}for(let p of c.snakeEdges){let m=u[p.fromSnakeIndex],f=u[p.toSnakeIndex];m===void 0||f===void 0||await this.#r.snakeEdges.save({fromSnakeId:m,toSnakeId:f,weight:p.weight})}}#s(){return[...this.#e].sort(vk).map(t=>this.#t[String(t)]).filter(t=>t!==void 0)}#c(){return[...this.#n].sort(v8).map(t=>this.#i[t]).filter(t=>t!==void 0)}#l(t){let r=String(t.id);this.#t[r]===void 0&&this.#e.push(t.id);let n=t.importance,i=t.retention;this.#t[r]={content:t.content,generation:t.generation,id:t.id,label:t.label,sentenceId:t.sentenceId,sentenceIds:[...t.sentenceIds],wordsCount:t.wordsCount,weight:0,...n===void 0?{}:{importance:n},...i===void 0?{}:{retention:i}}}#u(t){let r=te(t.fromId,t.toId);this.#i[r]===void 0&&this.#n.push(r);let n=t.strength;this.#i[r]={fromId:t.fromId,toId:t.toId,weight:0,...n===void 0?{}:{strength:n}}}}});var HN=l(()=>{"use strict";BN()});function qN(e){let t=e.document??e.workspace;if(t===void 0)throw new Error("SerialGeneration requires a document");return t}async function Ck(e,t){let r=await e.serials.getById(t);if(r===void 0)throw new Error(`Chapter ${t} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);return r}var Tk=l(()=>{"use strict"});function KN(){return Object.create(null)}function Rk(e,t,r,n){r.accept(n),e.push(...n.chunks);for(let i of n.edges)L8(t,i.fromId,i.toId)}function L8(e,t,r){let n=e[String(t)]??[];n.includes(r)||(e[String(t)]=[...n,r].sort(O8))}function O8(e,t){return e-t}var JN=l(()=>{"use strict"});async function Xt(e,t,r,n={}){await e.getSerialFragments(t).writeTextStream(await vO(r),{...n.segmenter===void 0?{}:{segmenter:n.segmenter}}),await e.serials.bumpRevision(t)}var Pk=l(()=>{"use strict";ah()});var us,_k,hh=l(()=>{"use strict";us=class{#e;#t;#r;constructor(t,r,n){this.#e=r,this.#t=n,this.#r=new _k(t,r)}get id(){return this.#e}getSummary(){return this.#t}getTopology(){return this.#r}},_k=class{#e;#t;#r;#n;#i;#a;#o;constructor(t,r){this.#e=t.chunks,this.#t=t.fragmentGroups,this.#r=t.readingEdges,this.#n=r,this.#i=t.snakeChunks,this.#a=t.snakeEdges,this.#o=t.snakes}async listChunks(){return await this.#e.listBySerial(this.#n)}async listEdges(){return await this.#r.listBySerial(this.#n)}async listGroups(){return await this.#t.listBySerial(this.#n)}async listSnakeChunks(t){let r=await this.#o.getById(t);if(r===void 0||r.serialId!==this.#n)throw new Error(`Snake ${t} does not belong to this serial`);return await this.#i.listBySnake(t)}async listSnakeEdges(){return await this.#a.listBySerial(this.#n)}async listSnakes(){return await this.#o.listBySerial(this.#n)}}});var hn,zN=l(()=>{"use strict";nc();oc();mk();nh();HN();ah();ih();Tk();JN();Pk();hh();hn=class{#e=320;#t;#r=new xt(1);#n;#i;#a;#o=new xt(1);#s;#c;#l=1;constructor(t){let r=qN(t);this.#t=r.fragmentGroups,this.#s=t.llm,this.#n=t.logDirPath,this.#i=r.serials,this.#c=t.segmenter,this.#a=r}async generate(t,r,n){let i=await this.#f();return await Xt(this.#a,i,t,{...this.#c===void 0?{}:{segmenter:this.#c}}),await this.#u(i,r,n)}async create(t,r){return await this.generate(t,r)}async generateInto(t,r,n,i){return await this.#b(t),await Xt(this.#a,t,r,{...this.#c===void 0?{}:{segmenter:this.#c}}),await this.#u(t,n,i)}async buildTopologyInto(t,r,n){await this.#p(t,r.extractionPrompt,r.userLanguage,n)}async buildSummary(t,r={}){let n=await this.#m(t,r.userLanguage);return new us(this.#a,t,n)}async#u(t,r,n){await this.#p(t,r.extractionPrompt,r.userLanguage,n);let i=await this.#m(t,r.userLanguage);return await n?.complete(),new us(this.#a,t,i)}async#d(){return await this.#r.use(()=>{let t=this.#l;return this.#l+=1,t})}async#m(t,r){if(!(await Ck(this.#a,t)).topologyReady)throw new Error(`Serial ${t} is not ready for summary`);let i=await this.#a.readSummary(t);if(i!==void 0)return i;let a=this.#g(t),o=await a.listFragmentIds();if(o.length<=1){let u=await CO(a,o);return await this.#o.use(async()=>await this.#a.writeSummary(t,u)),u}let s=await this.#t.listGroupIdsForSerial(t),c=[];for(let u of s){let p=await au({...this.#y({groupId:u,serialId:t,userLanguage:r})});p.trim()!==""&&c.push(p)}let d=c.join(`
|
|
2296
|
+
|
|
2297
|
+
`);return await this.#o.use(async()=>await this.#a.writeSummary(t,d)),d}async#p(t,r,n,i){let a=new nu({attention:{capacity:7,generationDecayFactor:.5,idGenerator:async()=>await this.#d()},extractionGuidance:r,llm:this.#s,scopes:Vg,sentenceTextSource:this.#a,...this.#c===void 0?{}:{segmenter:this.#c},...n===void 0?{}:{userLanguage:n}}),o=new lu(this.#a,t,3840),s=[],c=KN(),d=this.#a.getSerialFragments(t);for(let u of await xO(d,this.#e)){let p=ik(u.sentences);await this.#h({allChunks:s,fragment:{startSentenceIndex:u.startSentenceIndex,sentences:u.sentences},reader:a,serialId:t,successorIdsByChunkId:c,topology:o}),await i?.advance(p)}await this.#o.use(async()=>{await o.finalize(),await this.#i.setTopologyReady(t),await this.#i.bumpRevision(t)})}async#h(t){let r=t.fragment.sentences.map((o,s)=>({sentenceId:[t.serialId,t.fragment.startSentenceIndex+s],text:o.text,wordsCount:o.wordsCount})),n=r.map(o=>o.text).join(" "),i=await t.reader.extractUserFocused({sentences:r,text:n}),a=await t.reader.extractBookCoherence({sentences:r,text:n,userFocusedChunks:i.delta.chunks});Rk(t.allChunks,t.successorIdsByChunkId,t.topology,i.delta),Rk(t.allChunks,t.successorIdsByChunkId,t.topology,a),t.reader.completeFragment({allChunks:t.allChunks,getSuccessorChunkIds:o=>t.successorIdsByChunkId[String(o)]??[]})}async#f(){return await this.#r.use(async()=>await this.#i.create())}async#b(t){await this.#r.use(async()=>await this.#i.createWithId(t))}#g(t){return this.#a.getSerialFragments(t)}#y(t){return{compressionRatio:.2,groupId:t.groupId,llm:this.#s,maxClues:10,maxIterations:5,scopes:ac,serialId:t.serialId,document:this.#a,...this.#n===void 0?{}:{logDirPath:this.#n},...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}}}}});var QN=l(()=>{"use strict";Tk();hh()});var VN=l(()=>{"use strict";TO();zN();ih();QN();Pk();hh()});var ls=l(()=>{"use strict";VN()});async function ZN(e){return await e.adapter.openSession(e.path,async t=>await G8(t,e))}async function G8(e,t){await B8(t.document);let r=await e.readMeta(),n=await e.readCover(),i=await e.readSections(),a=[],o={nextSerialId:await t.document.peekNextSerialId()},s={version:Xe,items:[...$8({existingKeys:new Set,fallbackTitle:r.title,plannedSections:a,sections:i,serialIdAllocator:o})]},c=t.targetStage??"summarized",d=c==="planned"||c==="sourced"?void 0:new hn({document:t.document,llm:K8(t.llm,c),...t.logDirPath===void 0?{}:{logDirPath:t.logDirPath},...t.segmenter===void 0?{}:{segmenter:t.segmenter}});if(t.digestProgressTracker!==void 0&&c!=="planned"){let p=await H8(a,{...t.segmenter===void 0?{}:{segmenter:t.segmenter}});await t.digestProgressTracker.discoverSerials(p)}let u=await U8(a,{document:t.document,extractionPrompt:t.extractionPrompt,...d===void 0?{}:{generation:d},serialConcurrency:q8(t.llm),targetStage:c,...t.digestProgressTracker===void 0?{}:{digestProgressTracker:t.digestProgressTracker},...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}});return await t.document.openSession(async p=>{await p.writeBookMeta(r),await p.writeToc(s),n!==void 0&&await p.writeCover(n)}),{cover:n,meta:r,serials:u,toc:s}}async function U8(e,t){if(t.targetStage==="planned")return await t.document.openSession(async i=>{for(let a of e)await i.serials.createWithId(a.serialId)}),e.map(i=>({id:i.serialId}));let r=new xt(t.serialConcurrency);return[...await Promise.all(e.map(async i=>await r.use(async()=>{let a=t.document.createContext();a.ownSerial(i.serialId);try{let o=t.digestProgressTracker?.createSerialTracker({id:i.serialId}),s=await a.run(async()=>t.targetStage==="sourced"?(await t.document.serials.createWithId(i.serialId),await Xt(t.document,i.serialId,await i.section.open()),{id:i.serialId}):t.targetStage==="graphed"?(await t.document.serials.createWithId(i.serialId),await Xt(t.document,i.serialId,await i.section.open()),await YN(t.generation,t.targetStage).buildTopologyInto(i.serialId,{extractionPrompt:t.extractionPrompt,...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}},o),{id:i.serialId}):await YN(t.generation,t.targetStage).generateInto(i.serialId,await i.section.open(),{extractionPrompt:t.extractionPrompt,...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}},o));return a.complete(),s}finally{await a.dispose()}})))].sort((i,a)=>i.id-a.id)}function $8(e){return e.sections.map((t,r)=>ej({fallbackTitle:e.sections.length===1?e.fallbackTitle:void 0,existingKeys:e.existingKeys,indexPath:[r],plannedSections:e.plannedSections,section:t,serialIdAllocator:e.serialIdAllocator}))}function ej(e){let t=e.section.hasContent?e.serialIdAllocator.nextSerialId++:void 0,r=e.section.children.map((a,o)=>ej({existingKeys:e.existingKeys,fallbackTitle:void 0,indexPath:[...e.indexPath,o],plannedSections:e.plannedSections,section:a,serialIdAllocator:e.serialIdAllocator}));t!==void 0&&e.plannedSections.push({section:e.section,serialId:t});let n=XN(e.section.title)??XN(e.fallbackTitle),i=An(e.existingKeys);return e.existingKeys.add(i),{key:i,...n===void 0?{}:{title:n},...t===void 0?{}:{serialId:t},children:r}}function XN(e){let t=e?.trim();return t===void 0||t===""?void 0:t}async function B8(e){let[t,r,n,i]=await Promise.all([e.readBookMeta(),e.readCover(),e.readToc(),e.serials.listIds()]);if(t!==void 0)throw new Error("Archive metadata already exists");if(r!==void 0)throw new Error("Document cover already exists");if(n!==void 0)throw new Error("Document TOC already exists");if(i.length>0)throw new Error("Document already contains serials")}async function H8(e,t){if(e.every(n=>n.section.wordsCount!==void 0))return e.map(n=>({id:n.serialId,title:n.section.title?.trim()||void 0,words:n.section.wordsCount??0}));let r=[];for(let n of e)r.push({id:n.serialId,title:n.section.title?.trim()||void 0,...await nk({...t.segmenter===void 0?{}:{segmenter:t.segmenter},stream:await n.section.open()})});return r}function q8(e){return typeof e!="object"||e===null||!("config"in e)||typeof e.config!="object"||e.config===null||!("concurrent"in e.config)||typeof e.config.concurrent!="number"||!Number.isInteger(e.config.concurrent)||e.config.concurrent<1?1:e.config.concurrent}function K8(e,t){if(e===void 0)throw new Error(`LLM is required to import source to ${t}.`);return e}function YN(e,t){if(e===void 0)throw new Error(`Internal error: serial generation is required for ${t}.`);return e}var tj=l(()=>{"use strict";$r();nc();ls();ji()});import{mkdir as J8,writeFile as z8}from"fs/promises";import{dirname as Q8}from"path";async function Ak(e){let t=await e.document.openSession(async r=>await V8(r));await J8(Q8(e.path),{recursive:!0}),await z8(e.path,t,"utf8")}async function V8(e){let t=await e.readToc();if(t===void 0)throw new Error("Document TOC is missing");let r=await rj(e,t.items);return r.length===0?"":`${r.join(`
|
|
2298
|
+
|
|
2299
|
+
`)}
|
|
2300
|
+
`}async function rj(e,t){let r=[];for(let n of t){let i=await X8(e,n);i!==void 0&&r.push(i)}return r}async function X8(e,t){let r=[t.title?.trim()].filter(i=>i!==void 0&&i!=="");if(t.serialId!==void 0){let i=await e.readSummary(t.serialId);if(i===void 0)throw new Error(`Chapter ${t.serialId} summary is missing. Run \`wg wikg://local/job add --input <chapter-uri> --task reading-summary --accept-cost\` before export, or inspect the archive with \`wg <archive-uri>/chapter/tree get\`.`);i.trim()!==""&&r.push(i.trim())}let n=await rj(e,t.children);if(!(r.length===0&&n.length===0))return[...r,...n].join(`
|
|
2301
|
+
|
|
2302
|
+
`)}var nj=l(()=>{"use strict"});function fh(e){return e.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'")}function gh(e){let t=e?.trim();return t===void 0||t===""?"und":t}var yh=l(()=>{"use strict"});function bh(){return ij??=Ri(Mr()),ij}function aj(e){return bh().render("output/epub/cover.xhtml",e)}function oj(e){return bh().render("output/epub/nav.xhtml",e)}function sj(e){return bh().render("output/epub/package.opf.xml",e)}function Lk(e){return bh().render("output/epub/section.xhtml",e)}var ij,pu=l(()=>{"use strict";Hl();Ul()});import{createWriteStream as Y8}from"fs";import{dirname as Z8,extname as e3}from"path";import{finished as cj}from"stream/promises";import{mkdir as t3}from"fs/promises";import{ZipFile as r3}from"yazl";async function dj(e,t){await t3(Z8(e),{recursive:!0});let r=new r3;r.addBuffer(Buffer.from("application/epub+zip"),"mimetype",{compress:!1}),r.addBuffer(Buffer.from(n3,"utf8"),"META-INF/container.xml"),r.addBuffer(Buffer.from(t.packageOpf,"utf8"),"OEBPS/package.opf"),r.addBuffer(Buffer.from(t.navXhtml,"utf8"),"OEBPS/nav.xhtml");for(let o of t.sections)r.addBuffer(Buffer.from(o.xhtml,"utf8"),`OEBPS/${o.href}`);if(t.cover!==void 0){let o=Ok(t.cover),s=gh(t.meta.language);r.addBuffer(Buffer.from(t.cover.data),`OEBPS/${o}`),r.addBuffer(Buffer.from(i3(t.meta,o,s),"utf8"),"OEBPS/text/cover.xhtml")}r.end();let n=Y8(e),i=cj(n),a=cj(r.outputStream);r.outputStream.pipe(n),await Promise.all([i,a])}function Ok(e){return`images/cover${a3(e)}`}function i3(e,t,r){let n=e.title?.trim()||"Untitled";return aj({coverImageHref:t,language:r,title:n})}function a3(e){let t=e3(e.path).toLowerCase();if(t!=="")return t;switch(e.mediaType){case"image/gif":return".gif";case"image/jpeg":return".jpg";case"image/png":return".png";case"image/svg+xml":return".svg";case"image/webp":return".webp";default:return".bin"}}var n3,Nk=l(()=>{"use strict";yh();pu();n3=`<?xml version="1.0" encoding="UTF-8"?>
|
|
2303
|
+
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
|
2304
|
+
<rootfiles>
|
|
2305
|
+
<rootfile full-path="OEBPS/package.opf" media-type="application/oebps-package+xml"/>
|
|
2306
|
+
</rootfiles>
|
|
2307
|
+
</container>
|
|
2308
|
+
`});function uj(e,t){let r=e.title?.trim()||"Untitled";return{href:"text/section-1.xhtml",id:"section-1",title:r,xhtml:Lk({language:t,paragraphs:[],title:r})}}function lj(e,t,r,n){let i=r?.trim()||`Section ${e}`;return{href:`text/serial-${e}.xhtml`,id:`serial-${e}`,title:i,xhtml:Lk({language:t,paragraphs:o3(n.trim()),title:i})}}function o3(e){return e.split(/\n\s*\n/gu).map(t=>t.replace(/\s*\n\s*/gu," ").trim()).filter(t=>t!=="")}var pj=l(()=>{"use strict";pu()});function jk(e,t){return e.map(r=>({children:jk(r.children,t),href:r.serialId===void 0?void 0:t.get(r.serialId)?.href,title:r.title?.trim()||`Section ${r.serialId??1}`}))}function mj(e,t,r){let n=e.title?.trim()||"Untitled";return oj({itemsMarkup:s3(r),language:t,title:n})}function s3(e){return e.length===0?"<ol></ol>":`<ol>
|
|
2309
|
+
${e.map(t=>hj(t,1)).join(`
|
|
2310
|
+
`)}
|
|
2311
|
+
</ol>`}function hj(e,t){let r=" ".repeat(t),n=e.href===void 0?`<span>${fh(e.title)}</span>`:`<a href="${fh(e.href)}">${fh(e.title)}</a>`,i=e.children.length===0?"":`
|
|
2312
|
+
${r} <ol>
|
|
2313
|
+
${e.children.map(a=>hj(a,t+2)).join(`
|
|
2314
|
+
`)}
|
|
2315
|
+
${r} </ol>`;return`${r}<li>${n}${i}</li>`}var fj=l(()=>{"use strict";yh();pu()});function gj(e){let t=e.meta.identifier?.trim()||`urn:uuid:${crypto.randomUUID()}`,r=e.meta.title?.trim()||"Untitled";return sj({authors:e.meta.authors,coverImageHref:e.coverImageHref,coverMediaType:e.coverMediaType,coverPageHref:e.coverPageHref,description:e.meta.description,identifier:t,language:e.language,modifiedAt:e.modifiedAt,publishedAt:e.meta.publishedAt,publisher:e.meta.publisher,sections:e.sections.map(n=>({href:n.href,id:n.id})),title:r,version:Dk})}var Dk,Mk=l(()=>{"use strict";pu();Dk="3.0"});async function yj(e){let[t,r,n]=await Promise.all([e.readBookMeta(),e.readToc(),e.readCover()]);if(t===void 0)throw new Error("Archive metadata is missing");if(r===void 0)throw new Error("Document TOC is missing");let i=new Map,a=gh(t.language),o=await bj(e,a,r.items,i),s=jk(r.items,i);o.length===0&&o.push(uj(t,a));let c=n===void 0?void 0:Ok(n),d=n===void 0?void 0:"text/cover.xhtml";return{cover:n,meta:t,navXhtml:mj(t,a,s),packageOpf:gj({coverImageHref:c,coverMediaType:n?.mediaType,coverPageHref:d,language:a,meta:t,modifiedAt:new Date().toISOString().replace(/\.\d{3}Z$/u,"Z"),sections:o}),sections:o}}async function bj(e,t,r,n){let i=[];for(let a of r){if(a.serialId!==void 0){let o=await e.readSummary(a.serialId);if(o===void 0)throw new Error(`Chapter ${a.serialId} summary is missing. Run \`wg wikg://local/job add --input <chapter-uri> --task reading-summary --accept-cost\` before export, or inspect the archive with \`wg <archive-uri>/chapter/tree get\`.`);let s=lj(a.serialId,t,a.title,o);n.set(a.serialId,s),i.push(s)}i.push(...await bj(e,t,a.children,n))}return i}var wj=l(()=>{"use strict";Nk();pj();fj();Mk();yh()});async function Wk(e){let t=await e.document.openSession(async r=>await yj(r));await dj(e.path,t)}var Sj=l(()=>{"use strict";Nk();wj();Mk()});var kj=l(()=>{"use strict";nj();Sj()});function Ej(e,t){return e.serialId===t||e.children.some(r=>Ej(r,t))}async function c3(e){d3(e)&&await e.flush()}function d3(e){return"flush"in e&&typeof e.flush=="function"}async function Ij(e,t,r=[]){let n=[];for(let i of t){let a=i.title?.trim()||`Chapter ${i.serialId??"group"}`,o=[...r,a];i.serialId!==void 0&&await e.readSummary(i.serialId)!==void 0&&n.push({fragmentCount:(await e.getSerialFragments(i.serialId).listFragmentIds()).length,serialId:i.serialId,title:a,tocPath:o}),n.push(...await Ij(e,i.children,o))}return n}var fn,wh=l(()=>{"use strict";kj();xr();fn=class{#e;#t;constructor(t,r){this.#e=t,this.#t=r}async exportEpub(t){await Wk({document:this.#e,path:t})}async exportText(t){await Ak({document:this.#e,path:t})}async readCover(){return await this.#e.readCover()}async readMeta(){return await this.#e.readBookMeta()}async readToc(){return await this.#e.readToc()}async readArchiveFormatVersion(){return await Wc(this.#t)}async readChapterStage(t){return await this.#e.openSession(async r=>{let n=await r.readToc();if(n===void 0)throw new Error("Document TOC is missing");if(!n.items.some(o=>Ej(o,t)))throw new Error(`Chapter ${t} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);return await r.readSummary(t)!==void 0?"summarized":(await r.serials.getById(t))?.topologyReady===!0?"graphed":(await r.getSerialFragments(t).listFragmentIds()).length>0?"sourced":"planned"})}async listSerials(){return await this.#e.openSession(async t=>{let r=await t.readToc();if(r===void 0)throw new Error("Document TOC is missing");return await Ij(t,r.items)})}async readSerialSummary(t){return await this.#e.openSession(async r=>{if(await r.serials.getById(t)===void 0)throw new Error(`No completed summary exists for id ${t}. Use \`wg wikg://<archive.wikg>/chapter list\` to discover chapter ids, then \`wg wikg://<archive.wikg>/chapter/${t}/summary get\` after summary is ready.`);let i=await r.readSummary(t);if(i===void 0)throw new Error(`Chapter ${t} summary is missing. Run \`wg wikg://local/job add --input <chapter-uri> --task reading-summary --accept-cost\` before export, or inspect the archive with \`wg <archive-uri>/chapter/tree get\`.`);return i})}async saveAs(t){await c3(this.#e),await zr(this.#t,t)}}});import{rm as u3}from"fs/promises";import{resolve as l3}from"path";async function vj(e,t){return await Fk("digest-epub",rp,e,t)}async function Cj(e,t){return await Fk("digest-markdown",Dy,e,t)}async function Tj(e,t){let r=Bm({operation:"digest-text-stream",...e.onProgress===void 0?{}:{onProgress:e.onProgress}});return await Pj(async(n,i)=>(await n.openSession(async a=>{let o=await a.peekNextSerialId(),s=xj(e.title),c=An(new Set),d=e.targetStage??"summarized";if(await r.markDiscoveryUnavailable(),d==="planned")await a.serials.createWithId(o);else if(d==="sourced")await a.serials.createWithId(o),await Xt(a,o,e.stream,{...e.segmenter===void 0?{}:{segmenter:e.segmenter}});else{let u=r.createSerialTracker({id:o}),p=new hn({document:a,llm:p3(e.llm,d),...e.logDirPath===void 0?{}:{logDirPath:e.logDirPath},...e.segmenter===void 0?{}:{segmenter:e.segmenter}});d==="graphed"?(await a.serials.createWithId(o),await Xt(a,o,e.stream,{...e.segmenter===void 0?{}:{segmenter:e.segmenter}}),await p.buildTopologyInto(o,{extractionPrompt:e.extractionPrompt,...e.userLanguage===void 0?{}:{userLanguage:e.userLanguage}},u)):await p.generateInto(o,e.stream,{extractionPrompt:e.extractionPrompt,...e.userLanguage===void 0?{}:{userLanguage:e.userLanguage}},u)}await a.writeBookMeta({version:Ur,sourceFormat:e.sourceFormat??"txt",title:xj(e.title)??null,authors:[],description:null,identifier:null,language:e.bookLanguage??null,publishedAt:null,publisher:null}),await a.writeToc({version:Xe,items:[{serialId:o,children:[],key:c,...s===void 0?{}:{title:s}}]})}),await t(new fn(n,i))),e.documentDirPath)}async function Rj(e,t){return await Fk("digest-txt",jy,e,t)}async function Fk(e,t,r,n){let i=Bm({operation:e,...r.onProgress===void 0?{}:{onProgress:r.onProgress}});return await Pj(async(a,o)=>(await ZN({adapter:t,document:a,digestProgressTracker:i,extractionPrompt:r.extractionPrompt,...r.llm===void 0?{}:{llm:r.llm},path:r.path,...r.targetStage===void 0?{}:{targetStage:r.targetStage},...r.logDirPath===void 0?{}:{logDirPath:r.logDirPath},...r.segmenter===void 0?{}:{segmenter:r.segmenter},...r.userLanguage===void 0?{}:{userLanguage:r.userLanguage}}),await n(new fn(a,o))),r.documentDirPath)}async function Pj(e,t){let r=t===void 0?await Ye("archive-open"):l3(t),n=await ye.open(r);try{return await e(n,r)}finally{await n.release(),t===void 0&&await u3(r,{force:!0,recursive:!0})}}function xj(e){let t=e?.trim();return t===void 0||t===""?void 0:t}function p3(e,t){if(e===void 0)throw new Error(`LLM is required to digest source to ${t}.`);return e}var _j=l(()=>{"use strict";Yr();ji();ji();X();$r();nL();ls();tj();wh()});function fu(e={}){return Object.freeze({"serial-generation/editor-compress":hu(mu["serial-generation/editor-compress"],e),"serial-generation/editor-review":hu(mu["serial-generation/editor-review"],e),"serial-generation/editor-review-guide":hu(mu["serial-generation/editor-review-guide"],e),"serial-generation/reader-choice":hu(mu["serial-generation/reader-choice"],e),"serial-generation/reader-extraction":hu(mu["serial-generation/reader-extraction"],e)})}function hu(e,t){return Object.freeze({...e,...t.temperature===void 0?{}:{temperature:t.temperature},...t.topP===void 0?{}:{topP:t.topP}})}var mu,Gk=l(()=>{"use strict";oc();mu=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]})})});function et(e){let t=e?.trim();return t===void 0||t===""?Sh:t}function ua(e){let t=e?.trim();return t===void 0||t===""?kh:t}var Sh,kh,ui=l(()=>{"use strict";Sh="Focus on the main storyline and key character developments. Preserve important dialogues and critical plot points. Background descriptions and minor details can be compressed significantly.",kh=["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 Uk=l(()=>{"use strict";ui()});function Lj(e){return e===void 0?{}:{llm:e}}function h3(e){return f3(e)?e:{model:e}}function f3(e){return typeof e=="object"&&e!==null&&"model"in e}var m3,gn,Oj=l(()=>{"use strict";Hl();jt();Qg();_j();Gk();xr();Uk();m3=Mr(),gn=class{#e;#t;#r;constructor(t){if(this.#e=t.debugLogDirPath,this.#r=t.verbose??!1,t.llm===void 0){this.#t=void 0;return}let r=h3(t.llm);this.#t=new Dr({dataDirPath:m3,sampling:fu({...r.temperature===void 0?{}:{temperature:r.temperature},...r.topP===void 0?{}:{topP:r.topP}}),...r})}async digestEpubSession(t,r){return await this.#o("digest-epub",async()=>await vj(this.#n(t),r))}async digestMarkdownSession(t,r){return await this.#o("digest-markdown",async()=>await Cj(this.#n(t),r))}async digestTextStreamSession(t,r){return await this.#o("digest-text-stream",async()=>await Tj({extractionPrompt:et(t.extractionPrompt),...Lj(this.#a(t.targetStage)),...t.onProgress===void 0?{}:{onProgress:t.onProgress},stream:t.stream,...this.#e===void 0?{}:{logDirPath:this.#e},...t.bookLanguage===void 0?{}:{bookLanguage:t.bookLanguage},...t.documentDirPath===void 0?{}:{documentDirPath:t.documentDirPath},...t.sourceFormat===void 0?{}:{sourceFormat:t.sourceFormat},...t.targetStage===void 0?{}:{targetStage:t.targetStage},...t.title===void 0?{}:{title:t.title},...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}},r))}async digestTxtSession(t,r){return await this.#o("digest-txt",async()=>await Rj(this.#n(t),r))}async openSession(t,r,n={}){return await this.#o("open-wikg",async()=>await new G(t).read(r,{...n.documentDirPath===void 0?{}:{documentDirPath:n.documentDirPath}}))}#n(t){return{extractionPrompt:et(t.extractionPrompt),...Lj(this.#a(t.targetStage)),...t.onProgress===void 0?{}:{onProgress:t.onProgress},path:t.path,...this.#e===void 0?{}:{logDirPath:this.#e},...t.documentDirPath===void 0?{}:{documentDirPath:t.documentDirPath},...t.targetStage===void 0?{}:{targetStage:t.targetStage},...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}}}#i(){if(this.#t===void 0)throw new Error("LLM is required for digest operations. Configure `llm` when constructing WikiGraph.");return this.#t}#a(t){if(!(t==="planned"||t==="sourced"))return this.#i()}async#o(t,r){return await rc({operation:t,...this.#e===void 0?{}:{logDirPath:this.#e},...this.#r?{verbose:!0}:{}},r)}}});var $k,Bk=l(()=>{"use strict";$k=["queued","running","canceling","paused","succeeded","failed","canceled"]});import{mkdir as Eh}from"fs/promises";import{join as yn}from"path";function Nj(){return yn(gu(),"job.sqlite")}async function jj(e){let t=yn(Hk(),e);return await Eh(t,{recursive:!0}),t}function Hk(){return yn(gu(),"work")}async function Dj(e){let t=yn(g3(),e);return await Eh(t,{recursive:!0}),t}function g3(){return yn(gu(),"cache")}async function Mj(e){let t=yn(y3(),e);return await Eh(t,{recursive:!0}),t}function y3(){return yn(gu(),"logs")}async function Wj(e){let t=yn(gu(),"events");return await Eh(t,{recursive:!0}),yn(t,`${e}.ndjson`)}function gu(){return qa()}var Ih=l(()=>{"use strict";Ue()});import{createHash as b3}from"crypto";import{resolve as w3}from"path";function yu(e){return b3("sha256").update(w3(e)).digest("hex")}function bu(e){try{return process.kill(e,0),!0}catch{return!1}}function Fj(e){return e instanceof Error?{message:e.message,name:e.name,stack:e.stack}:e}async function Gj(e){await new Promise(t=>{setTimeout(t,e)})}var ps=l(()=>{"use strict"});function qk(e,t){return t===void 0?{}:{[e]:t}}function Kk(e,t){if(!(e===void 0&&t===void 0))return(e??0)+(t??0)}function S3(e){return e.phaseDetail??e.unit}function k3(e){if(e.phaseDetail!==void 0)return e.phaseDetail;switch(e.unit){case"candidate":return"candidates";case"char":return"chars";case"item":return"items";case"page":return"page";case"qid":return"qids";case"record":return"records";case"sentence":return"sentences";case"window":return"windows"}}function Jk(e,t){return t<=0?Math.max(0,e):Math.min(t,Math.max(0,e))}var xh,li,zk=l(()=>{"use strict";xh=class{#e;#t;#r;#n=4;#i=5e3;#a=0;#o=0;#s=0;#c;#l=new Map;#u;#d={};#m=0;#p=Promise.resolve();#h=0;#f=0;#b=Promise.resolve();constructor(t,r,n){this.#e=t,this.#t=r,this.#r=n}async addOutputCharacters(t){await this.#g(async()=>{await this.throwIfStopped(),this.#s+=t,await this.#y()})}async addTokenUsage(t){await this.#g(async()=>{await this.throwIfStopped(),this.#d={...qk("cacheReadTokens",Kk(this.#d.cacheReadTokens,t.cacheReadTokens)),...qk("inputTokens",Kk(this.#d.inputTokens,t.inputTokens)),...qk("outputTokens",Kk(this.#d.outputTokens,t.outputTokens))},await this.#y(!0)})}async setTotals(t){await this.#g(async()=>{await this.throwIfStopped(),this.#h=t.totalGraphWords??this.#h,this.#f=t.totalReadingSummaryWords??this.#f,await this.#y(!0)})}async stepStarted(t){await this.#g(async()=>{await this.throwIfStopped(),this.#u=t,this.#c=void 0,this.#l.clear(),await this.#r.markBuildJobStep(this.#e.jobId,t),await this.#r.appendBuildJobEvent(this.#e,{at:Date.now(),jobId:this.#e.jobId,seq:0,step:t,type:"step_started"}),await this.#y(!0)})}async stepCompleted(t){await this.#g(async()=>{await this.throwIfStopped(),this.#u=t,this.#c=void 0,this.#l.clear(),await this.#r.appendBuildJobEvent(this.#e,{at:Date.now(),jobId:this.#e.jobId,seq:0,step:t,type:"step_completed"}),await this.#y(!0)})}async updateWords(t){await this.#g(async()=>{await this.throwIfStopped(),this.#a=t.graphWords===void 0?this.#a:Jk(t.graphWords,this.#h),this.#m=t.readingSummaryWords===void 0?this.#m:Jk(t.readingSummaryWords,this.#f),await this.#y()})}async updatePhase(t){await this.#g(async()=>{await this.throwIfStopped(),this.#c!==t.phase&&this.#l.clear(),this.#c=t.phase,this.#l.set(S3(t),{done:Jk(t.done,t.total),name:k3(t),total:Math.max(0,t.total),unit:t.unit}),await this.#y(t.force??!0)})}async throwIfStopped(){let t=this.#p.then(async()=>{let r=await this.#r.readBuildJobForStopCheck(this.#e.jobId);if(!(r.state==="running"&&r.ownerId===this.#t))throw new li(`Job ${this.#e.jobId} is ${r.state}. Stop current worker execution.`)});this.#p=t.catch(()=>{}),await t}async#g(t){let r=this.#b.then(t,t);this.#b=r.catch(()=>{}),await r}async#y(t=!1){let r=Date.now();if(!t&&r-this.#o<this.#i)return;this.#o=r;let n=this.#k();await this.#r.appendBuildJobEvent(this.#e,{at:r,counters:this.#w(),jobId:this.#e.jobId,...this.#c===void 0?{}:{phase:this.#c},seq:0,...this.#u===void 0?{}:{step:this.#u},...n===void 0?{}:{tokens:n},type:"status_snapshot"})}#w(){let t=this.#S();return[...t===void 0?[]:[t],...this.#l.values()]}#S(){switch(this.#u){case"reading-graph":return this.#h<=0?void 0:{done:this.#a,name:"words",total:this.#h,unit:"word"};case"reading-summary":return this.#f<=0?void 0:{done:this.#m,name:"words",total:this.#f,unit:"word"};case void 0:return}}#k(){let t=this.#d.outputTokens??(this.#s===0?void 0:Math.floor(this.#s/this.#n)),r={...this.#d.cacheReadTokens===void 0?{}:{cacheReadTokens:this.#d.cacheReadTokens},...this.#d.inputTokens===void 0?{}:{inputTokens:this.#d.inputTokens},...t===void 0?{}:{outputTokens:t}};return Object.keys(r).length===0?void 0:r}};li=class extends Error{constructor(t){super(t),this.name="BuildJobStoppedError"}}});import{appendFile as E3,readFile as I3}from"fs/promises";async function la(e){let t;try{t=await I3(e.eventsPath,"utf8")}catch{return[]}return t.split(`
|
|
2316
|
+
`).filter(r=>r.trim()!=="").map(r=>JSON.parse(r))}async function Ke(e,t){let r=await x3(e)+1,n={...t,jobId:e.jobId,seq:r};await E3(e.eventsPath,`${JSON.stringify(n)}
|
|
2317
|
+
`,"utf8")}async function x3(e){return(await la(e)).at(-1)?.seq??0}var wu=l(()=>{"use strict"});var Uj,$j=l(()=>{"use strict";Uj=`
|
|
2318
|
+
CREATE TABLE IF NOT EXISTS build_jobs (
|
|
2319
|
+
job_id TEXT PRIMARY KEY,
|
|
2320
|
+
archive_key TEXT NOT NULL,
|
|
2321
|
+
archive_path TEXT NOT NULL,
|
|
2322
|
+
chapter_id INTEGER NOT NULL,
|
|
2323
|
+
target TEXT NOT NULL,
|
|
2324
|
+
current_step TEXT,
|
|
2325
|
+
state TEXT NOT NULL,
|
|
2326
|
+
queue_rank INTEGER NOT NULL,
|
|
2327
|
+
workspace_path TEXT NOT NULL,
|
|
2328
|
+
cache_path TEXT NOT NULL,
|
|
2329
|
+
log_path TEXT NOT NULL,
|
|
2330
|
+
events_path TEXT NOT NULL,
|
|
2331
|
+
input_revision INTEGER,
|
|
2332
|
+
llm_json TEXT,
|
|
2333
|
+
prompt TEXT,
|
|
2334
|
+
owner_id TEXT,
|
|
2335
|
+
owner_pid INTEGER,
|
|
2336
|
+
reading_summary_started_at INTEGER,
|
|
2337
|
+
created_at INTEGER NOT NULL,
|
|
2338
|
+
updated_at INTEGER NOT NULL,
|
|
2339
|
+
finished_at INTEGER,
|
|
2340
|
+
error_json TEXT
|
|
2341
|
+
);
|
|
2342
|
+
|
|
2343
|
+
DROP INDEX IF EXISTS idx_build_jobs_active_chapter;
|
|
2344
|
+
|
|
2345
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_build_jobs_active_knowledge_chapter
|
|
2346
|
+
ON build_jobs(archive_key, chapter_id)
|
|
2347
|
+
WHERE target = 'knowledge-graph'
|
|
2348
|
+
AND state IN ('queued', 'running', 'canceling', 'paused');
|
|
2349
|
+
|
|
2350
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_build_jobs_active_reading_chapter
|
|
2351
|
+
ON build_jobs(archive_key, chapter_id)
|
|
2352
|
+
WHERE target IN ('reading-graph', 'reading-summary')
|
|
2353
|
+
AND state IN ('queued', 'running', 'canceling', 'paused');
|
|
2354
|
+
|
|
2355
|
+
CREATE INDEX IF NOT EXISTS idx_build_jobs_queue
|
|
2356
|
+
ON build_jobs(state, queue_rank, updated_at);
|
|
2357
|
+
|
|
2358
|
+
CREATE TABLE IF NOT EXISTS build_worker_lease (
|
|
2359
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
2360
|
+
owner_id TEXT,
|
|
2361
|
+
owner_pid INTEGER,
|
|
2362
|
+
heartbeat_at INTEGER
|
|
2363
|
+
);
|
|
2364
|
+
|
|
2365
|
+
INSERT OR IGNORE INTO build_worker_lease (id)
|
|
2366
|
+
VALUES (1);
|
|
2367
|
+
`});function tt(e){let t=C3(pa(e,"current_step"),"current_step"),r=pa(e,"owner_id"),n=e.owner_pid===null?void 0:Fe(e,"owner_pid"),i=e.reading_summary_started_at===null?void 0:Fe(e,"reading_summary_started_at"),a=e.finished_at===null?void 0:Fe(e,"finished_at"),o=pa(e,"error_json"),s=e.input_revision===null?void 0:Fe(e,"input_revision"),c=pa(e,"llm_json"),d=pa(e,"prompt");return{archiveKey:Yt(e,"archive_key"),archivePath:Yt(e,"archive_path"),cachePath:Yt(e,"cache_path"),chapterId:Fe(e,"chapter_id"),createdAt:Fe(e,"created_at"),...t===void 0?{}:{currentStep:t},...o===void 0?{}:{errorJSON:o},eventsPath:Yt(e,"events_path"),...a===void 0?{}:{finishedAt:a},jobId:Yt(e,"job_id"),...s===void 0?{}:{inputRevision:s},logPath:Yt(e,"log_path"),...c===void 0?{}:{llmJSON:c},...r===void 0?{}:{ownerId:r},...n===void 0?{}:{ownerPid:n},...d===void 0?{}:{prompt:d},queueRank:Fe(e,"queue_rank"),state:v3(Yt(e,"state")),...i===void 0?{}:{readingSummaryStartedAt:i},target:Bj(Yt(e,"target"),"target"),updatedAt:Fe(e,"updated_at"),workspacePath:Yt(e,"workspace_path")}}function v3(e){if($k.includes(e))return e;throw new Error(`Invalid build job state: ${e}`)}function Bj(e,t){if(e==="reading-graph"||e==="knowledge-graph"||e==="reading-summary")return e;throw new Error(`Invalid ${t}: ${e}`)}function C3(e,t){return e===void 0?void 0:Bj(e,t)}function Hj(e){return e==="knowledge-graph"?"knowledge-graph":"reading"}function Yt(e,t){let r=e[t];if(typeof r!="string")throw new TypeError(`Expected ${t} to be a string`);return r}function Fe(e,t){let r=e[t];if(typeof r!="number")throw new TypeError(`Expected ${t} to be a number`);return r}function pa(e,t){let r=e[t];if(r!=null){if(typeof r!="string")throw new TypeError(`Expected ${t} to be a string`);return r}}var ma=l(()=>{"use strict";Bk()});async function pe(e,t){let r=await e.queryOne("SELECT * FROM build_jobs WHERE job_id = ?",[t],tt);if(r===void 0)throw new Error(`Build job not found: ${t}`);return r}async function qj(e,t){let r=t.trim();if(r==="")throw new Error("Build job id is empty.");let n=await e.queryAll(`
|
|
2368
|
+
SELECT job_id
|
|
2369
|
+
FROM build_jobs
|
|
2370
|
+
WHERE substr(job_id, 1, ?) = ?
|
|
2371
|
+
ORDER BY created_at DESC
|
|
2372
|
+
`,[r.length,r],i=>Yt(i,"job_id"));if(n.length===0)throw new Error(`Build job not found: ${t}`);if(n.length>1)throw new Error(`Build job id prefix is ambiguous: ${t}. Matches: ${n.join(", ")}`);return n[0]}async function Kj(e){return await e.queryOne("SELECT COALESCE(MAX(queue_rank), 0) AS rank FROM build_jobs",void 0,t=>Fe(t,"rank"))??0}async function vh(e){return await e.queryOne("SELECT COALESCE(MIN(queue_rank), 0) AS rank FROM build_jobs",void 0,t=>Fe(t,"rank"))??0}async function re(){return await V(Nj(),Uj)}async function Jj(){return await re()}var ms=l(()=>{"use strict";X();$j();Ih();ma()});import{rm as T3}from"fs/promises";async function zj(e,t){let r=Date.now();await e.run(`
|
|
2373
|
+
UPDATE build_jobs
|
|
2374
|
+
SET state = 'canceling', updated_at = ?
|
|
2375
|
+
WHERE job_id = ? AND state = 'running'
|
|
2376
|
+
`,[r,t.jobId]);let n=await pe(e,t.jobId);return await Ke(n,{at:r,jobId:t.jobId,seq:0,state:"canceling",type:"canceling"}),n}async function Su(e,t){let r=Date.now();await e.run(`
|
|
2377
|
+
UPDATE build_jobs
|
|
2378
|
+
SET state = 'canceled', owner_id = NULL, owner_pid = NULL,
|
|
2379
|
+
current_step = NULL, finished_at = ?, updated_at = ?
|
|
2380
|
+
WHERE job_id = ?
|
|
2381
|
+
`,[r,r,t.jobId]);let n=await pe(e,t.jobId);return await Ke(n,{at:r,jobId:t.jobId,seq:0,state:"canceled",type:"canceled"}),n}async function Qk(e,t,r){let n=Date.now(),i=Fj(r),a=JSON.stringify(i);await e.run(`
|
|
2382
|
+
UPDATE build_jobs
|
|
2383
|
+
SET state = 'failed', owner_id = NULL, owner_pid = NULL,
|
|
2384
|
+
current_step = NULL, finished_at = ?, updated_at = ?, error_json = ?
|
|
2385
|
+
WHERE job_id = ?
|
|
2386
|
+
`,[n,n,a,t.jobId]),await Ke(t,{at:n,error:i,jobId:t.jobId,seq:0,state:"failed",type:"failed"})}async function Qj(e,t){let r=await re();try{await r.transaction(async()=>{let n=await pe(r,e);if(n.ownerId!==t||n.state!=="running")return;let i=Date.now();await r.run(`
|
|
2387
|
+
UPDATE build_jobs
|
|
2388
|
+
SET state = 'succeeded', owner_id = NULL, owner_pid = NULL,
|
|
2389
|
+
current_step = NULL, finished_at = ?, updated_at = ?
|
|
2390
|
+
WHERE job_id = ?
|
|
2391
|
+
`,[i,i,e]),await Ke(n,{at:i,jobId:e,seq:0,state:"succeeded",type:"succeeded"}),await T3(n.workspacePath,{force:!0,recursive:!0})})}finally{await r.close()}}async function Vj(e,t,r){let n=await re();try{await n.transaction(async()=>{let i=await pe(n,e);i.ownerId!==t||i.state!=="running"||await Qk(n,i,r)})}finally{await n.close()}}async function Xj(e,t){let r=await re();try{await r.transaction(async()=>{let n=await pe(r,e);n.ownerId!==t||n.state!=="canceling"||await Su(r,n)})}finally{await r.close()}}async function Yj(e,t){let r=await re();try{await r.run(`
|
|
2392
|
+
UPDATE build_jobs
|
|
2393
|
+
SET current_step = ?,
|
|
2394
|
+
reading_summary_started_at = CASE
|
|
2395
|
+
WHEN ? = 'reading-summary' AND reading_summary_started_at IS NULL THEN ?
|
|
2396
|
+
ELSE reading_summary_started_at
|
|
2397
|
+
END,
|
|
2398
|
+
updated_at = ?
|
|
2399
|
+
WHERE job_id = ?
|
|
2400
|
+
`,[t,t,Date.now(),Date.now(),e])}finally{await r.close()}}var Ch=l(()=>{"use strict";ps();wu();ms()});import{rm as R3}from"fs/promises";async function ft(e){let t=[];await e.transaction(async()=>{let r=await e.queryAll(`
|
|
2401
|
+
SELECT *
|
|
2402
|
+
FROM build_jobs
|
|
2403
|
+
WHERE state IN ('running', 'canceling')
|
|
2404
|
+
AND owner_pid IS NOT NULL
|
|
2405
|
+
`,void 0,tt);for(let n of r)if(!(n.ownerPid!==void 0&&bu(n.ownerPid))){if(n.state==="canceling"){await Su(e,n);continue}await Qk(e,n,{message:"Build worker process disappeared before finishing the job.",name:"BuildJobWorkerLost"}),t.push(n.workspacePath)}});for(let r of t)await R3(r,{force:!0,recursive:!0})}var Th=l(()=>{"use strict";ps();ma();Ch()});import{randomUUID as P3}from"crypto";import{resolve as Zj}from"path";async function ku(e){let t=await re();try{return await ft(t),await t.transaction(async()=>{let r=Zj(e.archivePath),n=yu(r),i=Date.now(),a=await eD(t,{archiveKey:n,chapterId:e.chapterId,target:e.target});if(a!==void 0)return await A3(t,a,e,i);let o=e.jobId??P3(),s=await jj(o),c=await Dj(o),d=await Mj(o),u=await Wj(o),p=e.boost===!0?await vh(t)-1:await Kj(t)+1;await t.run(`
|
|
2406
|
+
INSERT INTO build_jobs (
|
|
2407
|
+
job_id, archive_key, archive_path, chapter_id, target, state, queue_rank,
|
|
2408
|
+
workspace_path, cache_path, log_path, events_path, llm_json, prompt,
|
|
2409
|
+
created_at, updated_at
|
|
2410
|
+
) VALUES (?, ?, ?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2411
|
+
`,[o,n,r,e.chapterId,e.target,p,s,c,d,u,e.llmJSON??null,e.prompt??null,i,i]);let m=await pe(t,o);return await Ke(m,{at:i,jobId:o,seq:0,state:"queued",type:"created"}),m})}finally{await t.close()}}async function hs(e){let t=await re();try{return await t.transaction(async()=>{await t.run(`
|
|
2412
|
+
UPDATE build_jobs
|
|
2413
|
+
SET input_revision = ?,
|
|
2414
|
+
updated_at = ?
|
|
2415
|
+
WHERE job_id = ?
|
|
2416
|
+
AND owner_id = ?
|
|
2417
|
+
AND state = 'running'
|
|
2418
|
+
`,[e.currentRevision,Date.now(),e.jobId,e.ownerId])}),await pe(t,e.jobId)}finally{await t.close()}}async function Eu(e){let t=await Ge(e.jobId);if(t.state!=="running"||t.ownerId!==e.ownerId)throw new li(`Job ${e.jobId} is ${t.state}. Stop current worker execution.`);if(t.inputRevision===void 0)throw new Error(`Job ${e.jobId} has no recorded chapter input revision. Requeue the job before committing build output.`);if(t.inputRevision!==e.currentRevision)throw new Error(`Chapter ${t.chapterId} changed while job ${t.jobId} was running. Requeue the job before committing build output.`)}async function Iu(e={}){let t=await re();try{await ft(t);let r=[],n=[];return e.archivePath!==void 0&&(r.push(yu(Zj(e.archivePath))),n.push("archive_key = ?")),(e.activeOnly===!0||e.all!==!0)&&n.push("state IN ('queued', 'running', 'canceling', 'paused')"),await t.queryAll(`
|
|
2419
|
+
SELECT *
|
|
2420
|
+
FROM build_jobs
|
|
2421
|
+
${n.length===0?"":`WHERE ${n.join(" AND ")}`}
|
|
2422
|
+
ORDER BY
|
|
2423
|
+
CASE state
|
|
2424
|
+
WHEN 'running' THEN 0
|
|
2425
|
+
WHEN 'canceling' THEN 1
|
|
2426
|
+
WHEN 'queued' THEN 2
|
|
2427
|
+
WHEN 'paused' THEN 3
|
|
2428
|
+
ELSE 4
|
|
2429
|
+
END,
|
|
2430
|
+
queue_rank ASC,
|
|
2431
|
+
updated_at DESC
|
|
2432
|
+
`,r,tt)}finally{await t.close()}}async function eD(e,t){let r=t.target==="knowledge-graph"?"target = 'knowledge-graph'":"target IN ('reading-graph', 'reading-summary')";return await e.queryOne(`
|
|
2433
|
+
SELECT *
|
|
2434
|
+
FROM build_jobs
|
|
2435
|
+
WHERE archive_key = ?
|
|
2436
|
+
AND chapter_id = ?
|
|
2437
|
+
AND state IN ('queued', 'running', 'canceling', 'paused')
|
|
2438
|
+
AND ${r}
|
|
2439
|
+
ORDER BY updated_at DESC
|
|
2440
|
+
LIMIT 1
|
|
2441
|
+
`,[t.archiveKey,t.chapterId],tt)}async function A3(e,t,r,n){let i=t.target==="reading-graph"&&r.target==="reading-summary"?"reading-summary":t.target,a=r.boost===!0&&t.state==="queued"?await vh(e)-1:t.queueRank;if(i===t.target&&a===t.queueRank)return t;await e.run(`
|
|
2442
|
+
UPDATE build_jobs
|
|
2443
|
+
SET target = ?, queue_rank = ?, updated_at = ?
|
|
2444
|
+
WHERE job_id = ?
|
|
2445
|
+
`,[i,a,n,t.jobId]);let o=await pe(e,t.jobId);return i!==t.target&&await Ke(o,{at:n,from:t.target,jobId:t.jobId,seq:0,to:i,type:"target_changed"}),a!==t.queueRank&&await Ke(o,{at:n,jobId:t.jobId,seq:0,state:o.state,type:"boosted"}),o}async function Ge(e){let t=await re();try{return await ft(t),await pe(t,e)}finally{await t.close()}}async function Vk(e){let t=await Jj();try{return await pe(t,e)}finally{await t.close()}}async function xu(e){let t=await re();try{return await ft(t),await qj(t,e)}finally{await t.close()}}async function vu(e){return await tD(e,"paused","paused",{allowedStates:["queued","running"]})}async function Cu(e){return await tD(e,"queued","resumed",{allowedStates:["paused"],clearOwner:!0})}async function Tu(e){let t=await re();try{return await ft(t),await t.transaction(async()=>{let r=await pe(t,e);if(r.state==="running")return await zj(t,r);if(r.state==="queued"||r.state==="paused")return await Su(t,r);throw new Error(`Cannot cancel ${r.state} job ${e}.`)})}finally{await t.close()}}async function Ru(e){let t=await re();try{return await ft(t),await t.transaction(async()=>{let r=await pe(t,e);if(r.state!=="queued")throw new Error(`Only queued jobs can be boosted. Job ${e} is ${r.state}.`);let n=Date.now();await t.run(`
|
|
2446
|
+
UPDATE build_jobs
|
|
2447
|
+
SET queue_rank = ?, updated_at = ?
|
|
2448
|
+
WHERE job_id = ?
|
|
2449
|
+
`,[await vh(t)-1,n,e]);let i=await pe(t,e);return await Ke(i,{at:n,jobId:e,seq:0,state:"queued",type:"boosted"}),i})}finally{await t.close()}}async function Pu(e,t){let r=await re();try{return await ft(r),await r.transaction(async()=>{let n=await pe(r,e);if(!_3.has(n.state))throw new Error(`Cannot change target for ${n.state} job ${e}.`);if(n.target===t)return n;let i=await eD(r,{archiveKey:n.archiveKey,chapterId:n.chapterId,target:t});if(i!==void 0&&i.jobId!==n.jobId)throw new Error(`Chapter ${n.chapterId} already has active ${Hj(t)} job ${i.jobId}.`);if(n.target==="reading-summary"&&t==="reading-graph"&&(n.readingSummaryStartedAt!==void 0||n.currentStep==="reading-summary"))throw new Error(`Cannot downgrade job ${e} after summary has started. Cancel it explicitly instead.`);let a=Date.now();await r.run(`
|
|
2450
|
+
UPDATE build_jobs
|
|
2451
|
+
SET target = ?, updated_at = ?
|
|
2452
|
+
WHERE job_id = ?
|
|
2453
|
+
`,[t,a,e]);let o=await pe(r,e);return await Ke(o,{at:a,from:n.target,jobId:e,seq:0,to:t,type:"target_changed"}),o})}finally{await r.close()}}async function tD(e,t,r,n){let i=await re();try{return await ft(i),await i.transaction(async()=>{let a=await pe(i,e);if(!n.allowedStates.includes(a.state))throw new Error(`Cannot ${r} ${a.state} job ${e}.`);let o=Date.now();await i.run(`
|
|
2454
|
+
UPDATE build_jobs
|
|
2455
|
+
SET state = ?, updated_at = ?, finished_at = ?,
|
|
2456
|
+
owner_id = CASE WHEN ? = 1 THEN NULL ELSE owner_id END,
|
|
2457
|
+
owner_pid = CASE WHEN ? = 1 THEN NULL ELSE owner_pid END
|
|
2458
|
+
WHERE job_id = ?
|
|
2459
|
+
`,[t,o,n.finished===!0?o:null,n.clearOwner===!0||n.finished===!0?1:0,n.clearOwner===!0||n.finished===!0?1:0,e]);let s=await pe(i,e);return await Ke(s,{at:o,jobId:e,seq:0,state:t,type:r}),s})}finally{await i.close()}}var _3,Xk=l(()=>{"use strict";Ih();ps();zk();wu();ms();Th();Ch();ma();_3=new Set(["queued","running","canceling","paused"])});import{resolve as L3}from"path";async function Zt(e){await bn({archivePath:e.archivePath,operation:e.operation,...e.requiresTarget===void 0?{}:{requiresTarget:e.requiresTarget},scope:{chapterIds:e.chapterIds,kind:"chapter"}})}async function bn(e){if(e.scope.kind==="chapter"&&e.scope.chapterIds.length===0)return;let t=await re();try{await ft(t);let r=yu(L3(e.archivePath)),n=e.requiresTarget===void 0?"":"AND target = ?",i=[r],a="";e.scope.kind==="chapter"&&(a=`AND chapter_id IN (${e.scope.chapterIds.map(()=>"?").join(", ")})`,i.push(...e.scope.chapterIds)),e.requiresTarget!==void 0&&i.push(e.requiresTarget);let o=await t.queryOne(`
|
|
2460
|
+
SELECT *
|
|
2461
|
+
FROM build_jobs
|
|
2462
|
+
WHERE archive_key = ?
|
|
2463
|
+
${a}
|
|
2464
|
+
AND state IN ('queued', 'running', 'canceling', 'paused')
|
|
2465
|
+
${n}
|
|
2466
|
+
ORDER BY updated_at DESC
|
|
2467
|
+
LIMIT 1
|
|
2468
|
+
`,i,tt);if(o===void 0)return;throw new Error(`Chapter ${o.chapterId} has active ${o.target} job ${o.jobId}. ${e.operation} is blocked until the job is paused/canceled or completed.`)}finally{await t.close()}}var rD=l(()=>{"use strict";ps();ms();Th();ma()});import{randomUUID as O3}from"crypto";async function _u(e){let t=await re(),r=`${process.pid}-${O3()}`,n=Math.max(1,e.concurrency),i=e.idleTimeoutMs??1e4,a=!1,o=0,s=Date.now(),c=u=>{a=!0};process.once("SIGINT",c),process.once("SIGTERM",c);let d=setInterval(()=>{nD(r).catch(()=>{})},N3);try{if(!await W3(t,r))return;let p=async()=>{for(;!a;){await nD(r,t),await ft(t),o+=1;let m;try{m=await M3(t,r)}finally{m===void 0&&(o-=1)}if(m===void 0){if(o===0&&Date.now()-s>=i)break;await Gj(500);continue}s=Date.now();try{await j3(m,r,e)}finally{o-=1,o===0&&(s=Date.now())}}};await Promise.all(Array.from({length:n},async()=>await p()))}finally{clearInterval(d),process.removeListener("SIGINT",c),process.removeListener("SIGTERM",c),await F3(t,r),await t.close()}}async function j3(e,t,r){let n=new xh(e,t,{appendBuildJobEvent:Ke,markBuildJobStep:Yj,readBuildJobForStopCheck:Vk}),i=new AbortController,a=setInterval(()=>{D3(e,t,i).catch(()=>{})},500);try{await Ke(e,{at:Date.now(),jobId:e.jobId,seq:0,state:"running",type:"started"}),await r.executeJob(e,n,{signal:i.signal}),await n.throwIfStopped(),await Qj(e.jobId,t)}catch(o){if(o instanceof li||i.signal.aborted){await Xj(e.jobId,t);return}await Vj(e.jobId,t,o)}finally{clearInterval(a)}}async function D3(e,t,r){if(r.signal.aborted)return;let n=await Vk(e.jobId);n.state==="running"&&n.ownerId===t||r.abort(new li(`Job ${e.jobId} is ${n.state}. Stop current worker execution.`))}async function M3(e,t){return await e.transaction(async()=>{let r=await e.queryOne(`
|
|
2469
|
+
SELECT *
|
|
2470
|
+
FROM build_jobs
|
|
2471
|
+
WHERE state = 'queued'
|
|
2472
|
+
ORDER BY queue_rank ASC, created_at ASC
|
|
2473
|
+
LIMIT 1
|
|
2474
|
+
`,void 0,tt);if(r===void 0)return;let n=Date.now();return await e.run(`
|
|
2475
|
+
UPDATE build_jobs
|
|
2476
|
+
SET state = 'running', owner_id = ?, owner_pid = ?, updated_at = ?
|
|
2477
|
+
WHERE job_id = ? AND state = 'queued'
|
|
2478
|
+
`,[t,process.pid,n,r.jobId]),await pe(e,r.jobId)})}async function W3(e,t){return await G3(e),await e.transaction(async()=>{let r=await e.queryOne(`
|
|
2479
|
+
SELECT owner_pid
|
|
2480
|
+
FROM build_worker_lease
|
|
2481
|
+
WHERE id = 1
|
|
2482
|
+
`,void 0,n=>({ownerPid:n.owner_pid===null?void 0:Fe(n,"owner_pid")}));return r?.ownerPid!==void 0&&bu(r.ownerPid)?!1:(await e.run(`
|
|
2483
|
+
UPDATE build_worker_lease
|
|
2484
|
+
SET owner_id = ?, owner_pid = ?, heartbeat_at = ?
|
|
2485
|
+
WHERE id = 1
|
|
2486
|
+
`,[t,process.pid,Date.now()]),!0)})}async function nD(e,t){let r=t??await re();try{await r.run(`
|
|
2487
|
+
UPDATE build_worker_lease
|
|
2488
|
+
SET heartbeat_at = ?
|
|
2489
|
+
WHERE id = 1 AND owner_id = ?
|
|
2490
|
+
`,[Date.now(),e])}finally{t===void 0&&await r.close()}}async function F3(e,t){await e.run(`
|
|
2491
|
+
UPDATE build_worker_lease
|
|
2492
|
+
SET owner_id = NULL, owner_pid = NULL, heartbeat_at = NULL
|
|
2493
|
+
WHERE id = 1 AND owner_id = ?
|
|
2494
|
+
`,[t])}async function G3(e){let t=await e.queryOne(`
|
|
2495
|
+
SELECT owner_id, owner_pid
|
|
2496
|
+
FROM build_worker_lease
|
|
2497
|
+
WHERE id = 1
|
|
2498
|
+
`,void 0,r=>({ownerId:pa(r,"owner_id"),ownerPid:r.owner_pid===null?void 0:Fe(r,"owner_pid")}));t?.ownerPid===void 0||bu(t.ownerPid)||await e.run(`
|
|
2499
|
+
UPDATE build_worker_lease
|
|
2500
|
+
SET owner_id = NULL, owner_pid = NULL, heartbeat_at = NULL
|
|
2501
|
+
WHERE id = 1
|
|
2502
|
+
`)}var N3,iD=l(()=>{"use strict";ps();zk();wu();ms();Th();Ch();Xk();ma();N3=5e3});import{readdir as Rh,rm as U3,rmdir as $3,stat as Yk}from"fs/promises";import{join as Ph}from"path";function Zk(e){return B3.has(e)}async function _h(e){let t=await Rh(e).catch(n=>{if(!(R(n)&&n.code==="ENOENT"))throw n});if(t===void 0||t.some(n=>!Zk(n)))return 0;let r=0;for(let n of t){let i=Ph(e,n);r+=await rt(i),await U3(i,{force:!0,recursive:!0})}return await $3(e).catch(n=>{if(!(R(n)&&n.code==="ENOENT"))throw n}),r}async function Ah(e){let t=await Rh(e,{withFileTypes:!0}).catch(a=>{if(R(a)&&a.code==="ENOENT")return[];throw a}),r=0,n=0,i=0;for(let a of t){if(!a.isDirectory())continue;i+=1;let o=Ph(e,a.name),s=await Yk(o).catch(d=>{if(!(R(d)&&d.code==="ENOENT"))throw d});if(s===void 0||Date.now()-s.mtimeMs<H3)continue;let c=await _h(o);if(await aD(o)){r+=c;continue}r+=c,n+=1}return{freedBytes:r,removed:n,scanned:i}}async function eE(e){let t=await Rh(e,{withFileTypes:!0}).catch(a=>{if(R(a)&&a.code==="ENOENT")return[];throw a}),r=0,n=0,i=0;for(let a of t){if(!a.isDirectory())continue;let o=Ph(e,a.name),s=await eE(o);r+=s.freedBytes,n+=s.removed,i+=s.scanned+1;let c=await _h(o);if(await aD(o)){r+=c;continue}r+=c,n+=1}return{freedBytes:r,removed:n,scanned:i}}async function rt(e){try{let t=await Yk(e);if(t.isDirectory()){let r=await Rh(e),n=0;for(let i of r)n+=await rt(Ph(e,i));return n}return t.size}catch(t){if(R(t)&&t.code==="ENOENT")return 0;throw t}}async function aD(e){try{return await Yk(e),!0}catch(t){if(R(t)&&t.code==="ENOENT")return!1;throw t}}var B3,H3,Au=l(()=>{"use strict";H();B3=new Set([".DS_Store"]),H3=6e4});import{rm as pi}from"fs/promises";async function Lu(e={}){let t=await re(),r=Date.now()-(e.olderThanMs??10080*60*1e3),n=e.states??["succeeded","failed","canceled"];try{let i=n.map(()=>"?").join(", "),a=await t.queryAll(`
|
|
2503
|
+
SELECT *
|
|
2504
|
+
FROM build_jobs
|
|
2505
|
+
WHERE state IN (${i})
|
|
2506
|
+
AND updated_at <= ?
|
|
2507
|
+
`,[...n,r],tt);for(let o of a)await pi(o.workspacePath,{force:!0,recursive:!0}),await pi(o.cachePath,{force:!0,recursive:!0}),await pi(o.logPath,{force:!0,recursive:!0}),await pi(o.eventsPath,{force:!0}),await t.run("DELETE FROM build_jobs WHERE job_id = ?",[o.jobId]);return a.length}finally{await t.close()}}async function Lh(e){let t=await re(),r=e.force?e.now:e.now-10080*60*1e3;try{let n=await t.queryOne("SELECT COUNT(*) AS count FROM build_jobs",void 0,s=>Fe(s,"count")),i=await t.queryAll(`
|
|
2508
|
+
SELECT *
|
|
2509
|
+
FROM build_jobs
|
|
2510
|
+
WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
2511
|
+
AND updated_at <= ?
|
|
2512
|
+
`,[r],tt),a=e.dryRun?{freedBytes:0,removed:0,scanned:0}:await Ah(Hk()),o=0;for(let s of i)o+=await rt(s.workspacePath),o+=await rt(s.cachePath),o+=await rt(s.logPath),o+=await rt(s.eventsPath),e.dryRun||(await pi(s.workspacePath,{force:!0,recursive:!0}),await pi(s.cachePath,{force:!0,recursive:!0}),await pi(s.logPath,{force:!0,recursive:!0}),await pi(s.eventsPath,{force:!0}),await t.run("DELETE FROM build_jobs WHERE job_id = ?",[s.jobId]));return{freedBytes:o+a.freedBytes,removed:i.length+a.removed,scanned:(n??0)+a.scanned}}finally{await t.close()}}var oD=l(()=>{"use strict";Au();Ih();ms();ma()});var sD=l(()=>{"use strict";Bk();Xk();rD();iD();wu();oD()});async function Oh(e,t){let r=e.getSerialFragments(t);return await Promise.all((await r.listFragmentIds()).map(async n=>await r.getFragment(n)))}async function*cD(e,t){let r=e.getSerialFragments(t);for(let n of await r.listFragmentIds()){let i=await r.getFragment(n);for(let a of i.sentences)yield a.text}}async function dD(e){let t=[];for await(let r of e)t.push(r);return t}var Nh=l(()=>{"use strict"});import{createReadStream as q3,createWriteStream as K3}from"fs";import{createInterface as J3}from"readline";import{z as v}from"zod";function tE(e){return u9.parse(e)}async function ha(e,t){let r=K3(e,{encoding:"utf8",flags:"wx"});try{for await(let n of t)await E9(r,`${JSON.stringify(tE(n))}
|
|
2513
|
+
`)}finally{await I9(r)}}async function*gs(e){let t=J3({crlfDelay:1/0,input:q3(e,{encoding:"utf8"})}),r=0;for await(let n of t)if(r+=1,n.trim()!=="")try{yield tE(JSON.parse(n))}catch(i){throw new Error(`Invalid WikgObject JSONL record at ${e}:${r}`,{cause:i})}}async function*rE(e){yield{chapterId:e.chapterId,schemaVersion:jh,stream:"knowledge-graph",type:"meta"},e.parameter!==void 0&&(yield{...e.parameter.language===void 0?{}:{language:e.parameter.language},prompt:e.parameter.prompt,scope:"knowledge-graph",type:"parameter"});for await(let t of e.mentions){if(t.chapterId!==e.chapterId)throw new Error(`Mention ${t.id} belongs to chapter ${t.chapterId}, expected chapter ${e.chapterId}.`);yield l9(t)}for await(let t of e.mentionLinks)yield m9(e.chapterId,t);yield{type:"end"}}async function nE(e,t){let r=[],n=[],i,a=!1,o=!1;for await(let s of t)switch(s.type){case"meta":if(s.chapterId!==e)throw new Error(`WikgObject stream belongs to chapter ${s.chapterId}, expected chapter ${e}.`);if(s.stream!=="knowledge-graph")throw new Error(`Expected knowledge-graph stream, received ${s.stream}.`);a=!0;break;case"parameter":s.scope==="knowledge-graph"&&(i=s);break;case"mention":r.push(p9(e,s));break;case"mention-link":n.push(h9(e,s));break;case"end":o=!0;break;default:throw new Error(`Unexpected ${s.type} record in knowledge graph stream.`)}if(!a)throw new Error("WikgObject stream is missing a meta record.");if(!o)throw new Error("WikgObject stream is missing an end record.");return{mentionLinks:n,mentions:r,...i===void 0?{}:{parameter:i}}}async function*Ou(e){yield{chapterId:e.chapterId,schemaVersion:jh,stream:"reading-graph",type:"meta"},e.parameter!==void 0&&(yield{...e.parameter.language===void 0?{}:{language:e.parameter.language},prompt:e.parameter.prompt,scope:"reading-graph",type:"parameter"});for(let o of await Oh(e.document,e.chapterId))yield f9(o);let t=new Map,r=0;for(let o of await e.document.chunks.listBySerial(e.chapterId)){let s=`chunk-${r}`;r+=1,t.set(o.id,s),yield y9(e.chapterId,s,o)}for(let o of await e.document.readingEdges.listBySerial(e.chapterId)){let s=t.get(o.fromId),c=t.get(o.toId);s===void 0||c===void 0||(yield b9(s,c,o))}for(let o of await e.document.fragmentGroups.listBySerial(e.chapterId))yield w9(o);let n=new Map,i=0,a=await e.document.snakes.listBySerial(e.chapterId);for(let o of a){let s=`snake-${i}`;i+=1,n.set(o.id,s),yield S9(s,o);for(let c of await e.document.snakeChunks.listBySnake(o.id)){let d=t.get(c.chunkId);d!==void 0&&(yield{chunkId:d,position:c.position,snakeId:s,type:"snake-chunk"})}}for(let o of await e.document.snakeEdges.listBySerial(e.chapterId)){let s=n.get(o.fromSnakeId),c=n.get(o.toSnakeId);s===void 0||c===void 0||(yield{fromSnakeId:s,toSnakeId:c,type:"snake-edge",weight:o.weight})}yield{type:"end"}}async function iE(e,t){let r=[],n=[],i=[],a=[],o=[],s=[],c=[],d,u=!1,p=!1;for await(let m of t)switch(m.type){case"meta":if(m.chapterId!==e)throw new Error(`WikgObject stream belongs to chapter ${m.chapterId}, expected chapter ${e}.`);if(m.stream!=="reading-graph")throw new Error(`Expected reading-graph stream, received ${m.stream}.`);u=!0;break;case"parameter":m.scope==="reading-graph"&&(d=m);break;case"source-fragment":i.push(g9(e,m));break;case"reading-chunk":r.push(m);break;case"reading-edge":a.push(m);break;case"fragment-group":n.push(m);break;case"snake":c.push(m);break;case"snake-chunk":o.push(m);break;case"snake-edge":s.push(m);break;case"end":p=!0;break;default:throw new Error(`Unexpected ${m.type} record in reading graph stream.`)}if(!u)throw new Error("WikgObject stream is missing a meta record.");if(!p)throw new Error("WikgObject stream is missing an end record.");return k9({chunks:r,readingEdges:a,snakeChunks:o,snakeEdges:s,snakes:c}),{chunks:r,fragmentGroups:n,fragments:i,...d===void 0?{}:{parameter:d},readingEdges:a,snakeChunks:o,snakeEdges:s,snakes:c}}function l9(e){return{...e.confidence===void 0?{}:{confidence:e.confidence},...e.fragmentId===void 0?{}:{fragmentId:e.fragmentId},id:e.id,...e.note===void 0?{}:{note:e.note},qid:e.qid,rangeEnd:e.rangeEnd,rangeStart:e.rangeStart,...e.sentenceIndex===void 0?{}:{sentenceIndex:e.sentenceIndex},surface:e.surface,type:"mention"}}function p9(e,t){return{chapterId:e,...t.confidence===void 0?{}:{confidence:t.confidence},...t.fragmentId===void 0?{}:{fragmentId:t.fragmentId},id:t.id,...t.note===void 0?{}:{note:t.note},qid:t.qid,rangeEnd:t.rangeEnd,rangeStart:t.rangeStart,...t.sentenceIndex===void 0?{}:{sentenceIndex:t.sentenceIndex},surface:t.surface}}function m9(e,t){return{...t.confidence===void 0?{}:{confidence:t.confidence},evidenceSentenceIndexes:t.evidenceSentenceIds.map(r=>{if(r[0]!==e)throw new Error(`Mention link ${t.id} evidence sentence ${pD(r)} is outside chapter ${e}.`);return r[1]}),id:t.id,...t.note===void 0?{}:{note:t.note},predicate:t.predicate,sourceMentionId:t.sourceMentionId,targetMentionId:t.targetMentionId,type:"mention-link"}}function h9(e,t){return{...t.confidence===void 0?{}:{confidence:t.confidence},evidenceSentenceIds:t.evidenceSentenceIndexes.map(r=>[e,r]),id:t.id,...t.note===void 0?{}:{note:t.note},predicate:t.predicate,sourceMentionId:t.sourceMentionId,targetMentionId:t.targetMentionId}}function f9(e){return{fragmentId:e.fragmentId,sentences:e.sentences.map(t=>({text:t.text,wordsCount:t.wordsCount})),summary:e.summary,type:"source-fragment"}}function g9(e,t){return{fragmentId:t.fragmentId,sentences:t.sentences,serialId:e,summary:t.summary}}function y9(e,t,r){return{content:r.content,generation:r.generation,id:t,...r.importance===void 0?{}:{importance:r.importance},label:r.label,...r.retention===void 0?{}:{retention:r.retention},sentenceIndex:uD(e,r.sentenceId),sentenceIndexes:r.sentenceIds.map(n=>uD(e,n)),type:"reading-chunk",weight:r.weight,wordsCount:r.wordsCount}}function b9(e,t,r){return{fromChunkId:e,...r.strength===void 0?{}:{strength:r.strength},toChunkId:t,type:"reading-edge",weight:r.weight}}function w9(e){return{endSentenceIndex:e.endSentenceIndex,groupId:e.groupId,startSentenceIndex:e.startSentenceIndex,type:"fragment-group"}}function S9(e,t){return{firstLabel:t.firstLabel,groupId:t.groupId,id:e,lastLabel:t.lastLabel,localSnakeId:t.localSnakeId,size:t.size,type:"snake",weight:t.weight,wordsCount:t.wordsCount}}function k9(e){let t=new Set;for(let n of e.chunks){if(t.has(n.id))throw new Error(`Duplicate reading chunk id ${n.id}.`);t.add(n.id)}let r=new Set;for(let n of e.snakes){if(r.has(n.id))throw new Error(`Duplicate snake id ${n.id}.`);r.add(n.id)}for(let n of e.readingEdges)fs(t,n.fromChunkId,"chunk"),fs(t,n.toChunkId,"chunk");for(let n of e.snakeChunks)fs(r,n.snakeId,"snake"),fs(t,n.chunkId,"chunk");for(let n of e.snakeEdges)fs(r,n.fromSnakeId,"snake"),fs(r,n.toSnakeId,"snake")}function fs(e,t,r){if(!e.has(t))throw new Error(`Unknown ${r} id ${t}.`)}function uD(e,t){if(t[0]!==e)throw new Error(`Sentence ${pD(t)} is outside chapter ${e}.`);return t[1]}function pD(e){return e.join(":")}async function E9(e,t){await new Promise((r,n)=>{e.write(t,i=>{if(i!=null){n(i);return}r()})})}async function I9(e){await new Promise((t,r)=>{e.end(n=>{if(n!=null){r(n);return}t()})})}var jh,Ie,z3,Q3,lD,V3,X3,Y3,Z3,e9,t9,r9,n9,i9,a9,o9,s9,c9,d9,u9,ys=l(()=>{"use strict";Gr();Nh();jh=1,Ie=v.number().int().nonnegative(),z3=v.literal(jh),Q3=v.string().regex(/^Q[1-9][0-9]*$/u),lD=v.number().min(0).max(1).optional(),V3=v.object({text:v.string(),wordsCount:Ie}).strict(),X3=v.object({type:v.literal("meta"),schemaVersion:z3,chapterId:v.number().int(),stream:v.enum(["knowledge-graph","reading-graph","summary"])}).strict(),Y3=v.object({type:v.literal("parameter"),scope:v.enum(["knowledge-graph","reading-graph","summary"]),language:v.string().optional(),prompt:v.string()}).strict(),Z3=v.object({type:v.literal("source-fragment"),fragmentId:Ie,summary:v.string(),sentences:v.array(V3)}).strict(),e9=v.object({type:v.literal("reading-chunk"),id:v.string().min(1),generation:v.number().int(),sentenceIndex:Ie,label:v.string(),content:v.string(),sentenceIndexes:v.array(Ie).min(1),retention:v.enum(Dt).optional(),importance:v.enum(or).optional(),wordsCount:Ie,weight:v.number()}).strict(),t9=v.object({type:v.literal("reading-edge"),fromChunkId:v.string().min(1),toChunkId:v.string().min(1),strength:v.string().optional(),weight:v.number()}).strict(),r9=v.object({type:v.literal("fragment-group"),groupId:Ie,startSentenceIndex:Ie,endSentenceIndex:Ie}).strict(),n9=v.object({type:v.literal("snake"),id:v.string().min(1),groupId:Ie,localSnakeId:Ie,size:Ie,firstLabel:v.string(),lastLabel:v.string(),wordsCount:Ie,weight:v.number()}).strict(),i9=v.object({type:v.literal("snake-chunk"),snakeId:v.string().min(1),chunkId:v.string().min(1),position:Ie}).strict(),a9=v.object({type:v.literal("snake-edge"),fromSnakeId:v.string().min(1),toSnakeId:v.string().min(1),weight:v.number()}).strict(),o9=v.object({type:v.literal("summary"),text:v.string()}).strict(),s9=v.object({type:v.literal("mention"),id:v.string().min(1),fragmentId:Ie.optional(),sentenceIndex:Ie.optional(),rangeStart:Ie,rangeEnd:Ie,surface:v.string().min(1),qid:Q3,confidence:lD,note:v.string().optional()}).strict(),c9=v.object({type:v.literal("mention-link"),id:v.string().min(1),sourceMentionId:v.string().min(1),targetMentionId:v.string().min(1),predicate:v.string().min(1),evidenceSentenceIndexes:v.array(Ie).min(1),confidence:lD,note:v.string().optional()}).strict(),d9=v.object({type:v.literal("end")}).strict(),u9=v.discriminatedUnion("type",[X3,Y3,Z3,e9,t9,r9,n9,i9,a9,o9,s9,c9,d9])});function mD(e){return{extractionPrompt:et(e.extractionPrompt),...e.userLanguage===void 0?{}:{userLanguage:e.userLanguage}}}function hD(e){let t=Ua(e.userLanguage);return{...t===void 0?{}:{language:t},prompt:et(e.extractionPrompt)}}var fD=l(()=>{"use strict";Nr();ui()});async function gD(e,t,r){let n=new Li(e),i=n.getSerial(t),a=await i.createDraft(),o=0,s=!1;await n.ensureCreated();for await(let c of ci(r)){let d=c.text.trim();d!==""&&(o>0&&o+c.wordsCount>x9&&(await a.commit(),a=await i.createDraft(),o=0),a.addSentence(d,c.wordsCount),o+=c.wordsCount,s=!0)}s?await a.commit():a.discard()}var x9,yD=l(()=>{"use strict";X();nh();x9=320});import{mkdir as v9,rm as C9}from"fs/promises";import{join as bD}from"path";async function Dh(e,t){let r=bD(t.workspacePath,"graph-document"),n=bD(t.workspacePath,"reading-graph.jsonl"),i=hD(t);await C9(r,{force:!0,recursive:!0}),await v9(t.workspacePath,{recursive:!0});let a=await ye.open(r);try{await a.openSession(async o=>{await o.serials.createWithId(e),await gD(r,e,t.sourceText);let s=Tc(o,r);await new hn({document:s,llm:t.llm,...t.logDirPath===void 0?{}:{logDirPath:t.logDirPath}}).buildTopologyInto(e,mD(t),t.progressTracker)}),await ha(n,Ou({chapterId:e,document:Tc(a,r),parameter:i}))}finally{await a.release()}return{chapterId:e,documentPath:r,objectsPath:n,parameter:i}}async function Mh(e,t){let r=await iE(t.chapterId,gs(t.objectsPath));return await e.openSession(async n=>{await Wo(n,t.chapterId,"sourced"),await n.clearSerialGraph(t.chapterId),await n.serials.ensure(t.chapterId);let i=new Map;for(let s of r.chunks){let c=await n.chunks.create({content:s.content,generation:s.generation,label:s.label,sentenceId:[t.chapterId,s.sentenceIndex],sentenceIds:s.sentenceIndexes.map(d=>[t.chapterId,d]),weight:s.weight,wordsCount:s.wordsCount,...s.importance===void 0?{}:{importance:s.importance},...s.retention===void 0?{}:{retention:s.retention}});i.set(s.id,c.id)}for(let s of r.readingEdges)await n.readingEdges.save({fromId:bs(i,s.fromChunkId,"chunk"),...s.strength===void 0?{}:{strength:s.strength},toId:bs(i,s.toChunkId,"chunk"),weight:s.weight});await n.fragmentGroups.saveMany(r.fragmentGroups.map(s=>({endSentenceIndex:s.endSentenceIndex,groupId:s.groupId,serialId:t.chapterId,startSentenceIndex:s.startSentenceIndex})));let a=new Map;for(let s of r.snakes){let c=await n.snakes.create({firstLabel:s.firstLabel,groupId:s.groupId,lastLabel:s.lastLabel,localSnakeId:s.localSnakeId,serialId:t.chapterId,size:s.size,weight:s.weight,wordsCount:s.wordsCount});a.set(s.id,c)}for(let s of r.snakeChunks)await n.snakeChunks.save({chunkId:bs(i,s.chunkId,"chunk"),position:s.position,snakeId:bs(a,s.snakeId,"snake")});for(let s of r.snakeEdges)await n.snakeEdges.save({fromSnakeId:bs(a,s.fromSnakeId,"snake"),toSnakeId:bs(a,s.toSnakeId,"snake"),weight:s.weight});let o=await n.graphBuildParameters.save(r.parameter??t.parameter);await n.serials.setTopologyReady(t.chapterId,!0,o.hash)}),await se(e,t.chapterId)}function bs(e,t,r){let n=e.get(t);if(n===void 0)throw new Error(`Unknown ${r} id ${t}.`);return n}var wD=l(()=>{"use strict";X();Ze();ls();ys();fD();yD()});function fa(e,t){return e-t}function SD(e){let t=new Map;for(let r of e){let n=t.get(r.serialId)??[];n.push(r.fragmentId),t.set(r.serialId,n)}return new Map([...t.entries()].map(([r,n])=>[r,n.sort(fa)]))}function kD(e,t){let r=new Set(t);return e.flatMap((n,i)=>{if(!r.has(n))return[];let a=e[i+1];return[{endSentenceIndex:a===void 0?1/0:a-1,startSentenceIndex:n}]})}function ED(e,t){return e.id-t.id}function ID(e,t){return e.serialId-t.serialId||e.groupId-t.groupId||e.startSentenceIndex-t.startSentenceIndex||e.endSentenceIndex-t.endSentenceIndex}function xD(e,t){return e.fromId-t.fromId||e.toId-t.toId}function vD(e,t){return e[0]-t[0]||e[1]-t[1]}function CD(e,t){return e.groupId-t.groupId||e.id-t.id}function TD(e,t){return e.snakeId-t.snakeId||e.position-t.position}function RD(e,t){return e.fromSnakeId-t.fromSnakeId||e.toSnakeId-t.toSnakeId}var Wh=l(()=>{"use strict"});var Fh,PD=l(()=>{"use strict";Wh();Fh=class{#e;#t;#r;constructor(t,r){this.#e=[...t].sort(ED),this.#t=new Map(t.map(n=>[n.id,n])),this.#r=SD(r)}getById(t){return Promise.resolve(this.#t.get(t))}countAll(){return Promise.resolve(this.#e.length)}listAll(){return Promise.resolve([...this.#e])}listBySentenceStartIndexes(t,r){let n=kD(this.#r.get(t)??[],r);return Promise.resolve(this.#e.filter(i=>i.sentenceId[0]===t&&n.some(a=>i.sentenceId[1]>=a.startSentenceIndex&&i.sentenceId[1]<=a.endSentenceIndex)))}listBySentenceRange(t,r,n){return Promise.resolve(this.#e.filter(i=>i.sentenceId[0]===t&&i.sentenceId[1]>=r&&i.sentenceId[1]<=n))}listBySerial(t){return Promise.resolve(this.#e.filter(r=>r.sentenceId[0]===t))}getMaxId(){return Promise.resolve(this.#e.reduce((t,r)=>Math.max(t,r.id),0))}listFragmentPairs(){let t=new Set;for(let r of this.#e)t.add(`${r.sentenceId[0]}:${r.sentenceId[1]}`);return Promise.resolve([...t].map(r=>r.split(":").map(Number)).sort(vD).map(([r,n])=>[r,n]))}}});var Gh,Uh,$h,Bh,_D=l(()=>{"use strict";Gh=class{countByQid(t,r){return Promise.resolve(0)}getById(t){return Promise.resolve(void 0)}listAll(){return Promise.resolve([])}listByQid(t,r){return Promise.resolve([])}listLabelsByQid(t,r){return Promise.resolve([])}listBySurfaceTerms(t){return Promise.resolve([])}listBySurfaces(t){return Promise.resolve([])}listByChapter(t){return Promise.resolve([])}},Uh=class{countByTriple(t){return Promise.resolve(0)}getById(t){return Promise.resolve(void 0)}listAll(){return Promise.resolve([])}listByTriple(t){return Promise.resolve([])}listByChapter(t){return Promise.resolve([])}},$h=class{getMap(t){return Promise.resolve({})}},Bh=class{getByHash(t){return Promise.resolve(void 0)}}});var Hh,qh,Kh,Jh,zh,AD=l(()=>{"use strict";Wh();Hh=class{#e;#t;constructor(t,r){this.#e=[...t].sort(xD),this.#t=new Map(r.map(n=>[n.id,n.sentenceId[0]]))}listAll(){return Promise.resolve([...this.#e])}countAll(){return Promise.resolve(this.#e.length)}listBySerial(t){return Promise.resolve(this.#e.filter(r=>this.#t.get(r.fromId)===t&&this.#t.get(r.toId)===t))}listIncoming(t){return Promise.resolve(this.#e.filter(r=>r.toId===t))}listOutgoing(t){return Promise.resolve(this.#e.filter(r=>r.fromId===t))}},qh=class{#e;#t;constructor(t){this.#e=[...t].sort(CD),this.#t=new Map(t.map(r=>[r.id,r]))}getById(t){return Promise.resolve(this.#t.get(t))}listIdsByGroup(t,r){return Promise.resolve(this.#e.filter(n=>n.serialId===t&&n.groupId===r).map(n=>n.id).sort(fa))}listBySerial(t){return Promise.resolve(this.#e.filter(r=>r.serialId===t))}},Kh=class{#e;constructor(t){this.#e=[...t].sort(TD)}listChunkIds(t){return Promise.resolve(this.#e.filter(r=>r.snakeId===t).map(r=>r.chunkId))}listBySnake(t){return Promise.resolve(this.#e.filter(r=>r.snakeId===t))}},Jh=class{#e;#t;constructor(t,r){this.#e=[...t].sort(RD),this.#t=new Map(r.map(n=>[n.id,n.serialId]))}listIncoming(t){return Promise.resolve(this.#e.filter(r=>r.toSnakeId===t))}listOutgoing(t){return Promise.resolve(this.#e.filter(r=>r.fromSnakeId===t))}listWithin(t){let r=new Set(t);return Promise.resolve(this.#e.filter(n=>r.has(n.fromSnakeId)&&r.has(n.toSnakeId)))}listBySerial(t){return Promise.resolve(this.#e.filter(r=>this.#t.get(r.fromSnakeId)===t&&this.#t.get(r.toSnakeId)===t))}},zh=class{#e;constructor(t){this.#e=[...t].sort(ID)}listBySerial(t){return Promise.resolve(this.#e.filter(r=>r.serialId===t))}listSerialIds(){return Promise.resolve([...new Set(this.#e.map(t=>t.serialId))].sort(fa))}listGroupIdsForSerial(t){return Promise.resolve([...new Set(this.#e.filter(r=>r.serialId===t).map(r=>r.groupId))].sort(fa))}}});var Qh,ws,LD=l(()=>{"use strict";Wh();Qh=class{#e;constructor(t){this.#e=t}getById(t){return Promise.resolve(t===this.#e.id?this.#e:void 0)}getRevision(t){return Promise.resolve(t===this.#e.id?this.#e.revision:0)}getRevisions(t){return Promise.resolve(new Map(t.filter(r=>r===this.#e.id).map(r=>[r,this.#e.revision])))}getChaptersRevision(){return Promise.resolve(this.#e.revision)}getMaxId(){return Promise.resolve(this.#e.id)}listIds(){return Promise.resolve([this.#e.id])}listDocumentOrders(){return Promise.resolve(new Map([[this.#e.id,this.#e.documentOrder]]))}},ws=class{path="";serialId;#e;constructor(t,r){this.serialId=t,this.#e=new Map(r.filter(n=>n.serialId===t).map(n=>[n.fragmentId,n]))}getFragment(t){let r=this.#e.get(t);if(r===void 0)throw new Error(`Fragment ${t} does not exist`);return Promise.resolve(r)}listFragmentIds(){return Promise.resolve([...this.#e.keys()].sort(fa))}async getSentence(t){let r=(await this.listSentences())[t];if(r===void 0)throw new RangeError(`Sentence ${t} does not exist`);return r}async listSentencesInRange(t,r){return(await this.listSentences()).slice(t,r+1)}async listSentences(){return(await Promise.all((await this.listFragmentIds()).map(async r=>await this.getFragment(r)))).flatMap(r=>r.sentences)}async readText(){let t=await this.listSentences();if(t.length!==0)return t.map(r=>r.text).join("")}}});var Vh,OD=l(()=>{"use strict";PD();_D();AD();LD();Vh=class{chunks;fragmentGroups;graphBuildParameters;readingEdges;mentionLinks;mentions;metadata;serials;snakeChunks;snakeEdges;snakes;#e;constructor(t){let r=t.serial.id;this.chunks=new Fh(t.chunks,t.fragments),this.fragmentGroups=new zh(t.fragmentGroups),this.graphBuildParameters=new Bh,this.readingEdges=new Hh(t.readingEdges,t.chunks),this.mentionLinks=new Uh,this.mentions=new Gh,this.metadata=new $h,this.serials=new Qh(t.serial),this.snakeChunks=new Kh(t.snakeChunks),this.snakeEdges=new Jh(t.snakeEdges,t.snakes),this.snakes=new qh(t.snakes),this.#e=new ws(r,t.fragments)}async getSentence(t){let[r,n]=t,i=(await this.getSerialFragments(r).listSentences())[n];if(i===void 0)throw new RangeError(`Sentence ${n} does not exist`);return i.text}getSerialFragments(t){return t!==this.#e.serialId?new ws(t,[]):this.#e}getSummaryFragments(t){return new ws(t,[])}async openSession(t){return await t(this)}readDatabase(){return Promise.reject(new Error("Summary input snapshots do not expose a SQLite database."))}readSearchIndexDatabase(){return Promise.reject(new Error("Summary input snapshots do not expose a search index database."))}readBookMeta(){return Promise.resolve(void 0)}readCover(){return Promise.resolve(void 0)}readSummary(t){return Promise.resolve(void 0)}readToc(){return Promise.resolve(void 0)}release(){return Promise.resolve()}}});import{z as P}from"zod";function DD(e){return{id:e.id,generation:e.generation,sentenceId:e.sentenceId,label:e.label,content:e.content,sentenceIds:e.sentenceIds,...e.retention===void 0?{}:{retention:e.retention},...e.importance===void 0?{}:{importance:e.importance},wordsCount:e.wordsCount,weight:e.weight}}function MD(e){return{fromId:e.fromId,toId:e.toId,...e.strength===void 0?{}:{strength:e.strength},weight:e.weight}}var ND,T9,R9,P9,_9,A9,L9,O9,N9,j9,jD,WD=l(()=>{"use strict";Gr();ND=P.tuple([P.number(),P.number()]),T9=P.object({text:P.string(),wordsCount:P.number()}),R9=P.object({serialId:P.number(),fragmentId:P.number(),summary:P.string(),sentences:P.array(T9)}),P9=P.object({documentOrder:P.number().optional(),id:P.number(),knowledgeGraphReady:P.boolean(),knowledgeGraphParameterHash:P.string().optional(),revision:P.number().optional(),topologyParameterHash:P.string().optional(),topologyReady:P.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}})),_9=P.object({id:P.number(),generation:P.number(),sentenceId:ND,label:P.string(),content:P.string(),sentenceIds:P.array(ND),retention:P.enum(Dt).optional(),importance:P.enum(or).optional(),wordsCount:P.number(),weight:P.number()}),A9=P.object({fromId:P.number(),toId:P.number(),strength:P.string().optional(),weight:P.number()}),L9=P.object({id:P.number(),serialId:P.number(),groupId:P.number(),localSnakeId:P.number(),size:P.number(),firstLabel:P.string(),lastLabel:P.string(),wordsCount:P.number(),weight:P.number()}),O9=P.object({snakeId:P.number(),chunkId:P.number(),position:P.number()}),N9=P.object({fromSnakeId:P.number(),toSnakeId:P.number(),weight:P.number()}),j9=P.object({serialId:P.number(),groupId:P.number(),startSentenceIndex:P.number(),endSentenceIndex:P.number()}),jD=P.object({chunks:P.array(_9),fragmentGroups:P.array(j9),fragments:P.array(R9),readingEdges:P.array(A9),serial:P9,snakeChunks:P.array(O9),snakeEdges:P.array(N9),snakes:P.array(L9)})});import{readFile as D9,writeFile as M9}from"fs/promises";async function FD(e){let t=jD.parse(JSON.parse(await D9(e,"utf8")));return{...t,chunks:t.chunks.map(DD),readingEdges:t.readingEdges.map(MD)}}async function GD(e,t){await M9(e,`${JSON.stringify(t)}
|
|
2514
|
+
`,"utf8")}var UD=l(()=>{"use strict";WD()});async function $D(e,t,r){let n=e.getSerialFragments(t),i=await n.listFragmentIds();if(i.length<=1)return await W9(n,i);let a=[];for(let o of await e.fragmentGroups.listGroupIdsForSerial(t)){let s=await au({compressionRatio:.2,document:e,groupId:o,llm:r.llm,maxClues:10,maxIterations:5,scopes:ac,serialId:t,...r.logDirPath===void 0?{}:{logDirPath:r.logDirPath},...r.userLanguage===void 0?{}:{userLanguage:r.userLanguage}});s.trim()!==""&&a.push(s)}return a.join(`
|
|
2515
|
+
|
|
2516
|
+
`)}async function W9(e,t){return t.length===0?"":(await Promise.all(t.map(async n=>await e.getFragment(n)))).flatMap(n=>n.sentences.map(i=>i.text)).join(" ").trim()}var BD=l(()=>{"use strict";oc();mk()});import{mkdir as G9}from"fs/promises";import{join as HD}from"path";async function Xh(e,t){let r=await FD(t.snapshotPath);return await U9(r,e,t)}async function Yh(e,t,r){return await e.openSession(async n=>{await Wo(n,t,"graphed"),await n.writeSummary(t,r)}),await se(e,t)}async function Zh(e,t,r){let n=HD(r,"summary-input.json"),i=HD(r,"reading-graph.jsonl");await G9(r,{recursive:!0}),await Wo(e,t,"graphed");let a=await Oh(e,t),o=await e.snakes.listBySerial(t),s=(await Promise.all(o.map(async c=>await e.snakeChunks.listBySnake(c.id)))).flat();return await GD(n,{chunks:await e.chunks.listBySerial(t),fragmentGroups:await e.fragmentGroups.listBySerial(t),fragments:a,readingEdges:await e.readingEdges.listBySerial(t),serial:{documentOrder:t,id:t,knowledgeGraphReady:!1,revision:0,topologyReady:!0},snakeChunks:s,snakeEdges:await e.snakeEdges.listBySerial(t),snakes:o}),await ha(i,Ou({chapterId:t,document:e})),{filePath:n,objectsPath:i}}async function U9(e,t,r){if(e.serial.id!==t)throw new Error(`Summary snapshot belongs to chapter ${e.serial.id}, not chapter ${t}.`);if(!e.serial.topologyReady)throw new Error(`Chapter ${t} is not ready for summary.`);return await $D(new Vh(e),t,r)}var qD=l(()=>{"use strict";X();Ze();OD();UD();Nh();BD();ys()});var KD=l(()=>{"use strict"});var aE=l(()=>{"use strict";qD();Nh();KD()});async function Ss(e,t){return{details:await se(e,t),revision:await e.serials.getRevision(t),sourceText:await dD(cD(e,t))}}var JD=l(()=>{"use strict";Ze();aE()});var zD=l(()=>{"use strict"});var QD=l(()=>{"use strict";wD();JD();zD()});var VD=l(()=>{"use strict";QD();aE()});import{createReadStream as uPe,createWriteStream as $9}from"fs";import{createInterface as pPe}from"readline";import{z as me}from"zod";function XD(e,t){let r=new Set;for(let i of t.mentions){if(i.chapterId!==e)throw new Error(`Mention ${i.id} belongs to chapter ${i.chapterId}, expected chapter ${e}.`);if(i.rangeEnd<=i.rangeStart)throw new Error(`Mention ${i.id} has invalid range [${i.rangeStart}, ${i.rangeEnd}).`);if(r.has(i.id))throw new Error(`Duplicate mention id ${i.id}.`);r.add(i.id)}let n=new Set;for(let i of t.mentionLinks){if(n.has(i.id))throw new Error(`Duplicate mention link id ${i.id}.`);if(!r.has(i.sourceMentionId))throw new Error(`Mention link ${i.id} references unknown source mention ${i.sourceMentionId}.`);if(!r.has(i.targetMentionId))throw new Error(`Mention link ${i.id} references unknown target mention ${i.targetMentionId}.`);if(i.evidenceSentenceIds.length===0)throw new Error(`Mention link ${i.id} has no evidence sentences.`);for(let a of i.evidenceSentenceIds)if(a[0]!==e)throw new Error(`Mention link ${i.id} evidence sentence ${K9(a)} is outside chapter ${e}.`);n.add(i.id)}}function K9(e){return e.join(":")}async function oE(e,t,r){let n=$9(e,{encoding:"utf8",flags:"wx"});try{for await(let i of t)n.write(`${JSON.stringify(r(i))}
|
|
2517
|
+
`)}finally{await J9(n)}}function sE(e){let t=B9.parse(e);return{chapterId:t.chapterId,...t.confidence===void 0?{}:{confidence:t.confidence},id:t.id,...t.note===void 0?{}:{note:t.note},qid:t.qid,rangeEnd:t.rangeEnd,rangeStart:t.rangeStart,...t.sentenceIndex===void 0?{}:{sentenceIndex:t.sentenceIndex},surface:t.surface}}function cE(e){let t=q9.parse(e);return{...t.confidence===void 0?{}:{confidence:t.confidence},evidenceSentenceIds:t.evidenceSentenceIds,id:t.id,...t.note===void 0?{}:{note:t.note},predicate:t.predicate,sourceMentionId:t.sourceMentionId,targetMentionId:t.targetMentionId}}async function J9(e){await new Promise((t,r)=>{e.end(n=>{if(n!=null){r(n);return}t()})})}var B9,H9,q9,dE=l(()=>{"use strict";B9=me.object({id:me.string().min(1),chapterId:me.number().int(),sentenceIndex:me.number().int().nonnegative().optional(),rangeStart:me.number().int().nonnegative(),rangeEnd:me.number().int().nonnegative(),surface:me.string().min(1),qid:me.string().regex(/^Q[1-9][0-9]*$/),confidence:me.number().min(0).max(1).optional(),note:me.string().optional()}),H9=me.tuple([me.number().int().nonnegative(),me.number().int().nonnegative()]).readonly(),q9=me.object({id:me.string().min(1),sourceMentionId:me.string().min(1),targetMentionId:me.string().min(1),predicate:me.string().min(1),evidenceSentenceIds:me.array(H9).min(1),confidence:me.number().min(0).max(1).optional(),note:me.string().optional()})});import{mkdir as z9,rm as Q9}from"fs/promises";import{join as ef}from"path";async function tf(e,t){let r=ef(t.workspacePath,"knowledge-graph",`chapter-${e}`),n=ef(r,"mentions.jsonl"),i=ef(r,"mention-links.jsonl"),a=ef(r,"wikg-objects.jsonl"),o=t.parameter??{language:"zh",prompt:""};await Q9(r,{force:!0,recursive:!0}),await z9(r,{recursive:!0});let s=await YD(t.mentions,sE),c=await YD(t.mentionLinks,cE);return await ha(a,rE({chapterId:e,mentionLinks:c,mentions:s,parameter:o})),await oE(n,s,sE),await oE(i,c,cE),{chapterId:e,mentionLinksPath:i,mentionsPath:n,objectsPath:a,parameter:o,workspacePath:r}}async function YD(e,t){let r=[];for await(let n of e)r.push(t(n));return r}var uE=l(()=>{"use strict";Nr();ys();dE()});async function rf(e,t){let r=await nE(t.chapterId,gs(t.objectsPath)),n=r.mentions,i=r.mentionLinks,a=r.parameter??t.parameter;XD(t.chapterId,{mentionLinks:i,mentions:n}),await e.openSession(async o=>{if(await o.serials.ensure(t.chapterId),(await o.mentionLinks.listByChapter(t.chapterId)).length>0&&i.length===0)throw new Error(`Refusing to replace chapter ${t.chapterId} knowledge graph with an artifact that contains no mention links.`);await o.mentionLinks.deleteByChapter(t.chapterId),await o.mentions.deleteByChapter(t.chapterId),await o.mentions.saveMany(n),await o.mentionLinks.saveMany(i);let c=await o.graphBuildParameters.save(a);await o.serials.setKnowledgeGraphReady(t.chapterId,!0,c.hash)})}var eM=l(()=>{"use strict";ys();dE()});function lE(e){return e.qidOptions.reduce((t,r)=>t+tM(r),0)}function tM(e){return e.disambiguation===void 0?1:rM(e).length}function ks(e){return[...new Set(e.qidOptions.flatMap(t=>rM(t)))]}function nf(e,t){return{...e,qidOptions:e.qidOptions.flatMap(r=>{let n=V9(r,t);return n===void 0?[]:[n]})}}function V9(e,t){if(e.disambiguation===void 0)return t.has(e.qid)?e:void 0;let n=(e.disambiguation.profile?.meanings??[]).length>0,i=n?e.disambiguation.linkedQids:e.disambiguation.linkedQids.filter(s=>t.has(s.qid)),a=X9(e.disambiguation.profile,t),o=(a?.meanings.length??0)>0;if(!(n&&!o)&&!(i.length===0&&!o))return{...e,disambiguation:{...e.disambiguation,linkedQids:i,...a===void 0?{}:{profile:a}}}}function X9(e,t){if(e!==void 0)return{...e,meanings:e.meanings.filter(r=>t.has(r.qid))}}function rM(e){if(e.disambiguation===void 0)return[e.qid];let t=e.disambiguation.profile?.meanings.map(r=>r.qid)??[];return t.length>0?[...new Set(t)]:[...new Set(e.disambiguation.linkedQids.map(r=>r.qid))]}var Nu=l(()=>{"use strict"});import{z as wn}from"zod";async function af(e){try{return await Ae({messages:d5(e),parse:t=>pE(e.candidates,t5(t),e.window.groups),request:e.request,responseIntentClassifierPrompt:Me,schema:e5,...e.maxRetries===void 0?{}:{maxRetries:e.maxRetries}})}catch(t){return{continuations:[],fallback:{issues:[y5(t)],reason:"guaranteed_json_failed"},mentions:[],policyUpdates:[]}}}function t5(e){return{groups:e.groups.map(t=>({decisions:t.decisions.map(r=>({candidateId:r.candidateId,decision:r.decision,...r5(r.qid)})),groupId:t.groupId}))}}function r5(e){let t=e?.trim();return t===void 0||t===""?{}:{qid:t}}function pE(e,t,r=iM(e)){let n=mE(e,t,r);if(n.length>0)throw new Y(n);let i=hE(e),a=[],o=[],s=[];for(let c of t.groups){let d=[];for(let u of c.decisions){let p=i.get(u.candidateId);if(u.decision==="continue"){if(p.hasMoreOptions!==!0){s.push({candidateId:p.id,decision:"skip_this_time",surface:p.surface});continue}d.push(p.id);continue}if(u.decision==="recall"){o.push({candidateId:p.id,qid:u.qid,range:p.range,surface:p.surface});continue}s.push({candidateId:p.id,decision:u.decision,...u.qid===void 0?{}:{qid:u.qid},surface:p.surface})}d.length>0&&a.push({candidateIds:d,groupId:c.groupId})}return{continuations:a,mentions:o,policyUpdates:s}}function mE(e,t,r=iM(e)){let n=[],i=hE(e),a=c5(r),o=new Set,s=new Set,c=[];for(let d of t.groups){let u=a.get(d.groupId);if(u===void 0){n.push(`Unknown groupId "${d.groupId}". Use exactly these group IDs: ${[...a.keys()].join(", ")}.`);continue}if(o.has(d.groupId)){n.push(`Duplicate group result for ${d.groupId}.`);continue}o.add(d.groupId);for(let p of d.decisions){let m=i.get(p.candidateId);if(m===void 0){n.push(`Unknown candidateId "${p.candidateId}". Use only one of: ${[...i.keys()].join(", ")}.`);continue}if(!u.has(p.candidateId)){n.push(`Candidate ${p.candidateId} does not belong to group ${d.groupId}. Use only candidates from that group: ${[...u].join(", ")}.`);continue}if(s.has(p.candidateId)){n.push(`Duplicate decision for candidate ${p.candidateId}. Return exactly one decision per candidate at most.`);continue}if(s.add(p.candidateId),p.decision==="recall"){n5(n,m,p),c.push({candidate:m,decision:p});continue}if(p.decision==="continue"){p.qid!==void 0&&n.push(`Candidate ${m.id} uses decision "continue" but includes qid. Continue means this candidate page has no final choice yet.`);continue}p.qid!==void 0&&n.push(`Candidate ${m.id} uses decision "${p.decision}" but includes qid. Include qid only when decision is "recall".`)}}for(let d of a.keys())o.has(d)||n.push(`Missing group result for ${d}. Return exactly one result for every input group; use decisions: [] when nothing should be recalled.`);for(let d=0;d<c.length;d+=1)for(let u=d+1;u<c.length;u+=1){let p=c[d],m=c[u];s5(p.candidate.range,m.candidate.range)&&n.push(`Decision conflict: candidates ${p.candidate.id} and ${m.candidate.id} both use decision "recall", but their ranges overlap.
|
|
2518
|
+
- ${p.candidate.id}: "${p.candidate.surface}" [${p.candidate.range.start}, ${p.candidate.range.end})
|
|
2519
|
+
- ${m.candidate.id}: "${m.candidate.surface}" [${m.candidate.range.start}, ${m.candidate.range.end})
|
|
2520
|
+
Only one recalled mention may occupy an overlapping text range. Choose the most specific valid mention, or mark the others as skip_this_time / never_recall.`)}return n}function n5(e,t,r){if(r.qid===void 0){e.push(`Candidate ${t.id} uses decision "recall" but does not provide qid. A recalled mention must choose one concrete QID from the candidate options.`);return}if(a5(t,r.qid)){e.push(`Candidate ${t.id} selected source disambiguation QID ${r.qid}. Source disambiguation QIDs are hidden behind DIS references and cannot be final mention groundings. Choose a QID from entityOptions or disambiguationOptions.meanings instead.`);return}i5(t,r.qid)||e.push(o5(t,r.qid))}function i5(e,t){return nM(e).includes(t)}function a5(e,t){return e.qidOptions.some(r=>r.disambiguation!==void 0&&r.qid===t)}function nM(e){return ks(e)}function o5(e,t){return`Candidate ${e.id} selected qid ${t}, but that QID is not available for this candidate. Allowed QIDs for "${e.surface}" are: ${nM(e).join(", ")}.`}function s5(e,t){return e.start<t.end&&t.start<e.end}function hE(e){return new Map(e.map(t=>[t.id,t]))}function c5(e){return new Map(e.map(t=>[t.id,new Set(t.candidateIds)]))}function iM(e){return e.length===0?[]:[{candidateIds:e.map(t=>t.id),id:"g1",range:{end:Math.max(...e.map(t=>t.range.end)),start:Math.min(...e.map(t=>t.range.start))}}]}function d5(e){return[{role:"system",content:u5(e)},{role:"user",content:l5(e)}]}function u5(e){return["You judge which precomputed Wikidata mention candidates should be recalled.","","Recall policy:",e.policyPrompt,"","Rules:","- Return JSON only.","- Return exactly one group result for every input group.","- Use decisions: [] only when this group has no selected mention and no policy update.","- First judge whether each candidate mention is worth recalling in this exact tagged context, before choosing among QID candidates.",'- If the mention is not worth recalling in context, use "never_recall" immediately; do not inspect later pages just because more QIDs might exist.','- Use "never_recall" when this exact candidate span is not a standalone knowledge graph object in context.',"- Candidate lists may be incomplete when a candidate has more pages.",'- Use decision "continue" only after the mention has passed the context-worthiness test and the current candidate page has no good QID but more pages are available.','- "continue" is not a rejection. It asks to inspect the next candidate page for that candidate.','- If a surface should be recalled but the current incomplete page lacks a suitable QID, use "continue" instead of skip_this_time.','- Do not use "continue" for single-character surnames, abbreviations, side labels, generic words, fragments, roles, quantities, attributes, or discourse words that are not useful standalone knowledge graph objects in context.',"- Do not invent candidates, ranges, surfaces, or QIDs.","- A recalled mention must choose a QID from entityOptions or disambiguationOptions.meanings.",'- Include qid only when decision is "recall"; do not include qid for continue, skip_this_time, or never_recall.',"- Never return DIS identifiers as qid values; DIS identifiers are only disambiguation references.","- Overlapping recalled ranges are illegal; choose the most specific valid mention."].join(`
|
|
2521
|
+
`)}function l5(e){return["Tagged context:",p5(e),"","Candidate groups:",m5(e).map(t=>JSON.stringify(t)).join(`
|
|
2522
|
+
`),"","Return this JSON shape:",JSON.stringify({groups:[{decisions:[{candidateId:"candidate id from this group",decision:"recall | continue | skip_this_time | never_recall",qid:"required only when decision is recall"}],groupId:"group id from the input"}]})].join(`
|
|
2523
|
+
`)}function p5(e){let t=[...e.window.groups].sort((i,a)=>i.range.start-a.range.start),r=[],n=e.window.baseOffset;for(let i of t)r.push(e.window.text.slice(n-e.window.baseOffset,i.range.start-e.window.baseOffset)),r.push(`<group id="${g5(i.id)}">${aM(e.window.text.slice(i.range.start-e.window.baseOffset,i.range.end-e.window.baseOffset))}</group>`),n=i.range.end;return r.push(e.window.text.slice(n-e.window.baseOffset)),r.join("")}function m5(e){let t=hE(e.window.candidates);return e.window.groups.map(r=>({candidates:r.candidateIds.flatMap(n=>{let i=t.get(n);return i===void 0?[]:[fE(i)]}),groupId:r.id}))}function fE(e,t){let r=h5(e.qidOptions);return{candidateId:e.id,...e.hasMoreOptions===!0?{hasMoreOptions:!0}:{},...r.disambiguationOptions.length===0?{}:{disambiguationOptions:r.disambiguationOptions},...r.entityOptions.length===0?{}:{entityOptions:r.entityOptions}}}function h5(e){let t=[],r=[];for(let n of e){if(n.disambiguation!==void 0){let i=n.disambiguation.profile?.meanings??[];t.push({...n.label===void 0?{}:{label:n.label},meanings:i.length>0?i.map(f5):n.disambiguation.linkedQids.map(a=>({name:a.title,qid:a.qid}))});continue}r.push({...n.description===void 0?{}:{description:n.description},...n.label===void 0?{}:{label:n.label},qid:n.qid})}return{disambiguationOptions:t,entityOptions:r}}function f5(e){return{...e.information===""?{}:{information:e.information},name:e.name,qid:e.qid}}function g5(e){return aM(e).replaceAll('"',""")}function aM(e){return e.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")}function y5(e){if(e instanceof Y)return e.issues.join(`
|
|
2524
|
+
`);if(e instanceof Error){let t=e.issues;return Array.isArray(t)&&t.every(r=>typeof r=="string")?t.join(`
|
|
2525
|
+
`):e.message}return String(e)}var Y9,Z9,e5,gE=l(()=>{"use strict";Rt();Nu();Y9=wn.object({candidateId:wn.string().min(1),decision:wn.enum(["continue","recall","skip_this_time","never_recall"]),qid:wn.string().nullable().optional()}).strict(),Z9=wn.object({decisions:wn.array(Y9),groupId:wn.string().min(1)}).strict(),e5=wn.object({groups:wn.array(Z9)}).strict()});import{z as ju}from"zod";var w5,OPe,oM=l(()=>{"use strict";Rt();Nu();gE();w5=ju.object({decision:ju.enum(["keep","reject"]),qid:ju.string().regex(/^Q[1-9][0-9]*$/)}).strict(),OPe=ju.object({qids:ju.array(w5)}).strict()});async function bE(e,t={}){if(e.length===0)return[];if(t.resolver!==void 0)return yE(e,await t.resolver.resolveQids(sM(e)));let r=await ri.open({...t.resolverOptions??{},...t.progress===void 0?{}:{progress:t.progress}});try{return yE(e,await r.resolveQids(sM(e)))}finally{await r.close()}}function yE(e,t){let r=new Map(t.map(n=>[n.qid,n]));return e.map(n=>({...n,qidOptions:n.qidOptions.map(i=>S5(i,r.get(i.qid)))}))}function S5(e,t){return t===void 0?e:{...e,...t.description===void 0?{}:{description:t.description},...t.disambiguation===void 0?{}:{disambiguation:t.disambiguation},isDisambiguation:t.isDisambiguation,...t.label===void 0?{}:{label:t.label}}}function sM(e){return[...new Set(e.flatMap(t=>t.qidOptions.map(r=>r.qid)))]}var cM=l(()=>{"use strict";Bd()});import{z as Du}from"zod";async function wE(e){if(e.suspiciousSurfaces.length===0)return{protectedSurfaces:[]};try{return await Ae({messages:x5(e),parse:t=>dM(e.suspiciousSurfaces,I5(t)),request:e.request,responseIntentClassifierPrompt:Me,schema:E5,...e.maxRetries===void 0?{}:{maxRetries:e.maxRetries}})}catch(t){return{fallback:{issues:[T5(t)],reason:"guaranteed_json_failed"},protectedSurfaces:[]}}}function I5(e){return{protectedSurfaces:e.protectedSurfaces.map(t=>({...t.note===void 0?{}:{note:t.note},surfaceId:t.surfaceId}))}}function dM(e,t){let r=uM(e,t);if(r.length>0)throw new Y(r);let n=lM(e),i=[];for(let a of t.protectedSurfaces){let o=n.get(a.surfaceId);i.push({...a.note===void 0?{}:{note:a.note},surfaceId:a.surfaceId,text:o.text})}return{protectedSurfaces:i}}function uM(e,t){let r=[],n=lM(e),i=new Set;for(let a of t.protectedSurfaces){if(!n.has(a.surfaceId)){r.push(`Unknown surfaceId "${a.surfaceId}". Use only these surface IDs: ${[...n.keys()].join(", ")}.`);continue}if(i.has(a.surfaceId)){r.push(`Duplicate protected surface ${a.surfaceId}.`);continue}i.add(a.surfaceId)}return r}function x5(e){return[{role:"system",content:v5(e)},{role:"user",content:C5(e)}]}function v5(e){return["You protect useful high-frequency surface strings before Wikidata grounding.","","The input surfaces are suspicious only because they are frequent after range suppression.","Most high-frequency function words, fragments, punctuation, generic words, and discourse words should NOT be protected.","A surface that is not protected will be removed before grounding.","","User recall policy:",e.policyPrompt,"","Protect a surface only when it is likely to be a meaningful entity, named concept, named event, place, person, organization, work, or domain term under the policy.","","Rules:","- Return JSON only.","- Return only protected surface IDs.","- Do not return a result for every input surface.","- Do not protect generic grammar words merely because they appear often.","- Do not infer or choose Wikidata QIDs in this stage."].join(`
|
|
2526
|
+
`)}function C5(e){return["Suspicious high-frequency surfaces:",JSON.stringify(e.suspiciousSurfaces.map(t=>({count:t.count,surfaceId:t.id,text:t.text})),null,2),"","Return this JSON shape:",JSON.stringify({protectedSurfaces:[{note:"optional, <= 12 Chinese chars or 6 English words",surfaceId:"surface id from the input"}]},null,2)].join(`
|
|
2527
|
+
`)}function lM(e){return new Map(e.map(t=>[t.id,t]))}function T5(e){if(e instanceof Y)return e.issues.join(`
|
|
2528
|
+
`);if(e instanceof Error){let t=e.issues;return Array.isArray(t)&&t.every(r=>typeof r=="string")?t.join(`
|
|
2529
|
+
`):e.message}return String(e)}var k5,E5,pM=l(()=>{"use strict";Rt();k5=Du.object({note:Du.string().max(24).optional(),surfaceId:Du.string().min(1)}).strict(),E5=Du.object({protectedSurfaces:Du.array(k5)}).strict()});function SE(e){let t=[...e].filter(n=>n.range.start<n.range.end).sort(R5),r=[];for(let n of t)r.some(i=>_5(i,n))||r.push(n);return r.sort(P5)}function R5(e,t){return e.range.start-t.range.start||t.range.end-e.range.end||Es(t)-Es(e)}function P5(e,t){return e.range.start-t.range.start||e.range.end-t.range.end||Es(e)-Es(t)}function _5(e,t){return e.range.start<=t.range.start&&t.range.end<=e.range.end&&Es(e)>Es(t)}function Es(e){return e.range.end-e.range.start}var kE=l(()=>{"use strict"});function EE(e){let t=SE(e.candidates),r=A5(t,e.text),n=L5(r,e.percentile),i=new Set(n.map(a=>a.text));return{candidates:t.filter(a=>!i.has(e.text.slice(a.range.start,a.range.end))),suppressedCandidates:t,suspiciousSurfaces:n}}function A5(e,t){let r=new Map;for(let n of e){let i=t.slice(n.range.start,n.range.end);r.set(i,(r.get(i)??0)+1)}return[...r.entries()].map(([n,i],a)=>({count:i,id:`s${a+1}`,text:n})).sort(O5)}function L5(e,t){if(e.length===0||t<=0)return[];let r=Math.ceil(e.length*Math.min(1,t));return e.slice(0,r)}function O5(e,t){return t.count-e.count||e.text.localeCompare(t.text)||e.id.localeCompare(t.id)}var mM=l(()=>{"use strict";kE()});import{spawn as N5}from"child_process";async function xE(e){let t=[],r=1;for(let n of e.sentences){for(let i of await D5(n,e)){let a=n.text.slice(i.start,i.end);t.push({id:`c${r}`,qidOptions:i.qids.map($5),range:{end:n.range.start+i.end,start:n.range.start+i.start},surface:a}),r+=1}await e.onProgress?.({coveredRangeEnd:n.range.end})}return t}async function D5(e,t){return wM(t)==="fetch"?await gM(t,e):await hM(t.command??"wikispine",fM(t),e,t)}async function of(e){let t=wM(e),r=Date.now();if(t==="fetch"){let n=SM(e.endpoint),i=await M5(n,e.fetch);return await gM({...e,endpoint:n,maxCandidatesPerSurface:1},{id:"test",range:{end:7,start:0},text:"\u5317\u4EAC\u5927\u5B66\u4F4D\u4E8E\u5317\u4EAC\u3002"}),{durationMs:Date.now()-r,metadata:i,ok:!0,provider:t}}return await hM(e.command??"wikispine",fM({...e,maxCandidatesPerSurface:1}),{id:"test",range:{end:7,start:0},text:"\u5317\u4EAC\u5927\u5B66\u4F4D\u4E8E\u5317\u4EAC\u3002"},{}),{durationMs:Date.now()-r,ok:!0,provider:t}}async function hM(e,t,r,n){return await new Promise((i,a)=>{let o=N5(e,t,{stdio:["pipe","pipe","pipe"]}),s=yM(n.onProgress,{onFailure:u=>{a(u),o.kill()}}),c=bM({onMatch:u=>{s.report({coveredRangeEnd:r.range.start+u.end})}}),d=[];o.stdout.setEncoding("utf8"),o.stdout.on("data",u=>{try{c.push(u)}catch(p){a(IE(p)),o.kill()}}),o.stderr.on("data",u=>{d.push(u)}),o.on("error",u=>{a(new Error(vr(`Failed to start wikispine command: ${u.message}`)))}),o.on("close",u=>{if(u!==0){a(new Error(vr(`wikispine match failed with exit code ${u}: ${Buffer.concat(d).toString("utf8")}`)));return}(async()=>{try{let p=c.finish();await s.wait(),i(p)}catch(p){a(IE(p))}})()}),o.stdin.end(r.text)})}function fM(e){let t=["match"];return e.dataDir!==void 0&&t.push("--data-dir",e.dataDir),e.includeDisambiguation===!1&&t.push("--exclude-disambiguation"),e.maxCandidatesPerSurface!==void 0&&t.push("--max-candidates-per-surface",String(e.maxCandidatesPerSurface)),t}async function gM(e,t){let r=SM(e.endpoint),n=await(e.fetch??fetch)(`${r}/match`,{body:JSON.stringify({options:{...e.includeDisambiguation===void 0?{}:{include_disambiguation:e.includeDisambiguation},...e.maxCandidatesPerSurface===void 0?{}:{max_candidates_per_surface:e.maxCandidatesPerSurface}},text:t.text}),headers:{accept:"application/x-ndjson","content-type":"application/json"},method:"POST"});if(!n.ok)throw new Error(vr(`WikiSpine fetch provider failed with HTTP ${n.status}: ${await n.text()}`));let i=yM(e.onProgress),a=bM({onMatch:d=>{i.report({coveredRangeEnd:t.range.start+d.end})}});if(n.body===null){a.push(await n.text());let d=a.finish();return await i.wait(),d}let o=n.body.getReader(),s=new TextDecoder;for(;;){let{done:d,value:u}=await o.read();if(d)break;a.push(s.decode(u,{stream:!0})),i.throwIfFailed()}a.push(s.decode());let c=a.finish();return await i.wait(),c}function yM(e,t){let r=[],n;function i(a){n===void 0&&(n=IE(a),t?.onFailure?.(n))}return{report:a=>{if(!(e===void 0||n!==void 0))try{r.push(Promise.resolve(e(a)).catch(i))}catch(o){i(o)}},throwIfFailed:()=>{if(n!==void 0)throw n},wait:async()=>{if(await Promise.all(r),n!==void 0)throw n}}}async function M5(e,t=fetch){let r=await t(`${e}/readyz`);if(!r.ok)throw new Error(vr(`WikiSpine fetch provider is not ready: HTTP ${r.status}.`));let n=await t(`${e}/metadata`);if(!n.ok)throw new Error(vr(`WikiSpine metadata request failed with HTTP ${n.status}.`));try{return F5(await n.json())}catch(i){throw i instanceof Error&&i.message.includes("setup guide")?i:new Error(vr(`Invalid WikiSpine metadata response: ${i instanceof Error?i.message:String(i)}`))}}function bM(e){let t=[],r="";function n(a){if(a.trim()==="")return;let o=W5(JSON.parse(a));o.type==="match"&&(t.push(o.match),e?.onMatch?.(o.match))}function i(a){return new Error(vr(`Invalid WikiSpine match response: ${a instanceof Error?a.message:String(a)}`))}return{finish:()=>{try{return n(r),r="",t}catch(a){throw i(a)}},push:a=>{try{r+=a;let o=r.split(/\r?\n/u);r=o.pop()??"";for(let s of o)n(s)}catch(o){throw i(o)}}}}function W5(e){if(typeof e!="object"||e===null)throw new Error("Expected wikispine event to be an object.");let t=e;if(t.type==="done")return{type:"done"};if(t.type==="match"&&G5(t.match))return{match:t.match,type:"match"};throw new Error(`Unexpected wikispine event: ${JSON.stringify(e)}`)}function IE(e){return e instanceof Error?e:new Error(String(e))}function F5(e){if(typeof e!="object"||e===null)throw new Error(vr("Expected WikiSpine metadata."));let t=e;if(typeof t.automaton_shard_count!="number"||typeof t.format!="string"||typeof t.qid_count!="number"||typeof t.surface_count!="number"||typeof t.surface_normalization!="string")throw new Error(vr("Invalid WikiSpine metadata."));return{automaton_shard_count:t.automaton_shard_count,format:t.format,qid_count:t.qid_count,surface_count:t.surface_count,surface_normalization:t.surface_normalization}}function G5(e){if(typeof e!="object"||e===null)return!1;let t=e;return typeof t.end=="number"&&Array.isArray(t.qids)&&t.qids.every(U5)&&typeof t.start=="number"&&typeof t.surface_id=="number"}function U5(e){if(typeof e!="object"||e===null)return!1;let t=e;return(!("disambiguation"in t)||typeof t.disambiguation=="boolean")&&typeof t.qid=="string"&&/^Q[1-9]\d*$/u.test(t.qid)}function $5(e){return{isDisambiguation:e.disambiguation===!0,qid:e.qid}}function wM(e){return e.provider??"cli"}function SM(e){let t=(e??Mu).trim().replace(/\/+$/u,"");if(t==="")throw new Error(vr("WikiSpine fetch provider has no default endpoint."));return t}function vr(e){return`${e}
|
|
2530
|
+
WikiSpine setup guide: ${j5}`}var j5,Mu,kM=l(()=>{"use strict";j5="https://raw.githubusercontent.com/oomol-lab/wiki-graph/refs/heads/main/docs/wikispine-runtime.md",Mu="https://wikispi-service-cxbfjlteab.cn-hangzhou.fcapp.run"});function EM(e){let t=new Intl.Segmenter(void 0,{granularity:"word"}),r=new Intl.Segmenter(void 0,{granularity:"grapheme"}),n=[];for(let i of t.segment(e))"isWordLike"in i&&i.isWordLike&&n.push({end:i.index+i.segment.length,start:i.index});return n.length>0?n:[...r.segment(e)].filter(i=>i.segment.trim()!=="").map(i=>({end:i.index+i.segment.length,start:i.index}))}function vE(e){let t=EM(e.text);if(t.length===0)return{end:e.text.length,start:0};let r=B5(t,e.rangeStart),n=H5(t,e.rangeEnd),i=Math.max(0,Math.floor(e.words/2)),a=Math.max(0,r-i),o=Math.min(t.length-1,n+i);return{end:t[o]?.end??e.text.length,start:t[a]?.start??0}}function B5(e,t){let r=e.findIndex(n=>n.end>t);return r===-1?e.length-1:r}function H5(e,t){for(let r=e.length-1;r>=0;r-=1){let n=e[r];if(n!==void 0&&n.start<t)return r}return 0}var CE=l(()=>{"use strict"});function sf(e){let t=K5(J5(e.candidates??[])),r=[],n=[],i=0;for(let a of t){let o=q5(a);n.length>0&&i+o>e.optionBudget&&(r.push(IM(e.text,n,e)),n=[],i=0),n.push(a),i+=o}return n.length>0&&r.push(IM(e.text,n,e)),r}function q5(e){return e.candidates.reduce((t,r)=>t+lE(r),0)}function IM(e,t,r){let n=Math.min(...t.map(o=>o.range.start)),i=Math.max(...t.map(o=>o.range.end)),a=vE({rangeEnd:i,rangeStart:n,text:e,words:r.contextWords});return{baseOffset:a.start,candidates:t.flatMap(o=>o.candidates),groups:t.map((o,s)=>({candidateIds:o.candidates.map(c=>c.id),id:`g${s+1}`,range:o.range})),text:e.slice(a.start,a.end)}}function K5(e){let t=[],r;for(let n of e){if(r===void 0||n.range.start>=r.range.end){r={candidates:[n],range:n.range},t.push(r);continue}r={candidates:[...r.candidates,n],range:{end:Math.max(r.range.end,n.range.end),start:Math.min(r.range.start,n.range.start)}},t[t.length-1]=r}return t}function J5(e){return e.filter(t=>t.range.start<t.range.end).sort(z5)}function z5(e,t){return e.range.start-t.range.start||t.range.end-e.range.end||e.id.localeCompare(t.id)}var xM=l(()=>{"use strict";CE();Nu()});var ga=l(()=>{"use strict";gE();oM();Nu();cM();pM();mM();kE();kM();xM();CE()});var cf=l(()=>{"use strict"});async function CM(e){if(e.candidates.length===0)return[];let t=EE({candidates:e.candidates,percentile:.1,text:e.text});await e.progressTracker?.updatePhase({done:0,phase:"screening",phaseDetail:`${t.suspiciousSurfaces.length} high-frequency surfaces`,total:1,unit:"window"}),await e.progressTracker?.throwIfStopped();let r=await wE({policyPrompt:e.policyPrompt,request:e.request,suspiciousSurfaces:t.suspiciousSurfaces});await e.progressTracker?.throwIfStopped();let n=new Set(r.protectedSurfaces.map(a=>a.text)),i=new Set(t.candidates.map(vM));return await e.progressTracker?.updatePhase({done:1,phase:"screening",phaseDetail:`${n.size} protected surfaces`,total:1,unit:"window"}),t.suppressedCandidates.filter(a=>i.has(vM(a))||n.has(a.surface))}function TM(e){return new Set(e.flatMap(t=>t.qidOptions.map(r=>r.qid))).size}function vM(e){return`${e.range.start}\0${e.range.end}\0${e.surface}`}var RM=l(()=>{"use strict";ga();cf()});async function PM(e,t){let r=e.getSerialFragments(t);return await Promise.all((await r.listFragmentIds()).map(async n=>await r.getFragment(n)))}function _M(e){return e.flatMap(t=>t.sentences.map(r=>r.text)).join(" ")}function AM(e){let t=[],r=0;for(let n of e)for(let i=0;i<n.sentences.length;i+=1){let a=n.sentences[i];t.push({id:`${n.serialId}:${n.fragmentId+i}`,range:{end:r+a.text.length,start:r},text:a.text}),r+=a.text.length+1}return t}var TE=l(()=>{"use strict"});function NM(e){let t=new Map(e.map(c=>[c.id,c])),r=new Map(e.map(c=>[c.id,new Set])),n=new Map(e.map(c=>[c.id,0])),i=new Set,a=new Map,o=new Map,s={candidatePageCount:0,qidAppearanceCount:0};return{accept(c){i.add(c.candidateId),Wu(o,c.surface).recallCount+=1,a.set(Fu(c.surface,c.qid),(a.get(Fu(c.surface,c.qid))??0)+1)},close(c,d){i.add(c);let u=t.get(c);u!==void 0&&(d==="skip_this_time"||d==="never_recall")&&(Wu(o,u.surface).rejectCount+=1)},continue(c){let d=t.get(c);d!==void 0&&(Wu(o,d.surface).continueCount+=1)},getStats(){return{...s}},nextPage(c){let d=[],u=c===void 0?e.map(p=>p.id):[...c];for(let p of u){if(i.has(p))continue;let m=t.get(p),f=r.get(p);if(m===void 0||f===void 0)continue;let w=e6(m,a),b=ks(w),E=n.get(p)??0,N=Y5(Wu(o,m.surface),E),C=b.filter(x=>!f.has(x)).slice(0,N);if(C.length===0){i.add(p);continue}for(let x of C)f.add(x);let q=b.some(x=>!f.has(x)),Q=nf(w,new Set(C));q||i.add(p),Wu(o,m.surface).seenCount+=1,n.set(p,E+1),s.candidatePageCount+=1,s.qidAppearanceCount+=C.length,d.push({...Q,...q?{hasMoreOptions:!0}:{}})}return d}}}function Wu(e,t){let r=e.get(t);if(r!==void 0)return r;let n={continueCount:0,recallCount:0,rejectCount:0,seenCount:0};return e.set(t,n),n}function Y5(e,t){let r=Z5(e)?X5:V5;return r[Math.min(t,r.length-1)]}function Z5(e){return e.seenCount<LM?!1:e.recallCount>=2&&e.continueCount===0||e.rejectCount>=2&&e.recallCount===0||e.continueCount>=LM}function jM(e,t){return`efficiency qid/mention=${t===0?"n/a":(e.qidAppearanceCount/t).toFixed(1)} qids=${e.qidAppearanceCount} mentions=${t} pages=${e.candidatePageCount}`}function e6(e,t){return{...e,qidOptions:[...e.qidOptions].sort((r,n)=>OM(e.surface,n,t)-OM(e.surface,r,t))}}function OM(e,t,r){let n=r.get(Fu(e,t.qid))??0,i=t.disambiguation?.linkedQids.reduce((o,s)=>o+(r.get(Fu(e,s.qid))??0),0)??0,a=t.disambiguation?.profile?.meanings.reduce((o,s)=>o+(r.get(Fu(e,s.qid))??0),0)??0;return n+i+a}function Fu(e,t){return`${e}\0${t}`}var V5,X5,LM,DM=l(()=>{"use strict";ga();V5=[5,10,20,35],X5=[3,5,10,20,35],LM=3});async function df(e,t,r){let n=e.lazy;if(n!==void 0)return await Promise.all(t.map(async a=>await n(async o=>await r(a,o))));let i=[];for(let a of t)i.push(await r(a,e));return i}var RE=l(()=>{"use strict"});async function uf(e){let t=[],r=NM(e.candidates),n=0,i=0,a=r.nextPage();for(;a.length>0;){let o=sf({candidates:a,contextWords:220,optionBudget:50,text:e.text});if(i+=o.length,o.length===0)break;await e.progressTracker?.updatePhase({done:n,phase:"grounding",total:i,unit:"window"});let s=await df(e.request,o,async(d,u)=>{try{return await e.progressTracker?.throwIfStopped(),await af({candidates:d.candidates,policyPrompt:e.policyPrompt,request:u,window:d})}finally{n+=1,await e.progressTracker?.updatePhase({done:n,phase:"grounding",total:i,unit:"window"})}}),c=new Set;for(let d of s){for(let u of d.mentions)t.push(u),r.accept(u);for(let u of d.policyUpdates)r.close(u.candidateId,u.decision);for(let u of d.continuations)for(let p of u.candidateIds)c.add(p),r.continue(p)}a=r.nextPage(c)}return await e.progressTracker?.updatePhase({done:n,phase:"grounding",phaseDetail:jM(r.getStats(),t.length),total:i,unit:"window"}),t}var PE=l(()=>{"use strict";ga();cf();DM();RE()});async function MM(e){let t=[],r=n6(e.fragments),n=await uf({candidates:e.candidates,policyPrompt:e.policyPrompt,...e.progressTracker===void 0?{}:{progressTracker:e.progressTracker},request:e.request,text:e.text}),i=1;for(let a of n){let o=i6(r,a.range.start);t.push(r6(e.chapterId,a,o,i)),i+=1}return t}function r6(e,t,r,n){return{chapterId:e,...t.confidence===void 0?{}:{confidence:t.confidence},id:`m${e}-${n}`,...t.note===void 0?{}:{note:t.note},qid:t.qid,rangeEnd:r.rangeStart+t.surface.length,rangeStart:r.rangeStart,sentenceIndex:r.sentenceIndex,surface:t.surface}}function n6(e){let t=[],r=0;for(let n of e)for(let i=0;i<n.sentences.length;i+=1){let a=n.sentences[i].text.length;t.push({absoluteStart:r,length:a,sentenceIndex:n.fragmentId+i}),r+=a+1}return t}function i6(e,t){for(let r of e){let n=r.absoluteStart+r.length;if(t>=r.absoluteStart&&t<n)return{rangeStart:t-r.absoluteStart,sentenceIndex:r.sentenceIndex}}throw new Error(`Mention offset ${t} is outside chapter text.`)}var WM=l(()=>{"use strict";PE()});function FM(e){return{language:Ua(e.resolverOptions?.language)??"zh",prompt:e.policyPrompt}}var GM=l(()=>{"use strict";Nr()});function lf(e){return async t=>{await e.throwIfStopped();let r=a6(t);await e.updatePhase({done:t.done,...r.detail===void 0?{}:{phaseDetail:r.detail},phase:"enrichment",total:t.total,unit:r.unit})}}function a6(e){switch(e.detail){case"disambiguation-page":return{detail:"disambiguation",unit:"page"};case"entity":return{detail:"entity",unit:"record"};case"linked-page":return{detail:"linked",unit:"page"};case"page":return{detail:"page",unit:"page"};case"qid":return{unit:"qid"}}}var _E=l(()=>{"use strict"});var UM=l(()=>{"use strict";ZS();ns();eh()});import{z as ne}from"zod";async function AE(e){if(e.window.mentions.length<2)return[];try{return await Ae({messages:h6(e),parse:t=>p6(e,t),request:e.request,responseIntentClassifierPrompt:Me,schema:d6,...e.maxRetries===void 0?{}:{maxRetries:e.maxRetries}})}catch{return[]}}function p6(e,t){let r=new Map(e.window.mentions.map(p=>[p.id,p])),n=new si(e.sentences.map((p,m)=>({sentenceId:[e.chapterId,(e.fragmentId??0)+m],text:p.text,wordsCount:p.wordsCount}))),i=qM(e.sentences),a=n.sentences.flatMap((p,m)=>{let f=i[m];return f===void 0||!HM(f,e.window.range)?[]:[{id:`S${m+1}`,sentenceId:p.sentenceId,text:p.projectedText}]}),o=[],s=[],c=new Set,d=a.map(p=>p.sentenceId),u=a.map(p=>p.text);for(let[p,m]of t.relations.entries()){let f=`relations[${p}]`,w=r.get(m.sourceMentionId),b=r.get(m.targetMentionId);if(w===void 0){o.push(`${f}.sourceMentionId references an unknown mention.`);continue}if(b===void 0){o.push(`${f}.targetMentionId references an unknown mention.`);continue}if(w.id===b.id){o.push(`${f} links a mention to itself. Use two different tagged mention IDs, or remove the relation if the other endpoint is not tagged.`);continue}let E=I6(m.predicate);if(E===""){o.push(`${f}.predicate normalizes to an empty label.`);continue}if(E==="mentions"){o.push(`${f}.predicate must be semantic; "mentions" is not a relation predicate.`);continue}let[N,C]=w6({evidence:S6(m.evidence),sentences:a}),[q,Q]=!Array.isArray(m.evidence)&&N===void 0&&C===void 0?l6.resolve(m.evidence,d,u):[void 0,void 0],x=N??q,ce=C===void 0?Q:k6(C);if(x===void 0){o.push(`${f}.evidence could not be resolved: ${ce?.message??"unknown failure"}`);continue}let ot=m6(x.sentenceIds);if(ot.length===0){o.push(`${f}.evidence resolved to no sentences.`);continue}if(ot.some(([Ga,yB])=>Ga!==e.chapterId||i[yB-(e.fragmentId??0)]===void 0)){o.push(`${f}.evidence resolved outside this fragment.`);continue}let ze=`${w.id}\0${b.id}\0${E}\0${ot.map(Ga=>Ga.join(":")).join(",")}`;c.has(ze)||(c.add(ze),s.push({...m.confidence===void 0?{}:{confidence:m.confidence},evidenceSentenceIds:ot,predicate:E,sourceMentionId:w.id,targetMentionId:b.id}))}if(o.length>0)throw new Y(o);return s}function m6(e){let t=new Set,r=[];for(let n of e){let i=n.join(":");t.has(i)||(t.add(i),r.push(n))}return r.sort(([n,i],[a,o])=>n-a||i-o)}function h6(e){return[{content:f6(),role:"system"},{content:g6(e),role:"user"}]}function f6(){return["You discover semantic relations between grounded entity mentions in source text.","","Rules:","- Return JSON only.","- Return only relations that are directly supported by the source text.",'- Every relation must connect two mention IDs from the <mention id="..." qid="..."> tags.',"- A relation is valid only when both endpoints are explicitly tagged mentions in the source context.","- If a supported relation would require a subject or object that is not tagged with a mention ID, skip that relation.","- Do not invent mention IDs, and do not reuse another mention as a placeholder for an untagged endpoint.","- sourceMentionId and targetMentionId must be different mention IDs; never link a mention to itself.","- Do not create a relation just because two mentions are nearby.","- If the text explicitly states both a positive relation and a negated/distinction relation between mentions, return both relations when both are directly supported.","- Prefer the suggested predicates when one fits.","- If none fits, create one short snake_case predicate.","- Never use mentions as a predicate.",da,"- Source sentences may contain XML-like mention tags. Use those tags for mention IDs, but ignore the tags when copying evidence quote text.","- Do not use offsets or invented paraphrases as evidence.","","Suggested predicates:",u6.join(", ")].join(`
|
|
2531
|
+
`)}function g6(e){return["Source sentences with mention tags:",y6(e),"","Return this JSON shape:",JSON.stringify({relations:[{confidence:.86,evidence:zm,predicate:"predicate_name",sourceMentionId:"source mention id",targetMentionId:"target mention id"}]},null,2),"","If the source text does not support any semantic relation, return:",JSON.stringify({relations:[]})].join(`
|
|
2532
|
+
`)}function y6(e){let t=qM(e.sentences),r=e.sentences.flatMap((n,i)=>{let a=t[i];return a===void 0||!HM(a,e.window.range)?[]:[`S${i+1}: ${oi(b6({mentions:e.window.mentions,sentence:n,sentenceOffset:a}))}`]});return r.length===0?E6(e.window):r.join(`
|
|
2533
|
+
`)}function b6(e){let t=e.mentions.filter(i=>i.range.start>=e.sentenceOffset.start&&i.range.end<=e.sentenceOffset.end).sort((i,a)=>i.range.start-a.range.start),r=[],n=e.sentenceOffset.start;for(let i of t)r.push(ya(e.sentence.text.slice(n-e.sentenceOffset.start,i.range.start-e.sentenceOffset.start))),r.push(`<mention id="${pf(i.id)}" qid="${pf(i.qid??"")}">${ya(e.sentence.text.slice(i.range.start-e.sentenceOffset.start,i.range.end-e.sentenceOffset.start))}</mention>`),n=i.range.end;return r.push(ya(e.sentence.text.slice(n-e.sentenceOffset.start))),r.join("")}function HM(e,t){return e.start<t.end&&t.start<e.end}function w6(e){return e.evidence===void 0?[void 0,void 0]:rs({evidence:e.evidence,sentences:e.sentences})}function S6(e){return Array.isArray(e)?e.map(BM):typeof e.quote=="string"||typeof e.sentence_id=="string"?BM(e):void 0}function BM(e){return{...typeof e.quote=="string"?{quote:e.quote}:{},...typeof e.sentence_id=="string"?{sentence_id:e.sentence_id}:{}}}function k6(e){return{candidates:e.candidates.map(t=>({exactNormalized:t.exactNormalized,exactRaw:t.exactRaw,exactSubstring:t.exactSubstring,index:t.index,nextText:t.nextText,occurrenceId:t.occurrenceId,prevText:t.prevText,score:t.score,sentenceId:t.sentence.sentenceId,text:t.sentence.text})),code:e.code,fieldName:"evidence",message:e.message}}function E6(e){let t=[...e.mentions].sort((i,a)=>i.range.start-a.range.start),r=[],n=e.range.start;for(let i of t)r.push(ya(e.text.slice(n-e.range.start,i.range.start-e.range.start))),r.push(`<mention id="${pf(i.id)}" qid="${pf(i.qid??"")}">${ya(e.text.slice(i.range.start-e.range.start,i.range.end-e.range.start))}</mention>`),n=i.range.end;return r.push(ya(e.text.slice(n-e.range.start))),r.join("")}function qM(e){let t=[],r=0;for(let n of e){let i=r,a=i+n.text.length;t.push({end:a,start:i}),r=a+1}return t}function I6(e){return e.trim().replace(/[\s-]+/gu,"_").replace(/_+/gu,"_").replace(/^_+|_+$/gu,"").toLowerCase()}function pf(e){return ya(e).replaceAll('"',""")}function ya(e){return e.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")}var $M,o6,s6,c6,d6,u6,l6,KM=l(()=>{"use strict";Rt();ns();UM();$M=ne.object({head:ne.string().optional(),mode:ne.enum(["full","head_tail"]).optional(),tail:ne.string().optional(),text:ne.string().optional()}).strict(),o6=ne.object({quote:ne.string().optional(),sentence_id:ne.string().optional()}).strict(),s6=ne.union([ne.object({end_anchor:ne.union([ne.string(),$M]).optional(),quote:ne.string().optional(),sentence_id:ne.string().optional(),start_anchor:ne.union([ne.string(),$M]).optional()}).strict(),ne.array(o6)]),c6=ne.object({confidence:ne.number().min(0).max(1).optional(),evidence:s6,predicate:ne.string().min(1).max(64),sourceMentionId:ne.string().min(1),targetMentionId:ne.string().min(1)}).strict(),d6=ne.object({relations:ne.array(c6)}).strict(),u6=["instance_of","subclass_of","part_of","has_part","located_in","contains","member_of","owned_by","creator","author","developer","founded_by","leader","position_held","employer","educated_at","student_of","parent","child","spouse","sibling","relative","replaces","replaced_by","precedes","follows","influenced_by","influences","participant_in","participant","conflict","opposes","allied_with","supports","causes","caused_by","results_in","has_effect","uses","used_by","applies_to","based_on","derived_from","depicts","named_after","has_characteristic","related_to","associated_with","connected_to","corresponds_to","main_subject","about","context_for","described_as","refers_to","concerns","discusses","compares_with","overlaps_with","coexists_with","parallel_to","analogous_to","mentioned_with","points_to","interpreted_as","seen_as","different_from","distinct_from","not_same_as","not_to_be_confused_with","contrasts_with","opposite_of","conflicts_with","contradicts","excludes","incompatible_with","rejects","criticizes","denies","not_reducible_to"],l6=new ai});function LE(e){v6(e);let t=C6(e.mentions);if(t.length===0||e.text.length===0)return[];let r=e.windowLength-e.maxEvidenceDistance,n=[];for(let i=0;i<e.text.length;i=x6(i,r,e.text.length)){let a=Math.min(i+e.windowLength,e.text.length),o=t.filter(s=>s.range.start>=i&&s.range.end<=a);if(o.length>=2&&n.push({baseOffset:i,mentions:o,range:{end:a,start:i},text:e.text.slice(i,a)}),a>=e.text.length)break}return n}function x6(e,t,r){return Math.min(e+t,r)}function v6(e){if(!Number.isInteger(e.windowLength)||e.windowLength<=0)throw new Error("windowLength must be a positive integer.");if(!Number.isInteger(e.maxEvidenceDistance)||e.maxEvidenceDistance<=0)throw new Error("maxEvidenceDistance must be a positive integer.");if(e.windowLength<=e.maxEvidenceDistance)throw new Error("windowLength must be greater than maxEvidenceDistance so eligible mention pairs are guaranteed to share at least one evidence window.")}function C6(e){return e.filter(t=>t.range.start<t.range.end).sort(T6)}function T6(e,t){return e.range.start-t.range.start||e.range.end-t.range.end||e.id.localeCompare(t.id)}var JM=l(()=>{"use strict"});var zM=l(()=>{"use strict";KM();JM()});async function QM(e){let t=_6(e.fragments,e.mentions),r=0;return await e.progressTracker?.updatePhase({done:0,phase:"relation-discovery",total:t.length,unit:"window"}),(await df(e.request,t,async(i,a)=>{try{return await e.progressTracker?.throwIfStopped(),await AE({chapterId:i.fragment.serialId,fragmentId:i.fragment.fragmentId,request:a,sentences:i.fragment.sentences,window:i.window})}finally{r+=1,await e.progressTracker?.updatePhase({done:r,phase:"relation-discovery",total:t.length,unit:"window"})}})).flat().map((i,a)=>({...i.confidence===void 0?{}:{confidence:i.confidence},evidenceSentenceIds:i.evidenceSentenceIds,id:`l${e.fragments[0]?.serialId??0}-${a+1}`,predicate:i.predicate,sourceMentionId:i.sourceMentionId,targetMentionId:i.targetMentionId}))}function _6(e,t){return e.flatMap(r=>{let n=r.fragmentId,i=n+r.sentences.length-1,a=A6(r.sentences,t.filter(s=>s.sentenceIndex!==void 0&&s.sentenceIndex>=n&&s.sentenceIndex<=i),n);return LE({maxEvidenceDistance:700,mentions:a,text:O6(r.sentences),windowLength:1800}).map(s=>({fragment:r,window:s}))})}function A6(e,t,r){let n=L6(e);return t.flatMap(i=>{if(i.sentenceIndex===void 0)return[];let a=n[i.sentenceIndex-r];return a===void 0?[]:[{id:i.id,qid:i.qid,range:{end:a.start+i.rangeEnd,start:a.start+i.rangeStart},surface:i.surface}]})}function L6(e){let t=[],r=0;for(let n of e){let i=r,a=i+n.text.length;t.push({end:a,start:i}),r=a+1}return t}function O6(e){return e.map(t=>t.text).join(" ")}var VM=l(()=>{"use strict";zM();cf();RE()});async function Gu(e,t){let r=await se(e,t);if(r.stage==="planned")throw new Error(`Chapter ${t} is planned. Set source before generating Knowledge Graph.`);return{details:r,fragments:await PM(e,t)}}var OE=l(()=>{"use strict";Ze();TE()});async function mf(e,t,r){if(t.details.chapterId!==e)throw new Error(`Knowledge Graph snapshot belongs to chapter ${t.details.chapterId}, not chapter ${e}.`);if(t.details.stage==="planned")throw new Error(`Chapter ${e} is planned. Set source before generating Knowledge Graph.`);let n=t.fragments,i=_M(n),a=AM(n),o=ua(r.policyPrompt);await r.progressTracker?.updatePhase({done:0,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"});let s=await xE({includeDisambiguation:!0,onProgress:async f=>{await r.progressTracker?.updatePhase({done:f.coveredRangeEnd,force:!1,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"})},...r.wikispine??{},sentences:a});await r.progressTracker?.throwIfStopped(),await r.progressTracker?.updatePhase({done:i.length,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"});let c=await CM({candidates:s,policyPrompt:o,...r.progressTracker===void 0?{}:{progressTracker:r.progressTracker},request:r.request,text:i});await r.progressTracker?.throwIfStopped();let d=TM(c);await r.progressTracker?.updatePhase({done:0,phase:"enrichment",total:d,unit:"qid"});let u=await bE(c,{...r.progressTracker===void 0?{}:{progress:lf(r.progressTracker)},...r.resolverOptions===void 0?{}:{resolverOptions:r.resolverOptions}});await r.progressTracker?.throwIfStopped(),await r.progressTracker?.updatePhase({done:d,phase:"enrichment",total:d,unit:"qid"});let p=await MM({candidates:u,chapterId:e,fragments:n,policyPrompt:o,...r.progressTracker===void 0?{}:{progressTracker:r.progressTracker},request:r.request,text:i});await r.progressTracker?.throwIfStopped();let m=await QM({fragments:n,mentions:p,...r.progressTracker===void 0?{}:{progressTracker:r.progressTracker},request:r.request});return await r.progressTracker?.throwIfStopped(),await tf(e,{mentionLinks:m,mentions:p,parameter:FM({policyPrompt:o,...r.resolverOptions===void 0?{}:{resolverOptions:r.resolverOptions}}),workspacePath:r.workspacePath})}var XM=l(()=>{"use strict";ga();ui();uE();RM();TE();WM();GM();_E();VM();OE()});var YM=l(()=>{"use strict"});var ZM=l(()=>{"use strict";uE();eM();XM();PE();_E();OE();YM()});var eW=l(()=>{"use strict";ZM()});var Uu=l(()=>{"use strict";Ze()});var NE=l(()=>{"use strict";ui()});var jE=l(()=>{"use strict";Nr();ui();ls();Uu();NE()});var nW=l(()=>{"use strict";ui();Uu();jE()});function DE(e,t,r){for(let n of e){if(n.serialId===t)return n.children=[...n.children,r],!0;if(DE(n.children,t,r))return!0}return!1}function aW(e,t){let r=[],n=[],i=0;for(let[a,o]of e){let s=t.get(a);if(s===void 0)continue;let c=o.parentChapterId!==s.parentChapterId||o.index!==s.index,d=o.title!==s.title;c&&r.push({newUri:sr(s.path.join("/")),newIndex:s.index,newPath:s.path,newParentUri:iW(s.path),oldUri:sr(o.path.join("/")),oldIndex:o.index,oldPath:o.path,oldParentUri:iW(o.path)}),d&&n.push({uri:sr(s.path.join("/")),newTitle:s.title,oldTitle:o.title}),!c&&!d&&(i+=1)}return{changed:r.length>0||n.length>0,moved:r,renamed:n,unchanged:i}}function iW(e){let t=e.slice(0,-1);return t.length===0?null:sr(t.join("/"))}function oW(e,t){let r=new Map,n=new Map,i=new Set;for(let c of t)cW(c,r,n,i);let a=new Set,o=e.chapters.map(c=>sW(c,r,n,a,[])),s=[...i].filter(c=>!a.has(c));if(s.length>0)throw new Error(`Chapter tree is missing chapter URIs: ${s.map(c=>{let d=ky(t,c);return d===void 0?"<unresolved internal chapter>":sr(d)}).join(", ")}.`);return{items:o}}function sW(e,t,r,n,i){let a=mc(e.uri);if(a===void 0)throw new Error(`Chapter tree node URI must be a chapter URI: ${e.uri}.`);let o=a.split("/");if(o.slice(0,-1).join("/")!==i.join("/"))throw new Error(`Chapter tree URI ${e.uri} does not match its JSON parent path.`);let c=o.at(-1),u=(c===void 0?void 0:r.get(c))?.serialId,p=u===void 0?void 0:t.get(u);if(u===void 0||p===void 0)throw new Error(`Chapter tree references unknown chapter URI: ${e.uri}.`);if(n.has(u))throw new Error(`Chapter tree repeats chapter URI: ${e.uri}.`);n.add(u);let m=Object.prototype.hasOwnProperty.call(e,"title")?ba(e.title):ba(p.title),f={children:e.children.map(w=>sW(w,t,r,n,o)),key:p.key,serialId:u};return m!==void 0&&(f.title=m),f}function cW(e,t,r,n){e.serialId!==void 0&&(t.set(e.serialId,e),n.add(e.serialId)),e.key!==void 0&&r.set(e.key,e);for(let i of e.children)cW(i,t,r,n)}function hf(e,t=null,r=[]){let n=new Map;return e.forEach((i,a)=>{if(i.serialId===void 0)return;let o=ba(i.title)??null,s=i.key??`chapter-${i.serialId}`,c=[...r,s];n.set(i.serialId,{index:a,parentChapterId:t,path:c,title:o});for(let[d,u]of hf(i.children,i.serialId,c))n.set(d,u)}),n}function ME(e,t){let r=[];for(let n of e){if(n.serialId===t)return{item:n,items:[...r,...e.slice(r.length+1)]};let i=ME(n.children,t);if(i.item!==void 0)return r.push({...n,children:i.items}),r.push(...e.slice(r.length)),{item:i.item,items:r};r.push(n)}return{items:[...e]}}function dW(e,t){if(t===void 0)return e;for(let r of e){if(r.serialId===t)return r.children;let n=dW(r.children,t);if(n!==void 0)return n}}function WE(e,t){return uW(e,t)}function uW(e,t,r){for(let[n,i]of e.entries()){if(i.serialId===t)return{index:n,parentChapterId:r,siblings:e};let a=uW(i.children,t,i.serialId);if(a!==void 0)return a}}function lW(e,t,r){if(r.beforeChapterId!==void 0||r.afterChapterId!==void 0){let a=r.beforeChapterId??r.afterChapterId,o=WE(e,a);if(o===void 0)throw new Error(`Target chapter ${a} does not exist.`);o.siblings.splice(r.beforeChapterId===void 0?o.index+1:o.index,0,t);return}let n=r.root===!0?void 0:r.parentChapterId!==void 0?r.parentChapterId:r.originalParentChapterId,i=dW(e,n);if(i===void 0)throw new Error(`Chapter ${n} does not exist.`);r.first===!0?i.splice(0,0,t):i.push(t)}function pW(e,t,r){let n=[r.parentChapterId,r.beforeChapterId,r.afterChapterId].filter(i=>i!==void 0);for(let i of n)if(i===e||mW(t.children,i))throw new Error(`Cannot move chapter ${e} into or next to its own descendant ${i}.`)}function mW(e,t){for(let r of e)if(r.serialId===t||mW(r.children,t))return!0;return!1}function Is(e){return{children:e.children.map(Is),...e.key===void 0?{}:{key:e.key},...e.serialId===void 0?{}:{serialId:e.serialId},title:e.title}}function ba(e){let t=e?.trim();return t===void 0||t===""?void 0:t}function FE(e,t,r){let n=[],i=!1;for(let a of e){if(a.serialId===t){if(!r.recursive&&a.children.length>0)throw new Error(`Chapter ${t} has child chapters. Use --recursive to remove it and its descendants.`);hW(a,r.removedChapterIds),i=!0;continue}let o=FE(a.children,t,r);n.push({...a,children:o.items}),i||=o.removed}return{items:n,removed:i}}function GE(e,t,r){for(let n of e){if(n.serialId===t)return r===void 0?delete n.title:n.title=r,!0;if(GE(n.children,t,r))return!0}return!1}function hW(e,t){e.serialId!==void 0&&(Array.isArray(t)?t.push(e.serialId):t.add(e.serialId));for(let r of e.children)hW(r,t)}var UE=l(()=>{"use strict";$r()});async function wa(e){let t=await e.readToc(),r=t?.items.map(Is)??[],n=!1,i=async s=>{for(let c of s)c.serialId===void 0?(c.serialId=await e.createSerial(),n=!0):await e.serials.ensure(c.serialId),await i(c.children)};await i(r);let a=Za(r);n||=a;let o={items:r,version:t?.version??Xe};return(t===void 0||n)&&await e.replaceToc(o),o}async function fW(e){let t=await e.readToc(),r=t?.items.map(Is)??[];return N6(r),t===void 0?{items:[],version:Xe}:{items:r,version:t.version}}function N6(e){let t=new Set,r=n=>{for(let i of n){if(i.key===void 0)throw new Error("Missing chapter key in TOC. Run a writable chapter operation or `wg maintenance upgrade` before using read-only chapter paths.");if(t.has(i.key))throw new Error(`Duplicate chapter key: ${i.key}.`);t.add(i.key),r(i.children)}};r(e)}var gW=l(()=>{"use strict";$r();Ic();ji();UE()});async function ff(e,t){return await e.openSession(async r=>{let n=await wa(r),i=ba(t.title),a=Sy(n.items),o=await r.createSerial(),s={children:[],key:An(a),serialId:o,...i===void 0?{}:{title:i}};if(t.parentChapterId===void 0)n.items=[...n.items,s];else if(!DE(n.items,t.parentChapterId,s))throw new Error("Parent chapter does not exist. Use `wg <archive-uri>/chapter` to discover chapter URIs.");return await r.replaceToc(n),await se(r,o)})}async function $u(e,t){let r=await wa(e),n=wy(r.items,t);if(n===void 0)throw new Error(`Chapter path ${t} does not exist.`);return n}async function xs(e,t){let r=await fW(e),n=wy(r.items,t);if(n===void 0)throw new Error(`Chapter path ${t} does not exist.`);return n}async function gf(e,t,r={}){return await e.openSession(async n=>{let i=await wa(n),a=i.items.map(Is),o=hf(a),{items:s}=oW(t,a),c=hf(s),d=aW(o,c);return r.dryRun!==!0&&d.changed&&await n.replaceToc({items:s,version:i.version}),d})}async function yf(e,t,r){return await e.openSession(async n=>{let i=await wa(n),a=WE(i.items,t),o=ME(i.items,t);if(o.item===void 0)throw new Error("Chapter does not exist. Use `wg <archive-uri>/chapter` to discover chapter URIs.");return pW(t,o.item,r),i.items=o.items,lW(i.items,o.item,{originalParentChapterId:a?.parentChapterId,...r}),await n.replaceToc(i),await se(n,t)})}async function bf(e,t,r={}){await e.openSession(async n=>{let i=await wa(n),a=[],o=FE(i.items,t,{recursive:r.recursive??!1,removedChapterIds:a});if(!o.removed)throw new Error("Chapter does not exist. Use `wg <archive-uri>/chapter` to discover chapter URIs.");i.items=o.items,await n.replaceToc(i);for(let s of a)await n.deleteSerial(s)})}async function wf(e,t,r){return await e.openSession(async n=>{switch(await ta(n,t),r){case"planned":await n.clearSerialSource(t);break;case"sourced":await n.clearSerialGraph(t);break;case"graphed":await n.deleteSummary(t);break}return await se(n,t)})}async function Bu(e,t,r){return await e.openSession(async n=>{let i=await ta(n,t);if(i.stage!=="planned")throw new Error(`Chapter ${i.uri} is ${i.stage}. Reset it to planned before setting source.`);return await Xt(n,t,r),await se(n,t)})}async function Sf(e,t,r){return await e.openSession(async n=>{let i=await ta(n,t);if(i.stage!=="graphed")throw new Error(`Chapter ${i.uri} is ${i.stage}. Set a summary only for graphed chapters.`);return await n.writeSummary(t,r),await se(n,t)})}async function kf(e,t,r){return await e.openSession(async n=>{let i=await wa(n),a=ba(r);if(!GE(i.items,t,a))throw new Error("Chapter does not exist. Use `wg <archive-uri>/chapter` to discover chapter URIs.");return await n.replaceToc(i),await se(n,t)})}var yW=l(()=>{"use strict";ls();$r();Uu();gW();UE()});import{z as Sa}from"zod";function Ef(e){return j6.parse(e)}var bW,j6,wW=l(()=>{"use strict";bW=Sa.object({children:Sa.lazy(()=>Sa.array(bW)),uri:Sa.string().min(1),title:Sa.string().nullable().optional()}).strict(),j6=Sa.object({chapters:Sa.array(bW)}).strict()});var SW=l(()=>{"use strict";Ze()});var kW=l(()=>{"use strict";nW();Uu();jE();yW();NE();wW();SW()});var EW=l(()=>{"use strict";Id()});var mi=l(()=>{"use strict";Oj();ts();sD();VD();eW();ts();Uk();kW();EW();wh()});import{randomUUID as U6}from"crypto";import{join as $6}from"path";async function IW(e="global"){let t=await $E(),r=`${process.pid}-${U6()}`,n=Date.now(),i=!1;try{await t.transaction(async()=>{await J6(t),await t.run(`
|
|
2534
|
+
INSERT OR IGNORE INTO gc_locks (
|
|
2535
|
+
scope, owner_id, owner_pid, heartbeat_at, created_at
|
|
2536
|
+
) VALUES (?, ?, ?, ?, ?)
|
|
2537
|
+
`,[e,r,process.pid,n,n]),i=await t.queryOne("SELECT owner_id FROM gc_locks WHERE scope = ?",[e],o=>k(o,"owner_id"))===r})}finally{await t.close()}if(!i)return;let a=setInterval(()=>{K6(e,r).catch(()=>{})},q6);return async()=>{clearInterval(a);let o=await $E();try{await o.run("DELETE FROM gc_locks WHERE scope = ? AND owner_id = ?",[e,r])}finally{await o.close()}}}async function K6(e,t){let r=await $E();try{await r.run(`
|
|
2538
|
+
UPDATE gc_locks
|
|
2539
|
+
SET heartbeat_at = ?, owner_pid = ?
|
|
2540
|
+
WHERE scope = ? AND owner_id = ?
|
|
2541
|
+
`,[Date.now(),process.pid,e,t])}finally{await r.close()}}async function J6(e){let t=await e.queryAll("SELECT * FROM gc_locks",void 0,V6);for(let{lock:r,scope:n}of t.filter(({lock:i})=>z6(i)))await e.run(`
|
|
2542
|
+
DELETE FROM gc_locks
|
|
2543
|
+
WHERE scope = ?
|
|
2544
|
+
AND owner_id = ?
|
|
2545
|
+
AND heartbeat_at = ?
|
|
2546
|
+
`,[n,r.ownerId,r.heartbeatAt])}function z6(e){return Date.now()-e.heartbeatAt<H6?!1:!X6(e.ownerPid)}async function $E(){return await V($6(bo(),"gc.sqlite"),B6)}function Q6(e){return{createdAt:y(e,"created_at"),heartbeatAt:y(e,"heartbeat_at"),ownerId:k(e,"owner_id"),ownerPid:y(e,"owner_pid")}}function V6(e){return{lock:Q6(e),scope:k(e,"scope")}}function X6(e){try{return process.kill(e,0),!0}catch(t){return!(R(t)&&t.code==="ESRCH")}}var B6,H6,q6,xW=l(()=>{"use strict";Yr();M();X();H();B6=`
|
|
2547
|
+
CREATE TABLE IF NOT EXISTS gc_locks (
|
|
2548
|
+
scope TEXT PRIMARY KEY,
|
|
2549
|
+
owner_id TEXT NOT NULL,
|
|
2550
|
+
owner_pid INTEGER NOT NULL,
|
|
2551
|
+
heartbeat_at INTEGER NOT NULL,
|
|
2552
|
+
created_at INTEGER NOT NULL
|
|
2553
|
+
);
|
|
2554
|
+
`,H6=6e4,q6=2e4});import{readdir as vW,rm as Y6,stat as Z6}from"fs/promises";import{join as CW}from"path";async function TW(e){let t=vb(),r=0,n=0,i=0;for(let a of await t4(t)){r+=1;let o=await Z6(a).catch(c=>{if(!(R(c)&&c.code==="ENOENT"))throw c});if(o===void 0||!e.force&&e.now-o.mtimeMs<e4)continue;let s=await rt(a);e.dryRun||await Y6(a,{force:!0,recursive:!0}),n+=1,i+=s}return{freedBytes:i,removed:n,scanned:r}}async function t4(e){try{let t=await vW(e,{withFileTypes:!0}),r=[];for(let n of t){if(!n.isDirectory())continue;let i=CW(e,n.name),a=await vW(i,{withFileTypes:!0});for(let o of a)r.push(CW(i,o.name))}return r}catch(t){if(R(t)&&t.code==="ENOENT")return[];throw t}}var e4,RW=l(()=>{"use strict";Yr();H();Au();e4=3600*1e3});import{mkdir as r4,readFile as n4,writeFile as i4}from"fs/promises";import{join as a4}from"path";async function PW(e={}){let t=Date.now(),r=bo(),n=await IW();if(n===void 0)return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:t};try{if(e.opportunistic===!0&&!await d4(r,t))return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:t};let i={dryRun:e.dryRun===!0,force:e.force===!0,now:t,stateDirectoryPath:r},a=[];for(let s of o4)a.push(await c4(s,i));let o={finishedAt:Date.now(),freedBytes:BE(a,"freedBytes"),jobs:a,removed:BE(a,"removed"),scanned:BE(a,"scanned"),skipped:!1,startedAt:t};return await u4(r,o.finishedAt).catch(()=>{}),o}finally{await n()}}async function c4(e,t){try{return{name:e.name,...await e.run(t)}}catch(r){return{error:Ce(r),freedBytes:0,name:e.name,removed:0,scanned:0}}}function BE(e,t){return e.reduce((r,n)=>{let i=n[t];return r+(typeof i=="number"?i:0)},0)}async function d4(e,t){let r=Number((await n4(_W(e),"utf8").catch(()=>"")).trim());return!Number.isFinite(r)||t-r>=s4}async function u4(e,t){await r4(e,{recursive:!0}),await i4(_W(e),`${t}
|
|
2555
|
+
`,"utf8")}function _W(e){return a4(e,"gc.last-run")}var o4,s4,AW=l(()=>{"use strict";Yr();ts();mi();xr();Bd();xf();H();xW();RW();o4=[{name:"wikg-coordinator",run:If},{name:"search-cache",run:qp},{name:"library-index",run:LW},{name:"wikipage-cache",run:eS},{name:"build-queue",run:Lh},{name:"tmp",run:TW}],s4=600*1e3});var OW={};Qe(OW,{tryRunWikiGraphGc:()=>PW});var NW=l(()=>{"use strict";AW()});import{mkdir as p4,rm as m4,writeFile as h4}from"fs/promises";import{dirname as f4,isAbsolute as g4,relative as y4,resolve as HE}from"path";var Hu,qE=l(()=>{"use strict";Yr();$n();Qr();dr();Xb();Wp();bP();So();zc();Hu=class e{#e;#t;#r=Bi();#n;#i=new Set;#a=new Set;#o=!1;constructor(t,r){if(r!==Cb)throw new Error("Use WikgCoordinator.withArchiveSession().");this.#t=HE(t),this.#e=Ui(this.#t),this.#n=setInterval(()=>{fP({archiveKey:this.#e,ownerId:this.#r}).catch(()=>{})},2e4)}static async open(t){let r=new e(t,Cb);try{return await hP({archiveKey:r.#e,ownerId:r.#r}),r}catch(n){throw clearInterval(r.#n),n}}get archiveKey(){return this.#e}get archivePath(){return this.#t}get ownerId(){return this.#r}observeDirtyEntry(t){this.#i.add(t)}modifyEntry(t){this.#a.add(t)}createFileStore(t={}){return new xo(this.#t,{...t,session:this})}async materializeReadWorkspace(t,r){let n=t===void 0,i=n?await Ye("archive-open"):HE(t);n||await nP(i);let a=this.createFileStore({readonlyDatabase:!0}),o=await xp(await Ip(this.#t),{archiveKey:this.#e,prefix:""});try{for(let s of o){let c=await a.readFile(s);if(c===void 0)continue;let d=HE(i,s),u=y4(i,d);if(u.startsWith("..")||g4(u))throw new Error(`Archive entry escapes read workspace: ${s}`);await p4(f4(d),{recursive:!0}),await h4(d,c)}return await r(i)}finally{await a.close(),n&&await m4(i,{force:!0,recursive:!0})}}async close(){if(!this.#o){this.#o=!0,clearInterval(this.#n);try{await this.#s(),await yP(this.#e)}finally{await gP({archiveKey:this.#e,ownerId:this.#r})}await Promise.resolve().then(()=>(NW(),OW)).then(async({tryRunWikiGraphGc:t})=>{await t({opportunistic:!0})}).catch(()=>{})}}async#s(){let t=new Set([...this.#i,...this.#a]);t.size!==0&&await Mp(this.#e,t)}}});import{rm as jW}from"fs/promises";import{resolve as qu}from"path";var vs,DW=l(()=>{"use strict";Yr();$n();Xb();qE();vs=class{createFileStore(t,r={}){return new xo(qu(t),r)}async withArchiveSession(t,r){let n=await Hu.open(qu(t));try{return await r(n)}finally{await n.close()}}async withReadWorkspace(t,r,n={}){let i=n.documentDirPath===void 0?await Ye("archive-open"):qu(n.documentDirPath);try{return await Mc(qu(t),i),await r(i)}finally{n.documentDirPath===void 0&&await jW(i,{force:!0,recursive:!0})}}async withWriteWorkspace(t,r){let n=await Ye("archive-write");try{return await Mc(qu(t),n),await r(n)}finally{await jW(n,{force:!0,recursive:!0})}}}});import{readdir as w4,rm as WW}from"fs/promises";import{basename as S4,dirname as k4,join as MW}from"path";async function If(e){let t=await E4(),r=0,n=0;for(let o of t){let s=await I4(o,e);s.removed&&(r+=1,n+=s.freedBytes)}let i=await C4(e),a=e.dryRun?{freedBytes:0,removed:0,scanned:0}:await Ah(Np());return{freedBytes:n+i.freedBytes+a.freedBytes,removed:r+i.removed+a.removed,scanned:t.length+i.scanned+a.scanned}}async function E4(){return await K(async e=>(await Ne(e),await e.queryAll(`
|
|
2556
|
+
SELECT overlay.*
|
|
2557
|
+
FROM entry_overlays AS overlay
|
|
2558
|
+
WHERE overlay.entry_path IN (?, ?)
|
|
2559
|
+
AND overlay.kind = 'file'
|
|
2560
|
+
AND overlay.workspace_path IS NOT NULL
|
|
2561
|
+
ORDER BY overlay.updated_at ASC
|
|
2562
|
+
`,[Zr,Z],Bn)))}async function I4(e,t){let r=await lr(e.archiveKey,e.entryPath,"write");try{let n=await lr(e.archiveKey,e.entryPath,"state");try{let i=await Hn(e.archiveKey,e.entryPath);if(i?.workspacePath===void 0||i.workspacePath!==e.workspacePath||!await x4(i,t))return{freedBytes:0,removed:!1};let a=await rt(i.workspacePath);return t.dryRun||(await wo(i.archiveKey,i.entryPath),await WW(i.workspacePath,{force:!0}),a+=await _h(k4(i.workspacePath))),{freedBytes:a,removed:!0}}finally{await n()}}finally{await r()}}async function x4(e,t){return await Ap(e.archiveKey,e.entryPath)||e.workspacePath===void 0?!1:await v4(e)?!0:!(!t.force&&t.now-e.updatedAt<36e5||e.entryPath===Z&&!t.force)}async function v4(e){return e.workspacePath===void 0||!await Fn(e.workspacePath)||!await Fn(e.archivePath)?!0:e.entryPath!==Z?!1:await aP(e)!=="current"}async function C4(e){let t=Np(),r;try{r=await w4(t,{withFileTypes:!0})}catch(o){if(R(o)&&o.code==="ENOENT")return{freedBytes:0,removed:0,scanned:0};throw o}let n=0,i=0,a=0;for(let o of r){if(!o.isDirectory())continue;let s=o.name;if(await Lp(s))continue;let c=new Set((await Ki(s)).map(d=>d.workspacePath).filter(d=>d!==void 0));for(let d of await Qb(MW(t,s))){if(a+=1,Zk(S4(d))||c.has(d))continue;let u=await rt(d);e.dryRun||await WW(d,{force:!0}),n+=u,i+=1}if(!e.dryRun){let d=await eE(MW(t,s));n+=d.freedBytes,i+=d.removed,a+=d.scanned}}return{freedBytes:n,removed:i,scanned:a}}var FW=l(()=>{"use strict";Au();H();zb();Qr();dr();Gc();So();Vb();en();zc()});var GW=l(()=>{"use strict";DW();qE();Wp();FW()});var KE=l(()=>{"use strict";GW()});import{resolve as T4}from"path";var G,UW=l(()=>{"use strict";X();KE();ts();wh();G=class{#e;#t=new vs;constructor(t){this.#e=T4(t)}async read(t,r={}){return await this.readDocument(async(n,i)=>await t(new fn(n,i)),r)}async readDocument(t,r={}){return await this.#t.withArchiveSession(this.#e,async n=>{if(r.documentDirPath!==void 0)return await n.materializeReadWorkspace(r.documentDirPath,async a=>{let o=await ye.open(a);try{return await t(o,a)}finally{await o.release()}});let i=await ye.open(this.#e,{fileStore:n.createFileStore({readonlyDatabase:!0,...r.searchIndexWritebackPolicy===void 0?{}:{searchIndexWritebackPolicy:r.searchIndexWritebackPolicy}})});try{return await t(i,this.#e)}finally{await i.release()}})}async write(t,r={}){return await this.#t.withArchiveSession(this.#e,async n=>{let i=await ye.open(this.#e,{fileStore:n.createFileStore({...r.searchIndexWritebackPolicy===void 0?{}:{searchIndexWritebackPolicy:r.searchIndexWritebackPolicy}})});try{return await t(i)}finally{try{await i.release()}finally{await $t(this.#e)}}})}}});var xr=l(()=>{"use strict";$n();kR();UW();KE()});import{createHash as BW}from"crypto";import{mkdir as HW,readdir as R4,rm as JE,stat as P4,writeFile as _4}from"fs/promises";import{join as Ku}from"path";async function Cs(e){let t=await U(e),r=await j4(e),n=JW(r),i=!await QW(Tf(t));if(!i)return{enabled:i,sourceFingerprint:n,sources:r,status:"disabled"};let a=new hi(t),o=await rn(a);if(o==="missing")return{enabled:i,sourceFingerprint:n,sources:r,status:"missing"};let s=await a.readSearchIndexDatabase(async c=>({fingerprint:await hr(c),sourceFingerprint:await M4(c,"sourceFingerprint")}));return{enabled:i,...s.fingerprint===void 0?{}:{fingerprint:s.fingerprint},sourceFingerprint:n,sources:r,status:o==="current"&&s.sourceFingerprint===n?"current":"dirty"}}async function zE(e,t){let r=await U(e);return await Pe(r.id,"write",async()=>{await JE(Tf(r),{force:!0});let n=await gt(e),i=n.map(KW),a=n.filter(d=>d.exists&&d.status==="present"),o=new hi(r),s=JW(i),c=N4(s);return await O4(o,a,c,t),await o.writeSearchIndexDatabase(async d=>{await $W(d,"sourceFingerprint",s),await $W(d,"libraryFingerprint",c)}),await Cs(e)})}async function QE(e){let t=await U(e);return await Pe(t.id,"write",async()=>(await HW(Cf(t),{recursive:!0}),await JE(zW(t),{force:!0}),await _4(Tf(t),`disabled
|
|
2563
|
+
`,"utf8"),await Cs(e)))}async function Sn(e){let t="folderPath"in e?e:await U(e);if(await QW(Tf(t)))return;let r=new hi(t);try{await r.writeSearchIndexDatabase(async()=>{await _p(r,[0],{archiveId:0,updatedAt:Date.now()})})}catch(n){if(W4(n))return;throw n}}async function ka(e){let t=await Cs(e);if(t.status!=="current")throw new Error(`Wiki Graph library index is ${t.status}. Run \`<lib-uri>/index enable\` before querying.`);return t}async function Ea(e,t,r={}){let n=await U(e);return await Pe(n.id,"read",async()=>{let i=await ka(e),a=new Map(i.sources.map(c=>[c.archiveId,c])),{querySearchIndex:o}=await Promise.resolve().then(()=>(Qi(),eP)),s=await o(new hi(n),t,r);if(s!==void 0)return{objectHits:s.objectHits.map(c=>({...c,...vf(a,c.archiveId)})),terms:s.terms,textHits:s.textHits.map(c=>({...c,...vf(a,c.archiveId)}))}})}async function qW(e,t={}){let r=await U(e);return await Pe(r.id,"read",async()=>{let n=await ka(e),i=new Map(n.sources.map(a=>[a.archiveId,a]));return await new hi(r).readSearchIndexDatabase(async a=>{let o=await a.queryAll(`
|
|
2564
|
+
SELECT DISTINCT
|
|
2565
|
+
archive_id,
|
|
2566
|
+
owner_kind,
|
|
2567
|
+
owner_id,
|
|
2568
|
+
property_kind,
|
|
2569
|
+
chapter_id
|
|
2570
|
+
FROM search_object_properties_records
|
|
2571
|
+
WHERE owner_kind != ? OR property_kind = ?
|
|
2572
|
+
ORDER BY archive_id, COALESCE(chapter_id, 0), owner_kind, owner_id, property_kind
|
|
2573
|
+
`,[J.chunk,je.label],c=>{let d=y(c,"archive_id");return{...vf(i,d),archiveId:d,ownerId:String(c.owner_id),ownerKind:y(c,"owner_kind"),propertyKind:y(c,"property_kind"),score:0,...c.chapter_id===null?{}:{chapterId:y(c,"chapter_id")}}}),s=t.includeText===!0?await a.queryAll(`
|
|
2574
|
+
SELECT archive_id, kind, chapter_id, sentence_index, words_count
|
|
2575
|
+
FROM text_sentence_records
|
|
2576
|
+
ORDER BY archive_id, chapter_id, sentence_index, kind
|
|
2577
|
+
`,void 0,c=>{let d=y(c,"archive_id");return{...vf(i,d),archiveId:d,chapterId:y(c,"chapter_id"),kind:y(c,"kind"),rank:0,score:0,sentenceIndex:y(c,"sentence_index"),wordsCount:y(c,"words_count")}}):[];return{objectHits:o,terms:[],textHits:s}})})}async function VE(e,t){let r=await U(e);return await Pe(r.id,"read",async()=>{await ka(e);let n=L4(t);return n===void 0?[]:await new hi(r).readSearchIndexDatabase(async i=>await i.queryAll(`
|
|
2578
|
+
SELECT DISTINCT archive_id
|
|
2579
|
+
FROM search_object_properties_records
|
|
2580
|
+
WHERE owner_kind = ? AND owner_id = ?
|
|
2581
|
+
ORDER BY archive_id
|
|
2582
|
+
`,[n.ownerKind,n.ownerId],a=>y(a,"archive_id")))})}function L4(e){let t=e.replace(/^wikg:\/\/|\/+$/gu,""),r=/^(?:chapter\/[1-9][0-9]*\/)?(chunk|entity)\/([^/]+)$/u.exec(t);if(!(r?.[1]===void 0||r[2]===void 0))return{ownerId:decodeURIComponent(r[2]),ownerKind:r[1]==="chunk"?J.chunk:J.entity}}async function LW(e){let t=Ku(Ve(),"library"),r=await D4();if(r===void 0)return{freedBytes:0,removed:0,scanned:0};let n=new Set(r.map(c=>String(c))),i=await R4(t,{withFileTypes:!0}).catch(c=>{if(R(c)&&c.code==="ENOENT")return[];throw c}),a=0,o=0,s=0;for(let c of i){if(!c.isDirectory()||(a+=1,n.has(c.name)))continue;let d=Number(c.name);if(Number.isInteger(d)&&await jT(d))continue;let u=Ku(t,c.name),p=await rt(u);e.dryRun||await JE(u,{force:!0,recursive:!0}),o+=1,s+=p}return{freedBytes:s,removed:o,scanned:a}}async function O4(e,t,r,n){await e.writeSearchIndexDatabase(async i=>{await Hc(i,n);let a={objectDone:0,textDone:0};for(let o of t)await new G(o.path).readDocument(async s=>{for await(let c of vw(s,o.id))a=await qc(i,c,a,n)});await Kc(i,r,0,n)})}function N4(e){return BW("sha256").update("library-index").update("\0").update(e).digest("hex")}function vf(e,t){let r=e.get(t);return r===void 0?{archiveUri:`library-archive:${t}`,libraryArchiveUri:`library-archive:${t}`}:{archiveUri:r.archiveUri,libraryArchiveUri:r.archiveUri}}async function j4(e){return(await gt(e)).map(KW)}function KW(e){return{archiveId:e.id,archiveUri:e.uri,exists:e.exists,...e.lastSeenMutationToken===void 0?{}:{lastSeenMutationToken:e.lastSeenMutationToken},relativePath:e.relativePath,status:e.status}}function JW(e){let t=BW("sha256");for(let r of[...e].sort((n,i)=>n.archiveId-i.archiveId))t.update(String(r.archiveId)),t.update("\0"),t.update(r.relativePath),t.update("\0"),t.update(r.status),t.update("\0"),t.update(r.lastSeenMutationToken??""),t.update("\0"),t.update(r.exists?"1":"0"),t.update("\0");return t.digest("hex")}async function D4(){let e=await V(Se(),"");try{return await e.queryAll("SELECT id FROM libraries",void 0,t=>y(t,"id"))}catch{return}finally{await e.close()}}async function M4(e,t){return await e.queryOne("SELECT value FROM search_index_state WHERE key = ?",[t],r=>k(r,"value"))}async function $W(e,t,r){await e.run(`
|
|
2583
|
+
INSERT INTO search_index_state(key, value)
|
|
2584
|
+
VALUES (?, ?)
|
|
2585
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
2586
|
+
`,[t,r])}function Cf(e){return Ku(e.stagingPath,"index")}function zW(e){return Ku(Cf(e),"fts.db")}function Tf(e){return Ku(Cf(e),A4)}async function QW(e){try{return await P4(e),!0}catch(t){if(R(t)&&t.code==="ENOENT")return!1;throw t}}function W4(e){return typeof e=="object"&&e!==null&&"code"in e&&e.code==="SQLITE_CANTOPEN"}var A4,hi,Ju=l(()=>{"use strict";M();Ai();xc();X();xr();yd();Qi();Au();Ue();H();zu();mo();Oc();A4="index.disabled";hi=class{#e;serials={getChaptersRevision:()=>Promise.resolve(0)};constructor(t){this.#e=t}async readSearchIndexDatabase(t){return await this.#t(t,!0)}async writeSearchIndexDatabase(t){return await this.#t(t,!1)}async#t(t,r){await ke(),await HW(Cf(this.#e),{recursive:!0});let n=await $.open(zW(this.#e),r?"":sp,{readonly:r});try{return await t(n)}finally{await n.close()}}}});import{randomBytes as F4}from"crypto";import{constants as G4,copyFile as U4,mkdir as XE,opendir as $4,realpath as Qu,rename as B4,rm as H4,stat as ZE}from"fs/promises";import{dirname as Rf,isAbsolute as eI,join as Ia,relative as tI,resolve as YE}from"path";async function rI(e){let t=await U(e);return await Pe(t.id,"write",async()=>{let r=await eF(e,t,{pathIdentity:"trusted"});return await Sn(t),r})}async function nI(e){if(e.target.kind!=="scope"||e.target.objectUri!==void 0)throw new Error("Library rebind requires a library scope URI.");let t=await U(e.target);return await Pe(t.id,"write",async()=>{let r=await HT(t,e.folderPath),n=await eF(e.target,r,{pathIdentity:"untrusted"});return await Sn(r),n})}async function eF(e,t,r){let n=await eZ(t.folderPath);return await fi(async i=>{await i.transaction(async()=>{let a=await Q4(i,t),o=new Set(n.map(d=>d.relativePath)),s=new Set;for(let d of n){let u=Z4(a,d);if(r.pathIdentity==="trusted"&&u!==void 0){await Vu(i,u.id,d,{status:"present"}),s.add(u.id);continue}let p=d.mutationToken===void 0?[]:a.filter(f=>f.lastSeenMutationToken===d.mutationToken&&!s.has(f.id)),m=p.filter(f=>f.relativePath===d.relativePath||!o.has(f.relativePath));if(p.length===1&&m.length===1&&d.mutationToken!==void 0){let f=m[0];await i.run(`
|
|
2587
|
+
UPDATE library_archives
|
|
2588
|
+
SET relative_path = ?
|
|
2589
|
+
WHERE id = ?
|
|
2590
|
+
`,[d.relativePath,f.id]),await Vu(i,f.id,d,{status:"present"}),s.add(f.id);continue}await rF(i,t.id,d,{status:p.length===0||d.mutationToken===void 0?"present":"conflict"})}let c=new Date().toISOString();for(let d of a)s.has(d.id)||r.pathIdentity==="trusted"&&o.has(d.relativePath)||await i.run(`
|
|
2591
|
+
UPDATE library_archives
|
|
2592
|
+
SET status = 'missing', updated_at = ?, last_scanned_at = ?
|
|
2593
|
+
WHERE id = ?
|
|
2594
|
+
`,[c,c,d.id])})}),{library:t,archives:await gt(e)}}async function gt(e){let t=await U(e);return await fi(async r=>await z4(r,t))}async function Xu(e){let t=await U(e);return await _f(e,t)}async function Yu(e,t){return await fi(async r=>await Y4(r,e,t))}async function iI(e){let t=de(e);if(t===void 0||t.kind!=="archive")throw new Error(`Expected a Wiki Graph library archive locator: ${e}`);let r=await Xu(t);if(!r.exists||r.status==="missing")throw new Error(`Wiki Graph library archive is missing: ${e}`);if(r.status==="conflict")throw new Error(`Wiki Graph library archive has a conflict and cannot be resolved: ${e}`);return r.path}async function aI(e){if(e.inputPath.startsWith("wikg://"))throw new Error("Library add --input accepts a file path, not a Wiki Graph URI.");if(/^[a-z][a-z0-9+.-]*:\/\//iu.test(e.inputPath))throw new Error("Library add does not support URL inputs yet.");let t=await U(e.target);await XE(t.folderPath,{recursive:!0});let r=YE(e.inputPath);if(!(await ZE(r)).isFile())throw new Error(`Library add --input must be a file: ${e.inputPath}`);let i=r.split(/[\\/]/u).at(-1)??"",a=sF(e.to??i),o=cF(t.folderPath,a);return await Pe(t.id,"write",async()=>{await XE(Rf(o),{recursive:!0}),await Pf(await Qu(Rf(o)),t.folderPath),await U4(r,o,G4.COPYFILE_EXCL),await Pf(await Qu(o),t.folderPath);let s=await iF(t.folderPath,a),c=await fi(async d=>(await d.transaction(async()=>{await V4(d,t.id,s)}),await X4(d,t,a)));return await Sn(t),c})}async function oI(e){if(e.target.kind!=="archive")throw new Error("Expected a Wiki Graph library archive URI.");let t=await U(e.target);return await Pe(t.id,"write",async()=>{let r=await _f(e.target,t),n=await sI(r.path),i=await uI(t.folderPath,r.relativePath);return await fi(async a=>{await Vu(a,r.id,i,{status:"present"})}),n})}async function sI(e){return await oF(e)===void 0||await yo(e,K4)===void 0?!1:(await new G(e).write(async r=>{await r.deleteSearchIndexDatabase()},{searchIndexWritebackPolicy:"archive"}),!0)}async function cI(e){let t=await U(e.target),r=await _f(e.target,t);return await Pe(t.id,"write",async()=>{await H4(r.path,{force:!0}),await fi(async n=>{await n.run("DELETE FROM library_archives WHERE id = ?",[r.id])}),await Sn(t)}),{...r,exists:!1,status:"missing"}}async function dI(e){let t=await U(e.target),r=await _f(e.target,t),n=sF(e.to),i=cF(t.folderPath,n);return await Pe(t.id,"write",async()=>{if(await XE(Rf(i),{recursive:!0}),await Pf(await Qu(Rf(i)),t.folderPath),await Af(i))throw new Error(`Library archive target already exists: ${n}`);await B4(r.path,i),await Pf(await Qu(i),t.folderPath);let a=await uI(t.folderPath,n),o=await fi(async s=>(await s.run("UPDATE library_archives SET relative_path = ? WHERE id = ?",[n,r.id]),await Vu(s,r.id,a,{status:"present"}),await nF(s,t,r.publicId)));return await Sn(t),o})}async function fi(e){let t=await V(Se(),J4);try{return await tZ(t),await rZ(t),await e(t)}finally{await t.close()}}async function z4(e,t){let r=await tF(e,t.id),n=[];for(let i of r){let a=k(i,"relative_path");n.push(Zu(t,i,await Af(Ia(t.folderPath,a))))}return n}async function Q4(e,t){return(await tF(e,t.id)).map(n=>Zu(t,n,!1))}async function tF(e,t){return await e.queryAll(`
|
|
2595
|
+
SELECT id, public_id, relative_path, status, last_seen_mutation_token,
|
|
2596
|
+
last_seen_size, last_seen_mtime_ms, last_scanned_at,
|
|
2597
|
+
created_at, updated_at
|
|
2598
|
+
FROM library_archives
|
|
2599
|
+
WHERE library_id = ?
|
|
2600
|
+
ORDER BY relative_path
|
|
2601
|
+
`,[t],r=>r)}async function V4(e,t,r){let n=await e.queryOne(`
|
|
2602
|
+
SELECT id
|
|
2603
|
+
FROM library_archives
|
|
2604
|
+
WHERE library_id = ? AND relative_path = ?
|
|
2605
|
+
ORDER BY CASE status
|
|
2606
|
+
WHEN 'present' THEN 0
|
|
2607
|
+
WHEN 'conflict' THEN 1
|
|
2608
|
+
ELSE 2
|
|
2609
|
+
END, id DESC
|
|
2610
|
+
`,[t,r.relativePath],i=>y(i,"id"));if(n!==void 0){await Vu(e,n,r,{status:"present"});return}await rF(e,t,r,{status:"present"})}async function rF(e,t,r,n){let i=new Date().toISOString();await e.run(`
|
|
2611
|
+
INSERT INTO library_archives (
|
|
2612
|
+
library_id, public_id, relative_path, status, last_seen_mutation_token,
|
|
2613
|
+
last_seen_size, last_seen_mtime_ms, last_scanned_at, created_at, updated_at
|
|
2614
|
+
)
|
|
2615
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2616
|
+
`,[t,await oZ(e,t),r.relativePath,n.status,r.mutationToken??null,r.size,Math.round(r.mtimeMs),i,i,i])}async function Vu(e,t,r,n){let i=new Date().toISOString();await e.run(`
|
|
2617
|
+
UPDATE library_archives
|
|
2618
|
+
SET status = ?,
|
|
2619
|
+
last_seen_mutation_token = ?,
|
|
2620
|
+
last_seen_size = ?,
|
|
2621
|
+
last_seen_mtime_ms = ?,
|
|
2622
|
+
last_scanned_at = ?,
|
|
2623
|
+
updated_at = ?
|
|
2624
|
+
WHERE id = ?
|
|
2625
|
+
`,[n.status,r.mutationToken??null,r.size,Math.round(r.mtimeMs),i,i,t])}async function X4(e,t,r){let n=await e.queryOne(`
|
|
2626
|
+
SELECT id, public_id, relative_path, status, last_seen_mutation_token,
|
|
2627
|
+
last_seen_size, last_seen_mtime_ms, last_scanned_at,
|
|
2628
|
+
created_at, updated_at
|
|
2629
|
+
FROM library_archives
|
|
2630
|
+
WHERE library_id = ? AND relative_path = ?
|
|
2631
|
+
ORDER BY CASE status
|
|
2632
|
+
WHEN 'present' THEN 0
|
|
2633
|
+
WHEN 'conflict' THEN 1
|
|
2634
|
+
ELSE 2
|
|
2635
|
+
END, id DESC
|
|
2636
|
+
`,[t.id,r],i=>Zu(t,i,!0));if(n===void 0)throw new Error(`Library archive registry record is missing: ${r}`);return n}async function nF(e,t,r){let n=await e.queryOne(`
|
|
2637
|
+
SELECT id, public_id, relative_path, status, last_seen_mutation_token,
|
|
2638
|
+
last_seen_size, last_seen_mtime_ms, last_scanned_at,
|
|
2639
|
+
created_at, updated_at
|
|
2640
|
+
FROM library_archives
|
|
2641
|
+
WHERE library_id = ? AND public_id = ?
|
|
2642
|
+
`,[t.id,r],a=>a);if(n===void 0)throw new Error(`Unknown Wiki Graph library archive: ${r}`);let i=k(n,"relative_path");return Zu(t,n,await Af(Ia(t.folderPath,i)))}async function Y4(e,t,r){let n=await e.queryOne(`
|
|
2643
|
+
SELECT id, public_id, relative_path, status, last_seen_mutation_token,
|
|
2644
|
+
last_seen_size, last_seen_mtime_ms, last_scanned_at,
|
|
2645
|
+
created_at, updated_at
|
|
2646
|
+
FROM library_archives
|
|
2647
|
+
WHERE library_id = ? AND id = ?
|
|
2648
|
+
`,[t.id,r],a=>a);if(n===void 0)throw new Error(`Unknown Wiki Graph library archive: ${r}`);let i=k(n,"relative_path");return Zu(t,n,await Af(Ia(t.folderPath,i)))}async function _f(e,t){if(e.kind!=="archive"||e.archivePublicId===void 0)throw new Error("Expected a Wiki Graph library archive URI.");return await fi(async r=>await nF(r,t,e.archivePublicId))}function Zu(e,t,r){let n=k(t,"public_id"),i=k(t,"relative_path"),a=sZ(Te(t,"status")??"present");return{createdAt:k(t,"created_at"),exists:r,id:y(t,"id"),libraryId:e.id,libraryUri:e.uri,...YW(t,"last_seen_mutation_token","lastSeenMutationToken"),...XW(t,"last_seen_mtime_ms","lastSeenMtimeMs"),...XW(t,"last_seen_size","lastSeenSize"),...YW(t,"last_scanned_at","lastScannedAt"),path:Ia(e.folderPath,i),publicId:n,relativePath:i,status:r?a:"missing",updatedAt:k(t,"updated_at"),uri:`${e.uri}/arc/${n}`}}function Z4(e,t){return e.filter(r=>r.relativePath===t.relativePath).sort((r,n)=>VW(r)-VW(n))[0]}function VW(e){return e.status==="present"?0:e.status==="conflict"?1:2}async function eZ(e){let t=[];await aF(e,e,t);let r=[];for(let n of t.sort((i,a)=>i.localeCompare(a)))r.push(await iF(e,n));return r}async function iF(e,t){return await sI(Ia(e,t)),await uI(e,t)}async function aF(e,t,r){let n;try{n=await $4(t)}catch(i){if(R(i)&&i.code==="ENOENT")return;throw i}for await(let i of n){let a=Ia(t,i.name);i.isDirectory()?await aF(e,a,r):i.isFile()&&i.name.endsWith(cr)&&r.push(tI(e,a).replace(/\\/gu,"/"))}}async function uI(e,t){let r=Ia(e,t),n=await ZE(r),i=await oF(r);return{mtimeMs:n.mtimeMs,...i===void 0?{}:{mutationToken:i},path:r,relativePath:t,size:n.size}}async function oF(e){try{return await Un(e)}catch{return}}function sF(e){let t=e.replace(/\\/gu,"/").replace(/^\/+|\/+$/gu,"");if(t===""||eI(e)||t.split("/").includes(".."))throw new Error("Library archive target must be a relative path inside the library folder.");if(!t.endsWith(cr))throw new Error("Library archive target must end with .wikg.");return t}function cF(e,t){let r=YE(e,t),n=tI(YE(e),r);if(n===""||n.startsWith("..")||eI(n))throw new Error("Library archive target escapes the library folder.");return r}async function Pf(e,t){let r=await Qu(t),n=tI(r,e);if(n.startsWith("..")||eI(n))throw new Error("Library archive path escapes the library folder.")}async function tZ(e){let t=await e.queryAll("PRAGMA table_info(library_archives)",void 0,i=>k(i,"name")),r=new Set(t),n=[["status","TEXT NOT NULL DEFAULT 'present'"],["last_seen_mutation_token","TEXT"],["last_seen_size","INTEGER"],["last_seen_mtime_ms","INTEGER"],["last_scanned_at","TEXT"]];for(let[i,a]of n)r.has(i)||await e.run(`ALTER TABLE library_archives ADD COLUMN ${i} ${a}`)}async function rZ(e){await nZ(e)&&await iZ(e),await e.run(`
|
|
2649
|
+
CREATE INDEX IF NOT EXISTS idx_library_archives_library_path
|
|
2650
|
+
ON library_archives(library_id, relative_path)
|
|
2651
|
+
`)}async function nZ(e){let t=await e.queryAll("PRAGMA index_list(library_archives)",void 0,r=>({name:k(r,"name"),unique:y(r,"unique")===1}));for(let r of t){if(!r.unique)continue;let n=await e.queryAll(`PRAGMA index_info(${aZ(r.name)})`,void 0,i=>k(i,"name"));if(n.length===2&&n[0]==="library_id"&&n[1]==="relative_path")return!0}return!1}async function iZ(e){await e.transaction(async()=>{await e.run("ALTER TABLE library_archives RENAME TO library_archives_unique_path_legacy"),await e.run(`
|
|
2652
|
+
CREATE TABLE library_archives (
|
|
2653
|
+
${ZW}
|
|
2654
|
+
)
|
|
2655
|
+
`),await e.run(`
|
|
2656
|
+
INSERT INTO library_archives (
|
|
2657
|
+
id, library_id, public_id, relative_path, status,
|
|
2658
|
+
last_seen_mutation_token, last_seen_size, last_seen_mtime_ms,
|
|
2659
|
+
last_scanned_at, created_at, updated_at
|
|
2660
|
+
)
|
|
2661
|
+
SELECT id, library_id, public_id, relative_path, status,
|
|
2662
|
+
last_seen_mutation_token, last_seen_size, last_seen_mtime_ms,
|
|
2663
|
+
last_scanned_at, created_at, updated_at
|
|
2664
|
+
FROM library_archives_unique_path_legacy
|
|
2665
|
+
`),await e.run("DROP TABLE library_archives_unique_path_legacy")})}function aZ(e){return`'${e.replace(/'/gu,"''")}'`}async function oZ(e,t){for(let r=0;r<20;r+=1){let n=F4(q4).toString("hex");if(await e.queryOne("SELECT public_id FROM library_archives WHERE library_id = ? AND public_id = ?",[t,n],a=>k(a,"public_id"))===void 0)return n}throw new Error("Could not generate a unique library archive id.")}async function Af(e){try{return await ZE(e),!0}catch(t){if(R(t)&&t.code==="ENOENT")return!1;throw t}}function sZ(e){return e==="conflict"||e==="missing"||e==="present"?e:"present"}function XW(e,t,r){let n=e[t];return typeof n!="number"?{}:{[r]:n}}function YW(e,t,r){let n=Te(e,t);return n===void 0?{}:{[r]:n}}var q4,K4,ZW,J4,zu=l(()=>{"use strict";M();X();Ue();Dn();xr();H();mo();Ju();Oc();q4=6,K4="fts.db",ZW=`
|
|
2666
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
2667
|
+
library_id INTEGER NOT NULL,
|
|
2668
|
+
public_id TEXT NOT NULL,
|
|
2669
|
+
relative_path TEXT NOT NULL,
|
|
2670
|
+
status TEXT NOT NULL DEFAULT 'present',
|
|
2671
|
+
last_seen_mutation_token TEXT,
|
|
2672
|
+
last_seen_size INTEGER,
|
|
2673
|
+
last_seen_mtime_ms INTEGER,
|
|
2674
|
+
last_scanned_at TEXT,
|
|
2675
|
+
created_at TEXT NOT NULL,
|
|
2676
|
+
updated_at TEXT NOT NULL,
|
|
2677
|
+
UNIQUE(library_id, public_id)
|
|
2678
|
+
`,J4=`
|
|
2679
|
+
CREATE TABLE IF NOT EXISTS library_archives (
|
|
2680
|
+
${ZW}
|
|
2681
|
+
);
|
|
2682
|
+
|
|
2683
|
+
CREATE INDEX IF NOT EXISTS idx_library_archives_library
|
|
2684
|
+
ON library_archives(library_id);
|
|
2685
|
+
|
|
2686
|
+
CREATE INDEX IF NOT EXISTS idx_library_archives_library_path
|
|
2687
|
+
ON library_archives(library_id, relative_path);
|
|
2688
|
+
`});function el(e){return[...new Set([...e.objectHits.map(t=>t.archiveId),...e.textHits.map(t=>t.archiveId)])].sort((t,r)=>t-r)}async function Ts(e,t,r){let n=await U(e),i=await Yu(n,t);if(!Lf(i))throw new Error(`Wiki Graph library archive ${t} is not readable while ${r.operation}.`);return i}async function gi(e,t){return await new G(e.path).readDocument(t)}function Cr(e){return{archiveId:e.id,libraryArchiveUri:e.uri}}function Lf(e){return e.exists&&e.status==="present"}var lI=l(()=>{"use strict";xr();zu();mo()});function pF(e){return e.triplePattern===void 0}async function mF(e,t,r){let n=r.limit??20,i=vo(t);if(i===void 0)return Qn(t,[],r);if(r.cursor!==void 0){let u=Tm(r.cursor);if(u===void 0)throw new Error("Invalid search cursor.");return await dF(e,u,{...r,limit:n})}let a=await ka(e),o=gF(e),s=r.types??null,c=await Xi({archiveKey:o,chapters:r.chapters??null,lens:r.types===void 0?"broad":"typed",match:r.match??"any",order:r.order??"doc-asc",query:t,revisionScope:a.sourceFingerprint,terms:i.terms,types:s}),d=await Lo(c,o);return await dF(e,{createdAt:d.createdAt,cursor:{bucket:0},sessionId:c},{...r,limit:n})}async function dF(e,t,r){let n=gF(e),i=await id(t.sessionId,n,t.createdAt);hZ(r.types,i.types);let a=[],o=t.cursor;for(;o!==void 0&&a.length<r.limit;){let s=r.limit-a.length,c=await cZ(e,i,o,s);a.push(...c.items),o=c.nextCursor}return{chapters:i.chapters,items:a,lens:i.types===null?"broad":"typed",lensHint:i.types===null?fr:null,limit:r.limit,match:i.match,nextCursor:o===void 0?null:Xc(t.sessionId,o,i.createdAt),order:r.order??"doc-asc",query:i.query,terms:i.terms,types:i.types}}async function cZ(e,t,r,n){switch(r.bucket){case 0:return Of(t,"chapter-title")?await dZ(e,t,r.key,n):{items:[],nextCursor:{bucket:1}};case 1:return Of(t,"entity","triple")?await uZ(e,t,r.key,n):{items:[],nextCursor:{bucket:2}};case 2:return Of(t,"node")?await lZ(e,t,r.key,n):{items:[],nextCursor:{bucket:3}};case 3:return Of(t,"source","summary")?await pZ(e,t,r.key,n):{items:[],nextCursor:void 0}}}async function dZ(e,t,r,n){let o=[...(await Ea(e,t.query,{...t.chapters===null?{}:{chapters:t.chapters},match:t.match,objectHitLimit:Ut,textHitLimit:0,types:["chapter-title"]}))?.objectHits??[]].filter(d=>d.ownerKind===J.chapter).sort(Ad).filter(d=>Im(d,r)).slice(0,n+1),s=await hF(e,{objectHits:o.slice(0,n),terms:t.terms,textHits:[]}),c=o.at(n-1);return{items:s,nextCursor:o.length>n&&c!==void 0?{bucket:0,key:{archiveId:c.archiveId,chapterId:Number(c.ownerId),score:c.score}}:{bucket:1}}}async function uZ(e,t,r,n){if(!t.objectCachesPopulated){let c=await fZ(e,t);await td({chunkHits:c.chunkHits,entityHits:c.entityHits,sessionId:t.sessionId})}let i=await ad(t.sessionId,1,r,n),a=i.slice(0,n).filter(c=>gZ(c,t)),o=await fF(e,a,async(c,d)=>await Sm(c,d)),s=i.at(Math.min(n,i.length)-1);return{items:o,nextCursor:i.length>n&&s!==void 0?{bucket:1,key:{archiveId:Nf(s),id:vm(s),kind:s.type==="triple"?"triple":"entity",score:s.score??0}}:{bucket:2}}}async function lZ(e,t,r,n){let i=await od(t.sessionId,r,n),a=i.slice(0,n),o=await fF(e,a,async(c,d)=>await km(c,d)),s=a.at(-1);return{items:o,nextCursor:i.length>n&&s!==void 0?{bucket:2,key:{archiveId:Nf(s),chunkId:Number(s.id.slice(13)),score:s.score??0}}:{bucket:3}}}async function pZ(e,t,r,n){let i=yZ(t),s=[...(await Ea(e,t.query,{...t.chapters===null?{}:{chapters:t.chapters},match:t.match,objectHitLimit:0,...r===void 0?{}:{textAfter:{archiveId:r.archiveId,chapterId:r.chapterId,kind:r.kind,rank:r.rank,sentenceIndex:r.sentenceIndex}},textHitLimit:mZ(n),types:i}))?.textHits??[]].sort(Ld).filter(u=>xm(u,r)).slice(0,n+1),c=await hF(e,{objectHits:[],terms:t.terms,textHits:s.slice(0,n)}),d=s.at(n-1);return{items:c,nextCursor:s.length>n&&d!==void 0?{bucket:3,key:{archiveId:d.archiveId,chapterId:d.chapterId,kind:d.kind,rank:d.rank,sentenceIndex:d.sentenceIndex}}:void 0}}function mZ(e){return Math.max(e+1,e*3+1,100)}function hZ(e,t){if(e===void 0)return;if(e.length!==(t?.length??0))throw new Error("Search cursor does not match the requested result types.");let r=new Set(t??[]);if(e.some(n=>!r.has(n)))throw new Error("Search cursor does not match the requested result types.")}async function fZ(e,t){let r=await Ea(e,t.query,{...t.chapters===null?{}:{chapters:t.chapters},match:t.match,objectHitLimit:Ut,textHitLimit:0,types:null}),n=new Map,i=new Map;for(let a of r?.objectHits??[]){if(a.ownerKind===J.entity){let o=uF(a.archiveId,a.ownerId),s=n.get(o)??[];s.push(a.score),n.set(o,s);continue}if(a.ownerKind===J.chunk){let o=uF(a.archiveId,a.ownerId),s=i.get(o)??[];s.push(a.score),i.set(o,s)}}return{chunkHits:[...i].map(([a,o])=>{let{archiveId:s,objectId:c}=lF(a);return{archiveId:s,chunkId:Number(c),propertyTopScores:o}}),entityHits:[...n].map(([a,o])=>{let{archiveId:s,objectId:c}=lF(a);return{archiveId:s,propertyTopScores:o,qid:c}})}}async function hF(e,t){let r=[];for(let n of el(t)){let i=await Ts(e,n,{operation:"searching library objects"}),a=Cr(i),o=await gi(i,async s=>await $o(s,{objectHits:t.objectHits.filter(c=>c.archiveId===i.id),terms:t.terms,textHits:t.textHits.filter(c=>c.archiveId===i.id)}));r.push(...o.map(s=>({...s,...a})))}return r}async function fF(e,t,r){let n=[];for(let i of[...new Set(t.map(a=>Nf(a)))].sort((a,o)=>a-o)){let a=await Ts(e,i,{operation:"searching library objects"}),o=Cr(a),s=t.filter(c=>Nf(c)===i);await gi(a,async c=>{for(let d of s){let u=await r(c,d);u!==void 0&&n.push({...u,...o})}})}return n}function Of(e,...t){return e.types===null||t.some(r=>e.types?.includes(r))}function gZ(e,t){return t.types===null||e.type==="entity"&&t.types.includes("entity")||e.type==="triple"&&t.types.includes("triple")}function yZ(e){return e.types===null?["source","summary"]:e.types.filter(t=>t==="source"||t==="summary")}function gF(e){return e.isDefault?"library:default":`library:${e.publicId??"unknown"}`}function Nf(e){if(e.archiveId===void 0)throw new Error("Internal error: library search hit is missing archiveId.");return e.archiveId}function uF(e,t){return`${e}:${t}`}function lF(e){let t=e.indexOf(":");if(t<=0)throw new Error(`Invalid library search cache key: ${e}`);return{archiveId:Number(e.slice(0,t)),objectId:e.slice(t+1)}}var yF=l(()=>{"use strict";Yp();im();Vc();Cm();Em();Jt();Rm();Yi();Qi();Ju();lI()});async function Rs(e,t,r={}){if(pF(r))return await mF(e,t,r);let n=SZ(r),i=await Ea(e,t,{objectHitLimit:n,textHitLimit:n});if(i===void 0)return Qn(t,[],r);let a=[];for(let o of el(i)){let s=await Ts(e,o,{operation:"searching library objects"}),c=Cr(s),d=await gi(s,async u=>await $o(u,{objectHits:i.objectHits.filter(p=>p.archiveId===s.id),terms:i.terms,textHits:i.textHits.filter(p=>p.archiveId===s.id)}));a.push(...d.map(u=>({...u,...c})))}return Qn(t,a,r,i.terms)}function SZ(e){return Math.max((e.limit??tl)*bZ,wZ)}async function xa(e,t={}){let r=[],n=await qW(e,{includeText:PZ(t)}),i=el(n);for(let a of i){let o=await Ts(e,a,{operation:"listing library objects"}),s=Cr(o),c=await gi(o,async d=>await $o(d,{objectHits:n.objectHits.filter(u=>u.archiveId===o.id),terms:n.terms,textHits:n.textHits.filter(u=>u.archiveId===o.id)}));r.push(...c.map(d=>({...d,...s})))}return nm(r,t)}async function pI(e,t,r={}){let n=await jf(e,t,async(a,o)=>({...await ht(a,t,r),...Cr(o)})),i=bF(n);return(i.type==="entity"||i.type==="triple")&&n.length>1?{...i,evidence:EZ(n,r.evidenceLimit??3,r.order??"doc-asc")}:i}async function Ps(e,t,r={}){let n=r.limit??tl,a=Df(r.cursor,"evidence")+n,o=await jf(e,t,async(s,c)=>{let{cursor:d,...u}=r,p=await Qt(s,t,{...u,limit:a}),m=Cr(c);return{...p,items:p.items.map(f=>({...f,...m}))}});return IZ(o.flatMap(s=>s.items),r)}async function rl(e,t,r={}){let n=r.limit??tl,a=Df(r.cursor,"related")+n,o=await jf(e,t,async(s,c)=>{let{cursor:d,...u}=r,p=await _t(s,t,{...u,limit:a}),m=Cr(c);return{...p,items:p.items.map(f=>({...f,...m}))}});return xZ(o.flatMap(s=>s.items),r)}async function mI(e,t,r){let n=await jf(e,t,async(a,o)=>{let s=await ni(a,t,r),c=Cr(o);return{...s,anchor:{...s.anchor,...c},related:s.related.map(d=>({...d,...c}))}}),[i]=n;if(i===void 0)throw new Error(`Wiki Graph library object was not found: ${t}`);return{anchor:bF(n.map(a=>a.anchor)),budget:r,related:n.flatMap(a=>a.related)}}async function hI(e){let t=await db(e);return de(t.uri)??{isDefault:t.isDefault,kind:"scope",publicId:t.publicId}}async function jf(e,t,r){let n=await U(e),i=await VE(e,t);if(i.length===0){if(!AZ(t))throw new Error(`Wiki Graph library object was not found: ${t}`);return await kZ(e,t,r)}let a=[];for(let o of i){let s=await Yu(n,o);if(!Lf(s))throw new Error(`Wiki Graph library archive ${o} is not readable while reading ${t}.`);try{a.push(await gi(s,async c=>await r(c,s)))}catch(c){throw new Error(`Failed to read Wiki Graph library archive ${o} (${s.uri}) for ${t}: ${SF(c)}`,{cause:c})}}if(a.length===0)throw new Error(`Wiki Graph library object was not found: ${t}`);return a}async function kZ(e,t,r){let n=[];for(let i of await RZ(e))try{n.push(await gi(i,async a=>await r(a,i)))}catch(a){if(_Z(a))continue;throw new Error(`Failed to read Wiki Graph library archive ${i.id} (${i.uri}) for ${t}: ${SF(a)}`,{cause:a})}if(n.length===0)throw new Error(`Wiki Graph library object was not found: ${t}`);return n}function bF(e){let[t]=e;if(t===void 0)throw new Error("Internal error: cannot merge an empty library page result.");let r=vZ(e);if(r.length===1)return t;let{archiveId:n,libraryArchiveUri:i,...a}=t;return{...a,sources:r}}function EZ(e,t,r){let n=e.flatMap(d=>d.type==="entity"||d.type==="triple"?[d]:[]),a=(r==="doc-desc"?n.slice().reverse():n).flatMap(d=>d.evidence.sources.map(u=>({...u,...d.archiveId===void 0?{}:{archiveId:d.archiveId},...d.libraryArchiveUri===void 0?{}:{libraryArchiveUri:d.libraryArchiveUri}}))),o=n.reduce((d,u)=>d+u.evidence.total,0),s=a.slice(0,t),c=s.length;return{nextCursor:t<o?String(t):null,shown:c,sources:s,total:o}}function IZ(e,t){let r=t.limit??tl,n=Df(t.cursor,"evidence"),i=[...e].sort((s,c)=>CZ(s,c,t.order??"doc-asc")),a=i.slice(n,n+r),o=n+a.length;return{items:a,limit:r,nextCursor:o<i.length?String(o):null}}function xZ(e,t){let r=t.limit??tl,n=Df(t.cursor,"related"),i=[...e].sort((s,c)=>TZ(s,c,t.order??"doc-asc")),a=i.slice(n,n+r),o=n+a.length;return{items:a,limit:r,nextCursor:o<i.length?String(o):null}}function vZ(e){let t=new Map;for(let r of e)r.archiveId===void 0||r.libraryArchiveUri===void 0||t.set(r.archiveId,{archiveId:r.archiveId,libraryArchiveUri:r.libraryArchiveUri});return[...t.values()].sort((r,n)=>r.archiveId-n.archiveId)}function CZ(e,t,r){let n=r==="doc-asc"?1:-1;return(wF(e.archiveId,t.archiveId)||e.chapterId-t.chapterId||e.startSentenceIndex-t.startSentenceIndex||e.endSentenceIndex-t.endSentenceIndex||e.id.localeCompare(t.id))*n}function TZ(e,t,r){let n=r==="doc-asc"?1:-1;return(wF(e.archiveId,t.archiveId)||e.id.localeCompare(t.id))*n}function wF(e,t){return(e??Number.MAX_SAFE_INTEGER)-(t??Number.MAX_SAFE_INTEGER)}function Df(e,t){if(e===void 0)return 0;if(!/^(0|[1-9][0-9]*)$/u.test(e))throw new Error(`Invalid library ${t} cursor: ${e}`);return Number(e)}async function RZ(e){return await ka(e),(await gt(e)).filter(Lf)}function PZ(e){return e.types!==void 0&&e.types.some(t=>t==="source"||t==="summary")}function SF(e){return e instanceof Error?e.message:String(e)}function _Z(e){return e instanceof Error&&e.message.includes(" was not found in this archive.")}function AZ(e){return/^wikg:\/\/(?:chapter\/[1-9][0-9]*\/)?triple\/Q[1-9][0-9]*\/[^/]+\/Q[1-9][0-9]*\/?$/u.test(e)}var tl,bZ,wZ,kF=l(()=>{"use strict";vS();im();yF();Jt();zu();lI();mo();Ju();tl=20,bZ=20,wZ=100});var xf=l(()=>{"use strict";mo();zu();kF();Ju();Oc()});import{posix as LZ,resolve as OZ,sep as EF}from"path";import{open as NZ}from"yauzl";async function IF(e){return await new Promise((t,r)=>{let n=[];e.on("entry",i=>{if(i.fileName.endsWith("/")){e.readEntry();return}n.push(i),e.readEntry()}),e.once("end",()=>{t(n)}),e.once("error",i=>{r(i)}),e.readEntry()})}function xF(e){return jZ.some(t=>t.test(e))}function vF(e,t,r){let n=OZ(e),i=n.endsWith(EF)?n:`${n}${EF}`;if(!(t===n||t.startsWith(i)))throw new Error(`Invalid archive entry path: ${r}`)}function Mf(e){let t=e.replaceAll("\\","/").trim(),r=t.startsWith("/")?t.slice(1):t;return LZ.normalize(r).replace(/^(\.\/)+/u,"").replace(/^\/+/u,"")}async function CF(e){return await new Promise((t,r)=>{NZ(e,{autoClose:!1,lazyEntries:!0},(n,i)=>{if(n!==null||i===void 0){r(n??new Error(`Cannot open archive: ${e}`));return}t(i)})})}async function fI(e,t){return await new Promise((r,n)=>{e.openReadStream(t,(i,a)=>{if(i!==null||a===void 0){n(i??new Error(`Cannot open archive entry: ${t.fileName}`));return}r(a)})})}async function TF(e,t){let r=[],n=await fI(e,t);return await new Promise((i,a)=>{n.on("data",o=>{r.push(o)}),n.once("end",i),n.once("error",a)}),Buffer.concat(r).toString("utf8")}var jZ,RF=l(()=>{"use strict";jZ=[/^manifest\.json$/u,/^database\.db$/u,/^toc\.json$/u,/^cover\/(?:data\.bin|info\.json)$/u,/^summaries\/serial-\d+\.txt$/u,/^fragments\/serial-\d+\/fragment_\d+\.json$/u]});import{createWriteStream as DZ}from"fs";import{mkdir as MZ}from"fs/promises";import{dirname as WZ,resolve as FZ}from"path";import{pipeline as GZ}from"stream/promises";async function PF(e,t){let r=await CF(e);try{let n=await IF(r);await $Z(r,n);for(let i of n){let a=Mf(i.fileName);if(a===""||!xF(a))continue;let o=FZ(t,a);vF(t,o,a),await MZ(WZ(o),{recursive:!0}),await GZ(await fI(r,i),DZ(o))}}finally{r.close()}}async function $Z(e,t){let r=new Set(t.map(n=>Mf(n.fileName)));if(!r.has("database.db")||!r.has("toc.json"))throw new Error("Unsupported legacy sdpub archive.");if(r.has("manifest.json")){let n=t.find(i=>Mf(i.fileName)==="manifest.json");if(n===void 0)throw new Error("Unsupported legacy sdpub archive.");BZ(await TF(e,n))}}function BZ(e){try{if(JSON.parse(e).formatVersion===UZ)return}catch{throw new Error("Unsupported legacy sdpub archive.")}throw new Error("Unsupported legacy sdpub archive.")}var UZ,_F=l(()=>{"use strict";RF();UZ=1});async function Wf(e){let t=await e.queryAll(`
|
|
2689
|
+
SELECT name
|
|
2690
|
+
FROM sqlite_master
|
|
2691
|
+
WHERE type = 'table'
|
|
2692
|
+
`,void 0,r=>String(r.name));return new Set(t)}async function AF(e,t){let r=await e.queryAll(`PRAGMA table_info(${HZ(t)})`,void 0,n=>String(n.name));return new Set(r)}function HZ(e){return`"${e.replaceAll('"','""')}"`}var gI=l(()=>{"use strict"});async function LF(e){let t=await $.open(e);try{await qZ(t),await KZ(t)}finally{await t.close()}}async function qZ(e){let t=await Wf(e);t.has("reading_edges")||t.has("knowledge_edges")&&await e.run(`
|
|
2693
|
+
ALTER TABLE knowledge_edges
|
|
2694
|
+
RENAME TO reading_edges
|
|
2695
|
+
`)}async function KZ(e){(await AF(e,"serials")).has("document_order")||await e.run(`
|
|
2696
|
+
ALTER TABLE serials
|
|
2697
|
+
ADD COLUMN document_order INTEGER NOT NULL DEFAULT 0
|
|
2698
|
+
`),await e.run(`
|
|
2699
|
+
CREATE INDEX IF NOT EXISTS idx_serials_document_order
|
|
2700
|
+
ON serials(document_order, id)
|
|
2701
|
+
`)}var OF=l(()=>{"use strict";M();gI()});import{readFile as JZ,readdir as NF}from"fs/promises";import{join as yI}from"path";async function jF(e){let t=yI(e,"fragments");try{let r=await NF(t,{withFileTypes:!0}),n=[];for(let i of r){if(!i.isDirectory())continue;let a=/^serial-(\d+)$/u.exec(i.name);a!==null&&n.push(Number(a[1]))}return n.sort((i,a)=>i-a)}catch(r){if(R(r)&&r.code==="ENOENT")return[];throw r}}async function DF(e,t){let r=yI(e,"fragments",`serial-${t}`),n=await NF(r,{withFileTypes:!0}),i=[];for(let a of n){if(!a.isFile())continue;let o=/^fragment_(\d+)\.json$/u.exec(a.name);if(o===null)continue;let s=yI(r,a.name),c=zZ(await JZ(s,"utf8"));i.push({content:c,fragmentId:Number(o[1]),path:s,signature:VZ(c)})}return i.sort((a,o)=>a.fragmentId-o.fragmentId)}function zZ(e){let t=JSON.parse(e);if(typeof t!="object"||t===null)throw new TypeError("Legacy fragment file must contain sentences.");let r=t;if(!Array.isArray(r.sentences))throw new TypeError("Legacy fragment file must contain sentences.");let n=r.sentences.map(a=>{if(typeof a!="object"||a===null||!("text"in a)||typeof a.text!="string")throw new TypeError("Legacy fragment sentence must contain text.");let o=a,s=o.text,c=o.wordsCount,d=typeof c=="number"?c:QZ(s);return{text:s,wordsCount:d}}),i=typeof r.summary=="string"?r.summary:"";return{sentences:n,summary:i}}function QZ(e){let t=e.trim();return t===""?0:t.split(/\s+/u).length}function VZ(e){return JSON.stringify(e.sentences.map(t=>t.text))}function MF(e){if(e.length<2||e.length%2!==0)return;let t=e.length/2,r=e.slice(0,t),n=e.slice(t);for(let c=0;c<t;c+=1)if(r[c]?.signature!==n[c]?.signature)return;let a=n.some(c=>c.content.summary.trim()!=="")?n:r,o=new Map;return{canonicalFragments:a.map((c,d)=>{let u=r[d],p=n[d];return u!==void 0&&o.set(u.fragmentId,d),p!==void 0&&o.set(p.fragmentId,d),{...c,fragmentId:d}}),fragmentIdMap:o}}var WF=l(()=>{"use strict";H()});import{join as XZ}from"path";async function FF(e,t){let r=await $.open(XZ(e,"database.db"));try{let n=await Wf(r);await r.transaction(async()=>{n.has("chunks")&&await YZ(r,t),n.has("chunk_sentences")&&await ZZ(r,t),n.has("mentions")&&await e7(r,t),n.has("mention_link_evidence_sentences")&&await t7(r,t),n.has("fragment_groups")&&await r7(r,t)})}finally{await r.close()}}async function YZ(e,t){let r=await e.queryAll(`
|
|
2702
|
+
SELECT *
|
|
2703
|
+
FROM chunks
|
|
2704
|
+
ORDER BY id
|
|
2705
|
+
`,void 0,n=>n);await e.run("ALTER TABLE chunks RENAME TO legacy_chunks"),await e.run(`
|
|
2706
|
+
CREATE TABLE chunks (
|
|
2707
|
+
id INTEGER PRIMARY KEY,
|
|
2708
|
+
generation INTEGER NOT NULL,
|
|
2709
|
+
serial_id INTEGER NOT NULL,
|
|
2710
|
+
sentence_index INTEGER NOT NULL,
|
|
2711
|
+
label TEXT NOT NULL,
|
|
2712
|
+
content TEXT NOT NULL,
|
|
2713
|
+
retention TEXT,
|
|
2714
|
+
importance TEXT,
|
|
2715
|
+
wordsCount INTEGER NOT NULL DEFAULT 0,
|
|
2716
|
+
weight REAL NOT NULL DEFAULT 0.0
|
|
2717
|
+
)
|
|
2718
|
+
`);for(let n of r){let i=Number(n.serial_id),a=_s(t,i,Number(n.fragment_id),Number(n.sentence_index));await e.run(`
|
|
2719
|
+
INSERT INTO chunks (
|
|
2720
|
+
id, generation, serial_id, sentence_index, label, content,
|
|
2721
|
+
retention, importance, wordsCount, weight
|
|
2722
|
+
)
|
|
2723
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2724
|
+
`,[n.id??null,n.generation??0,i,a,n.label??"",n.content??"",n.retention??null,n.importance??null,n.wordsCount??0,n.weight??0])}await e.run("DROP TABLE legacy_chunks")}async function ZZ(e,t){let r=await e.queryAll(`
|
|
2725
|
+
SELECT chunk_id, serial_id, fragment_id, sentence_index
|
|
2726
|
+
FROM chunk_sentences
|
|
2727
|
+
`,void 0,n=>n);await e.run("DROP TABLE chunk_sentences"),await e.run(`
|
|
2728
|
+
CREATE TABLE chunk_sentences (
|
|
2729
|
+
chunk_id INTEGER NOT NULL,
|
|
2730
|
+
serial_id INTEGER NOT NULL,
|
|
2731
|
+
sentence_index INTEGER NOT NULL,
|
|
2732
|
+
FOREIGN KEY (chunk_id) REFERENCES chunks(id),
|
|
2733
|
+
PRIMARY KEY (chunk_id, serial_id, sentence_index)
|
|
2734
|
+
)
|
|
2735
|
+
`);for(let n of r){let i=Number(n.serial_id);await e.run(`
|
|
2736
|
+
INSERT OR IGNORE INTO chunk_sentences (
|
|
2737
|
+
chunk_id, serial_id, sentence_index
|
|
2738
|
+
)
|
|
2739
|
+
VALUES (?, ?, ?)
|
|
2740
|
+
`,[Ff(n.chunk_id),i,_s(t,i,Number(n.fragment_id),Number(n.sentence_index))])}}async function e7(e,t){let r=await e.queryAll(`
|
|
2741
|
+
SELECT *
|
|
2742
|
+
FROM mentions
|
|
2743
|
+
ORDER BY id
|
|
2744
|
+
`,void 0,n=>n);await e.run("ALTER TABLE mentions RENAME TO legacy_mentions"),await e.run(`
|
|
2745
|
+
CREATE TABLE mentions (
|
|
2746
|
+
id TEXT PRIMARY KEY,
|
|
2747
|
+
chapter_id INTEGER NOT NULL,
|
|
2748
|
+
sentence_index INTEGER,
|
|
2749
|
+
range_start INTEGER NOT NULL,
|
|
2750
|
+
range_end INTEGER NOT NULL,
|
|
2751
|
+
surface TEXT NOT NULL,
|
|
2752
|
+
qid TEXT NOT NULL,
|
|
2753
|
+
confidence REAL,
|
|
2754
|
+
note TEXT,
|
|
2755
|
+
FOREIGN KEY (chapter_id) REFERENCES serials(id)
|
|
2756
|
+
)
|
|
2757
|
+
`);for(let n of r){let i=Number(n.chapter_id),a=n.sentence_index===null||n.sentence_index===void 0?null:_s(t,i,Number(n.fragment_id),Number(n.sentence_index));await e.run(`
|
|
2758
|
+
INSERT OR REPLACE INTO mentions (
|
|
2759
|
+
id, chapter_id, sentence_index, range_start, range_end, surface,
|
|
2760
|
+
qid, confidence, note
|
|
2761
|
+
)
|
|
2762
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2763
|
+
`,[Ff(n.id),i,a,n.range_start??0,n.range_end??0,n.surface??"",n.qid??"",n.confidence??null,n.note??null])}await e.run("DROP TABLE legacy_mentions")}async function t7(e,t){let r=await e.queryAll(`
|
|
2764
|
+
SELECT link_id, chapter_id, fragment_id, sentence_index
|
|
2765
|
+
FROM mention_link_evidence_sentences
|
|
2766
|
+
`,void 0,n=>n);await e.run("DROP TABLE mention_link_evidence_sentences"),await e.run(`
|
|
2767
|
+
CREATE TABLE mention_link_evidence_sentences (
|
|
2768
|
+
link_id TEXT NOT NULL,
|
|
2769
|
+
chapter_id INTEGER NOT NULL,
|
|
2770
|
+
sentence_index INTEGER NOT NULL,
|
|
2771
|
+
FOREIGN KEY (link_id) REFERENCES mention_links(id),
|
|
2772
|
+
PRIMARY KEY (link_id, chapter_id, sentence_index)
|
|
2773
|
+
)
|
|
2774
|
+
`);for(let n of r){let i=Number(n.chapter_id);await e.run(`
|
|
2775
|
+
INSERT OR IGNORE INTO mention_link_evidence_sentences (
|
|
2776
|
+
link_id, chapter_id, sentence_index
|
|
2777
|
+
)
|
|
2778
|
+
VALUES (?, ?, ?)
|
|
2779
|
+
`,[Ff(n.link_id),i,_s(t,i,Number(n.fragment_id),Number(n.sentence_index))])}}async function r7(e,t){let r=await e.queryAll(`
|
|
2780
|
+
SELECT serial_id, group_id, fragment_id
|
|
2781
|
+
FROM fragment_groups
|
|
2782
|
+
ORDER BY serial_id, group_id, fragment_id
|
|
2783
|
+
`,void 0,n=>n);await e.run("DROP TABLE fragment_groups"),await e.run(`
|
|
2784
|
+
CREATE TABLE sentence_groups (
|
|
2785
|
+
serial_id INTEGER NOT NULL,
|
|
2786
|
+
group_id INTEGER NOT NULL,
|
|
2787
|
+
start_sentence_index INTEGER NOT NULL,
|
|
2788
|
+
end_sentence_index INTEGER NOT NULL,
|
|
2789
|
+
PRIMARY KEY (serial_id, group_id, start_sentence_index, end_sentence_index)
|
|
2790
|
+
)
|
|
2791
|
+
`);for(let n of r){let i=Number(n.serial_id),a=_s(t,i,Number(n.fragment_id),0),o=n7(t,i,Number(n.fragment_id));await e.run(`
|
|
2792
|
+
INSERT OR IGNORE INTO sentence_groups (
|
|
2793
|
+
serial_id, group_id, start_sentence_index, end_sentence_index
|
|
2794
|
+
)
|
|
2795
|
+
VALUES (?, ?, ?, ?)
|
|
2796
|
+
`,[i,Ff(n.group_id),a,o])}}function _s(e,t,r,n){let a=e.get(t)?.get(r,n);if(a===void 0)throw new Error(`Cannot remap legacy sentence ${t}:${r}:${n}.`);return a}function n7(e,t,r){let n=0,i;for(;;){let a=e.get(t)?.get(r,n);if(a===void 0)break;i=a,n+=1}return i??_s(e,t,r,0)}function Ff(e){if(e===void 0)throw new TypeError("Expected a SQLite bind value.");return e}var GF=l(()=>{"use strict";M();gI()});import{readFile as i7,readdir as a7}from"fs/promises";import{join as UF}from"path";async function $F(e){let t=await o7(e),r=await ye.open(e);try{for(let n of t)await r.writeSummary(n.serialId,n.text)}finally{await r.release()}}async function o7(e){let t=UF(e,"summaries");try{let r=await a7(t,{withFileTypes:!0}),n=[];for(let i of r){if(!i.isFile())continue;let a=/^serial-(\d+)\.txt$/u.exec(i.name);a!==null&&n.push({serialId:Number(a[1]),text:await i7(UF(t,i.name),"utf8")})}return n.sort((i,a)=>i.serialId-a.serialId)}catch(r){if(R(r)&&r.code==="ENOENT")return[];throw r}}var BF=l(()=>{"use strict";tb();H()});import{mkdir as s7,writeFile as c7}from"fs/promises";import{join as HF}from"path";async function qF(e,t,r){await e.run(`
|
|
2797
|
+
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
2798
|
+
id INTEGER PRIMARY KEY,
|
|
2799
|
+
kind INTEGER NOT NULL,
|
|
2800
|
+
chapter_id INTEGER NOT NULL,
|
|
2801
|
+
sentence_index INTEGER NOT NULL,
|
|
2802
|
+
words_count INTEGER NOT NULL DEFAULT 0,
|
|
2803
|
+
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
2804
|
+
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
2805
|
+
UNIQUE(kind, chapter_id, sentence_index)
|
|
2806
|
+
)
|
|
2807
|
+
`),await s7(HF(t,"texts","source"),{recursive:!0}),await c7(HF(t,"texts","source",`${r.serialId}.txt`),r.text,"utf8"),await e.run(`
|
|
2808
|
+
DELETE FROM text_sentence_records
|
|
2809
|
+
WHERE kind = 1 AND chapter_id = ?
|
|
2810
|
+
`,[r.serialId]);let n=0,i=0;for(let a of r.fragments)for(let o of a.content.sentences){let s=Buffer.byteLength(o.text,"utf8");await e.run(`
|
|
2811
|
+
INSERT OR REPLACE INTO text_sentence_records (
|
|
2812
|
+
kind,
|
|
2813
|
+
chapter_id,
|
|
2814
|
+
sentence_index,
|
|
2815
|
+
words_count,
|
|
2816
|
+
byte_offset,
|
|
2817
|
+
byte_length
|
|
2818
|
+
)
|
|
2819
|
+
VALUES (1, ?, ?, ?, ?, ?)
|
|
2820
|
+
`,[r.serialId,i,o.wordsCount,n,s]),n+=s,i+=1}}var KF=l(()=>{"use strict"});import{rm as JF}from"fs/promises";import{join as bI}from"path";async function zF(e){let t=await jF(e),r=new Map;for(let n of t){let i=await DF(e,n),a=MF(i),o=a?.canonicalFragments??i,s=a?.fragmentIdMap??new Map,c=new Map(o.map(f=>[f.fragmentId,f])),d=new Map,u=[],p=0;for(let f of o)for(let w=0;w<f.content.sentences.length;w+=1){let b=f.content.sentences[w];b!==void 0&&(d.set(`${f.fragmentId}:${w}`,p),u.push(b.text),p+=1)}for(let[f,w]of s){let b=c.get(w);if(b!==void 0)for(let E=0;E<b.content.sentences.length;E+=1){let N=d.get(`${w}:${E}`);N!==void 0&&d.set(`${f}:${E}`,N)}}r.set(n,{get:(f,w)=>d.get(`${f}:${w}`),serialId:n});let m=await $.open(bI(e,"database.db"));try{await qF(m,e,{fragments:o,serialId:n,text:u.join("")})}finally{await m.close()}}await FF(e,r),await $F(e),await JF(bI(e,"fragments"),{force:!0,recursive:!0}),await JF(bI(e,"summaries"),{force:!0,recursive:!0})}var QF=l(()=>{"use strict";M();WF();GF();BF();KF()});var VF=l(()=>{"use strict";QF()});import{rm as d7}from"fs/promises";import{join as u7,resolve as XF}from"path";async function wI(e,t=p7(e)){if(XF(e)===XF(t))throw new Error("Legacy migration output path must differ from input path.");let r=await Ye("sdpub-upgrade");try{return await PF(e,r),await LF(u7(r,"database.db")),await zF(r),await l7(r),await zr(r,t),{inputPath:e,outputPath:t}}finally{await d7(r,{force:!0,recursive:!0})}}async function l7(e){let t=await ye.open(e);try{let r=await t.readToc();r!==void 0&&await t.replaceToc(r)}finally{await t.release()}}function p7(e){return e.toLowerCase().endsWith(".sdpub")?`${e.slice(0,-6)}.wikg`:`${e}.wikg`}var SI=l(()=>{"use strict";Yr();X();xr();_F();OF();VF()});import{rm as m7}from"fs/promises";import{homedir as h7}from"os";import{resolve as YF}from"path";async function nl(e,t={}){if(g7(e)){let a=await wI(e,t.outputPath);return{kind:"sdpub",inputPath:a.inputPath,outputPath:a.outputPath,status:"migrated"}}if(t.outputPath!==void 0)throw new Error("Only sdpub maintenance upgrade supports --output.");if(y7(e)){let a=ct(),o=await Ja();await ke();let s=await Ja();return{kind:"home",path:a,schemaVersionBefore:o,schemaVersionAfter:s,status:o===s?"already-current":"upgraded"}}if(e.startsWith("wikg://lib")){let a=de(e);if(a===void 0)throw new Error(`Invalid Wiki Graph library upgrade target: ${e}`);if(a.kind==="archive")throw new Error(`Library archive URIs cannot be upgraded individually. Run: wg maintenance upgrade ${w7(a)}`);return await eG(a)}let r=f7(e),n=await Vr(r);await kp(r);let i=await Vr(r);return{kind:"archive",path:r,schemaVersionBefore:n,schemaVersionAfter:i,status:n===i?"already-current":"upgraded"}}async function kI(e){let t=await U(e),r=await gt({isDefault:t.isDefault,kind:"scope",...t.isDefault?{}:{publicId:t.publicId}});for(let n of r){if(!rG(n))continue;if(await Vr(n.path)<Hi)throw new Error(`This Wiki Graph library must be upgraded before use.
|
|
2821
|
+
Run: wg maintenance upgrade ${t.uri}`)}}async function eG(e){await ke();let t=await U(e);return await Pe(t.id,"write",async()=>{await b7(t);let r=(await gt({isDefault:t.isDefault,kind:"scope",...t.isDefault?{}:{publicId:t.publicId}})).filter(rG),n=[],i=[];for(let a of r){let o=await Vr(a.path);if(o===Hi){i.push({path:a.path,publicId:a.publicId,schemaVersionAfter:o,schemaVersionBefore:o,uri:a.uri});continue}try{await kp(a.path)}catch(s){return{failed:{message:S7(s),path:a.path,publicId:a.publicId,uri:a.uri},kind:"lib",library:ZF(t),skipped:i,status:"partial",upgraded:n}}n.push({path:a.path,publicId:a.publicId,schemaVersionAfter:await Vr(a.path),schemaVersionBefore:o,uri:a.uri})}return{kind:"lib",library:ZF(t),skipped:i,status:n.length===0?"already-current":"upgraded",upgraded:n}})}function f7(e){if(e.startsWith("wikg://"))return Lc(e);if(!e.endsWith(".wikg"))throw new Error(`Unsupported maintenance upgrade target: ${e}. Expected home, a .wikg archive, a .sdpub archive, or wikg://lib.`);return tG(e)}function g7(e){return e.toLowerCase().endsWith(".sdpub")}function y7(e){let t=tG(e).replace(/[\\/]+$/u,"");return e==="home"||e==="~/.wikigraph"||t===ct().replace(/[\\/]+$/u,"")}function tG(e){return e.startsWith("~/")?YF(h7(),e.slice(2)):YF(e)}function rG(e){return e.exists&&e.status==="present"}async function b7(e){await m7(e.stagingPath,{force:!0,recursive:!0})}function w7(e){return e.isDefault?"wikg://lib":`wikg://lib/${e.publicId??"<lib-id>"}`}function ZF(e){return{folderPath:e.folderPath,id:e.id,publicId:e.publicId,stagingPath:e.stagingPath,uri:e.uri}}function S7(e){return e instanceof Error?e.message:String(e)}var nG=l(()=>{"use strict";xf();Ue();SI();xr();Ep()});var O=l(()=>{"use strict";Nr();Zx();Qg();oc();jt();Tv();Hl();Ul();xf();Ue();Yr();ns();Bd();ga();Gk();mi();$r();xr();X();ji();ts();Ep();nG();Qi();SI();H();mi();Qi();mi();mi();mi();mi();ui();ga();X();ys()});function h(e,t){return`${e}
|
|
2822
|
+
See: ${t}`}var _,EI=l(()=>{"use strict";O();_={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 k7}from"path";function il(e){switch(k7(e).toLowerCase()){case".epub":return"epub";case".markdown":case".md":return"markdown";case".wikg":return"wikg";case".txt":return"txt";default:return}}function II(e){return e==="markdown"||e==="txt"}function As(e,t){let r=e.trim().toLowerCase();if(E7(r))return r;throw new Error(h(`Invalid ${t}: ${e}. Expected one of ${Gf.join(", ")}.`,_.format))}function E7(e){return Gf.includes(e)}var Gf,iG=l(()=>{"use strict";EI();Gf=["wikg","epub","txt","markdown"]});import{AsyncLocalStorage as I7}from"async_hooks";async function aG(e,t){return await At.run(e,t)}function al(){return At.getStore()?.cwd??process.cwd()}function oG(){return At.getStore()?.env??process.env}function sG(){return At.getStore()?.devProjectRoot}function cG(){return At.getStore()?.stateDir}function dG(){return x7??At.getStore()?.queueAutostart??!0}function uG(){return At.getStore()?.stdin??process.stdin}function xI(){return At.getStore()?.stdout??process.stdout}function lG(){return At.getStore()?.stderr??process.stderr}function pG(){return At.getStore()?.stdoutIsTTY??xI().isTTY}function mG(){return At.getStore()?.exitCode??process.exitCode}function ol(e){let t=At.getStore();if(t===void 0){process.exitCode=e;return}t.exitCode=e}var At,x7,yi=l(()=>{"use strict";At=new I7});import{createReadStream as v7}from"fs";import{rm as C7}from"fs/promises";import{join as T7}from"path";function bi(){let e=uG();return e.setEncoding("utf8"),e}async function hG(e){for await(let t of v7(e,{encoding:"utf8"}))await CI(String(t))}async function S(e){await CI(e)}async function vI(e){await new Promise((t,r)=>{lG().write(e,n=>{if(n==null){t();return}r(n)})})}async function fG(e){await CI(e)}async function CI(e){await new Promise((t,r)=>{xI().write(e,n=>{if(n==null){t();return}if(R7(n)){ol(0),t();return}r(n)})})}function R7(e){return e.code==="EPIPE"}async function gG(e,t){let r=await Ye("cli-output");return{directoryPath:r,filePath:T7(r,`output${t}`)}}async function yG(e){await C7(e,{force:!0,recursive:!0})}var bG=l(()=>{"use strict";O();yi()});function L(e){return`${JSON.stringify(Uf(e),null,2)}
|
|
2823
|
+
`}function Ls(e){return`${JSON.stringify(Uf(e))}
|
|
2824
|
+
`}function Uf(e){if(e===null||typeof e=="boolean"||typeof e=="number"||typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>Uf(t)??null);if(typeof e=="object")return _7(e)}function _7(e){let t=Object.entries(e).map(([n,i],a)=>({index:a,key:n,priority:P7.get(n)??Number.MAX_SAFE_INTEGER,value:i})),r={};for(let n of t.sort((i,a)=>i.priority-a.priority||i.index-a.index)){let i=Uf(n.value);i!==void 0&&(r[n.key]=i)}return r}var P7,wG=l(()=>{"use strict";P7=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 L7(e){return e!==""&&A7.test(e)?e:`'${e.replaceAll("'","'\\''")}'`}function O7(e){return e.map(L7).join(" ")}function fe(e){return O7(["wg",...e])}var A7,SG=l(()=>{"use strict";O();A7=/^[A-Za-z0-9_@%+=:,./-]+$/u});function $f(e){let t=kG(e);return t.length===0?"":`${t.join(`
|
|
2825
|
+
`)}
|
|
2826
|
+
`}function kG(e,t=""){return e.flatMap((r,n)=>{let i=n===e.length-1,a=i?"\u2514\u2500 ":"\u251C\u2500 ",o=`${t}${i?" ":"\u2502 "}`;return[`${t}${a}${r.label}`,...kG(r.children,o)]})}var EG=l(()=>{"use strict"});import{existsSync as xG,readFileSync as vG}from"fs";import{dirname as N7,join as IG,parse as j7,resolve as D7}from"path";function M7(e){let t=JSON.parse(vG(e,"utf8"));return typeof t=="object"&&t!==null&&"name"in t&&t.name==="wiki-graph"}function W7(){let e=al(),t=j7(e).root;for(;;){for(let r of[IG(e,"package.json"),IG(e,"packages","cli","package.json")])if(xG(r)&&M7(r))return r;if(e===t)return;e=N7(e)}}function F7(){let e=globalThis.__WIKIGRAPH_CLI_DIST_DIR__,t=[typeof e=="string"?D7(e,"../package.json"):void 0,W7()];for(let r of t)if(r!==void 0&&xG(r))return r;throw new Error("Could not locate CLI package.json")}function CG(){let e=F7(),t=JSON.parse(vG(e,"utf8"));if(typeof t=="object"&&t!==null&&"version"in t&&typeof t.version=="string")return t.version;throw new Error(`Invalid package version in ${e}`)}var TG=l(()=>{"use strict";yi()});var T=l(()=>{"use strict";EI();iG();bG();wG();SG();EG();TG()});async function En(e){return await zf(async t=>await OI(t,e))}async function LI(e,t){let r=Jf(e,t);return await zf(async n=>(await NI(n,e,r),r))}async function $G(e,t,r){return await zf(async n=>{let i=await OI(n,e),a=Jf(e,{...i,[JG(e,t)]:r});return await NI(n,e,a),a})}async function BG(e,t){return await zf(async r=>{let n=await OI(r,e),i=JG(e,t),{[i]:a,...o}=n,s=Jf(e,o);return await NI(r,e,s),s})}async function HG(e){return await LI(e,{})}function qG(e){return V7.find(t=>t===e)}function KG(e,t){return e!=="llm"||t.apiKey===void 0?t:{...t,apiKey:"****"}}function JG(e,t){let r=t.trim();if(r==="")throw new Error("Config key cannot be empty.");if(e==="llm")switch(r){case"api-key":return"apiKey";case"base-url":return"baseURL";default:return r}return r}function Jf(e,t){switch(e){case"llm":return Z7(t);case"concurrent":return eee(t);case"wikispine":return tee(t)}}async function zf(e){let t=await V(Se(),X7);try{return await e(t)}finally{await t.close()}}async function OI(e,t){let r=await e.queryOne("SELECT value_json FROM config_sections WHERE section = ?",[t],n=>String(n.value_json));return r===void 0?{}:Y7(t,r)}async function NI(e,t,r){await e.run(`
|
|
2827
|
+
INSERT INTO config_sections (section, value_json, updated_at)
|
|
2828
|
+
VALUES (?, ?, ?)
|
|
2829
|
+
ON CONFLICT(section) DO UPDATE SET
|
|
2830
|
+
value_json = excluded.value_json,
|
|
2831
|
+
updated_at = excluded.updated_at
|
|
2832
|
+
`,[t,JSON.stringify(r),Date.now()])}function Y7(e,t){let r;try{r=JSON.parse(t)}catch(n){throw new Error(`Invalid local config JSON for ${e}: ${ree(n)}`)}if(r===null||typeof r!="object"||Array.isArray(r))throw new Error(`Invalid local config section ${e}: expected object.`);return Jf(e,r)}function Z7(e){let t=new Set(["apiKey","baseURL","model","name","provider"]),r=new Set(["anthropic","google","openai","openai-compatible"]),n={};for(let[i,a]of Object.entries(e)){if(!t.has(i))throw new Error(`Unknown llm config key: ${i}`);if(typeof a!="string"||a.trim()==="")throw new Error(`llm.${i} must be a non-empty string.`);let o=a.trim();if(i==="provider"&&!r.has(o))throw new Error(`Unknown llm.provider: ${o}`);n[i]=o}return n}function eee(e){let t=new Set(["job","request"]),r={};for(let[n,i]of Object.entries(e)){if(!t.has(n))throw new Error(`Unknown concurrent config key: ${n}`);let a=typeof i=="number"?i:typeof i=="string"?Number(i):NaN;if(!Number.isInteger(a)||a<=0)throw new Error(`concurrent.${n} must be a positive integer.`);r[n]=a}return r}function tee(e){let t=new Set(["provider"]),r=new Set(["cli","fetch"]),n={};for(let[i,a]of Object.entries(e)){if(!t.has(i))throw new Error(`Unknown wikispine config key: ${i}`);if(typeof a!="string"||a.trim()==="")throw new Error(`wikispine.${i} must be a non-empty string.`);let o=a.trim();if(i==="provider"&&!r.has(o))throw new Error(`Unknown wikispine.provider: ${o}`);n[i]=o}return n}function ree(e){return e instanceof Error?e.message:String(e)}var V7,X7,ll=l(()=>{"use strict";O();O();V7=["concurrent","llm","wikispine"],X7=`
|
|
2833
|
+
CREATE TABLE IF NOT EXISTS config_sections (
|
|
2834
|
+
section TEXT PRIMARY KEY,
|
|
2835
|
+
value_json TEXT NOT NULL,
|
|
2836
|
+
updated_at INTEGER NOT NULL
|
|
2837
|
+
);
|
|
2838
|
+
`});import{z as Je}from"zod";async function Ei(e){let[t,r,n]=await Promise.all([En("llm"),En("concurrent"),En("wikispine")]),i=e?.llmJSON===void 0?void 0:ote(e.llmJSON),a=YU({apiKey:wl(i?.apiKey,ag(t.apiKey)),baseURL:wl(i?.baseURL,ag(t.baseURL)),model:wl(i?.model,ag(t.model)),name:wl(i?.name,ag(t.name)),provider:wl(i?.provider,ute(t.provider))}),o=XU(r.request),s=XU(r.job),c=lte(n);return{...s===void 0&&o===void 0?{}:{concurrent:{...s===void 0?{}:{job:s},...o===void 0?{}:{request:o}}},...a===void 0?{}:{llm:a},...c===void 0?{}:{wikispine:c}}}function ote(e){let t=mte(e);if(t===void 0)throw new Error(h("--llm must be a non-empty JSON object.",_.config));let r;try{r=JSON.parse(t)}catch(c){throw new Error(h(`Invalid --llm JSON: ${hte(c)}`,_.config))}let n=ate.safeParse(r);if(!n.success)throw new Error(h(`Invalid --llm config: ${n.error.issues.map(c=>`${c.path.join(".")||"<root>"}: ${c.message}`).join("; ")}`,_.config));let i=n.data.llm??n.data,a=i.provider??ste(i),o=dte(i),s=YU({apiKey:i.apiKey,baseURL:o,model:i.model,name:i.name,provider:a});if(s===void 0)throw new Error(h("--llm must contain at least one supported LLM field.",_.config));return s}function ste(e){return e.provider===void 0&&(e.baseURL!==void 0||e.baseUrl!==void 0||e.chatCompletionsUrl!==void 0)?"openai-compatible":e.provider}function cte(e){if(e.chatCompletionsUrl===void 0)return;let t="/chat/completions";if(!e.chatCompletionsUrl.endsWith(t))throw new Error(h("--llm chatCompletionsUrl must end with /chat/completions when baseURL is not provided.",_.config));return e.chatCompletionsUrl.slice(0,-t.length)}function dte(e){return e.baseURL!==void 0?e.baseURL:e.baseUrl!==void 0?e.baseUrl:cte(e)}function YU(e){if(!(e.apiKey===void 0&&e.baseURL===void 0&&e.model===void 0&&e.name===void 0&&e.provider===void 0))return{...e.apiKey===void 0?{}:{apiKey:e.apiKey},...e.baseURL===void 0?{}:{baseURL:e.baseURL},...e.model===void 0?{}:{model:e.model},...e.name===void 0?{}:{name:e.name},...e.provider===void 0?{}:{provider:e.provider}}}function ute(e){if(typeof e!="string")return;let t=tx.safeParse(e);return t.success?t.data:void 0}function lte(e){let t=pte(e.provider);if(t!==void 0)return{provider:t}}function pte(e){return e==="cli"||e==="fetch"?e:void 0}function ag(e){return typeof e=="string"?e:void 0}function XU(e){return typeof e=="number"&&Number.isInteger(e)&&e>0?e:void 0}function wl(e,t){return e??t}function mte(e){let t=e?.trim();return t===void 0||t===""?void 0:t}function hte(e){return e instanceof Error?e.message:String(e)}var ite,tx,ate,Ws=l(()=>{"use strict";T();ll();ite=["anthropic","google","openai","openai-compatible"],tx=Je.enum(ite),ate=Je.object({apiKey:Je.string().min(1).optional(),baseURL:Je.string().min(1).optional(),baseUrl:Je.string().min(1).optional(),chatCompletionsUrl:Je.string().min(1).optional(),llm:Je.object({apiKey:Je.string().min(1).optional(),baseURL:Je.string().min(1).optional(),baseUrl:Je.string().min(1).optional(),chatCompletionsUrl:Je.string().min(1).optional(),model:Je.string().min(1).optional(),name:Je.string().min(1).optional(),provider:tx.optional()}).optional(),model:Je.string().min(1).optional(),name:Je.string().min(1).optional(),provider:tx.optional()})});import{createAnthropic as fte}from"@ai-sdk/anthropic";import{createGoogleGenerativeAI as gte}from"@ai-sdk/google";import{createOpenAI as yte}from"@ai-sdk/openai";import{createOpenAICompatible as bte}from"@ai-sdk/openai-compatible";function Fs(e){let t=e.llm;if(t?.provider===void 0||t.model===void 0)throw new Error(h("Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model.",_.config));return{model:wte(t.provider,t.model,{apiKey:t.apiKey,baseURL:t.baseURL,name:t.name}),...e.concurrent?.request===void 0?{}:{concurrent:e.concurrent.request}}}function wte(e,t,r){switch(e){case"anthropic":return fte({...r.apiKey===void 0?{}:{apiKey:r.apiKey},...r.name===void 0?{}:{name:r.name}})(t);case"google":return gte({...r.apiKey===void 0?{}:{apiKey:r.apiKey},...r.name===void 0?{}:{name:r.name}})(t);case"openai":{if(r.baseURL!==void 0)throw new Error(h("openai does not accept llm.baseURL or baseURL in --llm JSON. Use openai-compatible for third-party OpenAI-style APIs.",_.config));return yte({...r.apiKey===void 0?{}:{apiKey:r.apiKey},...r.name===void 0?{}:{name:r.name}})(t)}case"openai-compatible":{if(r.baseURL===void 0)throw new Error(h("openai-compatible requires llm.baseURL or baseURL in --llm JSON.",_.config));return bte({baseURL:r.baseURL,name:r.name??Ste(r.baseURL),...r.apiKey===void 0?{}:{apiKey:r.apiKey}})(t)}}}function Ste(e){try{return new URL(e).hostname}catch{return"openai-compatible"}}var Sl=l(()=>{"use strict";T()});function og(e,t,r,n,i){let a=kte(e),o=Math.max(r,Math.ceil(t/a.wordsPerCall)),s=Math.max(1,e==="reading-graph"?1:n.request),c=Math.ceil(o/s),d=t*a.secondsPerWord;return{model:i,timeSeconds:{max:Math.ceil(c*a.maxSecondsPerCall+d),min:Math.ceil(c*a.minSecondsPerCall+d)},tokens:{cacheableInput:Math.ceil(t*a.cacheableInputTokenPerWord),input:Math.ceil(t*a.inputTokenPerWord),output:Math.ceil(t*a.outputTokenPerWord)}}}function sg(e){if(!e.hasGenerationWork)return[];let t=[],r=e.concurrent.request<4?6:e.concurrent.request<8?8:void 0;return r!==void 0&&t.push({command:`wg wikg://local/config/concurrent put request ${r}`,current:e.concurrent.request,kind:"request",message:"LLM request concurrency can often be higher. Use at least 4; 6-8 is usually faster when the provider allows it.",recommended:r}),e.concurrent.job<4&&e.chapters>1&&t.push({command:"wg wikg://local/config/concurrent put job 4",current:e.concurrent.job,kind:"job",message:"Multiple chapters need generation and job concurrency is below 4. Raising it lets chapter jobs run in parallel.",recommended:4}),t}function cg(e){return e?.model===void 0?"not configured":e.provider===void 0?e.model:`${e.provider}/${e.model}`}function Gs(e){if(e<60)return`${e}s`;let t=Math.round(e/60);return t<60?`${t}m`:`${Math.round(t/60)}h`}function kte(e){switch(e){case"knowledge-graph":return{cacheableInputTokenPerWord:60,inputTokenPerWord:80,maxSecondsPerCall:35,minSecondsPerCall:15,outputTokenPerWord:25,secondsPerWord:.02,wordsPerCall:35};case"reading-graph":return{cacheableInputTokenPerWord:20,inputTokenPerWord:25,maxSecondsPerCall:45,minSecondsPerCall:15,outputTokenPerWord:4,secondsPerWord:.01,wordsPerCall:160};case"reading-summary":return{cacheableInputTokenPerWord:45,inputTokenPerWord:55,maxSecondsPerCall:25,minSecondsPerCall:10,outputTokenPerWord:4,secondsPerWord:.01,wordsPerCall:110}}}var ZU=l(()=>{"use strict"});import{clearLine as Ete,cursorTo as Ite,moveCursor as xte}from"readline";function r0(e){return e.enabled?new ix(e.stream??process.stderr):{async stop(){}}}function rx(){}function vte(e){return e.words!==void 0}function e0(e){return e.padEnd(8)}function Cte(e,t){if(t===void 0)return`#${e}`;let r=Rte(t);return r===""?`#${e}`:`#${e} ${r}`}function Tte(){return" ".repeat(7)}function t0(e,t){return`${Ii(e)} / ${Ii(t)} words`}function Rte(e){let t="";for(let r=0;r<e.length;r+=1){let n=e[r];if(n==="\x1B"){let i=e[r+1];if(i==="["){r=Pte(e,r+2);continue}if(i==="]"){r=_te(e,r+2);continue}continue}n!==void 0&&new RegExp("\\p{Cc}","u").test(n)||(t+=n??"")}return t.trim()}function Pte(e,t){for(let r=t;r<e.length;r+=1){let n=e[r];if(n===void 0)return e.length;if(n>="@"&&n<="~")return r}return e.length}function _te(e,t){for(let r=t;r<e.length;r+=1){let n=e[r],i=e[r+1];if(n===void 0)return e.length;if(n==="\x07")return r;if(n==="\x1B"&&i==="\\")return r+1}return e.length}function Ate(e,t){if(t!==void 0)return`${Ii(e)}/${Ii(t)} source units`}function nx(e,t){let n=t<=0?1:t,i=Math.max(0,Math.min(1,e/n)),a=Math.round(i*12);return`[${"#".repeat(a)}${".".repeat(12-a)}]`}function Ii(e){return new Intl.NumberFormat("en-US").format(e)}var ix,n0=l(()=>{"use strict";ix=class{#e;#t=new Map;#r;#n=Promise.resolve();#i=0;#a=!1;constructor(t){this.#e=t}onProgress=async t=>{if(this.#a)return;let r=this.#n.catch(rx).then(()=>{this.#o(t),this.#s()});this.#n=r,await r};async stop(){if(this.#a){await this.#n.catch(rx);return}this.#a=!0;let t=this.#n.catch(rx).then(()=>{this.#i!==0&&(this.#e.write(`
|
|
2839
|
+
`),this.#i=0)});this.#n=t,await t}#o(t){switch(t.type){case"serials-discovered":if(!t.available)return;for(let r of t.serials)this.#t.set(r.id,{completedFragments:0,completedWords:0,...r.title===void 0?{}:{title:r.title},words:r.words,...r.fragments===void 0?{}:{fragments:r.fragments}});return;case"serial-progress":{let r=this.#t.get(t.id)??{completedFragments:0,completedWords:0};r.completedFragments=t.completedFragments,r.completedWords=t.completedWords,this.#t.set(t.id,r);return}case"digest-progress":this.#r={completedWords:t.completedWords,totalWords:t.totalWords};return}}#s(){let t=this.#c();if(t.length===0)return;this.#i>1&&xte(this.#e,0,-(this.#i-1));let r=Math.max(t.length,this.#i);for(let n=0;n<r;n+=1){Ite(this.#e,0),Ete(this.#e,0);let i=t[n];i!==void 0&&this.#e.write(i),n<r-1&&this.#e.write(`
|
|
2840
|
+
`)}this.#i=r}#c(){let t=[],n=[...this.#t.entries()].sort(([c],[d])=>c-d).filter(c=>vte(c[1])),i=n.reduce((c,[,d])=>c+d.words,0),a=n.reduce((c,[,d])=>c+d.completedWords,0),o=n.filter(([,c])=>c.completedWords>0&&c.completedWords<c.words);n.length>0&&t.push(`${e0("Serial")}${nx(a,i)} ${Ii(a)} / ${Ii(i)} words`),this.#r!==void 0&&t.push(`${e0("Digest")}${nx(this.#r.completedWords,this.#r.totalWords)} ${Ii(this.#r.completedWords)} / ${Ii(this.#r.totalWords)} words`),o.length>0&&t.push("-".repeat(24));let s=o.reduce((c,[,d])=>Math.max(c,t0(d.completedWords,d.words).length),0);for(let[c,d]of o){let u=t0(d.completedWords,d.words),p=Ate(d.completedFragments,d.fragments);t.push(Cte(c,d.title)),t.push(`${Tte()}${nx(d.completedWords,d.words)} ${u.padEnd(s)}${p===void 0?"":` (${p})`}`)}return t}}});function Lte(e){let t=e.counters??[],r=t.length===0?"":` ${t.map(Ote).join(" | ")}`,n=Nte(e.metricGroups);return`${e.phase}${r}${n===""?"":` ${n}`}`}function Ote(e){return e.name===e.unit?`${e.name} ${e.done}/${e.total}`:`${e.name} ${e.done}/${e.total} ${e.unit}`}function Nte(e){return e===void 0?"":e.flatMap(r=>{let n=r.metrics.map(i=>`${i.name}: ${i.value}`);return n.length===0?[]:[`[${r.name} ${n.join(" / ")}]`]}).join(" ")}function jte(e){let t=e.counters??[];return t.length>0&&t.every(r=>r.total>0&&r.done>=r.total)}var xn,i0=l(()=>{"use strict";T();T();yi();xn=class{#e;#t;#r;#n;#i;#a=!1;constructor(t){this.#e=t.jsonl,this.#t=t.throttleMs,this.#r=t.tty??pG()===!0}async write(t){if(!(t.kind==="status"&&!this.#l(t))){if(this.#e){await this.#s(Ls(t.json));return}if(t.kind==="status"){await this.#c(Lte(t));return}await this.#o(t.text)}}async#o(t){this.#r&&this.#a&&(await S(`
|
|
2841
|
+
`),this.#a=!1),await this.#s(`${t}
|
|
2842
|
+
`)}async#s(t){await S(t)}async#c(t){if(this.#r){await S(`\r\x1B[2K${t}`),this.#a=!0;return}await this.#s(`${t}
|
|
2843
|
+
`)}#l(t){let r=t.phase,n=jte(t),i=Date.now(),a=this.#n===void 0?Number.POSITIVE_INFINITY:i-this.#n;return r!==this.#i||n||this.#n===void 0||a>=this.#t?(this.#n=i,this.#i=r,!0):!1}}});function a0(e,t){let r=Fs(e);return new Dr({dataDirPath:Mr(),sampling:fu({...r.temperature===void 0?{}:{temperature:r.temperature},...r.topP===void 0?{}:{topP:r.topP}}),...r,...t?.cacheDirPath===void 0?{}:{cacheDirPath:t.cacheDirPath},...t?.logDirPath===void 0?{}:{logDirPath:t.logDirPath},...t?.onStreamProgress===void 0?{}:{onStreamProgress:t.onStreamProgress},...t?.onTokenUsage===void 0?{}:{onTokenUsage:t.onTokenUsage}})}async function dg(e){let t=await Ei({...e.llmJSON===void 0?{}:{llmJSON:e.llmJSON}});if(t.llm?.provider===void 0||t.llm.model===void 0)throw new Error(h("Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model.",_.config));return t}var o0=l(()=>{"use strict";O();O();O();O();Ws();T();Sl()});var vn=l(()=>{"use strict";Ws();Sl();ll();ZU();n0();i0();o0()});var c0={};Qe(c0,{runConvertCommand:()=>El});import{rm as Dte}from"fs/promises";import{resolve as Mte}from"path";async function El(e){let t=Ute(e),r=$te(e),n=e.targetStage??"summarized";if(e.verbose&&r.standardStream==="stdout")throw new Error(h("Cannot use --verbose when writing digest output to stdout. Use --output <path> or disable --verbose.",_.runtime));if(e.targetStage!==void 0&&r.format!=="wikg")throw new Error(h("--stage is only supported when output format is wikg.",_.format));let i=t.format;if(e.targetStage!==void 0&&i==="wikg")throw new Error(h("--stage is only supported when creating .wikg from source input.",_.format));let a=i!=="wikg",o=a&&n!=="planned"&&n!=="sourced",s=await Wte(e,a),c=await Fte(e,o),d=new gn(Gte(e,c,o)),u=r0({enabled:a&&r.standardStream!=="stdout"&&process.stderr.isTTY===!0&&!e.verbose});if(i==="wikg"){if(t.path===void 0)throw new Error("Internal error: wikg input requires a file path.");try{await d.openSession(t.path,async m=>{await kl(m,r)});return}finally{await u.stop()}}let p=e.prompt??c.prompt;try{if(t.path===void 0){if(process.stdin.isTTY)throw new Error(h("Missing --input. Refusing to read from interactive stdin. Use --input <path> or pipe text into stdin.",_.runtime));await d.digestTextStreamSession({...s===void 0?{}:{documentDirPath:s},...u.onProgress===void 0?{}:{onProgress:u.onProgress},sourceFormat:t.format,stream:bi(),...p===void 0?{}:{extractionPrompt:p},targetStage:n},async m=>{await kl(m,r)});return}switch(i){case"epub":await d.digestEpubSession({...s===void 0?{}:{documentDirPath:s},...u.onProgress===void 0?{}:{onProgress:u.onProgress},path:t.path,...p===void 0?{}:{extractionPrompt:p},targetStage:n},async m=>{await kl(m,r)});return;case"markdown":await d.digestMarkdownSession({...s===void 0?{}:{documentDirPath:s},...u.onProgress===void 0?{}:{onProgress:u.onProgress},path:t.path,...p===void 0?{}:{extractionPrompt:p},targetStage:n},async m=>{await kl(m,r)});return;case"txt":await d.digestTxtSession({...s===void 0?{}:{documentDirPath:s},...u.onProgress===void 0?{}:{onProgress:u.onProgress},path:t.path,...p===void 0?{}:{extractionPrompt:p},targetStage:n},async m=>{await kl(m,r)});return}}finally{await u.stop()}}async function Wte(e,t){let r=ax(e.digestDirPath);if(r===void 0||!t)return;let n=Mte(r);return await Dte(n,{force:!0,recursive:!0}),n}async function Fte(e,t){let r=await Ei({...e.llmJSON===void 0?{}:{llmJSON:e.llmJSON}});if(!t)return r;if(r.llm?.provider===void 0||r.llm.model===void 0)throw new Error(h("Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model.",_.config));return r}function Gte(e,t,r){let n=r?Fs(t):void 0;return{...e.verbose?{verbose:!0}:{},...n===void 0?{}:{llm:n}}}async function kl(e,t){if(t.path!==void 0){await s0(e,t.path,t.format);return}if(t.standardStream!=="stdout")throw new Error("Internal error: missing output target.");let r=await gG("wikigraph-cli-output-",Bte(t.format));try{await s0(e,r.filePath,t.format),await hG(r.filePath)}finally{await yG(r.directoryPath)}}async function s0(e,t,r){switch(r){case"epub":await e.exportEpub(t);return;case"markdown":case"txt":await e.exportText(t);return;case"wikg":await e.saveAs(t);return}}function Ute(e){let t=ax(e.inputPath),r=t===void 0?void 0:il(t),n=e.inputFormat??r;if(n===void 0)throw new Error(h(t===void 0?"Cannot infer input format from stdin. Set --input-format.":`Cannot infer input format from ${t}. Set --input-format.`,_.format));if(t===void 0&&!II(n))throw new Error(h(`stdin only supports txt or markdown, but got ${n}.`,_.format));return t===void 0?{format:n,standardStream:"stdin"}:{format:n,path:t}}function $te(e){let t=ax(e.outputPath),r=t===void 0?void 0:il(t),n=e.outputFormat??r;if(n===void 0)throw new Error(h(t===void 0?"Cannot infer output format for stdout. Set --output-format.":`Cannot infer output format from ${t}. Set --output-format.`,_.format));if(t===void 0&&!II(n))throw new Error(h(`stdout only supports txt or markdown, but got ${n}.`,_.format));return t===void 0?{format:n,standardStream:"stdout"}:{format:n,path:t}}function ax(e){if(e!==void 0)return e==="-"?void 0:e}function Bte(e){switch(e){case"epub":return".epub";case"markdown":return".md";case"wikg":return".wikg";case"txt":return".txt"}}var ug=l(()=>{"use strict";O();Ws();T();T();Sl();T();vn()});import{rename as Hte,rm as d0,stat as qte}from"fs/promises";import{basename as Kte,dirname as Jte,join as zte}from"path";async function u0(e){let t=await Xte(e);try{await Qte(e,t),await Yte(e,t)}finally{t!==e.archivePath&&await d0(t,{force:!0,recursive:!0})}await rre(e)}async function Qte(e,t){if(e.importPath===void 0){await Vte(t);return}await El({help:!1,inputPath:e.importPath,outputFormat:"wikg",outputPath:t,targetStage:"sourced",verbose:!1})}async function Vte(e){let t=await Ye("archive-write"),r=await ye.open(t);try{try{await r.openSession(async n=>{await n.writeToc({items:[],version:Xe})})}finally{await r.release()}await zr(t,e)}finally{await d0(t,{force:!0,recursive:!0})}}async function Xte(e){if(e.replace!==!0&&await Zte(e.archivePath))throw new Error(tre(e.archivePath));return e.replace!==!0?e.archivePath:zte(Jte(e.archivePath),`.${Kte(e.archivePath)}.${process.pid}.${Date.now()}.tmp.wikg`)}async function Yte(e,t){t!==e.archivePath&&await Hte(t,e.archivePath)}async function Zte(e){try{return await qte(e),!0}catch(t){if(ere(t))return!1;throw t}}function ere(e){return e instanceof Error&&"code"in e&&e.code==="ENOENT"}function tre(e){let t=Ft(e);return[`Archive already exists: ${e}`,`Use \`${fe([t,"inspect"])}\` to view it, or rerun with \`--replace\` to overwrite it.`].join(`
|
|
2844
|
+
`)}async function rre(e){if(e.json===!0){await S(L({uri:Le(e.archivePath)}));return}await S(`<archive>
|
|
2845
|
+
`)}var l0=l(()=>{"use strict";O();ug();T()});async function m0(e,t){let r=await nre(e,t);if(t.json===!0){await S(L(r));return}await S(are(r))}async function nre(e,t){let r=ire(t.archivePath),n=t.chapterId===void 0?r:`${r}/chapter/${t.chapterId}`,[i,a,o,s,c]=await Promise.all([ore(e,t.chapterId),sre(e,t.chapterId),qe(e),an(e),Ei()]),d={job:c.concurrent?.job??3,request:c.concurrent?.request??6},u=cg(c.llm),p=i.filter(C=>C.stage!=="planned"),m=xi(p),f=p.filter(C=>C.readingGraphReady),w=p.filter(C=>C.knowledgeGraphReady),b=p.filter(C=>C.summaryReady),E=dre({archiveUri:r,concurrent:d,contentChapters:p,ftsCurrent:o,planningModel:u,scopeUri:n,summaryCovered:b,knowledgeGraphCovered:w,readingGraphCovered:f}),N=sg({chapters:Math.max(0,...E.map(C=>C.missingChapters??0)),concurrent:d,hasGenerationWork:E.some(C=>C.planning!==void 0)});return{uri:n,scope:t.chapterId===void 0?{type:"archive"}:{chapterId:t.chapterId,type:"chapter"},content:{chapters:{content:p.length,planned:i.length-p.length,total:i.length},sourceWords:m,summaryWords:a},index:{current:o,...o?{}:{fixCommand:fe([`${r}/index`,"enable"]),impact:"--query, related --query, and evidence --query are unavailable.",resource:"local CPU/disk time only; no LLM tokens."},querySupport:o,status:o?"current":"missing-or-outdated",storage:s.ftsEmbedded?"archive":"cache"},coverage:{knowledgeGraph:ox(w,p),readingGraph:ox(f,p),summary:ox(b,p)},retrievalGuidance:cre({ftsCurrent:o,knowledgeGraphCovered:w,readingGraphCovered:f,contentChapters:p,sourceWords:m}),improvements:E,performanceHints:N,help:{readiness:"wg help readiness"}}}function ire(e){return e.startsWith("wikg://lib/")?e:Ft(e)}function are(e){return["Archive Inspect",`URI: ${e.uri}`,`Scope: ${e.scope.type==="archive"?"archive":`chapter ${e.scope.chapterId}`}`,"","Content",`Chapters: ${e.content.chapters.content} content / ${e.content.chapters.total} total`,`Planned chapters: ${e.content.chapters.planned}`,`Source words: ${e.content.sourceWords}`,`Summary words: ${e.content.summaryWords}`,"","FTS Index",`Status: ${e.index.current?"current":"missing or outdated"}`,`Storage: ${e.index.storage==="archive"?"embedded in archive":"local cache"}`,`Query support: ${e.index.querySupport?"available":"unavailable"}`,...e.index.current?[]:[`Impact: ${e.index.impact}`,`Fix: ${e.index.fixCommand}`,`Resource: ${e.index.resource}`],"","Coverage",sx("Reading Graph",e.coverage.readingGraph),sx("Knowledge Graph",e.coverage.knowledgeGraph),sx("Summary",e.coverage.summary),"","Retrieval Guidance",...e.retrievalGuidance,"","Improvements",...e.improvements.length===0?["No immediate improvements recommended."]:[...e.improvements.flatMap(lre),"",...pre(e.performanceHints),...e.performanceHints.length===0?[]:[""],`Readiness details: ${e.help.readiness}`]].join(`
|
|
2846
|
+
`)+`
|
|
2847
|
+
`}async function ore(e,t){let r=t===void 0?await z(e):(await z(e)).filter(n=>n.chapterId===t);if(t!==void 0&&r.length===0)throw new Error(`Chapter ${t} does not exist.`);return await Promise.all(r.map(async n=>{let i=await e.serials.getById(n.chapterId);return{...n,knowledgeGraphReady:i?.knowledgeGraphReady===!0,readingGraphReady:i?.topologyReady===!0,summaryReady:n.stage==="summarized"}}))}async function sre(e,t){return await e.readDatabase(async r=>await r.queryOne(`
|
|
2848
|
+
SELECT COALESCE(SUM(words_count), 0) AS words
|
|
2849
|
+
FROM text_sentence_records
|
|
2850
|
+
WHERE kind = 2
|
|
2851
|
+
${t===void 0?"":"AND chapter_id = ?"}
|
|
2852
|
+
`,t===void 0?void 0:[t],n=>Number(n.words))??0)}function ox(e,t){let r=xi(e),n=xi(t);return{coveredChapters:e.length,coveredWords:r,percent:h0(r,n),totalChapters:t.length,totalWords:n}}function sx(e,t){return t.totalChapters===0&&t.totalWords===0?`${e}: n/a, no source content`:`${e}: ${t.coveredChapters}/${t.totalChapters} chapters, ${t.coveredWords}/${t.totalWords} words, ${t.percent}`}function cre(e){if(e.sourceWords===0||e.contentChapters.length===0)return["Source content: empty.","Add source text before using query coverage or graph-based retrieval."];let t=[`Query support: ${e.ftsCurrent?"available":"unavailable until the searchable index is enabled"}.`];return t.push(p0("Reading Graph object retrieval",e.readingGraphCovered,e.contentChapters)),t.push(p0("Entity/triple retrieval",e.knowledgeGraphCovered,e.contentChapters)),t}function p0(e,t,r){let n=xi(t),i=xi(r),a=i===0?0:n/i,o=h0(n,i);return a>=1?`${e}: covers all source content; it can represent the full scope for object retrieval.`:a>=.9?`${e}: covers ${o}; use it as the main path, but source --query is needed for uncovered content.`:a>=.5?`${e}: covers ${o}; use it as leads, not as a full-scope substitute for source --query.`:`${e}: covers ${o}; build missing graph coverage before relying on object retrieval.`}function dre(e){let t=[];return e.ftsCurrent||t.push({command:fe([`${e.archiveUri}/index`,"enable"]),recommendation:"Enable the searchable FTS index so --query filtering is available for scopes, related results, and evidence.",title:"Enable searchable index"}),e.contentChapters.length===0?(t.push({command:fe([`${e.archiveUri}/chapter`,"add","--input","source.txt"]),recommendation:"No source content is available yet; add or import source text before graph or summary generation.",title:"Add source content"}),t):(t.push(...cx({concurrent:e.concurrent,covered:e.readingGraphCovered,planningModel:e.planningModel,scopeUri:e.scopeUri,task:"reading-graph",title:"Complete Reading Graph coverage",total:e.contentChapters})),t.push(...cx({concurrent:e.concurrent,covered:e.knowledgeGraphCovered,planningModel:e.planningModel,scopeUri:e.scopeUri,task:"knowledge-graph",title:"Complete Knowledge Graph coverage",total:e.contentChapters})),t.push(...cx({concurrent:e.concurrent,covered:e.summaryCovered,planningModel:e.planningModel,scopeUri:e.scopeUri,task:"reading-summary",title:"Complete Summary coverage",total:e.contentChapters})),t)}function cx(e){let t=new Set(e.covered.map(i=>i.chapterId)),r=e.total.filter(i=>!t.has(i.chapterId));if(r.length===0)return[];let n=xi(r);return[{command:fe(["wikg://local/job","add","--input",e.scopeUri,"--task",e.task,"--accept-cost"]),missingChapters:r.length,missingWords:n,planning:og(e.task,n,r.length,e.concurrent,e.planningModel),recommendation:ure(e.covered,e.total,n),title:e.title}]}function ure(e,t,r){let n=xi(t),i=xi(e);return n===0?"No source content is available.":t.length===1||i/n>=.9||r<=1e3?"Queue the full scope to finish the remaining gap.":"Queue selected chapters first if only part of the scope matters."}function lre(e){return[`${e.title}:`,...e.missingChapters===void 0?[]:[` Missing: ${e.missingChapters} chapters / ${e.missingWords} words`],` Recommendation: ${e.recommendation}`,...e.planning===void 0?[` Command: ${e.command}`]:[" If completing this scope:",` Command: ${e.command}`,` Model: ${e.planning.model}`,` Tokens: ${e.planning.tokens.input} input / ${e.planning.tokens.cacheableInput} cacheable input / ${e.planning.tokens.output} output`,` Wait: ${Gs(e.planning.timeSeconds.min)}-${Gs(e.planning.timeSeconds.max)}`]]}function pre(e){return e.length===0?[]:["Performance hints:",...e.flatMap(t=>[` ${t.message}`,` Current ${t.kind}: ${t.current}; suggested: ${t.recommended}.`,` Command: ${t.command}`])]}function xi(e){return e.reduce((t,r)=>t+r.words,0)}function h0(e,t){if(t===0)return"n/a";let r=e/t*100;return`${Number.isInteger(r)?r.toFixed(0):r.toFixed(1)}%`}var f0=l(()=>{"use strict";O();Ws();vn();T()});async function vi(e,t){if(t==null)return null;let r;if(e.continuationKind==="evidence"){if(e.targetUri===void 0)throw new Error("Evidence continuation cursors require a target URI.");r={archiveKey:e.archiveKey,archivePath:e.archivePath,cursor:t,format:e.format,indexScope:e.indexScope,kind:"evidence",order:e.order??"doc-asc",...e.query===void 0?{}:{query:e.query},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},targetUri:e.targetUri}}else if(e.continuationKind==="related"){if(e.targetUri===void 0)throw new Error("Related continuation cursors require a target URI.");r={archiveKey:e.archiveKey,archivePath:e.archivePath,cursor:t,...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},format:e.format,indexScope:e.indexScope,kind:"related",order:e.order??"doc-asc",...e.query===void 0?{}:{query:e.query},...e.role===void 0?{}:{role:e.role},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},targetUri:e.targetUri}}else e.continuationKind==="collection"?r={archiveKey:e.archiveKey,archivePath:e.archivePath,chapters:e.chapters??null,cursor:t,...e.backlinks===void 0?{}:{backlinks:e.backlinks},...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},format:e.format,ids:e.ids??null,indexScope:e.indexScope,kind:"collection",order:e.order??"doc-asc",...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},types:e.types}:r={archiveKey:e.archiveKey,archivePath:e.archivePath,cursor:t,...e.backlinks===void 0?{}:{backlinks:e.backlinks},...e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit},format:e.format,indexScope:e.indexScope,kind:"search",...e.chapters===void 0?{}:{chapters:e.chapters},...e.query===void 0?{}:{query:e.query},...e.sourceContext===void 0?{}:{sourceContext:e.sourceContext},...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},types:e.types};return await Hd(r)}var xl=l(()=>{"use strict";O()});function lg(e,t){return e===void 0?[...t]:[`score: ${e}`,...t]}var pg=l(()=>{"use strict"});function vl(e){return ux(e.uri,e.text)}function ux(e,t){return[`@@ ${e} @@`,mre(t)].join(`
|
|
2853
|
+
`)}function mre(e){let t=e.replace(/\r\n?/g,`
|
|
2854
|
+
`).split(`
|
|
2855
|
+
`);for(;t.length>0&&t[0]?.trim()==="";)t.shift();for(;t.length>0&&t.at(-1)?.trim()==="";)t.pop();let r=[],n=!1;for(let i of t){if(i.trim()===""){n||r.push(""),n=!0;continue}r.push(i),n=!1}return r.join(`
|
|
2856
|
+
`)}var mg=l(()=>{"use strict"});function g0(e){return e===null?"":`
|
|
2857
|
+
|
|
2858
|
+
Next page: ${"wg"} next ${e}`}function lx(e){return lg(e.score,ux(e.id,e.source).split(`
|
|
2859
|
+
`)).join(`
|
|
2860
|
+
`)}function y0(e){if(e.sources.length===0)return["[none]"];let t=e.sources.flatMap((n,i)=>[...i===0?[]:[""],`-- evidence ${i+1}/${e.shown}`,vl(n)]),r=e.total-e.shown;return t.push(...px(e,r)),t}function px(e,t){return e.nextCursor!==null?["",`${t} more evidence: ${"wg"} next ${e.nextCursor}`]:t>0?["",`${t} evidence more...`]:[]}var hg=l(()=>{"use strict";O();O();pg();mg()});function fg(e){return e===null?"":`
|
|
2861
|
+
|
|
2862
|
+
Next page: ${"wg"} next ${e}`}function mx(e){if(e.match==="all"&&e.terms.length>1)return`No matches. Try a more specific scope URI, for example: ${"wg"} <archive-uri>/chunk --query "${e.query}"${gg(e)}
|
|
2863
|
+
`;let t=["No matches.","Try fewer or broader keywords, or use --query on a scope URI such as `<archive-uri>/chapter`, `<archive-uri>/chunk`, `<archive-uri>/entity`, or `<archive-uri>/triple`."];return e.lensHint!==null&&t.push(`Lens hint: ${e.lensHint.message}`),`${t.join(`
|
|
2864
|
+
`)}
|
|
2865
|
+
`}function gg(e){return e.lensHint===null?"":`
|
|
2866
|
+
|
|
2867
|
+
Lens hint: ${e.lensHint.message}`}var b0=l(()=>{"use strict";O()});function hx(e){return e==="source"||e==="summary"}function yg(e){return/^wikg:\/\/chapter\/.+\/(source|summary)(?:#.*)?$/u.exec(e)?.[1]}function ge(e){let[t,r,n]=e.split(":");switch(t){case"chapter":return`wikg://chapter/${r??""}`;case"chapter-title":return`wikg://chapter/${r??""}/title`;case"fragment":return`wikg://chapter/${r??""}/source/${n??"0"}`;case"meta":return"wikg://";case"node":return`wikg://chunk/${r??""}`;case"summary":return`wikg://chapter/${r??""}/summary`;default:return e}}var bg=l(()=>{"use strict"});var fx,gx=l(()=>{"use strict";fx=["uri","title","label","labels","state","value","authors","publisher","description"]});function Pr(e){return typeof e!="object"||e===null||Array.isArray(e)?String(e):Object.entries(e).filter(([,t])=>t!=null).sort(([t],[r])=>fre(t,r)).map(([t,r])=>`${t}: ${gre(r)}`).join(`
|
|
2868
|
+
`)}function S0(e){if(typeof e!="object"||e===null||Array.isArray(e))return String(e);let t=e;return t.value!==void 0?`${t.uri??"state"} ${t.value}`:t.state!==void 0?[t.uri,hre(t.state)].filter(r=>r!==void 0&&r!=="").join(`
|
|
2869
|
+
`):Pr(e)}function hre(e){return k0(e).map(([t,r])=>`${t}: ${r}`).join(`
|
|
2870
|
+
`)}function yx(e){return k0(e).map(([t,r])=>`${t}:${r}`).join(" ")}function k0(e){return[["source",e.source],["reading-graph",e["reading-graph"]],["reading-summary",e["reading-summary"]],["knowledge-graph",e["knowledge-graph"]]].filter(r=>r[1]!==void 0)}function fre(e,t){return w0(e)-w0(t)}function w0(e){let t=fx.indexOf(e);return t<0?fx.length:t}function gre(e){return Array.isArray(e)?e.map(t=>String(t)).join(", "):typeof e=="object"&&e!==null?L(e).trimEnd():String(e)}var wg=l(()=>{"use strict";T();gx()});function rr(e){let t=lg(e.score,E0(e));if(e.evidence!==void 0&&e.evidence.sources.length>0){t.push("",...e.evidence.sources.flatMap((n,i)=>[...i===0?[]:[""],`-- evidence ${i+1}/${e.evidence?.shown??e.evidence?.sources.length}`,vl(n)]));let r=e.evidence.total-e.evidence.shown;t.push(...px(e.evidence,r))}return e.backlinks!==void 0&&t.push("",...wre(e.backlinks)),t.join(`
|
|
2871
|
+
`)}function Sg(e,t){let r=e.find(i=>bre(i.uri));if(r===void 0)return"";let n=r.libraryArchiveUri??t.archivePath;return`
|
|
2872
|
+
|
|
2873
|
+
Open short URIs with the archive locator, for example:
|
|
2874
|
+
${fe([yre(n,r.uri)])}`}function yre(e,t){return e.startsWith("wikg://lib/")?`${e.replace(/\/+$/u,"")}/${t.slice(7)}`:Ft(e,t)}function bre(e){return e.startsWith("wikg://")&&B(e).archivePath===void 0}function wre(e){let t=[...e.chunks.objects,...e.entities.objects,...e.triples.objects].flatMap((r,n)=>[...n===0?[]:[""],rr(r)]);return t.length===0?[]:["Backlinks:","",...t]}function E0(e){let t=Sre(e);return yg(e.uri)!==void 0&&e.text!==void 0?[...vl({text:e.text,uri:e.uri}).split(`
|
|
2875
|
+
`),...t]:e.predicate!==void 0?[e.uri,kre(e),...t]:bx(e)?[[e.uri,e.title,yx(e.state)].filter(r=>r!==void 0&&r!=="").join(" "),...t]:[e.uri,e.title,e.label,e.state===void 0?void 0:yx(e.state),e.evidence===void 0?e.text:void 0,...t].filter(r=>r!==void 0&&r!=="")}function Sre(e){return(e.sources!==void 0&&e.sources.length>0?e.sources:e.archiveId===void 0&&e.libraryArchiveUri===void 0?[]:[{archiveId:e.archiveId,libraryArchiveUri:e.libraryArchiveUri}]).map(r=>[r.archiveId===void 0?void 0:`archiveId:${r.archiveId}`,r.libraryArchiveUri].filter(n=>n!==void 0).join(" "))}function Us(e){return e.every(bx)?`
|
|
2876
|
+
`:`
|
|
2877
|
+
|
|
2878
|
+
`}function bx(e){return e.state!==void 0&&e.uri.startsWith("wikg://chapter/")&&!e.uri.includes("/state")}function kre(e){let t=Ere(e.uri);return t===void 0?`${e.subjectLabel??"[subject]"} ${e.predicate??"[predicate]"} ${e.objectLabel??"[object]"}`:`${e.subjectLabel??t.subjectQid}(${t.subjectQid}) ${e.predicate??t.predicate} ${e.objectLabel??t.objectQid}(${t.objectQid})`}function Ere(e){let t=/^wikg:\/\/triple\/(Q[1-9][0-9]*)\/([^/]+)\/(Q[1-9][0-9]*)\/?$/u.exec(e);if(!(t?.[1]===void 0||t[2]===void 0||t[3]===void 0))return{objectQid:t[3],predicate:decodeURIComponent(t[2]),subjectQid:t[1]}}function I0(e){return bx(e)?E0(e).join(`
|
|
2879
|
+
`):Pr(e)}var x0=l(()=>{"use strict";O();T();bg();hg();pg();wg();mg()});async function $s(e,t){let r=xg(e);return e.type==="triple"?{...r,...t.evidenceLimit===void 0||e.evidence===void 0?{}:{evidence:await Ci(e.evidence,{...t,continuationKind:"evidence",targetUri:ge(e.id)})},objectLabel:e.objectLabel,predicate:e.predicate,...e.score===void 0?{}:{score:e.score},subjectLabel:e.subjectLabel,uri:ge(e.id)}:{...r,...t.evidenceLimit===void 0||e.evidence===void 0?{}:{evidence:await Ci(e.evidence,{...t,continuationKind:"evidence",targetUri:ge(e.id)})},label:e.label,...e.score===void 0?{}:{score:e.score},...hx(e.type)?{text:e.summary}:{type:e.type},uri:ge(e.id)}}function nr(e,t,r){return{limit:r,nextCursor:t,objects:e}}async function Eg(e,t){let r=ge(e.id),n=xg(e);if(e.type==="chapter")return{...n,...e.state===void 0?{}:{state:e.state},title:e.title,uri:r};if(e.type==="chapter-title")return{...n,title:e.title,type:"chapter-title",uri:r};if(e.type==="meta")return{...n,title:e.title,uri:r};if(e.type==="triple"){let i=e.triple;return{...n,...e.backlinks===void 0?{}:{backlinks:await kg(e.backlinks,t)},...t.evidenceLimit===void 0||e.evidence===void 0?{}:{evidence:await Ci(e.evidence,{...t,continuationKind:"evidence",targetUri:r})},objectLabel:i?.objectLabel??"",predicate:i?.predicate??"",subjectLabel:i?.subjectLabel??"",uri:r}}return{...n,...e.backlinks===void 0?{}:{backlinks:await kg(e.backlinks,t)},...t.evidenceLimit===void 0||e.evidence===void 0?{}:{evidence:await Ci(e.evidence,{...t,continuationKind:"evidence",targetUri:r})},label:e.title,...hx(e.type)?{text:e.snippet}:{type:e.type==="node"?"chunk":e.type},uri:r}}async function Ci(e,t){return{nextCursor:await vi({...t,continuationKind:"evidence"},e.nextCursor),shown:e.shown,sources:e.sources.map(Ig),total:e.total}}function Ig(e){return{...xg(e),...e.score===void 0?{}:{score:e.score},text:e.source,uri:e.id}}async function kg(e,t){return{chunks:await wx(e.chunks,t),entities:await wx(e.entities,t),triples:await wx(e.triples,t)}}async function wx(e,t){let r=await Promise.all(e.items.map(async n=>await Eg(n,{...t,backlinks:!1})));return nr(r,e.nextCursor,e.limit)}async function kt(e,t){let r=xg(e);switch(e.type){case"entity-wikipage":return{...r,en:e.en,uri:e.id,zh:e.zh};case"entity":return{...r,labels:e.labels.slice(0,7),qid:e.qid,...t.evidenceLimit===void 0?{}:{evidence:await Ci(e.evidence,{...t,continuationKind:"evidence",targetUri:e.id})},uri:e.id};case"triple":return{...r,label:e.label,...t.evidenceLimit===void 0?{}:{evidence:await Ci(e.evidence,{...t,continuationKind:"evidence",targetUri:e.id})},uri:e.id};case"chapter":return{...r,state:e.state,title:e.title,uri:ge(e.id)};case"chapter-title":return{...r,title:e.title,type:"chapter-title",uri:ge(e.id)};case"chapter-tree":{let{id:n,...i}=e;return{...r,...i,uri:ge(e.id)}}case"fragment":{let{backlinks:n,id:i,nextFragmentId:a,previousFragmentId:o,...s}=e;return yg(e.id)!==void 0?{...r,...n===void 0?{}:{backlinks:await kg(n,t)},text:e.fragment.text,uri:ge(e.id)}:{...r,...s,...n===void 0?{}:{backlinks:await kg(n,t)},...a===void 0?{}:{nextUri:ge(a)},...o===void 0?{}:{previousUri:ge(o)},uri:ge(e.id)}}case"meta":{let{id:n,type:i,...a}=e;return{...r,...a,uri:ge(e.id)}}case"state":return"state"in e?{...r,...e.state,uri:ge(e.id)}:{...r,uri:ge(e.id),value:e.value};case"node":{let{id:n,...i}=e;return{...r,...i,uri:ge(e.id)}}case"summary":{let{id:n,...i}=e;return{...r,...i,uri:ge(e.id)}}}}function xg(e){return{...e.archiveId===void 0?{}:{archiveId:e.archiveId},...e.libraryArchiveUri===void 0?{}:{libraryArchiveUri:e.libraryArchiveUri},...e.sources===void 0?{}:{sources:e.sources}}}async function Sx(e,t){let r=await Promise.all(e.related.map(async n=>await $s(n,t)));return{anchor:await kt(e.anchor,t),related:nr(r,null,r.length)}}var ja=l(()=>{"use strict";xl();bg()});async function Cl(e,t,r,n){return n.evidenceLimit===void 0?[`${e}`,t].join(`
|
|
2880
|
+
`):[`${e}`,t,"","Evidence:",...y0(await Ci(r,{...n,continuationKind:"evidence",targetUri:e}))].join(`
|
|
2881
|
+
`)}function C0(e,t,r){if(!Ire(t))return e;let n=Ft(r,t.replace(/\/$/u,""));return[e,"","Next:",` ${fe([n,"evidence"])}`,` ${fe([n,"related"])}`,` ${fe([`${n}/wikipage`])}`].join(`
|
|
2882
|
+
`)}function Ire(e){return/^wikg:\/\/(?:chapter\/[1-9][0-9]*\/)?entity\/Q[1-9][0-9]*\/?$/u.test(e)}function kx(e){return[e.id,"",...v0("zh",e.zh),"",...v0("en",e.en)].join(`
|
|
2883
|
+
`)}function v0(e,t){return t===null?[`${e}: [missing]`]:[`${t.title} ${t.url}`,...t.description===void 0?[]:[t.description]]}function Ex(e){return e.length===0?[" [none]"]:e.map(t=>` ${t.direction==="incoming"?"<-":"->"} ${ge(`node:${t.node.id}`)} ${t.node.label}`)}function T0(e){return e.length===0?[" [none]"]:e.map(t=>` ${t.id} ${t.title}`)}function Ix(e){return e.length===0?[" [none]"]:e.flatMap(t=>[` ${t.id}${t.truncated?" [excerpt]":""}`,...t.text.split(`
|
|
2884
|
+
`).map(r=>` ${r}`)])}function R0(e){return e===void 0?"[unknown]":[`chapter ${e.chapter}`,e.fragment===void 0?void 0:`fragment ${e.fragment}`].filter(t=>t!==void 0).join(", ")}async function P0(e,t){switch(e.type){case"chapter-title":case"chapter":return Pr(await kt(e,t));case"chapter-tree":return`${e.id} ${e.title}
|
|
2885
|
+
${L(e.tree).trimEnd()}`;case"fragment":return`${e.id}
|
|
2886
|
+
${e.fragment.text}`;case"meta":return Pr(await kt(e,t));case"state":return Pr(await kt(e,t));case"node":return[`${e.id} ${e.title}`,"","Generated Node Summary:",e.generatedNodeSummary,"","Source Fragments:",...Ix(e.sourceFragments)].join(`
|
|
2887
|
+
`);case"summary":return`${e.id} ${e.title}
|
|
2888
|
+
${e.content}`;case"entity":return await Cl(e.id,e.label,e.evidence,t);case"entity-wikipage":return kx(e);case"triple":return await Cl(e.id,e.label,e.evidence,t)}}function _0(e,t){return e.length<=t?e:`${e.slice(0,Math.max(0,t-20))}
|
|
2889
|
+
[truncated]`}var A0=l(()=>{"use strict";O();T();ja();bg();hg();wg()});var Bs=l(()=>{"use strict";hg();b0();pg();x0();A0();wg();mg()});async function Et(e){await S(e.map(t=>Ls(t)).join("")+(e.length===0,""))}var Hs=l(()=>{"use strict";T()});function qs(e){return{nextCursor:e,type:"page"}}var vg=l(()=>{"use strict"});async function Ks(e,t,r){let n=await vi(t,e.nextCursor),i=e.items.map(Ig);if(r==="json"){await S(L(nr(i,n,e.limit)));return}if(r==="jsonl"){await Et([...i,qs(n)]);return}if(e.items.length===0){await S(`No evidence.
|
|
2890
|
+
`);return}await S(`${e.items.map(lx).join(`
|
|
2891
|
+
|
|
2892
|
+
`)}${g0(n)}
|
|
2893
|
+
`)}async function xx(e,t,r){let n=[],i=t;for(;;){let a=await e(i);if(r==="jsonl"?await L0(a,r):n.push(a),a.nextCursor===null)break;i=a.nextCursor}r!=="jsonl"&&await L0(xre(n),r)}async function L0(e,t){let r=e.items.map(Ig);if(t==="json"){await S(L(nr(r,null,e.limit)));return}if(t==="jsonl"){await Et(r);return}if(e.items.length===0){await S(`No evidence.
|
|
2894
|
+
`);return}await S(`${e.items.map(lx).join(`
|
|
2895
|
+
|
|
2896
|
+
`)}
|
|
2897
|
+
`)}function xre(e){let[t]=e;if(t===void 0)throw new Error("Internal error: no evidence pages were loaded.");return{...t,items:e.flatMap(r=>r.items),limit:e.reduce((r,n)=>r+n.items.length,0),nextCursor:null}}var O0=l(()=>{"use strict";T();xl();Bs();Hs();ja();vg()});async function _r(e,t,r){let n=await Promise.all(e.items.map(async a=>await Eg(a,t))),i=await vi(t,e.nextCursor);if(r==="json"){await S(L(nr(n,i,e.limit)));return}if(r==="jsonl"){await Et([...n,qs(i)]);return}if(n.length===0){await S(mx(e));return}await S(`${n.map(a=>rr(a)).join(Us(n))}${Sg(n,t)}${fg(i)}${gg(e)}
|
|
2898
|
+
`)}async function Rl(e,t,r){let n=[],i;for(;;){let o=await e(i);if(r==="jsonl"?await Tl(o,t,r):n.push(o),o.nextCursor===null)break;i=o.nextCursor}if(r==="jsonl")return;let a=vre(n);await Tl(a,t,r)}async function Tl(e,t,r){let n=await Promise.all(e.items.map(async i=>await Eg(i,t)));if(r==="json"){await S(L(nr(n,null,e.limit)));return}if(r==="jsonl"){await Et(n);return}if(n.length===0){await S(mx(e));return}await S(`${n.map(i=>rr(i)).join(Us(n))}${gg(e)}
|
|
2899
|
+
`)}function vre(e){let[t]=e;if(t===void 0)throw new Error("Internal error: no result pages were loaded.");return{...t,items:e.flatMap(r=>r.items),limit:e.reduce((r,n)=>r+n.items.length,0),nextCursor:null}}var N0=l(()=>{"use strict";T();xl();Bs();Hs();ja();vg()});async function Js(e,t,r){let n=await Promise.all(e.items.map(async a=>await $s(a,t))),i=await vi(t,e.nextCursor);if(r==="json"){await S(L(nr(n,i,e.limit)));return}if(r==="jsonl"){await Et([...n,qs(i)]);return}if(n.length===0){await S(`No objects.
|
|
2900
|
+
`);return}await S(`${n.map(rr).join(Us(n))}${Sg(n,t)}${fg(i)}
|
|
2901
|
+
`)}async function vx(e,t,r,n){let i=[],a=t;for(;;){let o=await e(a);if(n==="jsonl"?await j0(o,r,n):i.push(o),o.nextCursor===null)break;a=o.nextCursor}n!=="jsonl"&&await j0(Cre(i),r,n)}async function j0(e,t,r){let n=await Promise.all(e.items.map(async i=>await $s(i,t)));if(r==="json"){await S(L(nr(n,null,e.limit)));return}if(r==="jsonl"){await Et(n);return}if(n.length===0){await S(`No objects.
|
|
2902
|
+
`);return}await S(`${n.map(rr).join(Us(n))}
|
|
2903
|
+
`)}function Cre(e){let[t]=e;if(t===void 0)throw new Error("Internal error: no related pages were loaded.");return{...t,items:e.flatMap(r=>r.items),limit:e.reduce((r,n)=>r+n.items.length,0),nextCursor:null}}var D0=l(()=>{"use strict";T();xl();Bs();Hs();ja();vg()});async function Cx(e,t,r){let n=t.evidenceDisabled===!0?t:{...t,evidenceLimit:t.evidenceLimit??3};if(r==="json"){await S(L(await Sx(e,n)));return}if(r==="jsonl"){await Et([await Sx(e,n)]);return}let i=[`Pack Budget: ${e.budget}`,"","# Anchor",await P0(e.anchor,n),"","# Related",(await Promise.all(e.related.map(async a=>rr(await $s(a,n))))).join(`
|
|
2904
|
+
|
|
2905
|
+
`)];await S(`${_0(i.join(`
|
|
2906
|
+
`),e.budget)}
|
|
2907
|
+
`)}var M0=l(()=>{"use strict";T();Bs();Hs();ja();gx()});async function Tx(e,t,r){if(r==="json"){await S(L(await kt(e,t)));return}if(r==="jsonl"){await Et([await kt(e,t)]);return}switch(e.type){case"chapter-title":await S(`${Pr(await kt(e,t))}
|
|
2908
|
+
`);return;case"chapter":await S(`${I0(await kt(e,t))}
|
|
2909
|
+
`);return;case"meta":await S(`${Pr(await kt(e,t))}
|
|
2910
|
+
`);return;case"state":await S(`${S0(await kt(e,t))}
|
|
2911
|
+
`);return;case"fragment":if(e.id.startsWith("wikg://")&&!e.id.includes("#")&&e.backlinks===void 0){await S(`${e.fragment.text}
|
|
2912
|
+
`);return}if(e.id.startsWith("wikg://chapter/")){await S(`${rr(await kt(e,t))}
|
|
2913
|
+
`);return}await S([`${e.id}`,`Words: ${e.fragment.wordsCount}`,`Previous: ${e.previousFragmentId??"[none]"}`,`Next: ${e.nextFragmentId??"[none]"}`,"",e.fragment.text,"","Related Nodes:",...T0(e.nodes)].join(`
|
|
2914
|
+
`)+`
|
|
2915
|
+
`);return;case"node":await S([`${e.id} ${e.title}`,`Chapter: ${e.position===void 0?"[unknown]":`chapter:${e.position.chapter}`}`,`Position: ${R0(e.position)}`,"","Generated Node Summary:",e.generatedNodeSummary,"","Source Fragments:",...Ix(e.sourceFragments),"","Outgoing Nodes:",...Ex(e.outgoing),"","Incoming Nodes:",...Ex(e.incoming)].join(`
|
|
2916
|
+
`)+`
|
|
2917
|
+
`);return;case"summary":await S(`${e.id} ${e.title}
|
|
2918
|
+
|
|
2919
|
+
${e.content}
|
|
2920
|
+
`);return;case"entity":await S(`${C0(await Cl(e.id,e.label,e.evidence,t),e.id,t.archivePath)}
|
|
2921
|
+
`);return;case"entity-wikipage":await S(`${kx(e)}
|
|
2922
|
+
`);return;case"triple":await S(`${await Cl(e.id,e.label,e.evidence,t)}
|
|
2923
|
+
`);return}}var W0=l(()=>{"use strict";T();Bs();Hs();ja()});var F0=l(()=>{"use strict";O0();N0();D0();M0();W0()});var Rx=l(()=>{"use strict";F0()});async function ve(e){if(!e.startsWith("wikg://"))return{archiveKey:e,archivePath:e,indexScope:{archiveKey:e,archivePath:e,kind:"archive-index"},locatedUri:Le(e)};let t=B(e),r=t.archivePath??e,n=r.startsWith("wikg://lib/")?de(r):void 0,i=n?.kind==="archive"?await iI(r):r;return{archiveKey:i,archivePath:i,indexScope:{archiveKey:i,archivePath:i,kind:"archive-index"},...n?.kind==="archive"?{libraryArchiveTarget:n,libraryDirtyTarget:{isDefault:n.isDefault,kind:"scope",...n.publicId===void 0?{}:{publicId:n.publicId}}}:{},locatedUri:Le(i,t.objectUri)}}async function G0(e){if(e.action==="create"||e.action==="export"||e.action==="inspect"||e.action==="next"||!e.archivePath.startsWith("wikg://lib/"))return e;let t=await ve(e.archivePath);return{...e,archivePath:t.locatedUri,...e.objectId===e.archivePath?{objectId:t.locatedUri}:{}}}function it(e){return de(e)?.kind==="scope"?e:Wi(e).archivePath}function U0(e){if(de(e)?.kind==="scope")return{kind:"library-index",libraryId:-1};let t=it(e);return{archiveKey:t,archivePath:t,kind:"archive-index"}}function Cn(e){let t=de(e);return t?.kind==="scope"&&t.objectUri!==void 0?t.objectUri:Wi(e).objectUri??"wikg://"}function $0(e){return e.objectId!==void 0&&Wi(e.objectId).objectUri===void 0}function B0(e){let t=/^wikg:\/\/chapter\/([1-9][0-9]*)(?:\/|$)/u.exec(e);return t?.[1]===void 0?void 0:Number(t[1])}var Ar=l(()=>{"use strict";O()});async function It(e,t){let r=await ve(e);await new G(r.archivePath).readDocument(t)}async function at(e,t,r={}){let n=await ve(e),i=await new G(n.archivePath).write(t,r);if(n.libraryDirtyTarget!==void 0){n.libraryArchiveTarget!==void 0&&await oI({target:n.libraryArchiveTarget});try{await Sn(n.libraryDirtyTarget)}catch(a){Rre(a)}}return i}function Rre(e){let t=e instanceof Error?e.message:String(e);try{process.stderr.write(`Warning: failed to mark library index dirty after archive write: ${t}
|
|
2924
|
+
`)}catch{}}var Da=l(()=>{"use strict";O();Ar()});var Cg=l(()=>{"use strict"});function Pl(e){let t=e.kinds?.map(r=>{let n=H0(r);if(n===void 0)throw new Error(`Unsupported archive search type: ${r}`);return n});return{archiveKey:it(e.archivePath),...e.backlinks===void 0?{}:{backlinks:e.backlinks},...Ax(e),...e.cursor===void 0?{}:{cursor:e.cursor},...zs(e),...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...Nt(e),...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},...t===void 0?{}:{types:t}}}function Tn(e,t={}){return{archiveKey:it(e.archivePath),archivePath:it(e.archivePath),indexScope:U0(e.archivePath),...e.backlinks===void 0?{}:{backlinks:e.backlinks},..._x(e.evidenceLimit)?{evidenceDisabled:!0}:{},...t.continuationKind===void 0?{}:{continuationKind:t.continuationKind},...zs(e),...Nt(e),format:e.format??"text",limit:e.limit??20,...e.reverse===!0?{order:"doc-desc"}:{},...e.action!=="evidence"&&e.action!=="related"&&e.action!=="search"?{}:e.query===void 0?{}:{query:e.query},...e.role===void 0?{}:{role:e.role},...t.continuationKind==="collection"?Ax(e):{},...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},...t.targetUri===void 0?{}:{targetUri:t.targetUri},types:e.kinds===void 0?null:e.kinds.map(r=>H0(r)).filter(r=>r!==void 0)}}function Px(e,t){if(!_x(e.evidenceLimit))return e.evidenceLimit!==void 0?e.evidenceLimit:_re(t)?3:void 0}function _x(e){return e===0}function zs(e){return e.evidenceLimit===void 0||_x(e.evidenceLimit)?{}:{evidenceLimit:e.evidenceLimit}}function Nt(e){return e.context===void 0?{}:{sourceContext:e.context}}function _re(e){return/^wikg:\/\/(?:(?:chapter\/[1-9][0-9]*\/)?entity\/Q[1-9][0-9]*|(?:chapter\/[1-9][0-9]*\/)?triple\/Q[1-9][0-9]*\/[^/]+\/Q[1-9][0-9]*)\/?$/u.test(e)}function Ma(e){let t=e.kinds?.map(r=>{let n=Are(r);if(n===void 0)throw new Error(`Unsupported archive list type: ${r}`);return n});return{...e.backlinks===void 0?{}:{backlinks:e.backlinks},...Ax(e),...e.cursor===void 0?{}:{cursor:e.cursor},...zs(e),...e.limit===void 0?{}:{limit:e.limit},...Nt(e),...e.triplePattern===void 0?{}:{triplePattern:e.triplePattern},...t===void 0?{}:{types:t}}}function Lr(e){return{chapters:e.chapters,items:e.items,lens:"typed",lensHint:null,limit:e.limit,match:"any",nextCursor:e.nextCursor,order:e.order,query:"",terms:[],types:null}}function Ax(e){if(e.chapters!==void 0)return{chapters:e.chapters};let t=B(e.archivePath).objectUri;if(t===void 0)return{};let r=B0(t);return r===void 0?{}:{chapters:[r]}}function H0(e){switch(e){case"chapter":return"chapter-title";case"chunk":return"node";case"source":return"source";case"summary":return"summary";case"entity":return"entity";case"meta":return"meta";case"triple":return"triple"}}function Are(e){switch(e){case"chapter":return"chapter-title";case"chunk":return"node";case"entity":return"entity";case"meta":return"meta";case"source":return"source";case"summary":return"summary";case"triple":return"triple"}}var Lx=l(()=>{"use strict";O();Cg();Ar()});async function K0(e){let t=e.cursor??e.archivePath,r=e.cursor===void 0?void 0:e.archivePath,n=await qd(t);if(r!==void 0){let{archivePath:i}=await ve(r),a=q0(n);if(i!==a)throw new Error(`Continuation cursor ${t} belongs to ${a}, not ${i}.`)}if(n.indexScope?.kind==="library-index"){await Lre(e,n);return}await It(q0(n),async i=>{let a=e.format??n.format,o=e.limit??20;switch(n.kind){case"collection":{let s={...n.backlinks===void 0?{}:{backlinks:n.backlinks},...n.chapters===null?{}:{chapters:n.chapters},cursor:n.cursor,...n.evidenceLimit===void 0?{}:{evidenceLimit:n.evidenceLimit},...n.ids===null?{}:{ids:n.ids},limit:o,order:n.order,...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext},...n.triplePattern===void 0?{}:{triplePattern:n.triplePattern}};n.types!==null&&Object.assign(s,{types:n.types}),await _r(Lr(await Ir(i,s)),{archiveKey:n.archiveKey,archivePath:n.archivePath,...n.backlinks===void 0?{}:{backlinks:n.backlinks},...n.chapters===null?{}:{chapters:n.chapters},continuationKind:"collection",...n.evidenceLimit===void 0?{}:{evidenceLimit:n.evidenceLimit},format:a,indexScope:n.indexScope,...n.ids===null?{}:{ids:n.ids},limit:o,order:n.order,...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext},...n.triplePattern===void 0?{}:{triplePattern:n.triplePattern},types:n.types},a);return}case"search":{let s={archiveKey:n.archiveKey,...n.backlinks===void 0?{}:{backlinks:n.backlinks},...n.chapters===null?{}:{chapters:n.chapters},cursor:n.cursor,...n.evidenceLimit===void 0?{}:{evidenceLimit:n.evidenceLimit},limit:o,...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext},...n.triplePattern===void 0?{}:{triplePattern:n.triplePattern}};n.types!==null&&Object.assign(s,{types:n.types}),await _r(await ln(i,n.query??"",s),{archiveKey:n.archiveKey,archivePath:n.archivePath,...n.backlinks===void 0?{}:{backlinks:n.backlinks},...n.chapters===null?{}:{chapters:n.chapters},...n.evidenceLimit===void 0?{}:{evidenceLimit:n.evidenceLimit},format:a,indexScope:n.indexScope,limit:o,...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext},...n.triplePattern===void 0?{}:{triplePattern:n.triplePattern},types:n.types},a);return}case"evidence":await Ks(await Qt(i,n.targetUri,{cursor:n.cursor,limit:o,order:n.order,...n.query===void 0?{}:{query:n.query},...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext}}),{archiveKey:n.archiveKey,archivePath:n.archivePath,continuationKind:"evidence",format:a,indexScope:n.indexScope,limit:o,order:n.order,...n.query===void 0?{}:{query:n.query},...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext},targetUri:n.targetUri,types:null},a);return;case"related":await Js(await _t(i,n.targetUri,{cursor:n.cursor,...n.evidenceLimit===void 0?{}:{evidenceLimit:n.evidenceLimit},limit:o,order:n.order,...n.query===void 0?{}:{query:n.query},...n.role===void 0?{}:{role:n.role},...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext}}),{archiveKey:n.archiveKey,archivePath:n.archivePath,continuationKind:"related",...n.evidenceLimit===void 0?{}:{evidenceLimit:n.evidenceLimit},format:a,indexScope:n.indexScope,limit:o,order:n.order,...n.query===void 0?{}:{query:n.query},...n.role===void 0?{}:{role:n.role},...n.sourceContext===void 0?{}:{sourceContext:n.sourceContext},targetUri:n.targetUri,types:null},a);return}})}async function Lre(e,t){if(t.indexScope.kind!=="library-index")throw new Error("Internal error: expected a library index cursor.");let r=await hI(t.indexScope.libraryId),n=e.format??t.format,i=e.limit??20;switch(t.kind){case"collection":{let a={...t.backlinks===void 0?{}:{backlinks:t.backlinks},...t.chapters===null?{}:{chapters:t.chapters},cursor:t.cursor,...t.evidenceLimit===void 0?{}:{evidenceLimit:t.evidenceLimit},...t.ids===null?{}:{ids:t.ids},limit:i,order:t.order,...t.sourceContext===void 0?{}:{sourceContext:t.sourceContext},...t.triplePattern===void 0?{}:{triplePattern:t.triplePattern}};t.types!==null&&Object.assign(a,{types:t.types}),await _r(Lr(await xa(r,a)),{...Rg(t,n,i),continuationKind:"collection"},n);return}case"search":{let a={archiveKey:t.archiveKey,...t.backlinks===void 0?{}:{backlinks:t.backlinks},...t.chapters===null?{}:{chapters:t.chapters},cursor:t.cursor,...t.evidenceLimit===void 0?{}:{evidenceLimit:t.evidenceLimit},limit:i,...t.sourceContext===void 0?{}:{sourceContext:t.sourceContext},...t.triplePattern===void 0?{}:{triplePattern:t.triplePattern}};t.types!==null&&Object.assign(a,{types:t.types}),await _r(await Rs(r,t.query??"",a),Rg(t,n,i),n);return}case"evidence":await Ks(await Ps(r,t.targetUri,{cursor:t.cursor,limit:i,order:t.order,...t.query===void 0?{}:{query:t.query},...t.sourceContext===void 0?{}:{sourceContext:t.sourceContext}}),{...Rg(t,n,i),continuationKind:"evidence",order:t.order,...t.query===void 0?{}:{query:t.query},targetUri:t.targetUri},n);return;case"related":await Js(await rl(r,t.targetUri,{cursor:t.cursor,...t.evidenceLimit===void 0?{}:{evidenceLimit:t.evidenceLimit},limit:i,order:t.order,...t.query===void 0?{}:{query:t.query},...t.role===void 0?{}:{role:t.role},...t.sourceContext===void 0?{}:{sourceContext:t.sourceContext}}),{...Rg(t,n,i),continuationKind:"related",...t.evidenceLimit===void 0?{}:{evidenceLimit:t.evidenceLimit},order:t.order,...t.query===void 0?{}:{query:t.query},...t.role===void 0?{}:{role:t.role},targetUri:t.targetUri},n);return}}function Rg(e,t,r){return{archiveKey:e.archiveKey,archivePath:e.archivePath,format:t,indexScope:e.indexScope,limit:r,types:e.kind==="evidence"||e.kind==="related"?null:e.types,...e.kind==="search"||e.kind==="collection"?e.backlinks===void 0?{}:{backlinks:e.backlinks}:{},...e.kind==="search"||e.kind==="collection"?e.evidenceLimit===void 0?{}:{evidenceLimit:e.evidenceLimit}:{},...e.kind==="search"||e.kind==="collection"?e.sourceContext===void 0?{}:{sourceContext:e.sourceContext}:{},...e.kind==="search"?e.query===void 0?{}:{query:e.query}:{}}}function q0(e){return e.indexScope===void 0?e.archivePath:e.indexScope.kind==="archive-index"?e.indexScope.archivePath:e.archivePath}var J0=l(()=>{"use strict";O();Rx();Da();Lx();Cg();Ar()});async function z0(e){let t=it(e.objectId);if(e.format==="json"){await S(L({uri:Le(t)}));return}await S(`<archive>
|
|
2925
|
+
`)}var Q0=l(()=>{"use strict";O();T();Ar()});var V0=l(()=>{"use strict";Da();J0();Lx();Q0();Cg();Ar()});async function Qs(e,t){let r=B(t.archivePath).objectUri;if(r===void 0)return;let n=Ore(r);if(n===void 0||n.kind==="collection"&&t.depth===void 0)return;let i=await z(e),a=n.kind==="collection"?Nre(i,t.depth):jre(i,await xs(e,n.chapterPath),t.depth);return{chapterIds:a.map(o=>o.chapterId),entries:a}}function Ore(e){if(e==="wikg://chapter"||e==="wikg://chapter/")return{kind:"collection"};let t=/^wikg:\/\/chapter\/([^/].*?)(?:\/)?$/u.exec(e);if(t?.[1]!==void 0)return{chapterPath:decodeURIComponent(t[1]),kind:"chapter"}}function Nre(e,t){return t===void 0?e:e.filter(r=>r.depth<=t)}function jre(e,t,r){let n=e.find(a=>a.chapterId===t);if(n===void 0)throw new Error(`Chapter ${t} does not exist.`);let i=`${n.path}/`;return e.filter(a=>a.chapterId===n.chapterId?!0:a.path.startsWith(i)?r===void 0||a.depth-n.depth<=r:!1)}var Ox=l(()=>{"use strict";O()});var Y0={};Qe(Y0,{runArchiveCommand:()=>Dre});async function Dre(e){let t=de(e.archivePath);if(t?.kind==="scope"&&t.objectUri!=="wikg://index"&&(t.objectUri!==void 0||e.action==="search"||e.action==="list")){await Mre(e,t);return}switch(e=await G0(e),e.action){case"create":await u0(e);return;case"export":if(e.outputFormat===void 0)throw new Error("Internal error: missing export output format.");await El({help:!1,inputFormat:"wikg",inputPath:e.archivePath,...e.outputPath===void 0?{}:{outputPath:e.outputPath},outputFormat:e.outputFormat,verbose:!1});return;case"inspect":await It((await ve(e.archivePath)).archivePath,async r=>{await m0(r,e)});return;case"search":await It(it(e.archivePath),async r=>{let n=await Qs(r,e),i=n===void 0?e:{...e,chapters:n.chapterIds},a=Tn(i);if(e.all===!0){await Rl(async o=>await ln(r,i.query,{...Pl(i),...o===void 0?{}:{cursor:o}}),a,e.format??"text");return}await _r(await ln(r,i.query,Pl(i)),a,e.format??"text")});return;case"list":await It(it(e.archivePath),async r=>{let n=await Qs(r,e),i=n===void 0?e:{...e,chapters:n.chapterIds},a=Tn(i,{continuationKind:"collection"});if(e.all===!0){if(e.limit!==void 0){await Rl(async o=>Lr(await Ir(r,{...Ma(i),...o===void 0?{}:{cursor:o}})),a,e.format??"text");return}await Tl(Lr(await Ir(r,{...Ma(i),limit:1e9})),a,e.format??"text");return}await _r(Lr(await Ir(r,Ma(i))),a,e.format??"text")});return;case"get":if($0(e)){await z0(e);return}await It(it(e.archivePath),async r=>{let n=Cn(e.objectId),i=Px(e,n),a=i===void 0?Tn(e):Tn({...e,evidenceLimit:i});await Tx(await ht(r,n,{...e.backlinks===void 0?{}:{backlinks:e.backlinks},...i===void 0?{}:{evidenceLimit:i},...e.reverse===!0?{order:"doc-desc"}:{},...Nt(e)}),a,e.format??"text")});return;case"related":await It(it(e.archivePath),async r=>{let n=Tn(e,{continuationKind:"related",targetUri:Cn(e.objectId)}),i=async a=>await _t(r,Cn(e.objectId),{...a===void 0?{}:{cursor:a},...zs(e),...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...e.query===void 0?{}:{query:e.query},...e.role===void 0?{}:{role:e.role},...Nt(e)});if(e.all===!0){await vx(i,e.cursor,n,e.format??"text");return}await Js(await i(e.cursor),n,e.format??"text")});return;case"evidence":await It(it(e.archivePath),async r=>{let n=Tn(e,{continuationKind:"evidence",targetUri:Cn(e.objectId)});if(e.all===!0){await xx(async i=>await Qt(r,Cn(e.objectId),{...i===void 0?{}:{cursor:i},...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...e.query===void 0?{}:{query:e.query},...Nt(e)}),e.cursor,e.format??"text");return}await Ks(await Qt(r,Cn(e.objectId),{...e.cursor===void 0?{}:{cursor:e.cursor},...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...e.query===void 0?{}:{query:e.query},...Nt(e)}),n,e.format??"text")});return;case"next":await K0(e);return;case"pack":await It(it(e.archivePath),async r=>{await Cx(await ni(r,Cn(e.objectId),e.budget??5e3),Tn(e),e.format??"text")});return}}async function Mre(e,t){let r=await U(t),n=t.objectUri===void 0&&e.objectId===void 0?"wikg://":Cn(e.objectId??e.archivePath),i={...Tn(e),archiveKey:e.archivePath,archivePath:e.archivePath,indexScope:{kind:"library-index",libraryId:r.id}};switch(e.action){case"search":if(e.all===!0){await Rl(async a=>await Rs(t,e.query,{...Pl(e),...a===void 0?{}:{cursor:a}}),i,e.format??"text");return}await _r(await Rs(t,e.query,Pl(e)),i,e.format??"text");return;case"list":{let a={...i,continuationKind:"collection"};if(e.all===!0){if(e.limit!==void 0){await Rl(async o=>Lr(await xa(t,{...Ma(e),...o===void 0?{}:{cursor:o}})),a,e.format??"text");return}await Tl(Lr(await xa(t,{...Ma(e),limit:1e9})),a,e.format??"text");return}await _r(Lr(await xa(t,Ma(e))),a,e.format??"text");return}case"get":{let a=Px(e,n);await Tx(await pI(t,n,{...e.backlinks===void 0?{}:{backlinks:e.backlinks},...a===void 0?{}:{evidenceLimit:a},...e.reverse===!0?{order:"doc-desc"}:{},...Nt(e)}),a===void 0?i:{...i,evidenceLimit:a},e.format??"text");return}case"related":{let a={...i,continuationKind:"related",targetUri:n},o=async s=>await rl(t,n,{...s===void 0?{}:{cursor:s},...zs(e),...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...e.query===void 0?{}:{query:e.query},...e.role===void 0?{}:{role:e.role},...Nt(e)});if(e.all===!0){await vx(o,e.cursor,a,e.format??"text");return}await Js(await o(e.cursor),a,e.format??"text");return}case"evidence":{let a={...i,continuationKind:"evidence",targetUri:n};if(e.all===!0){await xx(async o=>await Ps(t,n,{...o===void 0?{}:{cursor:o},...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...e.query===void 0?{}:{query:e.query},...Nt(e)}),e.cursor,e.format??"text");return}await Ks(await Ps(t,n,{...e.cursor===void 0?{}:{cursor:e.cursor},...e.limit===void 0?{}:{limit:e.limit},...e.reverse===!0?{order:"doc-desc"}:{},...e.query===void 0?{}:{query:e.query},...Nt(e)}),a,e.format??"text");return}case"pack":await Cx(await mI(t,n,e.budget??5e3),i,e.format??"text");return;case"create":case"export":case"inspect":case"next":throw new Error(`The library index scope does not support \`${e.action}\`.`)}}var Z0=l(()=>{"use strict";O();ug();l0();f0();Rx();V0();Ox()});var i$={};Qe(i$,{runArchiveChapterCommand:()=>Fre});import{readFile as Wre}from"fs/promises";import{Readable as e$}from"stream";async function Fre(e){switch(e.action){case"add":await Ti(e.path,async t=>{let r=await Pg(t,e.parentChapterPath);await bn({archivePath:e.path,operation:"Adding chapter",scope:{kind:"archive"}});let n=await ff(t,{...r===void 0?{}:{parentChapterId:r},...e.title===void 0?{}:{title:e.title}});e.inputPath!==void 0&&(n=await Bu(t,n.chapterId,e$.from([await t$(e)]))),await Xs(n,e.json??!1)});return;case"list":await It(e.path,async t=>{await Gre(await z(t),e.json??!1)});return;case"move":await Ti(e.path,async t=>{let r=await Vs(t,e.chapterPath),n=await Pg(t,e.afterChapterPath),i=await Pg(t,e.beforeChapterPath),a=await Pg(t,e.parentChapterPath);await bn({archivePath:e.path,operation:"Moving chapter",scope:{kind:"archive"}});let o=await yf(t,r,{...n===void 0?{}:{afterChapterId:n},...i===void 0?{}:{beforeChapterId:i},...e.first===void 0?{}:{first:e.first},...e.last===void 0?{}:{last:e.last},...e.moveToRoot===void 0?{}:{root:e.moveToRoot},...a===void 0?{}:{parentChapterId:a}});await Xs(o,e.json??!1)});return;case"remove":await Ti(e.path,async t=>{let r=await Vs(t,e.chapterPath);if(await bn({archivePath:e.path,operation:"Removing chapter",scope:{kind:"archive"}}),await bf(t,r,{recursive:e.recursive??!1}),e.json===!0){await S(L({removed:!0,uri:`wikg://chapter/${e.chapterPath}`}));return}await S(`Removed chapter wikg://chapter/${e.chapterPath}.
|
|
2926
|
+
`)});return;case"reset":await Ti(e.path,async t=>{let r=await Vs(t,e.chapterPath);await qre(e.path,r,e.resetStage);let n=await wf(t,r,e.resetStage);await Xs(n,e.json??!1)});return;case"set-source":await Ti(e.path,async t=>{let r=await Vs(t,e.chapterPath);await Zt({archivePath:e.path,chapterIds:[r],operation:"Setting chapter source"});let n=await Bu(t,r,e$.from([await t$(e)]));await Xs(n,e.json??!1)});return;case"set-summary":await Ti(e.path,async t=>{let r=await Vs(t,e.chapterPath);await Zt({archivePath:e.path,chapterIds:[r],operation:"Setting chapter summary",requiresTarget:"reading-summary"});let n=await Sf(t,r,await Nx(e));await Xs(n,e.json??!1)});return;case"set-title":await Ti(e.path,async t=>{let r=await Vs(t,e.chapterPath);await Zt({archivePath:e.path,chapterIds:[r],operation:"Setting chapter title"});let n=await kf(t,r,e.clearTitle===!0?null:e.title);await Xs(n,!1)});return;case"tree":if(e.treeAction==="apply"){await Ti(e.path,async t=>{e.dryRun!==!0&&await bn({archivePath:e.path,operation:"Changing chapter tree",scope:{kind:"archive"}}),await Hre(await gf(t,Ef(JSON.parse(await Nx(e))),{dryRun:e.dryRun??!1}),e.dryRun??!1)},{markLibraryDirty:e.dryRun!==!0});return}await It(e.path,async t=>{await Ure(await Vn(t),e.json??!1)});return}}async function Vs(e,t){if(t===void 0)throw new Error("Missing chapter path.");return await $u(e,t)}async function Pg(e,t){return t===void 0?void 0:await $u(e,t)}async function Ti(e,t,r={}){if(r.markLibraryDirty===!1){let n=await ve(e);await new G(n.archivePath).write(t);return}await at(e,t)}async function Nx(e){if(e.inputValue!==void 0&&e.inputPath!==void 0)throw new Error("Choose either a positional value or --input, not both.");if(e.inputValue!==void 0)return e.inputValue;if(e.inputPath==="-"){let t="";for await(let r of bi())t+=r;return t}if(e.inputPath!==void 0)return await Wre(e.inputPath,"utf8");throw new Error("Missing input. Pass a positional value, use --input <path>, or use --input - for stdin.")}async function t$(e){let t=await Nx(e);if(t.trim()==="")throw new Error("Source input is empty. Pass non-empty text with --input <path> or --input -.");return t}async function Xs(e,t){if(t){await S(L({childCount:e.childCount,graphReady:e.graphReady,hasSummary:e.hasSummary,sourceUnits:e.fragmentCount,stage:_g(e.stage),title:e.title,uri:e.uri}));return}let r=[`Chapter: ${e.uri}`,`Title: ${e.title??"[untitled]"}`,`Stage: ${_g(e.stage)}`,`Source Units: ${e.fragmentCount}`,`Children: ${e.childCount}`,`Graph: ${e.graphReady?"yes":"no"}`,`Summary: ${e.hasSummary?"yes":"no"}`];await S(`${r.join(`
|
|
2927
|
+
`)}
|
|
2928
|
+
`)}async function Gre(e,t){if(t){await S(L({chapters:e.map(r=>({uri:r.uri,title:r.title,stage:_g(r.stage)}))}));return}if(e.length===0){await S(`No chapters.
|
|
2929
|
+
`);return}await S(`${e.map(r=>`${" ".repeat(r.depth)}[${_g(r.stage)}] ${r.title??"[untitled]"} (${r.uri})`).join(`
|
|
2930
|
+
`)}
|
|
2931
|
+
`)}async function Ure(e,t){if(t){await S(L(e));return}if(e.chapters.length===0){await S(`No chapters.
|
|
2932
|
+
`);return}await S($f(e.chapters.map(n$)))}function n$(e){return{children:e.children.map(n$),label:`${Bre(e.title)} (${$re(e.uri)})`}}function $re(e){return e.split("/").at(-1)??e}function Bre(e){return e??"[untitled]"}async function Hre(e,t){let r=[t?"Dry run: chapter tree not changed.":"Applied chapter tree.",`Changed: ${e.changed?"yes":"no"}`,`Moved: ${e.moved.length}`,`Renamed: ${e.renamed.length}`,`Unchanged: ${e.unchanged}`];for(let n of e.moved)r.push(`Move ${n.oldUri} [index ${n.oldIndex}] -> ${n.newUri} [index ${n.newIndex}]`);for(let n of e.renamed)r.push(`Rename ${n.uri}: ${r$(n.oldTitle)} -> ${r$(n.newTitle)}`);await S(`${r.join(`
|
|
2933
|
+
`)}
|
|
2934
|
+
`)}function r$(e){return e===null?"null":JSON.stringify(e)}function _g(e){switch(e){case"planned":return"planned";case"sourced":return"source";case"graphed":return"reading-graph";case"summarized":return"reading-summary"}}async function qre(e,t,r){switch(r){case"planned":await Zt({archivePath:e,chapterIds:[t],operation:"Resetting chapter to planned"});return;case"sourced":await Zt({archivePath:e,chapterIds:[t],operation:"Resetting chapter graph",requiresTarget:"reading-graph"}),await Zt({archivePath:e,chapterIds:[t],operation:"Resetting chapter summary",requiresTarget:"reading-summary"});return;case"graphed":await Zt({archivePath:e,chapterIds:[t],operation:"Resetting chapter summary",requiresTarget:"reading-summary"});return}}var a$=l(()=>{"use strict";O();O();T();T();Da();Ar()});var c$={};Qe(c$,{runArchiveIndexCommand:()=>Jre});async function Jre(e){switch(e.action){case"get":await zre(e);return;case"enable":await Qre(e);return;case"embed":await Vre(e);return;case"external":await Xre(e);return;case"disable":await Yre(e);return}}async function zre(e){let t=await ve(e.archivePath);await new G(t.archivePath).readDocument(async r=>{let n=await an(r);await Ag(e,{ftsEmbedded:n.ftsEmbedded,ftsCurrent:await qe(r)})})}async function Qre(e){let t=new xn({jsonl:e.jsonl??!1,throttleMs:Kre});await at(e.archivePath,async r=>{await t.write({json:{type:"started"},kind:"lifecycle",text:`index enable started
|
|
2935
|
+
steps: checking -> collecting -> clearing -> indexing-text -> indexing-objects -> finalizing`}),await t.write({json:{phase:"checking",type:"status_snapshot"},kind:"status",phase:"checking"}),await qe(r)?await t.write({json:{type:"already-current"},kind:"lifecycle",text:"already current"}):await ra(r,async n=>{await t.write({counters:n.done===void 0||n.total===void 0?[]:[o$(n)],json:{counters:n.done===void 0||n.total===void 0?[]:[o$(n)],phase:n.phase,type:"status_snapshot"},kind:"status",phase:n.phase})}),await t.write({json:{type:"completed"},kind:"lifecycle",text:"index enabled"}),await t.write({json:{type:"succeeded"},kind:"lifecycle",text:"succeeded"})},{searchIndexWritebackPolicy:await s$(e.archivePath)}),await $t(e.archivePath)}async function Vre(e){let t=!1;await at(e.archivePath,async r=>{await Io(r,!0),await qe(r)?await r.writeSearchIndexDatabase(async n=>{await n.run("UPDATE search_index_state SET value = value WHERE key = 'version'")}):(await ra(r),t=!0),await Ag(e,{built:t,ftsEmbedded:!0,ftsCurrent:await qe(r)})},{searchIndexWritebackPolicy:"archive"}),await $t(e.archivePath)}async function Xre(e){await at(e.archivePath,async t=>{await Io(t,!1),await t.deleteSearchIndexDatabase(),await Ag(e,{ftsEmbedded:!1,ftsCurrent:!1})},{searchIndexWritebackPolicy:"archive"}),await $t(e.archivePath)}async function Yre(e){await at(e.archivePath,async t=>{await t.deleteSearchIndexDatabase();let r=await an(t);await Ag(e,{ftsEmbedded:r.ftsEmbedded,ftsCurrent:!1})},{searchIndexWritebackPolicy:await s$(e.archivePath)}),await $t(e.archivePath)}async function s$(e){let t=!1,r=await ve(e);return await new G(r.archivePath).readDocument(async n=>{t=(await an(n)).ftsEmbedded}),t?"archive":"cache"}function o$(e){return{done:e.done??0,name:Zre(e.unit),total:e.total??0,unit:ene(e.unit)}}function Zre(e){switch(e){case"chapter":return"chapters";case"object":return"objects";case"sentence":return"sentences";case void 0:return"items"}}function ene(e){switch(e){case"chapter":return"chapters";case"object":return"objects";case"sentence":return"sentences";case void 0:return"items"}}async function Ag(e,t){if(e.json===!0){await S(L(t));return}await S([`FTS embedded: ${t.ftsEmbedded?"yes":"no"}`,`FTS current: ${t.ftsCurrent?"yes":"no"}`,...t.built===void 0?[]:[`Built: ${t.built?"yes":"no"}`],""].join(`
|
|
2936
|
+
`))}var Kre,d$=l(()=>{"use strict";O();O();O();T();T();vn();Da();Ar();Kre=6e3});var jx={};Qe(jx,{runArchiveCoverCommand:()=>rne,runArchiveMetaCommand:()=>tne});async function tne(e){if(e.metaPatch!==void 0){await nne(e.inputPath,e.metaPatch);return}let t=new gn({}),r=await ve(e.inputPath);await t.openSession(r.archivePath,async n=>{await u$(await n.readMeta(),{json:e.json??!1})})}async function rne(e){let t=new gn({}),r=await ve(e.inputPath);await t.openSession(r.archivePath,async n=>{if(process.stdout.isTTY===!0)throw new Error("Refusing to write binary cover data to an interactive terminal. Redirect stdout or pipe it.");let i=await n.readCover();if(i===void 0)throw new Error("Document cover is missing.");await fG(i.data)})}async function nne(e,t){await at(e,async r=>{let n=await r.readBookMeta();if(n===void 0)throw new Error("Archive metadata is missing.");let i=ine(n,t);await r.replaceBookMeta(i),await u$(i,{json:!1})})}async function u$(e,t){if(t.json){await S(L(one(e)));return}if(e===void 0){await S(`No document metadata is available.
|
|
2937
|
+
`);return}let r=[];ane(r,e),await S(`${r.join(`
|
|
2938
|
+
`)}
|
|
2939
|
+
`)}function ine(e,t){return{...e,title:t.clearTitle===!0?null:t.title??e.title,authors:t.clearAuthors===!0?[]:t.authors===void 0?e.authors:[...t.authors],language:t.clearLanguage===!0?null:t.language??e.language,identifier:t.clearIdentifier===!0?null:t.identifier??e.identifier,publisher:t.clearPublisher===!0?null:t.publisher??e.publisher,publishedAt:t.clearPublishedAt===!0?null:t.publishedAt??e.publishedAt,description:t.clearDescription===!0?null:t.description??e.description}}function ane(e,t){e.push(`Source Format: ${t?.sourceFormat??"[none]"}`),e.push(`Title: ${t?.title??"[none]"}`),e.push(`Authors: ${t?.authors.length===void 0||t.authors.length===0?"[none]":t.authors.join(", ")}`),e.push(`Language: ${t?.language??"[none]"}`),e.push(`Identifier: ${t?.identifier??"[none]"}`),e.push(`Publisher: ${t?.publisher??"[none]"}`),e.push(`Published At: ${t?.publishedAt??"[none]"}`),e.push(`Description: ${t?.description??"[none]"}`)}function one(e){return{authors:e?.authors??[],description:e?.description??null,identifier:e?.identifier??null,language:e?.language??null,publishedAt:e?.publishedAt??null,publisher:e?.publisher??null,sourceFormat:e?.sourceFormat??null,title:e?.title??null}}var Dx=l(()=>{"use strict";O();T();T();Da();Ar()});import{spawn as sne}from"child_process";import{existsSync as cne}from"fs";import{join as Mx,resolve as dne}from"path";function Wx(e,t={}){let r=une(e,t.args??[]);return sne(r.command,r.args,{cwd:al(),detached:t.detached===!0,env:oG(),stdio:t.detached===!0?"ignore":["ignore","pipe","pipe"]})}async function l$(e,t={}){let r=Wx(e,t),n=[],i=[];r.stdout?.on("data",a=>n.push(a)),r.stderr?.on("data",a=>i.push(a)),await new Promise((a,o)=>{r.on("error",o),r.on("close",s=>{if(s!==0){o(new Error(`Internal ${e} failed with exit code ${s}: ${Buffer.concat(i).toString("utf8").trim()}`));return}a()})});try{return JSON.parse(Buffer.concat(n).toString("utf8"))}catch(a){throw new Error(`Internal ${e} returned invalid JSON: ${a instanceof Error?a.message:String(a)}`)}}function une(e,t){let r=sG(),n=cG();return r!==void 0&&n!==void 0?{args:[Mx(r,"node_modules","tsx","dist","cli.mjs"),Mx(r,"packages","cli","src","bin",`${e}.ts`),"--wikigraph-internal-child",e,"--wikigraph-state-dir",n,...t],command:process.execPath}:{args:[lne(e),"--wikigraph-internal-child",e,...n===void 0?[]:["--wikigraph-state-dir",n],...t],command:process.execPath}}function lne(e){let t=`${e}.js`,r=globalThis.__WIKIGRAPH_CLI_DIST_DIR__??dne(al(),"packages","cli","dist"),n=Mx(r,t);return cne(n),n}var Fx=l(()=>{"use strict";yi()});var m$={};Qe(m$,{runGcCommand:()=>pne});async function pne(e){let t=await l$("gc-worker",{args:[...e.dryRun===!0?["--dry-run"]:[],...e.force===!0?["--force"]:[]]});if(e.json===!0){await S(L(t));return}if(t.skipped){await S(`GC skipped: another GC run is active.
|
|
2940
|
+
`);return}await S(["GC completed.",`Scanned: ${t.scanned}`,`Removed: ${t.removed}`,`Freed: ${p$(t.freedBytes)}`,...t.jobs.map(r=>r.error===void 0?`- ${r.name}: scanned ${r.scanned}, removed ${r.removed}, freed ${p$(r.freedBytes)}`:`- ${r.name}: failed: ${r.error}`),""].join(`
|
|
2941
|
+
`))}function p$(e){return e<1024?`${e} B`:e<1024*1024?`${(e/1024).toFixed(1)} KB`:e<1024*1024*1024?`${(e/1024/1024).toFixed(1)} MB`:`${(e/1024/1024/1024).toFixed(1)} GB`}var h$=l(()=>{"use strict";Fx();T();T()});var f$={};Qe(f$,{runLegacyCommand:()=>mne});async function mne(e){switch(e.action){case"migrate":{process.stderr.write("`wg legacy migrate` is deprecated. Use `wg maintenance upgrade <sdpub-path>`.\n");let t=await nl(e.inputPath,{...e.outputPath===void 0?{}:{outputPath:e.outputPath}});if(t.kind!=="sdpub")throw new Error("Legacy migrate only supports sdpub inputs.");process.stdout.write(`Wrote ${t.outputPath}
|
|
2942
|
+
`);return}}}var g$=l(()=>{"use strict";O()});var y$={};Qe(y$,{runMaintenanceCommand:()=>hne});async function hne(e){if(e.action==="upgrade"){let t=await nl(e.target,{...e.outputPath===void 0?{}:{outputPath:e.outputPath}});await S(e.json===!0?L(t):fne(t));return}}function fne(e){switch(e.kind){case"home":return`Home ${e.status}: ${e.path} (schema v${e.schemaVersionBefore} -> v${e.schemaVersionAfter})
|
|
2943
|
+
`;case"archive":return`Archive ${e.status}: ${e.path} (schema v${e.schemaVersionBefore} -> v${e.schemaVersionAfter})
|
|
2944
|
+
`;case"sdpub":return`Wrote ${e.outputPath}
|
|
2945
|
+
`;case"lib":{let t=[`Library ${e.status}: ${e.library.uri}`,`upgraded: ${e.upgraded.length}`,`already current: ${e.skipped.length}`];return e.failed!==void 0&&t.push(`failed: ${e.failed.uri} (${e.failed.message})`),`${t.join(`
|
|
2946
|
+
`)}
|
|
2947
|
+
`}}}var b$=l(()=>{"use strict";O();T()});var E$={};Qe(E$,{mergeMaskedSecretsForSet:()=>S$,runLocalConfigCommand:()=>wne});import{Writable as gne}from"stream";import{createInterface as yne}from"readline/promises";import{generateText as bne}from"ai";async function wne(e){switch(e.action){case"get":await _l(e.section,await En(e.section));return;case"set":await _l(e.section,await LI(e.section,S$(e.section,xne(e),await En(e.section))));return;case"put":{let t=w$(e.key),r=e.secret===!0?await Rne(t):await Ine(e);await _l(e.section,await $G(e.section,t,r));return}case"delete":await _l(e.section,await BG(e.section,w$(e.key)));return;case"clear":await _l(e.section,await HG(e.section));return;case"test":await Sne(e);return}}async function Sne(e){switch(e.section){case"llm":await kne(e);return;case"wikispine":await Ene(e);return;default:throw new Error("Only wikg://local/config/llm and wikg://local/config/wikispine support test.")}}async function kne(e){if(e.section!=="llm")throw new Error("Only wikg://local/config/llm supports test.");let t=Date.now(),r=await En("llm");try{let n={...typeof r.apiKey=="string"?{apiKey:r.apiKey}:{},...typeof r.baseURL=="string"?{baseURL:r.baseURL}:{},...typeof r.model=="string"?{model:r.model}:{},...typeof r.name=="string"?{name:r.name}:{},...typeof r.provider=="string"?{provider:Cne(r.provider)}:{}},i=Fs({llm:n}),a=await bne({maxRetries:0,messages:[{content:"Reply with exactly: ok",role:"user"}],model:i.model,temperature:0}),o={durationMs:Date.now()-t,model:n.model,ok:!0,provider:n.provider,response:a.text.trim()};if(e.json===!0){await S(L(o));return}await S(["LLM connection ok.",`Provider: ${o.provider}`,`Model: ${o.model}`,`Response: ${o.response}`,""].join(`
|
|
2948
|
+
`))}catch(n){let i={durationMs:Date.now()-t,error:{message:n instanceof Error?n.message:String(n)},model:typeof r.model=="string"?r.model:void 0,ok:!1,provider:typeof r.provider=="string"?r.provider:void 0};if(e.json===!0){await S(L(i)),ol(1);return}throw n}}async function Ene(e){let t=Date.now(),r=await En("wikispine");try{let n=Tne(r.provider),i=await of({provider:n}),a={durationMs:i.durationMs,...n==="fetch"?{endpoint:Mu}:{},...i.metadata===void 0?{}:{metadata:i.metadata},ok:!0,provider:n};if(e.json===!0){await S(L(a));return}await S(["WikiSpine connection ok.",`Provider: ${a.provider}`,...a.endpoint===void 0?[]:[`Endpoint: ${a.endpoint}`],...a.metadata===void 0?[]:[`Runtime: ${a.metadata.format}`,`Surfaces: ${a.metadata.surface_count}`,`QIDs: ${a.metadata.qid_count}`],""].join(`
|
|
2949
|
+
`))}catch(n){let i={durationMs:Date.now()-t,error:{message:n instanceof Error?n.message:String(n)},ok:!1,provider:typeof r.provider=="string"?r.provider:void 0};if(e.json===!0){await S(L(i)),ol(1);return}throw n}}async function _l(e,t){await S(L(KG(e,t)))}function Ine(e){if(e.inputValue!==void 0&&e.jsonInputValue!==void 0)throw new Error("Choose only one input source: positional value or --json value.");if(e.jsonInputValue!==void 0)return k$(e.jsonInputValue);if(e.inputValue!==void 0)return e.inputValue;throw new Error("Missing config value.")}function xne(e){let t=e.jsonInputValue??e.inputValue;if(t===void 0)throw new Error("Config set requires a JSON object.");let r=k$(t);if(r===null||typeof r!="object"||Array.isArray(r))throw new Error("Config set requires a JSON object.");return r}function S$(e,t,r){if(e!=="llm"||t.apiKey===void 0)return t;if(typeof t.apiKey=="string"&&/^\*+$/u.test(t.apiKey))return r.apiKey===void 0?vne(t,"apiKey"):{...t,apiKey:r.apiKey};throw new Error("apiKey is sensitive and cannot be set from JSON. Use `wg wikg://local/config/llm put apiKey --secret`.")}function vne(e,t){let{[t]:r,...n}=e;return n}function k$(e){try{return JSON.parse(e)}catch(t){throw new Error(`Invalid JSON input: ${t instanceof Error?t.message:String(t)}`)}}function w$(e){let t=e?.trim()??"";if(t==="")throw new Error("Missing config key.");return t}function Cne(e){switch(e){case"anthropic":case"google":case"openai":case"openai-compatible":return e;default:throw new Error(`Invalid llm.provider: ${e}. Expected anthropic, google, openai, or openai-compatible.`)}}function Tne(e){switch(e){case"cli":case"fetch":return e;default:throw new Error("Missing wikispine.provider. Configure `wikg://local/config/wikispine` with provider `cli` or `fetch`.")}}async function Rne(e){if(process.stdin.isTTY!==!0)throw new Error(`${e} requires an interactive terminal.`);let t=new gne({write(n,i,a){a()}}),r=yne({input:process.stdin,output:t,terminal:!0});try{await vI(`${e}: `);let n=await r.question("");if(await vI(`
|
|
2950
|
+
`),n.trim()==="")throw new Error(`${e} cannot be empty.`);return n.trim()}finally{r.close()}}var I$=l(()=>{"use strict";O();Sl();yi();T();T();ll()});var L$={};Qe(L$,{runLibraryCommand:()=>Ane});import{readFile as Pne}from"fs/promises";async function Ane(e){switch(e.action!=="add"&&e.action!=="create"&&e.action!=="scan"&&e.action!=="rebind"&&await kI(e.target),e.action){case"add":{if(e.inputPath===void 0)throw new Error("Missing --input <path> for library add.");await Lg(await aI({inputPath:e.inputPath,target:e.target,...e.to===void 0?{}:{to:e.to}}),e.json??!1);return}case"create":{if(e.path===void 0)throw new Error("Missing --path <folder> for library create.");let t=await sb({folderPath:e.path});await Lne(t,e.json??!1);return}case"list":{if(e.target.kind==="registry"){await One(await cb(),e.json??!1);return}if(e.target.kind==="archive-collection"){await v$(e.json??!1);return}await P$(await gt(e.target),e.json??!1);return}case"scan":{await x$("scan",async()=>await rI(e.target),e.json??!1,e.jsonl??!1);return}case"rebind":{let t=e.path;if(t===void 0)throw new Error("Missing path value for library path set.");await x$("path set",async()=>await nI({folderPath:t,target:e.target}),e.json??!1,e.jsonl??!1);return}case"archive-tree":{await Dne(await gt(e.target),{...e.depth===void 0?{}:{depth:e.depth},json:e.json??!1,...e.parent===void 0?{}:{parent:e.parent}});return}case"get-index":{await C$(await Cs(e.target),e.json??!1);return}case"enable-index":{let t=new xn({jsonl:e.jsonl??!1,throttleMs:_ne});await t.write({json:{type:"started"},kind:"lifecycle",text:`library index enable started
|
|
2951
|
+
steps: collecting -> clearing -> indexing-text -> indexing-objects -> finalizing`});let r=await zE(e.target,async n=>{let i=n.done===void 0||n.total===void 0?[]:[Gne(n)];await t.write({counters:i,json:{counters:i,phase:n.phase,type:"status_snapshot"},kind:"status",phase:n.phase})});await t.write({json:{status:r.status,type:"completed"},kind:"lifecycle",text:"library index enabled"}),await t.write({json:{type:"succeeded"},kind:"lifecycle",text:"succeeded"});return}case"disable-index":{await C$(await QE(e.target),e.json??!1);return}case"remove":{if(e.target.kind==="archive"){await Lg(await cI({target:e.target}),e.json??!1,"Removed library archive");return}let t=await ub(e.target);await S(e.json===!0?L({removed:t.uri}):`Removed library registry: ${t.uri}
|
|
2952
|
+
`);return}case"get":{if(e.target.kind==="path"){await Nne(await U(e.target),e.json??!1);return}if(e.target.kind==="archive-path"){await jne(await Xu({...e.target,kind:"archive"}),e.json??!1);return}if(e.target.kind==="metadata"){await Al(await lb(e.target),e.json??!1);return}if(e.target.kind==="archive"){await Lg(await Xu(e.target),e.json??!1,"Library archive");return}await U(e.target),await v$(e.json??!1);return}case"set":{if(e.target.kind==="archive-path"){if(e.to===void 0)throw new Error("Missing path value for library archive path set.");await Lg(await dI({target:{...e.target,kind:"archive"},to:e.to}),e.json??!1,"Moved library archive");return}let t=await T$(e,{jsonRequired:!0});await Al(await pb(e.target,$ne(t)),e.json??!1);return}case"put":{await Al(await mb(e.target,R$(e.key),await T$(e,{jsonRequired:!1})),e.json??!1);return}case"delete":{await Al(await hb(e.target,R$(e.key)),e.json??!1);return}case"clear":{await Al(await fb(e.target),e.json??!1);return}}}async function Lne(e,t){if(t){await S(L({uri:e.uri,id:e.publicId,folderPath:e.folderPath,isDefault:e.isDefault,stagingPath:e.stagingPath,createdAt:e.createdAt,updatedAt:e.updatedAt}));return}await S(`${e.uri}
|
|
2953
|
+
`)}async function One(e,t){if(t){await S(L({items:e.map(r=>({uri:r.uri,id:r.publicId,path:r.folderPath,isDefault:r.isDefault}))}));return}await S(e.map(r=>[r.uri,r.publicId,r.folderPath,r.isDefault?"default":""].join(" ")).join(`
|
|
2954
|
+
`)+(e.length===0?"":`
|
|
2955
|
+
`))}async function Nne(e,t){if(t){await S(L({uri:`${e.uri}/path`,path:e.folderPath}));return}await S(`${e.folderPath}
|
|
2956
|
+
`)}async function jne(e,t){if(t){await S(L({uri:`${e.uri}/path`,path:e.relativePath}));return}await S(`${e.relativePath}
|
|
2957
|
+
`)}async function x$(e,t,r,n){if(n){let a=new xn({jsonl:!0,throttleMs:0});await a.write({json:{type:"started",action:e},kind:"lifecycle",text:`${e} started`});let o=await t();for(let s of o.archives)await a.write({json:{type:"archive",action:e,archive:Ux(s)},kind:"status",phase:e});await a.write({json:{type:"completed",action:e,archives:o.archives.length},kind:"lifecycle",text:`${e} completed`}),await a.write({json:{type:"succeeded",action:e},kind:"lifecycle",text:"succeeded"});return}let i=await t();await P$(i.archives,r)}async function P$(e,t){if(t){await S(L({items:e.map(Ux)}));return}await S(e.map(r=>[r.uri,r.relativePath,r.status].join(" ")).join(`
|
|
2958
|
+
`)+(e.length===0?"":`
|
|
2959
|
+
`))}async function v$(e){await S(e?L({items:[]}):"")}async function Lg(e,t,r="Added library archive"){if(t){await S(L(Ux(e)));return}await S(`${r}: ${e.uri}
|
|
2960
|
+
${e.relativePath}
|
|
2961
|
+
`)}function Ux(e){return{uri:e.uri,id:e.publicId,libraryUri:e.libraryUri,relativePath:e.relativePath,path:e.path,exists:e.exists,status:e.status,lastSeenMutationToken:e.lastSeenMutationToken,lastSeenSize:e.lastSeenSize,lastSeenMtimeMs:e.lastSeenMtimeMs,lastScannedAt:e.lastScannedAt,createdAt:e.createdAt,updatedAt:e.updatedAt}}async function Dne(e,t){let r=t.parent?.replace(/\\/gu,"/").replace(/^\/+|\/+$/gu,""),n={children:new Map,name:"",path:""};for(let i of e)r!==void 0&&!Mne(i.relativePath,r)||Wne(n,i);if(Gx(n,t.depth,0),t.json){await S(L({items:_$(n.children)}));return}await S(Fne(n.children))}function Mne(e,t){return e===t||e.startsWith(`${t.replace(/\/$/u,"")}/`)}function Wne(e,t){let r=e,n=t.relativePath.split("/");for(let i=0;i<n.length;i+=1){let a=n[i],o=n.slice(0,i+1).join("/"),s=r.children.get(a);s===void 0&&(s={children:new Map,name:a,path:o},r.children.set(a,s)),r=s}r.archive=t}function Gx(e,t,r){if(t===void 0){for(let n of e.children.values())Gx(n,t,r+1);return}if(r>=t){e.children.clear();return}for(let n of e.children.values())Gx(n,t,r+1)}function _$(e){return[...e.values()].map(t=>({children:_$(t.children),name:t.name,path:t.path,...t.archive===void 0?{}:{uri:t.archive.uri}}))}function Fne(e){return $f([...e.values()].map(A$))}function A$(e){return{children:[...e.children.values()].map(A$),label:e.archive===void 0?e.name:`${e.name} (${e.archive.uri})`}}async function C$(e,t){if(t){await S(L(e));return}await S([`Status: ${e.status}`,`Enabled: ${e.enabled?"yes":"no"}`,`Source fingerprint: ${e.sourceFingerprint}`,...e.fingerprint===void 0?[]:[`Index fingerprint: ${e.fingerprint}`],`Sources: ${e.sources.length}`,""].join(`
|
|
2962
|
+
`))}function Gne(e){let t=e.unit==="chapter"?"chapters":e.unit==="sentence"?"sentences":"objects";return{done:e.done??0,name:t,total:e.total??0,unit:t}}async function T$(e,t){let r=await Une(e);return t.jsonRequired||e.jsonInputValue!==void 0?Bne(r):r}async function Une(e){let t=[e.inputValue===void 0?void 0:"positional value",e.inputPath===void 0?void 0:"--input",e.jsonInputValue===void 0?void 0:"--json value"].filter(r=>r!==void 0);if(t.length>1)throw new Error(`Choose only one input source: ${t.join(", ")}.`);if(e.jsonInputValue!==void 0)return e.jsonInputValue;if(e.inputValue!==void 0)return e.inputValue;if(e.inputPath==="-"){let r="";for await(let n of bi())r+=n;return r}if(e.inputPath!==void 0)return await Pne(e.inputPath,"utf8");throw new Error("Missing input. Pass a value, use --input <path>, or use --input - for stdin.")}function $ne(e){if(e===null||typeof e!="object"||Array.isArray(e))throw new Error("Metadata set requires a JSON object.");return e}function Bne(e){try{return JSON.parse(e)}catch(t){throw new Error(`Invalid JSON input: ${t instanceof Error?t.message:String(t)}`)}}function R$(e){let t=e?.trim()??"";if(t==="")throw new Error("Metadata key cannot be empty.");return t}function Al(e,t){if(t)return S(L(e));let r=Object.entries(e).map(([n,i])=>`${n}: ${Hne(i)}`);return S(r.length===0?"":`${r.join(`
|
|
2963
|
+
`)}
|
|
2964
|
+
`)}function Hne(e){return Array.isArray(e)?e.map(t=>String(t)).join(" "):e!==null&&typeof e=="object"?JSON.stringify(e):String(e)}var _ne,O$=l(()=>{"use strict";O();T();vn();_ne=6e3});var D$={};Qe(D$,{runObjectMetadataCommand:()=>Kne});import{readFile as qne}from"fs/promises";async function Kne(e){let t=Yne(e.objectPath);switch(e.action){case"get":{let r=await ve(e.archivePath);await new G(r.archivePath).readDocument(async n=>{await Ll(await n.metadata.getMap(e.objectPath),e.json??!1)});return}case"set":{let r=await N$(e,{jsonRequired:!0}),n=zne(r);await at(e.archivePath,async i=>{await i.metadata.replaceMap(t,n),await Ll(await i.metadata.getMap(e.objectPath),e.json??!1)});return}case"put":{let r=j$(e.key),n=await N$(e,{jsonRequired:!1});await at(e.archivePath,async i=>{await i.metadata.put(t,r,n),await Ll(await i.metadata.getMap(e.objectPath),e.json??!1)});return}case"delete":await at(e.archivePath,async r=>{await r.metadata.deleteKey(e.objectPath,j$(e.key)),await Ll(await r.metadata.getMap(e.objectPath),e.json??!1)});return;case"clear":await at(e.archivePath,async r=>{await r.metadata.clear(e.objectPath),await Ll(await r.metadata.getMap(e.objectPath),e.json??!1)});return}}async function N$(e,t){let r=await Jne(e);return e.json===!0||t.jsonRequired?Qne(r):r}async function Jne(e){let t=[e.inputValue===void 0?void 0:"positional value",e.inputPath===void 0?void 0:"--input",e.jsonInputValue===void 0?void 0:"--json value"].filter(r=>r!==void 0);if(t.length>1)throw new Error(`Choose only one input source: ${t.join(", ")}.`);if(e.jsonInputValue!==void 0)return e.jsonInputValue;if(e.inputValue!==void 0)return e.inputValue;if(e.inputPath==="-"){let r="";for await(let n of bi())r+=n;return r}if(e.inputPath!==void 0)return await qne(e.inputPath,"utf8");throw new Error("Missing input. Pass a value, use --input <path>, or use --input - for stdin.")}function zne(e){if(e===null||typeof e!="object"||Array.isArray(e))throw new Error("Metadata set requires a JSON object.");return e}function Qne(e){try{return JSON.parse(e)}catch(t){throw new Error(`Invalid JSON input: ${t instanceof Error?t.message:String(t)}`)}}function j$(e){let t=e?.trim()??"";if(t==="")throw new Error("Metadata key cannot be empty.");return t}function Ll(e,t){return t?S(L(e)):S(Vne(e))}function Vne(e){let t=Object.entries(e).map(([r,n])=>`${r}: ${Xne(n)}`);return t.length===0?"":`${t.join(`
|
|
2965
|
+
`)}
|
|
2966
|
+
`}function Xne(e){return Array.isArray(e)?e.map(t=>String(t)).join(" "):String(e)}function Yne(e){if(e==="")return{kind:1,objectPath:e};let t=/^chapter\/([1-9][0-9]*)(?:\/.*)?$/u.exec(e);if(t?.[1]!==void 0)return{chapterId:Number(t[1]),kind:2,objectPath:e};let r=/^chunk\/([1-9][0-9]*)$/u.exec(e);if(r?.[1]!==void 0)return{chunkId:Number(r[1]),kind:3,objectPath:e};let n=/^entity\/(Q[1-9][0-9]*)$/u.exec(e);if(n?.[1]!==void 0)return{entityQid:n[1],kind:4,objectPath:e};let i=/^triple\/(Q[1-9][0-9]*)\/([^/]+)\/(Q[1-9][0-9]*)$/u.exec(e);return i?.[1]!==void 0?{kind:5,objectPath:e,tripleObjectQid:i[3],triplePredicate:decodeURIComponent(i[2]),tripleSubjectQid:i[1]}:{kind:6,objectPath:e}}var M$=l(()=>{"use strict";O();O();Da();Ar();T();T()});function Ng(e){let t={job:e.config.concurrent?.job??3,request:e.config.concurrent?.request??6},r=e.chapters.reduce((o,s)=>o+s.words,0),n=cg(e.config.llm),i=Zne({chapters:e.chapters,concurrent:t,model:n,target:e.target}),a=Math.max(0,...i.map(o=>o.chapters));return{chapters:e.chapters.length,concurrent:t,includesPrerequisites:i.some(o=>o.prerequisite),performanceHints:sg({chapters:a,concurrent:t,hasGenerationWork:i.length>0}),planning:eie(n,i),steps:i,target:e.target,words:r}}function Zne(e){switch(e.target){case"knowledge-graph":return[Og({chapters:e.chapters,concurrent:e.concurrent,model:e.model,prerequisite:!1,task:"knowledge-graph"})].filter(t=>t.chapters>0);case"reading-graph":return[Og({chapters:e.chapters.filter(t=>t.stage==="sourced"),concurrent:e.concurrent,model:e.model,prerequisite:!1,task:"reading-graph"})].filter(t=>t.chapters>0);case"reading-summary":{let t=e.chapters.filter(n=>n.stage==="sourced"),r=e.chapters.filter(n=>n.stage==="sourced"||n.stage==="graphed");return[Og({chapters:t,concurrent:e.concurrent,model:e.model,prerequisite:!0,task:"reading-graph"}),Og({chapters:r,concurrent:e.concurrent,model:e.model,prerequisite:!1,task:"reading-summary"})].filter(n=>n.chapters>0)}}}function Og(e){let t=e.chapters.reduce((r,n)=>r+n.words,0);return{chapters:e.chapters.length,planning:og(e.task,t,e.chapters.length,e.concurrent,e.model),prerequisite:e.prerequisite,task:e.task,words:t}}function eie(e,t){return{model:e,timeSeconds:{max:t.reduce((r,n)=>r+n.planning.timeSeconds.max,0),min:t.reduce((r,n)=>r+n.planning.timeSeconds.min,0)},tokens:{cacheableInput:t.reduce((r,n)=>r+n.planning.tokens.cacheableInput,0),input:t.reduce((r,n)=>r+n.planning.tokens.input,0),output:t.reduce((r,n)=>r+n.planning.tokens.output,0)}}}function $x(e){return{chapters:e.chapters,concurrent:e.concurrent,includesPrerequisites:e.includesPrerequisites,performanceHints:e.performanceHints,steps:e.steps.map(t=>({chapters:t.chapters,prerequisite:t.prerequisite,task:t.task,tokens:t.planning.tokens,waitSeconds:t.planning.timeSeconds,words:t.words})),target:e.target,tokens:e.planning.tokens,waitSeconds:e.planning.timeSeconds,words:e.words,model:e.planning.model}}function Bx(e){return["Estimate:",` Work: ${e.target} over ${e.chapters} chapter${e.chapters===1?"":"s"} / ${e.words} words`,...e.includesPrerequisites?[" Includes prerequisite Reading Graph work where missing."]:[],` Model: ${e.planning.model}`,` Tokens: ${e.planning.tokens.input} input / ${e.planning.tokens.cacheableInput} cacheable input / ${e.planning.tokens.output} output`,` Wait: ${Gs(e.planning.timeSeconds.min)}-${Gs(e.planning.timeSeconds.max)}`,` Current concurrency: job=${e.concurrent.job} request=${e.concurrent.request}`,...tie(e.performanceHints)]}function tie(e){return e.length===0?[]:["Performance hints:",...e.flatMap(t=>[` ${t.message}`,` Command: ${t.command}`])]}var jg=l(()=>{"use strict";vn()});async function F$(e,t){if(t.json){await S(L({items:e.map(Dg)}));return}if(e.length===0){await S(`No jobs.
|
|
2967
|
+
`);return}await S(`${rie()}
|
|
2968
|
+
${e.map(r=>`${r.jobId.slice(0,8).padEnd(8)} ${r.state.padEnd(9)} ${(r.currentStep??"-").padEnd(7)} ${r.target.padEnd(7)} ${r.chapterId.toString().padStart(7)} ${aie(r.archivePath)}`).join(`
|
|
2969
|
+
`)}
|
|
2970
|
+
`)}function rie(){return`${"JOB".padEnd(8)} ${"STATE".padEnd(9)} ${"STEP".padEnd(7)} ${"TARGET".padEnd(7)} ${"CHAPTER".padStart(7)} ARCHIVE`}async function G$(e,t){if(t.json){await S(L(Dg(e)));return}await S([`Job: ${e.jobId}`,`State: ${e.state}`,`Archive: ${e.archivePath}`,`Chapter: ${e.chapterId}`,`Target: ${e.target}`,`Step: ${e.currentStep??"-"}`,`Workspace: ${e.workspacePath}`,`Cache: ${e.cachePath}`,`Logs: ${e.logPath}`,...e.errorJSON===void 0?[]:[`Error: ${e.errorJSON}`]].join(`
|
|
2971
|
+
`)+`
|
|
2972
|
+
`)}function Dg(e){return{archiveKey:e.archiveKey,archivePath:e.archivePath,cachePath:e.cachePath,chapterId:e.chapterId,createdAt:e.createdAt,...e.currentStep===void 0?{}:{currentStep:e.currentStep},...e.errorJSON===void 0?{}:{errorJSON:e.errorJSON},eventsPath:e.eventsPath,...e.finishedAt===void 0?{}:{finishedAt:e.finishedAt},jobId:e.jobId,logPath:e.logPath,...e.llmJSON===void 0?{}:{llm:nie(e.llmJSON)},...e.ownerId===void 0?{}:{ownerId:e.ownerId},...e.ownerPid===void 0?{}:{ownerPid:e.ownerPid},...e.prompt===void 0?{}:{prompt:e.prompt},queueRank:e.queueRank,state:e.state,...e.readingSummaryStartedAt===void 0?{}:{readingSummaryStartedAt:e.readingSummaryStartedAt},target:e.target,updatedAt:e.updatedAt,workspacePath:e.workspacePath}}function nie(e){let t;try{t=JSON.parse(e)}catch{return{configured:!0,invalid:!0}}let r=iie(t);return{configured:!0,...Or(r,"provider")===void 0?{}:{provider:Or(r,"provider")},...Or(r,"model")===void 0?{}:{model:Or(r,"model")},...Or(r,"name")===void 0?{}:{name:Or(r,"name")},hasApiKey:Or(r,"apiKey")!==void 0,hasBaseURL:Or(r,"baseURL")!==void 0||Or(r,"baseUrl")!==void 0||Or(r,"chatCompletionsUrl")!==void 0}}function iie(e){if(!W$(e))return{};let t=e.llm;return W$(t)?t:e}function Or(e,t){let r=e[t];return typeof r=="string"&&r!==""?r:void 0}function W$(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)}async function Wa(e,t={json:!1}){if(t.json){await S(L({...Dg(e),...t.estimate===void 0?{}:{estimate:$x(t.estimate)},...t.watch===!0?{watchCommand:fe([`wikg://local/job/${e.jobId}`,"watch"])}:{}}));return}await S([`Job ${e.jobId} ${e.state} ${e.target} chapter ${e.chapterId} ${e.archivePath}`,...t.watch===!0?[`Watch: ${fe([`wikg://local/job/${e.jobId}`,"watch"])}`]:[],...t.estimate===void 0?[]:["",...Bx(t.estimate)],""].join(`
|
|
2973
|
+
`))}async function U$(e){if(e.json){await S(L({created:e.created.map(r=>Dg(r.job)),...e.estimate===void 0?{}:{estimate:$x(e.estimate)},skipped:e.skipped}));return}let t=[`Created: ${e.created.length}`,`Skipped: ${e.skipped.length}`];for(let r of e.created)t.push(`Job ${r.job.jobId} ${r.job.state} ${r.job.target} chapter ${r.job.chapterId}`);for(let r of e.skipped)t.push(`Skipped chapter ${r.chapterId}: ${r.reason}`);e.estimate!==void 0&&t.push("",...Bx(e.estimate)),await S(`${t.join(`
|
|
2974
|
+
`)}
|
|
2975
|
+
`)}function aie(e){return e.split(/[\\/]/u).at(-1)??e}var Hx=l(()=>{"use strict";T();jg()});async function qx(e,t){return await ku({archivePath:e.archivePath,boost:e.boost??!1,chapterId:t,...e.llmJSON===void 0?{}:{llmJSON:e.llmJSON},...e.prompt===void 0?{}:{prompt:e.prompt},target:e.target??"reading-summary"})}async function Kx(e,t){let r=[],n=[];await new G(e.archivePath).readDocument(async i=>{let a=e.chapterIds===void 0?void 0:new Set(e.chapterIds);for(let o of await z(i))if(!(a!==void 0&&!a.has(o.chapterId))){if(o.stage==="planned"){n.push({chapterId:o.chapterId,reason:"planned"});continue}try{r.push({chapter:o,job:await qx(e,o.chapterId)})}catch(s){n.push({chapterId:o.chapterId,reason:s instanceof Error?s.message:String(s)})}}}),await U$({created:r,...r.length===0?{}:{estimate:Ng({chapters:r.map(i=>i.chapter),config:t,target:e.target??"reading-summary"})},json:e.json??!1,skipped:n})}async function $$(e,t){let r;if(await new G(e.archivePath).readDocument(async i=>{r=(await z(i)).find(a=>a.chapterId===t)}),r===void 0)throw new Error("Chapter does not exist.");let n=r.uri;await new G(e.archivePath).read(async i=>{if(await i.readChapterStage(t)==="planned")throw new Error(`Chapter ${n} is planned. Set source before queueing a build job.`)})}async function B$(e,t){let r;if(await new G(e.archivePath).readDocument(async n=>{r=(await z(n)).find(i=>i.chapterId===t)}),r===void 0)throw new Error(`Chapter ${t} does not exist.`);return r}function H$(e){if(e.acceptCost!==!0)throw new Error("Generation tasks can call an LLM, consume tokens, incur provider charges, and run for minutes to hours on large archives. Run `wg <archive-uri> inspect`, then rerun `wg wikg://local/job add` with --accept-cost if the cost and wait time are acceptable.")}function Ol(){if(!dG())return;Wx("queue-worker",{detached:!0}).unref()}var q$=l(()=>{"use strict";O();O();yi();Fx();jg();Hx()});async function K$(e,t){let r=0,n=new xn({jsonl:t.jsonl,throttleMs:sie});if(t.from==="now"){let i=await Ge(e);r=(await la(i)).at(-1)?.seq??0}for(;;){let i=await Ge(e),a=(await la(i)).filter(o=>o.seq>r);for(let o of a)r=Math.max(r,o.seq),await n.write(cie(o));if(oie.has(i.state))return;await hie(1e3)}}function cie(e){switch(e.type){case"status_snapshot":{let t=mie(e.tokens);return{counters:e.counters.map(lie),json:e,kind:"status",...t===void 0?{}:{metricGroups:[t]},phase:e.phase??die(e.step)}}case"target_changed":return{json:e,kind:"lifecycle",text:`target ${e.from} -> ${e.to}`};case"step_started":return{json:e,kind:"lifecycle",text:`${e.step} started
|
|
2976
|
+
steps: ${uie(e.step)}`};case"step_completed":return{json:e,kind:"lifecycle",text:`${e.step} completed`};case"created":return{json:e,kind:"lifecycle",text:"created"};default:return{json:e,kind:"lifecycle",text:e.type}}}function die(e){switch(e){case"reading-graph":return"extracting";case"reading-summary":return"summarizing";case"knowledge-graph":return"knowledge-graph";case void 0:return"status";default:return"status"}}function uie(e){switch(e){case"knowledge-graph":return"matching -> screening -> enrichment -> grounding -> relation-discovery -> committing";case"reading-summary":return"reading-graph -> summarizing -> committing";case"reading-graph":return"extracting -> committing";default:return e}}function lie(e){return{done:e.done,name:e.name,total:e.total,unit:pie(e.unit)}}function pie(e){switch(e){case"candidate":return"candidates";case"char":return"chars";case"item":return"items";case"page":return"pages";case"qid":return"qids";case"record":return"records";case"sentence":return"sentences";case"word":return"words";case"window":return"windows";default:return e}}function mie(e){if(e===void 0)return;let t=[...e.inputTokens===void 0?[]:[{name:"input",value:e.inputTokens}],...e.cacheReadTokens===void 0?[]:[{name:"cache",value:e.cacheReadTokens}],...e.outputTokens===void 0?[]:[{name:"output",value:e.outputTokens}]];return t.length===0?void 0:{metrics:t,name:"tokens"}}async function hie(e){await new Promise(t=>{setTimeout(t,e)})}var oie,sie,J$=l(()=>{"use strict";O();vn();oie=new Set(["succeeded","failed","canceled"]),sie=6e3});async function z$(){let e=await Ei();await _u({concurrency:e.concurrent?.job??3,executeJob:async(t,r,n)=>{await fie(t,r,n)}})}async function fie(e,t,r){await rc({logDirPath:e.logPath,operation:"build-job"},async()=>{await gie(e,t,r)})}async function gie(e,t,r){let n=await dg({...e.llmJSON===void 0?{}:{llmJSON:e.llmJSON}}),i=a0(n,{cacheDirPath:e.cachePath,logDirPath:e.logPath,onStreamProgress:async b=>{await t.addOutputCharacters(b.outputCharacters)},onTokenUsage:async b=>{await t.addTokenUsage({...b.cacheReadTokens===void 0?{}:{cacheReadTokens:b.cacheReadTokens},...b.inputTokens===void 0?{}:{inputTokens:b.inputTokens},...b.outputTokens===void 0?{}:{outputTokens:b.outputTokens}})}}),a=e.prompt??n.prompt,o=et(a),s=ua(a),c=async(b,E,N)=>await i.request(b,{retryIndex:E,retryMax:N,scope:"serial-generation/reader-extraction",signal:r.signal});c.lazy=async b=>await i.request(async()=>await b(c));let d=await new G(e.archivePath).readDocument(async b=>await Ss(b,e.chapterId)),{details:u}=d,{sourceText:p}=d;if(await hs({currentRevision:d.revision,jobId:e.jobId,ownerId:Jx(e)}),await t.setTotals({totalGraphWords:u.stage==="sourced"?u.words:0,totalReadingSummaryWords:u.stage==="sourced"||u.stage==="graphed"?u.words:0}),u.stage==="planned")throw new Error(`Chapter ${e.chapterId} is planned. Set source before queueing a build job.`);if(e.target==="knowledge-graph"){let b=zx(n);await t.stepStarted("knowledge-graph");let E=await new G(e.archivePath).readDocument(async C=>(await Nl(e,C),await Gu(C,e.chapterId))),N=await mf(e.chapterId,E,{policyPrompt:s,progressTracker:t,request:c,resolverOptions:{logDirPath:e.logPath,normalizer:Nm({request:c})},wikispine:b,workspacePath:e.workspacePath});await t.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),await new G(e.archivePath).write(async C=>{Ys(await Ge(e.jobId)),await Nl(e,C),await rf(C,N)}),await t.updatePhase({done:1,phase:"committing",total:1,unit:"item"}),await t.stepCompleted("knowledge-graph"),Ys(await Ge(e.jobId));return}if(u.stage==="sourced"){let b=0;await t.stepStarted("reading-graph");let E=await Dh(e.chapterId,{extractionPrompt:o,llm:i,sourceText:p,workspacePath:e.workspacePath,progressTracker:{async advance(C){b+=C,await t.updateWords({graphWords:b})},async complete(C){await t.updateWords({graphWords:C??u.words})}}});await t.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),u=await new G(e.archivePath).write(async C=>(Ys(await Ge(e.jobId)),await Nl(e,C),await Mh(C,E))),await t.updatePhase({done:1,phase:"committing",total:1,unit:"item"});let N=await new G(e.archivePath).readDocument(async C=>await Ss(C,e.chapterId));u=N.details,await hs({currentRevision:N.revision,jobId:e.jobId,ownerId:Jx(e)}),await t.updateWords({graphWords:u.words}),await t.stepCompleted("reading-graph")}let m=await Ge(e.jobId);if(Ys(m),m.target==="reading-graph"||u.stage==="summarized")return;if(u.stage!=="graphed"&&({details:u}=await new G(e.archivePath).readDocument(async b=>await Ss(b,e.chapterId))),u.stage!=="graphed")throw new Error(`Chapter ${e.chapterId} is ${u.stage}. Cannot generate summary.`);await t.stepStarted("reading-summary");let f=await new G(e.archivePath).readDocument(async b=>(await Nl(e,b),await Zh(b,e.chapterId,e.workspacePath))),w=await Xh(e.chapterId,{llm:i,snapshotPath:f.filePath,workspacePath:e.workspacePath});await t.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),u=await new G(e.archivePath).write(async b=>(Ys(await Ge(e.jobId)),await Nl(e,b),await Yh(b,e.chapterId,w))),await t.updatePhase({done:1,phase:"committing",total:1,unit:"item"}),await t.updateWords({readingSummaryWords:u.words}),await t.stepCompleted("reading-summary"),Ys(await Ge(e.jobId))}function zx(e){if(e.wikispine?.provider!==void 0)return e.wikispine;throw new Error(h(["Knowledge Graph requires WikiSpine.","Configure `wikg://local/config/wikispine` with provider `cli` or `fetch`, then run `wg wikg://local/config/wikispine test`."].join(" "),_.config))}function Ys(e){if(e.state!=="running")throw new Error(`Job ${e.jobId} is ${e.state}. Stop before flushing.`)}async function Nl(e,t){await Eu({currentRevision:await t.serials.getRevision(e.chapterId),jobId:e.jobId,ownerId:Jx(e)})}function Jx(e){if(e.ownerId===void 0)throw new Error(`Job ${e.jobId} is not owned by this worker.`);return e.ownerId}var Qx=l(()=>{"use strict";O();O();O();O();Ws();vn();T()});var Q$={};Qe(Q$,{runQueueCommand:()=>yie,runQueueWorker:()=>z$});async function yie(e){switch(e.action){case"add":{let t=await bie(e),r=t?.length===1?t[0]:void 0;r!==void 0&&await $$(e,r),H$(e);let n=await dg({...e.llmJSON===void 0?{}:{llmJSON:e.llmJSON}});if(e.target==="knowledge-graph"&&zx(n),t===void 0)await Kx(e,n);else if(t.length===1){let i=t[0],a=await B$(e,i),o=Ng({chapters:[a],config:n,target:e.target??"reading-summary"});await Wa(await qx(e,i),{estimate:o,json:e.json??!1,watch:!0})}else await Kx({...e,chapterIds:t},n);Ol();return}case"list":await F$(await Iu({...e.activeOnly===void 0?{}:{activeOnly:e.activeOnly},...e.all===void 0?{}:{all:e.all},...e.archivePath===void 0?{}:{archivePath:e.archivePath}}),{json:e.json??!1});return;case"status":await G$(await Ge(await Fa(e)),{json:e.json??!1});return;case"watch":await K$(await Fa(e),{from:e.from??"beginning",jsonl:e.jsonl??!process.stdout.isTTY});return;case"pause":await Wa(await vu(await Fa(e)));return;case"resume":await Wa(await Cu(await Fa(e))),Ol();return;case"cancel":await Wa(await Tu(await Fa(e)));return;case"boost":await Wa(await Ru(await Fa(e))),Ol();return;case"target":await Wa(await Pu(await Fa(e),e.target??"reading-summary")),Ol();return;case"clean":await S(`Cleaned ${await Lu()} jobs.
|
|
2977
|
+
`);return}}async function Fa(e){return await xu(e.jobId)}async function bie(e){if(e.chapterId!==void 0)return[e.chapterId];if(e.chapterPath===void 0&&e.depth===void 0)return;let t;return await new G(e.archivePath).readDocument(async r=>{if(e.chapterPath===void 0){t=(await Qs(r,{archivePath:Le(e.archivePath,"wikg://chapter"),...e.depth===void 0?{}:{depth:e.depth}}))?.chapterIds;return}t=(await Qs(r,{archivePath:qr(e.archivePath,e.chapterPath),...e.depth===void 0?{}:{depth:e.depth}}))?.chapterIds??[await xs(r,e.chapterPath)]}),t}var V$=l(()=>{"use strict";O();vn();T();q$();jg();Hx();J$();Qx();Ox();Qx()});O();import{Readable as xie,Writable as fHe}from"stream";O();O();O();T();T();T();O();function PG(e){if(e.startsWith("wikg://triple/"))return Bf(e.slice(7))}function Bf(e){let t=e.split("/");if(t[0]!=="triple"||t.length<2||t.length>4)return;let[r="_",n="_",i="_"]=t.slice(1),a=t.slice(1).includes("_"),o=t.length<4;if(!(!a&&!o)&&!(!RG(r)||!G7(n)||!RG(i)))return{...i==="_"?{}:{objectQid:i},...n==="_"?{}:{predicate:decodeURIComponent(n)},...r==="_"?{}:{subjectQid:r}}}function sl(e){if(e==="triple"||/^chapter\/[1-9][0-9]*\/triple$/u.test(e))return!0;let t=e.startsWith("chapter/")?/^chapter\/[1-9][0-9]*\/(.+)$/u.exec(e)?.[1]:e;return t===void 0?!1:Bf(t)!==void 0}function RG(e){return e==="_"||/^Q[1-9][0-9]*$/u.test(e)}function G7(e){return e==="_"||e!==""&&!e.includes("/")}function _G(e){switch(e){case"wikg://chapter":return"chapter";case"wikg://chunk":return"chunk";case"wikg://entity":return"entity";case"wikg://triple":return"triple";default:return}}function Tr(e){if(e==="wikg://chapter")return{kind:"collection"};if(e==="wikg://chapter/tree")return{kind:"tree"};let t=_G(e);if(t!==void 0)return{kind:"lens",lens:t};let r=PG(e);if(r!==void 0)return{kind:"triple-pattern-lens",pattern:r};let n=/^wikg:\/\/chapter\/(.+)$/u.exec(e);if(n?.[1]===void 0)return;let i=U7(n[1]);if(i===void 0)return;let{chapterPath:a,suffix:o}=i,s=o===void 0?void 0:Bf(o);if(s!==void 0)return{chapterPath:a,kind:"chapter-triple-pattern-lens",pattern:s};let c=$7(o);if(c!==void 0)return{chapterPath:a,kind:"chapter-state",...c==="all"?{}:{target:c}};let d=B7(o);if(!(o!==void 0&&d===void 0))return d===void 0?{chapterPath:a,kind:"chapter"}:d==="state"?{chapterPath:a,kind:"chapter-state"}:d==="chunk"||d==="entity"||d==="triple"?{chapterPath:a,kind:"chapter-lens",lens:d}:d==="source"||d==="summary"?{chapterPath:a,kind:"chapter-resource",resource:d}:{chapterPath:a,kind:"chapter-resource",resource:d}}function U7(e){let r=e.replace(/#.*$/u,"").replace(/\/+$/u,"").split("/"),n=r.findIndex(s=>["chunk","entity","source","state","summary","title","triple"].includes(s)),i=n===-1?r:r.slice(0,n),a=n===-1?[]:r.slice(n);return{chapterPath:Oi(i.join("/"),"chapter URI"),...a.length===0?{}:{suffix:a.join("/")}}}function $7(e){if(e==="state")return"all";switch(e){case"state/source":return"source";case"state/reading-graph":return"reading-graph";case"state/reading-summary":return"reading-summary";case"state/knowledge-graph":return"knowledge-graph";default:return}}function B7(e){switch(e){case void 0:case"chunk":case"entity":case"source":case"state":case"summary":case"title":case"triple":return e;default:return}}var AG=["format","config","runtime","uri","recipe","readiness","library"],NG=[{name:"archive-scope",predicates:["create","export","inspect"]},{name:"chapter-collection-scope",predicates:["add"]},{name:"chapter-scope",predicates:["move","remove","reset"]},{name:"chapter-source-range-object",predicates:[]},{name:"chapter-source-object",predicates:["set"]},{name:"chapter-summary-range-object",predicates:[]},{name:"chapter-summary-object",predicates:["set"]},{name:"chapter-title-object",predicates:["clear","set"]},{name:"chapter-tree-object",predicates:["set"]},{name:"chapter-state-object",predicates:[]},{name:"chunk-scope",predicates:[]},{name:"chunk-object",predicates:["evidence","pack","related"]},{name:"cover-object",predicates:[]},{name:"entity-scope",predicates:[]},{name:"entity-object",predicates:["evidence","pack","related"]},{name:"entity-wikipage-object",predicates:[]},{name:"index-object",predicates:["enable","disable","embed","external"]},{name:"job-collection-scope",predicates:["add","clean"]},{name:"job-object",predicates:["boost","cancel","pause","resume","watch"]},{name:"job-target-object",predicates:["set"]},{name:"local-config-namespace",predicates:[]},{name:"local-config-section",predicates:["clear","delete","put","set","test"]},{name:"metadata-object",predicates:["clear","delete","put","set"]},{name:"summary-object",predicates:[]},{name:"triple-scope",predicates:[]},{name:"triple-object",predicates:["evidence"]}],H7=new Map(NG.map(e=>[e.name,e]));var q7=[{name:"format",summary:"Supported formats, inference rules, and IO constraints."},{name:"config",summary:"Configuration overview, precedence, and when each layer applies."},{name:"runtime",summary:"Advanced runtime, worker, cache, log, JSONL, and debug behavior."},{name:"uri",summary:"URI grammar, command routing, scopes, objects, and retrieval strategy."},{name:"recipe",summary:"Recommended workflow and best practices after root help."},{name:"readiness",summary:"Search index, LLM, WikiSpine, and generated-data prerequisites."},{name:"library",summary:"Library registries, archive memberships, path binding, and aggregate indexes."}],K7=[{name:"cover",summary:"Write raw cover bytes to stdout for redirection or piping."},{name:"meta",summary:"Read or edit metadata attached to an object."},{name:"chapter",summary:"Edit the chapter tree and per-chapter digest stages."}],J7={format:"help/topics/format",config:"help/topics/config",runtime:"help/topics/runtime",uri:"help/topics/uri",recipe:"help/topics/recipe",readiness:"help/topics/readiness",library:"help/topics/library"},LG;function Os(){return yt("help/commands/root")}function jG(){return yt("help/commands/transform")}function DG(){return yt("help/commands/gc")}function RI(e){return yt(e===void 0?"help/commands/legacy":`help/commands/legacy/${e}`)}function PI(e){return yt(e===void 0?"help/commands/maintenance":`help/commands/maintenance/${e}`)}function Hf(e){return yt(`help/commands/archive/${e}`)}function MG(e){return yt(J7[e])}function er(e,t){return yt("help/commands/uri",{libraryContext:GG(t),target:_I(e),uriContext:UG(t),uri:t})}function kn(e,t,r){let n=_I(e);if(!n.predicates.includes(t))throw new Error(h(`The URI target ${r} does not support \`${t}\`.`,fe([r,"--help"])));return yt("help/commands/predicate",{libraryContext:GG(r),predicate:t,target:n,uriContext:UG(r),uri:r})}function cl(e,t){return yt("help/commands/library",{target:t,uri:WG(e,t)})}function qf(e,t,r){return yt("help/commands/library-predicate",{predicate:r,target:t,uri:WG(e,t)})}function WG(e,t){let r=fp(t.publicId);switch(t.kind){case"archive":return t.archivePublicId===void 0?e.replace(/\/+$/u,""):TI(TI(`${r}/arc`,t.archivePublicId),OG(t.objectUri));case"archive-collection":return`${r}/arc`;case"archive-path":return t.archivePublicId===void 0?`${r}/arc/path`:`${r}/arc/${t.archivePublicId}/path`;case"archive-tree":return`${r}/arc/tree`;case"metadata":return`${r}/meta`;case"path":return`${r}/path`;case"registry":return"wikg://lib/registry";case"scope":return TI(r,OG(t.objectUri));default:throw new Error("Internal error: unknown library help target.")}}function TI(e,t){return t===void 0||t===""?e:`${e.replace(/\/+$/u,"")}/${t.replace(/^\/+|\/+$/gu,"")}`}function OG(e){return e?.replace(/^wikg:\/\//u,"")}function wi(e,t){return _I(e).predicates.includes(t)}function dl(e){return yt(`help/commands/maintenance/${e}`)}function Kf(e){return yt(`help/commands/maintenance/chapter/${e}`)}function FG(e){let t=e.trim().toLowerCase();if(AG.includes(t))return t;throw new Error(h(`Invalid help topic: ${e}. Expected one of ${AG.join(", ")}.`,_.root))}function _I(e){let t=H7.get(e);if(t===void 0)throw new Error(`Internal error: unknown URI help target ${e}.`);return t}function GG(e){let t=(()=>{try{return de(e)}catch{return}})();return t===void 0?{isArchiveShortcut:!1,isLibraryUri:!1,isLibraryWide:!1}:{isArchiveShortcut:t.kind==="archive",isLibraryUri:!0,isLibraryWide:t.kind==="scope"&&t.objectUri!==void 0}}function UG(e){let t=z7(e),r=t===void 0?void 0:Tr(t);return{isChapterQualified:r?.kind==="chapter-lens"||r?.kind==="chapter-triple-pattern-lens"}}function z7(e){try{let t=de(e);if(t?.objectUri!==void 0)return t.objectUri}catch{}try{return B(e).objectUri}catch{return}}function yt(e,t={}){return Q7().render(e,{formats:Gf,commandName:"wg",fullCommandName:Xg,helpTopics:q7,archiveMaintenanceCommands:K7,uriHelpTargets:NG,...t})}function Q7(){return LG??=Ri(Mr(),{autoescape:!1}),LG}T();T();O();T();function ul(e){return e==="add"||e==="list"||e==="move"||e==="remove"||e==="reset"||e==="set-source"||e==="set-summary"||e==="set-title"||e==="tree"}function AI(e){return e==="chapter"||e==="cover"||e==="meta"}O();T();T();ll();O();function QG(e,t){if(!(e==="create"||e==="export"||e==="inspect"||e==="next")){if(Rr(t)){if(B(t).archivePath===void 0)throw new Error(va(t));return}if(eU(t))throw new Error(tU(t))}}function VG(e){if(e===void 0||!Rr(e))return;let t=B(e).objectUri,r=t===void 0?void 0:/^wikg:\/\/chapter\/([1-9][0-9]*)$/u.exec(t);return r?.[1]===void 0?void 0:Number(r[1])}function XG(e,t){let r=B(e);if(r.archivePath===void 0)throw new Error(h(va(e),t));if(r.objectUri===void 0)throw new Error(h(zG(e),_.uri));if(!iee(r.objectUri))throw new Error(h(zG(e),t))}function YG(e,t){let r=B(e);if(r.archivePath===void 0)throw new Error(h(va(e),t));if(r.objectUri===void 0)throw new Error(h(`Related requires a chunk or entity URI: ${e}`,_.uri));let n=aee(r.objectUri);if(n===void 0)throw new Error(h(`Related is only available for chunk and entity objects: ${e}`,t));return n}function ZG(e,t){let r=B(e);if(r.archivePath===void 0)throw new Error(h(va(e),t));if(r.objectUri===void 0||!nee(r.objectUri))throw new Error(h(`Evidence is only available for chunk, entity, and triple objects: ${e}`,t))}function nee(e){let t=Lt(e),r=t.startsWith("chapter/")?/^chapter\/[1-9][0-9]*\/(.+)$/u.exec(t)?.[1]:t;if(r===void 0)return!1;let n=r.split("/");return n[0]==="chunk"&&n.length===2||n[0]==="entity"&&n.length===2||n[0]==="triple"&&n.length===4}function iee(e){return/^wikg:\/\/chunk\/[1-9][0-9]*$/u.test(e)||/^wikg:\/\/chapter\/[1-9][0-9]*\/chunk\/[1-9][0-9]*$/u.test(e)||/^wikg:\/\/entity\/[^/]+$/u.test(e)||/^wikg:\/\/chapter\/[1-9][0-9]*\/entity\/[^/]+$/u.test(e)}function aee(e){if(/^wikg:\/\/chunk\/[1-9][0-9]*$/u.test(e)||/^wikg:\/\/chapter\/[1-9][0-9]*\/chunk\/[1-9][0-9]*$/u.test(e))return"chunk";if(/^wikg:\/\/entity\/[^/]+$/u.test(e)||/^wikg:\/\/chapter\/[1-9][0-9]*\/entity\/[^/]+$/u.test(e))return"entity"}function jI(e){return eU(e)?tU(e):h(`Unknown command: ${e}.`,_.command)}function eU(e){let t=rU(e);return t.endsWith(".wikg")||t.includes(".wikg/")||t.includes(".wikg#")}function tU(e){let t=rU(e),[r=t,n=""]=oee(t),i=r.startsWith("/")?`wikg://${r}${n}`:`wikg://${r.replace(/^\.\/+/u,"")}${n}`;return[`Expected a Wiki Graph URI, not a filesystem path: ${e}`,`Use: ${i}`,"See: wg help uri"].join(`
|
|
2978
|
+
`)}function oee(e){let t=e.indexOf(".wikg")+5;return[e.slice(0,t),e.slice(t)]}function rU(e){return e.replace(/\\/gu,"/")}function va(e){return[`Expected a located Wiki Graph URI with a .wikg archive locator: ${e}`,"Short object URIs from output are archive-relative handles.","Example: wikg://book.wikg/entity/Q9957","See: wg help uri"].join(`
|
|
2979
|
+
`)}function zG(e){return[`Pack requires a graph object URI: ${e}`,"Supported pack targets are chunk and entity objects.","Use `wg <uri> --help` to inspect valid predicates."].join(`
|
|
2980
|
+
`)}function nU(e){switch(e){case"create":return"Missing archive URI. Use `wg wikg://<archive.wikg> create`.";case"export":return"Missing archive URI. Use `wg wikg://<archive.wikg> export --output-format <format>`.";case"inspect":return"Missing archive URI. Use `wg wikg://<archive.wikg> inspect`.";case"search":return"Missing scope URI with .wikg locator. Use `wg wikg://<archive.wikg> --query <query>`.";case"list":return"Missing scope URI with .wikg locator. Use `wg wikg://<archive.wikg>`.";case"get":return"Missing object URI with .wikg locator. Use `wg wikg://<archive.wikg>/<object>`.";case"related":case"evidence":case"pack":return`Missing object URI. Use \`wg wikg://<archive.wikg>/<object> ${e}\`.`;case"next":return"Missing continuation cursor. Use `wg next <cursor>`."}}function Rr(e){return e?.startsWith(ae)===!0}function Lt(e){let t=see(e);if(t===void 0)throw new Error(`Expected Wiki Graph object URI: ${e}`);return e.slice(t.length).replace(/^\/+|\/+$/gu,"")}function Qf(e){return cee(e)}function pl(e){let t=Yf(e);return t?.[0]==="local"&&t[1]==="config"&&t.length>=2}function Vf(e){let t=Yf(e);if(t?.[0]!=="local"||t[1]!=="config")return;let r=t[2];return qG(r)}function Xf(e){let t=aU(e);if(t===void 0)throw new Error(h(`Expected a Wiki Graph job URI: ${e}`,"wg wikg://local/job --help"));let r=oU(t).trim();if(r!=="")return r}function iU(e){if(e===void 0)return;let t=aU(e);if(t===void 0||!t.endsWith("/target"))return;let r=oU(t.slice(0,-7)).trim();if(r==="")throw new Error(h(`Expected a job id before /target: ${e}`,"wg wikg://local/job/<job-id>/target set --help"));return r}function see(e){if(e.startsWith(ae))return ae}function cee(e){let t=Yf(e);return t?.[0]==="local"&&t[1]==="job"}function aU(e){let t=Yf(e);if(!(t?.[0]!=="local"||t[1]!=="job"))return t.length===2?"":`/${t.slice(2).join("/")}`}function oU(e){return e.replace(/^\/+/u,"")}function Yf(e){if(e!==void 0)try{let t=dt(e);return t.protocol==="wikg"?t.path:void 0}catch{return}}function sU(e,t){let r=B(e);if(r.archivePath===void 0||r.objectUri!==void 0)throw new Error(h(`Expected an archive Wiki Graph URI: ${e}`,t));return r.archivePath}function ml(e,t,r,n){let i=e.trim();try{if(!Rr(i))return Oi(i,t);let a=B(i);if(a.archivePath!==void 0&&a.archivePath!==r)throw new Error(`Invalid ${t}: ${e}. Chapter URI belongs to a different archive.`);let o=mc(a.objectUri??i);if(o===void 0)throw new Error(`Invalid ${t}: ${e}. Expected a chapter path or URI.`);return o}catch(a){throw new Error(h(a instanceof Error?a.message:String(a),n))}}T();T();function DI(e,t,r){let n=dU(e);if(n!==void 0)return n;throw new Error(h(`Invalid ${t}: ${e}. Expected planned, source, reading-graph, or reading-summary.`,r))}function cU(e,t){let r=dU(e);if(r==="planned"||r==="sourced"||r==="graphed")return r;throw r!==void 0?new Error(h("`chapter reset` does not support --to reading-summary.",t)):new Error(h(`Invalid --to: ${e}. Expected planned, source, or reading-graph.`,t))}function dU(e){switch(e.trim().toLowerCase()){case"planned":return"planned";case"source":return"sourced";case"reading-graph":return"graphed";case"reading-summary":return"summarized";default:return}}T();function I(e,t,r,n){if(e!==void 0)throw new Error(h(`The \`chapter ${r}\` action does not support ${t}.`,n))}function j(e,t,r,n){if(e!==void 0)throw new Error(h(`The \`chapter ${r}\` action does not support ${t}.`,n))}function uU(e,t){let r=[e.parent===void 0?void 0:"--parent",e.root===void 0?void 0:"--root"].filter(i=>i!==void 0),n=[e.before===void 0?void 0:"--before",e.after===void 0?void 0:"--after",e.first===void 0?void 0:"--first",e.last===void 0?void 0:"--last"].filter(i=>i!==void 0);if(r.length>1)throw new Error(h(`Choose only one parent target: ${r.join(", ")}.`,t));if(n.length>1)throw new Error(h(`Choose only one position target: ${n.join(", ")}.`,t));if(r.length>0&&(e.before!==void 0||e.after!==void 0))throw new Error(h("Do not combine --parent or --root with --before or --after.",t));if(r.length===0&&n.length===0)throw new Error(h("`chapter move` requires --parent, --root, --before, --after, --first, or --last.",t))}function lU(e,t,r,n,i,a){let o=r.chapter===void 0?void 0:ml(r.chapter,"--chapter",t,n),s=r.parent===void 0?void 0:ml(r.parent,"--parent",t,n),c=r.before===void 0?void 0:ml(r.before,"--before",t,n),d=r.after===void 0?void 0:ml(r.after,"--after",t,n),u=r.to===void 0?void 0:cU(r.to,n);switch(e){case"set":throw new Error(h("`set` requires a concrete chapter sub-resource URI such as `/source`, `/summary`, or `/title`.",n));case"add":return I(r.chapter,"--chapter",e,n),I(r.import,"--import",e,n),I(r.prompt,"--prompt",e,n),I(r.stage,"--stage",e,n),I(r.to,"--to",e,n),j(r.recursive,"--recursive",e,n),I(r["input-format"],"--input-format",e,n),{action:e,path:t,...r.input===void 0?{}:{inputPath:r.input},...r.json===void 0?{}:{json:r.json},...r.llm===void 0?{}:{llmJSON:r.llm},...s===void 0?{}:{parentChapterPath:s},...r.title===void 0?{}:{title:r.title}};case"list":return I(r.stage,"--stage",e,n),I(r.chapter,"--chapter",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),I(r.input,"--input",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),I(r.to,"--to",e,n),I(r["json-input"],"--json",e,n),j(r.json,"--json",e,n),j(r.clear,"--clear",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),j(r.recursive,"--recursive",e,n),{action:e,...r.json===void 0?{}:{json:r.json},path:t,...r.llm===void 0?{}:{llmJSON:r.llm}};case"move":return Ns(o,e,n),I(r.stage,"--stage",e,n),I(r.input,"--input",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),I(r.to,"--to",e,n),j(r.clear,"--clear",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.recursive,"--recursive",e,n),uU(r,n),{action:e,...d===void 0?{}:{afterChapterPath:d},...c===void 0?{}:{beforeChapterPath:c},chapterPath:o,...r.first===void 0?{}:{first:r.first},...r.json===void 0?{}:{json:r.json},...r.last===void 0?{}:{last:r.last},...r.llm===void 0?{}:{llmJSON:r.llm},...r.root===void 0?{}:{moveToRoot:r.root},...s===void 0?{}:{parentChapterPath:s},path:t};case"remove":return Ns(o,e,n),I(r.stage,"--stage",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),I(r.input,"--input",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),I(r.to,"--to",e,n),j(r.clear,"--clear",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),{action:e,chapterPath:o,...r.json===void 0?{}:{json:r.json},path:t,...r.llm===void 0?{}:{llmJSON:r.llm},...r.recursive===void 0?{}:{recursive:r.recursive}};case"reset":if(Ns(o,e,n),I(r.stage,"--stage",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),u===void 0)throw new Error(h("Missing --to. `chapter reset` requires planned, source, or reading-graph.",n));return I(r.input,"--input",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),j(r.clear,"--clear",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),j(r.recursive,"--recursive",e,n),{action:e,chapterPath:o,...r.json===void 0?{}:{json:r.json},path:t,resetStage:u,...r.llm===void 0?{}:{llmJSON:r.llm}};case"set-source":return Ns(o,e,n),I(r.stage,"--stage",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),I(r.to,"--to",e,n),j(r.clear,"--clear",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),j(r.recursive,"--recursive",e,n),{action:e,chapterPath:o,...a===void 0?{}:{inputValue:a},...r.json===void 0?{}:{json:r.json},path:t,...r.input===void 0?{}:{inputPath:r.input},...r.llm===void 0?{}:{llmJSON:r.llm}};case"set-summary":return Ns(o,e,n),I(r.stage,"--stage",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),I(r.to,"--to",e,n),I(r["json-input"],"--json",e,n),j(r.json,"--json",e,n),j(r.clear,"--clear",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),j(r.recursive,"--recursive",e,n),{action:e,chapterPath:o,...a===void 0?{}:{inputValue:a},path:t,...r.input===void 0?{}:{inputPath:r.input},...r.llm===void 0?{}:{llmJSON:r.llm}};case"set-title":if(Ns(o,e,n),I(r.stage,"--stage",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),I(r.import,"--import",e,n),I(r["json-input"],"--json",e,n),j(r.json,"--json",e,n),r.title!==void 0)throw new Error(h("`chapter title set` uses a positional value instead of --title.",n));if(a===void 0&&r.clear!==!0)throw new Error(h("Missing title value.",n));if(a!==void 0&&r.clear===!0)throw new Error(h("`chapter title clear` cannot combine with a title value.",n));return I(r.input,"--input",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.to,"--to",e,n),j(r["dry-run"],"--dry-run",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),j(r.recursive,"--recursive",e,n),{action:e,chapterPath:o,...r.clear===void 0?{}:{clearTitle:r.clear},path:t,...a===void 0?{}:{title:a},...r.llm===void 0?{}:{llmJSON:r.llm}};case"tree":if(I(r.stage,"--stage",e,n),I(r.chapter,"--chapter",e,n),I(r.after,"--after",e,n),I(r.before,"--before",e,n),I(r.import,"--import",e,n),I(r["input-format"],"--input-format",e,n),I(r.parent,"--parent",e,n),I(r.prompt,"--prompt",e,n),I(r.title,"--title",e,n),I(r.to,"--to",e,n),j(r.clear,"--clear",e,n),j(r.first,"--first",e,n),j(r.root,"--root",e,n),j(r.last,"--last",e,n),j(r.recursive,"--recursive",e,n),i==="apply"&&r.json===!0)throw new Error(h("`chapter tree apply` does not support --json.",n));return i==="apply"?{action:e,...r["dry-run"]===void 0?{}:{dryRun:r["dry-run"]},...r.input===void 0?{}:{inputPath:r.input},...r.llm===void 0?{}:{llmJSON:r.llm},path:t,treeAction:"apply"}:(I(r.input,"--input",e,n),j(r["dry-run"],"--dry-run",e,n),{action:e,json:r.json??!1,...r.llm===void 0?{}:{llmJSON:r.llm},path:t,treeAction:"show"})}}function Ns(e,t,r){if(e===void 0)throw new Error(h(`Missing --chapter. chapter ${t} requires a complete absolute chapter path.`,r))}function MI(e){return e==="create"||e==="evidence"||e==="export"||e==="get"||e==="inspect"||e==="list"||e==="next"||e==="pack"||e==="related"||e==="search"}function hl(e){return e==="next"}function fl(e){return e==="get"||e==="list"||e==="search"}function Ca(e){return h("This command form is not available. Pass the URI directly, or add --query to a scope URI.",_.uri)}function pU(e){return MI(e)||ul(e)||WI(e)||FI(e)}function WI(e){return e==="disable"||e==="embed"||e==="enable"||e==="external"||e==="get"}function FI(e){return e==="clear"||e==="delete"||e==="get"||e==="put"||e==="set"}function mU(e){return e==="evidence"||e==="get"||e==="list"||e==="pack"||e==="related"||e==="search"}function hU(e){return e==="get"||e==="list"||e==="search"}function fU(e){return e==="add"||e==="boost"||e==="cancel"||e==="clean"||e==="get"||e==="list"||e==="pause"||e==="resume"||e==="set"||e==="watch"}function Zf(e){switch(e){case void 0:case"reading-summary":return"reading-summary";case"reading-graph":return"reading-graph";case"knowledge-graph":return"knowledge-graph";default:throw new Error(h(`Invalid queue task: ${e}. Expected reading-graph, reading-summary, or knowledge-graph.`,"wg wikg://local/job add --help"))}}T();O();function Ta(e,t,r){if(t!==void 0)throw new Error(h(`The \`chapter\` command does not support --${e}.`,r))}function eg(e,t){for(let r of tg(e,{includeTitle:!1}))throw new Error(h(`The \`chapter\` command does not support ${r}.`,t))}function GI(e){Ra(e,"help",_.root)}function UI(e,t){if(!(t.force!==!0||e[0]==="gc"))throw new Error(h("The --force option is only supported by `gc`.",_.root))}function $I(e,t){if(t.replace!==!0)return;let[r,n]=e;if(!(r?.startsWith(ae)===!0&&n==="create"))throw new Error(h("The --replace option is only supported by `wg <archive-uri> create`.",_.root))}function BI(e){Ra(e,"gc","wg gc --help")}function Ra(e,t,r){for(let n of tg(e))throw new Error(h(`The \`${t}\` command does not support ${n}.`,r))}function bt(e,t,r){if(!(t===void 0||t===!1))throw new Error(h(`The \`gc\` command does not support --${e}.`,r))}function Ot(e,t,r){if(t!==void 0)throw new Error(h(`The \`transform\` command does not support --${e}.`,r))}function HI(e){for(let t of tg(e))throw new Error(h(`The \`transform\` command does not support ${t}.`,"wg transform --help"))}function tg(e,t={}){let r=t.includeTitle??!0,n=[];return e.author!==void 0&&n.push("--author"),e["clear-authors"]!==void 0&&n.push("--clear-authors"),e["clear-description"]!==void 0&&n.push("--clear-description"),e["clear-identifier"]!==void 0&&n.push("--clear-identifier"),e["clear-language"]!==void 0&&n.push("--clear-language"),e["clear-published-at"]!==void 0&&n.push("--clear-published-at"),e["clear-publisher"]!==void 0&&n.push("--clear-publisher"),r&&e["clear-title"]!==void 0&&n.push("--clear-title"),e.description!==void 0&&n.push("--description"),e.identifier!==void 0&&n.push("--identifier"),e.language!==void 0&&n.push("--language"),e["published-at"]!==void 0&&n.push("--published-at"),e.publisher!==void 0&&n.push("--publisher"),r&&e.title!==void 0&&n.push("--title"),n}function wt(e,t){if(t!==void 0)throw new Error(h(`The \`help\` command does not support --${e}.`,_.root))}function gl(e,t){if(e.query!==void 0&&e.reverse===!0)throw new Error(h("`--reverse` cannot be combined with --query.",t))}function gU(e,t){if(e===void 0)return{};if(e!=="any"&&e!=="object"&&e!=="self"&&e!=="subject")throw new Error(h("--role must be one of: any, subject, object, self.",t));return{role:e}}function we(e,t,r,n){if(t.length>r)throw new Error(h(`Unexpected positional arguments for \`${e}\`: ${t.slice(r).join(" ")}.`,n))}function js(e,t,r,n){if(t.length>r)throw new Error(h(`Unexpected positional arguments for \`queue ${e}\`: ${t.slice(r).join(" ")}.`,n))}function Pa(e,t,r,n){if(t.length>r)throw new Error(h(`Unexpected positional arguments for \`${e}\`: ${t.slice(r).join(" ")}.`,n))}function ie(e,t,r){if(t!==void 0)throw new Error(h(`The \`meta\` command does not support --${e}.`,r))}function _a(e,t,r){if(t!==void 0)throw new Error(h(`The \`meta\` command does not support --${e}.`,r))}function nt(e,t,r){if(t!==void 0)throw new Error(h(`The \`cover\` command does not support --${e}.`,r))}function qI(e,t,r){if(t!==void 0)throw new Error(h(`The \`cover\` command does not support --${e}.`,r))}function yU(e){for(let t of tg(e))throw new Error(h(`The \`cover\` command does not support ${t}.`,"wg cover --help"))}function g(e,t,r,n){if(r!==void 0)throw new Error(h(`The \`${e}\` command does not support ${t}.`,n))}function A(e,t,r,n){if(r!==void 0)throw new Error(h(`The \`${e}\` command does not support ${t}.`,n))}function rg(e,t,r){if(t===!0)throw new Error(h(`The \`${e}\` command does not support --json because it streams progress events. Use --jsonl for line-delimited progress output.`,r))}function tr(e,t,r){g(e,"--input",t.input,r),g(e,"--import",t.import,r),g(e,"--input-format",t["input-format"],r),g(e,"--llm",t.llm,r),g(e,"--output",t.output,r),g(e,"--output-format",t["output-format"],r),g(e,"--prompt",t.prompt,r)}T();T();function bU(e,t){if(e!==void 0){if(e==="beginning"||e==="now")return e;throw new Error(h(`Invalid --from: ${e}. Expected beginning or now.`,t))}}function Si(e){if(e.json===!0&&e.jsonl===!0)throw new Error(h("`--json` and `--jsonl` cannot be combined.",_.command));return e.json===!0?"json":e.jsonl===!0?"jsonl":"text"}function yl(e,t){return e===void 0?{}:{evidenceLimit:In(e,"--evidence",t)}}function Ds(e,t){return e===void 0?{}:{context:In(e,"--context",t)}}function In(e,t,r){let n=Number(e);if(!Number.isInteger(n)||n<0)throw new Error(h(`${t} must be a non-negative integer.`,r));return n}function Aa(e,t,r){let n=Number(e);if(!Number.isInteger(n)||n<=0)throw new Error(h(`${t} must be a positive integer.`,r));return n}function wU(e,t){let r=[],n={...t};for(let i=0;i<e.length;i+=1){let a=e[i];if(a!==void 0)switch(a){case"--json":case"--confirm":case"--reverse":n[a.slice(2)]=!0;continue;case"--budget":case"--chapter":case"--context":case"--cursor":case"--depth":case"--import":case"--input":case"--input-format":case"--limit":case"--llm":case"--output":case"--output-format":case"--path":case"--prompt":case"--stage":case"--to":{let o=e[i+1];if(o===void 0){r.push(a);continue}n[a.slice(2)]=o,i+=1;continue}default:r.push(a)}}return{positionals:r,values:n}}function KI(e){let t=[],r=!1,n=dee(e);for(let i=0;i<e.length;i+=1){let a=e[i];if(a===void 0)continue;if(r){t.push(a);continue}if(a==="--"){t.push(a),r=!0;continue}if(a.startsWith("--json=")&&n){t.push("--json"),t.push(`--json-input=${a.slice(7)}`);continue}if(a==="--json"&&n){let s=e[i+1];t.push(a),s!==void 0&&!s.startsWith("-")&&(t.push("--json-input"),t.push(s),i+=1);continue}if(a!=="--evidence"&&a!=="--context"&&a!=="--depth"){t.push(a);continue}let o=e[i+1];if(o!==void 0&&!o.startsWith("-")){t.push(a),t.push(o),i+=1;continue}if(o!==void 0&&/^-\d/.test(o)){t.push(`${a}=${o}`),i+=1;continue}t.push(a),a==="--evidence"&&t.push("3")}return t}function dee(e){let t=e[0],r=e[1];return r!=="set"&&r!=="put"?!1:pl(t)?!0:Rr(t)&&e.some(n=>n.includes("/meta")||n.endsWith(".wikg/meta"))}function W(e,t){return fe([e,...t===void 0?[]:[t],"--help"])}function ue(e,t,r,n=`wg ${e} --help`,i={}){let a=wU(t,r);t=a.positionals,r=a.values;let o=t[0];if(r.help===!0)return{help:!0,helpText:Hf(e),kind:"help"};if(o===void 0||o==="-")throw new Error(h(nU(e),n));if(QG(e,o),r.verbose===!0)throw new Error(h(`The \`${e}\` command does not support --verbose.`,n));switch(e){case"create":{if(we(e,t,1,n),g(e,"--input",r.input,n),g(e,"--input-format",r["input-format"],n),g(e,"--output",r.output,n),g(e,"--output-format",r["output-format"],n),g(e,"--stage",r.stage,n),g(e,"--to",r.to,n),g(e,"--chapter",r.chapter,n),g(e,"--context",r.context,n),g(e,"--limit",r.limit,n),g(e,"--depth",r.depth,n),g(e,"--evidence",r.evidence,n),g(e,"--budget",r.budget,n),A(e,"--all",r.all,n),A(e,"--backlinks",r.backlinks,n),g(e,"--role",r.role,n),g(e,"--llm",r.llm,n),g(e,"--prompt",r.prompt,n),g(e,"--query",r.query,n),A(e,"--reverse",r.reverse,n),A(e,"--confirm",r.confirm,n),A(e,"--jsonl",r.jsonl,n),r.import!==void 0&&il(r.import)!=="epub")throw new Error(h("`create --import` only supports EPUB input.",n));return{args:{action:e,archivePath:o,...r.import===void 0?{}:{importPath:r.import},...r.json===void 0?{}:{json:r.json},...r.replace===void 0?{}:{replace:r.replace}},help:!1,kind:"archive"}}case"export":return we(e,t,1,n),g(e,"--import",r.import,n),g(e,"--input",r.input,n),g(e,"--input-format",r["input-format"],n),g(e,"--prompt",r.prompt,n),g(e,"--stage",r.stage,n),g(e,"--to",r.to,n),g(e,"--chapter",r.chapter,n),g(e,"--context",r.context,n),g(e,"--limit",r.limit,n),g(e,"--depth",r.depth,n),g(e,"--evidence",r.evidence,n),g(e,"--budget",r.budget,n),A(e,"--all",r.all,n),A(e,"--backlinks",r.backlinks,n),g(e,"--role",r.role,n),A(e,"--confirm",r.confirm,n),A(e,"--json",r.json,n),{args:{action:e,archivePath:o,...r.output===void 0?{}:{outputPath:r.output},outputFormat:r["output-format"]===void 0?As("markdown","--output-format"):As(r["output-format"],"--output-format")},help:!1,kind:"archive"};case"inspect":{let s=VG(t[0]),c=B(o).archivePath??o;return we(e,t,1,n),tr(e,r,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--context",r.context,n),g(e,"--cursor",r.cursor,n),g(e,"--depth",r.depth,n),g(e,"--evidence",r.evidence,n),g(e,"--from",r.from,n),g(e,"--query",r.query,n),g(e,"--stage",r.stage,n),g(e,"--to",r.to,n),A(e,"--all",r.all,n),A(e,"--backlinks",r.backlinks,n),A(e,"--confirm",r.confirm,n),A(e,"--jsonl",r.jsonl,n),g(e,"--limit",r.limit,n),g(e,"--role",r.role,n),{args:{action:e,archivePath:c,...s===void 0?{}:{chapterId:s},...r.json===!0?{json:!0}:{}},help:!1,kind:"archive"}}case"search":{let s=r.query;if(s===void 0)throw new Error(h("Scope keyword retrieval requires --query.",n));return we(e,t,1,n),tr(e,r,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--from",r.from,n),g(e,"--role",r.role,n),A(e,"--reverse",r.reverse,n),g(e,"--to",r.to,n),A(e,"--confirm",r.confirm,n),SU(e,o,r.depth,n),{args:{action:e,...r.all===void 0?{}:{all:r.all},archivePath:o,...r.backlinks===void 0?{}:{backlinks:r.backlinks},...r.cursor===void 0?{}:{cursor:r.cursor},...r.depth===void 0?{}:{depth:In(r.depth,"--depth",n)},...Ds(r.context,n),...yl(r.evidence,n),format:Si(r),...r.limit===void 0?{}:{limit:Aa(r.limit,"--limit",n)},query:s,...i.defaultKinds===void 0?{}:{kinds:i.defaultKinds},...i.triplePattern===void 0?{}:{triplePattern:i.triplePattern}},help:!1,kind:"archive"}}case"list":return we(e,t,1,n),tr(e,r,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--from",r.from,n),g(e,"--role",r.role,n),g(e,"--query",r.query,n),gl(r,n),g(e,"--to",r.to,n),A(e,"--confirm",r.confirm,n),SU(e,o,r.depth,n),{args:{action:e,...r.all===void 0?{}:{all:r.all},archivePath:o,...r.backlinks===void 0?{}:{backlinks:r.backlinks},...r.cursor===void 0?{}:{cursor:r.cursor},...r.depth===void 0?{}:{depth:In(r.depth,"--depth",n)},...Ds(r.context,n),...yl(r.evidence,n),format:Si(r),...r.limit===void 0?{}:{limit:Aa(r.limit,"--limit",n)},...r.reverse===!0?{reverse:!0}:{},...i.defaultKinds===void 0?{}:{kinds:i.defaultKinds},...i.triplePattern===void 0?{}:{triplePattern:i.triplePattern}},help:!1,kind:"archive"};case"get":return we(e,t,1,n),tr(e,r,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--from",r.from,n),g(e,"--limit",r.limit,n),g(e,"--cursor",r.cursor,n),g(e,"--depth",r.depth,n),g(e,"--query",r.query,n),gl(r,n),g(e,"--role",r.role,n),g(e,"--to",r.to,n),A(e,"--all",r.all,n),A(e,"--confirm",r.confirm,n),{args:{action:e,archivePath:o,...r.backlinks===void 0?{}:{backlinks:r.backlinks},...Ds(r.context,n),...yl(r.evidence,n),format:Si(r),objectId:o,...r.reverse===!0?{reverse:!0}:{}},help:!1,kind:"archive"};case"related":{we(e,t,1,n),tr(e,r,n),A(e,"--backlinks",r.backlinks,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--depth",r.depth,n),g(e,"--from",r.from,n);let s=YG(o,n);return s==="chunk"&&g(e,"--role",r.role,n),gl(r,n),g(e,"--to",r.to,n),A(e,"--confirm",r.confirm,n),{args:{action:e,...r.all===void 0?{}:{all:r.all},archivePath:o,...r.cursor===void 0?{}:{cursor:r.cursor},...Ds(r.context,n),...yl(r.evidence,n),format:Si(r),...r.limit===void 0?{}:{limit:Aa(r.limit,"--limit",n)},objectId:o,...r.query===void 0?{}:{query:r.query},...r.reverse===!0?{reverse:!0}:{},...s==="entity"?gU(r.role,n):{}},help:!1,kind:"archive"}}case"evidence":return we(e,t,1,n),tr(e,r,n),A(e,"--backlinks",r.backlinks,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--depth",r.depth,n),g(e,"--from",r.from,n),g(e,"--role",r.role,n),g(e,"--to",r.to,n),g(e,"--evidence",r.evidence,n),gl(r,n),A(e,"--confirm",r.confirm,n),ZG(o,n),{args:{action:e,...r.all===void 0?{}:{all:r.all},archivePath:o,...r.cursor===void 0?{}:{cursor:r.cursor},...Ds(r.context,n),format:Si(r),...r.limit===void 0?{}:{limit:Aa(r.limit,"--limit",n)},objectId:o,...r.query===void 0?{}:{query:r.query},...r.reverse===!0?{reverse:!0}:{}},help:!1,kind:"archive"};case"pack":return we(e,t,1,n),tr(e,r,n),A(e,"--backlinks",r.backlinks,n),g(e,"--chapter",r.chapter,n),g(e,"--context",r.context,n),g(e,"--depth",r.depth,n),g(e,"--from",r.from,n),g(e,"--limit",r.limit,n),g(e,"--cursor",r.cursor,n),g(e,"--evidence",r.evidence,n),A(e,"--reverse",r.reverse,n),g(e,"--role",r.role,n),g(e,"--to",r.to,n),A(e,"--all",r.all,n),XG(o,n),{args:{action:e,archivePath:o,budget:r.budget===void 0?5e3:Aa(r.budget,"--budget",n),format:Si(r),objectId:o},help:!1,kind:"archive"};case"next":return we(e,t,2,n),tr(e,r,n),g(e,"--budget",r.budget,n),g(e,"--chapter",r.chapter,n),g(e,"--depth",r.depth,n),g(e,"--context",r.context,n),g(e,"--cursor",r.cursor,n),g(e,"--evidence",r.evidence,n),g(e,"--from",r.from,n),A(e,"--reverse",r.reverse,n),g(e,"--to",r.to,n),A(e,"--all",r.all,n),A(e,"--confirm",r.confirm,n),{args:{action:e,archivePath:o,...t[1]===void 0?{}:{cursor:t[1]},format:Si(r),...r.limit===void 0?{}:{limit:Aa(r.limit,"--limit",n)}},help:!1,kind:"archive"}}}function SU(e,t,r,n){if(r===void 0)return;let i=B(t).objectUri;i==="wikg://chapter"||i?.startsWith("wikg://chapter/")===!0||g(e,"--depth",r,n)}O();import{parseArgs as nte}from"util";T();O();T();O();T();function kU(e,t,r,n,i,a){let o=W(e,n);switch(r.kind){case"collection":return uee(e,t,n,i,a,o);case"lens":return EU(e,r.lens,n,i,a,o);case"triple-pattern-lens":return pee(e,r.pattern,n,i,a,o);case"tree":return mee(t,n,i,a,o);case"chapter":return hee(t,r.chapterPath,n,i,a,o);case"chapter-lens":return fee(t,r.chapterPath,r.lens,n,i,a,o);case"chapter-triple-pattern-lens":return gee(t,r.chapterPath,r.pattern,n,i,a,o);case"chapter-state":return yee(e,n,i,a,o);case"chapter-resource":return bee(e,t,r.chapterPath,r.resource,n,i,a,o)}}function uee(e,t,r,n,i,a){if(r==="list"||r==="search")return EU(e,"chapter",r,n,i,a);if(!lee(r))throw new Error(h(`The chapter collection does not support \`${r}\`. Read it directly, add --query, or use add.`,"wg <chapter-uri> --help"));return ng(r,t,n,i,a)}function lee(e){return e==="add"||e==="move"||e==="remove"||e==="reset"}function EU(e,t,r,n,i,a){if(r!=="list"&&r!=="search")throw new Error(h(`The ${t} collection does not support \`${r}\`. Read it directly, or add --query.`,"wg <scope-uri> --help"));return ue(r,[e,...n],i,a,{defaultKinds:[t]})}function pee(e,t,r,n,i,a){if(r!=="list"&&r!=="search")throw new Error(h(`The triple pattern collection does not support \`${r}\`. Read it directly, or add --query.`,"wg <triple-uri> --help"));return ue(r,[e,...n],i,a,{defaultKinds:["triple"],triplePattern:t})}function mee(e,t,r,n,i){if(t!=="get"&&t!=="set")throw new Error(h(`The chapter tree does not support \`${t}\`. Read it directly, or use set.`,"wg <archive-uri>/chapter/tree --help"));return ng("tree",e,r,n,i,t==="set"?"apply":void 0)}function hee(e,t,r,n,i,a){switch(r){case"search":case"list":return ue(r,[qr(e,t),...n],i,a);case"get":throw new Error(h("`chapter/<path>` is a scope URI. Use `chapter/<path>/title` or `chapter/<path>/state` to read a concrete chapter object.",_.uri));case"inspect":throw new Error(h("`chapter/<path>` inspect is not available. Inspect the archive or read concrete chapter resources instead.",_.uri));case"move":case"remove":case"reset":return ng(r,e,n,{...i,chapter:t},a);default:throw new Error(h(`The chapter object does not support \`${r}\`.`,"wg <chapter-uri> --help"))}}function fee(e,t,r,n,i,a,o){if(n!=="list"&&n!=="search")throw new Error(h(`The chapter ${r} collection does not support \`${n}\`. Read it directly, or add --query.`,"wg <scope-uri> --help"));return ue(n,[JI(e,t),...i],a,o,{defaultKinds:[r]})}function gee(e,t,r,n,i,a,o){if(n!=="list"&&n!=="search")throw new Error(h(`The chapter triple pattern collection does not support \`${n}\`. Read it directly, or add --query.`,"wg <triple-uri> --help"));return ue(n,[JI(e,t),...i],a,o,{defaultKinds:["triple"],triplePattern:r})}function yee(e,t,r,n,i){if(t!=="get")throw new Error(h(`The chapter state object does not support \`${t}\`. Read the state URI directly.`,"wg <chapter-uri>/state --help"));return ue("get",[e,...r],n,i)}function bee(e,t,r,n,i,a,o,s){let c=`wg <chapter-uri>/${n} --help`;if(i==="list"||i==="search"){if(n==="title")throw new Error(h(`The chapter ${n} resource does not support \`${i}\`.`,c));return ue(i,[n==="source"||n==="summary"?JI(t,r):qr(t,r),...a],o,s,{defaultKinds:[n]})}if(i!=="set"&&i!=="get"&&i!=="clear")throw new Error(h(`The chapter ${n} resource does not support \`${i}\`. Read it directly, or use set.`,c));if(i==="clear"&&n!=="title")throw new Error(h(`The chapter ${n} resource does not support clear.`,c));if(i==="set"&&o.clear===!0)throw new Error(h(`The chapter ${n} set command does not support --clear. Use \`clear\`.`,c));if(i==="get"){let u=e.includes("#")&&(n==="source"||n==="summary")||IU(t)?e:n==="source"?Ac(t,r):_c(t,r,n);return ue("get",[u,...a],o,s)}return ng(n==="source"?"set-source":n==="summary"?"set-summary":"set-title",t,a,{...o,chapter:r,...i==="clear"?{clear:!0}:{}},s)}function JI(e,t){return IU(e)?`${e}/chapter/${t}`:qr(e,t)}function IU(e){return e.startsWith("wikg://lib/arc/")||/^wikg:\/\/lib\/[^/]+\/arc\//u.test(e)}function ng(e,t,r,n,i,a){if(n.help===!0)return{help:!0,helpText:Kf(e),kind:"chapter"};if(Ta("digest-dir",n["digest-dir"],i),Ta("depth",n.depth,i),Ta("jsonl",n.jsonl,i),Ta("limit",n.limit,i),Ta("output",n.output,i),Ta("output-format",n["output-format"],i),eg(n,i),n.verbose)throw new Error(h("The chapter command does not support --verbose.",i));let o=e==="set-source"||e==="set-summary"||e==="set-title"?1:0;if(r.length>o)throw new Error(h(`Unexpected positional arguments: ${r.join(" ")}.`,i));return{args:lU(e,t,n,i,a,r[0]),help:!1,kind:"chapter"}}O();T();T();function xU(e,t,r,n,i,a){if(!MI(r))throw new Error(h(`The archive URI form does not support \`${r}\`. Use \`wg <archive-uri> --help\` to inspect valid verbs.`,"wg <archive-uri> --help"));if(r==="get")return ue(r,[e,...n],i,a);if(r!=="create"&&r!=="export"&&r!=="inspect"&&r!=="list"&&r!=="search")throw new Error(h(`The archive URI ${e} cannot be used with \`${r}\`; use a concrete object URI. Use \`wg <archive-uri> --help\` to inspect valid archive verbs.`,"wg <archive-uri> --help"));return ue(r,[r==="create"||r==="export"?t:e,...n],i,a)}function vU(e,t,r,n,i){let a=W(e,r);if(i.help===!0)return{help:!0,helpText:dl("cover"),kind:"maintenance"};if(r!=="get")throw new Error(h(`The cover object does not support \`${r}\`. Read the cover URI directly.`,"wg <cover-uri> --help"));if(Pa("cover",n,0,a),nt("budget",i.budget,a),nt("chapter",i.chapter,a),nt("cursor",i.cursor,a),nt("digest-dir",i["digest-dir"],a),nt("import",i.import,a),nt("input",i.input,a),nt("input-format",i["input-format"],a),nt("limit",i.limit,a),nt("output",i.output,a),nt("output-format",i["output-format"],a),nt("prompt",i.prompt,a),nt("stage",i.stage,a),nt("to",i.to,a),qI("confirm",i.confirm,a),qI("json",i.json,a),yU(i),i.verbose===!0)throw new Error(h("The cover command does not support --verbose.",a));return{args:{inputPath:t,...i.llm===void 0?{}:{llmJSON:i.llm}},help:!1,kind:"cover"}}function CU(e,t,r,n){let i=`wg wikg://<archive.wikg>/index ${t} --help`;if(n.help===!0)return{help:!0,helpText:er("index-object","wikg://<archive.wikg>/index"),kind:"help"};if(!WI(t))throw new Error(h(`The index object does not support \`${t}\`. Read the index object directly, or use enable, disable, embed, or external.`,_.uri));return we(t,r,0,i),tr(t,n,i),g(t,"--after",n.after,i),g(t,"--budget",n.budget,i),g(t,"--before",n.before,i),g(t,"--chapter",n.chapter,i),g(t,"--context",n.context,i),g(t,"--cursor",n.cursor,i),g(t,"--digest-dir",n["digest-dir"],i),g(t,"--evidence",n.evidence,i),g(t,"--from",n.from,i),g(t,"--json-input",n["json-input"],i),g(t,"--limit",n.limit,i),g(t,"--parent",n.parent,i),g(t,"--predicate",n.predicate,i),g(t,"--role",n.role,i),g(t,"--stage",n.stage,i),g(t,"--task",n.task,i),g(t,"--to",n.to,i),A(t,"--accept-cost",n["accept-cost"],i),A(t,"--active",n.active,i),A(t,"--all",n.all,i),A(t,"--backlinks",n.backlinks,i),A(t,"--boost",n.boost,i),A(t,"--clear",n.clear,i),A(t,"--confirm",n.confirm,i),A(t,"--dry-run",n["dry-run"],i),A(t,"--first",n.first,i),t==="enable"?rg(t,n.json,i):A(t,"--jsonl",n.jsonl,i),A(t,"--last",n.last,i),A(t,"--reverse",n.reverse,i),A(t,"--root",n.root,i),A(t,"--verbose",n.verbose,i),Ra(n,t,i),{args:{action:t,archivePath:e,...n.json===void 0?{}:{json:n.json},...n.jsonl===void 0?{}:{jsonl:n.jsonl}},help:!1,kind:"archive-index"}}T();function TU(e,t,r,n,i,a){if(!FI(r))throw new Error(h(`The metadata object does not support \`${r}\`. Read it directly, or use set, put, delete, or clear.`,"wg <object-uri>/meta --help"));switch(wee(r,i,a),r){case"get":case"clear":Pa("meta",n,0,a);break;case"delete":if(Pa("meta",n,1,a),n[0]===void 0)throw new Error(h("Missing metadata key.",a));break;case"put":if(Pa("meta",n,2,a),n[0]===void 0)throw new Error(h("Missing metadata key.",a));break;case"set":Pa("meta",n,1,a);break}return{args:{action:r,archivePath:e,...i.input===void 0?{}:{inputPath:i.input},...r!=="set"&&r!=="put"?{}:n[r==="put"?1:0]===void 0?{}:{inputValue:n[r==="put"?1:0]},...i.json===void 0?{}:{json:i.json},...i["json-input"]===void 0?{}:{jsonInputValue:i["json-input"]},...n[0]===void 0||r!=="put"&&r!=="delete"?{}:{key:n[0]},...i.llm===void 0?{}:{llmJSON:i.llm},objectPath:t},help:!1,kind:"object-metadata"}}function wee(e,t,r){if(ie("budget",t.budget,r),ie("chapter",t.chapter,r),ie("cursor",t.cursor,r),ie("digest-dir",t["digest-dir"],r),ie("import",t.import,r),ie("input-format",t["input-format"],r),ie("limit",t.limit,r),ie("output",t.output,r),ie("output-format",t["output-format"],r),ie("prompt",t.prompt,r),ie("stage",t.stage,r),ie("to",t.to,r),_a("confirm",t.confirm,r),_a("jsonl",t.jsonl,r),eg(t,r),t.verbose===!0)throw new Error(h("The metadata command does not support --verbose.",r));if(e==="get"){ie("input",t.input,r),ie("json-input",t["json-input"],r);return}(e==="clear"||e==="delete")&&(ie("input",t.input,r),ie("json-input",t["json-input"],r))}function zI(e){let t=Lt(e);if(t==="meta")return"";if(t.endsWith("/meta"))return t.slice(0,-5)}function RU(e){return/(?:^|\/)meta\/.+/u.test(Lt(e))}function QI(e,t){let r=e[0],n=e[1];if(r===void 0)throw new Error("Internal error: missing URI-first archive URI.");let i=See(r,n??kee(r,t.query));if(n==="get")throw new Error(Ca(n));if(t.help===!0&&n===void 0){let a=PU(r);return{help:!0,helpText:er(a,r),kind:"help"}}if(t.help===!0&&n!==void 0){let a=PU(r);if(!wi(a,i))throw new Error(h(`The URI target ${r} does not support \`${i}\`.`,W(r)));return{help:!0,helpText:kn(a,i,r),kind:"help"}}if(!pU(i)){let a=W(r);throw new Error(h(`The URI-first form does not support \`${i}\`. Use \`${a}\` to inspect valid predicates.`,a))}return vee(r,i,n===void 0?[]:e.slice(2),t)}function See(e,t){return t!=="apply"?t:B(e).objectUri==="wikg://chapter/tree"?"set":t}function kee(e,t){let r=B(e);if(r.archivePath===void 0)throw new Error(va(e));if(AU(r.objectUri)==="scope")return t===void 0?"list":"search";if(t!==void 0)throw new Error(h("`--query` requires a scope URI, or an explicit `related` or `evidence` command for supported object URIs.",_.uri));return"get"}function AU(e){if(e===void 0)return"scope";let t=Lt(e),r=Tr(e);if(r!==void 0)switch(r.kind){case"collection":case"chapter":case"lens":case"triple-pattern-lens":case"chapter-lens":case"chapter-triple-pattern-lens":return"scope";case"tree":case"chapter-resource":case"chapter-state":return"object"}return sl(t)||/^(?:chunk|entity)$/u.test(t)?"scope":"object"}function PU(e){let r=B(e).objectUri;if(r===void 0)return"archive-scope";let n=Lt(r);if(Eee(e,n),zI(r)!==void 0)return"metadata-object";if(n==="cover")return"cover-object";if(n==="index")return"index-object";let i=Tr(r);if(i!==void 0)switch(i.kind){case"collection":return"chapter-collection-scope";case"chapter":return"chapter-scope";case"tree":return"chapter-tree-object";case"chapter-state":return"chapter-state-object";case"chapter-resource":switch(i.resource){case"source":return _U(e)?"chapter-source-range-object":"chapter-source-object";case"summary":return _U(e)?"chapter-summary-range-object":"chapter-summary-object";case"title":return"chapter-title-object"}break;case"lens":case"chapter-lens":switch(i.lens){case"chunk":return"chunk-scope";case"entity":return"entity-scope";case"source":return"chapter-source-object";case"summary":return"chapter-summary-object";case"triple":return"triple-scope"}break;case"triple-pattern-lens":case"chapter-triple-pattern-lens":return"triple-scope"}if(/^(?:chapter\/[1-9][0-9]*\/)?chunk$/u.test(n))return"chunk-scope";if(/^(?:chapter\/[1-9][0-9]*\/)?chunk\/.+$/u.test(n))return"chunk-object";if(/^(?:chapter\/[1-9][0-9]*\/)?entity$/u.test(n))return"entity-scope";if(/^(?:chapter\/[1-9][0-9]*\/)?entity\/[^/]+\/wikipage$/u.test(n))return"entity-wikipage-object";if(/^(?:chapter\/[1-9][0-9]*\/)?entity\/.+$/u.test(n))return"entity-object";if(sl(n))return"triple-scope";if(/^(?:chapter\/[1-9][0-9]*\/)?triple\/[^/]+\/[^/]+\/[^/]+$/u.test(n))return"triple-object";throw new Error(h(`Unknown Wiki Graph URI target: ${e}. Use the archive root help or URI guide to choose a valid target.`,_.uri))}function Eee(e,t){let r=Iee(e,t);if(r!==void 0)throw new Error(h(`Predicate-like URI suffixes are not object URIs: ${e}. Use predicate form: ${r}`,_.uri))}function Iee(e,t){let r=t.split("/"),n=r.at(-1);if(n!=="evidence"&&n!=="related"&&n!=="pack")return;let i=xee(r.slice(0,-1));if(i[0]==="entity"&&i.length===2)return`${e.slice(0,-n.length-1)} ${n}`;if(i[0]==="chunk"&&i.length===2)return`${e.slice(0,-n.length-1)} ${n}`;if(i[0]==="triple"&&i.length===4)return`${e.slice(0,-n.length-1)} ${n}`}function xee(e){return e[0]==="chapter"&&e[1]!==void 0?e.slice(2):e}function _U(e){return e.includes("#")}function vee(e,t,r,n){let i=B(e),a=Cee(e),o=a===void 0?i.archivePath:Tee(a),s=a?.objectUri??i.objectUri,c=hU(t)?W(e):W(e,t);if(o===void 0)throw new Error(va(e));if(Ree(t,n.reverse,c),s===void 0)return xU(e,o,t,r,n,c);let d=zI(s);if(d!==void 0)return TU(o,d,t,r,n,c);if(RU(s))throw new Error(h("Metadata keys are not addressed in the URI. Read `<object>/meta` and filter the output, or use `<object>/meta put <key> ...`.","wg <object-uri>/meta --help"));if(s==="wikg://cover")return vU(e,o,t,r,n);if(s==="wikg://index")return CU(o,t,r,n);let u=Tr(s);if(u!==void 0)return kU(e,o,u,t,r,n);let p=AU(s);if(p==="object"&&(t==="list"||t==="search"))throw new Error(h(`The object URI ${e} does not support \`${t}\`. Use a scope URI directly, or add --query to a scope URI.`,_.uri));if(p==="scope"&&t==="get")throw new Error(h(`The scope URI ${e} cannot be read as one object. Use a concrete object URI.`,_.uri));if(!mU(t)){let m=W(e);throw new Error(h(`The URI target ${e} does not support \`${t}\`. Use \`${m}\` to inspect valid predicates.`,m))}return ue(t,[e,...r],n,c)}function Cee(e){let t=de(e);if(t?.kind==="archive"&&t.objectUri!==void 0)return t}function Tee(e){if(e.kind!=="archive"||e.archivePublicId===void 0)throw new Error("Internal error: expected library archive object target.");return e.publicId===void 0?`wikg://lib/arc/${e.archivePublicId}`:`wikg://lib/${e.publicId}/arc/${e.archivePublicId}`}function Ree(e,t,r){if(t===!0){if(e==="search")throw new Error(h("`--reverse` cannot be combined with --query.",r));e==="evidence"||e==="get"||e==="list"||e==="related"||A(e,"--reverse",t,r)}}function VI(e,t){let r=e[0],n=iU(r),i=e[1];if(r===void 0)throw new Error("Internal error: missing job URI.");if(fl(i))throw new Error(Ca(i));if(t.help===!0){if(i===void 0)return{help:!0,helpText:er(n===void 0&&Xf(r)===void 0?"job-collection-scope":n===void 0?"job-object":"job-target-object",r),kind:"help"};let d=n===void 0&&Xf(r)===void 0?"job-collection-scope":n===void 0?"job-object":"job-target-object";if(wi(d,i))return{help:!0,helpText:kn(d,i,r),kind:"help"}}let a=n??Xf(r),o=i??(n!==void 0?void 0:a===void 0?"list":"get");if(!fU(o))throw new Error(h(o===void 0?`Missing action after ${r}.`:`The job URI form does not support \`${o}\`.`,"wg wikg://local/job --help"));let s=i===void 0?W(r):W(r,o),c=i===void 0?[]:e.slice(2);if(n!==void 0){if(o!=="set")throw new Error(h(`The job target URI form does not support \`${o}\`. Expected set.`,"wg wikg://local/job/<job-id>/target set --help"));return Pee(n,c,t,s)}switch(o){case"add":if(a!==void 0)throw new Error(h("Job add requires `wikg://local/job`.",s));return _ee(c,t,s);case"clean":if(a!==void 0)throw new Error(h("Job clean requires `wikg://local/job`.",s));return XI("clean",t.json,s),Ms("clean",t.jsonl,s),js("clean",["clean",...c],1,s),{args:{action:"clean"},help:!1,kind:"queue"};case"list":if(a!==void 0)throw new Error(h("Job list requires `wikg://local/job`.",s));return Ms("list",t.jsonl,s),js("list",["list",...c],1,s),{args:{action:"list",...t.active===void 0?{}:{activeOnly:t.active},...t.all===void 0?{}:{all:t.all},...t.input===void 0?{}:{archivePath:sU(t.input,s)},...t.json===void 0?{}:{json:t.json}},help:!1,kind:"queue"};case"get":return LU("status",a,c,t,s);case"watch":case"pause":case"resume":case"cancel":case"boost":return LU(o,a,c,t,s);case"set":throw new Error(h("`wikg://local/job/<job-id> set` is not supported. Use `wikg://local/job/<job-id>/target set <target>`.","wg wikg://local/job/<job-id>/target set --help"))}throw new Error("Internal error: unsupported job URI action.")}function Pee(e,t,r,n){XI("target",r.json,n),Ms("target",r.jsonl,n),js("target",["target",e,...t],3,n);let i=t[0];if(i===void 0)throw new Error(h("Missing build job target.",n));return{args:{action:"target",jobId:e,target:Zf(i)},help:!1,kind:"queue"}}function XI(e,t,r){if(t===!0)throw new Error(h(`\`wg wikg://local/job ${e}\` does not support --json.`,r))}function _ee(e,t,r){if(Ms("add",t.jsonl,r),La("add","--stage",t.stage,r),La("add","--to",t.to,r),La("add","--chapter",t.chapter,r),js("add",["add",...e],1,r),t.input===void 0)throw new Error(h("Missing --input.",r));let i=Aee(t.input,r);return{args:{action:"add",...t["accept-cost"]===void 0?{}:{acceptCost:t["accept-cost"]},archivePath:i.archivePath,...t.boost===void 0?{}:{boost:t.boost},...i.chapterPath===void 0?{}:{chapterPath:i.chapterPath},...t.depth===void 0?{}:{depth:In(t.depth,"--depth",r)},inputPath:t.input,...t.json===void 0?{}:{json:t.json},...t.llm===void 0?{}:{llmJSON:t.llm},...t.prompt===void 0?{}:{prompt:t.prompt},target:Zf(t.task)},help:!1,kind:"queue"}}function Aee(e,t){let r=B(e);if(r.archivePath===void 0)throw new Error(h(`Expected an archive or chapter Wiki Graph URI: ${e}`,t));if(r.objectUri===void 0)return{archivePath:r.archivePath};let n=Tr(r.objectUri);if(n?.kind!=="chapter")throw new Error(h(`Expected an archive or chapter Wiki Graph URI: ${e}`,t));return{archivePath:r.archivePath,chapterPath:n.chapterPath}}function LU(e,t,r,n,i){if(e==="status"?Ms(e,n.jsonl,i):e==="watch"?rg(e,n.json,i):(XI(e,n.json,i),Ms(e,n.jsonl,i)),La(e,"--stage",n.stage,i),La(e,"--to",n.to,i),t===void 0)throw new Error(h(`\`wg wikg://local/job/<job-id> ${e}\` requires <job-id>.`,i));if(js(e,[e,t,...r],2,i),e==="watch"){let a=bU(n.from,i);return{args:{action:e,jobId:t,...a===void 0?{}:{from:a},...n.jsonl===void 0?{}:{jsonl:n.jsonl}},help:!1,kind:"queue"}}return e==="status"?{args:{action:e,jobId:t,...n.json===void 0?{}:{json:n.json}},help:!1,kind:"queue"}:e==="target"?{args:{action:e,jobId:t,target:Zf(n.task)},help:!1,kind:"queue"}:{args:{action:e,jobId:t},help:!1,kind:"queue"}}function Ms(e,t,r){if(t===!0)throw new Error(h(`\`wg wikg://local/job ${e}\` does not support --jsonl.`,r))}function La(e,t,r,n){if(r!==void 0)throw new Error(h(`\`wg wikg://local/job ${e}\` does not support ${t}.`,n))}T();function OU(e,t){let r="wg gc --help";if(t.help===!0)return{help:!0,helpText:DG(),kind:"help"};if(bt("digest-dir",t["digest-dir"],r),bt("import",t.import,r),bt("input",t.input,r),bt("input-format",t["input-format"],r),bt("jsonl",t.jsonl,r),bt("limit",t.limit,r),bt("llm",t.llm,r),bt("output",t.output,r),bt("output-format",t["output-format"],r),bt("prompt",t.prompt,r),bt("stage",t.stage,r),BI(t),t.verbose===!0)throw new Error(h("The `gc` command does not support --verbose.",r));if(e.length>0)throw new Error(h(`Unexpected positional arguments for \`gc\`: ${e.join(" ")}.`,r));return{args:{...t["dry-run"]===void 0?{}:{dryRun:t["dry-run"]},...t.force===void 0?{}:{force:t.force},...t.json===void 0?{}:{json:t.json}},help:!1,kind:"gc"}}T();function NU(e,t){if(wt("digest-dir",t["digest-dir"]),wt("import",t.import),wt("input",t.input),wt("input-format",t["input-format"]),wt("json",t.json),wt("limit",t.limit),wt("llm",t.llm),wt("output",t.output),wt("output-format",t["output-format"]),wt("prompt",t.prompt),wt("stage",t.stage),GI(t),t.verbose)throw new Error(h("The `help` command does not support --verbose.",_.root));if(e.length>1)throw new Error(h(`Unexpected positional arguments: ${e.slice(1).join(" ")}.`,_.root));return e[0]===void 0?{help:!0,helpText:Os(),kind:"help"}:hl(e[0])?{help:!0,helpText:Hf(e[0]),kind:"help"}:{help:!0,helpText:MG(FG(e[0])),kind:"help"}}T();function jU(e,t){let r=e[0];if(t.help===!0&&r===void 0)return{help:!0,helpText:RI(),kind:"help"};if(r==="migrate"){if(t.help===!0)return{help:!0,helpText:RI("migrate"),kind:"help"};Oa("--input",t.input),Oa("--import",t.import),Oa("--input-format",t["input-format"]),Oa("--output-format",t["output-format"]),Oa("--llm",t.llm),Oa("--prompt",t.prompt),YI("--json",t.json),YI("--jsonl",t.jsonl),YI("--verbose",t.verbose);let n=e[1];if(n===void 0)throw new Error(h("Missing legacy input path.","wg legacy migrate --help"));if(e.length>2)throw new Error(h(`Unexpected positional arguments: ${e.slice(2).join(" ")}.`,"wg legacy migrate --help"));return{args:{action:r,inputPath:n,...t.output===void 0?{}:{outputPath:t.output}},help:!1,kind:"legacy"}}throw new Error(h(r===void 0?"Missing legacy command.":`Invalid legacy command: ${r}.`,"wg legacy --help"))}function Oa(e,t){if(t!==void 0)throw new Error(h(`\`wg legacy migrate\` does not support ${e}.`,"wg legacy migrate --help"))}function YI(e,t){t===!0&&Oa(e,t)}T();function DU(e,t){let r=e[0];if(r===void 0)throw new Error("Internal error: missing local config URI.");let n=Vf(r),i=e[1];if(fl(i))throw new Error(Ca(i));let a=i??"get",o=i===void 0?W(r):W(r,a);if(n===void 0){if(t.help===!0&&i===void 0&&Vf(r)===void 0)return{help:!0,helpText:er("local-config-namespace",r),kind:"help"};throw new Error(h("Expected a local config section URI such as wikg://local/config/llm.",_.config))}if(t.help===!0&&i===void 0)return{help:!0,helpText:er("local-config-section",r),kind:"help"};if(t.help===!0&&i!==void 0){if(!wi("local-config-section",a))throw new Error(h(`The URI target ${r} does not support \`${a}\`.`,W(r)));return{help:!0,helpText:kn("local-config-section",a,r),kind:"help"}}if(!Nee(a))throw new Error(h(`The local config URI form does not support \`${a}\`. Pass the URI directly to read it, or use set, put, delete, clear, or test.`,o));return Lee(n,a,i===void 0?[]:e.slice(2),t,o)}function Lee(e,t,r,n,i){switch(Oee(t,n,i),t){case"get":case"clear":case"test":we(t,r,0,i);break;case"delete":if(we(t,r,1,i),r[0]===void 0)throw new Error(h("Missing config key.",i));break;case"put":if(we(t,r,n.secret===!0?1:2,i),r[0]===void 0)throw new Error(h("Missing config key.",i));if(n.secret!==!0&&r[1]===void 0&&n["json-input"]===void 0)throw new Error(h("Missing config value.",i));break;case"set":we(t,r,1,i);break}return{args:{action:t,...r[0]===void 0||t!=="put"&&t!=="delete"?{}:{key:r[0]},...n.json===void 0?{}:{json:n.json},...n["json-input"]===void 0?{}:{jsonInputValue:n["json-input"]},...t!=="set"&&t!=="put"?{}:r[t==="put"?1:0]===void 0?{}:{inputValue:r[t==="put"?1:0]},section:e,...n.secret===void 0?{}:{secret:n.secret}},help:!1,kind:"local-config"}}function Oee(e,t,r){if(ie("input",t.input,r),ie("llm",t.llm,r),ie("output",t.output,r),ie("prompt",t.prompt,r),ie("task",t.task,r),_a("jsonl",t.jsonl,r),_a("verbose",t.verbose,r),Ra(t,e,r),t.secret===!0&&e!=="put")throw new Error(h("`--secret` is only valid for config put.",r));if(!(e==="get"||e==="test")&&t.json===!0&&e!=="set"&&e!=="put")throw new Error(h(`\`${e}\` does not support --json.`,r))}function Nee(e){return e==="clear"||e==="delete"||e==="get"||e==="put"||e==="set"||e==="test"}T();function WU(e,t){let r=e[0];if(t.help===!0&&r===void 0)return{help:!0,helpText:PI(),kind:"help"};if(r!=="upgrade")throw new Error(h(r===void 0?"Missing maintenance command.":`Invalid maintenance command: ${r}.`,"wg maintenance --help"));if(t.help===!0)return{help:!0,helpText:PI("upgrade"),kind:"help"};Na("--input",t.input),Na("--import",t.import),Na("--input-format",t["input-format"]),Na("--output-format",t["output-format"]),Na("--llm",t.llm),Na("--prompt",t.prompt),MU("--jsonl",t.jsonl),MU("--verbose",t.verbose);let n=e[1];if(n===void 0)throw new Error(h("Missing maintenance upgrade target.","wg maintenance upgrade --help"));if(e.length>2)throw new Error(h(`Unexpected positional arguments: ${e.slice(2).join(" ")}.`,"wg maintenance upgrade --help"));return{args:{action:r,...t.json===void 0?{}:{json:t.json},...t.output===void 0?{}:{outputPath:t.output},target:n},help:!1,kind:"maintenance-command"}}function Na(e,t){if(t!==void 0)throw new Error(h(`\`wg maintenance upgrade\` does not support ${e}.`,"wg maintenance upgrade --help"))}function MU(e,t){t===!0&&Na(e,t)}T();function FU(e,t,r){if(r.help!==!0)throw new Error(h(`Unknown command: ${e}.`,_.command));switch(e){case"cover":case"meta":if(t.length>0)throw new Error(h(`Unexpected positional arguments: ${t.join(" ")}.`,_.command));return{help:!0,helpText:dl(e),kind:"maintenance"};case"chapter":{let n=t[0];if(n===void 0)return{help:!0,helpText:dl("chapter"),kind:"chapter"};if(!ul(n))throw new Error(h(`Invalid chapter action: ${n}. Use the chapter collection URI directly, add --query, or use add, move, remove, reset, or tree. Use concrete chapter resource URIs such as /source, /summary, or /title for set operations.`,_.command));if(n==="set-source"||n==="set-summary"||n==="set-title")throw new Error(h(`Invalid chapter action: ${n}. Use concrete chapter resource URIs such as /source, /summary, or /title for set operations.`,_.command));if(t.length>1)throw new Error(h(`Unexpected positional arguments: ${t.slice(1).join(" ")}.`,_.command));return{help:!0,helpText:Kf(n),kind:"chapter"}}}}T();function GU(e,t){let r="wg transform --help";if(e.length>0)throw new Error(h(`Unexpected positional arguments: ${e.join(" ")}.`,r));HI(t),Ot("budget",t.budget,r),Ot("chapter",t.chapter,r),Ot("confirm",t.confirm,r),Ot("cursor",t.cursor,r),Ot("evidence",t.evidence,r),Ot("json",t.json,r),Ot("import",t.import,r),Ot("limit",t.limit,r),Ot("parent",t.parent,r),Ot("to",t.to,r);let n={...t["digest-dir"]===void 0?{}:{digestDirPath:t["digest-dir"]},help:t.help??!1,...t.input===void 0?{}:{inputPath:t.input},...t["input-format"]===void 0?{}:{inputFormat:As(t["input-format"],"--input-format")},...t.llm===void 0?{}:{llmJSON:t.llm},...t.output===void 0?{}:{outputPath:t.output},...t["output-format"]===void 0?{}:{outputFormat:As(t["output-format"],"--output-format")},...t.prompt===void 0?{}:{prompt:t.prompt},...t.stage===void 0?{}:{targetStage:DI(t.stage,"--stage",r)},verbose:t.verbose??!1};return t.help??!1?{args:n,help:!0,helpText:jG(),kind:"convert"}:{args:n,help:!1,kind:"convert"}}O();T();var $U=new Set(["get","remove"]),BU=new Set(["add","get","scan"]),jee=new Set(["get","set"]),HU=new Set(["archive-tree"]),qU=new Set(["clear","delete","get","put","set"]),KU=new Set(["get","set"]),JU=new Set(["add","list"]),zU=new Set(["get","remove"]),QU=new Set(["disable","enable","get"]),Dee=new Set(["evidence","get","list","pack","related","search"]);function VU(e,t){let r=e[0],n=e[1];if(r===void 0)throw new Error("Internal error: missing library URI.");let i=de(r);if(i===void 0)throw new Error(`Expected a Wiki Graph library URI: ${r}`);let a=n??(i.kind==="scope"&&(i.objectUri!==void 0&&i.objectUri!=="wikg://index"||t.query!==void 0)?qee(i.objectUri,t.query):i.kind==="scope"||i.kind==="archive-collection"||i.kind==="metadata"||i.kind==="path"||i.kind==="archive"||i.kind==="archive-path"||i.objectUri==="wikg://index"?"get":i.kind==="archive-tree"?"archive-tree":"list");if(a==="inspect"&&i.kind!=="archive")throw new Error(h("Library-level inspection is not supported. Inspect one managed archive with `wg wikg://lib/arc/<archive-id> inspect`.",W(r)));if(t.help===!0)return Mee(r,i,a,n);if(i.kind==="archive"&&a==="inspect")return Zee(r,n===void 0?[]:e.slice(2),t);if(i.kind==="archive"&&i.objectUri===void 0&&(a==="search"||t.query!==void 0))return ete(r,n===void 0?[]:e.slice(2),t);if(i.kind==="registry")return Kee(r,i,a,n===void 0?[]:e.slice(2),t);if(i.kind==="path"||i.kind==="archive-path")return Jee(r,i,a,n===void 0?[]:e.slice(2),t);if(i.kind==="archive-tree")return Vee(r,i,a,n===void 0?[]:e.slice(2),t);if(i.kind==="archive-collection")return Qee(r,i,a,n===void 0?[]:e.slice(2),t);if(i.kind==="scope"&&i.objectUri==="wikg://index")return Xee(r,i,a,n===void 0?[]:e.slice(2),t);if(i.kind==="scope"&&(i.objectUri!==void 0||a==="search"))return Gee(r,i,a,n===void 0?[]:e.slice(2),t);if(!ig(a))throw new Error(h(`The library URI target ${r} does not support \`${a}\`.`,W(r)));return ZI(r,i,a),i.kind==="archive"?Yee(r,i,a,n===void 0?[]:e.slice(2),t):i.kind==="metadata"?rte(r,i,a,n===void 0?[]:e.slice(2),t):tte(r,i,a,n===void 0?[]:e.slice(2),t)}function Mee(e,t,r,n){if(t.kind==="archive"&&r==="inspect")return{help:!0,helpText:kn("archive-scope","inspect",e),kind:"help"};if(t.kind==="scope"&&t.objectUri===void 0&&r==="search"&&n===void 0)return{help:!0,helpText:cl(e,t),kind:"help"};if(t.kind==="registry"||t.kind==="path"||t.kind==="archive-collection"||t.kind==="archive-tree"||t.kind==="archive-path"){if(n!==void 0&&!ig(r))throw new Error(h(`The library URI target ${e} does not support \`${r}\`.`,W(e)));return ig(r)&&ZI(e,t,r),{help:!0,helpText:n===void 0?cl(e,t):qf(e,t,r),kind:"help"}}if(t.kind==="scope"&&t.objectUri==="wikg://index"){if(n===void 0)return{help:!0,helpText:cl(e,t),kind:"help"};if(!QU.has(r))throw new Error(h(`The library index ${e} does not support \`${r}\`.`,W(e)));return{help:!0,helpText:qf(e,t,r),kind:"help"}}if(t.kind==="scope"&&t.objectUri!==void 0){let i=Wee(t.objectUri);if(n===void 0)return{help:!0,helpText:er(i,e),kind:"help"};if(Fee(i))throw new Error(h(`The library-wide chapter target ${e} is read-only and does not support \`${r}\`. Use a standalone archive URI or a library archive shortcut such as wikg://lib/arc/<archive-id>/chapter/... for chapter maintenance.`,W(e)));if(!wi(i,r))throw new Error(h(`The library URI target ${e} does not support \`${r}\`.`,W(e)));return{help:!0,helpText:kn(i,r,e),kind:"help"}}if(!ig(r))throw new Error(h(`The library URI target ${e} does not support \`${r}\`.`,W(e)));return ZI(e,t,r),{help:!0,helpText:n===void 0?cl(e,t):qf(e,t,r),kind:"help"}}function Wee(e){let t=Lt(e),r=Tr(e);if(r!==void 0)switch(r.kind){case"collection":return"chapter-collection-scope";case"chapter":return"chapter-scope";case"tree":return"chapter-tree-object";case"chapter-state":return"chapter-state-object";case"chapter-resource":switch(r.resource){case"source":return UU(e)?"chapter-source-range-object":"chapter-source-object";case"summary":return UU(e)?"chapter-summary-range-object":"chapter-summary-object";case"title":return"chapter-title-object"}break;case"lens":case"chapter-lens":switch(r.lens){case"chunk":return"chunk-scope";case"entity":return"entity-scope";case"source":return"chapter-source-object";case"summary":return"chapter-summary-object";case"triple":return"triple-scope"}break;case"triple-pattern-lens":case"chapter-triple-pattern-lens":return"triple-scope"}return t==="chunk"?"chunk-scope":/^chunk\/.+$/u.test(t)?"chunk-object":t==="entity"?"entity-scope":/^entity\/[^/]+\/wikipage$/u.test(t)?"entity-wikipage-object":/^entity\/.+$/u.test(t)?"entity-object":sl(t)?"triple-scope":/^triple(?:\/.*)?$/u.test(t)?"triple-object":"archive-scope"}function UU(e){return e.includes("#")}function Fee(e){return e==="chapter-collection-scope"||e==="chapter-scope"||e==="chapter-source-object"||e==="chapter-summary-object"||e==="chapter-title-object"||e==="chapter-tree-object"}function Gee(e,t,r,n,i){if(!Dee.has(r))throw new Error(h(`The library scope ${e} does not support \`${r}\`.`,W(e,r)));if(t.objectUri===void 0&&r!=="search"&&r!=="list")throw new Error("Internal error: missing library query object URI.");let a=ue(r,[t.objectUri===void 0?"wikg://__library_index__.wikg":Bee(t.objectUri),...n],i,W(e,r),{...Uee(Hee(t.objectUri))});if(a.kind!=="archive")return a;let o=a.args.archivePath,s=c=>c===void 0?void 0:c===o?e:c.replace(o,e);return{...a,args:{...a.args,archivePath:e,...$ee(s(a.args.objectId))}}}function Uee(e){return e===void 0?{}:{defaultKinds:e}}function $ee(e){return e===void 0?{}:{objectId:e}}function Bee(e){return`wikg://__library_index__.wikg/${Lt(e)}`}function Hee(e){if(e===void 0)return;let t=Lt(e),[r]=t.split("/");switch(r){case"chapter":return["chapter"];case"chunk":return["chunk"];case"entity":return["entity"];case"source":return["source"];case"summary":return["summary"];case"triple":return["triple"];default:return}}function qee(e,t){if(e===void 0)return t===void 0?"list":"search";let r=Lt(e);return/^(?:chapter|chunk|entity|source|summary|triple)$/u.test(r)?t===void 0?"list":"search":"get"}function Kee(e,t,r,n,i){let a=W(e,r);if(!JU.has(r))throw new Error(h(`The library registry ${e} does not support \`${r}\`.`,a));if(St(r,n,0,a),ki(r,i,a),g(r,"--input",i.input,a),g(r,"--to",i.to,a),g(r,"--json-input",i["json-input"],a),A(r,"--jsonl",i.jsonl,a),r==="add"){if(i.path===void 0)throw new Error(h("Missing --path <folder>.",a));return{args:{action:"create",json:i.json,path:i.path,target:t},help:!1,kind:"library"}}return g(r,"--path",i.path,a),{args:{action:"list",json:i.json,target:t},help:!1,kind:"library"}}function Jee(e,t,r,n,i){let a=W(e,r);if(!KU.has(r))throw new Error(h(`The library path object ${e} does not support \`${r}\`.`,a));if(ki(r,i,a),g(r,"--path",i.path,a),g(r,"--input",i.input,a),g(r,"--to",i.to,a),g(r,"--json-input",i["json-input"],a),r==="get")return St(r,n,0,a),A(r,"--jsonl",i.jsonl,a),{args:{action:"get",json:i.json,target:t},help:!1,kind:"library"};if(St(r,n,1,a),n[0]===void 0)throw new Error(h("Missing path value.",a));return t.kind==="archive-path"?(A(r,"--jsonl",i.jsonl,a),{args:{action:"set",json:i.json,target:t,to:n[0]},help:!1,kind:"library"}):{args:{action:"rebind",json:i.json,jsonl:i.jsonl,path:n[0],target:zee(t)},help:!1,kind:"library"}}function zee(e){if(e.kind!=="path")throw new Error("Internal error: expected library path target.");return{isDefault:e.isDefault,kind:"scope",...e.publicId===void 0?{}:{publicId:e.publicId}}}function Qee(e,t,r,n,i){let a=W(e,r);if(!BU.has(r))throw new Error(h(`The archive member collection ${e} does not support \`${r}\`.`,a));if(St(r,n,0,a),ki(r,i,a),g(r,"--path",i.path,a),g(r,"--json-input",i["json-input"],a),r==="add"){if(A(r,"--jsonl",i.jsonl,a),i.input===void 0)throw new Error(h("Missing --input <path>.",a));return{args:{action:"add",inputPath:i.input,json:i.json,target:t,...i.to===void 0?{}:{to:i.to}},help:!1,kind:"library"}}return g(r,"--input",i.input,a),g(r,"--to",i.to,a),{args:{action:r==="get"?"get":"scan",json:i.json,jsonl:i.jsonl,target:t},help:!1,kind:"library"}}function Vee(e,t,r,n,i){let a=W(e,r);if(!HU.has(r))throw new Error(h(`The archive member tree ${e} is read-only and does not support \`${r}\`.`,a));return St(r,n,0,a),ki(r,i,a),g(r,"--path",i.path,a),g(r,"--input",i.input,a),g(r,"--to",i.to,a),g(r,"--json-input",i["json-input"],a),A(r,"--jsonl",i.jsonl,a),{args:{action:"archive-tree",...i.depth===void 0?{}:{depth:In(i.depth,"--depth",a)},json:i.json,...i.parent===void 0?{}:{parent:i.parent},target:t},help:!1,kind:"library"}}function Xee(e,t,r,n,i){let a=W(e,r);if(!QU.has(r))throw new Error(h(`The library index ${e} does not support \`${r}\`.`,a));if(St(r,n,0,a),ki(r,i,a),g(r,"--path",i.path,a),g(r,"--input",i.input,a),g(r,"--json-input",i["json-input"],a),g(r,"--to",i.to,a),r==="enable"){if(i.json===!0)throw new Error(h("The `enable` command does not support --json because it streams progress events. Use --jsonl for line-delimited progress output.",a));return{args:{action:"enable-index",jsonl:i.jsonl,target:t},help:!1,kind:"library"}}return A(r,"--jsonl",i.jsonl,a),{args:{action:r==="disable"?"disable-index":"get-index",...i.json===void 0?{}:{json:i.json},target:t},help:!1,kind:"library"}}function Yee(e,t,r,n,i){let a=W(e,r);if(!$U.has(r))throw new Error(h(`The library archive ${e} does not support \`${r}\`.`,a));if(ki(r,i,a,{allowConfirm:r==="remove"}),g(r,"--path",i.path,a),g(r,"--input",i.input,a),g(r,"--json-input",i["json-input"],a),A(r,"--jsonl",i.jsonl,a),St(r,n,0,a),r==="get")return g(r,"--to",i.to,a),A(r,"--confirm",i.confirm,a),{args:{action:r,json:i.json,target:t},help:!1,kind:"library"};if(r==="remove"){if(i.confirm!==!0)throw new Error(h("Missing --confirm.",a));return g(r,"--to",i.to,a),{args:{action:r,confirm:!0,json:i.json,target:t},help:!1,kind:"library"}}throw new Error("Internal error: unsupported action routed to library archive.")}function Zee(e,t,r){return ue("inspect",[e,...t],r,W(e,"inspect"))}function ete(e,t,r){return ue("search",[e,...t],r,W(e,"search"))}function tte(e,t,r,n,i){let a=W(e,r);if(!zU.has(r))throw new Error(h(`The library scope ${e} does not support \`${r}\`.`,a));switch(ki(r,i,a,{allowConfirm:r==="remove"}),A(r,"--jsonl",i.jsonl,a),g(r,"--json-input",i["json-input"],a),r){case"remove":if(St(r,n,0,a),g(r,"--path",i.path,a),g(r,"--input",i.input,a),g(r,"--to",i.to,a),!t.isDefault&&i.confirm!==!0)throw new Error(h("Missing --confirm.",a));return{args:{action:r,...i.confirm===void 0?{}:{confirm:i.confirm},json:i.json,target:t},help:!1,kind:"library"};case"get":case"list":return St(r,n,0,a),g(r,"--path",i.path,a),g(r,"--input",i.input,a),g(r,"--to",i.to,a),{args:{action:r,json:i.json,target:t},help:!1,kind:"library"};case"set":case"put":case"delete":case"disable-index":case"enable-index":case"get-index":case"clear":case"archive-tree":case"add":case"create":case"rebind":case"scan":throw new Error("Internal error: unsupported action routed to library scope.")}}function rte(e,t,r,n,i){let a=W(e,r);if(!qU.has(r))throw new Error(h(`The library metadata object ${e} does not support \`${r}\`.`,a));switch(ki(r,i,a),g(r,"--path",i.path,a),A(r,"--jsonl",i.jsonl,a),r){case"get":case"clear":return St(r,n,0,a),{args:{action:r,json:i.json,target:t},help:!1,kind:"library"};case"set":return St(r,n,1,a),{args:{action:r,inputPath:i.input,inputValue:n[0],json:i.json,jsonInputValue:i["json-input"],target:t},help:!1,kind:"library"};case"put":return St(r,n,2,a),{args:{action:r,inputPath:i.input,inputValue:n[1],json:i.json,jsonInputValue:i["json-input"],key:n[0],target:t},help:!1,kind:"library"};case"delete":return St(r,n,1,a),{args:{action:r,json:i.json,key:n[0],target:t},help:!1,kind:"library"};case"create":case"list":case"remove":case"add":case"rebind":case"disable-index":case"enable-index":case"get-index":case"scan":case"archive-tree":throw new Error("Internal error: scope action routed to library metadata.")}}function ki(e,t,r,n={}){g(e,"--query",t.query,r),g(e,"--limit",t.limit,r),g(e,"--cursor",t.cursor,r),g(e,"--context",t.context,r),g(e,"--evidence",t.evidence,r),g(e,"--llm",t.llm,r),g(e,"--output",t.output,r),g(e,"--output-format",t["output-format"],r),A(e,"--all",t.all,r),A(e,"--backlinks",t.backlinks,r),A(e,"--reverse",t.reverse,r),n.allowConfirm!==!0&&A(e,"--confirm",t.confirm,r)}function St(e,t,r,n){if(t.length>r)throw new Error(h(`The \`${e}\` command received too many positional arguments.`,n))}function ig(e){return e==="add"||e==="archive-tree"||e==="clear"||e==="create"||e==="delete"||e==="disable-index"||e==="enable-index"||e==="get"||e==="get-index"||e==="list"||e==="put"||e==="rebind"||e==="remove"||e==="scan"||e==="set"}function bl(e,t,r,n){if(!r.has(n))throw new Error(h(`The ${t} ${e} does not support \`${n}\`.`,W(e)))}function ZI(e,t,r){let n=W(e);if(t.kind==="metadata"){if(!qU.has(r))throw new Error(h(`The library metadata object ${e} does not support \`${r}\`.`,n));return}if(t.kind==="archive"){if(!$U.has(r))throw new Error(h(`The library archive ${e} does not support \`${r}\`.`,n));return}if(t.kind==="registry"){bl(e,"library registry",JU,r);return}if(t.kind==="path"){bl(e,"library path object",KU,r);return}if(t.kind==="archive-collection"){bl(e,"archive member collection",BU,r);return}if(t.kind==="archive-tree"){bl(e,"archive member tree",HU,r);return}if(t.kind==="archive-path"){bl(e,"archive path object",jee,r);return}if(!zU.has(r))throw new Error(h(`The library scope ${e} does not support \`${r}\`.`,n));if(r==="create"&&!t.isDefault)throw new Error(h("Create libraries from wikg://lib.",n));if(r==="remove"&&t.isDefault)throw new Error(h("The default library cannot be removed. Use a non-default library URI such as wikg://lib/<lib-id> for library registry removal.",n))}function ex(e=process.argv.slice(2)){let{positionals:t,values:r}=nte({allowPositionals:!0,args:KI(e),options:{author:{multiple:!0,type:"string"},after:{type:"string"},before:{type:"string"},backlinks:{type:"boolean"},budget:{type:"string"},clear:{type:"boolean"},"clear-authors":{type:"boolean"},"clear-description":{type:"boolean"},"clear-identifier":{type:"boolean"},"clear-language":{type:"boolean"},"clear-published-at":{type:"boolean"},"clear-publisher":{type:"boolean"},"clear-title":{type:"boolean"},description:{type:"string"},help:{short:"h",type:"boolean"},"digest-dir":{type:"string"},depth:{type:"string"},"dry-run":{type:"boolean"},active:{type:"boolean"},"accept-cost":{type:"boolean"},all:{type:"boolean"},boost:{type:"boolean"},first:{type:"boolean"},identifier:{type:"string"},from:{type:"string"},force:{type:"boolean"},input:{type:"string"},import:{type:"string"},"input-format":{type:"string"},limit:{type:"string"},language:{type:"string"},json:{type:"boolean"},"json-input":{type:"string"},jsonl:{type:"boolean"},llm:{type:"string"},output:{type:"string"},path:{type:"string"},"output-format":{type:"string"},prompt:{type:"string"},query:{type:"string"},replace:{type:"boolean"},reverse:{type:"boolean"},stage:{type:"string"},task:{type:"string"},chapter:{type:"string"},confirm:{type:"boolean"},context:{type:"string"},cursor:{type:"string"},evidence:{type:"string"},parent:{type:"string"},"published-at":{type:"string"},publisher:{type:"string"},recursive:{type:"boolean"},secret:{type:"boolean"},role:{type:"string"},root:{type:"boolean"},last:{type:"boolean"},title:{type:"string"},to:{type:"string"},verbose:{short:"v",type:"boolean"},version:{type:"boolean"}},strict:!0});if(UI(t,r),$I(t,r),r.version===!0)return{help:!1,kind:"version"};if(r.help===!0&&t.length===0)return{help:!0,helpText:Os(),kind:"help"};if(r["accept-cost"]===!0&&!(Qf(t[0])&&t[1]==="add"))throw new Error(h("`--accept-cost` is only valid for `wg wikg://local/job add`.","wg wikg://local/job add --help"));if(r.reverse===!0&&(t[0]===void 0||!Rr(t[0])&&t[0]!=="next"))throw new Error("The current command does not support --reverse.");if(t[0]==="help")return NU(t.slice(1),r);if(t[0]==="gc")return OU(t.slice(1),r);if(t[0]==="legacy")return jU(t.slice(1),r);if(t[0]==="maintenance")return WU(t.slice(1),r);if(t[0]==="transform")return GU(t.slice(1),r);if(t[0]==="next")return ue("next",t.slice(1),r);if(AI(t[0])&&r.help===!0&&t.length<=2)return FU(t[0],t.slice(1),r);if(Qf(t[0]))return A(t[1]??"job","--reverse",r.reverse,"wg wikg://local/job --help"),VI(t,r);if(pl(t[0]))return A(t[1]??"config","--reverse",r.reverse,"wg wikg://local/config --help"),DU(t,r);if(ab(t[0]))return VU(t,r);if(Rr(t[0]))return QI(t,r);if(hl(t[0])&&r.help===!0&&t.length===1)return ue(t[0],t.slice(1),r);throw t.length===0?new Error(h("Missing command.",_.command)):new Error(jI(t[0]))}async function X$(e){return(await Promise.resolve().then(()=>(Z0(),Y0))).runArchiveCommand(e)}async function Y$(e){return(await Promise.resolve().then(()=>(a$(),i$))).runArchiveChapterCommand(e)}async function Z$(e){return(await Promise.resolve().then(()=>(d$(),c$))).runArchiveIndexCommand(e)}async function eB(e){return(await Promise.resolve().then(()=>(Dx(),jx))).runArchiveCoverCommand(e)}async function tB(e){return(await Promise.resolve().then(()=>(Dx(),jx))).runArchiveMetaCommand(e)}async function rB(e){return(await Promise.resolve().then(()=>(ug(),c0))).runConvertCommand(e)}async function nB(e){return(await Promise.resolve().then(()=>(h$(),m$))).runGcCommand(e)}async function iB(e){return(await Promise.resolve().then(()=>(g$(),f$))).runLegacyCommand(e)}async function aB(e){return(await Promise.resolve().then(()=>(b$(),y$))).runMaintenanceCommand(e)}async function oB(e){return(await Promise.resolve().then(()=>(I$(),E$))).runLocalConfigCommand(e)}async function sB(e){return(await Promise.resolve().then(()=>(O$(),L$))).runLibraryCommand(e)}async function cB(e){return(await Promise.resolve().then(()=>(M$(),D$))).runObjectMetadataCommand(e)}async function dB(e){return(await Promise.resolve().then(()=>(V$(),Q$))).runQueueCommand(e)}T();T();O();async function lB(e){try{if(wie(e.argv,e.stdinIsTTY))return e.stdout.write(`${Os()}
|
|
2981
|
+
`),{exitCode:0};let t=ex([...e.argv]);if(t.help)return e.stdout.write(`${t.helpText}
|
|
2982
|
+
`),{exitCode:0};if(t.kind==="version")return e.stdout.write(`${CG()}
|
|
2983
|
+
`),{exitCode:0};switch(await ke(),t.kind){case"convert":return await rB(t.args),{exitCode:0};case"meta":return await tB(t.args),{exitCode:0};case"cover":return await eB(t.args),{exitCode:0};case"object-metadata":return await cB(t.args),{exitCode:0};case"library":return await sB(t.args),{exitCode:0};case"chapter":return await Y$(t.args),{exitCode:0};case"archive":return await X$(t.args),{exitCode:0};case"archive-index":return await Z$(t.args),{exitCode:0};case"queue":return await dB(t.args),{exitCode:0};case"gc":return await nB(t.args),{exitCode:0};case"legacy":return await iB(t.args),{exitCode:0};case"maintenance-command":return await aB(t.args),{exitCode:0};case"local-config":return await oB(t.args),{exitCode:0}}}catch(t){return Sie(e.argv)?(e.stdout.write(L(uB(t))),{exitCode:1}):kie(e.argv)?(e.stdout.write(Ls(uB(t))),{exitCode:1}):(e.stderr.write(`${pB(t)}
|
|
2984
|
+
`),{exitCode:1})}}function wie(e,t){return e.length===0&&t===!0}function pB(e){return e instanceof jr?"LLM payment required. Check your provider billing status or account balance.":Ce(e)}function uB(e){return{error:{message:pB(e),type:e instanceof jr?"llm_payment_required":"error"}}}function Sie(e){return e.some(t=>t==="--json"||t.startsWith("--json="))}function kie(e){return e.includes("--jsonl")}yi();var Eie=["WIKIGRAPH_DEV","WIKIGRAPH_ENV_POLICY","WIKIGRAPH_QUEUE_DISABLE_AUTOSTART","WIKIGRAPH_STATE_DIR"];function hB(e){let t=Iie(e.env);if(e.envPolicy==="production"){let a=Eie.filter(o=>t[o]!==void 0);if(a.length>0)throw new Error(`Wiki Graph production entries do not support runtime env overrides: ${a.join(", ")}. Use the development entry or explicit SDK options instead.`);return{env:t,envPolicy:e.envPolicy,stateDir:e.stateDir}}let r=t.WIKIGRAPH_DEV??t.WIKIGRAPH_STATE_DIR,n=mB(e.stateDir??r);if(n===void 0)throw new Error("Wiki Graph development entries require an explicit runtime state directory.");let i=mB(e.devProjectRoot);if(i===void 0)throw new Error("Wiki Graph development entries require an explicit project root.");return{devProjectRoot:i,env:t,envPolicy:e.envPolicy,stateDir:n}}function Iie(e){return e===void 0?{...process.env}:{...process.env,...e}}function mB(e){if(!(e===void 0||e.trim()===""))return e}async function gB(e={},t){fB(e.signal);let r=vie(e.stdin??process.stdin),n=e.stdout??process.stdout,i=e.stderr??process.stderr,a=e.argv??process.argv.slice(2),o;try{o=hB({argv:a,cwd:e.cwd??process.cwd(),devProjectRoot:e.devProjectRoot,env:e.env,envPolicy:t,stateDir:e.stateDir})}catch(c){return i.write(`${c instanceof Error?c.message:String(c)}
|
|
2985
|
+
`),{exitCode:1}}let s={argv:a,cwd:e.cwd??process.cwd(),devProjectRoot:o.devProjectRoot,env:o.env,envPolicy:o.envPolicy,exitCode:0,queueAutostart:!0,signal:e.signal,stateDir:o.stateDir,stderr:i,stderrIsTTY:e.stderrIsTTY,stdin:r,stdinIsTTY:e.stdinIsTTY,stdout:n,stdoutIsTTY:e.stdoutIsTTY};return await ey(o.stateDir,async()=>aG(s,async()=>{let c=await lB({argv:a,stderr:i,stdinIsTTY:s.stdinIsTTY??r.isTTY,stdout:n});return fB(e.signal),{exitCode:Cie(mG(),c.exitCode)}}))}function vie(e){return typeof e=="string"||e instanceof Uint8Array?xie.from([e]):e}function Cie(e,t){if(e===void 0||e===0)return t;let r=Number(e);return Number.isFinite(r)?r:1}function fB(e){if(e?.aborted===!0)throw e.reason instanceof Error?e.reason:new Error("The Wiki Graph CLI run was aborted.")}async function Vx(e={},t="production"){let r=await gB(e,t);process.exitCode=r.exitCode}Vx();
|
|
2986
|
+
//# sourceMappingURL=cli.js.map
|